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