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