xref: /openbmc/linux/fs/afs/write.c (revision 03275585)
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  */
afs_dirty_folio(struct address_space * mapping,struct folio * folio)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 }
afs_folio_start_fscache(bool caching,struct folio * folio)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
afs_folio_start_fscache(bool caching,struct folio * folio)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  */
afs_flush_conflicting_write(struct address_space * mapping,struct folio * folio)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  */
afs_write_begin(struct file * file,struct address_space * mapping,loff_t pos,unsigned len,struct page ** _page,void ** fsdata)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  */
afs_write_end(struct file * file,struct address_space * mapping,loff_t pos,unsigned len,unsigned copied,struct page * subpage,void * fsdata)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  */
afs_kill_pages(struct address_space * mapping,loff_t start,loff_t len)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  */
afs_redirty_pages(struct writeback_control * wbc,struct address_space * mapping,loff_t start,loff_t len)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  */
afs_pages_written_back(struct afs_vnode * vnode,loff_t start,unsigned int len)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  */
afs_get_writeback_key(struct afs_vnode * vnode,struct afs_wb_key ** _wbk)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 
afs_store_data_success(struct afs_operation * op)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  */
afs_store_data(struct afs_vnode * vnode,struct iov_iter * iter,loff_t pos,bool laundering)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.pos = pos;
417bd80d8a8SDavid Howells 	op->store.size = size;
418d383e346SDavid Howells 	op->store.laundering = laundering;
419811f04baSDavid Howells 	op->flags |= AFS_OPERATION_UNINTR;
420e49c7b2fSDavid Howells 	op->ops = &afs_store_data_operation;
421e49c7b2fSDavid Howells 
422e49c7b2fSDavid Howells try_next_key:
423e49c7b2fSDavid Howells 	afs_begin_vnode_operation(op);
424*03275585SDavid Howells 
425*03275585SDavid Howells 	op->store.write_iter = iter;
426*03275585SDavid Howells 	op->store.i_size = max(pos + size, vnode->netfs.remote_i_size);
427*03275585SDavid Howells 	op->mtime = vnode->netfs.inode.i_mtime;
428*03275585SDavid Howells 
429e49c7b2fSDavid Howells 	afs_wait_for_operation(op);
430e49c7b2fSDavid Howells 
431e49c7b2fSDavid Howells 	switch (op->error) {
4324343d008SDavid Howells 	case -EACCES:
4334343d008SDavid Howells 	case -EPERM:
4344343d008SDavid Howells 	case -ENOKEY:
4354343d008SDavid Howells 	case -EKEYEXPIRED:
4364343d008SDavid Howells 	case -EKEYREJECTED:
4374343d008SDavid Howells 	case -EKEYREVOKED:
4384343d008SDavid Howells 		_debug("next");
439e49c7b2fSDavid Howells 
440e49c7b2fSDavid Howells 		ret = afs_get_writeback_key(vnode, &wbk);
441e49c7b2fSDavid Howells 		if (ret == 0) {
442e49c7b2fSDavid Howells 			key_put(op->key);
443e49c7b2fSDavid Howells 			op->key = key_get(wbk->key);
4444343d008SDavid Howells 			goto try_next_key;
4454343d008SDavid Howells 		}
446e49c7b2fSDavid Howells 		break;
447e49c7b2fSDavid Howells 	}
4484343d008SDavid Howells 
4494343d008SDavid Howells 	afs_put_wb_key(wbk);
450e49c7b2fSDavid Howells 	_leave(" = %d", op->error);
451e49c7b2fSDavid Howells 	return afs_put_operation(op);
452d2ddc776SDavid Howells }
453d2ddc776SDavid Howells 
454d2ddc776SDavid Howells /*
455810caa3eSDavid Howells  * Extend the region to be written back to include subsequent contiguously
456810caa3eSDavid Howells  * dirty pages if possible, but don't sleep while doing so.
457810caa3eSDavid Howells  *
458810caa3eSDavid Howells  * If this page holds new content, then we can include filler zeros in the
459810caa3eSDavid Howells  * writeback.
46031143d5dSDavid Howells  */
afs_extend_writeback(struct address_space * mapping,struct afs_vnode * vnode,long * _count,loff_t start,loff_t max_len,bool new_content,bool caching,unsigned int * _len)461810caa3eSDavid Howells static void afs_extend_writeback(struct address_space *mapping,
462810caa3eSDavid Howells 				 struct afs_vnode *vnode,
463810caa3eSDavid Howells 				 long *_count,
464e87b03f5SDavid Howells 				 loff_t start,
465e87b03f5SDavid Howells 				 loff_t max_len,
466e87b03f5SDavid Howells 				 bool new_content,
467c7f75ef3SDavid Howells 				 bool caching,
468e87b03f5SDavid Howells 				 unsigned int *_len)
46931143d5dSDavid Howells {
470f5f288a0SMatthew Wilcox (Oracle) 	struct folio_batch fbatch;
47178525c74SDavid Howells 	struct folio *folio;
472e87b03f5SDavid Howells 	unsigned long priv;
473e87b03f5SDavid Howells 	unsigned int psize, filler = 0;
474e87b03f5SDavid Howells 	unsigned int f, t;
475e87b03f5SDavid Howells 	loff_t len = *_len;
476e87b03f5SDavid Howells 	pgoff_t index = (start + len) / PAGE_SIZE;
477e87b03f5SDavid Howells 	bool stop = true;
478e87b03f5SDavid Howells 	unsigned int i;
4794343d008SDavid Howells 
480e87b03f5SDavid Howells 	XA_STATE(xas, &mapping->i_pages, index);
481f5f288a0SMatthew Wilcox (Oracle) 	folio_batch_init(&fbatch);
482e87b03f5SDavid Howells 
48331143d5dSDavid Howells 	do {
484e87b03f5SDavid Howells 		/* Firstly, we gather up a batch of contiguous dirty pages
485e87b03f5SDavid Howells 		 * under the RCU read lock - but we can't clear the dirty flags
486e87b03f5SDavid Howells 		 * there if any of those pages are mapped.
487e87b03f5SDavid Howells 		 */
488e87b03f5SDavid Howells 		rcu_read_lock();
489e87b03f5SDavid Howells 
49078525c74SDavid Howells 		xas_for_each(&xas, folio, ULONG_MAX) {
491e87b03f5SDavid Howells 			stop = true;
49278525c74SDavid Howells 			if (xas_retry(&xas, folio))
493e87b03f5SDavid Howells 				continue;
49478525c74SDavid Howells 			if (xa_is_value(folio))
495e87b03f5SDavid Howells 				break;
49678525c74SDavid Howells 			if (folio_index(folio) != index)
497e87b03f5SDavid Howells 				break;
498e87b03f5SDavid Howells 
49978525c74SDavid Howells 			if (!folio_try_get_rcu(folio)) {
500e87b03f5SDavid Howells 				xas_reset(&xas);
501e87b03f5SDavid Howells 				continue;
50231143d5dSDavid Howells 			}
50331143d5dSDavid Howells 
504e87b03f5SDavid Howells 			/* Has the page moved or been split? */
50578525c74SDavid Howells 			if (unlikely(folio != xas_reload(&xas))) {
50678525c74SDavid Howells 				folio_put(folio);
5075a813276SDavid Howells 				break;
508581b2027SDavid Howells 			}
509e87b03f5SDavid Howells 
51078525c74SDavid Howells 			if (!folio_trylock(folio)) {
51178525c74SDavid Howells 				folio_put(folio);
51231143d5dSDavid Howells 				break;
513581b2027SDavid Howells 			}
514c7f75ef3SDavid Howells 			if (!folio_test_dirty(folio) ||
515c7f75ef3SDavid Howells 			    folio_test_writeback(folio) ||
516c7f75ef3SDavid Howells 			    folio_test_fscache(folio)) {
51778525c74SDavid Howells 				folio_unlock(folio);
51878525c74SDavid Howells 				folio_put(folio);
51931143d5dSDavid Howells 				break;
52031143d5dSDavid Howells 			}
5214343d008SDavid Howells 
52278525c74SDavid Howells 			psize = folio_size(folio);
52378525c74SDavid Howells 			priv = (unsigned long)folio_get_private(folio);
52478525c74SDavid Howells 			f = afs_folio_dirty_from(folio, priv);
52578525c74SDavid Howells 			t = afs_folio_dirty_to(folio, priv);
526810caa3eSDavid Howells 			if (f != 0 && !new_content) {
52778525c74SDavid Howells 				folio_unlock(folio);
52878525c74SDavid Howells 				folio_put(folio);
5294343d008SDavid Howells 				break;
5304343d008SDavid Howells 			}
5314343d008SDavid Howells 
532e87b03f5SDavid Howells 			len += filler + t;
533e87b03f5SDavid Howells 			filler = psize - t;
534e87b03f5SDavid Howells 			if (len >= max_len || *_count <= 0)
535e87b03f5SDavid Howells 				stop = true;
536e87b03f5SDavid Howells 			else if (t == psize || new_content)
537e87b03f5SDavid Howells 				stop = false;
538e87b03f5SDavid Howells 
53978525c74SDavid Howells 			index += folio_nr_pages(folio);
540f5f288a0SMatthew Wilcox (Oracle) 			if (!folio_batch_add(&fbatch, folio))
541e87b03f5SDavid Howells 				break;
542e87b03f5SDavid Howells 			if (stop)
543e87b03f5SDavid Howells 				break;
544e87b03f5SDavid Howells 		}
545e87b03f5SDavid Howells 
546e87b03f5SDavid Howells 		if (!stop)
547e87b03f5SDavid Howells 			xas_pause(&xas);
548e87b03f5SDavid Howells 		rcu_read_unlock();
549e87b03f5SDavid Howells 
550f5f288a0SMatthew Wilcox (Oracle) 		/* Now, if we obtained any folios, we can shift them to being
551e87b03f5SDavid Howells 		 * writable and mark them for caching.
552e87b03f5SDavid Howells 		 */
553f5f288a0SMatthew Wilcox (Oracle) 		if (!folio_batch_count(&fbatch))
554e87b03f5SDavid Howells 			break;
555e87b03f5SDavid Howells 
556f5f288a0SMatthew Wilcox (Oracle) 		for (i = 0; i < folio_batch_count(&fbatch); i++) {
557f5f288a0SMatthew Wilcox (Oracle) 			folio = fbatch.folios[i];
55878525c74SDavid Howells 			trace_afs_folio_dirty(vnode, tracepoint_string("store+"), folio);
55913524ab3SDavid Howells 
56078525c74SDavid Howells 			if (!folio_clear_dirty_for_io(folio))
56131143d5dSDavid Howells 				BUG();
56278525c74SDavid Howells 			if (folio_start_writeback(folio))
56331143d5dSDavid Howells 				BUG();
564c7f75ef3SDavid Howells 			afs_folio_start_fscache(caching, folio);
565e87b03f5SDavid Howells 
56678525c74SDavid Howells 			*_count -= folio_nr_pages(folio);
56778525c74SDavid Howells 			folio_unlock(folio);
56831143d5dSDavid Howells 		}
56931143d5dSDavid Howells 
570f5f288a0SMatthew Wilcox (Oracle) 		folio_batch_release(&fbatch);
571e87b03f5SDavid Howells 		cond_resched();
572e87b03f5SDavid Howells 	} while (!stop);
57331143d5dSDavid Howells 
574e87b03f5SDavid Howells 	*_len = len;
575810caa3eSDavid Howells }
576810caa3eSDavid Howells 
577810caa3eSDavid Howells /*
578810caa3eSDavid Howells  * Synchronously write back the locked page and any subsequent non-locked dirty
579810caa3eSDavid Howells  * pages.
580810caa3eSDavid Howells  */
afs_write_back_from_locked_folio(struct address_space * mapping,struct writeback_control * wbc,struct folio * folio,loff_t start,loff_t end)58178525c74SDavid Howells static ssize_t afs_write_back_from_locked_folio(struct address_space *mapping,
582810caa3eSDavid Howells 						struct writeback_control *wbc,
58378525c74SDavid Howells 						struct folio *folio,
584e87b03f5SDavid Howells 						loff_t start, loff_t end)
585810caa3eSDavid Howells {
586810caa3eSDavid Howells 	struct afs_vnode *vnode = AFS_FS_I(mapping->host);
587810caa3eSDavid Howells 	struct iov_iter iter;
588e87b03f5SDavid Howells 	unsigned long priv;
589e87b03f5SDavid Howells 	unsigned int offset, to, len, max_len;
590874c8ca1SDavid Howells 	loff_t i_size = i_size_read(&vnode->netfs.inode);
591810caa3eSDavid Howells 	bool new_content = test_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags);
592c7f75ef3SDavid Howells 	bool caching = fscache_cookie_enabled(afs_vnode_cache(vnode));
593e87b03f5SDavid Howells 	long count = wbc->nr_to_write;
594810caa3eSDavid Howells 	int ret;
595810caa3eSDavid Howells 
59678525c74SDavid Howells 	_enter(",%lx,%llx-%llx", folio_index(folio), start, end);
597810caa3eSDavid Howells 
59878525c74SDavid Howells 	if (folio_start_writeback(folio))
599810caa3eSDavid Howells 		BUG();
600c7f75ef3SDavid Howells 	afs_folio_start_fscache(caching, folio);
601810caa3eSDavid Howells 
60278525c74SDavid Howells 	count -= folio_nr_pages(folio);
603e87b03f5SDavid Howells 
604810caa3eSDavid Howells 	/* Find all consecutive lockable dirty pages that have contiguous
605810caa3eSDavid Howells 	 * written regions, stopping when we find a page that is not
606810caa3eSDavid Howells 	 * immediately lockable, is not dirty or is missing, or we reach the
607810caa3eSDavid Howells 	 * end of the range.
608810caa3eSDavid Howells 	 */
60978525c74SDavid Howells 	priv = (unsigned long)folio_get_private(folio);
61078525c74SDavid Howells 	offset = afs_folio_dirty_from(folio, priv);
61178525c74SDavid Howells 	to = afs_folio_dirty_to(folio, priv);
61278525c74SDavid Howells 	trace_afs_folio_dirty(vnode, tracepoint_string("store"), folio);
613810caa3eSDavid Howells 
614e87b03f5SDavid Howells 	len = to - offset;
615e87b03f5SDavid Howells 	start += offset;
616e87b03f5SDavid Howells 	if (start < i_size) {
617e87b03f5SDavid Howells 		/* Trim the write to the EOF; the extra data is ignored.  Also
618e87b03f5SDavid Howells 		 * put an upper limit on the size of a single storedata op.
619e87b03f5SDavid Howells 		 */
620e87b03f5SDavid Howells 		max_len = 65536 * 4096;
621e87b03f5SDavid Howells 		max_len = min_t(unsigned long long, max_len, end - start + 1);
622e87b03f5SDavid Howells 		max_len = min_t(unsigned long long, max_len, i_size - start);
623810caa3eSDavid Howells 
624e87b03f5SDavid Howells 		if (len < max_len &&
62578525c74SDavid Howells 		    (to == folio_size(folio) || new_content))
626e87b03f5SDavid Howells 			afs_extend_writeback(mapping, vnode, &count,
627c7f75ef3SDavid Howells 					     start, max_len, new_content,
628c7f75ef3SDavid Howells 					     caching, &len);
629e87b03f5SDavid Howells 		len = min_t(loff_t, len, max_len);
630e87b03f5SDavid Howells 	}
631810caa3eSDavid Howells 
6324343d008SDavid Howells 	/* We now have a contiguous set of dirty pages, each with writeback
6334343d008SDavid Howells 	 * set; the first page is still locked at this point, but all the rest
6344343d008SDavid Howells 	 * have been unlocked.
6354343d008SDavid Howells 	 */
63678525c74SDavid Howells 	folio_unlock(folio);
6374343d008SDavid Howells 
638e87b03f5SDavid Howells 	if (start < i_size) {
639e87b03f5SDavid Howells 		_debug("write back %x @%llx [%llx]", len, start, i_size);
64031143d5dSDavid Howells 
641c7f75ef3SDavid Howells 		/* Speculatively write to the cache.  We have to fix this up
642c7f75ef3SDavid Howells 		 * later if the store fails.
643c7f75ef3SDavid Howells 		 */
644c7f75ef3SDavid Howells 		afs_write_to_cache(vnode, start, len, i_size, caching);
645c7f75ef3SDavid Howells 
646de4eda9dSAl Viro 		iov_iter_xarray(&iter, ITER_SOURCE, &mapping->i_pages, start, len);
647e87b03f5SDavid Howells 		ret = afs_store_data(vnode, &iter, start, false);
648bd80d8a8SDavid Howells 	} else {
649e87b03f5SDavid Howells 		_debug("write discard %x @%llx [%llx]", len, start, i_size);
650e87b03f5SDavid Howells 
651bd80d8a8SDavid Howells 		/* The dirty region was entirely beyond the EOF. */
6522c547f29SYue Hu 		fscache_clear_page_bits(mapping, start, len, caching);
653e87b03f5SDavid Howells 		afs_pages_written_back(vnode, start, len);
654bd80d8a8SDavid Howells 		ret = 0;
655bd80d8a8SDavid Howells 	}
656bd80d8a8SDavid Howells 
65731143d5dSDavid Howells 	switch (ret) {
6584343d008SDavid Howells 	case 0:
659e87b03f5SDavid Howells 		wbc->nr_to_write = count;
660e87b03f5SDavid Howells 		ret = len;
6614343d008SDavid Howells 		break;
6624343d008SDavid Howells 
6634343d008SDavid Howells 	default:
6644343d008SDavid Howells 		pr_notice("kAFS: Unexpected error from FS.StoreData %d\n", ret);
665df561f66SGustavo A. R. Silva 		fallthrough;
6664343d008SDavid Howells 	case -EACCES:
6674343d008SDavid Howells 	case -EPERM:
6684343d008SDavid Howells 	case -ENOKEY:
6694343d008SDavid Howells 	case -EKEYEXPIRED:
6704343d008SDavid Howells 	case -EKEYREJECTED:
6714343d008SDavid Howells 	case -EKEYREVOKED:
672adc9613fSDavid Howells 	case -ENETRESET:
673e87b03f5SDavid Howells 		afs_redirty_pages(wbc, mapping, start, len);
6744343d008SDavid Howells 		mapping_set_error(mapping, ret);
6754343d008SDavid Howells 		break;
6764343d008SDavid Howells 
67731143d5dSDavid Howells 	case -EDQUOT:
67831143d5dSDavid Howells 	case -ENOSPC:
679e87b03f5SDavid Howells 		afs_redirty_pages(wbc, mapping, start, len);
6804343d008SDavid Howells 		mapping_set_error(mapping, -ENOSPC);
68131143d5dSDavid Howells 		break;
6824343d008SDavid Howells 
68331143d5dSDavid Howells 	case -EROFS:
68431143d5dSDavid Howells 	case -EIO:
68531143d5dSDavid Howells 	case -EREMOTEIO:
68631143d5dSDavid Howells 	case -EFBIG:
68731143d5dSDavid Howells 	case -ENOENT:
68831143d5dSDavid Howells 	case -ENOMEDIUM:
68931143d5dSDavid Howells 	case -ENXIO:
690f51375cdSDavid Howells 		trace_afs_file_error(vnode, ret, afs_file_error_writeback_fail);
691e87b03f5SDavid Howells 		afs_kill_pages(mapping, start, len);
6924343d008SDavid Howells 		mapping_set_error(mapping, ret);
69331143d5dSDavid Howells 		break;
69431143d5dSDavid Howells 	}
69531143d5dSDavid Howells 
69631143d5dSDavid Howells 	_leave(" = %d", ret);
69731143d5dSDavid Howells 	return ret;
69831143d5dSDavid Howells }
69931143d5dSDavid Howells 
70031143d5dSDavid Howells /*
70131143d5dSDavid Howells  * write a region of pages back to the server
70231143d5dSDavid Howells  */
afs_writepages_region(struct address_space * mapping,struct writeback_control * wbc,loff_t start,loff_t end,loff_t * _next,bool max_one_loop)703c1206a2cSAdrian Bunk static int afs_writepages_region(struct address_space *mapping,
70431143d5dSDavid Howells 				 struct writeback_control *wbc,
705a9eb558aSDavid Howells 				 loff_t start, loff_t end, loff_t *_next,
706a9eb558aSDavid Howells 				 bool max_one_loop)
70731143d5dSDavid Howells {
70878525c74SDavid Howells 	struct folio *folio;
709acc8d858SVishal Moola (Oracle) 	struct folio_batch fbatch;
710e87b03f5SDavid Howells 	ssize_t ret;
711acc8d858SVishal Moola (Oracle) 	unsigned int i;
712173ce1caSDavid Howells 	int n, skips = 0;
71331143d5dSDavid Howells 
714e87b03f5SDavid Howells 	_enter("%llx,%llx,", start, end);
715acc8d858SVishal Moola (Oracle) 	folio_batch_init(&fbatch);
71631143d5dSDavid Howells 
71731143d5dSDavid Howells 	do {
718e87b03f5SDavid Howells 		pgoff_t index = start / PAGE_SIZE;
719e87b03f5SDavid Howells 
720acc8d858SVishal Moola (Oracle) 		n = filemap_get_folios_tag(mapping, &index, end / PAGE_SIZE,
721acc8d858SVishal Moola (Oracle) 					PAGECACHE_TAG_DIRTY, &fbatch);
722acc8d858SVishal Moola (Oracle) 
72331143d5dSDavid Howells 		if (!n)
72431143d5dSDavid Howells 			break;
725acc8d858SVishal Moola (Oracle) 		for (i = 0; i < n; i++) {
726acc8d858SVishal Moola (Oracle) 			folio = fbatch.folios[i];
72778525c74SDavid Howells 			start = folio_pos(folio); /* May regress with THPs */
728e87b03f5SDavid Howells 
72978525c74SDavid Howells 			_debug("wback %lx", folio_index(folio));
73031143d5dSDavid Howells 
731e87b03f5SDavid Howells 			/* At this point we hold neither the i_pages lock nor the
732b93b0163SMatthew Wilcox 			 * page lock: the page may be truncated or invalidated
733b93b0163SMatthew Wilcox 			 * (changing page->mapping to NULL), or even swizzled
734b93b0163SMatthew Wilcox 			 * back from swapper_space to tmpfs file mapping
73531143d5dSDavid Howells 			 */
736819da022SVishal Moola (Oracle) try_again:
737e87b03f5SDavid Howells 			if (wbc->sync_mode != WB_SYNC_NONE) {
73878525c74SDavid Howells 				ret = folio_lock_killable(folio);
7394343d008SDavid Howells 				if (ret < 0) {
740acc8d858SVishal Moola (Oracle) 					folio_batch_release(&fbatch);
7414343d008SDavid Howells 					return ret;
7424343d008SDavid Howells 				}
743e87b03f5SDavid Howells 			} else {
744acc8d858SVishal Moola (Oracle) 				if (!folio_trylock(folio))
745acc8d858SVishal Moola (Oracle) 					continue;
746e87b03f5SDavid Howells 			}
74731143d5dSDavid Howells 
748acc8d858SVishal Moola (Oracle) 			if (folio->mapping != mapping ||
74978525c74SDavid Howells 			    !folio_test_dirty(folio)) {
75078525c74SDavid Howells 				start += folio_size(folio);
75178525c74SDavid Howells 				folio_unlock(folio);
75231143d5dSDavid Howells 				continue;
75331143d5dSDavid Howells 			}
75431143d5dSDavid Howells 
755c7f75ef3SDavid Howells 			if (folio_test_writeback(folio) ||
756c7f75ef3SDavid Howells 			    folio_test_fscache(folio)) {
75778525c74SDavid Howells 				folio_unlock(folio);
758c7f75ef3SDavid Howells 				if (wbc->sync_mode != WB_SYNC_NONE) {
75978525c74SDavid Howells 					folio_wait_writeback(folio);
760c7f75ef3SDavid Howells #ifdef CONFIG_AFS_FSCACHE
761c7f75ef3SDavid Howells 					folio_wait_fscache(folio);
762c7f75ef3SDavid Howells #endif
763819da022SVishal Moola (Oracle) 					goto try_again;
764c7f75ef3SDavid Howells 				}
765819da022SVishal Moola (Oracle) 
766819da022SVishal Moola (Oracle) 				start += folio_size(folio);
767173ce1caSDavid Howells 				if (wbc->sync_mode == WB_SYNC_NONE) {
768acc8d858SVishal Moola (Oracle) 					if (skips >= 5 || need_resched()) {
769acc8d858SVishal Moola (Oracle) 						*_next = start;
770a2b6f2abSVishal Moola (Oracle) 						folio_batch_release(&fbatch);
771acc8d858SVishal Moola (Oracle) 						_leave(" = 0 [%llx]", *_next);
772acc8d858SVishal Moola (Oracle) 						return 0;
773acc8d858SVishal Moola (Oracle) 					}
774173ce1caSDavid Howells 					skips++;
775173ce1caSDavid Howells 				}
77631143d5dSDavid Howells 				continue;
77731143d5dSDavid Howells 			}
77831143d5dSDavid Howells 
77978525c74SDavid Howells 			if (!folio_clear_dirty_for_io(folio))
78065a15109SDavid Howells 				BUG();
781acc8d858SVishal Moola (Oracle) 			ret = afs_write_back_from_locked_folio(mapping, wbc,
782acc8d858SVishal Moola (Oracle) 					folio, start, end);
78331143d5dSDavid Howells 			if (ret < 0) {
784e87b03f5SDavid Howells 				_leave(" = %zd", ret);
785acc8d858SVishal Moola (Oracle) 				folio_batch_release(&fbatch);
78631143d5dSDavid Howells 				return ret;
78731143d5dSDavid Howells 			}
78831143d5dSDavid Howells 
789dc255730SMarc Dionne 			start += ret;
790acc8d858SVishal Moola (Oracle) 		}
79131143d5dSDavid Howells 
792acc8d858SVishal Moola (Oracle) 		folio_batch_release(&fbatch);
79331143d5dSDavid Howells 		cond_resched();
794e87b03f5SDavid Howells 	} while (wbc->nr_to_write > 0);
79531143d5dSDavid Howells 
796e87b03f5SDavid Howells 	*_next = start;
797e87b03f5SDavid Howells 	_leave(" = 0 [%llx]", *_next);
79831143d5dSDavid Howells 	return 0;
79931143d5dSDavid Howells }
80031143d5dSDavid Howells 
80131143d5dSDavid Howells /*
80231143d5dSDavid Howells  * write some of the pending data back to the server
80331143d5dSDavid Howells  */
afs_writepages(struct address_space * mapping,struct writeback_control * wbc)80431143d5dSDavid Howells int afs_writepages(struct address_space *mapping,
80531143d5dSDavid Howells 		   struct writeback_control *wbc)
80631143d5dSDavid Howells {
807ec0fa0b6SDavid Howells 	struct afs_vnode *vnode = AFS_FS_I(mapping->host);
808e87b03f5SDavid Howells 	loff_t start, next;
80931143d5dSDavid Howells 	int ret;
81031143d5dSDavid Howells 
81131143d5dSDavid Howells 	_enter("");
81231143d5dSDavid Howells 
813ec0fa0b6SDavid Howells 	/* We have to be careful as we can end up racing with setattr()
814ec0fa0b6SDavid Howells 	 * truncating the pagecache since the caller doesn't take a lock here
815ec0fa0b6SDavid Howells 	 * to prevent it.
816ec0fa0b6SDavid Howells 	 */
817ec0fa0b6SDavid Howells 	if (wbc->sync_mode == WB_SYNC_ALL)
818ec0fa0b6SDavid Howells 		down_read(&vnode->validate_lock);
819ec0fa0b6SDavid Howells 	else if (!down_read_trylock(&vnode->validate_lock))
820ec0fa0b6SDavid Howells 		return 0;
821ec0fa0b6SDavid Howells 
82231143d5dSDavid Howells 	if (wbc->range_cyclic) {
823e87b03f5SDavid Howells 		start = mapping->writeback_index * PAGE_SIZE;
824a9eb558aSDavid Howells 		ret = afs_writepages_region(mapping, wbc, start, LLONG_MAX,
825a9eb558aSDavid Howells 					    &next, false);
826afe69498STom Rix 		if (ret == 0) {
827e87b03f5SDavid Howells 			mapping->writeback_index = next / PAGE_SIZE;
828afe69498STom Rix 			if (start > 0 && wbc->nr_to_write > 0) {
829afe69498STom Rix 				ret = afs_writepages_region(mapping, wbc, 0,
830a9eb558aSDavid Howells 							    start, &next, false);
831afe69498STom Rix 				if (ret == 0)
832afe69498STom Rix 					mapping->writeback_index =
833afe69498STom Rix 						next / PAGE_SIZE;
834afe69498STom Rix 			}
835afe69498STom Rix 		}
83631143d5dSDavid Howells 	} else if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) {
837a9eb558aSDavid Howells 		ret = afs_writepages_region(mapping, wbc, 0, LLONG_MAX,
838a9eb558aSDavid Howells 					    &next, false);
839afe69498STom Rix 		if (wbc->nr_to_write > 0 && ret == 0)
8405a972474SDavid Howells 			mapping->writeback_index = next / PAGE_SIZE;
84131143d5dSDavid Howells 	} else {
842e87b03f5SDavid Howells 		ret = afs_writepages_region(mapping, wbc,
843a9eb558aSDavid Howells 					    wbc->range_start, wbc->range_end,
844a9eb558aSDavid Howells 					    &next, false);
84531143d5dSDavid Howells 	}
84631143d5dSDavid Howells 
847ec0fa0b6SDavid Howells 	up_read(&vnode->validate_lock);
84831143d5dSDavid Howells 	_leave(" = %d", ret);
84931143d5dSDavid Howells 	return ret;
85031143d5dSDavid Howells }
85131143d5dSDavid Howells 
85231143d5dSDavid Howells /*
85331143d5dSDavid Howells  * write to an AFS file
85431143d5dSDavid Howells  */
afs_file_write(struct kiocb * iocb,struct iov_iter * from)85550b5551dSAl Viro ssize_t afs_file_write(struct kiocb *iocb, struct iov_iter *from)
85631143d5dSDavid Howells {
857496ad9aaSAl Viro 	struct afs_vnode *vnode = AFS_FS_I(file_inode(iocb->ki_filp));
8583978d816SDavid Howells 	struct afs_file *af = iocb->ki_filp->private_data;
85931143d5dSDavid Howells 	ssize_t result;
86050b5551dSAl Viro 	size_t count = iov_iter_count(from);
86131143d5dSDavid Howells 
8623b6492dfSDavid Howells 	_enter("{%llx:%llu},{%zu},",
86350b5551dSAl Viro 	       vnode->fid.vid, vnode->fid.vnode, count);
86431143d5dSDavid Howells 
865874c8ca1SDavid Howells 	if (IS_SWAPFILE(&vnode->netfs.inode)) {
86631143d5dSDavid Howells 		printk(KERN_INFO
86731143d5dSDavid Howells 		       "AFS: Attempt to write to active swap file!\n");
86831143d5dSDavid Howells 		return -EBUSY;
86931143d5dSDavid Howells 	}
87031143d5dSDavid Howells 
87131143d5dSDavid Howells 	if (!count)
87231143d5dSDavid Howells 		return 0;
87331143d5dSDavid Howells 
8743978d816SDavid Howells 	result = afs_validate(vnode, af->key);
8753978d816SDavid Howells 	if (result < 0)
8763978d816SDavid Howells 		return result;
8773978d816SDavid Howells 
87850b5551dSAl Viro 	result = generic_file_write_iter(iocb, from);
87931143d5dSDavid Howells 
88031143d5dSDavid Howells 	_leave(" = %zd", result);
88131143d5dSDavid Howells 	return result;
88231143d5dSDavid Howells }
88331143d5dSDavid Howells 
88431143d5dSDavid Howells /*
88531143d5dSDavid Howells  * flush any dirty pages for this process, and check for write errors.
88631143d5dSDavid Howells  * - the return status from this call provides a reliable indication of
88731143d5dSDavid Howells  *   whether any write errors occurred for this process.
88831143d5dSDavid Howells  */
afs_fsync(struct file * file,loff_t start,loff_t end,int datasync)88902c24a82SJosef Bacik int afs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
89031143d5dSDavid Howells {
8913978d816SDavid Howells 	struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
8923978d816SDavid Howells 	struct afs_file *af = file->private_data;
8933978d816SDavid Howells 	int ret;
89431143d5dSDavid Howells 
8953b6492dfSDavid Howells 	_enter("{%llx:%llu},{n=%pD},%d",
8963c981bfcSAl Viro 	       vnode->fid.vid, vnode->fid.vnode, file,
89731143d5dSDavid Howells 	       datasync);
89831143d5dSDavid Howells 
8993978d816SDavid Howells 	ret = afs_validate(vnode, af->key);
9003978d816SDavid Howells 	if (ret < 0)
9013978d816SDavid Howells 		return ret;
9023978d816SDavid Howells 
9034343d008SDavid Howells 	return file_write_and_wait_range(file, start, end);
90431143d5dSDavid Howells }
9059b3f26c9SDavid Howells 
9069b3f26c9SDavid Howells /*
9079b3f26c9SDavid Howells  * notification that a previously read-only page is about to become writable
9089b3f26c9SDavid Howells  * - if it returns an error, the caller will deliver a bus error signal
9099b3f26c9SDavid Howells  */
afs_page_mkwrite(struct vm_fault * vmf)9100722f186SSouptick Joarder vm_fault_t afs_page_mkwrite(struct vm_fault *vmf)
9119b3f26c9SDavid Howells {
912490e016fSMatthew Wilcox (Oracle) 	struct folio *folio = page_folio(vmf->page);
9131cf7a151SDavid Howells 	struct file *file = vmf->vma->vm_file;
9141cf7a151SDavid Howells 	struct inode *inode = file_inode(file);
9151cf7a151SDavid Howells 	struct afs_vnode *vnode = AFS_FS_I(inode);
9163978d816SDavid Howells 	struct afs_file *af = file->private_data;
9171cf7a151SDavid Howells 	unsigned long priv;
9189620ad86SMatthew Wilcox (Oracle) 	vm_fault_t ret = VM_FAULT_RETRY;
9199b3f26c9SDavid Howells 
92078525c74SDavid Howells 	_enter("{{%llx:%llu}},{%lx}", vnode->fid.vid, vnode->fid.vnode, folio_index(folio));
9219b3f26c9SDavid Howells 
9223978d816SDavid Howells 	afs_validate(vnode, af->key);
9233978d816SDavid Howells 
9241cf7a151SDavid Howells 	sb_start_pagefault(inode->i_sb);
9251cf7a151SDavid Howells 
9261cf7a151SDavid Howells 	/* Wait for the page to be written to the cache before we allow it to
9271cf7a151SDavid Howells 	 * be modified.  We then assume the entire page will need writing back.
9281cf7a151SDavid Howells 	 */
929630f5ddaSDavid Howells #ifdef CONFIG_AFS_FSCACHE
93078525c74SDavid Howells 	if (folio_test_fscache(folio) &&
93178525c74SDavid Howells 	    folio_wait_fscache_killable(folio) < 0)
9329620ad86SMatthew Wilcox (Oracle) 		goto out;
933630f5ddaSDavid Howells #endif
9349b3f26c9SDavid Howells 
935490e016fSMatthew Wilcox (Oracle) 	if (folio_wait_writeback_killable(folio))
9369620ad86SMatthew Wilcox (Oracle) 		goto out;
9371cf7a151SDavid Howells 
93878525c74SDavid Howells 	if (folio_lock_killable(folio) < 0)
9399620ad86SMatthew Wilcox (Oracle) 		goto out;
9401cf7a151SDavid Howells 
94178525c74SDavid Howells 	/* We mustn't change folio->private until writeback is complete as that
9421cf7a151SDavid Howells 	 * details the portion of the page we need to write back and we might
9431cf7a151SDavid Howells 	 * need to redirty the page if there's a problem.
9441cf7a151SDavid Howells 	 */
945490e016fSMatthew Wilcox (Oracle) 	if (folio_wait_writeback_killable(folio) < 0) {
946490e016fSMatthew Wilcox (Oracle) 		folio_unlock(folio);
9479620ad86SMatthew Wilcox (Oracle) 		goto out;
9485cbf0398SDavid Howells 	}
9491cf7a151SDavid Howells 
95078525c74SDavid Howells 	priv = afs_folio_dirty(folio, 0, folio_size(folio));
95178525c74SDavid Howells 	priv = afs_folio_dirty_mmapped(priv);
95278525c74SDavid Howells 	if (folio_test_private(folio)) {
95378525c74SDavid Howells 		folio_change_private(folio, (void *)priv);
95478525c74SDavid Howells 		trace_afs_folio_dirty(vnode, tracepoint_string("mkwrite+"), folio);
955e87b03f5SDavid Howells 	} else {
95678525c74SDavid Howells 		folio_attach_private(folio, (void *)priv);
95778525c74SDavid Howells 		trace_afs_folio_dirty(vnode, tracepoint_string("mkwrite"), folio);
958e87b03f5SDavid Howells 	}
959bb413489SDavid Howells 	file_update_time(file);
9601cf7a151SDavid Howells 
9619620ad86SMatthew Wilcox (Oracle) 	ret = VM_FAULT_LOCKED;
9629620ad86SMatthew Wilcox (Oracle) out:
9631cf7a151SDavid Howells 	sb_end_pagefault(inode->i_sb);
9649620ad86SMatthew Wilcox (Oracle) 	return ret;
9659b3f26c9SDavid Howells }
9664343d008SDavid Howells 
9674343d008SDavid Howells /*
9684343d008SDavid Howells  * Prune the keys cached for writeback.  The caller must hold vnode->wb_lock.
9694343d008SDavid Howells  */
afs_prune_wb_keys(struct afs_vnode * vnode)9704343d008SDavid Howells void afs_prune_wb_keys(struct afs_vnode *vnode)
9714343d008SDavid Howells {
9724343d008SDavid Howells 	LIST_HEAD(graveyard);
9734343d008SDavid Howells 	struct afs_wb_key *wbk, *tmp;
9744343d008SDavid Howells 
9754343d008SDavid Howells 	/* Discard unused keys */
9764343d008SDavid Howells 	spin_lock(&vnode->wb_lock);
9774343d008SDavid Howells 
978874c8ca1SDavid Howells 	if (!mapping_tagged(&vnode->netfs.inode.i_data, PAGECACHE_TAG_WRITEBACK) &&
979874c8ca1SDavid Howells 	    !mapping_tagged(&vnode->netfs.inode.i_data, PAGECACHE_TAG_DIRTY)) {
9804343d008SDavid Howells 		list_for_each_entry_safe(wbk, tmp, &vnode->wb_keys, vnode_link) {
9814343d008SDavid Howells 			if (refcount_read(&wbk->usage) == 1)
9824343d008SDavid Howells 				list_move(&wbk->vnode_link, &graveyard);
9834343d008SDavid Howells 		}
9844343d008SDavid Howells 	}
9854343d008SDavid Howells 
9864343d008SDavid Howells 	spin_unlock(&vnode->wb_lock);
9874343d008SDavid Howells 
9884343d008SDavid Howells 	while (!list_empty(&graveyard)) {
9894343d008SDavid Howells 		wbk = list_entry(graveyard.next, struct afs_wb_key, vnode_link);
9904343d008SDavid Howells 		list_del(&wbk->vnode_link);
9914343d008SDavid Howells 		afs_put_wb_key(wbk);
9924343d008SDavid Howells 	}
9934343d008SDavid Howells }
9944343d008SDavid Howells 
9954343d008SDavid Howells /*
9964343d008SDavid Howells  * Clean up a page during invalidation.
9974343d008SDavid Howells  */
afs_launder_folio(struct folio * folio)998a42442ddSMatthew Wilcox (Oracle) int afs_launder_folio(struct folio *folio)
9994343d008SDavid Howells {
100078525c74SDavid Howells 	struct afs_vnode *vnode = AFS_FS_I(folio_inode(folio));
1001bd80d8a8SDavid Howells 	struct iov_iter iter;
1002a8173be1SChristoph Hellwig 	struct bio_vec bv;
10034343d008SDavid Howells 	unsigned long priv;
10044343d008SDavid Howells 	unsigned int f, t;
10054343d008SDavid Howells 	int ret = 0;
10064343d008SDavid Howells 
1007a42442ddSMatthew Wilcox (Oracle) 	_enter("{%lx}", folio->index);
10084343d008SDavid Howells 
100978525c74SDavid Howells 	priv = (unsigned long)folio_get_private(folio);
101078525c74SDavid Howells 	if (folio_clear_dirty_for_io(folio)) {
10114343d008SDavid Howells 		f = 0;
101278525c74SDavid Howells 		t = folio_size(folio);
101378525c74SDavid Howells 		if (folio_test_private(folio)) {
101478525c74SDavid Howells 			f = afs_folio_dirty_from(folio, priv);
101578525c74SDavid Howells 			t = afs_folio_dirty_to(folio, priv);
10164343d008SDavid Howells 		}
10174343d008SDavid Howells 
1018a8173be1SChristoph Hellwig 		bvec_set_folio(&bv, folio, t - f, f);
1019a8173be1SChristoph Hellwig 		iov_iter_bvec(&iter, ITER_SOURCE, &bv, 1, bv.bv_len);
1020bd80d8a8SDavid Howells 
102178525c74SDavid Howells 		trace_afs_folio_dirty(vnode, tracepoint_string("launder"), folio);
102278525c74SDavid Howells 		ret = afs_store_data(vnode, &iter, folio_pos(folio) + f, true);
10234343d008SDavid Howells 	}
10244343d008SDavid Howells 
102578525c74SDavid Howells 	trace_afs_folio_dirty(vnode, tracepoint_string("laundered"), folio);
102678525c74SDavid Howells 	folio_detach_private(folio);
102778525c74SDavid Howells 	folio_wait_fscache(folio);
10284343d008SDavid Howells 	return ret;
102931143d5dSDavid Howells }
1030c7f75ef3SDavid Howells 
1031c7f75ef3SDavid Howells /*
1032c7f75ef3SDavid Howells  * Deal with the completion of writing the data to the cache.
1033c7f75ef3SDavid Howells  */
afs_write_to_cache_done(void * priv,ssize_t transferred_or_error,bool was_async)1034c7f75ef3SDavid Howells static void afs_write_to_cache_done(void *priv, ssize_t transferred_or_error,
1035c7f75ef3SDavid Howells 				    bool was_async)
1036c7f75ef3SDavid Howells {
1037c7f75ef3SDavid Howells 	struct afs_vnode *vnode = priv;
1038c7f75ef3SDavid Howells 
1039c7f75ef3SDavid Howells 	if (IS_ERR_VALUE(transferred_or_error) &&
1040c7f75ef3SDavid Howells 	    transferred_or_error != -ENOBUFS)
1041c7f75ef3SDavid Howells 		afs_invalidate_cache(vnode, 0);
1042c7f75ef3SDavid Howells }
1043c7f75ef3SDavid Howells 
1044c7f75ef3SDavid Howells /*
1045c7f75ef3SDavid Howells  * Save the write to the cache also.
1046c7f75ef3SDavid Howells  */
afs_write_to_cache(struct afs_vnode * vnode,loff_t start,size_t len,loff_t i_size,bool caching)1047c7f75ef3SDavid Howells static void afs_write_to_cache(struct afs_vnode *vnode,
1048c7f75ef3SDavid Howells 			       loff_t start, size_t len, loff_t i_size,
1049c7f75ef3SDavid Howells 			       bool caching)
1050c7f75ef3SDavid Howells {
1051c7f75ef3SDavid Howells 	fscache_write_to_cache(afs_vnode_cache(vnode),
1052874c8ca1SDavid Howells 			       vnode->netfs.inode.i_mapping, start, len, i_size,
1053c7f75ef3SDavid Howells 			       afs_write_to_cache_done, vnode, caching);
1054c7f75ef3SDavid Howells }
1055