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