Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/nfs/write.c |
| 3 | * |
Trond Myklebust | 7c85d90 | 2006-12-13 15:23:48 -0500 | [diff] [blame] | 4 | * Write file data over NFS. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * |
| 6 | * Copyright (C) 1996, 1997, Olaf Kirch <okir@monad.swb.de> |
| 7 | */ |
| 8 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | #include <linux/types.h> |
| 10 | #include <linux/slab.h> |
| 11 | #include <linux/mm.h> |
| 12 | #include <linux/pagemap.h> |
| 13 | #include <linux/file.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/writeback.h> |
Peter Zijlstra | 89a0914 | 2007-03-16 13:38:26 -0800 | [diff] [blame] | 15 | #include <linux/swap.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | |
| 17 | #include <linux/sunrpc/clnt.h> |
| 18 | #include <linux/nfs_fs.h> |
| 19 | #include <linux/nfs_mount.h> |
| 20 | #include <linux/nfs_page.h> |
Andrew Morton | 3fcfab1 | 2006-10-19 23:28:16 -0700 | [diff] [blame] | 21 | #include <linux/backing-dev.h> |
| 22 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include <asm/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
| 25 | #include "delegation.h" |
Trond Myklebust | 49a70f2 | 2006-12-05 00:35:38 -0500 | [diff] [blame] | 26 | #include "internal.h" |
Chuck Lever | 91d5b47 | 2006-03-20 13:44:14 -0500 | [diff] [blame] | 27 | #include "iostat.h" |
Andy Adamson | def6ed7 | 2009-04-01 09:22:26 -0400 | [diff] [blame] | 28 | #include "nfs4_fs.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
| 30 | #define NFSDBG_FACILITY NFSDBG_PAGECACHE |
| 31 | |
| 32 | #define MIN_POOL_WRITE (32) |
| 33 | #define MIN_POOL_COMMIT (4) |
| 34 | |
| 35 | /* |
| 36 | * Local function declarations |
| 37 | */ |
Trond Myklebust | c63c7b0 | 2007-04-02 19:29:52 -0400 | [diff] [blame] | 38 | static void nfs_pageio_init_write(struct nfs_pageio_descriptor *desc, |
| 39 | struct inode *inode, int ioflags); |
Fred Isaman | f8512ad | 2008-03-19 11:24:39 -0400 | [diff] [blame] | 40 | static void nfs_redirty_request(struct nfs_page *req); |
Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 41 | static const struct rpc_call_ops nfs_write_partial_ops; |
| 42 | static const struct rpc_call_ops nfs_write_full_ops; |
| 43 | static const struct rpc_call_ops nfs_commit_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 45 | static struct kmem_cache *nfs_wdata_cachep; |
Trond Myklebust | 3feb2d4 | 2006-03-20 13:44:37 -0500 | [diff] [blame] | 46 | static mempool_t *nfs_wdata_mempool; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | static mempool_t *nfs_commit_mempool; |
| 48 | |
Trond Myklebust | c9d8f89 | 2008-04-15 16:56:39 -0400 | [diff] [blame] | 49 | struct nfs_write_data *nfs_commitdata_alloc(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | { |
Christoph Lameter | e6b4f8d | 2006-12-06 20:33:14 -0800 | [diff] [blame] | 51 | struct nfs_write_data *p = mempool_alloc(nfs_commit_mempool, GFP_NOFS); |
Chuck Lever | 40859d7 | 2005-11-30 18:09:02 -0500 | [diff] [blame] | 52 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | if (p) { |
| 54 | memset(p, 0, sizeof(*p)); |
| 55 | INIT_LIST_HEAD(&p->pages); |
Andy Adamson | 5f7dbd5 | 2009-04-01 09:22:05 -0400 | [diff] [blame] | 56 | p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | } |
| 58 | return p; |
| 59 | } |
| 60 | |
Trond Myklebust | 5e4424a | 2008-02-25 21:53:49 -0800 | [diff] [blame] | 61 | void nfs_commit_free(struct nfs_write_data *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | { |
Chuck Lever | 40859d7 | 2005-11-30 18:09:02 -0500 | [diff] [blame] | 63 | if (p && (p->pagevec != &p->page_array[0])) |
| 64 | kfree(p->pagevec); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | mempool_free(p, nfs_commit_mempool); |
| 66 | } |
| 67 | |
Trond Myklebust | 8d5658c | 2007-04-10 09:26:35 -0400 | [diff] [blame] | 68 | struct nfs_write_data *nfs_writedata_alloc(unsigned int pagecount) |
Trond Myklebust | 3feb2d4 | 2006-03-20 13:44:37 -0500 | [diff] [blame] | 69 | { |
Christoph Lameter | e6b4f8d | 2006-12-06 20:33:14 -0800 | [diff] [blame] | 70 | struct nfs_write_data *p = mempool_alloc(nfs_wdata_mempool, GFP_NOFS); |
Trond Myklebust | 3feb2d4 | 2006-03-20 13:44:37 -0500 | [diff] [blame] | 71 | |
| 72 | if (p) { |
| 73 | memset(p, 0, sizeof(*p)); |
| 74 | INIT_LIST_HEAD(&p->pages); |
Trond Myklebust | e9f7bee | 2006-09-08 09:48:54 -0700 | [diff] [blame] | 75 | p->npages = pagecount; |
Andy Adamson | 5f7dbd5 | 2009-04-01 09:22:05 -0400 | [diff] [blame] | 76 | p->res.seq_res.sr_slotid = NFS4_MAX_SLOT_TABLE; |
Chuck Lever | 0d0b5cb | 2006-05-25 01:40:53 -0400 | [diff] [blame] | 77 | if (pagecount <= ARRAY_SIZE(p->page_array)) |
| 78 | p->pagevec = p->page_array; |
Trond Myklebust | 3feb2d4 | 2006-03-20 13:44:37 -0500 | [diff] [blame] | 79 | else { |
Chuck Lever | 0d0b5cb | 2006-05-25 01:40:53 -0400 | [diff] [blame] | 80 | p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS); |
| 81 | if (!p->pagevec) { |
Trond Myklebust | 3feb2d4 | 2006-03-20 13:44:37 -0500 | [diff] [blame] | 82 | mempool_free(p, nfs_wdata_mempool); |
| 83 | p = NULL; |
| 84 | } |
| 85 | } |
| 86 | } |
| 87 | return p; |
| 88 | } |
| 89 | |
Trond Myklebust | 5e4424a | 2008-02-25 21:53:49 -0800 | [diff] [blame] | 90 | static void nfs_writedata_free(struct nfs_write_data *p) |
Trond Myklebust | 3feb2d4 | 2006-03-20 13:44:37 -0500 | [diff] [blame] | 91 | { |
| 92 | if (p && (p->pagevec != &p->page_array[0])) |
| 93 | kfree(p->pagevec); |
| 94 | mempool_free(p, nfs_wdata_mempool); |
| 95 | } |
| 96 | |
Trond Myklebust | 383ba71 | 2008-02-19 20:04:20 -0500 | [diff] [blame] | 97 | void nfs_writedata_release(void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | { |
Trond Myklebust | 383ba71 | 2008-02-19 20:04:20 -0500 | [diff] [blame] | 99 | struct nfs_write_data *wdata = data; |
| 100 | |
| 101 | put_nfs_open_context(wdata->args.context); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | nfs_writedata_free(wdata); |
| 103 | } |
| 104 | |
Trond Myklebust | 7b159fc | 2007-07-25 14:09:54 -0400 | [diff] [blame] | 105 | static void nfs_context_set_write_error(struct nfs_open_context *ctx, int error) |
| 106 | { |
| 107 | ctx->error = error; |
| 108 | smp_wmb(); |
| 109 | set_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags); |
| 110 | } |
| 111 | |
Trond Myklebust | 277459d | 2006-12-05 00:35:35 -0500 | [diff] [blame] | 112 | static struct nfs_page *nfs_page_find_request_locked(struct page *page) |
| 113 | { |
| 114 | struct nfs_page *req = NULL; |
| 115 | |
| 116 | if (PagePrivate(page)) { |
| 117 | req = (struct nfs_page *)page_private(page); |
| 118 | if (req != NULL) |
Trond Myklebust | c03b402 | 2007-06-17 13:26:38 -0400 | [diff] [blame] | 119 | kref_get(&req->wb_kref); |
Trond Myklebust | 277459d | 2006-12-05 00:35:35 -0500 | [diff] [blame] | 120 | } |
| 121 | return req; |
| 122 | } |
| 123 | |
| 124 | static struct nfs_page *nfs_page_find_request(struct page *page) |
| 125 | { |
Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 126 | struct inode *inode = page->mapping->host; |
Trond Myklebust | 277459d | 2006-12-05 00:35:35 -0500 | [diff] [blame] | 127 | struct nfs_page *req = NULL; |
Trond Myklebust | 277459d | 2006-12-05 00:35:35 -0500 | [diff] [blame] | 128 | |
Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 129 | spin_lock(&inode->i_lock); |
Trond Myklebust | 277459d | 2006-12-05 00:35:35 -0500 | [diff] [blame] | 130 | req = nfs_page_find_request_locked(page); |
Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 131 | spin_unlock(&inode->i_lock); |
Trond Myklebust | 277459d | 2006-12-05 00:35:35 -0500 | [diff] [blame] | 132 | return req; |
| 133 | } |
| 134 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | /* Adjust the file length if we're writing beyond the end */ |
| 136 | static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count) |
| 137 | { |
| 138 | struct inode *inode = page->mapping->host; |
Trond Myklebust | a3d0145 | 2008-06-11 12:21:19 -0400 | [diff] [blame] | 139 | loff_t end, i_size; |
| 140 | pgoff_t end_index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | |
Trond Myklebust | a3d0145 | 2008-06-11 12:21:19 -0400 | [diff] [blame] | 142 | spin_lock(&inode->i_lock); |
| 143 | i_size = i_size_read(inode); |
| 144 | end_index = (i_size - 1) >> PAGE_CACHE_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | if (i_size > 0 && page->index < end_index) |
Trond Myklebust | a3d0145 | 2008-06-11 12:21:19 -0400 | [diff] [blame] | 146 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count); |
| 148 | if (i_size >= end) |
Trond Myklebust | a3d0145 | 2008-06-11 12:21:19 -0400 | [diff] [blame] | 149 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | i_size_write(inode, end); |
Trond Myklebust | a3d0145 | 2008-06-11 12:21:19 -0400 | [diff] [blame] | 151 | nfs_inc_stats(inode, NFSIOS_EXTENDWRITE); |
| 152 | out: |
| 153 | spin_unlock(&inode->i_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Trond Myklebust | a301b77 | 2007-02-06 11:07:15 -0800 | [diff] [blame] | 156 | /* A writeback failed: mark the page as bad, and invalidate the page cache */ |
| 157 | static void nfs_set_pageerror(struct page *page) |
| 158 | { |
| 159 | SetPageError(page); |
| 160 | nfs_zap_mapping(page->mapping->host, page->mapping); |
| 161 | } |
| 162 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | /* We can set the PG_uptodate flag if we see that a write request |
| 164 | * covers the full page. |
| 165 | */ |
| 166 | static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count) |
| 167 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | if (PageUptodate(page)) |
| 169 | return; |
| 170 | if (base != 0) |
| 171 | return; |
Trond Myklebust | 49a70f2 | 2006-12-05 00:35:38 -0500 | [diff] [blame] | 172 | if (count != nfs_page_length(page)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | return; |
Trond Myklebust | 49a70f2 | 2006-12-05 00:35:38 -0500 | [diff] [blame] | 174 | SetPageUptodate(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | static int wb_priority(struct writeback_control *wbc) |
| 178 | { |
| 179 | if (wbc->for_reclaim) |
Trond Myklebust | c63c7b0 | 2007-04-02 19:29:52 -0400 | [diff] [blame] | 180 | return FLUSH_HIGHPRI | FLUSH_STABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | if (wbc->for_kupdate) |
| 182 | return FLUSH_LOWPRI; |
| 183 | return 0; |
| 184 | } |
| 185 | |
| 186 | /* |
Peter Zijlstra | 89a0914 | 2007-03-16 13:38:26 -0800 | [diff] [blame] | 187 | * NFS congestion control |
| 188 | */ |
| 189 | |
| 190 | int nfs_congestion_kb; |
| 191 | |
| 192 | #define NFS_CONGESTION_ON_THRESH (nfs_congestion_kb >> (PAGE_SHIFT-10)) |
| 193 | #define NFS_CONGESTION_OFF_THRESH \ |
| 194 | (NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2)) |
| 195 | |
Trond Myklebust | 5a6d41b | 2007-04-14 19:10:12 -0400 | [diff] [blame] | 196 | static int nfs_set_page_writeback(struct page *page) |
Peter Zijlstra | 89a0914 | 2007-03-16 13:38:26 -0800 | [diff] [blame] | 197 | { |
Trond Myklebust | 5a6d41b | 2007-04-14 19:10:12 -0400 | [diff] [blame] | 198 | int ret = test_set_page_writeback(page); |
| 199 | |
| 200 | if (!ret) { |
Peter Zijlstra | 89a0914 | 2007-03-16 13:38:26 -0800 | [diff] [blame] | 201 | struct inode *inode = page->mapping->host; |
| 202 | struct nfs_server *nfss = NFS_SERVER(inode); |
| 203 | |
Peter Zijlstra | 277866a | 2007-05-08 00:35:12 -0700 | [diff] [blame] | 204 | if (atomic_long_inc_return(&nfss->writeback) > |
Jens Axboe | 8aa7e84 | 2009-07-09 14:52:32 +0200 | [diff] [blame] | 205 | NFS_CONGESTION_ON_THRESH) { |
| 206 | set_bdi_congested(&nfss->backing_dev_info, |
| 207 | BLK_RW_ASYNC); |
| 208 | } |
Peter Zijlstra | 89a0914 | 2007-03-16 13:38:26 -0800 | [diff] [blame] | 209 | } |
Trond Myklebust | 5a6d41b | 2007-04-14 19:10:12 -0400 | [diff] [blame] | 210 | return ret; |
Peter Zijlstra | 89a0914 | 2007-03-16 13:38:26 -0800 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | static void nfs_end_page_writeback(struct page *page) |
| 214 | { |
| 215 | struct inode *inode = page->mapping->host; |
| 216 | struct nfs_server *nfss = NFS_SERVER(inode); |
| 217 | |
| 218 | end_page_writeback(page); |
Peter Zijlstra | c4dc4be | 2007-10-16 23:25:41 -0700 | [diff] [blame] | 219 | if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH) |
Jens Axboe | 8aa7e84 | 2009-07-09 14:52:32 +0200 | [diff] [blame] | 220 | clear_bdi_congested(&nfss->backing_dev_info, BLK_RW_ASYNC); |
Peter Zijlstra | 89a0914 | 2007-03-16 13:38:26 -0800 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | /* |
Trond Myklebust | e261f51 | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 224 | * Find an associated nfs write request, and prepare to flush it out |
Trond Myklebust | 9cccef9 | 2007-07-22 17:09:05 -0400 | [diff] [blame] | 225 | * May return an error if the user signalled nfs_wait_on_request(). |
Trond Myklebust | e261f51 | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 226 | */ |
Trond Myklebust | c63c7b0 | 2007-04-02 19:29:52 -0400 | [diff] [blame] | 227 | static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio, |
| 228 | struct page *page) |
Trond Myklebust | e261f51 | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 229 | { |
Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 230 | struct inode *inode = page->mapping->host; |
Trond Myklebust | e261f51 | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 231 | struct nfs_page *req; |
Trond Myklebust | e261f51 | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 232 | int ret; |
| 233 | |
Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 234 | spin_lock(&inode->i_lock); |
Trond Myklebust | e261f51 | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 235 | for(;;) { |
| 236 | req = nfs_page_find_request_locked(page); |
| 237 | if (req == NULL) { |
Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 238 | spin_unlock(&inode->i_lock); |
Trond Myklebust | 9cccef9 | 2007-07-22 17:09:05 -0400 | [diff] [blame] | 239 | return 0; |
Trond Myklebust | e261f51 | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 240 | } |
Trond Myklebust | acee478 | 2008-01-22 17:13:07 -0500 | [diff] [blame] | 241 | if (nfs_set_page_tag_locked(req)) |
Trond Myklebust | e261f51 | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 242 | break; |
| 243 | /* Note: If we hold the page lock, as is the case in nfs_writepage, |
Trond Myklebust | acee478 | 2008-01-22 17:13:07 -0500 | [diff] [blame] | 244 | * then the call to nfs_set_page_tag_locked() will always |
Trond Myklebust | e261f51 | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 245 | * succeed provided that someone hasn't already marked the |
| 246 | * request as dirty (in which case we don't care). |
| 247 | */ |
Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 248 | spin_unlock(&inode->i_lock); |
Trond Myklebust | e261f51 | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 249 | ret = nfs_wait_on_request(req); |
| 250 | nfs_release_request(req); |
| 251 | if (ret != 0) |
| 252 | return ret; |
Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 253 | spin_lock(&inode->i_lock); |
Trond Myklebust | e261f51 | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 254 | } |
Trond Myklebust | e468bae | 2008-06-13 13:25:22 -0400 | [diff] [blame] | 255 | if (test_bit(PG_CLEAN, &req->wb_flags)) { |
Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 256 | spin_unlock(&inode->i_lock); |
Trond Myklebust | e468bae | 2008-06-13 13:25:22 -0400 | [diff] [blame] | 257 | BUG(); |
Trond Myklebust | 612c938 | 2007-04-20 16:12:45 -0400 | [diff] [blame] | 258 | } |
Trond Myklebust | c63c7b0 | 2007-04-02 19:29:52 -0400 | [diff] [blame] | 259 | if (nfs_set_page_writeback(page) != 0) { |
Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 260 | spin_unlock(&inode->i_lock); |
Trond Myklebust | c63c7b0 | 2007-04-02 19:29:52 -0400 | [diff] [blame] | 261 | BUG(); |
| 262 | } |
Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 263 | spin_unlock(&inode->i_lock); |
Fred Isaman | f8512ad | 2008-03-19 11:24:39 -0400 | [diff] [blame] | 264 | if (!nfs_pageio_add_request(pgio, req)) { |
| 265 | nfs_redirty_request(req); |
Fred Isaman | f8512ad | 2008-03-19 11:24:39 -0400 | [diff] [blame] | 266 | return pgio->pg_error; |
| 267 | } |
Trond Myklebust | 9cccef9 | 2007-07-22 17:09:05 -0400 | [diff] [blame] | 268 | return 0; |
Trond Myklebust | e261f51 | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 269 | } |
| 270 | |
Trond Myklebust | f758c885 | 2007-07-22 19:27:32 -0400 | [diff] [blame] | 271 | static int nfs_do_writepage(struct page *page, struct writeback_control *wbc, struct nfs_pageio_descriptor *pgio) |
| 272 | { |
| 273 | struct inode *inode = page->mapping->host; |
| 274 | |
| 275 | nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE); |
| 276 | nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1); |
| 277 | |
| 278 | nfs_pageio_cond_complete(pgio, page->index); |
| 279 | return nfs_page_async_flush(pgio, page); |
| 280 | } |
| 281 | |
Trond Myklebust | e261f51 | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 282 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | * Write an mmapped page to the server. |
| 284 | */ |
Trond Myklebust | 4d770cc | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 285 | static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | { |
Trond Myklebust | f758c885 | 2007-07-22 19:27:32 -0400 | [diff] [blame] | 287 | struct nfs_pageio_descriptor pgio; |
Trond Myklebust | e261f51 | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 288 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | |
Trond Myklebust | f758c885 | 2007-07-22 19:27:32 -0400 | [diff] [blame] | 290 | nfs_pageio_init_write(&pgio, page->mapping->host, wb_priority(wbc)); |
| 291 | err = nfs_do_writepage(page, wbc, &pgio); |
| 292 | nfs_pageio_complete(&pgio); |
| 293 | if (err < 0) |
| 294 | return err; |
| 295 | if (pgio.pg_error < 0) |
| 296 | return pgio.pg_error; |
| 297 | return 0; |
Trond Myklebust | 4d770cc | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | int nfs_writepage(struct page *page, struct writeback_control *wbc) |
| 301 | { |
Trond Myklebust | f758c885 | 2007-07-22 19:27:32 -0400 | [diff] [blame] | 302 | int ret; |
Trond Myklebust | 4d770cc | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 303 | |
Trond Myklebust | f758c885 | 2007-07-22 19:27:32 -0400 | [diff] [blame] | 304 | ret = nfs_writepage_locked(page, wbc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | unlock_page(page); |
Trond Myklebust | f758c885 | 2007-07-22 19:27:32 -0400 | [diff] [blame] | 306 | return ret; |
| 307 | } |
| 308 | |
| 309 | static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data) |
| 310 | { |
| 311 | int ret; |
| 312 | |
| 313 | ret = nfs_do_writepage(page, wbc, data); |
| 314 | unlock_page(page); |
| 315 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 316 | } |
| 317 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc) |
| 319 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | struct inode *inode = mapping->host; |
Trond Myklebust | 72cb77f | 2009-03-11 14:10:30 -0400 | [diff] [blame] | 321 | unsigned long *bitlock = &NFS_I(inode)->flags; |
Trond Myklebust | c63c7b0 | 2007-04-02 19:29:52 -0400 | [diff] [blame] | 322 | struct nfs_pageio_descriptor pgio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | int err; |
| 324 | |
Trond Myklebust | 72cb77f | 2009-03-11 14:10:30 -0400 | [diff] [blame] | 325 | /* Stop dirtying of new pages while we sync */ |
| 326 | err = wait_on_bit_lock(bitlock, NFS_INO_FLUSHING, |
| 327 | nfs_wait_bit_killable, TASK_KILLABLE); |
| 328 | if (err) |
| 329 | goto out_err; |
| 330 | |
Chuck Lever | 91d5b47 | 2006-03-20 13:44:14 -0500 | [diff] [blame] | 331 | nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES); |
| 332 | |
Trond Myklebust | c63c7b0 | 2007-04-02 19:29:52 -0400 | [diff] [blame] | 333 | nfs_pageio_init_write(&pgio, inode, wb_priority(wbc)); |
Trond Myklebust | f758c885 | 2007-07-22 19:27:32 -0400 | [diff] [blame] | 334 | err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio); |
Trond Myklebust | c63c7b0 | 2007-04-02 19:29:52 -0400 | [diff] [blame] | 335 | nfs_pageio_complete(&pgio); |
Trond Myklebust | 72cb77f | 2009-03-11 14:10:30 -0400 | [diff] [blame] | 336 | |
| 337 | clear_bit_unlock(NFS_INO_FLUSHING, bitlock); |
| 338 | smp_mb__after_clear_bit(); |
| 339 | wake_up_bit(bitlock, NFS_INO_FLUSHING); |
| 340 | |
Trond Myklebust | f758c885 | 2007-07-22 19:27:32 -0400 | [diff] [blame] | 341 | if (err < 0) |
Trond Myklebust | 72cb77f | 2009-03-11 14:10:30 -0400 | [diff] [blame] | 342 | goto out_err; |
| 343 | err = pgio.pg_error; |
| 344 | if (err < 0) |
| 345 | goto out_err; |
Trond Myklebust | c63c7b0 | 2007-04-02 19:29:52 -0400 | [diff] [blame] | 346 | return 0; |
Trond Myklebust | 72cb77f | 2009-03-11 14:10:30 -0400 | [diff] [blame] | 347 | out_err: |
| 348 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | /* |
| 352 | * Insert a write request into an inode |
| 353 | */ |
Trond Myklebust | e7d3906 | 2008-06-13 12:12:32 -0400 | [diff] [blame] | 354 | static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | { |
| 356 | struct nfs_inode *nfsi = NFS_I(inode); |
| 357 | int error; |
| 358 | |
Trond Myklebust | e7d3906 | 2008-06-13 12:12:32 -0400 | [diff] [blame] | 359 | error = radix_tree_preload(GFP_NOFS); |
| 360 | if (error != 0) |
| 361 | goto out; |
| 362 | |
| 363 | /* Lock the request! */ |
| 364 | nfs_lock_request_dontget(req); |
| 365 | |
| 366 | spin_lock(&inode->i_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req); |
Nick Piggin | 2785259 | 2008-02-04 23:48:37 -0800 | [diff] [blame] | 368 | BUG_ON(error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | if (!nfsi->npages) { |
| 370 | igrab(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | if (nfs_have_delegation(inode, FMODE_WRITE)) |
| 372 | nfsi->change_attr++; |
| 373 | } |
Trond Myklebust | deb7d63 | 2006-03-20 13:44:50 -0500 | [diff] [blame] | 374 | SetPagePrivate(req->wb_page); |
Trond Myklebust | 277459d | 2006-12-05 00:35:35 -0500 | [diff] [blame] | 375 | set_page_private(req->wb_page, (unsigned long)req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | nfsi->npages++; |
Trond Myklebust | c03b402 | 2007-06-17 13:26:38 -0400 | [diff] [blame] | 377 | kref_get(&req->wb_kref); |
Nick Piggin | 2785259 | 2008-02-04 23:48:37 -0800 | [diff] [blame] | 378 | radix_tree_tag_set(&nfsi->nfs_page_tree, req->wb_index, |
| 379 | NFS_PAGE_TAG_LOCKED); |
Trond Myklebust | e7d3906 | 2008-06-13 12:12:32 -0400 | [diff] [blame] | 380 | spin_unlock(&inode->i_lock); |
| 381 | radix_tree_preload_end(); |
| 382 | out: |
| 383 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | /* |
Peter Zijlstra | 89a0914 | 2007-03-16 13:38:26 -0800 | [diff] [blame] | 387 | * Remove a write request from an inode |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | */ |
| 389 | static void nfs_inode_remove_request(struct nfs_page *req) |
| 390 | { |
Trond Myklebust | 88be9f9 | 2007-06-05 10:42:27 -0400 | [diff] [blame] | 391 | struct inode *inode = req->wb_context->path.dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | struct nfs_inode *nfsi = NFS_I(inode); |
| 393 | |
| 394 | BUG_ON (!NFS_WBACK_BUSY(req)); |
| 395 | |
Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 396 | spin_lock(&inode->i_lock); |
Trond Myklebust | 277459d | 2006-12-05 00:35:35 -0500 | [diff] [blame] | 397 | set_page_private(req->wb_page, 0); |
Trond Myklebust | deb7d63 | 2006-03-20 13:44:50 -0500 | [diff] [blame] | 398 | ClearPagePrivate(req->wb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index); |
| 400 | nfsi->npages--; |
| 401 | if (!nfsi->npages) { |
Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 402 | spin_unlock(&inode->i_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | iput(inode); |
| 404 | } else |
Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 405 | spin_unlock(&inode->i_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | nfs_clear_request(req); |
| 407 | nfs_release_request(req); |
| 408 | } |
| 409 | |
Trond Myklebust | 61822ab | 2006-12-05 00:35:42 -0500 | [diff] [blame] | 410 | static void |
Fred | 6d884e8 | 2008-03-19 11:24:38 -0400 | [diff] [blame] | 411 | nfs_mark_request_dirty(struct nfs_page *req) |
Trond Myklebust | 61822ab | 2006-12-05 00:35:42 -0500 | [diff] [blame] | 412 | { |
Trond Myklebust | 61822ab | 2006-12-05 00:35:42 -0500 | [diff] [blame] | 413 | __set_page_dirty_nobuffers(req->wb_page); |
| 414 | } |
| 415 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) |
| 417 | /* |
| 418 | * Add a request to the inode's commit list. |
| 419 | */ |
| 420 | static void |
| 421 | nfs_mark_request_commit(struct nfs_page *req) |
| 422 | { |
Trond Myklebust | 88be9f9 | 2007-06-05 10:42:27 -0400 | [diff] [blame] | 423 | struct inode *inode = req->wb_context->path.dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | struct nfs_inode *nfsi = NFS_I(inode); |
| 425 | |
Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 426 | spin_lock(&inode->i_lock); |
Trond Myklebust | e468bae | 2008-06-13 13:25:22 -0400 | [diff] [blame] | 427 | set_bit(PG_CLEAN, &(req)->wb_flags); |
Trond Myklebust | 5c36968 | 2007-06-17 15:27:42 -0400 | [diff] [blame] | 428 | radix_tree_tag_set(&nfsi->nfs_page_tree, |
| 429 | req->wb_index, |
| 430 | NFS_PAGE_TAG_COMMIT); |
Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 431 | spin_unlock(&inode->i_lock); |
Christoph Lameter | fd39fc8 | 2006-06-30 01:55:40 -0700 | [diff] [blame] | 432 | inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS); |
Peter Zijlstra | c9e51e4 | 2007-10-16 23:25:47 -0700 | [diff] [blame] | 433 | inc_bdi_stat(req->wb_page->mapping->backing_dev_info, BDI_RECLAIMABLE); |
Trond Myklebust | a180304 | 2006-12-05 00:45:19 -0500 | [diff] [blame] | 434 | __mark_inode_dirty(inode, I_DIRTY_DATASYNC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | } |
Trond Myklebust | 8e821ca | 2007-04-20 16:12:34 -0400 | [diff] [blame] | 436 | |
Trond Myklebust | e468bae | 2008-06-13 13:25:22 -0400 | [diff] [blame] | 437 | static int |
| 438 | nfs_clear_request_commit(struct nfs_page *req) |
| 439 | { |
| 440 | struct page *page = req->wb_page; |
| 441 | |
| 442 | if (test_and_clear_bit(PG_CLEAN, &(req)->wb_flags)) { |
| 443 | dec_zone_page_state(page, NR_UNSTABLE_NFS); |
| 444 | dec_bdi_stat(page->mapping->backing_dev_info, BDI_RECLAIMABLE); |
| 445 | return 1; |
| 446 | } |
| 447 | return 0; |
| 448 | } |
| 449 | |
Trond Myklebust | 8e821ca | 2007-04-20 16:12:34 -0400 | [diff] [blame] | 450 | static inline |
| 451 | int nfs_write_need_commit(struct nfs_write_data *data) |
| 452 | { |
| 453 | return data->verf.committed != NFS_FILE_SYNC; |
| 454 | } |
| 455 | |
| 456 | static inline |
| 457 | int nfs_reschedule_unstable_write(struct nfs_page *req) |
| 458 | { |
Trond Myklebust | e468bae | 2008-06-13 13:25:22 -0400 | [diff] [blame] | 459 | if (test_and_clear_bit(PG_NEED_COMMIT, &req->wb_flags)) { |
Trond Myklebust | 8e821ca | 2007-04-20 16:12:34 -0400 | [diff] [blame] | 460 | nfs_mark_request_commit(req); |
| 461 | return 1; |
| 462 | } |
| 463 | if (test_and_clear_bit(PG_NEED_RESCHED, &req->wb_flags)) { |
Fred | 6d884e8 | 2008-03-19 11:24:38 -0400 | [diff] [blame] | 464 | nfs_mark_request_dirty(req); |
Trond Myklebust | 8e821ca | 2007-04-20 16:12:34 -0400 | [diff] [blame] | 465 | return 1; |
| 466 | } |
| 467 | return 0; |
| 468 | } |
| 469 | #else |
| 470 | static inline void |
| 471 | nfs_mark_request_commit(struct nfs_page *req) |
| 472 | { |
| 473 | } |
| 474 | |
Trond Myklebust | e468bae | 2008-06-13 13:25:22 -0400 | [diff] [blame] | 475 | static inline int |
| 476 | nfs_clear_request_commit(struct nfs_page *req) |
| 477 | { |
| 478 | return 0; |
| 479 | } |
| 480 | |
Trond Myklebust | 8e821ca | 2007-04-20 16:12:34 -0400 | [diff] [blame] | 481 | static inline |
| 482 | int nfs_write_need_commit(struct nfs_write_data *data) |
| 483 | { |
| 484 | return 0; |
| 485 | } |
| 486 | |
| 487 | static inline |
| 488 | int nfs_reschedule_unstable_write(struct nfs_page *req) |
| 489 | { |
| 490 | return 0; |
| 491 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | #endif |
| 493 | |
| 494 | /* |
| 495 | * Wait for a request to complete. |
| 496 | * |
Matthew Wilcox | 150030b | 2007-12-06 16:24:39 -0500 | [diff] [blame] | 497 | * Interruptible by fatal signals only. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | */ |
Trond Myklebust | ca52fec | 2007-04-17 17:22:13 -0400 | [diff] [blame] | 499 | static int nfs_wait_on_requests_locked(struct inode *inode, pgoff_t idx_start, unsigned int npages) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | { |
| 501 | struct nfs_inode *nfsi = NFS_I(inode); |
| 502 | struct nfs_page *req; |
Trond Myklebust | ca52fec | 2007-04-17 17:22:13 -0400 | [diff] [blame] | 503 | pgoff_t idx_end, next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | unsigned int res = 0; |
| 505 | int error; |
| 506 | |
| 507 | if (npages == 0) |
| 508 | idx_end = ~0; |
| 509 | else |
| 510 | idx_end = idx_start + npages - 1; |
| 511 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | next = idx_start; |
Trond Myklebust | 9fd367f | 2007-06-17 15:10:24 -0400 | [diff] [blame] | 513 | while (radix_tree_gang_lookup_tag(&nfsi->nfs_page_tree, (void **)&req, next, 1, NFS_PAGE_TAG_LOCKED)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | if (req->wb_index > idx_end) |
| 515 | break; |
| 516 | |
| 517 | next = req->wb_index + 1; |
Trond Myklebust | c6a556b | 2005-06-22 17:16:30 +0000 | [diff] [blame] | 518 | BUG_ON(!NFS_WBACK_BUSY(req)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | |
Trond Myklebust | c03b402 | 2007-06-17 13:26:38 -0400 | [diff] [blame] | 520 | kref_get(&req->wb_kref); |
Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 521 | spin_unlock(&inode->i_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | error = nfs_wait_on_request(req); |
| 523 | nfs_release_request(req); |
Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 524 | spin_lock(&inode->i_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | if (error < 0) |
| 526 | return error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | res++; |
| 528 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | return res; |
| 530 | } |
| 531 | |
Trond Myklebust | 83715ad | 2006-07-05 13:17:12 -0400 | [diff] [blame] | 532 | static void nfs_cancel_commit_list(struct list_head *head) |
| 533 | { |
| 534 | struct nfs_page *req; |
| 535 | |
| 536 | while(!list_empty(head)) { |
| 537 | req = nfs_list_entry(head->next); |
| 538 | nfs_list_remove_request(req); |
Trond Myklebust | e468bae | 2008-06-13 13:25:22 -0400 | [diff] [blame] | 539 | nfs_clear_request_commit(req); |
Trond Myklebust | 83715ad | 2006-07-05 13:17:12 -0400 | [diff] [blame] | 540 | nfs_inode_remove_request(req); |
Trond Myklebust | b6dff26 | 2006-10-19 23:28:38 -0700 | [diff] [blame] | 541 | nfs_unlock_request(req); |
Trond Myklebust | 83715ad | 2006-07-05 13:17:12 -0400 | [diff] [blame] | 542 | } |
| 543 | } |
| 544 | |
Trond Myklebust | 47c6256 | 2009-03-16 08:13:41 -0400 | [diff] [blame] | 545 | #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) |
Trond Myklebust | fb8a1f1 | 2009-03-11 14:10:29 -0400 | [diff] [blame] | 546 | static int |
| 547 | nfs_need_commit(struct nfs_inode *nfsi) |
| 548 | { |
| 549 | return radix_tree_tagged(&nfsi->nfs_page_tree, NFS_PAGE_TAG_COMMIT); |
| 550 | } |
| 551 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | /* |
| 553 | * nfs_scan_commit - Scan an inode for commit requests |
| 554 | * @inode: NFS inode to scan |
| 555 | * @dst: destination list |
| 556 | * @idx_start: lower bound of page->index to scan. |
| 557 | * @npages: idx_start + npages sets the upper bound to scan. |
| 558 | * |
| 559 | * Moves requests from the inode's 'commit' request list. |
| 560 | * The requests are *not* checked to ensure that they form a contiguous set. |
| 561 | */ |
| 562 | static int |
Trond Myklebust | ca52fec | 2007-04-17 17:22:13 -0400 | [diff] [blame] | 563 | nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | { |
| 565 | struct nfs_inode *nfsi = NFS_I(inode); |
Trond Myklebust | 3da28eb | 2005-06-22 17:16:31 +0000 | [diff] [blame] | 566 | |
Trond Myklebust | fb8a1f1 | 2009-03-11 14:10:29 -0400 | [diff] [blame] | 567 | if (!nfs_need_commit(nfsi)) |
| 568 | return 0; |
| 569 | |
| 570 | return nfs_scan_list(nfsi, dst, idx_start, npages, NFS_PAGE_TAG_COMMIT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | } |
Trond Myklebust | c42de9d | 2006-03-20 13:44:51 -0500 | [diff] [blame] | 572 | #else |
Trond Myklebust | fb8a1f1 | 2009-03-11 14:10:29 -0400 | [diff] [blame] | 573 | static inline int nfs_need_commit(struct nfs_inode *nfsi) |
| 574 | { |
| 575 | return 0; |
| 576 | } |
| 577 | |
Trond Myklebust | ca52fec | 2007-04-17 17:22:13 -0400 | [diff] [blame] | 578 | static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, pgoff_t idx_start, unsigned int npages) |
Trond Myklebust | c42de9d | 2006-03-20 13:44:51 -0500 | [diff] [blame] | 579 | { |
| 580 | return 0; |
| 581 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | #endif |
| 583 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | /* |
Trond Myklebust | e7d3906 | 2008-06-13 12:12:32 -0400 | [diff] [blame] | 585 | * Search for an existing write request, and attempt to update |
| 586 | * it to reflect a new dirty region on a given page. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | * |
Trond Myklebust | e7d3906 | 2008-06-13 12:12:32 -0400 | [diff] [blame] | 588 | * If the attempt fails, then the existing request is flushed out |
| 589 | * to disk. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 590 | */ |
Trond Myklebust | e7d3906 | 2008-06-13 12:12:32 -0400 | [diff] [blame] | 591 | static struct nfs_page *nfs_try_to_update_request(struct inode *inode, |
| 592 | struct page *page, |
| 593 | unsigned int offset, |
| 594 | unsigned int bytes) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | { |
Trond Myklebust | e7d3906 | 2008-06-13 12:12:32 -0400 | [diff] [blame] | 596 | struct nfs_page *req; |
| 597 | unsigned int rqend; |
| 598 | unsigned int end; |
| 599 | int error; |
| 600 | |
| 601 | if (!PagePrivate(page)) |
| 602 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | |
| 604 | end = offset + bytes; |
Trond Myklebust | e7d3906 | 2008-06-13 12:12:32 -0400 | [diff] [blame] | 605 | spin_lock(&inode->i_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | for (;;) { |
Trond Myklebust | 277459d | 2006-12-05 00:35:35 -0500 | [diff] [blame] | 608 | req = nfs_page_find_request_locked(page); |
Trond Myklebust | e7d3906 | 2008-06-13 12:12:32 -0400 | [diff] [blame] | 609 | if (req == NULL) |
| 610 | goto out_unlock; |
Trond Myklebust | 277459d | 2006-12-05 00:35:35 -0500 | [diff] [blame] | 611 | |
Trond Myklebust | e7d3906 | 2008-06-13 12:12:32 -0400 | [diff] [blame] | 612 | rqend = req->wb_offset + req->wb_bytes; |
| 613 | /* |
| 614 | * Tell the caller to flush out the request if |
| 615 | * the offsets are non-contiguous. |
| 616 | * Note: nfs_flush_incompatible() will already |
| 617 | * have flushed out requests having wrong owners. |
| 618 | */ |
Trond Myklebust | e468bae | 2008-06-13 13:25:22 -0400 | [diff] [blame] | 619 | if (offset > rqend |
Trond Myklebust | e7d3906 | 2008-06-13 12:12:32 -0400 | [diff] [blame] | 620 | || end < req->wb_offset) |
| 621 | goto out_flushme; |
| 622 | |
| 623 | if (nfs_set_page_tag_locked(req)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | |
Trond Myklebust | e7d3906 | 2008-06-13 12:12:32 -0400 | [diff] [blame] | 626 | /* The request is locked, so wait and then retry */ |
Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 627 | spin_unlock(&inode->i_lock); |
Trond Myklebust | e7d3906 | 2008-06-13 12:12:32 -0400 | [diff] [blame] | 628 | error = nfs_wait_on_request(req); |
| 629 | nfs_release_request(req); |
| 630 | if (error != 0) |
| 631 | goto out_err; |
| 632 | spin_lock(&inode->i_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | } |
| 634 | |
Trond Myklebust | e468bae | 2008-06-13 13:25:22 -0400 | [diff] [blame] | 635 | if (nfs_clear_request_commit(req)) |
| 636 | radix_tree_tag_clear(&NFS_I(inode)->nfs_page_tree, |
| 637 | req->wb_index, NFS_PAGE_TAG_COMMIT); |
| 638 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | /* Okay, the request matches. Update the region */ |
| 640 | if (offset < req->wb_offset) { |
| 641 | req->wb_offset = offset; |
| 642 | req->wb_pgbase = offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 644 | if (end > rqend) |
| 645 | req->wb_bytes = end - req->wb_offset; |
Trond Myklebust | e7d3906 | 2008-06-13 12:12:32 -0400 | [diff] [blame] | 646 | else |
| 647 | req->wb_bytes = rqend - req->wb_offset; |
| 648 | out_unlock: |
| 649 | spin_unlock(&inode->i_lock); |
| 650 | return req; |
| 651 | out_flushme: |
| 652 | spin_unlock(&inode->i_lock); |
| 653 | nfs_release_request(req); |
| 654 | error = nfs_wb_page(inode, page); |
| 655 | out_err: |
| 656 | return ERR_PTR(error); |
| 657 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | |
Trond Myklebust | e7d3906 | 2008-06-13 12:12:32 -0400 | [diff] [blame] | 659 | /* |
| 660 | * Try to update an existing write request, or create one if there is none. |
| 661 | * |
| 662 | * Note: Should always be called with the Page Lock held to prevent races |
| 663 | * if we have to add a new request. Also assumes that the caller has |
| 664 | * already called nfs_flush_incompatible() if necessary. |
| 665 | */ |
| 666 | static struct nfs_page * nfs_setup_write_request(struct nfs_open_context* ctx, |
| 667 | struct page *page, unsigned int offset, unsigned int bytes) |
| 668 | { |
| 669 | struct inode *inode = page->mapping->host; |
| 670 | struct nfs_page *req; |
| 671 | int error; |
| 672 | |
| 673 | req = nfs_try_to_update_request(inode, page, offset, bytes); |
| 674 | if (req != NULL) |
| 675 | goto out; |
| 676 | req = nfs_create_request(ctx, inode, page, offset, bytes); |
| 677 | if (IS_ERR(req)) |
| 678 | goto out; |
| 679 | error = nfs_inode_add_request(inode, req); |
| 680 | if (error != 0) { |
| 681 | nfs_release_request(req); |
| 682 | req = ERR_PTR(error); |
| 683 | } |
Trond Myklebust | efc91ed | 2008-06-10 18:31:00 -0400 | [diff] [blame] | 684 | out: |
Trond Myklebust | 61e930a | 2007-10-18 17:08:05 -0400 | [diff] [blame] | 685 | return req; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 686 | } |
| 687 | |
Trond Myklebust | e7d3906 | 2008-06-13 12:12:32 -0400 | [diff] [blame] | 688 | static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page, |
| 689 | unsigned int offset, unsigned int count) |
| 690 | { |
| 691 | struct nfs_page *req; |
| 692 | |
| 693 | req = nfs_setup_write_request(ctx, page, offset, count); |
| 694 | if (IS_ERR(req)) |
| 695 | return PTR_ERR(req); |
| 696 | /* Update file length */ |
| 697 | nfs_grow_file(page, offset, count); |
| 698 | nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes); |
| 699 | nfs_clear_page_tag_locked(req); |
| 700 | return 0; |
| 701 | } |
| 702 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | int nfs_flush_incompatible(struct file *file, struct page *page) |
| 704 | { |
Trond Myklebust | cd3758e | 2007-08-10 17:44:32 -0400 | [diff] [blame] | 705 | struct nfs_open_context *ctx = nfs_file_open_context(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | struct nfs_page *req; |
Trond Myklebust | 1a54533 | 2006-12-05 00:35:40 -0500 | [diff] [blame] | 707 | int do_flush, status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | /* |
| 709 | * Look for a request corresponding to this page. If there |
| 710 | * is one, and it belongs to another file, we flush it out |
| 711 | * before we try to copy anything into the page. Do this |
| 712 | * due to the lack of an ACCESS-type call in NFSv2. |
| 713 | * Also do the same if we find a request from an existing |
| 714 | * dropped page. |
| 715 | */ |
Trond Myklebust | 1a54533 | 2006-12-05 00:35:40 -0500 | [diff] [blame] | 716 | do { |
| 717 | req = nfs_page_find_request(page); |
| 718 | if (req == NULL) |
| 719 | return 0; |
Trond Myklebust | e468bae | 2008-06-13 13:25:22 -0400 | [diff] [blame] | 720 | do_flush = req->wb_page != page || req->wb_context != ctx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | nfs_release_request(req); |
Trond Myklebust | 1a54533 | 2006-12-05 00:35:40 -0500 | [diff] [blame] | 722 | if (!do_flush) |
| 723 | return 0; |
| 724 | status = nfs_wb_page(page->mapping->host, page); |
| 725 | } while (status == 0); |
| 726 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | /* |
Trond Myklebust | 5d47a35 | 2008-02-07 17:24:07 -0500 | [diff] [blame] | 730 | * If the page cache is marked as unsafe or invalid, then we can't rely on |
| 731 | * the PageUptodate() flag. In this case, we will need to turn off |
| 732 | * write optimisations that depend on the page contents being correct. |
| 733 | */ |
| 734 | static int nfs_write_pageuptodate(struct page *page, struct inode *inode) |
| 735 | { |
| 736 | return PageUptodate(page) && |
| 737 | !(NFS_I(inode)->cache_validity & (NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA)); |
| 738 | } |
| 739 | |
| 740 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | * Update and possibly write a cached page of an NFS file. |
| 742 | * |
| 743 | * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad |
| 744 | * things with a page scheduled for an RPC call (e.g. invalidate it). |
| 745 | */ |
| 746 | int nfs_updatepage(struct file *file, struct page *page, |
| 747 | unsigned int offset, unsigned int count) |
| 748 | { |
Trond Myklebust | cd3758e | 2007-08-10 17:44:32 -0400 | [diff] [blame] | 749 | struct nfs_open_context *ctx = nfs_file_open_context(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | struct inode *inode = page->mapping->host; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | int status = 0; |
| 752 | |
Chuck Lever | 91d5b47 | 2006-03-20 13:44:14 -0500 | [diff] [blame] | 753 | nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE); |
| 754 | |
Chuck Lever | 48186c7 | 2008-06-11 17:56:05 -0400 | [diff] [blame] | 755 | dprintk("NFS: nfs_updatepage(%s/%s %d@%lld)\n", |
Josef "Jeff" Sipek | 01cce93 | 2006-12-08 02:36:40 -0800 | [diff] [blame] | 756 | file->f_path.dentry->d_parent->d_name.name, |
| 757 | file->f_path.dentry->d_name.name, count, |
Chuck Lever | 48186c7 | 2008-06-11 17:56:05 -0400 | [diff] [blame] | 758 | (long long)(page_offset(page) + offset)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | /* If we're not using byte range locks, and we know the page |
Trond Myklebust | 5d47a35 | 2008-02-07 17:24:07 -0500 | [diff] [blame] | 761 | * is up to date, it may be more efficient to extend the write |
| 762 | * to cover the entire page in order to avoid fragmentation |
| 763 | * inefficiencies. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 764 | */ |
Trond Myklebust | 5d47a35 | 2008-02-07 17:24:07 -0500 | [diff] [blame] | 765 | if (nfs_write_pageuptodate(page, inode) && |
| 766 | inode->i_flock == NULL && |
Trond Myklebust | 4b5621f | 2008-02-25 15:56:29 -0800 | [diff] [blame] | 767 | !(file->f_flags & O_SYNC)) { |
Trond Myklebust | 49a70f2 | 2006-12-05 00:35:38 -0500 | [diff] [blame] | 768 | count = max(count + offset, nfs_page_length(page)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | offset = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | } |
| 771 | |
Trond Myklebust | e21195a | 2006-12-05 00:35:39 -0500 | [diff] [blame] | 772 | status = nfs_writepage_setup(ctx, page, offset, count); |
Trond Myklebust | 03fa9e8 | 2008-06-05 16:02:35 -0400 | [diff] [blame] | 773 | if (status < 0) |
| 774 | nfs_set_pageerror(page); |
| 775 | else |
| 776 | __set_page_dirty_nobuffers(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | |
Chuck Lever | 48186c7 | 2008-06-11 17:56:05 -0400 | [diff] [blame] | 778 | dprintk("NFS: nfs_updatepage returns %d (isize %lld)\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | status, (long long)i_size_read(inode)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | return status; |
| 781 | } |
| 782 | |
| 783 | static void nfs_writepage_release(struct nfs_page *req) |
| 784 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | |
Trond Myklebust | 7e5f614 | 2008-05-25 12:59:19 -0400 | [diff] [blame] | 786 | if (PageError(req->wb_page) || !nfs_reschedule_unstable_write(req)) { |
Trond Myklebust | 8e821ca | 2007-04-20 16:12:34 -0400 | [diff] [blame] | 787 | nfs_end_page_writeback(req->wb_page); |
| 788 | nfs_inode_remove_request(req); |
| 789 | } else |
| 790 | nfs_end_page_writeback(req->wb_page); |
Trond Myklebust | 9fd367f | 2007-06-17 15:10:24 -0400 | [diff] [blame] | 791 | nfs_clear_page_tag_locked(req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 792 | } |
| 793 | |
Trond Myklebust | 3ff7576 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 794 | static int flush_task_priority(int how) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 795 | { |
| 796 | switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) { |
| 797 | case FLUSH_HIGHPRI: |
| 798 | return RPC_PRIORITY_HIGH; |
| 799 | case FLUSH_LOWPRI: |
| 800 | return RPC_PRIORITY_LOW; |
| 801 | } |
| 802 | return RPC_PRIORITY_NORMAL; |
| 803 | } |
| 804 | |
| 805 | /* |
| 806 | * Set up the argument/result storage required for the RPC call. |
| 807 | */ |
Trond Myklebust | dbae4c7 | 2008-04-14 14:54:53 -0400 | [diff] [blame] | 808 | static int nfs_write_rpcsetup(struct nfs_page *req, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | struct nfs_write_data *data, |
Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 810 | const struct rpc_call_ops *call_ops, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | unsigned int count, unsigned int offset, |
| 812 | int how) |
| 813 | { |
Trond Myklebust | 84115e1 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 814 | struct inode *inode = req->wb_context->path.dentry->d_inode; |
| 815 | int flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC; |
Trond Myklebust | 3ff7576 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 816 | int priority = flush_task_priority(how); |
Trond Myklebust | 0773769 | 2007-10-25 18:42:54 -0400 | [diff] [blame] | 817 | struct rpc_task *task; |
Trond Myklebust | bdc7f02 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 818 | struct rpc_message msg = { |
| 819 | .rpc_argp = &data->args, |
| 820 | .rpc_resp = &data->res, |
| 821 | .rpc_cred = req->wb_context->cred, |
| 822 | }; |
Trond Myklebust | 84115e1 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 823 | struct rpc_task_setup task_setup_data = { |
| 824 | .rpc_client = NFS_CLIENT(inode), |
Trond Myklebust | 0773769 | 2007-10-25 18:42:54 -0400 | [diff] [blame] | 825 | .task = &data->task, |
Trond Myklebust | bdc7f02 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 826 | .rpc_message = &msg, |
Trond Myklebust | 84115e1 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 827 | .callback_ops = call_ops, |
| 828 | .callback_data = data, |
Trond Myklebust | 101070c | 2008-02-19 20:04:23 -0500 | [diff] [blame] | 829 | .workqueue = nfsiod_workqueue, |
Trond Myklebust | 84115e1 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 830 | .flags = flags, |
Trond Myklebust | 3ff7576 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 831 | .priority = priority, |
Trond Myklebust | 84115e1 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 832 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 833 | |
| 834 | /* Set up the RPC argument and reply structs |
| 835 | * NB: take care not to mess about with data->commit et al. */ |
| 836 | |
| 837 | data->req = req; |
Trond Myklebust | 88be9f9 | 2007-06-05 10:42:27 -0400 | [diff] [blame] | 838 | data->inode = inode = req->wb_context->path.dentry->d_inode; |
Trond Myklebust | bdc7f02 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 839 | data->cred = msg.rpc_cred; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | |
| 841 | data->args.fh = NFS_FH(inode); |
| 842 | data->args.offset = req_offset(req) + offset; |
| 843 | data->args.pgbase = req->wb_pgbase + offset; |
| 844 | data->args.pages = data->pagevec; |
| 845 | data->args.count = count; |
Trond Myklebust | 383ba71 | 2008-02-19 20:04:20 -0500 | [diff] [blame] | 846 | data->args.context = get_nfs_open_context(req->wb_context); |
Trond Myklebust | bdc7f02 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 847 | data->args.stable = NFS_UNSTABLE; |
| 848 | if (how & FLUSH_STABLE) { |
| 849 | data->args.stable = NFS_DATA_SYNC; |
Trond Myklebust | fb8a1f1 | 2009-03-11 14:10:29 -0400 | [diff] [blame] | 850 | if (!nfs_need_commit(NFS_I(inode))) |
Trond Myklebust | bdc7f02 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 851 | data->args.stable = NFS_FILE_SYNC; |
| 852 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | |
| 854 | data->res.fattr = &data->fattr; |
| 855 | data->res.count = count; |
| 856 | data->res.verf = &data->verf; |
Trond Myklebust | 0e574af | 2005-10-27 22:12:38 -0400 | [diff] [blame] | 857 | nfs_fattr_init(&data->fattr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | |
Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 859 | /* Set up the initial task struct. */ |
Trond Myklebust | bdc7f02 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 860 | NFS_PROTO(inode)->write_setup(data, &msg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 861 | |
Chuck Lever | a3f565b | 2007-01-31 12:14:01 -0500 | [diff] [blame] | 862 | dprintk("NFS: %5u initiated write call " |
Chuck Lever | 48186c7 | 2008-06-11 17:56:05 -0400 | [diff] [blame] | 863 | "(req %s/%lld, %u bytes @ offset %llu)\n", |
Chuck Lever | 0bbacc4 | 2005-11-01 16:53:32 -0500 | [diff] [blame] | 864 | data->task.tk_pid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 865 | inode->i_sb->s_id, |
| 866 | (long long)NFS_FILEID(inode), |
| 867 | count, |
| 868 | (unsigned long long)data->args.offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | |
Trond Myklebust | 0773769 | 2007-10-25 18:42:54 -0400 | [diff] [blame] | 870 | task = rpc_run_task(&task_setup_data); |
Trond Myklebust | dbae4c7 | 2008-04-14 14:54:53 -0400 | [diff] [blame] | 871 | if (IS_ERR(task)) |
| 872 | return PTR_ERR(task); |
| 873 | rpc_put_task(task); |
| 874 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | } |
| 876 | |
Fred | 6d884e8 | 2008-03-19 11:24:38 -0400 | [diff] [blame] | 877 | /* If a nfs_flush_* function fails, it should remove reqs from @head and |
| 878 | * call this on each, which will prepare them to be retried on next |
| 879 | * writeback using standard nfs. |
| 880 | */ |
| 881 | static void nfs_redirty_request(struct nfs_page *req) |
| 882 | { |
| 883 | nfs_mark_request_dirty(req); |
| 884 | nfs_end_page_writeback(req->wb_page); |
| 885 | nfs_clear_page_tag_locked(req); |
| 886 | } |
| 887 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 888 | /* |
| 889 | * Generate multiple small requests to write out a single |
| 890 | * contiguous dirty area on one page. |
| 891 | */ |
Trond Myklebust | 8d5658c | 2007-04-10 09:26:35 -0400 | [diff] [blame] | 892 | static int nfs_flush_multi(struct inode *inode, struct list_head *head, unsigned int npages, size_t count, int how) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | { |
| 894 | struct nfs_page *req = nfs_list_entry(head->next); |
| 895 | struct page *page = req->wb_page; |
| 896 | struct nfs_write_data *data; |
Trond Myklebust | e9f7bee | 2006-09-08 09:48:54 -0700 | [diff] [blame] | 897 | size_t wsize = NFS_SERVER(inode)->wsize, nbytes; |
| 898 | unsigned int offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 899 | int requests = 0; |
Trond Myklebust | dbae4c7 | 2008-04-14 14:54:53 -0400 | [diff] [blame] | 900 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | LIST_HEAD(list); |
| 902 | |
| 903 | nfs_list_remove_request(req); |
| 904 | |
Trond Myklebust | bcb71bb | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 905 | nbytes = count; |
Trond Myklebust | e9f7bee | 2006-09-08 09:48:54 -0700 | [diff] [blame] | 906 | do { |
| 907 | size_t len = min(nbytes, wsize); |
| 908 | |
Trond Myklebust | 8d5658c | 2007-04-10 09:26:35 -0400 | [diff] [blame] | 909 | data = nfs_writedata_alloc(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | if (!data) |
| 911 | goto out_bad; |
| 912 | list_add(&data->pages, &list); |
| 913 | requests++; |
Trond Myklebust | e9f7bee | 2006-09-08 09:48:54 -0700 | [diff] [blame] | 914 | nbytes -= len; |
| 915 | } while (nbytes != 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 916 | atomic_set(&req->wb_complete, requests); |
| 917 | |
| 918 | ClearPageError(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | offset = 0; |
Trond Myklebust | bcb71bb | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 920 | nbytes = count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 921 | do { |
Trond Myklebust | dbae4c7 | 2008-04-14 14:54:53 -0400 | [diff] [blame] | 922 | int ret2; |
| 923 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | data = list_entry(list.next, struct nfs_write_data, pages); |
| 925 | list_del_init(&data->pages); |
| 926 | |
| 927 | data->pagevec[0] = page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | |
Trond Myklebust | bcb71bb | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 929 | if (nbytes < wsize) |
| 930 | wsize = nbytes; |
Trond Myklebust | dbae4c7 | 2008-04-14 14:54:53 -0400 | [diff] [blame] | 931 | ret2 = nfs_write_rpcsetup(req, data, &nfs_write_partial_ops, |
Trond Myklebust | bcb71bb | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 932 | wsize, offset, how); |
Trond Myklebust | dbae4c7 | 2008-04-14 14:54:53 -0400 | [diff] [blame] | 933 | if (ret == 0) |
| 934 | ret = ret2; |
Trond Myklebust | bcb71bb | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 935 | offset += wsize; |
| 936 | nbytes -= wsize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | } while (nbytes != 0); |
| 938 | |
Trond Myklebust | dbae4c7 | 2008-04-14 14:54:53 -0400 | [diff] [blame] | 939 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | |
| 941 | out_bad: |
| 942 | while (!list_empty(&list)) { |
| 943 | data = list_entry(list.next, struct nfs_write_data, pages); |
| 944 | list_del(&data->pages); |
Trond Myklebust | 8aca67f | 2006-11-13 16:23:44 -0500 | [diff] [blame] | 945 | nfs_writedata_release(data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | } |
Trond Myklebust | 61822ab | 2006-12-05 00:35:42 -0500 | [diff] [blame] | 947 | nfs_redirty_request(req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | return -ENOMEM; |
| 949 | } |
| 950 | |
| 951 | /* |
| 952 | * Create an RPC task for the given write request and kick it. |
| 953 | * The page must have been locked by the caller. |
| 954 | * |
| 955 | * It may happen that the page we're passed is not marked dirty. |
| 956 | * This is the case if nfs_updatepage detects a conflicting request |
| 957 | * that has been written but not committed. |
| 958 | */ |
Trond Myklebust | 8d5658c | 2007-04-10 09:26:35 -0400 | [diff] [blame] | 959 | static int nfs_flush_one(struct inode *inode, struct list_head *head, unsigned int npages, size_t count, int how) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 960 | { |
| 961 | struct nfs_page *req; |
| 962 | struct page **pages; |
| 963 | struct nfs_write_data *data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | |
Trond Myklebust | 8d5658c | 2007-04-10 09:26:35 -0400 | [diff] [blame] | 965 | data = nfs_writedata_alloc(npages); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 966 | if (!data) |
| 967 | goto out_bad; |
| 968 | |
| 969 | pages = data->pagevec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | while (!list_empty(head)) { |
| 971 | req = nfs_list_entry(head->next); |
| 972 | nfs_list_remove_request(req); |
| 973 | nfs_list_add_request(req, &data->pages); |
| 974 | ClearPageError(req->wb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 975 | *pages++ = req->wb_page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 976 | } |
| 977 | req = nfs_list_entry(data->pages.next); |
| 978 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 979 | /* Set up the argument struct */ |
Trond Myklebust | dbae4c7 | 2008-04-14 14:54:53 -0400 | [diff] [blame] | 980 | return nfs_write_rpcsetup(req, data, &nfs_write_full_ops, count, 0, how); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | out_bad: |
| 982 | while (!list_empty(head)) { |
Trond Myklebust | 10afec9 | 2007-05-14 17:16:04 -0400 | [diff] [blame] | 983 | req = nfs_list_entry(head->next); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 984 | nfs_list_remove_request(req); |
Trond Myklebust | 61822ab | 2006-12-05 00:35:42 -0500 | [diff] [blame] | 985 | nfs_redirty_request(req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 986 | } |
| 987 | return -ENOMEM; |
| 988 | } |
| 989 | |
Trond Myklebust | c63c7b0 | 2007-04-02 19:29:52 -0400 | [diff] [blame] | 990 | static void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio, |
| 991 | struct inode *inode, int ioflags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 992 | { |
Chuck Lever | bf4285e | 2007-12-20 14:54:57 -0500 | [diff] [blame] | 993 | size_t wsize = NFS_SERVER(inode)->wsize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 994 | |
Trond Myklebust | bcb71bb | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 995 | if (wsize < PAGE_CACHE_SIZE) |
Trond Myklebust | c63c7b0 | 2007-04-02 19:29:52 -0400 | [diff] [blame] | 996 | nfs_pageio_init(pgio, inode, nfs_flush_multi, wsize, ioflags); |
Trond Myklebust | bcb71bb | 2007-04-02 18:48:28 -0400 | [diff] [blame] | 997 | else |
Trond Myklebust | c63c7b0 | 2007-04-02 19:29:52 -0400 | [diff] [blame] | 998 | nfs_pageio_init(pgio, inode, nfs_flush_one, wsize, ioflags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | } |
| 1000 | |
| 1001 | /* |
| 1002 | * Handle a write reply that flushed part of a page. |
| 1003 | */ |
Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1004 | static void nfs_writeback_done_partial(struct rpc_task *task, void *calldata) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1005 | { |
Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1006 | struct nfs_write_data *data = calldata; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1007 | |
Chuck Lever | 48186c7 | 2008-06-11 17:56:05 -0400 | [diff] [blame] | 1008 | dprintk("NFS: %5u write(%s/%lld %d@%lld)", |
| 1009 | task->tk_pid, |
| 1010 | data->req->wb_context->path.dentry->d_inode->i_sb->s_id, |
| 1011 | (long long) |
| 1012 | NFS_FILEID(data->req->wb_context->path.dentry->d_inode), |
| 1013 | data->req->wb_bytes, (long long)req_offset(data->req)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1014 | |
Trond Myklebust | c9d8f89 | 2008-04-15 16:56:39 -0400 | [diff] [blame] | 1015 | nfs_writeback_done(task, data); |
| 1016 | } |
Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1017 | |
Trond Myklebust | c9d8f89 | 2008-04-15 16:56:39 -0400 | [diff] [blame] | 1018 | static void nfs_writeback_release_partial(void *calldata) |
| 1019 | { |
| 1020 | struct nfs_write_data *data = calldata; |
| 1021 | struct nfs_page *req = data->req; |
| 1022 | struct page *page = req->wb_page; |
| 1023 | int status = data->task.tk_status; |
| 1024 | |
| 1025 | if (status < 0) { |
Trond Myklebust | a301b77 | 2007-02-06 11:07:15 -0800 | [diff] [blame] | 1026 | nfs_set_pageerror(page); |
Trond Myklebust | c9d8f89 | 2008-04-15 16:56:39 -0400 | [diff] [blame] | 1027 | nfs_context_set_write_error(req->wb_context, status); |
| 1028 | dprintk(", error = %d\n", status); |
Trond Myklebust | 8e821ca | 2007-04-20 16:12:34 -0400 | [diff] [blame] | 1029 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | } |
| 1031 | |
Trond Myklebust | 8e821ca | 2007-04-20 16:12:34 -0400 | [diff] [blame] | 1032 | if (nfs_write_need_commit(data)) { |
Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 1033 | struct inode *inode = page->mapping->host; |
Trond Myklebust | 8e821ca | 2007-04-20 16:12:34 -0400 | [diff] [blame] | 1034 | |
Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 1035 | spin_lock(&inode->i_lock); |
Trond Myklebust | 8e821ca | 2007-04-20 16:12:34 -0400 | [diff] [blame] | 1036 | if (test_bit(PG_NEED_RESCHED, &req->wb_flags)) { |
| 1037 | /* Do nothing we need to resend the writes */ |
| 1038 | } else if (!test_and_set_bit(PG_NEED_COMMIT, &req->wb_flags)) { |
| 1039 | memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf)); |
| 1040 | dprintk(" defer commit\n"); |
| 1041 | } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) { |
| 1042 | set_bit(PG_NEED_RESCHED, &req->wb_flags); |
| 1043 | clear_bit(PG_NEED_COMMIT, &req->wb_flags); |
| 1044 | dprintk(" server reboot detected\n"); |
| 1045 | } |
Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 1046 | spin_unlock(&inode->i_lock); |
Trond Myklebust | 8e821ca | 2007-04-20 16:12:34 -0400 | [diff] [blame] | 1047 | } else |
| 1048 | dprintk(" OK\n"); |
| 1049 | |
| 1050 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1051 | if (atomic_dec_and_test(&req->wb_complete)) |
| 1052 | nfs_writepage_release(req); |
Trond Myklebust | c9d8f89 | 2008-04-15 16:56:39 -0400 | [diff] [blame] | 1053 | nfs_writedata_release(calldata); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1054 | } |
| 1055 | |
Andy Adamson | def6ed7 | 2009-04-01 09:22:26 -0400 | [diff] [blame] | 1056 | #if defined(CONFIG_NFS_V4_1) |
| 1057 | void nfs_write_prepare(struct rpc_task *task, void *calldata) |
| 1058 | { |
| 1059 | struct nfs_write_data *data = calldata; |
| 1060 | struct nfs_client *clp = (NFS_SERVER(data->inode))->nfs_client; |
| 1061 | |
| 1062 | if (nfs4_setup_sequence(clp, &data->args.seq_args, |
| 1063 | &data->res.seq_res, 1, task)) |
| 1064 | return; |
| 1065 | rpc_call_start(task); |
| 1066 | } |
| 1067 | #endif /* CONFIG_NFS_V4_1 */ |
| 1068 | |
Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1069 | static const struct rpc_call_ops nfs_write_partial_ops = { |
Andy Adamson | def6ed7 | 2009-04-01 09:22:26 -0400 | [diff] [blame] | 1070 | #if defined(CONFIG_NFS_V4_1) |
| 1071 | .rpc_call_prepare = nfs_write_prepare, |
| 1072 | #endif /* CONFIG_NFS_V4_1 */ |
Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1073 | .rpc_call_done = nfs_writeback_done_partial, |
Trond Myklebust | c9d8f89 | 2008-04-15 16:56:39 -0400 | [diff] [blame] | 1074 | .rpc_release = nfs_writeback_release_partial, |
Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1075 | }; |
| 1076 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | /* |
| 1078 | * Handle a write reply that flushes a whole page. |
| 1079 | * |
| 1080 | * FIXME: There is an inherent race with invalidate_inode_pages and |
| 1081 | * writebacks since the page->count is kept > 1 for as long |
| 1082 | * as the page has a write request pending. |
| 1083 | */ |
Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1084 | static void nfs_writeback_done_full(struct rpc_task *task, void *calldata) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1085 | { |
Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1086 | struct nfs_write_data *data = calldata; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1087 | |
Trond Myklebust | c9d8f89 | 2008-04-15 16:56:39 -0400 | [diff] [blame] | 1088 | nfs_writeback_done(task, data); |
| 1089 | } |
| 1090 | |
| 1091 | static void nfs_writeback_release_full(void *calldata) |
| 1092 | { |
| 1093 | struct nfs_write_data *data = calldata; |
| 1094 | int status = data->task.tk_status; |
Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1095 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1096 | /* Update attributes as result of writeback. */ |
| 1097 | while (!list_empty(&data->pages)) { |
Trond Myklebust | c9d8f89 | 2008-04-15 16:56:39 -0400 | [diff] [blame] | 1098 | struct nfs_page *req = nfs_list_entry(data->pages.next); |
| 1099 | struct page *page = req->wb_page; |
| 1100 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1101 | nfs_list_remove_request(req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1102 | |
Chuck Lever | 48186c7 | 2008-06-11 17:56:05 -0400 | [diff] [blame] | 1103 | dprintk("NFS: %5u write (%s/%lld %d@%lld)", |
| 1104 | data->task.tk_pid, |
Trond Myklebust | 88be9f9 | 2007-06-05 10:42:27 -0400 | [diff] [blame] | 1105 | req->wb_context->path.dentry->d_inode->i_sb->s_id, |
| 1106 | (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1107 | req->wb_bytes, |
| 1108 | (long long)req_offset(req)); |
| 1109 | |
Trond Myklebust | c9d8f89 | 2008-04-15 16:56:39 -0400 | [diff] [blame] | 1110 | if (status < 0) { |
Trond Myklebust | a301b77 | 2007-02-06 11:07:15 -0800 | [diff] [blame] | 1111 | nfs_set_pageerror(page); |
Trond Myklebust | c9d8f89 | 2008-04-15 16:56:39 -0400 | [diff] [blame] | 1112 | nfs_context_set_write_error(req->wb_context, status); |
| 1113 | dprintk(", error = %d\n", status); |
Trond Myklebust | 8e821ca | 2007-04-20 16:12:34 -0400 | [diff] [blame] | 1114 | goto remove_request; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1115 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1116 | |
Trond Myklebust | 8e821ca | 2007-04-20 16:12:34 -0400 | [diff] [blame] | 1117 | if (nfs_write_need_commit(data)) { |
| 1118 | memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf)); |
| 1119 | nfs_mark_request_commit(req); |
| 1120 | nfs_end_page_writeback(page); |
| 1121 | dprintk(" marked for commit\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1122 | goto next; |
| 1123 | } |
Trond Myklebust | 8e821ca | 2007-04-20 16:12:34 -0400 | [diff] [blame] | 1124 | dprintk(" OK\n"); |
| 1125 | remove_request: |
| 1126 | nfs_end_page_writeback(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1127 | nfs_inode_remove_request(req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1128 | next: |
Trond Myklebust | 9fd367f | 2007-06-17 15:10:24 -0400 | [diff] [blame] | 1129 | nfs_clear_page_tag_locked(req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1130 | } |
Trond Myklebust | c9d8f89 | 2008-04-15 16:56:39 -0400 | [diff] [blame] | 1131 | nfs_writedata_release(calldata); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1132 | } |
| 1133 | |
Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1134 | static const struct rpc_call_ops nfs_write_full_ops = { |
Andy Adamson | def6ed7 | 2009-04-01 09:22:26 -0400 | [diff] [blame] | 1135 | #if defined(CONFIG_NFS_V4_1) |
| 1136 | .rpc_call_prepare = nfs_write_prepare, |
| 1137 | #endif /* CONFIG_NFS_V4_1 */ |
Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1138 | .rpc_call_done = nfs_writeback_done_full, |
Trond Myklebust | c9d8f89 | 2008-04-15 16:56:39 -0400 | [diff] [blame] | 1139 | .rpc_release = nfs_writeback_release_full, |
Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1140 | }; |
| 1141 | |
| 1142 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1143 | /* |
| 1144 | * This function is called when the WRITE call is complete. |
| 1145 | */ |
Chuck Lever | 462d5b3 | 2006-03-20 13:44:32 -0500 | [diff] [blame] | 1146 | 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] | 1147 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1148 | struct nfs_writeargs *argp = &data->args; |
| 1149 | struct nfs_writeres *resp = &data->res; |
Andy Adamson | eedc020 | 2009-04-01 09:22:41 -0400 | [diff] [blame] | 1150 | struct nfs_server *server = NFS_SERVER(data->inode); |
Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1151 | int status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1152 | |
Chuck Lever | a3f565b | 2007-01-31 12:14:01 -0500 | [diff] [blame] | 1153 | dprintk("NFS: %5u nfs_writeback_done (status %d)\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1154 | task->tk_pid, task->tk_status); |
| 1155 | |
Chuck Lever | f551e44 | 2006-09-20 14:33:04 -0400 | [diff] [blame] | 1156 | /* |
| 1157 | * ->write_done will attempt to use post-op attributes to detect |
| 1158 | * conflicting writes by other clients. A strict interpretation |
| 1159 | * of close-to-open would allow us to continue caching even if |
| 1160 | * another writer had changed the file, but some applications |
| 1161 | * depend on tighter cache coherency when writing. |
| 1162 | */ |
Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1163 | status = NFS_PROTO(data->inode)->write_done(task, data); |
| 1164 | if (status != 0) |
| 1165 | return status; |
Chuck Lever | 91d5b47 | 2006-03-20 13:44:14 -0500 | [diff] [blame] | 1166 | nfs_add_stats(data->inode, NFSIOS_SERVERWRITTENBYTES, resp->count); |
| 1167 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1168 | #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) |
| 1169 | if (resp->verf->committed < argp->stable && task->tk_status >= 0) { |
| 1170 | /* We tried a write call, but the server did not |
| 1171 | * commit data to stable storage even though we |
| 1172 | * requested it. |
| 1173 | * Note: There is a known bug in Tru64 < 5.0 in which |
| 1174 | * the server reports NFS_DATA_SYNC, but performs |
| 1175 | * NFS_FILE_SYNC. We therefore implement this checking |
| 1176 | * as a dprintk() in order to avoid filling syslog. |
| 1177 | */ |
| 1178 | static unsigned long complain; |
| 1179 | |
| 1180 | if (time_before(complain, jiffies)) { |
Chuck Lever | 48186c7 | 2008-06-11 17:56:05 -0400 | [diff] [blame] | 1181 | dprintk("NFS: faulty NFS server %s:" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1182 | " (committed = %d) != (stable = %d)\n", |
Andy Adamson | eedc020 | 2009-04-01 09:22:41 -0400 | [diff] [blame] | 1183 | server->nfs_client->cl_hostname, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1184 | resp->verf->committed, argp->stable); |
| 1185 | complain = jiffies + 300 * HZ; |
| 1186 | } |
| 1187 | } |
| 1188 | #endif |
| 1189 | /* Is this a short write? */ |
| 1190 | if (task->tk_status >= 0 && resp->count < argp->count) { |
| 1191 | static unsigned long complain; |
| 1192 | |
Chuck Lever | 91d5b47 | 2006-03-20 13:44:14 -0500 | [diff] [blame] | 1193 | nfs_inc_stats(data->inode, NFSIOS_SHORTWRITE); |
| 1194 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1195 | /* Has the server at least made some progress? */ |
| 1196 | if (resp->count != 0) { |
| 1197 | /* Was this an NFSv2 write or an NFSv3 stable write? */ |
| 1198 | if (resp->verf->committed != NFS_UNSTABLE) { |
| 1199 | /* Resend from where the server left off */ |
| 1200 | argp->offset += resp->count; |
| 1201 | argp->pgbase += resp->count; |
| 1202 | argp->count -= resp->count; |
| 1203 | } else { |
| 1204 | /* Resend as a stable write in order to avoid |
| 1205 | * headaches in the case of a server crash. |
| 1206 | */ |
| 1207 | argp->stable = NFS_FILE_SYNC; |
| 1208 | } |
Andy Adamson | eedc020 | 2009-04-01 09:22:41 -0400 | [diff] [blame] | 1209 | nfs4_restart_rpc(task, server->nfs_client); |
Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1210 | return -EAGAIN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1211 | } |
| 1212 | if (time_before(complain, jiffies)) { |
| 1213 | printk(KERN_WARNING |
| 1214 | "NFS: Server wrote zero bytes, expected %u.\n", |
| 1215 | argp->count); |
| 1216 | complain = jiffies + 300 * HZ; |
| 1217 | } |
| 1218 | /* Can't do anything about it except throw an error. */ |
| 1219 | task->tk_status = -EIO; |
| 1220 | } |
Andy Adamson | eedc020 | 2009-04-01 09:22:41 -0400 | [diff] [blame] | 1221 | nfs4_sequence_free_slot(server->nfs_client, &data->res.seq_res); |
Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1222 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1223 | } |
| 1224 | |
| 1225 | |
| 1226 | #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) |
Trond Myklebust | c9d8f89 | 2008-04-15 16:56:39 -0400 | [diff] [blame] | 1227 | void nfs_commitdata_release(void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1228 | { |
Trond Myklebust | 383ba71 | 2008-02-19 20:04:20 -0500 | [diff] [blame] | 1229 | struct nfs_write_data *wdata = data; |
| 1230 | |
| 1231 | put_nfs_open_context(wdata->args.context); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1232 | nfs_commit_free(wdata); |
| 1233 | } |
| 1234 | |
| 1235 | /* |
| 1236 | * Set up the argument/result storage required for the RPC call. |
| 1237 | */ |
Trond Myklebust | dbae4c7 | 2008-04-14 14:54:53 -0400 | [diff] [blame] | 1238 | static int nfs_commit_rpcsetup(struct list_head *head, |
Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1239 | struct nfs_write_data *data, |
| 1240 | int how) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1241 | { |
Trond Myklebust | 84115e1 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 1242 | struct nfs_page *first = nfs_list_entry(head->next); |
| 1243 | struct inode *inode = first->wb_context->path.dentry->d_inode; |
| 1244 | int flags = (how & FLUSH_SYNC) ? 0 : RPC_TASK_ASYNC; |
Trond Myklebust | 3ff7576 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 1245 | int priority = flush_task_priority(how); |
Trond Myklebust | 0773769 | 2007-10-25 18:42:54 -0400 | [diff] [blame] | 1246 | struct rpc_task *task; |
Trond Myklebust | bdc7f02 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 1247 | struct rpc_message msg = { |
| 1248 | .rpc_argp = &data->args, |
| 1249 | .rpc_resp = &data->res, |
| 1250 | .rpc_cred = first->wb_context->cred, |
| 1251 | }; |
Trond Myklebust | 84115e1 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 1252 | struct rpc_task_setup task_setup_data = { |
Trond Myklebust | 0773769 | 2007-10-25 18:42:54 -0400 | [diff] [blame] | 1253 | .task = &data->task, |
Trond Myklebust | 84115e1 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 1254 | .rpc_client = NFS_CLIENT(inode), |
Trond Myklebust | bdc7f02 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 1255 | .rpc_message = &msg, |
Trond Myklebust | 84115e1 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 1256 | .callback_ops = &nfs_commit_ops, |
| 1257 | .callback_data = data, |
Trond Myklebust | 101070c | 2008-02-19 20:04:23 -0500 | [diff] [blame] | 1258 | .workqueue = nfsiod_workqueue, |
Trond Myklebust | 84115e1 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 1259 | .flags = flags, |
Trond Myklebust | 3ff7576 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 1260 | .priority = priority, |
Trond Myklebust | 84115e1 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 1261 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1262 | |
| 1263 | /* Set up the RPC argument and reply structs |
| 1264 | * NB: take care not to mess about with data->commit et al. */ |
| 1265 | |
| 1266 | list_splice_init(head, &data->pages); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1267 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1268 | data->inode = inode; |
Trond Myklebust | bdc7f02 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 1269 | data->cred = msg.rpc_cred; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1270 | |
| 1271 | data->args.fh = NFS_FH(data->inode); |
Trond Myklebust | 3da28eb | 2005-06-22 17:16:31 +0000 | [diff] [blame] | 1272 | /* Note: we always request a commit of the entire inode */ |
| 1273 | data->args.offset = 0; |
| 1274 | data->args.count = 0; |
Trond Myklebust | 383ba71 | 2008-02-19 20:04:20 -0500 | [diff] [blame] | 1275 | data->args.context = get_nfs_open_context(first->wb_context); |
Trond Myklebust | 3da28eb | 2005-06-22 17:16:31 +0000 | [diff] [blame] | 1276 | data->res.count = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1277 | data->res.fattr = &data->fattr; |
| 1278 | data->res.verf = &data->verf; |
Trond Myklebust | 0e574af | 2005-10-27 22:12:38 -0400 | [diff] [blame] | 1279 | nfs_fattr_init(&data->fattr); |
Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1280 | |
| 1281 | /* Set up the initial task struct. */ |
Trond Myklebust | bdc7f02 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 1282 | NFS_PROTO(inode)->commit_setup(data, &msg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1283 | |
Chuck Lever | a3f565b | 2007-01-31 12:14:01 -0500 | [diff] [blame] | 1284 | dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid); |
Trond Myklebust | bdc7f02 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 1285 | |
Trond Myklebust | 0773769 | 2007-10-25 18:42:54 -0400 | [diff] [blame] | 1286 | task = rpc_run_task(&task_setup_data); |
Trond Myklebust | dbae4c7 | 2008-04-14 14:54:53 -0400 | [diff] [blame] | 1287 | if (IS_ERR(task)) |
| 1288 | return PTR_ERR(task); |
| 1289 | rpc_put_task(task); |
| 1290 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1291 | } |
| 1292 | |
| 1293 | /* |
| 1294 | * Commit dirty pages |
| 1295 | */ |
| 1296 | static int |
Chuck Lever | 40859d7 | 2005-11-30 18:09:02 -0500 | [diff] [blame] | 1297 | nfs_commit_list(struct inode *inode, struct list_head *head, int how) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1298 | { |
| 1299 | struct nfs_write_data *data; |
| 1300 | struct nfs_page *req; |
| 1301 | |
Trond Myklebust | c9d8f89 | 2008-04-15 16:56:39 -0400 | [diff] [blame] | 1302 | data = nfs_commitdata_alloc(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1303 | |
| 1304 | if (!data) |
| 1305 | goto out_bad; |
| 1306 | |
| 1307 | /* Set up the argument struct */ |
Trond Myklebust | dbae4c7 | 2008-04-14 14:54:53 -0400 | [diff] [blame] | 1308 | return nfs_commit_rpcsetup(head, data, how); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1309 | out_bad: |
| 1310 | while (!list_empty(head)) { |
| 1311 | req = nfs_list_entry(head->next); |
| 1312 | nfs_list_remove_request(req); |
| 1313 | nfs_mark_request_commit(req); |
Trond Myklebust | 83715ad | 2006-07-05 13:17:12 -0400 | [diff] [blame] | 1314 | dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS); |
Peter Zijlstra | c9e51e4 | 2007-10-16 23:25:47 -0700 | [diff] [blame] | 1315 | dec_bdi_stat(req->wb_page->mapping->backing_dev_info, |
| 1316 | BDI_RECLAIMABLE); |
Trond Myklebust | 9fd367f | 2007-06-17 15:10:24 -0400 | [diff] [blame] | 1317 | nfs_clear_page_tag_locked(req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1318 | } |
| 1319 | return -ENOMEM; |
| 1320 | } |
| 1321 | |
| 1322 | /* |
| 1323 | * COMMIT call returned |
| 1324 | */ |
Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1325 | static void nfs_commit_done(struct rpc_task *task, void *calldata) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1326 | { |
Trond Myklebust | 963d8fe | 2006-01-03 09:55:04 +0100 | [diff] [blame] | 1327 | struct nfs_write_data *data = calldata; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1328 | |
Chuck Lever | a3f565b | 2007-01-31 12:14:01 -0500 | [diff] [blame] | 1329 | dprintk("NFS: %5u nfs_commit_done (status %d)\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1330 | task->tk_pid, task->tk_status); |
| 1331 | |
Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1332 | /* Call the NFS version-specific code */ |
| 1333 | if (NFS_PROTO(data->inode)->commit_done(task, data) != 0) |
| 1334 | return; |
Trond Myklebust | c9d8f89 | 2008-04-15 16:56:39 -0400 | [diff] [blame] | 1335 | } |
| 1336 | |
| 1337 | static void nfs_commit_release(void *calldata) |
| 1338 | { |
| 1339 | struct nfs_write_data *data = calldata; |
| 1340 | struct nfs_page *req; |
| 1341 | int status = data->task.tk_status; |
Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1342 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1343 | while (!list_empty(&data->pages)) { |
| 1344 | req = nfs_list_entry(data->pages.next); |
| 1345 | nfs_list_remove_request(req); |
Trond Myklebust | e468bae | 2008-06-13 13:25:22 -0400 | [diff] [blame] | 1346 | nfs_clear_request_commit(req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1347 | |
Chuck Lever | 48186c7 | 2008-06-11 17:56:05 -0400 | [diff] [blame] | 1348 | dprintk("NFS: commit (%s/%lld %d@%lld)", |
Trond Myklebust | 88be9f9 | 2007-06-05 10:42:27 -0400 | [diff] [blame] | 1349 | req->wb_context->path.dentry->d_inode->i_sb->s_id, |
| 1350 | (long long)NFS_FILEID(req->wb_context->path.dentry->d_inode), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1351 | req->wb_bytes, |
| 1352 | (long long)req_offset(req)); |
Trond Myklebust | c9d8f89 | 2008-04-15 16:56:39 -0400 | [diff] [blame] | 1353 | if (status < 0) { |
| 1354 | nfs_context_set_write_error(req->wb_context, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1355 | nfs_inode_remove_request(req); |
Trond Myklebust | c9d8f89 | 2008-04-15 16:56:39 -0400 | [diff] [blame] | 1356 | dprintk(", error = %d\n", status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1357 | goto next; |
| 1358 | } |
| 1359 | |
| 1360 | /* Okay, COMMIT succeeded, apparently. Check the verifier |
| 1361 | * returned by the server against all stored verfs. */ |
| 1362 | if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) { |
| 1363 | /* We have a match */ |
| 1364 | nfs_inode_remove_request(req); |
| 1365 | dprintk(" OK\n"); |
| 1366 | goto next; |
| 1367 | } |
| 1368 | /* We have a mismatch. Write the page again */ |
| 1369 | dprintk(" mismatch\n"); |
Fred | 6d884e8 | 2008-03-19 11:24:38 -0400 | [diff] [blame] | 1370 | nfs_mark_request_dirty(req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1371 | next: |
Trond Myklebust | 9fd367f | 2007-06-17 15:10:24 -0400 | [diff] [blame] | 1372 | nfs_clear_page_tag_locked(req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1373 | } |
Trond Myklebust | c9d8f89 | 2008-04-15 16:56:39 -0400 | [diff] [blame] | 1374 | nfs_commitdata_release(calldata); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1375 | } |
Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1376 | |
| 1377 | static const struct rpc_call_ops nfs_commit_ops = { |
Andy Adamson | 21d9a85 | 2009-04-01 09:22:27 -0400 | [diff] [blame] | 1378 | #if defined(CONFIG_NFS_V4_1) |
| 1379 | .rpc_call_prepare = nfs_write_prepare, |
| 1380 | #endif /* CONFIG_NFS_V4_1 */ |
Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1381 | .rpc_call_done = nfs_commit_done, |
| 1382 | .rpc_release = nfs_commit_release, |
| 1383 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1384 | |
Trond Myklebust | 3da28eb | 2005-06-22 17:16:31 +0000 | [diff] [blame] | 1385 | int nfs_commit_inode(struct inode *inode, int how) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1386 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1387 | LIST_HEAD(head); |
Trond Myklebust | 7d46a49 | 2006-03-20 13:44:50 -0500 | [diff] [blame] | 1388 | int res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1389 | |
Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 1390 | spin_lock(&inode->i_lock); |
Trond Myklebust | 3da28eb | 2005-06-22 17:16:31 +0000 | [diff] [blame] | 1391 | res = nfs_scan_commit(inode, &head, 0, 0); |
Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 1392 | spin_unlock(&inode->i_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1393 | if (res) { |
Trond Myklebust | 7d46a49 | 2006-03-20 13:44:50 -0500 | [diff] [blame] | 1394 | int error = nfs_commit_list(inode, &head, how); |
Trond Myklebust | 3da28eb | 2005-06-22 17:16:31 +0000 | [diff] [blame] | 1395 | if (error < 0) |
| 1396 | return error; |
| 1397 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1398 | return res; |
| 1399 | } |
Trond Myklebust | c63c7b0 | 2007-04-02 19:29:52 -0400 | [diff] [blame] | 1400 | #else |
| 1401 | static inline int nfs_commit_list(struct inode *inode, struct list_head *head, int how) |
| 1402 | { |
| 1403 | return 0; |
| 1404 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1405 | #endif |
| 1406 | |
Trond Myklebust | 1c75950 | 2006-10-09 16:18:38 -0400 | [diff] [blame] | 1407 | 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] | 1408 | { |
Trond Myklebust | 1c75950 | 2006-10-09 16:18:38 -0400 | [diff] [blame] | 1409 | struct inode *inode = mapping->host; |
Trond Myklebust | ca52fec | 2007-04-17 17:22:13 -0400 | [diff] [blame] | 1410 | pgoff_t idx_start, idx_end; |
Trond Myklebust | 1c75950 | 2006-10-09 16:18:38 -0400 | [diff] [blame] | 1411 | unsigned int npages = 0; |
Trond Myklebust | c42de9d | 2006-03-20 13:44:51 -0500 | [diff] [blame] | 1412 | LIST_HEAD(head); |
Trond Myklebust | 70b9ecb | 2006-01-03 09:55:34 +0100 | [diff] [blame] | 1413 | int nocommit = how & FLUSH_NOCOMMIT; |
Trond Myklebust | 3f44254 | 2006-09-17 14:46:44 -0400 | [diff] [blame] | 1414 | long pages, ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1415 | |
Trond Myklebust | 1c75950 | 2006-10-09 16:18:38 -0400 | [diff] [blame] | 1416 | /* FIXME */ |
| 1417 | if (wbc->range_cyclic) |
| 1418 | idx_start = 0; |
| 1419 | else { |
| 1420 | idx_start = wbc->range_start >> PAGE_CACHE_SHIFT; |
| 1421 | idx_end = wbc->range_end >> PAGE_CACHE_SHIFT; |
| 1422 | if (idx_end > idx_start) { |
Trond Myklebust | ca52fec | 2007-04-17 17:22:13 -0400 | [diff] [blame] | 1423 | pgoff_t l_npages = 1 + idx_end - idx_start; |
Trond Myklebust | 1c75950 | 2006-10-09 16:18:38 -0400 | [diff] [blame] | 1424 | npages = l_npages; |
| 1425 | if (sizeof(npages) != sizeof(l_npages) && |
Trond Myklebust | ca52fec | 2007-04-17 17:22:13 -0400 | [diff] [blame] | 1426 | (pgoff_t)npages != l_npages) |
Trond Myklebust | 1c75950 | 2006-10-09 16:18:38 -0400 | [diff] [blame] | 1427 | npages = 0; |
| 1428 | } |
| 1429 | } |
Trond Myklebust | c42de9d | 2006-03-20 13:44:51 -0500 | [diff] [blame] | 1430 | how &= ~FLUSH_NOCOMMIT; |
Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 1431 | spin_lock(&inode->i_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1432 | do { |
Trond Myklebust | c42de9d | 2006-03-20 13:44:51 -0500 | [diff] [blame] | 1433 | ret = nfs_wait_on_requests_locked(inode, idx_start, npages); |
| 1434 | if (ret != 0) |
Trond Myklebust | 70b9ecb | 2006-01-03 09:55:34 +0100 | [diff] [blame] | 1435 | continue; |
Trond Myklebust | c42de9d | 2006-03-20 13:44:51 -0500 | [diff] [blame] | 1436 | if (nocommit) |
| 1437 | break; |
Trond Myklebust | d2ccddf | 2006-05-31 01:13:38 -0400 | [diff] [blame] | 1438 | pages = nfs_scan_commit(inode, &head, idx_start, npages); |
Trond Myklebust | 724c439 | 2007-04-17 17:22:13 -0400 | [diff] [blame] | 1439 | if (pages == 0) |
Trond Myklebust | c42de9d | 2006-03-20 13:44:51 -0500 | [diff] [blame] | 1440 | break; |
Trond Myklebust | d2ccddf | 2006-05-31 01:13:38 -0400 | [diff] [blame] | 1441 | if (how & FLUSH_INVALIDATE) { |
Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 1442 | spin_unlock(&inode->i_lock); |
Trond Myklebust | 83715ad | 2006-07-05 13:17:12 -0400 | [diff] [blame] | 1443 | nfs_cancel_commit_list(&head); |
Trond Myklebust | e8e058e | 2006-11-15 17:31:56 -0500 | [diff] [blame] | 1444 | ret = pages; |
Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 1445 | spin_lock(&inode->i_lock); |
Trond Myklebust | d2ccddf | 2006-05-31 01:13:38 -0400 | [diff] [blame] | 1446 | continue; |
| 1447 | } |
| 1448 | pages += nfs_scan_commit(inode, &head, 0, 0); |
Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 1449 | spin_unlock(&inode->i_lock); |
Trond Myklebust | c42de9d | 2006-03-20 13:44:51 -0500 | [diff] [blame] | 1450 | ret = nfs_commit_list(inode, &head, how); |
Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 1451 | spin_lock(&inode->i_lock); |
| 1452 | |
Trond Myklebust | c42de9d | 2006-03-20 13:44:51 -0500 | [diff] [blame] | 1453 | } while (ret >= 0); |
Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 1454 | spin_unlock(&inode->i_lock); |
Trond Myklebust | c42de9d | 2006-03-20 13:44:51 -0500 | [diff] [blame] | 1455 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1456 | } |
| 1457 | |
Trond Myklebust | 34901f7 | 2007-07-25 14:09:54 -0400 | [diff] [blame] | 1458 | static int __nfs_write_mapping(struct address_space *mapping, struct writeback_control *wbc, int how) |
Trond Myklebust | 1c75950 | 2006-10-09 16:18:38 -0400 | [diff] [blame] | 1459 | { |
Trond Myklebust | 1c75950 | 2006-10-09 16:18:38 -0400 | [diff] [blame] | 1460 | int ret; |
| 1461 | |
Trond Myklebust | 34901f7 | 2007-07-25 14:09:54 -0400 | [diff] [blame] | 1462 | ret = nfs_writepages(mapping, wbc); |
Trond Myklebust | 61822ab | 2006-12-05 00:35:42 -0500 | [diff] [blame] | 1463 | if (ret < 0) |
| 1464 | goto out; |
Trond Myklebust | 34901f7 | 2007-07-25 14:09:54 -0400 | [diff] [blame] | 1465 | ret = nfs_sync_mapping_wait(mapping, wbc, how); |
Trond Myklebust | ed90ef5 | 2007-07-20 13:13:28 -0400 | [diff] [blame] | 1466 | if (ret < 0) |
| 1467 | goto out; |
| 1468 | return 0; |
Trond Myklebust | 61822ab | 2006-12-05 00:35:42 -0500 | [diff] [blame] | 1469 | out: |
Trond Myklebust | e507d9e | 2006-12-05 00:35:42 -0500 | [diff] [blame] | 1470 | __mark_inode_dirty(mapping->host, I_DIRTY_PAGES); |
Trond Myklebust | 1c75950 | 2006-10-09 16:18:38 -0400 | [diff] [blame] | 1471 | return ret; |
| 1472 | } |
| 1473 | |
Trond Myklebust | 34901f7 | 2007-07-25 14:09:54 -0400 | [diff] [blame] | 1474 | /* Two pass sync: first using WB_SYNC_NONE, then WB_SYNC_ALL */ |
| 1475 | static int nfs_write_mapping(struct address_space *mapping, int how) |
| 1476 | { |
| 1477 | struct writeback_control wbc = { |
| 1478 | .bdi = mapping->backing_dev_info, |
Trond Myklebust | 72cb77f | 2009-03-11 14:10:30 -0400 | [diff] [blame] | 1479 | .sync_mode = WB_SYNC_ALL, |
Trond Myklebust | 34901f7 | 2007-07-25 14:09:54 -0400 | [diff] [blame] | 1480 | .nr_to_write = LONG_MAX, |
Trond Myklebust | d7fb120 | 2008-10-06 20:08:56 -0400 | [diff] [blame] | 1481 | .range_start = 0, |
| 1482 | .range_end = LLONG_MAX, |
Trond Myklebust | 34901f7 | 2007-07-25 14:09:54 -0400 | [diff] [blame] | 1483 | .for_writepages = 1, |
Trond Myklebust | 34901f7 | 2007-07-25 14:09:54 -0400 | [diff] [blame] | 1484 | }; |
Trond Myklebust | 34901f7 | 2007-07-25 14:09:54 -0400 | [diff] [blame] | 1485 | |
Trond Myklebust | 34901f7 | 2007-07-25 14:09:54 -0400 | [diff] [blame] | 1486 | return __nfs_write_mapping(mapping, &wbc, how); |
| 1487 | } |
| 1488 | |
Trond Myklebust | ed90ef5 | 2007-07-20 13:13:28 -0400 | [diff] [blame] | 1489 | /* |
| 1490 | * flush the inode to disk. |
| 1491 | */ |
| 1492 | int nfs_wb_all(struct inode *inode) |
Trond Myklebust | 1c75950 | 2006-10-09 16:18:38 -0400 | [diff] [blame] | 1493 | { |
Trond Myklebust | ed90ef5 | 2007-07-20 13:13:28 -0400 | [diff] [blame] | 1494 | return nfs_write_mapping(inode->i_mapping, 0); |
| 1495 | } |
Trond Myklebust | 1c75950 | 2006-10-09 16:18:38 -0400 | [diff] [blame] | 1496 | |
Trond Myklebust | ed90ef5 | 2007-07-20 13:13:28 -0400 | [diff] [blame] | 1497 | int nfs_wb_nocommit(struct inode *inode) |
| 1498 | { |
| 1499 | return nfs_write_mapping(inode->i_mapping, FLUSH_NOCOMMIT); |
Trond Myklebust | 1c75950 | 2006-10-09 16:18:38 -0400 | [diff] [blame] | 1500 | } |
| 1501 | |
Trond Myklebust | 1b3b4a1 | 2007-08-28 10:29:36 -0400 | [diff] [blame] | 1502 | int nfs_wb_page_cancel(struct inode *inode, struct page *page) |
| 1503 | { |
| 1504 | struct nfs_page *req; |
| 1505 | loff_t range_start = page_offset(page); |
| 1506 | loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1); |
| 1507 | struct writeback_control wbc = { |
| 1508 | .bdi = page->mapping->backing_dev_info, |
| 1509 | .sync_mode = WB_SYNC_ALL, |
| 1510 | .nr_to_write = LONG_MAX, |
| 1511 | .range_start = range_start, |
| 1512 | .range_end = range_end, |
| 1513 | }; |
| 1514 | int ret = 0; |
| 1515 | |
| 1516 | BUG_ON(!PageLocked(page)); |
| 1517 | for (;;) { |
| 1518 | req = nfs_page_find_request(page); |
| 1519 | if (req == NULL) |
| 1520 | goto out; |
Trond Myklebust | e468bae | 2008-06-13 13:25:22 -0400 | [diff] [blame] | 1521 | if (test_bit(PG_CLEAN, &req->wb_flags)) { |
Trond Myklebust | 1b3b4a1 | 2007-08-28 10:29:36 -0400 | [diff] [blame] | 1522 | nfs_release_request(req); |
| 1523 | break; |
| 1524 | } |
| 1525 | if (nfs_lock_request_dontget(req)) { |
| 1526 | nfs_inode_remove_request(req); |
| 1527 | /* |
| 1528 | * In case nfs_inode_remove_request has marked the |
| 1529 | * page as being dirty |
| 1530 | */ |
| 1531 | cancel_dirty_page(page, PAGE_CACHE_SIZE); |
| 1532 | nfs_unlock_request(req); |
| 1533 | break; |
| 1534 | } |
| 1535 | ret = nfs_wait_on_request(req); |
| 1536 | if (ret < 0) |
| 1537 | goto out; |
| 1538 | } |
| 1539 | if (!PagePrivate(page)) |
| 1540 | return 0; |
| 1541 | ret = nfs_sync_mapping_wait(page->mapping, &wbc, FLUSH_INVALIDATE); |
| 1542 | out: |
| 1543 | return ret; |
| 1544 | } |
| 1545 | |
Adrian Bunk | 5334eb1 | 2007-11-21 15:04:31 -0800 | [diff] [blame] | 1546 | static int nfs_wb_page_priority(struct inode *inode, struct page *page, |
| 1547 | int how) |
Trond Myklebust | 1c75950 | 2006-10-09 16:18:38 -0400 | [diff] [blame] | 1548 | { |
| 1549 | loff_t range_start = page_offset(page); |
| 1550 | loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1); |
Trond Myklebust | 4d770cc | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 1551 | struct writeback_control wbc = { |
| 1552 | .bdi = page->mapping->backing_dev_info, |
| 1553 | .sync_mode = WB_SYNC_ALL, |
| 1554 | .nr_to_write = LONG_MAX, |
| 1555 | .range_start = range_start, |
| 1556 | .range_end = range_end, |
| 1557 | }; |
| 1558 | int ret; |
Trond Myklebust | 1c75950 | 2006-10-09 16:18:38 -0400 | [diff] [blame] | 1559 | |
Trond Myklebust | 73e3302 | 2008-04-11 16:03:54 -0400 | [diff] [blame] | 1560 | do { |
| 1561 | if (clear_page_dirty_for_io(page)) { |
| 1562 | ret = nfs_writepage_locked(page, &wbc); |
| 1563 | if (ret < 0) |
| 1564 | goto out_error; |
| 1565 | } else if (!PagePrivate(page)) |
| 1566 | break; |
| 1567 | ret = nfs_sync_mapping_wait(page->mapping, &wbc, how); |
Trond Myklebust | 4d770cc | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 1568 | if (ret < 0) |
Trond Myklebust | 73e3302 | 2008-04-11 16:03:54 -0400 | [diff] [blame] | 1569 | goto out_error; |
| 1570 | } while (PagePrivate(page)); |
| 1571 | return 0; |
| 1572 | out_error: |
Trond Myklebust | e507d9e | 2006-12-05 00:35:42 -0500 | [diff] [blame] | 1573 | __mark_inode_dirty(inode, I_DIRTY_PAGES); |
Trond Myklebust | 4d770cc | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 1574 | return ret; |
Trond Myklebust | 1c75950 | 2006-10-09 16:18:38 -0400 | [diff] [blame] | 1575 | } |
| 1576 | |
| 1577 | /* |
| 1578 | * Write back all requests on one page - we do this before reading it. |
| 1579 | */ |
| 1580 | int nfs_wb_page(struct inode *inode, struct page* page) |
| 1581 | { |
Trond Myklebust | 4d770cc | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 1582 | return nfs_wb_page_priority(inode, page, FLUSH_STABLE); |
Trond Myklebust | 1c75950 | 2006-10-09 16:18:38 -0400 | [diff] [blame] | 1583 | } |
| 1584 | |
David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 1585 | int __init nfs_init_writepagecache(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1586 | { |
| 1587 | nfs_wdata_cachep = kmem_cache_create("nfs_write_data", |
| 1588 | sizeof(struct nfs_write_data), |
| 1589 | 0, SLAB_HWCACHE_ALIGN, |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 1590 | NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1591 | if (nfs_wdata_cachep == NULL) |
| 1592 | return -ENOMEM; |
| 1593 | |
Matthew Dobson | 93d2341 | 2006-03-26 01:37:50 -0800 | [diff] [blame] | 1594 | nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE, |
| 1595 | nfs_wdata_cachep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1596 | if (nfs_wdata_mempool == NULL) |
| 1597 | return -ENOMEM; |
| 1598 | |
Matthew Dobson | 93d2341 | 2006-03-26 01:37:50 -0800 | [diff] [blame] | 1599 | nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT, |
| 1600 | nfs_wdata_cachep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1601 | if (nfs_commit_mempool == NULL) |
| 1602 | return -ENOMEM; |
| 1603 | |
Peter Zijlstra | 89a0914 | 2007-03-16 13:38:26 -0800 | [diff] [blame] | 1604 | /* |
| 1605 | * NFS congestion size, scale with available memory. |
| 1606 | * |
| 1607 | * 64MB: 8192k |
| 1608 | * 128MB: 11585k |
| 1609 | * 256MB: 16384k |
| 1610 | * 512MB: 23170k |
| 1611 | * 1GB: 32768k |
| 1612 | * 2GB: 46340k |
| 1613 | * 4GB: 65536k |
| 1614 | * 8GB: 92681k |
| 1615 | * 16GB: 131072k |
| 1616 | * |
| 1617 | * This allows larger machines to have larger/more transfers. |
| 1618 | * Limit the default to 256M |
| 1619 | */ |
| 1620 | nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10); |
| 1621 | if (nfs_congestion_kb > 256*1024) |
| 1622 | nfs_congestion_kb = 256*1024; |
| 1623 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1624 | return 0; |
| 1625 | } |
| 1626 | |
David Brownell | 266bee8 | 2006-06-27 12:59:15 -0700 | [diff] [blame] | 1627 | void nfs_destroy_writepagecache(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1628 | { |
| 1629 | mempool_destroy(nfs_commit_mempool); |
| 1630 | mempool_destroy(nfs_wdata_mempool); |
Alexey Dobriyan | 1a1d92c | 2006-09-27 01:49:40 -0700 | [diff] [blame] | 1631 | kmem_cache_destroy(nfs_wdata_cachep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1632 | } |
| 1633 | |