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