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