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