]>
Commit | Line | Data |
---|---|---|
31143d5d DH |
1 | /* handling of writes to regular files and writing back to the server |
2 | * | |
3 | * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved. | |
4 | * Written by David Howells (dhowells@redhat.com) | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU General Public License | |
8 | * as published by the Free Software Foundation; either version | |
9 | * 2 of the License, or (at your option) any later version. | |
10 | */ | |
4343d008 | 11 | |
4af3c9cc | 12 | #include <linux/backing-dev.h> |
31143d5d DH |
13 | #include <linux/slab.h> |
14 | #include <linux/fs.h> | |
15 | #include <linux/pagemap.h> | |
16 | #include <linux/writeback.h> | |
17 | #include <linux/pagevec.h> | |
18 | #include "internal.h" | |
19 | ||
31143d5d DH |
20 | /* |
21 | * mark a page as having been made dirty and thus needing writeback | |
22 | */ | |
23 | int afs_set_page_dirty(struct page *page) | |
24 | { | |
25 | _enter(""); | |
26 | return __set_page_dirty_nobuffers(page); | |
27 | } | |
28 | ||
31143d5d DH |
29 | /* |
30 | * partly or wholly fill a page that's under preparation for writing | |
31 | */ | |
32 | static int afs_fill_page(struct afs_vnode *vnode, struct key *key, | |
e8e581a8 | 33 | loff_t pos, unsigned int len, struct page *page) |
31143d5d | 34 | { |
196ee9cd | 35 | struct afs_read *req; |
2a0b4f64 DH |
36 | size_t p; |
37 | void *data; | |
31143d5d DH |
38 | int ret; |
39 | ||
5e7f2337 | 40 | _enter(",,%llu", (unsigned long long)pos); |
31143d5d | 41 | |
2a0b4f64 DH |
42 | if (pos >= vnode->vfs_inode.i_size) { |
43 | p = pos & ~PAGE_MASK; | |
44 | ASSERTCMP(p + len, <=, PAGE_SIZE); | |
45 | data = kmap(page); | |
46 | memset(data + p, 0, len); | |
47 | kunmap(page); | |
48 | return 0; | |
49 | } | |
50 | ||
196ee9cd DH |
51 | req = kzalloc(sizeof(struct afs_read) + sizeof(struct page *), |
52 | GFP_KERNEL); | |
53 | if (!req) | |
54 | return -ENOMEM; | |
55 | ||
f3ddee8d | 56 | refcount_set(&req->usage, 1); |
196ee9cd | 57 | req->pos = pos; |
e8e581a8 | 58 | req->len = len; |
196ee9cd | 59 | req->nr_pages = 1; |
f3ddee8d | 60 | req->pages = req->array; |
196ee9cd | 61 | req->pages[0] = page; |
5611ef28 | 62 | get_page(page); |
196ee9cd | 63 | |
d2ddc776 | 64 | ret = afs_fetch_data(vnode, key, req); |
196ee9cd | 65 | afs_put_read(req); |
31143d5d DH |
66 | if (ret < 0) { |
67 | if (ret == -ENOENT) { | |
68 | _debug("got NOENT from server" | |
69 | " - marking file deleted and stale"); | |
70 | set_bit(AFS_VNODE_DELETED, &vnode->flags); | |
71 | ret = -ESTALE; | |
72 | } | |
73 | } | |
74 | ||
75 | _leave(" = %d", ret); | |
76 | return ret; | |
77 | } | |
78 | ||
31143d5d DH |
79 | /* |
80 | * prepare to perform part of a write to a page | |
31143d5d | 81 | */ |
15b4650e NP |
82 | int afs_write_begin(struct file *file, struct address_space *mapping, |
83 | loff_t pos, unsigned len, unsigned flags, | |
84 | struct page **pagep, void **fsdata) | |
31143d5d | 85 | { |
496ad9aa | 86 | struct afs_vnode *vnode = AFS_FS_I(file_inode(file)); |
15b4650e | 87 | struct page *page; |
215804a9 | 88 | struct key *key = afs_file_key(file); |
4343d008 DH |
89 | unsigned long priv; |
90 | unsigned f, from = pos & (PAGE_SIZE - 1); | |
91 | unsigned t, to = from + len; | |
09cbfeaf | 92 | pgoff_t index = pos >> PAGE_SHIFT; |
31143d5d DH |
93 | int ret; |
94 | ||
3b6492df | 95 | _enter("{%llx:%llu},{%lx},%u,%u", |
15b4650e | 96 | vnode->fid.vid, vnode->fid.vnode, index, from, to); |
31143d5d | 97 | |
4343d008 DH |
98 | /* We want to store information about how much of a page is altered in |
99 | * page->private. | |
100 | */ | |
101 | BUILD_BUG_ON(PAGE_SIZE > 32768 && sizeof(page->private) < 8); | |
31143d5d | 102 | |
54566b2c | 103 | page = grab_cache_page_write_begin(mapping, index, flags); |
4343d008 | 104 | if (!page) |
15b4650e | 105 | return -ENOMEM; |
15b4650e | 106 | |
09cbfeaf | 107 | if (!PageUptodate(page) && len != PAGE_SIZE) { |
e8e581a8 | 108 | ret = afs_fill_page(vnode, key, pos & PAGE_MASK, PAGE_SIZE, page); |
31143d5d | 109 | if (ret < 0) { |
6d06b0d2 DH |
110 | unlock_page(page); |
111 | put_page(page); | |
31143d5d DH |
112 | _leave(" = %d [prep]", ret); |
113 | return ret; | |
114 | } | |
15b4650e | 115 | SetPageUptodate(page); |
31143d5d DH |
116 | } |
117 | ||
6d06b0d2 DH |
118 | /* page won't leak in error case: it eventually gets cleaned off LRU */ |
119 | *pagep = page; | |
120 | ||
31143d5d | 121 | try_again: |
4343d008 DH |
122 | /* See if this page is already partially written in a way that we can |
123 | * merge the new write with. | |
124 | */ | |
125 | t = f = 0; | |
126 | if (PagePrivate(page)) { | |
127 | priv = page_private(page); | |
128 | f = priv & AFS_PRIV_MAX; | |
129 | t = priv >> AFS_PRIV_SHIFT; | |
130 | ASSERTCMP(f, <=, t); | |
31143d5d DH |
131 | } |
132 | ||
4343d008 | 133 | if (f != t) { |
5a039c32 DH |
134 | if (PageWriteback(page)) { |
135 | trace_afs_page_dirty(vnode, tracepoint_string("alrdy"), | |
136 | page->index, priv); | |
137 | goto flush_conflicting_write; | |
138 | } | |
5a813276 DH |
139 | /* If the file is being filled locally, allow inter-write |
140 | * spaces to be merged into writes. If it's not, only write | |
141 | * back what the user gives us. | |
142 | */ | |
143 | if (!test_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags) && | |
144 | (to < f || from > t)) | |
4343d008 DH |
145 | goto flush_conflicting_write; |
146 | if (from < f) | |
147 | f = from; | |
148 | if (to > t) | |
149 | t = to; | |
150 | } else { | |
151 | f = from; | |
152 | t = to; | |
31143d5d DH |
153 | } |
154 | ||
4343d008 DH |
155 | priv = (unsigned long)t << AFS_PRIV_SHIFT; |
156 | priv |= f; | |
13524ab3 DH |
157 | trace_afs_page_dirty(vnode, tracepoint_string("begin"), |
158 | page->index, priv); | |
31143d5d | 159 | SetPagePrivate(page); |
4343d008 DH |
160 | set_page_private(page, priv); |
161 | _leave(" = 0"); | |
31143d5d DH |
162 | return 0; |
163 | ||
4343d008 DH |
164 | /* The previous write and this write aren't adjacent or overlapping, so |
165 | * flush the page out. | |
166 | */ | |
167 | flush_conflicting_write: | |
31143d5d | 168 | _debug("flush conflict"); |
4343d008 DH |
169 | ret = write_one_page(page); |
170 | if (ret < 0) { | |
171 | _leave(" = %d", ret); | |
172 | return ret; | |
31143d5d DH |
173 | } |
174 | ||
4343d008 DH |
175 | ret = lock_page_killable(page); |
176 | if (ret < 0) { | |
177 | _leave(" = %d", ret); | |
178 | return ret; | |
179 | } | |
31143d5d DH |
180 | goto try_again; |
181 | } | |
182 | ||
183 | /* | |
184 | * finalise part of a write to a page | |
185 | */ | |
15b4650e NP |
186 | int afs_write_end(struct file *file, struct address_space *mapping, |
187 | loff_t pos, unsigned len, unsigned copied, | |
188 | struct page *page, void *fsdata) | |
31143d5d | 189 | { |
496ad9aa | 190 | struct afs_vnode *vnode = AFS_FS_I(file_inode(file)); |
215804a9 | 191 | struct key *key = afs_file_key(file); |
31143d5d | 192 | loff_t i_size, maybe_i_size; |
e8e581a8 | 193 | int ret; |
31143d5d | 194 | |
3b6492df | 195 | _enter("{%llx:%llu},{%lx}", |
15b4650e | 196 | vnode->fid.vid, vnode->fid.vnode, page->index); |
31143d5d | 197 | |
15b4650e | 198 | maybe_i_size = pos + copied; |
31143d5d DH |
199 | |
200 | i_size = i_size_read(&vnode->vfs_inode); | |
201 | if (maybe_i_size > i_size) { | |
4343d008 | 202 | spin_lock(&vnode->wb_lock); |
31143d5d DH |
203 | i_size = i_size_read(&vnode->vfs_inode); |
204 | if (maybe_i_size > i_size) | |
205 | i_size_write(&vnode->vfs_inode, maybe_i_size); | |
4343d008 | 206 | spin_unlock(&vnode->wb_lock); |
31143d5d DH |
207 | } |
208 | ||
e8e581a8 DH |
209 | if (!PageUptodate(page)) { |
210 | if (copied < len) { | |
211 | /* Try and load any missing data from the server. The | |
212 | * unmarshalling routine will take care of clearing any | |
213 | * bits that are beyond the EOF. | |
214 | */ | |
215 | ret = afs_fill_page(vnode, key, pos + copied, | |
216 | len - copied, page); | |
217 | if (ret < 0) | |
afae457d | 218 | goto out; |
e8e581a8 DH |
219 | } |
220 | SetPageUptodate(page); | |
221 | } | |
222 | ||
31143d5d | 223 | set_page_dirty(page); |
31143d5d DH |
224 | if (PageDirty(page)) |
225 | _debug("dirtied"); | |
afae457d DH |
226 | ret = copied; |
227 | ||
228 | out: | |
15b4650e | 229 | unlock_page(page); |
09cbfeaf | 230 | put_page(page); |
afae457d | 231 | return ret; |
31143d5d DH |
232 | } |
233 | ||
234 | /* | |
235 | * kill all the pages in the given range | |
236 | */ | |
4343d008 | 237 | static void afs_kill_pages(struct address_space *mapping, |
31143d5d DH |
238 | pgoff_t first, pgoff_t last) |
239 | { | |
4343d008 | 240 | struct afs_vnode *vnode = AFS_FS_I(mapping->host); |
31143d5d DH |
241 | struct pagevec pv; |
242 | unsigned count, loop; | |
243 | ||
3b6492df | 244 | _enter("{%llx:%llu},%lx-%lx", |
31143d5d DH |
245 | vnode->fid.vid, vnode->fid.vnode, first, last); |
246 | ||
86679820 | 247 | pagevec_init(&pv); |
31143d5d DH |
248 | |
249 | do { | |
250 | _debug("kill %lx-%lx", first, last); | |
251 | ||
252 | count = last - first + 1; | |
253 | if (count > PAGEVEC_SIZE) | |
254 | count = PAGEVEC_SIZE; | |
4343d008 | 255 | pv.nr = find_get_pages_contig(mapping, first, count, pv.pages); |
31143d5d DH |
256 | ASSERTCMP(pv.nr, ==, count); |
257 | ||
258 | for (loop = 0; loop < count; loop++) { | |
7286a35e DH |
259 | struct page *page = pv.pages[loop]; |
260 | ClearPageUptodate(page); | |
4343d008 DH |
261 | SetPageError(page); |
262 | end_page_writeback(page); | |
7286a35e DH |
263 | if (page->index >= first) |
264 | first = page->index + 1; | |
4343d008 DH |
265 | lock_page(page); |
266 | generic_error_remove_page(mapping, page); | |
21bd68f1 | 267 | unlock_page(page); |
31143d5d DH |
268 | } |
269 | ||
270 | __pagevec_release(&pv); | |
4343d008 | 271 | } while (first <= last); |
31143d5d DH |
272 | |
273 | _leave(""); | |
274 | } | |
275 | ||
276 | /* | |
4343d008 DH |
277 | * Redirty all the pages in a given range. |
278 | */ | |
279 | static void afs_redirty_pages(struct writeback_control *wbc, | |
280 | struct address_space *mapping, | |
281 | pgoff_t first, pgoff_t last) | |
282 | { | |
283 | struct afs_vnode *vnode = AFS_FS_I(mapping->host); | |
284 | struct pagevec pv; | |
285 | unsigned count, loop; | |
286 | ||
3b6492df | 287 | _enter("{%llx:%llu},%lx-%lx", |
4343d008 DH |
288 | vnode->fid.vid, vnode->fid.vnode, first, last); |
289 | ||
487e2c9f | 290 | pagevec_init(&pv); |
4343d008 DH |
291 | |
292 | do { | |
293 | _debug("redirty %lx-%lx", first, last); | |
294 | ||
295 | count = last - first + 1; | |
296 | if (count > PAGEVEC_SIZE) | |
297 | count = PAGEVEC_SIZE; | |
298 | pv.nr = find_get_pages_contig(mapping, first, count, pv.pages); | |
299 | ASSERTCMP(pv.nr, ==, count); | |
300 | ||
301 | for (loop = 0; loop < count; loop++) { | |
302 | struct page *page = pv.pages[loop]; | |
303 | ||
304 | redirty_page_for_writepage(wbc, page); | |
305 | end_page_writeback(page); | |
306 | if (page->index >= first) | |
307 | first = page->index + 1; | |
308 | } | |
309 | ||
310 | __pagevec_release(&pv); | |
311 | } while (first <= last); | |
31143d5d DH |
312 | |
313 | _leave(""); | |
314 | } | |
315 | ||
d2ddc776 DH |
316 | /* |
317 | * write to a file | |
318 | */ | |
4343d008 DH |
319 | static int afs_store_data(struct address_space *mapping, |
320 | pgoff_t first, pgoff_t last, | |
d2ddc776 DH |
321 | unsigned offset, unsigned to) |
322 | { | |
4343d008 | 323 | struct afs_vnode *vnode = AFS_FS_I(mapping->host); |
d2ddc776 | 324 | struct afs_fs_cursor fc; |
4343d008 DH |
325 | struct afs_wb_key *wbk = NULL; |
326 | struct list_head *p; | |
327 | int ret = -ENOKEY, ret2; | |
d2ddc776 | 328 | |
3b6492df | 329 | _enter("%s{%llx:%llu.%u},%lx,%lx,%x,%x", |
d2ddc776 DH |
330 | vnode->volume->name, |
331 | vnode->fid.vid, | |
332 | vnode->fid.vnode, | |
333 | vnode->fid.unique, | |
d2ddc776 DH |
334 | first, last, offset, to); |
335 | ||
4343d008 DH |
336 | spin_lock(&vnode->wb_lock); |
337 | p = vnode->wb_keys.next; | |
338 | ||
339 | /* Iterate through the list looking for a valid key to use. */ | |
340 | try_next_key: | |
341 | while (p != &vnode->wb_keys) { | |
342 | wbk = list_entry(p, struct afs_wb_key, vnode_link); | |
343 | _debug("wbk %u", key_serial(wbk->key)); | |
344 | ret2 = key_validate(wbk->key); | |
345 | if (ret2 == 0) | |
346 | goto found_key; | |
347 | if (ret == -ENOKEY) | |
348 | ret = ret2; | |
349 | p = p->next; | |
350 | } | |
351 | ||
352 | spin_unlock(&vnode->wb_lock); | |
353 | afs_put_wb_key(wbk); | |
354 | _leave(" = %d [no keys]", ret); | |
355 | return ret; | |
356 | ||
357 | found_key: | |
358 | refcount_inc(&wbk->usage); | |
359 | spin_unlock(&vnode->wb_lock); | |
360 | ||
361 | _debug("USE WB KEY %u", key_serial(wbk->key)); | |
362 | ||
d2ddc776 | 363 | ret = -ERESTARTSYS; |
20b8391f | 364 | if (afs_begin_vnode_operation(&fc, vnode, wbk->key, false)) { |
d2ddc776 | 365 | while (afs_select_fileserver(&fc)) { |
68251f0a | 366 | fc.cb_break = afs_calc_vnode_cb_break(vnode); |
4343d008 | 367 | afs_fs_store_data(&fc, mapping, first, last, offset, to); |
d2ddc776 DH |
368 | } |
369 | ||
370 | afs_check_for_remote_deletion(&fc, fc.vnode); | |
371 | afs_vnode_commit_status(&fc, vnode, fc.cb_break); | |
372 | ret = afs_end_vnode_operation(&fc); | |
373 | } | |
374 | ||
4343d008 | 375 | switch (ret) { |
76a5cb6f DH |
376 | case 0: |
377 | afs_stat_v(vnode, n_stores); | |
378 | atomic_long_add((last * PAGE_SIZE + to) - | |
379 | (first * PAGE_SIZE + offset), | |
380 | &afs_v2net(vnode)->n_store_bytes); | |
381 | break; | |
4343d008 DH |
382 | case -EACCES: |
383 | case -EPERM: | |
384 | case -ENOKEY: | |
385 | case -EKEYEXPIRED: | |
386 | case -EKEYREJECTED: | |
387 | case -EKEYREVOKED: | |
388 | _debug("next"); | |
389 | spin_lock(&vnode->wb_lock); | |
390 | p = wbk->vnode_link.next; | |
391 | afs_put_wb_key(wbk); | |
392 | goto try_next_key; | |
393 | } | |
394 | ||
395 | afs_put_wb_key(wbk); | |
d2ddc776 DH |
396 | _leave(" = %d", ret); |
397 | return ret; | |
398 | } | |
399 | ||
31143d5d | 400 | /* |
4343d008 DH |
401 | * Synchronously write back the locked page and any subsequent non-locked dirty |
402 | * pages. | |
31143d5d | 403 | */ |
4343d008 DH |
404 | static int afs_write_back_from_locked_page(struct address_space *mapping, |
405 | struct writeback_control *wbc, | |
406 | struct page *primary_page, | |
407 | pgoff_t final_page) | |
31143d5d | 408 | { |
13524ab3 | 409 | struct afs_vnode *vnode = AFS_FS_I(mapping->host); |
31143d5d | 410 | struct page *pages[8], *page; |
4343d008 DH |
411 | unsigned long count, priv; |
412 | unsigned n, offset, to, f, t; | |
31143d5d DH |
413 | pgoff_t start, first, last; |
414 | int loop, ret; | |
415 | ||
416 | _enter(",%lx", primary_page->index); | |
417 | ||
418 | count = 1; | |
31143d5d DH |
419 | if (test_set_page_writeback(primary_page)) |
420 | BUG(); | |
421 | ||
4343d008 DH |
422 | /* Find all consecutive lockable dirty pages that have contiguous |
423 | * written regions, stopping when we find a page that is not | |
424 | * immediately lockable, is not dirty or is missing, or we reach the | |
425 | * end of the range. | |
426 | */ | |
31143d5d | 427 | start = primary_page->index; |
4343d008 DH |
428 | priv = page_private(primary_page); |
429 | offset = priv & AFS_PRIV_MAX; | |
430 | to = priv >> AFS_PRIV_SHIFT; | |
13524ab3 DH |
431 | trace_afs_page_dirty(vnode, tracepoint_string("store"), |
432 | primary_page->index, priv); | |
4343d008 DH |
433 | |
434 | WARN_ON(offset == to); | |
13524ab3 DH |
435 | if (offset == to) |
436 | trace_afs_page_dirty(vnode, tracepoint_string("WARN"), | |
437 | primary_page->index, priv); | |
4343d008 | 438 | |
5a813276 DH |
439 | if (start >= final_page || |
440 | (to < PAGE_SIZE && !test_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags))) | |
31143d5d | 441 | goto no_more; |
4343d008 | 442 | |
31143d5d DH |
443 | start++; |
444 | do { | |
445 | _debug("more %lx [%lx]", start, count); | |
4343d008 | 446 | n = final_page - start + 1; |
31143d5d DH |
447 | if (n > ARRAY_SIZE(pages)) |
448 | n = ARRAY_SIZE(pages); | |
4343d008 | 449 | n = find_get_pages_contig(mapping, start, ARRAY_SIZE(pages), pages); |
31143d5d DH |
450 | _debug("fgpc %u", n); |
451 | if (n == 0) | |
452 | goto no_more; | |
453 | if (pages[0]->index != start) { | |
9d577b6a DH |
454 | do { |
455 | put_page(pages[--n]); | |
456 | } while (n > 0); | |
31143d5d DH |
457 | goto no_more; |
458 | } | |
459 | ||
460 | for (loop = 0; loop < n; loop++) { | |
461 | page = pages[loop]; | |
5a813276 DH |
462 | if (to != PAGE_SIZE && |
463 | !test_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags)) | |
464 | break; | |
4343d008 | 465 | if (page->index > final_page) |
31143d5d | 466 | break; |
529ae9aa | 467 | if (!trylock_page(page)) |
31143d5d | 468 | break; |
4343d008 | 469 | if (!PageDirty(page) || PageWriteback(page)) { |
31143d5d DH |
470 | unlock_page(page); |
471 | break; | |
472 | } | |
4343d008 DH |
473 | |
474 | priv = page_private(page); | |
475 | f = priv & AFS_PRIV_MAX; | |
476 | t = priv >> AFS_PRIV_SHIFT; | |
5a813276 DH |
477 | if (f != 0 && |
478 | !test_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags)) { | |
31143d5d DH |
479 | unlock_page(page); |
480 | break; | |
481 | } | |
4343d008 DH |
482 | to = t; |
483 | ||
13524ab3 DH |
484 | trace_afs_page_dirty(vnode, tracepoint_string("store+"), |
485 | page->index, priv); | |
486 | ||
31143d5d DH |
487 | if (!clear_page_dirty_for_io(page)) |
488 | BUG(); | |
489 | if (test_set_page_writeback(page)) | |
490 | BUG(); | |
491 | unlock_page(page); | |
492 | put_page(page); | |
493 | } | |
494 | count += loop; | |
495 | if (loop < n) { | |
496 | for (; loop < n; loop++) | |
497 | put_page(pages[loop]); | |
498 | goto no_more; | |
499 | } | |
500 | ||
501 | start += loop; | |
4343d008 | 502 | } while (start <= final_page && count < 65536); |
31143d5d DH |
503 | |
504 | no_more: | |
4343d008 DH |
505 | /* We now have a contiguous set of dirty pages, each with writeback |
506 | * set; the first page is still locked at this point, but all the rest | |
507 | * have been unlocked. | |
508 | */ | |
509 | unlock_page(primary_page); | |
510 | ||
31143d5d DH |
511 | first = primary_page->index; |
512 | last = first + count - 1; | |
513 | ||
31143d5d DH |
514 | _debug("write back %lx[%u..] to %lx[..%u]", first, offset, last, to); |
515 | ||
4343d008 DH |
516 | ret = afs_store_data(mapping, first, last, offset, to); |
517 | switch (ret) { | |
518 | case 0: | |
31143d5d | 519 | ret = count; |
4343d008 DH |
520 | break; |
521 | ||
522 | default: | |
523 | pr_notice("kAFS: Unexpected error from FS.StoreData %d\n", ret); | |
524 | /* Fall through */ | |
525 | case -EACCES: | |
526 | case -EPERM: | |
527 | case -ENOKEY: | |
528 | case -EKEYEXPIRED: | |
529 | case -EKEYREJECTED: | |
530 | case -EKEYREVOKED: | |
531 | afs_redirty_pages(wbc, mapping, first, last); | |
532 | mapping_set_error(mapping, ret); | |
533 | break; | |
534 | ||
535 | case -EDQUOT: | |
536 | case -ENOSPC: | |
537 | afs_redirty_pages(wbc, mapping, first, last); | |
538 | mapping_set_error(mapping, -ENOSPC); | |
539 | break; | |
540 | ||
541 | case -EROFS: | |
542 | case -EIO: | |
543 | case -EREMOTEIO: | |
544 | case -EFBIG: | |
545 | case -ENOENT: | |
546 | case -ENOMEDIUM: | |
547 | case -ENXIO: | |
f51375cd | 548 | trace_afs_file_error(vnode, ret, afs_file_error_writeback_fail); |
4343d008 DH |
549 | afs_kill_pages(mapping, first, last); |
550 | mapping_set_error(mapping, ret); | |
551 | break; | |
31143d5d DH |
552 | } |
553 | ||
554 | _leave(" = %d", ret); | |
555 | return ret; | |
556 | } | |
557 | ||
558 | /* | |
559 | * write a page back to the server | |
560 | * - the caller locked the page for us | |
561 | */ | |
562 | int afs_writepage(struct page *page, struct writeback_control *wbc) | |
563 | { | |
31143d5d DH |
564 | int ret; |
565 | ||
566 | _enter("{%lx},", page->index); | |
567 | ||
4343d008 DH |
568 | ret = afs_write_back_from_locked_page(page->mapping, wbc, page, |
569 | wbc->range_end >> PAGE_SHIFT); | |
31143d5d DH |
570 | if (ret < 0) { |
571 | _leave(" = %d", ret); | |
572 | return 0; | |
573 | } | |
574 | ||
575 | wbc->nr_to_write -= ret; | |
31143d5d DH |
576 | |
577 | _leave(" = 0"); | |
578 | return 0; | |
579 | } | |
580 | ||
581 | /* | |
582 | * write a region of pages back to the server | |
583 | */ | |
c1206a2c AB |
584 | static int afs_writepages_region(struct address_space *mapping, |
585 | struct writeback_control *wbc, | |
586 | pgoff_t index, pgoff_t end, pgoff_t *_next) | |
31143d5d | 587 | { |
31143d5d DH |
588 | struct page *page; |
589 | int ret, n; | |
590 | ||
591 | _enter(",,%lx,%lx,", index, end); | |
592 | ||
593 | do { | |
aef6e415 JK |
594 | n = find_get_pages_range_tag(mapping, &index, end, |
595 | PAGECACHE_TAG_DIRTY, 1, &page); | |
31143d5d DH |
596 | if (!n) |
597 | break; | |
598 | ||
599 | _debug("wback %lx", page->index); | |
600 | ||
b93b0163 MW |
601 | /* |
602 | * at this point we hold neither the i_pages lock nor the | |
603 | * page lock: the page may be truncated or invalidated | |
604 | * (changing page->mapping to NULL), or even swizzled | |
605 | * back from swapper_space to tmpfs file mapping | |
31143d5d | 606 | */ |
4343d008 DH |
607 | ret = lock_page_killable(page); |
608 | if (ret < 0) { | |
609 | put_page(page); | |
610 | _leave(" = %d", ret); | |
611 | return ret; | |
612 | } | |
31143d5d | 613 | |
c5051c7b | 614 | if (page->mapping != mapping || !PageDirty(page)) { |
31143d5d | 615 | unlock_page(page); |
09cbfeaf | 616 | put_page(page); |
31143d5d DH |
617 | continue; |
618 | } | |
619 | ||
c5051c7b | 620 | if (PageWriteback(page)) { |
31143d5d | 621 | unlock_page(page); |
c5051c7b DH |
622 | if (wbc->sync_mode != WB_SYNC_NONE) |
623 | wait_on_page_writeback(page); | |
29c8bbbd | 624 | put_page(page); |
31143d5d DH |
625 | continue; |
626 | } | |
627 | ||
65a15109 DH |
628 | if (!clear_page_dirty_for_io(page)) |
629 | BUG(); | |
4343d008 | 630 | ret = afs_write_back_from_locked_page(mapping, wbc, page, end); |
09cbfeaf | 631 | put_page(page); |
31143d5d DH |
632 | if (ret < 0) { |
633 | _leave(" = %d", ret); | |
634 | return ret; | |
635 | } | |
636 | ||
637 | wbc->nr_to_write -= ret; | |
638 | ||
31143d5d DH |
639 | cond_resched(); |
640 | } while (index < end && wbc->nr_to_write > 0); | |
641 | ||
642 | *_next = index; | |
643 | _leave(" = 0 [%lx]", *_next); | |
644 | return 0; | |
645 | } | |
646 | ||
647 | /* | |
648 | * write some of the pending data back to the server | |
649 | */ | |
650 | int afs_writepages(struct address_space *mapping, | |
651 | struct writeback_control *wbc) | |
652 | { | |
31143d5d DH |
653 | pgoff_t start, end, next; |
654 | int ret; | |
655 | ||
656 | _enter(""); | |
657 | ||
31143d5d DH |
658 | if (wbc->range_cyclic) { |
659 | start = mapping->writeback_index; | |
660 | end = -1; | |
661 | ret = afs_writepages_region(mapping, wbc, start, end, &next); | |
1b430bee | 662 | if (start > 0 && wbc->nr_to_write > 0 && ret == 0) |
31143d5d DH |
663 | ret = afs_writepages_region(mapping, wbc, 0, start, |
664 | &next); | |
665 | mapping->writeback_index = next; | |
666 | } else if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) { | |
09cbfeaf | 667 | end = (pgoff_t)(LLONG_MAX >> PAGE_SHIFT); |
31143d5d DH |
668 | ret = afs_writepages_region(mapping, wbc, 0, end, &next); |
669 | if (wbc->nr_to_write > 0) | |
670 | mapping->writeback_index = next; | |
671 | } else { | |
09cbfeaf KS |
672 | start = wbc->range_start >> PAGE_SHIFT; |
673 | end = wbc->range_end >> PAGE_SHIFT; | |
31143d5d DH |
674 | ret = afs_writepages_region(mapping, wbc, start, end, &next); |
675 | } | |
676 | ||
677 | _leave(" = %d", ret); | |
678 | return ret; | |
679 | } | |
680 | ||
31143d5d DH |
681 | /* |
682 | * completion of write to server | |
683 | */ | |
684 | void afs_pages_written_back(struct afs_vnode *vnode, struct afs_call *call) | |
685 | { | |
31143d5d | 686 | struct pagevec pv; |
13524ab3 | 687 | unsigned long priv; |
31143d5d DH |
688 | unsigned count, loop; |
689 | pgoff_t first = call->first, last = call->last; | |
31143d5d | 690 | |
3b6492df | 691 | _enter("{%llx:%llu},{%lx-%lx}", |
31143d5d DH |
692 | vnode->fid.vid, vnode->fid.vnode, first, last); |
693 | ||
86679820 | 694 | pagevec_init(&pv); |
31143d5d DH |
695 | |
696 | do { | |
5bbf5d39 | 697 | _debug("done %lx-%lx", first, last); |
31143d5d DH |
698 | |
699 | count = last - first + 1; | |
700 | if (count > PAGEVEC_SIZE) | |
701 | count = PAGEVEC_SIZE; | |
4343d008 DH |
702 | pv.nr = find_get_pages_contig(vnode->vfs_inode.i_mapping, |
703 | first, count, pv.pages); | |
31143d5d DH |
704 | ASSERTCMP(pv.nr, ==, count); |
705 | ||
31143d5d | 706 | for (loop = 0; loop < count; loop++) { |
13524ab3 DH |
707 | priv = page_private(pv.pages[loop]); |
708 | trace_afs_page_dirty(vnode, tracepoint_string("clear"), | |
709 | pv.pages[loop]->index, priv); | |
4343d008 DH |
710 | set_page_private(pv.pages[loop], 0); |
711 | end_page_writeback(pv.pages[loop]); | |
31143d5d | 712 | } |
31143d5d | 713 | first += count; |
31143d5d | 714 | __pagevec_release(&pv); |
5bbf5d39 | 715 | } while (first <= last); |
31143d5d | 716 | |
4343d008 | 717 | afs_prune_wb_keys(vnode); |
31143d5d DH |
718 | _leave(""); |
719 | } | |
720 | ||
721 | /* | |
722 | * write to an AFS file | |
723 | */ | |
50b5551d | 724 | ssize_t afs_file_write(struct kiocb *iocb, struct iov_iter *from) |
31143d5d | 725 | { |
496ad9aa | 726 | struct afs_vnode *vnode = AFS_FS_I(file_inode(iocb->ki_filp)); |
31143d5d | 727 | ssize_t result; |
50b5551d | 728 | size_t count = iov_iter_count(from); |
31143d5d | 729 | |
3b6492df | 730 | _enter("{%llx:%llu},{%zu},", |
50b5551d | 731 | vnode->fid.vid, vnode->fid.vnode, count); |
31143d5d DH |
732 | |
733 | if (IS_SWAPFILE(&vnode->vfs_inode)) { | |
734 | printk(KERN_INFO | |
735 | "AFS: Attempt to write to active swap file!\n"); | |
736 | return -EBUSY; | |
737 | } | |
738 | ||
739 | if (!count) | |
740 | return 0; | |
741 | ||
50b5551d | 742 | result = generic_file_write_iter(iocb, from); |
31143d5d | 743 | |
31143d5d DH |
744 | _leave(" = %zd", result); |
745 | return result; | |
746 | } | |
747 | ||
31143d5d DH |
748 | /* |
749 | * flush any dirty pages for this process, and check for write errors. | |
750 | * - the return status from this call provides a reliable indication of | |
751 | * whether any write errors occurred for this process. | |
752 | */ | |
02c24a82 | 753 | int afs_fsync(struct file *file, loff_t start, loff_t end, int datasync) |
31143d5d | 754 | { |
3c981bfc | 755 | struct inode *inode = file_inode(file); |
3c981bfc | 756 | struct afs_vnode *vnode = AFS_FS_I(inode); |
31143d5d | 757 | |
3b6492df | 758 | _enter("{%llx:%llu},{n=%pD},%d", |
3c981bfc | 759 | vnode->fid.vid, vnode->fid.vnode, file, |
31143d5d DH |
760 | datasync); |
761 | ||
4343d008 | 762 | return file_write_and_wait_range(file, start, end); |
31143d5d | 763 | } |
9b3f26c9 DH |
764 | |
765 | /* | |
766 | * notification that a previously read-only page is about to become writable | |
767 | * - if it returns an error, the caller will deliver a bus error signal | |
768 | */ | |
0722f186 | 769 | vm_fault_t afs_page_mkwrite(struct vm_fault *vmf) |
9b3f26c9 | 770 | { |
1cf7a151 DH |
771 | struct file *file = vmf->vma->vm_file; |
772 | struct inode *inode = file_inode(file); | |
773 | struct afs_vnode *vnode = AFS_FS_I(inode); | |
774 | unsigned long priv; | |
9b3f26c9 | 775 | |
3b6492df | 776 | _enter("{{%llx:%llu}},{%lx}", |
1cf7a151 | 777 | vnode->fid.vid, vnode->fid.vnode, vmf->page->index); |
9b3f26c9 | 778 | |
1cf7a151 | 779 | sb_start_pagefault(inode->i_sb); |
9b3f26c9 | 780 | |
1cf7a151 DH |
781 | /* Wait for the page to be written to the cache before we allow it to |
782 | * be modified. We then assume the entire page will need writing back. | |
783 | */ | |
9b3f26c9 | 784 | #ifdef CONFIG_AFS_FSCACHE |
1cf7a151 | 785 | fscache_wait_on_page_write(vnode->cache, vmf->page); |
9b3f26c9 DH |
786 | #endif |
787 | ||
1cf7a151 DH |
788 | if (PageWriteback(vmf->page) && |
789 | wait_on_page_bit_killable(vmf->page, PG_writeback) < 0) | |
790 | return VM_FAULT_RETRY; | |
791 | ||
792 | if (lock_page_killable(vmf->page) < 0) | |
793 | return VM_FAULT_RETRY; | |
794 | ||
795 | /* We mustn't change page->private until writeback is complete as that | |
796 | * details the portion of the page we need to write back and we might | |
797 | * need to redirty the page if there's a problem. | |
798 | */ | |
799 | wait_on_page_writeback(vmf->page); | |
800 | ||
801 | priv = (unsigned long)PAGE_SIZE << AFS_PRIV_SHIFT; /* To */ | |
802 | priv |= 0; /* From */ | |
13524ab3 DH |
803 | trace_afs_page_dirty(vnode, tracepoint_string("mkwrite"), |
804 | vmf->page->index, priv); | |
1cf7a151 DH |
805 | SetPagePrivate(vmf->page); |
806 | set_page_private(vmf->page, priv); | |
807 | ||
808 | sb_end_pagefault(inode->i_sb); | |
809 | return VM_FAULT_LOCKED; | |
9b3f26c9 | 810 | } |
4343d008 DH |
811 | |
812 | /* | |
813 | * Prune the keys cached for writeback. The caller must hold vnode->wb_lock. | |
814 | */ | |
815 | void afs_prune_wb_keys(struct afs_vnode *vnode) | |
816 | { | |
817 | LIST_HEAD(graveyard); | |
818 | struct afs_wb_key *wbk, *tmp; | |
819 | ||
820 | /* Discard unused keys */ | |
821 | spin_lock(&vnode->wb_lock); | |
822 | ||
823 | if (!mapping_tagged(&vnode->vfs_inode.i_data, PAGECACHE_TAG_WRITEBACK) && | |
824 | !mapping_tagged(&vnode->vfs_inode.i_data, PAGECACHE_TAG_DIRTY)) { | |
825 | list_for_each_entry_safe(wbk, tmp, &vnode->wb_keys, vnode_link) { | |
826 | if (refcount_read(&wbk->usage) == 1) | |
827 | list_move(&wbk->vnode_link, &graveyard); | |
828 | } | |
829 | } | |
830 | ||
831 | spin_unlock(&vnode->wb_lock); | |
832 | ||
833 | while (!list_empty(&graveyard)) { | |
834 | wbk = list_entry(graveyard.next, struct afs_wb_key, vnode_link); | |
835 | list_del(&wbk->vnode_link); | |
836 | afs_put_wb_key(wbk); | |
837 | } | |
838 | } | |
839 | ||
840 | /* | |
841 | * Clean up a page during invalidation. | |
842 | */ | |
843 | int afs_launder_page(struct page *page) | |
844 | { | |
845 | struct address_space *mapping = page->mapping; | |
846 | struct afs_vnode *vnode = AFS_FS_I(mapping->host); | |
847 | unsigned long priv; | |
848 | unsigned int f, t; | |
849 | int ret = 0; | |
850 | ||
851 | _enter("{%lx}", page->index); | |
852 | ||
853 | priv = page_private(page); | |
854 | if (clear_page_dirty_for_io(page)) { | |
855 | f = 0; | |
856 | t = PAGE_SIZE; | |
857 | if (PagePrivate(page)) { | |
858 | f = priv & AFS_PRIV_MAX; | |
859 | t = priv >> AFS_PRIV_SHIFT; | |
860 | } | |
861 | ||
13524ab3 DH |
862 | trace_afs_page_dirty(vnode, tracepoint_string("launder"), |
863 | page->index, priv); | |
4343d008 DH |
864 | ret = afs_store_data(mapping, page->index, page->index, t, f); |
865 | } | |
866 | ||
13524ab3 DH |
867 | trace_afs_page_dirty(vnode, tracepoint_string("laundered"), |
868 | page->index, priv); | |
4343d008 DH |
869 | set_page_private(page, 0); |
870 | ClearPagePrivate(page); | |
871 | ||
872 | #ifdef CONFIG_AFS_FSCACHE | |
873 | if (PageFsCache(page)) { | |
874 | fscache_wait_on_page_write(vnode->cache, page); | |
875 | fscache_uncache_page(vnode->cache, page); | |
876 | } | |
877 | #endif | |
878 | return ret; | |
9b3f26c9 | 879 | } |