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