xref: /openbmc/linux/fs/afs/write.c (revision ba936421)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* handling of writes to regular files and writing back to the server
3  *
4  * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
5  * Written by David Howells (dhowells@redhat.com)
6  */
7 
8 #include <linux/backing-dev.h>
9 #include <linux/slab.h>
10 #include <linux/fs.h>
11 #include <linux/pagemap.h>
12 #include <linux/writeback.h>
13 #include <linux/pagevec.h>
14 #include <linux/netfs.h>
15 #include "internal.h"
16 
17 static void afs_write_to_cache(struct afs_vnode *vnode, loff_t start, size_t len,
18 			       loff_t i_size, bool caching);
19 
20 #ifdef CONFIG_AFS_FSCACHE
21 /*
22  * Mark a page as having been made dirty and thus needing writeback.  We also
23  * need to pin the cache object to write back to.
24  */
25 bool afs_dirty_folio(struct address_space *mapping, struct folio *folio)
26 {
27 	return fscache_dirty_folio(mapping, folio,
28 				afs_vnode_cache(AFS_FS_I(mapping->host)));
29 }
30 static void afs_folio_start_fscache(bool caching, struct folio *folio)
31 {
32 	if (caching)
33 		folio_start_fscache(folio);
34 }
35 #else
36 static void afs_folio_start_fscache(bool caching, struct folio *folio)
37 {
38 }
39 #endif
40 
41 /*
42  * prepare to perform part of a write to a page
43  */
44 int afs_write_begin(struct file *file, struct address_space *mapping,
45 		    loff_t pos, unsigned len, unsigned flags,
46 		    struct page **_page, void **fsdata)
47 {
48 	struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
49 	struct folio *folio;
50 	unsigned long priv;
51 	unsigned f, from;
52 	unsigned t, to;
53 	pgoff_t index;
54 	int ret;
55 
56 	_enter("{%llx:%llu},%llx,%x",
57 	       vnode->fid.vid, vnode->fid.vnode, pos, len);
58 
59 	/* Prefetch area to be written into the cache if we're caching this
60 	 * file.  We need to do this before we get a lock on the page in case
61 	 * there's more than one writer competing for the same cache block.
62 	 */
63 	ret = netfs_write_begin(file, mapping, pos, len, flags, &folio, fsdata);
64 	if (ret < 0)
65 		return ret;
66 
67 	index = folio_index(folio);
68 	from = pos - index * PAGE_SIZE;
69 	to = from + len;
70 
71 try_again:
72 	/* See if this page is already partially written in a way that we can
73 	 * merge the new write with.
74 	 */
75 	if (folio_test_private(folio)) {
76 		priv = (unsigned long)folio_get_private(folio);
77 		f = afs_folio_dirty_from(folio, priv);
78 		t = afs_folio_dirty_to(folio, priv);
79 		ASSERTCMP(f, <=, t);
80 
81 		if (folio_test_writeback(folio)) {
82 			trace_afs_folio_dirty(vnode, tracepoint_string("alrdy"), folio);
83 			goto flush_conflicting_write;
84 		}
85 		/* If the file is being filled locally, allow inter-write
86 		 * spaces to be merged into writes.  If it's not, only write
87 		 * back what the user gives us.
88 		 */
89 		if (!test_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags) &&
90 		    (to < f || from > t))
91 			goto flush_conflicting_write;
92 	}
93 
94 	*_page = &folio->page;
95 	_leave(" = 0");
96 	return 0;
97 
98 	/* The previous write and this write aren't adjacent or overlapping, so
99 	 * flush the page out.
100 	 */
101 flush_conflicting_write:
102 	_debug("flush conflict");
103 	ret = folio_write_one(folio);
104 	if (ret < 0)
105 		goto error;
106 
107 	ret = folio_lock_killable(folio);
108 	if (ret < 0)
109 		goto error;
110 	goto try_again;
111 
112 error:
113 	folio_put(folio);
114 	_leave(" = %d", ret);
115 	return ret;
116 }
117 
118 /*
119  * finalise part of a write to a page
120  */
121 int afs_write_end(struct file *file, struct address_space *mapping,
122 		  loff_t pos, unsigned len, unsigned copied,
123 		  struct page *subpage, void *fsdata)
124 {
125 	struct folio *folio = page_folio(subpage);
126 	struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
127 	unsigned long priv;
128 	unsigned int f, from = offset_in_folio(folio, pos);
129 	unsigned int t, to = from + copied;
130 	loff_t i_size, write_end_pos;
131 
132 	_enter("{%llx:%llu},{%lx}",
133 	       vnode->fid.vid, vnode->fid.vnode, folio_index(folio));
134 
135 	if (!folio_test_uptodate(folio)) {
136 		if (copied < len) {
137 			copied = 0;
138 			goto out;
139 		}
140 
141 		folio_mark_uptodate(folio);
142 	}
143 
144 	if (copied == 0)
145 		goto out;
146 
147 	write_end_pos = pos + copied;
148 
149 	i_size = i_size_read(&vnode->vfs_inode);
150 	if (write_end_pos > i_size) {
151 		write_seqlock(&vnode->cb_lock);
152 		i_size = i_size_read(&vnode->vfs_inode);
153 		if (write_end_pos > i_size)
154 			afs_set_i_size(vnode, write_end_pos);
155 		write_sequnlock(&vnode->cb_lock);
156 		fscache_update_cookie(afs_vnode_cache(vnode), NULL, &write_end_pos);
157 	}
158 
159 	if (folio_test_private(folio)) {
160 		priv = (unsigned long)folio_get_private(folio);
161 		f = afs_folio_dirty_from(folio, priv);
162 		t = afs_folio_dirty_to(folio, priv);
163 		if (from < f)
164 			f = from;
165 		if (to > t)
166 			t = to;
167 		priv = afs_folio_dirty(folio, f, t);
168 		folio_change_private(folio, (void *)priv);
169 		trace_afs_folio_dirty(vnode, tracepoint_string("dirty+"), folio);
170 	} else {
171 		priv = afs_folio_dirty(folio, from, to);
172 		folio_attach_private(folio, (void *)priv);
173 		trace_afs_folio_dirty(vnode, tracepoint_string("dirty"), folio);
174 	}
175 
176 	if (folio_mark_dirty(folio))
177 		_debug("dirtied %lx", folio_index(folio));
178 
179 out:
180 	folio_unlock(folio);
181 	folio_put(folio);
182 	return copied;
183 }
184 
185 /*
186  * kill all the pages in the given range
187  */
188 static void afs_kill_pages(struct address_space *mapping,
189 			   loff_t start, loff_t len)
190 {
191 	struct afs_vnode *vnode = AFS_FS_I(mapping->host);
192 	struct folio *folio;
193 	pgoff_t index = start / PAGE_SIZE;
194 	pgoff_t last = (start + len - 1) / PAGE_SIZE, next;
195 
196 	_enter("{%llx:%llu},%llx @%llx",
197 	       vnode->fid.vid, vnode->fid.vnode, len, start);
198 
199 	do {
200 		_debug("kill %lx (to %lx)", index, last);
201 
202 		folio = filemap_get_folio(mapping, index);
203 		if (!folio) {
204 			next = index + 1;
205 			continue;
206 		}
207 
208 		next = folio_next_index(folio);
209 
210 		folio_clear_uptodate(folio);
211 		folio_end_writeback(folio);
212 		folio_lock(folio);
213 		generic_error_remove_page(mapping, &folio->page);
214 		folio_unlock(folio);
215 		folio_put(folio);
216 
217 	} while (index = next, index <= last);
218 
219 	_leave("");
220 }
221 
222 /*
223  * Redirty all the pages in a given range.
224  */
225 static void afs_redirty_pages(struct writeback_control *wbc,
226 			      struct address_space *mapping,
227 			      loff_t start, loff_t len)
228 {
229 	struct afs_vnode *vnode = AFS_FS_I(mapping->host);
230 	struct folio *folio;
231 	pgoff_t index = start / PAGE_SIZE;
232 	pgoff_t last = (start + len - 1) / PAGE_SIZE, next;
233 
234 	_enter("{%llx:%llu},%llx @%llx",
235 	       vnode->fid.vid, vnode->fid.vnode, len, start);
236 
237 	do {
238 		_debug("redirty %llx @%llx", len, start);
239 
240 		folio = filemap_get_folio(mapping, index);
241 		if (!folio) {
242 			next = index + 1;
243 			continue;
244 		}
245 
246 		next = index + folio_nr_pages(folio);
247 		folio_redirty_for_writepage(wbc, folio);
248 		folio_end_writeback(folio);
249 		folio_put(folio);
250 	} while (index = next, index <= last);
251 
252 	_leave("");
253 }
254 
255 /*
256  * completion of write to server
257  */
258 static void afs_pages_written_back(struct afs_vnode *vnode, loff_t start, unsigned int len)
259 {
260 	struct address_space *mapping = vnode->vfs_inode.i_mapping;
261 	struct folio *folio;
262 	pgoff_t end;
263 
264 	XA_STATE(xas, &mapping->i_pages, start / PAGE_SIZE);
265 
266 	_enter("{%llx:%llu},{%x @%llx}",
267 	       vnode->fid.vid, vnode->fid.vnode, len, start);
268 
269 	rcu_read_lock();
270 
271 	end = (start + len - 1) / PAGE_SIZE;
272 	xas_for_each(&xas, folio, end) {
273 		if (!folio_test_writeback(folio)) {
274 			kdebug("bad %x @%llx page %lx %lx",
275 			       len, start, folio_index(folio), end);
276 			ASSERT(folio_test_writeback(folio));
277 		}
278 
279 		trace_afs_folio_dirty(vnode, tracepoint_string("clear"), folio);
280 		folio_detach_private(folio);
281 		folio_end_writeback(folio);
282 	}
283 
284 	rcu_read_unlock();
285 
286 	afs_prune_wb_keys(vnode);
287 	_leave("");
288 }
289 
290 /*
291  * Find a key to use for the writeback.  We cached the keys used to author the
292  * writes on the vnode.  *_wbk will contain the last writeback key used or NULL
293  * and we need to start from there if it's set.
294  */
295 static int afs_get_writeback_key(struct afs_vnode *vnode,
296 				 struct afs_wb_key **_wbk)
297 {
298 	struct afs_wb_key *wbk = NULL;
299 	struct list_head *p;
300 	int ret = -ENOKEY, ret2;
301 
302 	spin_lock(&vnode->wb_lock);
303 	if (*_wbk)
304 		p = (*_wbk)->vnode_link.next;
305 	else
306 		p = vnode->wb_keys.next;
307 
308 	while (p != &vnode->wb_keys) {
309 		wbk = list_entry(p, struct afs_wb_key, vnode_link);
310 		_debug("wbk %u", key_serial(wbk->key));
311 		ret2 = key_validate(wbk->key);
312 		if (ret2 == 0) {
313 			refcount_inc(&wbk->usage);
314 			_debug("USE WB KEY %u", key_serial(wbk->key));
315 			break;
316 		}
317 
318 		wbk = NULL;
319 		if (ret == -ENOKEY)
320 			ret = ret2;
321 		p = p->next;
322 	}
323 
324 	spin_unlock(&vnode->wb_lock);
325 	if (*_wbk)
326 		afs_put_wb_key(*_wbk);
327 	*_wbk = wbk;
328 	return 0;
329 }
330 
331 static void afs_store_data_success(struct afs_operation *op)
332 {
333 	struct afs_vnode *vnode = op->file[0].vnode;
334 
335 	op->ctime = op->file[0].scb.status.mtime_client;
336 	afs_vnode_commit_status(op, &op->file[0]);
337 	if (op->error == 0) {
338 		if (!op->store.laundering)
339 			afs_pages_written_back(vnode, op->store.pos, op->store.size);
340 		afs_stat_v(vnode, n_stores);
341 		atomic_long_add(op->store.size, &afs_v2net(vnode)->n_store_bytes);
342 	}
343 }
344 
345 static const struct afs_operation_ops afs_store_data_operation = {
346 	.issue_afs_rpc	= afs_fs_store_data,
347 	.issue_yfs_rpc	= yfs_fs_store_data,
348 	.success	= afs_store_data_success,
349 };
350 
351 /*
352  * write to a file
353  */
354 static int afs_store_data(struct afs_vnode *vnode, struct iov_iter *iter, loff_t pos,
355 			  bool laundering)
356 {
357 	struct netfs_i_context *ictx = &vnode->netfs_ctx;
358 	struct afs_operation *op;
359 	struct afs_wb_key *wbk = NULL;
360 	loff_t size = iov_iter_count(iter);
361 	int ret = -ENOKEY;
362 
363 	_enter("%s{%llx:%llu.%u},%llx,%llx",
364 	       vnode->volume->name,
365 	       vnode->fid.vid,
366 	       vnode->fid.vnode,
367 	       vnode->fid.unique,
368 	       size, pos);
369 
370 	ret = afs_get_writeback_key(vnode, &wbk);
371 	if (ret) {
372 		_leave(" = %d [no keys]", ret);
373 		return ret;
374 	}
375 
376 	op = afs_alloc_operation(wbk->key, vnode->volume);
377 	if (IS_ERR(op)) {
378 		afs_put_wb_key(wbk);
379 		return -ENOMEM;
380 	}
381 
382 	afs_op_set_vnode(op, 0, vnode);
383 	op->file[0].dv_delta = 1;
384 	op->file[0].modification = true;
385 	op->store.write_iter = iter;
386 	op->store.pos = pos;
387 	op->store.size = size;
388 	op->store.i_size = max(pos + size, ictx->remote_i_size);
389 	op->store.laundering = laundering;
390 	op->mtime = vnode->vfs_inode.i_mtime;
391 	op->flags |= AFS_OPERATION_UNINTR;
392 	op->ops = &afs_store_data_operation;
393 
394 try_next_key:
395 	afs_begin_vnode_operation(op);
396 	afs_wait_for_operation(op);
397 
398 	switch (op->error) {
399 	case -EACCES:
400 	case -EPERM:
401 	case -ENOKEY:
402 	case -EKEYEXPIRED:
403 	case -EKEYREJECTED:
404 	case -EKEYREVOKED:
405 		_debug("next");
406 
407 		ret = afs_get_writeback_key(vnode, &wbk);
408 		if (ret == 0) {
409 			key_put(op->key);
410 			op->key = key_get(wbk->key);
411 			goto try_next_key;
412 		}
413 		break;
414 	}
415 
416 	afs_put_wb_key(wbk);
417 	_leave(" = %d", op->error);
418 	return afs_put_operation(op);
419 }
420 
421 /*
422  * Extend the region to be written back to include subsequent contiguously
423  * dirty pages if possible, but don't sleep while doing so.
424  *
425  * If this page holds new content, then we can include filler zeros in the
426  * writeback.
427  */
428 static void afs_extend_writeback(struct address_space *mapping,
429 				 struct afs_vnode *vnode,
430 				 long *_count,
431 				 loff_t start,
432 				 loff_t max_len,
433 				 bool new_content,
434 				 bool caching,
435 				 unsigned int *_len)
436 {
437 	struct pagevec pvec;
438 	struct folio *folio;
439 	unsigned long priv;
440 	unsigned int psize, filler = 0;
441 	unsigned int f, t;
442 	loff_t len = *_len;
443 	pgoff_t index = (start + len) / PAGE_SIZE;
444 	bool stop = true;
445 	unsigned int i;
446 
447 	XA_STATE(xas, &mapping->i_pages, index);
448 	pagevec_init(&pvec);
449 
450 	do {
451 		/* Firstly, we gather up a batch of contiguous dirty pages
452 		 * under the RCU read lock - but we can't clear the dirty flags
453 		 * there if any of those pages are mapped.
454 		 */
455 		rcu_read_lock();
456 
457 		xas_for_each(&xas, folio, ULONG_MAX) {
458 			stop = true;
459 			if (xas_retry(&xas, folio))
460 				continue;
461 			if (xa_is_value(folio))
462 				break;
463 			if (folio_index(folio) != index)
464 				break;
465 
466 			if (!folio_try_get_rcu(folio)) {
467 				xas_reset(&xas);
468 				continue;
469 			}
470 
471 			/* Has the page moved or been split? */
472 			if (unlikely(folio != xas_reload(&xas))) {
473 				folio_put(folio);
474 				break;
475 			}
476 
477 			if (!folio_trylock(folio)) {
478 				folio_put(folio);
479 				break;
480 			}
481 			if (!folio_test_dirty(folio) ||
482 			    folio_test_writeback(folio) ||
483 			    folio_test_fscache(folio)) {
484 				folio_unlock(folio);
485 				folio_put(folio);
486 				break;
487 			}
488 
489 			psize = folio_size(folio);
490 			priv = (unsigned long)folio_get_private(folio);
491 			f = afs_folio_dirty_from(folio, priv);
492 			t = afs_folio_dirty_to(folio, priv);
493 			if (f != 0 && !new_content) {
494 				folio_unlock(folio);
495 				folio_put(folio);
496 				break;
497 			}
498 
499 			len += filler + t;
500 			filler = psize - t;
501 			if (len >= max_len || *_count <= 0)
502 				stop = true;
503 			else if (t == psize || new_content)
504 				stop = false;
505 
506 			index += folio_nr_pages(folio);
507 			if (!pagevec_add(&pvec, &folio->page))
508 				break;
509 			if (stop)
510 				break;
511 		}
512 
513 		if (!stop)
514 			xas_pause(&xas);
515 		rcu_read_unlock();
516 
517 		/* Now, if we obtained any pages, we can shift them to being
518 		 * writable and mark them for caching.
519 		 */
520 		if (!pagevec_count(&pvec))
521 			break;
522 
523 		for (i = 0; i < pagevec_count(&pvec); i++) {
524 			folio = page_folio(pvec.pages[i]);
525 			trace_afs_folio_dirty(vnode, tracepoint_string("store+"), folio);
526 
527 			if (!folio_clear_dirty_for_io(folio))
528 				BUG();
529 			if (folio_start_writeback(folio))
530 				BUG();
531 			afs_folio_start_fscache(caching, folio);
532 
533 			*_count -= folio_nr_pages(folio);
534 			folio_unlock(folio);
535 		}
536 
537 		pagevec_release(&pvec);
538 		cond_resched();
539 	} while (!stop);
540 
541 	*_len = len;
542 }
543 
544 /*
545  * Synchronously write back the locked page and any subsequent non-locked dirty
546  * pages.
547  */
548 static ssize_t afs_write_back_from_locked_folio(struct address_space *mapping,
549 						struct writeback_control *wbc,
550 						struct folio *folio,
551 						loff_t start, loff_t end)
552 {
553 	struct afs_vnode *vnode = AFS_FS_I(mapping->host);
554 	struct iov_iter iter;
555 	unsigned long priv;
556 	unsigned int offset, to, len, max_len;
557 	loff_t i_size = i_size_read(&vnode->vfs_inode);
558 	bool new_content = test_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags);
559 	bool caching = fscache_cookie_enabled(afs_vnode_cache(vnode));
560 	long count = wbc->nr_to_write;
561 	int ret;
562 
563 	_enter(",%lx,%llx-%llx", folio_index(folio), start, end);
564 
565 	if (folio_start_writeback(folio))
566 		BUG();
567 	afs_folio_start_fscache(caching, folio);
568 
569 	count -= folio_nr_pages(folio);
570 
571 	/* Find all consecutive lockable dirty pages that have contiguous
572 	 * written regions, stopping when we find a page that is not
573 	 * immediately lockable, is not dirty or is missing, or we reach the
574 	 * end of the range.
575 	 */
576 	priv = (unsigned long)folio_get_private(folio);
577 	offset = afs_folio_dirty_from(folio, priv);
578 	to = afs_folio_dirty_to(folio, priv);
579 	trace_afs_folio_dirty(vnode, tracepoint_string("store"), folio);
580 
581 	len = to - offset;
582 	start += offset;
583 	if (start < i_size) {
584 		/* Trim the write to the EOF; the extra data is ignored.  Also
585 		 * put an upper limit on the size of a single storedata op.
586 		 */
587 		max_len = 65536 * 4096;
588 		max_len = min_t(unsigned long long, max_len, end - start + 1);
589 		max_len = min_t(unsigned long long, max_len, i_size - start);
590 
591 		if (len < max_len &&
592 		    (to == folio_size(folio) || new_content))
593 			afs_extend_writeback(mapping, vnode, &count,
594 					     start, max_len, new_content,
595 					     caching, &len);
596 		len = min_t(loff_t, len, max_len);
597 	}
598 
599 	/* We now have a contiguous set of dirty pages, each with writeback
600 	 * set; the first page is still locked at this point, but all the rest
601 	 * have been unlocked.
602 	 */
603 	folio_unlock(folio);
604 
605 	if (start < i_size) {
606 		_debug("write back %x @%llx [%llx]", len, start, i_size);
607 
608 		/* Speculatively write to the cache.  We have to fix this up
609 		 * later if the store fails.
610 		 */
611 		afs_write_to_cache(vnode, start, len, i_size, caching);
612 
613 		iov_iter_xarray(&iter, WRITE, &mapping->i_pages, start, len);
614 		ret = afs_store_data(vnode, &iter, start, false);
615 	} else {
616 		_debug("write discard %x @%llx [%llx]", len, start, i_size);
617 
618 		/* The dirty region was entirely beyond the EOF. */
619 		fscache_clear_page_bits(afs_vnode_cache(vnode),
620 					mapping, start, len, caching);
621 		afs_pages_written_back(vnode, start, len);
622 		ret = 0;
623 	}
624 
625 	switch (ret) {
626 	case 0:
627 		wbc->nr_to_write = count;
628 		ret = len;
629 		break;
630 
631 	default:
632 		pr_notice("kAFS: Unexpected error from FS.StoreData %d\n", ret);
633 		fallthrough;
634 	case -EACCES:
635 	case -EPERM:
636 	case -ENOKEY:
637 	case -EKEYEXPIRED:
638 	case -EKEYREJECTED:
639 	case -EKEYREVOKED:
640 		afs_redirty_pages(wbc, mapping, start, len);
641 		mapping_set_error(mapping, ret);
642 		break;
643 
644 	case -EDQUOT:
645 	case -ENOSPC:
646 		afs_redirty_pages(wbc, mapping, start, len);
647 		mapping_set_error(mapping, -ENOSPC);
648 		break;
649 
650 	case -EROFS:
651 	case -EIO:
652 	case -EREMOTEIO:
653 	case -EFBIG:
654 	case -ENOENT:
655 	case -ENOMEDIUM:
656 	case -ENXIO:
657 		trace_afs_file_error(vnode, ret, afs_file_error_writeback_fail);
658 		afs_kill_pages(mapping, start, len);
659 		mapping_set_error(mapping, ret);
660 		break;
661 	}
662 
663 	_leave(" = %d", ret);
664 	return ret;
665 }
666 
667 /*
668  * write a page back to the server
669  * - the caller locked the page for us
670  */
671 int afs_writepage(struct page *subpage, struct writeback_control *wbc)
672 {
673 	struct folio *folio = page_folio(subpage);
674 	ssize_t ret;
675 	loff_t start;
676 
677 	_enter("{%lx},", folio_index(folio));
678 
679 #ifdef CONFIG_AFS_FSCACHE
680 	folio_wait_fscache(folio);
681 #endif
682 
683 	start = folio_index(folio) * PAGE_SIZE;
684 	ret = afs_write_back_from_locked_folio(folio_mapping(folio), wbc,
685 					       folio, start, LLONG_MAX - start);
686 	if (ret < 0) {
687 		_leave(" = %zd", ret);
688 		return ret;
689 	}
690 
691 	_leave(" = 0");
692 	return 0;
693 }
694 
695 /*
696  * write a region of pages back to the server
697  */
698 static int afs_writepages_region(struct address_space *mapping,
699 				 struct writeback_control *wbc,
700 				 loff_t start, loff_t end, loff_t *_next)
701 {
702 	struct folio *folio;
703 	struct page *head_page;
704 	ssize_t ret;
705 	int n, skips = 0;
706 
707 	_enter("%llx,%llx,", start, end);
708 
709 	do {
710 		pgoff_t index = start / PAGE_SIZE;
711 
712 		n = find_get_pages_range_tag(mapping, &index, end / PAGE_SIZE,
713 					     PAGECACHE_TAG_DIRTY, 1, &head_page);
714 		if (!n)
715 			break;
716 
717 		folio = page_folio(head_page);
718 		start = folio_pos(folio); /* May regress with THPs */
719 
720 		_debug("wback %lx", folio_index(folio));
721 
722 		/* At this point we hold neither the i_pages lock nor the
723 		 * page lock: the page may be truncated or invalidated
724 		 * (changing page->mapping to NULL), or even swizzled
725 		 * back from swapper_space to tmpfs file mapping
726 		 */
727 		if (wbc->sync_mode != WB_SYNC_NONE) {
728 			ret = folio_lock_killable(folio);
729 			if (ret < 0) {
730 				folio_put(folio);
731 				return ret;
732 			}
733 		} else {
734 			if (!folio_trylock(folio)) {
735 				folio_put(folio);
736 				return 0;
737 			}
738 		}
739 
740 		if (folio_mapping(folio) != mapping ||
741 		    !folio_test_dirty(folio)) {
742 			start += folio_size(folio);
743 			folio_unlock(folio);
744 			folio_put(folio);
745 			continue;
746 		}
747 
748 		if (folio_test_writeback(folio) ||
749 		    folio_test_fscache(folio)) {
750 			folio_unlock(folio);
751 			if (wbc->sync_mode != WB_SYNC_NONE) {
752 				folio_wait_writeback(folio);
753 #ifdef CONFIG_AFS_FSCACHE
754 				folio_wait_fscache(folio);
755 #endif
756 			} else {
757 				start += folio_size(folio);
758 			}
759 			folio_put(folio);
760 			if (wbc->sync_mode == WB_SYNC_NONE) {
761 				if (skips >= 5 || need_resched())
762 					break;
763 				skips++;
764 			}
765 			continue;
766 		}
767 
768 		if (!folio_clear_dirty_for_io(folio))
769 			BUG();
770 		ret = afs_write_back_from_locked_folio(mapping, wbc, folio, start, end);
771 		folio_put(folio);
772 		if (ret < 0) {
773 			_leave(" = %zd", ret);
774 			return ret;
775 		}
776 
777 		start += ret;
778 
779 		cond_resched();
780 	} while (wbc->nr_to_write > 0);
781 
782 	*_next = start;
783 	_leave(" = 0 [%llx]", *_next);
784 	return 0;
785 }
786 
787 /*
788  * write some of the pending data back to the server
789  */
790 int afs_writepages(struct address_space *mapping,
791 		   struct writeback_control *wbc)
792 {
793 	struct afs_vnode *vnode = AFS_FS_I(mapping->host);
794 	loff_t start, next;
795 	int ret;
796 
797 	_enter("");
798 
799 	/* We have to be careful as we can end up racing with setattr()
800 	 * truncating the pagecache since the caller doesn't take a lock here
801 	 * to prevent it.
802 	 */
803 	if (wbc->sync_mode == WB_SYNC_ALL)
804 		down_read(&vnode->validate_lock);
805 	else if (!down_read_trylock(&vnode->validate_lock))
806 		return 0;
807 
808 	if (wbc->range_cyclic) {
809 		start = mapping->writeback_index * PAGE_SIZE;
810 		ret = afs_writepages_region(mapping, wbc, start, LLONG_MAX, &next);
811 		if (ret == 0) {
812 			mapping->writeback_index = next / PAGE_SIZE;
813 			if (start > 0 && wbc->nr_to_write > 0) {
814 				ret = afs_writepages_region(mapping, wbc, 0,
815 							    start, &next);
816 				if (ret == 0)
817 					mapping->writeback_index =
818 						next / PAGE_SIZE;
819 			}
820 		}
821 	} else if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) {
822 		ret = afs_writepages_region(mapping, wbc, 0, LLONG_MAX, &next);
823 		if (wbc->nr_to_write > 0 && ret == 0)
824 			mapping->writeback_index = next / PAGE_SIZE;
825 	} else {
826 		ret = afs_writepages_region(mapping, wbc,
827 					    wbc->range_start, wbc->range_end, &next);
828 	}
829 
830 	up_read(&vnode->validate_lock);
831 	_leave(" = %d", ret);
832 	return ret;
833 }
834 
835 /*
836  * write to an AFS file
837  */
838 ssize_t afs_file_write(struct kiocb *iocb, struct iov_iter *from)
839 {
840 	struct afs_vnode *vnode = AFS_FS_I(file_inode(iocb->ki_filp));
841 	struct afs_file *af = iocb->ki_filp->private_data;
842 	ssize_t result;
843 	size_t count = iov_iter_count(from);
844 
845 	_enter("{%llx:%llu},{%zu},",
846 	       vnode->fid.vid, vnode->fid.vnode, count);
847 
848 	if (IS_SWAPFILE(&vnode->vfs_inode)) {
849 		printk(KERN_INFO
850 		       "AFS: Attempt to write to active swap file!\n");
851 		return -EBUSY;
852 	}
853 
854 	if (!count)
855 		return 0;
856 
857 	result = afs_validate(vnode, af->key);
858 	if (result < 0)
859 		return result;
860 
861 	result = generic_file_write_iter(iocb, from);
862 
863 	_leave(" = %zd", result);
864 	return result;
865 }
866 
867 /*
868  * flush any dirty pages for this process, and check for write errors.
869  * - the return status from this call provides a reliable indication of
870  *   whether any write errors occurred for this process.
871  */
872 int afs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
873 {
874 	struct afs_vnode *vnode = AFS_FS_I(file_inode(file));
875 	struct afs_file *af = file->private_data;
876 	int ret;
877 
878 	_enter("{%llx:%llu},{n=%pD},%d",
879 	       vnode->fid.vid, vnode->fid.vnode, file,
880 	       datasync);
881 
882 	ret = afs_validate(vnode, af->key);
883 	if (ret < 0)
884 		return ret;
885 
886 	return file_write_and_wait_range(file, start, end);
887 }
888 
889 /*
890  * notification that a previously read-only page is about to become writable
891  * - if it returns an error, the caller will deliver a bus error signal
892  */
893 vm_fault_t afs_page_mkwrite(struct vm_fault *vmf)
894 {
895 	struct folio *folio = page_folio(vmf->page);
896 	struct file *file = vmf->vma->vm_file;
897 	struct inode *inode = file_inode(file);
898 	struct afs_vnode *vnode = AFS_FS_I(inode);
899 	struct afs_file *af = file->private_data;
900 	unsigned long priv;
901 	vm_fault_t ret = VM_FAULT_RETRY;
902 
903 	_enter("{{%llx:%llu}},{%lx}", vnode->fid.vid, vnode->fid.vnode, folio_index(folio));
904 
905 	afs_validate(vnode, af->key);
906 
907 	sb_start_pagefault(inode->i_sb);
908 
909 	/* Wait for the page to be written to the cache before we allow it to
910 	 * be modified.  We then assume the entire page will need writing back.
911 	 */
912 #ifdef CONFIG_AFS_FSCACHE
913 	if (folio_test_fscache(folio) &&
914 	    folio_wait_fscache_killable(folio) < 0)
915 		goto out;
916 #endif
917 
918 	if (folio_wait_writeback_killable(folio))
919 		goto out;
920 
921 	if (folio_lock_killable(folio) < 0)
922 		goto out;
923 
924 	/* We mustn't change folio->private until writeback is complete as that
925 	 * details the portion of the page we need to write back and we might
926 	 * need to redirty the page if there's a problem.
927 	 */
928 	if (folio_wait_writeback_killable(folio) < 0) {
929 		folio_unlock(folio);
930 		goto out;
931 	}
932 
933 	priv = afs_folio_dirty(folio, 0, folio_size(folio));
934 	priv = afs_folio_dirty_mmapped(priv);
935 	if (folio_test_private(folio)) {
936 		folio_change_private(folio, (void *)priv);
937 		trace_afs_folio_dirty(vnode, tracepoint_string("mkwrite+"), folio);
938 	} else {
939 		folio_attach_private(folio, (void *)priv);
940 		trace_afs_folio_dirty(vnode, tracepoint_string("mkwrite"), folio);
941 	}
942 	file_update_time(file);
943 
944 	ret = VM_FAULT_LOCKED;
945 out:
946 	sb_end_pagefault(inode->i_sb);
947 	return ret;
948 }
949 
950 /*
951  * Prune the keys cached for writeback.  The caller must hold vnode->wb_lock.
952  */
953 void afs_prune_wb_keys(struct afs_vnode *vnode)
954 {
955 	LIST_HEAD(graveyard);
956 	struct afs_wb_key *wbk, *tmp;
957 
958 	/* Discard unused keys */
959 	spin_lock(&vnode->wb_lock);
960 
961 	if (!mapping_tagged(&vnode->vfs_inode.i_data, PAGECACHE_TAG_WRITEBACK) &&
962 	    !mapping_tagged(&vnode->vfs_inode.i_data, PAGECACHE_TAG_DIRTY)) {
963 		list_for_each_entry_safe(wbk, tmp, &vnode->wb_keys, vnode_link) {
964 			if (refcount_read(&wbk->usage) == 1)
965 				list_move(&wbk->vnode_link, &graveyard);
966 		}
967 	}
968 
969 	spin_unlock(&vnode->wb_lock);
970 
971 	while (!list_empty(&graveyard)) {
972 		wbk = list_entry(graveyard.next, struct afs_wb_key, vnode_link);
973 		list_del(&wbk->vnode_link);
974 		afs_put_wb_key(wbk);
975 	}
976 }
977 
978 /*
979  * Clean up a page during invalidation.
980  */
981 int afs_launder_folio(struct folio *folio)
982 {
983 	struct afs_vnode *vnode = AFS_FS_I(folio_inode(folio));
984 	struct iov_iter iter;
985 	struct bio_vec bv[1];
986 	unsigned long priv;
987 	unsigned int f, t;
988 	int ret = 0;
989 
990 	_enter("{%lx}", folio->index);
991 
992 	priv = (unsigned long)folio_get_private(folio);
993 	if (folio_clear_dirty_for_io(folio)) {
994 		f = 0;
995 		t = folio_size(folio);
996 		if (folio_test_private(folio)) {
997 			f = afs_folio_dirty_from(folio, priv);
998 			t = afs_folio_dirty_to(folio, priv);
999 		}
1000 
1001 		bv[0].bv_page = &folio->page;
1002 		bv[0].bv_offset = f;
1003 		bv[0].bv_len = t - f;
1004 		iov_iter_bvec(&iter, WRITE, bv, 1, bv[0].bv_len);
1005 
1006 		trace_afs_folio_dirty(vnode, tracepoint_string("launder"), folio);
1007 		ret = afs_store_data(vnode, &iter, folio_pos(folio) + f, true);
1008 	}
1009 
1010 	trace_afs_folio_dirty(vnode, tracepoint_string("laundered"), folio);
1011 	folio_detach_private(folio);
1012 	folio_wait_fscache(folio);
1013 	return ret;
1014 }
1015 
1016 /*
1017  * Deal with the completion of writing the data to the cache.
1018  */
1019 static void afs_write_to_cache_done(void *priv, ssize_t transferred_or_error,
1020 				    bool was_async)
1021 {
1022 	struct afs_vnode *vnode = priv;
1023 
1024 	if (IS_ERR_VALUE(transferred_or_error) &&
1025 	    transferred_or_error != -ENOBUFS)
1026 		afs_invalidate_cache(vnode, 0);
1027 }
1028 
1029 /*
1030  * Save the write to the cache also.
1031  */
1032 static void afs_write_to_cache(struct afs_vnode *vnode,
1033 			       loff_t start, size_t len, loff_t i_size,
1034 			       bool caching)
1035 {
1036 	fscache_write_to_cache(afs_vnode_cache(vnode),
1037 			       vnode->vfs_inode.i_mapping, start, len, i_size,
1038 			       afs_write_to_cache_done, vnode, caching);
1039 }
1040