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