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