xref: /openbmc/linux/fs/afs/write.c (revision ab487a4c)
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 
17c7f75ef3SDavid Howells static void afs_write_to_cache(struct afs_vnode *vnode, loff_t start, size_t len,
18c7f75ef3SDavid Howells 			       loff_t i_size, bool caching);
19c7f75ef3SDavid Howells 
20c7f75ef3SDavid Howells #ifdef CONFIG_AFS_FSCACHE
2131143d5dSDavid Howells /*
22c7f75ef3SDavid Howells  * Mark a page as having been made dirty and thus needing writeback.  We also
23c7f75ef3SDavid Howells  * need to pin the cache object to write back to.
2431143d5dSDavid Howells  */
2531143d5dSDavid Howells int afs_set_page_dirty(struct page *page)
2631143d5dSDavid Howells {
27c7f75ef3SDavid Howells 	return fscache_set_page_dirty(page, afs_vnode_cache(AFS_FS_I(page->mapping->host)));
2831143d5dSDavid Howells }
29c7f75ef3SDavid Howells static void afs_folio_start_fscache(bool caching, struct folio *folio)
30c7f75ef3SDavid Howells {
31c7f75ef3SDavid Howells 	if (caching)
32c7f75ef3SDavid Howells 		folio_start_fscache(folio);
33c7f75ef3SDavid Howells }
34c7f75ef3SDavid Howells #else
35c7f75ef3SDavid Howells static void afs_folio_start_fscache(bool caching, struct folio *folio)
36c7f75ef3SDavid Howells {
37c7f75ef3SDavid Howells }
38c7f75ef3SDavid Howells #endif
3931143d5dSDavid Howells 
4031143d5dSDavid Howells /*
4131143d5dSDavid Howells  * prepare to perform part of a write to a page
4231143d5dSDavid Howells  */
4315b4650eSNick Piggin int afs_write_begin(struct file *file, struct address_space *mapping,
4415b4650eSNick Piggin 		    loff_t pos, unsigned len, unsigned flags,
4521db2cdcSDavid Howells 		    struct page **_page, void **fsdata)
4631143d5dSDavid Howells {
47496ad9aaSAl Viro 	struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
4878525c74SDavid Howells 	struct folio *folio;
494343d008SDavid Howells 	unsigned long priv;
50e87b03f5SDavid Howells 	unsigned f, from;
51e87b03f5SDavid Howells 	unsigned t, to;
52e87b03f5SDavid Howells 	pgoff_t index;
5331143d5dSDavid Howells 	int ret;
5431143d5dSDavid Howells 
55e87b03f5SDavid Howells 	_enter("{%llx:%llu},%llx,%x",
56e87b03f5SDavid Howells 	       vnode->fid.vid, vnode->fid.vnode, pos, len);
5731143d5dSDavid Howells 
583003bbd0SDavid Howells 	/* Prefetch area to be written into the cache if we're caching this
593003bbd0SDavid Howells 	 * file.  We need to do this before we get a lock on the page in case
603003bbd0SDavid Howells 	 * there's more than one writer competing for the same cache block.
613003bbd0SDavid Howells 	 */
62bc899ee1SDavid Howells 	ret = netfs_write_begin(file, mapping, pos, len, flags, &folio, fsdata);
633003bbd0SDavid Howells 	if (ret < 0)
6431143d5dSDavid Howells 		return ret;
65630f5ddaSDavid Howells 
6678525c74SDavid Howells 	index = folio_index(folio);
67e87b03f5SDavid Howells 	from = pos - index * PAGE_SIZE;
68e87b03f5SDavid Howells 	to = from + len;
69e87b03f5SDavid Howells 
7031143d5dSDavid Howells try_again:
714343d008SDavid Howells 	/* See if this page is already partially written in a way that we can
724343d008SDavid Howells 	 * merge the new write with.
734343d008SDavid Howells 	 */
7478525c74SDavid Howells 	if (folio_test_private(folio)) {
7578525c74SDavid Howells 		priv = (unsigned long)folio_get_private(folio);
7678525c74SDavid Howells 		f = afs_folio_dirty_from(folio, priv);
7778525c74SDavid Howells 		t = afs_folio_dirty_to(folio, priv);
784343d008SDavid Howells 		ASSERTCMP(f, <=, t);
7931143d5dSDavid Howells 
8078525c74SDavid Howells 		if (folio_test_writeback(folio)) {
8178525c74SDavid Howells 			trace_afs_folio_dirty(vnode, tracepoint_string("alrdy"), folio);
825a039c32SDavid Howells 			goto flush_conflicting_write;
835a039c32SDavid Howells 		}
845a813276SDavid Howells 		/* If the file is being filled locally, allow inter-write
855a813276SDavid Howells 		 * spaces to be merged into writes.  If it's not, only write
865a813276SDavid Howells 		 * back what the user gives us.
875a813276SDavid Howells 		 */
885a813276SDavid Howells 		if (!test_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags) &&
895a813276SDavid Howells 		    (to < f || from > t))
904343d008SDavid Howells 			goto flush_conflicting_write;
9131143d5dSDavid Howells 	}
9231143d5dSDavid Howells 
9378525c74SDavid Howells 	*_page = &folio->page;
944343d008SDavid Howells 	_leave(" = 0");
9531143d5dSDavid Howells 	return 0;
9631143d5dSDavid Howells 
974343d008SDavid Howells 	/* The previous write and this write aren't adjacent or overlapping, so
984343d008SDavid Howells 	 * flush the page out.
994343d008SDavid Howells 	 */
1004343d008SDavid Howells flush_conflicting_write:
10131143d5dSDavid Howells 	_debug("flush conflict");
10278525c74SDavid Howells 	ret = folio_write_one(folio);
10321db2cdcSDavid Howells 	if (ret < 0)
10421db2cdcSDavid Howells 		goto error;
10531143d5dSDavid Howells 
10678525c74SDavid Howells 	ret = folio_lock_killable(folio);
10721db2cdcSDavid Howells 	if (ret < 0)
10821db2cdcSDavid Howells 		goto error;
10921db2cdcSDavid Howells 	goto try_again;
11021db2cdcSDavid Howells 
11121db2cdcSDavid Howells error:
11278525c74SDavid Howells 	folio_put(folio);
1134343d008SDavid Howells 	_leave(" = %d", ret);
1144343d008SDavid Howells 	return ret;
1154343d008SDavid Howells }
11631143d5dSDavid Howells 
11731143d5dSDavid Howells /*
11831143d5dSDavid Howells  * finalise part of a write to a page
11931143d5dSDavid Howells  */
12015b4650eSNick Piggin int afs_write_end(struct file *file, struct address_space *mapping,
12115b4650eSNick Piggin 		  loff_t pos, unsigned len, unsigned copied,
12278525c74SDavid Howells 		  struct page *subpage, void *fsdata)
12331143d5dSDavid Howells {
12478525c74SDavid Howells 	struct folio *folio = page_folio(subpage);
125496ad9aaSAl Viro 	struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
126f792e3acSDavid Howells 	unsigned long priv;
12778525c74SDavid Howells 	unsigned int f, from = offset_in_folio(folio, pos);
128f792e3acSDavid Howells 	unsigned int t, to = from + copied;
129c7f75ef3SDavid Howells 	loff_t i_size, write_end_pos;
13031143d5dSDavid Howells 
1313b6492dfSDavid Howells 	_enter("{%llx:%llu},{%lx}",
13278525c74SDavid Howells 	       vnode->fid.vid, vnode->fid.vnode, folio_index(folio));
13331143d5dSDavid Howells 
13478525c74SDavid Howells 	if (!folio_test_uptodate(folio)) {
13566e9c6a8SDavid Howells 		if (copied < len) {
13666e9c6a8SDavid Howells 			copied = 0;
13766e9c6a8SDavid Howells 			goto out;
13866e9c6a8SDavid Howells 		}
13966e9c6a8SDavid Howells 
14078525c74SDavid Howells 		folio_mark_uptodate(folio);
14166e9c6a8SDavid Howells 	}
14266e9c6a8SDavid Howells 
1433ad216eeSDavid Howells 	if (copied == 0)
1443ad216eeSDavid Howells 		goto out;
1453ad216eeSDavid Howells 
146c7f75ef3SDavid Howells 	write_end_pos = pos + copied;
14731143d5dSDavid Howells 
14831143d5dSDavid Howells 	i_size = i_size_read(&vnode->vfs_inode);
149c7f75ef3SDavid Howells 	if (write_end_pos > i_size) {
1501f32ef79SDavid Howells 		write_seqlock(&vnode->cb_lock);
15131143d5dSDavid Howells 		i_size = i_size_read(&vnode->vfs_inode);
152c7f75ef3SDavid Howells 		if (write_end_pos > i_size)
153c7f75ef3SDavid Howells 			afs_set_i_size(vnode, write_end_pos);
1541f32ef79SDavid Howells 		write_sequnlock(&vnode->cb_lock);
155c7f75ef3SDavid Howells 		fscache_update_cookie(afs_vnode_cache(vnode), NULL, &write_end_pos);
15631143d5dSDavid Howells 	}
15731143d5dSDavid Howells 
15878525c74SDavid Howells 	if (folio_test_private(folio)) {
15978525c74SDavid Howells 		priv = (unsigned long)folio_get_private(folio);
16078525c74SDavid Howells 		f = afs_folio_dirty_from(folio, priv);
16178525c74SDavid Howells 		t = afs_folio_dirty_to(folio, priv);
162f792e3acSDavid Howells 		if (from < f)
163f792e3acSDavid Howells 			f = from;
164f792e3acSDavid Howells 		if (to > t)
165f792e3acSDavid Howells 			t = to;
16678525c74SDavid Howells 		priv = afs_folio_dirty(folio, f, t);
16778525c74SDavid Howells 		folio_change_private(folio, (void *)priv);
16878525c74SDavid Howells 		trace_afs_folio_dirty(vnode, tracepoint_string("dirty+"), folio);
169f792e3acSDavid Howells 	} else {
17078525c74SDavid Howells 		priv = afs_folio_dirty(folio, from, to);
17178525c74SDavid Howells 		folio_attach_private(folio, (void *)priv);
17278525c74SDavid Howells 		trace_afs_folio_dirty(vnode, tracepoint_string("dirty"), folio);
173f792e3acSDavid Howells 	}
174f792e3acSDavid Howells 
17578525c74SDavid Howells 	if (folio_mark_dirty(folio))
17678525c74SDavid Howells 		_debug("dirtied %lx", folio_index(folio));
177afae457dSDavid Howells 
178afae457dSDavid Howells out:
17978525c74SDavid Howells 	folio_unlock(folio);
18078525c74SDavid Howells 	folio_put(folio);
1813003bbd0SDavid Howells 	return copied;
18231143d5dSDavid Howells }
18331143d5dSDavid Howells 
18431143d5dSDavid Howells /*
18531143d5dSDavid Howells  * kill all the pages in the given range
18631143d5dSDavid Howells  */
1874343d008SDavid Howells static void afs_kill_pages(struct address_space *mapping,
188e87b03f5SDavid Howells 			   loff_t start, loff_t len)
18931143d5dSDavid Howells {
1904343d008SDavid Howells 	struct afs_vnode *vnode = AFS_FS_I(mapping->host);
19178525c74SDavid Howells 	struct folio *folio;
19278525c74SDavid Howells 	pgoff_t index = start / PAGE_SIZE;
19378525c74SDavid Howells 	pgoff_t last = (start + len - 1) / PAGE_SIZE, next;
19431143d5dSDavid Howells 
195e87b03f5SDavid Howells 	_enter("{%llx:%llu},%llx @%llx",
196e87b03f5SDavid Howells 	       vnode->fid.vid, vnode->fid.vnode, len, start);
19731143d5dSDavid Howells 
19831143d5dSDavid Howells 	do {
19978525c74SDavid Howells 		_debug("kill %lx (to %lx)", index, last);
20031143d5dSDavid Howells 
20178525c74SDavid Howells 		folio = filemap_get_folio(mapping, index);
20278525c74SDavid Howells 		if (!folio) {
20378525c74SDavid Howells 			next = index + 1;
20478525c74SDavid Howells 			continue;
2054343d008SDavid Howells 		}
2064343d008SDavid Howells 
20778525c74SDavid Howells 		next = folio_next_index(folio);
20878525c74SDavid Howells 
20978525c74SDavid Howells 		folio_clear_uptodate(folio);
21078525c74SDavid Howells 		folio_end_writeback(folio);
21178525c74SDavid Howells 		folio_lock(folio);
21278525c74SDavid Howells 		generic_error_remove_page(mapping, &folio->page);
21378525c74SDavid Howells 		folio_unlock(folio);
21478525c74SDavid Howells 		folio_put(folio);
21578525c74SDavid Howells 
21678525c74SDavid Howells 	} while (index = next, index <= last);
2174343d008SDavid Howells 
2184343d008SDavid Howells 	_leave("");
2194343d008SDavid Howells }
2204343d008SDavid Howells 
2214343d008SDavid Howells /*
2224343d008SDavid Howells  * Redirty all the pages in a given range.
2234343d008SDavid Howells  */
2244343d008SDavid Howells static void afs_redirty_pages(struct writeback_control *wbc,
2254343d008SDavid Howells 			      struct address_space *mapping,
226e87b03f5SDavid Howells 			      loff_t start, loff_t len)
2274343d008SDavid Howells {
2284343d008SDavid Howells 	struct afs_vnode *vnode = AFS_FS_I(mapping->host);
22978525c74SDavid Howells 	struct folio *folio;
23078525c74SDavid Howells 	pgoff_t index = start / PAGE_SIZE;
23178525c74SDavid Howells 	pgoff_t last = (start + len - 1) / PAGE_SIZE, next;
2324343d008SDavid Howells 
233e87b03f5SDavid Howells 	_enter("{%llx:%llu},%llx @%llx",
234e87b03f5SDavid Howells 	       vnode->fid.vid, vnode->fid.vnode, len, start);
2354343d008SDavid Howells 
2364343d008SDavid Howells 	do {
237e87b03f5SDavid Howells 		_debug("redirty %llx @%llx", len, start);
2384343d008SDavid Howells 
23978525c74SDavid Howells 		folio = filemap_get_folio(mapping, index);
24078525c74SDavid Howells 		if (!folio) {
24178525c74SDavid Howells 			next = index + 1;
24278525c74SDavid Howells 			continue;
24331143d5dSDavid Howells 		}
24431143d5dSDavid Howells 
24578525c74SDavid Howells 		next = index + folio_nr_pages(folio);
24678525c74SDavid Howells 		folio_redirty_for_writepage(wbc, folio);
24778525c74SDavid Howells 		folio_end_writeback(folio);
24878525c74SDavid Howells 		folio_put(folio);
24978525c74SDavid Howells 	} while (index = next, index <= last);
25031143d5dSDavid Howells 
25131143d5dSDavid Howells 	_leave("");
25231143d5dSDavid Howells }
25331143d5dSDavid Howells 
25431143d5dSDavid Howells /*
255a58823acSDavid Howells  * completion of write to server
256a58823acSDavid Howells  */
257e87b03f5SDavid Howells static void afs_pages_written_back(struct afs_vnode *vnode, loff_t start, unsigned int len)
258a58823acSDavid Howells {
259bd80d8a8SDavid Howells 	struct address_space *mapping = vnode->vfs_inode.i_mapping;
26078525c74SDavid Howells 	struct folio *folio;
261e87b03f5SDavid Howells 	pgoff_t end;
262bd80d8a8SDavid Howells 
263e87b03f5SDavid Howells 	XA_STATE(xas, &mapping->i_pages, start / PAGE_SIZE);
264a58823acSDavid Howells 
265e87b03f5SDavid Howells 	_enter("{%llx:%llu},{%x @%llx}",
266e87b03f5SDavid Howells 	       vnode->fid.vid, vnode->fid.vnode, len, start);
267a58823acSDavid Howells 
268bd80d8a8SDavid Howells 	rcu_read_lock();
269a58823acSDavid Howells 
270e87b03f5SDavid Howells 	end = (start + len - 1) / PAGE_SIZE;
27178525c74SDavid Howells 	xas_for_each(&xas, folio, end) {
27278525c74SDavid Howells 		if (!folio_test_writeback(folio)) {
27378525c74SDavid Howells 			kdebug("bad %x @%llx page %lx %lx",
27478525c74SDavid Howells 			       len, start, folio_index(folio), end);
27578525c74SDavid Howells 			ASSERT(folio_test_writeback(folio));
276e87b03f5SDavid Howells 		}
277a58823acSDavid Howells 
27878525c74SDavid Howells 		trace_afs_folio_dirty(vnode, tracepoint_string("clear"), folio);
27978525c74SDavid Howells 		folio_detach_private(folio);
28078525c74SDavid Howells 		folio_end_writeback(folio);
281a58823acSDavid Howells 	}
282bd80d8a8SDavid Howells 
283bd80d8a8SDavid Howells 	rcu_read_unlock();
284a58823acSDavid Howells 
285a58823acSDavid Howells 	afs_prune_wb_keys(vnode);
286a58823acSDavid Howells 	_leave("");
287a58823acSDavid Howells }
288a58823acSDavid Howells 
289a58823acSDavid Howells /*
290e49c7b2fSDavid Howells  * Find a key to use for the writeback.  We cached the keys used to author the
291e49c7b2fSDavid Howells  * writes on the vnode.  *_wbk will contain the last writeback key used or NULL
292e49c7b2fSDavid Howells  * and we need to start from there if it's set.
293e49c7b2fSDavid Howells  */
294e49c7b2fSDavid Howells static int afs_get_writeback_key(struct afs_vnode *vnode,
295e49c7b2fSDavid Howells 				 struct afs_wb_key **_wbk)
296e49c7b2fSDavid Howells {
297e49c7b2fSDavid Howells 	struct afs_wb_key *wbk = NULL;
298e49c7b2fSDavid Howells 	struct list_head *p;
299e49c7b2fSDavid Howells 	int ret = -ENOKEY, ret2;
300e49c7b2fSDavid Howells 
301e49c7b2fSDavid Howells 	spin_lock(&vnode->wb_lock);
302e49c7b2fSDavid Howells 	if (*_wbk)
303e49c7b2fSDavid Howells 		p = (*_wbk)->vnode_link.next;
304e49c7b2fSDavid Howells 	else
305e49c7b2fSDavid Howells 		p = vnode->wb_keys.next;
306e49c7b2fSDavid Howells 
307e49c7b2fSDavid Howells 	while (p != &vnode->wb_keys) {
308e49c7b2fSDavid Howells 		wbk = list_entry(p, struct afs_wb_key, vnode_link);
309e49c7b2fSDavid Howells 		_debug("wbk %u", key_serial(wbk->key));
310e49c7b2fSDavid Howells 		ret2 = key_validate(wbk->key);
311e49c7b2fSDavid Howells 		if (ret2 == 0) {
312e49c7b2fSDavid Howells 			refcount_inc(&wbk->usage);
313e49c7b2fSDavid Howells 			_debug("USE WB KEY %u", key_serial(wbk->key));
314e49c7b2fSDavid Howells 			break;
315e49c7b2fSDavid Howells 		}
316e49c7b2fSDavid Howells 
317e49c7b2fSDavid Howells 		wbk = NULL;
318e49c7b2fSDavid Howells 		if (ret == -ENOKEY)
319e49c7b2fSDavid Howells 			ret = ret2;
320e49c7b2fSDavid Howells 		p = p->next;
321e49c7b2fSDavid Howells 	}
322e49c7b2fSDavid Howells 
323e49c7b2fSDavid Howells 	spin_unlock(&vnode->wb_lock);
324e49c7b2fSDavid Howells 	if (*_wbk)
325e49c7b2fSDavid Howells 		afs_put_wb_key(*_wbk);
326e49c7b2fSDavid Howells 	*_wbk = wbk;
327e49c7b2fSDavid Howells 	return 0;
328e49c7b2fSDavid Howells }
329e49c7b2fSDavid Howells 
330e49c7b2fSDavid Howells static void afs_store_data_success(struct afs_operation *op)
331e49c7b2fSDavid Howells {
332e49c7b2fSDavid Howells 	struct afs_vnode *vnode = op->file[0].vnode;
333e49c7b2fSDavid Howells 
334da8d0755SDavid Howells 	op->ctime = op->file[0].scb.status.mtime_client;
335e49c7b2fSDavid Howells 	afs_vnode_commit_status(op, &op->file[0]);
336e49c7b2fSDavid Howells 	if (op->error == 0) {
337d383e346SDavid Howells 		if (!op->store.laundering)
338e87b03f5SDavid Howells 			afs_pages_written_back(vnode, op->store.pos, op->store.size);
339e49c7b2fSDavid Howells 		afs_stat_v(vnode, n_stores);
340bd80d8a8SDavid Howells 		atomic_long_add(op->store.size, &afs_v2net(vnode)->n_store_bytes);
341e49c7b2fSDavid Howells 	}
342e49c7b2fSDavid Howells }
343e49c7b2fSDavid Howells 
344e49c7b2fSDavid Howells static const struct afs_operation_ops afs_store_data_operation = {
345e49c7b2fSDavid Howells 	.issue_afs_rpc	= afs_fs_store_data,
346e49c7b2fSDavid Howells 	.issue_yfs_rpc	= yfs_fs_store_data,
347e49c7b2fSDavid Howells 	.success	= afs_store_data_success,
348e49c7b2fSDavid Howells };
349e49c7b2fSDavid Howells 
350e49c7b2fSDavid Howells /*
351d2ddc776SDavid Howells  * write to a file
35231143d5dSDavid Howells  */
353e87b03f5SDavid Howells static int afs_store_data(struct afs_vnode *vnode, struct iov_iter *iter, loff_t pos,
354bd80d8a8SDavid Howells 			  bool laundering)
35531143d5dSDavid Howells {
356*ab487a4cSDavid Howells 	struct netfs_i_context *ictx = &vnode->netfs_ctx;
357e49c7b2fSDavid Howells 	struct afs_operation *op;
3584343d008SDavid Howells 	struct afs_wb_key *wbk = NULL;
359*ab487a4cSDavid Howells 	loff_t size = iov_iter_count(iter);
360bd80d8a8SDavid Howells 	int ret = -ENOKEY;
361d2ddc776SDavid Howells 
362bd80d8a8SDavid Howells 	_enter("%s{%llx:%llu.%u},%llx,%llx",
363d2ddc776SDavid Howells 	       vnode->volume->name,
364d2ddc776SDavid Howells 	       vnode->fid.vid,
365d2ddc776SDavid Howells 	       vnode->fid.vnode,
366d2ddc776SDavid Howells 	       vnode->fid.unique,
367bd80d8a8SDavid Howells 	       size, pos);
368d2ddc776SDavid Howells 
369e49c7b2fSDavid Howells 	ret = afs_get_writeback_key(vnode, &wbk);
370e49c7b2fSDavid Howells 	if (ret) {
3714343d008SDavid Howells 		_leave(" = %d [no keys]", ret);
3724343d008SDavid Howells 		return ret;
373d2ddc776SDavid Howells 	}
374d2ddc776SDavid Howells 
375e49c7b2fSDavid Howells 	op = afs_alloc_operation(wbk->key, vnode->volume);
376e49c7b2fSDavid Howells 	if (IS_ERR(op)) {
377e49c7b2fSDavid Howells 		afs_put_wb_key(wbk);
378e49c7b2fSDavid Howells 		return -ENOMEM;
379d2ddc776SDavid Howells 	}
380d2ddc776SDavid Howells 
381e49c7b2fSDavid Howells 	afs_op_set_vnode(op, 0, vnode);
382e49c7b2fSDavid Howells 	op->file[0].dv_delta = 1;
38322650f14SDavid Howells 	op->file[0].modification = true;
384bd80d8a8SDavid Howells 	op->store.write_iter = iter;
385bd80d8a8SDavid Howells 	op->store.pos = pos;
386bd80d8a8SDavid Howells 	op->store.size = size;
387*ab487a4cSDavid Howells 	op->store.i_size = max(pos + size, ictx->remote_i_size);
388d383e346SDavid Howells 	op->store.laundering = laundering;
389b3597945SDavid Howells 	op->mtime = vnode->vfs_inode.i_mtime;
390811f04baSDavid Howells 	op->flags |= AFS_OPERATION_UNINTR;
391e49c7b2fSDavid Howells 	op->ops = &afs_store_data_operation;
392e49c7b2fSDavid Howells 
393e49c7b2fSDavid Howells try_next_key:
394e49c7b2fSDavid Howells 	afs_begin_vnode_operation(op);
395e49c7b2fSDavid Howells 	afs_wait_for_operation(op);
396e49c7b2fSDavid Howells 
397e49c7b2fSDavid Howells 	switch (op->error) {
3984343d008SDavid Howells 	case -EACCES:
3994343d008SDavid Howells 	case -EPERM:
4004343d008SDavid Howells 	case -ENOKEY:
4014343d008SDavid Howells 	case -EKEYEXPIRED:
4024343d008SDavid Howells 	case -EKEYREJECTED:
4034343d008SDavid Howells 	case -EKEYREVOKED:
4044343d008SDavid Howells 		_debug("next");
405e49c7b2fSDavid Howells 
406e49c7b2fSDavid Howells 		ret = afs_get_writeback_key(vnode, &wbk);
407e49c7b2fSDavid Howells 		if (ret == 0) {
408e49c7b2fSDavid Howells 			key_put(op->key);
409e49c7b2fSDavid Howells 			op->key = key_get(wbk->key);
4104343d008SDavid Howells 			goto try_next_key;
4114343d008SDavid Howells 		}
412e49c7b2fSDavid Howells 		break;
413e49c7b2fSDavid Howells 	}
4144343d008SDavid Howells 
4154343d008SDavid Howells 	afs_put_wb_key(wbk);
416e49c7b2fSDavid Howells 	_leave(" = %d", op->error);
417e49c7b2fSDavid Howells 	return afs_put_operation(op);
418d2ddc776SDavid Howells }
419d2ddc776SDavid Howells 
420d2ddc776SDavid Howells /*
421810caa3eSDavid Howells  * Extend the region to be written back to include subsequent contiguously
422810caa3eSDavid Howells  * dirty pages if possible, but don't sleep while doing so.
423810caa3eSDavid Howells  *
424810caa3eSDavid Howells  * If this page holds new content, then we can include filler zeros in the
425810caa3eSDavid Howells  * writeback.
42631143d5dSDavid Howells  */
427810caa3eSDavid Howells static void afs_extend_writeback(struct address_space *mapping,
428810caa3eSDavid Howells 				 struct afs_vnode *vnode,
429810caa3eSDavid Howells 				 long *_count,
430e87b03f5SDavid Howells 				 loff_t start,
431e87b03f5SDavid Howells 				 loff_t max_len,
432e87b03f5SDavid Howells 				 bool new_content,
433c7f75ef3SDavid Howells 				 bool caching,
434e87b03f5SDavid Howells 				 unsigned int *_len)
43531143d5dSDavid Howells {
436e87b03f5SDavid Howells 	struct pagevec pvec;
43778525c74SDavid Howells 	struct folio *folio;
438e87b03f5SDavid Howells 	unsigned long priv;
439e87b03f5SDavid Howells 	unsigned int psize, filler = 0;
440e87b03f5SDavid Howells 	unsigned int f, t;
441e87b03f5SDavid Howells 	loff_t len = *_len;
442e87b03f5SDavid Howells 	pgoff_t index = (start + len) / PAGE_SIZE;
443e87b03f5SDavid Howells 	bool stop = true;
444e87b03f5SDavid Howells 	unsigned int i;
4454343d008SDavid Howells 
446e87b03f5SDavid Howells 	XA_STATE(xas, &mapping->i_pages, index);
447e87b03f5SDavid Howells 	pagevec_init(&pvec);
448e87b03f5SDavid Howells 
44931143d5dSDavid Howells 	do {
450e87b03f5SDavid Howells 		/* Firstly, we gather up a batch of contiguous dirty pages
451e87b03f5SDavid Howells 		 * under the RCU read lock - but we can't clear the dirty flags
452e87b03f5SDavid Howells 		 * there if any of those pages are mapped.
453e87b03f5SDavid Howells 		 */
454e87b03f5SDavid Howells 		rcu_read_lock();
455e87b03f5SDavid Howells 
45678525c74SDavid Howells 		xas_for_each(&xas, folio, ULONG_MAX) {
457e87b03f5SDavid Howells 			stop = true;
45878525c74SDavid Howells 			if (xas_retry(&xas, folio))
459e87b03f5SDavid Howells 				continue;
46078525c74SDavid Howells 			if (xa_is_value(folio))
461e87b03f5SDavid Howells 				break;
46278525c74SDavid Howells 			if (folio_index(folio) != index)
463e87b03f5SDavid Howells 				break;
464e87b03f5SDavid Howells 
46578525c74SDavid Howells 			if (!folio_try_get_rcu(folio)) {
466e87b03f5SDavid Howells 				xas_reset(&xas);
467e87b03f5SDavid Howells 				continue;
46831143d5dSDavid Howells 			}
46931143d5dSDavid Howells 
470e87b03f5SDavid Howells 			/* Has the page moved or been split? */
47178525c74SDavid Howells 			if (unlikely(folio != xas_reload(&xas))) {
47278525c74SDavid Howells 				folio_put(folio);
4735a813276SDavid Howells 				break;
474581b2027SDavid Howells 			}
475e87b03f5SDavid Howells 
47678525c74SDavid Howells 			if (!folio_trylock(folio)) {
47778525c74SDavid Howells 				folio_put(folio);
47831143d5dSDavid Howells 				break;
479581b2027SDavid Howells 			}
480c7f75ef3SDavid Howells 			if (!folio_test_dirty(folio) ||
481c7f75ef3SDavid Howells 			    folio_test_writeback(folio) ||
482c7f75ef3SDavid Howells 			    folio_test_fscache(folio)) {
48378525c74SDavid Howells 				folio_unlock(folio);
48478525c74SDavid Howells 				folio_put(folio);
48531143d5dSDavid Howells 				break;
48631143d5dSDavid Howells 			}
4874343d008SDavid Howells 
48878525c74SDavid Howells 			psize = folio_size(folio);
48978525c74SDavid Howells 			priv = (unsigned long)folio_get_private(folio);
49078525c74SDavid Howells 			f = afs_folio_dirty_from(folio, priv);
49178525c74SDavid Howells 			t = afs_folio_dirty_to(folio, priv);
492810caa3eSDavid Howells 			if (f != 0 && !new_content) {
49378525c74SDavid Howells 				folio_unlock(folio);
49478525c74SDavid Howells 				folio_put(folio);
4954343d008SDavid Howells 				break;
4964343d008SDavid Howells 			}
4974343d008SDavid Howells 
498e87b03f5SDavid Howells 			len += filler + t;
499e87b03f5SDavid Howells 			filler = psize - t;
500e87b03f5SDavid Howells 			if (len >= max_len || *_count <= 0)
501e87b03f5SDavid Howells 				stop = true;
502e87b03f5SDavid Howells 			else if (t == psize || new_content)
503e87b03f5SDavid Howells 				stop = false;
504e87b03f5SDavid Howells 
50578525c74SDavid Howells 			index += folio_nr_pages(folio);
50678525c74SDavid Howells 			if (!pagevec_add(&pvec, &folio->page))
507e87b03f5SDavid Howells 				break;
508e87b03f5SDavid Howells 			if (stop)
509e87b03f5SDavid Howells 				break;
510e87b03f5SDavid Howells 		}
511e87b03f5SDavid Howells 
512e87b03f5SDavid Howells 		if (!stop)
513e87b03f5SDavid Howells 			xas_pause(&xas);
514e87b03f5SDavid Howells 		rcu_read_unlock();
515e87b03f5SDavid Howells 
516e87b03f5SDavid Howells 		/* Now, if we obtained any pages, we can shift them to being
517e87b03f5SDavid Howells 		 * writable and mark them for caching.
518e87b03f5SDavid Howells 		 */
519e87b03f5SDavid Howells 		if (!pagevec_count(&pvec))
520e87b03f5SDavid Howells 			break;
521e87b03f5SDavid Howells 
522e87b03f5SDavid Howells 		for (i = 0; i < pagevec_count(&pvec); i++) {
52378525c74SDavid Howells 			folio = page_folio(pvec.pages[i]);
52478525c74SDavid Howells 			trace_afs_folio_dirty(vnode, tracepoint_string("store+"), folio);
52513524ab3SDavid Howells 
52678525c74SDavid Howells 			if (!folio_clear_dirty_for_io(folio))
52731143d5dSDavid Howells 				BUG();
52878525c74SDavid Howells 			if (folio_start_writeback(folio))
52931143d5dSDavid Howells 				BUG();
530c7f75ef3SDavid Howells 			afs_folio_start_fscache(caching, folio);
531e87b03f5SDavid Howells 
53278525c74SDavid Howells 			*_count -= folio_nr_pages(folio);
53378525c74SDavid Howells 			folio_unlock(folio);
53431143d5dSDavid Howells 		}
53531143d5dSDavid Howells 
536e87b03f5SDavid Howells 		pagevec_release(&pvec);
537e87b03f5SDavid Howells 		cond_resched();
538e87b03f5SDavid Howells 	} while (!stop);
53931143d5dSDavid Howells 
540e87b03f5SDavid Howells 	*_len = len;
541810caa3eSDavid Howells }
542810caa3eSDavid Howells 
543810caa3eSDavid Howells /*
544810caa3eSDavid Howells  * Synchronously write back the locked page and any subsequent non-locked dirty
545810caa3eSDavid Howells  * pages.
546810caa3eSDavid Howells  */
54778525c74SDavid Howells static ssize_t afs_write_back_from_locked_folio(struct address_space *mapping,
548810caa3eSDavid Howells 						struct writeback_control *wbc,
54978525c74SDavid Howells 						struct folio *folio,
550e87b03f5SDavid Howells 						loff_t start, loff_t end)
551810caa3eSDavid Howells {
552810caa3eSDavid Howells 	struct afs_vnode *vnode = AFS_FS_I(mapping->host);
553810caa3eSDavid Howells 	struct iov_iter iter;
554e87b03f5SDavid Howells 	unsigned long priv;
555e87b03f5SDavid Howells 	unsigned int offset, to, len, max_len;
556e87b03f5SDavid Howells 	loff_t i_size = i_size_read(&vnode->vfs_inode);
557810caa3eSDavid Howells 	bool new_content = test_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags);
558c7f75ef3SDavid Howells 	bool caching = fscache_cookie_enabled(afs_vnode_cache(vnode));
559e87b03f5SDavid Howells 	long count = wbc->nr_to_write;
560810caa3eSDavid Howells 	int ret;
561810caa3eSDavid Howells 
56278525c74SDavid Howells 	_enter(",%lx,%llx-%llx", folio_index(folio), start, end);
563810caa3eSDavid Howells 
56478525c74SDavid Howells 	if (folio_start_writeback(folio))
565810caa3eSDavid Howells 		BUG();
566c7f75ef3SDavid Howells 	afs_folio_start_fscache(caching, folio);
567810caa3eSDavid Howells 
56878525c74SDavid Howells 	count -= folio_nr_pages(folio);
569e87b03f5SDavid Howells 
570810caa3eSDavid Howells 	/* Find all consecutive lockable dirty pages that have contiguous
571810caa3eSDavid Howells 	 * written regions, stopping when we find a page that is not
572810caa3eSDavid Howells 	 * immediately lockable, is not dirty or is missing, or we reach the
573810caa3eSDavid Howells 	 * end of the range.
574810caa3eSDavid Howells 	 */
57578525c74SDavid Howells 	priv = (unsigned long)folio_get_private(folio);
57678525c74SDavid Howells 	offset = afs_folio_dirty_from(folio, priv);
57778525c74SDavid Howells 	to = afs_folio_dirty_to(folio, priv);
57878525c74SDavid Howells 	trace_afs_folio_dirty(vnode, tracepoint_string("store"), folio);
579810caa3eSDavid Howells 
580e87b03f5SDavid Howells 	len = to - offset;
581e87b03f5SDavid Howells 	start += offset;
582e87b03f5SDavid Howells 	if (start < i_size) {
583e87b03f5SDavid Howells 		/* Trim the write to the EOF; the extra data is ignored.  Also
584e87b03f5SDavid Howells 		 * put an upper limit on the size of a single storedata op.
585e87b03f5SDavid Howells 		 */
586e87b03f5SDavid Howells 		max_len = 65536 * 4096;
587e87b03f5SDavid Howells 		max_len = min_t(unsigned long long, max_len, end - start + 1);
588e87b03f5SDavid Howells 		max_len = min_t(unsigned long long, max_len, i_size - start);
589810caa3eSDavid Howells 
590e87b03f5SDavid Howells 		if (len < max_len &&
59178525c74SDavid Howells 		    (to == folio_size(folio) || new_content))
592e87b03f5SDavid Howells 			afs_extend_writeback(mapping, vnode, &count,
593c7f75ef3SDavid Howells 					     start, max_len, new_content,
594c7f75ef3SDavid Howells 					     caching, &len);
595e87b03f5SDavid Howells 		len = min_t(loff_t, len, max_len);
596e87b03f5SDavid Howells 	}
597810caa3eSDavid Howells 
5984343d008SDavid Howells 	/* We now have a contiguous set of dirty pages, each with writeback
5994343d008SDavid Howells 	 * set; the first page is still locked at this point, but all the rest
6004343d008SDavid Howells 	 * have been unlocked.
6014343d008SDavid Howells 	 */
60278525c74SDavid Howells 	folio_unlock(folio);
6034343d008SDavid Howells 
604e87b03f5SDavid Howells 	if (start < i_size) {
605e87b03f5SDavid Howells 		_debug("write back %x @%llx [%llx]", len, start, i_size);
60631143d5dSDavid Howells 
607c7f75ef3SDavid Howells 		/* Speculatively write to the cache.  We have to fix this up
608c7f75ef3SDavid Howells 		 * later if the store fails.
609c7f75ef3SDavid Howells 		 */
610c7f75ef3SDavid Howells 		afs_write_to_cache(vnode, start, len, i_size, caching);
611c7f75ef3SDavid Howells 
612e87b03f5SDavid Howells 		iov_iter_xarray(&iter, WRITE, &mapping->i_pages, start, len);
613e87b03f5SDavid Howells 		ret = afs_store_data(vnode, &iter, start, false);
614bd80d8a8SDavid Howells 	} else {
615e87b03f5SDavid Howells 		_debug("write discard %x @%llx [%llx]", len, start, i_size);
616e87b03f5SDavid Howells 
617bd80d8a8SDavid Howells 		/* The dirty region was entirely beyond the EOF. */
618c7f75ef3SDavid Howells 		fscache_clear_page_bits(afs_vnode_cache(vnode),
619c7f75ef3SDavid Howells 					mapping, start, len, caching);
620e87b03f5SDavid Howells 		afs_pages_written_back(vnode, start, len);
621bd80d8a8SDavid Howells 		ret = 0;
622bd80d8a8SDavid Howells 	}
623bd80d8a8SDavid Howells 
62431143d5dSDavid Howells 	switch (ret) {
6254343d008SDavid Howells 	case 0:
626e87b03f5SDavid Howells 		wbc->nr_to_write = count;
627e87b03f5SDavid Howells 		ret = len;
6284343d008SDavid Howells 		break;
6294343d008SDavid Howells 
6304343d008SDavid Howells 	default:
6314343d008SDavid Howells 		pr_notice("kAFS: Unexpected error from FS.StoreData %d\n", ret);
632df561f66SGustavo A. R. Silva 		fallthrough;
6334343d008SDavid Howells 	case -EACCES:
6344343d008SDavid Howells 	case -EPERM:
6354343d008SDavid Howells 	case -ENOKEY:
6364343d008SDavid Howells 	case -EKEYEXPIRED:
6374343d008SDavid Howells 	case -EKEYREJECTED:
6384343d008SDavid Howells 	case -EKEYREVOKED:
639e87b03f5SDavid Howells 		afs_redirty_pages(wbc, mapping, start, len);
6404343d008SDavid Howells 		mapping_set_error(mapping, ret);
6414343d008SDavid Howells 		break;
6424343d008SDavid Howells 
64331143d5dSDavid Howells 	case -EDQUOT:
64431143d5dSDavid Howells 	case -ENOSPC:
645e87b03f5SDavid Howells 		afs_redirty_pages(wbc, mapping, start, len);
6464343d008SDavid Howells 		mapping_set_error(mapping, -ENOSPC);
64731143d5dSDavid Howells 		break;
6484343d008SDavid Howells 
64931143d5dSDavid Howells 	case -EROFS:
65031143d5dSDavid Howells 	case -EIO:
65131143d5dSDavid Howells 	case -EREMOTEIO:
65231143d5dSDavid Howells 	case -EFBIG:
65331143d5dSDavid Howells 	case -ENOENT:
65431143d5dSDavid Howells 	case -ENOMEDIUM:
65531143d5dSDavid Howells 	case -ENXIO:
656f51375cdSDavid Howells 		trace_afs_file_error(vnode, ret, afs_file_error_writeback_fail);
657e87b03f5SDavid Howells 		afs_kill_pages(mapping, start, len);
6584343d008SDavid Howells 		mapping_set_error(mapping, ret);
65931143d5dSDavid Howells 		break;
66031143d5dSDavid Howells 	}
66131143d5dSDavid Howells 
66231143d5dSDavid Howells 	_leave(" = %d", ret);
66331143d5dSDavid Howells 	return ret;
66431143d5dSDavid Howells }
66531143d5dSDavid Howells 
66631143d5dSDavid Howells /*
66731143d5dSDavid Howells  * write a page back to the server
66831143d5dSDavid Howells  * - the caller locked the page for us
66931143d5dSDavid Howells  */
67078525c74SDavid Howells int afs_writepage(struct page *subpage, struct writeback_control *wbc)
67131143d5dSDavid Howells {
67278525c74SDavid Howells 	struct folio *folio = page_folio(subpage);
673e87b03f5SDavid Howells 	ssize_t ret;
674e87b03f5SDavid Howells 	loff_t start;
67531143d5dSDavid Howells 
67678525c74SDavid Howells 	_enter("{%lx},", folio_index(folio));
67731143d5dSDavid Howells 
678c7f75ef3SDavid Howells #ifdef CONFIG_AFS_FSCACHE
679c7f75ef3SDavid Howells 	folio_wait_fscache(folio);
680c7f75ef3SDavid Howells #endif
681c7f75ef3SDavid Howells 
68278525c74SDavid Howells 	start = folio_index(folio) * PAGE_SIZE;
68378525c74SDavid Howells 	ret = afs_write_back_from_locked_folio(folio_mapping(folio), wbc,
68478525c74SDavid Howells 					       folio, start, LLONG_MAX - start);
68531143d5dSDavid Howells 	if (ret < 0) {
686e87b03f5SDavid Howells 		_leave(" = %zd", ret);
687e87b03f5SDavid Howells 		return ret;
68831143d5dSDavid Howells 	}
68931143d5dSDavid Howells 
69031143d5dSDavid Howells 	_leave(" = 0");
69131143d5dSDavid Howells 	return 0;
69231143d5dSDavid Howells }
69331143d5dSDavid Howells 
69431143d5dSDavid Howells /*
69531143d5dSDavid Howells  * write a region of pages back to the server
69631143d5dSDavid Howells  */
697c1206a2cSAdrian Bunk static int afs_writepages_region(struct address_space *mapping,
69831143d5dSDavid Howells 				 struct writeback_control *wbc,
699e87b03f5SDavid Howells 				 loff_t start, loff_t end, loff_t *_next)
70031143d5dSDavid Howells {
70178525c74SDavid Howells 	struct folio *folio;
70278525c74SDavid Howells 	struct page *head_page;
703e87b03f5SDavid Howells 	ssize_t ret;
704e87b03f5SDavid Howells 	int n;
70531143d5dSDavid Howells 
706e87b03f5SDavid Howells 	_enter("%llx,%llx,", start, end);
70731143d5dSDavid Howells 
70831143d5dSDavid Howells 	do {
709e87b03f5SDavid Howells 		pgoff_t index = start / PAGE_SIZE;
710e87b03f5SDavid Howells 
711e87b03f5SDavid Howells 		n = find_get_pages_range_tag(mapping, &index, end / PAGE_SIZE,
71278525c74SDavid Howells 					     PAGECACHE_TAG_DIRTY, 1, &head_page);
71331143d5dSDavid Howells 		if (!n)
71431143d5dSDavid Howells 			break;
71531143d5dSDavid Howells 
71678525c74SDavid Howells 		folio = page_folio(head_page);
71778525c74SDavid Howells 		start = folio_pos(folio); /* May regress with THPs */
718e87b03f5SDavid Howells 
71978525c74SDavid Howells 		_debug("wback %lx", folio_index(folio));
72031143d5dSDavid Howells 
721e87b03f5SDavid Howells 		/* At this point we hold neither the i_pages lock nor the
722b93b0163SMatthew Wilcox 		 * page lock: the page may be truncated or invalidated
723b93b0163SMatthew Wilcox 		 * (changing page->mapping to NULL), or even swizzled
724b93b0163SMatthew Wilcox 		 * back from swapper_space to tmpfs file mapping
72531143d5dSDavid Howells 		 */
726e87b03f5SDavid Howells 		if (wbc->sync_mode != WB_SYNC_NONE) {
72778525c74SDavid Howells 			ret = folio_lock_killable(folio);
7284343d008SDavid Howells 			if (ret < 0) {
72978525c74SDavid Howells 				folio_put(folio);
7304343d008SDavid Howells 				return ret;
7314343d008SDavid Howells 			}
732e87b03f5SDavid Howells 		} else {
73378525c74SDavid Howells 			if (!folio_trylock(folio)) {
73478525c74SDavid Howells 				folio_put(folio);
735e87b03f5SDavid Howells 				return 0;
736e87b03f5SDavid Howells 			}
737e87b03f5SDavid Howells 		}
73831143d5dSDavid Howells 
73978525c74SDavid Howells 		if (folio_mapping(folio) != mapping ||
74078525c74SDavid Howells 		    !folio_test_dirty(folio)) {
74178525c74SDavid Howells 			start += folio_size(folio);
74278525c74SDavid Howells 			folio_unlock(folio);
74378525c74SDavid Howells 			folio_put(folio);
74431143d5dSDavid Howells 			continue;
74531143d5dSDavid Howells 		}
74631143d5dSDavid Howells 
747c7f75ef3SDavid Howells 		if (folio_test_writeback(folio) ||
748c7f75ef3SDavid Howells 		    folio_test_fscache(folio)) {
74978525c74SDavid Howells 			folio_unlock(folio);
750c7f75ef3SDavid Howells 			if (wbc->sync_mode != WB_SYNC_NONE) {
75178525c74SDavid Howells 				folio_wait_writeback(folio);
752c7f75ef3SDavid Howells #ifdef CONFIG_AFS_FSCACHE
753c7f75ef3SDavid Howells 				folio_wait_fscache(folio);
754c7f75ef3SDavid Howells #endif
755c7f75ef3SDavid Howells 			}
75678525c74SDavid Howells 			folio_put(folio);
75731143d5dSDavid Howells 			continue;
75831143d5dSDavid Howells 		}
75931143d5dSDavid Howells 
76078525c74SDavid Howells 		if (!folio_clear_dirty_for_io(folio))
76165a15109SDavid Howells 			BUG();
76278525c74SDavid Howells 		ret = afs_write_back_from_locked_folio(mapping, wbc, folio, start, end);
76378525c74SDavid Howells 		folio_put(folio);
76431143d5dSDavid Howells 		if (ret < 0) {
765e87b03f5SDavid Howells 			_leave(" = %zd", ret);
76631143d5dSDavid Howells 			return ret;
76731143d5dSDavid Howells 		}
76831143d5dSDavid Howells 
769dc255730SMarc Dionne 		start += ret;
77031143d5dSDavid Howells 
77131143d5dSDavid Howells 		cond_resched();
772e87b03f5SDavid Howells 	} while (wbc->nr_to_write > 0);
77331143d5dSDavid Howells 
774e87b03f5SDavid Howells 	*_next = start;
775e87b03f5SDavid Howells 	_leave(" = 0 [%llx]", *_next);
77631143d5dSDavid Howells 	return 0;
77731143d5dSDavid Howells }
77831143d5dSDavid Howells 
77931143d5dSDavid Howells /*
78031143d5dSDavid Howells  * write some of the pending data back to the server
78131143d5dSDavid Howells  */
78231143d5dSDavid Howells int afs_writepages(struct address_space *mapping,
78331143d5dSDavid Howells 		   struct writeback_control *wbc)
78431143d5dSDavid Howells {
785ec0fa0b6SDavid Howells 	struct afs_vnode *vnode = AFS_FS_I(mapping->host);
786e87b03f5SDavid Howells 	loff_t start, next;
78731143d5dSDavid Howells 	int ret;
78831143d5dSDavid Howells 
78931143d5dSDavid Howells 	_enter("");
79031143d5dSDavid Howells 
791ec0fa0b6SDavid Howells 	/* We have to be careful as we can end up racing with setattr()
792ec0fa0b6SDavid Howells 	 * truncating the pagecache since the caller doesn't take a lock here
793ec0fa0b6SDavid Howells 	 * to prevent it.
794ec0fa0b6SDavid Howells 	 */
795ec0fa0b6SDavid Howells 	if (wbc->sync_mode == WB_SYNC_ALL)
796ec0fa0b6SDavid Howells 		down_read(&vnode->validate_lock);
797ec0fa0b6SDavid Howells 	else if (!down_read_trylock(&vnode->validate_lock))
798ec0fa0b6SDavid Howells 		return 0;
799ec0fa0b6SDavid Howells 
80031143d5dSDavid Howells 	if (wbc->range_cyclic) {
801e87b03f5SDavid Howells 		start = mapping->writeback_index * PAGE_SIZE;
802e87b03f5SDavid Howells 		ret = afs_writepages_region(mapping, wbc, start, LLONG_MAX, &next);
803afe69498STom Rix 		if (ret == 0) {
804e87b03f5SDavid Howells 			mapping->writeback_index = next / PAGE_SIZE;
805afe69498STom Rix 			if (start > 0 && wbc->nr_to_write > 0) {
806afe69498STom Rix 				ret = afs_writepages_region(mapping, wbc, 0,
807afe69498STom Rix 							    start, &next);
808afe69498STom Rix 				if (ret == 0)
809afe69498STom Rix 					mapping->writeback_index =
810afe69498STom Rix 						next / PAGE_SIZE;
811afe69498STom Rix 			}
812afe69498STom Rix 		}
81331143d5dSDavid Howells 	} else if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) {
814e87b03f5SDavid Howells 		ret = afs_writepages_region(mapping, wbc, 0, LLONG_MAX, &next);
815afe69498STom Rix 		if (wbc->nr_to_write > 0 && ret == 0)
8165a972474SDavid Howells 			mapping->writeback_index = next / PAGE_SIZE;
81731143d5dSDavid Howells 	} else {
818e87b03f5SDavid Howells 		ret = afs_writepages_region(mapping, wbc,
819e87b03f5SDavid Howells 					    wbc->range_start, wbc->range_end, &next);
82031143d5dSDavid Howells 	}
82131143d5dSDavid Howells 
822ec0fa0b6SDavid Howells 	up_read(&vnode->validate_lock);
82331143d5dSDavid Howells 	_leave(" = %d", ret);
82431143d5dSDavid Howells 	return ret;
82531143d5dSDavid Howells }
82631143d5dSDavid Howells 
82731143d5dSDavid Howells /*
82831143d5dSDavid Howells  * write to an AFS file
82931143d5dSDavid Howells  */
83050b5551dSAl Viro ssize_t afs_file_write(struct kiocb *iocb, struct iov_iter *from)
83131143d5dSDavid Howells {
832496ad9aaSAl Viro 	struct afs_vnode *vnode = AFS_FS_I(file_inode(iocb->ki_filp));
8333978d816SDavid Howells 	struct afs_file *af = iocb->ki_filp->private_data;
83431143d5dSDavid Howells 	ssize_t result;
83550b5551dSAl Viro 	size_t count = iov_iter_count(from);
83631143d5dSDavid Howells 
8373b6492dfSDavid Howells 	_enter("{%llx:%llu},{%zu},",
83850b5551dSAl Viro 	       vnode->fid.vid, vnode->fid.vnode, count);
83931143d5dSDavid Howells 
84031143d5dSDavid Howells 	if (IS_SWAPFILE(&vnode->vfs_inode)) {
84131143d5dSDavid Howells 		printk(KERN_INFO
84231143d5dSDavid Howells 		       "AFS: Attempt to write to active swap file!\n");
84331143d5dSDavid Howells 		return -EBUSY;
84431143d5dSDavid Howells 	}
84531143d5dSDavid Howells 
84631143d5dSDavid Howells 	if (!count)
84731143d5dSDavid Howells 		return 0;
84831143d5dSDavid Howells 
8493978d816SDavid Howells 	result = afs_validate(vnode, af->key);
8503978d816SDavid Howells 	if (result < 0)
8513978d816SDavid Howells 		return result;
8523978d816SDavid Howells 
85350b5551dSAl Viro 	result = generic_file_write_iter(iocb, from);
85431143d5dSDavid Howells 
85531143d5dSDavid Howells 	_leave(" = %zd", result);
85631143d5dSDavid Howells 	return result;
85731143d5dSDavid Howells }
85831143d5dSDavid Howells 
85931143d5dSDavid Howells /*
86031143d5dSDavid Howells  * flush any dirty pages for this process, and check for write errors.
86131143d5dSDavid Howells  * - the return status from this call provides a reliable indication of
86231143d5dSDavid Howells  *   whether any write errors occurred for this process.
86331143d5dSDavid Howells  */
86402c24a82SJosef Bacik int afs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
86531143d5dSDavid Howells {
8663978d816SDavid Howells 	struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
8673978d816SDavid Howells 	struct afs_file *af = file->private_data;
8683978d816SDavid Howells 	int ret;
86931143d5dSDavid Howells 
8703b6492dfSDavid Howells 	_enter("{%llx:%llu},{n=%pD},%d",
8713c981bfcSAl Viro 	       vnode->fid.vid, vnode->fid.vnode, file,
87231143d5dSDavid Howells 	       datasync);
87331143d5dSDavid Howells 
8743978d816SDavid Howells 	ret = afs_validate(vnode, af->key);
8753978d816SDavid Howells 	if (ret < 0)
8763978d816SDavid Howells 		return ret;
8773978d816SDavid Howells 
8784343d008SDavid Howells 	return file_write_and_wait_range(file, start, end);
87931143d5dSDavid Howells }
8809b3f26c9SDavid Howells 
8819b3f26c9SDavid Howells /*
8829b3f26c9SDavid Howells  * notification that a previously read-only page is about to become writable
8839b3f26c9SDavid Howells  * - if it returns an error, the caller will deliver a bus error signal
8849b3f26c9SDavid Howells  */
8850722f186SSouptick Joarder vm_fault_t afs_page_mkwrite(struct vm_fault *vmf)
8869b3f26c9SDavid Howells {
887490e016fSMatthew Wilcox (Oracle) 	struct folio *folio = page_folio(vmf->page);
8881cf7a151SDavid Howells 	struct file *file = vmf->vma->vm_file;
8891cf7a151SDavid Howells 	struct inode *inode = file_inode(file);
8901cf7a151SDavid Howells 	struct afs_vnode *vnode = AFS_FS_I(inode);
8913978d816SDavid Howells 	struct afs_file *af = file->private_data;
8921cf7a151SDavid Howells 	unsigned long priv;
8939620ad86SMatthew Wilcox (Oracle) 	vm_fault_t ret = VM_FAULT_RETRY;
8949b3f26c9SDavid Howells 
89578525c74SDavid Howells 	_enter("{{%llx:%llu}},{%lx}", vnode->fid.vid, vnode->fid.vnode, folio_index(folio));
8969b3f26c9SDavid Howells 
8973978d816SDavid Howells 	afs_validate(vnode, af->key);
8983978d816SDavid Howells 
8991cf7a151SDavid Howells 	sb_start_pagefault(inode->i_sb);
9001cf7a151SDavid Howells 
9011cf7a151SDavid Howells 	/* Wait for the page to be written to the cache before we allow it to
9021cf7a151SDavid Howells 	 * be modified.  We then assume the entire page will need writing back.
9031cf7a151SDavid Howells 	 */
904630f5ddaSDavid Howells #ifdef CONFIG_AFS_FSCACHE
90578525c74SDavid Howells 	if (folio_test_fscache(folio) &&
90678525c74SDavid Howells 	    folio_wait_fscache_killable(folio) < 0)
9079620ad86SMatthew Wilcox (Oracle) 		goto out;
908630f5ddaSDavid Howells #endif
9099b3f26c9SDavid Howells 
910490e016fSMatthew Wilcox (Oracle) 	if (folio_wait_writeback_killable(folio))
9119620ad86SMatthew Wilcox (Oracle) 		goto out;
9121cf7a151SDavid Howells 
91378525c74SDavid Howells 	if (folio_lock_killable(folio) < 0)
9149620ad86SMatthew Wilcox (Oracle) 		goto out;
9151cf7a151SDavid Howells 
91678525c74SDavid Howells 	/* We mustn't change folio->private until writeback is complete as that
9171cf7a151SDavid Howells 	 * details the portion of the page we need to write back and we might
9181cf7a151SDavid Howells 	 * need to redirty the page if there's a problem.
9191cf7a151SDavid Howells 	 */
920490e016fSMatthew Wilcox (Oracle) 	if (folio_wait_writeback_killable(folio) < 0) {
921490e016fSMatthew Wilcox (Oracle) 		folio_unlock(folio);
9229620ad86SMatthew Wilcox (Oracle) 		goto out;
9235cbf0398SDavid Howells 	}
9241cf7a151SDavid Howells 
92578525c74SDavid Howells 	priv = afs_folio_dirty(folio, 0, folio_size(folio));
92678525c74SDavid Howells 	priv = afs_folio_dirty_mmapped(priv);
92778525c74SDavid Howells 	if (folio_test_private(folio)) {
92878525c74SDavid Howells 		folio_change_private(folio, (void *)priv);
92978525c74SDavid Howells 		trace_afs_folio_dirty(vnode, tracepoint_string("mkwrite+"), folio);
930e87b03f5SDavid Howells 	} else {
93178525c74SDavid Howells 		folio_attach_private(folio, (void *)priv);
93278525c74SDavid Howells 		trace_afs_folio_dirty(vnode, tracepoint_string("mkwrite"), folio);
933e87b03f5SDavid Howells 	}
934bb413489SDavid Howells 	file_update_time(file);
9351cf7a151SDavid Howells 
9369620ad86SMatthew Wilcox (Oracle) 	ret = VM_FAULT_LOCKED;
9379620ad86SMatthew Wilcox (Oracle) out:
9381cf7a151SDavid Howells 	sb_end_pagefault(inode->i_sb);
9399620ad86SMatthew Wilcox (Oracle) 	return ret;
9409b3f26c9SDavid Howells }
9414343d008SDavid Howells 
9424343d008SDavid Howells /*
9434343d008SDavid Howells  * Prune the keys cached for writeback.  The caller must hold vnode->wb_lock.
9444343d008SDavid Howells  */
9454343d008SDavid Howells void afs_prune_wb_keys(struct afs_vnode *vnode)
9464343d008SDavid Howells {
9474343d008SDavid Howells 	LIST_HEAD(graveyard);
9484343d008SDavid Howells 	struct afs_wb_key *wbk, *tmp;
9494343d008SDavid Howells 
9504343d008SDavid Howells 	/* Discard unused keys */
9514343d008SDavid Howells 	spin_lock(&vnode->wb_lock);
9524343d008SDavid Howells 
9534343d008SDavid Howells 	if (!mapping_tagged(&vnode->vfs_inode.i_data, PAGECACHE_TAG_WRITEBACK) &&
9544343d008SDavid Howells 	    !mapping_tagged(&vnode->vfs_inode.i_data, PAGECACHE_TAG_DIRTY)) {
9554343d008SDavid Howells 		list_for_each_entry_safe(wbk, tmp, &vnode->wb_keys, vnode_link) {
9564343d008SDavid Howells 			if (refcount_read(&wbk->usage) == 1)
9574343d008SDavid Howells 				list_move(&wbk->vnode_link, &graveyard);
9584343d008SDavid Howells 		}
9594343d008SDavid Howells 	}
9604343d008SDavid Howells 
9614343d008SDavid Howells 	spin_unlock(&vnode->wb_lock);
9624343d008SDavid Howells 
9634343d008SDavid Howells 	while (!list_empty(&graveyard)) {
9644343d008SDavid Howells 		wbk = list_entry(graveyard.next, struct afs_wb_key, vnode_link);
9654343d008SDavid Howells 		list_del(&wbk->vnode_link);
9664343d008SDavid Howells 		afs_put_wb_key(wbk);
9674343d008SDavid Howells 	}
9684343d008SDavid Howells }
9694343d008SDavid Howells 
9704343d008SDavid Howells /*
9714343d008SDavid Howells  * Clean up a page during invalidation.
9724343d008SDavid Howells  */
97378525c74SDavid Howells int afs_launder_page(struct page *subpage)
9744343d008SDavid Howells {
97578525c74SDavid Howells 	struct folio *folio = page_folio(subpage);
97678525c74SDavid Howells 	struct afs_vnode *vnode = AFS_FS_I(folio_inode(folio));
977bd80d8a8SDavid Howells 	struct iov_iter iter;
978bd80d8a8SDavid Howells 	struct bio_vec bv[1];
9794343d008SDavid Howells 	unsigned long priv;
9804343d008SDavid Howells 	unsigned int f, t;
9814343d008SDavid Howells 	int ret = 0;
9824343d008SDavid Howells 
98378525c74SDavid Howells 	_enter("{%lx}", folio_index(folio));
9844343d008SDavid Howells 
98578525c74SDavid Howells 	priv = (unsigned long)folio_get_private(folio);
98678525c74SDavid Howells 	if (folio_clear_dirty_for_io(folio)) {
9874343d008SDavid Howells 		f = 0;
98878525c74SDavid Howells 		t = folio_size(folio);
98978525c74SDavid Howells 		if (folio_test_private(folio)) {
99078525c74SDavid Howells 			f = afs_folio_dirty_from(folio, priv);
99178525c74SDavid Howells 			t = afs_folio_dirty_to(folio, priv);
9924343d008SDavid Howells 		}
9934343d008SDavid Howells 
99478525c74SDavid Howells 		bv[0].bv_page = &folio->page;
995bd80d8a8SDavid Howells 		bv[0].bv_offset = f;
996bd80d8a8SDavid Howells 		bv[0].bv_len = t - f;
997bd80d8a8SDavid Howells 		iov_iter_bvec(&iter, WRITE, bv, 1, bv[0].bv_len);
998bd80d8a8SDavid Howells 
99978525c74SDavid Howells 		trace_afs_folio_dirty(vnode, tracepoint_string("launder"), folio);
100078525c74SDavid Howells 		ret = afs_store_data(vnode, &iter, folio_pos(folio) + f, true);
10014343d008SDavid Howells 	}
10024343d008SDavid Howells 
100378525c74SDavid Howells 	trace_afs_folio_dirty(vnode, tracepoint_string("laundered"), folio);
100478525c74SDavid Howells 	folio_detach_private(folio);
100578525c74SDavid Howells 	folio_wait_fscache(folio);
10064343d008SDavid Howells 	return ret;
100731143d5dSDavid Howells }
1008c7f75ef3SDavid Howells 
1009c7f75ef3SDavid Howells /*
1010c7f75ef3SDavid Howells  * Deal with the completion of writing the data to the cache.
1011c7f75ef3SDavid Howells  */
1012c7f75ef3SDavid Howells static void afs_write_to_cache_done(void *priv, ssize_t transferred_or_error,
1013c7f75ef3SDavid Howells 				    bool was_async)
1014c7f75ef3SDavid Howells {
1015c7f75ef3SDavid Howells 	struct afs_vnode *vnode = priv;
1016c7f75ef3SDavid Howells 
1017c7f75ef3SDavid Howells 	if (IS_ERR_VALUE(transferred_or_error) &&
1018c7f75ef3SDavid Howells 	    transferred_or_error != -ENOBUFS)
1019c7f75ef3SDavid Howells 		afs_invalidate_cache(vnode, 0);
1020c7f75ef3SDavid Howells }
1021c7f75ef3SDavid Howells 
1022c7f75ef3SDavid Howells /*
1023c7f75ef3SDavid Howells  * Save the write to the cache also.
1024c7f75ef3SDavid Howells  */
1025c7f75ef3SDavid Howells static void afs_write_to_cache(struct afs_vnode *vnode,
1026c7f75ef3SDavid Howells 			       loff_t start, size_t len, loff_t i_size,
1027c7f75ef3SDavid Howells 			       bool caching)
1028c7f75ef3SDavid Howells {
1029c7f75ef3SDavid Howells 	fscache_write_to_cache(afs_vnode_cache(vnode),
1030c7f75ef3SDavid Howells 			       vnode->vfs_inode.i_mapping, start, len, i_size,
1031c7f75ef3SDavid Howells 			       afs_write_to_cache_done, vnode, caching);
1032c7f75ef3SDavid Howells }
1033