1457c8996SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * linux/fs/nfs/write.c 41da177e4SLinus Torvalds * 57c85d900STrond Myklebust * Write file data over NFS. 61da177e4SLinus Torvalds * 71da177e4SLinus Torvalds * Copyright (C) 1996, 1997, Olaf Kirch <okir@monad.swb.de> 81da177e4SLinus Torvalds */ 91da177e4SLinus Torvalds 101da177e4SLinus Torvalds #include <linux/types.h> 111da177e4SLinus Torvalds #include <linux/slab.h> 121da177e4SLinus Torvalds #include <linux/mm.h> 131da177e4SLinus Torvalds #include <linux/pagemap.h> 141da177e4SLinus Torvalds #include <linux/file.h> 151da177e4SLinus Torvalds #include <linux/writeback.h> 1689a09141SPeter Zijlstra #include <linux/swap.h> 17074cc1deSTrond Myklebust #include <linux/migrate.h> 181da177e4SLinus Torvalds 191da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h> 201da177e4SLinus Torvalds #include <linux/nfs_fs.h> 211da177e4SLinus Torvalds #include <linux/nfs_mount.h> 221da177e4SLinus Torvalds #include <linux/nfs_page.h> 233fcfab16SAndrew Morton #include <linux/backing-dev.h> 24afeacc8cSPaul Gortmaker #include <linux/export.h> 25af7cf057STrond Myklebust #include <linux/freezer.h> 26af7cf057STrond Myklebust #include <linux/wait.h> 271eb5d98fSJeff Layton #include <linux/iversion.h> 283fcfab16SAndrew Morton 297c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 30875bc3fbSTrond Myklebust #include <linux/sched/mm.h> 311da177e4SLinus Torvalds 321da177e4SLinus Torvalds #include "delegation.h" 3349a70f27STrond Myklebust #include "internal.h" 3491d5b470SChuck Lever #include "iostat.h" 35def6ed7eSAndy Adamson #include "nfs4_fs.h" 36074cc1deSTrond Myklebust #include "fscache.h" 3794ad1c80SFred Isaman #include "pnfs.h" 381da177e4SLinus Torvalds 39f4ce1299STrond Myklebust #include "nfstrace.h" 40f4ce1299STrond Myklebust 411da177e4SLinus Torvalds #define NFSDBG_FACILITY NFSDBG_PAGECACHE 421da177e4SLinus Torvalds 431da177e4SLinus Torvalds #define MIN_POOL_WRITE (32) 441da177e4SLinus Torvalds #define MIN_POOL_COMMIT (4) 451da177e4SLinus Torvalds 46919e3bd9STrond Myklebust struct nfs_io_completion { 47919e3bd9STrond Myklebust void (*complete)(void *data); 48919e3bd9STrond Myklebust void *data; 49919e3bd9STrond Myklebust struct kref refcount; 50919e3bd9STrond Myklebust }; 51919e3bd9STrond Myklebust 521da177e4SLinus Torvalds /* 531da177e4SLinus Torvalds * Local function declarations 541da177e4SLinus Torvalds */ 55f8512ad0SFred Isaman static void nfs_redirty_request(struct nfs_page *req); 56788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops; 57061ae2edSFred Isaman static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops; 58f453a54aSFred Isaman static const struct nfs_commit_completion_ops nfs_commit_completion_ops; 594a0de55cSAnna Schumaker static const struct nfs_rw_ops nfs_rw_write_ops; 6006c9fdf3STrond Myklebust static void nfs_inode_remove_request(struct nfs_page *req); 61d4581383SWeston Andros Adamson static void nfs_clear_request_commit(struct nfs_page *req); 6202d1426cSWeston Andros Adamson static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo, 6302d1426cSWeston Andros Adamson struct inode *inode); 643a3908c8STrond Myklebust static struct nfs_page * 653a3908c8STrond Myklebust nfs_page_search_commits_for_head_request_locked(struct nfs_inode *nfsi, 663a3908c8STrond Myklebust struct page *page); 671da177e4SLinus Torvalds 68e18b890bSChristoph Lameter static struct kmem_cache *nfs_wdata_cachep; 693feb2d49STrond Myklebust static mempool_t *nfs_wdata_mempool; 700b7c0153SFred Isaman static struct kmem_cache *nfs_cdata_cachep; 711da177e4SLinus Torvalds static mempool_t *nfs_commit_mempool; 721da177e4SLinus Torvalds 73518662e0SNeilBrown struct nfs_commit_data *nfs_commitdata_alloc(bool never_fail) 741da177e4SLinus Torvalds { 75518662e0SNeilBrown struct nfs_commit_data *p; 7640859d7eSChuck Lever 77518662e0SNeilBrown if (never_fail) 78518662e0SNeilBrown p = mempool_alloc(nfs_commit_mempool, GFP_NOIO); 79518662e0SNeilBrown else { 80518662e0SNeilBrown /* It is OK to do some reclaim, not no safe to wait 81518662e0SNeilBrown * for anything to be returned to the pool. 82518662e0SNeilBrown * mempool_alloc() cannot handle that particular combination, 83518662e0SNeilBrown * so we need two separate attempts. 84518662e0SNeilBrown */ 85518662e0SNeilBrown p = mempool_alloc(nfs_commit_mempool, GFP_NOWAIT); 86518662e0SNeilBrown if (!p) 87518662e0SNeilBrown p = kmem_cache_alloc(nfs_cdata_cachep, GFP_NOIO | 88518662e0SNeilBrown __GFP_NOWARN | __GFP_NORETRY); 89518662e0SNeilBrown if (!p) 90518662e0SNeilBrown return NULL; 91518662e0SNeilBrown } 92518662e0SNeilBrown 931da177e4SLinus Torvalds memset(p, 0, sizeof(*p)); 941da177e4SLinus Torvalds INIT_LIST_HEAD(&p->pages); 951da177e4SLinus Torvalds return p; 961da177e4SLinus Torvalds } 97e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commitdata_alloc); 981da177e4SLinus Torvalds 990b7c0153SFred Isaman void nfs_commit_free(struct nfs_commit_data *p) 1001da177e4SLinus Torvalds { 1011da177e4SLinus Torvalds mempool_free(p, nfs_commit_mempool); 1021da177e4SLinus Torvalds } 103e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commit_free); 1041da177e4SLinus Torvalds 1051e7f3a48SWeston Andros Adamson static struct nfs_pgio_header *nfs_writehdr_alloc(void) 1063feb2d49STrond Myklebust { 1072b17d725STrond Myklebust struct nfs_pgio_header *p = mempool_alloc(nfs_wdata_mempool, GFP_KERNEL); 1083feb2d49STrond Myklebust 1093feb2d49STrond Myklebust memset(p, 0, sizeof(*p)); 110fbe77c30SBenjamin Coddington p->rw_mode = FMODE_WRITE; 1113feb2d49STrond Myklebust return p; 1123feb2d49STrond Myklebust } 1133feb2d49STrond Myklebust 1141e7f3a48SWeston Andros Adamson static void nfs_writehdr_free(struct nfs_pgio_header *hdr) 1156c75dc0dSFred Isaman { 1161e7f3a48SWeston Andros Adamson mempool_free(hdr, nfs_wdata_mempool); 1173feb2d49STrond Myklebust } 1181da177e4SLinus Torvalds 119919e3bd9STrond Myklebust static struct nfs_io_completion *nfs_io_completion_alloc(gfp_t gfp_flags) 120919e3bd9STrond Myklebust { 121919e3bd9STrond Myklebust return kmalloc(sizeof(struct nfs_io_completion), gfp_flags); 122919e3bd9STrond Myklebust } 123919e3bd9STrond Myklebust 124919e3bd9STrond Myklebust static void nfs_io_completion_init(struct nfs_io_completion *ioc, 125919e3bd9STrond Myklebust void (*complete)(void *), void *data) 126919e3bd9STrond Myklebust { 127919e3bd9STrond Myklebust ioc->complete = complete; 128919e3bd9STrond Myklebust ioc->data = data; 129919e3bd9STrond Myklebust kref_init(&ioc->refcount); 130919e3bd9STrond Myklebust } 131919e3bd9STrond Myklebust 132919e3bd9STrond Myklebust static void nfs_io_completion_release(struct kref *kref) 133919e3bd9STrond Myklebust { 134919e3bd9STrond Myklebust struct nfs_io_completion *ioc = container_of(kref, 135919e3bd9STrond Myklebust struct nfs_io_completion, refcount); 136919e3bd9STrond Myklebust ioc->complete(ioc->data); 137919e3bd9STrond Myklebust kfree(ioc); 138919e3bd9STrond Myklebust } 139919e3bd9STrond Myklebust 140919e3bd9STrond Myklebust static void nfs_io_completion_get(struct nfs_io_completion *ioc) 141919e3bd9STrond Myklebust { 142919e3bd9STrond Myklebust if (ioc != NULL) 143919e3bd9STrond Myklebust kref_get(&ioc->refcount); 144919e3bd9STrond Myklebust } 145919e3bd9STrond Myklebust 146919e3bd9STrond Myklebust static void nfs_io_completion_put(struct nfs_io_completion *ioc) 147919e3bd9STrond Myklebust { 148919e3bd9STrond Myklebust if (ioc != NULL) 149919e3bd9STrond Myklebust kref_put(&ioc->refcount, nfs_io_completion_release); 150919e3bd9STrond Myklebust } 151919e3bd9STrond Myklebust 152bd37d6fcSTrond Myklebust static struct nfs_page * 153bd37d6fcSTrond Myklebust nfs_page_private_request(struct page *page) 154bd37d6fcSTrond Myklebust { 155bd37d6fcSTrond Myklebust if (!PagePrivate(page)) 156bd37d6fcSTrond Myklebust return NULL; 157bd37d6fcSTrond Myklebust return (struct nfs_page *)page_private(page); 158bd37d6fcSTrond Myklebust } 159bd37d6fcSTrond Myklebust 16084d3a9a9SWeston Andros Adamson /* 16184d3a9a9SWeston Andros Adamson * nfs_page_find_head_request_locked - find head request associated with @page 16284d3a9a9SWeston Andros Adamson * 16384d3a9a9SWeston Andros Adamson * must be called while holding the inode lock. 16484d3a9a9SWeston Andros Adamson * 16584d3a9a9SWeston Andros Adamson * returns matching head request with reference held, or NULL if not found. 16684d3a9a9SWeston Andros Adamson */ 16729418aa4SMel Gorman static struct nfs_page * 168b30d2f04STrond Myklebust nfs_page_find_private_request(struct page *page) 169277459d2STrond Myklebust { 1704b9bb25bSTrond Myklebust struct address_space *mapping = page_file_mapping(page); 171bd37d6fcSTrond Myklebust struct nfs_page *req; 172277459d2STrond Myklebust 173b30d2f04STrond Myklebust if (!PagePrivate(page)) 174b30d2f04STrond Myklebust return NULL; 1754b9bb25bSTrond Myklebust spin_lock(&mapping->private_lock); 176bd37d6fcSTrond Myklebust req = nfs_page_private_request(page); 17784d3a9a9SWeston Andros Adamson if (req) { 17884d3a9a9SWeston Andros Adamson WARN_ON_ONCE(req->wb_head != req); 17929418aa4SMel Gorman kref_get(&req->wb_kref); 18084d3a9a9SWeston Andros Adamson } 1814b9bb25bSTrond Myklebust spin_unlock(&mapping->private_lock); 182b30d2f04STrond Myklebust return req; 183b30d2f04STrond Myklebust } 18429418aa4SMel Gorman 185b30d2f04STrond Myklebust static struct nfs_page * 186b30d2f04STrond Myklebust nfs_page_find_swap_request(struct page *page) 187b30d2f04STrond Myklebust { 188b30d2f04STrond Myklebust struct inode *inode = page_file_mapping(page)->host; 189b30d2f04STrond Myklebust struct nfs_inode *nfsi = NFS_I(inode); 190b30d2f04STrond Myklebust struct nfs_page *req = NULL; 191b30d2f04STrond Myklebust if (!PageSwapCache(page)) 192b30d2f04STrond Myklebust return NULL; 193e824f99aSTrond Myklebust mutex_lock(&nfsi->commit_mutex); 194b30d2f04STrond Myklebust if (PageSwapCache(page)) { 195b30d2f04STrond Myklebust req = nfs_page_search_commits_for_head_request_locked(nfsi, 196b30d2f04STrond Myklebust page); 197b30d2f04STrond Myklebust if (req) { 198b30d2f04STrond Myklebust WARN_ON_ONCE(req->wb_head != req); 199b30d2f04STrond Myklebust kref_get(&req->wb_kref); 200b30d2f04STrond Myklebust } 201b30d2f04STrond Myklebust } 202e824f99aSTrond Myklebust mutex_unlock(&nfsi->commit_mutex); 203277459d2STrond Myklebust return req; 204277459d2STrond Myklebust } 205277459d2STrond Myklebust 20684d3a9a9SWeston Andros Adamson /* 20784d3a9a9SWeston Andros Adamson * nfs_page_find_head_request - find head request associated with @page 20884d3a9a9SWeston Andros Adamson * 20984d3a9a9SWeston Andros Adamson * returns matching head request with reference held, or NULL if not found. 21084d3a9a9SWeston Andros Adamson */ 21184d3a9a9SWeston Andros Adamson static struct nfs_page *nfs_page_find_head_request(struct page *page) 212277459d2STrond Myklebust { 213b30d2f04STrond Myklebust struct nfs_page *req; 214277459d2STrond Myklebust 215b30d2f04STrond Myklebust req = nfs_page_find_private_request(page); 216b30d2f04STrond Myklebust if (!req) 217b30d2f04STrond Myklebust req = nfs_page_find_swap_request(page); 218277459d2STrond Myklebust return req; 219277459d2STrond Myklebust } 220277459d2STrond Myklebust 2211da177e4SLinus Torvalds /* Adjust the file length if we're writing beyond the end */ 2221da177e4SLinus Torvalds static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count) 2231da177e4SLinus Torvalds { 224d56b4ddfSMel Gorman struct inode *inode = page_file_mapping(page)->host; 225a3d01454STrond Myklebust loff_t end, i_size; 226a3d01454STrond Myklebust pgoff_t end_index; 2271da177e4SLinus Torvalds 228a3d01454STrond Myklebust spin_lock(&inode->i_lock); 229a3d01454STrond Myklebust i_size = i_size_read(inode); 23009cbfeafSKirill A. Shutemov end_index = (i_size - 1) >> PAGE_SHIFT; 2318cd79788SHuang Ying if (i_size > 0 && page_index(page) < end_index) 232a3d01454STrond Myklebust goto out; 233d56b4ddfSMel Gorman end = page_file_offset(page) + ((loff_t)offset+count); 2341da177e4SLinus Torvalds if (i_size >= end) 235a3d01454STrond Myklebust goto out; 2361da177e4SLinus Torvalds i_size_write(inode, end); 237f6cdfa6dSTrond Myklebust NFS_I(inode)->cache_validity &= ~NFS_INO_INVALID_SIZE; 238a3d01454STrond Myklebust nfs_inc_stats(inode, NFSIOS_EXTENDWRITE); 239a3d01454STrond Myklebust out: 240a3d01454STrond Myklebust spin_unlock(&inode->i_lock); 2411da177e4SLinus Torvalds } 2421da177e4SLinus Torvalds 243a301b777STrond Myklebust /* A writeback failed: mark the page as bad, and invalidate the page cache */ 244d2ceb7e5SBenjamin Coddington static void nfs_set_pageerror(struct address_space *mapping) 245a301b777STrond Myklebust { 2460df68cedSTrond Myklebust struct inode *inode = mapping->host; 2470df68cedSTrond Myklebust 248d2ceb7e5SBenjamin Coddington nfs_zap_mapping(mapping->host, mapping); 2490df68cedSTrond Myklebust /* Force file size revalidation */ 2500df68cedSTrond Myklebust spin_lock(&inode->i_lock); 2510df68cedSTrond Myklebust NFS_I(inode)->cache_validity |= NFS_INO_REVAL_FORCED | 2520df68cedSTrond Myklebust NFS_INO_REVAL_PAGECACHE | 2530df68cedSTrond Myklebust NFS_INO_INVALID_SIZE; 2540df68cedSTrond Myklebust spin_unlock(&inode->i_lock); 255a301b777STrond Myklebust } 256a301b777STrond Myklebust 2576fbda89bSTrond Myklebust static void nfs_mapping_set_error(struct page *page, int error) 2586fbda89bSTrond Myklebust { 259b8946d7bSTrond Myklebust struct address_space *mapping = page_file_mapping(page); 260b8946d7bSTrond Myklebust 2616fbda89bSTrond Myklebust SetPageError(page); 262b8946d7bSTrond Myklebust mapping_set_error(mapping, error); 263b8946d7bSTrond Myklebust nfs_set_pageerror(mapping); 2646fbda89bSTrond Myklebust } 2656fbda89bSTrond Myklebust 266d72ddcbaSWeston Andros Adamson /* 267d72ddcbaSWeston Andros Adamson * nfs_page_group_search_locked 268d72ddcbaSWeston Andros Adamson * @head - head request of page group 269d72ddcbaSWeston Andros Adamson * @page_offset - offset into page 270d72ddcbaSWeston Andros Adamson * 271d72ddcbaSWeston Andros Adamson * Search page group with head @head to find a request that contains the 272d72ddcbaSWeston Andros Adamson * page offset @page_offset. 273d72ddcbaSWeston Andros Adamson * 274d72ddcbaSWeston Andros Adamson * Returns a pointer to the first matching nfs request, or NULL if no 275d72ddcbaSWeston Andros Adamson * match is found. 276d72ddcbaSWeston Andros Adamson * 277d72ddcbaSWeston Andros Adamson * Must be called with the page group lock held 278d72ddcbaSWeston Andros Adamson */ 279d72ddcbaSWeston Andros Adamson static struct nfs_page * 280d72ddcbaSWeston Andros Adamson nfs_page_group_search_locked(struct nfs_page *head, unsigned int page_offset) 281d72ddcbaSWeston Andros Adamson { 282d72ddcbaSWeston Andros Adamson struct nfs_page *req; 283d72ddcbaSWeston Andros Adamson 284d72ddcbaSWeston Andros Adamson req = head; 285d72ddcbaSWeston Andros Adamson do { 286d72ddcbaSWeston Andros Adamson if (page_offset >= req->wb_pgbase && 287d72ddcbaSWeston Andros Adamson page_offset < (req->wb_pgbase + req->wb_bytes)) 288d72ddcbaSWeston Andros Adamson return req; 289d72ddcbaSWeston Andros Adamson 290d72ddcbaSWeston Andros Adamson req = req->wb_this_page; 291d72ddcbaSWeston Andros Adamson } while (req != head); 292d72ddcbaSWeston Andros Adamson 293d72ddcbaSWeston Andros Adamson return NULL; 294d72ddcbaSWeston Andros Adamson } 295d72ddcbaSWeston Andros Adamson 296d72ddcbaSWeston Andros Adamson /* 297d72ddcbaSWeston Andros Adamson * nfs_page_group_covers_page 298d72ddcbaSWeston Andros Adamson * @head - head request of page group 299d72ddcbaSWeston Andros Adamson * 300d72ddcbaSWeston Andros Adamson * Return true if the page group with head @head covers the whole page, 301d72ddcbaSWeston Andros Adamson * returns false otherwise 302d72ddcbaSWeston Andros Adamson */ 303d72ddcbaSWeston Andros Adamson static bool nfs_page_group_covers_page(struct nfs_page *req) 304d72ddcbaSWeston Andros Adamson { 305d72ddcbaSWeston Andros Adamson struct nfs_page *tmp; 306d72ddcbaSWeston Andros Adamson unsigned int pos = 0; 307d72ddcbaSWeston Andros Adamson unsigned int len = nfs_page_length(req->wb_page); 308d72ddcbaSWeston Andros Adamson 3091344b7eaSTrond Myklebust nfs_page_group_lock(req); 310d72ddcbaSWeston Andros Adamson 3117e8a30f8STrond Myklebust for (;;) { 312d72ddcbaSWeston Andros Adamson tmp = nfs_page_group_search_locked(req->wb_head, pos); 3137e8a30f8STrond Myklebust if (!tmp) 3147e8a30f8STrond Myklebust break; 3157e8a30f8STrond Myklebust pos = tmp->wb_pgbase + tmp->wb_bytes; 316d72ddcbaSWeston Andros Adamson } 317d72ddcbaSWeston Andros Adamson 318d72ddcbaSWeston Andros Adamson nfs_page_group_unlock(req); 3197e8a30f8STrond Myklebust return pos >= len; 320d72ddcbaSWeston Andros Adamson } 321d72ddcbaSWeston Andros Adamson 3221da177e4SLinus Torvalds /* We can set the PG_uptodate flag if we see that a write request 3231da177e4SLinus Torvalds * covers the full page. 3241da177e4SLinus Torvalds */ 325d72ddcbaSWeston Andros Adamson static void nfs_mark_uptodate(struct nfs_page *req) 3261da177e4SLinus Torvalds { 327d72ddcbaSWeston Andros Adamson if (PageUptodate(req->wb_page)) 3281da177e4SLinus Torvalds return; 329d72ddcbaSWeston Andros Adamson if (!nfs_page_group_covers_page(req)) 3301da177e4SLinus Torvalds return; 331d72ddcbaSWeston Andros Adamson SetPageUptodate(req->wb_page); 3321da177e4SLinus Torvalds } 3331da177e4SLinus Torvalds 3341da177e4SLinus Torvalds static int wb_priority(struct writeback_control *wbc) 3351da177e4SLinus Torvalds { 336e87b4c7aSNeilBrown int ret = 0; 337cca588d6STrond Myklebust 338e87b4c7aSNeilBrown if (wbc->sync_mode == WB_SYNC_ALL) 339e87b4c7aSNeilBrown ret = FLUSH_COND_STABLE; 340e87b4c7aSNeilBrown return ret; 3411da177e4SLinus Torvalds } 3421da177e4SLinus Torvalds 3431da177e4SLinus Torvalds /* 34489a09141SPeter Zijlstra * NFS congestion control 34589a09141SPeter Zijlstra */ 34689a09141SPeter Zijlstra 34789a09141SPeter Zijlstra int nfs_congestion_kb; 34889a09141SPeter Zijlstra 34989a09141SPeter Zijlstra #define NFS_CONGESTION_ON_THRESH (nfs_congestion_kb >> (PAGE_SHIFT-10)) 35089a09141SPeter Zijlstra #define NFS_CONGESTION_OFF_THRESH \ 35189a09141SPeter Zijlstra (NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2)) 35289a09141SPeter Zijlstra 353deed85e7STrond Myklebust static void nfs_set_page_writeback(struct page *page) 35489a09141SPeter Zijlstra { 3550db10944SJan Kara struct inode *inode = page_file_mapping(page)->host; 3560db10944SJan Kara struct nfs_server *nfss = NFS_SERVER(inode); 3575a6d41b3STrond Myklebust int ret = test_set_page_writeback(page); 3585a6d41b3STrond Myklebust 359deed85e7STrond Myklebust WARN_ON_ONCE(ret != 0); 36089a09141SPeter Zijlstra 361277866a0SPeter Zijlstra if (atomic_long_inc_return(&nfss->writeback) > 3620db10944SJan Kara NFS_CONGESTION_ON_THRESH) 3630db10944SJan Kara set_bdi_congested(inode_to_bdi(inode), BLK_RW_ASYNC); 36489a09141SPeter Zijlstra } 36589a09141SPeter Zijlstra 36620633f04SWeston Andros Adamson static void nfs_end_page_writeback(struct nfs_page *req) 36789a09141SPeter Zijlstra { 36820633f04SWeston Andros Adamson struct inode *inode = page_file_mapping(req->wb_page)->host; 36989a09141SPeter Zijlstra struct nfs_server *nfss = NFS_SERVER(inode); 37031a01f09STrond Myklebust bool is_done; 37189a09141SPeter Zijlstra 37231a01f09STrond Myklebust is_done = nfs_page_group_sync_on_bit(req, PG_WB_END); 37331a01f09STrond Myklebust nfs_unlock_request(req); 37431a01f09STrond Myklebust if (!is_done) 37520633f04SWeston Andros Adamson return; 37620633f04SWeston Andros Adamson 37720633f04SWeston Andros Adamson end_page_writeback(req->wb_page); 378c4dc4beeSPeter Zijlstra if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH) 3790db10944SJan Kara clear_bdi_congested(inode_to_bdi(inode), BLK_RW_ASYNC); 38089a09141SPeter Zijlstra } 38189a09141SPeter Zijlstra 382d4581383SWeston Andros Adamson /* 383d4581383SWeston Andros Adamson * nfs_destroy_unlinked_subrequests - destroy recently unlinked subrequests 384d4581383SWeston Andros Adamson * 385d4581383SWeston Andros Adamson * @destroy_list - request list (using wb_this_page) terminated by @old_head 386d4581383SWeston Andros Adamson * @old_head - the old head of the list 387d4581383SWeston Andros Adamson * 388d4581383SWeston Andros Adamson * All subrequests must be locked and removed from all lists, so at this point 389d4581383SWeston Andros Adamson * they are only "active" in this function, and possibly in nfs_wait_on_request 390d4581383SWeston Andros Adamson * with a reference held by some other context. 391d4581383SWeston Andros Adamson */ 392d4581383SWeston Andros Adamson static void 393d4581383SWeston Andros Adamson nfs_destroy_unlinked_subrequests(struct nfs_page *destroy_list, 394b66aaa8dSTrond Myklebust struct nfs_page *old_head, 395b66aaa8dSTrond Myklebust struct inode *inode) 396d4581383SWeston Andros Adamson { 397d4581383SWeston Andros Adamson while (destroy_list) { 398d4581383SWeston Andros Adamson struct nfs_page *subreq = destroy_list; 399d4581383SWeston Andros Adamson 400d4581383SWeston Andros Adamson destroy_list = (subreq->wb_this_page == old_head) ? 401d4581383SWeston Andros Adamson NULL : subreq->wb_this_page; 402d4581383SWeston Andros Adamson 40308ca8b21STrond Myklebust /* Note: lock subreq in order to change subreq->wb_head */ 40408ca8b21STrond Myklebust nfs_page_set_headlock(subreq); 405d4581383SWeston Andros Adamson WARN_ON_ONCE(old_head != subreq->wb_head); 406d4581383SWeston Andros Adamson 407d4581383SWeston Andros Adamson /* make sure old group is not used */ 408d4581383SWeston Andros Adamson subreq->wb_this_page = subreq; 40908ca8b21STrond Myklebust subreq->wb_head = subreq; 410d4581383SWeston Andros Adamson 411902a4c00STrond Myklebust clear_bit(PG_REMOVE, &subreq->wb_flags); 412902a4c00STrond Myklebust 4135b2b5187STrond Myklebust /* Note: races with nfs_page_group_destroy() */ 4145b2b5187STrond Myklebust if (!kref_read(&subreq->wb_kref)) { 4155b2b5187STrond Myklebust /* Check if we raced with nfs_page_group_destroy() */ 41608ca8b21STrond Myklebust if (test_and_clear_bit(PG_TEARDOWN, &subreq->wb_flags)) { 41708ca8b21STrond Myklebust nfs_page_clear_headlock(subreq); 4185b2b5187STrond Myklebust nfs_free_request(subreq); 41908ca8b21STrond Myklebust } else 42008ca8b21STrond Myklebust nfs_page_clear_headlock(subreq); 4215b2b5187STrond Myklebust continue; 4225b2b5187STrond Myklebust } 42308ca8b21STrond Myklebust nfs_page_clear_headlock(subreq); 424d4581383SWeston Andros Adamson 425add42de3STrond Myklebust nfs_release_request(old_head); 4265b2b5187STrond Myklebust 427b66aaa8dSTrond Myklebust if (test_and_clear_bit(PG_INODE_REF, &subreq->wb_flags)) { 428d4581383SWeston Andros Adamson nfs_release_request(subreq); 429a6b6d5b8STrond Myklebust atomic_long_dec(&NFS_I(inode)->nrequests); 430d4581383SWeston Andros Adamson } 4315b2b5187STrond Myklebust 4325b2b5187STrond Myklebust /* subreq is now totally disconnected from page group or any 4335b2b5187STrond Myklebust * write / commit lists. last chance to wake any waiters */ 4345b2b5187STrond Myklebust nfs_unlock_and_release_request(subreq); 435d4581383SWeston Andros Adamson } 436d4581383SWeston Andros Adamson } 437d4581383SWeston Andros Adamson 438d4581383SWeston Andros Adamson /* 439d4581383SWeston Andros Adamson * nfs_lock_and_join_requests - join all subreqs to the head req and return 440d4581383SWeston Andros Adamson * a locked reference, cancelling any pending 441d4581383SWeston Andros Adamson * operations for this page. 442d4581383SWeston Andros Adamson * 443d4581383SWeston Andros Adamson * @page - the page used to lookup the "page group" of nfs_page structures 444d4581383SWeston Andros Adamson * 445d4581383SWeston Andros Adamson * This function joins all sub requests to the head request by first 446d4581383SWeston Andros Adamson * locking all requests in the group, cancelling any pending operations 447d4581383SWeston Andros Adamson * and finally updating the head request to cover the whole range covered by 448d4581383SWeston Andros Adamson * the (former) group. All subrequests are removed from any write or commit 449d4581383SWeston Andros Adamson * lists, unlinked from the group and destroyed. 450d4581383SWeston Andros Adamson * 451d4581383SWeston Andros Adamson * Returns a locked, referenced pointer to the head request - which after 452d4581383SWeston Andros Adamson * this call is guaranteed to be the only request associated with the page. 453d4581383SWeston Andros Adamson * Returns NULL if no requests are found for @page, or a ERR_PTR if an 454d4581383SWeston Andros Adamson * error was encountered. 455d4581383SWeston Andros Adamson */ 456d4581383SWeston Andros Adamson static struct nfs_page * 4576d17d653STrond Myklebust nfs_lock_and_join_requests(struct page *page) 458d4581383SWeston Andros Adamson { 459d4581383SWeston Andros Adamson struct inode *inode = page_file_mapping(page)->host; 460d4581383SWeston Andros Adamson struct nfs_page *head, *subreq; 461d4581383SWeston Andros Adamson struct nfs_page *destroy_list = NULL; 462a62f8e3bSTrond Myklebust unsigned int pgbase, off, bytes; 463d4581383SWeston Andros Adamson int ret; 464d4581383SWeston Andros Adamson 465d4581383SWeston Andros Adamson try_again: 466d4581383SWeston Andros Adamson /* 467d4581383SWeston Andros Adamson * A reference is taken only on the head request which acts as a 468d4581383SWeston Andros Adamson * reference to the whole page group - the group will not be destroyed 469d4581383SWeston Andros Adamson * until the head reference is released. 470d4581383SWeston Andros Adamson */ 471bd37d6fcSTrond Myklebust head = nfs_page_find_head_request(page); 472bd37d6fcSTrond Myklebust if (!head) 473d4581383SWeston Andros Adamson return NULL; 474d4581383SWeston Andros Adamson 475a0e265bcSTrond Myklebust /* lock the page head first in order to avoid an ABBA inefficiency */ 476a0e265bcSTrond Myklebust if (!nfs_lock_request(head)) { 477a0e265bcSTrond Myklebust ret = nfs_wait_on_request(head); 478a0e265bcSTrond Myklebust nfs_release_request(head); 479a0e265bcSTrond Myklebust if (ret < 0) 480a0e265bcSTrond Myklebust return ERR_PTR(ret); 481a0e265bcSTrond Myklebust goto try_again; 482a0e265bcSTrond Myklebust } 483bd37d6fcSTrond Myklebust 484bd37d6fcSTrond Myklebust /* Ensure that nobody removed the request before we locked it */ 485bd37d6fcSTrond Myklebust if (head != nfs_page_private_request(page) && !PageSwapCache(page)) { 486bd37d6fcSTrond Myklebust nfs_unlock_and_release_request(head); 487bd37d6fcSTrond Myklebust goto try_again; 488bd37d6fcSTrond Myklebust } 4897c3af975SWeston Andros Adamson 4901344b7eaSTrond Myklebust ret = nfs_page_group_lock(head); 4910671d8f1SMarkus Elfring if (ret < 0) 4920671d8f1SMarkus Elfring goto release_request; 4937c3af975SWeston Andros Adamson 4947c3af975SWeston Andros Adamson /* lock each request in the page group */ 495a62f8e3bSTrond Myklebust ret = nfs_page_group_lock_subrequests(head); 496a62f8e3bSTrond Myklebust if (ret < 0) 497a62f8e3bSTrond Myklebust goto release_request; 498a62f8e3bSTrond Myklebust 499a62f8e3bSTrond Myklebust pgbase = head->wb_pgbase; 500a62f8e3bSTrond Myklebust bytes = head->wb_bytes; 501a62f8e3bSTrond Myklebust off = head->wb_offset; 502a0e265bcSTrond Myklebust for (subreq = head->wb_this_page; subreq != head; 503a0e265bcSTrond Myklebust subreq = subreq->wb_this_page) { 504a62f8e3bSTrond Myklebust /* Subrequests should always form a contiguous range */ 505a62f8e3bSTrond Myklebust if (pgbase > subreq->wb_pgbase) { 506a62f8e3bSTrond Myklebust off -= pgbase - subreq->wb_pgbase; 507a62f8e3bSTrond Myklebust bytes += pgbase - subreq->wb_pgbase; 508a62f8e3bSTrond Myklebust pgbase = subreq->wb_pgbase; 509a62f8e3bSTrond Myklebust } 510a62f8e3bSTrond Myklebust bytes = max(subreq->wb_pgbase + subreq->wb_bytes 511a62f8e3bSTrond Myklebust - pgbase, bytes); 5121bd5d6d0STrond Myklebust } 5131bd5d6d0STrond Myklebust 514a62f8e3bSTrond Myklebust /* Set the head request's range to cover the former page group */ 515a62f8e3bSTrond Myklebust head->wb_pgbase = pgbase; 516a62f8e3bSTrond Myklebust head->wb_bytes = bytes; 517a62f8e3bSTrond Myklebust head->wb_offset = off; 518d4581383SWeston Andros Adamson 519d4581383SWeston Andros Adamson /* Now that all requests are locked, make sure they aren't on any list. 520d4581383SWeston Andros Adamson * Commit list removal accounting is done after locks are dropped */ 521d4581383SWeston Andros Adamson subreq = head; 522d4581383SWeston Andros Adamson do { 523411a99adSWeston Andros Adamson nfs_clear_request_commit(subreq); 524d4581383SWeston Andros Adamson subreq = subreq->wb_this_page; 525d4581383SWeston Andros Adamson } while (subreq != head); 526d4581383SWeston Andros Adamson 527d4581383SWeston Andros Adamson /* unlink subrequests from head, destroy them later */ 528d4581383SWeston Andros Adamson if (head->wb_this_page != head) { 529d4581383SWeston Andros Adamson /* destroy list will be terminated by head */ 530d4581383SWeston Andros Adamson destroy_list = head->wb_this_page; 531d4581383SWeston Andros Adamson head->wb_this_page = head; 532d4581383SWeston Andros Adamson } 533d4581383SWeston Andros Adamson 534b66aaa8dSTrond Myklebust /* Postpone destruction of this request */ 535b66aaa8dSTrond Myklebust if (test_and_clear_bit(PG_REMOVE, &head->wb_flags)) { 536b66aaa8dSTrond Myklebust set_bit(PG_INODE_REF, &head->wb_flags); 537b66aaa8dSTrond Myklebust kref_get(&head->wb_kref); 538a6b6d5b8STrond Myklebust atomic_long_inc(&NFS_I(inode)->nrequests); 539b66aaa8dSTrond Myklebust } 540b66aaa8dSTrond Myklebust 541d4581383SWeston Andros Adamson nfs_page_group_unlock(head); 542d4581383SWeston Andros Adamson 543b66aaa8dSTrond Myklebust nfs_destroy_unlinked_subrequests(destroy_list, head, inode); 544d4581383SWeston Andros Adamson 545b5bab9bfSTrond Myklebust /* Did we lose a race with nfs_inode_remove_request()? */ 546b5bab9bfSTrond Myklebust if (!(PagePrivate(page) || PageSwapCache(page))) { 547b5bab9bfSTrond Myklebust nfs_unlock_and_release_request(head); 548b5bab9bfSTrond Myklebust return NULL; 549b5bab9bfSTrond Myklebust } 550b5bab9bfSTrond Myklebust 551bd37d6fcSTrond Myklebust /* still holds ref on head from nfs_page_find_head_request 552d4581383SWeston Andros Adamson * and still has lock on head from lock loop */ 553d4581383SWeston Andros Adamson return head; 5540671d8f1SMarkus Elfring 5550671d8f1SMarkus Elfring release_request: 5560671d8f1SMarkus Elfring nfs_unlock_and_release_request(head); 5570671d8f1SMarkus Elfring return ERR_PTR(ret); 558612c9384STrond Myklebust } 559074cc1deSTrond Myklebust 5606fbda89bSTrond Myklebust static void nfs_write_error(struct nfs_page *req, int error) 5610bcbf039SPeng Tao { 562861e1671SChuck Lever trace_nfs_write_error(req, error); 5636fbda89bSTrond Myklebust nfs_mapping_set_error(req->wb_page, error); 56406c9fdf3STrond Myklebust nfs_inode_remove_request(req); 5650bcbf039SPeng Tao nfs_end_page_writeback(req); 5661f84ccdfSFred Isaman nfs_release_request(req); 5670bcbf039SPeng Tao } 5680bcbf039SPeng Tao 569074cc1deSTrond Myklebust /* 570074cc1deSTrond Myklebust * Find an associated nfs write request, and prepare to flush it out 571074cc1deSTrond Myklebust * May return an error if the user signalled nfs_wait_on_request(). 572074cc1deSTrond Myklebust */ 573074cc1deSTrond Myklebust static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio, 5746d17d653STrond Myklebust struct page *page) 575074cc1deSTrond Myklebust { 576074cc1deSTrond Myklebust struct nfs_page *req; 577074cc1deSTrond Myklebust int ret = 0; 578074cc1deSTrond Myklebust 5796d17d653STrond Myklebust req = nfs_lock_and_join_requests(page); 580074cc1deSTrond Myklebust if (!req) 581074cc1deSTrond Myklebust goto out; 582074cc1deSTrond Myklebust ret = PTR_ERR(req); 583074cc1deSTrond Myklebust if (IS_ERR(req)) 584074cc1deSTrond Myklebust goto out; 585074cc1deSTrond Myklebust 586deed85e7STrond Myklebust nfs_set_page_writeback(page); 587deed85e7STrond Myklebust WARN_ON_ONCE(test_bit(PG_CLEAN, &req->wb_flags)); 588074cc1deSTrond Myklebust 589a6598813STrond Myklebust /* If there is a fatal error that covers this write, just exit */ 59096c41455STrond Myklebust ret = pgio->pg_error; 59196c41455STrond Myklebust if (nfs_error_is_fatal_on_server(ret)) 592a6598813STrond Myklebust goto out_launder; 593a6598813STrond Myklebust 59496c41455STrond Myklebust ret = 0; 595f8512ad0SFred Isaman if (!nfs_pageio_add_request(pgio, req)) { 596074cc1deSTrond Myklebust ret = pgio->pg_error; 5970bcbf039SPeng Tao /* 598c373fff7STrond Myklebust * Remove the problematic req upon fatal errors on the server 5990bcbf039SPeng Tao */ 6000bcbf039SPeng Tao if (nfs_error_is_fatal(ret)) { 601c373fff7STrond Myklebust if (nfs_error_is_fatal_on_server(ret)) 602a6598813STrond Myklebust goto out_launder; 6038fc75bedSTrond Myklebust } else 6040bcbf039SPeng Tao ret = -EAGAIN; 6058fc75bedSTrond Myklebust nfs_redirty_request(req); 60696c41455STrond Myklebust pgio->pg_error = 0; 60740f90271STrond Myklebust } else 60840f90271STrond Myklebust nfs_add_stats(page_file_mapping(page)->host, 60940f90271STrond Myklebust NFSIOS_WRITEPAGES, 1); 610074cc1deSTrond Myklebust out: 611074cc1deSTrond Myklebust return ret; 612a6598813STrond Myklebust out_launder: 6136fbda89bSTrond Myklebust nfs_write_error(req, ret); 61414bebe3cSTrond Myklebust return 0; 615e261f51fSTrond Myklebust } 616e261f51fSTrond Myklebust 617d6c843b9SPeng Tao static int nfs_do_writepage(struct page *page, struct writeback_control *wbc, 618c373fff7STrond Myklebust struct nfs_pageio_descriptor *pgio) 619f758c885STrond Myklebust { 620cfb506e1STrond Myklebust int ret; 621f758c885STrond Myklebust 6228cd79788SHuang Ying nfs_pageio_cond_complete(pgio, page_index(page)); 6236d17d653STrond Myklebust ret = nfs_page_async_flush(pgio, page); 624cfb506e1STrond Myklebust if (ret == -EAGAIN) { 625cfb506e1STrond Myklebust redirty_page_for_writepage(wbc, page); 62696c41455STrond Myklebust ret = AOP_WRITEPAGE_ACTIVATE; 627cfb506e1STrond Myklebust } 628cfb506e1STrond Myklebust return ret; 629f758c885STrond Myklebust } 630f758c885STrond Myklebust 631e261f51fSTrond Myklebust /* 6321da177e4SLinus Torvalds * Write an mmapped page to the server. 6331da177e4SLinus Torvalds */ 634d6c843b9SPeng Tao static int nfs_writepage_locked(struct page *page, 635c373fff7STrond Myklebust struct writeback_control *wbc) 6361da177e4SLinus Torvalds { 637f758c885STrond Myklebust struct nfs_pageio_descriptor pgio; 63840f90271STrond Myklebust struct inode *inode = page_file_mapping(page)->host; 639e261f51fSTrond Myklebust int err; 6401da177e4SLinus Torvalds 64140f90271STrond Myklebust nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE); 642811ed92eSTrond Myklebust nfs_pageio_init_write(&pgio, inode, 0, 643a20c93e3SChristoph Hellwig false, &nfs_async_write_completion_ops); 644c373fff7STrond Myklebust err = nfs_do_writepage(page, wbc, &pgio); 64596c41455STrond Myklebust pgio.pg_error = 0; 646f758c885STrond Myklebust nfs_pageio_complete(&pgio); 647f758c885STrond Myklebust if (err < 0) 6484d770ccfSTrond Myklebust return err; 64996c41455STrond Myklebust if (nfs_error_is_fatal(pgio.pg_error)) 650f758c885STrond Myklebust return pgio.pg_error; 651f758c885STrond Myklebust return 0; 6524d770ccfSTrond Myklebust } 6534d770ccfSTrond Myklebust 6544d770ccfSTrond Myklebust int nfs_writepage(struct page *page, struct writeback_control *wbc) 6554d770ccfSTrond Myklebust { 656f758c885STrond Myklebust int ret; 6574d770ccfSTrond Myklebust 658c373fff7STrond Myklebust ret = nfs_writepage_locked(page, wbc); 65996c41455STrond Myklebust if (ret != AOP_WRITEPAGE_ACTIVATE) 6601da177e4SLinus Torvalds unlock_page(page); 661f758c885STrond Myklebust return ret; 662f758c885STrond Myklebust } 663f758c885STrond Myklebust 664f758c885STrond Myklebust static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data) 665f758c885STrond Myklebust { 666f758c885STrond Myklebust int ret; 667f758c885STrond Myklebust 668c373fff7STrond Myklebust ret = nfs_do_writepage(page, wbc, data); 66996c41455STrond Myklebust if (ret != AOP_WRITEPAGE_ACTIVATE) 670f758c885STrond Myklebust unlock_page(page); 671f758c885STrond Myklebust return ret; 6721da177e4SLinus Torvalds } 6731da177e4SLinus Torvalds 674919e3bd9STrond Myklebust static void nfs_io_completion_commit(void *inode) 675919e3bd9STrond Myklebust { 676919e3bd9STrond Myklebust nfs_commit_inode(inode, 0); 677919e3bd9STrond Myklebust } 678919e3bd9STrond Myklebust 6791da177e4SLinus Torvalds int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc) 6801da177e4SLinus Torvalds { 6811da177e4SLinus Torvalds struct inode *inode = mapping->host; 682c63c7b05STrond Myklebust struct nfs_pageio_descriptor pgio; 683875bc3fbSTrond Myklebust struct nfs_io_completion *ioc; 6841da177e4SLinus Torvalds int err; 6851da177e4SLinus Torvalds 68691d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES); 68791d5b470SChuck Lever 6882b17d725STrond Myklebust ioc = nfs_io_completion_alloc(GFP_KERNEL); 689919e3bd9STrond Myklebust if (ioc) 690919e3bd9STrond Myklebust nfs_io_completion_init(ioc, nfs_io_completion_commit, inode); 691919e3bd9STrond Myklebust 692a20c93e3SChristoph Hellwig nfs_pageio_init_write(&pgio, inode, wb_priority(wbc), false, 693a20c93e3SChristoph Hellwig &nfs_async_write_completion_ops); 694919e3bd9STrond Myklebust pgio.pg_io_completion = ioc; 695f758c885STrond Myklebust err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio); 69696c41455STrond Myklebust pgio.pg_error = 0; 697c63c7b05STrond Myklebust nfs_pageio_complete(&pgio); 698919e3bd9STrond Myklebust nfs_io_completion_put(ioc); 69972cb77f4STrond Myklebust 700f758c885STrond Myklebust if (err < 0) 70172cb77f4STrond Myklebust goto out_err; 70272cb77f4STrond Myklebust err = pgio.pg_error; 70396c41455STrond Myklebust if (nfs_error_is_fatal(err)) 70472cb77f4STrond Myklebust goto out_err; 705c63c7b05STrond Myklebust return 0; 70672cb77f4STrond Myklebust out_err: 70772cb77f4STrond Myklebust return err; 7081da177e4SLinus Torvalds } 7091da177e4SLinus Torvalds 7101da177e4SLinus Torvalds /* 7111da177e4SLinus Torvalds * Insert a write request into an inode 7121da177e4SLinus Torvalds */ 713d6d6dc7cSFred Isaman static void nfs_inode_add_request(struct inode *inode, struct nfs_page *req) 7141da177e4SLinus Torvalds { 7154b9bb25bSTrond Myklebust struct address_space *mapping = page_file_mapping(req->wb_page); 7161da177e4SLinus Torvalds struct nfs_inode *nfsi = NFS_I(inode); 717e7d39069STrond Myklebust 7182bfc6e56SWeston Andros Adamson WARN_ON_ONCE(req->wb_this_page != req); 7192bfc6e56SWeston Andros Adamson 720e7d39069STrond Myklebust /* Lock the request! */ 7217ad84aa9STrond Myklebust nfs_lock_request(req); 722e7d39069STrond Myklebust 72329418aa4SMel Gorman /* 72429418aa4SMel Gorman * Swap-space should not get truncated. Hence no need to plug the race 72529418aa4SMel Gorman * with invalidate/truncate. 72629418aa4SMel Gorman */ 7274b9bb25bSTrond Myklebust spin_lock(&mapping->private_lock); 7284b9bb25bSTrond Myklebust if (!nfs_have_writebacks(inode) && 7291eb5d98fSJeff Layton NFS_PROTO(inode)->have_delegation(inode, FMODE_WRITE)) 7301eb5d98fSJeff Layton inode_inc_iversion_raw(inode); 73129418aa4SMel Gorman if (likely(!PageSwapCache(req->wb_page))) { 7322df485a7STrond Myklebust set_bit(PG_MAPPED, &req->wb_flags); 733deb7d638STrond Myklebust SetPagePrivate(req->wb_page); 734277459d2STrond Myklebust set_page_private(req->wb_page, (unsigned long)req); 73529418aa4SMel Gorman } 7364b9bb25bSTrond Myklebust spin_unlock(&mapping->private_lock); 737a6b6d5b8STrond Myklebust atomic_long_inc(&nfsi->nrequests); 73817089a29SWeston Andros Adamson /* this a head request for a page group - mark it as having an 739cb1410c7SWeston Andros Adamson * extra reference so sub groups can follow suit. 740cb1410c7SWeston Andros Adamson * This flag also informs pgio layer when to bump nrequests when 741cb1410c7SWeston Andros Adamson * adding subrequests. */ 74217089a29SWeston Andros Adamson WARN_ON(test_and_set_bit(PG_INODE_REF, &req->wb_flags)); 743c03b4024STrond Myklebust kref_get(&req->wb_kref); 7441da177e4SLinus Torvalds } 7451da177e4SLinus Torvalds 7461da177e4SLinus Torvalds /* 74789a09141SPeter Zijlstra * Remove a write request from an inode 7481da177e4SLinus Torvalds */ 7491da177e4SLinus Torvalds static void nfs_inode_remove_request(struct nfs_page *req) 7501da177e4SLinus Torvalds { 7514b9bb25bSTrond Myklebust struct address_space *mapping = page_file_mapping(req->wb_page); 7524b9bb25bSTrond Myklebust struct inode *inode = mapping->host; 7531da177e4SLinus Torvalds struct nfs_inode *nfsi = NFS_I(inode); 75420633f04SWeston Andros Adamson struct nfs_page *head; 75520633f04SWeston Andros Adamson 75620633f04SWeston Andros Adamson if (nfs_page_group_sync_on_bit(req, PG_REMOVE)) { 75720633f04SWeston Andros Adamson head = req->wb_head; 7581da177e4SLinus Torvalds 7594b9bb25bSTrond Myklebust spin_lock(&mapping->private_lock); 76067911c8fSAnna Schumaker if (likely(head->wb_page && !PageSwapCache(head->wb_page))) { 76120633f04SWeston Andros Adamson set_page_private(head->wb_page, 0); 76220633f04SWeston Andros Adamson ClearPagePrivate(head->wb_page); 76320633f04SWeston Andros Adamson clear_bit(PG_MAPPED, &head->wb_flags); 76429418aa4SMel Gorman } 7654b9bb25bSTrond Myklebust spin_unlock(&mapping->private_lock); 76620633f04SWeston Andros Adamson } 76717089a29SWeston Andros Adamson 76833ea5aaaSZhangXiaoxu if (test_and_clear_bit(PG_INODE_REF, &req->wb_flags)) { 7691da177e4SLinus Torvalds nfs_release_request(req); 77033ea5aaaSZhangXiaoxu atomic_long_dec(&nfsi->nrequests); 77133ea5aaaSZhangXiaoxu } 7721da177e4SLinus Torvalds } 7731da177e4SLinus Torvalds 77461822ab5STrond Myklebust static void 7756d884e8fSFred nfs_mark_request_dirty(struct nfs_page *req) 77661822ab5STrond Myklebust { 77767911c8fSAnna Schumaker if (req->wb_page) 77861822ab5STrond Myklebust __set_page_dirty_nobuffers(req->wb_page); 77961822ab5STrond Myklebust } 78061822ab5STrond Myklebust 7813a3908c8STrond Myklebust /* 7823a3908c8STrond Myklebust * nfs_page_search_commits_for_head_request_locked 7833a3908c8STrond Myklebust * 7843a3908c8STrond Myklebust * Search through commit lists on @inode for the head request for @page. 7853a3908c8STrond Myklebust * Must be called while holding the inode (which is cinfo) lock. 7863a3908c8STrond Myklebust * 7873a3908c8STrond Myklebust * Returns the head request if found, or NULL if not found. 7883a3908c8STrond Myklebust */ 7893a3908c8STrond Myklebust static struct nfs_page * 7903a3908c8STrond Myklebust nfs_page_search_commits_for_head_request_locked(struct nfs_inode *nfsi, 7913a3908c8STrond Myklebust struct page *page) 7923a3908c8STrond Myklebust { 7933a3908c8STrond Myklebust struct nfs_page *freq, *t; 7943a3908c8STrond Myklebust struct nfs_commit_info cinfo; 7953a3908c8STrond Myklebust struct inode *inode = &nfsi->vfs_inode; 7963a3908c8STrond Myklebust 7973a3908c8STrond Myklebust nfs_init_cinfo_from_inode(&cinfo, inode); 7983a3908c8STrond Myklebust 7993a3908c8STrond Myklebust /* search through pnfs commit lists */ 8003a3908c8STrond Myklebust freq = pnfs_search_commit_reqs(inode, &cinfo, page); 8013a3908c8STrond Myklebust if (freq) 8023a3908c8STrond Myklebust return freq->wb_head; 8033a3908c8STrond Myklebust 8043a3908c8STrond Myklebust /* Linearly search the commit list for the correct request */ 8053a3908c8STrond Myklebust list_for_each_entry_safe(freq, t, &cinfo.mds->list, wb_list) { 8063a3908c8STrond Myklebust if (freq->wb_page == page) 8073a3908c8STrond Myklebust return freq->wb_head; 8083a3908c8STrond Myklebust } 8093a3908c8STrond Myklebust 8103a3908c8STrond Myklebust return NULL; 8113a3908c8STrond Myklebust } 8123a3908c8STrond Myklebust 8138dd37758STrond Myklebust /** 81486d80f97STrond Myklebust * nfs_request_add_commit_list_locked - add request to a commit list 81586d80f97STrond Myklebust * @req: pointer to a struct nfs_page 81686d80f97STrond Myklebust * @dst: commit list head 81786d80f97STrond Myklebust * @cinfo: holds list lock and accounting info 81886d80f97STrond Myklebust * 81986d80f97STrond Myklebust * This sets the PG_CLEAN bit, updates the cinfo count of 82086d80f97STrond Myklebust * number of outstanding requests requiring a commit as well as 82186d80f97STrond Myklebust * the MM page stats. 82286d80f97STrond Myklebust * 823e824f99aSTrond Myklebust * The caller must hold NFS_I(cinfo->inode)->commit_mutex, and the 824e824f99aSTrond Myklebust * nfs_page lock. 82586d80f97STrond Myklebust */ 82686d80f97STrond Myklebust void 82786d80f97STrond Myklebust nfs_request_add_commit_list_locked(struct nfs_page *req, struct list_head *dst, 82886d80f97STrond Myklebust struct nfs_commit_info *cinfo) 82986d80f97STrond Myklebust { 83086d80f97STrond Myklebust set_bit(PG_CLEAN, &req->wb_flags); 83186d80f97STrond Myklebust nfs_list_add_request(req, dst); 8325cb953d4STrond Myklebust atomic_long_inc(&cinfo->mds->ncommit); 83386d80f97STrond Myklebust } 83486d80f97STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_add_commit_list_locked); 83586d80f97STrond Myklebust 83686d80f97STrond Myklebust /** 8378dd37758STrond Myklebust * nfs_request_add_commit_list - add request to a commit list 8388dd37758STrond Myklebust * @req: pointer to a struct nfs_page 839ea2cf228SFred Isaman * @cinfo: holds list lock and accounting info 8408dd37758STrond Myklebust * 841ea2cf228SFred Isaman * This sets the PG_CLEAN bit, updates the cinfo count of 8428dd37758STrond Myklebust * number of outstanding requests requiring a commit as well as 8438dd37758STrond Myklebust * the MM page stats. 8448dd37758STrond Myklebust * 845ea2cf228SFred Isaman * The caller must _not_ hold the cinfo->lock, but must be 8468dd37758STrond Myklebust * holding the nfs_page lock. 8478dd37758STrond Myklebust */ 8488dd37758STrond Myklebust void 8496272dcc6SAnna Schumaker nfs_request_add_commit_list(struct nfs_page *req, struct nfs_commit_info *cinfo) 8508dd37758STrond Myklebust { 851e824f99aSTrond Myklebust mutex_lock(&NFS_I(cinfo->inode)->commit_mutex); 8526272dcc6SAnna Schumaker nfs_request_add_commit_list_locked(req, &cinfo->mds->list, cinfo); 853e824f99aSTrond Myklebust mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex); 85467911c8fSAnna Schumaker if (req->wb_page) 85586d80f97STrond Myklebust nfs_mark_page_unstable(req->wb_page, cinfo); 8568dd37758STrond Myklebust } 8578dd37758STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_add_commit_list); 8588dd37758STrond Myklebust 8598dd37758STrond Myklebust /** 8608dd37758STrond Myklebust * nfs_request_remove_commit_list - Remove request from a commit list 8618dd37758STrond Myklebust * @req: pointer to a nfs_page 862ea2cf228SFred Isaman * @cinfo: holds list lock and accounting info 8638dd37758STrond Myklebust * 864ea2cf228SFred Isaman * This clears the PG_CLEAN bit, and updates the cinfo's count of 8658dd37758STrond Myklebust * number of outstanding requests requiring a commit 8668dd37758STrond Myklebust * It does not update the MM page stats. 8678dd37758STrond Myklebust * 868ea2cf228SFred Isaman * The caller _must_ hold the cinfo->lock and the nfs_page lock. 8698dd37758STrond Myklebust */ 8708dd37758STrond Myklebust void 871ea2cf228SFred Isaman nfs_request_remove_commit_list(struct nfs_page *req, 872ea2cf228SFred Isaman struct nfs_commit_info *cinfo) 8738dd37758STrond Myklebust { 8748dd37758STrond Myklebust if (!test_and_clear_bit(PG_CLEAN, &(req)->wb_flags)) 8758dd37758STrond Myklebust return; 8768dd37758STrond Myklebust nfs_list_remove_request(req); 8775cb953d4STrond Myklebust atomic_long_dec(&cinfo->mds->ncommit); 8788dd37758STrond Myklebust } 8798dd37758STrond Myklebust EXPORT_SYMBOL_GPL(nfs_request_remove_commit_list); 8808dd37758STrond Myklebust 881ea2cf228SFred Isaman static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo, 882ea2cf228SFred Isaman struct inode *inode) 883ea2cf228SFred Isaman { 884fe238e60SDave Wysochanski cinfo->inode = inode; 885ea2cf228SFred Isaman cinfo->mds = &NFS_I(inode)->commit_info; 886ea2cf228SFred Isaman cinfo->ds = pnfs_get_ds_info(inode); 887b359f9d0SFred Isaman cinfo->dreq = NULL; 888f453a54aSFred Isaman cinfo->completion_ops = &nfs_commit_completion_ops; 889ea2cf228SFred Isaman } 890ea2cf228SFred Isaman 891ea2cf228SFred Isaman void nfs_init_cinfo(struct nfs_commit_info *cinfo, 892ea2cf228SFred Isaman struct inode *inode, 893ea2cf228SFred Isaman struct nfs_direct_req *dreq) 894ea2cf228SFred Isaman { 8951763da12SFred Isaman if (dreq) 8961763da12SFred Isaman nfs_init_cinfo_from_dreq(cinfo, dreq); 8971763da12SFred Isaman else 898ea2cf228SFred Isaman nfs_init_cinfo_from_inode(cinfo, inode); 899ea2cf228SFred Isaman } 900ea2cf228SFred Isaman EXPORT_SYMBOL_GPL(nfs_init_cinfo); 9018dd37758STrond Myklebust 9021da177e4SLinus Torvalds /* 9031da177e4SLinus Torvalds * Add a request to the inode's commit list. 9041da177e4SLinus Torvalds */ 9051763da12SFred Isaman void 906ea2cf228SFred Isaman nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg, 907b57ff130SWeston Andros Adamson struct nfs_commit_info *cinfo, u32 ds_commit_idx) 9081da177e4SLinus Torvalds { 909b57ff130SWeston Andros Adamson if (pnfs_mark_request_commit(req, lseg, cinfo, ds_commit_idx)) 9108dd37758STrond Myklebust return; 9116272dcc6SAnna Schumaker nfs_request_add_commit_list(req, cinfo); 9121da177e4SLinus Torvalds } 9138e821cadSTrond Myklebust 914d6d6dc7cSFred Isaman static void 915d6d6dc7cSFred Isaman nfs_clear_page_commit(struct page *page) 916e468bae9STrond Myklebust { 91711fb9989SMel Gorman dec_node_page_state(page, NR_UNSTABLE_NFS); 91893f78d88STejun Heo dec_wb_stat(&inode_to_bdi(page_file_mapping(page)->host)->wb, 91993f78d88STejun Heo WB_RECLAIMABLE); 920e468bae9STrond Myklebust } 921d6d6dc7cSFred Isaman 922b5bab9bfSTrond Myklebust /* Called holding the request lock on @req */ 9238dd37758STrond Myklebust static void 924d6d6dc7cSFred Isaman nfs_clear_request_commit(struct nfs_page *req) 925d6d6dc7cSFred Isaman { 9268dd37758STrond Myklebust if (test_bit(PG_CLEAN, &req->wb_flags)) { 9279fcd5960STrond Myklebust struct nfs_open_context *ctx = nfs_req_openctx(req); 9289fcd5960STrond Myklebust struct inode *inode = d_inode(ctx->dentry); 929ea2cf228SFred Isaman struct nfs_commit_info cinfo; 930d6d6dc7cSFred Isaman 931ea2cf228SFred Isaman nfs_init_cinfo_from_inode(&cinfo, inode); 932e824f99aSTrond Myklebust mutex_lock(&NFS_I(inode)->commit_mutex); 933ea2cf228SFred Isaman if (!pnfs_clear_request_commit(req, &cinfo)) { 934ea2cf228SFred Isaman nfs_request_remove_commit_list(req, &cinfo); 935d6d6dc7cSFred Isaman } 936e824f99aSTrond Myklebust mutex_unlock(&NFS_I(inode)->commit_mutex); 9378dd37758STrond Myklebust nfs_clear_page_commit(req->wb_page); 9388dd37758STrond Myklebust } 939e468bae9STrond Myklebust } 940e468bae9STrond Myklebust 941d45f60c6SWeston Andros Adamson int nfs_write_need_commit(struct nfs_pgio_header *hdr) 9428e821cadSTrond Myklebust { 943c65e6254SWeston Andros Adamson if (hdr->verf.committed == NFS_DATA_SYNC) 944d45f60c6SWeston Andros Adamson return hdr->lseg == NULL; 945c65e6254SWeston Andros Adamson return hdr->verf.committed != NFS_FILE_SYNC; 9468e821cadSTrond Myklebust } 9478e821cadSTrond Myklebust 948919e3bd9STrond Myklebust static void nfs_async_write_init(struct nfs_pgio_header *hdr) 949919e3bd9STrond Myklebust { 950919e3bd9STrond Myklebust nfs_io_completion_get(hdr->io_completion); 951919e3bd9STrond Myklebust } 952919e3bd9STrond Myklebust 953061ae2edSFred Isaman static void nfs_write_completion(struct nfs_pgio_header *hdr) 9546c75dc0dSFred Isaman { 955ea2cf228SFred Isaman struct nfs_commit_info cinfo; 9566c75dc0dSFred Isaman unsigned long bytes = 0; 9576c75dc0dSFred Isaman 9586c75dc0dSFred Isaman if (test_bit(NFS_IOHDR_REDO, &hdr->flags)) 9596c75dc0dSFred Isaman goto out; 960ea2cf228SFred Isaman nfs_init_cinfo_from_inode(&cinfo, hdr->inode); 9616c75dc0dSFred Isaman while (!list_empty(&hdr->pages)) { 9626c75dc0dSFred Isaman struct nfs_page *req = nfs_list_entry(hdr->pages.next); 9636c75dc0dSFred Isaman 9646c75dc0dSFred Isaman bytes += req->wb_bytes; 9656c75dc0dSFred Isaman nfs_list_remove_request(req); 9666c75dc0dSFred Isaman if (test_bit(NFS_IOHDR_ERROR, &hdr->flags) && 9676c75dc0dSFred Isaman (hdr->good_bytes < bytes)) { 968861e1671SChuck Lever trace_nfs_comp_error(req, hdr->error); 9696fbda89bSTrond Myklebust nfs_mapping_set_error(req->wb_page, hdr->error); 9706c75dc0dSFred Isaman goto remove_req; 9716c75dc0dSFred Isaman } 972c65e6254SWeston Andros Adamson if (nfs_write_need_commit(hdr)) { 97333344e0fSTrond Myklebust /* Reset wb_nio, since the write was successful. */ 97433344e0fSTrond Myklebust req->wb_nio = 0; 975f79d06f5SAnna Schumaker memcpy(&req->wb_verf, &hdr->verf.verifier, sizeof(req->wb_verf)); 976b57ff130SWeston Andros Adamson nfs_mark_request_commit(req, hdr->lseg, &cinfo, 977a7d42ddbSWeston Andros Adamson hdr->pgio_mirror_idx); 9786c75dc0dSFred Isaman goto next; 9796c75dc0dSFred Isaman } 9806c75dc0dSFred Isaman remove_req: 9816c75dc0dSFred Isaman nfs_inode_remove_request(req); 9826c75dc0dSFred Isaman next: 98320633f04SWeston Andros Adamson nfs_end_page_writeback(req); 9843aff4ebbSTrond Myklebust nfs_release_request(req); 9856c75dc0dSFred Isaman } 9866c75dc0dSFred Isaman out: 987919e3bd9STrond Myklebust nfs_io_completion_put(hdr->io_completion); 9886c75dc0dSFred Isaman hdr->release(hdr); 9896c75dc0dSFred Isaman } 9906c75dc0dSFred Isaman 991ce59515cSAnna Schumaker unsigned long 992ea2cf228SFred Isaman nfs_reqs_to_commit(struct nfs_commit_info *cinfo) 993fb8a1f11STrond Myklebust { 9945cb953d4STrond Myklebust return atomic_long_read(&cinfo->mds->ncommit); 995fb8a1f11STrond Myklebust } 996fb8a1f11STrond Myklebust 997e824f99aSTrond Myklebust /* NFS_I(cinfo->inode)->commit_mutex held by caller */ 9981763da12SFred Isaman int 999ea2cf228SFred Isaman nfs_scan_commit_list(struct list_head *src, struct list_head *dst, 1000ea2cf228SFred Isaman struct nfs_commit_info *cinfo, int max) 1001d6d6dc7cSFred Isaman { 1002137da553STrond Myklebust struct nfs_page *req, *tmp; 1003d6d6dc7cSFred Isaman int ret = 0; 1004d6d6dc7cSFred Isaman 1005137da553STrond Myklebust restart: 1006137da553STrond Myklebust list_for_each_entry_safe(req, tmp, src, wb_list) { 10077ad84aa9STrond Myklebust kref_get(&req->wb_kref); 10082ce209c4STrond Myklebust if (!nfs_lock_request(req)) { 10092ce209c4STrond Myklebust int status; 1010137da553STrond Myklebust 1011137da553STrond Myklebust /* Prevent deadlock with nfs_lock_and_join_requests */ 1012137da553STrond Myklebust if (!list_empty(dst)) { 1013137da553STrond Myklebust nfs_release_request(req); 1014137da553STrond Myklebust continue; 1015137da553STrond Myklebust } 1016137da553STrond Myklebust /* Ensure we make progress to prevent livelock */ 10172ce209c4STrond Myklebust mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex); 10182ce209c4STrond Myklebust status = nfs_wait_on_request(req); 10192ce209c4STrond Myklebust nfs_release_request(req); 10202ce209c4STrond Myklebust mutex_lock(&NFS_I(cinfo->inode)->commit_mutex); 10212ce209c4STrond Myklebust if (status < 0) 10222ce209c4STrond Myklebust break; 1023137da553STrond Myklebust goto restart; 10242ce209c4STrond Myklebust } 1025ea2cf228SFred Isaman nfs_request_remove_commit_list(req, cinfo); 10265d2a9d9dSTrond Myklebust clear_bit(PG_COMMIT_TO_DS, &req->wb_flags); 10278dd37758STrond Myklebust nfs_list_add_request(req, dst); 1028d6d6dc7cSFred Isaman ret++; 10291763da12SFred Isaman if ((ret == max) && !cinfo->dreq) 1030d6d6dc7cSFred Isaman break; 1031e824f99aSTrond Myklebust cond_resched(); 1032d6d6dc7cSFred Isaman } 1033d6d6dc7cSFred Isaman return ret; 1034d6d6dc7cSFred Isaman } 10355d2a9d9dSTrond Myklebust EXPORT_SYMBOL_GPL(nfs_scan_commit_list); 1036d6d6dc7cSFred Isaman 10371da177e4SLinus Torvalds /* 10381da177e4SLinus Torvalds * nfs_scan_commit - Scan an inode for commit requests 10391da177e4SLinus Torvalds * @inode: NFS inode to scan 1040ea2cf228SFred Isaman * @dst: mds destination list 1041ea2cf228SFred Isaman * @cinfo: mds and ds lists of reqs ready to commit 10421da177e4SLinus Torvalds * 10431da177e4SLinus Torvalds * Moves requests from the inode's 'commit' request list. 10441da177e4SLinus Torvalds * The requests are *not* checked to ensure that they form a contiguous set. 10451da177e4SLinus Torvalds */ 10461763da12SFred Isaman int 1047ea2cf228SFred Isaman nfs_scan_commit(struct inode *inode, struct list_head *dst, 1048ea2cf228SFred Isaman struct nfs_commit_info *cinfo) 10491da177e4SLinus Torvalds { 1050d6d6dc7cSFred Isaman int ret = 0; 1051fb8a1f11STrond Myklebust 10525cb953d4STrond Myklebust if (!atomic_long_read(&cinfo->mds->ncommit)) 10535cb953d4STrond Myklebust return 0; 1054e824f99aSTrond Myklebust mutex_lock(&NFS_I(cinfo->inode)->commit_mutex); 10555cb953d4STrond Myklebust if (atomic_long_read(&cinfo->mds->ncommit) > 0) { 10568dd37758STrond Myklebust const int max = INT_MAX; 1057d6d6dc7cSFred Isaman 1058ea2cf228SFred Isaman ret = nfs_scan_commit_list(&cinfo->mds->list, dst, 1059ea2cf228SFred Isaman cinfo, max); 1060ea2cf228SFred Isaman ret += pnfs_scan_commit_lists(inode, cinfo, max - ret); 1061d6d6dc7cSFred Isaman } 1062e824f99aSTrond Myklebust mutex_unlock(&NFS_I(cinfo->inode)->commit_mutex); 1063ff778d02STrond Myklebust return ret; 10641da177e4SLinus Torvalds } 1065d6d6dc7cSFred Isaman 10661da177e4SLinus Torvalds /* 1067e7d39069STrond Myklebust * Search for an existing write request, and attempt to update 1068e7d39069STrond Myklebust * it to reflect a new dirty region on a given page. 10691da177e4SLinus Torvalds * 1070e7d39069STrond Myklebust * If the attempt fails, then the existing request is flushed out 1071e7d39069STrond Myklebust * to disk. 10721da177e4SLinus Torvalds */ 1073e7d39069STrond Myklebust static struct nfs_page *nfs_try_to_update_request(struct inode *inode, 1074e7d39069STrond Myklebust struct page *page, 1075e7d39069STrond Myklebust unsigned int offset, 1076e7d39069STrond Myklebust unsigned int bytes) 10771da177e4SLinus Torvalds { 1078e7d39069STrond Myklebust struct nfs_page *req; 1079e7d39069STrond Myklebust unsigned int rqend; 1080e7d39069STrond Myklebust unsigned int end; 10811da177e4SLinus Torvalds int error; 1082277459d2STrond Myklebust 1083e7d39069STrond Myklebust end = offset + bytes; 1084e7d39069STrond Myklebust 1085f6032f21STrond Myklebust req = nfs_lock_and_join_requests(page); 1086f6032f21STrond Myklebust if (IS_ERR_OR_NULL(req)) 1087f6032f21STrond Myklebust return req; 10882bfc6e56SWeston Andros Adamson 1089e7d39069STrond Myklebust rqend = req->wb_offset + req->wb_bytes; 1090e7d39069STrond Myklebust /* 1091e7d39069STrond Myklebust * Tell the caller to flush out the request if 1092e7d39069STrond Myklebust * the offsets are non-contiguous. 1093e7d39069STrond Myklebust * Note: nfs_flush_incompatible() will already 1094e7d39069STrond Myklebust * have flushed out requests having wrong owners. 1095e7d39069STrond Myklebust */ 1096f6032f21STrond Myklebust if (offset > rqend || end < req->wb_offset) 1097e7d39069STrond Myklebust goto out_flushme; 1098e7d39069STrond Myklebust 10991da177e4SLinus Torvalds /* Okay, the request matches. Update the region */ 11001da177e4SLinus Torvalds if (offset < req->wb_offset) { 11011da177e4SLinus Torvalds req->wb_offset = offset; 11021da177e4SLinus Torvalds req->wb_pgbase = offset; 11031da177e4SLinus Torvalds } 11041da177e4SLinus Torvalds if (end > rqend) 11051da177e4SLinus Torvalds req->wb_bytes = end - req->wb_offset; 1106e7d39069STrond Myklebust else 1107e7d39069STrond Myklebust req->wb_bytes = rqend - req->wb_offset; 110833344e0fSTrond Myklebust req->wb_nio = 0; 1109e7d39069STrond Myklebust return req; 1110e7d39069STrond Myklebust out_flushme: 1111f6032f21STrond Myklebust /* 1112f6032f21STrond Myklebust * Note: we mark the request dirty here because 1113f6032f21STrond Myklebust * nfs_lock_and_join_requests() cannot preserve 1114f6032f21STrond Myklebust * commit flags, so we have to replay the write. 1115f6032f21STrond Myklebust */ 1116f6032f21STrond Myklebust nfs_mark_request_dirty(req); 1117f6032f21STrond Myklebust nfs_unlock_and_release_request(req); 1118e7d39069STrond Myklebust error = nfs_wb_page(inode, page); 1119f6032f21STrond Myklebust return (error < 0) ? ERR_PTR(error) : NULL; 1120e7d39069STrond Myklebust } 11211da177e4SLinus Torvalds 1122e7d39069STrond Myklebust /* 1123e7d39069STrond Myklebust * Try to update an existing write request, or create one if there is none. 1124e7d39069STrond Myklebust * 1125e7d39069STrond Myklebust * Note: Should always be called with the Page Lock held to prevent races 1126e7d39069STrond Myklebust * if we have to add a new request. Also assumes that the caller has 1127e7d39069STrond Myklebust * already called nfs_flush_incompatible() if necessary. 1128e7d39069STrond Myklebust */ 1129e7d39069STrond Myklebust static struct nfs_page * nfs_setup_write_request(struct nfs_open_context* ctx, 1130e7d39069STrond Myklebust struct page *page, unsigned int offset, unsigned int bytes) 1131e7d39069STrond Myklebust { 1132d56b4ddfSMel Gorman struct inode *inode = page_file_mapping(page)->host; 1133e7d39069STrond Myklebust struct nfs_page *req; 1134e7d39069STrond Myklebust 1135e7d39069STrond Myklebust req = nfs_try_to_update_request(inode, page, offset, bytes); 1136e7d39069STrond Myklebust if (req != NULL) 1137e7d39069STrond Myklebust goto out; 113828b1d3f5STrond Myklebust req = nfs_create_request(ctx, page, offset, bytes); 1139e7d39069STrond Myklebust if (IS_ERR(req)) 1140e7d39069STrond Myklebust goto out; 1141d6d6dc7cSFred Isaman nfs_inode_add_request(inode, req); 1142efc91ed0STrond Myklebust out: 114361e930a9STrond Myklebust return req; 11441da177e4SLinus Torvalds } 11451da177e4SLinus Torvalds 1146e7d39069STrond Myklebust static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page, 1147e7d39069STrond Myklebust unsigned int offset, unsigned int count) 1148e7d39069STrond Myklebust { 1149e7d39069STrond Myklebust struct nfs_page *req; 1150e7d39069STrond Myklebust 1151e7d39069STrond Myklebust req = nfs_setup_write_request(ctx, page, offset, count); 1152e7d39069STrond Myklebust if (IS_ERR(req)) 1153e7d39069STrond Myklebust return PTR_ERR(req); 1154e7d39069STrond Myklebust /* Update file length */ 1155e7d39069STrond Myklebust nfs_grow_file(page, offset, count); 1156d72ddcbaSWeston Andros Adamson nfs_mark_uptodate(req); 1157a6305ddbSTrond Myklebust nfs_mark_request_dirty(req); 11581d1afcbcSTrond Myklebust nfs_unlock_and_release_request(req); 1159e7d39069STrond Myklebust return 0; 1160e7d39069STrond Myklebust } 1161e7d39069STrond Myklebust 11621da177e4SLinus Torvalds int nfs_flush_incompatible(struct file *file, struct page *page) 11631da177e4SLinus Torvalds { 1164cd3758e3STrond Myklebust struct nfs_open_context *ctx = nfs_file_open_context(file); 11652a369153STrond Myklebust struct nfs_lock_context *l_ctx; 1166bd61e0a9SJeff Layton struct file_lock_context *flctx = file_inode(file)->i_flctx; 11671da177e4SLinus Torvalds struct nfs_page *req; 11681a54533eSTrond Myklebust int do_flush, status; 11691da177e4SLinus Torvalds /* 11701da177e4SLinus Torvalds * Look for a request corresponding to this page. If there 11711da177e4SLinus Torvalds * is one, and it belongs to another file, we flush it out 11721da177e4SLinus Torvalds * before we try to copy anything into the page. Do this 11731da177e4SLinus Torvalds * due to the lack of an ACCESS-type call in NFSv2. 11741da177e4SLinus Torvalds * Also do the same if we find a request from an existing 11751da177e4SLinus Torvalds * dropped page. 11761da177e4SLinus Torvalds */ 11771a54533eSTrond Myklebust do { 117884d3a9a9SWeston Andros Adamson req = nfs_page_find_head_request(page); 11791a54533eSTrond Myklebust if (req == NULL) 11801a54533eSTrond Myklebust return 0; 11812a369153STrond Myklebust l_ctx = req->wb_lock_context; 1182138a2935STrond Myklebust do_flush = req->wb_page != page || 11839fcd5960STrond Myklebust !nfs_match_open_context(nfs_req_openctx(req), ctx); 1184bd61e0a9SJeff Layton if (l_ctx && flctx && 1185bd61e0a9SJeff Layton !(list_empty_careful(&flctx->flc_posix) && 1186bd61e0a9SJeff Layton list_empty_careful(&flctx->flc_flock))) { 1187d51fdb87SNeilBrown do_flush |= l_ctx->lockowner != current->files; 11885263e31eSJeff Layton } 11891da177e4SLinus Torvalds nfs_release_request(req); 11901a54533eSTrond Myklebust if (!do_flush) 11911a54533eSTrond Myklebust return 0; 1192d56b4ddfSMel Gorman status = nfs_wb_page(page_file_mapping(page)->host, page); 11931a54533eSTrond Myklebust } while (status == 0); 11941a54533eSTrond Myklebust return status; 11951da177e4SLinus Torvalds } 11961da177e4SLinus Torvalds 11971da177e4SLinus Torvalds /* 1198dc24826bSAndy Adamson * Avoid buffered writes when a open context credential's key would 1199dc24826bSAndy Adamson * expire soon. 1200dc24826bSAndy Adamson * 1201dc24826bSAndy Adamson * Returns -EACCES if the key will expire within RPC_KEY_EXPIRE_FAIL. 1202dc24826bSAndy Adamson * 1203dc24826bSAndy Adamson * Return 0 and set a credential flag which triggers the inode to flush 1204dc24826bSAndy Adamson * and performs NFS_FILE_SYNC writes if the key will expired within 1205dc24826bSAndy Adamson * RPC_KEY_EXPIRE_TIMEO. 1206dc24826bSAndy Adamson */ 1207dc24826bSAndy Adamson int 1208dc24826bSAndy Adamson nfs_key_timeout_notify(struct file *filp, struct inode *inode) 1209dc24826bSAndy Adamson { 1210dc24826bSAndy Adamson struct nfs_open_context *ctx = nfs_file_open_context(filp); 1211dc24826bSAndy Adamson 1212ddf529eeSNeilBrown if (nfs_ctx_key_to_expire(ctx, inode) && 1213ddf529eeSNeilBrown !ctx->ll_cred) 1214ddf529eeSNeilBrown /* Already expired! */ 1215ddf529eeSNeilBrown return -EACCES; 1216ddf529eeSNeilBrown return 0; 1217dc24826bSAndy Adamson } 1218dc24826bSAndy Adamson 1219dc24826bSAndy Adamson /* 1220dc24826bSAndy Adamson * Test if the open context credential key is marked to expire soon. 1221dc24826bSAndy Adamson */ 1222ce52914eSScott Mayhew bool nfs_ctx_key_to_expire(struct nfs_open_context *ctx, struct inode *inode) 1223dc24826bSAndy Adamson { 1224ce52914eSScott Mayhew struct rpc_auth *auth = NFS_SERVER(inode)->client->cl_auth; 1225ddf529eeSNeilBrown struct rpc_cred *cred = ctx->ll_cred; 1226ddf529eeSNeilBrown struct auth_cred acred = { 1227a52458b4SNeilBrown .cred = ctx->cred, 1228ddf529eeSNeilBrown }; 1229ce52914eSScott Mayhew 1230ddf529eeSNeilBrown if (cred && !cred->cr_ops->crmatch(&acred, cred, 0)) { 1231ddf529eeSNeilBrown put_rpccred(cred); 1232ddf529eeSNeilBrown ctx->ll_cred = NULL; 1233ddf529eeSNeilBrown cred = NULL; 1234ddf529eeSNeilBrown } 1235ddf529eeSNeilBrown if (!cred) 1236ddf529eeSNeilBrown cred = auth->au_ops->lookup_cred(auth, &acred, 0); 1237ddf529eeSNeilBrown if (!cred || IS_ERR(cred)) 1238ddf529eeSNeilBrown return true; 1239ddf529eeSNeilBrown ctx->ll_cred = cred; 1240ddf529eeSNeilBrown return !!(cred->cr_ops->crkey_timeout && 1241ddf529eeSNeilBrown cred->cr_ops->crkey_timeout(cred)); 1242dc24826bSAndy Adamson } 1243dc24826bSAndy Adamson 1244dc24826bSAndy Adamson /* 12455d47a356STrond Myklebust * If the page cache is marked as unsafe or invalid, then we can't rely on 12465d47a356STrond Myklebust * the PageUptodate() flag. In this case, we will need to turn off 12475d47a356STrond Myklebust * write optimisations that depend on the page contents being correct. 12485d47a356STrond Myklebust */ 12498d197a56STrond Myklebust static bool nfs_write_pageuptodate(struct page *page, struct inode *inode) 12505d47a356STrond Myklebust { 1251d529ef83SJeff Layton struct nfs_inode *nfsi = NFS_I(inode); 1252d529ef83SJeff Layton 12538d197a56STrond Myklebust if (nfs_have_delegated_attributes(inode)) 12548d197a56STrond Myklebust goto out; 125518dd78c4SScott Mayhew if (nfsi->cache_validity & NFS_INO_REVAL_PAGECACHE) 1256d529ef83SJeff Layton return false; 12574db72b40SJeff Layton smp_rmb(); 1258d529ef83SJeff Layton if (test_bit(NFS_INO_INVALIDATING, &nfsi->flags)) 12598d197a56STrond Myklebust return false; 12608d197a56STrond Myklebust out: 126118dd78c4SScott Mayhew if (nfsi->cache_validity & NFS_INO_INVALID_DATA) 126218dd78c4SScott Mayhew return false; 12638d197a56STrond Myklebust return PageUptodate(page) != 0; 12645d47a356STrond Myklebust } 12655d47a356STrond Myklebust 12665263e31eSJeff Layton static bool 12675263e31eSJeff Layton is_whole_file_wrlock(struct file_lock *fl) 12685263e31eSJeff Layton { 12695263e31eSJeff Layton return fl->fl_start == 0 && fl->fl_end == OFFSET_MAX && 12705263e31eSJeff Layton fl->fl_type == F_WRLCK; 12715263e31eSJeff Layton } 12725263e31eSJeff Layton 1273c7559663SScott Mayhew /* If we know the page is up to date, and we're not using byte range locks (or 1274c7559663SScott Mayhew * if we have the whole file locked for writing), it may be more efficient to 1275c7559663SScott Mayhew * extend the write to cover the entire page in order to avoid fragmentation 1276c7559663SScott Mayhew * inefficiencies. 1277c7559663SScott Mayhew * 1278263b4509SScott Mayhew * If the file is opened for synchronous writes then we can just skip the rest 1279263b4509SScott Mayhew * of the checks. 1280c7559663SScott Mayhew */ 1281c7559663SScott Mayhew static int nfs_can_extend_write(struct file *file, struct page *page, struct inode *inode) 1282c7559663SScott Mayhew { 12835263e31eSJeff Layton int ret; 12845263e31eSJeff Layton struct file_lock_context *flctx = inode->i_flctx; 12855263e31eSJeff Layton struct file_lock *fl; 12865263e31eSJeff Layton 1287c7559663SScott Mayhew if (file->f_flags & O_DSYNC) 1288c7559663SScott Mayhew return 0; 1289263b4509SScott Mayhew if (!nfs_write_pageuptodate(page, inode)) 1290263b4509SScott Mayhew return 0; 1291c7559663SScott Mayhew if (NFS_PROTO(inode)->have_delegation(inode, FMODE_WRITE)) 1292c7559663SScott Mayhew return 1; 1293bd61e0a9SJeff Layton if (!flctx || (list_empty_careful(&flctx->flc_flock) && 1294bd61e0a9SJeff Layton list_empty_careful(&flctx->flc_posix))) 12958fa4592aSTrond Myklebust return 1; 12965263e31eSJeff Layton 12975263e31eSJeff Layton /* Check to see if there are whole file write locks */ 12985263e31eSJeff Layton ret = 0; 12996109c850SJeff Layton spin_lock(&flctx->flc_lock); 1300bd61e0a9SJeff Layton if (!list_empty(&flctx->flc_posix)) { 1301bd61e0a9SJeff Layton fl = list_first_entry(&flctx->flc_posix, struct file_lock, 1302bd61e0a9SJeff Layton fl_list); 1303bd61e0a9SJeff Layton if (is_whole_file_wrlock(fl)) 13045263e31eSJeff Layton ret = 1; 1305bd61e0a9SJeff Layton } else if (!list_empty(&flctx->flc_flock)) { 13065263e31eSJeff Layton fl = list_first_entry(&flctx->flc_flock, struct file_lock, 13075263e31eSJeff Layton fl_list); 13085263e31eSJeff Layton if (fl->fl_type == F_WRLCK) 13095263e31eSJeff Layton ret = 1; 13105263e31eSJeff Layton } 13116109c850SJeff Layton spin_unlock(&flctx->flc_lock); 13125263e31eSJeff Layton return ret; 1313c7559663SScott Mayhew } 1314c7559663SScott Mayhew 13155d47a356STrond Myklebust /* 13161da177e4SLinus Torvalds * Update and possibly write a cached page of an NFS file. 13171da177e4SLinus Torvalds * 13181da177e4SLinus Torvalds * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad 13191da177e4SLinus Torvalds * things with a page scheduled for an RPC call (e.g. invalidate it). 13201da177e4SLinus Torvalds */ 13211da177e4SLinus Torvalds int nfs_updatepage(struct file *file, struct page *page, 13221da177e4SLinus Torvalds unsigned int offset, unsigned int count) 13231da177e4SLinus Torvalds { 1324cd3758e3STrond Myklebust struct nfs_open_context *ctx = nfs_file_open_context(file); 1325d2ceb7e5SBenjamin Coddington struct address_space *mapping = page_file_mapping(page); 1326d2ceb7e5SBenjamin Coddington struct inode *inode = mapping->host; 13271da177e4SLinus Torvalds int status = 0; 13281da177e4SLinus Torvalds 132991d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE); 133091d5b470SChuck Lever 13316de1472fSAl Viro dprintk("NFS: nfs_updatepage(%pD2 %d@%lld)\n", 13326de1472fSAl Viro file, count, (long long)(page_file_offset(page) + offset)); 13331da177e4SLinus Torvalds 1334149a4fddSBenjamin Coddington if (!count) 1335149a4fddSBenjamin Coddington goto out; 1336149a4fddSBenjamin Coddington 1337c7559663SScott Mayhew if (nfs_can_extend_write(file, page, inode)) { 133849a70f27STrond Myklebust count = max(count + offset, nfs_page_length(page)); 13391da177e4SLinus Torvalds offset = 0; 13401da177e4SLinus Torvalds } 13411da177e4SLinus Torvalds 1342e21195a7STrond Myklebust status = nfs_writepage_setup(ctx, page, offset, count); 134303fa9e84STrond Myklebust if (status < 0) 1344d2ceb7e5SBenjamin Coddington nfs_set_pageerror(mapping); 134559b7c05fSTrond Myklebust else 134659b7c05fSTrond Myklebust __set_page_dirty_nobuffers(page); 1347149a4fddSBenjamin Coddington out: 134848186c7dSChuck Lever dprintk("NFS: nfs_updatepage returns %d (isize %lld)\n", 13491da177e4SLinus Torvalds status, (long long)i_size_read(inode)); 13501da177e4SLinus Torvalds return status; 13511da177e4SLinus Torvalds } 13521da177e4SLinus Torvalds 13533ff7576dSTrond Myklebust static int flush_task_priority(int how) 13541da177e4SLinus Torvalds { 13551da177e4SLinus Torvalds switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) { 13561da177e4SLinus Torvalds case FLUSH_HIGHPRI: 13571da177e4SLinus Torvalds return RPC_PRIORITY_HIGH; 13581da177e4SLinus Torvalds case FLUSH_LOWPRI: 13591da177e4SLinus Torvalds return RPC_PRIORITY_LOW; 13601da177e4SLinus Torvalds } 13611da177e4SLinus Torvalds return RPC_PRIORITY_NORMAL; 13621da177e4SLinus Torvalds } 13631da177e4SLinus Torvalds 1364d45f60c6SWeston Andros Adamson static void nfs_initiate_write(struct nfs_pgio_header *hdr, 1365d45f60c6SWeston Andros Adamson struct rpc_message *msg, 1366abde71f4STom Haynes const struct nfs_rpc_ops *rpc_ops, 13671ed26f33SAnna Schumaker struct rpc_task_setup *task_setup_data, int how) 13681da177e4SLinus Torvalds { 13693ff7576dSTrond Myklebust int priority = flush_task_priority(how); 13701da177e4SLinus Torvalds 13711ed26f33SAnna Schumaker task_setup_data->priority = priority; 1372fb91fb0eSAnna Schumaker rpc_ops->write_setup(hdr, msg, &task_setup_data->rpc_client); 13735bb2a7cbSTrond Myklebust trace_nfs_initiate_write(hdr); 1374275acaafSTrond Myklebust } 1375275acaafSTrond Myklebust 13766d884e8fSFred /* If a nfs_flush_* function fails, it should remove reqs from @head and 13776d884e8fSFred * call this on each, which will prepare them to be retried on next 13786d884e8fSFred * writeback using standard nfs. 13796d884e8fSFred */ 13806d884e8fSFred static void nfs_redirty_request(struct nfs_page *req) 13816d884e8fSFred { 138233344e0fSTrond Myklebust /* Bump the transmission count */ 138333344e0fSTrond Myklebust req->wb_nio++; 13846d884e8fSFred nfs_mark_request_dirty(req); 13859fcd5960STrond Myklebust set_bit(NFS_CONTEXT_RESEND_WRITES, &nfs_req_openctx(req)->flags); 138620633f04SWeston Andros Adamson nfs_end_page_writeback(req); 13873aff4ebbSTrond Myklebust nfs_release_request(req); 13886d884e8fSFred } 13896d884e8fSFred 1390df3accb8STrond Myklebust static void nfs_async_write_error(struct list_head *head, int error) 13916c75dc0dSFred Isaman { 13926c75dc0dSFred Isaman struct nfs_page *req; 13936c75dc0dSFred Isaman 13946c75dc0dSFred Isaman while (!list_empty(head)) { 13956c75dc0dSFred Isaman req = nfs_list_entry(head->next); 13966c75dc0dSFred Isaman nfs_list_remove_request(req); 13976fbda89bSTrond Myklebust if (nfs_error_is_fatal(error)) 13986fbda89bSTrond Myklebust nfs_write_error(req, error); 13996fbda89bSTrond Myklebust else 14006c75dc0dSFred Isaman nfs_redirty_request(req); 14016c75dc0dSFred Isaman } 14026c75dc0dSFred Isaman } 14036c75dc0dSFred Isaman 1404dc602dd7STrond Myklebust static void nfs_async_write_reschedule_io(struct nfs_pgio_header *hdr) 1405dc602dd7STrond Myklebust { 1406df3accb8STrond Myklebust nfs_async_write_error(&hdr->pages, 0); 14077be7b3caSTrond Myklebust filemap_fdatawrite_range(hdr->inode->i_mapping, hdr->args.offset, 14087be7b3caSTrond Myklebust hdr->args.offset + hdr->args.count - 1); 1409dc602dd7STrond Myklebust } 1410dc602dd7STrond Myklebust 1411061ae2edSFred Isaman static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops = { 1412919e3bd9STrond Myklebust .init_hdr = nfs_async_write_init, 1413061ae2edSFred Isaman .error_cleanup = nfs_async_write_error, 1414061ae2edSFred Isaman .completion = nfs_write_completion, 1415dc602dd7STrond Myklebust .reschedule_io = nfs_async_write_reschedule_io, 1416061ae2edSFred Isaman }; 1417061ae2edSFred Isaman 141857208fa7SBryan Schumaker void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio, 1419a20c93e3SChristoph Hellwig struct inode *inode, int ioflags, bool force_mds, 1420061ae2edSFred Isaman const struct nfs_pgio_completion_ops *compl_ops) 14211751c363STrond Myklebust { 1422a20c93e3SChristoph Hellwig struct nfs_server *server = NFS_SERVER(inode); 142341d8d5b7SAnna Schumaker const struct nfs_pageio_ops *pg_ops = &nfs_pgio_rw_ops; 1424a20c93e3SChristoph Hellwig 1425a20c93e3SChristoph Hellwig #ifdef CONFIG_NFS_V4_1 1426a20c93e3SChristoph Hellwig if (server->pnfs_curr_ld && !force_mds) 1427a20c93e3SChristoph Hellwig pg_ops = server->pnfs_curr_ld->pg_write_ops; 1428a20c93e3SChristoph Hellwig #endif 14294a0de55cSAnna Schumaker nfs_pageio_init(pgio, inode, pg_ops, compl_ops, &nfs_rw_write_ops, 14303bde7afdSTrond Myklebust server->wsize, ioflags); 14311751c363STrond Myklebust } 1432ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_pageio_init_write); 14331751c363STrond Myklebust 1434dce81290STrond Myklebust void nfs_pageio_reset_write_mds(struct nfs_pageio_descriptor *pgio) 1435dce81290STrond Myklebust { 1436a7d42ddbSWeston Andros Adamson struct nfs_pgio_mirror *mirror; 1437a7d42ddbSWeston Andros Adamson 14386f29b9bbSKinglong Mee if (pgio->pg_ops && pgio->pg_ops->pg_cleanup) 14396f29b9bbSKinglong Mee pgio->pg_ops->pg_cleanup(pgio); 14406f29b9bbSKinglong Mee 144141d8d5b7SAnna Schumaker pgio->pg_ops = &nfs_pgio_rw_ops; 1442a7d42ddbSWeston Andros Adamson 1443a7d42ddbSWeston Andros Adamson nfs_pageio_stop_mirroring(pgio); 1444a7d42ddbSWeston Andros Adamson 1445a7d42ddbSWeston Andros Adamson mirror = &pgio->pg_mirrors[0]; 1446a7d42ddbSWeston Andros Adamson mirror->pg_bsize = NFS_SERVER(pgio->pg_inode)->wsize; 1447dce81290STrond Myklebust } 14481f945357STrond Myklebust EXPORT_SYMBOL_GPL(nfs_pageio_reset_write_mds); 1449dce81290STrond Myklebust 14501da177e4SLinus Torvalds 14510b7c0153SFred Isaman void nfs_commit_prepare(struct rpc_task *task, void *calldata) 14520b7c0153SFred Isaman { 14530b7c0153SFred Isaman struct nfs_commit_data *data = calldata; 14540b7c0153SFred Isaman 14550b7c0153SFred Isaman NFS_PROTO(data->inode)->commit_rpc_prepare(task, data); 14560b7c0153SFred Isaman } 14570b7c0153SFred Isaman 14581f2edbe3STrond Myklebust /* 14591f2edbe3STrond Myklebust * Special version of should_remove_suid() that ignores capabilities. 14601f2edbe3STrond Myklebust */ 14611f2edbe3STrond Myklebust static int nfs_should_remove_suid(const struct inode *inode) 14621f2edbe3STrond Myklebust { 14631f2edbe3STrond Myklebust umode_t mode = inode->i_mode; 14641f2edbe3STrond Myklebust int kill = 0; 1465788e7a89STrond Myklebust 14661f2edbe3STrond Myklebust /* suid always must be killed */ 14671f2edbe3STrond Myklebust if (unlikely(mode & S_ISUID)) 14681f2edbe3STrond Myklebust kill = ATTR_KILL_SUID; 14691f2edbe3STrond Myklebust 14701f2edbe3STrond Myklebust /* 14711f2edbe3STrond Myklebust * sgid without any exec bits is just a mandatory locking mark; leave 14721f2edbe3STrond Myklebust * it alone. If some exec bits are set, it's a real sgid; kill it. 14731f2edbe3STrond Myklebust */ 14741f2edbe3STrond Myklebust if (unlikely((mode & S_ISGID) && (mode & S_IXGRP))) 14751f2edbe3STrond Myklebust kill |= ATTR_KILL_SGID; 14761f2edbe3STrond Myklebust 14771f2edbe3STrond Myklebust if (unlikely(kill && S_ISREG(mode))) 14781f2edbe3STrond Myklebust return kill; 14791f2edbe3STrond Myklebust 14801f2edbe3STrond Myklebust return 0; 14811f2edbe3STrond Myklebust } 1482788e7a89STrond Myklebust 1483a08a8cd3STrond Myklebust static void nfs_writeback_check_extend(struct nfs_pgio_header *hdr, 1484a08a8cd3STrond Myklebust struct nfs_fattr *fattr) 1485a08a8cd3STrond Myklebust { 1486a08a8cd3STrond Myklebust struct nfs_pgio_args *argp = &hdr->args; 1487a08a8cd3STrond Myklebust struct nfs_pgio_res *resp = &hdr->res; 14882b83d3deSTrond Myklebust u64 size = argp->offset + resp->count; 1489a08a8cd3STrond Myklebust 1490a08a8cd3STrond Myklebust if (!(fattr->valid & NFS_ATTR_FATTR_SIZE)) 14912b83d3deSTrond Myklebust fattr->size = size; 14922b83d3deSTrond Myklebust if (nfs_size_to_loff_t(fattr->size) < i_size_read(hdr->inode)) { 14932b83d3deSTrond Myklebust fattr->valid &= ~NFS_ATTR_FATTR_SIZE; 1494a08a8cd3STrond Myklebust return; 14952b83d3deSTrond Myklebust } 14962b83d3deSTrond Myklebust if (size != fattr->size) 1497a08a8cd3STrond Myklebust return; 1498a08a8cd3STrond Myklebust /* Set attribute barrier */ 1499a08a8cd3STrond Myklebust nfs_fattr_set_barrier(fattr); 15002b83d3deSTrond Myklebust /* ...and update size */ 15012b83d3deSTrond Myklebust fattr->valid |= NFS_ATTR_FATTR_SIZE; 1502a08a8cd3STrond Myklebust } 1503a08a8cd3STrond Myklebust 1504a08a8cd3STrond Myklebust void nfs_writeback_update_inode(struct nfs_pgio_header *hdr) 1505a08a8cd3STrond Myklebust { 15062b83d3deSTrond Myklebust struct nfs_fattr *fattr = &hdr->fattr; 1507a08a8cd3STrond Myklebust struct inode *inode = hdr->inode; 1508a08a8cd3STrond Myklebust 1509a08a8cd3STrond Myklebust spin_lock(&inode->i_lock); 1510a08a8cd3STrond Myklebust nfs_writeback_check_extend(hdr, fattr); 1511a08a8cd3STrond Myklebust nfs_post_op_update_inode_force_wcc_locked(inode, fattr); 1512a08a8cd3STrond Myklebust spin_unlock(&inode->i_lock); 1513a08a8cd3STrond Myklebust } 1514a08a8cd3STrond Myklebust EXPORT_SYMBOL_GPL(nfs_writeback_update_inode); 1515a08a8cd3STrond Myklebust 15161da177e4SLinus Torvalds /* 15171da177e4SLinus Torvalds * This function is called when the WRITE call is complete. 15181da177e4SLinus Torvalds */ 1519d45f60c6SWeston Andros Adamson static int nfs_writeback_done(struct rpc_task *task, 1520d45f60c6SWeston Andros Adamson struct nfs_pgio_header *hdr, 15210eecb214SAnna Schumaker struct inode *inode) 15221da177e4SLinus Torvalds { 1523788e7a89STrond Myklebust int status; 15241da177e4SLinus Torvalds 1525f551e44fSChuck Lever /* 1526f551e44fSChuck Lever * ->write_done will attempt to use post-op attributes to detect 1527f551e44fSChuck Lever * conflicting writes by other clients. A strict interpretation 1528f551e44fSChuck Lever * of close-to-open would allow us to continue caching even if 1529f551e44fSChuck Lever * another writer had changed the file, but some applications 1530f551e44fSChuck Lever * depend on tighter cache coherency when writing. 1531f551e44fSChuck Lever */ 1532d45f60c6SWeston Andros Adamson status = NFS_PROTO(inode)->write_done(task, hdr); 1533788e7a89STrond Myklebust if (status != 0) 15340eecb214SAnna Schumaker return status; 15358224b273SChuck Lever 1536d45f60c6SWeston Andros Adamson nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, hdr->res.count); 15375bb2a7cbSTrond Myklebust trace_nfs_writeback_done(task, hdr); 153891d5b470SChuck Lever 1539d45f60c6SWeston Andros Adamson if (hdr->res.verf->committed < hdr->args.stable && 1540d45f60c6SWeston Andros Adamson task->tk_status >= 0) { 15411da177e4SLinus Torvalds /* We tried a write call, but the server did not 15421da177e4SLinus Torvalds * commit data to stable storage even though we 15431da177e4SLinus Torvalds * requested it. 15441da177e4SLinus Torvalds * Note: There is a known bug in Tru64 < 5.0 in which 15451da177e4SLinus Torvalds * the server reports NFS_DATA_SYNC, but performs 15461da177e4SLinus Torvalds * NFS_FILE_SYNC. We therefore implement this checking 15471da177e4SLinus Torvalds * as a dprintk() in order to avoid filling syslog. 15481da177e4SLinus Torvalds */ 15491da177e4SLinus Torvalds static unsigned long complain; 15501da177e4SLinus Torvalds 1551a69aef14SFred Isaman /* Note this will print the MDS for a DS write */ 15521da177e4SLinus Torvalds if (time_before(complain, jiffies)) { 15531da177e4SLinus Torvalds dprintk("NFS: faulty NFS server %s:" 15541da177e4SLinus Torvalds " (committed = %d) != (stable = %d)\n", 1555cd841605SFred Isaman NFS_SERVER(inode)->nfs_client->cl_hostname, 1556d45f60c6SWeston Andros Adamson hdr->res.verf->committed, hdr->args.stable); 15571da177e4SLinus Torvalds complain = jiffies + 300 * HZ; 15581da177e4SLinus Torvalds } 15591da177e4SLinus Torvalds } 15601f2edbe3STrond Myklebust 15611f2edbe3STrond Myklebust /* Deal with the suid/sgid bit corner case */ 156216e14375STrond Myklebust if (nfs_should_remove_suid(inode)) { 156316e14375STrond Myklebust spin_lock(&inode->i_lock); 156416e14375STrond Myklebust NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER; 156516e14375STrond Myklebust spin_unlock(&inode->i_lock); 156616e14375STrond Myklebust } 15670eecb214SAnna Schumaker return 0; 15680eecb214SAnna Schumaker } 15690eecb214SAnna Schumaker 15700eecb214SAnna Schumaker /* 15710eecb214SAnna Schumaker * This function is called when the WRITE call is complete. 15720eecb214SAnna Schumaker */ 1573d45f60c6SWeston Andros Adamson static void nfs_writeback_result(struct rpc_task *task, 1574d45f60c6SWeston Andros Adamson struct nfs_pgio_header *hdr) 15750eecb214SAnna Schumaker { 1576d45f60c6SWeston Andros Adamson struct nfs_pgio_args *argp = &hdr->args; 1577d45f60c6SWeston Andros Adamson struct nfs_pgio_res *resp = &hdr->res; 15781f2edbe3STrond Myklebust 15791f2edbe3STrond Myklebust if (resp->count < argp->count) { 15801da177e4SLinus Torvalds static unsigned long complain; 15811da177e4SLinus Torvalds 15826c75dc0dSFred Isaman /* This a short write! */ 1583d45f60c6SWeston Andros Adamson nfs_inc_stats(hdr->inode, NFSIOS_SHORTWRITE); 158491d5b470SChuck Lever 15851da177e4SLinus Torvalds /* Has the server at least made some progress? */ 15866c75dc0dSFred Isaman if (resp->count == 0) { 15876c75dc0dSFred Isaman if (time_before(complain, jiffies)) { 15886c75dc0dSFred Isaman printk(KERN_WARNING 15896c75dc0dSFred Isaman "NFS: Server wrote zero bytes, expected %u.\n", 15906c75dc0dSFred Isaman argp->count); 15916c75dc0dSFred Isaman complain = jiffies + 300 * HZ; 15926c75dc0dSFred Isaman } 1593d45f60c6SWeston Andros Adamson nfs_set_pgio_error(hdr, -EIO, argp->offset); 15946c75dc0dSFred Isaman task->tk_status = -EIO; 15956c75dc0dSFred Isaman return; 15966c75dc0dSFred Isaman } 1597f8417b48SKinglong Mee 1598f8417b48SKinglong Mee /* For non rpc-based layout drivers, retry-through-MDS */ 1599f8417b48SKinglong Mee if (!task->tk_ops) { 1600f8417b48SKinglong Mee hdr->pnfs_error = -EAGAIN; 1601f8417b48SKinglong Mee return; 1602f8417b48SKinglong Mee } 1603f8417b48SKinglong Mee 16041da177e4SLinus Torvalds /* Was this an NFSv2 write or an NFSv3 stable write? */ 16051da177e4SLinus Torvalds if (resp->verf->committed != NFS_UNSTABLE) { 16061da177e4SLinus Torvalds /* Resend from where the server left off */ 1607d45f60c6SWeston Andros Adamson hdr->mds_offset += resp->count; 16081da177e4SLinus Torvalds argp->offset += resp->count; 16091da177e4SLinus Torvalds argp->pgbase += resp->count; 16101da177e4SLinus Torvalds argp->count -= resp->count; 16111da177e4SLinus Torvalds } else { 16121da177e4SLinus Torvalds /* Resend as a stable write in order to avoid 16131da177e4SLinus Torvalds * headaches in the case of a server crash. 16141da177e4SLinus Torvalds */ 16151da177e4SLinus Torvalds argp->stable = NFS_FILE_SYNC; 16161da177e4SLinus Torvalds } 16178c9cb714STrond Myklebust resp->count = 0; 16188c9cb714STrond Myklebust resp->verf->committed = 0; 1619d00c5d43STrond Myklebust rpc_restart_call_prepare(task); 16201da177e4SLinus Torvalds } 16211da177e4SLinus Torvalds } 16221da177e4SLinus Torvalds 1623af7cf057STrond Myklebust static int wait_on_commit(struct nfs_mds_commit_info *cinfo) 162471d0a611STrond Myklebust { 1625723c921eSPeter Zijlstra return wait_var_event_killable(&cinfo->rpcs_out, 1626723c921eSPeter Zijlstra !atomic_read(&cinfo->rpcs_out)); 1627af7cf057STrond Myklebust } 1628af7cf057STrond Myklebust 1629af7cf057STrond Myklebust static void nfs_commit_begin(struct nfs_mds_commit_info *cinfo) 1630af7cf057STrond Myklebust { 1631af7cf057STrond Myklebust atomic_inc(&cinfo->rpcs_out); 1632af7cf057STrond Myklebust } 1633af7cf057STrond Myklebust 1634af7cf057STrond Myklebust static void nfs_commit_end(struct nfs_mds_commit_info *cinfo) 1635af7cf057STrond Myklebust { 1636af7cf057STrond Myklebust if (atomic_dec_and_test(&cinfo->rpcs_out)) 1637723c921eSPeter Zijlstra wake_up_var(&cinfo->rpcs_out); 163871d0a611STrond Myklebust } 163971d0a611STrond Myklebust 16400b7c0153SFred Isaman void nfs_commitdata_release(struct nfs_commit_data *data) 16411da177e4SLinus Torvalds { 16420b7c0153SFred Isaman put_nfs_open_context(data->context); 16430b7c0153SFred Isaman nfs_commit_free(data); 16441da177e4SLinus Torvalds } 1645e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_commitdata_release); 16461da177e4SLinus Torvalds 16470b7c0153SFred Isaman int nfs_initiate_commit(struct rpc_clnt *clnt, struct nfs_commit_data *data, 1648c36aae9aSPeng Tao const struct nfs_rpc_ops *nfs_ops, 16499ace33cdSFred Isaman const struct rpc_call_ops *call_ops, 16509f0ec176SAndy Adamson int how, int flags) 16511da177e4SLinus Torvalds { 165207737691STrond Myklebust struct rpc_task *task; 16539ace33cdSFred Isaman int priority = flush_task_priority(how); 1654bdc7f021STrond Myklebust struct rpc_message msg = { 1655bdc7f021STrond Myklebust .rpc_argp = &data->args, 1656bdc7f021STrond Myklebust .rpc_resp = &data->res, 16579ace33cdSFred Isaman .rpc_cred = data->cred, 1658bdc7f021STrond Myklebust }; 165984115e1cSTrond Myklebust struct rpc_task_setup task_setup_data = { 166007737691STrond Myklebust .task = &data->task, 16619ace33cdSFred Isaman .rpc_client = clnt, 1662bdc7f021STrond Myklebust .rpc_message = &msg, 16639ace33cdSFred Isaman .callback_ops = call_ops, 166484115e1cSTrond Myklebust .callback_data = data, 1665101070caSTrond Myklebust .workqueue = nfsiod_workqueue, 1666542b994bSTrond Myklebust .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF | flags, 16673ff7576dSTrond Myklebust .priority = priority, 166884115e1cSTrond Myklebust }; 1669788e7a89STrond Myklebust /* Set up the initial task struct. */ 1670e9ae1ee2SAnna Schumaker nfs_ops->commit_setup(data, &msg, &task_setup_data.rpc_client); 16718224b273SChuck Lever trace_nfs_initiate_commit(data); 16721da177e4SLinus Torvalds 1673b4839ebeSKinglong Mee dprintk("NFS: initiated commit call\n"); 1674bdc7f021STrond Myklebust 167507737691STrond Myklebust task = rpc_run_task(&task_setup_data); 1676dbae4c73STrond Myklebust if (IS_ERR(task)) 1677dbae4c73STrond Myklebust return PTR_ERR(task); 1678d2224e7aSJeff Layton if (how & FLUSH_SYNC) 1679d2224e7aSJeff Layton rpc_wait_for_completion_task(task); 168007737691STrond Myklebust rpc_put_task(task); 1681dbae4c73STrond Myklebust return 0; 16821da177e4SLinus Torvalds } 1683e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_initiate_commit); 16841da177e4SLinus Torvalds 1685378520b8SPeng Tao static loff_t nfs_get_lwb(struct list_head *head) 1686378520b8SPeng Tao { 1687378520b8SPeng Tao loff_t lwb = 0; 1688378520b8SPeng Tao struct nfs_page *req; 1689378520b8SPeng Tao 1690378520b8SPeng Tao list_for_each_entry(req, head, wb_list) 1691378520b8SPeng Tao if (lwb < (req_offset(req) + req->wb_bytes)) 1692378520b8SPeng Tao lwb = req_offset(req) + req->wb_bytes; 1693378520b8SPeng Tao 1694378520b8SPeng Tao return lwb; 1695378520b8SPeng Tao } 1696378520b8SPeng Tao 16971da177e4SLinus Torvalds /* 16989ace33cdSFred Isaman * Set up the argument/result storage required for the RPC call. 16999ace33cdSFred Isaman */ 17000b7c0153SFred Isaman void nfs_init_commit(struct nfs_commit_data *data, 1701988b6dceSFred Isaman struct list_head *head, 1702f453a54aSFred Isaman struct pnfs_layout_segment *lseg, 1703f453a54aSFred Isaman struct nfs_commit_info *cinfo) 17049ace33cdSFred Isaman { 170519573c93STrond Myklebust struct nfs_page *first; 170619573c93STrond Myklebust struct nfs_open_context *ctx; 170719573c93STrond Myklebust struct inode *inode; 17089ace33cdSFred Isaman 17099ace33cdSFred Isaman /* Set up the RPC argument and reply structs 17109ace33cdSFred Isaman * NB: take care not to mess about with data->commit et al. */ 17119ace33cdSFred Isaman 171219573c93STrond Myklebust if (head) 17139ace33cdSFred Isaman list_splice_init(head, &data->pages); 17149ace33cdSFred Isaman 171519573c93STrond Myklebust first = nfs_list_entry(data->pages.next); 171619573c93STrond Myklebust ctx = nfs_req_openctx(first); 171719573c93STrond Myklebust inode = d_inode(ctx->dentry); 171819573c93STrond Myklebust 17199ace33cdSFred Isaman data->inode = inode; 17209fcd5960STrond Myklebust data->cred = ctx->cred; 1721988b6dceSFred Isaman data->lseg = lseg; /* reference transferred */ 1722378520b8SPeng Tao /* only set lwb for pnfs commit */ 1723378520b8SPeng Tao if (lseg) 1724378520b8SPeng Tao data->lwb = nfs_get_lwb(&data->pages); 17259ace33cdSFred Isaman data->mds_ops = &nfs_commit_ops; 1726f453a54aSFred Isaman data->completion_ops = cinfo->completion_ops; 1727b359f9d0SFred Isaman data->dreq = cinfo->dreq; 17289ace33cdSFred Isaman 17299ace33cdSFred Isaman data->args.fh = NFS_FH(data->inode); 17309ace33cdSFred Isaman /* Note: we always request a commit of the entire inode */ 17319ace33cdSFred Isaman data->args.offset = 0; 17329ace33cdSFred Isaman data->args.count = 0; 17339fcd5960STrond Myklebust data->context = get_nfs_open_context(ctx); 17349ace33cdSFred Isaman data->res.fattr = &data->fattr; 17359ace33cdSFred Isaman data->res.verf = &data->verf; 17369ace33cdSFred Isaman nfs_fattr_init(&data->fattr); 17379ace33cdSFred Isaman } 1738e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_init_commit); 17399ace33cdSFred Isaman 1740e0c2b380SFred Isaman void nfs_retry_commit(struct list_head *page_list, 1741ea2cf228SFred Isaman struct pnfs_layout_segment *lseg, 1742b57ff130SWeston Andros Adamson struct nfs_commit_info *cinfo, 1743b57ff130SWeston Andros Adamson u32 ds_commit_idx) 174464bfeb49SFred Isaman { 174564bfeb49SFred Isaman struct nfs_page *req; 174664bfeb49SFred Isaman 174764bfeb49SFred Isaman while (!list_empty(page_list)) { 174864bfeb49SFred Isaman req = nfs_list_entry(page_list->next); 174964bfeb49SFred Isaman nfs_list_remove_request(req); 1750b57ff130SWeston Andros Adamson nfs_mark_request_commit(req, lseg, cinfo, ds_commit_idx); 1751487b9b8aSTom Haynes if (!cinfo->dreq) 1752487b9b8aSTom Haynes nfs_clear_page_commit(req->wb_page); 17531d1afcbcSTrond Myklebust nfs_unlock_and_release_request(req); 175464bfeb49SFred Isaman } 175564bfeb49SFred Isaman } 1756e0c2b380SFred Isaman EXPORT_SYMBOL_GPL(nfs_retry_commit); 175764bfeb49SFred Isaman 1758b20135d0STrond Myklebust static void 1759b20135d0STrond Myklebust nfs_commit_resched_write(struct nfs_commit_info *cinfo, 1760b20135d0STrond Myklebust struct nfs_page *req) 1761b20135d0STrond Myklebust { 1762b20135d0STrond Myklebust __set_page_dirty_nobuffers(req->wb_page); 1763b20135d0STrond Myklebust } 1764b20135d0STrond Myklebust 17659ace33cdSFred Isaman /* 17661da177e4SLinus Torvalds * Commit dirty pages 17671da177e4SLinus Torvalds */ 17681da177e4SLinus Torvalds static int 1769ea2cf228SFred Isaman nfs_commit_list(struct inode *inode, struct list_head *head, int how, 1770ea2cf228SFred Isaman struct nfs_commit_info *cinfo) 17711da177e4SLinus Torvalds { 17720b7c0153SFred Isaman struct nfs_commit_data *data; 17731da177e4SLinus Torvalds 1774ade8febdSWeston Andros Adamson /* another commit raced with us */ 1775ade8febdSWeston Andros Adamson if (list_empty(head)) 1776ade8febdSWeston Andros Adamson return 0; 1777ade8febdSWeston Andros Adamson 1778518662e0SNeilBrown data = nfs_commitdata_alloc(true); 17791da177e4SLinus Torvalds 17801da177e4SLinus Torvalds /* Set up the argument struct */ 1781f453a54aSFred Isaman nfs_init_commit(data, head, NULL, cinfo); 1782f453a54aSFred Isaman atomic_inc(&cinfo->mds->rpcs_out); 1783c36aae9aSPeng Tao return nfs_initiate_commit(NFS_CLIENT(inode), data, NFS_PROTO(inode), 1784c36aae9aSPeng Tao data->mds_ops, how, 0); 17851da177e4SLinus Torvalds } 17861da177e4SLinus Torvalds 17871da177e4SLinus Torvalds /* 17881da177e4SLinus Torvalds * COMMIT call returned 17891da177e4SLinus Torvalds */ 1790788e7a89STrond Myklebust static void nfs_commit_done(struct rpc_task *task, void *calldata) 17911da177e4SLinus Torvalds { 17920b7c0153SFred Isaman struct nfs_commit_data *data = calldata; 17931da177e4SLinus Torvalds 1794a3f565b1SChuck Lever dprintk("NFS: %5u nfs_commit_done (status %d)\n", 17951da177e4SLinus Torvalds task->tk_pid, task->tk_status); 17961da177e4SLinus Torvalds 1797788e7a89STrond Myklebust /* Call the NFS version-specific code */ 1798c0d0e96bSTrond Myklebust NFS_PROTO(data->inode)->commit_done(task, data); 17997bdd297eSTrond Myklebust trace_nfs_commit_done(task, data); 1800c9d8f89dSTrond Myklebust } 1801c9d8f89dSTrond Myklebust 1802f453a54aSFred Isaman static void nfs_commit_release_pages(struct nfs_commit_data *data) 1803c9d8f89dSTrond Myklebust { 1804221203ceSTrond Myklebust const struct nfs_writeverf *verf = data->res.verf; 1805c9d8f89dSTrond Myklebust struct nfs_page *req; 1806c9d8f89dSTrond Myklebust int status = data->task.tk_status; 1807f453a54aSFred Isaman struct nfs_commit_info cinfo; 1808353db796SNeilBrown struct nfs_server *nfss; 1809788e7a89STrond Myklebust 18101da177e4SLinus Torvalds while (!list_empty(&data->pages)) { 18111da177e4SLinus Torvalds req = nfs_list_entry(data->pages.next); 18121da177e4SLinus Torvalds nfs_list_remove_request(req); 181367911c8fSAnna Schumaker if (req->wb_page) 1814d6d6dc7cSFred Isaman nfs_clear_page_commit(req->wb_page); 18151da177e4SLinus Torvalds 18161e8968c5SNiels de Vos dprintk("NFS: commit (%s/%llu %d@%lld)", 18179fcd5960STrond Myklebust nfs_req_openctx(req)->dentry->d_sb->s_id, 18189fcd5960STrond Myklebust (unsigned long long)NFS_FILEID(d_inode(nfs_req_openctx(req)->dentry)), 18191da177e4SLinus Torvalds req->wb_bytes, 18201da177e4SLinus Torvalds (long long)req_offset(req)); 1821c9d8f89dSTrond Myklebust if (status < 0) { 18226fbda89bSTrond Myklebust if (req->wb_page) { 1823861e1671SChuck Lever trace_nfs_commit_error(req, status); 18246fbda89bSTrond Myklebust nfs_mapping_set_error(req->wb_page, status); 18251da177e4SLinus Torvalds nfs_inode_remove_request(req); 18266fbda89bSTrond Myklebust } 1827ddeaa637SJoe Perches dprintk_cont(", error = %d\n", status); 18281da177e4SLinus Torvalds goto next; 18291da177e4SLinus Torvalds } 18301da177e4SLinus Torvalds 18311da177e4SLinus Torvalds /* Okay, COMMIT succeeded, apparently. Check the verifier 18321da177e4SLinus Torvalds * returned by the server against all stored verfs. */ 18331f28476dSTrond Myklebust if (nfs_write_match_verf(verf, req)) { 18341da177e4SLinus Torvalds /* We have a match */ 183538a33101SKinglong Mee if (req->wb_page) 18361da177e4SLinus Torvalds nfs_inode_remove_request(req); 1837ddeaa637SJoe Perches dprintk_cont(" OK\n"); 18381da177e4SLinus Torvalds goto next; 18391da177e4SLinus Torvalds } 18401da177e4SLinus Torvalds /* We have a mismatch. Write the page again */ 1841ddeaa637SJoe Perches dprintk_cont(" mismatch\n"); 18426d884e8fSFred nfs_mark_request_dirty(req); 18439fcd5960STrond Myklebust set_bit(NFS_CONTEXT_RESEND_WRITES, &nfs_req_openctx(req)->flags); 18441da177e4SLinus Torvalds next: 18451d1afcbcSTrond Myklebust nfs_unlock_and_release_request(req); 18467f1bda44STrond Myklebust /* Latency breaker */ 18477f1bda44STrond Myklebust cond_resched(); 18481da177e4SLinus Torvalds } 1849353db796SNeilBrown nfss = NFS_SERVER(data->inode); 1850353db796SNeilBrown if (atomic_long_read(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH) 18510db10944SJan Kara clear_bdi_congested(inode_to_bdi(data->inode), BLK_RW_ASYNC); 1852353db796SNeilBrown 1853f453a54aSFred Isaman nfs_init_cinfo(&cinfo, data->inode, data->dreq); 1854af7cf057STrond Myklebust nfs_commit_end(cinfo.mds); 18555917ce84SFred Isaman } 18565917ce84SFred Isaman 18575917ce84SFred Isaman static void nfs_commit_release(void *calldata) 18585917ce84SFred Isaman { 18590b7c0153SFred Isaman struct nfs_commit_data *data = calldata; 18605917ce84SFred Isaman 1861f453a54aSFred Isaman data->completion_ops->completion(data); 1862c9d8f89dSTrond Myklebust nfs_commitdata_release(calldata); 18631da177e4SLinus Torvalds } 1864788e7a89STrond Myklebust 1865788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops = { 18660b7c0153SFred Isaman .rpc_call_prepare = nfs_commit_prepare, 1867788e7a89STrond Myklebust .rpc_call_done = nfs_commit_done, 1868788e7a89STrond Myklebust .rpc_release = nfs_commit_release, 1869788e7a89STrond Myklebust }; 18701da177e4SLinus Torvalds 1871f453a54aSFred Isaman static const struct nfs_commit_completion_ops nfs_commit_completion_ops = { 1872f453a54aSFred Isaman .completion = nfs_commit_release_pages, 1873b20135d0STrond Myklebust .resched_write = nfs_commit_resched_write, 1874f453a54aSFred Isaman }; 1875f453a54aSFred Isaman 18761763da12SFred Isaman int nfs_generic_commit_list(struct inode *inode, struct list_head *head, 1877ea2cf228SFred Isaman int how, struct nfs_commit_info *cinfo) 187884c53ab5SFred Isaman { 187984c53ab5SFred Isaman int status; 188084c53ab5SFred Isaman 1881ea2cf228SFred Isaman status = pnfs_commit_list(inode, head, how, cinfo); 188284c53ab5SFred Isaman if (status == PNFS_NOT_ATTEMPTED) 1883ea2cf228SFred Isaman status = nfs_commit_list(inode, head, how, cinfo); 188484c53ab5SFred Isaman return status; 188584c53ab5SFred Isaman } 188684c53ab5SFred Isaman 1887c4f24df9STrond Myklebust static int __nfs_commit_inode(struct inode *inode, int how, 1888c4f24df9STrond Myklebust struct writeback_control *wbc) 18891da177e4SLinus Torvalds { 18901da177e4SLinus Torvalds LIST_HEAD(head); 1891ea2cf228SFred Isaman struct nfs_commit_info cinfo; 189271d0a611STrond Myklebust int may_wait = how & FLUSH_SYNC; 1893c4f24df9STrond Myklebust int ret, nscan; 18941da177e4SLinus Torvalds 1895ea2cf228SFred Isaman nfs_init_cinfo_from_inode(&cinfo, inode); 1896af7cf057STrond Myklebust nfs_commit_begin(cinfo.mds); 1897c4f24df9STrond Myklebust for (;;) { 1898c4f24df9STrond Myklebust ret = nscan = nfs_scan_commit(inode, &head, &cinfo); 1899c4f24df9STrond Myklebust if (ret <= 0) 1900c4f24df9STrond Myklebust break; 1901c4f24df9STrond Myklebust ret = nfs_generic_commit_list(inode, &head, how, &cinfo); 1902c4f24df9STrond Myklebust if (ret < 0) 1903c4f24df9STrond Myklebust break; 1904c4f24df9STrond Myklebust ret = 0; 1905c4f24df9STrond Myklebust if (wbc && wbc->sync_mode == WB_SYNC_NONE) { 1906c4f24df9STrond Myklebust if (nscan < wbc->nr_to_write) 1907c4f24df9STrond Myklebust wbc->nr_to_write -= nscan; 1908c4f24df9STrond Myklebust else 1909c4f24df9STrond Myklebust wbc->nr_to_write = 0; 1910c4f24df9STrond Myklebust } 1911c4f24df9STrond Myklebust if (nscan < INT_MAX) 1912c4f24df9STrond Myklebust break; 1913c4f24df9STrond Myklebust cond_resched(); 1914c4f24df9STrond Myklebust } 1915af7cf057STrond Myklebust nfs_commit_end(cinfo.mds); 1916c4f24df9STrond Myklebust if (ret || !may_wait) 1917c4f24df9STrond Myklebust return ret; 1918c4f24df9STrond Myklebust return wait_on_commit(cinfo.mds); 1919c4f24df9STrond Myklebust } 1920c4f24df9STrond Myklebust 1921c4f24df9STrond Myklebust int nfs_commit_inode(struct inode *inode, int how) 1922c4f24df9STrond Myklebust { 1923c4f24df9STrond Myklebust return __nfs_commit_inode(inode, how, NULL); 19241da177e4SLinus Torvalds } 1925b20135d0STrond Myklebust EXPORT_SYMBOL_GPL(nfs_commit_inode); 19268fc795f7STrond Myklebust 1927ae09c31fSAnna Schumaker int nfs_write_inode(struct inode *inode, struct writeback_control *wbc) 19288fc795f7STrond Myklebust { 1929420e3646STrond Myklebust struct nfs_inode *nfsi = NFS_I(inode); 1930420e3646STrond Myklebust int flags = FLUSH_SYNC; 1931420e3646STrond Myklebust int ret = 0; 19328fc795f7STrond Myklebust 1933c4f24df9STrond Myklebust if (wbc->sync_mode == WB_SYNC_NONE) { 19343236c3e1SJeff Layton /* no commits means nothing needs to be done */ 19355cb953d4STrond Myklebust if (!atomic_long_read(&nfsi->commit_info.ncommit)) 1936c4f24df9STrond Myklebust goto check_requests_outstanding; 19373236c3e1SJeff Layton 1938a00dd6c0SJeff Layton /* Don't commit yet if this is a non-blocking flush and there 1939a00dd6c0SJeff Layton * are a lot of outstanding writes for this mapping. 1940420e3646STrond Myklebust */ 19411a4edf0fSTrond Myklebust if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK)) 1942420e3646STrond Myklebust goto out_mark_dirty; 1943420e3646STrond Myklebust 1944a00dd6c0SJeff Layton /* don't wait for the COMMIT response */ 1945420e3646STrond Myklebust flags = 0; 1946a00dd6c0SJeff Layton } 1947a00dd6c0SJeff Layton 1948c4f24df9STrond Myklebust ret = __nfs_commit_inode(inode, flags, wbc); 1949c4f24df9STrond Myklebust if (!ret) { 1950c4f24df9STrond Myklebust if (flags & FLUSH_SYNC) 19518fc795f7STrond Myklebust return 0; 1952c4f24df9STrond Myklebust } else if (atomic_long_read(&nfsi->commit_info.ncommit)) 1953c4f24df9STrond Myklebust goto out_mark_dirty; 1954c4f24df9STrond Myklebust 1955c4f24df9STrond Myklebust check_requests_outstanding: 1956c4f24df9STrond Myklebust if (!atomic_read(&nfsi->commit_info.rpcs_out)) 1957c4f24df9STrond Myklebust return ret; 1958420e3646STrond Myklebust out_mark_dirty: 19598fc795f7STrond Myklebust __mark_inode_dirty(inode, I_DIRTY_DATASYNC); 19608fc795f7STrond Myklebust return ret; 19618fc795f7STrond Myklebust } 196289d77c8fSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_write_inode); 1963863a3c6cSAndy Adamson 1964acdc53b2STrond Myklebust /* 1965837bb1d7STrond Myklebust * Wrapper for filemap_write_and_wait_range() 1966837bb1d7STrond Myklebust * 1967837bb1d7STrond Myklebust * Needed for pNFS in order to ensure data becomes visible to the 1968837bb1d7STrond Myklebust * client. 1969837bb1d7STrond Myklebust */ 1970837bb1d7STrond Myklebust int nfs_filemap_write_and_wait_range(struct address_space *mapping, 1971837bb1d7STrond Myklebust loff_t lstart, loff_t lend) 1972837bb1d7STrond Myklebust { 1973837bb1d7STrond Myklebust int ret; 1974837bb1d7STrond Myklebust 1975837bb1d7STrond Myklebust ret = filemap_write_and_wait_range(mapping, lstart, lend); 1976837bb1d7STrond Myklebust if (ret == 0) 1977837bb1d7STrond Myklebust ret = pnfs_sync_inode(mapping->host, true); 1978837bb1d7STrond Myklebust return ret; 1979837bb1d7STrond Myklebust } 1980837bb1d7STrond Myklebust EXPORT_SYMBOL_GPL(nfs_filemap_write_and_wait_range); 1981837bb1d7STrond Myklebust 1982837bb1d7STrond Myklebust /* 1983acdc53b2STrond Myklebust * flush the inode to disk. 1984acdc53b2STrond Myklebust */ 1985acdc53b2STrond Myklebust int nfs_wb_all(struct inode *inode) 198634901f70STrond Myklebust { 1987f4ce1299STrond Myklebust int ret; 198834901f70STrond Myklebust 1989f4ce1299STrond Myklebust trace_nfs_writeback_inode_enter(inode); 1990f4ce1299STrond Myklebust 19915bb89b47STrond Myklebust ret = filemap_write_and_wait(inode->i_mapping); 19926b196875SChuck Lever if (ret) 19936b196875SChuck Lever goto out; 19945bb89b47STrond Myklebust ret = nfs_commit_inode(inode, FLUSH_SYNC); 19956b196875SChuck Lever if (ret < 0) 19966b196875SChuck Lever goto out; 19975bb89b47STrond Myklebust pnfs_sync_inode(inode, true); 19986b196875SChuck Lever ret = 0; 1999f4ce1299STrond Myklebust 20006b196875SChuck Lever out: 2001f4ce1299STrond Myklebust trace_nfs_writeback_inode_exit(inode, ret); 2002f4ce1299STrond Myklebust return ret; 20031c75950bSTrond Myklebust } 2004ddda8e0aSBryan Schumaker EXPORT_SYMBOL_GPL(nfs_wb_all); 20051c75950bSTrond Myklebust 20061b3b4a1aSTrond Myklebust int nfs_wb_page_cancel(struct inode *inode, struct page *page) 20071b3b4a1aSTrond Myklebust { 20081b3b4a1aSTrond Myklebust struct nfs_page *req; 20091b3b4a1aSTrond Myklebust int ret = 0; 20101b3b4a1aSTrond Myklebust 2011ba8b06e6STrond Myklebust wait_on_page_writeback(page); 20123e217045SWeston Andros Adamson 20133e217045SWeston Andros Adamson /* blocking call to cancel all requests and join to a single (head) 20143e217045SWeston Andros Adamson * request */ 20156d17d653STrond Myklebust req = nfs_lock_and_join_requests(page); 20163e217045SWeston Andros Adamson 20173e217045SWeston Andros Adamson if (IS_ERR(req)) { 20183e217045SWeston Andros Adamson ret = PTR_ERR(req); 20193e217045SWeston Andros Adamson } else if (req) { 20203e217045SWeston Andros Adamson /* all requests from this page have been cancelled by 20213e217045SWeston Andros Adamson * nfs_lock_and_join_requests, so just remove the head 20223e217045SWeston Andros Adamson * request from the inode / page_private pointer and 20233e217045SWeston Andros Adamson * release it */ 20241b3b4a1aSTrond Myklebust nfs_inode_remove_request(req); 20251d1afcbcSTrond Myklebust nfs_unlock_and_release_request(req); 20261b3b4a1aSTrond Myklebust } 20273e217045SWeston Andros Adamson 20281b3b4a1aSTrond Myklebust return ret; 20291b3b4a1aSTrond Myklebust } 20301b3b4a1aSTrond Myklebust 20311c75950bSTrond Myklebust /* 20321c75950bSTrond Myklebust * Write back all requests on one page - we do this before reading it. 20331c75950bSTrond Myklebust */ 2034c373fff7STrond Myklebust int nfs_wb_page(struct inode *inode, struct page *page) 20351c75950bSTrond Myklebust { 203629418aa4SMel Gorman loff_t range_start = page_file_offset(page); 203709cbfeafSKirill A. Shutemov loff_t range_end = range_start + (loff_t)(PAGE_SIZE - 1); 20387f2f12d9STrond Myklebust struct writeback_control wbc = { 20397f2f12d9STrond Myklebust .sync_mode = WB_SYNC_ALL, 20407f2f12d9STrond Myklebust .nr_to_write = 0, 20417f2f12d9STrond Myklebust .range_start = range_start, 20427f2f12d9STrond Myklebust .range_end = range_end, 20437f2f12d9STrond Myklebust }; 20447f2f12d9STrond Myklebust int ret; 20457f2f12d9STrond Myklebust 2046f4ce1299STrond Myklebust trace_nfs_writeback_page_enter(inode); 2047f4ce1299STrond Myklebust 20480522f6adSTrond Myklebust for (;;) { 2049ba8b06e6STrond Myklebust wait_on_page_writeback(page); 20507f2f12d9STrond Myklebust if (clear_page_dirty_for_io(page)) { 2051c373fff7STrond Myklebust ret = nfs_writepage_locked(page, &wbc); 20527f2f12d9STrond Myklebust if (ret < 0) 20537f2f12d9STrond Myklebust goto out_error; 20540522f6adSTrond Myklebust continue; 20557f2f12d9STrond Myklebust } 2056f4ce1299STrond Myklebust ret = 0; 20570522f6adSTrond Myklebust if (!PagePrivate(page)) 20580522f6adSTrond Myklebust break; 20590522f6adSTrond Myklebust ret = nfs_commit_inode(inode, FLUSH_SYNC); 20607f2f12d9STrond Myklebust if (ret < 0) 20617f2f12d9STrond Myklebust goto out_error; 20627f2f12d9STrond Myklebust } 20637f2f12d9STrond Myklebust out_error: 2064f4ce1299STrond Myklebust trace_nfs_writeback_page_exit(inode, ret); 20657f2f12d9STrond Myklebust return ret; 20661c75950bSTrond Myklebust } 20671c75950bSTrond Myklebust 2068074cc1deSTrond Myklebust #ifdef CONFIG_MIGRATION 2069074cc1deSTrond Myklebust int nfs_migrate_page(struct address_space *mapping, struct page *newpage, 2070a6bc32b8SMel Gorman struct page *page, enum migrate_mode mode) 2071074cc1deSTrond Myklebust { 20722da95652SJeff Layton /* 20732da95652SJeff Layton * If PagePrivate is set, then the page is currently associated with 20742da95652SJeff Layton * an in-progress read or write request. Don't try to migrate it. 20752da95652SJeff Layton * 20762da95652SJeff Layton * FIXME: we could do this in principle, but we'll need a way to ensure 20772da95652SJeff Layton * that we can safely release the inode reference while holding 20782da95652SJeff Layton * the page lock. 20792da95652SJeff Layton */ 20802da95652SJeff Layton if (PagePrivate(page)) 20812da95652SJeff Layton return -EBUSY; 2082074cc1deSTrond Myklebust 20838c209ce7SDavid Howells if (!nfs_fscache_release_page(page, GFP_KERNEL)) 20848c209ce7SDavid Howells return -EBUSY; 2085074cc1deSTrond Myklebust 2086a6bc32b8SMel Gorman return migrate_page(mapping, newpage, page, mode); 2087074cc1deSTrond Myklebust } 2088074cc1deSTrond Myklebust #endif 2089074cc1deSTrond Myklebust 2090f7b422b1SDavid Howells int __init nfs_init_writepagecache(void) 20911da177e4SLinus Torvalds { 20921da177e4SLinus Torvalds nfs_wdata_cachep = kmem_cache_create("nfs_write_data", 20931e7f3a48SWeston Andros Adamson sizeof(struct nfs_pgio_header), 20941da177e4SLinus Torvalds 0, SLAB_HWCACHE_ALIGN, 209520c2df83SPaul Mundt NULL); 20961da177e4SLinus Torvalds if (nfs_wdata_cachep == NULL) 20971da177e4SLinus Torvalds return -ENOMEM; 20981da177e4SLinus Torvalds 209993d2341cSMatthew Dobson nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE, 21001da177e4SLinus Torvalds nfs_wdata_cachep); 21011da177e4SLinus Torvalds if (nfs_wdata_mempool == NULL) 21023dd4765fSJeff Layton goto out_destroy_write_cache; 21031da177e4SLinus Torvalds 21040b7c0153SFred Isaman nfs_cdata_cachep = kmem_cache_create("nfs_commit_data", 21050b7c0153SFred Isaman sizeof(struct nfs_commit_data), 21060b7c0153SFred Isaman 0, SLAB_HWCACHE_ALIGN, 21070b7c0153SFred Isaman NULL); 21080b7c0153SFred Isaman if (nfs_cdata_cachep == NULL) 21093dd4765fSJeff Layton goto out_destroy_write_mempool; 21100b7c0153SFred Isaman 211193d2341cSMatthew Dobson nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT, 21124c100210SYanchuan Nian nfs_cdata_cachep); 21131da177e4SLinus Torvalds if (nfs_commit_mempool == NULL) 21143dd4765fSJeff Layton goto out_destroy_commit_cache; 21151da177e4SLinus Torvalds 211689a09141SPeter Zijlstra /* 211789a09141SPeter Zijlstra * NFS congestion size, scale with available memory. 211889a09141SPeter Zijlstra * 211989a09141SPeter Zijlstra * 64MB: 8192k 212089a09141SPeter Zijlstra * 128MB: 11585k 212189a09141SPeter Zijlstra * 256MB: 16384k 212289a09141SPeter Zijlstra * 512MB: 23170k 212389a09141SPeter Zijlstra * 1GB: 32768k 212489a09141SPeter Zijlstra * 2GB: 46340k 212589a09141SPeter Zijlstra * 4GB: 65536k 212689a09141SPeter Zijlstra * 8GB: 92681k 212789a09141SPeter Zijlstra * 16GB: 131072k 212889a09141SPeter Zijlstra * 212989a09141SPeter Zijlstra * This allows larger machines to have larger/more transfers. 213089a09141SPeter Zijlstra * Limit the default to 256M 213189a09141SPeter Zijlstra */ 2132ca79b0c2SArun KS nfs_congestion_kb = (16*int_sqrt(totalram_pages())) << (PAGE_SHIFT-10); 213389a09141SPeter Zijlstra if (nfs_congestion_kb > 256*1024) 213489a09141SPeter Zijlstra nfs_congestion_kb = 256*1024; 213589a09141SPeter Zijlstra 21361da177e4SLinus Torvalds return 0; 21373dd4765fSJeff Layton 21383dd4765fSJeff Layton out_destroy_commit_cache: 21393dd4765fSJeff Layton kmem_cache_destroy(nfs_cdata_cachep); 21403dd4765fSJeff Layton out_destroy_write_mempool: 21413dd4765fSJeff Layton mempool_destroy(nfs_wdata_mempool); 21423dd4765fSJeff Layton out_destroy_write_cache: 21433dd4765fSJeff Layton kmem_cache_destroy(nfs_wdata_cachep); 21443dd4765fSJeff Layton return -ENOMEM; 21451da177e4SLinus Torvalds } 21461da177e4SLinus Torvalds 2147266bee88SDavid Brownell void nfs_destroy_writepagecache(void) 21481da177e4SLinus Torvalds { 21491da177e4SLinus Torvalds mempool_destroy(nfs_commit_mempool); 21503dd4765fSJeff Layton kmem_cache_destroy(nfs_cdata_cachep); 21511da177e4SLinus Torvalds mempool_destroy(nfs_wdata_mempool); 21521a1d92c1SAlexey Dobriyan kmem_cache_destroy(nfs_wdata_cachep); 21531da177e4SLinus Torvalds } 21541da177e4SLinus Torvalds 21554a0de55cSAnna Schumaker static const struct nfs_rw_ops nfs_rw_write_ops = { 21564a0de55cSAnna Schumaker .rw_alloc_header = nfs_writehdr_alloc, 21574a0de55cSAnna Schumaker .rw_free_header = nfs_writehdr_free, 21580eecb214SAnna Schumaker .rw_done = nfs_writeback_done, 21590eecb214SAnna Schumaker .rw_result = nfs_writeback_result, 21601ed26f33SAnna Schumaker .rw_initiate = nfs_initiate_write, 21614a0de55cSAnna Schumaker }; 2162