Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/fs/nfs/write.c | |
3 | * | |
4 | * Writing file data over NFS. | |
5 | * | |
6 | * We do it like this: When a (user) process wishes to write data to an | |
7 | * NFS file, a write request is allocated that contains the RPC task data | |
8 | * plus some info on the page to be written, and added to the inode's | |
9 | * write chain. If the process writes past the end of the page, an async | |
10 | * RPC call to write the page is scheduled immediately; otherwise, the call | |
11 | * is delayed for a few seconds. | |
12 | * | |
13 | * Just like readahead, no async I/O is performed if wsize < PAGE_SIZE. | |
14 | * | |
15 | * Write requests are kept on the inode's writeback list. Each entry in | |
16 | * that list references the page (portion) to be written. When the | |
17 | * cache timeout has expired, the RPC task is woken up, and tries to | |
18 | * lock the page. As soon as it manages to do so, the request is moved | |
19 | * from the writeback list to the writelock list. | |
20 | * | |
21 | * Note: we must make sure never to confuse the inode passed in the | |
22 | * write_page request with the one in page->inode. As far as I understand | |
23 | * it, these are different when doing a swap-out. | |
24 | * | |
25 | * To understand everything that goes on here and in the NFS read code, | |
26 | * one should be aware that a page is locked in exactly one of the following | |
27 | * cases: | |
28 | * | |
29 | * - A write request is in progress. | |
30 | * - A user process is in generic_file_write/nfs_update_page | |
31 | * - A user process is in generic_file_read | |
32 | * | |
33 | * Also note that because of the way pages are invalidated in | |
34 | * nfs_revalidate_inode, the following assertions hold: | |
35 | * | |
36 | * - If a page is dirty, there will be no read requests (a page will | |
37 | * not be re-read unless invalidated by nfs_revalidate_inode). | |
38 | * - If the page is not uptodate, there will be no pending write | |
39 | * requests, and no process will be in nfs_update_page. | |
40 | * | |
41 | * FIXME: Interaction with the vmscan routines is not optimal yet. | |
42 | * Either vmscan must be made nfs-savvy, or we need a different page | |
43 | * reclaim concept that supports something like FS-independent | |
44 | * buffer_heads with a b_ops-> field. | |
45 | * | |
46 | * Copyright (C) 1996, 1997, Olaf Kirch <okir@monad.swb.de> | |
47 | */ | |
48 | ||
49 | #include <linux/config.h> | |
50 | #include <linux/types.h> | |
51 | #include <linux/slab.h> | |
52 | #include <linux/mm.h> | |
53 | #include <linux/pagemap.h> | |
54 | #include <linux/file.h> | |
55 | #include <linux/mpage.h> | |
56 | #include <linux/writeback.h> | |
57 | ||
58 | #include <linux/sunrpc/clnt.h> | |
59 | #include <linux/nfs_fs.h> | |
60 | #include <linux/nfs_mount.h> | |
61 | #include <linux/nfs_page.h> | |
62 | #include <asm/uaccess.h> | |
63 | #include <linux/smp_lock.h> | |
64 | ||
65 | #include "delegation.h" | |
66 | ||
67 | #define NFSDBG_FACILITY NFSDBG_PAGECACHE | |
68 | ||
69 | #define MIN_POOL_WRITE (32) | |
70 | #define MIN_POOL_COMMIT (4) | |
71 | ||
72 | /* | |
73 | * Local function declarations | |
74 | */ | |
75 | static struct nfs_page * nfs_update_request(struct nfs_open_context*, | |
76 | struct inode *, | |
77 | struct page *, | |
78 | unsigned int, unsigned int); | |
79 | static void nfs_writeback_done_partial(struct nfs_write_data *, int); | |
80 | static void nfs_writeback_done_full(struct nfs_write_data *, int); | |
81 | static int nfs_wait_on_write_congestion(struct address_space *, int); | |
82 | static int nfs_wait_on_requests(struct inode *, unsigned long, unsigned int); | |
83 | static int nfs_flush_inode(struct inode *inode, unsigned long idx_start, | |
84 | unsigned int npages, int how); | |
85 | ||
86 | static kmem_cache_t *nfs_wdata_cachep; | |
87 | mempool_t *nfs_wdata_mempool; | |
88 | static mempool_t *nfs_commit_mempool; | |
89 | ||
90 | static DECLARE_WAIT_QUEUE_HEAD(nfs_write_congestion); | |
91 | ||
92 | static inline struct nfs_write_data *nfs_commit_alloc(void) | |
93 | { | |
94 | struct nfs_write_data *p = mempool_alloc(nfs_commit_mempool, SLAB_NOFS); | |
95 | if (p) { | |
96 | memset(p, 0, sizeof(*p)); | |
97 | INIT_LIST_HEAD(&p->pages); | |
98 | } | |
99 | return p; | |
100 | } | |
101 | ||
102 | static inline void nfs_commit_free(struct nfs_write_data *p) | |
103 | { | |
104 | mempool_free(p, nfs_commit_mempool); | |
105 | } | |
106 | ||
107 | static void nfs_writedata_release(struct rpc_task *task) | |
108 | { | |
109 | struct nfs_write_data *wdata = (struct nfs_write_data *)task->tk_calldata; | |
110 | nfs_writedata_free(wdata); | |
111 | } | |
112 | ||
113 | /* Adjust the file length if we're writing beyond the end */ | |
114 | static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count) | |
115 | { | |
116 | struct inode *inode = page->mapping->host; | |
117 | loff_t end, i_size = i_size_read(inode); | |
118 | unsigned long end_index = (i_size - 1) >> PAGE_CACHE_SHIFT; | |
119 | ||
120 | if (i_size > 0 && page->index < end_index) | |
121 | return; | |
122 | end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count); | |
123 | if (i_size >= end) | |
124 | return; | |
125 | i_size_write(inode, end); | |
126 | } | |
127 | ||
128 | /* We can set the PG_uptodate flag if we see that a write request | |
129 | * covers the full page. | |
130 | */ | |
131 | static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count) | |
132 | { | |
133 | loff_t end_offs; | |
134 | ||
135 | if (PageUptodate(page)) | |
136 | return; | |
137 | if (base != 0) | |
138 | return; | |
139 | if (count == PAGE_CACHE_SIZE) { | |
140 | SetPageUptodate(page); | |
141 | return; | |
142 | } | |
143 | ||
144 | end_offs = i_size_read(page->mapping->host) - 1; | |
145 | if (end_offs < 0) | |
146 | return; | |
147 | /* Is this the last page? */ | |
148 | if (page->index != (unsigned long)(end_offs >> PAGE_CACHE_SHIFT)) | |
149 | return; | |
150 | /* This is the last page: set PG_uptodate if we cover the entire | |
151 | * extent of the data, then zero the rest of the page. | |
152 | */ | |
153 | if (count == (unsigned int)(end_offs & (PAGE_CACHE_SIZE - 1)) + 1) { | |
154 | memclear_highpage_flush(page, count, PAGE_CACHE_SIZE - count); | |
155 | SetPageUptodate(page); | |
156 | } | |
157 | } | |
158 | ||
159 | /* | |
160 | * Write a page synchronously. | |
161 | * Offset is the data offset within the page. | |
162 | */ | |
163 | static int nfs_writepage_sync(struct nfs_open_context *ctx, struct inode *inode, | |
164 | struct page *page, unsigned int offset, unsigned int count, | |
165 | int how) | |
166 | { | |
167 | unsigned int wsize = NFS_SERVER(inode)->wsize; | |
168 | int result, written = 0; | |
169 | struct nfs_write_data *wdata; | |
170 | ||
171 | wdata = nfs_writedata_alloc(); | |
172 | if (!wdata) | |
173 | return -ENOMEM; | |
174 | ||
175 | wdata->flags = how; | |
176 | wdata->cred = ctx->cred; | |
177 | wdata->inode = inode; | |
178 | wdata->args.fh = NFS_FH(inode); | |
179 | wdata->args.context = ctx; | |
180 | wdata->args.pages = &page; | |
181 | wdata->args.stable = NFS_FILE_SYNC; | |
182 | wdata->args.pgbase = offset; | |
183 | wdata->args.count = wsize; | |
184 | wdata->res.fattr = &wdata->fattr; | |
185 | wdata->res.verf = &wdata->verf; | |
186 | ||
187 | dprintk("NFS: nfs_writepage_sync(%s/%Ld %d@%Ld)\n", | |
188 | inode->i_sb->s_id, | |
189 | (long long)NFS_FILEID(inode), | |
190 | count, (long long)(page_offset(page) + offset)); | |
191 | ||
bb713d6d | 192 | set_page_writeback(page); |
1da177e4 LT |
193 | nfs_begin_data_update(inode); |
194 | do { | |
195 | if (count < wsize) | |
196 | wdata->args.count = count; | |
197 | wdata->args.offset = page_offset(page) + wdata->args.pgbase; | |
198 | ||
199 | result = NFS_PROTO(inode)->write(wdata); | |
200 | ||
201 | if (result < 0) { | |
202 | /* Must mark the page invalid after I/O error */ | |
203 | ClearPageUptodate(page); | |
204 | goto io_error; | |
205 | } | |
206 | if (result < wdata->args.count) | |
207 | printk(KERN_WARNING "NFS: short write, count=%u, result=%d\n", | |
208 | wdata->args.count, result); | |
209 | ||
210 | wdata->args.offset += result; | |
211 | wdata->args.pgbase += result; | |
212 | written += result; | |
213 | count -= result; | |
214 | } while (count); | |
215 | /* Update file length */ | |
216 | nfs_grow_file(page, offset, written); | |
217 | /* Set the PG_uptodate flag? */ | |
218 | nfs_mark_uptodate(page, offset, written); | |
219 | ||
220 | if (PageError(page)) | |
221 | ClearPageError(page); | |
222 | ||
223 | io_error: | |
951a143b | 224 | nfs_end_data_update(inode); |
bb713d6d | 225 | end_page_writeback(page); |
1da177e4 LT |
226 | nfs_writedata_free(wdata); |
227 | return written ? written : result; | |
228 | } | |
229 | ||
230 | static int nfs_writepage_async(struct nfs_open_context *ctx, | |
231 | struct inode *inode, struct page *page, | |
232 | unsigned int offset, unsigned int count) | |
233 | { | |
234 | struct nfs_page *req; | |
1da177e4 LT |
235 | |
236 | req = nfs_update_request(ctx, inode, page, offset, count); | |
abd3e641 TM |
237 | if (IS_ERR(req)) |
238 | return PTR_ERR(req); | |
1da177e4 LT |
239 | /* Update file length */ |
240 | nfs_grow_file(page, offset, count); | |
241 | /* Set the PG_uptodate flag? */ | |
242 | nfs_mark_uptodate(page, offset, count); | |
243 | nfs_unlock_request(req); | |
abd3e641 | 244 | return 0; |
1da177e4 LT |
245 | } |
246 | ||
247 | static int wb_priority(struct writeback_control *wbc) | |
248 | { | |
249 | if (wbc->for_reclaim) | |
250 | return FLUSH_HIGHPRI; | |
251 | if (wbc->for_kupdate) | |
252 | return FLUSH_LOWPRI; | |
253 | return 0; | |
254 | } | |
255 | ||
256 | /* | |
257 | * Write an mmapped page to the server. | |
258 | */ | |
259 | int nfs_writepage(struct page *page, struct writeback_control *wbc) | |
260 | { | |
261 | struct nfs_open_context *ctx; | |
262 | struct inode *inode = page->mapping->host; | |
263 | unsigned long end_index; | |
264 | unsigned offset = PAGE_CACHE_SIZE; | |
265 | loff_t i_size = i_size_read(inode); | |
266 | int inode_referenced = 0; | |
267 | int priority = wb_priority(wbc); | |
268 | int err; | |
269 | ||
270 | /* | |
271 | * Note: We need to ensure that we have a reference to the inode | |
272 | * if we are to do asynchronous writes. If not, waiting | |
273 | * in nfs_wait_on_request() may deadlock with clear_inode(). | |
274 | * | |
275 | * If igrab() fails here, then it is in any case safe to | |
276 | * call nfs_wb_page(), since there will be no pending writes. | |
277 | */ | |
278 | if (igrab(inode) != 0) | |
279 | inode_referenced = 1; | |
280 | end_index = i_size >> PAGE_CACHE_SHIFT; | |
281 | ||
282 | /* Ensure we've flushed out any previous writes */ | |
283 | nfs_wb_page_priority(inode, page, priority); | |
284 | ||
285 | /* easy case */ | |
286 | if (page->index < end_index) | |
287 | goto do_it; | |
288 | /* things got complicated... */ | |
289 | offset = i_size & (PAGE_CACHE_SIZE-1); | |
290 | ||
291 | /* OK, are we completely out? */ | |
292 | err = 0; /* potential race with truncate - ignore */ | |
293 | if (page->index >= end_index+1 || !offset) | |
294 | goto out; | |
295 | do_it: | |
d530838b | 296 | ctx = nfs_find_open_context(inode, NULL, FMODE_WRITE); |
1da177e4 LT |
297 | if (ctx == NULL) { |
298 | err = -EBADF; | |
299 | goto out; | |
300 | } | |
301 | lock_kernel(); | |
302 | if (!IS_SYNC(inode) && inode_referenced) { | |
303 | err = nfs_writepage_async(ctx, inode, page, 0, offset); | |
abd3e641 TM |
304 | if (!wbc->for_writepages) |
305 | nfs_flush_inode(inode, 0, 0, wb_priority(wbc)); | |
1da177e4 LT |
306 | } else { |
307 | err = nfs_writepage_sync(ctx, inode, page, 0, | |
308 | offset, priority); | |
309 | if (err >= 0) { | |
310 | if (err != offset) | |
311 | redirty_page_for_writepage(wbc, page); | |
312 | err = 0; | |
313 | } | |
314 | } | |
315 | unlock_kernel(); | |
316 | put_nfs_open_context(ctx); | |
317 | out: | |
318 | unlock_page(page); | |
319 | if (inode_referenced) | |
320 | iput(inode); | |
321 | return err; | |
322 | } | |
323 | ||
324 | /* | |
325 | * Note: causes nfs_update_request() to block on the assumption | |
326 | * that the writeback is generated due to memory pressure. | |
327 | */ | |
328 | int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc) | |
329 | { | |
330 | struct backing_dev_info *bdi = mapping->backing_dev_info; | |
331 | struct inode *inode = mapping->host; | |
332 | int err; | |
333 | ||
334 | err = generic_writepages(mapping, wbc); | |
335 | if (err) | |
336 | return err; | |
337 | while (test_and_set_bit(BDI_write_congested, &bdi->state) != 0) { | |
338 | if (wbc->nonblocking) | |
339 | return 0; | |
340 | nfs_wait_on_write_congestion(mapping, 0); | |
341 | } | |
342 | err = nfs_flush_inode(inode, 0, 0, wb_priority(wbc)); | |
343 | if (err < 0) | |
344 | goto out; | |
345 | wbc->nr_to_write -= err; | |
346 | if (!wbc->nonblocking && wbc->sync_mode == WB_SYNC_ALL) { | |
347 | err = nfs_wait_on_requests(inode, 0, 0); | |
348 | if (err < 0) | |
349 | goto out; | |
350 | } | |
3da28eb1 | 351 | err = nfs_commit_inode(inode, wb_priority(wbc)); |
1da177e4 LT |
352 | if (err > 0) { |
353 | wbc->nr_to_write -= err; | |
354 | err = 0; | |
355 | } | |
356 | out: | |
357 | clear_bit(BDI_write_congested, &bdi->state); | |
358 | wake_up_all(&nfs_write_congestion); | |
359 | return err; | |
360 | } | |
361 | ||
362 | /* | |
363 | * Insert a write request into an inode | |
364 | */ | |
365 | static int nfs_inode_add_request(struct inode *inode, struct nfs_page *req) | |
366 | { | |
367 | struct nfs_inode *nfsi = NFS_I(inode); | |
368 | int error; | |
369 | ||
370 | error = radix_tree_insert(&nfsi->nfs_page_tree, req->wb_index, req); | |
371 | BUG_ON(error == -EEXIST); | |
372 | if (error) | |
373 | return error; | |
374 | if (!nfsi->npages) { | |
375 | igrab(inode); | |
376 | nfs_begin_data_update(inode); | |
377 | if (nfs_have_delegation(inode, FMODE_WRITE)) | |
378 | nfsi->change_attr++; | |
379 | } | |
380 | nfsi->npages++; | |
381 | atomic_inc(&req->wb_count); | |
382 | return 0; | |
383 | } | |
384 | ||
385 | /* | |
386 | * Insert a write request into an inode | |
387 | */ | |
388 | static void nfs_inode_remove_request(struct nfs_page *req) | |
389 | { | |
390 | struct inode *inode = req->wb_context->dentry->d_inode; | |
391 | struct nfs_inode *nfsi = NFS_I(inode); | |
392 | ||
393 | BUG_ON (!NFS_WBACK_BUSY(req)); | |
394 | ||
395 | spin_lock(&nfsi->req_lock); | |
396 | radix_tree_delete(&nfsi->nfs_page_tree, req->wb_index); | |
397 | nfsi->npages--; | |
398 | if (!nfsi->npages) { | |
399 | spin_unlock(&nfsi->req_lock); | |
951a143b | 400 | nfs_end_data_update(inode); |
1da177e4 LT |
401 | iput(inode); |
402 | } else | |
403 | spin_unlock(&nfsi->req_lock); | |
404 | nfs_clear_request(req); | |
405 | nfs_release_request(req); | |
406 | } | |
407 | ||
408 | /* | |
409 | * Find a request | |
410 | */ | |
411 | static inline struct nfs_page * | |
412 | _nfs_find_request(struct inode *inode, unsigned long index) | |
413 | { | |
414 | struct nfs_inode *nfsi = NFS_I(inode); | |
415 | struct nfs_page *req; | |
416 | ||
417 | req = (struct nfs_page*)radix_tree_lookup(&nfsi->nfs_page_tree, index); | |
418 | if (req) | |
419 | atomic_inc(&req->wb_count); | |
420 | return req; | |
421 | } | |
422 | ||
423 | static struct nfs_page * | |
424 | nfs_find_request(struct inode *inode, unsigned long index) | |
425 | { | |
426 | struct nfs_page *req; | |
427 | struct nfs_inode *nfsi = NFS_I(inode); | |
428 | ||
429 | spin_lock(&nfsi->req_lock); | |
430 | req = _nfs_find_request(inode, index); | |
431 | spin_unlock(&nfsi->req_lock); | |
432 | return req; | |
433 | } | |
434 | ||
435 | /* | |
436 | * Add a request to the inode's dirty list. | |
437 | */ | |
438 | static void | |
439 | nfs_mark_request_dirty(struct nfs_page *req) | |
440 | { | |
441 | struct inode *inode = req->wb_context->dentry->d_inode; | |
442 | struct nfs_inode *nfsi = NFS_I(inode); | |
443 | ||
444 | spin_lock(&nfsi->req_lock); | |
3da28eb1 TM |
445 | radix_tree_tag_set(&nfsi->nfs_page_tree, |
446 | req->wb_index, NFS_PAGE_TAG_DIRTY); | |
1da177e4 LT |
447 | nfs_list_add_request(req, &nfsi->dirty); |
448 | nfsi->ndirty++; | |
449 | spin_unlock(&nfsi->req_lock); | |
450 | inc_page_state(nr_dirty); | |
451 | mark_inode_dirty(inode); | |
452 | } | |
453 | ||
454 | /* | |
455 | * Check if a request is dirty | |
456 | */ | |
457 | static inline int | |
458 | nfs_dirty_request(struct nfs_page *req) | |
459 | { | |
460 | struct nfs_inode *nfsi = NFS_I(req->wb_context->dentry->d_inode); | |
461 | return !list_empty(&req->wb_list) && req->wb_list_head == &nfsi->dirty; | |
462 | } | |
463 | ||
464 | #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) | |
465 | /* | |
466 | * Add a request to the inode's commit list. | |
467 | */ | |
468 | static void | |
469 | nfs_mark_request_commit(struct nfs_page *req) | |
470 | { | |
471 | struct inode *inode = req->wb_context->dentry->d_inode; | |
472 | struct nfs_inode *nfsi = NFS_I(inode); | |
473 | ||
474 | spin_lock(&nfsi->req_lock); | |
475 | nfs_list_add_request(req, &nfsi->commit); | |
476 | nfsi->ncommit++; | |
477 | spin_unlock(&nfsi->req_lock); | |
478 | inc_page_state(nr_unstable); | |
479 | mark_inode_dirty(inode); | |
480 | } | |
481 | #endif | |
482 | ||
483 | /* | |
484 | * Wait for a request to complete. | |
485 | * | |
486 | * Interruptible by signals only if mounted with intr flag. | |
487 | */ | |
488 | static int | |
489 | nfs_wait_on_requests(struct inode *inode, unsigned long idx_start, unsigned int npages) | |
490 | { | |
491 | struct nfs_inode *nfsi = NFS_I(inode); | |
492 | struct nfs_page *req; | |
493 | unsigned long idx_end, next; | |
494 | unsigned int res = 0; | |
495 | int error; | |
496 | ||
497 | if (npages == 0) | |
498 | idx_end = ~0; | |
499 | else | |
500 | idx_end = idx_start + npages - 1; | |
501 | ||
502 | spin_lock(&nfsi->req_lock); | |
503 | next = idx_start; | |
c6a556b8 | 504 | while (radix_tree_gang_lookup_tag(&nfsi->nfs_page_tree, (void **)&req, next, 1, NFS_PAGE_TAG_WRITEBACK)) { |
1da177e4 LT |
505 | if (req->wb_index > idx_end) |
506 | break; | |
507 | ||
508 | next = req->wb_index + 1; | |
c6a556b8 | 509 | BUG_ON(!NFS_WBACK_BUSY(req)); |
1da177e4 LT |
510 | |
511 | atomic_inc(&req->wb_count); | |
512 | spin_unlock(&nfsi->req_lock); | |
513 | error = nfs_wait_on_request(req); | |
514 | nfs_release_request(req); | |
515 | if (error < 0) | |
516 | return error; | |
517 | spin_lock(&nfsi->req_lock); | |
518 | res++; | |
519 | } | |
520 | spin_unlock(&nfsi->req_lock); | |
521 | return res; | |
522 | } | |
523 | ||
524 | /* | |
525 | * nfs_scan_dirty - Scan an inode for dirty requests | |
526 | * @inode: NFS inode to scan | |
527 | * @dst: destination list | |
528 | * @idx_start: lower bound of page->index to scan. | |
529 | * @npages: idx_start + npages sets the upper bound to scan. | |
530 | * | |
531 | * Moves requests from the inode's dirty page list. | |
532 | * The requests are *not* checked to ensure that they form a contiguous set. | |
533 | */ | |
534 | static int | |
535 | nfs_scan_dirty(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages) | |
536 | { | |
537 | struct nfs_inode *nfsi = NFS_I(inode); | |
3da28eb1 TM |
538 | int res = 0; |
539 | ||
540 | if (nfsi->ndirty != 0) { | |
541 | res = nfs_scan_lock_dirty(nfsi, dst, idx_start, npages); | |
542 | nfsi->ndirty -= res; | |
543 | sub_page_state(nr_dirty,res); | |
544 | if ((nfsi->ndirty == 0) != list_empty(&nfsi->dirty)) | |
545 | printk(KERN_ERR "NFS: desynchronized value of nfs_i.ndirty.\n"); | |
546 | } | |
1da177e4 LT |
547 | return res; |
548 | } | |
549 | ||
550 | #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) | |
551 | /* | |
552 | * nfs_scan_commit - Scan an inode for commit requests | |
553 | * @inode: NFS inode to scan | |
554 | * @dst: destination list | |
555 | * @idx_start: lower bound of page->index to scan. | |
556 | * @npages: idx_start + npages sets the upper bound to scan. | |
557 | * | |
558 | * Moves requests from the inode's 'commit' request list. | |
559 | * The requests are *not* checked to ensure that they form a contiguous set. | |
560 | */ | |
561 | static int | |
562 | nfs_scan_commit(struct inode *inode, struct list_head *dst, unsigned long idx_start, unsigned int npages) | |
563 | { | |
564 | struct nfs_inode *nfsi = NFS_I(inode); | |
3da28eb1 TM |
565 | int res = 0; |
566 | ||
567 | if (nfsi->ncommit != 0) { | |
568 | res = nfs_scan_list(&nfsi->commit, dst, idx_start, npages); | |
569 | nfsi->ncommit -= res; | |
570 | if ((nfsi->ncommit == 0) != list_empty(&nfsi->commit)) | |
571 | printk(KERN_ERR "NFS: desynchronized value of nfs_i.ncommit.\n"); | |
572 | } | |
1da177e4 LT |
573 | return res; |
574 | } | |
575 | #endif | |
576 | ||
577 | static int nfs_wait_on_write_congestion(struct address_space *mapping, int intr) | |
578 | { | |
579 | struct backing_dev_info *bdi = mapping->backing_dev_info; | |
580 | DEFINE_WAIT(wait); | |
581 | int ret = 0; | |
582 | ||
583 | might_sleep(); | |
584 | ||
585 | if (!bdi_write_congested(bdi)) | |
586 | return 0; | |
587 | if (intr) { | |
588 | struct rpc_clnt *clnt = NFS_CLIENT(mapping->host); | |
589 | sigset_t oldset; | |
590 | ||
591 | rpc_clnt_sigmask(clnt, &oldset); | |
592 | prepare_to_wait(&nfs_write_congestion, &wait, TASK_INTERRUPTIBLE); | |
593 | if (bdi_write_congested(bdi)) { | |
594 | if (signalled()) | |
595 | ret = -ERESTARTSYS; | |
596 | else | |
597 | schedule(); | |
598 | } | |
599 | rpc_clnt_sigunmask(clnt, &oldset); | |
600 | } else { | |
601 | prepare_to_wait(&nfs_write_congestion, &wait, TASK_UNINTERRUPTIBLE); | |
602 | if (bdi_write_congested(bdi)) | |
603 | schedule(); | |
604 | } | |
605 | finish_wait(&nfs_write_congestion, &wait); | |
606 | return ret; | |
607 | } | |
608 | ||
609 | ||
610 | /* | |
611 | * Try to update any existing write request, or create one if there is none. | |
612 | * In order to match, the request's credentials must match those of | |
613 | * the calling process. | |
614 | * | |
615 | * Note: Should always be called with the Page Lock held! | |
616 | */ | |
617 | static struct nfs_page * nfs_update_request(struct nfs_open_context* ctx, | |
618 | struct inode *inode, struct page *page, | |
619 | unsigned int offset, unsigned int bytes) | |
620 | { | |
621 | struct nfs_server *server = NFS_SERVER(inode); | |
622 | struct nfs_inode *nfsi = NFS_I(inode); | |
623 | struct nfs_page *req, *new = NULL; | |
624 | unsigned long rqend, end; | |
625 | ||
626 | end = offset + bytes; | |
627 | ||
628 | if (nfs_wait_on_write_congestion(page->mapping, server->flags & NFS_MOUNT_INTR)) | |
629 | return ERR_PTR(-ERESTARTSYS); | |
630 | for (;;) { | |
631 | /* Loop over all inode entries and see if we find | |
632 | * A request for the page we wish to update | |
633 | */ | |
634 | spin_lock(&nfsi->req_lock); | |
635 | req = _nfs_find_request(inode, page->index); | |
636 | if (req) { | |
637 | if (!nfs_lock_request_dontget(req)) { | |
638 | int error; | |
639 | spin_unlock(&nfsi->req_lock); | |
640 | error = nfs_wait_on_request(req); | |
641 | nfs_release_request(req); | |
642 | if (error < 0) | |
643 | return ERR_PTR(error); | |
644 | continue; | |
645 | } | |
646 | spin_unlock(&nfsi->req_lock); | |
647 | if (new) | |
648 | nfs_release_request(new); | |
649 | break; | |
650 | } | |
651 | ||
652 | if (new) { | |
653 | int error; | |
654 | nfs_lock_request_dontget(new); | |
655 | error = nfs_inode_add_request(inode, new); | |
656 | if (error) { | |
657 | spin_unlock(&nfsi->req_lock); | |
658 | nfs_unlock_request(new); | |
659 | return ERR_PTR(error); | |
660 | } | |
661 | spin_unlock(&nfsi->req_lock); | |
662 | nfs_mark_request_dirty(new); | |
663 | return new; | |
664 | } | |
665 | spin_unlock(&nfsi->req_lock); | |
666 | ||
667 | new = nfs_create_request(ctx, inode, page, offset, bytes); | |
668 | if (IS_ERR(new)) | |
669 | return new; | |
670 | } | |
671 | ||
672 | /* We have a request for our page. | |
673 | * If the creds don't match, or the | |
674 | * page addresses don't match, | |
675 | * tell the caller to wait on the conflicting | |
676 | * request. | |
677 | */ | |
678 | rqend = req->wb_offset + req->wb_bytes; | |
679 | if (req->wb_context != ctx | |
680 | || req->wb_page != page | |
681 | || !nfs_dirty_request(req) | |
682 | || offset > rqend || end < req->wb_offset) { | |
683 | nfs_unlock_request(req); | |
684 | return ERR_PTR(-EBUSY); | |
685 | } | |
686 | ||
687 | /* Okay, the request matches. Update the region */ | |
688 | if (offset < req->wb_offset) { | |
689 | req->wb_offset = offset; | |
690 | req->wb_pgbase = offset; | |
691 | req->wb_bytes = rqend - req->wb_offset; | |
692 | } | |
693 | ||
694 | if (end > rqend) | |
695 | req->wb_bytes = end - req->wb_offset; | |
696 | ||
697 | return req; | |
698 | } | |
699 | ||
700 | int nfs_flush_incompatible(struct file *file, struct page *page) | |
701 | { | |
702 | struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data; | |
703 | struct inode *inode = page->mapping->host; | |
704 | struct nfs_page *req; | |
705 | int status = 0; | |
706 | /* | |
707 | * Look for a request corresponding to this page. If there | |
708 | * is one, and it belongs to another file, we flush it out | |
709 | * before we try to copy anything into the page. Do this | |
710 | * due to the lack of an ACCESS-type call in NFSv2. | |
711 | * Also do the same if we find a request from an existing | |
712 | * dropped page. | |
713 | */ | |
714 | req = nfs_find_request(inode, page->index); | |
715 | if (req) { | |
716 | if (req->wb_page != page || ctx != req->wb_context) | |
717 | status = nfs_wb_page(inode, page); | |
718 | nfs_release_request(req); | |
719 | } | |
720 | return (status < 0) ? status : 0; | |
721 | } | |
722 | ||
723 | /* | |
724 | * Update and possibly write a cached page of an NFS file. | |
725 | * | |
726 | * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad | |
727 | * things with a page scheduled for an RPC call (e.g. invalidate it). | |
728 | */ | |
729 | int nfs_updatepage(struct file *file, struct page *page, | |
730 | unsigned int offset, unsigned int count) | |
731 | { | |
732 | struct nfs_open_context *ctx = (struct nfs_open_context *)file->private_data; | |
1da177e4 LT |
733 | struct inode *inode = page->mapping->host; |
734 | struct nfs_page *req; | |
735 | int status = 0; | |
736 | ||
737 | dprintk("NFS: nfs_updatepage(%s/%s %d@%Ld)\n", | |
0bbacc40 CL |
738 | file->f_dentry->d_parent->d_name.name, |
739 | file->f_dentry->d_name.name, count, | |
740 | (long long)(page_offset(page) +offset)); | |
1da177e4 LT |
741 | |
742 | if (IS_SYNC(inode)) { | |
743 | status = nfs_writepage_sync(ctx, inode, page, offset, count, 0); | |
744 | if (status > 0) { | |
745 | if (offset == 0 && status == PAGE_CACHE_SIZE) | |
746 | SetPageUptodate(page); | |
747 | return 0; | |
748 | } | |
749 | return status; | |
750 | } | |
751 | ||
752 | /* If we're not using byte range locks, and we know the page | |
753 | * is entirely in cache, it may be more efficient to avoid | |
754 | * fragmenting write requests. | |
755 | */ | |
ab0a3dbe | 756 | if (PageUptodate(page) && inode->i_flock == NULL && !(file->f_mode & O_SYNC)) { |
1da177e4 LT |
757 | loff_t end_offs = i_size_read(inode) - 1; |
758 | unsigned long end_index = end_offs >> PAGE_CACHE_SHIFT; | |
759 | ||
760 | count += offset; | |
761 | offset = 0; | |
762 | if (unlikely(end_offs < 0)) { | |
763 | /* Do nothing */ | |
764 | } else if (page->index == end_index) { | |
765 | unsigned int pglen; | |
766 | pglen = (unsigned int)(end_offs & (PAGE_CACHE_SIZE-1)) + 1; | |
767 | if (count < pglen) | |
768 | count = pglen; | |
769 | } else if (page->index < end_index) | |
770 | count = PAGE_CACHE_SIZE; | |
771 | } | |
772 | ||
773 | /* | |
774 | * Try to find an NFS request corresponding to this page | |
775 | * and update it. | |
776 | * If the existing request cannot be updated, we must flush | |
777 | * it out now. | |
778 | */ | |
779 | do { | |
780 | req = nfs_update_request(ctx, inode, page, offset, count); | |
781 | status = (IS_ERR(req)) ? PTR_ERR(req) : 0; | |
782 | if (status != -EBUSY) | |
783 | break; | |
784 | /* Request could not be updated. Flush it out and try again */ | |
785 | status = nfs_wb_page(inode, page); | |
786 | } while (status >= 0); | |
787 | if (status < 0) | |
788 | goto done; | |
789 | ||
790 | status = 0; | |
791 | ||
792 | /* Update file length */ | |
793 | nfs_grow_file(page, offset, count); | |
794 | /* Set the PG_uptodate flag? */ | |
795 | nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes); | |
796 | nfs_unlock_request(req); | |
797 | done: | |
798 | dprintk("NFS: nfs_updatepage returns %d (isize %Ld)\n", | |
799 | status, (long long)i_size_read(inode)); | |
800 | if (status < 0) | |
801 | ClearPageUptodate(page); | |
802 | return status; | |
803 | } | |
804 | ||
805 | static void nfs_writepage_release(struct nfs_page *req) | |
806 | { | |
807 | end_page_writeback(req->wb_page); | |
808 | ||
809 | #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) | |
810 | if (!PageError(req->wb_page)) { | |
811 | if (NFS_NEED_RESCHED(req)) { | |
812 | nfs_mark_request_dirty(req); | |
813 | goto out; | |
814 | } else if (NFS_NEED_COMMIT(req)) { | |
815 | nfs_mark_request_commit(req); | |
816 | goto out; | |
817 | } | |
818 | } | |
819 | nfs_inode_remove_request(req); | |
820 | ||
821 | out: | |
822 | nfs_clear_commit(req); | |
823 | nfs_clear_reschedule(req); | |
824 | #else | |
825 | nfs_inode_remove_request(req); | |
826 | #endif | |
c6a556b8 | 827 | nfs_clear_page_writeback(req); |
1da177e4 LT |
828 | } |
829 | ||
830 | static inline int flush_task_priority(int how) | |
831 | { | |
832 | switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) { | |
833 | case FLUSH_HIGHPRI: | |
834 | return RPC_PRIORITY_HIGH; | |
835 | case FLUSH_LOWPRI: | |
836 | return RPC_PRIORITY_LOW; | |
837 | } | |
838 | return RPC_PRIORITY_NORMAL; | |
839 | } | |
840 | ||
841 | /* | |
842 | * Set up the argument/result storage required for the RPC call. | |
843 | */ | |
844 | static void nfs_write_rpcsetup(struct nfs_page *req, | |
845 | struct nfs_write_data *data, | |
846 | unsigned int count, unsigned int offset, | |
847 | int how) | |
848 | { | |
1da177e4 LT |
849 | struct inode *inode; |
850 | ||
851 | /* Set up the RPC argument and reply structs | |
852 | * NB: take care not to mess about with data->commit et al. */ | |
853 | ||
854 | data->req = req; | |
855 | data->inode = inode = req->wb_context->dentry->d_inode; | |
856 | data->cred = req->wb_context->cred; | |
857 | ||
858 | data->args.fh = NFS_FH(inode); | |
859 | data->args.offset = req_offset(req) + offset; | |
860 | data->args.pgbase = req->wb_pgbase + offset; | |
861 | data->args.pages = data->pagevec; | |
862 | data->args.count = count; | |
863 | data->args.context = req->wb_context; | |
864 | ||
865 | data->res.fattr = &data->fattr; | |
866 | data->res.count = count; | |
867 | data->res.verf = &data->verf; | |
0e574af1 | 868 | nfs_fattr_init(&data->fattr); |
1da177e4 LT |
869 | |
870 | NFS_PROTO(inode)->write_setup(data, how); | |
871 | ||
872 | data->task.tk_priority = flush_task_priority(how); | |
873 | data->task.tk_cookie = (unsigned long)inode; | |
874 | data->task.tk_calldata = data; | |
875 | /* Release requests */ | |
876 | data->task.tk_release = nfs_writedata_release; | |
877 | ||
878 | dprintk("NFS: %4d initiated write call (req %s/%Ld, %u bytes @ offset %Lu)\n", | |
0bbacc40 | 879 | data->task.tk_pid, |
1da177e4 LT |
880 | inode->i_sb->s_id, |
881 | (long long)NFS_FILEID(inode), | |
882 | count, | |
883 | (unsigned long long)data->args.offset); | |
884 | } | |
885 | ||
886 | static void nfs_execute_write(struct nfs_write_data *data) | |
887 | { | |
888 | struct rpc_clnt *clnt = NFS_CLIENT(data->inode); | |
889 | sigset_t oldset; | |
890 | ||
891 | rpc_clnt_sigmask(clnt, &oldset); | |
892 | lock_kernel(); | |
893 | rpc_execute(&data->task); | |
894 | unlock_kernel(); | |
895 | rpc_clnt_sigunmask(clnt, &oldset); | |
896 | } | |
897 | ||
898 | /* | |
899 | * Generate multiple small requests to write out a single | |
900 | * contiguous dirty area on one page. | |
901 | */ | |
902 | static int nfs_flush_multi(struct list_head *head, struct inode *inode, int how) | |
903 | { | |
904 | struct nfs_page *req = nfs_list_entry(head->next); | |
905 | struct page *page = req->wb_page; | |
906 | struct nfs_write_data *data; | |
907 | unsigned int wsize = NFS_SERVER(inode)->wsize; | |
908 | unsigned int nbytes, offset; | |
909 | int requests = 0; | |
910 | LIST_HEAD(list); | |
911 | ||
912 | nfs_list_remove_request(req); | |
913 | ||
914 | nbytes = req->wb_bytes; | |
915 | for (;;) { | |
916 | data = nfs_writedata_alloc(); | |
917 | if (!data) | |
918 | goto out_bad; | |
919 | list_add(&data->pages, &list); | |
920 | requests++; | |
921 | if (nbytes <= wsize) | |
922 | break; | |
923 | nbytes -= wsize; | |
924 | } | |
925 | atomic_set(&req->wb_complete, requests); | |
926 | ||
927 | ClearPageError(page); | |
bb713d6d | 928 | set_page_writeback(page); |
1da177e4 LT |
929 | offset = 0; |
930 | nbytes = req->wb_bytes; | |
931 | do { | |
932 | data = list_entry(list.next, struct nfs_write_data, pages); | |
933 | list_del_init(&data->pages); | |
934 | ||
935 | data->pagevec[0] = page; | |
936 | data->complete = nfs_writeback_done_partial; | |
937 | ||
938 | if (nbytes > wsize) { | |
939 | nfs_write_rpcsetup(req, data, wsize, offset, how); | |
940 | offset += wsize; | |
941 | nbytes -= wsize; | |
942 | } else { | |
943 | nfs_write_rpcsetup(req, data, nbytes, offset, how); | |
944 | nbytes = 0; | |
945 | } | |
946 | nfs_execute_write(data); | |
947 | } while (nbytes != 0); | |
948 | ||
949 | return 0; | |
950 | ||
951 | out_bad: | |
952 | while (!list_empty(&list)) { | |
953 | data = list_entry(list.next, struct nfs_write_data, pages); | |
954 | list_del(&data->pages); | |
955 | nfs_writedata_free(data); | |
956 | } | |
957 | nfs_mark_request_dirty(req); | |
c6a556b8 | 958 | nfs_clear_page_writeback(req); |
1da177e4 LT |
959 | return -ENOMEM; |
960 | } | |
961 | ||
962 | /* | |
963 | * Create an RPC task for the given write request and kick it. | |
964 | * The page must have been locked by the caller. | |
965 | * | |
966 | * It may happen that the page we're passed is not marked dirty. | |
967 | * This is the case if nfs_updatepage detects a conflicting request | |
968 | * that has been written but not committed. | |
969 | */ | |
970 | static int nfs_flush_one(struct list_head *head, struct inode *inode, int how) | |
971 | { | |
972 | struct nfs_page *req; | |
973 | struct page **pages; | |
974 | struct nfs_write_data *data; | |
975 | unsigned int count; | |
976 | ||
977 | if (NFS_SERVER(inode)->wsize < PAGE_CACHE_SIZE) | |
978 | return nfs_flush_multi(head, inode, how); | |
979 | ||
980 | data = nfs_writedata_alloc(); | |
981 | if (!data) | |
982 | goto out_bad; | |
983 | ||
984 | pages = data->pagevec; | |
985 | count = 0; | |
986 | while (!list_empty(head)) { | |
987 | req = nfs_list_entry(head->next); | |
988 | nfs_list_remove_request(req); | |
989 | nfs_list_add_request(req, &data->pages); | |
990 | ClearPageError(req->wb_page); | |
bb713d6d | 991 | set_page_writeback(req->wb_page); |
1da177e4 LT |
992 | *pages++ = req->wb_page; |
993 | count += req->wb_bytes; | |
994 | } | |
995 | req = nfs_list_entry(data->pages.next); | |
996 | ||
997 | data->complete = nfs_writeback_done_full; | |
998 | /* Set up the argument struct */ | |
999 | nfs_write_rpcsetup(req, data, count, 0, how); | |
1000 | ||
1001 | nfs_execute_write(data); | |
1002 | return 0; | |
1003 | out_bad: | |
1004 | while (!list_empty(head)) { | |
1005 | struct nfs_page *req = nfs_list_entry(head->next); | |
1006 | nfs_list_remove_request(req); | |
1007 | nfs_mark_request_dirty(req); | |
c6a556b8 | 1008 | nfs_clear_page_writeback(req); |
1da177e4 LT |
1009 | } |
1010 | return -ENOMEM; | |
1011 | } | |
1012 | ||
1013 | static int | |
1014 | nfs_flush_list(struct list_head *head, int wpages, int how) | |
1015 | { | |
1016 | LIST_HEAD(one_request); | |
1017 | struct nfs_page *req; | |
1018 | int error = 0; | |
1019 | unsigned int pages = 0; | |
1020 | ||
1021 | while (!list_empty(head)) { | |
1022 | pages += nfs_coalesce_requests(head, &one_request, wpages); | |
1023 | req = nfs_list_entry(one_request.next); | |
1024 | error = nfs_flush_one(&one_request, req->wb_context->dentry->d_inode, how); | |
1025 | if (error < 0) | |
1026 | break; | |
1027 | } | |
1028 | if (error >= 0) | |
1029 | return pages; | |
1030 | ||
1031 | while (!list_empty(head)) { | |
1032 | req = nfs_list_entry(head->next); | |
1033 | nfs_list_remove_request(req); | |
1034 | nfs_mark_request_dirty(req); | |
c6a556b8 | 1035 | nfs_clear_page_writeback(req); |
1da177e4 LT |
1036 | } |
1037 | return error; | |
1038 | } | |
1039 | ||
1040 | /* | |
1041 | * Handle a write reply that flushed part of a page. | |
1042 | */ | |
1043 | static void nfs_writeback_done_partial(struct nfs_write_data *data, int status) | |
1044 | { | |
1045 | struct nfs_page *req = data->req; | |
1046 | struct page *page = req->wb_page; | |
1047 | ||
1048 | dprintk("NFS: write (%s/%Ld %d@%Ld)", | |
1049 | req->wb_context->dentry->d_inode->i_sb->s_id, | |
1050 | (long long)NFS_FILEID(req->wb_context->dentry->d_inode), | |
1051 | req->wb_bytes, | |
1052 | (long long)req_offset(req)); | |
1053 | ||
1054 | if (status < 0) { | |
1055 | ClearPageUptodate(page); | |
1056 | SetPageError(page); | |
1057 | req->wb_context->error = status; | |
1058 | dprintk(", error = %d\n", status); | |
1059 | } else { | |
1060 | #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) | |
1061 | if (data->verf.committed < NFS_FILE_SYNC) { | |
1062 | if (!NFS_NEED_COMMIT(req)) { | |
1063 | nfs_defer_commit(req); | |
1064 | memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf)); | |
1065 | dprintk(" defer commit\n"); | |
1066 | } else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) { | |
1067 | nfs_defer_reschedule(req); | |
1068 | dprintk(" server reboot detected\n"); | |
1069 | } | |
1070 | } else | |
1071 | #endif | |
1072 | dprintk(" OK\n"); | |
1073 | } | |
1074 | ||
1075 | if (atomic_dec_and_test(&req->wb_complete)) | |
1076 | nfs_writepage_release(req); | |
1077 | } | |
1078 | ||
1079 | /* | |
1080 | * Handle a write reply that flushes a whole page. | |
1081 | * | |
1082 | * FIXME: There is an inherent race with invalidate_inode_pages and | |
1083 | * writebacks since the page->count is kept > 1 for as long | |
1084 | * as the page has a write request pending. | |
1085 | */ | |
1086 | static void nfs_writeback_done_full(struct nfs_write_data *data, int status) | |
1087 | { | |
1088 | struct nfs_page *req; | |
1089 | struct page *page; | |
1090 | ||
1091 | /* Update attributes as result of writeback. */ | |
1092 | while (!list_empty(&data->pages)) { | |
1093 | req = nfs_list_entry(data->pages.next); | |
1094 | nfs_list_remove_request(req); | |
1095 | page = req->wb_page; | |
1096 | ||
1097 | dprintk("NFS: write (%s/%Ld %d@%Ld)", | |
1098 | req->wb_context->dentry->d_inode->i_sb->s_id, | |
1099 | (long long)NFS_FILEID(req->wb_context->dentry->d_inode), | |
1100 | req->wb_bytes, | |
1101 | (long long)req_offset(req)); | |
1102 | ||
1103 | if (status < 0) { | |
1104 | ClearPageUptodate(page); | |
1105 | SetPageError(page); | |
1106 | req->wb_context->error = status; | |
1107 | end_page_writeback(page); | |
1108 | nfs_inode_remove_request(req); | |
1109 | dprintk(", error = %d\n", status); | |
1110 | goto next; | |
1111 | } | |
1112 | end_page_writeback(page); | |
1113 | ||
1114 | #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) | |
1115 | if (data->args.stable != NFS_UNSTABLE || data->verf.committed == NFS_FILE_SYNC) { | |
1116 | nfs_inode_remove_request(req); | |
1117 | dprintk(" OK\n"); | |
1118 | goto next; | |
1119 | } | |
1120 | memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf)); | |
1121 | nfs_mark_request_commit(req); | |
1122 | dprintk(" marked for commit\n"); | |
1123 | #else | |
1124 | nfs_inode_remove_request(req); | |
1125 | #endif | |
1126 | next: | |
c6a556b8 | 1127 | nfs_clear_page_writeback(req); |
1da177e4 LT |
1128 | } |
1129 | } | |
1130 | ||
1131 | /* | |
1132 | * This function is called when the WRITE call is complete. | |
1133 | */ | |
1134 | void nfs_writeback_done(struct rpc_task *task) | |
1135 | { | |
1136 | struct nfs_write_data *data = (struct nfs_write_data *) task->tk_calldata; | |
1137 | struct nfs_writeargs *argp = &data->args; | |
1138 | struct nfs_writeres *resp = &data->res; | |
1139 | ||
1140 | dprintk("NFS: %4d nfs_writeback_done (status %d)\n", | |
1141 | task->tk_pid, task->tk_status); | |
1142 | ||
1143 | #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) | |
1144 | if (resp->verf->committed < argp->stable && task->tk_status >= 0) { | |
1145 | /* We tried a write call, but the server did not | |
1146 | * commit data to stable storage even though we | |
1147 | * requested it. | |
1148 | * Note: There is a known bug in Tru64 < 5.0 in which | |
1149 | * the server reports NFS_DATA_SYNC, but performs | |
1150 | * NFS_FILE_SYNC. We therefore implement this checking | |
1151 | * as a dprintk() in order to avoid filling syslog. | |
1152 | */ | |
1153 | static unsigned long complain; | |
1154 | ||
1155 | if (time_before(complain, jiffies)) { | |
1156 | dprintk("NFS: faulty NFS server %s:" | |
1157 | " (committed = %d) != (stable = %d)\n", | |
1158 | NFS_SERVER(data->inode)->hostname, | |
1159 | resp->verf->committed, argp->stable); | |
1160 | complain = jiffies + 300 * HZ; | |
1161 | } | |
1162 | } | |
1163 | #endif | |
1164 | /* Is this a short write? */ | |
1165 | if (task->tk_status >= 0 && resp->count < argp->count) { | |
1166 | static unsigned long complain; | |
1167 | ||
1168 | /* Has the server at least made some progress? */ | |
1169 | if (resp->count != 0) { | |
1170 | /* Was this an NFSv2 write or an NFSv3 stable write? */ | |
1171 | if (resp->verf->committed != NFS_UNSTABLE) { | |
1172 | /* Resend from where the server left off */ | |
1173 | argp->offset += resp->count; | |
1174 | argp->pgbase += resp->count; | |
1175 | argp->count -= resp->count; | |
1176 | } else { | |
1177 | /* Resend as a stable write in order to avoid | |
1178 | * headaches in the case of a server crash. | |
1179 | */ | |
1180 | argp->stable = NFS_FILE_SYNC; | |
1181 | } | |
1182 | rpc_restart_call(task); | |
1183 | return; | |
1184 | } | |
1185 | if (time_before(complain, jiffies)) { | |
1186 | printk(KERN_WARNING | |
1187 | "NFS: Server wrote zero bytes, expected %u.\n", | |
1188 | argp->count); | |
1189 | complain = jiffies + 300 * HZ; | |
1190 | } | |
1191 | /* Can't do anything about it except throw an error. */ | |
1192 | task->tk_status = -EIO; | |
1193 | } | |
1194 | ||
1195 | /* | |
1196 | * Process the nfs_page list | |
1197 | */ | |
1198 | data->complete(data, task->tk_status); | |
1199 | } | |
1200 | ||
1201 | ||
1202 | #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) | |
1203 | static void nfs_commit_release(struct rpc_task *task) | |
1204 | { | |
1205 | struct nfs_write_data *wdata = (struct nfs_write_data *)task->tk_calldata; | |
1206 | nfs_commit_free(wdata); | |
1207 | } | |
1208 | ||
1209 | /* | |
1210 | * Set up the argument/result storage required for the RPC call. | |
1211 | */ | |
1212 | static void nfs_commit_rpcsetup(struct list_head *head, | |
1213 | struct nfs_write_data *data, int how) | |
1214 | { | |
3da28eb1 | 1215 | struct nfs_page *first; |
1da177e4 | 1216 | struct inode *inode; |
1da177e4 LT |
1217 | |
1218 | /* Set up the RPC argument and reply structs | |
1219 | * NB: take care not to mess about with data->commit et al. */ | |
1220 | ||
1221 | list_splice_init(head, &data->pages); | |
1222 | first = nfs_list_entry(data->pages.next); | |
1da177e4 LT |
1223 | inode = first->wb_context->dentry->d_inode; |
1224 | ||
1da177e4 LT |
1225 | data->inode = inode; |
1226 | data->cred = first->wb_context->cred; | |
1227 | ||
1228 | data->args.fh = NFS_FH(data->inode); | |
3da28eb1 TM |
1229 | /* Note: we always request a commit of the entire inode */ |
1230 | data->args.offset = 0; | |
1231 | data->args.count = 0; | |
1232 | data->res.count = 0; | |
1da177e4 LT |
1233 | data->res.fattr = &data->fattr; |
1234 | data->res.verf = &data->verf; | |
0e574af1 | 1235 | nfs_fattr_init(&data->fattr); |
1da177e4 LT |
1236 | |
1237 | NFS_PROTO(inode)->commit_setup(data, how); | |
1238 | ||
1239 | data->task.tk_priority = flush_task_priority(how); | |
1240 | data->task.tk_cookie = (unsigned long)inode; | |
1241 | data->task.tk_calldata = data; | |
1242 | /* Release requests */ | |
1243 | data->task.tk_release = nfs_commit_release; | |
1244 | ||
0bbacc40 | 1245 | dprintk("NFS: %4d initiated commit call\n", data->task.tk_pid); |
1da177e4 LT |
1246 | } |
1247 | ||
1248 | /* | |
1249 | * Commit dirty pages | |
1250 | */ | |
1251 | static int | |
1252 | nfs_commit_list(struct list_head *head, int how) | |
1253 | { | |
1254 | struct nfs_write_data *data; | |
1255 | struct nfs_page *req; | |
1256 | ||
1257 | data = nfs_commit_alloc(); | |
1258 | ||
1259 | if (!data) | |
1260 | goto out_bad; | |
1261 | ||
1262 | /* Set up the argument struct */ | |
1263 | nfs_commit_rpcsetup(head, data, how); | |
1264 | ||
1265 | nfs_execute_write(data); | |
1266 | return 0; | |
1267 | out_bad: | |
1268 | while (!list_empty(head)) { | |
1269 | req = nfs_list_entry(head->next); | |
1270 | nfs_list_remove_request(req); | |
1271 | nfs_mark_request_commit(req); | |
c6a556b8 | 1272 | nfs_clear_page_writeback(req); |
1da177e4 LT |
1273 | } |
1274 | return -ENOMEM; | |
1275 | } | |
1276 | ||
1277 | /* | |
1278 | * COMMIT call returned | |
1279 | */ | |
1280 | void | |
1281 | nfs_commit_done(struct rpc_task *task) | |
1282 | { | |
1283 | struct nfs_write_data *data = (struct nfs_write_data *)task->tk_calldata; | |
1284 | struct nfs_page *req; | |
1285 | int res = 0; | |
1286 | ||
1287 | dprintk("NFS: %4d nfs_commit_done (status %d)\n", | |
1288 | task->tk_pid, task->tk_status); | |
1289 | ||
1290 | while (!list_empty(&data->pages)) { | |
1291 | req = nfs_list_entry(data->pages.next); | |
1292 | nfs_list_remove_request(req); | |
1293 | ||
1294 | dprintk("NFS: commit (%s/%Ld %d@%Ld)", | |
1295 | req->wb_context->dentry->d_inode->i_sb->s_id, | |
1296 | (long long)NFS_FILEID(req->wb_context->dentry->d_inode), | |
1297 | req->wb_bytes, | |
1298 | (long long)req_offset(req)); | |
1299 | if (task->tk_status < 0) { | |
1300 | req->wb_context->error = task->tk_status; | |
1301 | nfs_inode_remove_request(req); | |
1302 | dprintk(", error = %d\n", task->tk_status); | |
1303 | goto next; | |
1304 | } | |
1305 | ||
1306 | /* Okay, COMMIT succeeded, apparently. Check the verifier | |
1307 | * returned by the server against all stored verfs. */ | |
1308 | if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) { | |
1309 | /* We have a match */ | |
1310 | nfs_inode_remove_request(req); | |
1311 | dprintk(" OK\n"); | |
1312 | goto next; | |
1313 | } | |
1314 | /* We have a mismatch. Write the page again */ | |
1315 | dprintk(" mismatch\n"); | |
1316 | nfs_mark_request_dirty(req); | |
1317 | next: | |
c6a556b8 | 1318 | nfs_clear_page_writeback(req); |
1da177e4 LT |
1319 | res++; |
1320 | } | |
1321 | sub_page_state(nr_unstable,res); | |
1322 | } | |
1323 | #endif | |
1324 | ||
1325 | static int nfs_flush_inode(struct inode *inode, unsigned long idx_start, | |
1326 | unsigned int npages, int how) | |
1327 | { | |
1328 | struct nfs_inode *nfsi = NFS_I(inode); | |
1329 | LIST_HEAD(head); | |
1330 | int res, | |
1331 | error = 0; | |
1332 | ||
1333 | spin_lock(&nfsi->req_lock); | |
1334 | res = nfs_scan_dirty(inode, &head, idx_start, npages); | |
1335 | spin_unlock(&nfsi->req_lock); | |
ab0a3dbe TM |
1336 | if (res) { |
1337 | struct nfs_server *server = NFS_SERVER(inode); | |
1338 | ||
1339 | /* For single writes, FLUSH_STABLE is more efficient */ | |
1340 | if (res == nfsi->npages && nfsi->npages <= server->wpages) { | |
1341 | if (res > 1 || nfs_list_entry(head.next)->wb_bytes <= server->wsize) | |
1342 | how |= FLUSH_STABLE; | |
1343 | } | |
1344 | error = nfs_flush_list(&head, server->wpages, how); | |
1345 | } | |
1da177e4 LT |
1346 | if (error < 0) |
1347 | return error; | |
1348 | return res; | |
1349 | } | |
1350 | ||
1351 | #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) | |
3da28eb1 | 1352 | int nfs_commit_inode(struct inode *inode, int how) |
1da177e4 LT |
1353 | { |
1354 | struct nfs_inode *nfsi = NFS_I(inode); | |
1355 | LIST_HEAD(head); | |
1356 | int res, | |
1357 | error = 0; | |
1358 | ||
1359 | spin_lock(&nfsi->req_lock); | |
3da28eb1 TM |
1360 | res = nfs_scan_commit(inode, &head, 0, 0); |
1361 | spin_unlock(&nfsi->req_lock); | |
1da177e4 | 1362 | if (res) { |
1da177e4 | 1363 | error = nfs_commit_list(&head, how); |
3da28eb1 TM |
1364 | if (error < 0) |
1365 | return error; | |
1366 | } | |
1da177e4 LT |
1367 | return res; |
1368 | } | |
1369 | #endif | |
1370 | ||
1371 | int nfs_sync_inode(struct inode *inode, unsigned long idx_start, | |
1372 | unsigned int npages, int how) | |
1373 | { | |
1374 | int error, | |
1375 | wait; | |
1376 | ||
1377 | wait = how & FLUSH_WAIT; | |
1378 | how &= ~FLUSH_WAIT; | |
1379 | ||
1380 | do { | |
1381 | error = 0; | |
1382 | if (wait) | |
1383 | error = nfs_wait_on_requests(inode, idx_start, npages); | |
1384 | if (error == 0) | |
1385 | error = nfs_flush_inode(inode, idx_start, npages, how); | |
1386 | #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) | |
1387 | if (error == 0) | |
3da28eb1 | 1388 | error = nfs_commit_inode(inode, how); |
1da177e4 LT |
1389 | #endif |
1390 | } while (error > 0); | |
1391 | return error; | |
1392 | } | |
1393 | ||
1394 | int nfs_init_writepagecache(void) | |
1395 | { | |
1396 | nfs_wdata_cachep = kmem_cache_create("nfs_write_data", | |
1397 | sizeof(struct nfs_write_data), | |
1398 | 0, SLAB_HWCACHE_ALIGN, | |
1399 | NULL, NULL); | |
1400 | if (nfs_wdata_cachep == NULL) | |
1401 | return -ENOMEM; | |
1402 | ||
1403 | nfs_wdata_mempool = mempool_create(MIN_POOL_WRITE, | |
1404 | mempool_alloc_slab, | |
1405 | mempool_free_slab, | |
1406 | nfs_wdata_cachep); | |
1407 | if (nfs_wdata_mempool == NULL) | |
1408 | return -ENOMEM; | |
1409 | ||
1410 | nfs_commit_mempool = mempool_create(MIN_POOL_COMMIT, | |
1411 | mempool_alloc_slab, | |
1412 | mempool_free_slab, | |
1413 | nfs_wdata_cachep); | |
1414 | if (nfs_commit_mempool == NULL) | |
1415 | return -ENOMEM; | |
1416 | ||
1417 | return 0; | |
1418 | } | |
1419 | ||
1420 | void nfs_destroy_writepagecache(void) | |
1421 | { | |
1422 | mempool_destroy(nfs_commit_mempool); | |
1423 | mempool_destroy(nfs_wdata_mempool); | |
1424 | if (kmem_cache_destroy(nfs_wdata_cachep)) | |
1425 | printk(KERN_INFO "nfs_write_data: not all structures were freed\n"); | |
1426 | } | |
1427 |