]>
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> |
074cc1de | 16 | #include <linux/migrate.h> |
1da177e4 LT |
17 | |
18 | #include <linux/sunrpc/clnt.h> | |
19 | #include <linux/nfs_fs.h> | |
20 | #include <linux/nfs_mount.h> | |
21 | #include <linux/nfs_page.h> | |
3fcfab16 | 22 | #include <linux/backing-dev.h> |
afeacc8c | 23 | #include <linux/export.h> |
3fcfab16 | 24 | |
1da177e4 | 25 | #include <asm/uaccess.h> |
1da177e4 LT |
26 | |
27 | #include "delegation.h" | |
49a70f27 | 28 | #include "internal.h" |
91d5b470 | 29 | #include "iostat.h" |
def6ed7e | 30 | #include "nfs4_fs.h" |
074cc1de | 31 | #include "fscache.h" |
94ad1c80 | 32 | #include "pnfs.h" |
1da177e4 LT |
33 | |
34 | #define NFSDBG_FACILITY NFSDBG_PAGECACHE | |
35 | ||
36 | #define MIN_POOL_WRITE (32) | |
37 | #define MIN_POOL_COMMIT (4) | |
38 | ||
39 | /* | |
40 | * Local function declarations | |
41 | */ | |
c63c7b05 | 42 | static void nfs_pageio_init_write(struct nfs_pageio_descriptor *desc, |
061ae2ed FI |
43 | struct inode *inode, int ioflags, |
44 | const struct nfs_pgio_completion_ops *compl_ops); | |
f8512ad0 | 45 | static void nfs_redirty_request(struct nfs_page *req); |
6c75dc0d | 46 | static const struct rpc_call_ops nfs_write_common_ops; |
788e7a89 | 47 | static const struct rpc_call_ops nfs_commit_ops; |
061ae2ed | 48 | static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops; |
f453a54a | 49 | static const struct nfs_commit_completion_ops nfs_commit_completion_ops; |
1da177e4 | 50 | |
e18b890b | 51 | static struct kmem_cache *nfs_wdata_cachep; |
3feb2d49 | 52 | static mempool_t *nfs_wdata_mempool; |
0b7c0153 | 53 | static struct kmem_cache *nfs_cdata_cachep; |
1da177e4 LT |
54 | static mempool_t *nfs_commit_mempool; |
55 | ||
0b7c0153 | 56 | struct nfs_commit_data *nfs_commitdata_alloc(void) |
1da177e4 | 57 | { |
0b7c0153 | 58 | struct nfs_commit_data *p = mempool_alloc(nfs_commit_mempool, GFP_NOFS); |
40859d7e | 59 | |
1da177e4 LT |
60 | if (p) { |
61 | memset(p, 0, sizeof(*p)); | |
62 | INIT_LIST_HEAD(&p->pages); | |
63 | } | |
64 | return p; | |
65 | } | |
e0c2b380 | 66 | EXPORT_SYMBOL_GPL(nfs_commitdata_alloc); |
1da177e4 | 67 | |
0b7c0153 | 68 | void nfs_commit_free(struct nfs_commit_data *p) |
1da177e4 LT |
69 | { |
70 | mempool_free(p, nfs_commit_mempool); | |
71 | } | |
e0c2b380 | 72 | EXPORT_SYMBOL_GPL(nfs_commit_free); |
1da177e4 | 73 | |
6c75dc0d | 74 | struct nfs_write_header *nfs_writehdr_alloc(void) |
3feb2d49 | 75 | { |
cd841605 | 76 | struct nfs_write_header *p = mempool_alloc(nfs_wdata_mempool, GFP_NOFS); |
3feb2d49 TM |
77 | |
78 | if (p) { | |
cd841605 | 79 | struct nfs_pgio_header *hdr = &p->header; |
cd841605 | 80 | |
3feb2d49 | 81 | memset(p, 0, sizeof(*p)); |
cd841605 | 82 | INIT_LIST_HEAD(&hdr->pages); |
6c75dc0d FI |
83 | INIT_LIST_HEAD(&hdr->rpc_list); |
84 | spin_lock_init(&hdr->lock); | |
85 | atomic_set(&hdr->refcnt, 0); | |
3feb2d49 TM |
86 | } |
87 | return p; | |
88 | } | |
89 | ||
6c75dc0d FI |
90 | struct nfs_write_data *nfs_writedata_alloc(struct nfs_pgio_header *hdr, |
91 | unsigned int pagecount) | |
92 | { | |
93 | struct nfs_write_data *data, *prealloc; | |
94 | ||
95 | prealloc = &container_of(hdr, struct nfs_write_header, header)->rpc_data; | |
96 | if (prealloc->header == NULL) | |
97 | data = prealloc; | |
98 | else | |
99 | data = kzalloc(sizeof(*data), GFP_KERNEL); | |
100 | if (!data) | |
101 | goto out; | |
102 | ||
103 | if (nfs_pgarray_set(&data->pages, pagecount)) { | |
104 | data->header = hdr; | |
105 | atomic_inc(&hdr->refcnt); | |
106 | } else { | |
107 | if (data != prealloc) | |
108 | kfree(data); | |
109 | data = NULL; | |
110 | } | |
111 | out: | |
112 | return data; | |
113 | } | |
114 | ||
cd841605 | 115 | void nfs_writehdr_free(struct nfs_pgio_header *hdr) |
3feb2d49 | 116 | { |
cd841605 FI |
117 | struct nfs_write_header *whdr = container_of(hdr, struct nfs_write_header, header); |
118 | mempool_free(whdr, nfs_wdata_mempool); | |
3feb2d49 TM |
119 | } |
120 | ||
dce81290 | 121 | void nfs_writedata_release(struct nfs_write_data *wdata) |
1da177e4 | 122 | { |
6c75dc0d FI |
123 | struct nfs_pgio_header *hdr = wdata->header; |
124 | struct nfs_write_header *write_header = container_of(hdr, struct nfs_write_header, header); | |
125 | ||
383ba719 | 126 | put_nfs_open_context(wdata->args.context); |
30dd374f FI |
127 | if (wdata->pages.pagevec != wdata->pages.page_array) |
128 | kfree(wdata->pages.pagevec); | |
6c75dc0d FI |
129 | if (wdata != &write_header->rpc_data) |
130 | kfree(wdata); | |
131 | else | |
132 | wdata->header = NULL; | |
133 | if (atomic_dec_and_test(&hdr->refcnt)) | |
061ae2ed | 134 | hdr->completion_ops->completion(hdr); |
1da177e4 LT |
135 | } |
136 | ||
7b159fc1 TM |
137 | static void nfs_context_set_write_error(struct nfs_open_context *ctx, int error) |
138 | { | |
139 | ctx->error = error; | |
140 | smp_wmb(); | |
141 | set_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags); | |
142 | } | |
143 | ||
277459d2 TM |
144 | static struct nfs_page *nfs_page_find_request_locked(struct page *page) |
145 | { | |
146 | struct nfs_page *req = NULL; | |
147 | ||
148 | if (PagePrivate(page)) { | |
149 | req = (struct nfs_page *)page_private(page); | |
150 | if (req != NULL) | |
c03b4024 | 151 | kref_get(&req->wb_kref); |
277459d2 TM |
152 | } |
153 | return req; | |
154 | } | |
155 | ||
156 | static struct nfs_page *nfs_page_find_request(struct page *page) | |
157 | { | |
587142f8 | 158 | struct inode *inode = page->mapping->host; |
277459d2 | 159 | struct nfs_page *req = NULL; |
277459d2 | 160 | |
587142f8 | 161 | spin_lock(&inode->i_lock); |
277459d2 | 162 | req = nfs_page_find_request_locked(page); |
587142f8 | 163 | spin_unlock(&inode->i_lock); |
277459d2 TM |
164 | return req; |
165 | } | |
166 | ||
1da177e4 LT |
167 | /* Adjust the file length if we're writing beyond the end */ |
168 | static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count) | |
169 | { | |
170 | struct inode *inode = page->mapping->host; | |
a3d01454 TM |
171 | loff_t end, i_size; |
172 | pgoff_t end_index; | |
1da177e4 | 173 | |
a3d01454 TM |
174 | spin_lock(&inode->i_lock); |
175 | i_size = i_size_read(inode); | |
176 | end_index = (i_size - 1) >> PAGE_CACHE_SHIFT; | |
1da177e4 | 177 | if (i_size > 0 && page->index < end_index) |
a3d01454 | 178 | goto out; |
1da177e4 LT |
179 | end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + ((loff_t)offset+count); |
180 | if (i_size >= end) | |
a3d01454 | 181 | goto out; |
1da177e4 | 182 | i_size_write(inode, end); |
a3d01454 TM |
183 | nfs_inc_stats(inode, NFSIOS_EXTENDWRITE); |
184 | out: | |
185 | spin_unlock(&inode->i_lock); | |
1da177e4 LT |
186 | } |
187 | ||
a301b777 TM |
188 | /* A writeback failed: mark the page as bad, and invalidate the page cache */ |
189 | static void nfs_set_pageerror(struct page *page) | |
190 | { | |
191 | SetPageError(page); | |
192 | nfs_zap_mapping(page->mapping->host, page->mapping); | |
193 | } | |
194 | ||
1da177e4 LT |
195 | /* We can set the PG_uptodate flag if we see that a write request |
196 | * covers the full page. | |
197 | */ | |
198 | static void nfs_mark_uptodate(struct page *page, unsigned int base, unsigned int count) | |
199 | { | |
1da177e4 LT |
200 | if (PageUptodate(page)) |
201 | return; | |
202 | if (base != 0) | |
203 | return; | |
49a70f27 | 204 | if (count != nfs_page_length(page)) |
1da177e4 | 205 | return; |
49a70f27 | 206 | SetPageUptodate(page); |
1da177e4 LT |
207 | } |
208 | ||
1da177e4 LT |
209 | static int wb_priority(struct writeback_control *wbc) |
210 | { | |
211 | if (wbc->for_reclaim) | |
c63c7b05 | 212 | return FLUSH_HIGHPRI | FLUSH_STABLE; |
b17621fe | 213 | if (wbc->for_kupdate || wbc->for_background) |
b31268ac TM |
214 | return FLUSH_LOWPRI | FLUSH_COND_STABLE; |
215 | return FLUSH_COND_STABLE; | |
1da177e4 LT |
216 | } |
217 | ||
89a09141 PZ |
218 | /* |
219 | * NFS congestion control | |
220 | */ | |
221 | ||
222 | int nfs_congestion_kb; | |
223 | ||
224 | #define NFS_CONGESTION_ON_THRESH (nfs_congestion_kb >> (PAGE_SHIFT-10)) | |
225 | #define NFS_CONGESTION_OFF_THRESH \ | |
226 | (NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2)) | |
227 | ||
5a6d41b3 | 228 | static int nfs_set_page_writeback(struct page *page) |
89a09141 | 229 | { |
5a6d41b3 TM |
230 | int ret = test_set_page_writeback(page); |
231 | ||
232 | if (!ret) { | |
89a09141 PZ |
233 | struct inode *inode = page->mapping->host; |
234 | struct nfs_server *nfss = NFS_SERVER(inode); | |
235 | ||
a6305ddb | 236 | page_cache_get(page); |
277866a0 | 237 | if (atomic_long_inc_return(&nfss->writeback) > |
8aa7e847 JA |
238 | NFS_CONGESTION_ON_THRESH) { |
239 | set_bdi_congested(&nfss->backing_dev_info, | |
240 | BLK_RW_ASYNC); | |
241 | } | |
89a09141 | 242 | } |
5a6d41b3 | 243 | return ret; |
89a09141 PZ |
244 | } |
245 | ||
246 | static void nfs_end_page_writeback(struct page *page) | |
247 | { | |
248 | struct inode *inode = page->mapping->host; | |
249 | struct nfs_server *nfss = NFS_SERVER(inode); | |
250 | ||
251 | end_page_writeback(page); | |
a6305ddb | 252 | page_cache_release(page); |
c4dc4bee | 253 | if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH) |
8aa7e847 | 254 | clear_bdi_congested(&nfss->backing_dev_info, BLK_RW_ASYNC); |
89a09141 PZ |
255 | } |
256 | ||
cfb506e1 | 257 | static struct nfs_page *nfs_find_and_lock_request(struct page *page, bool nonblock) |
e261f51f | 258 | { |
587142f8 | 259 | struct inode *inode = page->mapping->host; |
e261f51f | 260 | struct nfs_page *req; |
e261f51f TM |
261 | int ret; |
262 | ||
587142f8 | 263 | spin_lock(&inode->i_lock); |
074cc1de | 264 | for (;;) { |
e261f51f | 265 | req = nfs_page_find_request_locked(page); |
074cc1de TM |
266 | if (req == NULL) |
267 | break; | |
9994b62b | 268 | if (nfs_lock_request_dontget(req)) |
e261f51f TM |
269 | break; |
270 | /* Note: If we hold the page lock, as is the case in nfs_writepage, | |
9994b62b | 271 | * then the call to nfs_lock_request_dontget() will always |
e261f51f TM |
272 | * succeed provided that someone hasn't already marked the |
273 | * request as dirty (in which case we don't care). | |
274 | */ | |
587142f8 | 275 | spin_unlock(&inode->i_lock); |
cfb506e1 TM |
276 | if (!nonblock) |
277 | ret = nfs_wait_on_request(req); | |
278 | else | |
279 | ret = -EAGAIN; | |
e261f51f TM |
280 | nfs_release_request(req); |
281 | if (ret != 0) | |
074cc1de | 282 | return ERR_PTR(ret); |
587142f8 | 283 | spin_lock(&inode->i_lock); |
e261f51f | 284 | } |
587142f8 | 285 | spin_unlock(&inode->i_lock); |
074cc1de TM |
286 | return req; |
287 | } | |
288 | ||
289 | /* | |
290 | * Find an associated nfs write request, and prepare to flush it out | |
291 | * May return an error if the user signalled nfs_wait_on_request(). | |
292 | */ | |
293 | static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio, | |
cfb506e1 | 294 | struct page *page, bool nonblock) |
074cc1de TM |
295 | { |
296 | struct nfs_page *req; | |
297 | int ret = 0; | |
298 | ||
cfb506e1 | 299 | req = nfs_find_and_lock_request(page, nonblock); |
074cc1de TM |
300 | if (!req) |
301 | goto out; | |
302 | ret = PTR_ERR(req); | |
303 | if (IS_ERR(req)) | |
304 | goto out; | |
305 | ||
306 | ret = nfs_set_page_writeback(page); | |
307 | BUG_ON(ret != 0); | |
308 | BUG_ON(test_bit(PG_CLEAN, &req->wb_flags)); | |
309 | ||
f8512ad0 FI |
310 | if (!nfs_pageio_add_request(pgio, req)) { |
311 | nfs_redirty_request(req); | |
074cc1de | 312 | ret = pgio->pg_error; |
f8512ad0 | 313 | } |
074cc1de TM |
314 | out: |
315 | return ret; | |
e261f51f TM |
316 | } |
317 | ||
f758c885 | 318 | static int nfs_do_writepage(struct page *page, struct writeback_control *wbc, struct nfs_pageio_descriptor *pgio) |
1da177e4 | 319 | { |
1da177e4 | 320 | struct inode *inode = page->mapping->host; |
cfb506e1 | 321 | int ret; |
1da177e4 | 322 | |
91d5b470 CL |
323 | nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE); |
324 | nfs_add_stats(inode, NFSIOS_WRITEPAGES, 1); | |
325 | ||
7fe7f848 | 326 | nfs_pageio_cond_complete(pgio, page->index); |
1b430bee | 327 | ret = nfs_page_async_flush(pgio, page, wbc->sync_mode == WB_SYNC_NONE); |
cfb506e1 TM |
328 | if (ret == -EAGAIN) { |
329 | redirty_page_for_writepage(wbc, page); | |
330 | ret = 0; | |
331 | } | |
332 | return ret; | |
f758c885 | 333 | } |
7fe7f848 | 334 | |
f758c885 TM |
335 | /* |
336 | * Write an mmapped page to the server. | |
337 | */ | |
338 | static int nfs_writepage_locked(struct page *page, struct writeback_control *wbc) | |
339 | { | |
340 | struct nfs_pageio_descriptor pgio; | |
341 | int err; | |
49a70f27 | 342 | |
061ae2ed FI |
343 | nfs_pageio_init_write(&pgio, page->mapping->host, wb_priority(wbc), |
344 | &nfs_async_write_completion_ops); | |
f758c885 TM |
345 | err = nfs_do_writepage(page, wbc, &pgio); |
346 | nfs_pageio_complete(&pgio); | |
347 | if (err < 0) | |
348 | return err; | |
349 | if (pgio.pg_error < 0) | |
350 | return pgio.pg_error; | |
351 | return 0; | |
4d770ccf TM |
352 | } |
353 | ||
354 | int nfs_writepage(struct page *page, struct writeback_control *wbc) | |
355 | { | |
f758c885 | 356 | int ret; |
4d770ccf | 357 | |
f758c885 | 358 | ret = nfs_writepage_locked(page, wbc); |
1da177e4 | 359 | unlock_page(page); |
f758c885 TM |
360 | return ret; |
361 | } | |
362 | ||
363 | static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data) | |
364 | { | |
365 | int ret; | |
366 | ||
367 | ret = nfs_do_writepage(page, wbc, data); | |
368 | unlock_page(page); | |
369 | return ret; | |
1da177e4 LT |
370 | } |
371 | ||
1da177e4 LT |
372 | int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc) |
373 | { | |
1da177e4 | 374 | struct inode *inode = mapping->host; |
72cb77f4 | 375 | unsigned long *bitlock = &NFS_I(inode)->flags; |
c63c7b05 | 376 | struct nfs_pageio_descriptor pgio; |
1da177e4 LT |
377 | int err; |
378 | ||
72cb77f4 TM |
379 | /* Stop dirtying of new pages while we sync */ |
380 | err = wait_on_bit_lock(bitlock, NFS_INO_FLUSHING, | |
381 | nfs_wait_bit_killable, TASK_KILLABLE); | |
382 | if (err) | |
383 | goto out_err; | |
384 | ||
91d5b470 CL |
385 | nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES); |
386 | ||
061ae2ed FI |
387 | nfs_pageio_init_write(&pgio, inode, wb_priority(wbc), |
388 | &nfs_async_write_completion_ops); | |
f758c885 | 389 | err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio); |
c63c7b05 | 390 | nfs_pageio_complete(&pgio); |
72cb77f4 TM |
391 | |
392 | clear_bit_unlock(NFS_INO_FLUSHING, bitlock); | |
393 | smp_mb__after_clear_bit(); | |
394 | wake_up_bit(bitlock, NFS_INO_FLUSHING); | |
395 | ||
f758c885 | 396 | if (err < 0) |
72cb77f4 TM |
397 | goto out_err; |
398 | err = pgio.pg_error; | |
399 | if (err < 0) | |
400 | goto out_err; | |
c63c7b05 | 401 | return 0; |
72cb77f4 TM |
402 | out_err: |
403 | return err; | |
1da177e4 LT |
404 | } |
405 | ||
406 | /* | |
407 | * Insert a write request into an inode | |
408 | */ | |
d6d6dc7c | 409 | static void nfs_inode_add_request(struct inode *inode, struct nfs_page *req) |
1da177e4 LT |
410 | { |
411 | struct nfs_inode *nfsi = NFS_I(inode); | |
e7d39069 TM |
412 | |
413 | /* Lock the request! */ | |
414 | nfs_lock_request_dontget(req); | |
415 | ||
416 | spin_lock(&inode->i_lock); | |
4d65c520 | 417 | if (!nfsi->npages && nfs_have_delegation(inode, FMODE_WRITE)) |
a9a4a87a | 418 | inode->i_version++; |
2df485a7 | 419 | set_bit(PG_MAPPED, &req->wb_flags); |
deb7d638 | 420 | SetPagePrivate(req->wb_page); |
277459d2 | 421 | set_page_private(req->wb_page, (unsigned long)req); |
1da177e4 | 422 | nfsi->npages++; |
c03b4024 | 423 | kref_get(&req->wb_kref); |
e7d39069 | 424 | spin_unlock(&inode->i_lock); |
1da177e4 LT |
425 | } |
426 | ||
427 | /* | |
89a09141 | 428 | * Remove a write request from an inode |
1da177e4 LT |
429 | */ |
430 | static void nfs_inode_remove_request(struct nfs_page *req) | |
431 | { | |
3d4ff43d | 432 | struct inode *inode = req->wb_context->dentry->d_inode; |
1da177e4 LT |
433 | struct nfs_inode *nfsi = NFS_I(inode); |
434 | ||
435 | BUG_ON (!NFS_WBACK_BUSY(req)); | |
436 | ||
587142f8 | 437 | spin_lock(&inode->i_lock); |
277459d2 | 438 | set_page_private(req->wb_page, 0); |
deb7d638 | 439 | ClearPagePrivate(req->wb_page); |
2df485a7 | 440 | clear_bit(PG_MAPPED, &req->wb_flags); |
1da177e4 | 441 | nfsi->npages--; |
4d65c520 | 442 | spin_unlock(&inode->i_lock); |
1da177e4 LT |
443 | nfs_release_request(req); |
444 | } | |
445 | ||
61822ab5 | 446 | static void |
6d884e8f | 447 | nfs_mark_request_dirty(struct nfs_page *req) |
61822ab5 | 448 | { |
61822ab5 TM |
449 | __set_page_dirty_nobuffers(req->wb_page); |
450 | } | |
451 | ||
1da177e4 | 452 | #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) |
8dd37758 TM |
453 | /** |
454 | * nfs_request_add_commit_list - add request to a commit list | |
455 | * @req: pointer to a struct nfs_page | |
ea2cf228 FI |
456 | * @dst: commit list head |
457 | * @cinfo: holds list lock and accounting info | |
8dd37758 | 458 | * |
ea2cf228 | 459 | * This sets the PG_CLEAN bit, updates the cinfo count of |
8dd37758 TM |
460 | * number of outstanding requests requiring a commit as well as |
461 | * the MM page stats. | |
462 | * | |
ea2cf228 | 463 | * The caller must _not_ hold the cinfo->lock, but must be |
8dd37758 | 464 | * holding the nfs_page lock. |
1da177e4 | 465 | */ |
8dd37758 | 466 | void |
ea2cf228 FI |
467 | nfs_request_add_commit_list(struct nfs_page *req, struct list_head *dst, |
468 | struct nfs_commit_info *cinfo) | |
1da177e4 | 469 | { |
e468bae9 | 470 | set_bit(PG_CLEAN, &(req)->wb_flags); |
ea2cf228 FI |
471 | spin_lock(cinfo->lock); |
472 | nfs_list_add_request(req, dst); | |
473 | cinfo->mds->ncommit++; | |
474 | spin_unlock(cinfo->lock); | |
fd39fc85 | 475 | inc_zone_page_state(req->wb_page, NR_UNSTABLE_NFS); |
c9e51e41 | 476 | inc_bdi_stat(req->wb_page->mapping->backing_dev_info, BDI_RECLAIMABLE); |
ea2cf228 | 477 | __mark_inode_dirty(req->wb_context->dentry->d_inode, I_DIRTY_DATASYNC); |
1da177e4 | 478 | } |
8dd37758 TM |
479 | EXPORT_SYMBOL_GPL(nfs_request_add_commit_list); |
480 | ||
481 | /** | |
482 | * nfs_request_remove_commit_list - Remove request from a commit list | |
483 | * @req: pointer to a nfs_page | |
ea2cf228 | 484 | * @cinfo: holds list lock and accounting info |
8dd37758 | 485 | * |
ea2cf228 | 486 | * This clears the PG_CLEAN bit, and updates the cinfo's count of |
8dd37758 TM |
487 | * number of outstanding requests requiring a commit |
488 | * It does not update the MM page stats. | |
489 | * | |
ea2cf228 | 490 | * The caller _must_ hold the cinfo->lock and the nfs_page lock. |
8dd37758 TM |
491 | */ |
492 | void | |
ea2cf228 FI |
493 | nfs_request_remove_commit_list(struct nfs_page *req, |
494 | struct nfs_commit_info *cinfo) | |
8dd37758 | 495 | { |
8dd37758 TM |
496 | if (!test_and_clear_bit(PG_CLEAN, &(req)->wb_flags)) |
497 | return; | |
498 | nfs_list_remove_request(req); | |
ea2cf228 | 499 | cinfo->mds->ncommit--; |
8dd37758 TM |
500 | } |
501 | EXPORT_SYMBOL_GPL(nfs_request_remove_commit_list); | |
502 | ||
ea2cf228 FI |
503 | static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo, |
504 | struct inode *inode) | |
505 | { | |
506 | cinfo->lock = &inode->i_lock; | |
507 | cinfo->mds = &NFS_I(inode)->commit_info; | |
508 | cinfo->ds = pnfs_get_ds_info(inode); | |
b359f9d0 | 509 | cinfo->dreq = NULL; |
f453a54a | 510 | cinfo->completion_ops = &nfs_commit_completion_ops; |
ea2cf228 FI |
511 | } |
512 | ||
513 | void nfs_init_cinfo(struct nfs_commit_info *cinfo, | |
514 | struct inode *inode, | |
515 | struct nfs_direct_req *dreq) | |
516 | { | |
517 | nfs_init_cinfo_from_inode(cinfo, inode); | |
518 | } | |
519 | EXPORT_SYMBOL_GPL(nfs_init_cinfo); | |
8dd37758 TM |
520 | |
521 | /* | |
522 | * Add a request to the inode's commit list. | |
523 | */ | |
524 | static void | |
ea2cf228 FI |
525 | nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg, |
526 | struct nfs_commit_info *cinfo) | |
8dd37758 | 527 | { |
ea2cf228 | 528 | if (pnfs_mark_request_commit(req, lseg, cinfo)) |
8dd37758 | 529 | return; |
ea2cf228 | 530 | nfs_request_add_commit_list(req, &cinfo->mds->list, cinfo); |
8dd37758 | 531 | } |
8e821cad | 532 | |
d6d6dc7c FI |
533 | static void |
534 | nfs_clear_page_commit(struct page *page) | |
535 | { | |
536 | dec_zone_page_state(page, NR_UNSTABLE_NFS); | |
537 | dec_bdi_stat(page->mapping->backing_dev_info, BDI_RECLAIMABLE); | |
538 | } | |
539 | ||
8dd37758 | 540 | static void |
e468bae9 TM |
541 | nfs_clear_request_commit(struct nfs_page *req) |
542 | { | |
8dd37758 TM |
543 | if (test_bit(PG_CLEAN, &req->wb_flags)) { |
544 | struct inode *inode = req->wb_context->dentry->d_inode; | |
ea2cf228 | 545 | struct nfs_commit_info cinfo; |
e468bae9 | 546 | |
ea2cf228 FI |
547 | nfs_init_cinfo_from_inode(&cinfo, inode); |
548 | if (!pnfs_clear_request_commit(req, &cinfo)) { | |
549 | spin_lock(cinfo.lock); | |
550 | nfs_request_remove_commit_list(req, &cinfo); | |
551 | spin_unlock(cinfo.lock); | |
8dd37758 | 552 | } |
d6d6dc7c | 553 | nfs_clear_page_commit(req->wb_page); |
e468bae9 | 554 | } |
e468bae9 TM |
555 | } |
556 | ||
8e821cad TM |
557 | static inline |
558 | int nfs_write_need_commit(struct nfs_write_data *data) | |
559 | { | |
465d5243 | 560 | if (data->verf.committed == NFS_DATA_SYNC) |
cd841605 FI |
561 | return data->header->lseg == NULL; |
562 | return data->verf.committed != NFS_FILE_SYNC; | |
8e821cad TM |
563 | } |
564 | ||
8e821cad | 565 | #else |
8dd37758 | 566 | static void |
ea2cf228 FI |
567 | nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg, |
568 | struct nfs_commit_info *cinfo) | |
8e821cad TM |
569 | { |
570 | } | |
571 | ||
8dd37758 | 572 | static void |
e468bae9 TM |
573 | nfs_clear_request_commit(struct nfs_page *req) |
574 | { | |
e468bae9 TM |
575 | } |
576 | ||
8e821cad TM |
577 | static inline |
578 | int nfs_write_need_commit(struct nfs_write_data *data) | |
579 | { | |
580 | return 0; | |
581 | } | |
582 | ||
6c75dc0d FI |
583 | #endif |
584 | ||
061ae2ed | 585 | static void nfs_write_completion(struct nfs_pgio_header *hdr) |
8e821cad | 586 | { |
ea2cf228 | 587 | struct nfs_commit_info cinfo; |
6c75dc0d FI |
588 | unsigned long bytes = 0; |
589 | ||
590 | if (test_bit(NFS_IOHDR_REDO, &hdr->flags)) | |
591 | goto out; | |
ea2cf228 | 592 | nfs_init_cinfo_from_inode(&cinfo, hdr->inode); |
6c75dc0d FI |
593 | while (!list_empty(&hdr->pages)) { |
594 | struct nfs_page *req = nfs_list_entry(hdr->pages.next); | |
595 | struct page *page = req->wb_page; | |
596 | ||
597 | bytes += req->wb_bytes; | |
598 | nfs_list_remove_request(req); | |
599 | if (test_bit(NFS_IOHDR_ERROR, &hdr->flags) && | |
600 | (hdr->good_bytes < bytes)) { | |
601 | nfs_set_pageerror(page); | |
602 | nfs_context_set_write_error(req->wb_context, hdr->error); | |
603 | goto remove_req; | |
604 | } | |
605 | if (test_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags)) { | |
606 | nfs_mark_request_dirty(req); | |
607 | goto next; | |
608 | } | |
609 | if (test_bit(NFS_IOHDR_NEED_COMMIT, &hdr->flags)) { | |
ea2cf228 | 610 | nfs_mark_request_commit(req, hdr->lseg, &cinfo); |
6c75dc0d FI |
611 | goto next; |
612 | } | |
613 | remove_req: | |
614 | nfs_inode_remove_request(req); | |
615 | next: | |
616 | nfs_unlock_request(req); | |
617 | nfs_end_page_writeback(page); | |
618 | } | |
619 | out: | |
620 | hdr->release(hdr); | |
8e821cad | 621 | } |
1da177e4 | 622 | |
47c62564 | 623 | #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) |
ea2cf228 FI |
624 | static unsigned long |
625 | nfs_reqs_to_commit(struct nfs_commit_info *cinfo) | |
fb8a1f11 | 626 | { |
ea2cf228 | 627 | return cinfo->mds->ncommit; |
d6d6dc7c FI |
628 | } |
629 | ||
ea2cf228 | 630 | /* cinfo->lock held by caller */ |
8dd37758 | 631 | static int |
ea2cf228 FI |
632 | nfs_scan_commit_list(struct list_head *src, struct list_head *dst, |
633 | struct nfs_commit_info *cinfo, int max) | |
d6d6dc7c FI |
634 | { |
635 | struct nfs_page *req, *tmp; | |
636 | int ret = 0; | |
637 | ||
638 | list_for_each_entry_safe(req, tmp, src, wb_list) { | |
8dd37758 TM |
639 | if (!nfs_lock_request(req)) |
640 | continue; | |
ea2cf228 | 641 | if (cond_resched_lock(cinfo->lock)) |
3b3be88d | 642 | list_safe_reset_next(req, tmp, wb_list); |
ea2cf228 | 643 | nfs_request_remove_commit_list(req, cinfo); |
8dd37758 TM |
644 | nfs_list_add_request(req, dst); |
645 | ret++; | |
646 | if (ret == max) | |
647 | break; | |
d6d6dc7c FI |
648 | } |
649 | return ret; | |
fb8a1f11 TM |
650 | } |
651 | ||
1da177e4 LT |
652 | /* |
653 | * nfs_scan_commit - Scan an inode for commit requests | |
654 | * @inode: NFS inode to scan | |
ea2cf228 FI |
655 | * @dst: mds destination list |
656 | * @cinfo: mds and ds lists of reqs ready to commit | |
1da177e4 LT |
657 | * |
658 | * Moves requests from the inode's 'commit' request list. | |
659 | * The requests are *not* checked to ensure that they form a contiguous set. | |
660 | */ | |
661 | static int | |
ea2cf228 FI |
662 | nfs_scan_commit(struct inode *inode, struct list_head *dst, |
663 | struct nfs_commit_info *cinfo) | |
1da177e4 | 664 | { |
d6d6dc7c | 665 | int ret = 0; |
fb8a1f11 | 666 | |
ea2cf228 FI |
667 | spin_lock(cinfo->lock); |
668 | if (cinfo->mds->ncommit > 0) { | |
8dd37758 | 669 | const int max = INT_MAX; |
d6d6dc7c | 670 | |
ea2cf228 FI |
671 | ret = nfs_scan_commit_list(&cinfo->mds->list, dst, |
672 | cinfo, max); | |
673 | ret += pnfs_scan_commit_lists(inode, cinfo, max - ret); | |
d6d6dc7c | 674 | } |
ea2cf228 | 675 | spin_unlock(cinfo->lock); |
ff778d02 | 676 | return ret; |
1da177e4 | 677 | } |
d6d6dc7c | 678 | |
c42de9dd | 679 | #else |
ea2cf228 | 680 | static unsigned long nfs_reqs_to_commit(struct nfs_commit_info *cinfo) |
fb8a1f11 TM |
681 | { |
682 | return 0; | |
683 | } | |
684 | ||
ea2cf228 FI |
685 | static inline int nfs_scan_commit(struct inode *inode, struct list_head *dst, |
686 | struct nfs_commit_info *cinfo) | |
c42de9dd TM |
687 | { |
688 | return 0; | |
689 | } | |
1da177e4 LT |
690 | #endif |
691 | ||
1da177e4 | 692 | /* |
e7d39069 TM |
693 | * Search for an existing write request, and attempt to update |
694 | * it to reflect a new dirty region on a given page. | |
1da177e4 | 695 | * |
e7d39069 TM |
696 | * If the attempt fails, then the existing request is flushed out |
697 | * to disk. | |
1da177e4 | 698 | */ |
e7d39069 TM |
699 | static struct nfs_page *nfs_try_to_update_request(struct inode *inode, |
700 | struct page *page, | |
701 | unsigned int offset, | |
702 | unsigned int bytes) | |
1da177e4 | 703 | { |
e7d39069 TM |
704 | struct nfs_page *req; |
705 | unsigned int rqend; | |
706 | unsigned int end; | |
707 | int error; | |
708 | ||
709 | if (!PagePrivate(page)) | |
710 | return NULL; | |
1da177e4 LT |
711 | |
712 | end = offset + bytes; | |
e7d39069 | 713 | spin_lock(&inode->i_lock); |
1da177e4 | 714 | |
1da177e4 | 715 | for (;;) { |
277459d2 | 716 | req = nfs_page_find_request_locked(page); |
e7d39069 TM |
717 | if (req == NULL) |
718 | goto out_unlock; | |
719 | ||
720 | rqend = req->wb_offset + req->wb_bytes; | |
721 | /* | |
722 | * Tell the caller to flush out the request if | |
723 | * the offsets are non-contiguous. | |
724 | * Note: nfs_flush_incompatible() will already | |
725 | * have flushed out requests having wrong owners. | |
726 | */ | |
e468bae9 | 727 | if (offset > rqend |
e7d39069 TM |
728 | || end < req->wb_offset) |
729 | goto out_flushme; | |
730 | ||
9994b62b | 731 | if (nfs_lock_request_dontget(req)) |
1da177e4 | 732 | break; |
1da177e4 | 733 | |
e7d39069 | 734 | /* The request is locked, so wait and then retry */ |
587142f8 | 735 | spin_unlock(&inode->i_lock); |
e7d39069 TM |
736 | error = nfs_wait_on_request(req); |
737 | nfs_release_request(req); | |
738 | if (error != 0) | |
739 | goto out_err; | |
740 | spin_lock(&inode->i_lock); | |
1da177e4 LT |
741 | } |
742 | ||
743 | /* Okay, the request matches. Update the region */ | |
744 | if (offset < req->wb_offset) { | |
745 | req->wb_offset = offset; | |
746 | req->wb_pgbase = offset; | |
1da177e4 | 747 | } |
1da177e4 LT |
748 | if (end > rqend) |
749 | req->wb_bytes = end - req->wb_offset; | |
e7d39069 TM |
750 | else |
751 | req->wb_bytes = rqend - req->wb_offset; | |
752 | out_unlock: | |
753 | spin_unlock(&inode->i_lock); | |
ca138f36 FI |
754 | if (req) |
755 | nfs_clear_request_commit(req); | |
e7d39069 TM |
756 | return req; |
757 | out_flushme: | |
758 | spin_unlock(&inode->i_lock); | |
759 | nfs_release_request(req); | |
760 | error = nfs_wb_page(inode, page); | |
761 | out_err: | |
762 | return ERR_PTR(error); | |
763 | } | |
764 | ||
765 | /* | |
766 | * Try to update an existing write request, or create one if there is none. | |
767 | * | |
768 | * Note: Should always be called with the Page Lock held to prevent races | |
769 | * if we have to add a new request. Also assumes that the caller has | |
770 | * already called nfs_flush_incompatible() if necessary. | |
771 | */ | |
772 | static struct nfs_page * nfs_setup_write_request(struct nfs_open_context* ctx, | |
773 | struct page *page, unsigned int offset, unsigned int bytes) | |
774 | { | |
775 | struct inode *inode = page->mapping->host; | |
776 | struct nfs_page *req; | |
1da177e4 | 777 | |
e7d39069 TM |
778 | req = nfs_try_to_update_request(inode, page, offset, bytes); |
779 | if (req != NULL) | |
780 | goto out; | |
781 | req = nfs_create_request(ctx, inode, page, offset, bytes); | |
782 | if (IS_ERR(req)) | |
783 | goto out; | |
d6d6dc7c | 784 | nfs_inode_add_request(inode, req); |
efc91ed0 | 785 | out: |
61e930a9 | 786 | return req; |
1da177e4 LT |
787 | } |
788 | ||
e7d39069 TM |
789 | static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page, |
790 | unsigned int offset, unsigned int count) | |
791 | { | |
792 | struct nfs_page *req; | |
793 | ||
794 | req = nfs_setup_write_request(ctx, page, offset, count); | |
795 | if (IS_ERR(req)) | |
796 | return PTR_ERR(req); | |
797 | /* Update file length */ | |
798 | nfs_grow_file(page, offset, count); | |
799 | nfs_mark_uptodate(page, req->wb_pgbase, req->wb_bytes); | |
a6305ddb | 800 | nfs_mark_request_dirty(req); |
9994b62b | 801 | nfs_unlock_request(req); |
e7d39069 TM |
802 | return 0; |
803 | } | |
804 | ||
1da177e4 LT |
805 | int nfs_flush_incompatible(struct file *file, struct page *page) |
806 | { | |
cd3758e3 | 807 | struct nfs_open_context *ctx = nfs_file_open_context(file); |
1da177e4 | 808 | struct nfs_page *req; |
1a54533e | 809 | int do_flush, status; |
1da177e4 LT |
810 | /* |
811 | * Look for a request corresponding to this page. If there | |
812 | * is one, and it belongs to another file, we flush it out | |
813 | * before we try to copy anything into the page. Do this | |
814 | * due to the lack of an ACCESS-type call in NFSv2. | |
815 | * Also do the same if we find a request from an existing | |
816 | * dropped page. | |
817 | */ | |
1a54533e TM |
818 | do { |
819 | req = nfs_page_find_request(page); | |
820 | if (req == NULL) | |
821 | return 0; | |
f11ac8db TM |
822 | do_flush = req->wb_page != page || req->wb_context != ctx || |
823 | req->wb_lock_context->lockowner != current->files || | |
824 | req->wb_lock_context->pid != current->tgid; | |
1da177e4 | 825 | nfs_release_request(req); |
1a54533e TM |
826 | if (!do_flush) |
827 | return 0; | |
828 | status = nfs_wb_page(page->mapping->host, page); | |
829 | } while (status == 0); | |
830 | return status; | |
1da177e4 LT |
831 | } |
832 | ||
5d47a356 TM |
833 | /* |
834 | * If the page cache is marked as unsafe or invalid, then we can't rely on | |
835 | * the PageUptodate() flag. In this case, we will need to turn off | |
836 | * write optimisations that depend on the page contents being correct. | |
837 | */ | |
838 | static int nfs_write_pageuptodate(struct page *page, struct inode *inode) | |
839 | { | |
840 | return PageUptodate(page) && | |
841 | !(NFS_I(inode)->cache_validity & (NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_DATA)); | |
842 | } | |
843 | ||
1da177e4 LT |
844 | /* |
845 | * Update and possibly write a cached page of an NFS file. | |
846 | * | |
847 | * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad | |
848 | * things with a page scheduled for an RPC call (e.g. invalidate it). | |
849 | */ | |
850 | int nfs_updatepage(struct file *file, struct page *page, | |
851 | unsigned int offset, unsigned int count) | |
852 | { | |
cd3758e3 | 853 | struct nfs_open_context *ctx = nfs_file_open_context(file); |
1da177e4 | 854 | struct inode *inode = page->mapping->host; |
1da177e4 LT |
855 | int status = 0; |
856 | ||
91d5b470 CL |
857 | nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE); |
858 | ||
48186c7d | 859 | dprintk("NFS: nfs_updatepage(%s/%s %d@%lld)\n", |
01cce933 JJS |
860 | file->f_path.dentry->d_parent->d_name.name, |
861 | file->f_path.dentry->d_name.name, count, | |
48186c7d | 862 | (long long)(page_offset(page) + offset)); |
1da177e4 | 863 | |
1da177e4 | 864 | /* If we're not using byte range locks, and we know the page |
5d47a356 TM |
865 | * is up to date, it may be more efficient to extend the write |
866 | * to cover the entire page in order to avoid fragmentation | |
867 | * inefficiencies. | |
1da177e4 | 868 | */ |
5d47a356 TM |
869 | if (nfs_write_pageuptodate(page, inode) && |
870 | inode->i_flock == NULL && | |
6b2f3d1f | 871 | !(file->f_flags & O_DSYNC)) { |
49a70f27 | 872 | count = max(count + offset, nfs_page_length(page)); |
1da177e4 | 873 | offset = 0; |
1da177e4 LT |
874 | } |
875 | ||
e21195a7 | 876 | status = nfs_writepage_setup(ctx, page, offset, count); |
03fa9e84 TM |
877 | if (status < 0) |
878 | nfs_set_pageerror(page); | |
59b7c05f TM |
879 | else |
880 | __set_page_dirty_nobuffers(page); | |
1da177e4 | 881 | |
48186c7d | 882 | dprintk("NFS: nfs_updatepage returns %d (isize %lld)\n", |
1da177e4 | 883 | status, (long long)i_size_read(inode)); |
1da177e4 LT |
884 | return status; |
885 | } | |
886 | ||
3ff7576d | 887 | static int flush_task_priority(int how) |
1da177e4 LT |
888 | { |
889 | switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) { | |
890 | case FLUSH_HIGHPRI: | |
891 | return RPC_PRIORITY_HIGH; | |
892 | case FLUSH_LOWPRI: | |
893 | return RPC_PRIORITY_LOW; | |
894 | } | |
895 | return RPC_PRIORITY_NORMAL; | |
896 | } | |
897 | ||
c5996c4e FI |
898 | int nfs_initiate_write(struct rpc_clnt *clnt, |
899 | struct nfs_write_data *data, | |
d138d5d1 AA |
900 | const struct rpc_call_ops *call_ops, |
901 | int how) | |
1da177e4 | 902 | { |
cd841605 | 903 | struct inode *inode = data->header->inode; |
3ff7576d | 904 | int priority = flush_task_priority(how); |
07737691 | 905 | struct rpc_task *task; |
bdc7f021 TM |
906 | struct rpc_message msg = { |
907 | .rpc_argp = &data->args, | |
908 | .rpc_resp = &data->res, | |
cd841605 | 909 | .rpc_cred = data->header->cred, |
bdc7f021 | 910 | }; |
84115e1c | 911 | struct rpc_task_setup task_setup_data = { |
d138d5d1 | 912 | .rpc_client = clnt, |
07737691 | 913 | .task = &data->task, |
bdc7f021 | 914 | .rpc_message = &msg, |
84115e1c TM |
915 | .callback_ops = call_ops, |
916 | .callback_data = data, | |
101070ca | 917 | .workqueue = nfsiod_workqueue, |
2c61be0a | 918 | .flags = RPC_TASK_ASYNC, |
3ff7576d | 919 | .priority = priority, |
84115e1c | 920 | }; |
2c61be0a | 921 | int ret = 0; |
1da177e4 | 922 | |
d138d5d1 AA |
923 | /* Set up the initial task struct. */ |
924 | NFS_PROTO(inode)->write_setup(data, &msg); | |
925 | ||
926 | dprintk("NFS: %5u initiated write call " | |
927 | "(req %s/%lld, %u bytes @ offset %llu)\n", | |
928 | data->task.tk_pid, | |
929 | inode->i_sb->s_id, | |
930 | (long long)NFS_FILEID(inode), | |
931 | data->args.count, | |
932 | (unsigned long long)data->args.offset); | |
933 | ||
934 | task = rpc_run_task(&task_setup_data); | |
935 | if (IS_ERR(task)) { | |
936 | ret = PTR_ERR(task); | |
937 | goto out; | |
938 | } | |
939 | if (how & FLUSH_SYNC) { | |
940 | ret = rpc_wait_for_completion_task(task); | |
941 | if (ret == 0) | |
942 | ret = task->tk_status; | |
943 | } | |
944 | rpc_put_task(task); | |
945 | out: | |
946 | return ret; | |
947 | } | |
a69aef14 | 948 | EXPORT_SYMBOL_GPL(nfs_initiate_write); |
d138d5d1 AA |
949 | |
950 | /* | |
951 | * Set up the argument/result storage required for the RPC call. | |
952 | */ | |
6c75dc0d | 953 | static void nfs_write_rpcsetup(struct nfs_write_data *data, |
d138d5d1 | 954 | unsigned int count, unsigned int offset, |
ea2cf228 | 955 | int how, struct nfs_commit_info *cinfo) |
d138d5d1 | 956 | { |
6c75dc0d | 957 | struct nfs_page *req = data->header->req; |
d138d5d1 | 958 | |
1da177e4 LT |
959 | /* Set up the RPC argument and reply structs |
960 | * NB: take care not to mess about with data->commit et al. */ | |
961 | ||
6c75dc0d | 962 | data->args.fh = NFS_FH(data->header->inode); |
1da177e4 | 963 | data->args.offset = req_offset(req) + offset; |
2bea038c BH |
964 | /* pnfs_set_layoutcommit needs this */ |
965 | data->mds_offset = data->args.offset; | |
1da177e4 | 966 | data->args.pgbase = req->wb_pgbase + offset; |
30dd374f | 967 | data->args.pages = data->pages.pagevec; |
1da177e4 | 968 | data->args.count = count; |
383ba719 | 969 | data->args.context = get_nfs_open_context(req->wb_context); |
f11ac8db | 970 | data->args.lock_context = req->wb_lock_context; |
bdc7f021 | 971 | data->args.stable = NFS_UNSTABLE; |
87ed5eb4 TM |
972 | switch (how & (FLUSH_STABLE | FLUSH_COND_STABLE)) { |
973 | case 0: | |
974 | break; | |
975 | case FLUSH_COND_STABLE: | |
ea2cf228 | 976 | if (nfs_reqs_to_commit(cinfo)) |
87ed5eb4 TM |
977 | break; |
978 | default: | |
979 | data->args.stable = NFS_FILE_SYNC; | |
bdc7f021 | 980 | } |
1da177e4 LT |
981 | |
982 | data->res.fattr = &data->fattr; | |
983 | data->res.count = count; | |
984 | data->res.verf = &data->verf; | |
0e574af1 | 985 | nfs_fattr_init(&data->fattr); |
6e4efd56 | 986 | } |
1da177e4 | 987 | |
6e4efd56 TM |
988 | static int nfs_do_write(struct nfs_write_data *data, |
989 | const struct rpc_call_ops *call_ops, | |
6e4efd56 TM |
990 | int how) |
991 | { | |
cd841605 | 992 | struct inode *inode = data->header->inode; |
0382b744 | 993 | |
c5996c4e | 994 | return nfs_initiate_write(NFS_CLIENT(inode), data, call_ops, how); |
1da177e4 LT |
995 | } |
996 | ||
275acaaf TM |
997 | static int nfs_do_multiple_writes(struct list_head *head, |
998 | const struct rpc_call_ops *call_ops, | |
275acaaf TM |
999 | int how) |
1000 | { | |
1001 | struct nfs_write_data *data; | |
1002 | int ret = 0; | |
1003 | ||
1004 | while (!list_empty(head)) { | |
1005 | int ret2; | |
1006 | ||
6c75dc0d | 1007 | data = list_first_entry(head, struct nfs_write_data, list); |
275acaaf TM |
1008 | list_del_init(&data->list); |
1009 | ||
dce81290 | 1010 | ret2 = nfs_do_write(data, call_ops, how); |
275acaaf TM |
1011 | if (ret == 0) |
1012 | ret = ret2; | |
1013 | } | |
1014 | return ret; | |
1015 | } | |
1016 | ||
6d884e8f F |
1017 | /* If a nfs_flush_* function fails, it should remove reqs from @head and |
1018 | * call this on each, which will prepare them to be retried on next | |
1019 | * writeback using standard nfs. | |
1020 | */ | |
1021 | static void nfs_redirty_request(struct nfs_page *req) | |
1022 | { | |
a6305ddb TM |
1023 | struct page *page = req->wb_page; |
1024 | ||
6d884e8f | 1025 | nfs_mark_request_dirty(req); |
9994b62b | 1026 | nfs_unlock_request(req); |
a6305ddb | 1027 | nfs_end_page_writeback(page); |
6d884e8f F |
1028 | } |
1029 | ||
061ae2ed | 1030 | static void nfs_async_write_error(struct list_head *head) |
6c75dc0d FI |
1031 | { |
1032 | struct nfs_page *req; | |
1033 | ||
1034 | while (!list_empty(head)) { | |
1035 | req = nfs_list_entry(head->next); | |
1036 | nfs_list_remove_request(req); | |
1037 | nfs_redirty_request(req); | |
1038 | } | |
1039 | } | |
1040 | ||
061ae2ed FI |
1041 | static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops = { |
1042 | .error_cleanup = nfs_async_write_error, | |
1043 | .completion = nfs_write_completion, | |
1044 | }; | |
1045 | ||
1da177e4 LT |
1046 | /* |
1047 | * Generate multiple small requests to write out a single | |
1048 | * contiguous dirty area on one page. | |
1049 | */ | |
6c75dc0d FI |
1050 | static int nfs_flush_multi(struct nfs_pageio_descriptor *desc, |
1051 | struct nfs_pgio_header *hdr) | |
1da177e4 | 1052 | { |
6c75dc0d | 1053 | struct nfs_page *req = hdr->req; |
1da177e4 LT |
1054 | struct page *page = req->wb_page; |
1055 | struct nfs_write_data *data; | |
d097971d | 1056 | size_t wsize = desc->pg_bsize, nbytes; |
e9f7bee1 | 1057 | unsigned int offset; |
1da177e4 | 1058 | int requests = 0; |
dbae4c73 | 1059 | int ret = 0; |
ea2cf228 | 1060 | struct nfs_commit_info cinfo; |
1da177e4 | 1061 | |
ea2cf228 | 1062 | nfs_init_cinfo(&cinfo, desc->pg_inode, desc->pg_dreq); |
1da177e4 | 1063 | nfs_list_remove_request(req); |
6c75dc0d | 1064 | nfs_list_add_request(req, &hdr->pages); |
1da177e4 | 1065 | |
b31268ac | 1066 | if ((desc->pg_ioflags & FLUSH_COND_STABLE) && |
ea2cf228 | 1067 | (desc->pg_moreio || nfs_reqs_to_commit(&cinfo) || |
b31268ac TM |
1068 | desc->pg_count > wsize)) |
1069 | desc->pg_ioflags &= ~FLUSH_COND_STABLE; | |
1070 | ||
1071 | ||
275acaaf | 1072 | offset = 0; |
c76069bd | 1073 | nbytes = desc->pg_count; |
e9f7bee1 TM |
1074 | do { |
1075 | size_t len = min(nbytes, wsize); | |
1076 | ||
6c75dc0d FI |
1077 | data = nfs_writedata_alloc(hdr, 1); |
1078 | if (!data) | |
1da177e4 | 1079 | goto out_bad; |
30dd374f | 1080 | data->pages.pagevec[0] = page; |
ea2cf228 | 1081 | nfs_write_rpcsetup(data, len, offset, desc->pg_ioflags, &cinfo); |
6c75dc0d | 1082 | list_add(&data->list, &hdr->rpc_list); |
1da177e4 | 1083 | requests++; |
e9f7bee1 | 1084 | nbytes -= len; |
275acaaf | 1085 | offset += len; |
e9f7bee1 | 1086 | } while (nbytes != 0); |
6c75dc0d | 1087 | desc->pg_rpc_callops = &nfs_write_common_ops; |
dbae4c73 | 1088 | return ret; |
1da177e4 LT |
1089 | |
1090 | out_bad: | |
6c75dc0d FI |
1091 | while (!list_empty(&hdr->rpc_list)) { |
1092 | data = list_first_entry(&hdr->rpc_list, struct nfs_write_data, list); | |
6e4efd56 | 1093 | list_del(&data->list); |
8ccd271f | 1094 | nfs_writedata_release(data); |
1da177e4 | 1095 | } |
061ae2ed | 1096 | desc->pg_completion_ops->error_cleanup(&hdr->pages); |
1da177e4 LT |
1097 | return -ENOMEM; |
1098 | } | |
1099 | ||
1100 | /* | |
1101 | * Create an RPC task for the given write request and kick it. | |
1102 | * The page must have been locked by the caller. | |
1103 | * | |
1104 | * It may happen that the page we're passed is not marked dirty. | |
1105 | * This is the case if nfs_updatepage detects a conflicting request | |
1106 | * that has been written but not committed. | |
1107 | */ | |
6c75dc0d FI |
1108 | static int nfs_flush_one(struct nfs_pageio_descriptor *desc, |
1109 | struct nfs_pgio_header *hdr) | |
1da177e4 LT |
1110 | { |
1111 | struct nfs_page *req; | |
1112 | struct page **pages; | |
1113 | struct nfs_write_data *data; | |
c76069bd | 1114 | struct list_head *head = &desc->pg_list; |
3b609184 | 1115 | int ret = 0; |
ea2cf228 | 1116 | struct nfs_commit_info cinfo; |
1da177e4 | 1117 | |
6c75dc0d FI |
1118 | data = nfs_writedata_alloc(hdr, nfs_page_array_len(desc->pg_base, |
1119 | desc->pg_count)); | |
1120 | if (!data) { | |
061ae2ed | 1121 | desc->pg_completion_ops->error_cleanup(head); |
44b83799 FI |
1122 | ret = -ENOMEM; |
1123 | goto out; | |
1124 | } | |
6c75dc0d | 1125 | |
ea2cf228 | 1126 | nfs_init_cinfo(&cinfo, desc->pg_inode, desc->pg_dreq); |
30dd374f | 1127 | pages = data->pages.pagevec; |
1da177e4 LT |
1128 | while (!list_empty(head)) { |
1129 | req = nfs_list_entry(head->next); | |
1130 | nfs_list_remove_request(req); | |
6c75dc0d | 1131 | nfs_list_add_request(req, &hdr->pages); |
1da177e4 | 1132 | *pages++ = req->wb_page; |
1da177e4 | 1133 | } |
1da177e4 | 1134 | |
b31268ac | 1135 | if ((desc->pg_ioflags & FLUSH_COND_STABLE) && |
ea2cf228 | 1136 | (desc->pg_moreio || nfs_reqs_to_commit(&cinfo))) |
b31268ac TM |
1137 | desc->pg_ioflags &= ~FLUSH_COND_STABLE; |
1138 | ||
1da177e4 | 1139 | /* Set up the argument struct */ |
ea2cf228 | 1140 | nfs_write_rpcsetup(data, desc->pg_count, 0, desc->pg_ioflags, &cinfo); |
6c75dc0d FI |
1141 | list_add(&data->list, &hdr->rpc_list); |
1142 | desc->pg_rpc_callops = &nfs_write_common_ops; | |
44b83799 | 1143 | out: |
44b83799 | 1144 | return ret; |
1da177e4 LT |
1145 | } |
1146 | ||
6c75dc0d FI |
1147 | int nfs_generic_flush(struct nfs_pageio_descriptor *desc, |
1148 | struct nfs_pgio_header *hdr) | |
dce81290 TM |
1149 | { |
1150 | if (desc->pg_bsize < PAGE_CACHE_SIZE) | |
6c75dc0d FI |
1151 | return nfs_flush_multi(desc, hdr); |
1152 | return nfs_flush_one(desc, hdr); | |
dce81290 TM |
1153 | } |
1154 | ||
1155 | static int nfs_generic_pg_writepages(struct nfs_pageio_descriptor *desc) | |
1751c363 | 1156 | { |
6c75dc0d FI |
1157 | struct nfs_write_header *whdr; |
1158 | struct nfs_pgio_header *hdr; | |
275acaaf TM |
1159 | int ret; |
1160 | ||
6c75dc0d FI |
1161 | whdr = nfs_writehdr_alloc(); |
1162 | if (!whdr) { | |
061ae2ed | 1163 | desc->pg_completion_ops->error_cleanup(&hdr->pages); |
6c75dc0d FI |
1164 | return -ENOMEM; |
1165 | } | |
1166 | hdr = &whdr->header; | |
1167 | nfs_pgheader_init(desc, hdr, nfs_writehdr_free); | |
1168 | atomic_inc(&hdr->refcnt); | |
1169 | ret = nfs_generic_flush(desc, hdr); | |
50828d7e | 1170 | if (ret == 0) |
6c75dc0d FI |
1171 | ret = nfs_do_multiple_writes(&hdr->rpc_list, |
1172 | desc->pg_rpc_callops, | |
1173 | desc->pg_ioflags); | |
1174 | else | |
1175 | set_bit(NFS_IOHDR_REDO, &hdr->flags); | |
1176 | if (atomic_dec_and_test(&hdr->refcnt)) | |
061ae2ed | 1177 | hdr->completion_ops->completion(hdr); |
275acaaf | 1178 | return ret; |
1751c363 | 1179 | } |
1751c363 TM |
1180 | |
1181 | static const struct nfs_pageio_ops nfs_pageio_write_ops = { | |
1182 | .pg_test = nfs_generic_pg_test, | |
1183 | .pg_doio = nfs_generic_pg_writepages, | |
1184 | }; | |
1185 | ||
e2fecb21 | 1186 | void nfs_pageio_init_write_mds(struct nfs_pageio_descriptor *pgio, |
061ae2ed FI |
1187 | struct inode *inode, int ioflags, |
1188 | const struct nfs_pgio_completion_ops *compl_ops) | |
1da177e4 | 1189 | { |
061ae2ed | 1190 | nfs_pageio_init(pgio, inode, &nfs_pageio_write_ops, compl_ops, |
1751c363 TM |
1191 | NFS_SERVER(inode)->wsize, ioflags); |
1192 | } | |
1da177e4 | 1193 | |
dce81290 TM |
1194 | void nfs_pageio_reset_write_mds(struct nfs_pageio_descriptor *pgio) |
1195 | { | |
1196 | pgio->pg_ops = &nfs_pageio_write_ops; | |
1197 | pgio->pg_bsize = NFS_SERVER(pgio->pg_inode)->wsize; | |
1198 | } | |
1f945357 | 1199 | EXPORT_SYMBOL_GPL(nfs_pageio_reset_write_mds); |
dce81290 | 1200 | |
1751c363 | 1201 | static void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio, |
061ae2ed FI |
1202 | struct inode *inode, int ioflags, |
1203 | const struct nfs_pgio_completion_ops *compl_ops) | |
1751c363 | 1204 | { |
061ae2ed FI |
1205 | if (!pnfs_pageio_init_write(pgio, inode, ioflags, compl_ops)) |
1206 | nfs_pageio_init_write_mds(pgio, inode, ioflags, compl_ops); | |
1da177e4 LT |
1207 | } |
1208 | ||
def6ed7e AA |
1209 | void nfs_write_prepare(struct rpc_task *task, void *calldata) |
1210 | { | |
1211 | struct nfs_write_data *data = calldata; | |
cd841605 | 1212 | NFS_PROTO(data->header->inode)->write_rpc_prepare(task, data); |
def6ed7e | 1213 | } |
def6ed7e | 1214 | |
0b7c0153 FI |
1215 | void nfs_commit_prepare(struct rpc_task *task, void *calldata) |
1216 | { | |
1217 | struct nfs_commit_data *data = calldata; | |
1218 | ||
1219 | NFS_PROTO(data->inode)->commit_rpc_prepare(task, data); | |
1220 | } | |
1221 | ||
1da177e4 LT |
1222 | /* |
1223 | * Handle a write reply that flushes a whole page. | |
1224 | * | |
1225 | * FIXME: There is an inherent race with invalidate_inode_pages and | |
1226 | * writebacks since the page->count is kept > 1 for as long | |
1227 | * as the page has a write request pending. | |
1228 | */ | |
6c75dc0d | 1229 | static void nfs_writeback_done_common(struct rpc_task *task, void *calldata) |
1da177e4 | 1230 | { |
788e7a89 | 1231 | struct nfs_write_data *data = calldata; |
1da177e4 | 1232 | |
c9d8f89d TM |
1233 | nfs_writeback_done(task, data); |
1234 | } | |
1235 | ||
6c75dc0d | 1236 | static void nfs_writeback_release_common(void *calldata) |
c9d8f89d TM |
1237 | { |
1238 | struct nfs_write_data *data = calldata; | |
cd841605 | 1239 | struct nfs_pgio_header *hdr = data->header; |
e2fecb21 | 1240 | int status = data->task.tk_status; |
6c75dc0d | 1241 | struct nfs_page *req = hdr->req; |
788e7a89 | 1242 | |
6c75dc0d FI |
1243 | if ((status >= 0) && nfs_write_need_commit(data)) { |
1244 | spin_lock(&hdr->lock); | |
1245 | if (test_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags)) | |
1246 | ; /* Do nothing */ | |
1247 | else if (!test_and_set_bit(NFS_IOHDR_NEED_COMMIT, &hdr->flags)) | |
8e821cad | 1248 | memcpy(&req->wb_verf, &data->verf, sizeof(req->wb_verf)); |
6c75dc0d FI |
1249 | else if (memcmp(&req->wb_verf, &data->verf, sizeof(req->wb_verf))) |
1250 | set_bit(NFS_IOHDR_NEED_RESCHED, &hdr->flags); | |
1251 | spin_unlock(&hdr->lock); | |
1da177e4 | 1252 | } |
cd841605 | 1253 | nfs_writedata_release(data); |
1da177e4 LT |
1254 | } |
1255 | ||
6c75dc0d | 1256 | static const struct rpc_call_ops nfs_write_common_ops = { |
def6ed7e | 1257 | .rpc_call_prepare = nfs_write_prepare, |
6c75dc0d FI |
1258 | .rpc_call_done = nfs_writeback_done_common, |
1259 | .rpc_release = nfs_writeback_release_common, | |
788e7a89 TM |
1260 | }; |
1261 | ||
1262 | ||
1da177e4 LT |
1263 | /* |
1264 | * This function is called when the WRITE call is complete. | |
1265 | */ | |
13602896 | 1266 | void nfs_writeback_done(struct rpc_task *task, struct nfs_write_data *data) |
1da177e4 | 1267 | { |
1da177e4 LT |
1268 | struct nfs_writeargs *argp = &data->args; |
1269 | struct nfs_writeres *resp = &data->res; | |
cd841605 | 1270 | struct inode *inode = data->header->inode; |
788e7a89 | 1271 | int status; |
1da177e4 | 1272 | |
a3f565b1 | 1273 | dprintk("NFS: %5u nfs_writeback_done (status %d)\n", |
1da177e4 LT |
1274 | task->tk_pid, task->tk_status); |
1275 | ||
f551e44f CL |
1276 | /* |
1277 | * ->write_done will attempt to use post-op attributes to detect | |
1278 | * conflicting writes by other clients. A strict interpretation | |
1279 | * of close-to-open would allow us to continue caching even if | |
1280 | * another writer had changed the file, but some applications | |
1281 | * depend on tighter cache coherency when writing. | |
1282 | */ | |
cd841605 | 1283 | status = NFS_PROTO(inode)->write_done(task, data); |
788e7a89 | 1284 | if (status != 0) |
13602896 | 1285 | return; |
cd841605 | 1286 | nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, resp->count); |
91d5b470 | 1287 | |
1da177e4 LT |
1288 | #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) |
1289 | if (resp->verf->committed < argp->stable && task->tk_status >= 0) { | |
1290 | /* We tried a write call, but the server did not | |
1291 | * commit data to stable storage even though we | |
1292 | * requested it. | |
1293 | * Note: There is a known bug in Tru64 < 5.0 in which | |
1294 | * the server reports NFS_DATA_SYNC, but performs | |
1295 | * NFS_FILE_SYNC. We therefore implement this checking | |
1296 | * as a dprintk() in order to avoid filling syslog. | |
1297 | */ | |
1298 | static unsigned long complain; | |
1299 | ||
a69aef14 | 1300 | /* Note this will print the MDS for a DS write */ |
1da177e4 | 1301 | if (time_before(complain, jiffies)) { |
48186c7d | 1302 | dprintk("NFS: faulty NFS server %s:" |
1da177e4 | 1303 | " (committed = %d) != (stable = %d)\n", |
cd841605 | 1304 | NFS_SERVER(inode)->nfs_client->cl_hostname, |
1da177e4 LT |
1305 | resp->verf->committed, argp->stable); |
1306 | complain = jiffies + 300 * HZ; | |
1307 | } | |
1308 | } | |
1309 | #endif | |
6c75dc0d FI |
1310 | if (task->tk_status < 0) |
1311 | nfs_set_pgio_error(data->header, task->tk_status, argp->offset); | |
1312 | else if (resp->count < argp->count) { | |
1da177e4 LT |
1313 | static unsigned long complain; |
1314 | ||
6c75dc0d | 1315 | /* This a short write! */ |
cd841605 | 1316 | nfs_inc_stats(inode, NFSIOS_SHORTWRITE); |
91d5b470 | 1317 | |
1da177e4 | 1318 | /* Has the server at least made some progress? */ |
6c75dc0d FI |
1319 | if (resp->count == 0) { |
1320 | if (time_before(complain, jiffies)) { | |
1321 | printk(KERN_WARNING | |
1322 | "NFS: Server wrote zero bytes, expected %u.\n", | |
1323 | argp->count); | |
1324 | complain = jiffies + 300 * HZ; | |
1da177e4 | 1325 | } |
6c75dc0d FI |
1326 | nfs_set_pgio_error(data->header, -EIO, argp->offset); |
1327 | task->tk_status = -EIO; | |
13602896 | 1328 | return; |
1da177e4 | 1329 | } |
6c75dc0d FI |
1330 | /* Was this an NFSv2 write or an NFSv3 stable write? */ |
1331 | if (resp->verf->committed != NFS_UNSTABLE) { | |
1332 | /* Resend from where the server left off */ | |
1333 | data->mds_offset += resp->count; | |
1334 | argp->offset += resp->count; | |
1335 | argp->pgbase += resp->count; | |
1336 | argp->count -= resp->count; | |
1337 | } else { | |
1338 | /* Resend as a stable write in order to avoid | |
1339 | * headaches in the case of a server crash. | |
1340 | */ | |
1341 | argp->stable = NFS_FILE_SYNC; | |
1da177e4 | 1342 | } |
6c75dc0d | 1343 | rpc_restart_call_prepare(task); |
1da177e4 | 1344 | } |
1da177e4 LT |
1345 | } |
1346 | ||
1347 | ||
1348 | #if defined(CONFIG_NFS_V3) || defined(CONFIG_NFS_V4) | |
71d0a611 TM |
1349 | static int nfs_commit_set_lock(struct nfs_inode *nfsi, int may_wait) |
1350 | { | |
b8413f98 TM |
1351 | int ret; |
1352 | ||
71d0a611 TM |
1353 | if (!test_and_set_bit(NFS_INO_COMMIT, &nfsi->flags)) |
1354 | return 1; | |
b8413f98 TM |
1355 | if (!may_wait) |
1356 | return 0; | |
1357 | ret = out_of_line_wait_on_bit_lock(&nfsi->flags, | |
1358 | NFS_INO_COMMIT, | |
1359 | nfs_wait_bit_killable, | |
1360 | TASK_KILLABLE); | |
1361 | return (ret < 0) ? ret : 1; | |
71d0a611 TM |
1362 | } |
1363 | ||
f453a54a | 1364 | static void nfs_commit_clear_lock(struct nfs_inode *nfsi) |
71d0a611 TM |
1365 | { |
1366 | clear_bit(NFS_INO_COMMIT, &nfsi->flags); | |
1367 | smp_mb__after_clear_bit(); | |
1368 | wake_up_bit(&nfsi->flags, NFS_INO_COMMIT); | |
1369 | } | |
1370 | ||
0b7c0153 | 1371 | void nfs_commitdata_release(struct nfs_commit_data *data) |
1da177e4 | 1372 | { |
0b7c0153 FI |
1373 | put_nfs_open_context(data->context); |
1374 | nfs_commit_free(data); | |
1da177e4 | 1375 | } |
e0c2b380 | 1376 | EXPORT_SYMBOL_GPL(nfs_commitdata_release); |
1da177e4 | 1377 | |
0b7c0153 | 1378 | int nfs_initiate_commit(struct rpc_clnt *clnt, struct nfs_commit_data *data, |
9ace33cd FI |
1379 | const struct rpc_call_ops *call_ops, |
1380 | int how) | |
1da177e4 | 1381 | { |
07737691 | 1382 | struct rpc_task *task; |
9ace33cd | 1383 | int priority = flush_task_priority(how); |
bdc7f021 TM |
1384 | struct rpc_message msg = { |
1385 | .rpc_argp = &data->args, | |
1386 | .rpc_resp = &data->res, | |
9ace33cd | 1387 | .rpc_cred = data->cred, |
bdc7f021 | 1388 | }; |
84115e1c | 1389 | struct rpc_task_setup task_setup_data = { |
07737691 | 1390 | .task = &data->task, |
9ace33cd | 1391 | .rpc_client = clnt, |
bdc7f021 | 1392 | .rpc_message = &msg, |
9ace33cd | 1393 | .callback_ops = call_ops, |
84115e1c | 1394 | .callback_data = data, |
101070ca | 1395 | .workqueue = nfsiod_workqueue, |
2c61be0a | 1396 | .flags = RPC_TASK_ASYNC, |
3ff7576d | 1397 | .priority = priority, |
84115e1c | 1398 | }; |
9ace33cd FI |
1399 | /* Set up the initial task struct. */ |
1400 | NFS_PROTO(data->inode)->commit_setup(data, &msg); | |
1401 | ||
1402 | dprintk("NFS: %5u initiated commit call\n", data->task.tk_pid); | |
1403 | ||
1404 | task = rpc_run_task(&task_setup_data); | |
1405 | if (IS_ERR(task)) | |
1406 | return PTR_ERR(task); | |
1407 | if (how & FLUSH_SYNC) | |
1408 | rpc_wait_for_completion_task(task); | |
1409 | rpc_put_task(task); | |
1410 | return 0; | |
1411 | } | |
e0c2b380 | 1412 | EXPORT_SYMBOL_GPL(nfs_initiate_commit); |
9ace33cd FI |
1413 | |
1414 | /* | |
1415 | * Set up the argument/result storage required for the RPC call. | |
1416 | */ | |
0b7c0153 | 1417 | void nfs_init_commit(struct nfs_commit_data *data, |
f453a54a FI |
1418 | struct list_head *head, |
1419 | struct pnfs_layout_segment *lseg, | |
1420 | struct nfs_commit_info *cinfo) | |
9ace33cd FI |
1421 | { |
1422 | struct nfs_page *first = nfs_list_entry(head->next); | |
3d4ff43d | 1423 | struct inode *inode = first->wb_context->dentry->d_inode; |
1da177e4 LT |
1424 | |
1425 | /* Set up the RPC argument and reply structs | |
1426 | * NB: take care not to mess about with data->commit et al. */ | |
1427 | ||
1428 | list_splice_init(head, &data->pages); | |
1da177e4 | 1429 | |
1da177e4 | 1430 | data->inode = inode; |
9ace33cd | 1431 | data->cred = first->wb_context->cred; |
988b6dce | 1432 | data->lseg = lseg; /* reference transferred */ |
9ace33cd | 1433 | data->mds_ops = &nfs_commit_ops; |
f453a54a | 1434 | data->completion_ops = cinfo->completion_ops; |
b359f9d0 | 1435 | data->dreq = cinfo->dreq; |
1da177e4 LT |
1436 | |
1437 | data->args.fh = NFS_FH(data->inode); | |
3da28eb1 TM |
1438 | /* Note: we always request a commit of the entire inode */ |
1439 | data->args.offset = 0; | |
1440 | data->args.count = 0; | |
0b7c0153 | 1441 | data->context = get_nfs_open_context(first->wb_context); |
1da177e4 LT |
1442 | data->res.fattr = &data->fattr; |
1443 | data->res.verf = &data->verf; | |
0e574af1 | 1444 | nfs_fattr_init(&data->fattr); |
1da177e4 | 1445 | } |
e0c2b380 | 1446 | EXPORT_SYMBOL_GPL(nfs_init_commit); |
1da177e4 | 1447 | |
e0c2b380 | 1448 | void nfs_retry_commit(struct list_head *page_list, |
ea2cf228 FI |
1449 | struct pnfs_layout_segment *lseg, |
1450 | struct nfs_commit_info *cinfo) | |
64bfeb49 FI |
1451 | { |
1452 | struct nfs_page *req; | |
1453 | ||
1454 | while (!list_empty(page_list)) { | |
1455 | req = nfs_list_entry(page_list->next); | |
1456 | nfs_list_remove_request(req); | |
ea2cf228 | 1457 | nfs_mark_request_commit(req, lseg, cinfo); |
64bfeb49 FI |
1458 | dec_zone_page_state(req->wb_page, NR_UNSTABLE_NFS); |
1459 | dec_bdi_stat(req->wb_page->mapping->backing_dev_info, | |
1460 | BDI_RECLAIMABLE); | |
9994b62b | 1461 | nfs_unlock_request(req); |
64bfeb49 FI |
1462 | } |
1463 | } | |
e0c2b380 | 1464 | EXPORT_SYMBOL_GPL(nfs_retry_commit); |
64bfeb49 | 1465 | |
1da177e4 LT |
1466 | /* |
1467 | * Commit dirty pages | |
1468 | */ | |
1469 | static int | |
ea2cf228 FI |
1470 | nfs_commit_list(struct inode *inode, struct list_head *head, int how, |
1471 | struct nfs_commit_info *cinfo) | |
1da177e4 | 1472 | { |
0b7c0153 | 1473 | struct nfs_commit_data *data; |
1da177e4 | 1474 | |
c9d8f89d | 1475 | data = nfs_commitdata_alloc(); |
1da177e4 LT |
1476 | |
1477 | if (!data) | |
1478 | goto out_bad; | |
1479 | ||
1480 | /* Set up the argument struct */ | |
f453a54a FI |
1481 | nfs_init_commit(data, head, NULL, cinfo); |
1482 | atomic_inc(&cinfo->mds->rpcs_out); | |
0b7c0153 | 1483 | return nfs_initiate_commit(NFS_CLIENT(inode), data, data->mds_ops, how); |
1da177e4 | 1484 | out_bad: |
ea2cf228 | 1485 | nfs_retry_commit(head, NULL, cinfo); |
f453a54a | 1486 | cinfo->completion_ops->error_cleanup(NFS_I(inode)); |
1da177e4 LT |
1487 | return -ENOMEM; |
1488 | } | |
1489 | ||
1490 | /* | |
1491 | * COMMIT call returned | |
1492 | */ | |
788e7a89 | 1493 | static void nfs_commit_done(struct rpc_task *task, void *calldata) |
1da177e4 | 1494 | { |
0b7c0153 | 1495 | struct nfs_commit_data *data = calldata; |
1da177e4 | 1496 | |
a3f565b1 | 1497 | dprintk("NFS: %5u nfs_commit_done (status %d)\n", |
1da177e4 LT |
1498 | task->tk_pid, task->tk_status); |
1499 | ||
788e7a89 | 1500 | /* Call the NFS version-specific code */ |
c0d0e96b | 1501 | NFS_PROTO(data->inode)->commit_done(task, data); |
c9d8f89d TM |
1502 | } |
1503 | ||
f453a54a | 1504 | static void nfs_commit_release_pages(struct nfs_commit_data *data) |
c9d8f89d | 1505 | { |
5917ce84 | 1506 | struct nfs_page *req; |
c9d8f89d | 1507 | int status = data->task.tk_status; |
f453a54a | 1508 | struct nfs_commit_info cinfo; |
788e7a89 | 1509 | |
1da177e4 LT |
1510 | while (!list_empty(&data->pages)) { |
1511 | req = nfs_list_entry(data->pages.next); | |
1512 | nfs_list_remove_request(req); | |
d6d6dc7c | 1513 | nfs_clear_page_commit(req->wb_page); |
1da177e4 | 1514 | |
48186c7d | 1515 | dprintk("NFS: commit (%s/%lld %d@%lld)", |
3d4ff43d AV |
1516 | req->wb_context->dentry->d_sb->s_id, |
1517 | (long long)NFS_FILEID(req->wb_context->dentry->d_inode), | |
1da177e4 LT |
1518 | req->wb_bytes, |
1519 | (long long)req_offset(req)); | |
c9d8f89d TM |
1520 | if (status < 0) { |
1521 | nfs_context_set_write_error(req->wb_context, status); | |
1da177e4 | 1522 | nfs_inode_remove_request(req); |
c9d8f89d | 1523 | dprintk(", error = %d\n", status); |
1da177e4 LT |
1524 | goto next; |
1525 | } | |
1526 | ||
1527 | /* Okay, COMMIT succeeded, apparently. Check the verifier | |
1528 | * returned by the server against all stored verfs. */ | |
1529 | if (!memcmp(req->wb_verf.verifier, data->verf.verifier, sizeof(data->verf.verifier))) { | |
1530 | /* We have a match */ | |
1531 | nfs_inode_remove_request(req); | |
1532 | dprintk(" OK\n"); | |
1533 | goto next; | |
1534 | } | |
1535 | /* We have a mismatch. Write the page again */ | |
1536 | dprintk(" mismatch\n"); | |
6d884e8f | 1537 | nfs_mark_request_dirty(req); |
1da177e4 | 1538 | next: |
9994b62b | 1539 | nfs_unlock_request(req); |
1da177e4 | 1540 | } |
f453a54a FI |
1541 | nfs_init_cinfo(&cinfo, data->inode, data->dreq); |
1542 | if (atomic_dec_and_test(&cinfo.mds->rpcs_out)) | |
1543 | nfs_commit_clear_lock(NFS_I(data->inode)); | |
5917ce84 FI |
1544 | } |
1545 | ||
1546 | static void nfs_commit_release(void *calldata) | |
1547 | { | |
0b7c0153 | 1548 | struct nfs_commit_data *data = calldata; |
5917ce84 | 1549 | |
f453a54a | 1550 | data->completion_ops->completion(data); |
c9d8f89d | 1551 | nfs_commitdata_release(calldata); |
1da177e4 | 1552 | } |
788e7a89 TM |
1553 | |
1554 | static const struct rpc_call_ops nfs_commit_ops = { | |
0b7c0153 | 1555 | .rpc_call_prepare = nfs_commit_prepare, |
788e7a89 TM |
1556 | .rpc_call_done = nfs_commit_done, |
1557 | .rpc_release = nfs_commit_release, | |
1558 | }; | |
1da177e4 | 1559 | |
f453a54a FI |
1560 | static const struct nfs_commit_completion_ops nfs_commit_completion_ops = { |
1561 | .completion = nfs_commit_release_pages, | |
1562 | .error_cleanup = nfs_commit_clear_lock, | |
1563 | }; | |
1564 | ||
84c53ab5 | 1565 | static int nfs_generic_commit_list(struct inode *inode, struct list_head *head, |
ea2cf228 | 1566 | int how, struct nfs_commit_info *cinfo) |
84c53ab5 FI |
1567 | { |
1568 | int status; | |
1569 | ||
ea2cf228 | 1570 | status = pnfs_commit_list(inode, head, how, cinfo); |
84c53ab5 | 1571 | if (status == PNFS_NOT_ATTEMPTED) |
ea2cf228 | 1572 | status = nfs_commit_list(inode, head, how, cinfo); |
84c53ab5 FI |
1573 | return status; |
1574 | } | |
1575 | ||
b608b283 | 1576 | int nfs_commit_inode(struct inode *inode, int how) |
1da177e4 | 1577 | { |
1da177e4 | 1578 | LIST_HEAD(head); |
ea2cf228 | 1579 | struct nfs_commit_info cinfo; |
71d0a611 | 1580 | int may_wait = how & FLUSH_SYNC; |
b8413f98 | 1581 | int res; |
1da177e4 | 1582 | |
b8413f98 TM |
1583 | res = nfs_commit_set_lock(NFS_I(inode), may_wait); |
1584 | if (res <= 0) | |
c5efa5fc | 1585 | goto out_mark_dirty; |
ea2cf228 FI |
1586 | nfs_init_cinfo_from_inode(&cinfo, inode); |
1587 | res = nfs_scan_commit(inode, &head, &cinfo); | |
1da177e4 | 1588 | if (res) { |
a861a1e1 FI |
1589 | int error; |
1590 | ||
ea2cf228 | 1591 | error = nfs_generic_commit_list(inode, &head, how, &cinfo); |
3da28eb1 TM |
1592 | if (error < 0) |
1593 | return error; | |
b8413f98 | 1594 | if (!may_wait) |
c5efa5fc | 1595 | goto out_mark_dirty; |
b8413f98 TM |
1596 | error = wait_on_bit(&NFS_I(inode)->flags, |
1597 | NFS_INO_COMMIT, | |
1598 | nfs_wait_bit_killable, | |
1599 | TASK_KILLABLE); | |
1600 | if (error < 0) | |
1601 | return error; | |
71d0a611 TM |
1602 | } else |
1603 | nfs_commit_clear_lock(NFS_I(inode)); | |
c5efa5fc TM |
1604 | return res; |
1605 | /* Note: If we exit without ensuring that the commit is complete, | |
1606 | * we must mark the inode as dirty. Otherwise, future calls to | |
1607 | * sync_inode() with the WB_SYNC_ALL flag set will fail to ensure | |
1608 | * that the data is on the disk. | |
1609 | */ | |
1610 | out_mark_dirty: | |
1611 | __mark_inode_dirty(inode, I_DIRTY_DATASYNC); | |
1da177e4 LT |
1612 | return res; |
1613 | } | |
8fc795f7 TM |
1614 | |
1615 | static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc) | |
1616 | { | |
420e3646 TM |
1617 | struct nfs_inode *nfsi = NFS_I(inode); |
1618 | int flags = FLUSH_SYNC; | |
1619 | int ret = 0; | |
8fc795f7 | 1620 | |
3236c3e1 | 1621 | /* no commits means nothing needs to be done */ |
ea2cf228 | 1622 | if (!nfsi->commit_info.ncommit) |
3236c3e1 JL |
1623 | return ret; |
1624 | ||
a00dd6c0 JL |
1625 | if (wbc->sync_mode == WB_SYNC_NONE) { |
1626 | /* Don't commit yet if this is a non-blocking flush and there | |
1627 | * are a lot of outstanding writes for this mapping. | |
1628 | */ | |
ea2cf228 | 1629 | if (nfsi->commit_info.ncommit <= (nfsi->npages >> 1)) |
a00dd6c0 | 1630 | goto out_mark_dirty; |
420e3646 | 1631 | |
a00dd6c0 | 1632 | /* don't wait for the COMMIT response */ |
420e3646 | 1633 | flags = 0; |
a00dd6c0 JL |
1634 | } |
1635 | ||
420e3646 TM |
1636 | ret = nfs_commit_inode(inode, flags); |
1637 | if (ret >= 0) { | |
1638 | if (wbc->sync_mode == WB_SYNC_NONE) { | |
1639 | if (ret < wbc->nr_to_write) | |
1640 | wbc->nr_to_write -= ret; | |
1641 | else | |
1642 | wbc->nr_to_write = 0; | |
1643 | } | |
8fc795f7 | 1644 | return 0; |
420e3646 TM |
1645 | } |
1646 | out_mark_dirty: | |
8fc795f7 TM |
1647 | __mark_inode_dirty(inode, I_DIRTY_DATASYNC); |
1648 | return ret; | |
1649 | } | |
c63c7b05 | 1650 | #else |
8fc795f7 TM |
1651 | static int nfs_commit_unstable_pages(struct inode *inode, struct writeback_control *wbc) |
1652 | { | |
1653 | return 0; | |
1654 | } | |
1da177e4 LT |
1655 | #endif |
1656 | ||
8fc795f7 TM |
1657 | int nfs_write_inode(struct inode *inode, struct writeback_control *wbc) |
1658 | { | |
863a3c6c AA |
1659 | int ret; |
1660 | ||
1661 | ret = nfs_commit_unstable_pages(inode, wbc); | |
1662 | if (ret >= 0 && test_bit(NFS_INO_LAYOUTCOMMIT, &NFS_I(inode)->flags)) { | |
ef311537 AA |
1663 | int status; |
1664 | bool sync = true; | |
863a3c6c | 1665 | |
846d5a09 | 1666 | if (wbc->sync_mode == WB_SYNC_NONE) |
ef311537 | 1667 | sync = false; |
863a3c6c AA |
1668 | |
1669 | status = pnfs_layoutcommit_inode(inode, sync); | |
1670 | if (status < 0) | |
1671 | return status; | |
1672 | } | |
1673 | return ret; | |
8fc795f7 TM |
1674 | } |
1675 | ||
acdc53b2 TM |
1676 | /* |
1677 | * flush the inode to disk. | |
1678 | */ | |
1679 | int nfs_wb_all(struct inode *inode) | |
34901f70 TM |
1680 | { |
1681 | struct writeback_control wbc = { | |
72cb77f4 | 1682 | .sync_mode = WB_SYNC_ALL, |
34901f70 | 1683 | .nr_to_write = LONG_MAX, |
d7fb1207 TM |
1684 | .range_start = 0, |
1685 | .range_end = LLONG_MAX, | |
34901f70 | 1686 | }; |
34901f70 | 1687 | |
acdc53b2 | 1688 | return sync_inode(inode, &wbc); |
1c75950b TM |
1689 | } |
1690 | ||
1b3b4a1a TM |
1691 | int nfs_wb_page_cancel(struct inode *inode, struct page *page) |
1692 | { | |
1693 | struct nfs_page *req; | |
1b3b4a1a TM |
1694 | int ret = 0; |
1695 | ||
1696 | BUG_ON(!PageLocked(page)); | |
1697 | for (;;) { | |
ba8b06e6 | 1698 | wait_on_page_writeback(page); |
1b3b4a1a TM |
1699 | req = nfs_page_find_request(page); |
1700 | if (req == NULL) | |
1b3b4a1a | 1701 | break; |
1b3b4a1a | 1702 | if (nfs_lock_request_dontget(req)) { |
8dd37758 | 1703 | nfs_clear_request_commit(req); |
1b3b4a1a TM |
1704 | nfs_inode_remove_request(req); |
1705 | /* | |
1706 | * In case nfs_inode_remove_request has marked the | |
1707 | * page as being dirty | |
1708 | */ | |
1709 | cancel_dirty_page(page, PAGE_CACHE_SIZE); | |
1710 | nfs_unlock_request(req); | |
1711 | break; | |
1712 | } | |
1713 | ret = nfs_wait_on_request(req); | |
c9edda71 | 1714 | nfs_release_request(req); |
1b3b4a1a | 1715 | if (ret < 0) |
c988950e | 1716 | break; |
1b3b4a1a | 1717 | } |
1b3b4a1a TM |
1718 | return ret; |
1719 | } | |
1720 | ||
7f2f12d9 TM |
1721 | /* |
1722 | * Write back all requests on one page - we do this before reading it. | |
1723 | */ | |
1724 | int nfs_wb_page(struct inode *inode, struct page *page) | |
1c75950b TM |
1725 | { |
1726 | loff_t range_start = page_offset(page); | |
1727 | loff_t range_end = range_start + (loff_t)(PAGE_CACHE_SIZE - 1); | |
4d770ccf | 1728 | struct writeback_control wbc = { |
4d770ccf | 1729 | .sync_mode = WB_SYNC_ALL, |
7f2f12d9 | 1730 | .nr_to_write = 0, |
4d770ccf TM |
1731 | .range_start = range_start, |
1732 | .range_end = range_end, | |
1733 | }; | |
1734 | int ret; | |
1c75950b | 1735 | |
0522f6ad | 1736 | for (;;) { |
ba8b06e6 | 1737 | wait_on_page_writeback(page); |
73e3302f TM |
1738 | if (clear_page_dirty_for_io(page)) { |
1739 | ret = nfs_writepage_locked(page, &wbc); | |
1740 | if (ret < 0) | |
1741 | goto out_error; | |
0522f6ad | 1742 | continue; |
7f2f12d9 | 1743 | } |
0522f6ad TM |
1744 | if (!PagePrivate(page)) |
1745 | break; | |
1746 | ret = nfs_commit_inode(inode, FLUSH_SYNC); | |
ba8b06e6 | 1747 | if (ret < 0) |
73e3302f | 1748 | goto out_error; |
7f2f12d9 | 1749 | } |
73e3302f TM |
1750 | return 0; |
1751 | out_error: | |
4d770ccf | 1752 | return ret; |
1c75950b TM |
1753 | } |
1754 | ||
074cc1de TM |
1755 | #ifdef CONFIG_MIGRATION |
1756 | int nfs_migrate_page(struct address_space *mapping, struct page *newpage, | |
a6bc32b8 | 1757 | struct page *page, enum migrate_mode mode) |
074cc1de | 1758 | { |
2da95652 JL |
1759 | /* |
1760 | * If PagePrivate is set, then the page is currently associated with | |
1761 | * an in-progress read or write request. Don't try to migrate it. | |
1762 | * | |
1763 | * FIXME: we could do this in principle, but we'll need a way to ensure | |
1764 | * that we can safely release the inode reference while holding | |
1765 | * the page lock. | |
1766 | */ | |
1767 | if (PagePrivate(page)) | |
1768 | return -EBUSY; | |
074cc1de | 1769 | |
7549ad5f | 1770 | nfs_fscache_release_page(page, GFP_KERNEL); |
074cc1de | 1771 | |
a6bc32b8 | 1772 | return migrate_page(mapping, newpage, page, mode); |
074cc1de TM |
1773 | } |
1774 | #endif | |
1775 | ||
f7b422b1 | 1776 | int __init nfs_init_writepagecache(void) |
1da177e4 LT |
1777 | { |
1778 | nfs_wdata_cachep = kmem_cache_create("nfs_write_data", | |
cd841605 | 1779 | sizeof(struct nfs_write_header), |
1da177e4 | 1780 | 0, SLAB_HWCACHE_ALIGN, |
20c2df83 | 1781 | NULL); |
1da177e4 LT |
1782 | if (nfs_wdata_cachep == NULL) |
1783 | return -ENOMEM; | |
1784 | ||
93d2341c MD |
1785 | nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE, |
1786 | nfs_wdata_cachep); | |
1da177e4 LT |
1787 | if (nfs_wdata_mempool == NULL) |
1788 | return -ENOMEM; | |
1789 | ||
0b7c0153 FI |
1790 | nfs_cdata_cachep = kmem_cache_create("nfs_commit_data", |
1791 | sizeof(struct nfs_commit_data), | |
1792 | 0, SLAB_HWCACHE_ALIGN, | |
1793 | NULL); | |
1794 | if (nfs_cdata_cachep == NULL) | |
1795 | return -ENOMEM; | |
1796 | ||
93d2341c MD |
1797 | nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT, |
1798 | nfs_wdata_cachep); | |
1da177e4 LT |
1799 | if (nfs_commit_mempool == NULL) |
1800 | return -ENOMEM; | |
1801 | ||
89a09141 PZ |
1802 | /* |
1803 | * NFS congestion size, scale with available memory. | |
1804 | * | |
1805 | * 64MB: 8192k | |
1806 | * 128MB: 11585k | |
1807 | * 256MB: 16384k | |
1808 | * 512MB: 23170k | |
1809 | * 1GB: 32768k | |
1810 | * 2GB: 46340k | |
1811 | * 4GB: 65536k | |
1812 | * 8GB: 92681k | |
1813 | * 16GB: 131072k | |
1814 | * | |
1815 | * This allows larger machines to have larger/more transfers. | |
1816 | * Limit the default to 256M | |
1817 | */ | |
1818 | nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10); | |
1819 | if (nfs_congestion_kb > 256*1024) | |
1820 | nfs_congestion_kb = 256*1024; | |
1821 | ||
1da177e4 LT |
1822 | return 0; |
1823 | } | |
1824 | ||
266bee88 | 1825 | void nfs_destroy_writepagecache(void) |
1da177e4 LT |
1826 | { |
1827 | mempool_destroy(nfs_commit_mempool); | |
1828 | mempool_destroy(nfs_wdata_mempool); | |
1a1d92c1 | 1829 | kmem_cache_destroy(nfs_wdata_cachep); |
1da177e4 LT |
1830 | } |
1831 |