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