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> |
Trond Myklebust | 074cc1d | 2009-08-10 08:54:13 -0400 | [diff] [blame] | 16 | #include <linux/migrate.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | |
| 18 | #include <linux/sunrpc/clnt.h> |
| 19 | #include <linux/nfs_fs.h> |
| 20 | #include <linux/nfs_mount.h> |
| 21 | #include <linux/nfs_page.h> |
Andrew Morton | 3fcfab1 | 2006-10-19 23:28:16 -0700 | [diff] [blame] | 22 | #include <linux/backing-dev.h> |
Paul Gortmaker | afeacc8 | 2011-05-26 16:00:52 -0400 | [diff] [blame] | 23 | #include <linux/export.h> |
Andrew Morton | 3fcfab1 | 2006-10-19 23:28:16 -0700 | [diff] [blame] | 24 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <asm/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
| 27 | #include "delegation.h" |
Trond Myklebust | 49a70f2 | 2006-12-05 00:35:38 -0500 | [diff] [blame] | 28 | #include "internal.h" |
Chuck Lever | 91d5b47 | 2006-03-20 13:44:14 -0500 | [diff] [blame] | 29 | #include "iostat.h" |
Andy Adamson | def6ed7 | 2009-04-01 09:22:26 -0400 | [diff] [blame] | 30 | #include "nfs4_fs.h" |
Trond Myklebust | 074cc1d | 2009-08-10 08:54:13 -0400 | [diff] [blame] | 31 | #include "fscache.h" |
Fred Isaman | 94ad1c8 | 2011-03-01 01:34:14 +0000 | [diff] [blame] | 32 | #include "pnfs.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
| 34 | #define NFSDBG_FACILITY NFSDBG_PAGECACHE |
| 35 | |
| 36 | #define MIN_POOL_WRITE (32) |
| 37 | #define MIN_POOL_COMMIT (4) |
| 38 | |
| 39 | /* |
| 40 | * Local function declarations |
| 41 | */ |
Fred Isaman | f8512ad | 2008-03-19 11:24:39 -0400 | [diff] [blame] | 42 | static void nfs_redirty_request(struct nfs_page *req); |
Fred Isaman | 6c75dc0 | 2012-04-20 14:47:47 -0400 | [diff] [blame] | 43 | static const struct rpc_call_ops nfs_write_common_ops; |
Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 44 | static const struct rpc_call_ops nfs_commit_ops; |
Fred Isaman | 061ae2e | 2012-04-20 14:47:48 -0400 | [diff] [blame] | 45 | static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops; |
Fred Isaman | f453a54 | 2012-04-20 14:47:54 -0400 | [diff] [blame] | 46 | static const struct nfs_commit_completion_ops nfs_commit_completion_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 48 | static struct kmem_cache *nfs_wdata_cachep; |
Trond Myklebust | 3feb2d4 | 2006-03-20 13:44:37 -0500 | [diff] [blame] | 49 | static mempool_t *nfs_wdata_mempool; |
Fred Isaman | 0b7c015 | 2012-04-20 14:47:39 -0400 | [diff] [blame] | 50 | static struct kmem_cache *nfs_cdata_cachep; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | static mempool_t *nfs_commit_mempool; |
| 52 | |
Fred Isaman | 0b7c015 | 2012-04-20 14:47:39 -0400 | [diff] [blame] | 53 | struct nfs_commit_data *nfs_commitdata_alloc(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | { |
Mel Gorman | 192e501 | 2012-07-31 16:45:16 -0700 | [diff] [blame^] | 55 | struct nfs_commit_data *p = mempool_alloc(nfs_commit_mempool, GFP_NOIO); |
Chuck Lever | 40859d7 | 2005-11-30 18:09:02 -0500 | [diff] [blame] | 56 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | if (p) { |
| 58 | memset(p, 0, sizeof(*p)); |
| 59 | INIT_LIST_HEAD(&p->pages); |
| 60 | } |
| 61 | return p; |
| 62 | } |
Fred Isaman | e0c2b38 | 2011-03-23 13:27:53 +0000 | [diff] [blame] | 63 | EXPORT_SYMBOL_GPL(nfs_commitdata_alloc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
Fred Isaman | 0b7c015 | 2012-04-20 14:47:39 -0400 | [diff] [blame] | 65 | void nfs_commit_free(struct nfs_commit_data *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | { |
| 67 | mempool_free(p, nfs_commit_mempool); |
| 68 | } |
Fred Isaman | e0c2b38 | 2011-03-23 13:27:53 +0000 | [diff] [blame] | 69 | EXPORT_SYMBOL_GPL(nfs_commit_free); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | |
Fred Isaman | 6c75dc0 | 2012-04-20 14:47:47 -0400 | [diff] [blame] | 71 | struct nfs_write_header *nfs_writehdr_alloc(void) |
Trond Myklebust | 3feb2d4 | 2006-03-20 13:44:37 -0500 | [diff] [blame] | 72 | { |
Mel Gorman | 192e501 | 2012-07-31 16:45:16 -0700 | [diff] [blame^] | 73 | struct nfs_write_header *p = mempool_alloc(nfs_wdata_mempool, GFP_NOIO); |
Trond Myklebust | 3feb2d4 | 2006-03-20 13:44:37 -0500 | [diff] [blame] | 74 | |
| 75 | if (p) { |
Fred Isaman | cd84160 | 2012-04-20 14:47:44 -0400 | [diff] [blame] | 76 | struct nfs_pgio_header *hdr = &p->header; |
Fred Isaman | cd84160 | 2012-04-20 14:47:44 -0400 | [diff] [blame] | 77 | |
Trond Myklebust | 3feb2d4 | 2006-03-20 13:44:37 -0500 | [diff] [blame] | 78 | memset(p, 0, sizeof(*p)); |
Fred Isaman | cd84160 | 2012-04-20 14:47:44 -0400 | [diff] [blame] | 79 | INIT_LIST_HEAD(&hdr->pages); |
Fred Isaman | 6c75dc0 | 2012-04-20 14:47:47 -0400 | [diff] [blame] | 80 | INIT_LIST_HEAD(&hdr->rpc_list); |
| 81 | spin_lock_init(&hdr->lock); |
| 82 | atomic_set(&hdr->refcnt, 0); |
Trond Myklebust | 9bce008 | 2012-06-05 18:32:03 -0400 | [diff] [blame] | 83 | hdr->verf = &p->verf; |
Trond Myklebust | 3feb2d4 | 2006-03-20 13:44:37 -0500 | [diff] [blame] | 84 | } |
| 85 | return p; |
| 86 | } |
| 87 | |
Fred Isaman | 1763da1 | 2012-04-20 14:47:57 -0400 | [diff] [blame] | 88 | static struct nfs_write_data *nfs_writedata_alloc(struct nfs_pgio_header *hdr, |
| 89 | unsigned int pagecount) |
Fred Isaman | 6c75dc0 | 2012-04-20 14:47:47 -0400 | [diff] [blame] | 90 | { |
| 91 | struct nfs_write_data *data, *prealloc; |
| 92 | |
| 93 | prealloc = &container_of(hdr, struct nfs_write_header, header)->rpc_data; |
| 94 | if (prealloc->header == NULL) |
| 95 | data = prealloc; |
| 96 | else |
| 97 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
| 98 | if (!data) |
| 99 | goto out; |
| 100 | |
| 101 | if (nfs_pgarray_set(&data->pages, pagecount)) { |
| 102 | data->header = hdr; |
| 103 | atomic_inc(&hdr->refcnt); |
| 104 | } else { |
| 105 | if (data != prealloc) |
| 106 | kfree(data); |
| 107 | data = NULL; |
| 108 | } |
| 109 | out: |
| 110 | return data; |
| 111 | } |
| 112 | |
Fred Isaman | cd84160 | 2012-04-20 14:47:44 -0400 | [diff] [blame] | 113 | void nfs_writehdr_free(struct nfs_pgio_header *hdr) |
Trond Myklebust | 3feb2d4 | 2006-03-20 13:44:37 -0500 | [diff] [blame] | 114 | { |
Fred Isaman | cd84160 | 2012-04-20 14:47:44 -0400 | [diff] [blame] | 115 | struct nfs_write_header *whdr = container_of(hdr, struct nfs_write_header, header); |
| 116 | mempool_free(whdr, nfs_wdata_mempool); |
Trond Myklebust | 3feb2d4 | 2006-03-20 13:44:37 -0500 | [diff] [blame] | 117 | } |
| 118 | |
Trond Myklebust | dce8129 | 2011-07-13 15:59:19 -0400 | [diff] [blame] | 119 | void nfs_writedata_release(struct nfs_write_data *wdata) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | { |
Fred Isaman | 6c75dc0 | 2012-04-20 14:47:47 -0400 | [diff] [blame] | 121 | struct nfs_pgio_header *hdr = wdata->header; |
| 122 | struct nfs_write_header *write_header = container_of(hdr, struct nfs_write_header, header); |
| 123 | |
Trond Myklebust | 383ba71 | 2008-02-19 20:04:20 -0500 | [diff] [blame] | 124 | put_nfs_open_context(wdata->args.context); |
Fred Isaman | 30dd374 | 2012-04-20 14:47:45 -0400 | [diff] [blame] | 125 | if (wdata->pages.pagevec != wdata->pages.page_array) |
| 126 | kfree(wdata->pages.pagevec); |
Fred Isaman | 6c75dc0 | 2012-04-20 14:47:47 -0400 | [diff] [blame] | 127 | if (wdata != &write_header->rpc_data) |
| 128 | kfree(wdata); |
| 129 | else |
| 130 | wdata->header = NULL; |
| 131 | if (atomic_dec_and_test(&hdr->refcnt)) |
Fred Isaman | 061ae2e | 2012-04-20 14:47:48 -0400 | [diff] [blame] | 132 | hdr->completion_ops->completion(hdr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Trond Myklebust | 7b159fc | 2007-07-25 14:09:54 -0400 | [diff] [blame] | 135 | static void nfs_context_set_write_error(struct nfs_open_context *ctx, int error) |
| 136 | { |
| 137 | ctx->error = error; |
| 138 | smp_wmb(); |
| 139 | set_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags); |
| 140 | } |
| 141 | |
Mel Gorman | 29418aa | 2012-07-31 16:45:10 -0700 | [diff] [blame] | 142 | static struct nfs_page * |
| 143 | nfs_page_find_request_locked(struct nfs_inode *nfsi, struct page *page) |
Trond Myklebust | 277459d | 2006-12-05 00:35:35 -0500 | [diff] [blame] | 144 | { |
| 145 | struct nfs_page *req = NULL; |
| 146 | |
Mel Gorman | 29418aa | 2012-07-31 16:45:10 -0700 | [diff] [blame] | 147 | if (PagePrivate(page)) |
Trond Myklebust | 277459d | 2006-12-05 00:35:35 -0500 | [diff] [blame] | 148 | req = (struct nfs_page *)page_private(page); |
Mel Gorman | 29418aa | 2012-07-31 16:45:10 -0700 | [diff] [blame] | 149 | else if (unlikely(PageSwapCache(page))) { |
| 150 | struct nfs_page *freq, *t; |
| 151 | |
| 152 | /* Linearly search the commit list for the correct req */ |
| 153 | list_for_each_entry_safe(freq, t, &nfsi->commit_info.list, wb_list) { |
| 154 | if (freq->wb_page == page) { |
| 155 | req = freq; |
| 156 | break; |
| 157 | } |
| 158 | } |
Trond Myklebust | 277459d | 2006-12-05 00:35:35 -0500 | [diff] [blame] | 159 | } |
Mel Gorman | 29418aa | 2012-07-31 16:45:10 -0700 | [diff] [blame] | 160 | |
| 161 | if (req) |
| 162 | kref_get(&req->wb_kref); |
| 163 | |
Trond Myklebust | 277459d | 2006-12-05 00:35:35 -0500 | [diff] [blame] | 164 | return req; |
| 165 | } |
| 166 | |
| 167 | static struct nfs_page *nfs_page_find_request(struct page *page) |
| 168 | { |
Mel Gorman | d56b4dd | 2012-07-31 16:45:06 -0700 | [diff] [blame] | 169 | struct inode *inode = page_file_mapping(page)->host; |
Trond Myklebust | 277459d | 2006-12-05 00:35:35 -0500 | [diff] [blame] | 170 | struct nfs_page *req = NULL; |
Trond Myklebust | 277459d | 2006-12-05 00:35:35 -0500 | [diff] [blame] | 171 | |
Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 172 | spin_lock(&inode->i_lock); |
Mel Gorman | 29418aa | 2012-07-31 16:45:10 -0700 | [diff] [blame] | 173 | req = nfs_page_find_request_locked(NFS_I(inode), page); |
Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 174 | spin_unlock(&inode->i_lock); |
Trond Myklebust | 277459d | 2006-12-05 00:35:35 -0500 | [diff] [blame] | 175 | return req; |
| 176 | } |
| 177 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | /* Adjust the file length if we're writing beyond the end */ |
| 179 | static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count) |
| 180 | { |
Mel Gorman | d56b4dd | 2012-07-31 16:45:06 -0700 | [diff] [blame] | 181 | struct inode *inode = page_file_mapping(page)->host; |
Trond Myklebust | a3d0145 | 2008-06-11 12:21:19 -0400 | [diff] [blame] | 182 | loff_t end, i_size; |
| 183 | pgoff_t end_index; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | |
Trond Myklebust | a3d0145 | 2008-06-11 12:21:19 -0400 | [diff] [blame] | 185 | spin_lock(&inode->i_lock); |
| 186 | i_size = i_size_read(inode); |
| 187 | end_index = (i_size - 1) >> PAGE_CACHE_SHIFT; |
Mel Gorman | d56b4dd | 2012-07-31 16:45:06 -0700 | [diff] [blame] | 188 | if (i_size > 0 && page_file_index(page) < end_index) |
Trond Myklebust | a3d0145 | 2008-06-11 12:21:19 -0400 | [diff] [blame] | 189 | goto out; |
Mel Gorman | d56b4dd | 2012-07-31 16:45:06 -0700 | [diff] [blame] | 190 | end = page_file_offset(page) + ((loff_t)offset+count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | if (i_size >= end) |
Trond Myklebust | a3d0145 | 2008-06-11 12:21:19 -0400 | [diff] [blame] | 192 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | i_size_write(inode, end); |
Trond Myklebust | a3d0145 | 2008-06-11 12:21:19 -0400 | [diff] [blame] | 194 | nfs_inc_stats(inode, NFSIOS_EXTENDWRITE); |
| 195 | out: |
| 196 | spin_unlock(&inode->i_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Trond Myklebust | a301b77 | 2007-02-06 11:07:15 -0800 | [diff] [blame] | 199 | /* A writeback failed: mark the page as bad, and invalidate the page cache */ |
| 200 | static void nfs_set_pageerror(struct page *page) |
| 201 | { |
| 202 | SetPageError(page); |
Mel Gorman | d56b4dd | 2012-07-31 16:45:06 -0700 | [diff] [blame] | 203 | nfs_zap_mapping(page_file_mapping(page)->host, page_file_mapping(page)); |
Trond Myklebust | a301b77 | 2007-02-06 11:07:15 -0800 | [diff] [blame] | 204 | } |
| 205 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | /* We can set the PG_uptodate flag if we see that a write request |
| 207 | * covers the full page. |
| 208 | */ |
| 209 | static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count) |
| 210 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | if (PageUptodate(page)) |
| 212 | return; |
| 213 | if (base != 0) |
| 214 | return; |
Trond Myklebust | 49a70f2 | 2006-12-05 00:35:38 -0500 | [diff] [blame] | 215 | if (count != nfs_page_length(page)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | return; |
Trond Myklebust | 49a70f2 | 2006-12-05 00:35:38 -0500 | [diff] [blame] | 217 | SetPageUptodate(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | static int wb_priority(struct writeback_control *wbc) |
| 221 | { |
| 222 | if (wbc->for_reclaim) |
Trond Myklebust | c63c7b0 | 2007-04-02 19:29:52 -0400 | [diff] [blame] | 223 | return FLUSH_HIGHPRI | FLUSH_STABLE; |
Wu Fengguang | b17621f | 2009-12-03 13:54:25 +0100 | [diff] [blame] | 224 | if (wbc->for_kupdate || wbc->for_background) |
Trond Myklebust | b31268a | 2011-03-21 17:02:00 -0400 | [diff] [blame] | 225 | return FLUSH_LOWPRI | FLUSH_COND_STABLE; |
| 226 | return FLUSH_COND_STABLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | /* |
Peter Zijlstra | 89a0914 | 2007-03-16 13:38:26 -0800 | [diff] [blame] | 230 | * NFS congestion control |
| 231 | */ |
| 232 | |
| 233 | int nfs_congestion_kb; |
| 234 | |
| 235 | #define NFS_CONGESTION_ON_THRESH (nfs_congestion_kb >> (PAGE_SHIFT-10)) |
| 236 | #define NFS_CONGESTION_OFF_THRESH \ |
| 237 | (NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2)) |
| 238 | |
Trond Myklebust | 5a6d41b | 2007-04-14 19:10:12 -0400 | [diff] [blame] | 239 | static int nfs_set_page_writeback(struct page *page) |
Peter Zijlstra | 89a0914 | 2007-03-16 13:38:26 -0800 | [diff] [blame] | 240 | { |
Trond Myklebust | 5a6d41b | 2007-04-14 19:10:12 -0400 | [diff] [blame] | 241 | int ret = test_set_page_writeback(page); |
| 242 | |
| 243 | if (!ret) { |
Mel Gorman | d56b4dd | 2012-07-31 16:45:06 -0700 | [diff] [blame] | 244 | struct inode *inode = page_file_mapping(page)->host; |
Peter Zijlstra | 89a0914 | 2007-03-16 13:38:26 -0800 | [diff] [blame] | 245 | struct nfs_server *nfss = NFS_SERVER(inode); |
| 246 | |
Peter Zijlstra | 277866a | 2007-05-08 00:35:12 -0700 | [diff] [blame] | 247 | if (atomic_long_inc_return(&nfss->writeback) > |
Jens Axboe | 8aa7e84 | 2009-07-09 14:52:32 +0200 | [diff] [blame] | 248 | NFS_CONGESTION_ON_THRESH) { |
| 249 | set_bdi_congested(&nfss->backing_dev_info, |
| 250 | BLK_RW_ASYNC); |
| 251 | } |
Peter Zijlstra | 89a0914 | 2007-03-16 13:38:26 -0800 | [diff] [blame] | 252 | } |
Trond Myklebust | 5a6d41b | 2007-04-14 19:10:12 -0400 | [diff] [blame] | 253 | return ret; |
Peter Zijlstra | 89a0914 | 2007-03-16 13:38:26 -0800 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | static void nfs_end_page_writeback(struct page *page) |
| 257 | { |
Mel Gorman | d56b4dd | 2012-07-31 16:45:06 -0700 | [diff] [blame] | 258 | struct inode *inode = page_file_mapping(page)->host; |
Peter Zijlstra | 89a0914 | 2007-03-16 13:38:26 -0800 | [diff] [blame] | 259 | struct nfs_server *nfss = NFS_SERVER(inode); |
| 260 | |
| 261 | end_page_writeback(page); |
Peter Zijlstra | c4dc4be | 2007-10-16 23:25:41 -0700 | [diff] [blame] | 262 | if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH) |
Jens Axboe | 8aa7e84 | 2009-07-09 14:52:32 +0200 | [diff] [blame] | 263 | clear_bdi_congested(&nfss->backing_dev_info, BLK_RW_ASYNC); |
Peter Zijlstra | 89a0914 | 2007-03-16 13:38:26 -0800 | [diff] [blame] | 264 | } |
| 265 | |
Trond Myklebust | cfb506e | 2010-07-30 15:31:57 -0400 | [diff] [blame] | 266 | static struct nfs_page *nfs_find_and_lock_request(struct page *page, bool nonblock) |
Trond Myklebust | e261f51 | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 267 | { |
Mel Gorman | d56b4dd | 2012-07-31 16:45:06 -0700 | [diff] [blame] | 268 | struct inode *inode = page_file_mapping(page)->host; |
Trond Myklebust | e261f51 | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 269 | struct nfs_page *req; |
Trond Myklebust | e261f51 | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 270 | int ret; |
| 271 | |
Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 272 | spin_lock(&inode->i_lock); |
Trond Myklebust | 074cc1d | 2009-08-10 08:54:13 -0400 | [diff] [blame] | 273 | for (;;) { |
Mel Gorman | 29418aa | 2012-07-31 16:45:10 -0700 | [diff] [blame] | 274 | req = nfs_page_find_request_locked(NFS_I(inode), page); |
Trond Myklebust | 074cc1d | 2009-08-10 08:54:13 -0400 | [diff] [blame] | 275 | if (req == NULL) |
| 276 | break; |
Trond Myklebust | 7ad84aa | 2012-05-09 13:19:15 -0400 | [diff] [blame] | 277 | if (nfs_lock_request(req)) |
Trond Myklebust | e261f51 | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 278 | break; |
| 279 | /* Note: If we hold the page lock, as is the case in nfs_writepage, |
Trond Myklebust | 7ad84aa | 2012-05-09 13:19:15 -0400 | [diff] [blame] | 280 | * then the call to nfs_lock_request() will always |
Trond Myklebust | e261f51 | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 281 | * succeed provided that someone hasn't already marked the |
| 282 | * request as dirty (in which case we don't care). |
| 283 | */ |
Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 284 | spin_unlock(&inode->i_lock); |
Trond Myklebust | cfb506e | 2010-07-30 15:31:57 -0400 | [diff] [blame] | 285 | if (!nonblock) |
| 286 | ret = nfs_wait_on_request(req); |
| 287 | else |
| 288 | ret = -EAGAIN; |
Trond Myklebust | e261f51 | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 289 | nfs_release_request(req); |
| 290 | if (ret != 0) |
Trond Myklebust | 074cc1d | 2009-08-10 08:54:13 -0400 | [diff] [blame] | 291 | return ERR_PTR(ret); |
Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 292 | spin_lock(&inode->i_lock); |
Trond Myklebust | e261f51 | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 293 | } |
Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 294 | spin_unlock(&inode->i_lock); |
Trond Myklebust | 074cc1d | 2009-08-10 08:54:13 -0400 | [diff] [blame] | 295 | return req; |
| 296 | } |
| 297 | |
| 298 | /* |
| 299 | * Find an associated nfs write request, and prepare to flush it out |
| 300 | * May return an error if the user signalled nfs_wait_on_request(). |
| 301 | */ |
| 302 | static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio, |
Trond Myklebust | cfb506e | 2010-07-30 15:31:57 -0400 | [diff] [blame] | 303 | struct page *page, bool nonblock) |
Trond Myklebust | 074cc1d | 2009-08-10 08:54:13 -0400 | [diff] [blame] | 304 | { |
| 305 | struct nfs_page *req; |
| 306 | int ret = 0; |
| 307 | |
Trond Myklebust | cfb506e | 2010-07-30 15:31:57 -0400 | [diff] [blame] | 308 | req = nfs_find_and_lock_request(page, nonblock); |
Trond Myklebust | 074cc1d | 2009-08-10 08:54:13 -0400 | [diff] [blame] | 309 | if (!req) |
| 310 | goto out; |
| 311 | ret = PTR_ERR(req); |
| 312 | if (IS_ERR(req)) |
| 313 | goto out; |
| 314 | |
| 315 | ret = nfs_set_page_writeback(page); |
| 316 | BUG_ON(ret != 0); |
| 317 | BUG_ON(test_bit(PG_CLEAN, &req->wb_flags)); |
| 318 | |
Fred Isaman | f8512ad | 2008-03-19 11:24:39 -0400 | [diff] [blame] | 319 | if (!nfs_pageio_add_request(pgio, req)) { |
| 320 | nfs_redirty_request(req); |
Trond Myklebust | 074cc1d | 2009-08-10 08:54:13 -0400 | [diff] [blame] | 321 | ret = pgio->pg_error; |
Fred Isaman | f8512ad | 2008-03-19 11:24:39 -0400 | [diff] [blame] | 322 | } |
Trond Myklebust | 074cc1d | 2009-08-10 08:54:13 -0400 | [diff] [blame] | 323 | out: |
| 324 | return ret; |
Trond Myklebust | e261f51 | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 325 | } |
| 326 | |
Trond Myklebust | f758c885 | 2007-07-22 19:27:32 -0400 | [diff] [blame] | 327 | static int nfs_do_writepage(struct page *page, struct writeback_control *wbc, struct nfs_pageio_descriptor *pgio) |
| 328 | { |
Mel Gorman | d56b4dd | 2012-07-31 16:45:06 -0700 | [diff] [blame] | 329 | struct inode *inode = page_file_mapping(page)->host; |
Trond Myklebust | cfb506e | 2010-07-30 15:31:57 -0400 | [diff] [blame] | 330 | int ret; |
Trond Myklebust | f758c885 | 2007-07-22 19:27:32 -0400 | [diff] [blame] | 331 | |
| 332 | nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE); |
| 333 | nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1); |
| 334 | |
Mel Gorman | d56b4dd | 2012-07-31 16:45:06 -0700 | [diff] [blame] | 335 | nfs_pageio_cond_complete(pgio, page_file_index(page)); |
Wu Fengguang | 1b430be | 2010-10-26 14:21:26 -0700 | [diff] [blame] | 336 | ret = nfs_page_async_flush(pgio, page, wbc->sync_mode == WB_SYNC_NONE); |
Trond Myklebust | cfb506e | 2010-07-30 15:31:57 -0400 | [diff] [blame] | 337 | if (ret == -EAGAIN) { |
| 338 | redirty_page_for_writepage(wbc, page); |
| 339 | ret = 0; |
| 340 | } |
| 341 | return ret; |
Trond Myklebust | f758c885 | 2007-07-22 19:27:32 -0400 | [diff] [blame] | 342 | } |
| 343 | |
Trond Myklebust | e261f51 | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 344 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | * Write an mmapped page to the server. |
| 346 | */ |
Trond Myklebust | 4d770cc | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 347 | static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | { |
Trond Myklebust | f758c885 | 2007-07-22 19:27:32 -0400 | [diff] [blame] | 349 | struct nfs_pageio_descriptor pgio; |
Trond Myklebust | e261f51 | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 350 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | |
Mel Gorman | d56b4dd | 2012-07-31 16:45:06 -0700 | [diff] [blame] | 352 | NFS_PROTO(page_file_mapping(page)->host)->write_pageio_init(&pgio, |
Bryan Schumaker | 57208fa | 2012-06-20 15:53:48 -0400 | [diff] [blame] | 353 | page->mapping->host, |
| 354 | wb_priority(wbc), |
| 355 | &nfs_async_write_completion_ops); |
Trond Myklebust | f758c885 | 2007-07-22 19:27:32 -0400 | [diff] [blame] | 356 | err = nfs_do_writepage(page, wbc, &pgio); |
| 357 | nfs_pageio_complete(&pgio); |
| 358 | if (err < 0) |
| 359 | return err; |
| 360 | if (pgio.pg_error < 0) |
| 361 | return pgio.pg_error; |
| 362 | return 0; |
Trond Myklebust | 4d770cc | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | int nfs_writepage(struct page *page, struct writeback_control *wbc) |
| 366 | { |
Trond Myklebust | f758c885 | 2007-07-22 19:27:32 -0400 | [diff] [blame] | 367 | int ret; |
Trond Myklebust | 4d770cc | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 368 | |
Trond Myklebust | f758c885 | 2007-07-22 19:27:32 -0400 | [diff] [blame] | 369 | ret = nfs_writepage_locked(page, wbc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | unlock_page(page); |
Trond Myklebust | f758c885 | 2007-07-22 19:27:32 -0400 | [diff] [blame] | 371 | return ret; |
| 372 | } |
| 373 | |
| 374 | static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data) |
| 375 | { |
| 376 | int ret; |
| 377 | |
| 378 | ret = nfs_do_writepage(page, wbc, data); |
| 379 | unlock_page(page); |
| 380 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | } |
| 382 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc) |
| 384 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | struct inode *inode = mapping->host; |
Trond Myklebust | 72cb77f | 2009-03-11 14:10:30 -0400 | [diff] [blame] | 386 | unsigned long *bitlock = &NFS_I(inode)->flags; |
Trond Myklebust | c63c7b0 | 2007-04-02 19:29:52 -0400 | [diff] [blame] | 387 | struct nfs_pageio_descriptor pgio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | int err; |
| 389 | |
Trond Myklebust | 72cb77f | 2009-03-11 14:10:30 -0400 | [diff] [blame] | 390 | /* Stop dirtying of new pages while we sync */ |
| 391 | err = wait_on_bit_lock(bitlock, NFS_INO_FLUSHING, |
| 392 | nfs_wait_bit_killable, TASK_KILLABLE); |
| 393 | if (err) |
| 394 | goto out_err; |
| 395 | |
Chuck Lever | 91d5b47 | 2006-03-20 13:44:14 -0500 | [diff] [blame] | 396 | nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES); |
| 397 | |
Bryan Schumaker | 57208fa | 2012-06-20 15:53:48 -0400 | [diff] [blame] | 398 | NFS_PROTO(inode)->write_pageio_init(&pgio, inode, wb_priority(wbc), &nfs_async_write_completion_ops); |
Trond Myklebust | f758c885 | 2007-07-22 19:27:32 -0400 | [diff] [blame] | 399 | err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio); |
Trond Myklebust | c63c7b0 | 2007-04-02 19:29:52 -0400 | [diff] [blame] | 400 | nfs_pageio_complete(&pgio); |
Trond Myklebust | 72cb77f | 2009-03-11 14:10:30 -0400 | [diff] [blame] | 401 | |
| 402 | clear_bit_unlock(NFS_INO_FLUSHING, bitlock); |
| 403 | smp_mb__after_clear_bit(); |
| 404 | wake_up_bit(bitlock, NFS_INO_FLUSHING); |
| 405 | |
Trond Myklebust | f758c885 | 2007-07-22 19:27:32 -0400 | [diff] [blame] | 406 | if (err < 0) |
Trond Myklebust | 72cb77f | 2009-03-11 14:10:30 -0400 | [diff] [blame] | 407 | goto out_err; |
| 408 | err = pgio.pg_error; |
| 409 | if (err < 0) |
| 410 | goto out_err; |
Trond Myklebust | c63c7b0 | 2007-04-02 19:29:52 -0400 | [diff] [blame] | 411 | return 0; |
Trond Myklebust | 72cb77f | 2009-03-11 14:10:30 -0400 | [diff] [blame] | 412 | out_err: |
| 413 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | /* |
| 417 | * Insert a write request into an inode |
| 418 | */ |
Fred Isaman | d6d6dc7 | 2012-03-08 17:29:35 -0500 | [diff] [blame] | 419 | static void nfs_inode_add_request(struct inode *inode, struct nfs_page *req) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | { |
| 421 | struct nfs_inode *nfsi = NFS_I(inode); |
Trond Myklebust | e7d3906 | 2008-06-13 12:12:32 -0400 | [diff] [blame] | 422 | |
| 423 | /* Lock the request! */ |
Trond Myklebust | 7ad84aa | 2012-05-09 13:19:15 -0400 | [diff] [blame] | 424 | nfs_lock_request(req); |
Trond Myklebust | e7d3906 | 2008-06-13 12:12:32 -0400 | [diff] [blame] | 425 | |
| 426 | spin_lock(&inode->i_lock); |
Bryan Schumaker | 011e2a7 | 2012-06-20 15:53:43 -0400 | [diff] [blame] | 427 | if (!nfsi->npages && NFS_PROTO(inode)->have_delegation(inode, FMODE_WRITE)) |
Trond Myklebust | a9a4a87 | 2011-10-17 16:08:46 -0700 | [diff] [blame] | 428 | inode->i_version++; |
Mel Gorman | 29418aa | 2012-07-31 16:45:10 -0700 | [diff] [blame] | 429 | /* |
| 430 | * Swap-space should not get truncated. Hence no need to plug the race |
| 431 | * with invalidate/truncate. |
| 432 | */ |
| 433 | if (likely(!PageSwapCache(req->wb_page))) { |
| 434 | set_bit(PG_MAPPED, &req->wb_flags); |
| 435 | SetPagePrivate(req->wb_page); |
| 436 | set_page_private(req->wb_page, (unsigned long)req); |
| 437 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | nfsi->npages++; |
Trond Myklebust | c03b402 | 2007-06-17 13:26:38 -0400 | [diff] [blame] | 439 | kref_get(&req->wb_kref); |
Trond Myklebust | e7d3906 | 2008-06-13 12:12:32 -0400 | [diff] [blame] | 440 | spin_unlock(&inode->i_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | /* |
Peter Zijlstra | 89a0914 | 2007-03-16 13:38:26 -0800 | [diff] [blame] | 444 | * Remove a write request from an inode |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | */ |
| 446 | static void nfs_inode_remove_request(struct nfs_page *req) |
| 447 | { |
Al Viro | 3d4ff43 | 2011-06-22 18:40:12 -0400 | [diff] [blame] | 448 | struct inode *inode = req->wb_context->dentry->d_inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | struct nfs_inode *nfsi = NFS_I(inode); |
| 450 | |
| 451 | BUG_ON (!NFS_WBACK_BUSY(req)); |
| 452 | |
Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 453 | spin_lock(&inode->i_lock); |
Mel Gorman | 29418aa | 2012-07-31 16:45:10 -0700 | [diff] [blame] | 454 | if (likely(!PageSwapCache(req->wb_page))) { |
| 455 | set_page_private(req->wb_page, 0); |
| 456 | ClearPagePrivate(req->wb_page); |
| 457 | clear_bit(PG_MAPPED, &req->wb_flags); |
| 458 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | nfsi->npages--; |
Trond Myklebust | 4d65c52 | 2011-03-25 14:15:11 -0400 | [diff] [blame] | 460 | spin_unlock(&inode->i_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | nfs_release_request(req); |
| 462 | } |
| 463 | |
Trond Myklebust | 61822ab | 2006-12-05 00:35:42 -0500 | [diff] [blame] | 464 | static void |
Fred | 6d884e8 | 2008-03-19 11:24:38 -0400 | [diff] [blame] | 465 | nfs_mark_request_dirty(struct nfs_page *req) |
Trond Myklebust | 61822ab | 2006-12-05 00:35:42 -0500 | [diff] [blame] | 466 | { |
Trond Myklebust | 61822ab | 2006-12-05 00:35:42 -0500 | [diff] [blame] | 467 | __set_page_dirty_nobuffers(req->wb_page); |
| 468 | } |
| 469 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) |
Trond Myklebust | 8dd3775 | 2012-03-15 17:16:40 -0400 | [diff] [blame] | 471 | /** |
| 472 | * nfs_request_add_commit_list - add request to a commit list |
| 473 | * @req: pointer to a struct nfs_page |
Fred Isaman | ea2cf22 | 2012-04-20 14:47:53 -0400 | [diff] [blame] | 474 | * @dst: commit list head |
| 475 | * @cinfo: holds list lock and accounting info |
Trond Myklebust | 8dd3775 | 2012-03-15 17:16:40 -0400 | [diff] [blame] | 476 | * |
Fred Isaman | ea2cf22 | 2012-04-20 14:47:53 -0400 | [diff] [blame] | 477 | * This sets the PG_CLEAN bit, updates the cinfo count of |
Trond Myklebust | 8dd3775 | 2012-03-15 17:16:40 -0400 | [diff] [blame] | 478 | * number of outstanding requests requiring a commit as well as |
| 479 | * the MM page stats. |
| 480 | * |
Fred Isaman | ea2cf22 | 2012-04-20 14:47:53 -0400 | [diff] [blame] | 481 | * The caller must _not_ hold the cinfo->lock, but must be |
Trond Myklebust | 8dd3775 | 2012-03-15 17:16:40 -0400 | [diff] [blame] | 482 | * holding the nfs_page lock. |
| 483 | */ |
| 484 | void |
Fred Isaman | ea2cf22 | 2012-04-20 14:47:53 -0400 | [diff] [blame] | 485 | nfs_request_add_commit_list(struct nfs_page *req, struct list_head *dst, |
| 486 | struct nfs_commit_info *cinfo) |
Trond Myklebust | 8dd3775 | 2012-03-15 17:16:40 -0400 | [diff] [blame] | 487 | { |
Trond Myklebust | 8dd3775 | 2012-03-15 17:16:40 -0400 | [diff] [blame] | 488 | set_bit(PG_CLEAN, &(req)->wb_flags); |
Fred Isaman | ea2cf22 | 2012-04-20 14:47:53 -0400 | [diff] [blame] | 489 | spin_lock(cinfo->lock); |
| 490 | nfs_list_add_request(req, dst); |
| 491 | cinfo->mds->ncommit++; |
| 492 | spin_unlock(cinfo->lock); |
Fred Isaman | 56f9cd6 | 2012-04-20 14:47:56 -0400 | [diff] [blame] | 493 | if (!cinfo->dreq) { |
| 494 | inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS); |
Mel Gorman | d56b4dd | 2012-07-31 16:45:06 -0700 | [diff] [blame] | 495 | inc_bdi_stat(page_file_mapping(req->wb_page)->backing_dev_info, |
Fred Isaman | 56f9cd6 | 2012-04-20 14:47:56 -0400 | [diff] [blame] | 496 | BDI_RECLAIMABLE); |
| 497 | __mark_inode_dirty(req->wb_context->dentry->d_inode, |
| 498 | I_DIRTY_DATASYNC); |
| 499 | } |
Trond Myklebust | 8dd3775 | 2012-03-15 17:16:40 -0400 | [diff] [blame] | 500 | } |
| 501 | EXPORT_SYMBOL_GPL(nfs_request_add_commit_list); |
| 502 | |
| 503 | /** |
| 504 | * nfs_request_remove_commit_list - Remove request from a commit list |
| 505 | * @req: pointer to a nfs_page |
Fred Isaman | ea2cf22 | 2012-04-20 14:47:53 -0400 | [diff] [blame] | 506 | * @cinfo: holds list lock and accounting info |
Trond Myklebust | 8dd3775 | 2012-03-15 17:16:40 -0400 | [diff] [blame] | 507 | * |
Fred Isaman | ea2cf22 | 2012-04-20 14:47:53 -0400 | [diff] [blame] | 508 | * This clears the PG_CLEAN bit, and updates the cinfo's count of |
Trond Myklebust | 8dd3775 | 2012-03-15 17:16:40 -0400 | [diff] [blame] | 509 | * number of outstanding requests requiring a commit |
| 510 | * It does not update the MM page stats. |
| 511 | * |
Fred Isaman | ea2cf22 | 2012-04-20 14:47:53 -0400 | [diff] [blame] | 512 | * The caller _must_ hold the cinfo->lock and the nfs_page lock. |
Trond Myklebust | 8dd3775 | 2012-03-15 17:16:40 -0400 | [diff] [blame] | 513 | */ |
| 514 | void |
Fred Isaman | ea2cf22 | 2012-04-20 14:47:53 -0400 | [diff] [blame] | 515 | nfs_request_remove_commit_list(struct nfs_page *req, |
| 516 | struct nfs_commit_info *cinfo) |
Trond Myklebust | 8dd3775 | 2012-03-15 17:16:40 -0400 | [diff] [blame] | 517 | { |
Trond Myklebust | 8dd3775 | 2012-03-15 17:16:40 -0400 | [diff] [blame] | 518 | if (!test_and_clear_bit(PG_CLEAN, &(req)->wb_flags)) |
| 519 | return; |
| 520 | nfs_list_remove_request(req); |
Fred Isaman | ea2cf22 | 2012-04-20 14:47:53 -0400 | [diff] [blame] | 521 | cinfo->mds->ncommit--; |
Trond Myklebust | 8dd3775 | 2012-03-15 17:16:40 -0400 | [diff] [blame] | 522 | } |
| 523 | EXPORT_SYMBOL_GPL(nfs_request_remove_commit_list); |
| 524 | |
Fred Isaman | ea2cf22 | 2012-04-20 14:47:53 -0400 | [diff] [blame] | 525 | static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo, |
| 526 | struct inode *inode) |
| 527 | { |
| 528 | cinfo->lock = &inode->i_lock; |
| 529 | cinfo->mds = &NFS_I(inode)->commit_info; |
| 530 | cinfo->ds = pnfs_get_ds_info(inode); |
Fred Isaman | b359f9d | 2012-04-20 14:47:55 -0400 | [diff] [blame] | 531 | cinfo->dreq = NULL; |
Fred Isaman | f453a54 | 2012-04-20 14:47:54 -0400 | [diff] [blame] | 532 | cinfo->completion_ops = &nfs_commit_completion_ops; |
Fred Isaman | ea2cf22 | 2012-04-20 14:47:53 -0400 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | void nfs_init_cinfo(struct nfs_commit_info *cinfo, |
| 536 | struct inode *inode, |
| 537 | struct nfs_direct_req *dreq) |
| 538 | { |
Fred Isaman | 1763da1 | 2012-04-20 14:47:57 -0400 | [diff] [blame] | 539 | if (dreq) |
| 540 | nfs_init_cinfo_from_dreq(cinfo, dreq); |
| 541 | else |
| 542 | nfs_init_cinfo_from_inode(cinfo, inode); |
Fred Isaman | ea2cf22 | 2012-04-20 14:47:53 -0400 | [diff] [blame] | 543 | } |
| 544 | EXPORT_SYMBOL_GPL(nfs_init_cinfo); |
Trond Myklebust | 8dd3775 | 2012-03-15 17:16:40 -0400 | [diff] [blame] | 545 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | /* |
| 547 | * Add a request to the inode's commit list. |
| 548 | */ |
Fred Isaman | 1763da1 | 2012-04-20 14:47:57 -0400 | [diff] [blame] | 549 | void |
Fred Isaman | ea2cf22 | 2012-04-20 14:47:53 -0400 | [diff] [blame] | 550 | nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg, |
| 551 | struct nfs_commit_info *cinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | { |
Fred Isaman | ea2cf22 | 2012-04-20 14:47:53 -0400 | [diff] [blame] | 553 | if (pnfs_mark_request_commit(req, lseg, cinfo)) |
Trond Myklebust | 8dd3775 | 2012-03-15 17:16:40 -0400 | [diff] [blame] | 554 | return; |
Fred Isaman | ea2cf22 | 2012-04-20 14:47:53 -0400 | [diff] [blame] | 555 | nfs_request_add_commit_list(req, &cinfo->mds->list, cinfo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | } |
Trond Myklebust | 8e821ca | 2007-04-20 16:12:34 -0400 | [diff] [blame] | 557 | |
Fred Isaman | d6d6dc7 | 2012-03-08 17:29:35 -0500 | [diff] [blame] | 558 | static void |
| 559 | nfs_clear_page_commit(struct page *page) |
| 560 | { |
| 561 | dec_zone_page_state(page, NR_UNSTABLE_NFS); |
Mel Gorman | d56b4dd | 2012-07-31 16:45:06 -0700 | [diff] [blame] | 562 | dec_bdi_stat(page_file_mapping(page)->backing_dev_info, BDI_RECLAIMABLE); |
Fred Isaman | d6d6dc7 | 2012-03-08 17:29:35 -0500 | [diff] [blame] | 563 | } |
| 564 | |
Trond Myklebust | 8dd3775 | 2012-03-15 17:16:40 -0400 | [diff] [blame] | 565 | static void |
Trond Myklebust | e468bae | 2008-06-13 13:25:22 -0400 | [diff] [blame] | 566 | nfs_clear_request_commit(struct nfs_page *req) |
| 567 | { |
Trond Myklebust | 8dd3775 | 2012-03-15 17:16:40 -0400 | [diff] [blame] | 568 | if (test_bit(PG_CLEAN, &req->wb_flags)) { |
| 569 | struct inode *inode = req->wb_context->dentry->d_inode; |
Fred Isaman | ea2cf22 | 2012-04-20 14:47:53 -0400 | [diff] [blame] | 570 | struct nfs_commit_info cinfo; |
Trond Myklebust | e468bae | 2008-06-13 13:25:22 -0400 | [diff] [blame] | 571 | |
Fred Isaman | ea2cf22 | 2012-04-20 14:47:53 -0400 | [diff] [blame] | 572 | nfs_init_cinfo_from_inode(&cinfo, inode); |
| 573 | if (!pnfs_clear_request_commit(req, &cinfo)) { |
| 574 | spin_lock(cinfo.lock); |
| 575 | nfs_request_remove_commit_list(req, &cinfo); |
| 576 | spin_unlock(cinfo.lock); |
Trond Myklebust | 8dd3775 | 2012-03-15 17:16:40 -0400 | [diff] [blame] | 577 | } |
Fred Isaman | d6d6dc7 | 2012-03-08 17:29:35 -0500 | [diff] [blame] | 578 | nfs_clear_page_commit(req->wb_page); |
Trond Myklebust | e468bae | 2008-06-13 13:25:22 -0400 | [diff] [blame] | 579 | } |
Trond Myklebust | e468bae | 2008-06-13 13:25:22 -0400 | [diff] [blame] | 580 | } |
| 581 | |
Trond Myklebust | 8e821ca | 2007-04-20 16:12:34 -0400 | [diff] [blame] | 582 | static inline |
| 583 | int nfs_write_need_commit(struct nfs_write_data *data) |
| 584 | { |
Fred Isaman | 465d524 | 2011-03-23 13:27:44 +0000 | [diff] [blame] | 585 | if (data->verf.committed == NFS_DATA_SYNC) |
Fred Isaman | cd84160 | 2012-04-20 14:47:44 -0400 | [diff] [blame] | 586 | return data->header->lseg == NULL; |
| 587 | return data->verf.committed != NFS_FILE_SYNC; |
Trond Myklebust | 8e821ca | 2007-04-20 16:12:34 -0400 | [diff] [blame] | 588 | } |
| 589 | |
Trond Myklebust | 8e821ca | 2007-04-20 16:12:34 -0400 | [diff] [blame] | 590 | #else |
Bryan Schumaker | 68cd6fa | 2012-04-30 14:30:22 -0400 | [diff] [blame] | 591 | static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo, |
| 592 | struct inode *inode) |
| 593 | { |
| 594 | } |
| 595 | |
| 596 | void nfs_init_cinfo(struct nfs_commit_info *cinfo, |
| 597 | struct inode *inode, |
| 598 | struct nfs_direct_req *dreq) |
| 599 | { |
| 600 | } |
| 601 | |
Fred Isaman | 1763da1 | 2012-04-20 14:47:57 -0400 | [diff] [blame] | 602 | void |
Fred Isaman | ea2cf22 | 2012-04-20 14:47:53 -0400 | [diff] [blame] | 603 | nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg, |
| 604 | struct nfs_commit_info *cinfo) |
Trond Myklebust | 8e821ca | 2007-04-20 16:12:34 -0400 | [diff] [blame] | 605 | { |
| 606 | } |
| 607 | |
Trond Myklebust | 8dd3775 | 2012-03-15 17:16:40 -0400 | [diff] [blame] | 608 | static void |
Trond Myklebust | e468bae | 2008-06-13 13:25:22 -0400 | [diff] [blame] | 609 | nfs_clear_request_commit(struct nfs_page *req) |
| 610 | { |
Trond Myklebust | e468bae | 2008-06-13 13:25:22 -0400 | [diff] [blame] | 611 | } |
| 612 | |
Trond Myklebust | 8e821ca | 2007-04-20 16:12:34 -0400 | [diff] [blame] | 613 | static inline |
| 614 | int nfs_write_need_commit(struct nfs_write_data *data) |
| 615 | { |
| 616 | return 0; |
| 617 | } |
| 618 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | #endif |
| 620 | |
Fred Isaman | 061ae2e | 2012-04-20 14:47:48 -0400 | [diff] [blame] | 621 | static void nfs_write_completion(struct nfs_pgio_header *hdr) |
Fred Isaman | 6c75dc0 | 2012-04-20 14:47:47 -0400 | [diff] [blame] | 622 | { |
Fred Isaman | ea2cf22 | 2012-04-20 14:47:53 -0400 | [diff] [blame] | 623 | struct nfs_commit_info cinfo; |
Fred Isaman | 6c75dc0 | 2012-04-20 14:47:47 -0400 | [diff] [blame] | 624 | unsigned long bytes = 0; |
| 625 | |
| 626 | if (test_bit(NFS_IOHDR_REDO, &hdr->flags)) |
| 627 | goto out; |
Fred Isaman | ea2cf22 | 2012-04-20 14:47:53 -0400 | [diff] [blame] | 628 | nfs_init_cinfo_from_inode(&cinfo, hdr->inode); |
Fred Isaman | 6c75dc0 | 2012-04-20 14:47:47 -0400 | [diff] [blame] | 629 | while (!list_empty(&hdr->pages)) { |
| 630 | struct nfs_page *req = nfs_list_entry(hdr->pages.next); |
Fred Isaman | 6c75dc0 | 2012-04-20 14:47:47 -0400 | [diff] [blame] | 631 | |
| 632 | bytes += req->wb_bytes; |
| 633 | nfs_list_remove_request(req); |
| 634 | if (test_bit(NFS_IOHDR_ERROR, &hdr->flags) && |
| 635 | (hdr->good_bytes < bytes)) { |
Trond Myklebust | d1182b3 | 2012-05-09 13:37:43 -0400 | [diff] [blame] | 636 | nfs_set_pageerror(req->wb_page); |
Fred Isaman | 6c75dc0 | 2012-04-20 14:47:47 -0400 | [diff] [blame] | 637 | nfs_context_set_write_error(req->wb_context, hdr->error); |
| 638 | goto remove_req; |
| 639 | } |
| 640 | if (test_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags)) { |
| 641 | nfs_mark_request_dirty(req); |
| 642 | goto next; |
| 643 | } |
| 644 | if (test_bit(NFS_IOHDR_NEED_COMMIT, &hdr->flags)) { |
Trond Myklebust | 2f2c63b | 2012-06-08 11:56:09 -0400 | [diff] [blame] | 645 | memcpy(&req->wb_verf, &hdr->verf->verifier, sizeof(req->wb_verf)); |
Fred Isaman | ea2cf22 | 2012-04-20 14:47:53 -0400 | [diff] [blame] | 646 | nfs_mark_request_commit(req, hdr->lseg, &cinfo); |
Fred Isaman | 6c75dc0 | 2012-04-20 14:47:47 -0400 | [diff] [blame] | 647 | goto next; |
| 648 | } |
| 649 | remove_req: |
| 650 | nfs_inode_remove_request(req); |
| 651 | next: |
Trond Myklebust | 1d1afcb | 2012-05-09 14:04:55 -0400 | [diff] [blame] | 652 | nfs_unlock_request(req); |
Trond Myklebust | d1182b3 | 2012-05-09 13:37:43 -0400 | [diff] [blame] | 653 | nfs_end_page_writeback(req->wb_page); |
Trond Myklebust | 3aff4eb | 2012-05-09 14:30:35 -0400 | [diff] [blame] | 654 | nfs_release_request(req); |
Fred Isaman | 6c75dc0 | 2012-04-20 14:47:47 -0400 | [diff] [blame] | 655 | } |
| 656 | out: |
| 657 | hdr->release(hdr); |
| 658 | } |
| 659 | |
Trond Myklebust | 47c6256 | 2009-03-16 08:13:41 -0400 | [diff] [blame] | 660 | #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) |
Fred Isaman | ea2cf22 | 2012-04-20 14:47:53 -0400 | [diff] [blame] | 661 | static unsigned long |
| 662 | nfs_reqs_to_commit(struct nfs_commit_info *cinfo) |
Trond Myklebust | fb8a1f1 | 2009-03-11 14:10:29 -0400 | [diff] [blame] | 663 | { |
Fred Isaman | ea2cf22 | 2012-04-20 14:47:53 -0400 | [diff] [blame] | 664 | return cinfo->mds->ncommit; |
Trond Myklebust | fb8a1f1 | 2009-03-11 14:10:29 -0400 | [diff] [blame] | 665 | } |
| 666 | |
Fred Isaman | ea2cf22 | 2012-04-20 14:47:53 -0400 | [diff] [blame] | 667 | /* cinfo->lock held by caller */ |
Fred Isaman | 1763da1 | 2012-04-20 14:47:57 -0400 | [diff] [blame] | 668 | int |
Fred Isaman | ea2cf22 | 2012-04-20 14:47:53 -0400 | [diff] [blame] | 669 | nfs_scan_commit_list(struct list_head *src, struct list_head *dst, |
| 670 | struct nfs_commit_info *cinfo, int max) |
Fred Isaman | d6d6dc7 | 2012-03-08 17:29:35 -0500 | [diff] [blame] | 671 | { |
| 672 | struct nfs_page *req, *tmp; |
| 673 | int ret = 0; |
| 674 | |
| 675 | list_for_each_entry_safe(req, tmp, src, wb_list) { |
Trond Myklebust | 8dd3775 | 2012-03-15 17:16:40 -0400 | [diff] [blame] | 676 | if (!nfs_lock_request(req)) |
| 677 | continue; |
Trond Myklebust | 7ad84aa | 2012-05-09 13:19:15 -0400 | [diff] [blame] | 678 | kref_get(&req->wb_kref); |
Fred Isaman | ea2cf22 | 2012-04-20 14:47:53 -0400 | [diff] [blame] | 679 | if (cond_resched_lock(cinfo->lock)) |
Trond Myklebust | 3b3be88 | 2012-03-17 11:59:30 -0400 | [diff] [blame] | 680 | list_safe_reset_next(req, tmp, wb_list); |
Fred Isaman | ea2cf22 | 2012-04-20 14:47:53 -0400 | [diff] [blame] | 681 | nfs_request_remove_commit_list(req, cinfo); |
Trond Myklebust | 8dd3775 | 2012-03-15 17:16:40 -0400 | [diff] [blame] | 682 | nfs_list_add_request(req, dst); |
| 683 | ret++; |
Fred Isaman | 1763da1 | 2012-04-20 14:47:57 -0400 | [diff] [blame] | 684 | if ((ret == max) && !cinfo->dreq) |
Trond Myklebust | 8dd3775 | 2012-03-15 17:16:40 -0400 | [diff] [blame] | 685 | break; |
Fred Isaman | d6d6dc7 | 2012-03-08 17:29:35 -0500 | [diff] [blame] | 686 | } |
| 687 | return ret; |
| 688 | } |
Fred Isaman | d6d6dc7 | 2012-03-08 17:29:35 -0500 | [diff] [blame] | 689 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | /* |
| 691 | * nfs_scan_commit - Scan an inode for commit requests |
| 692 | * @inode: NFS inode to scan |
Fred Isaman | ea2cf22 | 2012-04-20 14:47:53 -0400 | [diff] [blame] | 693 | * @dst: mds destination list |
| 694 | * @cinfo: mds and ds lists of reqs ready to commit |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | * |
| 696 | * Moves requests from the inode's 'commit' request list. |
| 697 | * The requests are *not* checked to ensure that they form a contiguous set. |
| 698 | */ |
Fred Isaman | 1763da1 | 2012-04-20 14:47:57 -0400 | [diff] [blame] | 699 | int |
Fred Isaman | ea2cf22 | 2012-04-20 14:47:53 -0400 | [diff] [blame] | 700 | nfs_scan_commit(struct inode *inode, struct list_head *dst, |
| 701 | struct nfs_commit_info *cinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | { |
Fred Isaman | d6d6dc7 | 2012-03-08 17:29:35 -0500 | [diff] [blame] | 703 | int ret = 0; |
Trond Myklebust | fb8a1f1 | 2009-03-11 14:10:29 -0400 | [diff] [blame] | 704 | |
Fred Isaman | ea2cf22 | 2012-04-20 14:47:53 -0400 | [diff] [blame] | 705 | spin_lock(cinfo->lock); |
| 706 | if (cinfo->mds->ncommit > 0) { |
Trond Myklebust | 8dd3775 | 2012-03-15 17:16:40 -0400 | [diff] [blame] | 707 | const int max = INT_MAX; |
Fred Isaman | d6d6dc7 | 2012-03-08 17:29:35 -0500 | [diff] [blame] | 708 | |
Fred Isaman | ea2cf22 | 2012-04-20 14:47:53 -0400 | [diff] [blame] | 709 | ret = nfs_scan_commit_list(&cinfo->mds->list, dst, |
| 710 | cinfo, max); |
| 711 | ret += pnfs_scan_commit_lists(inode, cinfo, max - ret); |
Fred Isaman | d6d6dc7 | 2012-03-08 17:29:35 -0500 | [diff] [blame] | 712 | } |
Fred Isaman | ea2cf22 | 2012-04-20 14:47:53 -0400 | [diff] [blame] | 713 | spin_unlock(cinfo->lock); |
Trond Myklebust | ff778d0 | 2010-02-19 16:53:39 -0800 | [diff] [blame] | 714 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | } |
Fred Isaman | d6d6dc7 | 2012-03-08 17:29:35 -0500 | [diff] [blame] | 716 | |
Trond Myklebust | c42de9d | 2006-03-20 13:44:51 -0500 | [diff] [blame] | 717 | #else |
Fred Isaman | ea2cf22 | 2012-04-20 14:47:53 -0400 | [diff] [blame] | 718 | static unsigned long nfs_reqs_to_commit(struct nfs_commit_info *cinfo) |
Trond Myklebust | fb8a1f1 | 2009-03-11 14:10:29 -0400 | [diff] [blame] | 719 | { |
| 720 | return 0; |
| 721 | } |
| 722 | |
Fred Isaman | 1763da1 | 2012-04-20 14:47:57 -0400 | [diff] [blame] | 723 | int nfs_scan_commit(struct inode *inode, struct list_head *dst, |
| 724 | struct nfs_commit_info *cinfo) |
Trond Myklebust | c42de9d | 2006-03-20 13:44:51 -0500 | [diff] [blame] | 725 | { |
| 726 | return 0; |
| 727 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | #endif |
| 729 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | /* |
Trond Myklebust | e7d3906 | 2008-06-13 12:12:32 -0400 | [diff] [blame] | 731 | * Search for an existing write request, and attempt to update |
| 732 | * it to reflect a new dirty region on a given page. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | * |
Trond Myklebust | e7d3906 | 2008-06-13 12:12:32 -0400 | [diff] [blame] | 734 | * If the attempt fails, then the existing request is flushed out |
| 735 | * to disk. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | */ |
Trond Myklebust | e7d3906 | 2008-06-13 12:12:32 -0400 | [diff] [blame] | 737 | static struct nfs_page *nfs_try_to_update_request(struct inode *inode, |
| 738 | struct page *page, |
| 739 | unsigned int offset, |
| 740 | unsigned int bytes) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | { |
Trond Myklebust | e7d3906 | 2008-06-13 12:12:32 -0400 | [diff] [blame] | 742 | struct nfs_page *req; |
| 743 | unsigned int rqend; |
| 744 | unsigned int end; |
| 745 | int error; |
| 746 | |
| 747 | if (!PagePrivate(page)) |
| 748 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | |
| 750 | end = offset + bytes; |
Trond Myklebust | e7d3906 | 2008-06-13 12:12:32 -0400 | [diff] [blame] | 751 | spin_lock(&inode->i_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | for (;;) { |
Mel Gorman | 29418aa | 2012-07-31 16:45:10 -0700 | [diff] [blame] | 754 | req = nfs_page_find_request_locked(NFS_I(inode), page); |
Trond Myklebust | e7d3906 | 2008-06-13 12:12:32 -0400 | [diff] [blame] | 755 | if (req == NULL) |
| 756 | goto out_unlock; |
Trond Myklebust | 277459d | 2006-12-05 00:35:35 -0500 | [diff] [blame] | 757 | |
Trond Myklebust | e7d3906 | 2008-06-13 12:12:32 -0400 | [diff] [blame] | 758 | rqend = req->wb_offset + req->wb_bytes; |
| 759 | /* |
| 760 | * Tell the caller to flush out the request if |
| 761 | * the offsets are non-contiguous. |
| 762 | * Note: nfs_flush_incompatible() will already |
| 763 | * have flushed out requests having wrong owners. |
| 764 | */ |
Trond Myklebust | e468bae | 2008-06-13 13:25:22 -0400 | [diff] [blame] | 765 | if (offset > rqend |
Trond Myklebust | e7d3906 | 2008-06-13 12:12:32 -0400 | [diff] [blame] | 766 | || end < req->wb_offset) |
| 767 | goto out_flushme; |
| 768 | |
Trond Myklebust | 7ad84aa | 2012-05-09 13:19:15 -0400 | [diff] [blame] | 769 | if (nfs_lock_request(req)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 771 | |
Trond Myklebust | e7d3906 | 2008-06-13 12:12:32 -0400 | [diff] [blame] | 772 | /* The request is locked, so wait and then retry */ |
Trond Myklebust | 587142f | 2007-07-02 09:57:54 -0400 | [diff] [blame] | 773 | spin_unlock(&inode->i_lock); |
Trond Myklebust | e7d3906 | 2008-06-13 12:12:32 -0400 | [diff] [blame] | 774 | error = nfs_wait_on_request(req); |
| 775 | nfs_release_request(req); |
| 776 | if (error != 0) |
| 777 | goto out_err; |
| 778 | spin_lock(&inode->i_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | } |
| 780 | |
| 781 | /* Okay, the request matches. Update the region */ |
| 782 | if (offset < req->wb_offset) { |
| 783 | req->wb_offset = offset; |
| 784 | req->wb_pgbase = offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 786 | if (end > rqend) |
| 787 | req->wb_bytes = end - req->wb_offset; |
Trond Myklebust | e7d3906 | 2008-06-13 12:12:32 -0400 | [diff] [blame] | 788 | else |
| 789 | req->wb_bytes = rqend - req->wb_offset; |
| 790 | out_unlock: |
| 791 | spin_unlock(&inode->i_lock); |
Fred Isaman | ca138f3 | 2012-04-05 15:26:36 -0400 | [diff] [blame] | 792 | if (req) |
| 793 | nfs_clear_request_commit(req); |
Trond Myklebust | e7d3906 | 2008-06-13 12:12:32 -0400 | [diff] [blame] | 794 | return req; |
| 795 | out_flushme: |
| 796 | spin_unlock(&inode->i_lock); |
| 797 | nfs_release_request(req); |
| 798 | error = nfs_wb_page(inode, page); |
| 799 | out_err: |
| 800 | return ERR_PTR(error); |
| 801 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 802 | |
Trond Myklebust | e7d3906 | 2008-06-13 12:12:32 -0400 | [diff] [blame] | 803 | /* |
| 804 | * Try to update an existing write request, or create one if there is none. |
| 805 | * |
| 806 | * Note: Should always be called with the Page Lock held to prevent races |
| 807 | * if we have to add a new request. Also assumes that the caller has |
| 808 | * already called nfs_flush_incompatible() if necessary. |
| 809 | */ |
| 810 | static struct nfs_page * nfs_setup_write_request(struct nfs_open_context* ctx, |
| 811 | struct page *page, unsigned int offset, unsigned int bytes) |
| 812 | { |
Mel Gorman | d56b4dd | 2012-07-31 16:45:06 -0700 | [diff] [blame] | 813 | struct inode *inode = page_file_mapping(page)->host; |
Trond Myklebust | e7d3906 | 2008-06-13 12:12:32 -0400 | [diff] [blame] | 814 | struct nfs_page *req; |
Trond Myklebust | e7d3906 | 2008-06-13 12:12:32 -0400 | [diff] [blame] | 815 | |
| 816 | req = nfs_try_to_update_request(inode, page, offset, bytes); |
| 817 | if (req != NULL) |
| 818 | goto out; |
| 819 | req = nfs_create_request(ctx, inode, page, offset, bytes); |
| 820 | if (IS_ERR(req)) |
| 821 | goto out; |
Fred Isaman | d6d6dc7 | 2012-03-08 17:29:35 -0500 | [diff] [blame] | 822 | nfs_inode_add_request(inode, req); |
Trond Myklebust | efc91ed | 2008-06-10 18:31:00 -0400 | [diff] [blame] | 823 | out: |
Trond Myklebust | 61e930a | 2007-10-18 17:08:05 -0400 | [diff] [blame] | 824 | return req; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | } |
| 826 | |
Trond Myklebust | e7d3906 | 2008-06-13 12:12:32 -0400 | [diff] [blame] | 827 | static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page, |
| 828 | unsigned int offset, unsigned int count) |
| 829 | { |
| 830 | struct nfs_page *req; |
| 831 | |
| 832 | req = nfs_setup_write_request(ctx, page, offset, count); |
| 833 | if (IS_ERR(req)) |
| 834 | return PTR_ERR(req); |
| 835 | /* Update file length */ |
| 836 | nfs_grow_file(page, offset, count); |
| 837 | nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes); |
Trond Myklebust | a6305dd | 2010-04-09 19:07:08 -0400 | [diff] [blame] | 838 | nfs_mark_request_dirty(req); |
Trond Myklebust | 1d1afcb | 2012-05-09 14:04:55 -0400 | [diff] [blame] | 839 | nfs_unlock_and_release_request(req); |
Trond Myklebust | e7d3906 | 2008-06-13 12:12:32 -0400 | [diff] [blame] | 840 | return 0; |
| 841 | } |
| 842 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 | int nfs_flush_incompatible(struct file *file, struct page *page) |
| 844 | { |
Trond Myklebust | cd3758e | 2007-08-10 17:44:32 -0400 | [diff] [blame] | 845 | struct nfs_open_context *ctx = nfs_file_open_context(file); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 846 | struct nfs_page *req; |
Trond Myklebust | 1a54533 | 2006-12-05 00:35:40 -0500 | [diff] [blame] | 847 | int do_flush, status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | /* |
| 849 | * Look for a request corresponding to this page. If there |
| 850 | * is one, and it belongs to another file, we flush it out |
| 851 | * before we try to copy anything into the page. Do this |
| 852 | * due to the lack of an ACCESS-type call in NFSv2. |
| 853 | * Also do the same if we find a request from an existing |
| 854 | * dropped page. |
| 855 | */ |
Trond Myklebust | 1a54533 | 2006-12-05 00:35:40 -0500 | [diff] [blame] | 856 | do { |
| 857 | req = nfs_page_find_request(page); |
| 858 | if (req == NULL) |
| 859 | return 0; |
Trond Myklebust | f11ac8d | 2010-06-25 16:35:53 -0400 | [diff] [blame] | 860 | do_flush = req->wb_page != page || req->wb_context != ctx || |
| 861 | req->wb_lock_context->lockowner != current->files || |
| 862 | req->wb_lock_context->pid != current->tgid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 | nfs_release_request(req); |
Trond Myklebust | 1a54533 | 2006-12-05 00:35:40 -0500 | [diff] [blame] | 864 | if (!do_flush) |
| 865 | return 0; |
Mel Gorman | d56b4dd | 2012-07-31 16:45:06 -0700 | [diff] [blame] | 866 | status = nfs_wb_page(page_file_mapping(page)->host, page); |
Trond Myklebust | 1a54533 | 2006-12-05 00:35:40 -0500 | [diff] [blame] | 867 | } while (status == 0); |
| 868 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | } |
| 870 | |
| 871 | /* |
Trond Myklebust | 5d47a35 | 2008-02-07 17:24:07 -0500 | [diff] [blame] | 872 | * If the page cache is marked as unsafe or invalid, then we can't rely on |
| 873 | * the PageUptodate() flag. In this case, we will need to turn off |
| 874 | * write optimisations that depend on the page contents being correct. |
| 875 | */ |
Trond Myklebust | 8d197a5 | 2012-04-29 12:50:01 -0400 | [diff] [blame] | 876 | static bool nfs_write_pageuptodate(struct page *page, struct inode *inode) |
Trond Myklebust | 5d47a35 | 2008-02-07 17:24:07 -0500 | [diff] [blame] | 877 | { |
Trond Myklebust | 8d197a5 | 2012-04-29 12:50:01 -0400 | [diff] [blame] | 878 | if (nfs_have_delegated_attributes(inode)) |
| 879 | goto out; |
| 880 | if (NFS_I(inode)->cache_validity & NFS_INO_REVAL_PAGECACHE) |
| 881 | return false; |
| 882 | out: |
| 883 | return PageUptodate(page) != 0; |
Trond Myklebust | 5d47a35 | 2008-02-07 17:24:07 -0500 | [diff] [blame] | 884 | } |
| 885 | |
| 886 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 | * Update and possibly write a cached page of an NFS file. |
| 888 | * |
| 889 | * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad |
| 890 | * things with a page scheduled for an RPC call (e.g. invalidate it). |
| 891 | */ |
| 892 | int nfs_updatepage(struct file *file, struct page *page, |
| 893 | unsigned int offset, unsigned int count) |
| 894 | { |
Trond Myklebust | cd3758e | 2007-08-10 17:44:32 -0400 | [diff] [blame] | 895 | struct nfs_open_context *ctx = nfs_file_open_context(file); |
Mel Gorman | d56b4dd | 2012-07-31 16:45:06 -0700 | [diff] [blame] | 896 | struct inode *inode = page_file_mapping(page)->host; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 897 | int status = 0; |
| 898 | |
Chuck Lever | 91d5b47 | 2006-03-20 13:44:14 -0500 | [diff] [blame] | 899 | nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE); |
| 900 | |
Chuck Lever | 48186c7 | 2008-06-11 17:56:05 -0400 | [diff] [blame] | 901 | dprintk("NFS: nfs_updatepage(%s/%s %d@%lld)\n", |
Josef "Jeff" Sipek | 01cce93 | 2006-12-08 02:36:40 -0800 | [diff] [blame] | 902 | file->f_path.dentry->d_parent->d_name.name, |
| 903 | file->f_path.dentry->d_name.name, count, |
Mel Gorman | d56b4dd | 2012-07-31 16:45:06 -0700 | [diff] [blame] | 904 | (long long)(page_file_offset(page) + offset)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 | /* 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] | 907 | * is up to date, it may be more efficient to extend the write |
| 908 | * to cover the entire page in order to avoid fragmentation |
| 909 | * inefficiencies. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 910 | */ |
Trond Myklebust | 5d47a35 | 2008-02-07 17:24:07 -0500 | [diff] [blame] | 911 | if (nfs_write_pageuptodate(page, inode) && |
| 912 | inode->i_flock == NULL && |
Christoph Hellwig | 6b2f3d1 | 2009-10-27 11:05:28 +0100 | [diff] [blame] | 913 | !(file->f_flags & O_DSYNC)) { |
Trond Myklebust | 49a70f2 | 2006-12-05 00:35:38 -0500 | [diff] [blame] | 914 | count = max(count + offset, nfs_page_length(page)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | offset = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 916 | } |
| 917 | |
Trond Myklebust | e21195a | 2006-12-05 00:35:39 -0500 | [diff] [blame] | 918 | status = nfs_writepage_setup(ctx, page, offset, count); |
Trond Myklebust | 03fa9e8 | 2008-06-05 16:02:35 -0400 | [diff] [blame] | 919 | if (status < 0) |
| 920 | nfs_set_pageerror(page); |
Trond Myklebust | 59b7c05 | 2011-10-17 18:22:55 -0700 | [diff] [blame] | 921 | else |
| 922 | __set_page_dirty_nobuffers(page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | |
Chuck Lever | 48186c7 | 2008-06-11 17:56:05 -0400 | [diff] [blame] | 924 | dprintk("NFS: nfs_updatepage returns %d (isize %lld)\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | status, (long long)i_size_read(inode)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | return status; |
| 927 | } |
| 928 | |
Trond Myklebust | 3ff7576 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 929 | static int flush_task_priority(int how) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | { |
| 931 | switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) { |
| 932 | case FLUSH_HIGHPRI: |
| 933 | return RPC_PRIORITY_HIGH; |
| 934 | case FLUSH_LOWPRI: |
| 935 | return RPC_PRIORITY_LOW; |
| 936 | } |
| 937 | return RPC_PRIORITY_NORMAL; |
| 938 | } |
| 939 | |
Fred Isaman | c5996c4 | 2012-04-20 14:47:41 -0400 | [diff] [blame] | 940 | int nfs_initiate_write(struct rpc_clnt *clnt, |
| 941 | struct nfs_write_data *data, |
Andy Adamson | d138d5d | 2011-03-03 15:13:41 +0000 | [diff] [blame] | 942 | const struct rpc_call_ops *call_ops, |
Andy Adamson | 9f0ec176 | 2012-04-27 17:53:44 -0400 | [diff] [blame] | 943 | int how, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | { |
Fred Isaman | cd84160 | 2012-04-20 14:47:44 -0400 | [diff] [blame] | 945 | struct inode *inode = data->header->inode; |
Trond Myklebust | 3ff7576 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 946 | int priority = flush_task_priority(how); |
Trond Myklebust | 0773769 | 2007-10-25 18:42:54 -0400 | [diff] [blame] | 947 | struct rpc_task *task; |
Trond Myklebust | bdc7f02 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 948 | struct rpc_message msg = { |
| 949 | .rpc_argp = &data->args, |
| 950 | .rpc_resp = &data->res, |
Fred Isaman | cd84160 | 2012-04-20 14:47:44 -0400 | [diff] [blame] | 951 | .rpc_cred = data->header->cred, |
Trond Myklebust | bdc7f02 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 952 | }; |
Trond Myklebust | 84115e1 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 953 | struct rpc_task_setup task_setup_data = { |
Andy Adamson | d138d5d | 2011-03-03 15:13:41 +0000 | [diff] [blame] | 954 | .rpc_client = clnt, |
Trond Myklebust | 0773769 | 2007-10-25 18:42:54 -0400 | [diff] [blame] | 955 | .task = &data->task, |
Trond Myklebust | bdc7f02 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 956 | .rpc_message = &msg, |
Trond Myklebust | 84115e1 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 957 | .callback_ops = call_ops, |
| 958 | .callback_data = data, |
Trond Myklebust | 101070c | 2008-02-19 20:04:23 -0500 | [diff] [blame] | 959 | .workqueue = nfsiod_workqueue, |
Andy Adamson | 9f0ec176 | 2012-04-27 17:53:44 -0400 | [diff] [blame] | 960 | .flags = RPC_TASK_ASYNC | flags, |
Trond Myklebust | 3ff7576 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 961 | .priority = priority, |
Trond Myklebust | 84115e1 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 962 | }; |
Trond Myklebust | 2c61be0 | 2010-04-09 19:54:50 -0400 | [diff] [blame] | 963 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | |
Andy Adamson | d138d5d | 2011-03-03 15:13:41 +0000 | [diff] [blame] | 965 | /* Set up the initial task struct. */ |
| 966 | NFS_PROTO(inode)->write_setup(data, &msg); |
| 967 | |
| 968 | dprintk("NFS: %5u initiated write call " |
| 969 | "(req %s/%lld, %u bytes @ offset %llu)\n", |
| 970 | data->task.tk_pid, |
| 971 | inode->i_sb->s_id, |
| 972 | (long long)NFS_FILEID(inode), |
| 973 | data->args.count, |
| 974 | (unsigned long long)data->args.offset); |
| 975 | |
| 976 | task = rpc_run_task(&task_setup_data); |
| 977 | if (IS_ERR(task)) { |
| 978 | ret = PTR_ERR(task); |
| 979 | goto out; |
| 980 | } |
| 981 | if (how & FLUSH_SYNC) { |
| 982 | ret = rpc_wait_for_completion_task(task); |
| 983 | if (ret == 0) |
| 984 | ret = task->tk_status; |
| 985 | } |
| 986 | rpc_put_task(task); |
| 987 | out: |
| 988 | return ret; |
| 989 | } |
Fred Isaman | a69aef1 | 2011-03-03 15:13:47 +0000 | [diff] [blame] | 990 | EXPORT_SYMBOL_GPL(nfs_initiate_write); |
Andy Adamson | d138d5d | 2011-03-03 15:13:41 +0000 | [diff] [blame] | 991 | |
| 992 | /* |
| 993 | * Set up the argument/result storage required for the RPC call. |
| 994 | */ |
Fred Isaman | 6c75dc0 | 2012-04-20 14:47:47 -0400 | [diff] [blame] | 995 | static void nfs_write_rpcsetup(struct nfs_write_data *data, |
Andy Adamson | d138d5d | 2011-03-03 15:13:41 +0000 | [diff] [blame] | 996 | unsigned int count, unsigned int offset, |
Fred Isaman | ea2cf22 | 2012-04-20 14:47:53 -0400 | [diff] [blame] | 997 | int how, struct nfs_commit_info *cinfo) |
Andy Adamson | d138d5d | 2011-03-03 15:13:41 +0000 | [diff] [blame] | 998 | { |
Fred Isaman | 6c75dc0 | 2012-04-20 14:47:47 -0400 | [diff] [blame] | 999 | struct nfs_page *req = data->header->req; |
Andy Adamson | d138d5d | 2011-03-03 15:13:41 +0000 | [diff] [blame] | 1000 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1001 | /* Set up the RPC argument and reply structs |
| 1002 | * NB: take care not to mess about with data->commit et al. */ |
| 1003 | |
Fred Isaman | 6c75dc0 | 2012-04-20 14:47:47 -0400 | [diff] [blame] | 1004 | data->args.fh = NFS_FH(data->header->inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1005 | data->args.offset = req_offset(req) + offset; |
Boaz Harrosh | 2bea038 | 2011-06-16 11:35:46 -0400 | [diff] [blame] | 1006 | /* pnfs_set_layoutcommit needs this */ |
| 1007 | data->mds_offset = data->args.offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1008 | data->args.pgbase = req->wb_pgbase + offset; |
Fred Isaman | 30dd374 | 2012-04-20 14:47:45 -0400 | [diff] [blame] | 1009 | data->args.pages = data->pages.pagevec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1010 | data->args.count = count; |
Trond Myklebust | 383ba71 | 2008-02-19 20:04:20 -0500 | [diff] [blame] | 1011 | data->args.context = get_nfs_open_context(req->wb_context); |
Trond Myklebust | f11ac8d | 2010-06-25 16:35:53 -0400 | [diff] [blame] | 1012 | data->args.lock_context = req->wb_lock_context; |
Trond Myklebust | bdc7f02 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 1013 | data->args.stable = NFS_UNSTABLE; |
Trond Myklebust | 87ed5eb | 2011-07-12 13:42:01 -0400 | [diff] [blame] | 1014 | switch (how & (FLUSH_STABLE | FLUSH_COND_STABLE)) { |
| 1015 | case 0: |
| 1016 | break; |
| 1017 | case FLUSH_COND_STABLE: |
Fred Isaman | ea2cf22 | 2012-04-20 14:47:53 -0400 | [diff] [blame] | 1018 | if (nfs_reqs_to_commit(cinfo)) |
Trond Myklebust | 87ed5eb | 2011-07-12 13:42:01 -0400 | [diff] [blame] | 1019 | break; |
| 1020 | default: |
| 1021 | data->args.stable = NFS_FILE_SYNC; |
Trond Myklebust | bdc7f02 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 1022 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1023 | |
| 1024 | data->res.fattr = &data->fattr; |
| 1025 | data->res.count = count; |
| 1026 | data->res.verf = &data->verf; |
Trond Myklebust | 0e574af | 2005-10-27 22:12:38 -0400 | [diff] [blame] | 1027 | nfs_fattr_init(&data->fattr); |
Trond Myklebust | 6e4efd5 | 2011-07-12 13:42:02 -0400 | [diff] [blame] | 1028 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1029 | |
Trond Myklebust | 6e4efd5 | 2011-07-12 13:42:02 -0400 | [diff] [blame] | 1030 | static int nfs_do_write(struct nfs_write_data *data, |
| 1031 | const struct rpc_call_ops *call_ops, |
Trond Myklebust | 6e4efd5 | 2011-07-12 13:42:02 -0400 | [diff] [blame] | 1032 | int how) |
| 1033 | { |
Fred Isaman | cd84160 | 2012-04-20 14:47:44 -0400 | [diff] [blame] | 1034 | struct inode *inode = data->header->inode; |
Andy Adamson | 0382b74 | 2011-03-03 15:13:45 +0000 | [diff] [blame] | 1035 | |
Andy Adamson | 9f0ec176 | 2012-04-27 17:53:44 -0400 | [diff] [blame] | 1036 | return nfs_initiate_write(NFS_CLIENT(inode), data, call_ops, how, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1037 | } |
| 1038 | |
Trond Myklebust | 275acaa | 2011-07-12 13:42:02 -0400 | [diff] [blame] | 1039 | static int nfs_do_multiple_writes(struct list_head *head, |
| 1040 | const struct rpc_call_ops *call_ops, |
Trond Myklebust | 275acaa | 2011-07-12 13:42:02 -0400 | [diff] [blame] | 1041 | int how) |
| 1042 | { |
| 1043 | struct nfs_write_data *data; |
| 1044 | int ret = 0; |
| 1045 | |
| 1046 | while (!list_empty(head)) { |
| 1047 | int ret2; |
| 1048 | |
Fred Isaman | 6c75dc0 | 2012-04-20 14:47:47 -0400 | [diff] [blame] | 1049 | data = list_first_entry(head, struct nfs_write_data, list); |
Trond Myklebust | 275acaa | 2011-07-12 13:42:02 -0400 | [diff] [blame] | 1050 | list_del_init(&data->list); |
| 1051 | |
Trond Myklebust | dce8129 | 2011-07-13 15:59:19 -0400 | [diff] [blame] | 1052 | ret2 = nfs_do_write(data, call_ops, how); |
Trond Myklebust | 275acaa | 2011-07-12 13:42:02 -0400 | [diff] [blame] | 1053 | if (ret == 0) |
| 1054 | ret = ret2; |
| 1055 | } |
| 1056 | return ret; |
| 1057 | } |
| 1058 | |
Fred | 6d884e8 | 2008-03-19 11:24:38 -0400 | [diff] [blame] | 1059 | /* If a nfs_flush_* function fails, it should remove reqs from @head and |
| 1060 | * call this on each, which will prepare them to be retried on next |
| 1061 | * writeback using standard nfs. |
| 1062 | */ |
| 1063 | static void nfs_redirty_request(struct nfs_page *req) |
| 1064 | { |
| 1065 | nfs_mark_request_dirty(req); |
Trond Myklebust | 1d1afcb | 2012-05-09 14:04:55 -0400 | [diff] [blame] | 1066 | nfs_unlock_request(req); |
Trond Myklebust | d1182b3 | 2012-05-09 13:37:43 -0400 | [diff] [blame] | 1067 | nfs_end_page_writeback(req->wb_page); |
Trond Myklebust | 3aff4eb | 2012-05-09 14:30:35 -0400 | [diff] [blame] | 1068 | nfs_release_request(req); |
Fred | 6d884e8 | 2008-03-19 11:24:38 -0400 | [diff] [blame] | 1069 | } |
| 1070 | |
Fred Isaman | 061ae2e | 2012-04-20 14:47:48 -0400 | [diff] [blame] | 1071 | static void nfs_async_write_error(struct list_head *head) |
Fred Isaman | 6c75dc0 | 2012-04-20 14:47:47 -0400 | [diff] [blame] | 1072 | { |
| 1073 | struct nfs_page *req; |
| 1074 | |
| 1075 | while (!list_empty(head)) { |
| 1076 | req = nfs_list_entry(head->next); |
| 1077 | nfs_list_remove_request(req); |
| 1078 | nfs_redirty_request(req); |
| 1079 | } |
| 1080 | } |
| 1081 | |
Fred Isaman | 061ae2e | 2012-04-20 14:47:48 -0400 | [diff] [blame] | 1082 | static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops = { |
| 1083 | .error_cleanup = nfs_async_write_error, |
| 1084 | .completion = nfs_write_completion, |
| 1085 | }; |
| 1086 | |
Trond Myklebust | 25b11dc | 2012-05-01 12:07:22 -0400 | [diff] [blame] | 1087 | static void nfs_flush_error(struct nfs_pageio_descriptor *desc, |
| 1088 | struct nfs_pgio_header *hdr) |
| 1089 | { |
| 1090 | set_bit(NFS_IOHDR_REDO, &hdr->flags); |
| 1091 | while (!list_empty(&hdr->rpc_list)) { |
| 1092 | struct nfs_write_data *data = list_first_entry(&hdr->rpc_list, |
| 1093 | struct nfs_write_data, list); |
| 1094 | list_del(&data->list); |
| 1095 | nfs_writedata_release(data); |
| 1096 | } |
| 1097 | desc->pg_completion_ops->error_cleanup(&desc->pg_list); |
| 1098 | } |
| 1099 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1100 | /* |
| 1101 | * Generate multiple small requests to write out a single |
| 1102 | * contiguous dirty area on one page. |
| 1103 | */ |
Fred Isaman | 6c75dc0 | 2012-04-20 14:47:47 -0400 | [diff] [blame] | 1104 | static int nfs_flush_multi(struct nfs_pageio_descriptor *desc, |
| 1105 | struct nfs_pgio_header *hdr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1106 | { |
Fred Isaman | 6c75dc0 | 2012-04-20 14:47:47 -0400 | [diff] [blame] | 1107 | struct nfs_page *req = hdr->req; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1108 | struct page *page = req->wb_page; |
| 1109 | struct nfs_write_data *data; |
Trond Myklebust | d097971 | 2011-07-12 13:42:02 -0400 | [diff] [blame] | 1110 | size_t wsize = desc->pg_bsize, nbytes; |
Trond Myklebust | e9f7bee | 2006-09-08 09:48:54 -0700 | [diff] [blame] | 1111 | unsigned int offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1112 | int requests = 0; |
Fred Isaman | ea2cf22 | 2012-04-20 14:47:53 -0400 | [diff] [blame] | 1113 | struct nfs_commit_info cinfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1114 | |
Fred Isaman | ea2cf22 | 2012-04-20 14:47:53 -0400 | [diff] [blame] | 1115 | nfs_init_cinfo(&cinfo, desc->pg_inode, desc->pg_dreq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1116 | |
Trond Myklebust | b31268a | 2011-03-21 17:02:00 -0400 | [diff] [blame] | 1117 | if ((desc->pg_ioflags & FLUSH_COND_STABLE) && |
Fred Isaman | ea2cf22 | 2012-04-20 14:47:53 -0400 | [diff] [blame] | 1118 | (desc->pg_moreio || nfs_reqs_to_commit(&cinfo) || |
Trond Myklebust | b31268a | 2011-03-21 17:02:00 -0400 | [diff] [blame] | 1119 | desc->pg_count > wsize)) |
| 1120 | desc->pg_ioflags &= ~FLUSH_COND_STABLE; |
| 1121 | |
| 1122 | |
Trond Myklebust | 275acaa | 2011-07-12 13:42:02 -0400 | [diff] [blame] | 1123 | offset = 0; |
Fred Isaman | c76069b | 2011-03-03 15:13:48 +0000 | [diff] [blame] | 1124 | nbytes = desc->pg_count; |
Trond Myklebust | e9f7bee | 2006-09-08 09:48:54 -0700 | [diff] [blame] | 1125 | do { |
| 1126 | size_t len = min(nbytes, wsize); |
| 1127 | |
Fred Isaman | 6c75dc0 | 2012-04-20 14:47:47 -0400 | [diff] [blame] | 1128 | data = nfs_writedata_alloc(hdr, 1); |
Trond Myklebust | 25b11dc | 2012-05-01 12:07:22 -0400 | [diff] [blame] | 1129 | if (!data) { |
| 1130 | nfs_flush_error(desc, hdr); |
| 1131 | return -ENOMEM; |
| 1132 | } |
Fred Isaman | 30dd374 | 2012-04-20 14:47:45 -0400 | [diff] [blame] | 1133 | data->pages.pagevec[0] = page; |
Fred Isaman | ea2cf22 | 2012-04-20 14:47:53 -0400 | [diff] [blame] | 1134 | nfs_write_rpcsetup(data, len, offset, desc->pg_ioflags, &cinfo); |
Fred Isaman | 6c75dc0 | 2012-04-20 14:47:47 -0400 | [diff] [blame] | 1135 | list_add(&data->list, &hdr->rpc_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1136 | requests++; |
Trond Myklebust | e9f7bee | 2006-09-08 09:48:54 -0700 | [diff] [blame] | 1137 | nbytes -= len; |
Trond Myklebust | 275acaa | 2011-07-12 13:42:02 -0400 | [diff] [blame] | 1138 | offset += len; |
Trond Myklebust | e9f7bee | 2006-09-08 09:48:54 -0700 | [diff] [blame] | 1139 | } while (nbytes != 0); |
Trond Myklebust | 25b11dc | 2012-05-01 12:07:22 -0400 | [diff] [blame] | 1140 | nfs_list_remove_request(req); |
| 1141 | nfs_list_add_request(req, &hdr->pages); |
Fred Isaman | 6c75dc0 | 2012-04-20 14:47:47 -0400 | [diff] [blame] | 1142 | desc->pg_rpc_callops = &nfs_write_common_ops; |
Trond Myklebust | 25b11dc | 2012-05-01 12:07:22 -0400 | [diff] [blame] | 1143 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1144 | } |
| 1145 | |
| 1146 | /* |
| 1147 | * Create an RPC task for the given write request and kick it. |
| 1148 | * The page must have been locked by the caller. |
| 1149 | * |
| 1150 | * It may happen that the page we're passed is not marked dirty. |
| 1151 | * This is the case if nfs_updatepage detects a conflicting request |
| 1152 | * that has been written but not committed. |
| 1153 | */ |
Fred Isaman | 6c75dc0 | 2012-04-20 14:47:47 -0400 | [diff] [blame] | 1154 | static int nfs_flush_one(struct nfs_pageio_descriptor *desc, |
| 1155 | struct nfs_pgio_header *hdr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1156 | { |
| 1157 | struct nfs_page *req; |
| 1158 | struct page **pages; |
| 1159 | struct nfs_write_data *data; |
Fred Isaman | c76069b | 2011-03-03 15:13:48 +0000 | [diff] [blame] | 1160 | struct list_head *head = &desc->pg_list; |
Fred Isaman | ea2cf22 | 2012-04-20 14:47:53 -0400 | [diff] [blame] | 1161 | struct nfs_commit_info cinfo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1162 | |
Fred Isaman | 6c75dc0 | 2012-04-20 14:47:47 -0400 | [diff] [blame] | 1163 | data = nfs_writedata_alloc(hdr, nfs_page_array_len(desc->pg_base, |
| 1164 | desc->pg_count)); |
| 1165 | if (!data) { |
Trond Myklebust | 25b11dc | 2012-05-01 12:07:22 -0400 | [diff] [blame] | 1166 | nfs_flush_error(desc, hdr); |
| 1167 | return -ENOMEM; |
Fred Isaman | 44b8379 | 2011-03-03 15:13:44 +0000 | [diff] [blame] | 1168 | } |
Fred Isaman | 6c75dc0 | 2012-04-20 14:47:47 -0400 | [diff] [blame] | 1169 | |
Fred Isaman | ea2cf22 | 2012-04-20 14:47:53 -0400 | [diff] [blame] | 1170 | nfs_init_cinfo(&cinfo, desc->pg_inode, desc->pg_dreq); |
Fred Isaman | 30dd374 | 2012-04-20 14:47:45 -0400 | [diff] [blame] | 1171 | pages = data->pages.pagevec; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1172 | while (!list_empty(head)) { |
| 1173 | req = nfs_list_entry(head->next); |
| 1174 | nfs_list_remove_request(req); |
Fred Isaman | 6c75dc0 | 2012-04-20 14:47:47 -0400 | [diff] [blame] | 1175 | nfs_list_add_request(req, &hdr->pages); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1176 | *pages++ = req->wb_page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1177 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1178 | |
Trond Myklebust | b31268a | 2011-03-21 17:02:00 -0400 | [diff] [blame] | 1179 | if ((desc->pg_ioflags & FLUSH_COND_STABLE) && |
Fred Isaman | ea2cf22 | 2012-04-20 14:47:53 -0400 | [diff] [blame] | 1180 | (desc->pg_moreio || nfs_reqs_to_commit(&cinfo))) |
Trond Myklebust | b31268a | 2011-03-21 17:02:00 -0400 | [diff] [blame] | 1181 | desc->pg_ioflags &= ~FLUSH_COND_STABLE; |
| 1182 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1183 | /* Set up the argument struct */ |
Fred Isaman | ea2cf22 | 2012-04-20 14:47:53 -0400 | [diff] [blame] | 1184 | nfs_write_rpcsetup(data, desc->pg_count, 0, desc->pg_ioflags, &cinfo); |
Fred Isaman | 6c75dc0 | 2012-04-20 14:47:47 -0400 | [diff] [blame] | 1185 | list_add(&data->list, &hdr->rpc_list); |
| 1186 | desc->pg_rpc_callops = &nfs_write_common_ops; |
Trond Myklebust | 25b11dc | 2012-05-01 12:07:22 -0400 | [diff] [blame] | 1187 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1188 | } |
| 1189 | |
Fred Isaman | 6c75dc0 | 2012-04-20 14:47:47 -0400 | [diff] [blame] | 1190 | int nfs_generic_flush(struct nfs_pageio_descriptor *desc, |
| 1191 | struct nfs_pgio_header *hdr) |
Trond Myklebust | dce8129 | 2011-07-13 15:59:19 -0400 | [diff] [blame] | 1192 | { |
| 1193 | if (desc->pg_bsize < PAGE_CACHE_SIZE) |
Fred Isaman | 6c75dc0 | 2012-04-20 14:47:47 -0400 | [diff] [blame] | 1194 | return nfs_flush_multi(desc, hdr); |
| 1195 | return nfs_flush_one(desc, hdr); |
Trond Myklebust | dce8129 | 2011-07-13 15:59:19 -0400 | [diff] [blame] | 1196 | } |
| 1197 | |
| 1198 | static int nfs_generic_pg_writepages(struct nfs_pageio_descriptor *desc) |
Trond Myklebust | 1751c36 | 2011-06-10 13:30:23 -0400 | [diff] [blame] | 1199 | { |
Fred Isaman | 6c75dc0 | 2012-04-20 14:47:47 -0400 | [diff] [blame] | 1200 | struct nfs_write_header *whdr; |
| 1201 | struct nfs_pgio_header *hdr; |
Trond Myklebust | 275acaa | 2011-07-12 13:42:02 -0400 | [diff] [blame] | 1202 | int ret; |
| 1203 | |
Fred Isaman | 6c75dc0 | 2012-04-20 14:47:47 -0400 | [diff] [blame] | 1204 | whdr = nfs_writehdr_alloc(); |
| 1205 | if (!whdr) { |
Trond Myklebust | 9b5415b | 2012-04-27 14:31:47 -0400 | [diff] [blame] | 1206 | desc->pg_completion_ops->error_cleanup(&desc->pg_list); |
Fred Isaman | 6c75dc0 | 2012-04-20 14:47:47 -0400 | [diff] [blame] | 1207 | return -ENOMEM; |
| 1208 | } |
| 1209 | hdr = &whdr->header; |
| 1210 | nfs_pgheader_init(desc, hdr, nfs_writehdr_free); |
| 1211 | atomic_inc(&hdr->refcnt); |
| 1212 | ret = nfs_generic_flush(desc, hdr); |
Trond Myklebust | 50828d7 | 2011-07-12 13:42:02 -0400 | [diff] [blame] | 1213 | if (ret == 0) |
Fred Isaman | 6c75dc0 | 2012-04-20 14:47:47 -0400 | [diff] [blame] | 1214 | ret = nfs_do_multiple_writes(&hdr->rpc_list, |
| 1215 | desc->pg_rpc_callops, |
| 1216 | desc->pg_ioflags); |
Fred Isaman | 6c75dc0 | 2012-04-20 14:47:47 -0400 | [diff] [blame] | 1217 | if (atomic_dec_and_test(&hdr->refcnt)) |
Fred Isaman | 061ae2e | 2012-04-20 14:47:48 -0400 | [diff] [blame] | 1218 | hdr->completion_ops->completion(hdr); |
Trond Myklebust | 275acaa | 2011-07-12 13:42:02 -0400 | [diff] [blame] | 1219 | return ret; |
Trond Myklebust | 1751c36 | 2011-06-10 13:30:23 -0400 | [diff] [blame] | 1220 | } |
Trond Myklebust | 1751c36 | 2011-06-10 13:30:23 -0400 | [diff] [blame] | 1221 | |
| 1222 | static const struct nfs_pageio_ops nfs_pageio_write_ops = { |
| 1223 | .pg_test = nfs_generic_pg_test, |
| 1224 | .pg_doio = nfs_generic_pg_writepages, |
| 1225 | }; |
| 1226 | |
Bryan Schumaker | 57208fa | 2012-06-20 15:53:48 -0400 | [diff] [blame] | 1227 | void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio, |
Fred Isaman | 061ae2e | 2012-04-20 14:47:48 -0400 | [diff] [blame] | 1228 | struct inode *inode, int ioflags, |
| 1229 | const struct nfs_pgio_completion_ops *compl_ops) |
Trond Myklebust | 1751c36 | 2011-06-10 13:30:23 -0400 | [diff] [blame] | 1230 | { |
Fred Isaman | 061ae2e | 2012-04-20 14:47:48 -0400 | [diff] [blame] | 1231 | nfs_pageio_init(pgio, inode, &nfs_pageio_write_ops, compl_ops, |
Trond Myklebust | 1751c36 | 2011-06-10 13:30:23 -0400 | [diff] [blame] | 1232 | NFS_SERVER(inode)->wsize, ioflags); |
| 1233 | } |
| 1234 | |
Trond Myklebust | dce8129 | 2011-07-13 15:59:19 -0400 | [diff] [blame] | 1235 | void nfs_pageio_reset_write_mds(struct nfs_pageio_descriptor *pgio) |
| 1236 | { |
| 1237 | pgio->pg_ops = &nfs_pageio_write_ops; |
| 1238 | pgio->pg_bsize = NFS_SERVER(pgio->pg_inode)->wsize; |
| 1239 | } |
Trond Myklebust | 1f94535 | 2011-07-13 15:59:57 -0400 | [diff] [blame] | 1240 | EXPORT_SYMBOL_GPL(nfs_pageio_reset_write_mds); |
Trond Myklebust | dce8129 | 2011-07-13 15:59:19 -0400 | [diff] [blame] | 1241 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1242 | |
Andy Adamson | def6ed7 | 2009-04-01 09:22:26 -0400 | [diff] [blame] | 1243 | void nfs_write_prepare(struct rpc_task *task, void *calldata) |
| 1244 | { |
| 1245 | struct nfs_write_data *data = calldata; |
Fred Isaman | cd84160 | 2012-04-20 14:47:44 -0400 | [diff] [blame] | 1246 | NFS_PROTO(data->header->inode)->write_rpc_prepare(task, data); |
Andy Adamson | def6ed7 | 2009-04-01 09:22:26 -0400 | [diff] [blame] | 1247 | } |
Andy Adamson | def6ed7 | 2009-04-01 09:22:26 -0400 | [diff] [blame] | 1248 | |
Fred Isaman | 0b7c015 | 2012-04-20 14:47:39 -0400 | [diff] [blame] | 1249 | void nfs_commit_prepare(struct rpc_task *task, void *calldata) |
| 1250 | { |
| 1251 | struct nfs_commit_data *data = calldata; |
| 1252 | |
| 1253 | NFS_PROTO(data->inode)->commit_rpc_prepare(task, data); |
| 1254 | } |
| 1255 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1256 | /* |
| 1257 | * Handle a write reply that flushes a whole page. |
| 1258 | * |
| 1259 | * FIXME: There is an inherent race with invalidate_inode_pages and |
| 1260 | * writebacks since the page->count is kept > 1 for as long |
| 1261 | * as the page has a write request pending. |
| 1262 | */ |
Fred Isaman | 6c75dc0 | 2012-04-20 14:47:47 -0400 | [diff] [blame] | 1263 | static void nfs_writeback_done_common(struct rpc_task *task, void *calldata) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1264 | { |
Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1265 | struct nfs_write_data *data = calldata; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1266 | |
Trond Myklebust | c9d8f89 | 2008-04-15 16:56:39 -0400 | [diff] [blame] | 1267 | nfs_writeback_done(task, data); |
| 1268 | } |
| 1269 | |
Fred Isaman | 6c75dc0 | 2012-04-20 14:47:47 -0400 | [diff] [blame] | 1270 | static void nfs_writeback_release_common(void *calldata) |
Trond Myklebust | c9d8f89 | 2008-04-15 16:56:39 -0400 | [diff] [blame] | 1271 | { |
| 1272 | struct nfs_write_data *data = calldata; |
Fred Isaman | cd84160 | 2012-04-20 14:47:44 -0400 | [diff] [blame] | 1273 | struct nfs_pgio_header *hdr = data->header; |
Trond Myklebust | e2fecb2 | 2012-01-06 08:57:46 -0500 | [diff] [blame] | 1274 | int status = data->task.tk_status; |
Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1275 | |
Fred Isaman | 6c75dc0 | 2012-04-20 14:47:47 -0400 | [diff] [blame] | 1276 | if ((status >= 0) && nfs_write_need_commit(data)) { |
| 1277 | spin_lock(&hdr->lock); |
| 1278 | if (test_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags)) |
| 1279 | ; /* Do nothing */ |
| 1280 | else if (!test_and_set_bit(NFS_IOHDR_NEED_COMMIT, &hdr->flags)) |
Trond Myklebust | 9bce008 | 2012-06-05 18:32:03 -0400 | [diff] [blame] | 1281 | memcpy(hdr->verf, &data->verf, sizeof(*hdr->verf)); |
| 1282 | else if (memcmp(hdr->verf, &data->verf, sizeof(*hdr->verf))) |
Fred Isaman | 6c75dc0 | 2012-04-20 14:47:47 -0400 | [diff] [blame] | 1283 | set_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags); |
| 1284 | spin_unlock(&hdr->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1285 | } |
Fred Isaman | cd84160 | 2012-04-20 14:47:44 -0400 | [diff] [blame] | 1286 | nfs_writedata_release(data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1287 | } |
| 1288 | |
Fred Isaman | 6c75dc0 | 2012-04-20 14:47:47 -0400 | [diff] [blame] | 1289 | static const struct rpc_call_ops nfs_write_common_ops = { |
Andy Adamson | def6ed7 | 2009-04-01 09:22:26 -0400 | [diff] [blame] | 1290 | .rpc_call_prepare = nfs_write_prepare, |
Fred Isaman | 6c75dc0 | 2012-04-20 14:47:47 -0400 | [diff] [blame] | 1291 | .rpc_call_done = nfs_writeback_done_common, |
| 1292 | .rpc_release = nfs_writeback_release_common, |
Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1293 | }; |
| 1294 | |
| 1295 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1296 | /* |
| 1297 | * This function is called when the WRITE call is complete. |
| 1298 | */ |
Fred Isaman | 1360289 | 2011-02-11 15:42:38 +0000 | [diff] [blame] | 1299 | void nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1300 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1301 | struct nfs_writeargs *argp = &data->args; |
| 1302 | struct nfs_writeres *resp = &data->res; |
Fred Isaman | cd84160 | 2012-04-20 14:47:44 -0400 | [diff] [blame] | 1303 | struct inode *inode = data->header->inode; |
Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1304 | int status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1305 | |
Chuck Lever | a3f565b | 2007-01-31 12:14:01 -0500 | [diff] [blame] | 1306 | dprintk("NFS: %5u nfs_writeback_done (status %d)\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1307 | task->tk_pid, task->tk_status); |
| 1308 | |
Chuck Lever | f551e44 | 2006-09-20 14:33:04 -0400 | [diff] [blame] | 1309 | /* |
| 1310 | * ->write_done will attempt to use post-op attributes to detect |
| 1311 | * conflicting writes by other clients. A strict interpretation |
| 1312 | * of close-to-open would allow us to continue caching even if |
| 1313 | * another writer had changed the file, but some applications |
| 1314 | * depend on tighter cache coherency when writing. |
| 1315 | */ |
Fred Isaman | cd84160 | 2012-04-20 14:47:44 -0400 | [diff] [blame] | 1316 | status = NFS_PROTO(inode)->write_done(task, data); |
Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1317 | if (status != 0) |
Fred Isaman | 1360289 | 2011-02-11 15:42:38 +0000 | [diff] [blame] | 1318 | return; |
Fred Isaman | cd84160 | 2012-04-20 14:47:44 -0400 | [diff] [blame] | 1319 | nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, resp->count); |
Chuck Lever | 91d5b47 | 2006-03-20 13:44:14 -0500 | [diff] [blame] | 1320 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1321 | #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) |
| 1322 | if (resp->verf->committed < argp->stable && task->tk_status >= 0) { |
| 1323 | /* We tried a write call, but the server did not |
| 1324 | * commit data to stable storage even though we |
| 1325 | * requested it. |
| 1326 | * Note: There is a known bug in Tru64 < 5.0 in which |
| 1327 | * the server reports NFS_DATA_SYNC, but performs |
| 1328 | * NFS_FILE_SYNC. We therefore implement this checking |
| 1329 | * as a dprintk() in order to avoid filling syslog. |
| 1330 | */ |
| 1331 | static unsigned long complain; |
| 1332 | |
Fred Isaman | a69aef1 | 2011-03-03 15:13:47 +0000 | [diff] [blame] | 1333 | /* Note this will print the MDS for a DS write */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1334 | if (time_before(complain, jiffies)) { |
Chuck Lever | 48186c7 | 2008-06-11 17:56:05 -0400 | [diff] [blame] | 1335 | dprintk("NFS: faulty NFS server %s:" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1336 | " (committed = %d) != (stable = %d)\n", |
Fred Isaman | cd84160 | 2012-04-20 14:47:44 -0400 | [diff] [blame] | 1337 | NFS_SERVER(inode)->nfs_client->cl_hostname, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1338 | resp->verf->committed, argp->stable); |
| 1339 | complain = jiffies + 300 * HZ; |
| 1340 | } |
| 1341 | } |
| 1342 | #endif |
Fred Isaman | 6c75dc0 | 2012-04-20 14:47:47 -0400 | [diff] [blame] | 1343 | if (task->tk_status < 0) |
| 1344 | nfs_set_pgio_error(data->header, task->tk_status, argp->offset); |
| 1345 | else if (resp->count < argp->count) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1346 | static unsigned long complain; |
| 1347 | |
Fred Isaman | 6c75dc0 | 2012-04-20 14:47:47 -0400 | [diff] [blame] | 1348 | /* This a short write! */ |
Fred Isaman | cd84160 | 2012-04-20 14:47:44 -0400 | [diff] [blame] | 1349 | nfs_inc_stats(inode, NFSIOS_SHORTWRITE); |
Chuck Lever | 91d5b47 | 2006-03-20 13:44:14 -0500 | [diff] [blame] | 1350 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1351 | /* Has the server at least made some progress? */ |
Fred Isaman | 6c75dc0 | 2012-04-20 14:47:47 -0400 | [diff] [blame] | 1352 | if (resp->count == 0) { |
| 1353 | if (time_before(complain, jiffies)) { |
| 1354 | printk(KERN_WARNING |
| 1355 | "NFS: Server wrote zero bytes, expected %u.\n", |
| 1356 | argp->count); |
| 1357 | complain = jiffies + 300 * HZ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1358 | } |
Fred Isaman | 6c75dc0 | 2012-04-20 14:47:47 -0400 | [diff] [blame] | 1359 | nfs_set_pgio_error(data->header, -EIO, argp->offset); |
| 1360 | task->tk_status = -EIO; |
Fred Isaman | 1360289 | 2011-02-11 15:42:38 +0000 | [diff] [blame] | 1361 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1362 | } |
Fred Isaman | 6c75dc0 | 2012-04-20 14:47:47 -0400 | [diff] [blame] | 1363 | /* Was this an NFSv2 write or an NFSv3 stable write? */ |
| 1364 | if (resp->verf->committed != NFS_UNSTABLE) { |
| 1365 | /* Resend from where the server left off */ |
| 1366 | data->mds_offset += resp->count; |
| 1367 | argp->offset += resp->count; |
| 1368 | argp->pgbase += resp->count; |
| 1369 | argp->count -= resp->count; |
| 1370 | } else { |
| 1371 | /* Resend as a stable write in order to avoid |
| 1372 | * headaches in the case of a server crash. |
| 1373 | */ |
| 1374 | argp->stable = NFS_FILE_SYNC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1375 | } |
Fred Isaman | 6c75dc0 | 2012-04-20 14:47:47 -0400 | [diff] [blame] | 1376 | rpc_restart_call_prepare(task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1377 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1378 | } |
| 1379 | |
| 1380 | |
| 1381 | #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) |
Trond Myklebust | 71d0a61 | 2010-04-22 15:35:57 -0400 | [diff] [blame] | 1382 | static int nfs_commit_set_lock(struct nfs_inode *nfsi, int may_wait) |
| 1383 | { |
Trond Myklebust | b8413f9 | 2011-03-21 15:37:01 -0400 | [diff] [blame] | 1384 | int ret; |
| 1385 | |
Trond Myklebust | 71d0a61 | 2010-04-22 15:35:57 -0400 | [diff] [blame] | 1386 | if (!test_and_set_bit(NFS_INO_COMMIT, &nfsi->flags)) |
| 1387 | return 1; |
Trond Myklebust | b8413f9 | 2011-03-21 15:37:01 -0400 | [diff] [blame] | 1388 | if (!may_wait) |
| 1389 | return 0; |
| 1390 | ret = out_of_line_wait_on_bit_lock(&nfsi->flags, |
| 1391 | NFS_INO_COMMIT, |
| 1392 | nfs_wait_bit_killable, |
| 1393 | TASK_KILLABLE); |
| 1394 | return (ret < 0) ? ret : 1; |
Trond Myklebust | 71d0a61 | 2010-04-22 15:35:57 -0400 | [diff] [blame] | 1395 | } |
| 1396 | |
Fred Isaman | f453a54 | 2012-04-20 14:47:54 -0400 | [diff] [blame] | 1397 | static void nfs_commit_clear_lock(struct nfs_inode *nfsi) |
Trond Myklebust | 71d0a61 | 2010-04-22 15:35:57 -0400 | [diff] [blame] | 1398 | { |
| 1399 | clear_bit(NFS_INO_COMMIT, &nfsi->flags); |
| 1400 | smp_mb__after_clear_bit(); |
| 1401 | wake_up_bit(&nfsi->flags, NFS_INO_COMMIT); |
| 1402 | } |
| 1403 | |
Fred Isaman | 0b7c015 | 2012-04-20 14:47:39 -0400 | [diff] [blame] | 1404 | void nfs_commitdata_release(struct nfs_commit_data *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1405 | { |
Fred Isaman | 0b7c015 | 2012-04-20 14:47:39 -0400 | [diff] [blame] | 1406 | put_nfs_open_context(data->context); |
| 1407 | nfs_commit_free(data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1408 | } |
Fred Isaman | e0c2b38 | 2011-03-23 13:27:53 +0000 | [diff] [blame] | 1409 | EXPORT_SYMBOL_GPL(nfs_commitdata_release); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1410 | |
Fred Isaman | 0b7c015 | 2012-04-20 14:47:39 -0400 | [diff] [blame] | 1411 | int nfs_initiate_commit(struct rpc_clnt *clnt, struct nfs_commit_data *data, |
Fred Isaman | 9ace33c | 2011-03-23 13:27:45 +0000 | [diff] [blame] | 1412 | const struct rpc_call_ops *call_ops, |
Andy Adamson | 9f0ec176 | 2012-04-27 17:53:44 -0400 | [diff] [blame] | 1413 | int how, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1414 | { |
Trond Myklebust | 0773769 | 2007-10-25 18:42:54 -0400 | [diff] [blame] | 1415 | struct rpc_task *task; |
Fred Isaman | 9ace33c | 2011-03-23 13:27:45 +0000 | [diff] [blame] | 1416 | int priority = flush_task_priority(how); |
Trond Myklebust | bdc7f02 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 1417 | struct rpc_message msg = { |
| 1418 | .rpc_argp = &data->args, |
| 1419 | .rpc_resp = &data->res, |
Fred Isaman | 9ace33c | 2011-03-23 13:27:45 +0000 | [diff] [blame] | 1420 | .rpc_cred = data->cred, |
Trond Myklebust | bdc7f02 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 1421 | }; |
Trond Myklebust | 84115e1 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 1422 | struct rpc_task_setup task_setup_data = { |
Trond Myklebust | 0773769 | 2007-10-25 18:42:54 -0400 | [diff] [blame] | 1423 | .task = &data->task, |
Fred Isaman | 9ace33c | 2011-03-23 13:27:45 +0000 | [diff] [blame] | 1424 | .rpc_client = clnt, |
Trond Myklebust | bdc7f02 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 1425 | .rpc_message = &msg, |
Fred Isaman | 9ace33c | 2011-03-23 13:27:45 +0000 | [diff] [blame] | 1426 | .callback_ops = call_ops, |
Trond Myklebust | 84115e1 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 1427 | .callback_data = data, |
Trond Myklebust | 101070c | 2008-02-19 20:04:23 -0500 | [diff] [blame] | 1428 | .workqueue = nfsiod_workqueue, |
Andy Adamson | 9f0ec176 | 2012-04-27 17:53:44 -0400 | [diff] [blame] | 1429 | .flags = RPC_TASK_ASYNC | flags, |
Trond Myklebust | 3ff7576 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 1430 | .priority = priority, |
Trond Myklebust | 84115e1 | 2007-07-14 15:39:59 -0400 | [diff] [blame] | 1431 | }; |
Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1432 | /* Set up the initial task struct. */ |
Fred Isaman | 9ace33c | 2011-03-23 13:27:45 +0000 | [diff] [blame] | 1433 | NFS_PROTO(data->inode)->commit_setup(data, &msg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1434 | |
Chuck Lever | a3f565b | 2007-01-31 12:14:01 -0500 | [diff] [blame] | 1435 | dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid); |
Trond Myklebust | bdc7f02 | 2007-07-14 15:40:00 -0400 | [diff] [blame] | 1436 | |
Trond Myklebust | 0773769 | 2007-10-25 18:42:54 -0400 | [diff] [blame] | 1437 | task = rpc_run_task(&task_setup_data); |
Trond Myklebust | dbae4c7 | 2008-04-14 14:54:53 -0400 | [diff] [blame] | 1438 | if (IS_ERR(task)) |
| 1439 | return PTR_ERR(task); |
Jeff Layton | d2224e7 | 2011-03-06 17:14:13 +0000 | [diff] [blame] | 1440 | if (how & FLUSH_SYNC) |
| 1441 | rpc_wait_for_completion_task(task); |
Trond Myklebust | dbae4c7 | 2008-04-14 14:54:53 -0400 | [diff] [blame] | 1442 | rpc_put_task(task); |
| 1443 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1444 | } |
Fred Isaman | e0c2b38 | 2011-03-23 13:27:53 +0000 | [diff] [blame] | 1445 | EXPORT_SYMBOL_GPL(nfs_initiate_commit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1446 | |
| 1447 | /* |
Fred Isaman | 9ace33c | 2011-03-23 13:27:45 +0000 | [diff] [blame] | 1448 | * Set up the argument/result storage required for the RPC call. |
| 1449 | */ |
Fred Isaman | 0b7c015 | 2012-04-20 14:47:39 -0400 | [diff] [blame] | 1450 | void nfs_init_commit(struct nfs_commit_data *data, |
Fred Isaman | f453a54 | 2012-04-20 14:47:54 -0400 | [diff] [blame] | 1451 | struct list_head *head, |
| 1452 | struct pnfs_layout_segment *lseg, |
| 1453 | struct nfs_commit_info *cinfo) |
Fred Isaman | 9ace33c | 2011-03-23 13:27:45 +0000 | [diff] [blame] | 1454 | { |
| 1455 | struct nfs_page *first = nfs_list_entry(head->next); |
Al Viro | 3d4ff43 | 2011-06-22 18:40:12 -0400 | [diff] [blame] | 1456 | struct inode *inode = first->wb_context->dentry->d_inode; |
Fred Isaman | 9ace33c | 2011-03-23 13:27:45 +0000 | [diff] [blame] | 1457 | |
| 1458 | /* Set up the RPC argument and reply structs |
| 1459 | * NB: take care not to mess about with data->commit et al. */ |
| 1460 | |
| 1461 | list_splice_init(head, &data->pages); |
| 1462 | |
| 1463 | data->inode = inode; |
| 1464 | data->cred = first->wb_context->cred; |
Fred Isaman | 988b6dc | 2011-03-23 13:27:52 +0000 | [diff] [blame] | 1465 | data->lseg = lseg; /* reference transferred */ |
Fred Isaman | 9ace33c | 2011-03-23 13:27:45 +0000 | [diff] [blame] | 1466 | data->mds_ops = &nfs_commit_ops; |
Fred Isaman | f453a54 | 2012-04-20 14:47:54 -0400 | [diff] [blame] | 1467 | data->completion_ops = cinfo->completion_ops; |
Fred Isaman | b359f9d | 2012-04-20 14:47:55 -0400 | [diff] [blame] | 1468 | data->dreq = cinfo->dreq; |
Fred Isaman | 9ace33c | 2011-03-23 13:27:45 +0000 | [diff] [blame] | 1469 | |
| 1470 | data->args.fh = NFS_FH(data->inode); |
| 1471 | /* Note: we always request a commit of the entire inode */ |
| 1472 | data->args.offset = 0; |
| 1473 | data->args.count = 0; |
Fred Isaman | 0b7c015 | 2012-04-20 14:47:39 -0400 | [diff] [blame] | 1474 | data->context = get_nfs_open_context(first->wb_context); |
Fred Isaman | 9ace33c | 2011-03-23 13:27:45 +0000 | [diff] [blame] | 1475 | data->res.fattr = &data->fattr; |
| 1476 | data->res.verf = &data->verf; |
| 1477 | nfs_fattr_init(&data->fattr); |
| 1478 | } |
Fred Isaman | e0c2b38 | 2011-03-23 13:27:53 +0000 | [diff] [blame] | 1479 | EXPORT_SYMBOL_GPL(nfs_init_commit); |
Fred Isaman | 9ace33c | 2011-03-23 13:27:45 +0000 | [diff] [blame] | 1480 | |
Fred Isaman | e0c2b38 | 2011-03-23 13:27:53 +0000 | [diff] [blame] | 1481 | void nfs_retry_commit(struct list_head *page_list, |
Fred Isaman | ea2cf22 | 2012-04-20 14:47:53 -0400 | [diff] [blame] | 1482 | struct pnfs_layout_segment *lseg, |
| 1483 | struct nfs_commit_info *cinfo) |
Fred Isaman | 64bfeb4 | 2011-03-23 13:27:47 +0000 | [diff] [blame] | 1484 | { |
| 1485 | struct nfs_page *req; |
| 1486 | |
| 1487 | while (!list_empty(page_list)) { |
| 1488 | req = nfs_list_entry(page_list->next); |
| 1489 | nfs_list_remove_request(req); |
Fred Isaman | ea2cf22 | 2012-04-20 14:47:53 -0400 | [diff] [blame] | 1490 | nfs_mark_request_commit(req, lseg, cinfo); |
Fred Isaman | 56f9cd6 | 2012-04-20 14:47:56 -0400 | [diff] [blame] | 1491 | if (!cinfo->dreq) { |
| 1492 | dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS); |
Mel Gorman | d56b4dd | 2012-07-31 16:45:06 -0700 | [diff] [blame] | 1493 | dec_bdi_stat(page_file_mapping(req->wb_page)->backing_dev_info, |
Fred Isaman | 56f9cd6 | 2012-04-20 14:47:56 -0400 | [diff] [blame] | 1494 | BDI_RECLAIMABLE); |
| 1495 | } |
Trond Myklebust | 1d1afcb | 2012-05-09 14:04:55 -0400 | [diff] [blame] | 1496 | nfs_unlock_and_release_request(req); |
Fred Isaman | 64bfeb4 | 2011-03-23 13:27:47 +0000 | [diff] [blame] | 1497 | } |
| 1498 | } |
Fred Isaman | e0c2b38 | 2011-03-23 13:27:53 +0000 | [diff] [blame] | 1499 | EXPORT_SYMBOL_GPL(nfs_retry_commit); |
Fred Isaman | 64bfeb4 | 2011-03-23 13:27:47 +0000 | [diff] [blame] | 1500 | |
Fred Isaman | 9ace33c | 2011-03-23 13:27:45 +0000 | [diff] [blame] | 1501 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1502 | * Commit dirty pages |
| 1503 | */ |
| 1504 | static int |
Fred Isaman | ea2cf22 | 2012-04-20 14:47:53 -0400 | [diff] [blame] | 1505 | nfs_commit_list(struct inode *inode, struct list_head *head, int how, |
| 1506 | struct nfs_commit_info *cinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1507 | { |
Fred Isaman | 0b7c015 | 2012-04-20 14:47:39 -0400 | [diff] [blame] | 1508 | struct nfs_commit_data *data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1509 | |
Trond Myklebust | c9d8f89 | 2008-04-15 16:56:39 -0400 | [diff] [blame] | 1510 | data = nfs_commitdata_alloc(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1511 | |
| 1512 | if (!data) |
| 1513 | goto out_bad; |
| 1514 | |
| 1515 | /* Set up the argument struct */ |
Fred Isaman | f453a54 | 2012-04-20 14:47:54 -0400 | [diff] [blame] | 1516 | nfs_init_commit(data, head, NULL, cinfo); |
| 1517 | atomic_inc(&cinfo->mds->rpcs_out); |
Andy Adamson | 9f0ec176 | 2012-04-27 17:53:44 -0400 | [diff] [blame] | 1518 | return nfs_initiate_commit(NFS_CLIENT(inode), data, data->mds_ops, |
| 1519 | how, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1520 | out_bad: |
Fred Isaman | ea2cf22 | 2012-04-20 14:47:53 -0400 | [diff] [blame] | 1521 | nfs_retry_commit(head, NULL, cinfo); |
Fred Isaman | f453a54 | 2012-04-20 14:47:54 -0400 | [diff] [blame] | 1522 | cinfo->completion_ops->error_cleanup(NFS_I(inode)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1523 | return -ENOMEM; |
| 1524 | } |
| 1525 | |
| 1526 | /* |
| 1527 | * COMMIT call returned |
| 1528 | */ |
Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1529 | static void nfs_commit_done(struct rpc_task *task, void *calldata) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1530 | { |
Fred Isaman | 0b7c015 | 2012-04-20 14:47:39 -0400 | [diff] [blame] | 1531 | struct nfs_commit_data *data = calldata; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1532 | |
Chuck Lever | a3f565b | 2007-01-31 12:14:01 -0500 | [diff] [blame] | 1533 | dprintk("NFS: %5u nfs_commit_done (status %d)\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1534 | task->tk_pid, task->tk_status); |
| 1535 | |
Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1536 | /* Call the NFS version-specific code */ |
Trond Myklebust | c0d0e96 | 2011-04-12 12:29:15 -0400 | [diff] [blame] | 1537 | NFS_PROTO(data->inode)->commit_done(task, data); |
Trond Myklebust | c9d8f89 | 2008-04-15 16:56:39 -0400 | [diff] [blame] | 1538 | } |
| 1539 | |
Fred Isaman | f453a54 | 2012-04-20 14:47:54 -0400 | [diff] [blame] | 1540 | static void nfs_commit_release_pages(struct nfs_commit_data *data) |
Trond Myklebust | c9d8f89 | 2008-04-15 16:56:39 -0400 | [diff] [blame] | 1541 | { |
Fred Isaman | 5917ce8 | 2011-03-23 13:27:48 +0000 | [diff] [blame] | 1542 | struct nfs_page *req; |
Trond Myklebust | c9d8f89 | 2008-04-15 16:56:39 -0400 | [diff] [blame] | 1543 | int status = data->task.tk_status; |
Fred Isaman | f453a54 | 2012-04-20 14:47:54 -0400 | [diff] [blame] | 1544 | struct nfs_commit_info cinfo; |
Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1545 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1546 | while (!list_empty(&data->pages)) { |
| 1547 | req = nfs_list_entry(data->pages.next); |
| 1548 | nfs_list_remove_request(req); |
Fred Isaman | d6d6dc7 | 2012-03-08 17:29:35 -0500 | [diff] [blame] | 1549 | nfs_clear_page_commit(req->wb_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1550 | |
Chuck Lever | 48186c7 | 2008-06-11 17:56:05 -0400 | [diff] [blame] | 1551 | dprintk("NFS: commit (%s/%lld %d@%lld)", |
Al Viro | 3d4ff43 | 2011-06-22 18:40:12 -0400 | [diff] [blame] | 1552 | req->wb_context->dentry->d_sb->s_id, |
| 1553 | (long long)NFS_FILEID(req->wb_context->dentry->d_inode), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1554 | req->wb_bytes, |
| 1555 | (long long)req_offset(req)); |
Trond Myklebust | c9d8f89 | 2008-04-15 16:56:39 -0400 | [diff] [blame] | 1556 | if (status < 0) { |
| 1557 | nfs_context_set_write_error(req->wb_context, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1558 | nfs_inode_remove_request(req); |
Trond Myklebust | c9d8f89 | 2008-04-15 16:56:39 -0400 | [diff] [blame] | 1559 | dprintk(", error = %d\n", status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1560 | goto next; |
| 1561 | } |
| 1562 | |
| 1563 | /* Okay, COMMIT succeeded, apparently. Check the verifier |
| 1564 | * returned by the server against all stored verfs. */ |
Trond Myklebust | 2f2c63b | 2012-06-08 11:56:09 -0400 | [diff] [blame] | 1565 | if (!memcmp(&req->wb_verf, &data->verf.verifier, sizeof(req->wb_verf))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1566 | /* We have a match */ |
| 1567 | nfs_inode_remove_request(req); |
| 1568 | dprintk(" OK\n"); |
| 1569 | goto next; |
| 1570 | } |
| 1571 | /* We have a mismatch. Write the page again */ |
| 1572 | dprintk(" mismatch\n"); |
Fred | 6d884e8 | 2008-03-19 11:24:38 -0400 | [diff] [blame] | 1573 | nfs_mark_request_dirty(req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1574 | next: |
Trond Myklebust | 1d1afcb | 2012-05-09 14:04:55 -0400 | [diff] [blame] | 1575 | nfs_unlock_and_release_request(req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1576 | } |
Fred Isaman | f453a54 | 2012-04-20 14:47:54 -0400 | [diff] [blame] | 1577 | nfs_init_cinfo(&cinfo, data->inode, data->dreq); |
| 1578 | if (atomic_dec_and_test(&cinfo.mds->rpcs_out)) |
| 1579 | nfs_commit_clear_lock(NFS_I(data->inode)); |
Fred Isaman | 5917ce8 | 2011-03-23 13:27:48 +0000 | [diff] [blame] | 1580 | } |
| 1581 | |
| 1582 | static void nfs_commit_release(void *calldata) |
| 1583 | { |
Fred Isaman | 0b7c015 | 2012-04-20 14:47:39 -0400 | [diff] [blame] | 1584 | struct nfs_commit_data *data = calldata; |
Fred Isaman | 5917ce8 | 2011-03-23 13:27:48 +0000 | [diff] [blame] | 1585 | |
Fred Isaman | f453a54 | 2012-04-20 14:47:54 -0400 | [diff] [blame] | 1586 | data->completion_ops->completion(data); |
Trond Myklebust | c9d8f89 | 2008-04-15 16:56:39 -0400 | [diff] [blame] | 1587 | nfs_commitdata_release(calldata); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1588 | } |
Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1589 | |
| 1590 | static const struct rpc_call_ops nfs_commit_ops = { |
Fred Isaman | 0b7c015 | 2012-04-20 14:47:39 -0400 | [diff] [blame] | 1591 | .rpc_call_prepare = nfs_commit_prepare, |
Trond Myklebust | 788e7a8 | 2006-03-20 13:44:27 -0500 | [diff] [blame] | 1592 | .rpc_call_done = nfs_commit_done, |
| 1593 | .rpc_release = nfs_commit_release, |
| 1594 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1595 | |
Fred Isaman | f453a54 | 2012-04-20 14:47:54 -0400 | [diff] [blame] | 1596 | static const struct nfs_commit_completion_ops nfs_commit_completion_ops = { |
| 1597 | .completion = nfs_commit_release_pages, |
| 1598 | .error_cleanup = nfs_commit_clear_lock, |
| 1599 | }; |
| 1600 | |
Fred Isaman | 1763da1 | 2012-04-20 14:47:57 -0400 | [diff] [blame] | 1601 | int nfs_generic_commit_list(struct inode *inode, struct list_head *head, |
| 1602 | int how, struct nfs_commit_info *cinfo) |
Fred Isaman | 84c53ab | 2012-04-20 14:47:52 -0400 | [diff] [blame] | 1603 | { |
| 1604 | int status; |
| 1605 | |
Fred Isaman | ea2cf22 | 2012-04-20 14:47:53 -0400 | [diff] [blame] | 1606 | status = pnfs_commit_list(inode, head, how, cinfo); |
Fred Isaman | 84c53ab | 2012-04-20 14:47:52 -0400 | [diff] [blame] | 1607 | if (status == PNFS_NOT_ATTEMPTED) |
Fred Isaman | ea2cf22 | 2012-04-20 14:47:53 -0400 | [diff] [blame] | 1608 | status = nfs_commit_list(inode, head, how, cinfo); |
Fred Isaman | 84c53ab | 2012-04-20 14:47:52 -0400 | [diff] [blame] | 1609 | return status; |
| 1610 | } |
| 1611 | |
Trond Myklebust | b608b28 | 2010-07-30 15:31:54 -0400 | [diff] [blame] | 1612 | int nfs_commit_inode(struct inode *inode, int how) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1613 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1614 | LIST_HEAD(head); |
Fred Isaman | ea2cf22 | 2012-04-20 14:47:53 -0400 | [diff] [blame] | 1615 | struct nfs_commit_info cinfo; |
Trond Myklebust | 71d0a61 | 2010-04-22 15:35:57 -0400 | [diff] [blame] | 1616 | int may_wait = how & FLUSH_SYNC; |
Trond Myklebust | b8413f9 | 2011-03-21 15:37:01 -0400 | [diff] [blame] | 1617 | int res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1618 | |
Trond Myklebust | b8413f9 | 2011-03-21 15:37:01 -0400 | [diff] [blame] | 1619 | res = nfs_commit_set_lock(NFS_I(inode), may_wait); |
| 1620 | if (res <= 0) |
Trond Myklebust | c5efa5f | 2010-05-26 08:42:11 -0400 | [diff] [blame] | 1621 | goto out_mark_dirty; |
Fred Isaman | ea2cf22 | 2012-04-20 14:47:53 -0400 | [diff] [blame] | 1622 | nfs_init_cinfo_from_inode(&cinfo, inode); |
| 1623 | res = nfs_scan_commit(inode, &head, &cinfo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1624 | if (res) { |
Fred Isaman | a861a1e | 2011-03-23 13:27:51 +0000 | [diff] [blame] | 1625 | int error; |
| 1626 | |
Fred Isaman | ea2cf22 | 2012-04-20 14:47:53 -0400 | [diff] [blame] | 1627 | error = nfs_generic_commit_list(inode, &head, how, &cinfo); |
Trond Myklebust | 3da28eb | 2005-06-22 17:16:31 +0000 | [diff] [blame] | 1628 | if (error < 0) |
| 1629 | return error; |
Trond Myklebust | b8413f9 | 2011-03-21 15:37:01 -0400 | [diff] [blame] | 1630 | if (!may_wait) |
Trond Myklebust | c5efa5f | 2010-05-26 08:42:11 -0400 | [diff] [blame] | 1631 | goto out_mark_dirty; |
Trond Myklebust | b8413f9 | 2011-03-21 15:37:01 -0400 | [diff] [blame] | 1632 | error = wait_on_bit(&NFS_I(inode)->flags, |
| 1633 | NFS_INO_COMMIT, |
| 1634 | nfs_wait_bit_killable, |
| 1635 | TASK_KILLABLE); |
| 1636 | if (error < 0) |
| 1637 | return error; |
Trond Myklebust | 71d0a61 | 2010-04-22 15:35:57 -0400 | [diff] [blame] | 1638 | } else |
| 1639 | nfs_commit_clear_lock(NFS_I(inode)); |
Trond Myklebust | c5efa5f | 2010-05-26 08:42:11 -0400 | [diff] [blame] | 1640 | return res; |
| 1641 | /* Note: If we exit without ensuring that the commit is complete, |
| 1642 | * we must mark the inode as dirty. Otherwise, future calls to |
| 1643 | * sync_inode() with the WB_SYNC_ALL flag set will fail to ensure |
| 1644 | * that the data is on the disk. |
| 1645 | */ |
| 1646 | out_mark_dirty: |
| 1647 | __mark_inode_dirty(inode, I_DIRTY_DATASYNC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1648 | return res; |
| 1649 | } |
Trond Myklebust | 8fc795f | 2010-02-19 16:46:56 -0800 | [diff] [blame] | 1650 | |
| 1651 | static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc) |
| 1652 | { |
Trond Myklebust | 420e364 | 2010-02-19 17:00:02 -0800 | [diff] [blame] | 1653 | struct nfs_inode *nfsi = NFS_I(inode); |
| 1654 | int flags = FLUSH_SYNC; |
| 1655 | int ret = 0; |
Trond Myklebust | 8fc795f | 2010-02-19 16:46:56 -0800 | [diff] [blame] | 1656 | |
Jeff Layton | 3236c3e | 2011-10-11 09:49:21 -0400 | [diff] [blame] | 1657 | /* no commits means nothing needs to be done */ |
Fred Isaman | ea2cf22 | 2012-04-20 14:47:53 -0400 | [diff] [blame] | 1658 | if (!nfsi->commit_info.ncommit) |
Jeff Layton | 3236c3e | 2011-10-11 09:49:21 -0400 | [diff] [blame] | 1659 | return ret; |
| 1660 | |
Jeff Layton | a00dd6c | 2010-09-28 09:14:01 -0400 | [diff] [blame] | 1661 | if (wbc->sync_mode == WB_SYNC_NONE) { |
| 1662 | /* Don't commit yet if this is a non-blocking flush and there |
| 1663 | * are a lot of outstanding writes for this mapping. |
| 1664 | */ |
Fred Isaman | ea2cf22 | 2012-04-20 14:47:53 -0400 | [diff] [blame] | 1665 | if (nfsi->commit_info.ncommit <= (nfsi->npages >> 1)) |
Jeff Layton | a00dd6c | 2010-09-28 09:14:01 -0400 | [diff] [blame] | 1666 | goto out_mark_dirty; |
Trond Myklebust | 420e364 | 2010-02-19 17:00:02 -0800 | [diff] [blame] | 1667 | |
Jeff Layton | a00dd6c | 2010-09-28 09:14:01 -0400 | [diff] [blame] | 1668 | /* don't wait for the COMMIT response */ |
Trond Myklebust | 420e364 | 2010-02-19 17:00:02 -0800 | [diff] [blame] | 1669 | flags = 0; |
Jeff Layton | a00dd6c | 2010-09-28 09:14:01 -0400 | [diff] [blame] | 1670 | } |
| 1671 | |
Trond Myklebust | 420e364 | 2010-02-19 17:00:02 -0800 | [diff] [blame] | 1672 | ret = nfs_commit_inode(inode, flags); |
| 1673 | if (ret >= 0) { |
| 1674 | if (wbc->sync_mode == WB_SYNC_NONE) { |
| 1675 | if (ret < wbc->nr_to_write) |
| 1676 | wbc->nr_to_write -= ret; |
| 1677 | else |
| 1678 | wbc->nr_to_write = 0; |
| 1679 | } |
Trond Myklebust | 8fc795f | 2010-02-19 16:46:56 -0800 | [diff] [blame] | 1680 | return 0; |
Trond Myklebust | 420e364 | 2010-02-19 17:00:02 -0800 | [diff] [blame] | 1681 | } |
| 1682 | out_mark_dirty: |
Trond Myklebust | 8fc795f | 2010-02-19 16:46:56 -0800 | [diff] [blame] | 1683 | __mark_inode_dirty(inode, I_DIRTY_DATASYNC); |
| 1684 | return ret; |
| 1685 | } |
Trond Myklebust | c63c7b0 | 2007-04-02 19:29:52 -0400 | [diff] [blame] | 1686 | #else |
Trond Myklebust | 8fc795f | 2010-02-19 16:46:56 -0800 | [diff] [blame] | 1687 | static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc) |
| 1688 | { |
| 1689 | return 0; |
| 1690 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1691 | #endif |
| 1692 | |
Trond Myklebust | 8fc795f | 2010-02-19 16:46:56 -0800 | [diff] [blame] | 1693 | int nfs_write_inode(struct inode *inode, struct writeback_control *wbc) |
| 1694 | { |
Bryan Schumaker | a8d8f02 | 2012-06-20 15:53:49 -0400 | [diff] [blame] | 1695 | return nfs_commit_unstable_pages(inode, wbc); |
| 1696 | } |
Andy Adamson | 863a3c6 | 2011-03-23 13:27:54 +0000 | [diff] [blame] | 1697 | |
Bryan Schumaker | a8d8f02 | 2012-06-20 15:53:49 -0400 | [diff] [blame] | 1698 | #ifdef CONFIG_NFS_V4 |
| 1699 | int nfs4_write_inode(struct inode *inode, struct writeback_control *wbc) |
| 1700 | { |
| 1701 | int ret = nfs_write_inode(inode, wbc); |
| 1702 | |
Andy Adamson | 863a3c6 | 2011-03-23 13:27:54 +0000 | [diff] [blame] | 1703 | if (ret >= 0 && test_bit(NFS_INO_LAYOUTCOMMIT, &NFS_I(inode)->flags)) { |
Andy Adamson | ef31153 | 2011-03-12 02:58:10 -0500 | [diff] [blame] | 1704 | int status; |
| 1705 | bool sync = true; |
Andy Adamson | 863a3c6 | 2011-03-23 13:27:54 +0000 | [diff] [blame] | 1706 | |
Wu Fengguang | 846d5a0 | 2011-05-05 21:10:38 -0600 | [diff] [blame] | 1707 | if (wbc->sync_mode == WB_SYNC_NONE) |
Andy Adamson | ef31153 | 2011-03-12 02:58:10 -0500 | [diff] [blame] | 1708 | sync = false; |
Andy Adamson | 863a3c6 | 2011-03-23 13:27:54 +0000 | [diff] [blame] | 1709 | |
| 1710 | status = pnfs_layoutcommit_inode(inode, sync); |
| 1711 | if (status < 0) |
| 1712 | return status; |
| 1713 | } |
| 1714 | return ret; |
Trond Myklebust | 8fc795f | 2010-02-19 16:46:56 -0800 | [diff] [blame] | 1715 | } |
Bryan Schumaker | a8d8f02 | 2012-06-20 15:53:49 -0400 | [diff] [blame] | 1716 | #endif |
Trond Myklebust | 8fc795f | 2010-02-19 16:46:56 -0800 | [diff] [blame] | 1717 | |
Trond Myklebust | acdc53b | 2010-02-19 17:03:26 -0800 | [diff] [blame] | 1718 | /* |
| 1719 | * flush the inode to disk. |
| 1720 | */ |
| 1721 | int nfs_wb_all(struct inode *inode) |
Trond Myklebust | 34901f7 | 2007-07-25 14:09:54 -0400 | [diff] [blame] | 1722 | { |
| 1723 | struct writeback_control wbc = { |
Trond Myklebust | 72cb77f | 2009-03-11 14:10:30 -0400 | [diff] [blame] | 1724 | .sync_mode = WB_SYNC_ALL, |
Trond Myklebust | 34901f7 | 2007-07-25 14:09:54 -0400 | [diff] [blame] | 1725 | .nr_to_write = LONG_MAX, |
Trond Myklebust | d7fb120 | 2008-10-06 20:08:56 -0400 | [diff] [blame] | 1726 | .range_start = 0, |
| 1727 | .range_end = LLONG_MAX, |
Trond Myklebust | 34901f7 | 2007-07-25 14:09:54 -0400 | [diff] [blame] | 1728 | }; |
Trond Myklebust | 34901f7 | 2007-07-25 14:09:54 -0400 | [diff] [blame] | 1729 | |
Trond Myklebust | acdc53b | 2010-02-19 17:03:26 -0800 | [diff] [blame] | 1730 | return sync_inode(inode, &wbc); |
Trond Myklebust | 1c75950 | 2006-10-09 16:18:38 -0400 | [diff] [blame] | 1731 | } |
| 1732 | |
Trond Myklebust | 1b3b4a1 | 2007-08-28 10:29:36 -0400 | [diff] [blame] | 1733 | int nfs_wb_page_cancel(struct inode *inode, struct page *page) |
| 1734 | { |
| 1735 | struct nfs_page *req; |
Trond Myklebust | 1b3b4a1 | 2007-08-28 10:29:36 -0400 | [diff] [blame] | 1736 | int ret = 0; |
| 1737 | |
| 1738 | BUG_ON(!PageLocked(page)); |
| 1739 | for (;;) { |
Trond Myklebust | ba8b06e | 2010-04-27 18:33:54 -0400 | [diff] [blame] | 1740 | wait_on_page_writeback(page); |
Trond Myklebust | 1b3b4a1 | 2007-08-28 10:29:36 -0400 | [diff] [blame] | 1741 | req = nfs_page_find_request(page); |
| 1742 | if (req == NULL) |
Trond Myklebust | 1b3b4a1 | 2007-08-28 10:29:36 -0400 | [diff] [blame] | 1743 | break; |
Trond Myklebust | 7ad84aa | 2012-05-09 13:19:15 -0400 | [diff] [blame] | 1744 | if (nfs_lock_request(req)) { |
Trond Myklebust | 8dd3775 | 2012-03-15 17:16:40 -0400 | [diff] [blame] | 1745 | nfs_clear_request_commit(req); |
Trond Myklebust | 1b3b4a1 | 2007-08-28 10:29:36 -0400 | [diff] [blame] | 1746 | nfs_inode_remove_request(req); |
| 1747 | /* |
| 1748 | * In case nfs_inode_remove_request has marked the |
| 1749 | * page as being dirty |
| 1750 | */ |
| 1751 | cancel_dirty_page(page, PAGE_CACHE_SIZE); |
Trond Myklebust | 1d1afcb | 2012-05-09 14:04:55 -0400 | [diff] [blame] | 1752 | nfs_unlock_and_release_request(req); |
Trond Myklebust | 1b3b4a1 | 2007-08-28 10:29:36 -0400 | [diff] [blame] | 1753 | break; |
| 1754 | } |
| 1755 | ret = nfs_wait_on_request(req); |
Trond Myklebust | c9edda7 | 2010-01-26 15:41:34 -0500 | [diff] [blame] | 1756 | nfs_release_request(req); |
Trond Myklebust | 1b3b4a1 | 2007-08-28 10:29:36 -0400 | [diff] [blame] | 1757 | if (ret < 0) |
Trond Myklebust | c988950 | 2010-02-19 17:03:21 -0800 | [diff] [blame] | 1758 | break; |
Trond Myklebust | 1b3b4a1 | 2007-08-28 10:29:36 -0400 | [diff] [blame] | 1759 | } |
Trond Myklebust | 1b3b4a1 | 2007-08-28 10:29:36 -0400 | [diff] [blame] | 1760 | return ret; |
| 1761 | } |
| 1762 | |
Trond Myklebust | 7f2f12d | 2010-02-19 17:03:28 -0800 | [diff] [blame] | 1763 | /* |
| 1764 | * Write back all requests on one page - we do this before reading it. |
| 1765 | */ |
| 1766 | int nfs_wb_page(struct inode *inode, struct page *page) |
Trond Myklebust | 1c75950 | 2006-10-09 16:18:38 -0400 | [diff] [blame] | 1767 | { |
Mel Gorman | 29418aa | 2012-07-31 16:45:10 -0700 | [diff] [blame] | 1768 | loff_t range_start = page_file_offset(page); |
Trond Myklebust | 1c75950 | 2006-10-09 16:18:38 -0400 | [diff] [blame] | 1769 | loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1); |
Trond Myklebust | 4d770cc | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 1770 | struct writeback_control wbc = { |
Trond Myklebust | 4d770cc | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 1771 | .sync_mode = WB_SYNC_ALL, |
Trond Myklebust | 7f2f12d | 2010-02-19 17:03:28 -0800 | [diff] [blame] | 1772 | .nr_to_write = 0, |
Trond Myklebust | 4d770cc | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 1773 | .range_start = range_start, |
| 1774 | .range_end = range_end, |
| 1775 | }; |
| 1776 | int ret; |
Trond Myklebust | 1c75950 | 2006-10-09 16:18:38 -0400 | [diff] [blame] | 1777 | |
Trond Myklebust | 0522f6a | 2010-05-26 08:42:24 -0400 | [diff] [blame] | 1778 | for (;;) { |
Trond Myklebust | ba8b06e | 2010-04-27 18:33:54 -0400 | [diff] [blame] | 1779 | wait_on_page_writeback(page); |
Trond Myklebust | 73e3302 | 2008-04-11 16:03:54 -0400 | [diff] [blame] | 1780 | if (clear_page_dirty_for_io(page)) { |
| 1781 | ret = nfs_writepage_locked(page, &wbc); |
| 1782 | if (ret < 0) |
| 1783 | goto out_error; |
Trond Myklebust | 0522f6a | 2010-05-26 08:42:24 -0400 | [diff] [blame] | 1784 | continue; |
Trond Myklebust | 7f2f12d | 2010-02-19 17:03:28 -0800 | [diff] [blame] | 1785 | } |
Trond Myklebust | 0522f6a | 2010-05-26 08:42:24 -0400 | [diff] [blame] | 1786 | if (!PagePrivate(page)) |
| 1787 | break; |
| 1788 | ret = nfs_commit_inode(inode, FLUSH_SYNC); |
Trond Myklebust | ba8b06e | 2010-04-27 18:33:54 -0400 | [diff] [blame] | 1789 | if (ret < 0) |
Trond Myklebust | 73e3302 | 2008-04-11 16:03:54 -0400 | [diff] [blame] | 1790 | goto out_error; |
Trond Myklebust | 7f2f12d | 2010-02-19 17:03:28 -0800 | [diff] [blame] | 1791 | } |
Trond Myklebust | 73e3302 | 2008-04-11 16:03:54 -0400 | [diff] [blame] | 1792 | return 0; |
| 1793 | out_error: |
Trond Myklebust | 4d770cc | 2006-12-05 00:35:41 -0500 | [diff] [blame] | 1794 | return ret; |
Trond Myklebust | 1c75950 | 2006-10-09 16:18:38 -0400 | [diff] [blame] | 1795 | } |
| 1796 | |
Trond Myklebust | 074cc1d | 2009-08-10 08:54:13 -0400 | [diff] [blame] | 1797 | #ifdef CONFIG_MIGRATION |
| 1798 | int nfs_migrate_page(struct address_space *mapping, struct page *newpage, |
Mel Gorman | a6bc32b | 2012-01-12 17:19:43 -0800 | [diff] [blame] | 1799 | struct page *page, enum migrate_mode mode) |
Trond Myklebust | 074cc1d | 2009-08-10 08:54:13 -0400 | [diff] [blame] | 1800 | { |
Jeff Layton | 2da9565 | 2011-10-12 10:57:42 -0400 | [diff] [blame] | 1801 | /* |
| 1802 | * If PagePrivate is set, then the page is currently associated with |
| 1803 | * an in-progress read or write request. Don't try to migrate it. |
| 1804 | * |
| 1805 | * FIXME: we could do this in principle, but we'll need a way to ensure |
| 1806 | * that we can safely release the inode reference while holding |
| 1807 | * the page lock. |
| 1808 | */ |
| 1809 | if (PagePrivate(page)) |
| 1810 | return -EBUSY; |
Trond Myklebust | 074cc1d | 2009-08-10 08:54:13 -0400 | [diff] [blame] | 1811 | |
Trond Myklebust | 7549ad5 | 2010-02-08 09:32:34 -0500 | [diff] [blame] | 1812 | nfs_fscache_release_page(page, GFP_KERNEL); |
Trond Myklebust | 074cc1d | 2009-08-10 08:54:13 -0400 | [diff] [blame] | 1813 | |
Mel Gorman | a6bc32b | 2012-01-12 17:19:43 -0800 | [diff] [blame] | 1814 | return migrate_page(mapping, newpage, page, mode); |
Trond Myklebust | 074cc1d | 2009-08-10 08:54:13 -0400 | [diff] [blame] | 1815 | } |
| 1816 | #endif |
| 1817 | |
David Howells | f7b422b | 2006-06-09 09:34:33 -0400 | [diff] [blame] | 1818 | int __init nfs_init_writepagecache(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1819 | { |
| 1820 | nfs_wdata_cachep = kmem_cache_create("nfs_write_data", |
Fred Isaman | cd84160 | 2012-04-20 14:47:44 -0400 | [diff] [blame] | 1821 | sizeof(struct nfs_write_header), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1822 | 0, SLAB_HWCACHE_ALIGN, |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 1823 | NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1824 | if (nfs_wdata_cachep == NULL) |
| 1825 | return -ENOMEM; |
| 1826 | |
Matthew Dobson | 93d2341 | 2006-03-26 01:37:50 -0800 | [diff] [blame] | 1827 | nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE, |
| 1828 | nfs_wdata_cachep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1829 | if (nfs_wdata_mempool == NULL) |
| 1830 | return -ENOMEM; |
| 1831 | |
Fred Isaman | 0b7c015 | 2012-04-20 14:47:39 -0400 | [diff] [blame] | 1832 | nfs_cdata_cachep = kmem_cache_create("nfs_commit_data", |
| 1833 | sizeof(struct nfs_commit_data), |
| 1834 | 0, SLAB_HWCACHE_ALIGN, |
| 1835 | NULL); |
| 1836 | if (nfs_cdata_cachep == NULL) |
| 1837 | return -ENOMEM; |
| 1838 | |
Matthew Dobson | 93d2341 | 2006-03-26 01:37:50 -0800 | [diff] [blame] | 1839 | nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT, |
| 1840 | nfs_wdata_cachep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1841 | if (nfs_commit_mempool == NULL) |
| 1842 | return -ENOMEM; |
| 1843 | |
Peter Zijlstra | 89a0914 | 2007-03-16 13:38:26 -0800 | [diff] [blame] | 1844 | /* |
| 1845 | * NFS congestion size, scale with available memory. |
| 1846 | * |
| 1847 | * 64MB: 8192k |
| 1848 | * 128MB: 11585k |
| 1849 | * 256MB: 16384k |
| 1850 | * 512MB: 23170k |
| 1851 | * 1GB: 32768k |
| 1852 | * 2GB: 46340k |
| 1853 | * 4GB: 65536k |
| 1854 | * 8GB: 92681k |
| 1855 | * 16GB: 131072k |
| 1856 | * |
| 1857 | * This allows larger machines to have larger/more transfers. |
| 1858 | * Limit the default to 256M |
| 1859 | */ |
| 1860 | nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10); |
| 1861 | if (nfs_congestion_kb > 256*1024) |
| 1862 | nfs_congestion_kb = 256*1024; |
| 1863 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1864 | return 0; |
| 1865 | } |
| 1866 | |
David Brownell | 266bee8 | 2006-06-27 12:59:15 -0700 | [diff] [blame] | 1867 | void nfs_destroy_writepagecache(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1868 | { |
| 1869 | mempool_destroy(nfs_commit_mempool); |
| 1870 | mempool_destroy(nfs_wdata_mempool); |
Alexey Dobriyan | 1a1d92c | 2006-09-27 01:49:40 -0700 | [diff] [blame] | 1871 | kmem_cache_destroy(nfs_wdata_cachep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1872 | } |
| 1873 | |