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