11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/fs/nfs/write.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Writing file data over NFS. 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * We do it like this: When a (user) process wishes to write data to an 71da177e4SLinus Torvalds * NFS file, a write request is allocated that contains the RPC task data 81da177e4SLinus Torvalds * plus some info on the page to be written, and added to the inode's 91da177e4SLinus Torvalds * write chain. If the process writes past the end of the page, an async 101da177e4SLinus Torvalds * RPC call to write the page is scheduled immediately; otherwise, the call 111da177e4SLinus Torvalds * is delayed for a few seconds. 121da177e4SLinus Torvalds * 131da177e4SLinus Torvalds * Just like readahead, no async I/O is performed if wsize < PAGE_SIZE. 141da177e4SLinus Torvalds * 151da177e4SLinus Torvalds * Write requests are kept on the inode's writeback list. Each entry in 161da177e4SLinus Torvalds * that list references the page (portion) to be written. When the 171da177e4SLinus Torvalds * cache timeout has expired, the RPC task is woken up, and tries to 181da177e4SLinus Torvalds * lock the page. As soon as it manages to do so, the request is moved 191da177e4SLinus Torvalds * from the writeback list to the writelock list. 201da177e4SLinus Torvalds * 211da177e4SLinus Torvalds * Note: we must make sure never to confuse the inode passed in the 221da177e4SLinus Torvalds * write_page request with the one in page->inode. As far as I understand 231da177e4SLinus Torvalds * it, these are different when doing a swap-out. 241da177e4SLinus Torvalds * 251da177e4SLinus Torvalds * To understand everything that goes on here and in the NFS read code, 261da177e4SLinus Torvalds * one should be aware that a page is locked in exactly one of the following 271da177e4SLinus Torvalds * cases: 281da177e4SLinus Torvalds * 291da177e4SLinus Torvalds * - A write request is in progress. 301da177e4SLinus Torvalds * - A user process is in generic_file_write/nfs_update_page 311da177e4SLinus Torvalds * - A user process is in generic_file_read 321da177e4SLinus Torvalds * 331da177e4SLinus Torvalds * Also note that because of the way pages are invalidated in 341da177e4SLinus Torvalds * nfs_revalidate_inode, the following assertions hold: 351da177e4SLinus Torvalds * 361da177e4SLinus Torvalds * - If a page is dirty, there will be no read requests (a page will 371da177e4SLinus Torvalds * not be re-read unless invalidated by nfs_revalidate_inode). 381da177e4SLinus Torvalds * - If the page is not uptodate, there will be no pending write 391da177e4SLinus Torvalds * requests, and no process will be in nfs_update_page. 401da177e4SLinus Torvalds * 411da177e4SLinus Torvalds * FIXME: Interaction with the vmscan routines is not optimal yet. 421da177e4SLinus Torvalds * Either vmscan must be made nfs-savvy, or we need a different page 431da177e4SLinus Torvalds * reclaim concept that supports something like FS-independent 441da177e4SLinus Torvalds * buffer_heads with a b_ops-> field. 451da177e4SLinus Torvalds * 461da177e4SLinus Torvalds * Copyright (C) 1996, 1997, Olaf Kirch <okir@monad.swb.de> 471da177e4SLinus Torvalds */ 481da177e4SLinus Torvalds 491da177e4SLinus Torvalds #include <linux/types.h> 501da177e4SLinus Torvalds #include <linux/slab.h> 511da177e4SLinus Torvalds #include <linux/mm.h> 521da177e4SLinus Torvalds #include <linux/pagemap.h> 531da177e4SLinus Torvalds #include <linux/file.h> 541da177e4SLinus Torvalds #include <linux/writeback.h> 551da177e4SLinus Torvalds 561da177e4SLinus Torvalds #include <linux/sunrpc/clnt.h> 571da177e4SLinus Torvalds #include <linux/nfs_fs.h> 581da177e4SLinus Torvalds #include <linux/nfs_mount.h> 591da177e4SLinus Torvalds #include <linux/nfs_page.h> 603fcfab16SAndrew Morton #include <linux/backing-dev.h> 613fcfab16SAndrew Morton 621da177e4SLinus Torvalds #include <asm/uaccess.h> 631da177e4SLinus Torvalds #include <linux/smp_lock.h> 641da177e4SLinus Torvalds 651da177e4SLinus Torvalds #include "delegation.h" 6691d5b470SChuck Lever #include "iostat.h" 671da177e4SLinus Torvalds 681da177e4SLinus Torvalds #define NFSDBG_FACILITY NFSDBG_PAGECACHE 691da177e4SLinus Torvalds 701da177e4SLinus Torvalds #define MIN_POOL_WRITE (32) 711da177e4SLinus Torvalds #define MIN_POOL_COMMIT (4) 721da177e4SLinus Torvalds 731da177e4SLinus Torvalds /* 741da177e4SLinus Torvalds * Local function declarations 751da177e4SLinus Torvalds */ 761da177e4SLinus Torvalds static struct nfs_page * nfs_update_request(struct nfs_open_context*, 771da177e4SLinus Torvalds struct inode *, 781da177e4SLinus Torvalds struct page *, 791da177e4SLinus Torvalds unsigned int, unsigned int); 801da177e4SLinus Torvalds static int nfs_wait_on_write_congestion(struct address_space *, int); 811da177e4SLinus Torvalds static int nfs_wait_on_requests(struct inode *, unsigned long, unsigned int); 82*3f442547STrond Myklebust static long nfs_flush_mapping(struct address_space *mapping, struct writeback_control *wbc, int how); 83788e7a89STrond Myklebust static const struct rpc_call_ops nfs_write_partial_ops; 84788e7a89STrond Myklebust static const struct rpc_call_ops nfs_write_full_ops; 85788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops; 861da177e4SLinus Torvalds 871da177e4SLinus Torvalds static kmem_cache_t *nfs_wdata_cachep; 883feb2d49STrond Myklebust static mempool_t *nfs_wdata_mempool; 891da177e4SLinus Torvalds static mempool_t *nfs_commit_mempool; 901da177e4SLinus Torvalds 911da177e4SLinus Torvalds static DECLARE_WAIT_QUEUE_HEAD(nfs_write_congestion); 921da177e4SLinus Torvalds 93e9f7bee1STrond Myklebust struct nfs_write_data *nfs_commit_alloc(void) 941da177e4SLinus Torvalds { 951da177e4SLinus Torvalds struct nfs_write_data *p = mempool_alloc(nfs_commit_mempool, SLAB_NOFS); 9640859d7eSChuck Lever 971da177e4SLinus Torvalds if (p) { 981da177e4SLinus Torvalds memset(p, 0, sizeof(*p)); 991da177e4SLinus Torvalds INIT_LIST_HEAD(&p->pages); 1001da177e4SLinus Torvalds } 1011da177e4SLinus Torvalds return p; 1021da177e4SLinus Torvalds } 1031da177e4SLinus Torvalds 1048aca67f0STrond Myklebust void nfs_commit_rcu_free(struct rcu_head *head) 1051da177e4SLinus Torvalds { 1068aca67f0STrond Myklebust struct nfs_write_data *p = container_of(head, struct nfs_write_data, task.u.tk_rcu); 10740859d7eSChuck Lever if (p && (p->pagevec != &p->page_array[0])) 10840859d7eSChuck Lever kfree(p->pagevec); 1091da177e4SLinus Torvalds mempool_free(p, nfs_commit_mempool); 1101da177e4SLinus Torvalds } 1111da177e4SLinus Torvalds 1128aca67f0STrond Myklebust void nfs_commit_free(struct nfs_write_data *wdata) 1138aca67f0STrond Myklebust { 1148aca67f0STrond Myklebust call_rcu_bh(&wdata->task.u.tk_rcu, nfs_commit_rcu_free); 1158aca67f0STrond Myklebust } 1168aca67f0STrond Myklebust 117e9f7bee1STrond Myklebust struct nfs_write_data *nfs_writedata_alloc(size_t len) 1183feb2d49STrond Myklebust { 119e9f7bee1STrond Myklebust unsigned int pagecount = (len + PAGE_SIZE - 1) >> PAGE_SHIFT; 1203feb2d49STrond Myklebust struct nfs_write_data *p = mempool_alloc(nfs_wdata_mempool, SLAB_NOFS); 1213feb2d49STrond Myklebust 1223feb2d49STrond Myklebust if (p) { 1233feb2d49STrond Myklebust memset(p, 0, sizeof(*p)); 1243feb2d49STrond Myklebust INIT_LIST_HEAD(&p->pages); 125e9f7bee1STrond Myklebust p->npages = pagecount; 1260d0b5cb3SChuck Lever if (pagecount <= ARRAY_SIZE(p->page_array)) 1270d0b5cb3SChuck Lever p->pagevec = p->page_array; 1283feb2d49STrond Myklebust else { 1290d0b5cb3SChuck Lever p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS); 1300d0b5cb3SChuck Lever if (!p->pagevec) { 1313feb2d49STrond Myklebust mempool_free(p, nfs_wdata_mempool); 1323feb2d49STrond Myklebust p = NULL; 1333feb2d49STrond Myklebust } 1343feb2d49STrond Myklebust } 1353feb2d49STrond Myklebust } 1363feb2d49STrond Myklebust return p; 1373feb2d49STrond Myklebust } 1383feb2d49STrond Myklebust 1398aca67f0STrond Myklebust static void nfs_writedata_rcu_free(struct rcu_head *head) 1403feb2d49STrond Myklebust { 1418aca67f0STrond Myklebust struct nfs_write_data *p = container_of(head, struct nfs_write_data, task.u.tk_rcu); 1423feb2d49STrond Myklebust if (p && (p->pagevec != &p->page_array[0])) 1433feb2d49STrond Myklebust kfree(p->pagevec); 1443feb2d49STrond Myklebust mempool_free(p, nfs_wdata_mempool); 1453feb2d49STrond Myklebust } 1463feb2d49STrond Myklebust 1478aca67f0STrond Myklebust static void nfs_writedata_free(struct nfs_write_data *wdata) 1488aca67f0STrond Myklebust { 1498aca67f0STrond Myklebust call_rcu_bh(&wdata->task.u.tk_rcu, nfs_writedata_rcu_free); 1508aca67f0STrond Myklebust } 1518aca67f0STrond Myklebust 152963d8fe5STrond Myklebust void nfs_writedata_release(void *wdata) 1531da177e4SLinus Torvalds { 1541da177e4SLinus Torvalds nfs_writedata_free(wdata); 1551da177e4SLinus Torvalds } 1561da177e4SLinus Torvalds 1571da177e4SLinus Torvalds /* Adjust the file length if we're writing beyond the end */ 1581da177e4SLinus Torvalds static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count) 1591da177e4SLinus Torvalds { 1601da177e4SLinus Torvalds struct inode *inode = page->mapping->host; 1611da177e4SLinus Torvalds loff_t end, i_size = i_size_read(inode); 1621da177e4SLinus Torvalds unsigned long end_index = (i_size - 1) >> PAGE_CACHE_SHIFT; 1631da177e4SLinus Torvalds 1641da177e4SLinus Torvalds if (i_size > 0 && page->index < end_index) 1651da177e4SLinus Torvalds return; 1661da177e4SLinus Torvalds end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count); 1671da177e4SLinus Torvalds if (i_size >= end) 1681da177e4SLinus Torvalds return; 16991d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_EXTENDWRITE); 1701da177e4SLinus Torvalds i_size_write(inode, end); 1711da177e4SLinus Torvalds } 1721da177e4SLinus Torvalds 1731da177e4SLinus Torvalds /* We can set the PG_uptodate flag if we see that a write request 1741da177e4SLinus Torvalds * covers the full page. 1751da177e4SLinus Torvalds */ 1761da177e4SLinus Torvalds static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count) 1771da177e4SLinus Torvalds { 1781da177e4SLinus Torvalds loff_t end_offs; 1791da177e4SLinus Torvalds 1801da177e4SLinus Torvalds if (PageUptodate(page)) 1811da177e4SLinus Torvalds return; 1821da177e4SLinus Torvalds if (base != 0) 1831da177e4SLinus Torvalds return; 1841da177e4SLinus Torvalds if (count == PAGE_CACHE_SIZE) { 1851da177e4SLinus Torvalds SetPageUptodate(page); 1861da177e4SLinus Torvalds return; 1871da177e4SLinus Torvalds } 1881da177e4SLinus Torvalds 1891da177e4SLinus Torvalds end_offs = i_size_read(page->mapping->host) - 1; 1901da177e4SLinus Torvalds if (end_offs < 0) 1911da177e4SLinus Torvalds return; 1921da177e4SLinus Torvalds /* Is this the last page? */ 1931da177e4SLinus Torvalds if (page->index != (unsigned long)(end_offs >> PAGE_CACHE_SHIFT)) 1941da177e4SLinus Torvalds return; 1951da177e4SLinus Torvalds /* This is the last page: set PG_uptodate if we cover the entire 1961da177e4SLinus Torvalds * extent of the data, then zero the rest of the page. 1971da177e4SLinus Torvalds */ 1981da177e4SLinus Torvalds if (count == (unsigned int)(end_offs & (PAGE_CACHE_SIZE - 1)) + 1) { 1991da177e4SLinus Torvalds memclear_highpage_flush(page, count, PAGE_CACHE_SIZE - count); 2001da177e4SLinus Torvalds SetPageUptodate(page); 2011da177e4SLinus Torvalds } 2021da177e4SLinus Torvalds } 2031da177e4SLinus Torvalds 2041da177e4SLinus Torvalds /* 2051da177e4SLinus Torvalds * Write a page synchronously. 2061da177e4SLinus Torvalds * Offset is the data offset within the page. 2071da177e4SLinus Torvalds */ 2081da177e4SLinus Torvalds static int nfs_writepage_sync(struct nfs_open_context *ctx, struct inode *inode, 2091da177e4SLinus Torvalds struct page *page, unsigned int offset, unsigned int count, 2101da177e4SLinus Torvalds int how) 2111da177e4SLinus Torvalds { 2121da177e4SLinus Torvalds unsigned int wsize = NFS_SERVER(inode)->wsize; 2131da177e4SLinus Torvalds int result, written = 0; 2141da177e4SLinus Torvalds struct nfs_write_data *wdata; 2151da177e4SLinus Torvalds 216e9f7bee1STrond Myklebust wdata = nfs_writedata_alloc(wsize); 2171da177e4SLinus Torvalds if (!wdata) 2181da177e4SLinus Torvalds return -ENOMEM; 2191da177e4SLinus Torvalds 2201da177e4SLinus Torvalds wdata->flags = how; 2211da177e4SLinus Torvalds wdata->cred = ctx->cred; 2221da177e4SLinus Torvalds wdata->inode = inode; 2231da177e4SLinus Torvalds wdata->args.fh = NFS_FH(inode); 2241da177e4SLinus Torvalds wdata->args.context = ctx; 2251da177e4SLinus Torvalds wdata->args.pages = &page; 2261da177e4SLinus Torvalds wdata->args.stable = NFS_FILE_SYNC; 2271da177e4SLinus Torvalds wdata->args.pgbase = offset; 2281da177e4SLinus Torvalds wdata->args.count = wsize; 2291da177e4SLinus Torvalds wdata->res.fattr = &wdata->fattr; 2301da177e4SLinus Torvalds wdata->res.verf = &wdata->verf; 2311da177e4SLinus Torvalds 2321da177e4SLinus Torvalds dprintk("NFS: nfs_writepage_sync(%s/%Ld %d@%Ld)\n", 2331da177e4SLinus Torvalds inode->i_sb->s_id, 2341da177e4SLinus Torvalds (long long)NFS_FILEID(inode), 2351da177e4SLinus Torvalds count, (long long)(page_offset(page) + offset)); 2361da177e4SLinus Torvalds 237bb713d6dSTrond Myklebust set_page_writeback(page); 2381da177e4SLinus Torvalds nfs_begin_data_update(inode); 2391da177e4SLinus Torvalds do { 2401da177e4SLinus Torvalds if (count < wsize) 2411da177e4SLinus Torvalds wdata->args.count = count; 2421da177e4SLinus Torvalds wdata->args.offset = page_offset(page) + wdata->args.pgbase; 2431da177e4SLinus Torvalds 2441da177e4SLinus Torvalds result = NFS_PROTO(inode)->write(wdata); 2451da177e4SLinus Torvalds 2461da177e4SLinus Torvalds if (result < 0) { 2471da177e4SLinus Torvalds /* Must mark the page invalid after I/O error */ 2481da177e4SLinus Torvalds ClearPageUptodate(page); 2491da177e4SLinus Torvalds goto io_error; 2501da177e4SLinus Torvalds } 2511da177e4SLinus Torvalds if (result < wdata->args.count) 2521da177e4SLinus Torvalds printk(KERN_WARNING "NFS: short write, count=%u, result=%d\n", 2531da177e4SLinus Torvalds wdata->args.count, result); 2541da177e4SLinus Torvalds 2551da177e4SLinus Torvalds wdata->args.offset += result; 2561da177e4SLinus Torvalds wdata->args.pgbase += result; 2571da177e4SLinus Torvalds written += result; 2581da177e4SLinus Torvalds count -= result; 25991d5b470SChuck Lever nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, result); 2601da177e4SLinus Torvalds } while (count); 2611da177e4SLinus Torvalds /* Update file length */ 2621da177e4SLinus Torvalds nfs_grow_file(page, offset, written); 2631da177e4SLinus Torvalds /* Set the PG_uptodate flag? */ 2641da177e4SLinus Torvalds nfs_mark_uptodate(page, offset, written); 2651da177e4SLinus Torvalds 2661da177e4SLinus Torvalds if (PageError(page)) 2671da177e4SLinus Torvalds ClearPageError(page); 2681da177e4SLinus Torvalds 2691da177e4SLinus Torvalds io_error: 270951a143bSTrond Myklebust nfs_end_data_update(inode); 271bb713d6dSTrond Myklebust end_page_writeback(page); 2728aca67f0STrond Myklebust nfs_writedata_release(wdata); 2731da177e4SLinus Torvalds return written ? written : result; 2741da177e4SLinus Torvalds } 2751da177e4SLinus Torvalds 2761da177e4SLinus Torvalds static int nfs_writepage_async(struct nfs_open_context *ctx, 2771da177e4SLinus Torvalds struct inode *inode, struct page *page, 2781da177e4SLinus Torvalds unsigned int offset, unsigned int count) 2791da177e4SLinus Torvalds { 2801da177e4SLinus Torvalds struct nfs_page *req; 2811da177e4SLinus Torvalds 2821da177e4SLinus Torvalds req = nfs_update_request(ctx, inode, page, offset, count); 283abd3e641STrond Myklebust if (IS_ERR(req)) 284abd3e641STrond Myklebust return PTR_ERR(req); 2851da177e4SLinus Torvalds /* Update file length */ 2861da177e4SLinus Torvalds nfs_grow_file(page, offset, count); 2871da177e4SLinus Torvalds /* Set the PG_uptodate flag? */ 2881da177e4SLinus Torvalds nfs_mark_uptodate(page, offset, count); 2891da177e4SLinus Torvalds nfs_unlock_request(req); 290abd3e641STrond Myklebust return 0; 2911da177e4SLinus Torvalds } 2921da177e4SLinus Torvalds 2931da177e4SLinus Torvalds static int wb_priority(struct writeback_control *wbc) 2941da177e4SLinus Torvalds { 2951da177e4SLinus Torvalds if (wbc->for_reclaim) 2961da177e4SLinus Torvalds return FLUSH_HIGHPRI; 2971da177e4SLinus Torvalds if (wbc->for_kupdate) 2981da177e4SLinus Torvalds return FLUSH_LOWPRI; 2991da177e4SLinus Torvalds return 0; 3001da177e4SLinus Torvalds } 3011da177e4SLinus Torvalds 3021da177e4SLinus Torvalds /* 3031da177e4SLinus Torvalds * Write an mmapped page to the server. 3041da177e4SLinus Torvalds */ 3051da177e4SLinus Torvalds int nfs_writepage(struct page *page, struct writeback_control *wbc) 3061da177e4SLinus Torvalds { 3071da177e4SLinus Torvalds struct nfs_open_context *ctx; 3081da177e4SLinus Torvalds struct inode *inode = page->mapping->host; 3091da177e4SLinus Torvalds unsigned long end_index; 3101da177e4SLinus Torvalds unsigned offset = PAGE_CACHE_SIZE; 3111da177e4SLinus Torvalds loff_t i_size = i_size_read(inode); 3121da177e4SLinus Torvalds int inode_referenced = 0; 3131da177e4SLinus Torvalds int priority = wb_priority(wbc); 3141da177e4SLinus Torvalds int err; 3151da177e4SLinus Torvalds 31691d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE); 31791d5b470SChuck Lever nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1); 31891d5b470SChuck Lever 3191da177e4SLinus Torvalds /* 3201da177e4SLinus Torvalds * Note: We need to ensure that we have a reference to the inode 3211da177e4SLinus Torvalds * if we are to do asynchronous writes. If not, waiting 3221da177e4SLinus Torvalds * in nfs_wait_on_request() may deadlock with clear_inode(). 3231da177e4SLinus Torvalds * 3241da177e4SLinus Torvalds * If igrab() fails here, then it is in any case safe to 3251da177e4SLinus Torvalds * call nfs_wb_page(), since there will be no pending writes. 3261da177e4SLinus Torvalds */ 3271da177e4SLinus Torvalds if (igrab(inode) != 0) 3281da177e4SLinus Torvalds inode_referenced = 1; 3291da177e4SLinus Torvalds end_index = i_size >> PAGE_CACHE_SHIFT; 3301da177e4SLinus Torvalds 3311da177e4SLinus Torvalds /* Ensure we've flushed out any previous writes */ 3321da177e4SLinus Torvalds nfs_wb_page_priority(inode, page, priority); 3331da177e4SLinus Torvalds 3341da177e4SLinus Torvalds /* easy case */ 3351da177e4SLinus Torvalds if (page->index < end_index) 3361da177e4SLinus Torvalds goto do_it; 3371da177e4SLinus Torvalds /* things got complicated... */ 3381da177e4SLinus Torvalds offset = i_size & (PAGE_CACHE_SIZE-1); 3391da177e4SLinus Torvalds 3401da177e4SLinus Torvalds /* OK, are we completely out? */ 3411da177e4SLinus Torvalds err = 0; /* potential race with truncate - ignore */ 3421da177e4SLinus Torvalds if (page->index >= end_index+1 || !offset) 3431da177e4SLinus Torvalds goto out; 3441da177e4SLinus Torvalds do_it: 345d530838bSTrond Myklebust ctx = nfs_find_open_context(inode, NULL, FMODE_WRITE); 3461da177e4SLinus Torvalds if (ctx == NULL) { 3471da177e4SLinus Torvalds err = -EBADF; 3481da177e4SLinus Torvalds goto out; 3491da177e4SLinus Torvalds } 3501da177e4SLinus Torvalds lock_kernel(); 3511da177e4SLinus Torvalds if (!IS_SYNC(inode) && inode_referenced) { 3521da177e4SLinus Torvalds err = nfs_writepage_async(ctx, inode, page, 0, offset); 353abd3e641STrond Myklebust if (!wbc->for_writepages) 35428c6925fSTrond Myklebust nfs_flush_mapping(page->mapping, wbc, wb_priority(wbc)); 3551da177e4SLinus Torvalds } else { 3561da177e4SLinus Torvalds err = nfs_writepage_sync(ctx, inode, page, 0, 3571da177e4SLinus Torvalds offset, priority); 3581da177e4SLinus Torvalds if (err >= 0) { 3591da177e4SLinus Torvalds if (err != offset) 3601da177e4SLinus Torvalds redirty_page_for_writepage(wbc, page); 3611da177e4SLinus Torvalds err = 0; 3621da177e4SLinus Torvalds } 3631da177e4SLinus Torvalds } 3641da177e4SLinus Torvalds unlock_kernel(); 3651da177e4SLinus Torvalds put_nfs_open_context(ctx); 3661da177e4SLinus Torvalds out: 3671da177e4SLinus Torvalds unlock_page(page); 3681da177e4SLinus Torvalds if (inode_referenced) 3691da177e4SLinus Torvalds iput(inode); 3701da177e4SLinus Torvalds return err; 3711da177e4SLinus Torvalds } 3721da177e4SLinus Torvalds 3731da177e4SLinus Torvalds /* 3741da177e4SLinus Torvalds * Note: causes nfs_update_request() to block on the assumption 3751da177e4SLinus Torvalds * that the writeback is generated due to memory pressure. 3761da177e4SLinus Torvalds */ 3771da177e4SLinus Torvalds int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc) 3781da177e4SLinus Torvalds { 3791da177e4SLinus Torvalds struct backing_dev_info *bdi = mapping->backing_dev_info; 3801da177e4SLinus Torvalds struct inode *inode = mapping->host; 3811da177e4SLinus Torvalds int err; 3821da177e4SLinus Torvalds 38391d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES); 38491d5b470SChuck Lever 3851da177e4SLinus Torvalds err = generic_writepages(mapping, wbc); 3861da177e4SLinus Torvalds if (err) 3871da177e4SLinus Torvalds return err; 3881da177e4SLinus Torvalds while (test_and_set_bit(BDI_write_congested, &bdi->state) != 0) { 3891da177e4SLinus Torvalds if (wbc->nonblocking) 3901da177e4SLinus Torvalds return 0; 3911da177e4SLinus Torvalds nfs_wait_on_write_congestion(mapping, 0); 3921da177e4SLinus Torvalds } 39328c6925fSTrond Myklebust err = nfs_flush_mapping(mapping, wbc, wb_priority(wbc)); 3941da177e4SLinus Torvalds if (err < 0) 3951da177e4SLinus Torvalds goto out; 39691d5b470SChuck Lever nfs_add_stats(inode, NFSIOS_WRITEPAGES, err); 3971da177e4SLinus Torvalds if (!wbc->nonblocking && wbc->sync_mode == WB_SYNC_ALL) { 3981da177e4SLinus Torvalds err = nfs_wait_on_requests(inode, 0, 0); 3991da177e4SLinus Torvalds if (err < 0) 4001da177e4SLinus Torvalds goto out; 4011da177e4SLinus Torvalds } 4023da28eb1STrond Myklebust err = nfs_commit_inode(inode, wb_priority(wbc)); 403*3f442547STrond Myklebust if (err > 0) 4041da177e4SLinus Torvalds err = 0; 4051da177e4SLinus Torvalds out: 4061da177e4SLinus Torvalds clear_bit(BDI_write_congested, &bdi->state); 4071da177e4SLinus Torvalds wake_up_all(&nfs_write_congestion); 4083fcfab16SAndrew Morton congestion_end(WRITE); 4091da177e4SLinus Torvalds return err; 4101da177e4SLinus Torvalds } 4111da177e4SLinus Torvalds 4121da177e4SLinus Torvalds /* 4131da177e4SLinus Torvalds * Insert a write request into an inode 4141da177e4SLinus Torvalds */ 4151da177e4SLinus Torvalds static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req) 4161da177e4SLinus Torvalds { 4171da177e4SLinus Torvalds struct nfs_inode *nfsi = NFS_I(inode); 4181da177e4SLinus Torvalds int error; 4191da177e4SLinus Torvalds 4201da177e4SLinus Torvalds error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req); 4211da177e4SLinus Torvalds BUG_ON(error == -EEXIST); 4221da177e4SLinus Torvalds if (error) 4231da177e4SLinus Torvalds return error; 4241da177e4SLinus Torvalds if (!nfsi->npages) { 4251da177e4SLinus Torvalds igrab(inode); 4261da177e4SLinus Torvalds nfs_begin_data_update(inode); 4271da177e4SLinus Torvalds if (nfs_have_delegation(inode, FMODE_WRITE)) 4281da177e4SLinus Torvalds nfsi->change_attr++; 4291da177e4SLinus Torvalds } 430deb7d638STrond Myklebust SetPagePrivate(req->wb_page); 4311da177e4SLinus Torvalds nfsi->npages++; 4321da177e4SLinus Torvalds atomic_inc(&req->wb_count); 4331da177e4SLinus Torvalds return 0; 4341da177e4SLinus Torvalds } 4351da177e4SLinus Torvalds 4361da177e4SLinus Torvalds /* 4371da177e4SLinus Torvalds * Insert a write request into an inode 4381da177e4SLinus Torvalds */ 4391da177e4SLinus Torvalds static void nfs_inode_remove_request(struct nfs_page *req) 4401da177e4SLinus Torvalds { 4411da177e4SLinus Torvalds struct inode *inode = req->wb_context->dentry->d_inode; 4421da177e4SLinus Torvalds struct nfs_inode *nfsi = NFS_I(inode); 4431da177e4SLinus Torvalds 4441da177e4SLinus Torvalds BUG_ON (!NFS_WBACK_BUSY(req)); 4451da177e4SLinus Torvalds 4461da177e4SLinus Torvalds spin_lock(&nfsi->req_lock); 447deb7d638STrond Myklebust ClearPagePrivate(req->wb_page); 4481da177e4SLinus Torvalds radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index); 4491da177e4SLinus Torvalds nfsi->npages--; 4501da177e4SLinus Torvalds if (!nfsi->npages) { 4511da177e4SLinus Torvalds spin_unlock(&nfsi->req_lock); 452951a143bSTrond Myklebust nfs_end_data_update(inode); 4531da177e4SLinus Torvalds iput(inode); 4541da177e4SLinus Torvalds } else 4551da177e4SLinus Torvalds spin_unlock(&nfsi->req_lock); 4561da177e4SLinus Torvalds nfs_clear_request(req); 4571da177e4SLinus Torvalds nfs_release_request(req); 4581da177e4SLinus Torvalds } 4591da177e4SLinus Torvalds 4601da177e4SLinus Torvalds /* 4611da177e4SLinus Torvalds * Find a request 4621da177e4SLinus Torvalds */ 4631da177e4SLinus Torvalds static inline struct nfs_page * 4641da177e4SLinus Torvalds _nfs_find_request(struct inode *inode, unsigned long index) 4651da177e4SLinus Torvalds { 4661da177e4SLinus Torvalds struct nfs_inode *nfsi = NFS_I(inode); 4671da177e4SLinus Torvalds struct nfs_page *req; 4681da177e4SLinus Torvalds 4691da177e4SLinus Torvalds req = (struct nfs_page*)radix_tree_lookup(&nfsi->nfs_page_tree, index); 4701da177e4SLinus Torvalds if (req) 4711da177e4SLinus Torvalds atomic_inc(&req->wb_count); 4721da177e4SLinus Torvalds return req; 4731da177e4SLinus Torvalds } 4741da177e4SLinus Torvalds 4751da177e4SLinus Torvalds static struct nfs_page * 4761da177e4SLinus Torvalds nfs_find_request(struct inode *inode, unsigned long index) 4771da177e4SLinus Torvalds { 4781da177e4SLinus Torvalds struct nfs_page *req; 4791da177e4SLinus Torvalds struct nfs_inode *nfsi = NFS_I(inode); 4801da177e4SLinus Torvalds 4811da177e4SLinus Torvalds spin_lock(&nfsi->req_lock); 4821da177e4SLinus Torvalds req = _nfs_find_request(inode, index); 4831da177e4SLinus Torvalds spin_unlock(&nfsi->req_lock); 4841da177e4SLinus Torvalds return req; 4851da177e4SLinus Torvalds } 4861da177e4SLinus Torvalds 4871da177e4SLinus Torvalds /* 4881da177e4SLinus Torvalds * Add a request to the inode's dirty list. 4891da177e4SLinus Torvalds */ 4901da177e4SLinus Torvalds static void 4911da177e4SLinus Torvalds nfs_mark_request_dirty(struct nfs_page *req) 4921da177e4SLinus Torvalds { 4931da177e4SLinus Torvalds struct inode *inode = req->wb_context->dentry->d_inode; 4941da177e4SLinus Torvalds struct nfs_inode *nfsi = NFS_I(inode); 4951da177e4SLinus Torvalds 4961da177e4SLinus Torvalds spin_lock(&nfsi->req_lock); 4973da28eb1STrond Myklebust radix_tree_tag_set(&nfsi->nfs_page_tree, 4983da28eb1STrond Myklebust req->wb_index, NFS_PAGE_TAG_DIRTY); 4991da177e4SLinus Torvalds nfs_list_add_request(req, &nfsi->dirty); 5001da177e4SLinus Torvalds nfsi->ndirty++; 5011da177e4SLinus Torvalds spin_unlock(&nfsi->req_lock); 502b1e7a8fdSChristoph Lameter inc_zone_page_state(req->wb_page, NR_FILE_DIRTY); 5031da177e4SLinus Torvalds mark_inode_dirty(inode); 5041da177e4SLinus Torvalds } 5051da177e4SLinus Torvalds 5061da177e4SLinus Torvalds /* 5071da177e4SLinus Torvalds * Check if a request is dirty 5081da177e4SLinus Torvalds */ 5091da177e4SLinus Torvalds static inline int 5101da177e4SLinus Torvalds nfs_dirty_request(struct nfs_page *req) 5111da177e4SLinus Torvalds { 5121da177e4SLinus Torvalds struct nfs_inode *nfsi = NFS_I(req->wb_context->dentry->d_inode); 5131da177e4SLinus Torvalds return !list_empty(&req->wb_list) && req->wb_list_head == &nfsi->dirty; 5141da177e4SLinus Torvalds } 5151da177e4SLinus Torvalds 5161da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) 5171da177e4SLinus Torvalds /* 5181da177e4SLinus Torvalds * Add a request to the inode's commit list. 5191da177e4SLinus Torvalds */ 5201da177e4SLinus Torvalds static void 5211da177e4SLinus Torvalds nfs_mark_request_commit(struct nfs_page *req) 5221da177e4SLinus Torvalds { 5231da177e4SLinus Torvalds struct inode *inode = req->wb_context->dentry->d_inode; 5241da177e4SLinus Torvalds struct nfs_inode *nfsi = NFS_I(inode); 5251da177e4SLinus Torvalds 5261da177e4SLinus Torvalds spin_lock(&nfsi->req_lock); 5271da177e4SLinus Torvalds nfs_list_add_request(req, &nfsi->commit); 5281da177e4SLinus Torvalds nfsi->ncommit++; 5291da177e4SLinus Torvalds spin_unlock(&nfsi->req_lock); 530fd39fc85SChristoph Lameter inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS); 5311da177e4SLinus Torvalds mark_inode_dirty(inode); 5321da177e4SLinus Torvalds } 5331da177e4SLinus Torvalds #endif 5341da177e4SLinus Torvalds 5351da177e4SLinus Torvalds /* 5361da177e4SLinus Torvalds * Wait for a request to complete. 5371da177e4SLinus Torvalds * 5381da177e4SLinus Torvalds * Interruptible by signals only if mounted with intr flag. 5391da177e4SLinus Torvalds */ 540c42de9ddSTrond Myklebust static int nfs_wait_on_requests_locked(struct inode *inode, unsigned long idx_start, unsigned int npages) 5411da177e4SLinus Torvalds { 5421da177e4SLinus Torvalds struct nfs_inode *nfsi = NFS_I(inode); 5431da177e4SLinus Torvalds struct nfs_page *req; 5441da177e4SLinus Torvalds unsigned long idx_end, next; 5451da177e4SLinus Torvalds unsigned int res = 0; 5461da177e4SLinus Torvalds int error; 5471da177e4SLinus Torvalds 5481da177e4SLinus Torvalds if (npages == 0) 5491da177e4SLinus Torvalds idx_end = ~0; 5501da177e4SLinus Torvalds else 5511da177e4SLinus Torvalds idx_end = idx_start + npages - 1; 5521da177e4SLinus Torvalds 5531da177e4SLinus Torvalds next = idx_start; 554c6a556b8STrond Myklebust while (radix_tree_gang_lookup_tag(&nfsi->nfs_page_tree, (void **)&req, next, 1, NFS_PAGE_TAG_WRITEBACK)) { 5551da177e4SLinus Torvalds if (req->wb_index > idx_end) 5561da177e4SLinus Torvalds break; 5571da177e4SLinus Torvalds 5581da177e4SLinus Torvalds next = req->wb_index + 1; 559c6a556b8STrond Myklebust BUG_ON(!NFS_WBACK_BUSY(req)); 5601da177e4SLinus Torvalds 5611da177e4SLinus Torvalds atomic_inc(&req->wb_count); 5621da177e4SLinus Torvalds spin_unlock(&nfsi->req_lock); 5631da177e4SLinus Torvalds error = nfs_wait_on_request(req); 5641da177e4SLinus Torvalds nfs_release_request(req); 565c42de9ddSTrond Myklebust spin_lock(&nfsi->req_lock); 5661da177e4SLinus Torvalds if (error < 0) 5671da177e4SLinus Torvalds return error; 5681da177e4SLinus Torvalds res++; 5691da177e4SLinus Torvalds } 5701da177e4SLinus Torvalds return res; 5711da177e4SLinus Torvalds } 5721da177e4SLinus Torvalds 573c42de9ddSTrond Myklebust static int nfs_wait_on_requests(struct inode *inode, unsigned long idx_start, unsigned int npages) 574c42de9ddSTrond Myklebust { 575c42de9ddSTrond Myklebust struct nfs_inode *nfsi = NFS_I(inode); 576c42de9ddSTrond Myklebust int ret; 577c42de9ddSTrond Myklebust 578c42de9ddSTrond Myklebust spin_lock(&nfsi->req_lock); 579c42de9ddSTrond Myklebust ret = nfs_wait_on_requests_locked(inode, idx_start, npages); 580c42de9ddSTrond Myklebust spin_unlock(&nfsi->req_lock); 581c42de9ddSTrond Myklebust return ret; 582c42de9ddSTrond Myklebust } 583c42de9ddSTrond Myklebust 58483715ad5STrond Myklebust static void nfs_cancel_dirty_list(struct list_head *head) 585d2ccddf0STrond Myklebust { 586d2ccddf0STrond Myklebust struct nfs_page *req; 587d2ccddf0STrond Myklebust while(!list_empty(head)) { 588d2ccddf0STrond Myklebust req = nfs_list_entry(head->next); 589d2ccddf0STrond Myklebust nfs_list_remove_request(req); 590d2ccddf0STrond Myklebust nfs_inode_remove_request(req); 591d2ccddf0STrond Myklebust nfs_clear_page_writeback(req); 592d2ccddf0STrond Myklebust } 593d2ccddf0STrond Myklebust } 594d2ccddf0STrond Myklebust 59583715ad5STrond Myklebust static void nfs_cancel_commit_list(struct list_head *head) 59683715ad5STrond Myklebust { 59783715ad5STrond Myklebust struct nfs_page *req; 59883715ad5STrond Myklebust 59983715ad5STrond Myklebust while(!list_empty(head)) { 60083715ad5STrond Myklebust req = nfs_list_entry(head->next); 601b6dff26aSTrond Myklebust dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS); 60283715ad5STrond Myklebust nfs_list_remove_request(req); 60383715ad5STrond Myklebust nfs_inode_remove_request(req); 604b6dff26aSTrond Myklebust nfs_unlock_request(req); 60583715ad5STrond Myklebust } 60683715ad5STrond Myklebust } 60783715ad5STrond Myklebust 6081da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) 6091da177e4SLinus Torvalds /* 6101da177e4SLinus Torvalds * nfs_scan_commit - Scan an inode for commit requests 6111da177e4SLinus Torvalds * @inode: NFS inode to scan 6121da177e4SLinus Torvalds * @dst: destination list 6131da177e4SLinus Torvalds * @idx_start: lower bound of page->index to scan. 6141da177e4SLinus Torvalds * @npages: idx_start + npages sets the upper bound to scan. 6151da177e4SLinus Torvalds * 6161da177e4SLinus Torvalds * Moves requests from the inode's 'commit' request list. 6171da177e4SLinus Torvalds * The requests are *not* checked to ensure that they form a contiguous set. 6181da177e4SLinus Torvalds */ 6191da177e4SLinus Torvalds static int 6201da177e4SLinus Torvalds nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages) 6211da177e4SLinus Torvalds { 6221da177e4SLinus Torvalds struct nfs_inode *nfsi = NFS_I(inode); 6233da28eb1STrond Myklebust int res = 0; 6243da28eb1STrond Myklebust 6253da28eb1STrond Myklebust if (nfsi->ncommit != 0) { 626d2ccddf0STrond Myklebust res = nfs_scan_list(nfsi, &nfsi->commit, dst, idx_start, npages); 6271da177e4SLinus Torvalds nfsi->ncommit -= res; 6281da177e4SLinus Torvalds if ((nfsi->ncommit == 0) != list_empty(&nfsi->commit)) 6291da177e4SLinus Torvalds printk(KERN_ERR "NFS: desynchronized value of nfs_i.ncommit.\n"); 6303da28eb1STrond Myklebust } 6311da177e4SLinus Torvalds return res; 6321da177e4SLinus Torvalds } 633c42de9ddSTrond Myklebust #else 634c42de9ddSTrond Myklebust static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages) 635c42de9ddSTrond Myklebust { 636c42de9ddSTrond Myklebust return 0; 637c42de9ddSTrond Myklebust } 6381da177e4SLinus Torvalds #endif 6391da177e4SLinus Torvalds 6401da177e4SLinus Torvalds static int nfs_wait_on_write_congestion(struct address_space *mapping, int intr) 6411da177e4SLinus Torvalds { 6421da177e4SLinus Torvalds struct backing_dev_info *bdi = mapping->backing_dev_info; 6431da177e4SLinus Torvalds DEFINE_WAIT(wait); 6441da177e4SLinus Torvalds int ret = 0; 6451da177e4SLinus Torvalds 6461da177e4SLinus Torvalds might_sleep(); 6471da177e4SLinus Torvalds 6481da177e4SLinus Torvalds if (!bdi_write_congested(bdi)) 6491da177e4SLinus Torvalds return 0; 65091d5b470SChuck Lever 65191d5b470SChuck Lever nfs_inc_stats(mapping->host, NFSIOS_CONGESTIONWAIT); 65291d5b470SChuck Lever 6531da177e4SLinus Torvalds if (intr) { 6541da177e4SLinus Torvalds struct rpc_clnt *clnt = NFS_CLIENT(mapping->host); 6551da177e4SLinus Torvalds sigset_t oldset; 6561da177e4SLinus Torvalds 6571da177e4SLinus Torvalds rpc_clnt_sigmask(clnt, &oldset); 6581da177e4SLinus Torvalds prepare_to_wait(&nfs_write_congestion, &wait, TASK_INTERRUPTIBLE); 6591da177e4SLinus Torvalds if (bdi_write_congested(bdi)) { 6601da177e4SLinus Torvalds if (signalled()) 6611da177e4SLinus Torvalds ret = -ERESTARTSYS; 6621da177e4SLinus Torvalds else 6631da177e4SLinus Torvalds schedule(); 6641da177e4SLinus Torvalds } 6651da177e4SLinus Torvalds rpc_clnt_sigunmask(clnt, &oldset); 6661da177e4SLinus Torvalds } else { 6671da177e4SLinus Torvalds prepare_to_wait(&nfs_write_congestion, &wait, TASK_UNINTERRUPTIBLE); 6681da177e4SLinus Torvalds if (bdi_write_congested(bdi)) 6691da177e4SLinus Torvalds schedule(); 6701da177e4SLinus Torvalds } 6711da177e4SLinus Torvalds finish_wait(&nfs_write_congestion, &wait); 6721da177e4SLinus Torvalds return ret; 6731da177e4SLinus Torvalds } 6741da177e4SLinus Torvalds 6751da177e4SLinus Torvalds 6761da177e4SLinus Torvalds /* 6771da177e4SLinus Torvalds * Try to update any existing write request, or create one if there is none. 6781da177e4SLinus Torvalds * In order to match, the request's credentials must match those of 6791da177e4SLinus Torvalds * the calling process. 6801da177e4SLinus Torvalds * 6811da177e4SLinus Torvalds * Note: Should always be called with the Page Lock held! 6821da177e4SLinus Torvalds */ 6831da177e4SLinus Torvalds static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx, 6841da177e4SLinus Torvalds struct inode *inode, struct page *page, 6851da177e4SLinus Torvalds unsigned int offset, unsigned int bytes) 6861da177e4SLinus Torvalds { 6871da177e4SLinus Torvalds struct nfs_server *server = NFS_SERVER(inode); 6881da177e4SLinus Torvalds struct nfs_inode *nfsi = NFS_I(inode); 6891da177e4SLinus Torvalds struct nfs_page *req, *new = NULL; 6901da177e4SLinus Torvalds unsigned long rqend, end; 6911da177e4SLinus Torvalds 6921da177e4SLinus Torvalds end = offset + bytes; 6931da177e4SLinus Torvalds 6941da177e4SLinus Torvalds if (nfs_wait_on_write_congestion(page->mapping, server->flags & NFS_MOUNT_INTR)) 6951da177e4SLinus Torvalds return ERR_PTR(-ERESTARTSYS); 6961da177e4SLinus Torvalds for (;;) { 6971da177e4SLinus Torvalds /* Loop over all inode entries and see if we find 6981da177e4SLinus Torvalds * A request for the page we wish to update 6991da177e4SLinus Torvalds */ 7001da177e4SLinus Torvalds spin_lock(&nfsi->req_lock); 7011da177e4SLinus Torvalds req = _nfs_find_request(inode, page->index); 7021da177e4SLinus Torvalds if (req) { 7031da177e4SLinus Torvalds if (!nfs_lock_request_dontget(req)) { 7041da177e4SLinus Torvalds int error; 7051da177e4SLinus Torvalds spin_unlock(&nfsi->req_lock); 7061da177e4SLinus Torvalds error = nfs_wait_on_request(req); 7071da177e4SLinus Torvalds nfs_release_request(req); 7081dd594b2SNeil Brown if (error < 0) { 7091dd594b2SNeil Brown if (new) 7101dd594b2SNeil Brown nfs_release_request(new); 7111da177e4SLinus Torvalds return ERR_PTR(error); 7121dd594b2SNeil Brown } 7131da177e4SLinus Torvalds continue; 7141da177e4SLinus Torvalds } 7151da177e4SLinus Torvalds spin_unlock(&nfsi->req_lock); 7161da177e4SLinus Torvalds if (new) 7171da177e4SLinus Torvalds nfs_release_request(new); 7181da177e4SLinus Torvalds break; 7191da177e4SLinus Torvalds } 7201da177e4SLinus Torvalds 7211da177e4SLinus Torvalds if (new) { 7221da177e4SLinus Torvalds int error; 7231da177e4SLinus Torvalds nfs_lock_request_dontget(new); 7241da177e4SLinus Torvalds error = nfs_inode_add_request(inode, new); 7251da177e4SLinus Torvalds if (error) { 7261da177e4SLinus Torvalds spin_unlock(&nfsi->req_lock); 7271da177e4SLinus Torvalds nfs_unlock_request(new); 7281da177e4SLinus Torvalds return ERR_PTR(error); 7291da177e4SLinus Torvalds } 7301da177e4SLinus Torvalds spin_unlock(&nfsi->req_lock); 7311da177e4SLinus Torvalds nfs_mark_request_dirty(new); 7321da177e4SLinus Torvalds return new; 7331da177e4SLinus Torvalds } 7341da177e4SLinus Torvalds spin_unlock(&nfsi->req_lock); 7351da177e4SLinus Torvalds 7361da177e4SLinus Torvalds new = nfs_create_request(ctx, inode, page, offset, bytes); 7371da177e4SLinus Torvalds if (IS_ERR(new)) 7381da177e4SLinus Torvalds return new; 7391da177e4SLinus Torvalds } 7401da177e4SLinus Torvalds 7411da177e4SLinus Torvalds /* We have a request for our page. 7421da177e4SLinus Torvalds * If the creds don't match, or the 7431da177e4SLinus Torvalds * page addresses don't match, 7441da177e4SLinus Torvalds * tell the caller to wait on the conflicting 7451da177e4SLinus Torvalds * request. 7461da177e4SLinus Torvalds */ 7471da177e4SLinus Torvalds rqend = req->wb_offset + req->wb_bytes; 7481da177e4SLinus Torvalds if (req->wb_context != ctx 7491da177e4SLinus Torvalds || req->wb_page != page 7501da177e4SLinus Torvalds || !nfs_dirty_request(req) 7511da177e4SLinus Torvalds || offset > rqend || end < req->wb_offset) { 7521da177e4SLinus Torvalds nfs_unlock_request(req); 7531da177e4SLinus Torvalds return ERR_PTR(-EBUSY); 7541da177e4SLinus Torvalds } 7551da177e4SLinus Torvalds 7561da177e4SLinus Torvalds /* Okay, the request matches. Update the region */ 7571da177e4SLinus Torvalds if (offset < req->wb_offset) { 7581da177e4SLinus Torvalds req->wb_offset = offset; 7591da177e4SLinus Torvalds req->wb_pgbase = offset; 7601da177e4SLinus Torvalds req->wb_bytes = rqend - req->wb_offset; 7611da177e4SLinus Torvalds } 7621da177e4SLinus Torvalds 7631da177e4SLinus Torvalds if (end > rqend) 7641da177e4SLinus Torvalds req->wb_bytes = end - req->wb_offset; 7651da177e4SLinus Torvalds 7661da177e4SLinus Torvalds return req; 7671da177e4SLinus Torvalds } 7681da177e4SLinus Torvalds 7691da177e4SLinus Torvalds int nfs_flush_incompatible(struct file *file, struct page *page) 7701da177e4SLinus Torvalds { 7711da177e4SLinus Torvalds struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data; 7721da177e4SLinus Torvalds struct inode *inode = page->mapping->host; 7731da177e4SLinus Torvalds struct nfs_page *req; 7741da177e4SLinus Torvalds int status = 0; 7751da177e4SLinus Torvalds /* 7761da177e4SLinus Torvalds * Look for a request corresponding to this page. If there 7771da177e4SLinus Torvalds * is one, and it belongs to another file, we flush it out 7781da177e4SLinus Torvalds * before we try to copy anything into the page. Do this 7791da177e4SLinus Torvalds * due to the lack of an ACCESS-type call in NFSv2. 7801da177e4SLinus Torvalds * Also do the same if we find a request from an existing 7811da177e4SLinus Torvalds * dropped page. 7821da177e4SLinus Torvalds */ 7831da177e4SLinus Torvalds req = nfs_find_request(inode, page->index); 7841da177e4SLinus Torvalds if (req) { 7851da177e4SLinus Torvalds if (req->wb_page != page || ctx != req->wb_context) 7861da177e4SLinus Torvalds status = nfs_wb_page(inode, page); 7871da177e4SLinus Torvalds nfs_release_request(req); 7881da177e4SLinus Torvalds } 7891da177e4SLinus Torvalds return (status < 0) ? status : 0; 7901da177e4SLinus Torvalds } 7911da177e4SLinus Torvalds 7921da177e4SLinus Torvalds /* 7931da177e4SLinus Torvalds * Update and possibly write a cached page of an NFS file. 7941da177e4SLinus Torvalds * 7951da177e4SLinus Torvalds * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad 7961da177e4SLinus Torvalds * things with a page scheduled for an RPC call (e.g. invalidate it). 7971da177e4SLinus Torvalds */ 7981da177e4SLinus Torvalds int nfs_updatepage(struct file *file, struct page *page, 7991da177e4SLinus Torvalds unsigned int offset, unsigned int count) 8001da177e4SLinus Torvalds { 8011da177e4SLinus Torvalds struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data; 8021da177e4SLinus Torvalds struct inode *inode = page->mapping->host; 8031da177e4SLinus Torvalds struct nfs_page *req; 8041da177e4SLinus Torvalds int status = 0; 8051da177e4SLinus Torvalds 80691d5b470SChuck Lever nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE); 80791d5b470SChuck Lever 8081da177e4SLinus Torvalds dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n", 8090bbacc40SChuck Lever file->f_dentry->d_parent->d_name.name, 8100bbacc40SChuck Lever file->f_dentry->d_name.name, count, 8110bbacc40SChuck Lever (long long)(page_offset(page) +offset)); 8121da177e4SLinus Torvalds 8131da177e4SLinus Torvalds if (IS_SYNC(inode)) { 8141da177e4SLinus Torvalds status = nfs_writepage_sync(ctx, inode, page, offset, count, 0); 8151da177e4SLinus Torvalds if (status > 0) { 8161da177e4SLinus Torvalds if (offset == 0 && status == PAGE_CACHE_SIZE) 8171da177e4SLinus Torvalds SetPageUptodate(page); 8181da177e4SLinus Torvalds return 0; 8191da177e4SLinus Torvalds } 8201da177e4SLinus Torvalds return status; 8211da177e4SLinus Torvalds } 8221da177e4SLinus Torvalds 8231da177e4SLinus Torvalds /* If we're not using byte range locks, and we know the page 8241da177e4SLinus Torvalds * is entirely in cache, it may be more efficient to avoid 8251da177e4SLinus Torvalds * fragmenting write requests. 8261da177e4SLinus Torvalds */ 827ab0a3dbeSTrond Myklebust if (PageUptodate(page) && inode->i_flock == NULL && !(file->f_mode & O_SYNC)) { 8281da177e4SLinus Torvalds loff_t end_offs = i_size_read(inode) - 1; 8291da177e4SLinus Torvalds unsigned long end_index = end_offs >> PAGE_CACHE_SHIFT; 8301da177e4SLinus Torvalds 8311da177e4SLinus Torvalds count += offset; 8321da177e4SLinus Torvalds offset = 0; 8331da177e4SLinus Torvalds if (unlikely(end_offs < 0)) { 8341da177e4SLinus Torvalds /* Do nothing */ 8351da177e4SLinus Torvalds } else if (page->index == end_index) { 8361da177e4SLinus Torvalds unsigned int pglen; 8371da177e4SLinus Torvalds pglen = (unsigned int)(end_offs & (PAGE_CACHE_SIZE-1)) + 1; 8381da177e4SLinus Torvalds if (count < pglen) 8391da177e4SLinus Torvalds count = pglen; 8401da177e4SLinus Torvalds } else if (page->index < end_index) 8411da177e4SLinus Torvalds count = PAGE_CACHE_SIZE; 8421da177e4SLinus Torvalds } 8431da177e4SLinus Torvalds 8441da177e4SLinus Torvalds /* 8451da177e4SLinus Torvalds * Try to find an NFS request corresponding to this page 8461da177e4SLinus Torvalds * and update it. 8471da177e4SLinus Torvalds * If the existing request cannot be updated, we must flush 8481da177e4SLinus Torvalds * it out now. 8491da177e4SLinus Torvalds */ 8501da177e4SLinus Torvalds do { 8511da177e4SLinus Torvalds req = nfs_update_request(ctx, inode, page, offset, count); 8521da177e4SLinus Torvalds status = (IS_ERR(req)) ? PTR_ERR(req) : 0; 8531da177e4SLinus Torvalds if (status != -EBUSY) 8541da177e4SLinus Torvalds break; 8551da177e4SLinus Torvalds /* Request could not be updated. Flush it out and try again */ 8561da177e4SLinus Torvalds status = nfs_wb_page(inode, page); 8571da177e4SLinus Torvalds } while (status >= 0); 8581da177e4SLinus Torvalds if (status < 0) 8591da177e4SLinus Torvalds goto done; 8601da177e4SLinus Torvalds 8611da177e4SLinus Torvalds status = 0; 8621da177e4SLinus Torvalds 8631da177e4SLinus Torvalds /* Update file length */ 8641da177e4SLinus Torvalds nfs_grow_file(page, offset, count); 8651da177e4SLinus Torvalds /* Set the PG_uptodate flag? */ 8661da177e4SLinus Torvalds nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes); 8671da177e4SLinus Torvalds nfs_unlock_request(req); 8681da177e4SLinus Torvalds done: 8691da177e4SLinus Torvalds dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n", 8701da177e4SLinus Torvalds status, (long long)i_size_read(inode)); 8711da177e4SLinus Torvalds if (status < 0) 8721da177e4SLinus Torvalds ClearPageUptodate(page); 8731da177e4SLinus Torvalds return status; 8741da177e4SLinus Torvalds } 8751da177e4SLinus Torvalds 8761da177e4SLinus Torvalds static void nfs_writepage_release(struct nfs_page *req) 8771da177e4SLinus Torvalds { 8781da177e4SLinus Torvalds end_page_writeback(req->wb_page); 8791da177e4SLinus Torvalds 8801da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) 8811da177e4SLinus Torvalds if (!PageError(req->wb_page)) { 8821da177e4SLinus Torvalds if (NFS_NEED_RESCHED(req)) { 8831da177e4SLinus Torvalds nfs_mark_request_dirty(req); 8841da177e4SLinus Torvalds goto out; 8851da177e4SLinus Torvalds } else if (NFS_NEED_COMMIT(req)) { 8861da177e4SLinus Torvalds nfs_mark_request_commit(req); 8871da177e4SLinus Torvalds goto out; 8881da177e4SLinus Torvalds } 8891da177e4SLinus Torvalds } 8901da177e4SLinus Torvalds nfs_inode_remove_request(req); 8911da177e4SLinus Torvalds 8921da177e4SLinus Torvalds out: 8931da177e4SLinus Torvalds nfs_clear_commit(req); 8941da177e4SLinus Torvalds nfs_clear_reschedule(req); 8951da177e4SLinus Torvalds #else 8961da177e4SLinus Torvalds nfs_inode_remove_request(req); 8971da177e4SLinus Torvalds #endif 898c6a556b8STrond Myklebust nfs_clear_page_writeback(req); 8991da177e4SLinus Torvalds } 9001da177e4SLinus Torvalds 9011da177e4SLinus Torvalds static inline int flush_task_priority(int how) 9021da177e4SLinus Torvalds { 9031da177e4SLinus Torvalds switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) { 9041da177e4SLinus Torvalds case FLUSH_HIGHPRI: 9051da177e4SLinus Torvalds return RPC_PRIORITY_HIGH; 9061da177e4SLinus Torvalds case FLUSH_LOWPRI: 9071da177e4SLinus Torvalds return RPC_PRIORITY_LOW; 9081da177e4SLinus Torvalds } 9091da177e4SLinus Torvalds return RPC_PRIORITY_NORMAL; 9101da177e4SLinus Torvalds } 9111da177e4SLinus Torvalds 9121da177e4SLinus Torvalds /* 9131da177e4SLinus Torvalds * Set up the argument/result storage required for the RPC call. 9141da177e4SLinus Torvalds */ 9151da177e4SLinus Torvalds static void nfs_write_rpcsetup(struct nfs_page *req, 9161da177e4SLinus Torvalds struct nfs_write_data *data, 917788e7a89STrond Myklebust const struct rpc_call_ops *call_ops, 9181da177e4SLinus Torvalds unsigned int count, unsigned int offset, 9191da177e4SLinus Torvalds int how) 9201da177e4SLinus Torvalds { 9211da177e4SLinus Torvalds struct inode *inode; 922788e7a89STrond Myklebust int flags; 9231da177e4SLinus Torvalds 9241da177e4SLinus Torvalds /* Set up the RPC argument and reply structs 9251da177e4SLinus Torvalds * NB: take care not to mess about with data->commit et al. */ 9261da177e4SLinus Torvalds 9271da177e4SLinus Torvalds data->req = req; 9281da177e4SLinus Torvalds data->inode = inode = req->wb_context->dentry->d_inode; 9291da177e4SLinus Torvalds data->cred = req->wb_context->cred; 9301da177e4SLinus Torvalds 9311da177e4SLinus Torvalds data->args.fh = NFS_FH(inode); 9321da177e4SLinus Torvalds data->args.offset = req_offset(req) + offset; 9331da177e4SLinus Torvalds data->args.pgbase = req->wb_pgbase + offset; 9341da177e4SLinus Torvalds data->args.pages = data->pagevec; 9351da177e4SLinus Torvalds data->args.count = count; 9361da177e4SLinus Torvalds data->args.context = req->wb_context; 9371da177e4SLinus Torvalds 9381da177e4SLinus Torvalds data->res.fattr = &data->fattr; 9391da177e4SLinus Torvalds data->res.count = count; 9401da177e4SLinus Torvalds data->res.verf = &data->verf; 9410e574af1STrond Myklebust nfs_fattr_init(&data->fattr); 9421da177e4SLinus Torvalds 943788e7a89STrond Myklebust /* Set up the initial task struct. */ 944788e7a89STrond Myklebust flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC; 945788e7a89STrond Myklebust rpc_init_task(&data->task, NFS_CLIENT(inode), flags, call_ops, data); 9461da177e4SLinus Torvalds NFS_PROTO(inode)->write_setup(data, how); 9471da177e4SLinus Torvalds 9481da177e4SLinus Torvalds data->task.tk_priority = flush_task_priority(how); 9491da177e4SLinus Torvalds data->task.tk_cookie = (unsigned long)inode; 9501da177e4SLinus Torvalds 9511da177e4SLinus Torvalds dprintk("NFS: %4d initiated write call (req %s/%Ld, %u bytes @ offset %Lu)\n", 9520bbacc40SChuck Lever data->task.tk_pid, 9531da177e4SLinus Torvalds inode->i_sb->s_id, 9541da177e4SLinus Torvalds (long long)NFS_FILEID(inode), 9551da177e4SLinus Torvalds count, 9561da177e4SLinus Torvalds (unsigned long long)data->args.offset); 9571da177e4SLinus Torvalds } 9581da177e4SLinus Torvalds 9591da177e4SLinus Torvalds static void nfs_execute_write(struct nfs_write_data *data) 9601da177e4SLinus Torvalds { 9611da177e4SLinus Torvalds struct rpc_clnt *clnt = NFS_CLIENT(data->inode); 9621da177e4SLinus Torvalds sigset_t oldset; 9631da177e4SLinus Torvalds 9641da177e4SLinus Torvalds rpc_clnt_sigmask(clnt, &oldset); 9651da177e4SLinus Torvalds rpc_execute(&data->task); 9661da177e4SLinus Torvalds rpc_clnt_sigunmask(clnt, &oldset); 9671da177e4SLinus Torvalds } 9681da177e4SLinus Torvalds 9691da177e4SLinus Torvalds /* 9701da177e4SLinus Torvalds * Generate multiple small requests to write out a single 9711da177e4SLinus Torvalds * contiguous dirty area on one page. 9721da177e4SLinus Torvalds */ 9737d46a49fSTrond Myklebust static int nfs_flush_multi(struct inode *inode, struct list_head *head, int how) 9741da177e4SLinus Torvalds { 9751da177e4SLinus Torvalds struct nfs_page *req = nfs_list_entry(head->next); 9761da177e4SLinus Torvalds struct page *page = req->wb_page; 9771da177e4SLinus Torvalds struct nfs_write_data *data; 978e9f7bee1STrond Myklebust size_t wsize = NFS_SERVER(inode)->wsize, nbytes; 979e9f7bee1STrond Myklebust unsigned int offset; 9801da177e4SLinus Torvalds int requests = 0; 9811da177e4SLinus Torvalds LIST_HEAD(list); 9821da177e4SLinus Torvalds 9831da177e4SLinus Torvalds nfs_list_remove_request(req); 9841da177e4SLinus Torvalds 9851da177e4SLinus Torvalds nbytes = req->wb_bytes; 986e9f7bee1STrond Myklebust do { 987e9f7bee1STrond Myklebust size_t len = min(nbytes, wsize); 988e9f7bee1STrond Myklebust 989e9f7bee1STrond Myklebust data = nfs_writedata_alloc(len); 9901da177e4SLinus Torvalds if (!data) 9911da177e4SLinus Torvalds goto out_bad; 9921da177e4SLinus Torvalds list_add(&data->pages, &list); 9931da177e4SLinus Torvalds requests++; 994e9f7bee1STrond Myklebust nbytes -= len; 995e9f7bee1STrond Myklebust } while (nbytes != 0); 9961da177e4SLinus Torvalds atomic_set(&req->wb_complete, requests); 9971da177e4SLinus Torvalds 9981da177e4SLinus Torvalds ClearPageError(page); 999bb713d6dSTrond Myklebust set_page_writeback(page); 10001da177e4SLinus Torvalds offset = 0; 10011da177e4SLinus Torvalds nbytes = req->wb_bytes; 10021da177e4SLinus Torvalds do { 10031da177e4SLinus Torvalds data = list_entry(list.next, struct nfs_write_data, pages); 10041da177e4SLinus Torvalds list_del_init(&data->pages); 10051da177e4SLinus Torvalds 10061da177e4SLinus Torvalds data->pagevec[0] = page; 10071da177e4SLinus Torvalds 10081da177e4SLinus Torvalds if (nbytes > wsize) { 1009788e7a89STrond Myklebust nfs_write_rpcsetup(req, data, &nfs_write_partial_ops, 1010788e7a89STrond Myklebust wsize, offset, how); 10111da177e4SLinus Torvalds offset += wsize; 10121da177e4SLinus Torvalds nbytes -= wsize; 10131da177e4SLinus Torvalds } else { 1014788e7a89STrond Myklebust nfs_write_rpcsetup(req, data, &nfs_write_partial_ops, 1015788e7a89STrond Myklebust nbytes, offset, how); 10161da177e4SLinus Torvalds nbytes = 0; 10171da177e4SLinus Torvalds } 10181da177e4SLinus Torvalds nfs_execute_write(data); 10191da177e4SLinus Torvalds } while (nbytes != 0); 10201da177e4SLinus Torvalds 10211da177e4SLinus Torvalds return 0; 10221da177e4SLinus Torvalds 10231da177e4SLinus Torvalds out_bad: 10241da177e4SLinus Torvalds while (!list_empty(&list)) { 10251da177e4SLinus Torvalds data = list_entry(list.next, struct nfs_write_data, pages); 10261da177e4SLinus Torvalds list_del(&data->pages); 10278aca67f0STrond Myklebust nfs_writedata_release(data); 10281da177e4SLinus Torvalds } 10291da177e4SLinus Torvalds nfs_mark_request_dirty(req); 1030c6a556b8STrond Myklebust nfs_clear_page_writeback(req); 10311da177e4SLinus Torvalds return -ENOMEM; 10321da177e4SLinus Torvalds } 10331da177e4SLinus Torvalds 10341da177e4SLinus Torvalds /* 10351da177e4SLinus Torvalds * Create an RPC task for the given write request and kick it. 10361da177e4SLinus Torvalds * The page must have been locked by the caller. 10371da177e4SLinus Torvalds * 10381da177e4SLinus Torvalds * It may happen that the page we're passed is not marked dirty. 10391da177e4SLinus Torvalds * This is the case if nfs_updatepage detects a conflicting request 10401da177e4SLinus Torvalds * that has been written but not committed. 10411da177e4SLinus Torvalds */ 10427d46a49fSTrond Myklebust static int nfs_flush_one(struct inode *inode, struct list_head *head, int how) 10431da177e4SLinus Torvalds { 10441da177e4SLinus Torvalds struct nfs_page *req; 10451da177e4SLinus Torvalds struct page **pages; 10461da177e4SLinus Torvalds struct nfs_write_data *data; 10471da177e4SLinus Torvalds unsigned int count; 10481da177e4SLinus Torvalds 1049e9f7bee1STrond Myklebust data = nfs_writedata_alloc(NFS_SERVER(inode)->wsize); 10501da177e4SLinus Torvalds if (!data) 10511da177e4SLinus Torvalds goto out_bad; 10521da177e4SLinus Torvalds 10531da177e4SLinus Torvalds pages = data->pagevec; 10541da177e4SLinus Torvalds count = 0; 10551da177e4SLinus Torvalds while (!list_empty(head)) { 10561da177e4SLinus Torvalds req = nfs_list_entry(head->next); 10571da177e4SLinus Torvalds nfs_list_remove_request(req); 10581da177e4SLinus Torvalds nfs_list_add_request(req, &data->pages); 10591da177e4SLinus Torvalds ClearPageError(req->wb_page); 1060bb713d6dSTrond Myklebust set_page_writeback(req->wb_page); 10611da177e4SLinus Torvalds *pages++ = req->wb_page; 10621da177e4SLinus Torvalds count += req->wb_bytes; 10631da177e4SLinus Torvalds } 10641da177e4SLinus Torvalds req = nfs_list_entry(data->pages.next); 10651da177e4SLinus Torvalds 10661da177e4SLinus Torvalds /* Set up the argument struct */ 1067788e7a89STrond Myklebust nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how); 10681da177e4SLinus Torvalds 10691da177e4SLinus Torvalds nfs_execute_write(data); 10701da177e4SLinus Torvalds return 0; 10711da177e4SLinus Torvalds out_bad: 10721da177e4SLinus Torvalds while (!list_empty(head)) { 10731da177e4SLinus Torvalds struct nfs_page *req = nfs_list_entry(head->next); 10741da177e4SLinus Torvalds nfs_list_remove_request(req); 10751da177e4SLinus Torvalds nfs_mark_request_dirty(req); 1076c6a556b8STrond Myklebust nfs_clear_page_writeback(req); 10771da177e4SLinus Torvalds } 10781da177e4SLinus Torvalds return -ENOMEM; 10791da177e4SLinus Torvalds } 10801da177e4SLinus Torvalds 10817d46a49fSTrond Myklebust static int nfs_flush_list(struct inode *inode, struct list_head *head, int npages, int how) 10821da177e4SLinus Torvalds { 10831da177e4SLinus Torvalds LIST_HEAD(one_request); 10847d46a49fSTrond Myklebust int (*flush_one)(struct inode *, struct list_head *, int); 10851da177e4SLinus Torvalds struct nfs_page *req; 10867d46a49fSTrond Myklebust int wpages = NFS_SERVER(inode)->wpages; 10877d46a49fSTrond Myklebust int wsize = NFS_SERVER(inode)->wsize; 10887d46a49fSTrond Myklebust int error; 10891da177e4SLinus Torvalds 10907d46a49fSTrond Myklebust flush_one = nfs_flush_one; 10917d46a49fSTrond Myklebust if (wsize < PAGE_CACHE_SIZE) 10927d46a49fSTrond Myklebust flush_one = nfs_flush_multi; 10937d46a49fSTrond Myklebust /* For single writes, FLUSH_STABLE is more efficient */ 10947d46a49fSTrond Myklebust if (npages <= wpages && npages == NFS_I(inode)->npages 10957d46a49fSTrond Myklebust && nfs_list_entry(head->next)->wb_bytes <= wsize) 10967d46a49fSTrond Myklebust how |= FLUSH_STABLE; 10977d46a49fSTrond Myklebust 10987d46a49fSTrond Myklebust do { 10997d46a49fSTrond Myklebust nfs_coalesce_requests(head, &one_request, wpages); 11001da177e4SLinus Torvalds req = nfs_list_entry(one_request.next); 11017d46a49fSTrond Myklebust error = flush_one(inode, &one_request, how); 11021da177e4SLinus Torvalds if (error < 0) 11037d46a49fSTrond Myklebust goto out_err; 11047d46a49fSTrond Myklebust } while (!list_empty(head)); 11057d46a49fSTrond Myklebust return 0; 11067d46a49fSTrond Myklebust out_err: 11071da177e4SLinus Torvalds while (!list_empty(head)) { 11081da177e4SLinus Torvalds req = nfs_list_entry(head->next); 11091da177e4SLinus Torvalds nfs_list_remove_request(req); 11101da177e4SLinus Torvalds nfs_mark_request_dirty(req); 1111c6a556b8STrond Myklebust nfs_clear_page_writeback(req); 11121da177e4SLinus Torvalds } 11131da177e4SLinus Torvalds return error; 11141da177e4SLinus Torvalds } 11151da177e4SLinus Torvalds 11161da177e4SLinus Torvalds /* 11171da177e4SLinus Torvalds * Handle a write reply that flushed part of a page. 11181da177e4SLinus Torvalds */ 1119788e7a89STrond Myklebust static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata) 11201da177e4SLinus Torvalds { 1121788e7a89STrond Myklebust struct nfs_write_data *data = calldata; 11221da177e4SLinus Torvalds struct nfs_page *req = data->req; 11231da177e4SLinus Torvalds struct page *page = req->wb_page; 11241da177e4SLinus Torvalds 11251da177e4SLinus Torvalds dprintk("NFS: write (%s/%Ld %d@%Ld)", 11261da177e4SLinus Torvalds req->wb_context->dentry->d_inode->i_sb->s_id, 11271da177e4SLinus Torvalds (long long)NFS_FILEID(req->wb_context->dentry->d_inode), 11281da177e4SLinus Torvalds req->wb_bytes, 11291da177e4SLinus Torvalds (long long)req_offset(req)); 11301da177e4SLinus Torvalds 1131788e7a89STrond Myklebust if (nfs_writeback_done(task, data) != 0) 1132788e7a89STrond Myklebust return; 1133788e7a89STrond Myklebust 1134788e7a89STrond Myklebust if (task->tk_status < 0) { 11351da177e4SLinus Torvalds ClearPageUptodate(page); 11361da177e4SLinus Torvalds SetPageError(page); 1137788e7a89STrond Myklebust req->wb_context->error = task->tk_status; 1138788e7a89STrond Myklebust dprintk(", error = %d\n", task->tk_status); 11391da177e4SLinus Torvalds } else { 11401da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) 11411da177e4SLinus Torvalds if (data->verf.committed < NFS_FILE_SYNC) { 11421da177e4SLinus Torvalds if (!NFS_NEED_COMMIT(req)) { 11431da177e4SLinus Torvalds nfs_defer_commit(req); 11441da177e4SLinus Torvalds memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf)); 11451da177e4SLinus Torvalds dprintk(" defer commit\n"); 11461da177e4SLinus Torvalds } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) { 11471da177e4SLinus Torvalds nfs_defer_reschedule(req); 11481da177e4SLinus Torvalds dprintk(" server reboot detected\n"); 11491da177e4SLinus Torvalds } 11501da177e4SLinus Torvalds } else 11511da177e4SLinus Torvalds #endif 11521da177e4SLinus Torvalds dprintk(" OK\n"); 11531da177e4SLinus Torvalds } 11541da177e4SLinus Torvalds 11551da177e4SLinus Torvalds if (atomic_dec_and_test(&req->wb_complete)) 11561da177e4SLinus Torvalds nfs_writepage_release(req); 11571da177e4SLinus Torvalds } 11581da177e4SLinus Torvalds 1159788e7a89STrond Myklebust static const struct rpc_call_ops nfs_write_partial_ops = { 1160788e7a89STrond Myklebust .rpc_call_done = nfs_writeback_done_partial, 1161788e7a89STrond Myklebust .rpc_release = nfs_writedata_release, 1162788e7a89STrond Myklebust }; 1163788e7a89STrond Myklebust 11641da177e4SLinus Torvalds /* 11651da177e4SLinus Torvalds * Handle a write reply that flushes a whole page. 11661da177e4SLinus Torvalds * 11671da177e4SLinus Torvalds * FIXME: There is an inherent race with invalidate_inode_pages and 11681da177e4SLinus Torvalds * writebacks since the page->count is kept > 1 for as long 11691da177e4SLinus Torvalds * as the page has a write request pending. 11701da177e4SLinus Torvalds */ 1171788e7a89STrond Myklebust static void nfs_writeback_done_full(struct rpc_task *task, void *calldata) 11721da177e4SLinus Torvalds { 1173788e7a89STrond Myklebust struct nfs_write_data *data = calldata; 11741da177e4SLinus Torvalds struct nfs_page *req; 11751da177e4SLinus Torvalds struct page *page; 11761da177e4SLinus Torvalds 1177788e7a89STrond Myklebust if (nfs_writeback_done(task, data) != 0) 1178788e7a89STrond Myklebust return; 1179788e7a89STrond Myklebust 11801da177e4SLinus Torvalds /* Update attributes as result of writeback. */ 11811da177e4SLinus Torvalds while (!list_empty(&data->pages)) { 11821da177e4SLinus Torvalds req = nfs_list_entry(data->pages.next); 11831da177e4SLinus Torvalds nfs_list_remove_request(req); 11841da177e4SLinus Torvalds page = req->wb_page; 11851da177e4SLinus Torvalds 11861da177e4SLinus Torvalds dprintk("NFS: write (%s/%Ld %d@%Ld)", 11871da177e4SLinus Torvalds req->wb_context->dentry->d_inode->i_sb->s_id, 11881da177e4SLinus Torvalds (long long)NFS_FILEID(req->wb_context->dentry->d_inode), 11891da177e4SLinus Torvalds req->wb_bytes, 11901da177e4SLinus Torvalds (long long)req_offset(req)); 11911da177e4SLinus Torvalds 1192788e7a89STrond Myklebust if (task->tk_status < 0) { 11931da177e4SLinus Torvalds ClearPageUptodate(page); 11941da177e4SLinus Torvalds SetPageError(page); 1195788e7a89STrond Myklebust req->wb_context->error = task->tk_status; 11961da177e4SLinus Torvalds end_page_writeback(page); 11971da177e4SLinus Torvalds nfs_inode_remove_request(req); 1198788e7a89STrond Myklebust dprintk(", error = %d\n", task->tk_status); 11991da177e4SLinus Torvalds goto next; 12001da177e4SLinus Torvalds } 12011da177e4SLinus Torvalds end_page_writeback(page); 12021da177e4SLinus Torvalds 12031da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) 12041da177e4SLinus Torvalds if (data->args.stable != NFS_UNSTABLE || data->verf.committed == NFS_FILE_SYNC) { 12051da177e4SLinus Torvalds nfs_inode_remove_request(req); 12061da177e4SLinus Torvalds dprintk(" OK\n"); 12071da177e4SLinus Torvalds goto next; 12081da177e4SLinus Torvalds } 12091da177e4SLinus Torvalds memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf)); 12101da177e4SLinus Torvalds nfs_mark_request_commit(req); 12111da177e4SLinus Torvalds dprintk(" marked for commit\n"); 12121da177e4SLinus Torvalds #else 12131da177e4SLinus Torvalds nfs_inode_remove_request(req); 12141da177e4SLinus Torvalds #endif 12151da177e4SLinus Torvalds next: 1216c6a556b8STrond Myklebust nfs_clear_page_writeback(req); 12171da177e4SLinus Torvalds } 12181da177e4SLinus Torvalds } 12191da177e4SLinus Torvalds 1220788e7a89STrond Myklebust static const struct rpc_call_ops nfs_write_full_ops = { 1221788e7a89STrond Myklebust .rpc_call_done = nfs_writeback_done_full, 1222788e7a89STrond Myklebust .rpc_release = nfs_writedata_release, 1223788e7a89STrond Myklebust }; 1224788e7a89STrond Myklebust 1225788e7a89STrond Myklebust 12261da177e4SLinus Torvalds /* 12271da177e4SLinus Torvalds * This function is called when the WRITE call is complete. 12281da177e4SLinus Torvalds */ 1229462d5b32SChuck Lever int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data) 12301da177e4SLinus Torvalds { 12311da177e4SLinus Torvalds struct nfs_writeargs *argp = &data->args; 12321da177e4SLinus Torvalds struct nfs_writeres *resp = &data->res; 1233788e7a89STrond Myklebust int status; 12341da177e4SLinus Torvalds 12351da177e4SLinus Torvalds dprintk("NFS: %4d nfs_writeback_done (status %d)\n", 12361da177e4SLinus Torvalds task->tk_pid, task->tk_status); 12371da177e4SLinus Torvalds 1238f551e44fSChuck Lever /* 1239f551e44fSChuck Lever * ->write_done will attempt to use post-op attributes to detect 1240f551e44fSChuck Lever * conflicting writes by other clients. A strict interpretation 1241f551e44fSChuck Lever * of close-to-open would allow us to continue caching even if 1242f551e44fSChuck Lever * another writer had changed the file, but some applications 1243f551e44fSChuck Lever * depend on tighter cache coherency when writing. 1244f551e44fSChuck Lever */ 1245788e7a89STrond Myklebust status = NFS_PROTO(data->inode)->write_done(task, data); 1246788e7a89STrond Myklebust if (status != 0) 1247788e7a89STrond Myklebust return status; 124891d5b470SChuck Lever nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count); 124991d5b470SChuck Lever 12501da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) 12511da177e4SLinus Torvalds if (resp->verf->committed < argp->stable && task->tk_status >= 0) { 12521da177e4SLinus Torvalds /* We tried a write call, but the server did not 12531da177e4SLinus Torvalds * commit data to stable storage even though we 12541da177e4SLinus Torvalds * requested it. 12551da177e4SLinus Torvalds * Note: There is a known bug in Tru64 < 5.0 in which 12561da177e4SLinus Torvalds * the server reports NFS_DATA_SYNC, but performs 12571da177e4SLinus Torvalds * NFS_FILE_SYNC. We therefore implement this checking 12581da177e4SLinus Torvalds * as a dprintk() in order to avoid filling syslog. 12591da177e4SLinus Torvalds */ 12601da177e4SLinus Torvalds static unsigned long complain; 12611da177e4SLinus Torvalds 12621da177e4SLinus Torvalds if (time_before(complain, jiffies)) { 12631da177e4SLinus Torvalds dprintk("NFS: faulty NFS server %s:" 12641da177e4SLinus Torvalds " (committed = %d) != (stable = %d)\n", 126554ceac45SDavid Howells NFS_SERVER(data->inode)->nfs_client->cl_hostname, 12661da177e4SLinus Torvalds resp->verf->committed, argp->stable); 12671da177e4SLinus Torvalds complain = jiffies + 300 * HZ; 12681da177e4SLinus Torvalds } 12691da177e4SLinus Torvalds } 12701da177e4SLinus Torvalds #endif 12711da177e4SLinus Torvalds /* Is this a short write? */ 12721da177e4SLinus Torvalds if (task->tk_status >= 0 && resp->count < argp->count) { 12731da177e4SLinus Torvalds static unsigned long complain; 12741da177e4SLinus Torvalds 127591d5b470SChuck Lever nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE); 127691d5b470SChuck Lever 12771da177e4SLinus Torvalds /* Has the server at least made some progress? */ 12781da177e4SLinus Torvalds if (resp->count != 0) { 12791da177e4SLinus Torvalds /* Was this an NFSv2 write or an NFSv3 stable write? */ 12801da177e4SLinus Torvalds if (resp->verf->committed != NFS_UNSTABLE) { 12811da177e4SLinus Torvalds /* Resend from where the server left off */ 12821da177e4SLinus Torvalds argp->offset += resp->count; 12831da177e4SLinus Torvalds argp->pgbase += resp->count; 12841da177e4SLinus Torvalds argp->count -= resp->count; 12851da177e4SLinus Torvalds } else { 12861da177e4SLinus Torvalds /* Resend as a stable write in order to avoid 12871da177e4SLinus Torvalds * headaches in the case of a server crash. 12881da177e4SLinus Torvalds */ 12891da177e4SLinus Torvalds argp->stable = NFS_FILE_SYNC; 12901da177e4SLinus Torvalds } 12911da177e4SLinus Torvalds rpc_restart_call(task); 1292788e7a89STrond Myklebust return -EAGAIN; 12931da177e4SLinus Torvalds } 12941da177e4SLinus Torvalds if (time_before(complain, jiffies)) { 12951da177e4SLinus Torvalds printk(KERN_WARNING 12961da177e4SLinus Torvalds "NFS: Server wrote zero bytes, expected %u.\n", 12971da177e4SLinus Torvalds argp->count); 12981da177e4SLinus Torvalds complain = jiffies + 300 * HZ; 12991da177e4SLinus Torvalds } 13001da177e4SLinus Torvalds /* Can't do anything about it except throw an error. */ 13011da177e4SLinus Torvalds task->tk_status = -EIO; 13021da177e4SLinus Torvalds } 1303788e7a89STrond Myklebust return 0; 13041da177e4SLinus Torvalds } 13051da177e4SLinus Torvalds 13061da177e4SLinus Torvalds 13071da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) 1308963d8fe5STrond Myklebust void nfs_commit_release(void *wdata) 13091da177e4SLinus Torvalds { 13101da177e4SLinus Torvalds nfs_commit_free(wdata); 13111da177e4SLinus Torvalds } 13121da177e4SLinus Torvalds 13131da177e4SLinus Torvalds /* 13141da177e4SLinus Torvalds * Set up the argument/result storage required for the RPC call. 13151da177e4SLinus Torvalds */ 13161da177e4SLinus Torvalds static void nfs_commit_rpcsetup(struct list_head *head, 1317788e7a89STrond Myklebust struct nfs_write_data *data, 1318788e7a89STrond Myklebust int how) 13191da177e4SLinus Torvalds { 13203da28eb1STrond Myklebust struct nfs_page *first; 13211da177e4SLinus Torvalds struct inode *inode; 1322788e7a89STrond Myklebust int flags; 13231da177e4SLinus Torvalds 13241da177e4SLinus Torvalds /* Set up the RPC argument and reply structs 13251da177e4SLinus Torvalds * NB: take care not to mess about with data->commit et al. */ 13261da177e4SLinus Torvalds 13271da177e4SLinus Torvalds list_splice_init(head, &data->pages); 13281da177e4SLinus Torvalds first = nfs_list_entry(data->pages.next); 13291da177e4SLinus Torvalds inode = first->wb_context->dentry->d_inode; 13301da177e4SLinus Torvalds 13311da177e4SLinus Torvalds data->inode = inode; 13321da177e4SLinus Torvalds data->cred = first->wb_context->cred; 13331da177e4SLinus Torvalds 13341da177e4SLinus Torvalds data->args.fh = NFS_FH(data->inode); 13353da28eb1STrond Myklebust /* Note: we always request a commit of the entire inode */ 13363da28eb1STrond Myklebust data->args.offset = 0; 13373da28eb1STrond Myklebust data->args.count = 0; 13383da28eb1STrond Myklebust data->res.count = 0; 13391da177e4SLinus Torvalds data->res.fattr = &data->fattr; 13401da177e4SLinus Torvalds data->res.verf = &data->verf; 13410e574af1STrond Myklebust nfs_fattr_init(&data->fattr); 13421da177e4SLinus Torvalds 1343788e7a89STrond Myklebust /* Set up the initial task struct. */ 1344788e7a89STrond Myklebust flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC; 1345788e7a89STrond Myklebust rpc_init_task(&data->task, NFS_CLIENT(inode), flags, &nfs_commit_ops, data); 13461da177e4SLinus Torvalds NFS_PROTO(inode)->commit_setup(data, how); 13471da177e4SLinus Torvalds 13481da177e4SLinus Torvalds data->task.tk_priority = flush_task_priority(how); 13491da177e4SLinus Torvalds data->task.tk_cookie = (unsigned long)inode; 13501da177e4SLinus Torvalds 13510bbacc40SChuck Lever dprintk("NFS: %4d initiated commit call\n", data->task.tk_pid); 13521da177e4SLinus Torvalds } 13531da177e4SLinus Torvalds 13541da177e4SLinus Torvalds /* 13551da177e4SLinus Torvalds * Commit dirty pages 13561da177e4SLinus Torvalds */ 13571da177e4SLinus Torvalds static int 135840859d7eSChuck Lever nfs_commit_list(struct inode *inode, struct list_head *head, int how) 13591da177e4SLinus Torvalds { 13601da177e4SLinus Torvalds struct nfs_write_data *data; 13611da177e4SLinus Torvalds struct nfs_page *req; 13621da177e4SLinus Torvalds 1363e9f7bee1STrond Myklebust data = nfs_commit_alloc(); 13641da177e4SLinus Torvalds 13651da177e4SLinus Torvalds if (!data) 13661da177e4SLinus Torvalds goto out_bad; 13671da177e4SLinus Torvalds 13681da177e4SLinus Torvalds /* Set up the argument struct */ 13691da177e4SLinus Torvalds nfs_commit_rpcsetup(head, data, how); 13701da177e4SLinus Torvalds 13711da177e4SLinus Torvalds nfs_execute_write(data); 13721da177e4SLinus Torvalds return 0; 13731da177e4SLinus Torvalds out_bad: 13741da177e4SLinus Torvalds while (!list_empty(head)) { 13751da177e4SLinus Torvalds req = nfs_list_entry(head->next); 13761da177e4SLinus Torvalds nfs_list_remove_request(req); 13771da177e4SLinus Torvalds nfs_mark_request_commit(req); 137883715ad5STrond Myklebust dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS); 13795c2d97cbSTrond Myklebust nfs_clear_page_writeback(req); 13801da177e4SLinus Torvalds } 13811da177e4SLinus Torvalds return -ENOMEM; 13821da177e4SLinus Torvalds } 13831da177e4SLinus Torvalds 13841da177e4SLinus Torvalds /* 13851da177e4SLinus Torvalds * COMMIT call returned 13861da177e4SLinus Torvalds */ 1387788e7a89STrond Myklebust static void nfs_commit_done(struct rpc_task *task, void *calldata) 13881da177e4SLinus Torvalds { 1389963d8fe5STrond Myklebust struct nfs_write_data *data = calldata; 13901da177e4SLinus Torvalds struct nfs_page *req; 13911da177e4SLinus Torvalds 13921da177e4SLinus Torvalds dprintk("NFS: %4d nfs_commit_done (status %d)\n", 13931da177e4SLinus Torvalds task->tk_pid, task->tk_status); 13941da177e4SLinus Torvalds 1395788e7a89STrond Myklebust /* Call the NFS version-specific code */ 1396788e7a89STrond Myklebust if (NFS_PROTO(data->inode)->commit_done(task, data) != 0) 1397788e7a89STrond Myklebust return; 1398788e7a89STrond Myklebust 13991da177e4SLinus Torvalds while (!list_empty(&data->pages)) { 14001da177e4SLinus Torvalds req = nfs_list_entry(data->pages.next); 14011da177e4SLinus Torvalds nfs_list_remove_request(req); 1402fd39fc85SChristoph Lameter dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS); 14031da177e4SLinus Torvalds 14041da177e4SLinus Torvalds dprintk("NFS: commit (%s/%Ld %d@%Ld)", 14051da177e4SLinus Torvalds req->wb_context->dentry->d_inode->i_sb->s_id, 14061da177e4SLinus Torvalds (long long)NFS_FILEID(req->wb_context->dentry->d_inode), 14071da177e4SLinus Torvalds req->wb_bytes, 14081da177e4SLinus Torvalds (long long)req_offset(req)); 14091da177e4SLinus Torvalds if (task->tk_status < 0) { 14101da177e4SLinus Torvalds req->wb_context->error = task->tk_status; 14111da177e4SLinus Torvalds nfs_inode_remove_request(req); 14121da177e4SLinus Torvalds dprintk(", error = %d\n", task->tk_status); 14131da177e4SLinus Torvalds goto next; 14141da177e4SLinus Torvalds } 14151da177e4SLinus Torvalds 14161da177e4SLinus Torvalds /* Okay, COMMIT succeeded, apparently. Check the verifier 14171da177e4SLinus Torvalds * returned by the server against all stored verfs. */ 14181da177e4SLinus Torvalds if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) { 14191da177e4SLinus Torvalds /* We have a match */ 14201da177e4SLinus Torvalds nfs_inode_remove_request(req); 14211da177e4SLinus Torvalds dprintk(" OK\n"); 14221da177e4SLinus Torvalds goto next; 14231da177e4SLinus Torvalds } 14241da177e4SLinus Torvalds /* We have a mismatch. Write the page again */ 14251da177e4SLinus Torvalds dprintk(" mismatch\n"); 14261da177e4SLinus Torvalds nfs_mark_request_dirty(req); 14271da177e4SLinus Torvalds next: 1428c6a556b8STrond Myklebust nfs_clear_page_writeback(req); 14291da177e4SLinus Torvalds } 14301da177e4SLinus Torvalds } 1431788e7a89STrond Myklebust 1432788e7a89STrond Myklebust static const struct rpc_call_ops nfs_commit_ops = { 1433788e7a89STrond Myklebust .rpc_call_done = nfs_commit_done, 1434788e7a89STrond Myklebust .rpc_release = nfs_commit_release, 1435788e7a89STrond Myklebust }; 1436c42de9ddSTrond Myklebust #else 1437c42de9ddSTrond Myklebust static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how) 1438c42de9ddSTrond Myklebust { 1439c42de9ddSTrond Myklebust return 0; 1440c42de9ddSTrond Myklebust } 14411da177e4SLinus Torvalds #endif 14421da177e4SLinus Torvalds 1443*3f442547STrond Myklebust static long nfs_flush_mapping(struct address_space *mapping, struct writeback_control *wbc, int how) 14441da177e4SLinus Torvalds { 144528c6925fSTrond Myklebust struct nfs_inode *nfsi = NFS_I(mapping->host); 14461da177e4SLinus Torvalds LIST_HEAD(head); 1447*3f442547STrond Myklebust long res; 14481da177e4SLinus Torvalds 14491da177e4SLinus Torvalds spin_lock(&nfsi->req_lock); 1450*3f442547STrond Myklebust res = nfs_scan_dirty(mapping, wbc, &head); 14511da177e4SLinus Torvalds spin_unlock(&nfsi->req_lock); 1452ab0a3dbeSTrond Myklebust if (res) { 145328c6925fSTrond Myklebust int error = nfs_flush_list(mapping->host, &head, res, how); 14541da177e4SLinus Torvalds if (error < 0) 14551da177e4SLinus Torvalds return error; 14567d46a49fSTrond Myklebust } 14571da177e4SLinus Torvalds return res; 14581da177e4SLinus Torvalds } 14591da177e4SLinus Torvalds 14601da177e4SLinus Torvalds #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) 14613da28eb1STrond Myklebust int nfs_commit_inode(struct inode *inode, int how) 14621da177e4SLinus Torvalds { 14631da177e4SLinus Torvalds struct nfs_inode *nfsi = NFS_I(inode); 14641da177e4SLinus Torvalds LIST_HEAD(head); 14657d46a49fSTrond Myklebust int res; 14661da177e4SLinus Torvalds 14671da177e4SLinus Torvalds spin_lock(&nfsi->req_lock); 14683da28eb1STrond Myklebust res = nfs_scan_commit(inode, &head, 0, 0); 14693da28eb1STrond Myklebust spin_unlock(&nfsi->req_lock); 14701da177e4SLinus Torvalds if (res) { 14717d46a49fSTrond Myklebust int error = nfs_commit_list(inode, &head, how); 14721da177e4SLinus Torvalds if (error < 0) 14731da177e4SLinus Torvalds return error; 14743da28eb1STrond Myklebust } 14751da177e4SLinus Torvalds return res; 14761da177e4SLinus Torvalds } 14771da177e4SLinus Torvalds #endif 14781da177e4SLinus Torvalds 1479*3f442547STrond Myklebust long nfs_sync_inode_wait(struct inode *inode, unsigned long idx_start, 14801da177e4SLinus Torvalds unsigned int npages, int how) 14811da177e4SLinus Torvalds { 1482c42de9ddSTrond Myklebust struct nfs_inode *nfsi = NFS_I(inode); 1483*3f442547STrond Myklebust struct address_space *mapping = inode->i_mapping; 1484*3f442547STrond Myklebust struct writeback_control wbc = { 1485*3f442547STrond Myklebust .bdi = mapping->backing_dev_info, 1486*3f442547STrond Myklebust .sync_mode = WB_SYNC_ALL, 1487*3f442547STrond Myklebust .nr_to_write = LONG_MAX, 1488*3f442547STrond Myklebust .range_start = ((loff_t)idx_start) << PAGE_CACHE_SHIFT, 1489*3f442547STrond Myklebust .range_end = ((loff_t)(idx_start + npages - 1)) << PAGE_CACHE_SHIFT, 1490*3f442547STrond Myklebust }; 1491c42de9ddSTrond Myklebust LIST_HEAD(head); 149270b9ecbdSTrond Myklebust int nocommit = how & FLUSH_NOCOMMIT; 1493*3f442547STrond Myklebust long pages, ret; 14941da177e4SLinus Torvalds 1495c42de9ddSTrond Myklebust how &= ~FLUSH_NOCOMMIT; 1496c42de9ddSTrond Myklebust spin_lock(&nfsi->req_lock); 14971da177e4SLinus Torvalds do { 1498c42de9ddSTrond Myklebust ret = nfs_wait_on_requests_locked(inode, idx_start, npages); 1499c42de9ddSTrond Myklebust if (ret != 0) 1500c42de9ddSTrond Myklebust continue; 1501*3f442547STrond Myklebust pages = nfs_scan_dirty(mapping, &wbc, &head); 1502c42de9ddSTrond Myklebust if (pages != 0) { 1503c42de9ddSTrond Myklebust spin_unlock(&nfsi->req_lock); 1504e8e058e8STrond Myklebust if (how & FLUSH_INVALIDATE) { 150583715ad5STrond Myklebust nfs_cancel_dirty_list(&head); 1506e8e058e8STrond Myklebust ret = pages; 1507e8e058e8STrond Myklebust } else 1508c42de9ddSTrond Myklebust ret = nfs_flush_list(inode, &head, pages, how); 1509c42de9ddSTrond Myklebust spin_lock(&nfsi->req_lock); 151070b9ecbdSTrond Myklebust continue; 151170b9ecbdSTrond Myklebust } 1512c42de9ddSTrond Myklebust if (nocommit) 1513c42de9ddSTrond Myklebust break; 1514d2ccddf0STrond Myklebust pages = nfs_scan_commit(inode, &head, idx_start, npages); 1515c42de9ddSTrond Myklebust if (pages == 0) 1516c42de9ddSTrond Myklebust break; 1517d2ccddf0STrond Myklebust if (how & FLUSH_INVALIDATE) { 1518d2ccddf0STrond Myklebust spin_unlock(&nfsi->req_lock); 151983715ad5STrond Myklebust nfs_cancel_commit_list(&head); 1520e8e058e8STrond Myklebust ret = pages; 1521d2ccddf0STrond Myklebust spin_lock(&nfsi->req_lock); 1522d2ccddf0STrond Myklebust continue; 1523d2ccddf0STrond Myklebust } 1524d2ccddf0STrond Myklebust pages += nfs_scan_commit(inode, &head, 0, 0); 1525c42de9ddSTrond Myklebust spin_unlock(&nfsi->req_lock); 1526c42de9ddSTrond Myklebust ret = nfs_commit_list(inode, &head, how); 1527c42de9ddSTrond Myklebust spin_lock(&nfsi->req_lock); 1528c42de9ddSTrond Myklebust } while (ret >= 0); 1529c42de9ddSTrond Myklebust spin_unlock(&nfsi->req_lock); 1530c42de9ddSTrond Myklebust return ret; 15311da177e4SLinus Torvalds } 15321da177e4SLinus Torvalds 1533f7b422b1SDavid Howells int __init nfs_init_writepagecache(void) 15341da177e4SLinus Torvalds { 15351da177e4SLinus Torvalds nfs_wdata_cachep = kmem_cache_create("nfs_write_data", 15361da177e4SLinus Torvalds sizeof(struct nfs_write_data), 15371da177e4SLinus Torvalds 0, SLAB_HWCACHE_ALIGN, 15381da177e4SLinus Torvalds NULL, NULL); 15391da177e4SLinus Torvalds if (nfs_wdata_cachep == NULL) 15401da177e4SLinus Torvalds return -ENOMEM; 15411da177e4SLinus Torvalds 154293d2341cSMatthew Dobson nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE, 15431da177e4SLinus Torvalds nfs_wdata_cachep); 15441da177e4SLinus Torvalds if (nfs_wdata_mempool == NULL) 15451da177e4SLinus Torvalds return -ENOMEM; 15461da177e4SLinus Torvalds 154793d2341cSMatthew Dobson nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT, 15481da177e4SLinus Torvalds nfs_wdata_cachep); 15491da177e4SLinus Torvalds if (nfs_commit_mempool == NULL) 15501da177e4SLinus Torvalds return -ENOMEM; 15511da177e4SLinus Torvalds 15521da177e4SLinus Torvalds return 0; 15531da177e4SLinus Torvalds } 15541da177e4SLinus Torvalds 1555266bee88SDavid Brownell void nfs_destroy_writepagecache(void) 15561da177e4SLinus Torvalds { 15571da177e4SLinus Torvalds mempool_destroy(nfs_commit_mempool); 15581da177e4SLinus Torvalds mempool_destroy(nfs_wdata_mempool); 15591a1d92c1SAlexey Dobriyan kmem_cache_destroy(nfs_wdata_cachep); 15601da177e4SLinus Torvalds } 15611da177e4SLinus Torvalds 1562