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