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