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