1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* handling of writes to regular files and writing back to the server
4 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
8 #include <linux/backing-dev.h>
9 #include <linux/slab.h>
11 #include <linux/pagemap.h>
12 #include <linux/writeback.h>
13 #include <linux/pagevec.h>
14 #include <linux/netfs.h>
15 #include <linux/fscache.h>
19 * mark a page as having been made dirty and thus needing writeback
21 int afs_set_page_dirty(struct page *page)
24 return __set_page_dirty_nobuffers(page);
28 * prepare to perform part of a write to a page
30 int afs_write_begin(struct file *file, struct address_space *mapping,
31 loff_t pos, unsigned len, unsigned flags,
32 struct page **_page, void **fsdata)
34 struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
42 _enter("{%llx:%llu},%llx,%x",
43 vnode->fid.vid, vnode->fid.vnode, pos, len);
45 /* Prefetch area to be written into the cache if we're caching this
46 * file. We need to do this before we get a lock on the page in case
47 * there's more than one writer competing for the same cache block.
49 ret = netfs_write_begin(file, mapping, pos, len, flags, &folio, fsdata,
54 index = folio_index(folio);
55 from = pos - index * PAGE_SIZE;
59 /* See if this page is already partially written in a way that we can
60 * merge the new write with.
62 if (folio_test_private(folio)) {
63 priv = (unsigned long)folio_get_private(folio);
64 f = afs_folio_dirty_from(folio, priv);
65 t = afs_folio_dirty_to(folio, priv);
68 if (folio_test_writeback(folio)) {
69 trace_afs_folio_dirty(vnode, tracepoint_string("alrdy"), folio);
70 goto flush_conflicting_write;
72 /* If the file is being filled locally, allow inter-write
73 * spaces to be merged into writes. If it's not, only write
74 * back what the user gives us.
76 if (!test_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags) &&
78 goto flush_conflicting_write;
81 *_page = &folio->page;
85 /* The previous write and this write aren't adjacent or overlapping, so
88 flush_conflicting_write:
89 _debug("flush conflict");
90 ret = folio_write_one(folio);
94 ret = folio_lock_killable(folio);
101 _leave(" = %d", ret);
106 * finalise part of a write to a page
108 int afs_write_end(struct file *file, struct address_space *mapping,
109 loff_t pos, unsigned len, unsigned copied,
110 struct page *subpage, void *fsdata)
112 struct folio *folio = page_folio(subpage);
113 struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
115 unsigned int f, from = offset_in_folio(folio, pos);
116 unsigned int t, to = from + copied;
117 loff_t i_size, maybe_i_size;
119 _enter("{%llx:%llu},{%lx}",
120 vnode->fid.vid, vnode->fid.vnode, folio_index(folio));
122 if (!folio_test_uptodate(folio)) {
128 folio_mark_uptodate(folio);
134 maybe_i_size = pos + copied;
136 i_size = i_size_read(&vnode->vfs_inode);
137 if (maybe_i_size > i_size) {
138 write_seqlock(&vnode->cb_lock);
139 i_size = i_size_read(&vnode->vfs_inode);
140 if (maybe_i_size > i_size)
141 afs_set_i_size(vnode, maybe_i_size);
142 write_sequnlock(&vnode->cb_lock);
145 if (folio_test_private(folio)) {
146 priv = (unsigned long)folio_get_private(folio);
147 f = afs_folio_dirty_from(folio, priv);
148 t = afs_folio_dirty_to(folio, priv);
153 priv = afs_folio_dirty(folio, f, t);
154 folio_change_private(folio, (void *)priv);
155 trace_afs_folio_dirty(vnode, tracepoint_string("dirty+"), folio);
157 priv = afs_folio_dirty(folio, from, to);
158 folio_attach_private(folio, (void *)priv);
159 trace_afs_folio_dirty(vnode, tracepoint_string("dirty"), folio);
162 if (folio_mark_dirty(folio))
163 _debug("dirtied %lx", folio_index(folio));
172 * kill all the pages in the given range
174 static void afs_kill_pages(struct address_space *mapping,
175 loff_t start, loff_t len)
177 struct afs_vnode *vnode = AFS_FS_I(mapping->host);
179 pgoff_t index = start / PAGE_SIZE;
180 pgoff_t last = (start + len - 1) / PAGE_SIZE, next;
182 _enter("{%llx:%llu},%llx @%llx",
183 vnode->fid.vid, vnode->fid.vnode, len, start);
186 _debug("kill %lx (to %lx)", index, last);
188 folio = filemap_get_folio(mapping, index);
194 next = folio_next_index(folio);
196 folio_clear_uptodate(folio);
197 folio_end_writeback(folio);
199 generic_error_remove_page(mapping, &folio->page);
203 } while (index = next, index <= last);
209 * Redirty all the pages in a given range.
211 static void afs_redirty_pages(struct writeback_control *wbc,
212 struct address_space *mapping,
213 loff_t start, loff_t len)
215 struct afs_vnode *vnode = AFS_FS_I(mapping->host);
217 pgoff_t index = start / PAGE_SIZE;
218 pgoff_t last = (start + len - 1) / PAGE_SIZE, next;
220 _enter("{%llx:%llu},%llx @%llx",
221 vnode->fid.vid, vnode->fid.vnode, len, start);
224 _debug("redirty %llx @%llx", len, start);
226 folio = filemap_get_folio(mapping, index);
232 next = index + folio_nr_pages(folio);
233 folio_redirty_for_writepage(wbc, folio);
234 folio_end_writeback(folio);
236 } while (index = next, index <= last);
242 * completion of write to server
244 static void afs_pages_written_back(struct afs_vnode *vnode, loff_t start, unsigned int len)
246 struct address_space *mapping = vnode->vfs_inode.i_mapping;
250 XA_STATE(xas, &mapping->i_pages, start / PAGE_SIZE);
252 _enter("{%llx:%llu},{%x @%llx}",
253 vnode->fid.vid, vnode->fid.vnode, len, start);
257 end = (start + len - 1) / PAGE_SIZE;
258 xas_for_each(&xas, folio, end) {
259 if (!folio_test_writeback(folio)) {
260 kdebug("bad %x @%llx page %lx %lx",
261 len, start, folio_index(folio), end);
262 ASSERT(folio_test_writeback(folio));
265 trace_afs_folio_dirty(vnode, tracepoint_string("clear"), folio);
266 folio_detach_private(folio);
267 folio_end_writeback(folio);
272 afs_prune_wb_keys(vnode);
277 * Find a key to use for the writeback. We cached the keys used to author the
278 * writes on the vnode. *_wbk will contain the last writeback key used or NULL
279 * and we need to start from there if it's set.
281 static int afs_get_writeback_key(struct afs_vnode *vnode,
282 struct afs_wb_key **_wbk)
284 struct afs_wb_key *wbk = NULL;
286 int ret = -ENOKEY, ret2;
288 spin_lock(&vnode->wb_lock);
290 p = (*_wbk)->vnode_link.next;
292 p = vnode->wb_keys.next;
294 while (p != &vnode->wb_keys) {
295 wbk = list_entry(p, struct afs_wb_key, vnode_link);
296 _debug("wbk %u", key_serial(wbk->key));
297 ret2 = key_validate(wbk->key);
299 refcount_inc(&wbk->usage);
300 _debug("USE WB KEY %u", key_serial(wbk->key));
310 spin_unlock(&vnode->wb_lock);
312 afs_put_wb_key(*_wbk);
317 static void afs_store_data_success(struct afs_operation *op)
319 struct afs_vnode *vnode = op->file[0].vnode;
321 op->ctime = op->file[0].scb.status.mtime_client;
322 afs_vnode_commit_status(op, &op->file[0]);
323 if (op->error == 0) {
324 if (!op->store.laundering)
325 afs_pages_written_back(vnode, op->store.pos, op->store.size);
326 afs_stat_v(vnode, n_stores);
327 atomic_long_add(op->store.size, &afs_v2net(vnode)->n_store_bytes);
331 static const struct afs_operation_ops afs_store_data_operation = {
332 .issue_afs_rpc = afs_fs_store_data,
333 .issue_yfs_rpc = yfs_fs_store_data,
334 .success = afs_store_data_success,
340 static int afs_store_data(struct afs_vnode *vnode, struct iov_iter *iter, loff_t pos,
343 struct afs_operation *op;
344 struct afs_wb_key *wbk = NULL;
345 loff_t size = iov_iter_count(iter), i_size;
348 _enter("%s{%llx:%llu.%u},%llx,%llx",
355 ret = afs_get_writeback_key(vnode, &wbk);
357 _leave(" = %d [no keys]", ret);
361 op = afs_alloc_operation(wbk->key, vnode->volume);
367 i_size = i_size_read(&vnode->vfs_inode);
369 afs_op_set_vnode(op, 0, vnode);
370 op->file[0].dv_delta = 1;
371 op->file[0].modification = true;
372 op->store.write_iter = iter;
374 op->store.size = size;
375 op->store.i_size = max(pos + size, i_size);
376 op->store.laundering = laundering;
377 op->mtime = vnode->vfs_inode.i_mtime;
378 op->flags |= AFS_OPERATION_UNINTR;
379 op->ops = &afs_store_data_operation;
382 afs_begin_vnode_operation(op);
383 afs_wait_for_operation(op);
394 ret = afs_get_writeback_key(vnode, &wbk);
397 op->key = key_get(wbk->key);
404 _leave(" = %d", op->error);
405 return afs_put_operation(op);
409 * Extend the region to be written back to include subsequent contiguously
410 * dirty pages if possible, but don't sleep while doing so.
412 * If this page holds new content, then we can include filler zeros in the
415 static void afs_extend_writeback(struct address_space *mapping,
416 struct afs_vnode *vnode,
426 unsigned int psize, filler = 0;
429 pgoff_t index = (start + len) / PAGE_SIZE;
433 XA_STATE(xas, &mapping->i_pages, index);
437 /* Firstly, we gather up a batch of contiguous dirty pages
438 * under the RCU read lock - but we can't clear the dirty flags
439 * there if any of those pages are mapped.
443 xas_for_each(&xas, folio, ULONG_MAX) {
445 if (xas_retry(&xas, folio))
447 if (xa_is_value(folio))
449 if (folio_index(folio) != index)
452 if (!folio_try_get_rcu(folio)) {
457 /* Has the page moved or been split? */
458 if (unlikely(folio != xas_reload(&xas))) {
463 if (!folio_trylock(folio)) {
467 if (!folio_test_dirty(folio) || folio_test_writeback(folio)) {
473 psize = folio_size(folio);
474 priv = (unsigned long)folio_get_private(folio);
475 f = afs_folio_dirty_from(folio, priv);
476 t = afs_folio_dirty_to(folio, priv);
477 if (f != 0 && !new_content) {
485 if (len >= max_len || *_count <= 0)
487 else if (t == psize || new_content)
490 index += folio_nr_pages(folio);
491 if (!pagevec_add(&pvec, &folio->page))
501 /* Now, if we obtained any pages, we can shift them to being
502 * writable and mark them for caching.
504 if (!pagevec_count(&pvec))
507 for (i = 0; i < pagevec_count(&pvec); i++) {
508 folio = page_folio(pvec.pages[i]);
509 trace_afs_folio_dirty(vnode, tracepoint_string("store+"), folio);
511 if (!folio_clear_dirty_for_io(folio))
513 if (folio_start_writeback(folio))
516 *_count -= folio_nr_pages(folio);
520 pagevec_release(&pvec);
528 * Synchronously write back the locked page and any subsequent non-locked dirty
531 static ssize_t afs_write_back_from_locked_folio(struct address_space *mapping,
532 struct writeback_control *wbc,
534 loff_t start, loff_t end)
536 struct afs_vnode *vnode = AFS_FS_I(mapping->host);
537 struct iov_iter iter;
539 unsigned int offset, to, len, max_len;
540 loff_t i_size = i_size_read(&vnode->vfs_inode);
541 bool new_content = test_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags);
542 long count = wbc->nr_to_write;
545 _enter(",%lx,%llx-%llx", folio_index(folio), start, end);
547 if (folio_start_writeback(folio))
550 count -= folio_nr_pages(folio);
552 /* Find all consecutive lockable dirty pages that have contiguous
553 * written regions, stopping when we find a page that is not
554 * immediately lockable, is not dirty or is missing, or we reach the
557 priv = (unsigned long)folio_get_private(folio);
558 offset = afs_folio_dirty_from(folio, priv);
559 to = afs_folio_dirty_to(folio, priv);
560 trace_afs_folio_dirty(vnode, tracepoint_string("store"), folio);
564 if (start < i_size) {
565 /* Trim the write to the EOF; the extra data is ignored. Also
566 * put an upper limit on the size of a single storedata op.
568 max_len = 65536 * 4096;
569 max_len = min_t(unsigned long long, max_len, end - start + 1);
570 max_len = min_t(unsigned long long, max_len, i_size - start);
573 (to == folio_size(folio) || new_content))
574 afs_extend_writeback(mapping, vnode, &count,
575 start, max_len, new_content, &len);
576 len = min_t(loff_t, len, max_len);
579 /* We now have a contiguous set of dirty pages, each with writeback
580 * set; the first page is still locked at this point, but all the rest
581 * have been unlocked.
585 if (start < i_size) {
586 _debug("write back %x @%llx [%llx]", len, start, i_size);
588 iov_iter_xarray(&iter, WRITE, &mapping->i_pages, start, len);
589 ret = afs_store_data(vnode, &iter, start, false);
591 _debug("write discard %x @%llx [%llx]", len, start, i_size);
593 /* The dirty region was entirely beyond the EOF. */
594 afs_pages_written_back(vnode, start, len);
600 wbc->nr_to_write = count;
605 pr_notice("kAFS: Unexpected error from FS.StoreData %d\n", ret);
613 afs_redirty_pages(wbc, mapping, start, len);
614 mapping_set_error(mapping, ret);
619 afs_redirty_pages(wbc, mapping, start, len);
620 mapping_set_error(mapping, -ENOSPC);
630 trace_afs_file_error(vnode, ret, afs_file_error_writeback_fail);
631 afs_kill_pages(mapping, start, len);
632 mapping_set_error(mapping, ret);
636 _leave(" = %d", ret);
641 * write a page back to the server
642 * - the caller locked the page for us
644 int afs_writepage(struct page *subpage, struct writeback_control *wbc)
646 struct folio *folio = page_folio(subpage);
650 _enter("{%lx},", folio_index(folio));
652 start = folio_index(folio) * PAGE_SIZE;
653 ret = afs_write_back_from_locked_folio(folio_mapping(folio), wbc,
654 folio, start, LLONG_MAX - start);
656 _leave(" = %zd", ret);
665 * write a region of pages back to the server
667 static int afs_writepages_region(struct address_space *mapping,
668 struct writeback_control *wbc,
669 loff_t start, loff_t end, loff_t *_next)
672 struct page *head_page;
676 _enter("%llx,%llx,", start, end);
679 pgoff_t index = start / PAGE_SIZE;
681 n = find_get_pages_range_tag(mapping, &index, end / PAGE_SIZE,
682 PAGECACHE_TAG_DIRTY, 1, &head_page);
686 folio = page_folio(head_page);
687 start = folio_pos(folio); /* May regress with THPs */
689 _debug("wback %lx", folio_index(folio));
691 /* At this point we hold neither the i_pages lock nor the
692 * page lock: the page may be truncated or invalidated
693 * (changing page->mapping to NULL), or even swizzled
694 * back from swapper_space to tmpfs file mapping
696 if (wbc->sync_mode != WB_SYNC_NONE) {
697 ret = folio_lock_killable(folio);
703 if (!folio_trylock(folio)) {
709 if (folio_mapping(folio) != mapping ||
710 !folio_test_dirty(folio)) {
711 start += folio_size(folio);
717 if (folio_test_writeback(folio)) {
719 if (wbc->sync_mode != WB_SYNC_NONE)
720 folio_wait_writeback(folio);
725 if (!folio_clear_dirty_for_io(folio))
727 ret = afs_write_back_from_locked_folio(mapping, wbc, folio, start, end);
730 _leave(" = %zd", ret);
737 } while (wbc->nr_to_write > 0);
740 _leave(" = 0 [%llx]", *_next);
745 * write some of the pending data back to the server
747 int afs_writepages(struct address_space *mapping,
748 struct writeback_control *wbc)
750 struct afs_vnode *vnode = AFS_FS_I(mapping->host);
756 /* We have to be careful as we can end up racing with setattr()
757 * truncating the pagecache since the caller doesn't take a lock here
760 if (wbc->sync_mode == WB_SYNC_ALL)
761 down_read(&vnode->validate_lock);
762 else if (!down_read_trylock(&vnode->validate_lock))
765 if (wbc->range_cyclic) {
766 start = mapping->writeback_index * PAGE_SIZE;
767 ret = afs_writepages_region(mapping, wbc, start, LLONG_MAX, &next);
769 mapping->writeback_index = next / PAGE_SIZE;
770 if (start > 0 && wbc->nr_to_write > 0) {
771 ret = afs_writepages_region(mapping, wbc, 0,
774 mapping->writeback_index =
778 } else if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) {
779 ret = afs_writepages_region(mapping, wbc, 0, LLONG_MAX, &next);
780 if (wbc->nr_to_write > 0 && ret == 0)
781 mapping->writeback_index = next / PAGE_SIZE;
783 ret = afs_writepages_region(mapping, wbc,
784 wbc->range_start, wbc->range_end, &next);
787 up_read(&vnode->validate_lock);
788 _leave(" = %d", ret);
793 * write to an AFS file
795 ssize_t afs_file_write(struct kiocb *iocb, struct iov_iter *from)
797 struct afs_vnode *vnode = AFS_FS_I(file_inode(iocb->ki_filp));
798 struct afs_file *af = iocb->ki_filp->private_data;
800 size_t count = iov_iter_count(from);
802 _enter("{%llx:%llu},{%zu},",
803 vnode->fid.vid, vnode->fid.vnode, count);
805 if (IS_SWAPFILE(&vnode->vfs_inode)) {
807 "AFS: Attempt to write to active swap file!\n");
814 result = afs_validate(vnode, af->key);
818 result = generic_file_write_iter(iocb, from);
820 _leave(" = %zd", result);
825 * flush any dirty pages for this process, and check for write errors.
826 * - the return status from this call provides a reliable indication of
827 * whether any write errors occurred for this process.
829 int afs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
831 struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
832 struct afs_file *af = file->private_data;
835 _enter("{%llx:%llu},{n=%pD},%d",
836 vnode->fid.vid, vnode->fid.vnode, file,
839 ret = afs_validate(vnode, af->key);
843 return file_write_and_wait_range(file, start, end);
847 * notification that a previously read-only page is about to become writable
848 * - if it returns an error, the caller will deliver a bus error signal
850 vm_fault_t afs_page_mkwrite(struct vm_fault *vmf)
852 struct folio *folio = page_folio(vmf->page);
853 struct file *file = vmf->vma->vm_file;
854 struct inode *inode = file_inode(file);
855 struct afs_vnode *vnode = AFS_FS_I(inode);
856 struct afs_file *af = file->private_data;
858 vm_fault_t ret = VM_FAULT_RETRY;
860 _enter("{{%llx:%llu}},{%lx}", vnode->fid.vid, vnode->fid.vnode, folio_index(folio));
862 afs_validate(vnode, af->key);
864 sb_start_pagefault(inode->i_sb);
866 /* Wait for the page to be written to the cache before we allow it to
867 * be modified. We then assume the entire page will need writing back.
869 #ifdef CONFIG_AFS_FSCACHE
870 if (folio_test_fscache(folio) &&
871 folio_wait_fscache_killable(folio) < 0)
875 if (folio_wait_writeback_killable(folio))
878 if (folio_lock_killable(folio) < 0)
881 /* We mustn't change folio->private until writeback is complete as that
882 * details the portion of the page we need to write back and we might
883 * need to redirty the page if there's a problem.
885 if (folio_wait_writeback_killable(folio) < 0) {
890 priv = afs_folio_dirty(folio, 0, folio_size(folio));
891 priv = afs_folio_dirty_mmapped(priv);
892 if (folio_test_private(folio)) {
893 folio_change_private(folio, (void *)priv);
894 trace_afs_folio_dirty(vnode, tracepoint_string("mkwrite+"), folio);
896 folio_attach_private(folio, (void *)priv);
897 trace_afs_folio_dirty(vnode, tracepoint_string("mkwrite"), folio);
899 file_update_time(file);
901 ret = VM_FAULT_LOCKED;
903 sb_end_pagefault(inode->i_sb);
908 * Prune the keys cached for writeback. The caller must hold vnode->wb_lock.
910 void afs_prune_wb_keys(struct afs_vnode *vnode)
912 LIST_HEAD(graveyard);
913 struct afs_wb_key *wbk, *tmp;
915 /* Discard unused keys */
916 spin_lock(&vnode->wb_lock);
918 if (!mapping_tagged(&vnode->vfs_inode.i_data, PAGECACHE_TAG_WRITEBACK) &&
919 !mapping_tagged(&vnode->vfs_inode.i_data, PAGECACHE_TAG_DIRTY)) {
920 list_for_each_entry_safe(wbk, tmp, &vnode->wb_keys, vnode_link) {
921 if (refcount_read(&wbk->usage) == 1)
922 list_move(&wbk->vnode_link, &graveyard);
926 spin_unlock(&vnode->wb_lock);
928 while (!list_empty(&graveyard)) {
929 wbk = list_entry(graveyard.next, struct afs_wb_key, vnode_link);
930 list_del(&wbk->vnode_link);
936 * Clean up a page during invalidation.
938 int afs_launder_page(struct page *subpage)
940 struct folio *folio = page_folio(subpage);
941 struct afs_vnode *vnode = AFS_FS_I(folio_inode(folio));
942 struct iov_iter iter;
943 struct bio_vec bv[1];
948 _enter("{%lx}", folio_index(folio));
950 priv = (unsigned long)folio_get_private(folio);
951 if (folio_clear_dirty_for_io(folio)) {
953 t = folio_size(folio);
954 if (folio_test_private(folio)) {
955 f = afs_folio_dirty_from(folio, priv);
956 t = afs_folio_dirty_to(folio, priv);
959 bv[0].bv_page = &folio->page;
961 bv[0].bv_len = t - f;
962 iov_iter_bvec(&iter, WRITE, bv, 1, bv[0].bv_len);
964 trace_afs_folio_dirty(vnode, tracepoint_string("launder"), folio);
965 ret = afs_store_data(vnode, &iter, folio_pos(folio) + f, true);
968 trace_afs_folio_dirty(vnode, tracepoint_string("laundered"), folio);
969 folio_detach_private(folio);
970 folio_wait_fscache(folio);