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