]>
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> |
af7cf057 TM |
24 | #include <linux/freezer.h> |
25 | #include <linux/wait.h> | |
3fcfab16 | 26 | |
7c0f6ba6 | 27 | #include <linux/uaccess.h> |
1da177e4 LT |
28 | |
29 | #include "delegation.h" | |
49a70f27 | 30 | #include "internal.h" |
91d5b470 | 31 | #include "iostat.h" |
def6ed7e | 32 | #include "nfs4_fs.h" |
074cc1de | 33 | #include "fscache.h" |
94ad1c80 | 34 | #include "pnfs.h" |
1da177e4 | 35 | |
f4ce1299 TM |
36 | #include "nfstrace.h" |
37 | ||
1da177e4 LT |
38 | #define NFSDBG_FACILITY NFSDBG_PAGECACHE |
39 | ||
40 | #define MIN_POOL_WRITE (32) | |
41 | #define MIN_POOL_COMMIT (4) | |
42 | ||
919e3bd9 TM |
43 | struct nfs_io_completion { |
44 | void (*complete)(void *data); | |
45 | void *data; | |
46 | struct kref refcount; | |
47 | }; | |
48 | ||
1da177e4 LT |
49 | /* |
50 | * Local function declarations | |
51 | */ | |
f8512ad0 | 52 | static void nfs_redirty_request(struct nfs_page *req); |
788e7a89 | 53 | static const struct rpc_call_ops nfs_commit_ops; |
061ae2ed | 54 | static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops; |
f453a54a | 55 | static const struct nfs_commit_completion_ops nfs_commit_completion_ops; |
4a0de55c | 56 | static const struct nfs_rw_ops nfs_rw_write_ops; |
d4581383 | 57 | static void nfs_clear_request_commit(struct nfs_page *req); |
02d1426c WAA |
58 | static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo, |
59 | struct inode *inode); | |
3a3908c8 TM |
60 | static struct nfs_page * |
61 | nfs_page_search_commits_for_head_request_locked(struct nfs_inode *nfsi, | |
62 | struct page *page); | |
1da177e4 | 63 | |
e18b890b | 64 | static struct kmem_cache *nfs_wdata_cachep; |
3feb2d49 | 65 | static mempool_t *nfs_wdata_mempool; |
0b7c0153 | 66 | static struct kmem_cache *nfs_cdata_cachep; |
1da177e4 LT |
67 | static mempool_t *nfs_commit_mempool; |
68 | ||
518662e0 | 69 | struct nfs_commit_data *nfs_commitdata_alloc(bool never_fail) |
1da177e4 | 70 | { |
518662e0 | 71 | struct nfs_commit_data *p; |
40859d7e | 72 | |
518662e0 N |
73 | if (never_fail) |
74 | p = mempool_alloc(nfs_commit_mempool, GFP_NOIO); | |
75 | else { | |
76 | /* It is OK to do some reclaim, not no safe to wait | |
77 | * for anything to be returned to the pool. | |
78 | * mempool_alloc() cannot handle that particular combination, | |
79 | * so we need two separate attempts. | |
80 | */ | |
81 | p = mempool_alloc(nfs_commit_mempool, GFP_NOWAIT); | |
82 | if (!p) | |
83 | p = kmem_cache_alloc(nfs_cdata_cachep, GFP_NOIO | | |
84 | __GFP_NOWARN | __GFP_NORETRY); | |
85 | if (!p) | |
86 | return NULL; | |
1da177e4 | 87 | } |
518662e0 N |
88 | |
89 | memset(p, 0, sizeof(*p)); | |
90 | INIT_LIST_HEAD(&p->pages); | |
1da177e4 LT |
91 | return p; |
92 | } | |
e0c2b380 | 93 | EXPORT_SYMBOL_GPL(nfs_commitdata_alloc); |
1da177e4 | 94 | |
0b7c0153 | 95 | void nfs_commit_free(struct nfs_commit_data *p) |
1da177e4 LT |
96 | { |
97 | mempool_free(p, nfs_commit_mempool); | |
98 | } | |
e0c2b380 | 99 | EXPORT_SYMBOL_GPL(nfs_commit_free); |
1da177e4 | 100 | |
1e7f3a48 | 101 | static struct nfs_pgio_header *nfs_writehdr_alloc(void) |
3feb2d49 | 102 | { |
1e7f3a48 | 103 | struct nfs_pgio_header *p = mempool_alloc(nfs_wdata_mempool, GFP_NOIO); |
cd841605 | 104 | |
fbe77c30 | 105 | if (p) { |
3feb2d49 | 106 | memset(p, 0, sizeof(*p)); |
fbe77c30 BC |
107 | p->rw_mode = FMODE_WRITE; |
108 | } | |
3feb2d49 TM |
109 | return p; |
110 | } | |
6c75dc0d | 111 | |
1e7f3a48 | 112 | static void nfs_writehdr_free(struct nfs_pgio_header *hdr) |
3feb2d49 | 113 | { |
1e7f3a48 | 114 | mempool_free(hdr, nfs_wdata_mempool); |
3feb2d49 | 115 | } |
1da177e4 | 116 | |
919e3bd9 TM |
117 | static struct nfs_io_completion *nfs_io_completion_alloc(gfp_t gfp_flags) |
118 | { | |
119 | return kmalloc(sizeof(struct nfs_io_completion), gfp_flags); | |
120 | } | |
121 | ||
122 | static void nfs_io_completion_init(struct nfs_io_completion *ioc, | |
123 | void (*complete)(void *), void *data) | |
124 | { | |
125 | ioc->complete = complete; | |
126 | ioc->data = data; | |
127 | kref_init(&ioc->refcount); | |
128 | } | |
129 | ||
130 | static void nfs_io_completion_release(struct kref *kref) | |
131 | { | |
132 | struct nfs_io_completion *ioc = container_of(kref, | |
133 | struct nfs_io_completion, refcount); | |
134 | ioc->complete(ioc->data); | |
135 | kfree(ioc); | |
136 | } | |
137 | ||
138 | static void nfs_io_completion_get(struct nfs_io_completion *ioc) | |
139 | { | |
140 | if (ioc != NULL) | |
141 | kref_get(&ioc->refcount); | |
142 | } | |
143 | ||
144 | static void nfs_io_completion_put(struct nfs_io_completion *ioc) | |
145 | { | |
146 | if (ioc != NULL) | |
147 | kref_put(&ioc->refcount, nfs_io_completion_release); | |
148 | } | |
149 | ||
7b159fc1 TM |
150 | static void nfs_context_set_write_error(struct nfs_open_context *ctx, int error) |
151 | { | |
152 | ctx->error = error; | |
153 | smp_wmb(); | |
154 | set_bit(NFS_CONTEXT_ERROR_WRITE, &ctx->flags); | |
155 | } | |
156 | ||
84d3a9a9 WAA |
157 | /* |
158 | * nfs_page_find_head_request_locked - find head request associated with @page | |
159 | * | |
160 | * must be called while holding the inode lock. | |
161 | * | |
162 | * returns matching head request with reference held, or NULL if not found. | |
163 | */ | |
29418aa4 | 164 | static struct nfs_page * |
84d3a9a9 | 165 | nfs_page_find_head_request_locked(struct nfs_inode *nfsi, struct page *page) |
277459d2 TM |
166 | { |
167 | struct nfs_page *req = NULL; | |
168 | ||
29418aa4 | 169 | if (PagePrivate(page)) |
277459d2 | 170 | req = (struct nfs_page *)page_private(page); |
02d1426c WAA |
171 | else if (unlikely(PageSwapCache(page))) |
172 | req = nfs_page_search_commits_for_head_request_locked(nfsi, | |
173 | page); | |
29418aa4 | 174 | |
84d3a9a9 WAA |
175 | if (req) { |
176 | WARN_ON_ONCE(req->wb_head != req); | |
29418aa4 | 177 | kref_get(&req->wb_kref); |
84d3a9a9 | 178 | } |
29418aa4 | 179 | |
277459d2 TM |
180 | return req; |
181 | } | |
182 | ||
84d3a9a9 WAA |
183 | /* |
184 | * nfs_page_find_head_request - find head request associated with @page | |
185 | * | |
186 | * returns matching head request with reference held, or NULL if not found. | |
187 | */ | |
188 | static struct nfs_page *nfs_page_find_head_request(struct page *page) | |
277459d2 | 189 | { |
d56b4ddf | 190 | struct inode *inode = page_file_mapping(page)->host; |
277459d2 | 191 | struct nfs_page *req = NULL; |
277459d2 | 192 | |
82749dd4 TM |
193 | if (PagePrivate(page) || PageSwapCache(page)) { |
194 | spin_lock(&inode->i_lock); | |
195 | req = nfs_page_find_head_request_locked(NFS_I(inode), page); | |
196 | spin_unlock(&inode->i_lock); | |
197 | } | |
277459d2 TM |
198 | return req; |
199 | } | |
200 | ||
1da177e4 LT |
201 | /* Adjust the file length if we're writing beyond the end */ |
202 | static void nfs_grow_file(struct page *page, unsigned int offset, unsigned int count) | |
203 | { | |
d56b4ddf | 204 | struct inode *inode = page_file_mapping(page)->host; |
a3d01454 TM |
205 | loff_t end, i_size; |
206 | pgoff_t end_index; | |
1da177e4 | 207 | |
a3d01454 TM |
208 | spin_lock(&inode->i_lock); |
209 | i_size = i_size_read(inode); | |
09cbfeaf | 210 | end_index = (i_size - 1) >> PAGE_SHIFT; |
8cd79788 | 211 | if (i_size > 0 && page_index(page) < end_index) |
a3d01454 | 212 | goto out; |
d56b4ddf | 213 | end = page_file_offset(page) + ((loff_t)offset+count); |
1da177e4 | 214 | if (i_size >= end) |
a3d01454 | 215 | goto out; |
1da177e4 | 216 | i_size_write(inode, end); |
a3d01454 TM |
217 | nfs_inc_stats(inode, NFSIOS_EXTENDWRITE); |
218 | out: | |
219 | spin_unlock(&inode->i_lock); | |
1da177e4 LT |
220 | } |
221 | ||
a301b777 TM |
222 | /* A writeback failed: mark the page as bad, and invalidate the page cache */ |
223 | static void nfs_set_pageerror(struct page *page) | |
224 | { | |
d56b4ddf | 225 | nfs_zap_mapping(page_file_mapping(page)->host, page_file_mapping(page)); |
a301b777 TM |
226 | } |
227 | ||
d72ddcba WAA |
228 | /* |
229 | * nfs_page_group_search_locked | |
230 | * @head - head request of page group | |
231 | * @page_offset - offset into page | |
232 | * | |
233 | * Search page group with head @head to find a request that contains the | |
234 | * page offset @page_offset. | |
235 | * | |
236 | * Returns a pointer to the first matching nfs request, or NULL if no | |
237 | * match is found. | |
238 | * | |
239 | * Must be called with the page group lock held | |
240 | */ | |
241 | static struct nfs_page * | |
242 | nfs_page_group_search_locked(struct nfs_page *head, unsigned int page_offset) | |
243 | { | |
244 | struct nfs_page *req; | |
245 | ||
246 | WARN_ON_ONCE(head != head->wb_head); | |
247 | WARN_ON_ONCE(!test_bit(PG_HEADLOCK, &head->wb_head->wb_flags)); | |
248 | ||
249 | req = head; | |
250 | do { | |
251 | if (page_offset >= req->wb_pgbase && | |
252 | page_offset < (req->wb_pgbase + req->wb_bytes)) | |
253 | return req; | |
254 | ||
255 | req = req->wb_this_page; | |
256 | } while (req != head); | |
257 | ||
258 | return NULL; | |
259 | } | |
260 | ||
261 | /* | |
262 | * nfs_page_group_covers_page | |
263 | * @head - head request of page group | |
264 | * | |
265 | * Return true if the page group with head @head covers the whole page, | |
266 | * returns false otherwise | |
267 | */ | |
268 | static bool nfs_page_group_covers_page(struct nfs_page *req) | |
269 | { | |
270 | struct nfs_page *tmp; | |
271 | unsigned int pos = 0; | |
272 | unsigned int len = nfs_page_length(req->wb_page); | |
273 | ||
fd2f3a06 | 274 | nfs_page_group_lock(req, false); |
d72ddcba WAA |
275 | |
276 | do { | |
277 | tmp = nfs_page_group_search_locked(req->wb_head, pos); | |
278 | if (tmp) { | |
279 | /* no way this should happen */ | |
280 | WARN_ON_ONCE(tmp->wb_pgbase != pos); | |
281 | pos += tmp->wb_bytes - (pos - tmp->wb_pgbase); | |
282 | } | |
283 | } while (tmp && pos < len); | |
284 | ||
285 | nfs_page_group_unlock(req); | |
286 | WARN_ON_ONCE(pos > len); | |
287 | return pos == len; | |
288 | } | |
289 | ||
1da177e4 LT |
290 | /* We can set the PG_uptodate flag if we see that a write request |
291 | * covers the full page. | |
292 | */ | |
d72ddcba | 293 | static void nfs_mark_uptodate(struct nfs_page *req) |
1da177e4 | 294 | { |
d72ddcba | 295 | if (PageUptodate(req->wb_page)) |
1da177e4 | 296 | return; |
d72ddcba | 297 | if (!nfs_page_group_covers_page(req)) |
1da177e4 | 298 | return; |
d72ddcba | 299 | SetPageUptodate(req->wb_page); |
1da177e4 LT |
300 | } |
301 | ||
1da177e4 LT |
302 | static int wb_priority(struct writeback_control *wbc) |
303 | { | |
e87b4c7a | 304 | int ret = 0; |
cca588d6 | 305 | |
e87b4c7a N |
306 | if (wbc->sync_mode == WB_SYNC_ALL) |
307 | ret = FLUSH_COND_STABLE; | |
e87b4c7a | 308 | return ret; |
1da177e4 LT |
309 | } |
310 | ||
89a09141 PZ |
311 | /* |
312 | * NFS congestion control | |
313 | */ | |
314 | ||
315 | int nfs_congestion_kb; | |
316 | ||
317 | #define NFS_CONGESTION_ON_THRESH (nfs_congestion_kb >> (PAGE_SHIFT-10)) | |
318 | #define NFS_CONGESTION_OFF_THRESH \ | |
319 | (NFS_CONGESTION_ON_THRESH - (NFS_CONGESTION_ON_THRESH >> 2)) | |
320 | ||
deed85e7 | 321 | static void nfs_set_page_writeback(struct page *page) |
89a09141 | 322 | { |
0db10944 JK |
323 | struct inode *inode = page_file_mapping(page)->host; |
324 | struct nfs_server *nfss = NFS_SERVER(inode); | |
5a6d41b3 TM |
325 | int ret = test_set_page_writeback(page); |
326 | ||
deed85e7 | 327 | WARN_ON_ONCE(ret != 0); |
89a09141 | 328 | |
deed85e7 | 329 | if (atomic_long_inc_return(&nfss->writeback) > |
0db10944 JK |
330 | NFS_CONGESTION_ON_THRESH) |
331 | set_bdi_congested(inode_to_bdi(inode), BLK_RW_ASYNC); | |
89a09141 PZ |
332 | } |
333 | ||
20633f04 | 334 | static void nfs_end_page_writeback(struct nfs_page *req) |
89a09141 | 335 | { |
20633f04 | 336 | struct inode *inode = page_file_mapping(req->wb_page)->host; |
89a09141 | 337 | struct nfs_server *nfss = NFS_SERVER(inode); |
31a01f09 | 338 | bool is_done; |
89a09141 | 339 | |
31a01f09 TM |
340 | is_done = nfs_page_group_sync_on_bit(req, PG_WB_END); |
341 | nfs_unlock_request(req); | |
342 | if (!is_done) | |
20633f04 WAA |
343 | return; |
344 | ||
345 | end_page_writeback(req->wb_page); | |
c4dc4bee | 346 | if (atomic_long_dec_return(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH) |
0db10944 | 347 | clear_bdi_congested(inode_to_bdi(inode), BLK_RW_ASYNC); |
89a09141 PZ |
348 | } |
349 | ||
d4581383 WAA |
350 | |
351 | /* nfs_page_group_clear_bits | |
352 | * @req - an nfs request | |
353 | * clears all page group related bits from @req | |
354 | */ | |
355 | static void | |
356 | nfs_page_group_clear_bits(struct nfs_page *req) | |
357 | { | |
358 | clear_bit(PG_TEARDOWN, &req->wb_flags); | |
359 | clear_bit(PG_UNLOCKPAGE, &req->wb_flags); | |
360 | clear_bit(PG_UPTODATE, &req->wb_flags); | |
361 | clear_bit(PG_WB_END, &req->wb_flags); | |
362 | clear_bit(PG_REMOVE, &req->wb_flags); | |
363 | } | |
364 | ||
365 | ||
366 | /* | |
367 | * nfs_unroll_locks_and_wait - unlock all newly locked reqs and wait on @req | |
368 | * | |
369 | * this is a helper function for nfs_lock_and_join_requests | |
370 | * | |
371 | * @inode - inode associated with request page group, must be holding inode lock | |
372 | * @head - head request of page group, must be holding head lock | |
373 | * @req - request that couldn't lock and needs to wait on the req bit lock | |
d4581383 WAA |
374 | * |
375 | * NOTE: this must be called holding page_group bit lock and inode spin lock | |
376 | * and BOTH will be released before returning. | |
377 | * | |
378 | * returns 0 on success, < 0 on error. | |
379 | */ | |
380 | static int | |
381 | nfs_unroll_locks_and_wait(struct inode *inode, struct nfs_page *head, | |
6d17d653 | 382 | struct nfs_page *req) |
d4581383 WAA |
383 | __releases(&inode->i_lock) |
384 | { | |
385 | struct nfs_page *tmp; | |
386 | int ret; | |
387 | ||
388 | /* relinquish all the locks successfully grabbed this run */ | |
a0e265bc | 389 | for (tmp = head->wb_this_page ; tmp != req; tmp = tmp->wb_this_page) |
d4581383 WAA |
390 | nfs_unlock_request(tmp); |
391 | ||
392 | WARN_ON_ONCE(test_bit(PG_TEARDOWN, &req->wb_flags)); | |
393 | ||
394 | /* grab a ref on the request that will be waited on */ | |
395 | kref_get(&req->wb_kref); | |
396 | ||
397 | nfs_page_group_unlock(head); | |
398 | spin_unlock(&inode->i_lock); | |
399 | ||
400 | /* release ref from nfs_page_find_head_request_locked */ | |
a0e265bc | 401 | nfs_unlock_and_release_request(head); |
d4581383 | 402 | |
6d17d653 | 403 | ret = nfs_wait_on_request(req); |
d4581383 WAA |
404 | nfs_release_request(req); |
405 | ||
406 | return ret; | |
407 | } | |
408 | ||
409 | /* | |
410 | * nfs_destroy_unlinked_subrequests - destroy recently unlinked subrequests | |
411 | * | |
412 | * @destroy_list - request list (using wb_this_page) terminated by @old_head | |
413 | * @old_head - the old head of the list | |
414 | * | |
415 | * All subrequests must be locked and removed from all lists, so at this point | |
416 | * they are only "active" in this function, and possibly in nfs_wait_on_request | |
417 | * with a reference held by some other context. | |
418 | */ | |
419 | static void | |
420 | nfs_destroy_unlinked_subrequests(struct nfs_page *destroy_list, | |
b66aaa8d TM |
421 | struct nfs_page *old_head, |
422 | struct inode *inode) | |
d4581383 WAA |
423 | { |
424 | while (destroy_list) { | |
425 | struct nfs_page *subreq = destroy_list; | |
426 | ||
427 | destroy_list = (subreq->wb_this_page == old_head) ? | |
428 | NULL : subreq->wb_this_page; | |
429 | ||
430 | WARN_ON_ONCE(old_head != subreq->wb_head); | |
431 | ||
432 | /* make sure old group is not used */ | |
433 | subreq->wb_head = subreq; | |
434 | subreq->wb_this_page = subreq; | |
435 | ||
d4581383 WAA |
436 | /* subreq is now totally disconnected from page group or any |
437 | * write / commit lists. last chance to wake any waiters */ | |
438 | nfs_unlock_request(subreq); | |
439 | ||
440 | if (!test_bit(PG_TEARDOWN, &subreq->wb_flags)) { | |
441 | /* release ref on old head request */ | |
442 | nfs_release_request(old_head); | |
443 | ||
444 | nfs_page_group_clear_bits(subreq); | |
445 | ||
446 | /* release the PG_INODE_REF reference */ | |
b66aaa8d | 447 | if (test_and_clear_bit(PG_INODE_REF, &subreq->wb_flags)) { |
d4581383 | 448 | nfs_release_request(subreq); |
b66aaa8d TM |
449 | spin_lock(&inode->i_lock); |
450 | NFS_I(inode)->nrequests--; | |
451 | spin_unlock(&inode->i_lock); | |
452 | } else | |
d4581383 WAA |
453 | WARN_ON_ONCE(1); |
454 | } else { | |
455 | WARN_ON_ONCE(test_bit(PG_CLEAN, &subreq->wb_flags)); | |
456 | /* zombie requests have already released the last | |
457 | * reference and were waiting on the rest of the | |
458 | * group to complete. Since it's no longer part of a | |
459 | * group, simply free the request */ | |
460 | nfs_page_group_clear_bits(subreq); | |
461 | nfs_free_request(subreq); | |
462 | } | |
463 | } | |
464 | } | |
465 | ||
466 | /* | |
467 | * nfs_lock_and_join_requests - join all subreqs to the head req and return | |
468 | * a locked reference, cancelling any pending | |
469 | * operations for this page. | |
470 | * | |
471 | * @page - the page used to lookup the "page group" of nfs_page structures | |
d4581383 WAA |
472 | * |
473 | * This function joins all sub requests to the head request by first | |
474 | * locking all requests in the group, cancelling any pending operations | |
475 | * and finally updating the head request to cover the whole range covered by | |
476 | * the (former) group. All subrequests are removed from any write or commit | |
477 | * lists, unlinked from the group and destroyed. | |
478 | * | |
479 | * Returns a locked, referenced pointer to the head request - which after | |
480 | * this call is guaranteed to be the only request associated with the page. | |
481 | * Returns NULL if no requests are found for @page, or a ERR_PTR if an | |
482 | * error was encountered. | |
483 | */ | |
484 | static struct nfs_page * | |
6d17d653 | 485 | nfs_lock_and_join_requests(struct page *page) |
e261f51f | 486 | { |
d56b4ddf | 487 | struct inode *inode = page_file_mapping(page)->host; |
d4581383 WAA |
488 | struct nfs_page *head, *subreq; |
489 | struct nfs_page *destroy_list = NULL; | |
490 | unsigned int total_bytes; | |
e261f51f TM |
491 | int ret; |
492 | ||
d4581383 | 493 | try_again: |
587142f8 | 494 | spin_lock(&inode->i_lock); |
d4581383 WAA |
495 | |
496 | /* | |
497 | * A reference is taken only on the head request which acts as a | |
498 | * reference to the whole page group - the group will not be destroyed | |
499 | * until the head reference is released. | |
500 | */ | |
501 | head = nfs_page_find_head_request_locked(NFS_I(inode), page); | |
502 | ||
503 | if (!head) { | |
587142f8 | 504 | spin_unlock(&inode->i_lock); |
d4581383 WAA |
505 | return NULL; |
506 | } | |
507 | ||
a0e265bc TM |
508 | /* lock the page head first in order to avoid an ABBA inefficiency */ |
509 | if (!nfs_lock_request(head)) { | |
510 | spin_unlock(&inode->i_lock); | |
511 | ret = nfs_wait_on_request(head); | |
512 | nfs_release_request(head); | |
513 | if (ret < 0) | |
514 | return ERR_PTR(ret); | |
515 | goto try_again; | |
516 | } | |
517 | ||
7c3af975 WAA |
518 | /* holding inode lock, so always make a non-blocking call to try the |
519 | * page group lock */ | |
fd2f3a06 | 520 | ret = nfs_page_group_lock(head, true); |
94970014 WAA |
521 | if (ret < 0) { |
522 | spin_unlock(&inode->i_lock); | |
7c3af975 | 523 | |
6d17d653 | 524 | nfs_page_group_lock_wait(head); |
a0e265bc | 525 | nfs_unlock_and_release_request(head); |
6d17d653 | 526 | goto try_again; |
94970014 | 527 | } |
7c3af975 WAA |
528 | |
529 | /* lock each request in the page group */ | |
a0e265bc TM |
530 | total_bytes = head->wb_bytes; |
531 | for (subreq = head->wb_this_page; subreq != head; | |
532 | subreq = subreq->wb_this_page) { | |
e14bebf6 TM |
533 | if (!nfs_lock_request(subreq)) { |
534 | /* releases page group bit lock and | |
535 | * inode spin lock and all references */ | |
536 | ret = nfs_unroll_locks_and_wait(inode, head, | |
537 | subreq); | |
538 | ||
539 | if (ret == 0) | |
540 | goto try_again; | |
541 | ||
542 | return ERR_PTR(ret); | |
543 | } | |
d4581383 WAA |
544 | /* |
545 | * Subrequests are always contiguous, non overlapping | |
309a1d65 | 546 | * and in order - but may be repeated (mirrored writes). |
d4581383 | 547 | */ |
309a1d65 WAA |
548 | if (subreq->wb_offset == (head->wb_offset + total_bytes)) { |
549 | /* keep track of how many bytes this group covers */ | |
550 | total_bytes += subreq->wb_bytes; | |
551 | } else if (WARN_ON_ONCE(subreq->wb_offset < head->wb_offset || | |
552 | ((subreq->wb_offset + subreq->wb_bytes) > | |
553 | (head->wb_offset + total_bytes)))) { | |
e14bebf6 | 554 | nfs_unlock_request(subreq); |
7cb9cd9a | 555 | nfs_unroll_locks_and_wait(inode, head, subreq); |
309a1d65 WAA |
556 | return ERR_PTR(-EIO); |
557 | } | |
a0e265bc | 558 | } |
d4581383 WAA |
559 | |
560 | /* Now that all requests are locked, make sure they aren't on any list. | |
561 | * Commit list removal accounting is done after locks are dropped */ | |
562 | subreq = head; | |
563 | do { | |
411a99ad | 564 | nfs_clear_request_commit(subreq); |
d4581383 WAA |
565 | subreq = subreq->wb_this_page; |
566 | } while (subreq != head); | |
567 | ||
568 | /* unlink subrequests from head, destroy them later */ | |
569 | if (head->wb_this_page != head) { | |
570 | /* destroy list will be terminated by head */ | |
571 | destroy_list = head->wb_this_page; | |
572 | head->wb_this_page = head; | |
573 | ||
574 | /* change head request to cover whole range that | |
575 | * the former page group covered */ | |
576 | head->wb_bytes = total_bytes; | |
e261f51f | 577 | } |
d4581383 | 578 | |
b66aaa8d TM |
579 | /* Postpone destruction of this request */ |
580 | if (test_and_clear_bit(PG_REMOVE, &head->wb_flags)) { | |
581 | set_bit(PG_INODE_REF, &head->wb_flags); | |
582 | kref_get(&head->wb_kref); | |
583 | NFS_I(inode)->nrequests++; | |
584 | } | |
585 | ||
d4581383 WAA |
586 | /* |
587 | * prepare head request to be added to new pgio descriptor | |
588 | */ | |
589 | nfs_page_group_clear_bits(head); | |
590 | ||
d4581383 WAA |
591 | nfs_page_group_unlock(head); |
592 | ||
411a99ad | 593 | /* drop lock to clean uprequests on destroy list */ |
587142f8 | 594 | spin_unlock(&inode->i_lock); |
d4581383 | 595 | |
b66aaa8d | 596 | nfs_destroy_unlinked_subrequests(destroy_list, head, inode); |
d4581383 | 597 | |
d4581383 WAA |
598 | /* still holds ref on head from nfs_page_find_head_request_locked |
599 | * and still has lock on head from lock loop */ | |
600 | return head; | |
074cc1de TM |
601 | } |
602 | ||
0bcbf039 PT |
603 | static void nfs_write_error_remove_page(struct nfs_page *req) |
604 | { | |
0bcbf039 | 605 | nfs_end_page_writeback(req); |
0bcbf039 PT |
606 | generic_error_remove_page(page_file_mapping(req->wb_page), |
607 | req->wb_page); | |
1f84ccdf | 608 | nfs_release_request(req); |
0bcbf039 PT |
609 | } |
610 | ||
a6598813 TM |
611 | static bool |
612 | nfs_error_is_fatal_on_server(int err) | |
613 | { | |
614 | switch (err) { | |
615 | case 0: | |
616 | case -ERESTARTSYS: | |
617 | case -EINTR: | |
618 | return false; | |
619 | } | |
620 | return nfs_error_is_fatal(err); | |
0bcbf039 PT |
621 | } |
622 | ||
074cc1de TM |
623 | /* |
624 | * Find an associated nfs write request, and prepare to flush it out | |
625 | * May return an error if the user signalled nfs_wait_on_request(). | |
626 | */ | |
627 | static int nfs_page_async_flush(struct nfs_pageio_descriptor *pgio, | |
6d17d653 | 628 | struct page *page) |
074cc1de TM |
629 | { |
630 | struct nfs_page *req; | |
631 | int ret = 0; | |
632 | ||
6d17d653 | 633 | req = nfs_lock_and_join_requests(page); |
074cc1de TM |
634 | if (!req) |
635 | goto out; | |
636 | ret = PTR_ERR(req); | |
637 | if (IS_ERR(req)) | |
638 | goto out; | |
639 | ||
deed85e7 TM |
640 | nfs_set_page_writeback(page); |
641 | WARN_ON_ONCE(test_bit(PG_CLEAN, &req->wb_flags)); | |
074cc1de | 642 | |
deed85e7 | 643 | ret = 0; |
a6598813 TM |
644 | /* If there is a fatal error that covers this write, just exit */ |
645 | if (nfs_error_is_fatal_on_server(req->wb_context->error)) | |
646 | goto out_launder; | |
647 | ||
f8512ad0 | 648 | if (!nfs_pageio_add_request(pgio, req)) { |
074cc1de | 649 | ret = pgio->pg_error; |
0bcbf039 | 650 | /* |
c373fff7 | 651 | * Remove the problematic req upon fatal errors on the server |
0bcbf039 PT |
652 | */ |
653 | if (nfs_error_is_fatal(ret)) { | |
654 | nfs_context_set_write_error(req->wb_context, ret); | |
c373fff7 | 655 | if (nfs_error_is_fatal_on_server(ret)) |
a6598813 | 656 | goto out_launder; |
0bcbf039 | 657 | } |
d6c843b9 PT |
658 | nfs_redirty_request(req); |
659 | ret = -EAGAIN; | |
40f90271 TM |
660 | } else |
661 | nfs_add_stats(page_file_mapping(page)->host, | |
662 | NFSIOS_WRITEPAGES, 1); | |
074cc1de TM |
663 | out: |
664 | return ret; | |
a6598813 TM |
665 | out_launder: |
666 | nfs_write_error_remove_page(req); | |
667 | return ret; | |
e261f51f TM |
668 | } |
669 | ||
d6c843b9 | 670 | static int nfs_do_writepage(struct page *page, struct writeback_control *wbc, |
c373fff7 | 671 | struct nfs_pageio_descriptor *pgio) |
1da177e4 | 672 | { |
cfb506e1 | 673 | int ret; |
1da177e4 | 674 | |
8cd79788 | 675 | nfs_pageio_cond_complete(pgio, page_index(page)); |
6d17d653 | 676 | ret = nfs_page_async_flush(pgio, page); |
cfb506e1 TM |
677 | if (ret == -EAGAIN) { |
678 | redirty_page_for_writepage(wbc, page); | |
679 | ret = 0; | |
680 | } | |
681 | return ret; | |
f758c885 | 682 | } |
7fe7f848 | 683 | |
f758c885 TM |
684 | /* |
685 | * Write an mmapped page to the server. | |
686 | */ | |
d6c843b9 | 687 | static int nfs_writepage_locked(struct page *page, |
c373fff7 | 688 | struct writeback_control *wbc) |
f758c885 TM |
689 | { |
690 | struct nfs_pageio_descriptor pgio; | |
40f90271 | 691 | struct inode *inode = page_file_mapping(page)->host; |
f758c885 | 692 | int err; |
49a70f27 | 693 | |
40f90271 | 694 | nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGE); |
811ed92e | 695 | nfs_pageio_init_write(&pgio, inode, 0, |
a20c93e3 | 696 | false, &nfs_async_write_completion_ops); |
c373fff7 | 697 | err = nfs_do_writepage(page, wbc, &pgio); |
f758c885 TM |
698 | nfs_pageio_complete(&pgio); |
699 | if (err < 0) | |
700 | return err; | |
701 | if (pgio.pg_error < 0) | |
702 | return pgio.pg_error; | |
703 | return 0; | |
4d770ccf TM |
704 | } |
705 | ||
706 | int nfs_writepage(struct page *page, struct writeback_control *wbc) | |
707 | { | |
f758c885 | 708 | int ret; |
4d770ccf | 709 | |
c373fff7 | 710 | ret = nfs_writepage_locked(page, wbc); |
1da177e4 | 711 | unlock_page(page); |
f758c885 TM |
712 | return ret; |
713 | } | |
714 | ||
715 | static int nfs_writepages_callback(struct page *page, struct writeback_control *wbc, void *data) | |
716 | { | |
717 | int ret; | |
718 | ||
c373fff7 | 719 | ret = nfs_do_writepage(page, wbc, data); |
f758c885 TM |
720 | unlock_page(page); |
721 | return ret; | |
1da177e4 LT |
722 | } |
723 | ||
919e3bd9 TM |
724 | static void nfs_io_completion_commit(void *inode) |
725 | { | |
726 | nfs_commit_inode(inode, 0); | |
727 | } | |
728 | ||
1da177e4 LT |
729 | int nfs_writepages(struct address_space *mapping, struct writeback_control *wbc) |
730 | { | |
1da177e4 | 731 | struct inode *inode = mapping->host; |
c63c7b05 | 732 | struct nfs_pageio_descriptor pgio; |
919e3bd9 | 733 | struct nfs_io_completion *ioc = nfs_io_completion_alloc(GFP_NOFS); |
1da177e4 LT |
734 | int err; |
735 | ||
91d5b470 CL |
736 | nfs_inc_stats(inode, NFSIOS_VFSWRITEPAGES); |
737 | ||
919e3bd9 TM |
738 | if (ioc) |
739 | nfs_io_completion_init(ioc, nfs_io_completion_commit, inode); | |
740 | ||
a20c93e3 CH |
741 | nfs_pageio_init_write(&pgio, inode, wb_priority(wbc), false, |
742 | &nfs_async_write_completion_ops); | |
919e3bd9 | 743 | pgio.pg_io_completion = ioc; |
f758c885 | 744 | err = write_cache_pages(mapping, wbc, nfs_writepages_callback, &pgio); |
c63c7b05 | 745 | nfs_pageio_complete(&pgio); |
919e3bd9 | 746 | nfs_io_completion_put(ioc); |
72cb77f4 | 747 | |
f758c885 | 748 | if (err < 0) |
72cb77f4 TM |
749 | goto out_err; |
750 | err = pgio.pg_error; | |
751 | if (err < 0) | |
752 | goto out_err; | |
c63c7b05 | 753 | return 0; |
72cb77f4 TM |
754 | out_err: |
755 | return err; | |
1da177e4 LT |
756 | } |
757 | ||
758 | /* | |
759 | * Insert a write request into an inode | |
760 | */ | |
d6d6dc7c | 761 | static void nfs_inode_add_request(struct inode *inode, struct nfs_page *req) |
1da177e4 LT |
762 | { |
763 | struct nfs_inode *nfsi = NFS_I(inode); | |
e7d39069 | 764 | |
2bfc6e56 WAA |
765 | WARN_ON_ONCE(req->wb_this_page != req); |
766 | ||
e7d39069 | 767 | /* Lock the request! */ |
7ad84aa9 | 768 | nfs_lock_request(req); |
e7d39069 TM |
769 | |
770 | spin_lock(&inode->i_lock); | |
cb1410c7 WAA |
771 | if (!nfsi->nrequests && |
772 | NFS_PROTO(inode)->have_delegation(inode, FMODE_WRITE)) | |
a9a4a87a | 773 | inode->i_version++; |
29418aa4 MG |
774 | /* |
775 | * Swap-space should not get truncated. Hence no need to plug the race | |
776 | * with invalidate/truncate. | |
777 | */ | |
778 | if (likely(!PageSwapCache(req->wb_page))) { | |
779 | set_bit(PG_MAPPED, &req->wb_flags); | |
780 | SetPagePrivate(req->wb_page); | |
781 | set_page_private(req->wb_page, (unsigned long)req); | |
782 | } | |
cb1410c7 | 783 | nfsi->nrequests++; |
17089a29 | 784 | /* this a head request for a page group - mark it as having an |
cb1410c7 WAA |
785 | * extra reference so sub groups can follow suit. |
786 | * This flag also informs pgio layer when to bump nrequests when | |
787 | * adding subrequests. */ | |
17089a29 | 788 | WARN_ON(test_and_set_bit(PG_INODE_REF, &req->wb_flags)); |
c03b4024 | 789 | kref_get(&req->wb_kref); |
e7d39069 | 790 | spin_unlock(&inode->i_lock); |
1da177e4 LT |
791 | } |
792 | ||
793 | /* | |
89a09141 | 794 | * Remove a write request from an inode |
1da177e4 LT |
795 | */ |
796 | static void nfs_inode_remove_request(struct nfs_page *req) | |
797 | { | |
2b0143b5 | 798 | struct inode *inode = d_inode(req->wb_context->dentry); |
1da177e4 | 799 | struct nfs_inode *nfsi = NFS_I(inode); |
20633f04 | 800 | struct nfs_page *head; |
1da177e4 | 801 | |
20633f04 WAA |
802 | if (nfs_page_group_sync_on_bit(req, PG_REMOVE)) { |
803 | head = req->wb_head; | |
804 | ||
805 | spin_lock(&inode->i_lock); | |
67911c8f | 806 | if (likely(head->wb_page && !PageSwapCache(head->wb_page))) { |
20633f04 WAA |
807 | set_page_private(head->wb_page, 0); |
808 | ClearPagePrivate(head->wb_page); | |
809 | clear_bit(PG_MAPPED, &head->wb_flags); | |
810 | } | |
cb1410c7 WAA |
811 | nfsi->nrequests--; |
812 | spin_unlock(&inode->i_lock); | |
813 | } else { | |
814 | spin_lock(&inode->i_lock); | |
815 | nfsi->nrequests--; | |
20633f04 | 816 | spin_unlock(&inode->i_lock); |
29418aa4 | 817 | } |
17089a29 WAA |
818 | |
819 | if (test_and_clear_bit(PG_INODE_REF, &req->wb_flags)) | |
820 | nfs_release_request(req); | |
1da177e4 LT |
821 | } |
822 | ||
61822ab5 | 823 | static void |
6d884e8f | 824 | nfs_mark_request_dirty(struct nfs_page *req) |
61822ab5 | 825 | { |
67911c8f AS |
826 | if (req->wb_page) |
827 | __set_page_dirty_nobuffers(req->wb_page); | |
61822ab5 TM |
828 | } |
829 | ||
3a3908c8 TM |
830 | /* |
831 | * nfs_page_search_commits_for_head_request_locked | |
832 | * | |
833 | * Search through commit lists on @inode for the head request for @page. | |
834 | * Must be called while holding the inode (which is cinfo) lock. | |
835 | * | |
836 | * Returns the head request if found, or NULL if not found. | |
837 | */ | |
838 | static struct nfs_page * | |
839 | nfs_page_search_commits_for_head_request_locked(struct nfs_inode *nfsi, | |
840 | struct page *page) | |
841 | { | |
842 | struct nfs_page *freq, *t; | |
843 | struct nfs_commit_info cinfo; | |
844 | struct inode *inode = &nfsi->vfs_inode; | |
845 | ||
846 | nfs_init_cinfo_from_inode(&cinfo, inode); | |
847 | ||
848 | /* search through pnfs commit lists */ | |
849 | freq = pnfs_search_commit_reqs(inode, &cinfo, page); | |
850 | if (freq) | |
851 | return freq->wb_head; | |
852 | ||
853 | /* Linearly search the commit list for the correct request */ | |
854 | list_for_each_entry_safe(freq, t, &cinfo.mds->list, wb_list) { | |
855 | if (freq->wb_page == page) | |
856 | return freq->wb_head; | |
857 | } | |
858 | ||
859 | return NULL; | |
860 | } | |
861 | ||
86d80f97 TM |
862 | /** |
863 | * nfs_request_add_commit_list_locked - add request to a commit list | |
864 | * @req: pointer to a struct nfs_page | |
865 | * @dst: commit list head | |
866 | * @cinfo: holds list lock and accounting info | |
867 | * | |
868 | * This sets the PG_CLEAN bit, updates the cinfo count of | |
869 | * number of outstanding requests requiring a commit as well as | |
870 | * the MM page stats. | |
871 | * | |
fe238e60 | 872 | * The caller must hold cinfo->inode->i_lock, and the nfs_page lock. |
86d80f97 TM |
873 | */ |
874 | void | |
875 | nfs_request_add_commit_list_locked(struct nfs_page *req, struct list_head *dst, | |
876 | struct nfs_commit_info *cinfo) | |
877 | { | |
878 | set_bit(PG_CLEAN, &req->wb_flags); | |
879 | nfs_list_add_request(req, dst); | |
880 | cinfo->mds->ncommit++; | |
881 | } | |
882 | EXPORT_SYMBOL_GPL(nfs_request_add_commit_list_locked); | |
883 | ||
8dd37758 TM |
884 | /** |
885 | * nfs_request_add_commit_list - add request to a commit list | |
886 | * @req: pointer to a struct nfs_page | |
ea2cf228 FI |
887 | * @dst: commit list head |
888 | * @cinfo: holds list lock and accounting info | |
8dd37758 | 889 | * |
ea2cf228 | 890 | * This sets the PG_CLEAN bit, updates the cinfo count of |
8dd37758 TM |
891 | * number of outstanding requests requiring a commit as well as |
892 | * the MM page stats. | |
893 | * | |
ea2cf228 | 894 | * The caller must _not_ hold the cinfo->lock, but must be |
8dd37758 | 895 | * holding the nfs_page lock. |
1da177e4 | 896 | */ |
8dd37758 | 897 | void |
6272dcc6 | 898 | nfs_request_add_commit_list(struct nfs_page *req, struct nfs_commit_info *cinfo) |
1da177e4 | 899 | { |
fe238e60 | 900 | spin_lock(&cinfo->inode->i_lock); |
6272dcc6 | 901 | nfs_request_add_commit_list_locked(req, &cinfo->mds->list, cinfo); |
fe238e60 | 902 | spin_unlock(&cinfo->inode->i_lock); |
67911c8f AS |
903 | if (req->wb_page) |
904 | nfs_mark_page_unstable(req->wb_page, cinfo); | |
1da177e4 | 905 | } |
8dd37758 TM |
906 | EXPORT_SYMBOL_GPL(nfs_request_add_commit_list); |
907 | ||
908 | /** | |
909 | * nfs_request_remove_commit_list - Remove request from a commit list | |
910 | * @req: pointer to a nfs_page | |
ea2cf228 | 911 | * @cinfo: holds list lock and accounting info |
8dd37758 | 912 | * |
ea2cf228 | 913 | * This clears the PG_CLEAN bit, and updates the cinfo's count of |
8dd37758 TM |
914 | * number of outstanding requests requiring a commit |
915 | * It does not update the MM page stats. | |
916 | * | |
ea2cf228 | 917 | * The caller _must_ hold the cinfo->lock and the nfs_page lock. |
8dd37758 TM |
918 | */ |
919 | void | |
ea2cf228 FI |
920 | nfs_request_remove_commit_list(struct nfs_page *req, |
921 | struct nfs_commit_info *cinfo) | |
8dd37758 | 922 | { |
8dd37758 TM |
923 | if (!test_and_clear_bit(PG_CLEAN, &(req)->wb_flags)) |
924 | return; | |
925 | nfs_list_remove_request(req); | |
ea2cf228 | 926 | cinfo->mds->ncommit--; |
8dd37758 TM |
927 | } |
928 | EXPORT_SYMBOL_GPL(nfs_request_remove_commit_list); | |
929 | ||
ea2cf228 FI |
930 | static void nfs_init_cinfo_from_inode(struct nfs_commit_info *cinfo, |
931 | struct inode *inode) | |
932 | { | |
fe238e60 | 933 | cinfo->inode = inode; |
ea2cf228 FI |
934 | cinfo->mds = &NFS_I(inode)->commit_info; |
935 | cinfo->ds = pnfs_get_ds_info(inode); | |
b359f9d0 | 936 | cinfo->dreq = NULL; |
f453a54a | 937 | cinfo->completion_ops = &nfs_commit_completion_ops; |
ea2cf228 FI |
938 | } |
939 | ||
940 | void nfs_init_cinfo(struct nfs_commit_info *cinfo, | |
941 | struct inode *inode, | |
942 | struct nfs_direct_req *dreq) | |
943 | { | |
1763da12 FI |
944 | if (dreq) |
945 | nfs_init_cinfo_from_dreq(cinfo, dreq); | |
946 | else | |
947 | nfs_init_cinfo_from_inode(cinfo, inode); | |
ea2cf228 FI |
948 | } |
949 | EXPORT_SYMBOL_GPL(nfs_init_cinfo); | |
8dd37758 TM |
950 | |
951 | /* | |
952 | * Add a request to the inode's commit list. | |
953 | */ | |
1763da12 | 954 | void |
ea2cf228 | 955 | nfs_mark_request_commit(struct nfs_page *req, struct pnfs_layout_segment *lseg, |
b57ff130 | 956 | struct nfs_commit_info *cinfo, u32 ds_commit_idx) |
8dd37758 | 957 | { |
b57ff130 | 958 | if (pnfs_mark_request_commit(req, lseg, cinfo, ds_commit_idx)) |
8dd37758 | 959 | return; |
6272dcc6 | 960 | nfs_request_add_commit_list(req, cinfo); |
8dd37758 | 961 | } |
8e821cad | 962 | |
d6d6dc7c FI |
963 | static void |
964 | nfs_clear_page_commit(struct page *page) | |
965 | { | |
11fb9989 | 966 | dec_node_page_state(page, NR_UNSTABLE_NFS); |
93f78d88 TH |
967 | dec_wb_stat(&inode_to_bdi(page_file_mapping(page)->host)->wb, |
968 | WB_RECLAIMABLE); | |
d6d6dc7c FI |
969 | } |
970 | ||
411a99ad | 971 | /* Called holding inode (/cinfo) lock */ |
8dd37758 | 972 | static void |
e468bae9 TM |
973 | nfs_clear_request_commit(struct nfs_page *req) |
974 | { | |
8dd37758 | 975 | if (test_bit(PG_CLEAN, &req->wb_flags)) { |
2b0143b5 | 976 | struct inode *inode = d_inode(req->wb_context->dentry); |
ea2cf228 | 977 | struct nfs_commit_info cinfo; |
e468bae9 | 978 | |
ea2cf228 FI |
979 | nfs_init_cinfo_from_inode(&cinfo, inode); |
980 | if (!pnfs_clear_request_commit(req, &cinfo)) { | |
ea2cf228 | 981 | nfs_request_remove_commit_list(req, &cinfo); |
8dd37758 | 982 | } |
d6d6dc7c | 983 | nfs_clear_page_commit(req->wb_page); |
e468bae9 | 984 | } |
e468bae9 TM |
985 | } |
986 | ||
d45f60c6 | 987 | int nfs_write_need_commit(struct nfs_pgio_header *hdr) |
8e821cad | 988 | { |
c65e6254 | 989 | if (hdr->verf.committed == NFS_DATA_SYNC) |
d45f60c6 | 990 | return hdr->lseg == NULL; |
c65e6254 | 991 | return hdr->verf.committed != NFS_FILE_SYNC; |
8e821cad TM |
992 | } |
993 | ||
919e3bd9 TM |
994 | static void nfs_async_write_init(struct nfs_pgio_header *hdr) |
995 | { | |
996 | nfs_io_completion_get(hdr->io_completion); | |
997 | } | |
998 | ||
061ae2ed | 999 | static void nfs_write_completion(struct nfs_pgio_header *hdr) |
8e821cad | 1000 | { |
ea2cf228 | 1001 | struct nfs_commit_info cinfo; |
6c75dc0d FI |
1002 | unsigned long bytes = 0; |
1003 | ||
1004 | if (test_bit(NFS_IOHDR_REDO, &hdr->flags)) | |
1005 | goto out; | |
ea2cf228 | 1006 | nfs_init_cinfo_from_inode(&cinfo, hdr->inode); |
6c75dc0d FI |
1007 | while (!list_empty(&hdr->pages)) { |
1008 | struct nfs_page *req = nfs_list_entry(hdr->pages.next); | |
6c75dc0d FI |
1009 | |
1010 | bytes += req->wb_bytes; | |
1011 | nfs_list_remove_request(req); | |
1012 | if (test_bit(NFS_IOHDR_ERROR, &hdr->flags) && | |
1013 | (hdr->good_bytes < bytes)) { | |
d1182b33 | 1014 | nfs_set_pageerror(req->wb_page); |
6c75dc0d FI |
1015 | nfs_context_set_write_error(req->wb_context, hdr->error); |
1016 | goto remove_req; | |
1017 | } | |
c65e6254 | 1018 | if (nfs_write_need_commit(hdr)) { |
f79d06f5 | 1019 | memcpy(&req->wb_verf, &hdr->verf.verifier, sizeof(req->wb_verf)); |
b57ff130 | 1020 | nfs_mark_request_commit(req, hdr->lseg, &cinfo, |
a7d42ddb | 1021 | hdr->pgio_mirror_idx); |
6c75dc0d FI |
1022 | goto next; |
1023 | } | |
1024 | remove_req: | |
1025 | nfs_inode_remove_request(req); | |
1026 | next: | |
20633f04 | 1027 | nfs_end_page_writeback(req); |
3aff4ebb | 1028 | nfs_release_request(req); |
6c75dc0d FI |
1029 | } |
1030 | out: | |
919e3bd9 | 1031 | nfs_io_completion_put(hdr->io_completion); |
6c75dc0d | 1032 | hdr->release(hdr); |
8e821cad | 1033 | } |
1da177e4 | 1034 | |
ce59515c | 1035 | unsigned long |
ea2cf228 | 1036 | nfs_reqs_to_commit(struct nfs_commit_info *cinfo) |
fb8a1f11 | 1037 | { |
ea2cf228 | 1038 | return cinfo->mds->ncommit; |
d6d6dc7c FI |
1039 | } |
1040 | ||
fe238e60 | 1041 | /* cinfo->inode->i_lock held by caller */ |
1763da12 | 1042 | int |
ea2cf228 FI |
1043 | nfs_scan_commit_list(struct list_head *src, struct list_head *dst, |
1044 | struct nfs_commit_info *cinfo, int max) | |
d6d6dc7c FI |
1045 | { |
1046 | struct nfs_page *req, *tmp; | |
1047 | int ret = 0; | |
1048 | ||
1049 | list_for_each_entry_safe(req, tmp, src, wb_list) { | |
8dd37758 TM |
1050 | if (!nfs_lock_request(req)) |
1051 | continue; | |
7ad84aa9 | 1052 | kref_get(&req->wb_kref); |
fe238e60 | 1053 | if (cond_resched_lock(&cinfo->inode->i_lock)) |
3b3be88d | 1054 | list_safe_reset_next(req, tmp, wb_list); |
ea2cf228 | 1055 | nfs_request_remove_commit_list(req, cinfo); |
8dd37758 TM |
1056 | nfs_list_add_request(req, dst); |
1057 | ret++; | |
1763da12 | 1058 | if ((ret == max) && !cinfo->dreq) |
8dd37758 | 1059 | break; |
d6d6dc7c FI |
1060 | } |
1061 | return ret; | |
fb8a1f11 TM |
1062 | } |
1063 | ||
1da177e4 LT |
1064 | /* |
1065 | * nfs_scan_commit - Scan an inode for commit requests | |
1066 | * @inode: NFS inode to scan | |
ea2cf228 FI |
1067 | * @dst: mds destination list |
1068 | * @cinfo: mds and ds lists of reqs ready to commit | |
1da177e4 LT |
1069 | * |
1070 | * Moves requests from the inode's 'commit' request list. | |
1071 | * The requests are *not* checked to ensure that they form a contiguous set. | |
1072 | */ | |
1763da12 | 1073 | int |
ea2cf228 FI |
1074 | nfs_scan_commit(struct inode *inode, struct list_head *dst, |
1075 | struct nfs_commit_info *cinfo) | |
1da177e4 | 1076 | { |
d6d6dc7c | 1077 | int ret = 0; |
fb8a1f11 | 1078 | |
fe238e60 | 1079 | spin_lock(&cinfo->inode->i_lock); |
ea2cf228 | 1080 | if (cinfo->mds->ncommit > 0) { |
8dd37758 | 1081 | const int max = INT_MAX; |
d6d6dc7c | 1082 | |
ea2cf228 FI |
1083 | ret = nfs_scan_commit_list(&cinfo->mds->list, dst, |
1084 | cinfo, max); | |
1085 | ret += pnfs_scan_commit_lists(inode, cinfo, max - ret); | |
d6d6dc7c | 1086 | } |
fe238e60 | 1087 | spin_unlock(&cinfo->inode->i_lock); |
ff778d02 | 1088 | return ret; |
1da177e4 | 1089 | } |
d6d6dc7c | 1090 | |
1da177e4 | 1091 | /* |
e7d39069 TM |
1092 | * Search for an existing write request, and attempt to update |
1093 | * it to reflect a new dirty region on a given page. | |
1da177e4 | 1094 | * |
e7d39069 TM |
1095 | * If the attempt fails, then the existing request is flushed out |
1096 | * to disk. | |
1da177e4 | 1097 | */ |
e7d39069 TM |
1098 | static struct nfs_page *nfs_try_to_update_request(struct inode *inode, |
1099 | struct page *page, | |
1100 | unsigned int offset, | |
1101 | unsigned int bytes) | |
1da177e4 | 1102 | { |
e7d39069 TM |
1103 | struct nfs_page *req; |
1104 | unsigned int rqend; | |
1105 | unsigned int end; | |
1106 | int error; | |
1107 | ||
1da177e4 LT |
1108 | end = offset + bytes; |
1109 | ||
1da177e4 | 1110 | for (;;) { |
1403390d TM |
1111 | if (!(PagePrivate(page) || PageSwapCache(page))) |
1112 | return NULL; | |
1113 | spin_lock(&inode->i_lock); | |
84d3a9a9 | 1114 | req = nfs_page_find_head_request_locked(NFS_I(inode), page); |
e7d39069 TM |
1115 | if (req == NULL) |
1116 | goto out_unlock; | |
1117 | ||
2bfc6e56 WAA |
1118 | /* should be handled by nfs_flush_incompatible */ |
1119 | WARN_ON_ONCE(req->wb_head != req); | |
1120 | WARN_ON_ONCE(req->wb_this_page != req); | |
1121 | ||
e7d39069 TM |
1122 | rqend = req->wb_offset + req->wb_bytes; |
1123 | /* | |
1124 | * Tell the caller to flush out the request if | |
1125 | * the offsets are non-contiguous. | |
1126 | * Note: nfs_flush_incompatible() will already | |
1127 | * have flushed out requests having wrong owners. | |
1128 | */ | |
e468bae9 | 1129 | if (offset > rqend |
e7d39069 TM |
1130 | || end < req->wb_offset) |
1131 | goto out_flushme; | |
1132 | ||
7ad84aa9 | 1133 | if (nfs_lock_request(req)) |
1da177e4 | 1134 | break; |
1da177e4 | 1135 | |
e7d39069 | 1136 | /* The request is locked, so wait and then retry */ |
587142f8 | 1137 | spin_unlock(&inode->i_lock); |
e7d39069 TM |
1138 | error = nfs_wait_on_request(req); |
1139 | nfs_release_request(req); | |
1140 | if (error != 0) | |
1141 | goto out_err; | |
1da177e4 LT |
1142 | } |
1143 | ||
1144 | /* Okay, the request matches. Update the region */ | |
1145 | if (offset < req->wb_offset) { | |
1146 | req->wb_offset = offset; | |
1147 | req->wb_pgbase = offset; | |
1da177e4 | 1148 | } |
1da177e4 LT |
1149 | if (end > rqend) |
1150 | req->wb_bytes = end - req->wb_offset; | |
e7d39069 TM |
1151 | else |
1152 | req->wb_bytes = rqend - req->wb_offset; | |
1153 | out_unlock: | |
ca138f36 FI |
1154 | if (req) |
1155 | nfs_clear_request_commit(req); | |
411a99ad | 1156 | spin_unlock(&inode->i_lock); |
e7d39069 TM |
1157 | return req; |
1158 | out_flushme: | |
1159 | spin_unlock(&inode->i_lock); | |
1160 | nfs_release_request(req); | |
1161 | error = nfs_wb_page(inode, page); | |
1162 | out_err: | |
1163 | return ERR_PTR(error); | |
1164 | } | |
1165 | ||
1166 | /* | |
1167 | * Try to update an existing write request, or create one if there is none. | |
1168 | * | |
1169 | * Note: Should always be called with the Page Lock held to prevent races | |
1170 | * if we have to add a new request. Also assumes that the caller has | |
1171 | * already called nfs_flush_incompatible() if necessary. | |
1172 | */ | |
1173 | static struct nfs_page * nfs_setup_write_request(struct nfs_open_context* ctx, | |
1174 | struct page *page, unsigned int offset, unsigned int bytes) | |
1175 | { | |
d56b4ddf | 1176 | struct inode *inode = page_file_mapping(page)->host; |
e7d39069 | 1177 | struct nfs_page *req; |
1da177e4 | 1178 | |
e7d39069 TM |
1179 | req = nfs_try_to_update_request(inode, page, offset, bytes); |
1180 | if (req != NULL) | |
1181 | goto out; | |
2bfc6e56 | 1182 | req = nfs_create_request(ctx, page, NULL, offset, bytes); |
e7d39069 TM |
1183 | if (IS_ERR(req)) |
1184 | goto out; | |
d6d6dc7c | 1185 | nfs_inode_add_request(inode, req); |
efc91ed0 | 1186 | out: |
61e930a9 | 1187 | return req; |
1da177e4 LT |
1188 | } |
1189 | ||
e7d39069 TM |
1190 | static int nfs_writepage_setup(struct nfs_open_context *ctx, struct page *page, |
1191 | unsigned int offset, unsigned int count) | |
1192 | { | |
1193 | struct nfs_page *req; | |
1194 | ||
1195 | req = nfs_setup_write_request(ctx, page, offset, count); | |
1196 | if (IS_ERR(req)) | |
1197 | return PTR_ERR(req); | |
1198 | /* Update file length */ | |
1199 | nfs_grow_file(page, offset, count); | |
d72ddcba | 1200 | nfs_mark_uptodate(req); |
a6305ddb | 1201 | nfs_mark_request_dirty(req); |
1d1afcbc | 1202 | nfs_unlock_and_release_request(req); |
e7d39069 TM |
1203 | return 0; |
1204 | } | |
1205 | ||
1da177e4 LT |
1206 | int nfs_flush_incompatible(struct file *file, struct page *page) |
1207 | { | |
cd3758e3 | 1208 | struct nfs_open_context *ctx = nfs_file_open_context(file); |
2a369153 | 1209 | struct nfs_lock_context *l_ctx; |
bd61e0a9 | 1210 | struct file_lock_context *flctx = file_inode(file)->i_flctx; |
1da177e4 | 1211 | struct nfs_page *req; |
1a54533e | 1212 | int do_flush, status; |
1da177e4 LT |
1213 | /* |
1214 | * Look for a request corresponding to this page. If there | |
1215 | * is one, and it belongs to another file, we flush it out | |
1216 | * before we try to copy anything into the page. Do this | |
1217 | * due to the lack of an ACCESS-type call in NFSv2. | |
1218 | * Also do the same if we find a request from an existing | |
1219 | * dropped page. | |
1220 | */ | |
1a54533e | 1221 | do { |
84d3a9a9 | 1222 | req = nfs_page_find_head_request(page); |
1a54533e TM |
1223 | if (req == NULL) |
1224 | return 0; | |
2a369153 | 1225 | l_ctx = req->wb_lock_context; |
138a2935 TM |
1226 | do_flush = req->wb_page != page || |
1227 | !nfs_match_open_context(req->wb_context, ctx); | |
2bfc6e56 WAA |
1228 | /* for now, flush if more than 1 request in page_group */ |
1229 | do_flush |= req->wb_this_page != req; | |
bd61e0a9 JL |
1230 | if (l_ctx && flctx && |
1231 | !(list_empty_careful(&flctx->flc_posix) && | |
1232 | list_empty_careful(&flctx->flc_flock))) { | |
d51fdb87 | 1233 | do_flush |= l_ctx->lockowner != current->files; |
5263e31e | 1234 | } |
1da177e4 | 1235 | nfs_release_request(req); |
1a54533e TM |
1236 | if (!do_flush) |
1237 | return 0; | |
d56b4ddf | 1238 | status = nfs_wb_page(page_file_mapping(page)->host, page); |
1a54533e TM |
1239 | } while (status == 0); |
1240 | return status; | |
1da177e4 LT |
1241 | } |
1242 | ||
dc24826b AA |
1243 | /* |
1244 | * Avoid buffered writes when a open context credential's key would | |
1245 | * expire soon. | |
1246 | * | |
1247 | * Returns -EACCES if the key will expire within RPC_KEY_EXPIRE_FAIL. | |
1248 | * | |
1249 | * Return 0 and set a credential flag which triggers the inode to flush | |
1250 | * and performs NFS_FILE_SYNC writes if the key will expired within | |
1251 | * RPC_KEY_EXPIRE_TIMEO. | |
1252 | */ | |
1253 | int | |
1254 | nfs_key_timeout_notify(struct file *filp, struct inode *inode) | |
1255 | { | |
1256 | struct nfs_open_context *ctx = nfs_file_open_context(filp); | |
1257 | struct rpc_auth *auth = NFS_SERVER(inode)->client->cl_auth; | |
1258 | ||
1259 | return rpcauth_key_timeout_notify(auth, ctx->cred); | |
1260 | } | |
1261 | ||
1262 | /* | |
1263 | * Test if the open context credential key is marked to expire soon. | |
1264 | */ | |
ce52914e | 1265 | bool nfs_ctx_key_to_expire(struct nfs_open_context *ctx, struct inode *inode) |
dc24826b | 1266 | { |
ce52914e SM |
1267 | struct rpc_auth *auth = NFS_SERVER(inode)->client->cl_auth; |
1268 | ||
1269 | return rpcauth_cred_key_to_expire(auth, ctx->cred); | |
dc24826b AA |
1270 | } |
1271 | ||
5d47a356 TM |
1272 | /* |
1273 | * If the page cache is marked as unsafe or invalid, then we can't rely on | |
1274 | * the PageUptodate() flag. In this case, we will need to turn off | |
1275 | * write optimisations that depend on the page contents being correct. | |
1276 | */ | |
8d197a56 | 1277 | static bool nfs_write_pageuptodate(struct page *page, struct inode *inode) |
5d47a356 | 1278 | { |
d529ef83 JL |
1279 | struct nfs_inode *nfsi = NFS_I(inode); |
1280 | ||
8d197a56 TM |
1281 | if (nfs_have_delegated_attributes(inode)) |
1282 | goto out; | |
18dd78c4 | 1283 | if (nfsi->cache_validity & NFS_INO_REVAL_PAGECACHE) |
d529ef83 | 1284 | return false; |
4db72b40 | 1285 | smp_rmb(); |
d529ef83 | 1286 | if (test_bit(NFS_INO_INVALIDATING, &nfsi->flags)) |
8d197a56 TM |
1287 | return false; |
1288 | out: | |
18dd78c4 SM |
1289 | if (nfsi->cache_validity & NFS_INO_INVALID_DATA) |
1290 | return false; | |
8d197a56 | 1291 | return PageUptodate(page) != 0; |
5d47a356 TM |
1292 | } |
1293 | ||
5263e31e JL |
1294 | static bool |
1295 | is_whole_file_wrlock(struct file_lock *fl) | |
1296 | { | |
1297 | return fl->fl_start == 0 && fl->fl_end == OFFSET_MAX && | |
1298 | fl->fl_type == F_WRLCK; | |
1299 | } | |
1300 | ||
c7559663 SM |
1301 | /* If we know the page is up to date, and we're not using byte range locks (or |
1302 | * if we have the whole file locked for writing), it may be more efficient to | |
1303 | * extend the write to cover the entire page in order to avoid fragmentation | |
1304 | * inefficiencies. | |
1305 | * | |
263b4509 SM |
1306 | * If the file is opened for synchronous writes then we can just skip the rest |
1307 | * of the checks. | |
c7559663 SM |
1308 | */ |
1309 | static int nfs_can_extend_write(struct file *file, struct page *page, struct inode *inode) | |
1310 | { | |
5263e31e JL |
1311 | int ret; |
1312 | struct file_lock_context *flctx = inode->i_flctx; | |
1313 | struct file_lock *fl; | |
1314 | ||
c7559663 SM |
1315 | if (file->f_flags & O_DSYNC) |
1316 | return 0; | |
263b4509 SM |
1317 | if (!nfs_write_pageuptodate(page, inode)) |
1318 | return 0; | |
c7559663 SM |
1319 | if (NFS_PROTO(inode)->have_delegation(inode, FMODE_WRITE)) |
1320 | return 1; | |
bd61e0a9 JL |
1321 | if (!flctx || (list_empty_careful(&flctx->flc_flock) && |
1322 | list_empty_careful(&flctx->flc_posix))) | |
8fa4592a | 1323 | return 1; |
5263e31e JL |
1324 | |
1325 | /* Check to see if there are whole file write locks */ | |
5263e31e | 1326 | ret = 0; |
6109c850 | 1327 | spin_lock(&flctx->flc_lock); |
bd61e0a9 JL |
1328 | if (!list_empty(&flctx->flc_posix)) { |
1329 | fl = list_first_entry(&flctx->flc_posix, struct file_lock, | |
1330 | fl_list); | |
1331 | if (is_whole_file_wrlock(fl)) | |
1332 | ret = 1; | |
1333 | } else if (!list_empty(&flctx->flc_flock)) { | |
5263e31e JL |
1334 | fl = list_first_entry(&flctx->flc_flock, struct file_lock, |
1335 | fl_list); | |
1336 | if (fl->fl_type == F_WRLCK) | |
1337 | ret = 1; | |
1338 | } | |
6109c850 | 1339 | spin_unlock(&flctx->flc_lock); |
5263e31e | 1340 | return ret; |
c7559663 SM |
1341 | } |
1342 | ||
1da177e4 LT |
1343 | /* |
1344 | * Update and possibly write a cached page of an NFS file. | |
1345 | * | |
1346 | * XXX: Keep an eye on generic_file_read to make sure it doesn't do bad | |
1347 | * things with a page scheduled for an RPC call (e.g. invalidate it). | |
1348 | */ | |
1349 | int nfs_updatepage(struct file *file, struct page *page, | |
1350 | unsigned int offset, unsigned int count) | |
1351 | { | |
cd3758e3 | 1352 | struct nfs_open_context *ctx = nfs_file_open_context(file); |
d56b4ddf | 1353 | struct inode *inode = page_file_mapping(page)->host; |
1da177e4 LT |
1354 | int status = 0; |
1355 | ||
91d5b470 CL |
1356 | nfs_inc_stats(inode, NFSIOS_VFSUPDATEPAGE); |
1357 | ||
6de1472f AV |
1358 | dprintk("NFS: nfs_updatepage(%pD2 %d@%lld)\n", |
1359 | file, count, (long long)(page_file_offset(page) + offset)); | |
1da177e4 | 1360 | |
149a4fdd BC |
1361 | if (!count) |
1362 | goto out; | |
1363 | ||
c7559663 | 1364 | if (nfs_can_extend_write(file, page, inode)) { |
49a70f27 | 1365 | count = max(count + offset, nfs_page_length(page)); |
1da177e4 | 1366 | offset = 0; |
1da177e4 LT |
1367 | } |
1368 | ||
e21195a7 | 1369 | status = nfs_writepage_setup(ctx, page, offset, count); |
03fa9e84 TM |
1370 | if (status < 0) |
1371 | nfs_set_pageerror(page); | |
59b7c05f TM |
1372 | else |
1373 | __set_page_dirty_nobuffers(page); | |
149a4fdd | 1374 | out: |
48186c7d | 1375 | dprintk("NFS: nfs_updatepage returns %d (isize %lld)\n", |
1da177e4 | 1376 | status, (long long)i_size_read(inode)); |
1da177e4 LT |
1377 | return status; |
1378 | } | |
1379 | ||
3ff7576d | 1380 | static int flush_task_priority(int how) |
1da177e4 LT |
1381 | { |
1382 | switch (how & (FLUSH_HIGHPRI|FLUSH_LOWPRI)) { | |
1383 | case FLUSH_HIGHPRI: | |
1384 | return RPC_PRIORITY_HIGH; | |
1385 | case FLUSH_LOWPRI: | |
1386 | return RPC_PRIORITY_LOW; | |
1387 | } | |
1388 | return RPC_PRIORITY_NORMAL; | |
1389 | } | |
1390 | ||
d45f60c6 WAA |
1391 | static void nfs_initiate_write(struct nfs_pgio_header *hdr, |
1392 | struct rpc_message *msg, | |
abde71f4 | 1393 | const struct nfs_rpc_ops *rpc_ops, |
1ed26f33 | 1394 | struct rpc_task_setup *task_setup_data, int how) |
1da177e4 | 1395 | { |
3ff7576d | 1396 | int priority = flush_task_priority(how); |
d138d5d1 | 1397 | |
1ed26f33 | 1398 | task_setup_data->priority = priority; |
abde71f4 | 1399 | rpc_ops->write_setup(hdr, msg); |
d138d5d1 | 1400 | |
abde71f4 | 1401 | nfs4_state_protect_write(NFS_SERVER(hdr->inode)->nfs_client, |
d45f60c6 | 1402 | &task_setup_data->rpc_client, msg, hdr); |
275acaaf TM |
1403 | } |
1404 | ||
6d884e8f F |
1405 | /* If a nfs_flush_* function fails, it should remove reqs from @head and |
1406 | * call this on each, which will prepare them to be retried on next | |
1407 | * writeback using standard nfs. | |
1408 | */ | |
1409 | static void nfs_redirty_request(struct nfs_page *req) | |
1410 | { | |
1411 | nfs_mark_request_dirty(req); | |
c7070113 | 1412 | set_bit(NFS_CONTEXT_RESEND_WRITES, &req->wb_context->flags); |
20633f04 | 1413 | nfs_end_page_writeback(req); |
3aff4ebb | 1414 | nfs_release_request(req); |
6d884e8f F |
1415 | } |
1416 | ||
061ae2ed | 1417 | static void nfs_async_write_error(struct list_head *head) |
6c75dc0d FI |
1418 | { |
1419 | struct nfs_page *req; | |
1420 | ||
1421 | while (!list_empty(head)) { | |
1422 | req = nfs_list_entry(head->next); | |
1423 | nfs_list_remove_request(req); | |
1424 | nfs_redirty_request(req); | |
1425 | } | |
1426 | } | |
1427 | ||
dc602dd7 TM |
1428 | static void nfs_async_write_reschedule_io(struct nfs_pgio_header *hdr) |
1429 | { | |
1430 | nfs_async_write_error(&hdr->pages); | |
1431 | } | |
1432 | ||
061ae2ed | 1433 | static const struct nfs_pgio_completion_ops nfs_async_write_completion_ops = { |
919e3bd9 | 1434 | .init_hdr = nfs_async_write_init, |
061ae2ed FI |
1435 | .error_cleanup = nfs_async_write_error, |
1436 | .completion = nfs_write_completion, | |
dc602dd7 | 1437 | .reschedule_io = nfs_async_write_reschedule_io, |
061ae2ed FI |
1438 | }; |
1439 | ||
57208fa7 | 1440 | void nfs_pageio_init_write(struct nfs_pageio_descriptor *pgio, |
a20c93e3 | 1441 | struct inode *inode, int ioflags, bool force_mds, |
061ae2ed | 1442 | const struct nfs_pgio_completion_ops *compl_ops) |
1da177e4 | 1443 | { |
a20c93e3 | 1444 | struct nfs_server *server = NFS_SERVER(inode); |
41d8d5b7 | 1445 | const struct nfs_pageio_ops *pg_ops = &nfs_pgio_rw_ops; |
a20c93e3 CH |
1446 | |
1447 | #ifdef CONFIG_NFS_V4_1 | |
1448 | if (server->pnfs_curr_ld && !force_mds) | |
1449 | pg_ops = server->pnfs_curr_ld->pg_write_ops; | |
1450 | #endif | |
4a0de55c | 1451 | nfs_pageio_init(pgio, inode, pg_ops, compl_ops, &nfs_rw_write_ops, |
fbe77c30 | 1452 | server->wsize, ioflags, GFP_NOIO); |
1751c363 | 1453 | } |
ddda8e0a | 1454 | EXPORT_SYMBOL_GPL(nfs_pageio_init_write); |
1da177e4 | 1455 | |
dce81290 TM |
1456 | void nfs_pageio_reset_write_mds(struct nfs_pageio_descriptor *pgio) |
1457 | { | |
a7d42ddb WAA |
1458 | struct nfs_pgio_mirror *mirror; |
1459 | ||
6f29b9bb KM |
1460 | if (pgio->pg_ops && pgio->pg_ops->pg_cleanup) |
1461 | pgio->pg_ops->pg_cleanup(pgio); | |
1462 | ||
41d8d5b7 | 1463 | pgio->pg_ops = &nfs_pgio_rw_ops; |
a7d42ddb WAA |
1464 | |
1465 | nfs_pageio_stop_mirroring(pgio); | |
1466 | ||
1467 | mirror = &pgio->pg_mirrors[0]; | |
1468 | mirror->pg_bsize = NFS_SERVER(pgio->pg_inode)->wsize; | |
dce81290 | 1469 | } |
1f945357 | 1470 | EXPORT_SYMBOL_GPL(nfs_pageio_reset_write_mds); |
dce81290 | 1471 | |
1da177e4 | 1472 | |
0b7c0153 FI |
1473 | void nfs_commit_prepare(struct rpc_task *task, void *calldata) |
1474 | { | |
1475 | struct nfs_commit_data *data = calldata; | |
1476 | ||
1477 | NFS_PROTO(data->inode)->commit_rpc_prepare(task, data); | |
1478 | } | |
1479 | ||
1f2edbe3 TM |
1480 | /* |
1481 | * Special version of should_remove_suid() that ignores capabilities. | |
1482 | */ | |
1483 | static int nfs_should_remove_suid(const struct inode *inode) | |
1484 | { | |
1485 | umode_t mode = inode->i_mode; | |
1486 | int kill = 0; | |
1487 | ||
1488 | /* suid always must be killed */ | |
1489 | if (unlikely(mode & S_ISUID)) | |
1490 | kill = ATTR_KILL_SUID; | |
788e7a89 | 1491 | |
1f2edbe3 TM |
1492 | /* |
1493 | * sgid without any exec bits is just a mandatory locking mark; leave | |
1494 | * it alone. If some exec bits are set, it's a real sgid; kill it. | |
1495 | */ | |
1496 | if (unlikely((mode & S_ISGID) && (mode & S_IXGRP))) | |
1497 | kill |= ATTR_KILL_SGID; | |
1498 | ||
1499 | if (unlikely(kill && S_ISREG(mode))) | |
1500 | return kill; | |
1501 | ||
1502 | return 0; | |
1503 | } | |
788e7a89 | 1504 | |
a08a8cd3 TM |
1505 | static void nfs_writeback_check_extend(struct nfs_pgio_header *hdr, |
1506 | struct nfs_fattr *fattr) | |
1507 | { | |
1508 | struct nfs_pgio_args *argp = &hdr->args; | |
1509 | struct nfs_pgio_res *resp = &hdr->res; | |
2b83d3de | 1510 | u64 size = argp->offset + resp->count; |
a08a8cd3 TM |
1511 | |
1512 | if (!(fattr->valid & NFS_ATTR_FATTR_SIZE)) | |
2b83d3de TM |
1513 | fattr->size = size; |
1514 | if (nfs_size_to_loff_t(fattr->size) < i_size_read(hdr->inode)) { | |
1515 | fattr->valid &= ~NFS_ATTR_FATTR_SIZE; | |
a08a8cd3 | 1516 | return; |
2b83d3de TM |
1517 | } |
1518 | if (size != fattr->size) | |
a08a8cd3 TM |
1519 | return; |
1520 | /* Set attribute barrier */ | |
1521 | nfs_fattr_set_barrier(fattr); | |
2b83d3de TM |
1522 | /* ...and update size */ |
1523 | fattr->valid |= NFS_ATTR_FATTR_SIZE; | |
a08a8cd3 TM |
1524 | } |
1525 | ||
1526 | void nfs_writeback_update_inode(struct nfs_pgio_header *hdr) | |
1527 | { | |
2b83d3de | 1528 | struct nfs_fattr *fattr = &hdr->fattr; |
a08a8cd3 TM |
1529 | struct inode *inode = hdr->inode; |
1530 | ||
a08a8cd3 TM |
1531 | spin_lock(&inode->i_lock); |
1532 | nfs_writeback_check_extend(hdr, fattr); | |
1533 | nfs_post_op_update_inode_force_wcc_locked(inode, fattr); | |
1534 | spin_unlock(&inode->i_lock); | |
1535 | } | |
1536 | EXPORT_SYMBOL_GPL(nfs_writeback_update_inode); | |
1537 | ||
1da177e4 LT |
1538 | /* |
1539 | * This function is called when the WRITE call is complete. | |
1540 | */ | |
d45f60c6 WAA |
1541 | static int nfs_writeback_done(struct rpc_task *task, |
1542 | struct nfs_pgio_header *hdr, | |
0eecb214 | 1543 | struct inode *inode) |
1da177e4 | 1544 | { |
788e7a89 | 1545 | int status; |
1da177e4 | 1546 | |
f551e44f CL |
1547 | /* |
1548 | * ->write_done will attempt to use post-op attributes to detect | |
1549 | * conflicting writes by other clients. A strict interpretation | |
1550 | * of close-to-open would allow us to continue caching even if | |
1551 | * another writer had changed the file, but some applications | |
1552 | * depend on tighter cache coherency when writing. | |
1553 | */ | |
d45f60c6 | 1554 | status = NFS_PROTO(inode)->write_done(task, hdr); |
788e7a89 | 1555 | if (status != 0) |
0eecb214 | 1556 | return status; |
d45f60c6 | 1557 | nfs_add_stats(inode, NFSIOS_SERVERWRITTENBYTES, hdr->res.count); |
91d5b470 | 1558 | |
d45f60c6 WAA |
1559 | if (hdr->res.verf->committed < hdr->args.stable && |
1560 | task->tk_status >= 0) { | |
1da177e4 LT |
1561 | /* We tried a write call, but the server did not |
1562 | * commit data to stable storage even though we | |
1563 | * requested it. | |
1564 | * Note: There is a known bug in Tru64 < 5.0 in which | |
1565 | * the server reports NFS_DATA_SYNC, but performs | |
1566 | * NFS_FILE_SYNC. We therefore implement this checking | |
1567 | * as a dprintk() in order to avoid filling syslog. | |
1568 | */ | |
1569 | static unsigned long complain; | |
1570 | ||
a69aef14 | 1571 | /* Note this will print the MDS for a DS write */ |
1da177e4 | 1572 | if (time_before(complain, jiffies)) { |
48186c7d | 1573 | dprintk("NFS: faulty NFS server %s:" |
1da177e4 | 1574 | " (committed = %d) != (stable = %d)\n", |
cd841605 | 1575 | NFS_SERVER(inode)->nfs_client->cl_hostname, |
d45f60c6 | 1576 | hdr->res.verf->committed, hdr->args.stable); |
1da177e4 LT |
1577 | complain = jiffies + 300 * HZ; |
1578 | } | |
1579 | } | |
1f2edbe3 TM |
1580 | |
1581 | /* Deal with the suid/sgid bit corner case */ | |
1582 | if (nfs_should_remove_suid(inode)) | |
1583 | nfs_mark_for_revalidate(inode); | |
0eecb214 AS |
1584 | return 0; |
1585 | } | |
1586 | ||
1587 | /* | |
1588 | * This function is called when the WRITE call is complete. | |
1589 | */ | |
d45f60c6 WAA |
1590 | static void nfs_writeback_result(struct rpc_task *task, |
1591 | struct nfs_pgio_header *hdr) | |
0eecb214 | 1592 | { |
d45f60c6 WAA |
1593 | struct nfs_pgio_args *argp = &hdr->args; |
1594 | struct nfs_pgio_res *resp = &hdr->res; | |
1f2edbe3 TM |
1595 | |
1596 | if (resp->count < argp->count) { | |
1da177e4 LT |
1597 | static unsigned long complain; |
1598 | ||
6c75dc0d | 1599 | /* This a short write! */ |
d45f60c6 | 1600 | nfs_inc_stats(hdr->inode, NFSIOS_SHORTWRITE); |
91d5b470 | 1601 | |
1da177e4 | 1602 | /* Has the server at least made some progress? */ |
6c75dc0d FI |
1603 | if (resp->count == 0) { |
1604 | if (time_before(complain, jiffies)) { | |
1605 | printk(KERN_WARNING | |
1606 | "NFS: Server wrote zero bytes, expected %u.\n", | |
1607 | argp->count); | |
1608 | complain = jiffies + 300 * HZ; | |
1da177e4 | 1609 | } |
d45f60c6 | 1610 | nfs_set_pgio_error(hdr, -EIO, argp->offset); |
6c75dc0d | 1611 | task->tk_status = -EIO; |
13602896 | 1612 | return; |
1da177e4 | 1613 | } |
f8417b48 KM |
1614 | |
1615 | /* For non rpc-based layout drivers, retry-through-MDS */ | |
1616 | if (!task->tk_ops) { | |
1617 | hdr->pnfs_error = -EAGAIN; | |
1618 | return; | |
1619 | } | |
1620 | ||
6c75dc0d FI |
1621 | /* Was this an NFSv2 write or an NFSv3 stable write? */ |
1622 | if (resp->verf->committed != NFS_UNSTABLE) { | |
1623 | /* Resend from where the server left off */ | |
d45f60c6 | 1624 | hdr->mds_offset += resp->count; |
6c75dc0d FI |
1625 | argp->offset += resp->count; |
1626 | argp->pgbase += resp->count; | |
1627 | argp->count -= resp->count; | |
1628 | } else { | |
1629 | /* Resend as a stable write in order to avoid | |
1630 | * headaches in the case of a server crash. | |
1631 | */ | |
1632 | argp->stable = NFS_FILE_SYNC; | |
1da177e4 | 1633 | } |
6c75dc0d | 1634 | rpc_restart_call_prepare(task); |
1da177e4 | 1635 | } |
1da177e4 LT |
1636 | } |
1637 | ||
af7cf057 | 1638 | static int wait_on_commit(struct nfs_mds_commit_info *cinfo) |
71d0a611 | 1639 | { |
af7cf057 TM |
1640 | return wait_on_atomic_t(&cinfo->rpcs_out, |
1641 | nfs_wait_atomic_killable, TASK_KILLABLE); | |
1642 | } | |
b8413f98 | 1643 | |
af7cf057 TM |
1644 | static void nfs_commit_begin(struct nfs_mds_commit_info *cinfo) |
1645 | { | |
1646 | atomic_inc(&cinfo->rpcs_out); | |
71d0a611 TM |
1647 | } |
1648 | ||
af7cf057 | 1649 | static void nfs_commit_end(struct nfs_mds_commit_info *cinfo) |
71d0a611 | 1650 | { |
af7cf057 TM |
1651 | if (atomic_dec_and_test(&cinfo->rpcs_out)) |
1652 | wake_up_atomic_t(&cinfo->rpcs_out); | |
71d0a611 TM |
1653 | } |
1654 | ||
0b7c0153 | 1655 | void nfs_commitdata_release(struct nfs_commit_data *data) |
1da177e4 | 1656 | { |
0b7c0153 FI |
1657 | put_nfs_open_context(data->context); |
1658 | nfs_commit_free(data); | |
1da177e4 | 1659 | } |
e0c2b380 | 1660 | EXPORT_SYMBOL_GPL(nfs_commitdata_release); |
1da177e4 | 1661 | |
0b7c0153 | 1662 | int nfs_initiate_commit(struct rpc_clnt *clnt, struct nfs_commit_data *data, |
c36aae9a | 1663 | const struct nfs_rpc_ops *nfs_ops, |
9ace33cd | 1664 | const struct rpc_call_ops *call_ops, |
9f0ec176 | 1665 | int how, int flags) |
1da177e4 | 1666 | { |
07737691 | 1667 | struct rpc_task *task; |
9ace33cd | 1668 | int priority = flush_task_priority(how); |
bdc7f021 TM |
1669 | struct rpc_message msg = { |
1670 | .rpc_argp = &data->args, | |
1671 | .rpc_resp = &data->res, | |
9ace33cd | 1672 | .rpc_cred = data->cred, |
bdc7f021 | 1673 | }; |
84115e1c | 1674 | struct rpc_task_setup task_setup_data = { |
07737691 | 1675 | .task = &data->task, |
9ace33cd | 1676 | .rpc_client = clnt, |
bdc7f021 | 1677 | .rpc_message = &msg, |
9ace33cd | 1678 | .callback_ops = call_ops, |
84115e1c | 1679 | .callback_data = data, |
101070ca | 1680 | .workqueue = nfsiod_workqueue, |
9f0ec176 | 1681 | .flags = RPC_TASK_ASYNC | flags, |
3ff7576d | 1682 | .priority = priority, |
84115e1c | 1683 | }; |
9ace33cd | 1684 | /* Set up the initial task struct. */ |
c36aae9a | 1685 | nfs_ops->commit_setup(data, &msg); |
9ace33cd | 1686 | |
b4839ebe | 1687 | dprintk("NFS: initiated commit call\n"); |
9ace33cd | 1688 | |
8c21c62c WAA |
1689 | nfs4_state_protect(NFS_SERVER(data->inode)->nfs_client, |
1690 | NFS_SP4_MACH_CRED_COMMIT, &task_setup_data.rpc_client, &msg); | |
1691 | ||
9ace33cd FI |
1692 | task = rpc_run_task(&task_setup_data); |
1693 | if (IS_ERR(task)) | |
1694 | return PTR_ERR(task); | |
1695 | if (how & FLUSH_SYNC) | |
1696 | rpc_wait_for_completion_task(task); | |
1697 | rpc_put_task(task); | |
1698 | return 0; | |
1699 | } | |
e0c2b380 | 1700 | EXPORT_SYMBOL_GPL(nfs_initiate_commit); |
9ace33cd | 1701 | |
378520b8 PT |
1702 | static loff_t nfs_get_lwb(struct list_head *head) |
1703 | { | |
1704 | loff_t lwb = 0; | |
1705 | struct nfs_page *req; | |
1706 | ||
1707 | list_for_each_entry(req, head, wb_list) | |
1708 | if (lwb < (req_offset(req) + req->wb_bytes)) | |
1709 | lwb = req_offset(req) + req->wb_bytes; | |
1710 | ||
1711 | return lwb; | |
1712 | } | |
1713 | ||
9ace33cd FI |
1714 | /* |
1715 | * Set up the argument/result storage required for the RPC call. | |
1716 | */ | |
0b7c0153 | 1717 | void nfs_init_commit(struct nfs_commit_data *data, |
f453a54a FI |
1718 | struct list_head *head, |
1719 | struct pnfs_layout_segment *lseg, | |
1720 | struct nfs_commit_info *cinfo) | |
9ace33cd FI |
1721 | { |
1722 | struct nfs_page *first = nfs_list_entry(head->next); | |
2b0143b5 | 1723 | struct inode *inode = d_inode(first->wb_context->dentry); |
1da177e4 LT |
1724 | |
1725 | /* Set up the RPC argument and reply structs | |
1726 | * NB: take care not to mess about with data->commit et al. */ | |
1727 | ||
1728 | list_splice_init(head, &data->pages); | |
1da177e4 | 1729 | |
1da177e4 | 1730 | data->inode = inode; |
9ace33cd | 1731 | data->cred = first->wb_context->cred; |
988b6dce | 1732 | data->lseg = lseg; /* reference transferred */ |
378520b8 PT |
1733 | /* only set lwb for pnfs commit */ |
1734 | if (lseg) | |
1735 | data->lwb = nfs_get_lwb(&data->pages); | |
9ace33cd | 1736 | data->mds_ops = &nfs_commit_ops; |
f453a54a | 1737 | data->completion_ops = cinfo->completion_ops; |
b359f9d0 | 1738 | data->dreq = cinfo->dreq; |
1da177e4 LT |
1739 | |
1740 | data->args.fh = NFS_FH(data->inode); | |
3da28eb1 TM |
1741 | /* Note: we always request a commit of the entire inode */ |
1742 | data->args.offset = 0; | |
1743 | data->args.count = 0; | |
0b7c0153 | 1744 | data->context = get_nfs_open_context(first->wb_context); |
1da177e4 LT |
1745 | data->res.fattr = &data->fattr; |
1746 | data->res.verf = &data->verf; | |
0e574af1 | 1747 | nfs_fattr_init(&data->fattr); |
1da177e4 | 1748 | } |
e0c2b380 | 1749 | EXPORT_SYMBOL_GPL(nfs_init_commit); |
1da177e4 | 1750 | |
e0c2b380 | 1751 | void nfs_retry_commit(struct list_head *page_list, |
ea2cf228 | 1752 | struct pnfs_layout_segment *lseg, |
b57ff130 WAA |
1753 | struct nfs_commit_info *cinfo, |
1754 | u32 ds_commit_idx) | |
64bfeb49 FI |
1755 | { |
1756 | struct nfs_page *req; | |
1757 | ||
1758 | while (!list_empty(page_list)) { | |
1759 | req = nfs_list_entry(page_list->next); | |
1760 | nfs_list_remove_request(req); | |
b57ff130 | 1761 | nfs_mark_request_commit(req, lseg, cinfo, ds_commit_idx); |
487b9b8a TH |
1762 | if (!cinfo->dreq) |
1763 | nfs_clear_page_commit(req->wb_page); | |
1d1afcbc | 1764 | nfs_unlock_and_release_request(req); |
64bfeb49 FI |
1765 | } |
1766 | } | |
e0c2b380 | 1767 | EXPORT_SYMBOL_GPL(nfs_retry_commit); |
64bfeb49 | 1768 | |
b20135d0 TM |
1769 | static void |
1770 | nfs_commit_resched_write(struct nfs_commit_info *cinfo, | |
1771 | struct nfs_page *req) | |
1772 | { | |
1773 | __set_page_dirty_nobuffers(req->wb_page); | |
1774 | } | |
1775 | ||
1da177e4 LT |
1776 | /* |
1777 | * Commit dirty pages | |
1778 | */ | |
1779 | static int | |
ea2cf228 FI |
1780 | nfs_commit_list(struct inode *inode, struct list_head *head, int how, |
1781 | struct nfs_commit_info *cinfo) | |
1da177e4 | 1782 | { |
0b7c0153 | 1783 | struct nfs_commit_data *data; |
1da177e4 | 1784 | |
ade8febd WAA |
1785 | /* another commit raced with us */ |
1786 | if (list_empty(head)) | |
1787 | return 0; | |
1788 | ||
518662e0 | 1789 | data = nfs_commitdata_alloc(true); |
1da177e4 LT |
1790 | |
1791 | /* Set up the argument struct */ | |
f453a54a FI |
1792 | nfs_init_commit(data, head, NULL, cinfo); |
1793 | atomic_inc(&cinfo->mds->rpcs_out); | |
c36aae9a PT |
1794 | return nfs_initiate_commit(NFS_CLIENT(inode), data, NFS_PROTO(inode), |
1795 | data->mds_ops, how, 0); | |
67911c8f | 1796 | } |
67911c8f | 1797 | |
1da177e4 LT |
1798 | /* |
1799 | * COMMIT call returned | |
1800 | */ | |
788e7a89 | 1801 | static void nfs_commit_done(struct rpc_task *task, void *calldata) |
1da177e4 | 1802 | { |
0b7c0153 | 1803 | struct nfs_commit_data *data = calldata; |
1da177e4 | 1804 | |
a3f565b1 | 1805 | dprintk("NFS: %5u nfs_commit_done (status %d)\n", |
1da177e4 LT |
1806 | task->tk_pid, task->tk_status); |
1807 | ||
788e7a89 | 1808 | /* Call the NFS version-specific code */ |
c0d0e96b | 1809 | NFS_PROTO(data->inode)->commit_done(task, data); |
c9d8f89d TM |
1810 | } |
1811 | ||
f453a54a | 1812 | static void nfs_commit_release_pages(struct nfs_commit_data *data) |
c9d8f89d | 1813 | { |
5917ce84 | 1814 | struct nfs_page *req; |
c9d8f89d | 1815 | int status = data->task.tk_status; |
f453a54a | 1816 | struct nfs_commit_info cinfo; |
353db796 | 1817 | struct nfs_server *nfss; |
788e7a89 | 1818 | |
1da177e4 LT |
1819 | while (!list_empty(&data->pages)) { |
1820 | req = nfs_list_entry(data->pages.next); | |
1821 | nfs_list_remove_request(req); | |
67911c8f AS |
1822 | if (req->wb_page) |
1823 | nfs_clear_page_commit(req->wb_page); | |
1da177e4 | 1824 | |
1e8968c5 | 1825 | dprintk("NFS: commit (%s/%llu %d@%lld)", |
3d4ff43d | 1826 | req->wb_context->dentry->d_sb->s_id, |
2b0143b5 | 1827 | (unsigned long long)NFS_FILEID(d_inode(req->wb_context->dentry)), |
1da177e4 LT |
1828 | req->wb_bytes, |
1829 | (long long)req_offset(req)); | |
c9d8f89d TM |
1830 | if (status < 0) { |
1831 | nfs_context_set_write_error(req->wb_context, status); | |
38a33101 KM |
1832 | if (req->wb_page) |
1833 | nfs_inode_remove_request(req); | |
ddeaa637 | 1834 | dprintk_cont(", error = %d\n", status); |
1da177e4 LT |
1835 | goto next; |
1836 | } | |
1837 | ||
1838 | /* Okay, COMMIT succeeded, apparently. Check the verifier | |
1839 | * returned by the server against all stored verfs. */ | |
8fc3c386 | 1840 | if (!nfs_write_verifier_cmp(&req->wb_verf, &data->verf.verifier)) { |
1da177e4 | 1841 | /* We have a match */ |
38a33101 KM |
1842 | if (req->wb_page) |
1843 | nfs_inode_remove_request(req); | |
ddeaa637 | 1844 | dprintk_cont(" OK\n"); |
1da177e4 LT |
1845 | goto next; |
1846 | } | |
1847 | /* We have a mismatch. Write the page again */ | |
ddeaa637 | 1848 | dprintk_cont(" mismatch\n"); |
6d884e8f | 1849 | nfs_mark_request_dirty(req); |
05990d1b | 1850 | set_bit(NFS_CONTEXT_RESEND_WRITES, &req->wb_context->flags); |
1da177e4 | 1851 | next: |
1d1afcbc | 1852 | nfs_unlock_and_release_request(req); |
1da177e4 | 1853 | } |
353db796 N |
1854 | nfss = NFS_SERVER(data->inode); |
1855 | if (atomic_long_read(&nfss->writeback) < NFS_CONGESTION_OFF_THRESH) | |
0db10944 | 1856 | clear_bdi_congested(inode_to_bdi(data->inode), BLK_RW_ASYNC); |
353db796 | 1857 | |
f453a54a | 1858 | nfs_init_cinfo(&cinfo, data->inode, data->dreq); |
af7cf057 | 1859 | nfs_commit_end(cinfo.mds); |
5917ce84 FI |
1860 | } |
1861 | ||
1862 | static void nfs_commit_release(void *calldata) | |
1863 | { | |
0b7c0153 | 1864 | struct nfs_commit_data *data = calldata; |
5917ce84 | 1865 | |
f453a54a | 1866 | data->completion_ops->completion(data); |
c9d8f89d | 1867 | nfs_commitdata_release(calldata); |
1da177e4 | 1868 | } |
788e7a89 TM |
1869 | |
1870 | static const struct rpc_call_ops nfs_commit_ops = { | |
0b7c0153 | 1871 | .rpc_call_prepare = nfs_commit_prepare, |
788e7a89 TM |
1872 | .rpc_call_done = nfs_commit_done, |
1873 | .rpc_release = nfs_commit_release, | |
1874 | }; | |
1da177e4 | 1875 | |
f453a54a FI |
1876 | static const struct nfs_commit_completion_ops nfs_commit_completion_ops = { |
1877 | .completion = nfs_commit_release_pages, | |
b20135d0 | 1878 | .resched_write = nfs_commit_resched_write, |
f453a54a FI |
1879 | }; |
1880 | ||
1763da12 FI |
1881 | int nfs_generic_commit_list(struct inode *inode, struct list_head *head, |
1882 | int how, struct nfs_commit_info *cinfo) | |
84c53ab5 FI |
1883 | { |
1884 | int status; | |
1885 | ||
ea2cf228 | 1886 | status = pnfs_commit_list(inode, head, how, cinfo); |
84c53ab5 | 1887 | if (status == PNFS_NOT_ATTEMPTED) |
ea2cf228 | 1888 | status = nfs_commit_list(inode, head, how, cinfo); |
84c53ab5 FI |
1889 | return status; |
1890 | } | |
1891 | ||
b608b283 | 1892 | int nfs_commit_inode(struct inode *inode, int how) |
1da177e4 | 1893 | { |
1da177e4 | 1894 | LIST_HEAD(head); |
ea2cf228 | 1895 | struct nfs_commit_info cinfo; |
71d0a611 | 1896 | int may_wait = how & FLUSH_SYNC; |
af7cf057 | 1897 | int error = 0; |
b8413f98 | 1898 | int res; |
1da177e4 | 1899 | |
ea2cf228 | 1900 | nfs_init_cinfo_from_inode(&cinfo, inode); |
af7cf057 | 1901 | nfs_commit_begin(cinfo.mds); |
ea2cf228 | 1902 | res = nfs_scan_commit(inode, &head, &cinfo); |
af7cf057 | 1903 | if (res) |
ea2cf228 | 1904 | error = nfs_generic_commit_list(inode, &head, how, &cinfo); |
af7cf057 TM |
1905 | nfs_commit_end(cinfo.mds); |
1906 | if (error < 0) | |
1907 | goto out_error; | |
1908 | if (!may_wait) | |
1909 | goto out_mark_dirty; | |
1910 | error = wait_on_commit(cinfo.mds); | |
1911 | if (error < 0) | |
1912 | return error; | |
c5efa5fc | 1913 | return res; |
af7cf057 TM |
1914 | out_error: |
1915 | res = error; | |
c5efa5fc TM |
1916 | /* Note: If we exit without ensuring that the commit is complete, |
1917 | * we must mark the inode as dirty. Otherwise, future calls to | |
1918 | * sync_inode() with the WB_SYNC_ALL flag set will fail to ensure | |
1919 | * that the data is on the disk. | |
1920 | */ | |
1921 | out_mark_dirty: | |
1922 | __mark_inode_dirty(inode, I_DIRTY_DATASYNC); | |
1da177e4 LT |
1923 | return res; |
1924 | } | |
b20135d0 | 1925 | EXPORT_SYMBOL_GPL(nfs_commit_inode); |
8fc795f7 | 1926 | |
ae09c31f | 1927 | int nfs_write_inode(struct inode *inode, struct writeback_control *wbc) |
8fc795f7 | 1928 | { |
420e3646 TM |
1929 | struct nfs_inode *nfsi = NFS_I(inode); |
1930 | int flags = FLUSH_SYNC; | |
1931 | int ret = 0; | |
8fc795f7 | 1932 | |
3236c3e1 | 1933 | /* no commits means nothing needs to be done */ |
ea2cf228 | 1934 | if (!nfsi->commit_info.ncommit) |
3236c3e1 JL |
1935 | return ret; |
1936 | ||
a00dd6c0 JL |
1937 | if (wbc->sync_mode == WB_SYNC_NONE) { |
1938 | /* Don't commit yet if this is a non-blocking flush and there | |
1939 | * are a lot of outstanding writes for this mapping. | |
1940 | */ | |
1a4edf0f | 1941 | if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK)) |
a00dd6c0 | 1942 | goto out_mark_dirty; |
420e3646 | 1943 | |
a00dd6c0 | 1944 | /* don't wait for the COMMIT response */ |
420e3646 | 1945 | flags = 0; |
a00dd6c0 JL |
1946 | } |
1947 | ||
420e3646 TM |
1948 | ret = nfs_commit_inode(inode, flags); |
1949 | if (ret >= 0) { | |
1950 | if (wbc->sync_mode == WB_SYNC_NONE) { | |
1951 | if (ret < wbc->nr_to_write) | |
1952 | wbc->nr_to_write -= ret; | |
1953 | else | |
1954 | wbc->nr_to_write = 0; | |
1955 | } | |
8fc795f7 | 1956 | return 0; |
420e3646 TM |
1957 | } |
1958 | out_mark_dirty: | |
8fc795f7 TM |
1959 | __mark_inode_dirty(inode, I_DIRTY_DATASYNC); |
1960 | return ret; | |
1961 | } | |
89d77c8f | 1962 | EXPORT_SYMBOL_GPL(nfs_write_inode); |
a8d8f02c | 1963 | |
837bb1d7 TM |
1964 | /* |
1965 | * Wrapper for filemap_write_and_wait_range() | |
1966 | * | |
1967 | * Needed for pNFS in order to ensure data becomes visible to the | |
1968 | * client. | |
1969 | */ | |
1970 | int nfs_filemap_write_and_wait_range(struct address_space *mapping, | |
1971 | loff_t lstart, loff_t lend) | |
1972 | { | |
1973 | int ret; | |
1974 | ||
1975 | ret = filemap_write_and_wait_range(mapping, lstart, lend); | |
1976 | if (ret == 0) | |
1977 | ret = pnfs_sync_inode(mapping->host, true); | |
1978 | return ret; | |
1979 | } | |
1980 | EXPORT_SYMBOL_GPL(nfs_filemap_write_and_wait_range); | |
1981 | ||
acdc53b2 TM |
1982 | /* |
1983 | * flush the inode to disk. | |
1984 | */ | |
1985 | int nfs_wb_all(struct inode *inode) | |
34901f70 | 1986 | { |
f4ce1299 TM |
1987 | int ret; |
1988 | ||
1989 | trace_nfs_writeback_inode_enter(inode); | |
1990 | ||
5bb89b47 | 1991 | ret = filemap_write_and_wait(inode->i_mapping); |
6b196875 CL |
1992 | if (ret) |
1993 | goto out; | |
1994 | ret = nfs_commit_inode(inode, FLUSH_SYNC); | |
1995 | if (ret < 0) | |
1996 | goto out; | |
1997 | pnfs_sync_inode(inode, true); | |
1998 | ret = 0; | |
34901f70 | 1999 | |
6b196875 | 2000 | out: |
f4ce1299 TM |
2001 | trace_nfs_writeback_inode_exit(inode, ret); |
2002 | return ret; | |
1c75950b | 2003 | } |
ddda8e0a | 2004 | EXPORT_SYMBOL_GPL(nfs_wb_all); |
1c75950b | 2005 | |
1b3b4a1a TM |
2006 | int nfs_wb_page_cancel(struct inode *inode, struct page *page) |
2007 | { | |
2008 | struct nfs_page *req; | |
1b3b4a1a TM |
2009 | int ret = 0; |
2010 | ||
3e217045 WAA |
2011 | wait_on_page_writeback(page); |
2012 | ||
2013 | /* blocking call to cancel all requests and join to a single (head) | |
2014 | * request */ | |
6d17d653 | 2015 | req = nfs_lock_and_join_requests(page); |
3e217045 WAA |
2016 | |
2017 | if (IS_ERR(req)) { | |
2018 | ret = PTR_ERR(req); | |
2019 | } else if (req) { | |
2020 | /* all requests from this page have been cancelled by | |
2021 | * nfs_lock_and_join_requests, so just remove the head | |
2022 | * request from the inode / page_private pointer and | |
2023 | * release it */ | |
2024 | nfs_inode_remove_request(req); | |
3e217045 | 2025 | nfs_unlock_and_release_request(req); |
1b3b4a1a | 2026 | } |
3e217045 | 2027 | |
1b3b4a1a TM |
2028 | return ret; |
2029 | } | |
2030 | ||
7f2f12d9 TM |
2031 | /* |
2032 | * Write back all requests on one page - we do this before reading it. | |
2033 | */ | |
c373fff7 | 2034 | int nfs_wb_page(struct inode *inode, struct page *page) |
1c75950b | 2035 | { |
29418aa4 | 2036 | loff_t range_start = page_file_offset(page); |
09cbfeaf | 2037 | loff_t range_end = range_start + (loff_t)(PAGE_SIZE - 1); |
4d770ccf | 2038 | struct writeback_control wbc = { |
4d770ccf | 2039 | .sync_mode = WB_SYNC_ALL, |
7f2f12d9 | 2040 | .nr_to_write = 0, |
4d770ccf TM |
2041 | .range_start = range_start, |
2042 | .range_end = range_end, | |
2043 | }; | |
2044 | int ret; | |
1c75950b | 2045 | |
f4ce1299 TM |
2046 | trace_nfs_writeback_page_enter(inode); |
2047 | ||
0522f6ad | 2048 | for (;;) { |
ba8b06e6 | 2049 | wait_on_page_writeback(page); |
73e3302f | 2050 | if (clear_page_dirty_for_io(page)) { |
c373fff7 | 2051 | ret = nfs_writepage_locked(page, &wbc); |
73e3302f TM |
2052 | if (ret < 0) |
2053 | goto out_error; | |
0522f6ad | 2054 | continue; |
7f2f12d9 | 2055 | } |
f4ce1299 | 2056 | ret = 0; |
0522f6ad TM |
2057 | if (!PagePrivate(page)) |
2058 | break; | |
2059 | ret = nfs_commit_inode(inode, FLUSH_SYNC); | |
ba8b06e6 | 2060 | if (ret < 0) |
73e3302f | 2061 | goto out_error; |
7f2f12d9 | 2062 | } |
73e3302f | 2063 | out_error: |
f4ce1299 | 2064 | trace_nfs_writeback_page_exit(inode, ret); |
4d770ccf | 2065 | return ret; |
1c75950b TM |
2066 | } |
2067 | ||
074cc1de TM |
2068 | #ifdef CONFIG_MIGRATION |
2069 | int nfs_migrate_page(struct address_space *mapping, struct page *newpage, | |
a6bc32b8 | 2070 | struct page *page, enum migrate_mode mode) |
074cc1de | 2071 | { |
2da95652 JL |
2072 | /* |
2073 | * If PagePrivate is set, then the page is currently associated with | |
2074 | * an in-progress read or write request. Don't try to migrate it. | |
2075 | * | |
2076 | * FIXME: we could do this in principle, but we'll need a way to ensure | |
2077 | * that we can safely release the inode reference while holding | |
2078 | * the page lock. | |
2079 | */ | |
2080 | if (PagePrivate(page)) | |
2081 | return -EBUSY; | |
074cc1de | 2082 | |
8c209ce7 DH |
2083 | if (!nfs_fscache_release_page(page, GFP_KERNEL)) |
2084 | return -EBUSY; | |
074cc1de | 2085 | |
a6bc32b8 | 2086 | return migrate_page(mapping, newpage, page, mode); |
074cc1de TM |
2087 | } |
2088 | #endif | |
2089 | ||
f7b422b1 | 2090 | int __init nfs_init_writepagecache(void) |
1da177e4 LT |
2091 | { |
2092 | nfs_wdata_cachep = kmem_cache_create("nfs_write_data", | |
1e7f3a48 | 2093 | sizeof(struct nfs_pgio_header), |
1da177e4 | 2094 | 0, SLAB_HWCACHE_ALIGN, |
20c2df83 | 2095 | NULL); |
1da177e4 LT |
2096 | if (nfs_wdata_cachep == NULL) |
2097 | return -ENOMEM; | |
2098 | ||
93d2341c MD |
2099 | nfs_wdata_mempool = mempool_create_slab_pool(MIN_POOL_WRITE, |
2100 | nfs_wdata_cachep); | |
1da177e4 | 2101 | if (nfs_wdata_mempool == NULL) |
3dd4765f | 2102 | goto out_destroy_write_cache; |
1da177e4 | 2103 | |
0b7c0153 FI |
2104 | nfs_cdata_cachep = kmem_cache_create("nfs_commit_data", |
2105 | sizeof(struct nfs_commit_data), | |
2106 | 0, SLAB_HWCACHE_ALIGN, | |
2107 | NULL); | |
2108 | if (nfs_cdata_cachep == NULL) | |
3dd4765f | 2109 | goto out_destroy_write_mempool; |
0b7c0153 | 2110 | |
93d2341c | 2111 | nfs_commit_mempool = mempool_create_slab_pool(MIN_POOL_COMMIT, |
4c100210 | 2112 | nfs_cdata_cachep); |
1da177e4 | 2113 | if (nfs_commit_mempool == NULL) |
3dd4765f | 2114 | goto out_destroy_commit_cache; |
1da177e4 | 2115 | |
89a09141 PZ |
2116 | /* |
2117 | * NFS congestion size, scale with available memory. | |
2118 | * | |
2119 | * 64MB: 8192k | |
2120 | * 128MB: 11585k | |
2121 | * 256MB: 16384k | |
2122 | * 512MB: 23170k | |
2123 | * 1GB: 32768k | |
2124 | * 2GB: 46340k | |
2125 | * 4GB: 65536k | |
2126 | * 8GB: 92681k | |
2127 | * 16GB: 131072k | |
2128 | * | |
2129 | * This allows larger machines to have larger/more transfers. | |
2130 | * Limit the default to 256M | |
2131 | */ | |
2132 | nfs_congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10); | |
2133 | if (nfs_congestion_kb > 256*1024) | |
2134 | nfs_congestion_kb = 256*1024; | |
2135 | ||
1da177e4 | 2136 | return 0; |
3dd4765f JL |
2137 | |
2138 | out_destroy_commit_cache: | |
2139 | kmem_cache_destroy(nfs_cdata_cachep); | |
2140 | out_destroy_write_mempool: | |
2141 | mempool_destroy(nfs_wdata_mempool); | |
2142 | out_destroy_write_cache: | |
2143 | kmem_cache_destroy(nfs_wdata_cachep); | |
2144 | return -ENOMEM; | |
1da177e4 LT |
2145 | } |
2146 | ||
266bee88 | 2147 | void nfs_destroy_writepagecache(void) |
1da177e4 LT |
2148 | { |
2149 | mempool_destroy(nfs_commit_mempool); | |
3dd4765f | 2150 | kmem_cache_destroy(nfs_cdata_cachep); |
1da177e4 | 2151 | mempool_destroy(nfs_wdata_mempool); |
1a1d92c1 | 2152 | kmem_cache_destroy(nfs_wdata_cachep); |
1da177e4 LT |
2153 | } |
2154 | ||
4a0de55c AS |
2155 | static const struct nfs_rw_ops nfs_rw_write_ops = { |
2156 | .rw_alloc_header = nfs_writehdr_alloc, | |
2157 | .rw_free_header = nfs_writehdr_free, | |
0eecb214 AS |
2158 | .rw_done = nfs_writeback_done, |
2159 | .rw_result = nfs_writeback_result, | |
1ed26f33 | 2160 | .rw_initiate = nfs_initiate_write, |
4a0de55c | 2161 | }; |