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