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