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 1053feb2d49STrond Myklebust memset(p, 0, sizeof(*p)); 106fbe77c30SBenjamin Coddington p->rw_mode = FMODE_WRITE; 1073feb2d49STrond Myklebust return p; 1083feb2d49STrond Myklebust } 1093feb2d49STrond Myklebust 1101e7f3a48SWeston Andros Adamson static void nfs_writehdr_free(struct nfs_pgio_header *hdr) 1116c75dc0dSFred Isaman { 1121e7f3a48SWeston Andros Adamson mempool_free(hdr, nfs_wdata_mempool); 1133feb2d49STrond Myklebust } 1141da177e4SLinus Torvalds 115919e3bd9STrond Myklebust static struct nfs_io_completion *nfs_io_completion_alloc(gfp_t gfp_flags) 116919e3bd9STrond Myklebust { 117919e3bd9STrond Myklebust return kmalloc(sizeof(struct nfs_io_completion), gfp_flags); 118919e3bd9STrond Myklebust } 119919e3bd9STrond Myklebust 120919e3bd9STrond Myklebust static void nfs_io_completion_init(struct nfs_io_completion *ioc, 121919e3bd9STrond Myklebust void (*complete)(void *), void *data) 122919e3bd9STrond Myklebust { 123919e3bd9STrond Myklebust ioc->complete = complete; 124919e3bd9STrond Myklebust ioc->data = data; 125919e3bd9STrond Myklebust kref_init(&ioc->refcount); 126919e3bd9STrond Myklebust } 127919e3bd9STrond Myklebust 128919e3bd9STrond Myklebust static void nfs_io_completion_release(struct kref *kref) 129919e3bd9STrond Myklebust { 130919e3bd9STrond Myklebust struct nfs_io_completion *ioc = container_of(kref, 131919e3bd9STrond Myklebust struct nfs_io_completion, refcount); 132919e3bd9STrond Myklebust ioc->complete(ioc->data); 133919e3bd9STrond Myklebust kfree(ioc); 134919e3bd9STrond Myklebust } 135919e3bd9STrond Myklebust 136919e3bd9STrond Myklebust static void nfs_io_completion_get(struct nfs_io_completion *ioc) 137919e3bd9STrond Myklebust { 138919e3bd9STrond Myklebust if (ioc != NULL) 139919e3bd9STrond Myklebust kref_get(&ioc->refcount); 140919e3bd9STrond Myklebust } 141919e3bd9STrond Myklebust 142919e3bd9STrond Myklebust static void nfs_io_completion_put(struct nfs_io_completion *ioc) 143919e3bd9STrond Myklebust { 144919e3bd9STrond Myklebust if (ioc != NULL) 145919e3bd9STrond Myklebust kref_put(&ioc->refcount, nfs_io_completion_release); 146919e3bd9STrond Myklebust } 147919e3bd9STrond Myklebust 1487b159fc1STrond Myklebust static void nfs_context_set_write_error(struct nfs_open_context *ctx, int error) 1497b159fc1STrond Myklebust { 1507b159fc1STrond Myklebust ctx->error = error; 1517b159fc1STrond Myklebust smp_wmb(); 1527b159fc1STrond Myklebust set_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags); 1537b159fc1STrond Myklebust } 1547b159fc1STrond Myklebust 155bd37d6fcSTrond Myklebust static struct nfs_page * 156bd37d6fcSTrond Myklebust nfs_page_private_request(struct page *page) 157bd37d6fcSTrond Myklebust { 158bd37d6fcSTrond Myklebust if (!PagePrivate(page)) 159bd37d6fcSTrond Myklebust return NULL; 160bd37d6fcSTrond Myklebust return (struct nfs_page *)page_private(page); 161bd37d6fcSTrond Myklebust } 162bd37d6fcSTrond Myklebust 16384d3a9a9SWeston Andros Adamson /* 16484d3a9a9SWeston Andros Adamson * nfs_page_find_head_request_locked - find head request associated with @page 16584d3a9a9SWeston Andros Adamson * 16684d3a9a9SWeston Andros Adamson * must be called while holding the inode lock. 16784d3a9a9SWeston Andros Adamson * 16884d3a9a9SWeston Andros Adamson * returns matching head request with reference held, or NULL if not found. 16984d3a9a9SWeston Andros Adamson */ 17029418aa4SMel Gorman static struct nfs_page * 171b30d2f04STrond Myklebust nfs_page_find_private_request(struct page *page) 172277459d2STrond Myklebust { 1734b9bb25bSTrond Myklebust struct address_space *mapping = page_file_mapping(page); 174bd37d6fcSTrond Myklebust struct nfs_page *req; 175277459d2STrond Myklebust 176b30d2f04STrond Myklebust if (!PagePrivate(page)) 177b30d2f04STrond Myklebust return NULL; 1784b9bb25bSTrond Myklebust spin_lock(&mapping->private_lock); 179bd37d6fcSTrond Myklebust req = nfs_page_private_request(page); 18084d3a9a9SWeston Andros Adamson if (req) { 18184d3a9a9SWeston Andros Adamson WARN_ON_ONCE(req->wb_head != req); 18229418aa4SMel Gorman kref_get(&req->wb_kref); 18384d3a9a9SWeston Andros Adamson } 1844b9bb25bSTrond Myklebust spin_unlock(&mapping->private_lock); 185b30d2f04STrond Myklebust return req; 186b30d2f04STrond Myklebust } 18729418aa4SMel Gorman 188b30d2f04STrond Myklebust static struct nfs_page * 189b30d2f04STrond Myklebust nfs_page_find_swap_request(struct page *page) 190b30d2f04STrond Myklebust { 191b30d2f04STrond Myklebust struct inode *inode = page_file_mapping(page)->host; 192b30d2f04STrond Myklebust struct nfs_inode *nfsi = NFS_I(inode); 193b30d2f04STrond Myklebust struct nfs_page *req = NULL; 194b30d2f04STrond Myklebust if (!PageSwapCache(page)) 195b30d2f04STrond Myklebust return NULL; 196e824f99aSTrond Myklebust mutex_lock(&nfsi->commit_mutex); 197b30d2f04STrond Myklebust if (PageSwapCache(page)) { 198b30d2f04STrond Myklebust req = nfs_page_search_commits_for_head_request_locked(nfsi, 199b30d2f04STrond Myklebust page); 200b30d2f04STrond Myklebust if (req) { 201b30d2f04STrond Myklebust WARN_ON_ONCE(req->wb_head != req); 202b30d2f04STrond Myklebust kref_get(&req->wb_kref); 203b30d2f04STrond Myklebust } 204b30d2f04STrond Myklebust } 205e824f99aSTrond Myklebust mutex_unlock(&nfsi->commit_mutex); 206277459d2STrond Myklebust return req; 207277459d2STrond Myklebust } 208277459d2STrond Myklebust 20984d3a9a9SWeston Andros Adamson /* 21084d3a9a9SWeston Andros Adamson * nfs_page_find_head_request - find head request associated with @page 21184d3a9a9SWeston Andros Adamson * 21284d3a9a9SWeston Andros Adamson * returns matching head request with reference held, or NULL if not found. 21384d3a9a9SWeston Andros Adamson */ 21484d3a9a9SWeston Andros Adamson static struct nfs_page *nfs_page_find_head_request(struct page *page) 215277459d2STrond Myklebust { 216b30d2f04STrond Myklebust struct nfs_page *req; 217277459d2STrond Myklebust 218b30d2f04STrond Myklebust req = nfs_page_find_private_request(page); 219b30d2f04STrond Myklebust if (!req) 220b30d2f04STrond Myklebust req = nfs_page_find_swap_request(page); 221277459d2STrond Myklebust return req; 222277459d2STrond Myklebust } 223277459d2STrond Myklebust 2241da177e4SLinus Torvalds /* Adjust the file length if we're writing beyond the end */ 2251da177e4SLinus Torvalds static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count) 2261da177e4SLinus Torvalds { 227d56b4ddfSMel Gorman struct inode *inode = page_file_mapping(page)->host; 228a3d01454STrond Myklebust loff_t end, i_size; 229a3d01454STrond Myklebust pgoff_t end_index; 2301da177e4SLinus Torvalds 231a3d01454STrond Myklebust spin_lock(&inode->i_lock); 232a3d01454STrond Myklebust i_size = i_size_read(inode); 23309cbfeafSKirill A. Shutemov end_index = (i_size - 1) >> PAGE_SHIFT; 2348cd79788SHuang Ying if (i_size > 0 && page_index(page) < end_index) 235a3d01454STrond Myklebust goto out; 236d56b4ddfSMel Gorman end = page_file_offset(page) + ((loff_t)offset+count); 2371da177e4SLinus Torvalds if (i_size >= end) 238a3d01454STrond Myklebust goto out; 2391da177e4SLinus Torvalds i_size_write(inode, end); 240a3d01454STrond Myklebust nfs_inc_stats(inode, NFSIOS_EXTENDWRITE); 241a3d01454STrond Myklebust out: 242a3d01454STrond Myklebust spin_unlock(&inode->i_lock); 2431da177e4SLinus Torvalds } 2441da177e4SLinus Torvalds 245a301b777STrond Myklebust /* A writeback failed: mark the page as bad, and invalidate the page cache */ 246a301b777STrond Myklebust static void nfs_set_pageerror(struct page *page) 247a301b777STrond Myklebust { 248d56b4ddfSMel Gorman nfs_zap_mapping(page_file_mapping(page)->host, page_file_mapping(page)); 249a301b777STrond Myklebust } 250a301b777STrond Myklebust 251d72ddcbaSWeston Andros Adamson /* 252d72ddcbaSWeston Andros Adamson * nfs_page_group_search_locked 253d72ddcbaSWeston Andros Adamson * @head - head request of page group 254d72ddcbaSWeston Andros Adamson * @page_offset - offset into page 255d72ddcbaSWeston Andros Adamson * 256d72ddcbaSWeston Andros Adamson * Search page group with head @head to find a request that contains the 257d72ddcbaSWeston Andros Adamson * page offset @page_offset. 258d72ddcbaSWeston Andros Adamson * 259d72ddcbaSWeston Andros Adamson * Returns a pointer to the first matching nfs request, or NULL if no 260d72ddcbaSWeston Andros Adamson * match is found. 261d72ddcbaSWeston Andros Adamson * 262d72ddcbaSWeston Andros Adamson * Must be called with the page group lock held 263d72ddcbaSWeston Andros Adamson */ 264d72ddcbaSWeston Andros Adamson static struct nfs_page * 265d72ddcbaSWeston Andros Adamson nfs_page_group_search_locked(struct nfs_page *head, unsigned int page_offset) 266d72ddcbaSWeston Andros Adamson { 267d72ddcbaSWeston Andros Adamson struct nfs_page *req; 268d72ddcbaSWeston Andros Adamson 269d72ddcbaSWeston Andros Adamson req = head; 270d72ddcbaSWeston Andros Adamson do { 271d72ddcbaSWeston Andros Adamson if (page_offset >= req->wb_pgbase && 272d72ddcbaSWeston Andros Adamson page_offset < (req->wb_pgbase + req->wb_bytes)) 273d72ddcbaSWeston Andros Adamson return req; 274d72ddcbaSWeston Andros Adamson 275d72ddcbaSWeston Andros Adamson req = req->wb_this_page; 276d72ddcbaSWeston Andros Adamson } while (req != head); 277d72ddcbaSWeston Andros Adamson 278d72ddcbaSWeston Andros Adamson return NULL; 279d72ddcbaSWeston Andros Adamson } 280d72ddcbaSWeston Andros Adamson 281d72ddcbaSWeston Andros Adamson /* 282d72ddcbaSWeston Andros Adamson * nfs_page_group_covers_page 283d72ddcbaSWeston Andros Adamson * @head - head request of page group 284d72ddcbaSWeston Andros Adamson * 285d72ddcbaSWeston Andros Adamson * Return true if the page group with head @head covers the whole page, 286d72ddcbaSWeston Andros Adamson * returns false otherwise 287d72ddcbaSWeston Andros Adamson */ 288d72ddcbaSWeston Andros Adamson static bool nfs_page_group_covers_page(struct nfs_page *req) 289d72ddcbaSWeston Andros Adamson { 290d72ddcbaSWeston Andros Adamson struct nfs_page *tmp; 291d72ddcbaSWeston Andros Adamson unsigned int pos = 0; 292d72ddcbaSWeston Andros Adamson unsigned int len = nfs_page_length(req->wb_page); 293d72ddcbaSWeston Andros Adamson 2941344b7eaSTrond Myklebust nfs_page_group_lock(req); 295d72ddcbaSWeston Andros Adamson 2967e8a30f8STrond Myklebust for (;;) { 297d72ddcbaSWeston Andros Adamson tmp = nfs_page_group_search_locked(req->wb_head, pos); 2987e8a30f8STrond Myklebust if (!tmp) 2997e8a30f8STrond Myklebust break; 3007e8a30f8STrond Myklebust pos = tmp->wb_pgbase + tmp->wb_bytes; 301d72ddcbaSWeston Andros Adamson } 302d72ddcbaSWeston Andros Adamson 303d72ddcbaSWeston Andros Adamson nfs_page_group_unlock(req); 3047e8a30f8STrond Myklebust return pos >= len; 305d72ddcbaSWeston Andros Adamson } 306d72ddcbaSWeston Andros Adamson 3071da177e4SLinus Torvalds /* We can set the PG_uptodate flag if we see that a write request 3081da177e4SLinus Torvalds * covers the full page. 3091da177e4SLinus Torvalds */ 310d72ddcbaSWeston Andros Adamson static void nfs_mark_uptodate(struct nfs_page *req) 3111da177e4SLinus Torvalds { 312d72ddcbaSWeston Andros Adamson if (PageUptodate(req->wb_page)) 3131da177e4SLinus Torvalds return; 314d72ddcbaSWeston Andros Adamson if (!nfs_page_group_covers_page(req)) 3151da177e4SLinus Torvalds return; 316d72ddcbaSWeston Andros Adamson SetPageUptodate(req->wb_page); 3171da177e4SLinus Torvalds } 3181da177e4SLinus Torvalds 3191da177e4SLinus Torvalds static int wb_priority(struct writeback_control *wbc) 3201da177e4SLinus Torvalds { 321e87b4c7aSNeilBrown int ret = 0; 322cca588d6STrond Myklebust 323e87b4c7aSNeilBrown if (wbc->sync_mode == WB_SYNC_ALL) 324e87b4c7aSNeilBrown ret = FLUSH_COND_STABLE; 325e87b4c7aSNeilBrown return ret; 3261da177e4SLinus Torvalds } 3271da177e4SLinus Torvalds 3281da177e4SLinus Torvalds /* 32989a09141SPeter Zijlstra * NFS congestion control 33089a09141SPeter Zijlstra */ 33189a09141SPeter Zijlstra 33289a09141SPeter Zijlstra int nfs_congestion_kb; 33389a09141SPeter Zijlstra 33489a09141SPeter Zijlstra #define NFS_CONGESTION_ON_THRESH (nfs_congestion_kb >> (PAGE_SHIFT-10)) 33589a09141SPeter Zijlstra #define NFS_CONGESTION_OFF_THRESH \ 33689a09141SPeter Zijlstra (NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2)) 33789a09141SPeter Zijlstra 338deed85e7STrond Myklebust static void nfs_set_page_writeback(struct page *page) 33989a09141SPeter Zijlstra { 3400db10944SJan Kara struct inode *inode = page_file_mapping(page)->host; 3410db10944SJan Kara struct nfs_server *nfss = NFS_SERVER(inode); 3425a6d41b3STrond Myklebust int ret = test_set_page_writeback(page); 3435a6d41b3STrond Myklebust 344deed85e7STrond Myklebust WARN_ON_ONCE(ret != 0); 34589a09141SPeter Zijlstra 346277866a0SPeter Zijlstra if (atomic_long_inc_return(&nfss->writeback) > 3470db10944SJan Kara NFS_CONGESTION_ON_THRESH) 3480db10944SJan Kara set_bdi_congested(inode_to_bdi(inode), BLK_RW_ASYNC); 34989a09141SPeter Zijlstra } 35089a09141SPeter Zijlstra 35120633f04SWeston Andros Adamson static void nfs_end_page_writeback(struct nfs_page *req) 35289a09141SPeter Zijlstra { 35320633f04SWeston Andros Adamson struct inode *inode = page_file_mapping(req->wb_page)->host; 35489a09141SPeter Zijlstra struct nfs_server *nfss = NFS_SERVER(inode); 35531a01f09STrond Myklebust bool is_done; 35689a09141SPeter Zijlstra 35731a01f09STrond Myklebust is_done = nfs_page_group_sync_on_bit(req, PG_WB_END); 35831a01f09STrond Myklebust nfs_unlock_request(req); 35931a01f09STrond Myklebust if (!is_done) 36020633f04SWeston Andros Adamson return; 36120633f04SWeston Andros Adamson 36220633f04SWeston Andros Adamson end_page_writeback(req->wb_page); 363c4dc4beeSPeter Zijlstra if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH) 3640db10944SJan Kara clear_bdi_congested(inode_to_bdi(inode), BLK_RW_ASYNC); 36589a09141SPeter Zijlstra } 36689a09141SPeter Zijlstra 367d4581383SWeston Andros Adamson /* 368d4581383SWeston Andros Adamson * nfs_unroll_locks_and_wait - unlock all newly locked reqs and wait on @req 369d4581383SWeston Andros Adamson * 370d4581383SWeston Andros Adamson * this is a helper function for nfs_lock_and_join_requests 371d4581383SWeston Andros Adamson * 372d4581383SWeston Andros Adamson * @inode - inode associated with request page group, must be holding inode lock 373d4581383SWeston Andros Adamson * @head - head request of page group, must be holding head lock 374d4581383SWeston Andros Adamson * @req - request that couldn't lock and needs to wait on the req bit lock 375d4581383SWeston Andros Adamson * 376b5bab9bfSTrond Myklebust * NOTE: this must be called holding page_group bit lock 377b5bab9bfSTrond Myklebust * which will be released before returning. 378d4581383SWeston Andros Adamson * 379d4581383SWeston Andros Adamson * returns 0 on success, < 0 on error. 380d4581383SWeston Andros Adamson */ 38174a6d4b5STrond Myklebust static void 38274a6d4b5STrond Myklebust nfs_unroll_locks(struct inode *inode, struct nfs_page *head, 3836d17d653STrond Myklebust struct nfs_page *req) 384d4581383SWeston Andros Adamson { 385d4581383SWeston Andros Adamson struct nfs_page *tmp; 386e261f51fSTrond Myklebust 387d4581383SWeston Andros Adamson /* relinquish all the locks successfully grabbed this run */ 3885b2b5187STrond Myklebust for (tmp = head->wb_this_page ; tmp != req; tmp = tmp->wb_this_page) { 3895b2b5187STrond Myklebust if (!kref_read(&tmp->wb_kref)) 3905b2b5187STrond Myklebust continue; 3915b2b5187STrond Myklebust nfs_unlock_and_release_request(tmp); 3925b2b5187STrond Myklebust } 393e261f51fSTrond Myklebust } 394d4581383SWeston Andros Adamson 395d4581383SWeston Andros Adamson /* 396d4581383SWeston Andros Adamson * nfs_destroy_unlinked_subrequests - destroy recently unlinked subrequests 397d4581383SWeston Andros Adamson * 398d4581383SWeston Andros Adamson * @destroy_list - request list (using wb_this_page) terminated by @old_head 399d4581383SWeston Andros Adamson * @old_head - the old head of the list 400d4581383SWeston Andros Adamson * 401d4581383SWeston Andros Adamson * All subrequests must be locked and removed from all lists, so at this point 402d4581383SWeston Andros Adamson * they are only "active" in this function, and possibly in nfs_wait_on_request 403d4581383SWeston Andros Adamson * with a reference held by some other context. 404d4581383SWeston Andros Adamson */ 405d4581383SWeston Andros Adamson static void 406d4581383SWeston Andros Adamson nfs_destroy_unlinked_subrequests(struct nfs_page *destroy_list, 407b66aaa8dSTrond Myklebust struct nfs_page *old_head, 408b66aaa8dSTrond Myklebust struct inode *inode) 409d4581383SWeston Andros Adamson { 410d4581383SWeston Andros Adamson while (destroy_list) { 411d4581383SWeston Andros Adamson struct nfs_page *subreq = destroy_list; 412d4581383SWeston Andros Adamson 413d4581383SWeston Andros Adamson destroy_list = (subreq->wb_this_page == old_head) ? 414d4581383SWeston Andros Adamson NULL : subreq->wb_this_page; 415d4581383SWeston Andros Adamson 416d4581383SWeston Andros Adamson WARN_ON_ONCE(old_head != subreq->wb_head); 417d4581383SWeston Andros Adamson 418d4581383SWeston Andros Adamson /* make sure old group is not used */ 419d4581383SWeston Andros Adamson subreq->wb_this_page = subreq; 420d4581383SWeston Andros Adamson 421902a4c00STrond Myklebust clear_bit(PG_REMOVE, &subreq->wb_flags); 422902a4c00STrond Myklebust 4235b2b5187STrond Myklebust /* Note: races with nfs_page_group_destroy() */ 4245b2b5187STrond Myklebust if (!kref_read(&subreq->wb_kref)) { 4255b2b5187STrond Myklebust /* Check if we raced with nfs_page_group_destroy() */ 426902a4c00STrond Myklebust if (test_and_clear_bit(PG_TEARDOWN, &subreq->wb_flags)) 4275b2b5187STrond Myklebust nfs_free_request(subreq); 4285b2b5187STrond Myklebust continue; 4295b2b5187STrond Myklebust } 430d4581383SWeston Andros Adamson 4315b2b5187STrond Myklebust subreq->wb_head = subreq; 4325b2b5187STrond Myklebust 433b66aaa8dSTrond Myklebust if (test_and_clear_bit(PG_INODE_REF, &subreq->wb_flags)) { 434d4581383SWeston Andros Adamson nfs_release_request(subreq); 435a6b6d5b8STrond Myklebust atomic_long_dec(&NFS_I(inode)->nrequests); 436d4581383SWeston Andros Adamson } 4375b2b5187STrond Myklebust 4385b2b5187STrond Myklebust /* subreq is now totally disconnected from page group or any 4395b2b5187STrond Myklebust * write / commit lists. last chance to wake any waiters */ 4405b2b5187STrond Myklebust nfs_unlock_and_release_request(subreq); 441d4581383SWeston Andros Adamson } 442d4581383SWeston Andros Adamson } 443d4581383SWeston Andros Adamson 444d4581383SWeston Andros Adamson /* 445d4581383SWeston Andros Adamson * nfs_lock_and_join_requests - join all subreqs to the head req and return 446d4581383SWeston Andros Adamson * a locked reference, cancelling any pending 447d4581383SWeston Andros Adamson * operations for this page. 448d4581383SWeston Andros Adamson * 449d4581383SWeston Andros Adamson * @page - the page used to lookup the "page group" of nfs_page structures 450d4581383SWeston Andros Adamson * 451d4581383SWeston Andros Adamson * This function joins all sub requests to the head request by first 452d4581383SWeston Andros Adamson * locking all requests in the group, cancelling any pending operations 453d4581383SWeston Andros Adamson * and finally updating the head request to cover the whole range covered by 454d4581383SWeston Andros Adamson * the (former) group. All subrequests are removed from any write or commit 455d4581383SWeston Andros Adamson * lists, unlinked from the group and destroyed. 456d4581383SWeston Andros Adamson * 457d4581383SWeston Andros Adamson * Returns a locked, referenced pointer to the head request - which after 458d4581383SWeston Andros Adamson * this call is guaranteed to be the only request associated with the page. 459d4581383SWeston Andros Adamson * Returns NULL if no requests are found for @page, or a ERR_PTR if an 460d4581383SWeston Andros Adamson * error was encountered. 461d4581383SWeston Andros Adamson */ 462d4581383SWeston Andros Adamson static struct nfs_page * 4636d17d653STrond Myklebust nfs_lock_and_join_requests(struct page *page) 464d4581383SWeston Andros Adamson { 465d4581383SWeston Andros Adamson struct inode *inode = page_file_mapping(page)->host; 466d4581383SWeston Andros Adamson struct nfs_page *head, *subreq; 467d4581383SWeston Andros Adamson struct nfs_page *destroy_list = NULL; 468d4581383SWeston Andros Adamson unsigned int total_bytes; 469d4581383SWeston Andros Adamson int ret; 470d4581383SWeston Andros Adamson 471d4581383SWeston Andros Adamson try_again: 472d4581383SWeston Andros Adamson /* 473d4581383SWeston Andros Adamson * A reference is taken only on the head request which acts as a 474d4581383SWeston Andros Adamson * reference to the whole page group - the group will not be destroyed 475d4581383SWeston Andros Adamson * until the head reference is released. 476d4581383SWeston Andros Adamson */ 477bd37d6fcSTrond Myklebust head = nfs_page_find_head_request(page); 478bd37d6fcSTrond Myklebust if (!head) 479d4581383SWeston Andros Adamson return NULL; 480d4581383SWeston Andros Adamson 481a0e265bcSTrond Myklebust /* lock the page head first in order to avoid an ABBA inefficiency */ 482a0e265bcSTrond Myklebust if (!nfs_lock_request(head)) { 483a0e265bcSTrond Myklebust ret = nfs_wait_on_request(head); 484a0e265bcSTrond Myklebust nfs_release_request(head); 485a0e265bcSTrond Myklebust if (ret < 0) 486a0e265bcSTrond Myklebust return ERR_PTR(ret); 487a0e265bcSTrond Myklebust goto try_again; 488a0e265bcSTrond Myklebust } 489bd37d6fcSTrond Myklebust 490bd37d6fcSTrond Myklebust /* Ensure that nobody removed the request before we locked it */ 491bd37d6fcSTrond Myklebust if (head != nfs_page_private_request(page) && !PageSwapCache(page)) { 492bd37d6fcSTrond Myklebust nfs_unlock_and_release_request(head); 493bd37d6fcSTrond Myklebust goto try_again; 494bd37d6fcSTrond Myklebust } 4957c3af975SWeston Andros Adamson 4961344b7eaSTrond Myklebust ret = nfs_page_group_lock(head); 497b5bab9bfSTrond Myklebust if (ret < 0) { 498a0e265bcSTrond Myklebust nfs_unlock_and_release_request(head); 499b5bab9bfSTrond Myklebust return ERR_PTR(ret); 5007c3af975SWeston Andros Adamson } 5017c3af975SWeston Andros Adamson 5027c3af975SWeston Andros Adamson /* lock each request in the page group */ 503a0e265bcSTrond Myklebust total_bytes = head->wb_bytes; 504a0e265bcSTrond Myklebust for (subreq = head->wb_this_page; subreq != head; 505a0e265bcSTrond Myklebust subreq = subreq->wb_this_page) { 50674a6d4b5STrond Myklebust 5075b2b5187STrond Myklebust if (!kref_get_unless_zero(&subreq->wb_kref)) 5085b2b5187STrond Myklebust continue; 50974a6d4b5STrond Myklebust while (!nfs_lock_request(subreq)) { 510b5bab9bfSTrond Myklebust /* 51174a6d4b5STrond Myklebust * Unlock page to allow nfs_page_group_sync_on_bit() 51274a6d4b5STrond Myklebust * to succeed 513b5bab9bfSTrond Myklebust */ 51474a6d4b5STrond Myklebust nfs_page_group_unlock(head); 51574a6d4b5STrond Myklebust ret = nfs_wait_on_request(subreq); 51674a6d4b5STrond Myklebust if (!ret) 5171344b7eaSTrond Myklebust ret = nfs_page_group_lock(head); 51874a6d4b5STrond Myklebust if (ret < 0) { 51974a6d4b5STrond Myklebust nfs_unroll_locks(inode, head, subreq); 5205b2b5187STrond Myklebust nfs_release_request(subreq); 52174a6d4b5STrond Myklebust nfs_unlock_and_release_request(head); 522d4581383SWeston Andros Adamson return ERR_PTR(ret); 523d4581383SWeston Andros Adamson } 52474a6d4b5STrond Myklebust } 525e14bebf6STrond Myklebust /* 526e14bebf6STrond Myklebust * Subrequests are always contiguous, non overlapping 527e14bebf6STrond Myklebust * and in order - but may be repeated (mirrored writes). 528e14bebf6STrond Myklebust */ 529e14bebf6STrond Myklebust if (subreq->wb_offset == (head->wb_offset + total_bytes)) { 530e14bebf6STrond Myklebust /* keep track of how many bytes this group covers */ 531e14bebf6STrond Myklebust total_bytes += subreq->wb_bytes; 532e14bebf6STrond Myklebust } else if (WARN_ON_ONCE(subreq->wb_offset < head->wb_offset || 533e14bebf6STrond Myklebust ((subreq->wb_offset + subreq->wb_bytes) > 534e14bebf6STrond Myklebust (head->wb_offset + total_bytes)))) { 535*8b77484fSTrond Myklebust nfs_page_group_unlock(head); 53674a6d4b5STrond Myklebust nfs_unroll_locks(inode, head, subreq); 5375b2b5187STrond Myklebust nfs_unlock_and_release_request(subreq); 53874a6d4b5STrond Myklebust nfs_unlock_and_release_request(head); 539e14bebf6STrond Myklebust return ERR_PTR(-EIO); 540e14bebf6STrond Myklebust } 541a0e265bcSTrond Myklebust } 542d4581383SWeston Andros Adamson 543d4581383SWeston Andros Adamson /* Now that all requests are locked, make sure they aren't on any list. 544d4581383SWeston Andros Adamson * Commit list removal accounting is done after locks are dropped */ 545d4581383SWeston Andros Adamson subreq = head; 546d4581383SWeston Andros Adamson do { 547411a99adSWeston Andros Adamson nfs_clear_request_commit(subreq); 548d4581383SWeston Andros Adamson subreq = subreq->wb_this_page; 549d4581383SWeston Andros Adamson } while (subreq != head); 550d4581383SWeston Andros Adamson 551d4581383SWeston Andros Adamson /* unlink subrequests from head, destroy them later */ 552d4581383SWeston Andros Adamson if (head->wb_this_page != head) { 553d4581383SWeston Andros Adamson /* destroy list will be terminated by head */ 554d4581383SWeston Andros Adamson destroy_list = head->wb_this_page; 555d4581383SWeston Andros Adamson head->wb_this_page = head; 556d4581383SWeston Andros Adamson 557d4581383SWeston Andros Adamson /* change head request to cover whole range that 558d4581383SWeston Andros Adamson * the former page group covered */ 559d4581383SWeston Andros Adamson head->wb_bytes = total_bytes; 560d4581383SWeston Andros Adamson } 561d4581383SWeston Andros Adamson 562b66aaa8dSTrond Myklebust /* Postpone destruction of this request */ 563b66aaa8dSTrond Myklebust if (test_and_clear_bit(PG_REMOVE, &head->wb_flags)) { 564b66aaa8dSTrond Myklebust set_bit(PG_INODE_REF, &head->wb_flags); 565b66aaa8dSTrond Myklebust kref_get(&head->wb_kref); 566a6b6d5b8STrond Myklebust atomic_long_inc(&NFS_I(inode)->nrequests); 567b66aaa8dSTrond Myklebust } 568b66aaa8dSTrond Myklebust 569d4581383SWeston Andros Adamson nfs_page_group_unlock(head); 570d4581383SWeston Andros Adamson 571b66aaa8dSTrond Myklebust nfs_destroy_unlinked_subrequests(destroy_list, head, inode); 572d4581383SWeston Andros Adamson 573b5bab9bfSTrond Myklebust /* Did we lose a race with nfs_inode_remove_request()? */ 574b5bab9bfSTrond Myklebust if (!(PagePrivate(page) || PageSwapCache(page))) { 575b5bab9bfSTrond Myklebust nfs_unlock_and_release_request(head); 576b5bab9bfSTrond Myklebust return NULL; 577b5bab9bfSTrond Myklebust } 578b5bab9bfSTrond Myklebust 579bd37d6fcSTrond Myklebust /* still holds ref on head from nfs_page_find_head_request 580d4581383SWeston Andros Adamson * and still has lock on head from lock loop */ 581d4581383SWeston Andros Adamson return head; 582612c9384STrond Myklebust } 583074cc1deSTrond Myklebust 5840bcbf039SPeng Tao static void nfs_write_error_remove_page(struct nfs_page *req) 5850bcbf039SPeng Tao { 5860bcbf039SPeng Tao nfs_end_page_writeback(req); 5870bcbf039SPeng Tao generic_error_remove_page(page_file_mapping(req->wb_page), 5880bcbf039SPeng Tao req->wb_page); 5891f84ccdfSFred Isaman nfs_release_request(req); 5900bcbf039SPeng Tao } 5910bcbf039SPeng Tao 592a6598813STrond Myklebust static bool 593a6598813STrond Myklebust nfs_error_is_fatal_on_server(int err) 594a6598813STrond Myklebust { 595a6598813STrond Myklebust switch (err) { 596a6598813STrond Myklebust case 0: 597a6598813STrond Myklebust case -ERESTARTSYS: 598a6598813STrond Myklebust case -EINTR: 599a6598813STrond Myklebust return false; 600a6598813STrond Myklebust } 601a6598813STrond Myklebust return nfs_error_is_fatal(err); 602e261f51fSTrond Myklebust } 603074cc1deSTrond Myklebust 604074cc1deSTrond Myklebust /* 605074cc1deSTrond Myklebust * Find an associated nfs write request, and prepare to flush it out 606074cc1deSTrond Myklebust * May return an error if the user signalled nfs_wait_on_request(). 607074cc1deSTrond Myklebust */ 608074cc1deSTrond Myklebust static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio, 6096d17d653STrond Myklebust struct page *page) 610074cc1deSTrond Myklebust { 611074cc1deSTrond Myklebust struct nfs_page *req; 612074cc1deSTrond Myklebust int ret = 0; 613074cc1deSTrond Myklebust 6146d17d653STrond Myklebust req = nfs_lock_and_join_requests(page); 615074cc1deSTrond Myklebust if (!req) 616074cc1deSTrond Myklebust goto out; 617074cc1deSTrond Myklebust ret = PTR_ERR(req); 618074cc1deSTrond Myklebust if (IS_ERR(req)) 619074cc1deSTrond Myklebust goto out; 620074cc1deSTrond Myklebust 621deed85e7STrond Myklebust nfs_set_page_writeback(page); 622deed85e7STrond Myklebust WARN_ON_ONCE(test_bit(PG_CLEAN, &req->wb_flags)); 623074cc1deSTrond Myklebust 624deed85e7STrond Myklebust ret = 0; 625a6598813STrond Myklebust /* If there is a fatal error that covers this write, just exit */ 626a6598813STrond Myklebust if (nfs_error_is_fatal_on_server(req->wb_context->error)) 627a6598813STrond Myklebust goto out_launder; 628a6598813STrond Myklebust 629f8512ad0SFred Isaman if (!nfs_pageio_add_request(pgio, req)) { 630074cc1deSTrond Myklebust ret = pgio->pg_error; 6310bcbf039SPeng Tao /* 632c373fff7STrond Myklebust * Remove the problematic req upon fatal errors on the server 6330bcbf039SPeng Tao */ 6340bcbf039SPeng Tao if (nfs_error_is_fatal(ret)) { 6350bcbf039SPeng Tao nfs_context_set_write_error(req->wb_context, ret); 636c373fff7STrond Myklebust if (nfs_error_is_fatal_on_server(ret)) 637a6598813STrond Myklebust goto out_launder; 638d6c843b9SPeng Tao } 6390bcbf039SPeng Tao nfs_redirty_request(req); 6400bcbf039SPeng Tao ret = -EAGAIN; 64140f90271STrond Myklebust } else 64240f90271STrond Myklebust nfs_add_stats(page_file_mapping(page)->host, 64340f90271STrond Myklebust NFSIOS_WRITEPAGES, 1); 644074cc1deSTrond Myklebust out: 645074cc1deSTrond Myklebust return ret; 646a6598813STrond Myklebust out_launder: 647a6598813STrond Myklebust nfs_write_error_remove_page(req); 648a6598813STrond Myklebust return ret; 649e261f51fSTrond Myklebust } 650e261f51fSTrond Myklebust 651d6c843b9SPeng Tao static int nfs_do_writepage(struct page *page, struct writeback_control *wbc, 652c373fff7STrond Myklebust struct nfs_pageio_descriptor *pgio) 653f758c885STrond Myklebust { 654cfb506e1STrond Myklebust int ret; 655f758c885STrond Myklebust 6568cd79788SHuang Ying nfs_pageio_cond_complete(pgio, page_index(page)); 6576d17d653STrond Myklebust ret = nfs_page_async_flush(pgio, page); 658cfb506e1STrond Myklebust if (ret == -EAGAIN) { 659cfb506e1STrond Myklebust redirty_page_for_writepage(wbc, page); 660cfb506e1STrond Myklebust ret = 0; 661cfb506e1STrond Myklebust } 662cfb506e1STrond Myklebust return ret; 663f758c885STrond Myklebust } 664f758c885STrond Myklebust 665e261f51fSTrond Myklebust /* 6661da177e4SLinus Torvalds * Write an mmapped page to the server. 6671da177e4SLinus Torvalds */ 668d6c843b9SPeng Tao static int nfs_writepage_locked(struct page *page, 669c373fff7STrond Myklebust struct writeback_control *wbc) 6701da177e4SLinus Torvalds { 671f758c885STrond Myklebust struct nfs_pageio_descriptor pgio; 67240f90271STrond Myklebust struct inode *inode = page_file_mapping(page)->host; 673e261f51fSTrond Myklebust int err; 6741da177e4SLinus Torvalds 67540f90271STrond Myklebust nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE); 676811ed92eSTrond Myklebust nfs_pageio_init_write(&pgio, inode, 0, 677a20c93e3SChristoph Hellwig false, &nfs_async_write_completion_ops); 678c373fff7STrond Myklebust err = nfs_do_writepage(page, wbc, &pgio); 679f758c885STrond Myklebust nfs_pageio_complete(&pgio); 680f758c885STrond Myklebust if (err < 0) 6814d770ccfSTrond Myklebust return err; 682f758c885STrond Myklebust if (pgio.pg_error < 0) 683f758c885STrond Myklebust return pgio.pg_error; 684f758c885STrond Myklebust return 0; 6854d770ccfSTrond Myklebust } 6864d770ccfSTrond Myklebust 6874d770ccfSTrond Myklebust int nfs_writepage(struct page *page, struct writeback_control *wbc) 6884d770ccfSTrond Myklebust { 689f758c885STrond Myklebust int ret; 6904d770ccfSTrond Myklebust 691c373fff7STrond Myklebust ret = nfs_writepage_locked(page, wbc); 6921da177e4SLinus Torvalds unlock_page(page); 693f758c885STrond Myklebust return ret; 694f758c885STrond Myklebust } 695f758c885STrond Myklebust 696f758c885STrond Myklebust static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data) 697f758c885STrond Myklebust { 698f758c885STrond Myklebust int ret; 699f758c885STrond Myklebust 700c373fff7STrond Myklebust ret = nfs_do_writepage(page, wbc, data); 701f758c885STrond Myklebust unlock_page(page); 702f758c885STrond Myklebust return ret; 7031da177e4SLinus Torvalds } 7041da177e4SLinus Torvalds 705919e3bd9STrond Myklebust static void nfs_io_completion_commit(void *inode) 706919e3bd9STrond Myklebust { 707919e3bd9STrond Myklebust nfs_commit_inode(inode, 0); 708919e3bd9STrond Myklebust } 709919e3bd9STrond Myklebust 7101da177e4SLinus Torvalds int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc) 7111da177e4SLinus Torvalds { 7121da177e4SLinus Torvalds struct inode *inode = mapping->host; 713c63c7b05STrond Myklebust struct nfs_pageio_descriptor pgio; 714919e3bd9STrond Myklebust struct nfs_io_completion *ioc = nfs_io_completion_alloc(GFP_NOFS); 7151da177e4SLinus Torvalds int err; 7161da177e4SLinus Torvalds 71791d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES); 71891d5b470SChuck Lever 719919e3bd9STrond Myklebust if (ioc) 720919e3bd9STrond Myklebust nfs_io_completion_init(ioc, nfs_io_completion_commit, inode); 721919e3bd9STrond Myklebust 722a20c93e3SChristoph Hellwig nfs_pageio_init_write(&pgio, inode, wb_priority(wbc), false, 723a20c93e3SChristoph Hellwig &nfs_async_write_completion_ops); 724919e3bd9STrond Myklebust pgio.pg_io_completion = ioc; 725f758c885STrond Myklebust err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio); 726c63c7b05STrond Myklebust nfs_pageio_complete(&pgio); 727919e3bd9STrond Myklebust nfs_io_completion_put(ioc); 72872cb77f4STrond Myklebust 729f758c885STrond Myklebust if (err < 0) 73072cb77f4STrond Myklebust goto out_err; 73172cb77f4STrond Myklebust err = pgio.pg_error; 73272cb77f4STrond Myklebust if (err < 0) 73372cb77f4STrond Myklebust goto out_err; 734c63c7b05STrond Myklebust return 0; 73572cb77f4STrond Myklebust out_err: 73672cb77f4STrond Myklebust return err; 7371da177e4SLinus Torvalds } 7381da177e4SLinus Torvalds 7391da177e4SLinus Torvalds /* 7401da177e4SLinus Torvalds * Insert a write request into an inode 7411da177e4SLinus Torvalds */ 742d6d6dc7cSFred Isaman static void nfs_inode_add_request(struct inode *inode, struct nfs_page *req) 7431da177e4SLinus Torvalds { 7444b9bb25bSTrond Myklebust struct address_space *mapping = page_file_mapping(req->wb_page); 7451da177e4SLinus Torvalds struct nfs_inode *nfsi = NFS_I(inode); 746e7d39069STrond Myklebust 7472bfc6e56SWeston Andros Adamson WARN_ON_ONCE(req->wb_this_page != req); 7482bfc6e56SWeston Andros Adamson 749e7d39069STrond Myklebust /* Lock the request! */ 7507ad84aa9STrond Myklebust nfs_lock_request(req); 751e7d39069STrond Myklebust 75229418aa4SMel Gorman /* 75329418aa4SMel Gorman * Swap-space should not get truncated. Hence no need to plug the race 75429418aa4SMel Gorman * with invalidate/truncate. 75529418aa4SMel Gorman */ 7564b9bb25bSTrond Myklebust spin_lock(&mapping->private_lock); 7574b9bb25bSTrond Myklebust if (!nfs_have_writebacks(inode) && 7584b9bb25bSTrond Myklebust NFS_PROTO(inode)->have_delegation(inode, FMODE_WRITE)) { 7594b9bb25bSTrond Myklebust spin_lock(&inode->i_lock); 7604b9bb25bSTrond Myklebust inode->i_version++; 7614b9bb25bSTrond Myklebust spin_unlock(&inode->i_lock); 7624b9bb25bSTrond Myklebust } 76329418aa4SMel Gorman if (likely(!PageSwapCache(req->wb_page))) { 7642df485a7STrond Myklebust set_bit(PG_MAPPED, &req->wb_flags); 765deb7d638STrond Myklebust SetPagePrivate(req->wb_page); 766277459d2STrond Myklebust set_page_private(req->wb_page, (unsigned long)req); 76729418aa4SMel Gorman } 7684b9bb25bSTrond Myklebust spin_unlock(&mapping->private_lock); 769a6b6d5b8STrond Myklebust atomic_long_inc(&nfsi->nrequests); 77017089a29SWeston Andros Adamson /* this a head request for a page group - mark it as having an 771cb1410c7SWeston Andros Adamson * extra reference so sub groups can follow suit. 772cb1410c7SWeston Andros Adamson * This flag also informs pgio layer when to bump nrequests when 773cb1410c7SWeston Andros Adamson * adding subrequests. */ 77417089a29SWeston Andros Adamson WARN_ON(test_and_set_bit(PG_INODE_REF, &req->wb_flags)); 775c03b4024STrond Myklebust kref_get(&req->wb_kref); 7761da177e4SLinus Torvalds } 7771da177e4SLinus Torvalds 7781da177e4SLinus Torvalds /* 77989a09141SPeter Zijlstra * Remove a write request from an inode 7801da177e4SLinus Torvalds */ 7811da177e4SLinus Torvalds static void nfs_inode_remove_request(struct nfs_page *req) 7821da177e4SLinus Torvalds { 7834b9bb25bSTrond Myklebust struct address_space *mapping = page_file_mapping(req->wb_page); 7844b9bb25bSTrond Myklebust struct inode *inode = mapping->host; 7851da177e4SLinus Torvalds struct nfs_inode *nfsi = NFS_I(inode); 78620633f04SWeston Andros Adamson struct nfs_page *head; 78720633f04SWeston Andros Adamson 788a6b6d5b8STrond Myklebust atomic_long_dec(&nfsi->nrequests); 78920633f04SWeston Andros Adamson if (nfs_page_group_sync_on_bit(req, PG_REMOVE)) { 79020633f04SWeston Andros Adamson head = req->wb_head; 7911da177e4SLinus Torvalds 7924b9bb25bSTrond Myklebust spin_lock(&mapping->private_lock); 79367911c8fSAnna Schumaker if (likely(head->wb_page && !PageSwapCache(head->wb_page))) { 79420633f04SWeston Andros Adamson set_page_private(head->wb_page, 0); 79520633f04SWeston Andros Adamson ClearPagePrivate(head->wb_page); 79620633f04SWeston Andros Adamson clear_bit(PG_MAPPED, &head->wb_flags); 79729418aa4SMel Gorman } 7984b9bb25bSTrond Myklebust spin_unlock(&mapping->private_lock); 79920633f04SWeston Andros Adamson } 80017089a29SWeston Andros Adamson 80117089a29SWeston Andros Adamson if (test_and_clear_bit(PG_INODE_REF, &req->wb_flags)) 8021da177e4SLinus Torvalds nfs_release_request(req); 8031da177e4SLinus Torvalds } 8041da177e4SLinus Torvalds 80561822ab5STrond Myklebust static void 8066d884e8fSFred nfs_mark_request_dirty(struct nfs_page *req) 80761822ab5STrond Myklebust { 80867911c8fSAnna Schumaker if (req->wb_page) 80961822ab5STrond Myklebust __set_page_dirty_nobuffers(req->wb_page); 81061822ab5STrond Myklebust } 81161822ab5STrond Myklebust 8123a3908c8STrond Myklebust /* 8133a3908c8STrond Myklebust * nfs_page_search_commits_for_head_request_locked 8143a3908c8STrond Myklebust * 8153a3908c8STrond Myklebust * Search through commit lists on @inode for the head request for @page. 8163a3908c8STrond Myklebust * Must be called while holding the inode (which is cinfo) lock. 8173a3908c8STrond Myklebust * 8183a3908c8STrond Myklebust * Returns the head request if found, or NULL if not found. 8193a3908c8STrond Myklebust */ 8203a3908c8STrond Myklebust static struct nfs_page * 8213a3908c8STrond Myklebust nfs_page_search_commits_for_head_request_locked(struct nfs_inode *nfsi, 8223a3908c8STrond Myklebust struct page *page) 8233a3908c8STrond Myklebust { 8243a3908c8STrond Myklebust struct nfs_page *freq, *t; 8253a3908c8STrond Myklebust struct nfs_commit_info cinfo; 8263a3908c8STrond Myklebust struct inode *inode = &nfsi->vfs_inode; 8273a3908c8STrond Myklebust 8283a3908c8STrond Myklebust nfs_init_cinfo_from_inode(&cinfo, inode); 8293a3908c8STrond Myklebust 8303a3908c8STrond Myklebust /* search through pnfs commit lists */ 8313a3908c8STrond Myklebust freq = pnfs_search_commit_reqs(inode, &cinfo, page); 8323a3908c8STrond Myklebust if (freq) 8333a3908c8STrond Myklebust return freq->wb_head; 8343a3908c8STrond Myklebust 8353a3908c8STrond Myklebust /* Linearly search the commit list for the correct request */ 8363a3908c8STrond Myklebust list_for_each_entry_safe(freq, t, &cinfo.mds->list, wb_list) { 8373a3908c8STrond Myklebust if (freq->wb_page == page) 8383a3908c8STrond Myklebust return freq->wb_head; 8393a3908c8STrond Myklebust } 8403a3908c8STrond Myklebust 8413a3908c8STrond Myklebust return NULL; 8423a3908c8STrond Myklebust } 8433a3908c8STrond Myklebust 8448dd37758STrond Myklebust /** 84586d80f97STrond Myklebust * nfs_request_add_commit_list_locked - add request to a commit list 84686d80f97STrond Myklebust * @req: pointer to a struct nfs_page 84786d80f97STrond Myklebust * @dst: commit list head 84886d80f97STrond Myklebust * @cinfo: holds list lock and accounting info 84986d80f97STrond Myklebust * 85086d80f97STrond Myklebust * This sets the PG_CLEAN bit, updates the cinfo count of 85186d80f97STrond Myklebust * number of outstanding requests requiring a commit as well as 85286d80f97STrond Myklebust * the MM page stats. 85386d80f97STrond Myklebust * 854e824f99aSTrond Myklebust * The caller must hold NFS_I(cinfo->inode)->commit_mutex, and the 855e824f99aSTrond Myklebust * nfs_page lock. 85686d80f97STrond Myklebust */ 85786d80f97STrond Myklebust void 85886d80f97STrond Myklebust nfs_request_add_commit_list_locked(struct nfs_page *req, struct list_head *dst, 85986d80f97STrond Myklebust struct nfs_commit_info *cinfo) 86086d80f97STrond Myklebust { 86186d80f97STrond Myklebust set_bit(PG_CLEAN, &req->wb_flags); 86286d80f97STrond Myklebust nfs_list_add_request(req, dst); 8635cb953d4STrond Myklebust atomic_long_inc(&cinfo->mds->ncommit); 86486d80f97STrond Myklebust } 86586d80f97STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_add_commit_list_locked); 86686d80f97STrond Myklebust 86786d80f97STrond Myklebust /** 8688dd37758STrond Myklebust * nfs_request_add_commit_list - add request to a commit list 8698dd37758STrond Myklebust * @req: pointer to a struct nfs_page 870ea2cf228SFred Isaman * @dst: commit list head 871ea2cf228SFred Isaman * @cinfo: holds list lock and accounting info 8728dd37758STrond Myklebust * 873ea2cf228SFred Isaman * This sets the PG_CLEAN bit, updates the cinfo count of 8748dd37758STrond Myklebust * number of outstanding requests requiring a commit as well as 8758dd37758STrond Myklebust * the MM page stats. 8768dd37758STrond Myklebust * 877ea2cf228SFred Isaman * The caller must _not_ hold the cinfo->lock, but must be 8788dd37758STrond Myklebust * holding the nfs_page lock. 8798dd37758STrond Myklebust */ 8808dd37758STrond Myklebust void 8816272dcc6SAnna Schumaker nfs_request_add_commit_list(struct nfs_page *req, struct nfs_commit_info *cinfo) 8828dd37758STrond Myklebust { 883e824f99aSTrond Myklebust mutex_lock(&NFS_I(cinfo->inode)->commit_mutex); 8846272dcc6SAnna Schumaker nfs_request_add_commit_list_locked(req, &cinfo->mds->list, cinfo); 885e824f99aSTrond Myklebust mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex); 88667911c8fSAnna Schumaker if (req->wb_page) 88786d80f97STrond Myklebust nfs_mark_page_unstable(req->wb_page, cinfo); 8888dd37758STrond Myklebust } 8898dd37758STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_add_commit_list); 8908dd37758STrond Myklebust 8918dd37758STrond Myklebust /** 8928dd37758STrond Myklebust * nfs_request_remove_commit_list - Remove request from a commit list 8938dd37758STrond Myklebust * @req: pointer to a nfs_page 894ea2cf228SFred Isaman * @cinfo: holds list lock and accounting info 8958dd37758STrond Myklebust * 896ea2cf228SFred Isaman * This clears the PG_CLEAN bit, and updates the cinfo's count of 8978dd37758STrond Myklebust * number of outstanding requests requiring a commit 8988dd37758STrond Myklebust * It does not update the MM page stats. 8998dd37758STrond Myklebust * 900ea2cf228SFred Isaman * The caller _must_ hold the cinfo->lock and the nfs_page lock. 9018dd37758STrond Myklebust */ 9028dd37758STrond Myklebust void 903ea2cf228SFred Isaman nfs_request_remove_commit_list(struct nfs_page *req, 904ea2cf228SFred Isaman struct nfs_commit_info *cinfo) 9058dd37758STrond Myklebust { 9068dd37758STrond Myklebust if (!test_and_clear_bit(PG_CLEAN, &(req)->wb_flags)) 9078dd37758STrond Myklebust return; 9088dd37758STrond Myklebust nfs_list_remove_request(req); 9095cb953d4STrond Myklebust atomic_long_dec(&cinfo->mds->ncommit); 9108dd37758STrond Myklebust } 9118dd37758STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_remove_commit_list); 9128dd37758STrond Myklebust 913ea2cf228SFred Isaman static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo, 914ea2cf228SFred Isaman struct inode *inode) 915ea2cf228SFred Isaman { 916fe238e60SDave Wysochanski cinfo->inode = inode; 917ea2cf228SFred Isaman cinfo->mds = &NFS_I(inode)->commit_info; 918ea2cf228SFred Isaman cinfo->ds = pnfs_get_ds_info(inode); 919b359f9d0SFred Isaman cinfo->dreq = NULL; 920f453a54aSFred Isaman cinfo->completion_ops = &nfs_commit_completion_ops; 921ea2cf228SFred Isaman } 922ea2cf228SFred Isaman 923ea2cf228SFred Isaman void nfs_init_cinfo(struct nfs_commit_info *cinfo, 924ea2cf228SFred Isaman struct inode *inode, 925ea2cf228SFred Isaman struct nfs_direct_req *dreq) 926ea2cf228SFred Isaman { 9271763da12SFred Isaman if (dreq) 9281763da12SFred Isaman nfs_init_cinfo_from_dreq(cinfo, dreq); 9291763da12SFred Isaman else 930ea2cf228SFred Isaman nfs_init_cinfo_from_inode(cinfo, inode); 931ea2cf228SFred Isaman } 932ea2cf228SFred Isaman EXPORT_SYMBOL_GPL(nfs_init_cinfo); 9338dd37758STrond Myklebust 9341da177e4SLinus Torvalds /* 9351da177e4SLinus Torvalds * Add a request to the inode's commit list. 9361da177e4SLinus Torvalds */ 9371763da12SFred Isaman void 938ea2cf228SFred Isaman nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg, 939b57ff130SWeston Andros Adamson struct nfs_commit_info *cinfo, u32 ds_commit_idx) 9401da177e4SLinus Torvalds { 941b57ff130SWeston Andros Adamson if (pnfs_mark_request_commit(req, lseg, cinfo, ds_commit_idx)) 9428dd37758STrond Myklebust return; 9436272dcc6SAnna Schumaker nfs_request_add_commit_list(req, cinfo); 9441da177e4SLinus Torvalds } 9458e821cadSTrond Myklebust 946d6d6dc7cSFred Isaman static void 947d6d6dc7cSFred Isaman nfs_clear_page_commit(struct page *page) 948e468bae9STrond Myklebust { 94911fb9989SMel Gorman dec_node_page_state(page, NR_UNSTABLE_NFS); 95093f78d88STejun Heo dec_wb_stat(&inode_to_bdi(page_file_mapping(page)->host)->wb, 95193f78d88STejun Heo WB_RECLAIMABLE); 952e468bae9STrond Myklebust } 953d6d6dc7cSFred Isaman 954b5bab9bfSTrond Myklebust /* Called holding the request lock on @req */ 9558dd37758STrond Myklebust static void 956d6d6dc7cSFred Isaman nfs_clear_request_commit(struct nfs_page *req) 957d6d6dc7cSFred Isaman { 9588dd37758STrond Myklebust if (test_bit(PG_CLEAN, &req->wb_flags)) { 9592b0143b5SDavid Howells struct inode *inode = d_inode(req->wb_context->dentry); 960ea2cf228SFred Isaman struct nfs_commit_info cinfo; 961d6d6dc7cSFred Isaman 962ea2cf228SFred Isaman nfs_init_cinfo_from_inode(&cinfo, inode); 963e824f99aSTrond Myklebust mutex_lock(&NFS_I(inode)->commit_mutex); 964ea2cf228SFred Isaman if (!pnfs_clear_request_commit(req, &cinfo)) { 965ea2cf228SFred Isaman nfs_request_remove_commit_list(req, &cinfo); 966d6d6dc7cSFred Isaman } 967e824f99aSTrond Myklebust mutex_unlock(&NFS_I(inode)->commit_mutex); 9688dd37758STrond Myklebust nfs_clear_page_commit(req->wb_page); 9698dd37758STrond Myklebust } 970e468bae9STrond Myklebust } 971e468bae9STrond Myklebust 972d45f60c6SWeston Andros Adamson int nfs_write_need_commit(struct nfs_pgio_header *hdr) 9738e821cadSTrond Myklebust { 974c65e6254SWeston Andros Adamson if (hdr->verf.committed == NFS_DATA_SYNC) 975d45f60c6SWeston Andros Adamson return hdr->lseg == NULL; 976c65e6254SWeston Andros Adamson return hdr->verf.committed != NFS_FILE_SYNC; 9778e821cadSTrond Myklebust } 9788e821cadSTrond Myklebust 979919e3bd9STrond Myklebust static void nfs_async_write_init(struct nfs_pgio_header *hdr) 980919e3bd9STrond Myklebust { 981919e3bd9STrond Myklebust nfs_io_completion_get(hdr->io_completion); 982919e3bd9STrond Myklebust } 983919e3bd9STrond Myklebust 984061ae2edSFred Isaman static void nfs_write_completion(struct nfs_pgio_header *hdr) 9856c75dc0dSFred Isaman { 986ea2cf228SFred Isaman struct nfs_commit_info cinfo; 9876c75dc0dSFred Isaman unsigned long bytes = 0; 9886c75dc0dSFred Isaman 9896c75dc0dSFred Isaman if (test_bit(NFS_IOHDR_REDO, &hdr->flags)) 9906c75dc0dSFred Isaman goto out; 991ea2cf228SFred Isaman nfs_init_cinfo_from_inode(&cinfo, hdr->inode); 9926c75dc0dSFred Isaman while (!list_empty(&hdr->pages)) { 9936c75dc0dSFred Isaman struct nfs_page *req = nfs_list_entry(hdr->pages.next); 9946c75dc0dSFred Isaman 9956c75dc0dSFred Isaman bytes += req->wb_bytes; 9966c75dc0dSFred Isaman nfs_list_remove_request(req); 9976c75dc0dSFred Isaman if (test_bit(NFS_IOHDR_ERROR, &hdr->flags) && 9986c75dc0dSFred Isaman (hdr->good_bytes < bytes)) { 999d1182b33STrond Myklebust nfs_set_pageerror(req->wb_page); 10006c75dc0dSFred Isaman nfs_context_set_write_error(req->wb_context, hdr->error); 10016c75dc0dSFred Isaman goto remove_req; 10026c75dc0dSFred Isaman } 1003c65e6254SWeston Andros Adamson if (nfs_write_need_commit(hdr)) { 1004f79d06f5SAnna Schumaker memcpy(&req->wb_verf, &hdr->verf.verifier, sizeof(req->wb_verf)); 1005b57ff130SWeston Andros Adamson nfs_mark_request_commit(req, hdr->lseg, &cinfo, 1006a7d42ddbSWeston Andros Adamson hdr->pgio_mirror_idx); 10076c75dc0dSFred Isaman goto next; 10086c75dc0dSFred Isaman } 10096c75dc0dSFred Isaman remove_req: 10106c75dc0dSFred Isaman nfs_inode_remove_request(req); 10116c75dc0dSFred Isaman next: 101220633f04SWeston Andros Adamson nfs_end_page_writeback(req); 10133aff4ebbSTrond Myklebust nfs_release_request(req); 10146c75dc0dSFred Isaman } 10156c75dc0dSFred Isaman out: 1016919e3bd9STrond Myklebust nfs_io_completion_put(hdr->io_completion); 10176c75dc0dSFred Isaman hdr->release(hdr); 10186c75dc0dSFred Isaman } 10196c75dc0dSFred Isaman 1020ce59515cSAnna Schumaker unsigned long 1021ea2cf228SFred Isaman nfs_reqs_to_commit(struct nfs_commit_info *cinfo) 1022fb8a1f11STrond Myklebust { 10235cb953d4STrond Myklebust return atomic_long_read(&cinfo->mds->ncommit); 1024fb8a1f11STrond Myklebust } 1025fb8a1f11STrond Myklebust 1026e824f99aSTrond Myklebust /* NFS_I(cinfo->inode)->commit_mutex held by caller */ 10271763da12SFred Isaman int 1028ea2cf228SFred Isaman nfs_scan_commit_list(struct list_head *src, struct list_head *dst, 1029ea2cf228SFred Isaman struct nfs_commit_info *cinfo, int max) 1030d6d6dc7cSFred Isaman { 1031137da553STrond Myklebust struct nfs_page *req, *tmp; 1032d6d6dc7cSFred Isaman int ret = 0; 1033d6d6dc7cSFred Isaman 1034137da553STrond Myklebust restart: 1035137da553STrond Myklebust list_for_each_entry_safe(req, tmp, src, wb_list) { 10367ad84aa9STrond Myklebust kref_get(&req->wb_kref); 10372ce209c4STrond Myklebust if (!nfs_lock_request(req)) { 10382ce209c4STrond Myklebust int status; 1039137da553STrond Myklebust 1040137da553STrond Myklebust /* Prevent deadlock with nfs_lock_and_join_requests */ 1041137da553STrond Myklebust if (!list_empty(dst)) { 1042137da553STrond Myklebust nfs_release_request(req); 1043137da553STrond Myklebust continue; 1044137da553STrond Myklebust } 1045137da553STrond Myklebust /* Ensure we make progress to prevent livelock */ 10462ce209c4STrond Myklebust mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex); 10472ce209c4STrond Myklebust status = nfs_wait_on_request(req); 10482ce209c4STrond Myklebust nfs_release_request(req); 10492ce209c4STrond Myklebust mutex_lock(&NFS_I(cinfo->inode)->commit_mutex); 10502ce209c4STrond Myklebust if (status < 0) 10512ce209c4STrond Myklebust break; 1052137da553STrond Myklebust goto restart; 10532ce209c4STrond Myklebust } 1054ea2cf228SFred Isaman nfs_request_remove_commit_list(req, cinfo); 10555d2a9d9dSTrond Myklebust clear_bit(PG_COMMIT_TO_DS, &req->wb_flags); 10568dd37758STrond Myklebust nfs_list_add_request(req, dst); 1057d6d6dc7cSFred Isaman ret++; 10581763da12SFred Isaman if ((ret == max) && !cinfo->dreq) 1059d6d6dc7cSFred Isaman break; 1060e824f99aSTrond Myklebust cond_resched(); 1061d6d6dc7cSFred Isaman } 1062d6d6dc7cSFred Isaman return ret; 1063d6d6dc7cSFred Isaman } 10645d2a9d9dSTrond Myklebust EXPORT_SYMBOL_GPL(nfs_scan_commit_list); 1065d6d6dc7cSFred Isaman 10661da177e4SLinus Torvalds /* 10671da177e4SLinus Torvalds * nfs_scan_commit - Scan an inode for commit requests 10681da177e4SLinus Torvalds * @inode: NFS inode to scan 1069ea2cf228SFred Isaman * @dst: mds destination list 1070ea2cf228SFred Isaman * @cinfo: mds and ds lists of reqs ready to commit 10711da177e4SLinus Torvalds * 10721da177e4SLinus Torvalds * Moves requests from the inode's 'commit' request list. 10731da177e4SLinus Torvalds * The requests are *not* checked to ensure that they form a contiguous set. 10741da177e4SLinus Torvalds */ 10751763da12SFred Isaman int 1076ea2cf228SFred Isaman nfs_scan_commit(struct inode *inode, struct list_head *dst, 1077ea2cf228SFred Isaman struct nfs_commit_info *cinfo) 10781da177e4SLinus Torvalds { 1079d6d6dc7cSFred Isaman int ret = 0; 1080fb8a1f11STrond Myklebust 10815cb953d4STrond Myklebust if (!atomic_long_read(&cinfo->mds->ncommit)) 10825cb953d4STrond Myklebust return 0; 1083e824f99aSTrond Myklebust mutex_lock(&NFS_I(cinfo->inode)->commit_mutex); 10845cb953d4STrond Myklebust if (atomic_long_read(&cinfo->mds->ncommit) > 0) { 10858dd37758STrond Myklebust const int max = INT_MAX; 1086d6d6dc7cSFred Isaman 1087ea2cf228SFred Isaman ret = nfs_scan_commit_list(&cinfo->mds->list, dst, 1088ea2cf228SFred Isaman cinfo, max); 1089ea2cf228SFred Isaman ret += pnfs_scan_commit_lists(inode, cinfo, max - ret); 1090d6d6dc7cSFred Isaman } 1091e824f99aSTrond Myklebust mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex); 1092ff778d02STrond Myklebust return ret; 10931da177e4SLinus Torvalds } 1094d6d6dc7cSFred Isaman 10951da177e4SLinus Torvalds /* 1096e7d39069STrond Myklebust * Search for an existing write request, and attempt to update 1097e7d39069STrond Myklebust * it to reflect a new dirty region on a given page. 10981da177e4SLinus Torvalds * 1099e7d39069STrond Myklebust * If the attempt fails, then the existing request is flushed out 1100e7d39069STrond Myklebust * to disk. 11011da177e4SLinus Torvalds */ 1102e7d39069STrond Myklebust static struct nfs_page *nfs_try_to_update_request(struct inode *inode, 1103e7d39069STrond Myklebust struct page *page, 1104e7d39069STrond Myklebust unsigned int offset, 1105e7d39069STrond Myklebust unsigned int bytes) 11061da177e4SLinus Torvalds { 1107e7d39069STrond Myklebust struct nfs_page *req; 1108e7d39069STrond Myklebust unsigned int rqend; 1109e7d39069STrond Myklebust unsigned int end; 11101da177e4SLinus Torvalds int error; 1111277459d2STrond Myklebust 1112e7d39069STrond Myklebust end = offset + bytes; 1113e7d39069STrond Myklebust 1114f6032f21STrond Myklebust req = nfs_lock_and_join_requests(page); 1115f6032f21STrond Myklebust if (IS_ERR_OR_NULL(req)) 1116f6032f21STrond Myklebust return req; 11172bfc6e56SWeston Andros Adamson 1118e7d39069STrond Myklebust rqend = req->wb_offset + req->wb_bytes; 1119e7d39069STrond Myklebust /* 1120e7d39069STrond Myklebust * Tell the caller to flush out the request if 1121e7d39069STrond Myklebust * the offsets are non-contiguous. 1122e7d39069STrond Myklebust * Note: nfs_flush_incompatible() will already 1123e7d39069STrond Myklebust * have flushed out requests having wrong owners. 1124e7d39069STrond Myklebust */ 1125f6032f21STrond Myklebust if (offset > rqend || end < req->wb_offset) 1126e7d39069STrond Myklebust goto out_flushme; 1127e7d39069STrond Myklebust 11281da177e4SLinus Torvalds /* Okay, the request matches. Update the region */ 11291da177e4SLinus Torvalds if (offset < req->wb_offset) { 11301da177e4SLinus Torvalds req->wb_offset = offset; 11311da177e4SLinus Torvalds req->wb_pgbase = offset; 11321da177e4SLinus Torvalds } 11331da177e4SLinus Torvalds if (end > rqend) 11341da177e4SLinus Torvalds req->wb_bytes = end - req->wb_offset; 1135e7d39069STrond Myklebust else 1136e7d39069STrond Myklebust req->wb_bytes = rqend - req->wb_offset; 1137e7d39069STrond Myklebust return req; 1138e7d39069STrond Myklebust out_flushme: 1139f6032f21STrond Myklebust /* 1140f6032f21STrond Myklebust * Note: we mark the request dirty here because 1141f6032f21STrond Myklebust * nfs_lock_and_join_requests() cannot preserve 1142f6032f21STrond Myklebust * commit flags, so we have to replay the write. 1143f6032f21STrond Myklebust */ 1144f6032f21STrond Myklebust nfs_mark_request_dirty(req); 1145f6032f21STrond Myklebust nfs_unlock_and_release_request(req); 1146e7d39069STrond Myklebust error = nfs_wb_page(inode, page); 1147f6032f21STrond Myklebust return (error < 0) ? ERR_PTR(error) : NULL; 1148e7d39069STrond Myklebust } 11491da177e4SLinus Torvalds 1150e7d39069STrond Myklebust /* 1151e7d39069STrond Myklebust * Try to update an existing write request, or create one if there is none. 1152e7d39069STrond Myklebust * 1153e7d39069STrond Myklebust * Note: Should always be called with the Page Lock held to prevent races 1154e7d39069STrond Myklebust * if we have to add a new request. Also assumes that the caller has 1155e7d39069STrond Myklebust * already called nfs_flush_incompatible() if necessary. 1156e7d39069STrond Myklebust */ 1157e7d39069STrond Myklebust static struct nfs_page * nfs_setup_write_request(struct nfs_open_context* ctx, 1158e7d39069STrond Myklebust struct page *page, unsigned int offset, unsigned int bytes) 1159e7d39069STrond Myklebust { 1160d56b4ddfSMel Gorman struct inode *inode = page_file_mapping(page)->host; 1161e7d39069STrond Myklebust struct nfs_page *req; 1162e7d39069STrond Myklebust 1163e7d39069STrond Myklebust req = nfs_try_to_update_request(inode, page, offset, bytes); 1164e7d39069STrond Myklebust if (req != NULL) 1165e7d39069STrond Myklebust goto out; 11662bfc6e56SWeston Andros Adamson req = nfs_create_request(ctx, page, NULL, offset, bytes); 1167e7d39069STrond Myklebust if (IS_ERR(req)) 1168e7d39069STrond Myklebust goto out; 1169d6d6dc7cSFred Isaman nfs_inode_add_request(inode, req); 1170efc91ed0STrond Myklebust out: 117161e930a9STrond Myklebust return req; 11721da177e4SLinus Torvalds } 11731da177e4SLinus Torvalds 1174e7d39069STrond Myklebust static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page, 1175e7d39069STrond Myklebust unsigned int offset, unsigned int count) 1176e7d39069STrond Myklebust { 1177e7d39069STrond Myklebust struct nfs_page *req; 1178e7d39069STrond Myklebust 1179e7d39069STrond Myklebust req = nfs_setup_write_request(ctx, page, offset, count); 1180e7d39069STrond Myklebust if (IS_ERR(req)) 1181e7d39069STrond Myklebust return PTR_ERR(req); 1182e7d39069STrond Myklebust /* Update file length */ 1183e7d39069STrond Myklebust nfs_grow_file(page, offset, count); 1184d72ddcbaSWeston Andros Adamson nfs_mark_uptodate(req); 1185a6305ddbSTrond Myklebust nfs_mark_request_dirty(req); 11861d1afcbcSTrond Myklebust nfs_unlock_and_release_request(req); 1187e7d39069STrond Myklebust return 0; 1188e7d39069STrond Myklebust } 1189e7d39069STrond Myklebust 11901da177e4SLinus Torvalds int nfs_flush_incompatible(struct file *file, struct page *page) 11911da177e4SLinus Torvalds { 1192cd3758e3STrond Myklebust struct nfs_open_context *ctx = nfs_file_open_context(file); 11932a369153STrond Myklebust struct nfs_lock_context *l_ctx; 1194bd61e0a9SJeff Layton struct file_lock_context *flctx = file_inode(file)->i_flctx; 11951da177e4SLinus Torvalds struct nfs_page *req; 11961a54533eSTrond Myklebust int do_flush, status; 11971da177e4SLinus Torvalds /* 11981da177e4SLinus Torvalds * Look for a request corresponding to this page. If there 11991da177e4SLinus Torvalds * is one, and it belongs to another file, we flush it out 12001da177e4SLinus Torvalds * before we try to copy anything into the page. Do this 12011da177e4SLinus Torvalds * due to the lack of an ACCESS-type call in NFSv2. 12021da177e4SLinus Torvalds * Also do the same if we find a request from an existing 12031da177e4SLinus Torvalds * dropped page. 12041da177e4SLinus Torvalds */ 12051a54533eSTrond Myklebust do { 120684d3a9a9SWeston Andros Adamson req = nfs_page_find_head_request(page); 12071a54533eSTrond Myklebust if (req == NULL) 12081a54533eSTrond Myklebust return 0; 12092a369153STrond Myklebust l_ctx = req->wb_lock_context; 1210138a2935STrond Myklebust do_flush = req->wb_page != page || 1211138a2935STrond Myklebust !nfs_match_open_context(req->wb_context, ctx); 1212bd61e0a9SJeff Layton if (l_ctx && flctx && 1213bd61e0a9SJeff Layton !(list_empty_careful(&flctx->flc_posix) && 1214bd61e0a9SJeff Layton list_empty_careful(&flctx->flc_flock))) { 1215d51fdb87SNeilBrown do_flush |= l_ctx->lockowner != current->files; 12165263e31eSJeff Layton } 12171da177e4SLinus Torvalds nfs_release_request(req); 12181a54533eSTrond Myklebust if (!do_flush) 12191a54533eSTrond Myklebust return 0; 1220d56b4ddfSMel Gorman status = nfs_wb_page(page_file_mapping(page)->host, page); 12211a54533eSTrond Myklebust } while (status == 0); 12221a54533eSTrond Myklebust return status; 12231da177e4SLinus Torvalds } 12241da177e4SLinus Torvalds 12251da177e4SLinus Torvalds /* 1226dc24826bSAndy Adamson * Avoid buffered writes when a open context credential's key would 1227dc24826bSAndy Adamson * expire soon. 1228dc24826bSAndy Adamson * 1229dc24826bSAndy Adamson * Returns -EACCES if the key will expire within RPC_KEY_EXPIRE_FAIL. 1230dc24826bSAndy Adamson * 1231dc24826bSAndy Adamson * Return 0 and set a credential flag which triggers the inode to flush 1232dc24826bSAndy Adamson * and performs NFS_FILE_SYNC writes if the key will expired within 1233dc24826bSAndy Adamson * RPC_KEY_EXPIRE_TIMEO. 1234dc24826bSAndy Adamson */ 1235dc24826bSAndy Adamson int 1236dc24826bSAndy Adamson nfs_key_timeout_notify(struct file *filp, struct inode *inode) 1237dc24826bSAndy Adamson { 1238dc24826bSAndy Adamson struct nfs_open_context *ctx = nfs_file_open_context(filp); 1239dc24826bSAndy Adamson struct rpc_auth *auth = NFS_SERVER(inode)->client->cl_auth; 1240dc24826bSAndy Adamson 1241dc24826bSAndy Adamson return rpcauth_key_timeout_notify(auth, ctx->cred); 1242dc24826bSAndy Adamson } 1243dc24826bSAndy Adamson 1244dc24826bSAndy Adamson /* 1245dc24826bSAndy Adamson * Test if the open context credential key is marked to expire soon. 1246dc24826bSAndy Adamson */ 1247ce52914eSScott Mayhew bool nfs_ctx_key_to_expire(struct nfs_open_context *ctx, struct inode *inode) 1248dc24826bSAndy Adamson { 1249ce52914eSScott Mayhew struct rpc_auth *auth = NFS_SERVER(inode)->client->cl_auth; 1250ce52914eSScott Mayhew 1251ce52914eSScott Mayhew return rpcauth_cred_key_to_expire(auth, ctx->cred); 1252dc24826bSAndy Adamson } 1253dc24826bSAndy Adamson 1254dc24826bSAndy Adamson /* 12555d47a356STrond Myklebust * If the page cache is marked as unsafe or invalid, then we can't rely on 12565d47a356STrond Myklebust * the PageUptodate() flag. In this case, we will need to turn off 12575d47a356STrond Myklebust * write optimisations that depend on the page contents being correct. 12585d47a356STrond Myklebust */ 12598d197a56STrond Myklebust static bool nfs_write_pageuptodate(struct page *page, struct inode *inode) 12605d47a356STrond Myklebust { 1261d529ef83SJeff Layton struct nfs_inode *nfsi = NFS_I(inode); 1262d529ef83SJeff Layton 12638d197a56STrond Myklebust if (nfs_have_delegated_attributes(inode)) 12648d197a56STrond Myklebust goto out; 126518dd78c4SScott Mayhew if (nfsi->cache_validity & NFS_INO_REVAL_PAGECACHE) 1266d529ef83SJeff Layton return false; 12674db72b40SJeff Layton smp_rmb(); 1268d529ef83SJeff Layton if (test_bit(NFS_INO_INVALIDATING, &nfsi->flags)) 12698d197a56STrond Myklebust return false; 12708d197a56STrond Myklebust out: 127118dd78c4SScott Mayhew if (nfsi->cache_validity & NFS_INO_INVALID_DATA) 127218dd78c4SScott Mayhew return false; 12738d197a56STrond Myklebust return PageUptodate(page) != 0; 12745d47a356STrond Myklebust } 12755d47a356STrond Myklebust 12765263e31eSJeff Layton static bool 12775263e31eSJeff Layton is_whole_file_wrlock(struct file_lock *fl) 12785263e31eSJeff Layton { 12795263e31eSJeff Layton return fl->fl_start == 0 && fl->fl_end == OFFSET_MAX && 12805263e31eSJeff Layton fl->fl_type == F_WRLCK; 12815263e31eSJeff Layton } 12825263e31eSJeff Layton 1283c7559663SScott Mayhew /* If we know the page is up to date, and we're not using byte range locks (or 1284c7559663SScott Mayhew * if we have the whole file locked for writing), it may be more efficient to 1285c7559663SScott Mayhew * extend the write to cover the entire page in order to avoid fragmentation 1286c7559663SScott Mayhew * inefficiencies. 1287c7559663SScott Mayhew * 1288263b4509SScott Mayhew * If the file is opened for synchronous writes then we can just skip the rest 1289263b4509SScott Mayhew * of the checks. 1290c7559663SScott Mayhew */ 1291c7559663SScott Mayhew static int nfs_can_extend_write(struct file *file, struct page *page, struct inode *inode) 1292c7559663SScott Mayhew { 12935263e31eSJeff Layton int ret; 12945263e31eSJeff Layton struct file_lock_context *flctx = inode->i_flctx; 12955263e31eSJeff Layton struct file_lock *fl; 12965263e31eSJeff Layton 1297c7559663SScott Mayhew if (file->f_flags & O_DSYNC) 1298c7559663SScott Mayhew return 0; 1299263b4509SScott Mayhew if (!nfs_write_pageuptodate(page, inode)) 1300263b4509SScott Mayhew return 0; 1301c7559663SScott Mayhew if (NFS_PROTO(inode)->have_delegation(inode, FMODE_WRITE)) 1302c7559663SScott Mayhew return 1; 1303bd61e0a9SJeff Layton if (!flctx || (list_empty_careful(&flctx->flc_flock) && 1304bd61e0a9SJeff Layton list_empty_careful(&flctx->flc_posix))) 13058fa4592aSTrond Myklebust return 1; 13065263e31eSJeff Layton 13075263e31eSJeff Layton /* Check to see if there are whole file write locks */ 13085263e31eSJeff Layton ret = 0; 13096109c850SJeff Layton spin_lock(&flctx->flc_lock); 1310bd61e0a9SJeff Layton if (!list_empty(&flctx->flc_posix)) { 1311bd61e0a9SJeff Layton fl = list_first_entry(&flctx->flc_posix, struct file_lock, 1312bd61e0a9SJeff Layton fl_list); 1313bd61e0a9SJeff Layton if (is_whole_file_wrlock(fl)) 13145263e31eSJeff Layton ret = 1; 1315bd61e0a9SJeff Layton } else if (!list_empty(&flctx->flc_flock)) { 13165263e31eSJeff Layton fl = list_first_entry(&flctx->flc_flock, struct file_lock, 13175263e31eSJeff Layton fl_list); 13185263e31eSJeff Layton if (fl->fl_type == F_WRLCK) 13195263e31eSJeff Layton ret = 1; 13205263e31eSJeff Layton } 13216109c850SJeff Layton spin_unlock(&flctx->flc_lock); 13225263e31eSJeff Layton return ret; 1323c7559663SScott Mayhew } 1324c7559663SScott Mayhew 13255d47a356STrond Myklebust /* 13261da177e4SLinus Torvalds * Update and possibly write a cached page of an NFS file. 13271da177e4SLinus Torvalds * 13281da177e4SLinus Torvalds * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad 13291da177e4SLinus Torvalds * things with a page scheduled for an RPC call (e.g. invalidate it). 13301da177e4SLinus Torvalds */ 13311da177e4SLinus Torvalds int nfs_updatepage(struct file *file, struct page *page, 13321da177e4SLinus Torvalds unsigned int offset, unsigned int count) 13331da177e4SLinus Torvalds { 1334cd3758e3STrond Myklebust struct nfs_open_context *ctx = nfs_file_open_context(file); 1335d56b4ddfSMel Gorman struct inode *inode = page_file_mapping(page)->host; 13361da177e4SLinus Torvalds int status = 0; 13371da177e4SLinus Torvalds 133891d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE); 133991d5b470SChuck Lever 13406de1472fSAl Viro dprintk("NFS: nfs_updatepage(%pD2 %d@%lld)\n", 13416de1472fSAl Viro file, count, (long long)(page_file_offset(page) + offset)); 13421da177e4SLinus Torvalds 1343149a4fddSBenjamin Coddington if (!count) 1344149a4fddSBenjamin Coddington goto out; 1345149a4fddSBenjamin Coddington 1346c7559663SScott Mayhew if (nfs_can_extend_write(file, page, inode)) { 134749a70f27STrond Myklebust count = max(count + offset, nfs_page_length(page)); 13481da177e4SLinus Torvalds offset = 0; 13491da177e4SLinus Torvalds } 13501da177e4SLinus Torvalds 1351e21195a7STrond Myklebust status = nfs_writepage_setup(ctx, page, offset, count); 135203fa9e84STrond Myklebust if (status < 0) 135303fa9e84STrond Myklebust nfs_set_pageerror(page); 135459b7c05fSTrond Myklebust else 135559b7c05fSTrond Myklebust __set_page_dirty_nobuffers(page); 1356149a4fddSBenjamin Coddington out: 135748186c7dSChuck Lever dprintk("NFS: nfs_updatepage returns %d (isize %lld)\n", 13581da177e4SLinus Torvalds status, (long long)i_size_read(inode)); 13591da177e4SLinus Torvalds return status; 13601da177e4SLinus Torvalds } 13611da177e4SLinus Torvalds 13623ff7576dSTrond Myklebust static int flush_task_priority(int how) 13631da177e4SLinus Torvalds { 13641da177e4SLinus Torvalds switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) { 13651da177e4SLinus Torvalds case FLUSH_HIGHPRI: 13661da177e4SLinus Torvalds return RPC_PRIORITY_HIGH; 13671da177e4SLinus Torvalds case FLUSH_LOWPRI: 13681da177e4SLinus Torvalds return RPC_PRIORITY_LOW; 13691da177e4SLinus Torvalds } 13701da177e4SLinus Torvalds return RPC_PRIORITY_NORMAL; 13711da177e4SLinus Torvalds } 13721da177e4SLinus Torvalds 1373d45f60c6SWeston Andros Adamson static void nfs_initiate_write(struct nfs_pgio_header *hdr, 1374d45f60c6SWeston Andros Adamson struct rpc_message *msg, 1375abde71f4STom Haynes const struct nfs_rpc_ops *rpc_ops, 13761ed26f33SAnna Schumaker struct rpc_task_setup *task_setup_data, int how) 13771da177e4SLinus Torvalds { 13783ff7576dSTrond Myklebust int priority = flush_task_priority(how); 13791da177e4SLinus Torvalds 13801ed26f33SAnna Schumaker task_setup_data->priority = priority; 1381abde71f4STom Haynes rpc_ops->write_setup(hdr, msg); 1382d138d5d1SAndy Adamson 1383abde71f4STom Haynes nfs4_state_protect_write(NFS_SERVER(hdr->inode)->nfs_client, 1384d45f60c6SWeston Andros Adamson &task_setup_data->rpc_client, msg, hdr); 1385275acaafSTrond Myklebust } 1386275acaafSTrond Myklebust 13876d884e8fSFred /* If a nfs_flush_* function fails, it should remove reqs from @head and 13886d884e8fSFred * call this on each, which will prepare them to be retried on next 13896d884e8fSFred * writeback using standard nfs. 13906d884e8fSFred */ 13916d884e8fSFred static void nfs_redirty_request(struct nfs_page *req) 13926d884e8fSFred { 13936d884e8fSFred nfs_mark_request_dirty(req); 1394c7070113STrond Myklebust set_bit(NFS_CONTEXT_RESEND_WRITES, &req->wb_context->flags); 139520633f04SWeston Andros Adamson nfs_end_page_writeback(req); 13963aff4ebbSTrond Myklebust nfs_release_request(req); 13976d884e8fSFred } 13986d884e8fSFred 1399061ae2edSFred Isaman static void nfs_async_write_error(struct list_head *head) 14006c75dc0dSFred Isaman { 14016c75dc0dSFred Isaman struct nfs_page *req; 14026c75dc0dSFred Isaman 14036c75dc0dSFred Isaman while (!list_empty(head)) { 14046c75dc0dSFred Isaman req = nfs_list_entry(head->next); 14056c75dc0dSFred Isaman nfs_list_remove_request(req); 14066c75dc0dSFred Isaman nfs_redirty_request(req); 14076c75dc0dSFred Isaman } 14086c75dc0dSFred Isaman } 14096c75dc0dSFred Isaman 1410dc602dd7STrond Myklebust static void nfs_async_write_reschedule_io(struct nfs_pgio_header *hdr) 1411dc602dd7STrond Myklebust { 1412dc602dd7STrond Myklebust nfs_async_write_error(&hdr->pages); 1413dc602dd7STrond Myklebust } 1414dc602dd7STrond Myklebust 1415061ae2edSFred Isaman static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops = { 1416919e3bd9STrond Myklebust .init_hdr = nfs_async_write_init, 1417061ae2edSFred Isaman .error_cleanup = nfs_async_write_error, 1418061ae2edSFred Isaman .completion = nfs_write_completion, 1419dc602dd7STrond Myklebust .reschedule_io = nfs_async_write_reschedule_io, 1420061ae2edSFred Isaman }; 1421061ae2edSFred Isaman 142257208fa7SBryan Schumaker void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio, 1423a20c93e3SChristoph Hellwig struct inode *inode, int ioflags, bool force_mds, 1424061ae2edSFred Isaman const struct nfs_pgio_completion_ops *compl_ops) 14251751c363STrond Myklebust { 1426a20c93e3SChristoph Hellwig struct nfs_server *server = NFS_SERVER(inode); 142741d8d5b7SAnna Schumaker const struct nfs_pageio_ops *pg_ops = &nfs_pgio_rw_ops; 1428a20c93e3SChristoph Hellwig 1429a20c93e3SChristoph Hellwig #ifdef CONFIG_NFS_V4_1 1430a20c93e3SChristoph Hellwig if (server->pnfs_curr_ld && !force_mds) 1431a20c93e3SChristoph Hellwig pg_ops = server->pnfs_curr_ld->pg_write_ops; 1432a20c93e3SChristoph Hellwig #endif 14334a0de55cSAnna Schumaker nfs_pageio_init(pgio, inode, pg_ops, compl_ops, &nfs_rw_write_ops, 14343bde7afdSTrond Myklebust server->wsize, ioflags); 14351751c363STrond Myklebust } 1436ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_pageio_init_write); 14371751c363STrond Myklebust 1438dce81290STrond Myklebust void nfs_pageio_reset_write_mds(struct nfs_pageio_descriptor *pgio) 1439dce81290STrond Myklebust { 1440a7d42ddbSWeston Andros Adamson struct nfs_pgio_mirror *mirror; 1441a7d42ddbSWeston Andros Adamson 14426f29b9bbSKinglong Mee if (pgio->pg_ops && pgio->pg_ops->pg_cleanup) 14436f29b9bbSKinglong Mee pgio->pg_ops->pg_cleanup(pgio); 14446f29b9bbSKinglong Mee 144541d8d5b7SAnna Schumaker pgio->pg_ops = &nfs_pgio_rw_ops; 1446a7d42ddbSWeston Andros Adamson 1447a7d42ddbSWeston Andros Adamson nfs_pageio_stop_mirroring(pgio); 1448a7d42ddbSWeston Andros Adamson 1449a7d42ddbSWeston Andros Adamson mirror = &pgio->pg_mirrors[0]; 1450a7d42ddbSWeston Andros Adamson mirror->pg_bsize = NFS_SERVER(pgio->pg_inode)->wsize; 1451dce81290STrond Myklebust } 14521f945357STrond Myklebust EXPORT_SYMBOL_GPL(nfs_pageio_reset_write_mds); 1453dce81290STrond Myklebust 14541da177e4SLinus Torvalds 14550b7c0153SFred Isaman void nfs_commit_prepare(struct rpc_task *task, void *calldata) 14560b7c0153SFred Isaman { 14570b7c0153SFred Isaman struct nfs_commit_data *data = calldata; 14580b7c0153SFred Isaman 14590b7c0153SFred Isaman NFS_PROTO(data->inode)->commit_rpc_prepare(task, data); 14600b7c0153SFred Isaman } 14610b7c0153SFred Isaman 14621f2edbe3STrond Myklebust /* 14631f2edbe3STrond Myklebust * Special version of should_remove_suid() that ignores capabilities. 14641f2edbe3STrond Myklebust */ 14651f2edbe3STrond Myklebust static int nfs_should_remove_suid(const struct inode *inode) 14661f2edbe3STrond Myklebust { 14671f2edbe3STrond Myklebust umode_t mode = inode->i_mode; 14681f2edbe3STrond Myklebust int kill = 0; 1469788e7a89STrond Myklebust 14701f2edbe3STrond Myklebust /* suid always must be killed */ 14711f2edbe3STrond Myklebust if (unlikely(mode & S_ISUID)) 14721f2edbe3STrond Myklebust kill = ATTR_KILL_SUID; 14731f2edbe3STrond Myklebust 14741f2edbe3STrond Myklebust /* 14751f2edbe3STrond Myklebust * sgid without any exec bits is just a mandatory locking mark; leave 14761f2edbe3STrond Myklebust * it alone. If some exec bits are set, it's a real sgid; kill it. 14771f2edbe3STrond Myklebust */ 14781f2edbe3STrond Myklebust if (unlikely((mode & S_ISGID) && (mode & S_IXGRP))) 14791f2edbe3STrond Myklebust kill |= ATTR_KILL_SGID; 14801f2edbe3STrond Myklebust 14811f2edbe3STrond Myklebust if (unlikely(kill && S_ISREG(mode))) 14821f2edbe3STrond Myklebust return kill; 14831f2edbe3STrond Myklebust 14841f2edbe3STrond Myklebust return 0; 14851f2edbe3STrond Myklebust } 1486788e7a89STrond Myklebust 1487a08a8cd3STrond Myklebust static void nfs_writeback_check_extend(struct nfs_pgio_header *hdr, 1488a08a8cd3STrond Myklebust struct nfs_fattr *fattr) 1489a08a8cd3STrond Myklebust { 1490a08a8cd3STrond Myklebust struct nfs_pgio_args *argp = &hdr->args; 1491a08a8cd3STrond Myklebust struct nfs_pgio_res *resp = &hdr->res; 14922b83d3deSTrond Myklebust u64 size = argp->offset + resp->count; 1493a08a8cd3STrond Myklebust 1494a08a8cd3STrond Myklebust if (!(fattr->valid & NFS_ATTR_FATTR_SIZE)) 14952b83d3deSTrond Myklebust fattr->size = size; 14962b83d3deSTrond Myklebust if (nfs_size_to_loff_t(fattr->size) < i_size_read(hdr->inode)) { 14972b83d3deSTrond Myklebust fattr->valid &= ~NFS_ATTR_FATTR_SIZE; 1498a08a8cd3STrond Myklebust return; 14992b83d3deSTrond Myklebust } 15002b83d3deSTrond Myklebust if (size != fattr->size) 1501a08a8cd3STrond Myklebust return; 1502a08a8cd3STrond Myklebust /* Set attribute barrier */ 1503a08a8cd3STrond Myklebust nfs_fattr_set_barrier(fattr); 15042b83d3deSTrond Myklebust /* ...and update size */ 15052b83d3deSTrond Myklebust fattr->valid |= NFS_ATTR_FATTR_SIZE; 1506a08a8cd3STrond Myklebust } 1507a08a8cd3STrond Myklebust 1508a08a8cd3STrond Myklebust void nfs_writeback_update_inode(struct nfs_pgio_header *hdr) 1509a08a8cd3STrond Myklebust { 15102b83d3deSTrond Myklebust struct nfs_fattr *fattr = &hdr->fattr; 1511a08a8cd3STrond Myklebust struct inode *inode = hdr->inode; 1512a08a8cd3STrond Myklebust 1513a08a8cd3STrond Myklebust spin_lock(&inode->i_lock); 1514a08a8cd3STrond Myklebust nfs_writeback_check_extend(hdr, fattr); 1515a08a8cd3STrond Myklebust nfs_post_op_update_inode_force_wcc_locked(inode, fattr); 1516a08a8cd3STrond Myklebust spin_unlock(&inode->i_lock); 1517a08a8cd3STrond Myklebust } 1518a08a8cd3STrond Myklebust EXPORT_SYMBOL_GPL(nfs_writeback_update_inode); 1519a08a8cd3STrond Myklebust 15201da177e4SLinus Torvalds /* 15211da177e4SLinus Torvalds * This function is called when the WRITE call is complete. 15221da177e4SLinus Torvalds */ 1523d45f60c6SWeston Andros Adamson static int nfs_writeback_done(struct rpc_task *task, 1524d45f60c6SWeston Andros Adamson struct nfs_pgio_header *hdr, 15250eecb214SAnna Schumaker struct inode *inode) 15261da177e4SLinus Torvalds { 1527788e7a89STrond Myklebust int status; 15281da177e4SLinus Torvalds 1529f551e44fSChuck Lever /* 1530f551e44fSChuck Lever * ->write_done will attempt to use post-op attributes to detect 1531f551e44fSChuck Lever * conflicting writes by other clients. A strict interpretation 1532f551e44fSChuck Lever * of close-to-open would allow us to continue caching even if 1533f551e44fSChuck Lever * another writer had changed the file, but some applications 1534f551e44fSChuck Lever * depend on tighter cache coherency when writing. 1535f551e44fSChuck Lever */ 1536d45f60c6SWeston Andros Adamson status = NFS_PROTO(inode)->write_done(task, hdr); 1537788e7a89STrond Myklebust if (status != 0) 15380eecb214SAnna Schumaker return status; 1539d45f60c6SWeston Andros Adamson nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, hdr->res.count); 154091d5b470SChuck Lever 1541d45f60c6SWeston Andros Adamson if (hdr->res.verf->committed < hdr->args.stable && 1542d45f60c6SWeston Andros Adamson task->tk_status >= 0) { 15431da177e4SLinus Torvalds /* We tried a write call, but the server did not 15441da177e4SLinus Torvalds * commit data to stable storage even though we 15451da177e4SLinus Torvalds * requested it. 15461da177e4SLinus Torvalds * Note: There is a known bug in Tru64 < 5.0 in which 15471da177e4SLinus Torvalds * the server reports NFS_DATA_SYNC, but performs 15481da177e4SLinus Torvalds * NFS_FILE_SYNC. We therefore implement this checking 15491da177e4SLinus Torvalds * as a dprintk() in order to avoid filling syslog. 15501da177e4SLinus Torvalds */ 15511da177e4SLinus Torvalds static unsigned long complain; 15521da177e4SLinus Torvalds 1553a69aef14SFred Isaman /* Note this will print the MDS for a DS write */ 15541da177e4SLinus Torvalds if (time_before(complain, jiffies)) { 15551da177e4SLinus Torvalds dprintk("NFS: faulty NFS server %s:" 15561da177e4SLinus Torvalds " (committed = %d) != (stable = %d)\n", 1557cd841605SFred Isaman NFS_SERVER(inode)->nfs_client->cl_hostname, 1558d45f60c6SWeston Andros Adamson hdr->res.verf->committed, hdr->args.stable); 15591da177e4SLinus Torvalds complain = jiffies + 300 * HZ; 15601da177e4SLinus Torvalds } 15611da177e4SLinus Torvalds } 15621f2edbe3STrond Myklebust 15631f2edbe3STrond Myklebust /* Deal with the suid/sgid bit corner case */ 15641f2edbe3STrond Myklebust if (nfs_should_remove_suid(inode)) 15651f2edbe3STrond Myklebust nfs_mark_for_revalidate(inode); 15660eecb214SAnna Schumaker return 0; 15670eecb214SAnna Schumaker } 15680eecb214SAnna Schumaker 15690eecb214SAnna Schumaker /* 15700eecb214SAnna Schumaker * This function is called when the WRITE call is complete. 15710eecb214SAnna Schumaker */ 1572d45f60c6SWeston Andros Adamson static void nfs_writeback_result(struct rpc_task *task, 1573d45f60c6SWeston Andros Adamson struct nfs_pgio_header *hdr) 15740eecb214SAnna Schumaker { 1575d45f60c6SWeston Andros Adamson struct nfs_pgio_args *argp = &hdr->args; 1576d45f60c6SWeston Andros Adamson struct nfs_pgio_res *resp = &hdr->res; 15771f2edbe3STrond Myklebust 15781f2edbe3STrond Myklebust if (resp->count < argp->count) { 15791da177e4SLinus Torvalds static unsigned long complain; 15801da177e4SLinus Torvalds 15816c75dc0dSFred Isaman /* This a short write! */ 1582d45f60c6SWeston Andros Adamson nfs_inc_stats(hdr->inode, NFSIOS_SHORTWRITE); 158391d5b470SChuck Lever 15841da177e4SLinus Torvalds /* Has the server at least made some progress? */ 15856c75dc0dSFred Isaman if (resp->count == 0) { 15866c75dc0dSFred Isaman if (time_before(complain, jiffies)) { 15876c75dc0dSFred Isaman printk(KERN_WARNING 15886c75dc0dSFred Isaman "NFS: Server wrote zero bytes, expected %u.\n", 15896c75dc0dSFred Isaman argp->count); 15906c75dc0dSFred Isaman complain = jiffies + 300 * HZ; 15916c75dc0dSFred Isaman } 1592d45f60c6SWeston Andros Adamson nfs_set_pgio_error(hdr, -EIO, argp->offset); 15936c75dc0dSFred Isaman task->tk_status = -EIO; 15946c75dc0dSFred Isaman return; 15956c75dc0dSFred Isaman } 1596f8417b48SKinglong Mee 1597f8417b48SKinglong Mee /* For non rpc-based layout drivers, retry-through-MDS */ 1598f8417b48SKinglong Mee if (!task->tk_ops) { 1599f8417b48SKinglong Mee hdr->pnfs_error = -EAGAIN; 1600f8417b48SKinglong Mee return; 1601f8417b48SKinglong Mee } 1602f8417b48SKinglong Mee 16031da177e4SLinus Torvalds /* Was this an NFSv2 write or an NFSv3 stable write? */ 16041da177e4SLinus Torvalds if (resp->verf->committed != NFS_UNSTABLE) { 16051da177e4SLinus Torvalds /* Resend from where the server left off */ 1606d45f60c6SWeston Andros Adamson hdr->mds_offset += resp->count; 16071da177e4SLinus Torvalds argp->offset += resp->count; 16081da177e4SLinus Torvalds argp->pgbase += resp->count; 16091da177e4SLinus Torvalds argp->count -= resp->count; 16101da177e4SLinus Torvalds } else { 16111da177e4SLinus Torvalds /* Resend as a stable write in order to avoid 16121da177e4SLinus Torvalds * headaches in the case of a server crash. 16131da177e4SLinus Torvalds */ 16141da177e4SLinus Torvalds argp->stable = NFS_FILE_SYNC; 16151da177e4SLinus Torvalds } 1616d00c5d43STrond Myklebust rpc_restart_call_prepare(task); 16171da177e4SLinus Torvalds } 16181da177e4SLinus Torvalds } 16191da177e4SLinus Torvalds 1620af7cf057STrond Myklebust static int wait_on_commit(struct nfs_mds_commit_info *cinfo) 162171d0a611STrond Myklebust { 1622af7cf057STrond Myklebust return wait_on_atomic_t(&cinfo->rpcs_out, 1623af7cf057STrond Myklebust nfs_wait_atomic_killable, TASK_KILLABLE); 1624af7cf057STrond Myklebust } 1625af7cf057STrond Myklebust 1626af7cf057STrond Myklebust static void nfs_commit_begin(struct nfs_mds_commit_info *cinfo) 1627af7cf057STrond Myklebust { 1628af7cf057STrond Myklebust atomic_inc(&cinfo->rpcs_out); 1629af7cf057STrond Myklebust } 1630af7cf057STrond Myklebust 1631af7cf057STrond Myklebust static void nfs_commit_end(struct nfs_mds_commit_info *cinfo) 1632af7cf057STrond Myklebust { 1633af7cf057STrond Myklebust if (atomic_dec_and_test(&cinfo->rpcs_out)) 1634af7cf057STrond Myklebust wake_up_atomic_t(&cinfo->rpcs_out); 163571d0a611STrond Myklebust } 163671d0a611STrond Myklebust 16370b7c0153SFred Isaman void nfs_commitdata_release(struct nfs_commit_data *data) 16381da177e4SLinus Torvalds { 16390b7c0153SFred Isaman put_nfs_open_context(data->context); 16400b7c0153SFred Isaman nfs_commit_free(data); 16411da177e4SLinus Torvalds } 1642e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commitdata_release); 16431da177e4SLinus Torvalds 16440b7c0153SFred Isaman int nfs_initiate_commit(struct rpc_clnt *clnt, struct nfs_commit_data *data, 1645c36aae9aSPeng Tao const struct nfs_rpc_ops *nfs_ops, 16469ace33cdSFred Isaman const struct rpc_call_ops *call_ops, 16479f0ec176SAndy Adamson int how, int flags) 16481da177e4SLinus Torvalds { 164907737691STrond Myklebust struct rpc_task *task; 16509ace33cdSFred Isaman int priority = flush_task_priority(how); 1651bdc7f021STrond Myklebust struct rpc_message msg = { 1652bdc7f021STrond Myklebust .rpc_argp = &data->args, 1653bdc7f021STrond Myklebust .rpc_resp = &data->res, 16549ace33cdSFred Isaman .rpc_cred = data->cred, 1655bdc7f021STrond Myklebust }; 165684115e1cSTrond Myklebust struct rpc_task_setup task_setup_data = { 165707737691STrond Myklebust .task = &data->task, 16589ace33cdSFred Isaman .rpc_client = clnt, 1659bdc7f021STrond Myklebust .rpc_message = &msg, 16609ace33cdSFred Isaman .callback_ops = call_ops, 166184115e1cSTrond Myklebust .callback_data = data, 1662101070caSTrond Myklebust .workqueue = nfsiod_workqueue, 16639f0ec176SAndy Adamson .flags = RPC_TASK_ASYNC | flags, 16643ff7576dSTrond Myklebust .priority = priority, 166584115e1cSTrond Myklebust }; 1666788e7a89STrond Myklebust /* Set up the initial task struct. */ 1667c36aae9aSPeng Tao nfs_ops->commit_setup(data, &msg); 16681da177e4SLinus Torvalds 1669b4839ebeSKinglong Mee dprintk("NFS: initiated commit call\n"); 1670bdc7f021STrond Myklebust 16718c21c62cSWeston Andros Adamson nfs4_state_protect(NFS_SERVER(data->inode)->nfs_client, 16728c21c62cSWeston Andros Adamson NFS_SP4_MACH_CRED_COMMIT, &task_setup_data.rpc_client, &msg); 16738c21c62cSWeston Andros Adamson 167407737691STrond Myklebust task = rpc_run_task(&task_setup_data); 1675dbae4c73STrond Myklebust if (IS_ERR(task)) 1676dbae4c73STrond Myklebust return PTR_ERR(task); 1677d2224e7aSJeff Layton if (how & FLUSH_SYNC) 1678d2224e7aSJeff Layton rpc_wait_for_completion_task(task); 167907737691STrond Myklebust rpc_put_task(task); 1680dbae4c73STrond Myklebust return 0; 16811da177e4SLinus Torvalds } 1682e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_initiate_commit); 16831da177e4SLinus Torvalds 1684378520b8SPeng Tao static loff_t nfs_get_lwb(struct list_head *head) 1685378520b8SPeng Tao { 1686378520b8SPeng Tao loff_t lwb = 0; 1687378520b8SPeng Tao struct nfs_page *req; 1688378520b8SPeng Tao 1689378520b8SPeng Tao list_for_each_entry(req, head, wb_list) 1690378520b8SPeng Tao if (lwb < (req_offset(req) + req->wb_bytes)) 1691378520b8SPeng Tao lwb = req_offset(req) + req->wb_bytes; 1692378520b8SPeng Tao 1693378520b8SPeng Tao return lwb; 1694378520b8SPeng Tao } 1695378520b8SPeng Tao 16961da177e4SLinus Torvalds /* 16979ace33cdSFred Isaman * Set up the argument/result storage required for the RPC call. 16989ace33cdSFred Isaman */ 16990b7c0153SFred Isaman void nfs_init_commit(struct nfs_commit_data *data, 1700988b6dceSFred Isaman struct list_head *head, 1701f453a54aSFred Isaman struct pnfs_layout_segment *lseg, 1702f453a54aSFred Isaman struct nfs_commit_info *cinfo) 17039ace33cdSFred Isaman { 17049ace33cdSFred Isaman struct nfs_page *first = nfs_list_entry(head->next); 17052b0143b5SDavid Howells struct inode *inode = d_inode(first->wb_context->dentry); 17069ace33cdSFred Isaman 17079ace33cdSFred Isaman /* Set up the RPC argument and reply structs 17089ace33cdSFred Isaman * NB: take care not to mess about with data->commit et al. */ 17099ace33cdSFred Isaman 17109ace33cdSFred Isaman list_splice_init(head, &data->pages); 17119ace33cdSFred Isaman 17129ace33cdSFred Isaman data->inode = inode; 17139ace33cdSFred Isaman data->cred = first->wb_context->cred; 1714988b6dceSFred Isaman data->lseg = lseg; /* reference transferred */ 1715378520b8SPeng Tao /* only set lwb for pnfs commit */ 1716378520b8SPeng Tao if (lseg) 1717378520b8SPeng Tao data->lwb = nfs_get_lwb(&data->pages); 17189ace33cdSFred Isaman data->mds_ops = &nfs_commit_ops; 1719f453a54aSFred Isaman data->completion_ops = cinfo->completion_ops; 1720b359f9d0SFred Isaman data->dreq = cinfo->dreq; 17219ace33cdSFred Isaman 17229ace33cdSFred Isaman data->args.fh = NFS_FH(data->inode); 17239ace33cdSFred Isaman /* Note: we always request a commit of the entire inode */ 17249ace33cdSFred Isaman data->args.offset = 0; 17259ace33cdSFred Isaman data->args.count = 0; 17260b7c0153SFred Isaman data->context = get_nfs_open_context(first->wb_context); 17279ace33cdSFred Isaman data->res.fattr = &data->fattr; 17289ace33cdSFred Isaman data->res.verf = &data->verf; 17299ace33cdSFred Isaman nfs_fattr_init(&data->fattr); 17309ace33cdSFred Isaman } 1731e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_init_commit); 17329ace33cdSFred Isaman 1733e0c2b380SFred Isaman void nfs_retry_commit(struct list_head *page_list, 1734ea2cf228SFred Isaman struct pnfs_layout_segment *lseg, 1735b57ff130SWeston Andros Adamson struct nfs_commit_info *cinfo, 1736b57ff130SWeston Andros Adamson u32 ds_commit_idx) 173764bfeb49SFred Isaman { 173864bfeb49SFred Isaman struct nfs_page *req; 173964bfeb49SFred Isaman 174064bfeb49SFred Isaman while (!list_empty(page_list)) { 174164bfeb49SFred Isaman req = nfs_list_entry(page_list->next); 174264bfeb49SFred Isaman nfs_list_remove_request(req); 1743b57ff130SWeston Andros Adamson nfs_mark_request_commit(req, lseg, cinfo, ds_commit_idx); 1744487b9b8aSTom Haynes if (!cinfo->dreq) 1745487b9b8aSTom Haynes nfs_clear_page_commit(req->wb_page); 17461d1afcbcSTrond Myklebust nfs_unlock_and_release_request(req); 174764bfeb49SFred Isaman } 174864bfeb49SFred Isaman } 1749e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_retry_commit); 175064bfeb49SFred Isaman 1751b20135d0STrond Myklebust static void 1752b20135d0STrond Myklebust nfs_commit_resched_write(struct nfs_commit_info *cinfo, 1753b20135d0STrond Myklebust struct nfs_page *req) 1754b20135d0STrond Myklebust { 1755b20135d0STrond Myklebust __set_page_dirty_nobuffers(req->wb_page); 1756b20135d0STrond Myklebust } 1757b20135d0STrond Myklebust 17589ace33cdSFred Isaman /* 17591da177e4SLinus Torvalds * Commit dirty pages 17601da177e4SLinus Torvalds */ 17611da177e4SLinus Torvalds static int 1762ea2cf228SFred Isaman nfs_commit_list(struct inode *inode, struct list_head *head, int how, 1763ea2cf228SFred Isaman struct nfs_commit_info *cinfo) 17641da177e4SLinus Torvalds { 17650b7c0153SFred Isaman struct nfs_commit_data *data; 17661da177e4SLinus Torvalds 1767ade8febdSWeston Andros Adamson /* another commit raced with us */ 1768ade8febdSWeston Andros Adamson if (list_empty(head)) 1769ade8febdSWeston Andros Adamson return 0; 1770ade8febdSWeston Andros Adamson 1771518662e0SNeilBrown data = nfs_commitdata_alloc(true); 17721da177e4SLinus Torvalds 17731da177e4SLinus Torvalds /* Set up the argument struct */ 1774f453a54aSFred Isaman nfs_init_commit(data, head, NULL, cinfo); 1775f453a54aSFred Isaman atomic_inc(&cinfo->mds->rpcs_out); 1776c36aae9aSPeng Tao return nfs_initiate_commit(NFS_CLIENT(inode), data, NFS_PROTO(inode), 1777c36aae9aSPeng Tao data->mds_ops, how, 0); 17781da177e4SLinus Torvalds } 17791da177e4SLinus Torvalds 17801da177e4SLinus Torvalds /* 17811da177e4SLinus Torvalds * COMMIT call returned 17821da177e4SLinus Torvalds */ 1783788e7a89STrond Myklebust static void nfs_commit_done(struct rpc_task *task, void *calldata) 17841da177e4SLinus Torvalds { 17850b7c0153SFred Isaman struct nfs_commit_data *data = calldata; 17861da177e4SLinus Torvalds 1787a3f565b1SChuck Lever dprintk("NFS: %5u nfs_commit_done (status %d)\n", 17881da177e4SLinus Torvalds task->tk_pid, task->tk_status); 17891da177e4SLinus Torvalds 1790788e7a89STrond Myklebust /* Call the NFS version-specific code */ 1791c0d0e96bSTrond Myklebust NFS_PROTO(data->inode)->commit_done(task, data); 1792c9d8f89dSTrond Myklebust } 1793c9d8f89dSTrond Myklebust 1794f453a54aSFred Isaman static void nfs_commit_release_pages(struct nfs_commit_data *data) 1795c9d8f89dSTrond Myklebust { 1796c9d8f89dSTrond Myklebust struct nfs_page *req; 1797c9d8f89dSTrond Myklebust int status = data->task.tk_status; 1798f453a54aSFred Isaman struct nfs_commit_info cinfo; 1799353db796SNeilBrown struct nfs_server *nfss; 1800788e7a89STrond Myklebust 18011da177e4SLinus Torvalds while (!list_empty(&data->pages)) { 18021da177e4SLinus Torvalds req = nfs_list_entry(data->pages.next); 18031da177e4SLinus Torvalds nfs_list_remove_request(req); 180467911c8fSAnna Schumaker if (req->wb_page) 1805d6d6dc7cSFred Isaman nfs_clear_page_commit(req->wb_page); 18061da177e4SLinus Torvalds 18071e8968c5SNiels de Vos dprintk("NFS: commit (%s/%llu %d@%lld)", 18083d4ff43dSAl Viro req->wb_context->dentry->d_sb->s_id, 18092b0143b5SDavid Howells (unsigned long long)NFS_FILEID(d_inode(req->wb_context->dentry)), 18101da177e4SLinus Torvalds req->wb_bytes, 18111da177e4SLinus Torvalds (long long)req_offset(req)); 1812c9d8f89dSTrond Myklebust if (status < 0) { 1813c9d8f89dSTrond Myklebust nfs_context_set_write_error(req->wb_context, status); 181438a33101SKinglong Mee if (req->wb_page) 18151da177e4SLinus Torvalds nfs_inode_remove_request(req); 1816ddeaa637SJoe Perches dprintk_cont(", error = %d\n", status); 18171da177e4SLinus Torvalds goto next; 18181da177e4SLinus Torvalds } 18191da177e4SLinus Torvalds 18201da177e4SLinus Torvalds /* Okay, COMMIT succeeded, apparently. Check the verifier 18211da177e4SLinus Torvalds * returned by the server against all stored verfs. */ 18228fc3c386STrond Myklebust if (!nfs_write_verifier_cmp(&req->wb_verf, &data->verf.verifier)) { 18231da177e4SLinus Torvalds /* We have a match */ 182438a33101SKinglong Mee if (req->wb_page) 18251da177e4SLinus Torvalds nfs_inode_remove_request(req); 1826ddeaa637SJoe Perches dprintk_cont(" OK\n"); 18271da177e4SLinus Torvalds goto next; 18281da177e4SLinus Torvalds } 18291da177e4SLinus Torvalds /* We have a mismatch. Write the page again */ 1830ddeaa637SJoe Perches dprintk_cont(" mismatch\n"); 18316d884e8fSFred nfs_mark_request_dirty(req); 183205990d1bSTrond Myklebust set_bit(NFS_CONTEXT_RESEND_WRITES, &req->wb_context->flags); 18331da177e4SLinus Torvalds next: 18341d1afcbcSTrond Myklebust nfs_unlock_and_release_request(req); 18351da177e4SLinus Torvalds } 1836353db796SNeilBrown nfss = NFS_SERVER(data->inode); 1837353db796SNeilBrown if (atomic_long_read(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH) 18380db10944SJan Kara clear_bdi_congested(inode_to_bdi(data->inode), BLK_RW_ASYNC); 1839353db796SNeilBrown 1840f453a54aSFred Isaman nfs_init_cinfo(&cinfo, data->inode, data->dreq); 1841af7cf057STrond Myklebust nfs_commit_end(cinfo.mds); 18425917ce84SFred Isaman } 18435917ce84SFred Isaman 18445917ce84SFred Isaman static void nfs_commit_release(void *calldata) 18455917ce84SFred Isaman { 18460b7c0153SFred Isaman struct nfs_commit_data *data = calldata; 18475917ce84SFred Isaman 1848f453a54aSFred Isaman data->completion_ops->completion(data); 1849c9d8f89dSTrond Myklebust nfs_commitdata_release(calldata); 18501da177e4SLinus Torvalds } 1851788e7a89STrond Myklebust 1852788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops = { 18530b7c0153SFred Isaman .rpc_call_prepare = nfs_commit_prepare, 1854788e7a89STrond Myklebust .rpc_call_done = nfs_commit_done, 1855788e7a89STrond Myklebust .rpc_release = nfs_commit_release, 1856788e7a89STrond Myklebust }; 18571da177e4SLinus Torvalds 1858f453a54aSFred Isaman static const struct nfs_commit_completion_ops nfs_commit_completion_ops = { 1859f453a54aSFred Isaman .completion = nfs_commit_release_pages, 1860b20135d0STrond Myklebust .resched_write = nfs_commit_resched_write, 1861f453a54aSFred Isaman }; 1862f453a54aSFred Isaman 18631763da12SFred Isaman int nfs_generic_commit_list(struct inode *inode, struct list_head *head, 1864ea2cf228SFred Isaman int how, struct nfs_commit_info *cinfo) 186584c53ab5SFred Isaman { 186684c53ab5SFred Isaman int status; 186784c53ab5SFred Isaman 1868ea2cf228SFred Isaman status = pnfs_commit_list(inode, head, how, cinfo); 186984c53ab5SFred Isaman if (status == PNFS_NOT_ATTEMPTED) 1870ea2cf228SFred Isaman status = nfs_commit_list(inode, head, how, cinfo); 187184c53ab5SFred Isaman return status; 187284c53ab5SFred Isaman } 187384c53ab5SFred Isaman 1874b608b283STrond Myklebust int nfs_commit_inode(struct inode *inode, int how) 18751da177e4SLinus Torvalds { 18761da177e4SLinus Torvalds LIST_HEAD(head); 1877ea2cf228SFred Isaman struct nfs_commit_info cinfo; 187871d0a611STrond Myklebust int may_wait = how & FLUSH_SYNC; 1879af7cf057STrond Myklebust int error = 0; 1880b8413f98STrond Myklebust int res; 18811da177e4SLinus Torvalds 1882ea2cf228SFred Isaman nfs_init_cinfo_from_inode(&cinfo, inode); 1883af7cf057STrond Myklebust nfs_commit_begin(cinfo.mds); 1884ea2cf228SFred Isaman res = nfs_scan_commit(inode, &head, &cinfo); 1885af7cf057STrond Myklebust if (res) 1886ea2cf228SFred Isaman error = nfs_generic_commit_list(inode, &head, how, &cinfo); 1887af7cf057STrond Myklebust nfs_commit_end(cinfo.mds); 18881da177e4SLinus Torvalds if (error < 0) 1889af7cf057STrond Myklebust goto out_error; 1890b8413f98STrond Myklebust if (!may_wait) 1891b8413f98STrond Myklebust goto out_mark_dirty; 1892af7cf057STrond Myklebust error = wait_on_commit(cinfo.mds); 1893b8413f98STrond Myklebust if (error < 0) 1894b8413f98STrond Myklebust return error; 1895c5efa5fcSTrond Myklebust return res; 1896af7cf057STrond Myklebust out_error: 1897af7cf057STrond Myklebust res = error; 1898c5efa5fcSTrond Myklebust /* Note: If we exit without ensuring that the commit is complete, 1899c5efa5fcSTrond Myklebust * we must mark the inode as dirty. Otherwise, future calls to 1900c5efa5fcSTrond Myklebust * sync_inode() with the WB_SYNC_ALL flag set will fail to ensure 1901c5efa5fcSTrond Myklebust * that the data is on the disk. 1902c5efa5fcSTrond Myklebust */ 1903c5efa5fcSTrond Myklebust out_mark_dirty: 1904c5efa5fcSTrond Myklebust __mark_inode_dirty(inode, I_DIRTY_DATASYNC); 19051da177e4SLinus Torvalds return res; 19061da177e4SLinus Torvalds } 1907b20135d0STrond Myklebust EXPORT_SYMBOL_GPL(nfs_commit_inode); 19088fc795f7STrond Myklebust 1909ae09c31fSAnna Schumaker int nfs_write_inode(struct inode *inode, struct writeback_control *wbc) 19108fc795f7STrond Myklebust { 1911420e3646STrond Myklebust struct nfs_inode *nfsi = NFS_I(inode); 1912420e3646STrond Myklebust int flags = FLUSH_SYNC; 1913420e3646STrond Myklebust int ret = 0; 19148fc795f7STrond Myklebust 19153236c3e1SJeff Layton /* no commits means nothing needs to be done */ 19165cb953d4STrond Myklebust if (!atomic_long_read(&nfsi->commit_info.ncommit)) 19173236c3e1SJeff Layton return ret; 19183236c3e1SJeff Layton 1919a00dd6c0SJeff Layton if (wbc->sync_mode == WB_SYNC_NONE) { 1920a00dd6c0SJeff Layton /* Don't commit yet if this is a non-blocking flush and there 1921a00dd6c0SJeff Layton * are a lot of outstanding writes for this mapping. 1922420e3646STrond Myklebust */ 19231a4edf0fSTrond Myklebust if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK)) 1924420e3646STrond Myklebust goto out_mark_dirty; 1925420e3646STrond Myklebust 1926a00dd6c0SJeff Layton /* don't wait for the COMMIT response */ 1927420e3646STrond Myklebust flags = 0; 1928a00dd6c0SJeff Layton } 1929a00dd6c0SJeff Layton 1930420e3646STrond Myklebust ret = nfs_commit_inode(inode, flags); 1931420e3646STrond Myklebust if (ret >= 0) { 1932420e3646STrond Myklebust if (wbc->sync_mode == WB_SYNC_NONE) { 1933420e3646STrond Myklebust if (ret < wbc->nr_to_write) 1934420e3646STrond Myklebust wbc->nr_to_write -= ret; 1935420e3646STrond Myklebust else 1936420e3646STrond Myklebust wbc->nr_to_write = 0; 1937420e3646STrond Myklebust } 19388fc795f7STrond Myklebust return 0; 1939420e3646STrond Myklebust } 1940420e3646STrond Myklebust out_mark_dirty: 19418fc795f7STrond Myklebust __mark_inode_dirty(inode, I_DIRTY_DATASYNC); 19428fc795f7STrond Myklebust return ret; 19438fc795f7STrond Myklebust } 194489d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_write_inode); 1945863a3c6cSAndy Adamson 1946acdc53b2STrond Myklebust /* 1947837bb1d7STrond Myklebust * Wrapper for filemap_write_and_wait_range() 1948837bb1d7STrond Myklebust * 1949837bb1d7STrond Myklebust * Needed for pNFS in order to ensure data becomes visible to the 1950837bb1d7STrond Myklebust * client. 1951837bb1d7STrond Myklebust */ 1952837bb1d7STrond Myklebust int nfs_filemap_write_and_wait_range(struct address_space *mapping, 1953837bb1d7STrond Myklebust loff_t lstart, loff_t lend) 1954837bb1d7STrond Myklebust { 1955837bb1d7STrond Myklebust int ret; 1956837bb1d7STrond Myklebust 1957837bb1d7STrond Myklebust ret = filemap_write_and_wait_range(mapping, lstart, lend); 1958837bb1d7STrond Myklebust if (ret == 0) 1959837bb1d7STrond Myklebust ret = pnfs_sync_inode(mapping->host, true); 1960837bb1d7STrond Myklebust return ret; 1961837bb1d7STrond Myklebust } 1962837bb1d7STrond Myklebust EXPORT_SYMBOL_GPL(nfs_filemap_write_and_wait_range); 1963837bb1d7STrond Myklebust 1964837bb1d7STrond Myklebust /* 1965acdc53b2STrond Myklebust * flush the inode to disk. 1966acdc53b2STrond Myklebust */ 1967acdc53b2STrond Myklebust int nfs_wb_all(struct inode *inode) 196834901f70STrond Myklebust { 1969f4ce1299STrond Myklebust int ret; 197034901f70STrond Myklebust 1971f4ce1299STrond Myklebust trace_nfs_writeback_inode_enter(inode); 1972f4ce1299STrond Myklebust 19735bb89b47STrond Myklebust ret = filemap_write_and_wait(inode->i_mapping); 19746b196875SChuck Lever if (ret) 19756b196875SChuck Lever goto out; 19765bb89b47STrond Myklebust ret = nfs_commit_inode(inode, FLUSH_SYNC); 19776b196875SChuck Lever if (ret < 0) 19786b196875SChuck Lever goto out; 19795bb89b47STrond Myklebust pnfs_sync_inode(inode, true); 19806b196875SChuck Lever ret = 0; 1981f4ce1299STrond Myklebust 19826b196875SChuck Lever out: 1983f4ce1299STrond Myklebust trace_nfs_writeback_inode_exit(inode, ret); 1984f4ce1299STrond Myklebust return ret; 19851c75950bSTrond Myklebust } 1986ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_wb_all); 19871c75950bSTrond Myklebust 19881b3b4a1aSTrond Myklebust int nfs_wb_page_cancel(struct inode *inode, struct page *page) 19891b3b4a1aSTrond Myklebust { 19901b3b4a1aSTrond Myklebust struct nfs_page *req; 19911b3b4a1aSTrond Myklebust int ret = 0; 19921b3b4a1aSTrond Myklebust 1993ba8b06e6STrond Myklebust wait_on_page_writeback(page); 19943e217045SWeston Andros Adamson 19953e217045SWeston Andros Adamson /* blocking call to cancel all requests and join to a single (head) 19963e217045SWeston Andros Adamson * request */ 19976d17d653STrond Myklebust req = nfs_lock_and_join_requests(page); 19983e217045SWeston Andros Adamson 19993e217045SWeston Andros Adamson if (IS_ERR(req)) { 20003e217045SWeston Andros Adamson ret = PTR_ERR(req); 20013e217045SWeston Andros Adamson } else if (req) { 20023e217045SWeston Andros Adamson /* all requests from this page have been cancelled by 20033e217045SWeston Andros Adamson * nfs_lock_and_join_requests, so just remove the head 20043e217045SWeston Andros Adamson * request from the inode / page_private pointer and 20053e217045SWeston Andros Adamson * release it */ 20061b3b4a1aSTrond Myklebust nfs_inode_remove_request(req); 20071d1afcbcSTrond Myklebust nfs_unlock_and_release_request(req); 20081b3b4a1aSTrond Myklebust } 20093e217045SWeston Andros Adamson 20101b3b4a1aSTrond Myklebust return ret; 20111b3b4a1aSTrond Myklebust } 20121b3b4a1aSTrond Myklebust 20131c75950bSTrond Myklebust /* 20141c75950bSTrond Myklebust * Write back all requests on one page - we do this before reading it. 20151c75950bSTrond Myklebust */ 2016c373fff7STrond Myklebust int nfs_wb_page(struct inode *inode, struct page *page) 20171c75950bSTrond Myklebust { 201829418aa4SMel Gorman loff_t range_start = page_file_offset(page); 201909cbfeafSKirill A. Shutemov loff_t range_end = range_start + (loff_t)(PAGE_SIZE - 1); 20207f2f12d9STrond Myklebust struct writeback_control wbc = { 20217f2f12d9STrond Myklebust .sync_mode = WB_SYNC_ALL, 20227f2f12d9STrond Myklebust .nr_to_write = 0, 20237f2f12d9STrond Myklebust .range_start = range_start, 20247f2f12d9STrond Myklebust .range_end = range_end, 20257f2f12d9STrond Myklebust }; 20267f2f12d9STrond Myklebust int ret; 20277f2f12d9STrond Myklebust 2028f4ce1299STrond Myklebust trace_nfs_writeback_page_enter(inode); 2029f4ce1299STrond Myklebust 20300522f6adSTrond Myklebust for (;;) { 2031ba8b06e6STrond Myklebust wait_on_page_writeback(page); 20327f2f12d9STrond Myklebust if (clear_page_dirty_for_io(page)) { 2033c373fff7STrond Myklebust ret = nfs_writepage_locked(page, &wbc); 20347f2f12d9STrond Myklebust if (ret < 0) 20357f2f12d9STrond Myklebust goto out_error; 20360522f6adSTrond Myklebust continue; 20377f2f12d9STrond Myklebust } 2038f4ce1299STrond Myklebust ret = 0; 20390522f6adSTrond Myklebust if (!PagePrivate(page)) 20400522f6adSTrond Myklebust break; 20410522f6adSTrond Myklebust ret = nfs_commit_inode(inode, FLUSH_SYNC); 20427f2f12d9STrond Myklebust if (ret < 0) 20437f2f12d9STrond Myklebust goto out_error; 20447f2f12d9STrond Myklebust } 20457f2f12d9STrond Myklebust out_error: 2046f4ce1299STrond Myklebust trace_nfs_writeback_page_exit(inode, ret); 20477f2f12d9STrond Myklebust return ret; 20481c75950bSTrond Myklebust } 20491c75950bSTrond Myklebust 2050074cc1deSTrond Myklebust #ifdef CONFIG_MIGRATION 2051074cc1deSTrond Myklebust int nfs_migrate_page(struct address_space *mapping, struct page *newpage, 2052a6bc32b8SMel Gorman struct page *page, enum migrate_mode mode) 2053074cc1deSTrond Myklebust { 20542da95652SJeff Layton /* 20552da95652SJeff Layton * If PagePrivate is set, then the page is currently associated with 20562da95652SJeff Layton * an in-progress read or write request. Don't try to migrate it. 20572da95652SJeff Layton * 20582da95652SJeff Layton * FIXME: we could do this in principle, but we'll need a way to ensure 20592da95652SJeff Layton * that we can safely release the inode reference while holding 20602da95652SJeff Layton * the page lock. 20612da95652SJeff Layton */ 20622da95652SJeff Layton if (PagePrivate(page)) 20632da95652SJeff Layton return -EBUSY; 2064074cc1deSTrond Myklebust 20658c209ce7SDavid Howells if (!nfs_fscache_release_page(page, GFP_KERNEL)) 20668c209ce7SDavid Howells return -EBUSY; 2067074cc1deSTrond Myklebust 2068a6bc32b8SMel Gorman return migrate_page(mapping, newpage, page, mode); 2069074cc1deSTrond Myklebust } 2070074cc1deSTrond Myklebust #endif 2071074cc1deSTrond Myklebust 2072f7b422b1SDavid Howells int __init nfs_init_writepagecache(void) 20731da177e4SLinus Torvalds { 20741da177e4SLinus Torvalds nfs_wdata_cachep = kmem_cache_create("nfs_write_data", 20751e7f3a48SWeston Andros Adamson sizeof(struct nfs_pgio_header), 20761da177e4SLinus Torvalds 0, SLAB_HWCACHE_ALIGN, 207720c2df83SPaul Mundt NULL); 20781da177e4SLinus Torvalds if (nfs_wdata_cachep == NULL) 20791da177e4SLinus Torvalds return -ENOMEM; 20801da177e4SLinus Torvalds 208193d2341cSMatthew Dobson nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE, 20821da177e4SLinus Torvalds nfs_wdata_cachep); 20831da177e4SLinus Torvalds if (nfs_wdata_mempool == NULL) 20843dd4765fSJeff Layton goto out_destroy_write_cache; 20851da177e4SLinus Torvalds 20860b7c0153SFred Isaman nfs_cdata_cachep = kmem_cache_create("nfs_commit_data", 20870b7c0153SFred Isaman sizeof(struct nfs_commit_data), 20880b7c0153SFred Isaman 0, SLAB_HWCACHE_ALIGN, 20890b7c0153SFred Isaman NULL); 20900b7c0153SFred Isaman if (nfs_cdata_cachep == NULL) 20913dd4765fSJeff Layton goto out_destroy_write_mempool; 20920b7c0153SFred Isaman 209393d2341cSMatthew Dobson nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT, 20944c100210SYanchuan Nian nfs_cdata_cachep); 20951da177e4SLinus Torvalds if (nfs_commit_mempool == NULL) 20963dd4765fSJeff Layton goto out_destroy_commit_cache; 20971da177e4SLinus Torvalds 209889a09141SPeter Zijlstra /* 209989a09141SPeter Zijlstra * NFS congestion size, scale with available memory. 210089a09141SPeter Zijlstra * 210189a09141SPeter Zijlstra * 64MB: 8192k 210289a09141SPeter Zijlstra * 128MB: 11585k 210389a09141SPeter Zijlstra * 256MB: 16384k 210489a09141SPeter Zijlstra * 512MB: 23170k 210589a09141SPeter Zijlstra * 1GB: 32768k 210689a09141SPeter Zijlstra * 2GB: 46340k 210789a09141SPeter Zijlstra * 4GB: 65536k 210889a09141SPeter Zijlstra * 8GB: 92681k 210989a09141SPeter Zijlstra * 16GB: 131072k 211089a09141SPeter Zijlstra * 211189a09141SPeter Zijlstra * This allows larger machines to have larger/more transfers. 211289a09141SPeter Zijlstra * Limit the default to 256M 211389a09141SPeter Zijlstra */ 211489a09141SPeter Zijlstra nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10); 211589a09141SPeter Zijlstra if (nfs_congestion_kb > 256*1024) 211689a09141SPeter Zijlstra nfs_congestion_kb = 256*1024; 211789a09141SPeter Zijlstra 21181da177e4SLinus Torvalds return 0; 21193dd4765fSJeff Layton 21203dd4765fSJeff Layton out_destroy_commit_cache: 21213dd4765fSJeff Layton kmem_cache_destroy(nfs_cdata_cachep); 21223dd4765fSJeff Layton out_destroy_write_mempool: 21233dd4765fSJeff Layton mempool_destroy(nfs_wdata_mempool); 21243dd4765fSJeff Layton out_destroy_write_cache: 21253dd4765fSJeff Layton kmem_cache_destroy(nfs_wdata_cachep); 21263dd4765fSJeff Layton return -ENOMEM; 21271da177e4SLinus Torvalds } 21281da177e4SLinus Torvalds 2129266bee88SDavid Brownell void nfs_destroy_writepagecache(void) 21301da177e4SLinus Torvalds { 21311da177e4SLinus Torvalds mempool_destroy(nfs_commit_mempool); 21323dd4765fSJeff Layton kmem_cache_destroy(nfs_cdata_cachep); 21331da177e4SLinus Torvalds mempool_destroy(nfs_wdata_mempool); 21341a1d92c1SAlexey Dobriyan kmem_cache_destroy(nfs_wdata_cachep); 21351da177e4SLinus Torvalds } 21361da177e4SLinus Torvalds 21374a0de55cSAnna Schumaker static const struct nfs_rw_ops nfs_rw_write_ops = { 21384a0de55cSAnna Schumaker .rw_alloc_header = nfs_writehdr_alloc, 21394a0de55cSAnna Schumaker .rw_free_header = nfs_writehdr_free, 21400eecb214SAnna Schumaker .rw_done = nfs_writeback_done, 21410eecb214SAnna Schumaker .rw_result = nfs_writeback_result, 21421ed26f33SAnna Schumaker .rw_initiate = nfs_initiate_write, 21434a0de55cSAnna Schumaker }; 2144