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