Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/nfs/write.c |
| 3 | * |
Trond Myklebust | 7c85d90 | 2006-12-13 15:23:48 -0500 | [diff] [blame] | 4 | * Write file data over NFS. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * |
| 6 | * Copyright (C) 1996, 1997, Olaf Kirch <okir@monad.swb.de> |
| 7 | */ |
| 8 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <linux/types.h> |
| 10 | #include <linux/slab.h> |
| 11 | #include <linux/mm.h> |
| 12 | #include <linux/pagemap.h> |
| 13 | #include <linux/file.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/writeback.h> |
Peter Zijlstra | 89a0914 | 2007-03-16 13:38:26 -0800 | [diff] [blame] | 15 | #include <linux/swap.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | |
| 17 | #include <linux/sunrpc/clnt.h> |
| 18 | #include <linux/nfs_fs.h> |
| 19 | #include <linux/nfs_mount.h> |
| 20 | #include <linux/nfs_page.h> |
Andrew Morton | 3fcfab1 | 2006-10-19 23:28:16 -0700 | [diff] [blame] | 21 | #include <linux/backing-dev.h> |
| 22 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <asm/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
| 25 | #include "delegation.h" |
Trond Myklebust | 49a70f2 | 2006-12-05 00:35:38 -0500 | [diff] [blame] | 26 | #include "internal.h" |
Chuck Lever | 91d5b47 | 2006-03-20 13:44:14 -0500 | [diff] [blame] | 27 | #include "iostat.h" |
Andy Adamson | def6ed7 | 2009-04-01 09:22:26 -0400 | [diff] [blame] | 28 | #include "nfs4_fs.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
| 30 | #define NFSDBG_FACILITY NFSDBG_PAGECACHE |
| 31 | |
| 32 | #define MIN_POOL_WRITE (32) |
| 33 | #define MIN_POOL_COMMIT (4) |
| 34 | |
| 35 | /* |
| 36 | * Local function declarations |
| 37 | */ |
Trond Myklebust | c63c7b0 | 2007-04-02 19:29:52 -0400 | [diff] [blame] | 38 | static void nfs_pageio_init_write(struct nfs_pageio_descriptor *desc, |
| 39 | struct inode *inode, int ioflags); |
Fred Isaman | f8512ad | 2008-03-19 11:24:39 -0400 | [diff] [blame] | 40 | static void nfs_redirty_request(struct nfs_page *req); |
Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 41 | static const struct rpc_call_ops nfs_write_partial_ops; |
| 42 | static const struct rpc_call_ops nfs_write_full_ops; |
| 43 | static const struct rpc_call_ops nfs_commit_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 45 | static struct kmem_cache *nfs_wdata_cachep; |
Trond Myklebust | 3feb2d4 | 2006-03-20 13:44:37 -0500 | [diff] [blame] | 46 | static mempool_t *nfs_wdata_mempool; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | static mempool_t *nfs_commit_mempool; |
| 48 | |
Trond Myklebust | c9d8f89 | 2008-04-15 16:56:39 -0400 | [diff] [blame] | 49 | struct nfs_write_data *nfs_commitdata_alloc(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | { |
Christoph Lameter | e6b4f8d | 2006-12-06 20:33:14 -0800 | [diff] [blame] | 51 | struct nfs_write_data *p = mempool_alloc(nfs_commit_mempool, GFP_NOFS); |
Chuck Lever | 40859d7 | 2005-11-30 18:09:02 -0500 | [diff] [blame] | 52 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | if (p) { |
| 54 | memset(p, 0, sizeof(*p)); |
| 55 | INIT_LIST_HEAD(&p->pages); |
Andy Adamson | 5f7dbd5 | 2009-04-01 09:22:05 -0400 | [diff] [blame] | 56 | p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | } |
| 58 | return p; |
| 59 | } |
| 60 | |
Trond Myklebust | 5e4424a | 2008-02-25 21:53:49 -0800 | [diff] [blame] | 61 | void nfs_commit_free(struct nfs_write_data *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | { |
Chuck Lever | 40859d7 | 2005-11-30 18:09:02 -0500 | [diff] [blame] | 63 | if (p && (p->pagevec != &p->page_array[0])) |
| 64 | kfree(p->pagevec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | mempool_free(p, nfs_commit_mempool); |
| 66 | } |
| 67 | |
Trond Myklebust | 8d5658c | 2007-04-10 09:26:35 -0400 | [diff] [blame] | 68 | struct nfs_write_data *nfs_writedata_alloc(unsigned int pagecount) |
Trond Myklebust | 3feb2d4 | 2006-03-20 13:44:37 -0500 | [diff] [blame] | 69 | { |
Christoph Lameter | e6b4f8d | 2006-12-06 20:33:14 -0800 | [diff] [blame] | 70 | struct nfs_write_data *p = mempool_alloc(nfs_wdata_mempool, GFP_NOFS); |
Trond Myklebust | 3feb2d4 | 2006-03-20 13:44:37 -0500 | [diff] [blame] | 71 | |
| 72 | if (p) { |
| 73 | memset(p, 0, sizeof(*p)); |
| 74 | INIT_LIST_HEAD(&p->pages); |
Trond Myklebust | e9f7bee | 2006-09-08 09:48:54 -0700 | [diff] [blame] | 75 | p->npages = pagecount; |
Andy Adamson | 5f7dbd5 | 2009-04-01 09:22:05 -0400 | [diff] [blame] | 76 | p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE; |
Chuck Lever | 0d0b5cb | 2006-05-25 01:40:53 -0400 | [diff] [blame] | 77 | if (pagecount <= ARRAY_SIZE(p->page_array)) |
| 78 | p->pagevec = p->page_array; |
Trond Myklebust | 3feb2d4 | 2006-03-20 13:44:37 -0500 | [diff] [blame] | 79 | else { |
Chuck Lever | 0d0b5cb | 2006-05-25 01:40:53 -0400 | [diff] [blame] | 80 | p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS); |
| 81 | if (!p->pagevec) { |
Trond Myklebust | 3feb2d4 | 2006-03-20 13:44:37 -0500 | [diff] [blame] | 82 | mempool_free(p, nfs_wdata_mempool); |
| 83 | p = NULL; |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | return p; |
| 88 | } |
| 89 | |
Trond Myklebust | 1ae88b2 | 2009-08-12 09:12:30 -0400 | [diff] [blame] | 90 | void nfs_writedata_free(struct nfs_write_data *p) |
Trond Myklebust | 3feb2d4 | 2006-03-20 13:44:37 -0500 | [diff] [blame] | 91 | { |
| 92 | if (p && (p->pagevec != &p->page_array[0])) |
| 93 | kfree(p->pagevec); |
| 94 | mempool_free(p, nfs_wdata_mempool); |
| 95 | } |
| 96 | |
Trond Myklebust | 1ae88b2 | 2009-08-12 09:12:30 -0400 | [diff] [blame] | 97 | static void nfs_writedata_release(struct nfs_write_data *wdata) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | { |
Trond Myklebust | 383ba71 | 2008-02-19 20:04:20 -0500 | [diff] [blame] | 99 | put_nfs_open_context(wdata->args.context); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | nfs_writedata_free(wdata); |
| 101 | } |
| 102 | |
Trond Myklebust | 7b159fc | 2007-07-25 14:09:54 -0400 | [diff] [blame] | 103 | static void nfs_context_set_write_error(struct nfs_open_context *ctx, int error) |
| 104 | { |
| 105 | ctx->error = error; |
| 106 | smp_wmb(); |
| 107 | set_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags); |
| 108 | } |
| 109 | |
Trond Myklebust | 277459d | 2006-12-05 00:35:35 -0500 | [diff] [blame] | 110 | static struct nfs_page *nfs_page_find_request_locked(struct page *page) |
| 111 | { |
| 112 | struct nfs_page *req = NULL; |
| 113 | |
| 114 | if (PagePrivate(page)) { |
| 115 | req = (struct nfs_page *)page_private(page); |
| 116 | if (req != NULL) |
Trond Myklebust | c03b402 | 2007-06-17 13:26:38 -0400 | [diff] [blame] | 117 | kref_get(&req->wb_kref); |
Trond Myklebust | 277459d | 2006-12-05 00:35:35 -0500 | [diff] [blame] | 118 | } |
| 119 | return req; |
| 120 | } |
| 121 | |
| 122 | static struct nfs_page *nfs_page_find_request(struct page *page) |
| 123 | { |
Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 124 | struct inode *inode = page->mapping->host; |
Trond Myklebust | 277459d | 2006-12-05 00:35:35 -0500 | [diff] [blame] | 125 | struct nfs_page *req = NULL; |
Trond Myklebust | 277459d | 2006-12-05 00:35:35 -0500 | [diff] [blame] | 126 | |
Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 127 | spin_lock(&inode->i_lock); |
Trond Myklebust | 277459d | 2006-12-05 00:35:35 -0500 | [diff] [blame] | 128 | req = nfs_page_find_request_locked(page); |
Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 129 | spin_unlock(&inode->i_lock); |
Trond Myklebust | 277459d | 2006-12-05 00:35:35 -0500 | [diff] [blame] | 130 | return req; |
| 131 | } |
| 132 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | /* Adjust the file length if we're writing beyond the end */ |
| 134 | static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count) |
| 135 | { |
| 136 | struct inode *inode = page->mapping->host; |
Trond Myklebust | a3d0145 | 2008-06-11 12:21:19 -0400 | [diff] [blame] | 137 | loff_t end, i_size; |
| 138 | pgoff_t end_index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | |
Trond Myklebust | a3d0145 | 2008-06-11 12:21:19 -0400 | [diff] [blame] | 140 | spin_lock(&inode->i_lock); |
| 141 | i_size = i_size_read(inode); |
| 142 | end_index = (i_size - 1) >> PAGE_CACHE_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | if (i_size > 0 && page->index < end_index) |
Trond Myklebust | a3d0145 | 2008-06-11 12:21:19 -0400 | [diff] [blame] | 144 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count); |
| 146 | if (i_size >= end) |
Trond Myklebust | a3d0145 | 2008-06-11 12:21:19 -0400 | [diff] [blame] | 147 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | i_size_write(inode, end); |
Trond Myklebust | a3d0145 | 2008-06-11 12:21:19 -0400 | [diff] [blame] | 149 | nfs_inc_stats(inode, NFSIOS_EXTENDWRITE); |
| 150 | out: |
| 151 | spin_unlock(&inode->i_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Trond Myklebust | a301b77 | 2007-02-06 11:07:15 -0800 | [diff] [blame] | 154 | /* A writeback failed: mark the page as bad, and invalidate the page cache */ |
| 155 | static void nfs_set_pageerror(struct page *page) |
| 156 | { |
| 157 | SetPageError(page); |
| 158 | nfs_zap_mapping(page->mapping->host, page->mapping); |
| 159 | } |
| 160 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | /* We can set the PG_uptodate flag if we see that a write request |
| 162 | * covers the full page. |
| 163 | */ |
| 164 | static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count) |
| 165 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | if (PageUptodate(page)) |
| 167 | return; |
| 168 | if (base != 0) |
| 169 | return; |
Trond Myklebust | 49a70f2 | 2006-12-05 00:35:38 -0500 | [diff] [blame] | 170 | if (count != nfs_page_length(page)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | return; |
Trond Myklebust | 49a70f2 | 2006-12-05 00:35:38 -0500 | [diff] [blame] | 172 | SetPageUptodate(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | static int wb_priority(struct writeback_control *wbc) |
| 176 | { |
| 177 | if (wbc->for_reclaim) |
Trond Myklebust | c63c7b0 | 2007-04-02 19:29:52 -0400 | [diff] [blame] | 178 | return FLUSH_HIGHPRI | FLUSH_STABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | if (wbc->for_kupdate) |
| 180 | return FLUSH_LOWPRI; |
| 181 | return 0; |
| 182 | } |
| 183 | |
| 184 | /* |
Peter Zijlstra | 89a0914 | 2007-03-16 13:38:26 -0800 | [diff] [blame] | 185 | * NFS congestion control |
| 186 | */ |
| 187 | |
| 188 | int nfs_congestion_kb; |
| 189 | |
| 190 | #define NFS_CONGESTION_ON_THRESH (nfs_congestion_kb >> (PAGE_SHIFT-10)) |
| 191 | #define NFS_CONGESTION_OFF_THRESH \ |
| 192 | (NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2)) |
| 193 | |
Trond Myklebust | 5a6d41b | 2007-04-14 19:10:12 -0400 | [diff] [blame] | 194 | static int nfs_set_page_writeback(struct page *page) |
Peter Zijlstra | 89a0914 | 2007-03-16 13:38:26 -0800 | [diff] [blame] | 195 | { |
Trond Myklebust | 5a6d41b | 2007-04-14 19:10:12 -0400 | [diff] [blame] | 196 | int ret = test_set_page_writeback(page); |
| 197 | |
| 198 | if (!ret) { |
Peter Zijlstra | 89a0914 | 2007-03-16 13:38:26 -0800 | [diff] [blame] | 199 | struct inode *inode = page->mapping->host; |
| 200 | struct nfs_server *nfss = NFS_SERVER(inode); |
| 201 | |
Peter Zijlstra | 277866a | 2007-05-08 00:35:12 -0700 | [diff] [blame] | 202 | if (atomic_long_inc_return(&nfss->writeback) > |
Jens Axboe | 8aa7e84 | 2009-07-09 14:52:32 +0200 | [diff] [blame] | 203 | NFS_CONGESTION_ON_THRESH) { |
| 204 | set_bdi_congested(&nfss->backing_dev_info, |
| 205 | BLK_RW_ASYNC); |
| 206 | } |
Peter Zijlstra | 89a0914 | 2007-03-16 13:38:26 -0800 | [diff] [blame] | 207 | } |
Trond Myklebust | 5a6d41b | 2007-04-14 19:10:12 -0400 | [diff] [blame] | 208 | return ret; |
Peter Zijlstra | 89a0914 | 2007-03-16 13:38:26 -0800 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | static void nfs_end_page_writeback(struct page *page) |
| 212 | { |
| 213 | struct inode *inode = page->mapping->host; |
| 214 | struct nfs_server *nfss = NFS_SERVER(inode); |
| 215 | |
| 216 | end_page_writeback(page); |
Peter Zijlstra | c4dc4be | 2007-10-16 23:25:41 -0700 | [diff] [blame] | 217 | if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH) |
Jens Axboe | 8aa7e84 | 2009-07-09 14:52:32 +0200 | [diff] [blame] | 218 | clear_bdi_congested(&nfss->backing_dev_info, BLK_RW_ASYNC); |
Peter Zijlstra | 89a0914 | 2007-03-16 13:38:26 -0800 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | /* |
Trond Myklebust | e261f51 | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 222 | * Find an associated nfs write request, and prepare to flush it out |
Trond Myklebust | 9cccef9 | 2007-07-22 17:09:05 -0400 | [diff] [blame] | 223 | * May return an error if the user signalled nfs_wait_on_request(). |
Trond Myklebust | e261f51 | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 224 | */ |
Trond Myklebust | c63c7b0 | 2007-04-02 19:29:52 -0400 | [diff] [blame] | 225 | static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio, |
| 226 | struct page *page) |
Trond Myklebust | e261f51 | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 227 | { |
Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 228 | struct inode *inode = page->mapping->host; |
Trond Myklebust | e261f51 | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 229 | struct nfs_page *req; |
Trond Myklebust | e261f51 | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 230 | int ret; |
| 231 | |
Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 232 | spin_lock(&inode->i_lock); |
Trond Myklebust | e261f51 | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 233 | for(;;) { |
| 234 | req = nfs_page_find_request_locked(page); |
| 235 | if (req == NULL) { |
Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 236 | spin_unlock(&inode->i_lock); |
Trond Myklebust | 9cccef9 | 2007-07-22 17:09:05 -0400 | [diff] [blame] | 237 | return 0; |
Trond Myklebust | e261f51 | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 238 | } |
Trond Myklebust | acee478 | 2008-01-22 17:13:07 -0500 | [diff] [blame] | 239 | if (nfs_set_page_tag_locked(req)) |
Trond Myklebust | e261f51 | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 240 | break; |
| 241 | /* Note: If we hold the page lock, as is the case in nfs_writepage, |
Trond Myklebust | acee478 | 2008-01-22 17:13:07 -0500 | [diff] [blame] | 242 | * then the call to nfs_set_page_tag_locked() will always |
Trond Myklebust | e261f51 | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 243 | * succeed provided that someone hasn't already marked the |
| 244 | * request as dirty (in which case we don't care). |
| 245 | */ |
Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 246 | spin_unlock(&inode->i_lock); |
Trond Myklebust | e261f51 | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 247 | ret = nfs_wait_on_request(req); |
| 248 | nfs_release_request(req); |
| 249 | if (ret != 0) |
| 250 | return ret; |
Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 251 | spin_lock(&inode->i_lock); |
Trond Myklebust | e261f51 | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 252 | } |
Trond Myklebust | e468bae | 2008-06-13 13:25:22 -0400 | [diff] [blame] | 253 | if (test_bit(PG_CLEAN, &req->wb_flags)) { |
Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 254 | spin_unlock(&inode->i_lock); |
Trond Myklebust | e468bae | 2008-06-13 13:25:22 -0400 | [diff] [blame] | 255 | BUG(); |
Trond Myklebust | 612c938 | 2007-04-20 16:12:45 -0400 | [diff] [blame] | 256 | } |
Trond Myklebust | c63c7b0 | 2007-04-02 19:29:52 -0400 | [diff] [blame] | 257 | if (nfs_set_page_writeback(page) != 0) { |
Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 258 | spin_unlock(&inode->i_lock); |
Trond Myklebust | c63c7b0 | 2007-04-02 19:29:52 -0400 | [diff] [blame] | 259 | BUG(); |
| 260 | } |
Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 261 | spin_unlock(&inode->i_lock); |
Fred Isaman | f8512ad | 2008-03-19 11:24:39 -0400 | [diff] [blame] | 262 | if (!nfs_pageio_add_request(pgio, req)) { |
| 263 | nfs_redirty_request(req); |
Fred Isaman | f8512ad | 2008-03-19 11:24:39 -0400 | [diff] [blame] | 264 | return pgio->pg_error; |
| 265 | } |
Trond Myklebust | 9cccef9 | 2007-07-22 17:09:05 -0400 | [diff] [blame] | 266 | return 0; |
Trond Myklebust | e261f51 | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 267 | } |
| 268 | |
Trond Myklebust | f758c885 | 2007-07-22 19:27:32 -0400 | [diff] [blame] | 269 | static int nfs_do_writepage(struct page *page, struct writeback_control *wbc, struct nfs_pageio_descriptor *pgio) |
| 270 | { |
| 271 | struct inode *inode = page->mapping->host; |
| 272 | |
| 273 | nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE); |
| 274 | nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1); |
| 275 | |
| 276 | nfs_pageio_cond_complete(pgio, page->index); |
| 277 | return nfs_page_async_flush(pgio, page); |
| 278 | } |
| 279 | |
Trond Myklebust | e261f51 | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 280 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | * Write an mmapped page to the server. |
| 282 | */ |
Trond Myklebust | 4d770cc | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 283 | static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | { |
Trond Myklebust | f758c885 | 2007-07-22 19:27:32 -0400 | [diff] [blame] | 285 | struct nfs_pageio_descriptor pgio; |
Trond Myklebust | e261f51 | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 286 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | |
Trond Myklebust | f758c885 | 2007-07-22 19:27:32 -0400 | [diff] [blame] | 288 | nfs_pageio_init_write(&pgio, page->mapping->host, wb_priority(wbc)); |
| 289 | err = nfs_do_writepage(page, wbc, &pgio); |
| 290 | nfs_pageio_complete(&pgio); |
| 291 | if (err < 0) |
| 292 | return err; |
| 293 | if (pgio.pg_error < 0) |
| 294 | return pgio.pg_error; |
| 295 | return 0; |
Trond Myklebust | 4d770cc | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | int nfs_writepage(struct page *page, struct writeback_control *wbc) |
| 299 | { |
Trond Myklebust | f758c885 | 2007-07-22 19:27:32 -0400 | [diff] [blame] | 300 | int ret; |
Trond Myklebust | 4d770cc | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 301 | |
Trond Myklebust | f758c885 | 2007-07-22 19:27:32 -0400 | [diff] [blame] | 302 | ret = nfs_writepage_locked(page, wbc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | unlock_page(page); |
Trond Myklebust | f758c885 | 2007-07-22 19:27:32 -0400 | [diff] [blame] | 304 | return ret; |
| 305 | } |
| 306 | |
| 307 | static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data) |
| 308 | { |
| 309 | int ret; |
| 310 | |
| 311 | ret = nfs_do_writepage(page, wbc, data); |
| 312 | unlock_page(page); |
| 313 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | } |
| 315 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc) |
| 317 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | struct inode *inode = mapping->host; |
Trond Myklebust | 72cb77f | 2009-03-11 14:10:30 -0400 | [diff] [blame] | 319 | unsigned long *bitlock = &NFS_I(inode)->flags; |
Trond Myklebust | c63c7b0 | 2007-04-02 19:29:52 -0400 | [diff] [blame] | 320 | struct nfs_pageio_descriptor pgio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | int err; |
| 322 | |
Trond Myklebust | 72cb77f | 2009-03-11 14:10:30 -0400 | [diff] [blame] | 323 | /* Stop dirtying of new pages while we sync */ |
| 324 | err = wait_on_bit_lock(bitlock, NFS_INO_FLUSHING, |
| 325 | nfs_wait_bit_killable, TASK_KILLABLE); |
| 326 | if (err) |
| 327 | goto out_err; |
| 328 | |
Chuck Lever | 91d5b47 | 2006-03-20 13:44:14 -0500 | [diff] [blame] | 329 | nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES); |
| 330 | |
Trond Myklebust | c63c7b0 | 2007-04-02 19:29:52 -0400 | [diff] [blame] | 331 | nfs_pageio_init_write(&pgio, inode, wb_priority(wbc)); |
Trond Myklebust | f758c885 | 2007-07-22 19:27:32 -0400 | [diff] [blame] | 332 | err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio); |
Trond Myklebust | c63c7b0 | 2007-04-02 19:29:52 -0400 | [diff] [blame] | 333 | nfs_pageio_complete(&pgio); |
Trond Myklebust | 72cb77f | 2009-03-11 14:10:30 -0400 | [diff] [blame] | 334 | |
| 335 | clear_bit_unlock(NFS_INO_FLUSHING, bitlock); |
| 336 | smp_mb__after_clear_bit(); |
| 337 | wake_up_bit(bitlock, NFS_INO_FLUSHING); |
| 338 | |
Trond Myklebust | f758c885 | 2007-07-22 19:27:32 -0400 | [diff] [blame] | 339 | if (err < 0) |
Trond Myklebust | 72cb77f | 2009-03-11 14:10:30 -0400 | [diff] [blame] | 340 | goto out_err; |
| 341 | err = pgio.pg_error; |
| 342 | if (err < 0) |
| 343 | goto out_err; |
Trond Myklebust | c63c7b0 | 2007-04-02 19:29:52 -0400 | [diff] [blame] | 344 | return 0; |
Trond Myklebust | 72cb77f | 2009-03-11 14:10:30 -0400 | [diff] [blame] | 345 | out_err: |
| 346 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | /* |
| 350 | * Insert a write request into an inode |
| 351 | */ |
Trond Myklebust | e7d3906 | 2008-06-13 12:12:32 -0400 | [diff] [blame] | 352 | static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | { |
| 354 | struct nfs_inode *nfsi = NFS_I(inode); |
| 355 | int error; |
| 356 | |
Trond Myklebust | e7d3906 | 2008-06-13 12:12:32 -0400 | [diff] [blame] | 357 | error = radix_tree_preload(GFP_NOFS); |
| 358 | if (error != 0) |
| 359 | goto out; |
| 360 | |
| 361 | /* Lock the request! */ |
| 362 | nfs_lock_request_dontget(req); |
| 363 | |
| 364 | spin_lock(&inode->i_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req); |
Nick Piggin | 2785259 | 2008-02-04 23:48:37 -0800 | [diff] [blame] | 366 | BUG_ON(error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | if (!nfsi->npages) { |
| 368 | igrab(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | if (nfs_have_delegation(inode, FMODE_WRITE)) |
| 370 | nfsi->change_attr++; |
| 371 | } |
Trond Myklebust | deb7d63 | 2006-03-20 13:44:50 -0500 | [diff] [blame] | 372 | SetPagePrivate(req->wb_page); |
Trond Myklebust | 277459d | 2006-12-05 00:35:35 -0500 | [diff] [blame] | 373 | set_page_private(req->wb_page, (unsigned long)req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | nfsi->npages++; |
Trond Myklebust | c03b402 | 2007-06-17 13:26:38 -0400 | [diff] [blame] | 375 | kref_get(&req->wb_kref); |
Nick Piggin | 2785259 | 2008-02-04 23:48:37 -0800 | [diff] [blame] | 376 | radix_tree_tag_set(&nfsi->nfs_page_tree, req->wb_index, |
| 377 | NFS_PAGE_TAG_LOCKED); |
Trond Myklebust | e7d3906 | 2008-06-13 12:12:32 -0400 | [diff] [blame] | 378 | spin_unlock(&inode->i_lock); |
| 379 | radix_tree_preload_end(); |
| 380 | out: |
| 381 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | /* |
Peter Zijlstra | 89a0914 | 2007-03-16 13:38:26 -0800 | [diff] [blame] | 385 | * Remove a write request from an inode |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | */ |
| 387 | static void nfs_inode_remove_request(struct nfs_page *req) |
| 388 | { |
Trond Myklebust | 88be9f9 | 2007-06-05 10:42:27 -0400 | [diff] [blame] | 389 | struct inode *inode = req->wb_context->path.dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | struct nfs_inode *nfsi = NFS_I(inode); |
| 391 | |
| 392 | BUG_ON (!NFS_WBACK_BUSY(req)); |
| 393 | |
Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 394 | spin_lock(&inode->i_lock); |
Trond Myklebust | 277459d | 2006-12-05 00:35:35 -0500 | [diff] [blame] | 395 | set_page_private(req->wb_page, 0); |
Trond Myklebust | deb7d63 | 2006-03-20 13:44:50 -0500 | [diff] [blame] | 396 | ClearPagePrivate(req->wb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index); |
| 398 | nfsi->npages--; |
| 399 | if (!nfsi->npages) { |
Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 400 | spin_unlock(&inode->i_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | iput(inode); |
| 402 | } else |
Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 403 | spin_unlock(&inode->i_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | nfs_clear_request(req); |
| 405 | nfs_release_request(req); |
| 406 | } |
| 407 | |
Trond Myklebust | 61822ab | 2006-12-05 00:35:42 -0500 | [diff] [blame] | 408 | static void |
Fred | 6d884e8 | 2008-03-19 11:24:38 -0400 | [diff] [blame] | 409 | nfs_mark_request_dirty(struct nfs_page *req) |
Trond Myklebust | 61822ab | 2006-12-05 00:35:42 -0500 | [diff] [blame] | 410 | { |
Trond Myklebust | 61822ab | 2006-12-05 00:35:42 -0500 | [diff] [blame] | 411 | __set_page_dirty_nobuffers(req->wb_page); |
| 412 | } |
| 413 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) |
| 415 | /* |
| 416 | * Add a request to the inode's commit list. |
| 417 | */ |
| 418 | static void |
| 419 | nfs_mark_request_commit(struct nfs_page *req) |
| 420 | { |
Trond Myklebust | 88be9f9 | 2007-06-05 10:42:27 -0400 | [diff] [blame] | 421 | struct inode *inode = req->wb_context->path.dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | struct nfs_inode *nfsi = NFS_I(inode); |
| 423 | |
Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 424 | spin_lock(&inode->i_lock); |
Trond Myklebust | e468bae | 2008-06-13 13:25:22 -0400 | [diff] [blame] | 425 | set_bit(PG_CLEAN, &(req)->wb_flags); |
Trond Myklebust | 5c36968 | 2007-06-17 15:27:42 -0400 | [diff] [blame] | 426 | radix_tree_tag_set(&nfsi->nfs_page_tree, |
| 427 | req->wb_index, |
| 428 | NFS_PAGE_TAG_COMMIT); |
Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 429 | spin_unlock(&inode->i_lock); |
Christoph Lameter | fd39fc8 | 2006-06-30 01:55:40 -0700 | [diff] [blame] | 430 | inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS); |
Peter Zijlstra | c9e51e4 | 2007-10-16 23:25:47 -0700 | [diff] [blame] | 431 | inc_bdi_stat(req->wb_page->mapping->backing_dev_info, BDI_RECLAIMABLE); |
Trond Myklebust | a180304 | 2006-12-05 00:45:19 -0500 | [diff] [blame] | 432 | __mark_inode_dirty(inode, I_DIRTY_DATASYNC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | } |
Trond Myklebust | 8e821ca | 2007-04-20 16:12:34 -0400 | [diff] [blame] | 434 | |
Trond Myklebust | e468bae | 2008-06-13 13:25:22 -0400 | [diff] [blame] | 435 | static int |
| 436 | nfs_clear_request_commit(struct nfs_page *req) |
| 437 | { |
| 438 | struct page *page = req->wb_page; |
| 439 | |
| 440 | if (test_and_clear_bit(PG_CLEAN, &(req)->wb_flags)) { |
| 441 | dec_zone_page_state(page, NR_UNSTABLE_NFS); |
| 442 | dec_bdi_stat(page->mapping->backing_dev_info, BDI_RECLAIMABLE); |
| 443 | return 1; |
| 444 | } |
| 445 | return 0; |
| 446 | } |
| 447 | |
Trond Myklebust | 8e821ca | 2007-04-20 16:12:34 -0400 | [diff] [blame] | 448 | static inline |
| 449 | int nfs_write_need_commit(struct nfs_write_data *data) |
| 450 | { |
| 451 | return data->verf.committed != NFS_FILE_SYNC; |
| 452 | } |
| 453 | |
| 454 | static inline |
| 455 | int nfs_reschedule_unstable_write(struct nfs_page *req) |
| 456 | { |
Trond Myklebust | e468bae | 2008-06-13 13:25:22 -0400 | [diff] [blame] | 457 | if (test_and_clear_bit(PG_NEED_COMMIT, &req->wb_flags)) { |
Trond Myklebust | 8e821ca | 2007-04-20 16:12:34 -0400 | [diff] [blame] | 458 | nfs_mark_request_commit(req); |
| 459 | return 1; |
| 460 | } |
| 461 | if (test_and_clear_bit(PG_NEED_RESCHED, &req->wb_flags)) { |
Fred | 6d884e8 | 2008-03-19 11:24:38 -0400 | [diff] [blame] | 462 | nfs_mark_request_dirty(req); |
Trond Myklebust | 8e821ca | 2007-04-20 16:12:34 -0400 | [diff] [blame] | 463 | return 1; |
| 464 | } |
| 465 | return 0; |
| 466 | } |
| 467 | #else |
| 468 | static inline void |
| 469 | nfs_mark_request_commit(struct nfs_page *req) |
| 470 | { |
| 471 | } |
| 472 | |
Trond Myklebust | e468bae | 2008-06-13 13:25:22 -0400 | [diff] [blame] | 473 | static inline int |
| 474 | nfs_clear_request_commit(struct nfs_page *req) |
| 475 | { |
| 476 | return 0; |
| 477 | } |
| 478 | |
Trond Myklebust | 8e821ca | 2007-04-20 16:12:34 -0400 | [diff] [blame] | 479 | static inline |
| 480 | int nfs_write_need_commit(struct nfs_write_data *data) |
| 481 | { |
| 482 | return 0; |
| 483 | } |
| 484 | |
| 485 | static inline |
| 486 | int nfs_reschedule_unstable_write(struct nfs_page *req) |
| 487 | { |
| 488 | return 0; |
| 489 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | #endif |
| 491 | |
| 492 | /* |
| 493 | * Wait for a request to complete. |
| 494 | * |
Matthew Wilcox | 150030b | 2007-12-06 16:24:39 -0500 | [diff] [blame] | 495 | * Interruptible by fatal signals only. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | */ |
Trond Myklebust | ca52fec | 2007-04-17 17:22:13 -0400 | [diff] [blame] | 497 | static int nfs_wait_on_requests_locked(struct inode *inode, pgoff_t idx_start, unsigned int npages) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | { |
| 499 | struct nfs_inode *nfsi = NFS_I(inode); |
| 500 | struct nfs_page *req; |
Trond Myklebust | ca52fec | 2007-04-17 17:22:13 -0400 | [diff] [blame] | 501 | pgoff_t idx_end, next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | unsigned int res = 0; |
| 503 | int error; |
| 504 | |
| 505 | if (npages == 0) |
| 506 | idx_end = ~0; |
| 507 | else |
| 508 | idx_end = idx_start + npages - 1; |
| 509 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | next = idx_start; |
Trond Myklebust | 9fd367f | 2007-06-17 15:10:24 -0400 | [diff] [blame] | 511 | while (radix_tree_gang_lookup_tag(&nfsi->nfs_page_tree, (void **)&req, next, 1, NFS_PAGE_TAG_LOCKED)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | if (req->wb_index > idx_end) |
| 513 | break; |
| 514 | |
| 515 | next = req->wb_index + 1; |
Trond Myklebust | c6a556b | 2005-06-22 17:16:30 +0000 | [diff] [blame] | 516 | BUG_ON(!NFS_WBACK_BUSY(req)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | |
Trond Myklebust | c03b402 | 2007-06-17 13:26:38 -0400 | [diff] [blame] | 518 | kref_get(&req->wb_kref); |
Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 519 | spin_unlock(&inode->i_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | error = nfs_wait_on_request(req); |
| 521 | nfs_release_request(req); |
Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 522 | spin_lock(&inode->i_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | if (error < 0) |
| 524 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | res++; |
| 526 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | return res; |
| 528 | } |
| 529 | |
Trond Myklebust | 83715ad | 2006-07-05 13:17:12 -0400 | [diff] [blame] | 530 | static void nfs_cancel_commit_list(struct list_head *head) |
| 531 | { |
| 532 | struct nfs_page *req; |
| 533 | |
| 534 | while(!list_empty(head)) { |
| 535 | req = nfs_list_entry(head->next); |
| 536 | nfs_list_remove_request(req); |
Trond Myklebust | e468bae | 2008-06-13 13:25:22 -0400 | [diff] [blame] | 537 | nfs_clear_request_commit(req); |
Trond Myklebust | 83715ad | 2006-07-05 13:17:12 -0400 | [diff] [blame] | 538 | nfs_inode_remove_request(req); |
Trond Myklebust | b6dff26 | 2006-10-19 23:28:38 -0700 | [diff] [blame] | 539 | nfs_unlock_request(req); |
Trond Myklebust | 83715ad | 2006-07-05 13:17:12 -0400 | [diff] [blame] | 540 | } |
| 541 | } |
| 542 | |
Trond Myklebust | 47c6256 | 2009-03-16 08:13:41 -0400 | [diff] [blame] | 543 | #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) |
Trond Myklebust | fb8a1f1 | 2009-03-11 14:10:29 -0400 | [diff] [blame] | 544 | static int |
| 545 | nfs_need_commit(struct nfs_inode *nfsi) |
| 546 | { |
| 547 | return radix_tree_tagged(&nfsi->nfs_page_tree, NFS_PAGE_TAG_COMMIT); |
| 548 | } |
| 549 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | /* |
| 551 | * nfs_scan_commit - Scan an inode for commit requests |
| 552 | * @inode: NFS inode to scan |
| 553 | * @dst: destination list |
| 554 | * @idx_start: lower bound of page->index to scan. |
| 555 | * @npages: idx_start + npages sets the upper bound to scan. |
| 556 | * |
| 557 | * Moves requests from the inode's 'commit' request list. |
| 558 | * The requests are *not* checked to ensure that they form a contiguous set. |
| 559 | */ |
| 560 | static int |
Trond Myklebust | ca52fec | 2007-04-17 17:22:13 -0400 | [diff] [blame] | 561 | nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | { |
| 563 | struct nfs_inode *nfsi = NFS_I(inode); |
Trond Myklebust | 3da28eb | 2005-06-22 17:16:31 +0000 | [diff] [blame] | 564 | |
Trond Myklebust | fb8a1f1 | 2009-03-11 14:10:29 -0400 | [diff] [blame] | 565 | if (!nfs_need_commit(nfsi)) |
| 566 | return 0; |
| 567 | |
| 568 | return nfs_scan_list(nfsi, dst, idx_start, npages, NFS_PAGE_TAG_COMMIT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | } |
Trond Myklebust | c42de9d | 2006-03-20 13:44:51 -0500 | [diff] [blame] | 570 | #else |
Trond Myklebust | fb8a1f1 | 2009-03-11 14:10:29 -0400 | [diff] [blame] | 571 | static inline int nfs_need_commit(struct nfs_inode *nfsi) |
| 572 | { |
| 573 | return 0; |
| 574 | } |
| 575 | |
Trond Myklebust | ca52fec | 2007-04-17 17:22:13 -0400 | [diff] [blame] | 576 | static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages) |
Trond Myklebust | c42de9d | 2006-03-20 13:44:51 -0500 | [diff] [blame] | 577 | { |
| 578 | return 0; |
| 579 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 580 | #endif |
| 581 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | /* |
Trond Myklebust | e7d3906 | 2008-06-13 12:12:32 -0400 | [diff] [blame] | 583 | * Search for an existing write request, and attempt to update |
| 584 | * it to reflect a new dirty region on a given page. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | * |
Trond Myklebust | e7d3906 | 2008-06-13 12:12:32 -0400 | [diff] [blame] | 586 | * If the attempt fails, then the existing request is flushed out |
| 587 | * to disk. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | */ |
Trond Myklebust | e7d3906 | 2008-06-13 12:12:32 -0400 | [diff] [blame] | 589 | static struct nfs_page *nfs_try_to_update_request(struct inode *inode, |
| 590 | struct page *page, |
| 591 | unsigned int offset, |
| 592 | unsigned int bytes) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | { |
Trond Myklebust | e7d3906 | 2008-06-13 12:12:32 -0400 | [diff] [blame] | 594 | struct nfs_page *req; |
| 595 | unsigned int rqend; |
| 596 | unsigned int end; |
| 597 | int error; |
| 598 | |
| 599 | if (!PagePrivate(page)) |
| 600 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | |
| 602 | end = offset + bytes; |
Trond Myklebust | e7d3906 | 2008-06-13 12:12:32 -0400 | [diff] [blame] | 603 | spin_lock(&inode->i_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | for (;;) { |
Trond Myklebust | 277459d | 2006-12-05 00:35:35 -0500 | [diff] [blame] | 606 | req = nfs_page_find_request_locked(page); |
Trond Myklebust | e7d3906 | 2008-06-13 12:12:32 -0400 | [diff] [blame] | 607 | if (req == NULL) |
| 608 | goto out_unlock; |
Trond Myklebust | 277459d | 2006-12-05 00:35:35 -0500 | [diff] [blame] | 609 | |
Trond Myklebust | e7d3906 | 2008-06-13 12:12:32 -0400 | [diff] [blame] | 610 | rqend = req->wb_offset + req->wb_bytes; |
| 611 | /* |
| 612 | * Tell the caller to flush out the request if |
| 613 | * the offsets are non-contiguous. |
| 614 | * Note: nfs_flush_incompatible() will already |
| 615 | * have flushed out requests having wrong owners. |
| 616 | */ |
Trond Myklebust | e468bae | 2008-06-13 13:25:22 -0400 | [diff] [blame] | 617 | if (offset > rqend |
Trond Myklebust | e7d3906 | 2008-06-13 12:12:32 -0400 | [diff] [blame] | 618 | || end < req->wb_offset) |
| 619 | goto out_flushme; |
| 620 | |
| 621 | if (nfs_set_page_tag_locked(req)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | |
Trond Myklebust | e7d3906 | 2008-06-13 12:12:32 -0400 | [diff] [blame] | 624 | /* The request is locked, so wait and then retry */ |
Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 625 | spin_unlock(&inode->i_lock); |
Trond Myklebust | e7d3906 | 2008-06-13 12:12:32 -0400 | [diff] [blame] | 626 | error = nfs_wait_on_request(req); |
| 627 | nfs_release_request(req); |
| 628 | if (error != 0) |
| 629 | goto out_err; |
| 630 | spin_lock(&inode->i_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | } |
| 632 | |
Trond Myklebust | e468bae | 2008-06-13 13:25:22 -0400 | [diff] [blame] | 633 | if (nfs_clear_request_commit(req)) |
| 634 | radix_tree_tag_clear(&NFS_I(inode)->nfs_page_tree, |
| 635 | req->wb_index, NFS_PAGE_TAG_COMMIT); |
| 636 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | /* Okay, the request matches. Update the region */ |
| 638 | if (offset < req->wb_offset) { |
| 639 | req->wb_offset = offset; |
| 640 | req->wb_pgbase = offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | if (end > rqend) |
| 643 | req->wb_bytes = end - req->wb_offset; |
Trond Myklebust | e7d3906 | 2008-06-13 12:12:32 -0400 | [diff] [blame] | 644 | else |
| 645 | req->wb_bytes = rqend - req->wb_offset; |
| 646 | out_unlock: |
| 647 | spin_unlock(&inode->i_lock); |
| 648 | return req; |
| 649 | out_flushme: |
| 650 | spin_unlock(&inode->i_lock); |
| 651 | nfs_release_request(req); |
| 652 | error = nfs_wb_page(inode, page); |
| 653 | out_err: |
| 654 | return ERR_PTR(error); |
| 655 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | |
Trond Myklebust | e7d3906 | 2008-06-13 12:12:32 -0400 | [diff] [blame] | 657 | /* |
| 658 | * Try to update an existing write request, or create one if there is none. |
| 659 | * |
| 660 | * Note: Should always be called with the Page Lock held to prevent races |
| 661 | * if we have to add a new request. Also assumes that the caller has |
| 662 | * already called nfs_flush_incompatible() if necessary. |
| 663 | */ |
| 664 | static struct nfs_page * nfs_setup_write_request(struct nfs_open_context* ctx, |
| 665 | struct page *page, unsigned int offset, unsigned int bytes) |
| 666 | { |
| 667 | struct inode *inode = page->mapping->host; |
| 668 | struct nfs_page *req; |
| 669 | int error; |
| 670 | |
| 671 | req = nfs_try_to_update_request(inode, page, offset, bytes); |
| 672 | if (req != NULL) |
| 673 | goto out; |
| 674 | req = nfs_create_request(ctx, inode, page, offset, bytes); |
| 675 | if (IS_ERR(req)) |
| 676 | goto out; |
| 677 | error = nfs_inode_add_request(inode, req); |
| 678 | if (error != 0) { |
| 679 | nfs_release_request(req); |
| 680 | req = ERR_PTR(error); |
| 681 | } |
Trond Myklebust | efc91ed | 2008-06-10 18:31:00 -0400 | [diff] [blame] | 682 | out: |
Trond Myklebust | 61e930a | 2007-10-18 17:08:05 -0400 | [diff] [blame] | 683 | return req; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | } |
| 685 | |
Trond Myklebust | e7d3906 | 2008-06-13 12:12:32 -0400 | [diff] [blame] | 686 | static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page, |
| 687 | unsigned int offset, unsigned int count) |
| 688 | { |
| 689 | struct nfs_page *req; |
| 690 | |
| 691 | req = nfs_setup_write_request(ctx, page, offset, count); |
| 692 | if (IS_ERR(req)) |
| 693 | return PTR_ERR(req); |
| 694 | /* Update file length */ |
| 695 | nfs_grow_file(page, offset, count); |
| 696 | nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes); |
| 697 | nfs_clear_page_tag_locked(req); |
| 698 | return 0; |
| 699 | } |
| 700 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | int nfs_flush_incompatible(struct file *file, struct page *page) |
| 702 | { |
Trond Myklebust | cd3758e | 2007-08-10 17:44:32 -0400 | [diff] [blame] | 703 | struct nfs_open_context *ctx = nfs_file_open_context(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | struct nfs_page *req; |
Trond Myklebust | 1a54533 | 2006-12-05 00:35:40 -0500 | [diff] [blame] | 705 | int do_flush, status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | /* |
| 707 | * Look for a request corresponding to this page. If there |
| 708 | * is one, and it belongs to another file, we flush it out |
| 709 | * before we try to copy anything into the page. Do this |
| 710 | * due to the lack of an ACCESS-type call in NFSv2. |
| 711 | * Also do the same if we find a request from an existing |
| 712 | * dropped page. |
| 713 | */ |
Trond Myklebust | 1a54533 | 2006-12-05 00:35:40 -0500 | [diff] [blame] | 714 | do { |
| 715 | req = nfs_page_find_request(page); |
| 716 | if (req == NULL) |
| 717 | return 0; |
Trond Myklebust | e468bae | 2008-06-13 13:25:22 -0400 | [diff] [blame] | 718 | do_flush = req->wb_page != page || req->wb_context != ctx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | nfs_release_request(req); |
Trond Myklebust | 1a54533 | 2006-12-05 00:35:40 -0500 | [diff] [blame] | 720 | if (!do_flush) |
| 721 | return 0; |
| 722 | status = nfs_wb_page(page->mapping->host, page); |
| 723 | } while (status == 0); |
| 724 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | /* |
Trond Myklebust | 5d47a35 | 2008-02-07 17:24:07 -0500 | [diff] [blame] | 728 | * If the page cache is marked as unsafe or invalid, then we can't rely on |
| 729 | * the PageUptodate() flag. In this case, we will need to turn off |
| 730 | * write optimisations that depend on the page contents being correct. |
| 731 | */ |
| 732 | static int nfs_write_pageuptodate(struct page *page, struct inode *inode) |
| 733 | { |
| 734 | return PageUptodate(page) && |
| 735 | !(NFS_I(inode)->cache_validity & (NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA)); |
| 736 | } |
| 737 | |
| 738 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | * Update and possibly write a cached page of an NFS file. |
| 740 | * |
| 741 | * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad |
| 742 | * things with a page scheduled for an RPC call (e.g. invalidate it). |
| 743 | */ |
| 744 | int nfs_updatepage(struct file *file, struct page *page, |
| 745 | unsigned int offset, unsigned int count) |
| 746 | { |
Trond Myklebust | cd3758e | 2007-08-10 17:44:32 -0400 | [diff] [blame] | 747 | struct nfs_open_context *ctx = nfs_file_open_context(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | struct inode *inode = page->mapping->host; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | int status = 0; |
| 750 | |
Chuck Lever | 91d5b47 | 2006-03-20 13:44:14 -0500 | [diff] [blame] | 751 | nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE); |
| 752 | |
Chuck Lever | 48186c7 | 2008-06-11 17:56:05 -0400 | [diff] [blame] | 753 | dprintk("NFS: nfs_updatepage(%s/%s %d@%lld)\n", |
Josef "Jeff" Sipek | 01cce93 | 2006-12-08 02:36:40 -0800 | [diff] [blame] | 754 | file->f_path.dentry->d_parent->d_name.name, |
| 755 | file->f_path.dentry->d_name.name, count, |
Chuck Lever | 48186c7 | 2008-06-11 17:56:05 -0400 | [diff] [blame] | 756 | (long long)(page_offset(page) + offset)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | /* If we're not using byte range locks, and we know the page |
Trond Myklebust | 5d47a35 | 2008-02-07 17:24:07 -0500 | [diff] [blame] | 759 | * is up to date, it may be more efficient to extend the write |
| 760 | * to cover the entire page in order to avoid fragmentation |
| 761 | * inefficiencies. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | */ |
Trond Myklebust | 5d47a35 | 2008-02-07 17:24:07 -0500 | [diff] [blame] | 763 | if (nfs_write_pageuptodate(page, inode) && |
| 764 | inode->i_flock == NULL && |
Trond Myklebust | 4b5621f | 2008-02-25 15:56:29 -0800 | [diff] [blame] | 765 | !(file->f_flags & O_SYNC)) { |
Trond Myklebust | 49a70f2 | 2006-12-05 00:35:38 -0500 | [diff] [blame] | 766 | count = max(count + offset, nfs_page_length(page)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | offset = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | } |
| 769 | |
Trond Myklebust | e21195a | 2006-12-05 00:35:39 -0500 | [diff] [blame] | 770 | status = nfs_writepage_setup(ctx, page, offset, count); |
Trond Myklebust | 03fa9e8 | 2008-06-05 16:02:35 -0400 | [diff] [blame] | 771 | if (status < 0) |
| 772 | nfs_set_pageerror(page); |
| 773 | else |
| 774 | __set_page_dirty_nobuffers(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | |
Chuck Lever | 48186c7 | 2008-06-11 17:56:05 -0400 | [diff] [blame] | 776 | dprintk("NFS: nfs_updatepage returns %d (isize %lld)\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | status, (long long)i_size_read(inode)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | return status; |
| 779 | } |
| 780 | |
| 781 | static void nfs_writepage_release(struct nfs_page *req) |
| 782 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | |
Trond Myklebust | 7e5f614 | 2008-05-25 12:59:19 -0400 | [diff] [blame] | 784 | if (PageError(req->wb_page) || !nfs_reschedule_unstable_write(req)) { |
Trond Myklebust | 8e821ca | 2007-04-20 16:12:34 -0400 | [diff] [blame] | 785 | nfs_end_page_writeback(req->wb_page); |
| 786 | nfs_inode_remove_request(req); |
| 787 | } else |
| 788 | nfs_end_page_writeback(req->wb_page); |
Trond Myklebust | 9fd367f | 2007-06-17 15:10:24 -0400 | [diff] [blame] | 789 | nfs_clear_page_tag_locked(req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | } |
| 791 | |
Trond Myklebust | 3ff7576 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 792 | static int flush_task_priority(int how) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | { |
| 794 | switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) { |
| 795 | case FLUSH_HIGHPRI: |
| 796 | return RPC_PRIORITY_HIGH; |
| 797 | case FLUSH_LOWPRI: |
| 798 | return RPC_PRIORITY_LOW; |
| 799 | } |
| 800 | return RPC_PRIORITY_NORMAL; |
| 801 | } |
| 802 | |
| 803 | /* |
| 804 | * Set up the argument/result storage required for the RPC call. |
| 805 | */ |
Trond Myklebust | dbae4c7 | 2008-04-14 14:54:53 -0400 | [diff] [blame] | 806 | static int nfs_write_rpcsetup(struct nfs_page *req, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | struct nfs_write_data *data, |
Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 808 | const struct rpc_call_ops *call_ops, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | unsigned int count, unsigned int offset, |
| 810 | int how) |
| 811 | { |
Trond Myklebust | 84115e1 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 812 | struct inode *inode = req->wb_context->path.dentry->d_inode; |
| 813 | int flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC; |
Trond Myklebust | 3ff7576 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 814 | int priority = flush_task_priority(how); |
Trond Myklebust | 0773769 | 2007-10-25 18:42:54 -0400 | [diff] [blame] | 815 | struct rpc_task *task; |
Trond Myklebust | bdc7f02 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 816 | struct rpc_message msg = { |
| 817 | .rpc_argp = &data->args, |
| 818 | .rpc_resp = &data->res, |
| 819 | .rpc_cred = req->wb_context->cred, |
| 820 | }; |
Trond Myklebust | 84115e1 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 821 | struct rpc_task_setup task_setup_data = { |
| 822 | .rpc_client = NFS_CLIENT(inode), |
Trond Myklebust | 0773769 | 2007-10-25 18:42:54 -0400 | [diff] [blame] | 823 | .task = &data->task, |
Trond Myklebust | bdc7f02 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 824 | .rpc_message = &msg, |
Trond Myklebust | 84115e1 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 825 | .callback_ops = call_ops, |
| 826 | .callback_data = data, |
Trond Myklebust | 101070c | 2008-02-19 20:04:23 -0500 | [diff] [blame] | 827 | .workqueue = nfsiod_workqueue, |
Trond Myklebust | 84115e1 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 828 | .flags = flags, |
Trond Myklebust | 3ff7576 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 829 | .priority = priority, |
Trond Myklebust | 84115e1 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 830 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | |
| 832 | /* Set up the RPC argument and reply structs |
| 833 | * NB: take care not to mess about with data->commit et al. */ |
| 834 | |
| 835 | data->req = req; |
Trond Myklebust | 88be9f9 | 2007-06-05 10:42:27 -0400 | [diff] [blame] | 836 | data->inode = inode = req->wb_context->path.dentry->d_inode; |
Trond Myklebust | bdc7f02 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 837 | data->cred = msg.rpc_cred; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 838 | |
| 839 | data->args.fh = NFS_FH(inode); |
| 840 | data->args.offset = req_offset(req) + offset; |
| 841 | data->args.pgbase = req->wb_pgbase + offset; |
| 842 | data->args.pages = data->pagevec; |
| 843 | data->args.count = count; |
Trond Myklebust | 383ba71 | 2008-02-19 20:04:20 -0500 | [diff] [blame] | 844 | data->args.context = get_nfs_open_context(req->wb_context); |
Trond Myklebust | bdc7f02 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 845 | data->args.stable = NFS_UNSTABLE; |
| 846 | if (how & FLUSH_STABLE) { |
| 847 | data->args.stable = NFS_DATA_SYNC; |
Trond Myklebust | fb8a1f1 | 2009-03-11 14:10:29 -0400 | [diff] [blame] | 848 | if (!nfs_need_commit(NFS_I(inode))) |
Trond Myklebust | bdc7f02 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 849 | data->args.stable = NFS_FILE_SYNC; |
| 850 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 851 | |
| 852 | data->res.fattr = &data->fattr; |
| 853 | data->res.count = count; |
| 854 | data->res.verf = &data->verf; |
Trond Myklebust | 0e574af | 2005-10-27 22:12:38 -0400 | [diff] [blame] | 855 | nfs_fattr_init(&data->fattr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | |
Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 857 | /* Set up the initial task struct. */ |
Trond Myklebust | bdc7f02 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 858 | NFS_PROTO(inode)->write_setup(data, &msg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 859 | |
Chuck Lever | a3f565b | 2007-01-31 12:14:01 -0500 | [diff] [blame] | 860 | dprintk("NFS: %5u initiated write call " |
Chuck Lever | 48186c7 | 2008-06-11 17:56:05 -0400 | [diff] [blame] | 861 | "(req %s/%lld, %u bytes @ offset %llu)\n", |
Chuck Lever | 0bbacc4 | 2005-11-01 16:53:32 -0500 | [diff] [blame] | 862 | data->task.tk_pid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 | inode->i_sb->s_id, |
| 864 | (long long)NFS_FILEID(inode), |
| 865 | count, |
| 866 | (unsigned long long)data->args.offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 | |
Trond Myklebust | 0773769 | 2007-10-25 18:42:54 -0400 | [diff] [blame] | 868 | task = rpc_run_task(&task_setup_data); |
Trond Myklebust | dbae4c7 | 2008-04-14 14:54:53 -0400 | [diff] [blame] | 869 | if (IS_ERR(task)) |
| 870 | return PTR_ERR(task); |
| 871 | rpc_put_task(task); |
| 872 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | } |
| 874 | |
Fred | 6d884e8 | 2008-03-19 11:24:38 -0400 | [diff] [blame] | 875 | /* If a nfs_flush_* function fails, it should remove reqs from @head and |
| 876 | * call this on each, which will prepare them to be retried on next |
| 877 | * writeback using standard nfs. |
| 878 | */ |
| 879 | static void nfs_redirty_request(struct nfs_page *req) |
| 880 | { |
| 881 | nfs_mark_request_dirty(req); |
| 882 | nfs_end_page_writeback(req->wb_page); |
| 883 | nfs_clear_page_tag_locked(req); |
| 884 | } |
| 885 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | /* |
| 887 | * Generate multiple small requests to write out a single |
| 888 | * contiguous dirty area on one page. |
| 889 | */ |
Trond Myklebust | 8d5658c | 2007-04-10 09:26:35 -0400 | [diff] [blame] | 890 | static int nfs_flush_multi(struct inode *inode, struct list_head *head, unsigned int npages, size_t count, int how) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | { |
| 892 | struct nfs_page *req = nfs_list_entry(head->next); |
| 893 | struct page *page = req->wb_page; |
| 894 | struct nfs_write_data *data; |
Trond Myklebust | e9f7bee | 2006-09-08 09:48:54 -0700 | [diff] [blame] | 895 | size_t wsize = NFS_SERVER(inode)->wsize, nbytes; |
| 896 | unsigned int offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 897 | int requests = 0; |
Trond Myklebust | dbae4c7 | 2008-04-14 14:54:53 -0400 | [diff] [blame] | 898 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | LIST_HEAD(list); |
| 900 | |
| 901 | nfs_list_remove_request(req); |
| 902 | |
Trond Myklebust | bcb71bb | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 903 | nbytes = count; |
Trond Myklebust | e9f7bee | 2006-09-08 09:48:54 -0700 | [diff] [blame] | 904 | do { |
| 905 | size_t len = min(nbytes, wsize); |
| 906 | |
Trond Myklebust | 8d5658c | 2007-04-10 09:26:35 -0400 | [diff] [blame] | 907 | data = nfs_writedata_alloc(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 908 | if (!data) |
| 909 | goto out_bad; |
| 910 | list_add(&data->pages, &list); |
| 911 | requests++; |
Trond Myklebust | e9f7bee | 2006-09-08 09:48:54 -0700 | [diff] [blame] | 912 | nbytes -= len; |
| 913 | } while (nbytes != 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | atomic_set(&req->wb_complete, requests); |
| 915 | |
| 916 | ClearPageError(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | offset = 0; |
Trond Myklebust | bcb71bb | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 918 | nbytes = count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | do { |
Trond Myklebust | dbae4c7 | 2008-04-14 14:54:53 -0400 | [diff] [blame] | 920 | int ret2; |
| 921 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | data = list_entry(list.next, struct nfs_write_data, pages); |
| 923 | list_del_init(&data->pages); |
| 924 | |
| 925 | data->pagevec[0] = page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | |
Trond Myklebust | bcb71bb | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 927 | if (nbytes < wsize) |
| 928 | wsize = nbytes; |
Trond Myklebust | dbae4c7 | 2008-04-14 14:54:53 -0400 | [diff] [blame] | 929 | ret2 = nfs_write_rpcsetup(req, data, &nfs_write_partial_ops, |
Trond Myklebust | bcb71bb | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 930 | wsize, offset, how); |
Trond Myklebust | dbae4c7 | 2008-04-14 14:54:53 -0400 | [diff] [blame] | 931 | if (ret == 0) |
| 932 | ret = ret2; |
Trond Myklebust | bcb71bb | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 933 | offset += wsize; |
| 934 | nbytes -= wsize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 935 | } while (nbytes != 0); |
| 936 | |
Trond Myklebust | dbae4c7 | 2008-04-14 14:54:53 -0400 | [diff] [blame] | 937 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | |
| 939 | out_bad: |
| 940 | while (!list_empty(&list)) { |
| 941 | data = list_entry(list.next, struct nfs_write_data, pages); |
| 942 | list_del(&data->pages); |
Trond Myklebust | 8aca67f | 2006-11-13 16:23:44 -0500 | [diff] [blame] | 943 | nfs_writedata_release(data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | } |
Trond Myklebust | 61822ab | 2006-12-05 00:35:42 -0500 | [diff] [blame] | 945 | nfs_redirty_request(req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | return -ENOMEM; |
| 947 | } |
| 948 | |
| 949 | /* |
| 950 | * Create an RPC task for the given write request and kick it. |
| 951 | * The page must have been locked by the caller. |
| 952 | * |
| 953 | * It may happen that the page we're passed is not marked dirty. |
| 954 | * This is the case if nfs_updatepage detects a conflicting request |
| 955 | * that has been written but not committed. |
| 956 | */ |
Trond Myklebust | 8d5658c | 2007-04-10 09:26:35 -0400 | [diff] [blame] | 957 | static int nfs_flush_one(struct inode *inode, struct list_head *head, unsigned int npages, size_t count, int how) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | { |
| 959 | struct nfs_page *req; |
| 960 | struct page **pages; |
| 961 | struct nfs_write_data *data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 962 | |
Trond Myklebust | 8d5658c | 2007-04-10 09:26:35 -0400 | [diff] [blame] | 963 | data = nfs_writedata_alloc(npages); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | if (!data) |
| 965 | goto out_bad; |
| 966 | |
| 967 | pages = data->pagevec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 968 | while (!list_empty(head)) { |
| 969 | req = nfs_list_entry(head->next); |
| 970 | nfs_list_remove_request(req); |
| 971 | nfs_list_add_request(req, &data->pages); |
| 972 | ClearPageError(req->wb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 973 | *pages++ = req->wb_page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 974 | } |
| 975 | req = nfs_list_entry(data->pages.next); |
| 976 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 977 | /* Set up the argument struct */ |
Trond Myklebust | dbae4c7 | 2008-04-14 14:54:53 -0400 | [diff] [blame] | 978 | return nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 979 | out_bad: |
| 980 | while (!list_empty(head)) { |
Trond Myklebust | 10afec9 | 2007-05-14 17:16:04 -0400 | [diff] [blame] | 981 | req = nfs_list_entry(head->next); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 982 | nfs_list_remove_request(req); |
Trond Myklebust | 61822ab | 2006-12-05 00:35:42 -0500 | [diff] [blame] | 983 | nfs_redirty_request(req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 984 | } |
| 985 | return -ENOMEM; |
| 986 | } |
| 987 | |
Trond Myklebust | c63c7b0 | 2007-04-02 19:29:52 -0400 | [diff] [blame] | 988 | static void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio, |
| 989 | struct inode *inode, int ioflags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | { |
Chuck Lever | bf4285e | 2007-12-20 14:54:57 -0500 | [diff] [blame] | 991 | size_t wsize = NFS_SERVER(inode)->wsize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 992 | |
Trond Myklebust | bcb71bb | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 993 | if (wsize < PAGE_CACHE_SIZE) |
Trond Myklebust | c63c7b0 | 2007-04-02 19:29:52 -0400 | [diff] [blame] | 994 | nfs_pageio_init(pgio, inode, nfs_flush_multi, wsize, ioflags); |
Trond Myklebust | bcb71bb | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 995 | else |
Trond Myklebust | c63c7b0 | 2007-04-02 19:29:52 -0400 | [diff] [blame] | 996 | nfs_pageio_init(pgio, inode, nfs_flush_one, wsize, ioflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 997 | } |
| 998 | |
| 999 | /* |
| 1000 | * Handle a write reply that flushed part of a page. |
| 1001 | */ |
Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1002 | static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1003 | { |
Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1004 | struct nfs_write_data *data = calldata; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1005 | |
Chuck Lever | 48186c7 | 2008-06-11 17:56:05 -0400 | [diff] [blame] | 1006 | dprintk("NFS: %5u write(%s/%lld %d@%lld)", |
| 1007 | task->tk_pid, |
| 1008 | data->req->wb_context->path.dentry->d_inode->i_sb->s_id, |
| 1009 | (long long) |
| 1010 | NFS_FILEID(data->req->wb_context->path.dentry->d_inode), |
| 1011 | data->req->wb_bytes, (long long)req_offset(data->req)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1012 | |
Trond Myklebust | c9d8f89 | 2008-04-15 16:56:39 -0400 | [diff] [blame] | 1013 | nfs_writeback_done(task, data); |
| 1014 | } |
Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1015 | |
Trond Myklebust | c9d8f89 | 2008-04-15 16:56:39 -0400 | [diff] [blame] | 1016 | static void nfs_writeback_release_partial(void *calldata) |
| 1017 | { |
| 1018 | struct nfs_write_data *data = calldata; |
| 1019 | struct nfs_page *req = data->req; |
| 1020 | struct page *page = req->wb_page; |
| 1021 | int status = data->task.tk_status; |
| 1022 | |
| 1023 | if (status < 0) { |
Trond Myklebust | a301b77 | 2007-02-06 11:07:15 -0800 | [diff] [blame] | 1024 | nfs_set_pageerror(page); |
Trond Myklebust | c9d8f89 | 2008-04-15 16:56:39 -0400 | [diff] [blame] | 1025 | nfs_context_set_write_error(req->wb_context, status); |
| 1026 | dprintk(", error = %d\n", status); |
Trond Myklebust | 8e821ca | 2007-04-20 16:12:34 -0400 | [diff] [blame] | 1027 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | } |
| 1029 | |
Trond Myklebust | 8e821ca | 2007-04-20 16:12:34 -0400 | [diff] [blame] | 1030 | if (nfs_write_need_commit(data)) { |
Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 1031 | struct inode *inode = page->mapping->host; |
Trond Myklebust | 8e821ca | 2007-04-20 16:12:34 -0400 | [diff] [blame] | 1032 | |
Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 1033 | spin_lock(&inode->i_lock); |
Trond Myklebust | 8e821ca | 2007-04-20 16:12:34 -0400 | [diff] [blame] | 1034 | if (test_bit(PG_NEED_RESCHED, &req->wb_flags)) { |
| 1035 | /* Do nothing we need to resend the writes */ |
| 1036 | } else if (!test_and_set_bit(PG_NEED_COMMIT, &req->wb_flags)) { |
| 1037 | memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf)); |
| 1038 | dprintk(" defer commit\n"); |
| 1039 | } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) { |
| 1040 | set_bit(PG_NEED_RESCHED, &req->wb_flags); |
| 1041 | clear_bit(PG_NEED_COMMIT, &req->wb_flags); |
| 1042 | dprintk(" server reboot detected\n"); |
| 1043 | } |
Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 1044 | spin_unlock(&inode->i_lock); |
Trond Myklebust | 8e821ca | 2007-04-20 16:12:34 -0400 | [diff] [blame] | 1045 | } else |
| 1046 | dprintk(" OK\n"); |
| 1047 | |
| 1048 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1049 | if (atomic_dec_and_test(&req->wb_complete)) |
| 1050 | nfs_writepage_release(req); |
Trond Myklebust | c9d8f89 | 2008-04-15 16:56:39 -0400 | [diff] [blame] | 1051 | nfs_writedata_release(calldata); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1052 | } |
| 1053 | |
Andy Adamson | def6ed7 | 2009-04-01 09:22:26 -0400 | [diff] [blame] | 1054 | #if defined(CONFIG_NFS_V4_1) |
| 1055 | void nfs_write_prepare(struct rpc_task *task, void *calldata) |
| 1056 | { |
| 1057 | struct nfs_write_data *data = calldata; |
| 1058 | struct nfs_client *clp = (NFS_SERVER(data->inode))->nfs_client; |
| 1059 | |
| 1060 | if (nfs4_setup_sequence(clp, &data->args.seq_args, |
| 1061 | &data->res.seq_res, 1, task)) |
| 1062 | return; |
| 1063 | rpc_call_start(task); |
| 1064 | } |
| 1065 | #endif /* CONFIG_NFS_V4_1 */ |
| 1066 | |
Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1067 | static const struct rpc_call_ops nfs_write_partial_ops = { |
Andy Adamson | def6ed7 | 2009-04-01 09:22:26 -0400 | [diff] [blame] | 1068 | #if defined(CONFIG_NFS_V4_1) |
| 1069 | .rpc_call_prepare = nfs_write_prepare, |
| 1070 | #endif /* CONFIG_NFS_V4_1 */ |
Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1071 | .rpc_call_done = nfs_writeback_done_partial, |
Trond Myklebust | c9d8f89 | 2008-04-15 16:56:39 -0400 | [diff] [blame] | 1072 | .rpc_release = nfs_writeback_release_partial, |
Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1073 | }; |
| 1074 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1075 | /* |
| 1076 | * Handle a write reply that flushes a whole page. |
| 1077 | * |
| 1078 | * FIXME: There is an inherent race with invalidate_inode_pages and |
| 1079 | * writebacks since the page->count is kept > 1 for as long |
| 1080 | * as the page has a write request pending. |
| 1081 | */ |
Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1082 | static void nfs_writeback_done_full(struct rpc_task *task, void *calldata) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1083 | { |
Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1084 | struct nfs_write_data *data = calldata; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1085 | |
Trond Myklebust | c9d8f89 | 2008-04-15 16:56:39 -0400 | [diff] [blame] | 1086 | nfs_writeback_done(task, data); |
| 1087 | } |
| 1088 | |
| 1089 | static void nfs_writeback_release_full(void *calldata) |
| 1090 | { |
| 1091 | struct nfs_write_data *data = calldata; |
| 1092 | int status = data->task.tk_status; |
Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1093 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1094 | /* Update attributes as result of writeback. */ |
| 1095 | while (!list_empty(&data->pages)) { |
Trond Myklebust | c9d8f89 | 2008-04-15 16:56:39 -0400 | [diff] [blame] | 1096 | struct nfs_page *req = nfs_list_entry(data->pages.next); |
| 1097 | struct page *page = req->wb_page; |
| 1098 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1099 | nfs_list_remove_request(req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1100 | |
Chuck Lever | 48186c7 | 2008-06-11 17:56:05 -0400 | [diff] [blame] | 1101 | dprintk("NFS: %5u write (%s/%lld %d@%lld)", |
| 1102 | data->task.tk_pid, |
Trond Myklebust | 88be9f9 | 2007-06-05 10:42:27 -0400 | [diff] [blame] | 1103 | req->wb_context->path.dentry->d_inode->i_sb->s_id, |
| 1104 | (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1105 | req->wb_bytes, |
| 1106 | (long long)req_offset(req)); |
| 1107 | |
Trond Myklebust | c9d8f89 | 2008-04-15 16:56:39 -0400 | [diff] [blame] | 1108 | if (status < 0) { |
Trond Myklebust | a301b77 | 2007-02-06 11:07:15 -0800 | [diff] [blame] | 1109 | nfs_set_pageerror(page); |
Trond Myklebust | c9d8f89 | 2008-04-15 16:56:39 -0400 | [diff] [blame] | 1110 | nfs_context_set_write_error(req->wb_context, status); |
| 1111 | dprintk(", error = %d\n", status); |
Trond Myklebust | 8e821ca | 2007-04-20 16:12:34 -0400 | [diff] [blame] | 1112 | goto remove_request; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1113 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1114 | |
Trond Myklebust | 8e821ca | 2007-04-20 16:12:34 -0400 | [diff] [blame] | 1115 | if (nfs_write_need_commit(data)) { |
| 1116 | memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf)); |
| 1117 | nfs_mark_request_commit(req); |
| 1118 | nfs_end_page_writeback(page); |
| 1119 | dprintk(" marked for commit\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 | goto next; |
| 1121 | } |
Trond Myklebust | 8e821ca | 2007-04-20 16:12:34 -0400 | [diff] [blame] | 1122 | dprintk(" OK\n"); |
| 1123 | remove_request: |
| 1124 | nfs_end_page_writeback(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1125 | nfs_inode_remove_request(req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1126 | next: |
Trond Myklebust | 9fd367f | 2007-06-17 15:10:24 -0400 | [diff] [blame] | 1127 | nfs_clear_page_tag_locked(req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1128 | } |
Trond Myklebust | c9d8f89 | 2008-04-15 16:56:39 -0400 | [diff] [blame] | 1129 | nfs_writedata_release(calldata); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1130 | } |
| 1131 | |
Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1132 | static const struct rpc_call_ops nfs_write_full_ops = { |
Andy Adamson | def6ed7 | 2009-04-01 09:22:26 -0400 | [diff] [blame] | 1133 | #if defined(CONFIG_NFS_V4_1) |
| 1134 | .rpc_call_prepare = nfs_write_prepare, |
| 1135 | #endif /* CONFIG_NFS_V4_1 */ |
Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1136 | .rpc_call_done = nfs_writeback_done_full, |
Trond Myklebust | c9d8f89 | 2008-04-15 16:56:39 -0400 | [diff] [blame] | 1137 | .rpc_release = nfs_writeback_release_full, |
Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1138 | }; |
| 1139 | |
| 1140 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1141 | /* |
| 1142 | * This function is called when the WRITE call is complete. |
| 1143 | */ |
Chuck Lever | 462d5b3 | 2006-03-20 13:44:32 -0500 | [diff] [blame] | 1144 | int nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1145 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1146 | struct nfs_writeargs *argp = &data->args; |
| 1147 | struct nfs_writeres *resp = &data->res; |
Andy Adamson | eedc020 | 2009-04-01 09:22:41 -0400 | [diff] [blame] | 1148 | struct nfs_server *server = NFS_SERVER(data->inode); |
Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1149 | int status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1150 | |
Chuck Lever | a3f565b | 2007-01-31 12:14:01 -0500 | [diff] [blame] | 1151 | dprintk("NFS: %5u nfs_writeback_done (status %d)\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1152 | task->tk_pid, task->tk_status); |
| 1153 | |
Chuck Lever | f551e44 | 2006-09-20 14:33:04 -0400 | [diff] [blame] | 1154 | /* |
| 1155 | * ->write_done will attempt to use post-op attributes to detect |
| 1156 | * conflicting writes by other clients. A strict interpretation |
| 1157 | * of close-to-open would allow us to continue caching even if |
| 1158 | * another writer had changed the file, but some applications |
| 1159 | * depend on tighter cache coherency when writing. |
| 1160 | */ |
Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1161 | status = NFS_PROTO(data->inode)->write_done(task, data); |
| 1162 | if (status != 0) |
| 1163 | return status; |
Chuck Lever | 91d5b47 | 2006-03-20 13:44:14 -0500 | [diff] [blame] | 1164 | nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count); |
| 1165 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1166 | #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) |
| 1167 | if (resp->verf->committed < argp->stable && task->tk_status >= 0) { |
| 1168 | /* We tried a write call, but the server did not |
| 1169 | * commit data to stable storage even though we |
| 1170 | * requested it. |
| 1171 | * Note: There is a known bug in Tru64 < 5.0 in which |
| 1172 | * the server reports NFS_DATA_SYNC, but performs |
| 1173 | * NFS_FILE_SYNC. We therefore implement this checking |
| 1174 | * as a dprintk() in order to avoid filling syslog. |
| 1175 | */ |
| 1176 | static unsigned long complain; |
| 1177 | |
| 1178 | if (time_before(complain, jiffies)) { |
Chuck Lever | 48186c7 | 2008-06-11 17:56:05 -0400 | [diff] [blame] | 1179 | dprintk("NFS: faulty NFS server %s:" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1180 | " (committed = %d) != (stable = %d)\n", |
Andy Adamson | eedc020 | 2009-04-01 09:22:41 -0400 | [diff] [blame] | 1181 | server->nfs_client->cl_hostname, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1182 | resp->verf->committed, argp->stable); |
| 1183 | complain = jiffies + 300 * HZ; |
| 1184 | } |
| 1185 | } |
| 1186 | #endif |
| 1187 | /* Is this a short write? */ |
| 1188 | if (task->tk_status >= 0 && resp->count < argp->count) { |
| 1189 | static unsigned long complain; |
| 1190 | |
Chuck Lever | 91d5b47 | 2006-03-20 13:44:14 -0500 | [diff] [blame] | 1191 | nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE); |
| 1192 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1193 | /* Has the server at least made some progress? */ |
| 1194 | if (resp->count != 0) { |
| 1195 | /* Was this an NFSv2 write or an NFSv3 stable write? */ |
| 1196 | if (resp->verf->committed != NFS_UNSTABLE) { |
| 1197 | /* Resend from where the server left off */ |
| 1198 | argp->offset += resp->count; |
| 1199 | argp->pgbase += resp->count; |
| 1200 | argp->count -= resp->count; |
| 1201 | } else { |
| 1202 | /* Resend as a stable write in order to avoid |
| 1203 | * headaches in the case of a server crash. |
| 1204 | */ |
| 1205 | argp->stable = NFS_FILE_SYNC; |
| 1206 | } |
Andy Adamson | eedc020 | 2009-04-01 09:22:41 -0400 | [diff] [blame] | 1207 | nfs4_restart_rpc(task, server->nfs_client); |
Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1208 | return -EAGAIN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1209 | } |
| 1210 | if (time_before(complain, jiffies)) { |
| 1211 | printk(KERN_WARNING |
| 1212 | "NFS: Server wrote zero bytes, expected %u.\n", |
| 1213 | argp->count); |
| 1214 | complain = jiffies + 300 * HZ; |
| 1215 | } |
| 1216 | /* Can't do anything about it except throw an error. */ |
| 1217 | task->tk_status = -EIO; |
| 1218 | } |
Andy Adamson | eedc020 | 2009-04-01 09:22:41 -0400 | [diff] [blame] | 1219 | nfs4_sequence_free_slot(server->nfs_client, &data->res.seq_res); |
Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1220 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1221 | } |
| 1222 | |
| 1223 | |
| 1224 | #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) |
Trond Myklebust | c9d8f89 | 2008-04-15 16:56:39 -0400 | [diff] [blame] | 1225 | void nfs_commitdata_release(void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1226 | { |
Trond Myklebust | 383ba71 | 2008-02-19 20:04:20 -0500 | [diff] [blame] | 1227 | struct nfs_write_data *wdata = data; |
| 1228 | |
| 1229 | put_nfs_open_context(wdata->args.context); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1230 | nfs_commit_free(wdata); |
| 1231 | } |
| 1232 | |
| 1233 | /* |
| 1234 | * Set up the argument/result storage required for the RPC call. |
| 1235 | */ |
Trond Myklebust | dbae4c7 | 2008-04-14 14:54:53 -0400 | [diff] [blame] | 1236 | static int nfs_commit_rpcsetup(struct list_head *head, |
Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1237 | struct nfs_write_data *data, |
| 1238 | int how) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1239 | { |
Trond Myklebust | 84115e1 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 1240 | struct nfs_page *first = nfs_list_entry(head->next); |
| 1241 | struct inode *inode = first->wb_context->path.dentry->d_inode; |
| 1242 | int flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC; |
Trond Myklebust | 3ff7576 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 1243 | int priority = flush_task_priority(how); |
Trond Myklebust | 0773769 | 2007-10-25 18:42:54 -0400 | [diff] [blame] | 1244 | struct rpc_task *task; |
Trond Myklebust | bdc7f02 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 1245 | struct rpc_message msg = { |
| 1246 | .rpc_argp = &data->args, |
| 1247 | .rpc_resp = &data->res, |
| 1248 | .rpc_cred = first->wb_context->cred, |
| 1249 | }; |
Trond Myklebust | 84115e1 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 1250 | struct rpc_task_setup task_setup_data = { |
Trond Myklebust | 0773769 | 2007-10-25 18:42:54 -0400 | [diff] [blame] | 1251 | .task = &data->task, |
Trond Myklebust | 84115e1 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 1252 | .rpc_client = NFS_CLIENT(inode), |
Trond Myklebust | bdc7f02 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 1253 | .rpc_message = &msg, |
Trond Myklebust | 84115e1 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 1254 | .callback_ops = &nfs_commit_ops, |
| 1255 | .callback_data = data, |
Trond Myklebust | 101070c | 2008-02-19 20:04:23 -0500 | [diff] [blame] | 1256 | .workqueue = nfsiod_workqueue, |
Trond Myklebust | 84115e1 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 1257 | .flags = flags, |
Trond Myklebust | 3ff7576 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 1258 | .priority = priority, |
Trond Myklebust | 84115e1 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 1259 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1260 | |
| 1261 | /* Set up the RPC argument and reply structs |
| 1262 | * NB: take care not to mess about with data->commit et al. */ |
| 1263 | |
| 1264 | list_splice_init(head, &data->pages); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1265 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1266 | data->inode = inode; |
Trond Myklebust | bdc7f02 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 1267 | data->cred = msg.rpc_cred; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1268 | |
| 1269 | data->args.fh = NFS_FH(data->inode); |
Trond Myklebust | 3da28eb | 2005-06-22 17:16:31 +0000 | [diff] [blame] | 1270 | /* Note: we always request a commit of the entire inode */ |
| 1271 | data->args.offset = 0; |
| 1272 | data->args.count = 0; |
Trond Myklebust | 383ba71 | 2008-02-19 20:04:20 -0500 | [diff] [blame] | 1273 | data->args.context = get_nfs_open_context(first->wb_context); |
Trond Myklebust | 3da28eb | 2005-06-22 17:16:31 +0000 | [diff] [blame] | 1274 | data->res.count = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1275 | data->res.fattr = &data->fattr; |
| 1276 | data->res.verf = &data->verf; |
Trond Myklebust | 0e574af | 2005-10-27 22:12:38 -0400 | [diff] [blame] | 1277 | nfs_fattr_init(&data->fattr); |
Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1278 | |
| 1279 | /* Set up the initial task struct. */ |
Trond Myklebust | bdc7f02 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 1280 | NFS_PROTO(inode)->commit_setup(data, &msg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1281 | |
Chuck Lever | a3f565b | 2007-01-31 12:14:01 -0500 | [diff] [blame] | 1282 | dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid); |
Trond Myklebust | bdc7f02 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 1283 | |
Trond Myklebust | 0773769 | 2007-10-25 18:42:54 -0400 | [diff] [blame] | 1284 | task = rpc_run_task(&task_setup_data); |
Trond Myklebust | dbae4c7 | 2008-04-14 14:54:53 -0400 | [diff] [blame] | 1285 | if (IS_ERR(task)) |
| 1286 | return PTR_ERR(task); |
| 1287 | rpc_put_task(task); |
| 1288 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1289 | } |
| 1290 | |
| 1291 | /* |
| 1292 | * Commit dirty pages |
| 1293 | */ |
| 1294 | static int |
Chuck Lever | 40859d7 | 2005-11-30 18:09:02 -0500 | [diff] [blame] | 1295 | nfs_commit_list(struct inode *inode, struct list_head *head, int how) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1296 | { |
| 1297 | struct nfs_write_data *data; |
| 1298 | struct nfs_page *req; |
| 1299 | |
Trond Myklebust | c9d8f89 | 2008-04-15 16:56:39 -0400 | [diff] [blame] | 1300 | data = nfs_commitdata_alloc(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1301 | |
| 1302 | if (!data) |
| 1303 | goto out_bad; |
| 1304 | |
| 1305 | /* Set up the argument struct */ |
Trond Myklebust | dbae4c7 | 2008-04-14 14:54:53 -0400 | [diff] [blame] | 1306 | return nfs_commit_rpcsetup(head, data, how); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1307 | out_bad: |
| 1308 | while (!list_empty(head)) { |
| 1309 | req = nfs_list_entry(head->next); |
| 1310 | nfs_list_remove_request(req); |
| 1311 | nfs_mark_request_commit(req); |
Trond Myklebust | 83715ad | 2006-07-05 13:17:12 -0400 | [diff] [blame] | 1312 | dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS); |
Peter Zijlstra | c9e51e4 | 2007-10-16 23:25:47 -0700 | [diff] [blame] | 1313 | dec_bdi_stat(req->wb_page->mapping->backing_dev_info, |
| 1314 | BDI_RECLAIMABLE); |
Trond Myklebust | 9fd367f | 2007-06-17 15:10:24 -0400 | [diff] [blame] | 1315 | nfs_clear_page_tag_locked(req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1316 | } |
| 1317 | return -ENOMEM; |
| 1318 | } |
| 1319 | |
| 1320 | /* |
| 1321 | * COMMIT call returned |
| 1322 | */ |
Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1323 | static void nfs_commit_done(struct rpc_task *task, void *calldata) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1324 | { |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 1325 | struct nfs_write_data *data = calldata; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1326 | |
Chuck Lever | a3f565b | 2007-01-31 12:14:01 -0500 | [diff] [blame] | 1327 | dprintk("NFS: %5u nfs_commit_done (status %d)\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1328 | task->tk_pid, task->tk_status); |
| 1329 | |
Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1330 | /* Call the NFS version-specific code */ |
| 1331 | if (NFS_PROTO(data->inode)->commit_done(task, data) != 0) |
| 1332 | return; |
Trond Myklebust | c9d8f89 | 2008-04-15 16:56:39 -0400 | [diff] [blame] | 1333 | } |
| 1334 | |
| 1335 | static void nfs_commit_release(void *calldata) |
| 1336 | { |
| 1337 | struct nfs_write_data *data = calldata; |
| 1338 | struct nfs_page *req; |
| 1339 | int status = data->task.tk_status; |
Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1340 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1341 | while (!list_empty(&data->pages)) { |
| 1342 | req = nfs_list_entry(data->pages.next); |
| 1343 | nfs_list_remove_request(req); |
Trond Myklebust | e468bae | 2008-06-13 13:25:22 -0400 | [diff] [blame] | 1344 | nfs_clear_request_commit(req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1345 | |
Chuck Lever | 48186c7 | 2008-06-11 17:56:05 -0400 | [diff] [blame] | 1346 | dprintk("NFS: commit (%s/%lld %d@%lld)", |
Trond Myklebust | 88be9f9 | 2007-06-05 10:42:27 -0400 | [diff] [blame] | 1347 | req->wb_context->path.dentry->d_inode->i_sb->s_id, |
| 1348 | (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1349 | req->wb_bytes, |
| 1350 | (long long)req_offset(req)); |
Trond Myklebust | c9d8f89 | 2008-04-15 16:56:39 -0400 | [diff] [blame] | 1351 | if (status < 0) { |
| 1352 | nfs_context_set_write_error(req->wb_context, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1353 | nfs_inode_remove_request(req); |
Trond Myklebust | c9d8f89 | 2008-04-15 16:56:39 -0400 | [diff] [blame] | 1354 | dprintk(", error = %d\n", status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1355 | goto next; |
| 1356 | } |
| 1357 | |
| 1358 | /* Okay, COMMIT succeeded, apparently. Check the verifier |
| 1359 | * returned by the server against all stored verfs. */ |
| 1360 | if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) { |
| 1361 | /* We have a match */ |
| 1362 | nfs_inode_remove_request(req); |
| 1363 | dprintk(" OK\n"); |
| 1364 | goto next; |
| 1365 | } |
| 1366 | /* We have a mismatch. Write the page again */ |
| 1367 | dprintk(" mismatch\n"); |
Fred | 6d884e8 | 2008-03-19 11:24:38 -0400 | [diff] [blame] | 1368 | nfs_mark_request_dirty(req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1369 | next: |
Trond Myklebust | 9fd367f | 2007-06-17 15:10:24 -0400 | [diff] [blame] | 1370 | nfs_clear_page_tag_locked(req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1371 | } |
Trond Myklebust | c9d8f89 | 2008-04-15 16:56:39 -0400 | [diff] [blame] | 1372 | nfs_commitdata_release(calldata); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1373 | } |
Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1374 | |
| 1375 | static const struct rpc_call_ops nfs_commit_ops = { |
Andy Adamson | 21d9a85 | 2009-04-01 09:22:27 -0400 | [diff] [blame] | 1376 | #if defined(CONFIG_NFS_V4_1) |
| 1377 | .rpc_call_prepare = nfs_write_prepare, |
| 1378 | #endif /* CONFIG_NFS_V4_1 */ |
Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1379 | .rpc_call_done = nfs_commit_done, |
| 1380 | .rpc_release = nfs_commit_release, |
| 1381 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1382 | |
Trond Myklebust | 3da28eb | 2005-06-22 17:16:31 +0000 | [diff] [blame] | 1383 | int nfs_commit_inode(struct inode *inode, int how) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1384 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1385 | LIST_HEAD(head); |
Trond Myklebust | 7d46a49 | 2006-03-20 13:44:50 -0500 | [diff] [blame] | 1386 | int res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1387 | |
Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 1388 | spin_lock(&inode->i_lock); |
Trond Myklebust | 3da28eb | 2005-06-22 17:16:31 +0000 | [diff] [blame] | 1389 | res = nfs_scan_commit(inode, &head, 0, 0); |
Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 1390 | spin_unlock(&inode->i_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1391 | if (res) { |
Trond Myklebust | 7d46a49 | 2006-03-20 13:44:50 -0500 | [diff] [blame] | 1392 | int error = nfs_commit_list(inode, &head, how); |
Trond Myklebust | 3da28eb | 2005-06-22 17:16:31 +0000 | [diff] [blame] | 1393 | if (error < 0) |
| 1394 | return error; |
| 1395 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1396 | return res; |
| 1397 | } |
Trond Myklebust | c63c7b0 | 2007-04-02 19:29:52 -0400 | [diff] [blame] | 1398 | #else |
| 1399 | static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how) |
| 1400 | { |
| 1401 | return 0; |
| 1402 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1403 | #endif |
| 1404 | |
Trond Myklebust | 1c75950 | 2006-10-09 16:18:38 -0400 | [diff] [blame] | 1405 | long nfs_sync_mapping_wait(struct address_space *mapping, struct writeback_control *wbc, int how) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1406 | { |
Trond Myklebust | 1c75950 | 2006-10-09 16:18:38 -0400 | [diff] [blame] | 1407 | struct inode *inode = mapping->host; |
Trond Myklebust | ca52fec | 2007-04-17 17:22:13 -0400 | [diff] [blame] | 1408 | pgoff_t idx_start, idx_end; |
Trond Myklebust | 1c75950 | 2006-10-09 16:18:38 -0400 | [diff] [blame] | 1409 | unsigned int npages = 0; |
Trond Myklebust | c42de9d | 2006-03-20 13:44:51 -0500 | [diff] [blame] | 1410 | LIST_HEAD(head); |
Trond Myklebust | 70b9ecb | 2006-01-03 09:55:34 +0100 | [diff] [blame] | 1411 | int nocommit = how & FLUSH_NOCOMMIT; |
Trond Myklebust | 3f44254 | 2006-09-17 14:46:44 -0400 | [diff] [blame] | 1412 | long pages, ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1413 | |
Trond Myklebust | 1c75950 | 2006-10-09 16:18:38 -0400 | [diff] [blame] | 1414 | /* FIXME */ |
| 1415 | if (wbc->range_cyclic) |
| 1416 | idx_start = 0; |
| 1417 | else { |
| 1418 | idx_start = wbc->range_start >> PAGE_CACHE_SHIFT; |
| 1419 | idx_end = wbc->range_end >> PAGE_CACHE_SHIFT; |
| 1420 | if (idx_end > idx_start) { |
Trond Myklebust | ca52fec | 2007-04-17 17:22:13 -0400 | [diff] [blame] | 1421 | pgoff_t l_npages = 1 + idx_end - idx_start; |
Trond Myklebust | 1c75950 | 2006-10-09 16:18:38 -0400 | [diff] [blame] | 1422 | npages = l_npages; |
| 1423 | if (sizeof(npages) != sizeof(l_npages) && |
Trond Myklebust | ca52fec | 2007-04-17 17:22:13 -0400 | [diff] [blame] | 1424 | (pgoff_t)npages != l_npages) |
Trond Myklebust | 1c75950 | 2006-10-09 16:18:38 -0400 | [diff] [blame] | 1425 | npages = 0; |
| 1426 | } |
| 1427 | } |
Trond Myklebust | c42de9d | 2006-03-20 13:44:51 -0500 | [diff] [blame] | 1428 | how &= ~FLUSH_NOCOMMIT; |
Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 1429 | spin_lock(&inode->i_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1430 | do { |
Trond Myklebust | c42de9d | 2006-03-20 13:44:51 -0500 | [diff] [blame] | 1431 | ret = nfs_wait_on_requests_locked(inode, idx_start, npages); |
| 1432 | if (ret != 0) |
Trond Myklebust | 70b9ecb | 2006-01-03 09:55:34 +0100 | [diff] [blame] | 1433 | continue; |
Trond Myklebust | c42de9d | 2006-03-20 13:44:51 -0500 | [diff] [blame] | 1434 | if (nocommit) |
| 1435 | break; |
Trond Myklebust | d2ccddf | 2006-05-31 01:13:38 -0400 | [diff] [blame] | 1436 | pages = nfs_scan_commit(inode, &head, idx_start, npages); |
Trond Myklebust | 724c439 | 2007-04-17 17:22:13 -0400 | [diff] [blame] | 1437 | if (pages == 0) |
Trond Myklebust | c42de9d | 2006-03-20 13:44:51 -0500 | [diff] [blame] | 1438 | break; |
Trond Myklebust | d2ccddf | 2006-05-31 01:13:38 -0400 | [diff] [blame] | 1439 | if (how & FLUSH_INVALIDATE) { |
Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 1440 | spin_unlock(&inode->i_lock); |
Trond Myklebust | 83715ad | 2006-07-05 13:17:12 -0400 | [diff] [blame] | 1441 | nfs_cancel_commit_list(&head); |
Trond Myklebust | e8e058e | 2006-11-15 17:31:56 -0500 | [diff] [blame] | 1442 | ret = pages; |
Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 1443 | spin_lock(&inode->i_lock); |
Trond Myklebust | d2ccddf | 2006-05-31 01:13:38 -0400 | [diff] [blame] | 1444 | continue; |
| 1445 | } |
| 1446 | pages += nfs_scan_commit(inode, &head, 0, 0); |
Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 1447 | spin_unlock(&inode->i_lock); |
Trond Myklebust | c42de9d | 2006-03-20 13:44:51 -0500 | [diff] [blame] | 1448 | ret = nfs_commit_list(inode, &head, how); |
Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 1449 | spin_lock(&inode->i_lock); |
| 1450 | |
Trond Myklebust | c42de9d | 2006-03-20 13:44:51 -0500 | [diff] [blame] | 1451 | } while (ret >= 0); |
Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 1452 | spin_unlock(&inode->i_lock); |
Trond Myklebust | c42de9d | 2006-03-20 13:44:51 -0500 | [diff] [blame] | 1453 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1454 | } |
| 1455 | |
Trond Myklebust | 34901f7 | 2007-07-25 14:09:54 -0400 | [diff] [blame] | 1456 | static int __nfs_write_mapping(struct address_space *mapping, struct writeback_control *wbc, int how) |
Trond Myklebust | 1c75950 | 2006-10-09 16:18:38 -0400 | [diff] [blame] | 1457 | { |
Trond Myklebust | 1c75950 | 2006-10-09 16:18:38 -0400 | [diff] [blame] | 1458 | int ret; |
| 1459 | |
Trond Myklebust | 34901f7 | 2007-07-25 14:09:54 -0400 | [diff] [blame] | 1460 | ret = nfs_writepages(mapping, wbc); |
Trond Myklebust | 61822ab | 2006-12-05 00:35:42 -0500 | [diff] [blame] | 1461 | if (ret < 0) |
| 1462 | goto out; |
Trond Myklebust | 34901f7 | 2007-07-25 14:09:54 -0400 | [diff] [blame] | 1463 | ret = nfs_sync_mapping_wait(mapping, wbc, how); |
Trond Myklebust | ed90ef5 | 2007-07-20 13:13:28 -0400 | [diff] [blame] | 1464 | if (ret < 0) |
| 1465 | goto out; |
| 1466 | return 0; |
Trond Myklebust | 61822ab | 2006-12-05 00:35:42 -0500 | [diff] [blame] | 1467 | out: |
Trond Myklebust | e507d9e | 2006-12-05 00:35:42 -0500 | [diff] [blame] | 1468 | __mark_inode_dirty(mapping->host, I_DIRTY_PAGES); |
Trond Myklebust | 1c75950 | 2006-10-09 16:18:38 -0400 | [diff] [blame] | 1469 | return ret; |
| 1470 | } |
| 1471 | |
Trond Myklebust | 34901f7 | 2007-07-25 14:09:54 -0400 | [diff] [blame] | 1472 | /* Two pass sync: first using WB_SYNC_NONE, then WB_SYNC_ALL */ |
| 1473 | static int nfs_write_mapping(struct address_space *mapping, int how) |
| 1474 | { |
| 1475 | struct writeback_control wbc = { |
| 1476 | .bdi = mapping->backing_dev_info, |
Trond Myklebust | 72cb77f | 2009-03-11 14:10:30 -0400 | [diff] [blame] | 1477 | .sync_mode = WB_SYNC_ALL, |
Trond Myklebust | 34901f7 | 2007-07-25 14:09:54 -0400 | [diff] [blame] | 1478 | .nr_to_write = LONG_MAX, |
Trond Myklebust | d7fb120 | 2008-10-06 20:08:56 -0400 | [diff] [blame] | 1479 | .range_start = 0, |
| 1480 | .range_end = LLONG_MAX, |
Trond Myklebust | 34901f7 | 2007-07-25 14:09:54 -0400 | [diff] [blame] | 1481 | .for_writepages = 1, |
Trond Myklebust | 34901f7 | 2007-07-25 14:09:54 -0400 | [diff] [blame] | 1482 | }; |
Trond Myklebust | 34901f7 | 2007-07-25 14:09:54 -0400 | [diff] [blame] | 1483 | |
Trond Myklebust | 34901f7 | 2007-07-25 14:09:54 -0400 | [diff] [blame] | 1484 | return __nfs_write_mapping(mapping, &wbc, how); |
| 1485 | } |
| 1486 | |
Trond Myklebust | ed90ef5 | 2007-07-20 13:13:28 -0400 | [diff] [blame] | 1487 | /* |
| 1488 | * flush the inode to disk. |
| 1489 | */ |
| 1490 | int nfs_wb_all(struct inode *inode) |
Trond Myklebust | 1c75950 | 2006-10-09 16:18:38 -0400 | [diff] [blame] | 1491 | { |
Trond Myklebust | ed90ef5 | 2007-07-20 13:13:28 -0400 | [diff] [blame] | 1492 | return nfs_write_mapping(inode->i_mapping, 0); |
| 1493 | } |
Trond Myklebust | 1c75950 | 2006-10-09 16:18:38 -0400 | [diff] [blame] | 1494 | |
Trond Myklebust | ed90ef5 | 2007-07-20 13:13:28 -0400 | [diff] [blame] | 1495 | int nfs_wb_nocommit(struct inode *inode) |
| 1496 | { |
| 1497 | return nfs_write_mapping(inode->i_mapping, FLUSH_NOCOMMIT); |
Trond Myklebust | 1c75950 | 2006-10-09 16:18:38 -0400 | [diff] [blame] | 1498 | } |
| 1499 | |
Trond Myklebust | 1b3b4a1 | 2007-08-28 10:29:36 -0400 | [diff] [blame] | 1500 | int nfs_wb_page_cancel(struct inode *inode, struct page *page) |
| 1501 | { |
| 1502 | struct nfs_page *req; |
| 1503 | loff_t range_start = page_offset(page); |
| 1504 | loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1); |
| 1505 | struct writeback_control wbc = { |
| 1506 | .bdi = page->mapping->backing_dev_info, |
| 1507 | .sync_mode = WB_SYNC_ALL, |
| 1508 | .nr_to_write = LONG_MAX, |
| 1509 | .range_start = range_start, |
| 1510 | .range_end = range_end, |
| 1511 | }; |
| 1512 | int ret = 0; |
| 1513 | |
| 1514 | BUG_ON(!PageLocked(page)); |
| 1515 | for (;;) { |
| 1516 | req = nfs_page_find_request(page); |
| 1517 | if (req == NULL) |
| 1518 | goto out; |
Trond Myklebust | e468bae | 2008-06-13 13:25:22 -0400 | [diff] [blame] | 1519 | if (test_bit(PG_CLEAN, &req->wb_flags)) { |
Trond Myklebust | 1b3b4a1 | 2007-08-28 10:29:36 -0400 | [diff] [blame] | 1520 | nfs_release_request(req); |
| 1521 | break; |
| 1522 | } |
| 1523 | if (nfs_lock_request_dontget(req)) { |
| 1524 | nfs_inode_remove_request(req); |
| 1525 | /* |
| 1526 | * In case nfs_inode_remove_request has marked the |
| 1527 | * page as being dirty |
| 1528 | */ |
| 1529 | cancel_dirty_page(page, PAGE_CACHE_SIZE); |
| 1530 | nfs_unlock_request(req); |
| 1531 | break; |
| 1532 | } |
| 1533 | ret = nfs_wait_on_request(req); |
| 1534 | if (ret < 0) |
| 1535 | goto out; |
| 1536 | } |
| 1537 | if (!PagePrivate(page)) |
| 1538 | return 0; |
| 1539 | ret = nfs_sync_mapping_wait(page->mapping, &wbc, FLUSH_INVALIDATE); |
| 1540 | out: |
| 1541 | return ret; |
| 1542 | } |
| 1543 | |
Adrian Bunk | 5334eb1 | 2007-11-21 15:04:31 -0800 | [diff] [blame] | 1544 | static int nfs_wb_page_priority(struct inode *inode, struct page *page, |
| 1545 | int how) |
Trond Myklebust | 1c75950 | 2006-10-09 16:18:38 -0400 | [diff] [blame] | 1546 | { |
| 1547 | loff_t range_start = page_offset(page); |
| 1548 | loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1); |
Trond Myklebust | 4d770cc | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 1549 | struct writeback_control wbc = { |
| 1550 | .bdi = page->mapping->backing_dev_info, |
| 1551 | .sync_mode = WB_SYNC_ALL, |
| 1552 | .nr_to_write = LONG_MAX, |
| 1553 | .range_start = range_start, |
| 1554 | .range_end = range_end, |
| 1555 | }; |
| 1556 | int ret; |
Trond Myklebust | 1c75950 | 2006-10-09 16:18:38 -0400 | [diff] [blame] | 1557 | |
Trond Myklebust | 73e3302 | 2008-04-11 16:03:54 -0400 | [diff] [blame] | 1558 | do { |
| 1559 | if (clear_page_dirty_for_io(page)) { |
| 1560 | ret = nfs_writepage_locked(page, &wbc); |
| 1561 | if (ret < 0) |
| 1562 | goto out_error; |
| 1563 | } else if (!PagePrivate(page)) |
| 1564 | break; |
| 1565 | ret = nfs_sync_mapping_wait(page->mapping, &wbc, how); |
Trond Myklebust | 4d770cc | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 1566 | if (ret < 0) |
Trond Myklebust | 73e3302 | 2008-04-11 16:03:54 -0400 | [diff] [blame] | 1567 | goto out_error; |
| 1568 | } while (PagePrivate(page)); |
| 1569 | return 0; |
| 1570 | out_error: |
Trond Myklebust | e507d9e | 2006-12-05 00:35:42 -0500 | [diff] [blame] | 1571 | __mark_inode_dirty(inode, I_DIRTY_PAGES); |
Trond Myklebust | 4d770cc | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 1572 | return ret; |
Trond Myklebust | 1c75950 | 2006-10-09 16:18:38 -0400 | [diff] [blame] | 1573 | } |
| 1574 | |
| 1575 | /* |
| 1576 | * Write back all requests on one page - we do this before reading it. |
| 1577 | */ |
| 1578 | int nfs_wb_page(struct inode *inode, struct page* page) |
| 1579 | { |
Trond Myklebust | 4d770cc | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 1580 | return nfs_wb_page_priority(inode, page, FLUSH_STABLE); |
Trond Myklebust | 1c75950 | 2006-10-09 16:18:38 -0400 | [diff] [blame] | 1581 | } |
| 1582 | |
David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 1583 | int __init nfs_init_writepagecache(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1584 | { |
| 1585 | nfs_wdata_cachep = kmem_cache_create("nfs_write_data", |
| 1586 | sizeof(struct nfs_write_data), |
| 1587 | 0, SLAB_HWCACHE_ALIGN, |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 1588 | NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1589 | if (nfs_wdata_cachep == NULL) |
| 1590 | return -ENOMEM; |
| 1591 | |
Matthew Dobson | 93d2341 | 2006-03-26 01:37:50 -0800 | [diff] [blame] | 1592 | nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE, |
| 1593 | nfs_wdata_cachep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1594 | if (nfs_wdata_mempool == NULL) |
| 1595 | return -ENOMEM; |
| 1596 | |
Matthew Dobson | 93d2341 | 2006-03-26 01:37:50 -0800 | [diff] [blame] | 1597 | nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT, |
| 1598 | nfs_wdata_cachep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1599 | if (nfs_commit_mempool == NULL) |
| 1600 | return -ENOMEM; |
| 1601 | |
Peter Zijlstra | 89a0914 | 2007-03-16 13:38:26 -0800 | [diff] [blame] | 1602 | /* |
| 1603 | * NFS congestion size, scale with available memory. |
| 1604 | * |
| 1605 | * 64MB: 8192k |
| 1606 | * 128MB: 11585k |
| 1607 | * 256MB: 16384k |
| 1608 | * 512MB: 23170k |
| 1609 | * 1GB: 32768k |
| 1610 | * 2GB: 46340k |
| 1611 | * 4GB: 65536k |
| 1612 | * 8GB: 92681k |
| 1613 | * 16GB: 131072k |
| 1614 | * |
| 1615 | * This allows larger machines to have larger/more transfers. |
| 1616 | * Limit the default to 256M |
| 1617 | */ |
| 1618 | nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10); |
| 1619 | if (nfs_congestion_kb > 256*1024) |
| 1620 | nfs_congestion_kb = 256*1024; |
| 1621 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1622 | return 0; |
| 1623 | } |
| 1624 | |
David Brownell | 266bee8 | 2006-06-27 12:59:15 -0700 | [diff] [blame] | 1625 | void nfs_destroy_writepagecache(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1626 | { |
| 1627 | mempool_destroy(nfs_commit_mempool); |
| 1628 | mempool_destroy(nfs_wdata_mempool); |
Alexey Dobriyan | 1a1d92c | 2006-09-27 01:49:40 -0700 | [diff] [blame] | 1629 | kmem_cache_destroy(nfs_wdata_cachep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1630 | } |
| 1631 | |