xref: /openbmc/linux/fs/afs/write.c (revision 819da022)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
231143d5dSDavid Howells /* handling of writes to regular files and writing back to the server
331143d5dSDavid Howells  *
431143d5dSDavid Howells  * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
531143d5dSDavid Howells  * Written by David Howells (dhowells@redhat.com)
631143d5dSDavid Howells  */
74343d008SDavid Howells 
84af3c9ccSAlexey Dobriyan #include <linux/backing-dev.h>
931143d5dSDavid Howells #include <linux/slab.h>
1031143d5dSDavid Howells #include <linux/fs.h>
1131143d5dSDavid Howells #include <linux/pagemap.h>
1231143d5dSDavid Howells #include <linux/writeback.h>
1331143d5dSDavid Howells #include <linux/pagevec.h>
143003bbd0SDavid Howells #include <linux/netfs.h>
1531143d5dSDavid Howells #include "internal.h"
1631143d5dSDavid Howells 
17a9eb558aSDavid Howells static int afs_writepages_region(struct address_space *mapping,
18a9eb558aSDavid Howells 				 struct writeback_control *wbc,
19a9eb558aSDavid Howells 				 loff_t start, loff_t end, loff_t *_next,
20a9eb558aSDavid Howells 				 bool max_one_loop);
21a9eb558aSDavid Howells 
22c7f75ef3SDavid Howells static void afs_write_to_cache(struct afs_vnode *vnode, loff_t start, size_t len,
23c7f75ef3SDavid Howells 			       loff_t i_size, bool caching);
24c7f75ef3SDavid Howells 
25c7f75ef3SDavid Howells #ifdef CONFIG_AFS_FSCACHE
2631143d5dSDavid Howells /*
27c7f75ef3SDavid Howells  * Mark a page as having been made dirty and thus needing writeback.  We also
28c7f75ef3SDavid Howells  * need to pin the cache object to write back to.
2931143d5dSDavid Howells  */
308fb72b4aSMatthew Wilcox (Oracle) bool afs_dirty_folio(struct address_space *mapping, struct folio *folio)
3131143d5dSDavid Howells {
328fb72b4aSMatthew Wilcox (Oracle) 	return fscache_dirty_folio(mapping, folio,
338fb72b4aSMatthew Wilcox (Oracle) 				afs_vnode_cache(AFS_FS_I(mapping->host)));
3431143d5dSDavid Howells }
35c7f75ef3SDavid Howells static void afs_folio_start_fscache(bool caching, struct folio *folio)
36c7f75ef3SDavid Howells {
37c7f75ef3SDavid Howells 	if (caching)
38c7f75ef3SDavid Howells 		folio_start_fscache(folio);
39c7f75ef3SDavid Howells }
40c7f75ef3SDavid Howells #else
41c7f75ef3SDavid Howells static void afs_folio_start_fscache(bool caching, struct folio *folio)
42c7f75ef3SDavid Howells {
43c7f75ef3SDavid Howells }
44c7f75ef3SDavid Howells #endif
4531143d5dSDavid Howells 
4631143d5dSDavid Howells /*
47a9eb558aSDavid Howells  * Flush out a conflicting write.  This may extend the write to the surrounding
48a9eb558aSDavid Howells  * pages if also dirty and contiguous to the conflicting region..
49a9eb558aSDavid Howells  */
50a9eb558aSDavid Howells static int afs_flush_conflicting_write(struct address_space *mapping,
51a9eb558aSDavid Howells 				       struct folio *folio)
52a9eb558aSDavid Howells {
53a9eb558aSDavid Howells 	struct writeback_control wbc = {
54a9eb558aSDavid Howells 		.sync_mode	= WB_SYNC_ALL,
55a9eb558aSDavid Howells 		.nr_to_write	= LONG_MAX,
56a9eb558aSDavid Howells 		.range_start	= folio_pos(folio),
57a9eb558aSDavid Howells 		.range_end	= LLONG_MAX,
58a9eb558aSDavid Howells 	};
59a9eb558aSDavid Howells 	loff_t next;
60a9eb558aSDavid Howells 
61a9eb558aSDavid Howells 	return afs_writepages_region(mapping, &wbc, folio_pos(folio), LLONG_MAX,
62a9eb558aSDavid Howells 				     &next, true);
63a9eb558aSDavid Howells }
64a9eb558aSDavid Howells 
65a9eb558aSDavid Howells /*
6631143d5dSDavid Howells  * prepare to perform part of a write to a page
6731143d5dSDavid Howells  */
6815b4650eSNick Piggin int afs_write_begin(struct file *file, struct address_space *mapping,
699d6b0cd7SMatthew Wilcox (Oracle) 		    loff_t pos, unsigned len,
7021db2cdcSDavid Howells 		    struct page **_page, void **fsdata)
7131143d5dSDavid Howells {
72496ad9aaSAl Viro 	struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
7378525c74SDavid Howells 	struct folio *folio;
744343d008SDavid Howells 	unsigned long priv;
75e87b03f5SDavid Howells 	unsigned f, from;
76e87b03f5SDavid Howells 	unsigned t, to;
77e87b03f5SDavid Howells 	pgoff_t index;
7831143d5dSDavid Howells 	int ret;
7931143d5dSDavid Howells 
80e87b03f5SDavid Howells 	_enter("{%llx:%llu},%llx,%x",
81e87b03f5SDavid Howells 	       vnode->fid.vid, vnode->fid.vnode, pos, len);
8231143d5dSDavid Howells 
833003bbd0SDavid Howells 	/* Prefetch area to be written into the cache if we're caching this
843003bbd0SDavid Howells 	 * file.  We need to do this before we get a lock on the page in case
853003bbd0SDavid Howells 	 * there's more than one writer competing for the same cache block.
863003bbd0SDavid Howells 	 */
87e81fb419SLinus Torvalds 	ret = netfs_write_begin(&vnode->netfs, file, mapping, pos, len, &folio, fsdata);
883003bbd0SDavid Howells 	if (ret < 0)
8931143d5dSDavid Howells 		return ret;
90630f5ddaSDavid Howells 
9178525c74SDavid Howells 	index = folio_index(folio);
92e87b03f5SDavid Howells 	from = pos - index * PAGE_SIZE;
93e87b03f5SDavid Howells 	to = from + len;
94e87b03f5SDavid Howells 
9531143d5dSDavid Howells try_again:
964343d008SDavid Howells 	/* See if this page is already partially written in a way that we can
974343d008SDavid Howells 	 * merge the new write with.
984343d008SDavid Howells 	 */
9978525c74SDavid Howells 	if (folio_test_private(folio)) {
10078525c74SDavid Howells 		priv = (unsigned long)folio_get_private(folio);
10178525c74SDavid Howells 		f = afs_folio_dirty_from(folio, priv);
10278525c74SDavid Howells 		t = afs_folio_dirty_to(folio, priv);
1034343d008SDavid Howells 		ASSERTCMP(f, <=, t);
10431143d5dSDavid Howells 
10578525c74SDavid Howells 		if (folio_test_writeback(folio)) {
10678525c74SDavid Howells 			trace_afs_folio_dirty(vnode, tracepoint_string("alrdy"), folio);
107a9eb558aSDavid Howells 			folio_unlock(folio);
108a9eb558aSDavid Howells 			goto wait_for_writeback;
1095a039c32SDavid Howells 		}
1105a813276SDavid Howells 		/* If the file is being filled locally, allow inter-write
1115a813276SDavid Howells 		 * spaces to be merged into writes.  If it's not, only write
1125a813276SDavid Howells 		 * back what the user gives us.
1135a813276SDavid Howells 		 */
1145a813276SDavid Howells 		if (!test_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags) &&
1155a813276SDavid Howells 		    (to < f || from > t))
1164343d008SDavid Howells 			goto flush_conflicting_write;
11731143d5dSDavid Howells 	}
11831143d5dSDavid Howells 
1198549a263SDavid Howells 	*_page = folio_file_page(folio, pos / PAGE_SIZE);
1204343d008SDavid Howells 	_leave(" = 0");
12131143d5dSDavid Howells 	return 0;
12231143d5dSDavid Howells 
1234343d008SDavid Howells 	/* The previous write and this write aren't adjacent or overlapping, so
1244343d008SDavid Howells 	 * flush the page out.
1254343d008SDavid Howells 	 */
1264343d008SDavid Howells flush_conflicting_write:
127a9eb558aSDavid Howells 	trace_afs_folio_dirty(vnode, tracepoint_string("confl"), folio);
128a9eb558aSDavid Howells 	folio_unlock(folio);
129a9eb558aSDavid Howells 
130a9eb558aSDavid Howells 	ret = afs_flush_conflicting_write(mapping, folio);
131a9eb558aSDavid Howells 	if (ret < 0)
132a9eb558aSDavid Howells 		goto error;
133a9eb558aSDavid Howells 
134a9eb558aSDavid Howells wait_for_writeback:
135a9eb558aSDavid Howells 	ret = folio_wait_writeback_killable(folio);
13621db2cdcSDavid Howells 	if (ret < 0)
13721db2cdcSDavid Howells 		goto error;
13831143d5dSDavid Howells 
13978525c74SDavid Howells 	ret = folio_lock_killable(folio);
14021db2cdcSDavid Howells 	if (ret < 0)
14121db2cdcSDavid Howells 		goto error;
14221db2cdcSDavid Howells 	goto try_again;
14321db2cdcSDavid Howells 
14421db2cdcSDavid Howells error:
14578525c74SDavid Howells 	folio_put(folio);
1464343d008SDavid Howells 	_leave(" = %d", ret);
1474343d008SDavid Howells 	return ret;
1484343d008SDavid Howells }
14931143d5dSDavid Howells 
15031143d5dSDavid Howells /*
15131143d5dSDavid Howells  * finalise part of a write to a page
15231143d5dSDavid Howells  */
15315b4650eSNick Piggin int afs_write_end(struct file *file, struct address_space *mapping,
15415b4650eSNick Piggin 		  loff_t pos, unsigned len, unsigned copied,
15578525c74SDavid Howells 		  struct page *subpage, void *fsdata)
15631143d5dSDavid Howells {
15778525c74SDavid Howells 	struct folio *folio = page_folio(subpage);
158496ad9aaSAl Viro 	struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
159f792e3acSDavid Howells 	unsigned long priv;
16078525c74SDavid Howells 	unsigned int f, from = offset_in_folio(folio, pos);
161f792e3acSDavid Howells 	unsigned int t, to = from + copied;
162c7f75ef3SDavid Howells 	loff_t i_size, write_end_pos;
16331143d5dSDavid Howells 
1643b6492dfSDavid Howells 	_enter("{%llx:%llu},{%lx}",
16578525c74SDavid Howells 	       vnode->fid.vid, vnode->fid.vnode, folio_index(folio));
16631143d5dSDavid Howells 
16778525c74SDavid Howells 	if (!folio_test_uptodate(folio)) {
16866e9c6a8SDavid Howells 		if (copied < len) {
16966e9c6a8SDavid Howells 			copied = 0;
17066e9c6a8SDavid Howells 			goto out;
17166e9c6a8SDavid Howells 		}
17266e9c6a8SDavid Howells 
17378525c74SDavid Howells 		folio_mark_uptodate(folio);
17466e9c6a8SDavid Howells 	}
17566e9c6a8SDavid Howells 
1763ad216eeSDavid Howells 	if (copied == 0)
1773ad216eeSDavid Howells 		goto out;
1783ad216eeSDavid Howells 
179c7f75ef3SDavid Howells 	write_end_pos = pos + copied;
18031143d5dSDavid Howells 
181874c8ca1SDavid Howells 	i_size = i_size_read(&vnode->netfs.inode);
182c7f75ef3SDavid Howells 	if (write_end_pos > i_size) {
1831f32ef79SDavid Howells 		write_seqlock(&vnode->cb_lock);
184874c8ca1SDavid Howells 		i_size = i_size_read(&vnode->netfs.inode);
185c7f75ef3SDavid Howells 		if (write_end_pos > i_size)
186c7f75ef3SDavid Howells 			afs_set_i_size(vnode, write_end_pos);
1871f32ef79SDavid Howells 		write_sequnlock(&vnode->cb_lock);
188c7f75ef3SDavid Howells 		fscache_update_cookie(afs_vnode_cache(vnode), NULL, &write_end_pos);
18931143d5dSDavid Howells 	}
19031143d5dSDavid Howells 
19178525c74SDavid Howells 	if (folio_test_private(folio)) {
19278525c74SDavid Howells 		priv = (unsigned long)folio_get_private(folio);
19378525c74SDavid Howells 		f = afs_folio_dirty_from(folio, priv);
19478525c74SDavid Howells 		t = afs_folio_dirty_to(folio, priv);
195f792e3acSDavid Howells 		if (from < f)
196f792e3acSDavid Howells 			f = from;
197f792e3acSDavid Howells 		if (to > t)
198f792e3acSDavid Howells 			t = to;
19978525c74SDavid Howells 		priv = afs_folio_dirty(folio, f, t);
20078525c74SDavid Howells 		folio_change_private(folio, (void *)priv);
20178525c74SDavid Howells 		trace_afs_folio_dirty(vnode, tracepoint_string("dirty+"), folio);
202f792e3acSDavid Howells 	} else {
20378525c74SDavid Howells 		priv = afs_folio_dirty(folio, from, to);
20478525c74SDavid Howells 		folio_attach_private(folio, (void *)priv);
20578525c74SDavid Howells 		trace_afs_folio_dirty(vnode, tracepoint_string("dirty"), folio);
206f792e3acSDavid Howells 	}
207f792e3acSDavid Howells 
20878525c74SDavid Howells 	if (folio_mark_dirty(folio))
20978525c74SDavid Howells 		_debug("dirtied %lx", folio_index(folio));
210afae457dSDavid Howells 
211afae457dSDavid Howells out:
21278525c74SDavid Howells 	folio_unlock(folio);
21378525c74SDavid Howells 	folio_put(folio);
2143003bbd0SDavid Howells 	return copied;
21531143d5dSDavid Howells }
21631143d5dSDavid Howells 
21731143d5dSDavid Howells /*
21831143d5dSDavid Howells  * kill all the pages in the given range
21931143d5dSDavid Howells  */
2204343d008SDavid Howells static void afs_kill_pages(struct address_space *mapping,
221e87b03f5SDavid Howells 			   loff_t start, loff_t len)
22231143d5dSDavid Howells {
2234343d008SDavid Howells 	struct afs_vnode *vnode = AFS_FS_I(mapping->host);
22478525c74SDavid Howells 	struct folio *folio;
22578525c74SDavid Howells 	pgoff_t index = start / PAGE_SIZE;
22678525c74SDavid Howells 	pgoff_t last = (start + len - 1) / PAGE_SIZE, next;
22731143d5dSDavid Howells 
228e87b03f5SDavid Howells 	_enter("{%llx:%llu},%llx @%llx",
229e87b03f5SDavid Howells 	       vnode->fid.vid, vnode->fid.vnode, len, start);
23031143d5dSDavid Howells 
23131143d5dSDavid Howells 	do {
23278525c74SDavid Howells 		_debug("kill %lx (to %lx)", index, last);
23331143d5dSDavid Howells 
23478525c74SDavid Howells 		folio = filemap_get_folio(mapping, index);
23566dabbb6SChristoph Hellwig 		if (IS_ERR(folio)) {
23678525c74SDavid Howells 			next = index + 1;
23778525c74SDavid Howells 			continue;
2384343d008SDavid Howells 		}
2394343d008SDavid Howells 
24078525c74SDavid Howells 		next = folio_next_index(folio);
24178525c74SDavid Howells 
24278525c74SDavid Howells 		folio_clear_uptodate(folio);
24378525c74SDavid Howells 		folio_end_writeback(folio);
24478525c74SDavid Howells 		folio_lock(folio);
24578525c74SDavid Howells 		generic_error_remove_page(mapping, &folio->page);
24678525c74SDavid Howells 		folio_unlock(folio);
24778525c74SDavid Howells 		folio_put(folio);
24878525c74SDavid Howells 
24978525c74SDavid Howells 	} while (index = next, index <= last);
2504343d008SDavid Howells 
2514343d008SDavid Howells 	_leave("");
2524343d008SDavid Howells }
2534343d008SDavid Howells 
2544343d008SDavid Howells /*
2554343d008SDavid Howells  * Redirty all the pages in a given range.
2564343d008SDavid Howells  */
2574343d008SDavid Howells static void afs_redirty_pages(struct writeback_control *wbc,
2584343d008SDavid Howells 			      struct address_space *mapping,
259e87b03f5SDavid Howells 			      loff_t start, loff_t len)
2604343d008SDavid Howells {
2614343d008SDavid Howells 	struct afs_vnode *vnode = AFS_FS_I(mapping->host);
26278525c74SDavid Howells 	struct folio *folio;
26378525c74SDavid Howells 	pgoff_t index = start / PAGE_SIZE;
26478525c74SDavid Howells 	pgoff_t last = (start + len - 1) / PAGE_SIZE, next;
2654343d008SDavid Howells 
266e87b03f5SDavid Howells 	_enter("{%llx:%llu},%llx @%llx",
267e87b03f5SDavid Howells 	       vnode->fid.vid, vnode->fid.vnode, len, start);
2684343d008SDavid Howells 
2694343d008SDavid Howells 	do {
270e87b03f5SDavid Howells 		_debug("redirty %llx @%llx", len, start);
2714343d008SDavid Howells 
27278525c74SDavid Howells 		folio = filemap_get_folio(mapping, index);
27366dabbb6SChristoph Hellwig 		if (IS_ERR(folio)) {
27478525c74SDavid Howells 			next = index + 1;
27578525c74SDavid Howells 			continue;
27631143d5dSDavid Howells 		}
27731143d5dSDavid Howells 
27878525c74SDavid Howells 		next = index + folio_nr_pages(folio);
27978525c74SDavid Howells 		folio_redirty_for_writepage(wbc, folio);
28078525c74SDavid Howells 		folio_end_writeback(folio);
28178525c74SDavid Howells 		folio_put(folio);
28278525c74SDavid Howells 	} while (index = next, index <= last);
28331143d5dSDavid Howells 
28431143d5dSDavid Howells 	_leave("");
28531143d5dSDavid Howells }
28631143d5dSDavid Howells 
28731143d5dSDavid Howells /*
288a58823acSDavid Howells  * completion of write to server
289a58823acSDavid Howells  */
290e87b03f5SDavid Howells static void afs_pages_written_back(struct afs_vnode *vnode, loff_t start, unsigned int len)
291a58823acSDavid Howells {
292874c8ca1SDavid Howells 	struct address_space *mapping = vnode->netfs.inode.i_mapping;
29378525c74SDavid Howells 	struct folio *folio;
294e87b03f5SDavid Howells 	pgoff_t end;
295bd80d8a8SDavid Howells 
296e87b03f5SDavid Howells 	XA_STATE(xas, &mapping->i_pages, start / PAGE_SIZE);
297a58823acSDavid Howells 
298e87b03f5SDavid Howells 	_enter("{%llx:%llu},{%x @%llx}",
299e87b03f5SDavid Howells 	       vnode->fid.vid, vnode->fid.vnode, len, start);
300a58823acSDavid Howells 
301bd80d8a8SDavid Howells 	rcu_read_lock();
302a58823acSDavid Howells 
303e87b03f5SDavid Howells 	end = (start + len - 1) / PAGE_SIZE;
30478525c74SDavid Howells 	xas_for_each(&xas, folio, end) {
30578525c74SDavid Howells 		if (!folio_test_writeback(folio)) {
30678525c74SDavid Howells 			kdebug("bad %x @%llx page %lx %lx",
30778525c74SDavid Howells 			       len, start, folio_index(folio), end);
30878525c74SDavid Howells 			ASSERT(folio_test_writeback(folio));
309e87b03f5SDavid Howells 		}
310a58823acSDavid Howells 
31178525c74SDavid Howells 		trace_afs_folio_dirty(vnode, tracepoint_string("clear"), folio);
31278525c74SDavid Howells 		folio_detach_private(folio);
31378525c74SDavid Howells 		folio_end_writeback(folio);
314a58823acSDavid Howells 	}
315bd80d8a8SDavid Howells 
316bd80d8a8SDavid Howells 	rcu_read_unlock();
317a58823acSDavid Howells 
318a58823acSDavid Howells 	afs_prune_wb_keys(vnode);
319a58823acSDavid Howells 	_leave("");
320a58823acSDavid Howells }
321a58823acSDavid Howells 
322a58823acSDavid Howells /*
323e49c7b2fSDavid Howells  * Find a key to use for the writeback.  We cached the keys used to author the
324e49c7b2fSDavid Howells  * writes on the vnode.  *_wbk will contain the last writeback key used or NULL
325e49c7b2fSDavid Howells  * and we need to start from there if it's set.
326e49c7b2fSDavid Howells  */
327e49c7b2fSDavid Howells static int afs_get_writeback_key(struct afs_vnode *vnode,
328e49c7b2fSDavid Howells 				 struct afs_wb_key **_wbk)
329e49c7b2fSDavid Howells {
330e49c7b2fSDavid Howells 	struct afs_wb_key *wbk = NULL;
331e49c7b2fSDavid Howells 	struct list_head *p;
332e49c7b2fSDavid Howells 	int ret = -ENOKEY, ret2;
333e49c7b2fSDavid Howells 
334e49c7b2fSDavid Howells 	spin_lock(&vnode->wb_lock);
335e49c7b2fSDavid Howells 	if (*_wbk)
336e49c7b2fSDavid Howells 		p = (*_wbk)->vnode_link.next;
337e49c7b2fSDavid Howells 	else
338e49c7b2fSDavid Howells 		p = vnode->wb_keys.next;
339e49c7b2fSDavid Howells 
340e49c7b2fSDavid Howells 	while (p != &vnode->wb_keys) {
341e49c7b2fSDavid Howells 		wbk = list_entry(p, struct afs_wb_key, vnode_link);
342e49c7b2fSDavid Howells 		_debug("wbk %u", key_serial(wbk->key));
343e49c7b2fSDavid Howells 		ret2 = key_validate(wbk->key);
344e49c7b2fSDavid Howells 		if (ret2 == 0) {
345e49c7b2fSDavid Howells 			refcount_inc(&wbk->usage);
346e49c7b2fSDavid Howells 			_debug("USE WB KEY %u", key_serial(wbk->key));
347e49c7b2fSDavid Howells 			break;
348e49c7b2fSDavid Howells 		}
349e49c7b2fSDavid Howells 
350e49c7b2fSDavid Howells 		wbk = NULL;
351e49c7b2fSDavid Howells 		if (ret == -ENOKEY)
352e49c7b2fSDavid Howells 			ret = ret2;
353e49c7b2fSDavid Howells 		p = p->next;
354e49c7b2fSDavid Howells 	}
355e49c7b2fSDavid Howells 
356e49c7b2fSDavid Howells 	spin_unlock(&vnode->wb_lock);
357e49c7b2fSDavid Howells 	if (*_wbk)
358e49c7b2fSDavid Howells 		afs_put_wb_key(*_wbk);
359e49c7b2fSDavid Howells 	*_wbk = wbk;
360e49c7b2fSDavid Howells 	return 0;
361e49c7b2fSDavid Howells }
362e49c7b2fSDavid Howells 
363e49c7b2fSDavid Howells static void afs_store_data_success(struct afs_operation *op)
364e49c7b2fSDavid Howells {
365e49c7b2fSDavid Howells 	struct afs_vnode *vnode = op->file[0].vnode;
366e49c7b2fSDavid Howells 
367da8d0755SDavid Howells 	op->ctime = op->file[0].scb.status.mtime_client;
368e49c7b2fSDavid Howells 	afs_vnode_commit_status(op, &op->file[0]);
369e49c7b2fSDavid Howells 	if (op->error == 0) {
370d383e346SDavid Howells 		if (!op->store.laundering)
371e87b03f5SDavid Howells 			afs_pages_written_back(vnode, op->store.pos, op->store.size);
372e49c7b2fSDavid Howells 		afs_stat_v(vnode, n_stores);
373bd80d8a8SDavid Howells 		atomic_long_add(op->store.size, &afs_v2net(vnode)->n_store_bytes);
374e49c7b2fSDavid Howells 	}
375e49c7b2fSDavid Howells }
376e49c7b2fSDavid Howells 
377e49c7b2fSDavid Howells static const struct afs_operation_ops afs_store_data_operation = {
378e49c7b2fSDavid Howells 	.issue_afs_rpc	= afs_fs_store_data,
379e49c7b2fSDavid Howells 	.issue_yfs_rpc	= yfs_fs_store_data,
380e49c7b2fSDavid Howells 	.success	= afs_store_data_success,
381e49c7b2fSDavid Howells };
382e49c7b2fSDavid Howells 
383e49c7b2fSDavid Howells /*
384d2ddc776SDavid Howells  * write to a file
38531143d5dSDavid Howells  */
386e87b03f5SDavid Howells static int afs_store_data(struct afs_vnode *vnode, struct iov_iter *iter, loff_t pos,
387bd80d8a8SDavid Howells 			  bool laundering)
38831143d5dSDavid Howells {
389e49c7b2fSDavid Howells 	struct afs_operation *op;
3904343d008SDavid Howells 	struct afs_wb_key *wbk = NULL;
391ab487a4cSDavid Howells 	loff_t size = iov_iter_count(iter);
392bd80d8a8SDavid Howells 	int ret = -ENOKEY;
393d2ddc776SDavid Howells 
394bd80d8a8SDavid Howells 	_enter("%s{%llx:%llu.%u},%llx,%llx",
395d2ddc776SDavid Howells 	       vnode->volume->name,
396d2ddc776SDavid Howells 	       vnode->fid.vid,
397d2ddc776SDavid Howells 	       vnode->fid.vnode,
398d2ddc776SDavid Howells 	       vnode->fid.unique,
399bd80d8a8SDavid Howells 	       size, pos);
400d2ddc776SDavid Howells 
401e49c7b2fSDavid Howells 	ret = afs_get_writeback_key(vnode, &wbk);
402e49c7b2fSDavid Howells 	if (ret) {
4034343d008SDavid Howells 		_leave(" = %d [no keys]", ret);
4044343d008SDavid Howells 		return ret;
405d2ddc776SDavid Howells 	}
406d2ddc776SDavid Howells 
407e49c7b2fSDavid Howells 	op = afs_alloc_operation(wbk->key, vnode->volume);
408e49c7b2fSDavid Howells 	if (IS_ERR(op)) {
409e49c7b2fSDavid Howells 		afs_put_wb_key(wbk);
410e49c7b2fSDavid Howells 		return -ENOMEM;
411d2ddc776SDavid Howells 	}
412d2ddc776SDavid Howells 
413e49c7b2fSDavid Howells 	afs_op_set_vnode(op, 0, vnode);
414e49c7b2fSDavid Howells 	op->file[0].dv_delta = 1;
41522650f14SDavid Howells 	op->file[0].modification = true;
416bd80d8a8SDavid Howells 	op->store.write_iter = iter;
417bd80d8a8SDavid Howells 	op->store.pos = pos;
418bd80d8a8SDavid Howells 	op->store.size = size;
419874c8ca1SDavid Howells 	op->store.i_size = max(pos + size, vnode->netfs.remote_i_size);
420d383e346SDavid Howells 	op->store.laundering = laundering;
421874c8ca1SDavid Howells 	op->mtime = vnode->netfs.inode.i_mtime;
422811f04baSDavid Howells 	op->flags |= AFS_OPERATION_UNINTR;
423e49c7b2fSDavid Howells 	op->ops = &afs_store_data_operation;
424e49c7b2fSDavid Howells 
425e49c7b2fSDavid Howells try_next_key:
426e49c7b2fSDavid Howells 	afs_begin_vnode_operation(op);
427e49c7b2fSDavid Howells 	afs_wait_for_operation(op);
428e49c7b2fSDavid Howells 
429e49c7b2fSDavid Howells 	switch (op->error) {
4304343d008SDavid Howells 	case -EACCES:
4314343d008SDavid Howells 	case -EPERM:
4324343d008SDavid Howells 	case -ENOKEY:
4334343d008SDavid Howells 	case -EKEYEXPIRED:
4344343d008SDavid Howells 	case -EKEYREJECTED:
4354343d008SDavid Howells 	case -EKEYREVOKED:
4364343d008SDavid Howells 		_debug("next");
437e49c7b2fSDavid Howells 
438e49c7b2fSDavid Howells 		ret = afs_get_writeback_key(vnode, &wbk);
439e49c7b2fSDavid Howells 		if (ret == 0) {
440e49c7b2fSDavid Howells 			key_put(op->key);
441e49c7b2fSDavid Howells 			op->key = key_get(wbk->key);
4424343d008SDavid Howells 			goto try_next_key;
4434343d008SDavid Howells 		}
444e49c7b2fSDavid Howells 		break;
445e49c7b2fSDavid Howells 	}
4464343d008SDavid Howells 
4474343d008SDavid Howells 	afs_put_wb_key(wbk);
448e49c7b2fSDavid Howells 	_leave(" = %d", op->error);
449e49c7b2fSDavid Howells 	return afs_put_operation(op);
450d2ddc776SDavid Howells }
451d2ddc776SDavid Howells 
452d2ddc776SDavid Howells /*
453810caa3eSDavid Howells  * Extend the region to be written back to include subsequent contiguously
454810caa3eSDavid Howells  * dirty pages if possible, but don't sleep while doing so.
455810caa3eSDavid Howells  *
456810caa3eSDavid Howells  * If this page holds new content, then we can include filler zeros in the
457810caa3eSDavid Howells  * writeback.
45831143d5dSDavid Howells  */
459810caa3eSDavid Howells static void afs_extend_writeback(struct address_space *mapping,
460810caa3eSDavid Howells 				 struct afs_vnode *vnode,
461810caa3eSDavid Howells 				 long *_count,
462e87b03f5SDavid Howells 				 loff_t start,
463e87b03f5SDavid Howells 				 loff_t max_len,
464e87b03f5SDavid Howells 				 bool new_content,
465c7f75ef3SDavid Howells 				 bool caching,
466e87b03f5SDavid Howells 				 unsigned int *_len)
46731143d5dSDavid Howells {
468e87b03f5SDavid Howells 	struct pagevec pvec;
46978525c74SDavid Howells 	struct folio *folio;
470e87b03f5SDavid Howells 	unsigned long priv;
471e87b03f5SDavid Howells 	unsigned int psize, filler = 0;
472e87b03f5SDavid Howells 	unsigned int f, t;
473e87b03f5SDavid Howells 	loff_t len = *_len;
474e87b03f5SDavid Howells 	pgoff_t index = (start + len) / PAGE_SIZE;
475e87b03f5SDavid Howells 	bool stop = true;
476e87b03f5SDavid Howells 	unsigned int i;
4774343d008SDavid Howells 
478e87b03f5SDavid Howells 	XA_STATE(xas, &mapping->i_pages, index);
479e87b03f5SDavid Howells 	pagevec_init(&pvec);
480e87b03f5SDavid Howells 
48131143d5dSDavid Howells 	do {
482e87b03f5SDavid Howells 		/* Firstly, we gather up a batch of contiguous dirty pages
483e87b03f5SDavid Howells 		 * under the RCU read lock - but we can't clear the dirty flags
484e87b03f5SDavid Howells 		 * there if any of those pages are mapped.
485e87b03f5SDavid Howells 		 */
486e87b03f5SDavid Howells 		rcu_read_lock();
487e87b03f5SDavid Howells 
48878525c74SDavid Howells 		xas_for_each(&xas, folio, ULONG_MAX) {
489e87b03f5SDavid Howells 			stop = true;
49078525c74SDavid Howells 			if (xas_retry(&xas, folio))
491e87b03f5SDavid Howells 				continue;
49278525c74SDavid Howells 			if (xa_is_value(folio))
493e87b03f5SDavid Howells 				break;
49478525c74SDavid Howells 			if (folio_index(folio) != index)
495e87b03f5SDavid Howells 				break;
496e87b03f5SDavid Howells 
49778525c74SDavid Howells 			if (!folio_try_get_rcu(folio)) {
498e87b03f5SDavid Howells 				xas_reset(&xas);
499e87b03f5SDavid Howells 				continue;
50031143d5dSDavid Howells 			}
50131143d5dSDavid Howells 
502e87b03f5SDavid Howells 			/* Has the page moved or been split? */
50378525c74SDavid Howells 			if (unlikely(folio != xas_reload(&xas))) {
50478525c74SDavid Howells 				folio_put(folio);
5055a813276SDavid Howells 				break;
506581b2027SDavid Howells 			}
507e87b03f5SDavid Howells 
50878525c74SDavid Howells 			if (!folio_trylock(folio)) {
50978525c74SDavid Howells 				folio_put(folio);
51031143d5dSDavid Howells 				break;
511581b2027SDavid Howells 			}
512c7f75ef3SDavid Howells 			if (!folio_test_dirty(folio) ||
513c7f75ef3SDavid Howells 			    folio_test_writeback(folio) ||
514c7f75ef3SDavid Howells 			    folio_test_fscache(folio)) {
51578525c74SDavid Howells 				folio_unlock(folio);
51678525c74SDavid Howells 				folio_put(folio);
51731143d5dSDavid Howells 				break;
51831143d5dSDavid Howells 			}
5194343d008SDavid Howells 
52078525c74SDavid Howells 			psize = folio_size(folio);
52178525c74SDavid Howells 			priv = (unsigned long)folio_get_private(folio);
52278525c74SDavid Howells 			f = afs_folio_dirty_from(folio, priv);
52378525c74SDavid Howells 			t = afs_folio_dirty_to(folio, priv);
524810caa3eSDavid Howells 			if (f != 0 && !new_content) {
52578525c74SDavid Howells 				folio_unlock(folio);
52678525c74SDavid Howells 				folio_put(folio);
5274343d008SDavid Howells 				break;
5284343d008SDavid Howells 			}
5294343d008SDavid Howells 
530e87b03f5SDavid Howells 			len += filler + t;
531e87b03f5SDavid Howells 			filler = psize - t;
532e87b03f5SDavid Howells 			if (len >= max_len || *_count <= 0)
533e87b03f5SDavid Howells 				stop = true;
534e87b03f5SDavid Howells 			else if (t == psize || new_content)
535e87b03f5SDavid Howells 				stop = false;
536e87b03f5SDavid Howells 
53778525c74SDavid Howells 			index += folio_nr_pages(folio);
53878525c74SDavid Howells 			if (!pagevec_add(&pvec, &folio->page))
539e87b03f5SDavid Howells 				break;
540e87b03f5SDavid Howells 			if (stop)
541e87b03f5SDavid Howells 				break;
542e87b03f5SDavid Howells 		}
543e87b03f5SDavid Howells 
544e87b03f5SDavid Howells 		if (!stop)
545e87b03f5SDavid Howells 			xas_pause(&xas);
546e87b03f5SDavid Howells 		rcu_read_unlock();
547e87b03f5SDavid Howells 
548e87b03f5SDavid Howells 		/* Now, if we obtained any pages, we can shift them to being
549e87b03f5SDavid Howells 		 * writable and mark them for caching.
550e87b03f5SDavid Howells 		 */
551e87b03f5SDavid Howells 		if (!pagevec_count(&pvec))
552e87b03f5SDavid Howells 			break;
553e87b03f5SDavid Howells 
554e87b03f5SDavid Howells 		for (i = 0; i < pagevec_count(&pvec); i++) {
55578525c74SDavid Howells 			folio = page_folio(pvec.pages[i]);
55678525c74SDavid Howells 			trace_afs_folio_dirty(vnode, tracepoint_string("store+"), folio);
55713524ab3SDavid Howells 
55878525c74SDavid Howells 			if (!folio_clear_dirty_for_io(folio))
55931143d5dSDavid Howells 				BUG();
56078525c74SDavid Howells 			if (folio_start_writeback(folio))
56131143d5dSDavid Howells 				BUG();
562c7f75ef3SDavid Howells 			afs_folio_start_fscache(caching, folio);
563e87b03f5SDavid Howells 
56478525c74SDavid Howells 			*_count -= folio_nr_pages(folio);
56578525c74SDavid Howells 			folio_unlock(folio);
56631143d5dSDavid Howells 		}
56731143d5dSDavid Howells 
568e87b03f5SDavid Howells 		pagevec_release(&pvec);
569e87b03f5SDavid Howells 		cond_resched();
570e87b03f5SDavid Howells 	} while (!stop);
57131143d5dSDavid Howells 
572e87b03f5SDavid Howells 	*_len = len;
573810caa3eSDavid Howells }
574810caa3eSDavid Howells 
575810caa3eSDavid Howells /*
576810caa3eSDavid Howells  * Synchronously write back the locked page and any subsequent non-locked dirty
577810caa3eSDavid Howells  * pages.
578810caa3eSDavid Howells  */
57978525c74SDavid Howells static ssize_t afs_write_back_from_locked_folio(struct address_space *mapping,
580810caa3eSDavid Howells 						struct writeback_control *wbc,
58178525c74SDavid Howells 						struct folio *folio,
582e87b03f5SDavid Howells 						loff_t start, loff_t end)
583810caa3eSDavid Howells {
584810caa3eSDavid Howells 	struct afs_vnode *vnode = AFS_FS_I(mapping->host);
585810caa3eSDavid Howells 	struct iov_iter iter;
586e87b03f5SDavid Howells 	unsigned long priv;
587e87b03f5SDavid Howells 	unsigned int offset, to, len, max_len;
588874c8ca1SDavid Howells 	loff_t i_size = i_size_read(&vnode->netfs.inode);
589810caa3eSDavid Howells 	bool new_content = test_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags);
590c7f75ef3SDavid Howells 	bool caching = fscache_cookie_enabled(afs_vnode_cache(vnode));
591e87b03f5SDavid Howells 	long count = wbc->nr_to_write;
592810caa3eSDavid Howells 	int ret;
593810caa3eSDavid Howells 
59478525c74SDavid Howells 	_enter(",%lx,%llx-%llx", folio_index(folio), start, end);
595810caa3eSDavid Howells 
59678525c74SDavid Howells 	if (folio_start_writeback(folio))
597810caa3eSDavid Howells 		BUG();
598c7f75ef3SDavid Howells 	afs_folio_start_fscache(caching, folio);
599810caa3eSDavid Howells 
60078525c74SDavid Howells 	count -= folio_nr_pages(folio);
601e87b03f5SDavid Howells 
602810caa3eSDavid Howells 	/* Find all consecutive lockable dirty pages that have contiguous
603810caa3eSDavid Howells 	 * written regions, stopping when we find a page that is not
604810caa3eSDavid Howells 	 * immediately lockable, is not dirty or is missing, or we reach the
605810caa3eSDavid Howells 	 * end of the range.
606810caa3eSDavid Howells 	 */
60778525c74SDavid Howells 	priv = (unsigned long)folio_get_private(folio);
60878525c74SDavid Howells 	offset = afs_folio_dirty_from(folio, priv);
60978525c74SDavid Howells 	to = afs_folio_dirty_to(folio, priv);
61078525c74SDavid Howells 	trace_afs_folio_dirty(vnode, tracepoint_string("store"), folio);
611810caa3eSDavid Howells 
612e87b03f5SDavid Howells 	len = to - offset;
613e87b03f5SDavid Howells 	start += offset;
614e87b03f5SDavid Howells 	if (start < i_size) {
615e87b03f5SDavid Howells 		/* Trim the write to the EOF; the extra data is ignored.  Also
616e87b03f5SDavid Howells 		 * put an upper limit on the size of a single storedata op.
617e87b03f5SDavid Howells 		 */
618e87b03f5SDavid Howells 		max_len = 65536 * 4096;
619e87b03f5SDavid Howells 		max_len = min_t(unsigned long long, max_len, end - start + 1);
620e87b03f5SDavid Howells 		max_len = min_t(unsigned long long, max_len, i_size - start);
621810caa3eSDavid Howells 
622e87b03f5SDavid Howells 		if (len < max_len &&
62378525c74SDavid Howells 		    (to == folio_size(folio) || new_content))
624e87b03f5SDavid Howells 			afs_extend_writeback(mapping, vnode, &count,
625c7f75ef3SDavid Howells 					     start, max_len, new_content,
626c7f75ef3SDavid Howells 					     caching, &len);
627e87b03f5SDavid Howells 		len = min_t(loff_t, len, max_len);
628e87b03f5SDavid Howells 	}
629810caa3eSDavid Howells 
6304343d008SDavid Howells 	/* We now have a contiguous set of dirty pages, each with writeback
6314343d008SDavid Howells 	 * set; the first page is still locked at this point, but all the rest
6324343d008SDavid Howells 	 * have been unlocked.
6334343d008SDavid Howells 	 */
63478525c74SDavid Howells 	folio_unlock(folio);
6354343d008SDavid Howells 
636e87b03f5SDavid Howells 	if (start < i_size) {
637e87b03f5SDavid Howells 		_debug("write back %x @%llx [%llx]", len, start, i_size);
63831143d5dSDavid Howells 
639c7f75ef3SDavid Howells 		/* Speculatively write to the cache.  We have to fix this up
640c7f75ef3SDavid Howells 		 * later if the store fails.
641c7f75ef3SDavid Howells 		 */
642c7f75ef3SDavid Howells 		afs_write_to_cache(vnode, start, len, i_size, caching);
643c7f75ef3SDavid Howells 
644de4eda9dSAl Viro 		iov_iter_xarray(&iter, ITER_SOURCE, &mapping->i_pages, start, len);
645e87b03f5SDavid Howells 		ret = afs_store_data(vnode, &iter, start, false);
646bd80d8a8SDavid Howells 	} else {
647e87b03f5SDavid Howells 		_debug("write discard %x @%llx [%llx]", len, start, i_size);
648e87b03f5SDavid Howells 
649bd80d8a8SDavid Howells 		/* The dirty region was entirely beyond the EOF. */
6502c547f29SYue Hu 		fscache_clear_page_bits(mapping, start, len, caching);
651e87b03f5SDavid Howells 		afs_pages_written_back(vnode, start, len);
652bd80d8a8SDavid Howells 		ret = 0;
653bd80d8a8SDavid Howells 	}
654bd80d8a8SDavid Howells 
65531143d5dSDavid Howells 	switch (ret) {
6564343d008SDavid Howells 	case 0:
657e87b03f5SDavid Howells 		wbc->nr_to_write = count;
658e87b03f5SDavid Howells 		ret = len;
6594343d008SDavid Howells 		break;
6604343d008SDavid Howells 
6614343d008SDavid Howells 	default:
6624343d008SDavid Howells 		pr_notice("kAFS: Unexpected error from FS.StoreData %d\n", ret);
663df561f66SGustavo A. R. Silva 		fallthrough;
6644343d008SDavid Howells 	case -EACCES:
6654343d008SDavid Howells 	case -EPERM:
6664343d008SDavid Howells 	case -ENOKEY:
6674343d008SDavid Howells 	case -EKEYEXPIRED:
6684343d008SDavid Howells 	case -EKEYREJECTED:
6694343d008SDavid Howells 	case -EKEYREVOKED:
670adc9613fSDavid Howells 	case -ENETRESET:
671e87b03f5SDavid Howells 		afs_redirty_pages(wbc, mapping, start, len);
6724343d008SDavid Howells 		mapping_set_error(mapping, ret);
6734343d008SDavid Howells 		break;
6744343d008SDavid Howells 
67531143d5dSDavid Howells 	case -EDQUOT:
67631143d5dSDavid Howells 	case -ENOSPC:
677e87b03f5SDavid Howells 		afs_redirty_pages(wbc, mapping, start, len);
6784343d008SDavid Howells 		mapping_set_error(mapping, -ENOSPC);
67931143d5dSDavid Howells 		break;
6804343d008SDavid Howells 
68131143d5dSDavid Howells 	case -EROFS:
68231143d5dSDavid Howells 	case -EIO:
68331143d5dSDavid Howells 	case -EREMOTEIO:
68431143d5dSDavid Howells 	case -EFBIG:
68531143d5dSDavid Howells 	case -ENOENT:
68631143d5dSDavid Howells 	case -ENOMEDIUM:
68731143d5dSDavid Howells 	case -ENXIO:
688f51375cdSDavid Howells 		trace_afs_file_error(vnode, ret, afs_file_error_writeback_fail);
689e87b03f5SDavid Howells 		afs_kill_pages(mapping, start, len);
6904343d008SDavid Howells 		mapping_set_error(mapping, ret);
69131143d5dSDavid Howells 		break;
69231143d5dSDavid Howells 	}
69331143d5dSDavid Howells 
69431143d5dSDavid Howells 	_leave(" = %d", ret);
69531143d5dSDavid Howells 	return ret;
69631143d5dSDavid Howells }
69731143d5dSDavid Howells 
69831143d5dSDavid Howells /*
69931143d5dSDavid Howells  * write a region of pages back to the server
70031143d5dSDavid Howells  */
701c1206a2cSAdrian Bunk static int afs_writepages_region(struct address_space *mapping,
70231143d5dSDavid Howells 				 struct writeback_control *wbc,
703a9eb558aSDavid Howells 				 loff_t start, loff_t end, loff_t *_next,
704a9eb558aSDavid Howells 				 bool max_one_loop)
70531143d5dSDavid Howells {
70678525c74SDavid Howells 	struct folio *folio;
707acc8d858SVishal Moola (Oracle) 	struct folio_batch fbatch;
708e87b03f5SDavid Howells 	ssize_t ret;
709acc8d858SVishal Moola (Oracle) 	unsigned int i;
710173ce1caSDavid Howells 	int n, skips = 0;
71131143d5dSDavid Howells 
712e87b03f5SDavid Howells 	_enter("%llx,%llx,", start, end);
713acc8d858SVishal Moola (Oracle) 	folio_batch_init(&fbatch);
71431143d5dSDavid Howells 
71531143d5dSDavid Howells 	do {
716e87b03f5SDavid Howells 		pgoff_t index = start / PAGE_SIZE;
717e87b03f5SDavid Howells 
718acc8d858SVishal Moola (Oracle) 		n = filemap_get_folios_tag(mapping, &index, end / PAGE_SIZE,
719acc8d858SVishal Moola (Oracle) 					PAGECACHE_TAG_DIRTY, &fbatch);
720acc8d858SVishal Moola (Oracle) 
72131143d5dSDavid Howells 		if (!n)
72231143d5dSDavid Howells 			break;
723acc8d858SVishal Moola (Oracle) 		for (i = 0; i < n; i++) {
724acc8d858SVishal Moola (Oracle) 			folio = fbatch.folios[i];
72578525c74SDavid Howells 			start = folio_pos(folio); /* May regress with THPs */
726e87b03f5SDavid Howells 
72778525c74SDavid Howells 			_debug("wback %lx", folio_index(folio));
72831143d5dSDavid Howells 
729e87b03f5SDavid Howells 			/* At this point we hold neither the i_pages lock nor the
730b93b0163SMatthew Wilcox 			 * page lock: the page may be truncated or invalidated
731b93b0163SMatthew Wilcox 			 * (changing page->mapping to NULL), or even swizzled
732b93b0163SMatthew Wilcox 			 * back from swapper_space to tmpfs file mapping
73331143d5dSDavid Howells 			 */
734*819da022SVishal Moola (Oracle) try_again:
735e87b03f5SDavid Howells 			if (wbc->sync_mode != WB_SYNC_NONE) {
73678525c74SDavid Howells 				ret = folio_lock_killable(folio);
7374343d008SDavid Howells 				if (ret < 0) {
738acc8d858SVishal Moola (Oracle) 					folio_batch_release(&fbatch);
7394343d008SDavid Howells 					return ret;
7404343d008SDavid Howells 				}
741e87b03f5SDavid Howells 			} else {
742acc8d858SVishal Moola (Oracle) 				if (!folio_trylock(folio))
743acc8d858SVishal Moola (Oracle) 					continue;
744e87b03f5SDavid Howells 			}
74531143d5dSDavid Howells 
746acc8d858SVishal Moola (Oracle) 			if (folio->mapping != mapping ||
74778525c74SDavid Howells 			    !folio_test_dirty(folio)) {
74878525c74SDavid Howells 				start += folio_size(folio);
74978525c74SDavid Howells 				folio_unlock(folio);
75031143d5dSDavid Howells 				continue;
75131143d5dSDavid Howells 			}
75231143d5dSDavid Howells 
753c7f75ef3SDavid Howells 			if (folio_test_writeback(folio) ||
754c7f75ef3SDavid Howells 			    folio_test_fscache(folio)) {
75578525c74SDavid Howells 				folio_unlock(folio);
756c7f75ef3SDavid Howells 				if (wbc->sync_mode != WB_SYNC_NONE) {
75778525c74SDavid Howells 					folio_wait_writeback(folio);
758c7f75ef3SDavid Howells #ifdef CONFIG_AFS_FSCACHE
759c7f75ef3SDavid Howells 					folio_wait_fscache(folio);
760c7f75ef3SDavid Howells #endif
761*819da022SVishal Moola (Oracle) 					goto try_again;
762c7f75ef3SDavid Howells 				}
763*819da022SVishal Moola (Oracle) 
764*819da022SVishal Moola (Oracle) 				start += folio_size(folio);
765173ce1caSDavid Howells 				if (wbc->sync_mode == WB_SYNC_NONE) {
766acc8d858SVishal Moola (Oracle) 					if (skips >= 5 || need_resched()) {
767acc8d858SVishal Moola (Oracle) 						*_next = start;
768a2b6f2abSVishal Moola (Oracle) 						folio_batch_release(&fbatch);
769acc8d858SVishal Moola (Oracle) 						_leave(" = 0 [%llx]", *_next);
770acc8d858SVishal Moola (Oracle) 						return 0;
771acc8d858SVishal Moola (Oracle) 					}
772173ce1caSDavid Howells 					skips++;
773173ce1caSDavid Howells 				}
77431143d5dSDavid Howells 				continue;
77531143d5dSDavid Howells 			}
77631143d5dSDavid Howells 
77778525c74SDavid Howells 			if (!folio_clear_dirty_for_io(folio))
77865a15109SDavid Howells 				BUG();
779acc8d858SVishal Moola (Oracle) 			ret = afs_write_back_from_locked_folio(mapping, wbc,
780acc8d858SVishal Moola (Oracle) 					folio, start, end);
78131143d5dSDavid Howells 			if (ret < 0) {
782e87b03f5SDavid Howells 				_leave(" = %zd", ret);
783acc8d858SVishal Moola (Oracle) 				folio_batch_release(&fbatch);
78431143d5dSDavid Howells 				return ret;
78531143d5dSDavid Howells 			}
78631143d5dSDavid Howells 
787dc255730SMarc Dionne 			start += ret;
788acc8d858SVishal Moola (Oracle) 		}
78931143d5dSDavid Howells 
790acc8d858SVishal Moola (Oracle) 		folio_batch_release(&fbatch);
79131143d5dSDavid Howells 		cond_resched();
792e87b03f5SDavid Howells 	} while (wbc->nr_to_write > 0);
79331143d5dSDavid Howells 
794e87b03f5SDavid Howells 	*_next = start;
795e87b03f5SDavid Howells 	_leave(" = 0 [%llx]", *_next);
79631143d5dSDavid Howells 	return 0;
79731143d5dSDavid Howells }
79831143d5dSDavid Howells 
79931143d5dSDavid Howells /*
80031143d5dSDavid Howells  * write some of the pending data back to the server
80131143d5dSDavid Howells  */
80231143d5dSDavid Howells int afs_writepages(struct address_space *mapping,
80331143d5dSDavid Howells 		   struct writeback_control *wbc)
80431143d5dSDavid Howells {
805ec0fa0b6SDavid Howells 	struct afs_vnode *vnode = AFS_FS_I(mapping->host);
806e87b03f5SDavid Howells 	loff_t start, next;
80731143d5dSDavid Howells 	int ret;
80831143d5dSDavid Howells 
80931143d5dSDavid Howells 	_enter("");
81031143d5dSDavid Howells 
811ec0fa0b6SDavid Howells 	/* We have to be careful as we can end up racing with setattr()
812ec0fa0b6SDavid Howells 	 * truncating the pagecache since the caller doesn't take a lock here
813ec0fa0b6SDavid Howells 	 * to prevent it.
814ec0fa0b6SDavid Howells 	 */
815ec0fa0b6SDavid Howells 	if (wbc->sync_mode == WB_SYNC_ALL)
816ec0fa0b6SDavid Howells 		down_read(&vnode->validate_lock);
817ec0fa0b6SDavid Howells 	else if (!down_read_trylock(&vnode->validate_lock))
818ec0fa0b6SDavid Howells 		return 0;
819ec0fa0b6SDavid Howells 
82031143d5dSDavid Howells 	if (wbc->range_cyclic) {
821e87b03f5SDavid Howells 		start = mapping->writeback_index * PAGE_SIZE;
822a9eb558aSDavid Howells 		ret = afs_writepages_region(mapping, wbc, start, LLONG_MAX,
823a9eb558aSDavid Howells 					    &next, false);
824afe69498STom Rix 		if (ret == 0) {
825e87b03f5SDavid Howells 			mapping->writeback_index = next / PAGE_SIZE;
826afe69498STom Rix 			if (start > 0 && wbc->nr_to_write > 0) {
827afe69498STom Rix 				ret = afs_writepages_region(mapping, wbc, 0,
828a9eb558aSDavid Howells 							    start, &next, false);
829afe69498STom Rix 				if (ret == 0)
830afe69498STom Rix 					mapping->writeback_index =
831afe69498STom Rix 						next / PAGE_SIZE;
832afe69498STom Rix 			}
833afe69498STom Rix 		}
83431143d5dSDavid Howells 	} else if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) {
835a9eb558aSDavid Howells 		ret = afs_writepages_region(mapping, wbc, 0, LLONG_MAX,
836a9eb558aSDavid Howells 					    &next, false);
837afe69498STom Rix 		if (wbc->nr_to_write > 0 && ret == 0)
8385a972474SDavid Howells 			mapping->writeback_index = next / PAGE_SIZE;
83931143d5dSDavid Howells 	} else {
840e87b03f5SDavid Howells 		ret = afs_writepages_region(mapping, wbc,
841a9eb558aSDavid Howells 					    wbc->range_start, wbc->range_end,
842a9eb558aSDavid Howells 					    &next, false);
84331143d5dSDavid Howells 	}
84431143d5dSDavid Howells 
845ec0fa0b6SDavid Howells 	up_read(&vnode->validate_lock);
84631143d5dSDavid Howells 	_leave(" = %d", ret);
84731143d5dSDavid Howells 	return ret;
84831143d5dSDavid Howells }
84931143d5dSDavid Howells 
85031143d5dSDavid Howells /*
85131143d5dSDavid Howells  * write to an AFS file
85231143d5dSDavid Howells  */
85350b5551dSAl Viro ssize_t afs_file_write(struct kiocb *iocb, struct iov_iter *from)
85431143d5dSDavid Howells {
855496ad9aaSAl Viro 	struct afs_vnode *vnode = AFS_FS_I(file_inode(iocb->ki_filp));
8563978d816SDavid Howells 	struct afs_file *af = iocb->ki_filp->private_data;
85731143d5dSDavid Howells 	ssize_t result;
85850b5551dSAl Viro 	size_t count = iov_iter_count(from);
85931143d5dSDavid Howells 
8603b6492dfSDavid Howells 	_enter("{%llx:%llu},{%zu},",
86150b5551dSAl Viro 	       vnode->fid.vid, vnode->fid.vnode, count);
86231143d5dSDavid Howells 
863874c8ca1SDavid Howells 	if (IS_SWAPFILE(&vnode->netfs.inode)) {
86431143d5dSDavid Howells 		printk(KERN_INFO
86531143d5dSDavid Howells 		       "AFS: Attempt to write to active swap file!\n");
86631143d5dSDavid Howells 		return -EBUSY;
86731143d5dSDavid Howells 	}
86831143d5dSDavid Howells 
86931143d5dSDavid Howells 	if (!count)
87031143d5dSDavid Howells 		return 0;
87131143d5dSDavid Howells 
8723978d816SDavid Howells 	result = afs_validate(vnode, af->key);
8733978d816SDavid Howells 	if (result < 0)
8743978d816SDavid Howells 		return result;
8753978d816SDavid Howells 
87650b5551dSAl Viro 	result = generic_file_write_iter(iocb, from);
87731143d5dSDavid Howells 
87831143d5dSDavid Howells 	_leave(" = %zd", result);
87931143d5dSDavid Howells 	return result;
88031143d5dSDavid Howells }
88131143d5dSDavid Howells 
88231143d5dSDavid Howells /*
88331143d5dSDavid Howells  * flush any dirty pages for this process, and check for write errors.
88431143d5dSDavid Howells  * - the return status from this call provides a reliable indication of
88531143d5dSDavid Howells  *   whether any write errors occurred for this process.
88631143d5dSDavid Howells  */
88702c24a82SJosef Bacik int afs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
88831143d5dSDavid Howells {
8893978d816SDavid Howells 	struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
8903978d816SDavid Howells 	struct afs_file *af = file->private_data;
8913978d816SDavid Howells 	int ret;
89231143d5dSDavid Howells 
8933b6492dfSDavid Howells 	_enter("{%llx:%llu},{n=%pD},%d",
8943c981bfcSAl Viro 	       vnode->fid.vid, vnode->fid.vnode, file,
89531143d5dSDavid Howells 	       datasync);
89631143d5dSDavid Howells 
8973978d816SDavid Howells 	ret = afs_validate(vnode, af->key);
8983978d816SDavid Howells 	if (ret < 0)
8993978d816SDavid Howells 		return ret;
9003978d816SDavid Howells 
9014343d008SDavid Howells 	return file_write_and_wait_range(file, start, end);
90231143d5dSDavid Howells }
9039b3f26c9SDavid Howells 
9049b3f26c9SDavid Howells /*
9059b3f26c9SDavid Howells  * notification that a previously read-only page is about to become writable
9069b3f26c9SDavid Howells  * - if it returns an error, the caller will deliver a bus error signal
9079b3f26c9SDavid Howells  */
9080722f186SSouptick Joarder vm_fault_t afs_page_mkwrite(struct vm_fault *vmf)
9099b3f26c9SDavid Howells {
910490e016fSMatthew Wilcox (Oracle) 	struct folio *folio = page_folio(vmf->page);
9111cf7a151SDavid Howells 	struct file *file = vmf->vma->vm_file;
9121cf7a151SDavid Howells 	struct inode *inode = file_inode(file);
9131cf7a151SDavid Howells 	struct afs_vnode *vnode = AFS_FS_I(inode);
9143978d816SDavid Howells 	struct afs_file *af = file->private_data;
9151cf7a151SDavid Howells 	unsigned long priv;
9169620ad86SMatthew Wilcox (Oracle) 	vm_fault_t ret = VM_FAULT_RETRY;
9179b3f26c9SDavid Howells 
91878525c74SDavid Howells 	_enter("{{%llx:%llu}},{%lx}", vnode->fid.vid, vnode->fid.vnode, folio_index(folio));
9199b3f26c9SDavid Howells 
9203978d816SDavid Howells 	afs_validate(vnode, af->key);
9213978d816SDavid Howells 
9221cf7a151SDavid Howells 	sb_start_pagefault(inode->i_sb);
9231cf7a151SDavid Howells 
9241cf7a151SDavid Howells 	/* Wait for the page to be written to the cache before we allow it to
9251cf7a151SDavid Howells 	 * be modified.  We then assume the entire page will need writing back.
9261cf7a151SDavid Howells 	 */
927630f5ddaSDavid Howells #ifdef CONFIG_AFS_FSCACHE
92878525c74SDavid Howells 	if (folio_test_fscache(folio) &&
92978525c74SDavid Howells 	    folio_wait_fscache_killable(folio) < 0)
9309620ad86SMatthew Wilcox (Oracle) 		goto out;
931630f5ddaSDavid Howells #endif
9329b3f26c9SDavid Howells 
933490e016fSMatthew Wilcox (Oracle) 	if (folio_wait_writeback_killable(folio))
9349620ad86SMatthew Wilcox (Oracle) 		goto out;
9351cf7a151SDavid Howells 
93678525c74SDavid Howells 	if (folio_lock_killable(folio) < 0)
9379620ad86SMatthew Wilcox (Oracle) 		goto out;
9381cf7a151SDavid Howells 
93978525c74SDavid Howells 	/* We mustn't change folio->private until writeback is complete as that
9401cf7a151SDavid Howells 	 * details the portion of the page we need to write back and we might
9411cf7a151SDavid Howells 	 * need to redirty the page if there's a problem.
9421cf7a151SDavid Howells 	 */
943490e016fSMatthew Wilcox (Oracle) 	if (folio_wait_writeback_killable(folio) < 0) {
944490e016fSMatthew Wilcox (Oracle) 		folio_unlock(folio);
9459620ad86SMatthew Wilcox (Oracle) 		goto out;
9465cbf0398SDavid Howells 	}
9471cf7a151SDavid Howells 
94878525c74SDavid Howells 	priv = afs_folio_dirty(folio, 0, folio_size(folio));
94978525c74SDavid Howells 	priv = afs_folio_dirty_mmapped(priv);
95078525c74SDavid Howells 	if (folio_test_private(folio)) {
95178525c74SDavid Howells 		folio_change_private(folio, (void *)priv);
95278525c74SDavid Howells 		trace_afs_folio_dirty(vnode, tracepoint_string("mkwrite+"), folio);
953e87b03f5SDavid Howells 	} else {
95478525c74SDavid Howells 		folio_attach_private(folio, (void *)priv);
95578525c74SDavid Howells 		trace_afs_folio_dirty(vnode, tracepoint_string("mkwrite"), folio);
956e87b03f5SDavid Howells 	}
957bb413489SDavid Howells 	file_update_time(file);
9581cf7a151SDavid Howells 
9599620ad86SMatthew Wilcox (Oracle) 	ret = VM_FAULT_LOCKED;
9609620ad86SMatthew Wilcox (Oracle) out:
9611cf7a151SDavid Howells 	sb_end_pagefault(inode->i_sb);
9629620ad86SMatthew Wilcox (Oracle) 	return ret;
9639b3f26c9SDavid Howells }
9644343d008SDavid Howells 
9654343d008SDavid Howells /*
9664343d008SDavid Howells  * Prune the keys cached for writeback.  The caller must hold vnode->wb_lock.
9674343d008SDavid Howells  */
9684343d008SDavid Howells void afs_prune_wb_keys(struct afs_vnode *vnode)
9694343d008SDavid Howells {
9704343d008SDavid Howells 	LIST_HEAD(graveyard);
9714343d008SDavid Howells 	struct afs_wb_key *wbk, *tmp;
9724343d008SDavid Howells 
9734343d008SDavid Howells 	/* Discard unused keys */
9744343d008SDavid Howells 	spin_lock(&vnode->wb_lock);
9754343d008SDavid Howells 
976874c8ca1SDavid Howells 	if (!mapping_tagged(&vnode->netfs.inode.i_data, PAGECACHE_TAG_WRITEBACK) &&
977874c8ca1SDavid Howells 	    !mapping_tagged(&vnode->netfs.inode.i_data, PAGECACHE_TAG_DIRTY)) {
9784343d008SDavid Howells 		list_for_each_entry_safe(wbk, tmp, &vnode->wb_keys, vnode_link) {
9794343d008SDavid Howells 			if (refcount_read(&wbk->usage) == 1)
9804343d008SDavid Howells 				list_move(&wbk->vnode_link, &graveyard);
9814343d008SDavid Howells 		}
9824343d008SDavid Howells 	}
9834343d008SDavid Howells 
9844343d008SDavid Howells 	spin_unlock(&vnode->wb_lock);
9854343d008SDavid Howells 
9864343d008SDavid Howells 	while (!list_empty(&graveyard)) {
9874343d008SDavid Howells 		wbk = list_entry(graveyard.next, struct afs_wb_key, vnode_link);
9884343d008SDavid Howells 		list_del(&wbk->vnode_link);
9894343d008SDavid Howells 		afs_put_wb_key(wbk);
9904343d008SDavid Howells 	}
9914343d008SDavid Howells }
9924343d008SDavid Howells 
9934343d008SDavid Howells /*
9944343d008SDavid Howells  * Clean up a page during invalidation.
9954343d008SDavid Howells  */
996a42442ddSMatthew Wilcox (Oracle) int afs_launder_folio(struct folio *folio)
9974343d008SDavid Howells {
99878525c74SDavid Howells 	struct afs_vnode *vnode = AFS_FS_I(folio_inode(folio));
999bd80d8a8SDavid Howells 	struct iov_iter iter;
1000a8173be1SChristoph Hellwig 	struct bio_vec bv;
10014343d008SDavid Howells 	unsigned long priv;
10024343d008SDavid Howells 	unsigned int f, t;
10034343d008SDavid Howells 	int ret = 0;
10044343d008SDavid Howells 
1005a42442ddSMatthew Wilcox (Oracle) 	_enter("{%lx}", folio->index);
10064343d008SDavid Howells 
100778525c74SDavid Howells 	priv = (unsigned long)folio_get_private(folio);
100878525c74SDavid Howells 	if (folio_clear_dirty_for_io(folio)) {
10094343d008SDavid Howells 		f = 0;
101078525c74SDavid Howells 		t = folio_size(folio);
101178525c74SDavid Howells 		if (folio_test_private(folio)) {
101278525c74SDavid Howells 			f = afs_folio_dirty_from(folio, priv);
101378525c74SDavid Howells 			t = afs_folio_dirty_to(folio, priv);
10144343d008SDavid Howells 		}
10154343d008SDavid Howells 
1016a8173be1SChristoph Hellwig 		bvec_set_folio(&bv, folio, t - f, f);
1017a8173be1SChristoph Hellwig 		iov_iter_bvec(&iter, ITER_SOURCE, &bv, 1, bv.bv_len);
1018bd80d8a8SDavid Howells 
101978525c74SDavid Howells 		trace_afs_folio_dirty(vnode, tracepoint_string("launder"), folio);
102078525c74SDavid Howells 		ret = afs_store_data(vnode, &iter, folio_pos(folio) + f, true);
10214343d008SDavid Howells 	}
10224343d008SDavid Howells 
102378525c74SDavid Howells 	trace_afs_folio_dirty(vnode, tracepoint_string("laundered"), folio);
102478525c74SDavid Howells 	folio_detach_private(folio);
102578525c74SDavid Howells 	folio_wait_fscache(folio);
10264343d008SDavid Howells 	return ret;
102731143d5dSDavid Howells }
1028c7f75ef3SDavid Howells 
1029c7f75ef3SDavid Howells /*
1030c7f75ef3SDavid Howells  * Deal with the completion of writing the data to the cache.
1031c7f75ef3SDavid Howells  */
1032c7f75ef3SDavid Howells static void afs_write_to_cache_done(void *priv, ssize_t transferred_or_error,
1033c7f75ef3SDavid Howells 				    bool was_async)
1034c7f75ef3SDavid Howells {
1035c7f75ef3SDavid Howells 	struct afs_vnode *vnode = priv;
1036c7f75ef3SDavid Howells 
1037c7f75ef3SDavid Howells 	if (IS_ERR_VALUE(transferred_or_error) &&
1038c7f75ef3SDavid Howells 	    transferred_or_error != -ENOBUFS)
1039c7f75ef3SDavid Howells 		afs_invalidate_cache(vnode, 0);
1040c7f75ef3SDavid Howells }
1041c7f75ef3SDavid Howells 
1042c7f75ef3SDavid Howells /*
1043c7f75ef3SDavid Howells  * Save the write to the cache also.
1044c7f75ef3SDavid Howells  */
1045c7f75ef3SDavid Howells static void afs_write_to_cache(struct afs_vnode *vnode,
1046c7f75ef3SDavid Howells 			       loff_t start, size_t len, loff_t i_size,
1047c7f75ef3SDavid Howells 			       bool caching)
1048c7f75ef3SDavid Howells {
1049c7f75ef3SDavid Howells 	fscache_write_to_cache(afs_vnode_cache(vnode),
1050874c8ca1SDavid Howells 			       vnode->netfs.inode.i_mapping, start, len, i_size,
1051c7f75ef3SDavid Howells 			       afs_write_to_cache_done, vnode, caching);
1052c7f75ef3SDavid Howells }
1053