1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Network filesystem high-level write support.
4 * Copyright (C) 2023 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells (dhowells@redhat.com)
8 #include <linux/export.h>
11 #include <linux/pagemap.h>
12 #include <linux/slab.h>
13 #include <linux/pagevec.h>
17 * Determined write method. Adjust netfs_folio_traces if this is changed.
19 enum netfs_how_to_modify {
20 NETFS_FOLIO_IS_UPTODATE, /* Folio is uptodate already */
21 NETFS_JUST_PREFETCH, /* We have to read the folio anyway */
22 NETFS_WHOLE_FOLIO_MODIFY, /* We're going to overwrite the whole folio */
23 NETFS_MODIFY_AND_CLEAR, /* We can assume there is no data to be downloaded. */
24 NETFS_STREAMING_WRITE, /* Store incomplete data in non-uptodate page. */
25 NETFS_STREAMING_WRITE_CONT, /* Continue streaming write. */
26 NETFS_FLUSH_CONTENT, /* Flush incompatible content. */
29 static void netfs_cleanup_buffered_write(struct netfs_io_request *wreq);
31 static void netfs_set_group(struct folio *folio, struct netfs_group *netfs_group)
33 if (netfs_group && !folio_get_private(folio))
34 folio_attach_private(folio, netfs_get_group(netfs_group));
37 #if IS_ENABLED(CONFIG_FSCACHE)
38 static void netfs_folio_start_fscache(bool caching, struct folio *folio)
41 folio_start_fscache(folio);
44 static void netfs_folio_start_fscache(bool caching, struct folio *folio)
50 * Decide how we should modify a folio. We might be attempting to do
51 * write-streaming, in which case we don't want to a local RMW cycle if we can
52 * avoid it. If we're doing local caching or content crypto, we award that
53 * priority over avoiding RMW. If the file is open readably, then we also
54 * assume that we may want to read what we wrote.
56 static enum netfs_how_to_modify netfs_how_to_modify(struct netfs_inode *ctx,
65 struct netfs_folio *finfo = netfs_folio_info(folio);
66 loff_t pos = folio_file_pos(folio);
70 if (netfs_folio_group(folio) != netfs_group)
71 return NETFS_FLUSH_CONTENT;
73 if (folio_test_uptodate(folio))
74 return NETFS_FOLIO_IS_UPTODATE;
76 if (pos >= ctx->zero_point)
77 return NETFS_MODIFY_AND_CLEAR;
79 if (!maybe_trouble && offset == 0 && len >= flen)
80 return NETFS_WHOLE_FOLIO_MODIFY;
82 if (file->f_mode & FMODE_READ)
83 goto no_write_streaming;
84 if (test_bit(NETFS_ICTX_NO_WRITE_STREAMING, &ctx->flags))
85 goto no_write_streaming;
87 if (netfs_is_cache_enabled(ctx)) {
88 /* We don't want to get a streaming write on a file that loses
89 * caching service temporarily because the backing store got
92 if (!test_bit(NETFS_ICTX_NO_WRITE_STREAMING, &ctx->flags))
93 set_bit(NETFS_ICTX_NO_WRITE_STREAMING, &ctx->flags);
94 goto no_write_streaming;
98 return NETFS_STREAMING_WRITE;
100 /* We can continue a streaming write only if it continues on from the
101 * previous. If it overlaps, we must flush lest we suffer a partial
102 * copy and disjoint dirty regions.
104 if (offset == finfo->dirty_offset + finfo->dirty_len)
105 return NETFS_STREAMING_WRITE_CONT;
106 return NETFS_FLUSH_CONTENT;
110 netfs_stat(&netfs_n_wh_wstream_conflict);
111 return NETFS_FLUSH_CONTENT;
113 return NETFS_JUST_PREFETCH;
117 * Grab a folio for writing and lock it. Attempt to allocate as large a folio
118 * as possible to hold as much of the remaining length as possible in one go.
120 static struct folio *netfs_grab_folio_for_write(struct address_space *mapping,
121 loff_t pos, size_t part)
123 pgoff_t index = pos / PAGE_SIZE;
124 fgf_t fgp_flags = FGP_WRITEBEGIN;
126 if (mapping_large_folio_support(mapping))
127 fgp_flags |= fgf_set_order(pos % PAGE_SIZE + part);
129 return __filemap_get_folio(mapping, index, fgp_flags,
130 mapping_gfp_mask(mapping));
134 * netfs_perform_write - Copy data into the pagecache.
135 * @iocb: The operation parameters
136 * @iter: The source buffer
137 * @netfs_group: Grouping for dirty pages (eg. ceph snaps).
139 * Copy data into pagecache pages attached to the inode specified by @iocb.
140 * The caller must hold appropriate inode locks.
142 * Dirty pages are tagged with a netfs_folio struct if they're not up to date
143 * to indicate the range modified. Dirty pages may also be tagged with a
144 * netfs-specific grouping such that data from an old group gets flushed before
145 * a new one is started.
147 ssize_t netfs_perform_write(struct kiocb *iocb, struct iov_iter *iter,
148 struct netfs_group *netfs_group)
150 struct file *file = iocb->ki_filp;
151 struct inode *inode = file_inode(file);
152 struct address_space *mapping = inode->i_mapping;
153 struct netfs_inode *ctx = netfs_inode(inode);
154 struct writeback_control wbc = {
155 .sync_mode = WB_SYNC_NONE,
157 .nr_to_write = LONG_MAX,
158 .range_start = iocb->ki_pos,
159 .range_end = iocb->ki_pos + iter->count,
161 struct netfs_io_request *wreq = NULL;
162 struct netfs_folio *finfo;
164 enum netfs_how_to_modify howto;
165 enum netfs_folio_trace trace;
166 unsigned int bdp_flags = (iocb->ki_flags & IOCB_SYNC) ? 0: BDP_ASYNC;
167 ssize_t written = 0, ret;
168 loff_t i_size, pos = iocb->ki_pos, from, to;
169 size_t max_chunk = PAGE_SIZE << MAX_PAGECACHE_ORDER;
170 bool maybe_trouble = false;
172 if (unlikely(test_bit(NETFS_ICTX_WRITETHROUGH, &ctx->flags) ||
173 iocb->ki_flags & (IOCB_DSYNC | IOCB_SYNC))
175 if (pos < i_size_read(inode)) {
176 ret = filemap_write_and_wait_range(mapping, pos, pos + iter->count);
182 wbc_attach_fdatawrite_inode(&wbc, mapping->host);
184 wreq = netfs_begin_writethrough(iocb, iter->count);
186 wbc_detach_inode(&wbc);
191 if (!is_sync_kiocb(iocb))
193 wreq->cleanup = netfs_cleanup_buffered_write;
198 size_t offset; /* Offset into pagecache folio */
199 size_t part; /* Bytes to write to folio */
200 size_t copied; /* Bytes copied from user */
202 ret = balance_dirty_pages_ratelimited_flags(mapping, bdp_flags);
203 if (unlikely(ret < 0))
206 offset = pos & (max_chunk - 1);
207 part = min(max_chunk - offset, iov_iter_count(iter));
209 /* Bring in the user pages that we will copy from _first_ lest
210 * we hit a nasty deadlock on copying from the same page as
211 * we're writing to, without it being marked uptodate.
213 * Not only is this an optimisation, but it is also required to
214 * check that the address is actually valid, when atomic
215 * usercopies are used below.
217 * We rely on the page being held onto long enough by the LRU
218 * that we can grab it below if this causes it to be read.
221 if (unlikely(fault_in_iov_iter_readable(iter, part) == part))
225 folio = netfs_grab_folio_for_write(mapping, pos, part);
229 flen = folio_size(folio);
230 offset = pos & (flen - 1);
231 part = min_t(size_t, flen - offset, part);
233 if (signal_pending(current)) {
234 ret = written ? -EINTR : -ERESTARTSYS;
235 goto error_folio_unlock;
238 /* See if we need to prefetch the area we're going to modify.
239 * We need to do this before we get a lock on the folio in case
240 * there's more than one writer competing for the same cache
243 howto = netfs_how_to_modify(ctx, file, folio, netfs_group,
244 flen, offset, part, maybe_trouble);
245 _debug("howto %u", howto);
247 case NETFS_JUST_PREFETCH:
248 ret = netfs_prefetch_for_write(file, folio, offset, part);
250 _debug("prefetch = %zd", ret);
251 goto error_folio_unlock;
254 case NETFS_FOLIO_IS_UPTODATE:
255 case NETFS_WHOLE_FOLIO_MODIFY:
256 case NETFS_STREAMING_WRITE_CONT:
258 case NETFS_MODIFY_AND_CLEAR:
259 zero_user_segment(&folio->page, 0, offset);
261 case NETFS_STREAMING_WRITE:
263 if (WARN_ON(folio_get_private(folio)))
264 goto error_folio_unlock;
266 case NETFS_FLUSH_CONTENT:
267 trace_netfs_folio(folio, netfs_flush_content);
268 from = folio_pos(folio);
269 to = from + folio_size(folio) - 1;
272 ret = filemap_write_and_wait_range(mapping, from, to);
274 goto error_folio_unlock;
278 if (mapping_writably_mapped(mapping))
279 flush_dcache_folio(folio);
281 copied = copy_folio_from_iter_atomic(folio, offset, part, iter);
283 flush_dcache_folio(folio);
285 /* Deal with a (partially) failed copy */
288 goto error_folio_unlock;
291 trace = (enum netfs_folio_trace)howto;
293 case NETFS_FOLIO_IS_UPTODATE:
294 case NETFS_JUST_PREFETCH:
295 netfs_set_group(folio, netfs_group);
297 case NETFS_MODIFY_AND_CLEAR:
298 zero_user_segment(&folio->page, offset + copied, flen);
299 netfs_set_group(folio, netfs_group);
300 folio_mark_uptodate(folio);
302 case NETFS_WHOLE_FOLIO_MODIFY:
303 if (unlikely(copied < part)) {
304 maybe_trouble = true;
305 iov_iter_revert(iter, copied);
309 netfs_set_group(folio, netfs_group);
310 folio_mark_uptodate(folio);
312 case NETFS_STREAMING_WRITE:
313 if (offset == 0 && copied == flen) {
314 netfs_set_group(folio, netfs_group);
315 folio_mark_uptodate(folio);
316 trace = netfs_streaming_filled_page;
319 finfo = kzalloc(sizeof(*finfo), GFP_KERNEL);
321 iov_iter_revert(iter, copied);
323 goto error_folio_unlock;
325 finfo->netfs_group = netfs_get_group(netfs_group);
326 finfo->dirty_offset = offset;
327 finfo->dirty_len = copied;
328 folio_attach_private(folio, (void *)((unsigned long)finfo |
331 case NETFS_STREAMING_WRITE_CONT:
332 finfo = netfs_folio_info(folio);
333 finfo->dirty_len += copied;
334 if (finfo->dirty_offset == 0 && finfo->dirty_len == flen) {
335 if (finfo->netfs_group)
336 folio_change_private(folio, finfo->netfs_group);
338 folio_detach_private(folio);
339 folio_mark_uptodate(folio);
341 trace = netfs_streaming_cont_filled_page;
345 WARN(true, "Unexpected modify type %u ix=%lx\n",
346 howto, folio_index(folio));
348 goto error_folio_unlock;
351 trace_netfs_folio(folio, trace);
353 /* Update the inode size if we moved the EOF marker */
354 i_size = i_size_read(inode);
357 if (ctx->ops->update_i_size) {
358 ctx->ops->update_i_size(inode, pos);
360 i_size_write(inode, pos);
361 #if IS_ENABLED(CONFIG_FSCACHE)
362 fscache_update_cookie(ctx->cache, NULL, &pos);
369 folio_mark_dirty(folio);
371 if (folio_test_dirty(folio))
373 folio_clear_dirty_for_io(folio);
374 /* We make multiple writes to the folio... */
375 if (!folio_test_writeback(folio)) {
376 folio_wait_fscache(folio);
377 folio_start_writeback(folio);
378 folio_start_fscache(folio);
379 if (wreq->iter.count == 0)
380 trace_netfs_folio(folio, netfs_folio_trace_wthru);
382 trace_netfs_folio(folio, netfs_folio_trace_wthru_plus);
384 netfs_advance_writethrough(wreq, copied,
385 offset + copied == flen);
393 } while (iov_iter_count(iter));
396 if (unlikely(wreq)) {
397 ret = netfs_end_writethrough(wreq, iocb);
398 wbc_detach_inode(&wbc);
399 if (ret == -EIOCBQUEUED)
403 iocb->ki_pos += written;
404 _leave(" = %zd [%zd]", written, ret);
405 return written ? written : ret;
412 EXPORT_SYMBOL(netfs_perform_write);
415 * netfs_buffered_write_iter_locked - write data to a file
416 * @iocb: IO state structure (file, offset, etc.)
417 * @from: iov_iter with data to write
418 * @netfs_group: Grouping for dirty pages (eg. ceph snaps).
420 * This function does all the work needed for actually writing data to a
421 * file. It does all basic checks, removes SUID from the file, updates
422 * modification times and calls proper subroutines depending on whether we
423 * do direct IO or a standard buffered write.
425 * The caller must hold appropriate locks around this function and have called
426 * generic_write_checks() already. The caller is also responsible for doing
427 * any necessary syncing afterwards.
429 * This function does *not* take care of syncing data in case of O_SYNC write.
430 * A caller has to handle it. This is mainly due to the fact that we want to
431 * avoid syncing under i_rwsem.
434 * * number of bytes written, even for truncated writes
435 * * negative error code if no data has been written at all
437 ssize_t netfs_buffered_write_iter_locked(struct kiocb *iocb, struct iov_iter *from,
438 struct netfs_group *netfs_group)
440 struct file *file = iocb->ki_filp;
443 trace_netfs_write_iter(iocb, from);
445 ret = file_remove_privs(file);
449 ret = file_update_time(file);
453 return netfs_perform_write(iocb, from, netfs_group);
455 EXPORT_SYMBOL(netfs_buffered_write_iter_locked);
458 * netfs_file_write_iter - write data to a file
459 * @iocb: IO state structure
460 * @from: iov_iter with data to write
462 * Perform a write to a file, writing into the pagecache if possible and doing
463 * an unbuffered write instead if not.
466 * * Negative error code if no data has been written at all of
467 * vfs_fsync_range() failed for a synchronous write
468 * * Number of bytes written, even for truncated writes
470 ssize_t netfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
472 struct file *file = iocb->ki_filp;
473 struct inode *inode = file->f_mapping->host;
474 struct netfs_inode *ictx = netfs_inode(inode);
477 _enter("%llx,%zx,%llx", iocb->ki_pos, iov_iter_count(from), i_size_read(inode));
479 if ((iocb->ki_flags & IOCB_DIRECT) ||
480 test_bit(NETFS_ICTX_UNBUFFERED, &ictx->flags))
481 return netfs_unbuffered_write_iter(iocb, from);
483 ret = netfs_start_io_write(inode);
487 ret = generic_write_checks(iocb, from);
489 ret = netfs_buffered_write_iter_locked(iocb, from, NULL);
490 netfs_end_io_write(inode);
492 ret = generic_write_sync(iocb, ret);
495 EXPORT_SYMBOL(netfs_file_write_iter);
498 * Notification that a previously read-only page is about to become writable.
499 * Note that the caller indicates a single page of a multipage folio.
501 vm_fault_t netfs_page_mkwrite(struct vm_fault *vmf, struct netfs_group *netfs_group)
503 struct folio *folio = page_folio(vmf->page);
504 struct file *file = vmf->vma->vm_file;
505 struct inode *inode = file_inode(file);
506 vm_fault_t ret = VM_FAULT_RETRY;
509 _enter("%lx", folio->index);
511 sb_start_pagefault(inode->i_sb);
513 if (folio_wait_writeback_killable(folio))
516 if (folio_lock_killable(folio) < 0)
519 /* Can we see a streaming write here? */
520 if (WARN_ON(!folio_test_uptodate(folio))) {
521 ret = VM_FAULT_SIGBUS | VM_FAULT_LOCKED;
525 if (netfs_folio_group(folio) != netfs_group) {
527 err = filemap_fdatawait_range(inode->i_mapping,
529 folio_pos(folio) + folio_size(folio));
532 ret = VM_FAULT_RETRY;
538 ret = VM_FAULT_SIGBUS;
543 if (folio_test_dirty(folio))
544 trace_netfs_folio(folio, netfs_folio_trace_mkwrite_plus);
546 trace_netfs_folio(folio, netfs_folio_trace_mkwrite);
547 netfs_set_group(folio, netfs_group);
548 file_update_time(file);
549 ret = VM_FAULT_LOCKED;
551 sb_end_pagefault(inode->i_sb);
554 EXPORT_SYMBOL(netfs_page_mkwrite);
557 * Kill all the pages in the given range
559 static void netfs_kill_pages(struct address_space *mapping,
560 loff_t start, loff_t len)
563 pgoff_t index = start / PAGE_SIZE;
564 pgoff_t last = (start + len - 1) / PAGE_SIZE, next;
566 _enter("%llx-%llx", start, start + len - 1);
569 _debug("kill %lx (to %lx)", index, last);
571 folio = filemap_get_folio(mapping, index);
577 next = folio_next_index(folio);
579 trace_netfs_folio(folio, netfs_folio_trace_kill);
580 folio_clear_uptodate(folio);
581 if (folio_test_fscache(folio))
582 folio_end_fscache(folio);
583 folio_end_writeback(folio);
585 generic_error_remove_folio(mapping, folio);
589 } while (index = next, index <= last);
595 * Redirty all the pages in a given range.
597 static void netfs_redirty_pages(struct address_space *mapping,
598 loff_t start, loff_t len)
601 pgoff_t index = start / PAGE_SIZE;
602 pgoff_t last = (start + len - 1) / PAGE_SIZE, next;
604 _enter("%llx-%llx", start, start + len - 1);
607 _debug("redirty %llx @%llx", len, start);
609 folio = filemap_get_folio(mapping, index);
615 next = folio_next_index(folio);
616 trace_netfs_folio(folio, netfs_folio_trace_redirty);
617 filemap_dirty_folio(mapping, folio);
618 if (folio_test_fscache(folio))
619 folio_end_fscache(folio);
620 folio_end_writeback(folio);
622 } while (index = next, index <= last);
624 balance_dirty_pages_ratelimited(mapping);
630 * Completion of write to server
632 static void netfs_pages_written_back(struct netfs_io_request *wreq)
634 struct address_space *mapping = wreq->mapping;
635 struct netfs_folio *finfo;
636 struct netfs_group *group = NULL;
641 XA_STATE(xas, &mapping->i_pages, wreq->start / PAGE_SIZE);
643 _enter("%llx-%llx", wreq->start, wreq->start + wreq->len);
647 last = (wreq->start + wreq->len - 1) / PAGE_SIZE;
648 xas_for_each(&xas, folio, last) {
649 WARN(!folio_test_writeback(folio),
650 "bad %zx @%llx page %lx %lx\n",
651 wreq->len, wreq->start, folio_index(folio), last);
653 if ((finfo = netfs_folio_info(folio))) {
654 /* Streaming writes cannot be redirtied whilst under
655 * writeback, so discard the streaming record.
657 folio_detach_private(folio);
658 group = finfo->netfs_group;
660 trace_netfs_folio(folio, netfs_folio_trace_clear_s);
662 } else if ((group = netfs_folio_group(folio))) {
663 /* Need to detach the group pointer if the page didn't
664 * get redirtied. If it has been redirtied, then it
665 * must be within the same group.
667 if (folio_test_dirty(folio)) {
668 trace_netfs_folio(folio, netfs_folio_trace_redirtied);
671 if (folio_trylock(folio)) {
672 if (!folio_test_dirty(folio)) {
673 folio_detach_private(folio);
675 trace_netfs_folio(folio, netfs_folio_trace_clear_g);
677 trace_netfs_folio(folio, netfs_folio_trace_redirtied);
686 if (!folio_test_dirty(folio)) {
687 folio_detach_private(folio);
689 trace_netfs_folio(folio, netfs_folio_trace_clear_g);
691 trace_netfs_folio(folio, netfs_folio_trace_redirtied);
696 trace_netfs_folio(folio, netfs_folio_trace_clear);
699 if (folio_test_fscache(folio))
700 folio_end_fscache(folio);
701 xas_advance(&xas, folio_next_index(folio) - 1);
702 folio_end_writeback(folio);
706 netfs_put_group_many(group, gcount);
711 * Deal with the disposition of the folios that are under writeback to close
714 static void netfs_cleanup_buffered_write(struct netfs_io_request *wreq)
716 struct address_space *mapping = wreq->mapping;
720 switch (wreq->error) {
722 netfs_pages_written_back(wreq);
726 pr_notice("R=%08x Unexpected error %d\n", wreq->debug_id, wreq->error);
737 netfs_redirty_pages(mapping, wreq->start, wreq->len);
747 netfs_kill_pages(mapping, wreq->start, wreq->len);
752 mapping_set_error(mapping, wreq->error);
753 if (wreq->netfs_ops->done)
754 wreq->netfs_ops->done(wreq);
758 * Extend the region to be written back to include subsequent contiguously
759 * dirty pages if possible, but don't sleep while doing so.
761 * If this page holds new content, then we can include filler zeros in the
764 static void netfs_extend_writeback(struct address_space *mapping,
765 struct netfs_group *group,
766 struct xa_state *xas,
774 struct netfs_folio *finfo;
775 struct folio_batch fbatch;
778 pgoff_t index = (start + *_len) / PAGE_SIZE;
783 folio_batch_init(&fbatch);
786 /* Firstly, we gather up a batch of contiguous dirty pages
787 * under the RCU read lock - but we can't clear the dirty flags
788 * there if any of those pages are mapped.
792 xas_for_each(xas, folio, ULONG_MAX) {
794 if (xas_retry(xas, folio))
796 if (xa_is_value(folio))
798 if (folio_index(folio) != index) {
803 if (!folio_try_get_rcu(folio)) {
808 /* Has the folio moved or been split? */
809 if (unlikely(folio != xas_reload(xas))) {
815 if (!folio_trylock(folio)) {
820 if (!folio_test_dirty(folio) ||
821 folio_test_writeback(folio) ||
822 folio_test_fscache(folio)) {
830 len = folio_size(folio);
831 priv = folio_get_private(folio);
832 if ((const struct netfs_group *)priv != group) {
834 finfo = netfs_folio_info(folio);
835 if (finfo->netfs_group != group ||
836 finfo->dirty_offset > 0) {
842 len = finfo->dirty_len;
845 *_top += folio_size(folio);
846 index += folio_nr_pages(folio);
847 *_count -= folio_nr_pages(folio);
849 if (*_len >= max_len || *_count <= 0)
852 if (!folio_batch_add(&fbatch, folio))
861 /* Now, if we obtained any folios, we can shift them to being
862 * writable and mark them for caching.
864 if (!folio_batch_count(&fbatch))
867 for (i = 0; i < folio_batch_count(&fbatch); i++) {
868 folio = fbatch.folios[i];
869 trace_netfs_folio(folio, netfs_folio_trace_store_plus);
871 if (!folio_clear_dirty_for_io(folio))
873 folio_start_writeback(folio);
874 netfs_folio_start_fscache(caching, folio);
878 folio_batch_release(&fbatch);
884 * Synchronously write back the locked page and any subsequent non-locked dirty
887 static ssize_t netfs_write_back_from_locked_folio(struct address_space *mapping,
888 struct writeback_control *wbc,
889 struct netfs_group *group,
890 struct xa_state *xas,
892 unsigned long long start,
893 unsigned long long end)
895 struct netfs_io_request *wreq;
896 struct netfs_folio *finfo;
897 struct netfs_inode *ctx = netfs_inode(mapping->host);
898 unsigned long long i_size = i_size_read(&ctx->inode);
900 bool caching = netfs_is_cache_enabled(ctx);
901 long count = wbc->nr_to_write;
904 _enter(",%lx,%llx-%llx,%u", folio_index(folio), start, end, caching);
906 wreq = netfs_alloc_request(mapping, NULL, start, folio_size(folio),
910 return PTR_ERR(wreq);
913 if (!folio_clear_dirty_for_io(folio))
915 folio_start_writeback(folio);
916 netfs_folio_start_fscache(caching, folio);
918 count -= folio_nr_pages(folio);
920 /* Find all consecutive lockable dirty pages that have contiguous
921 * written regions, stopping when we find a page that is not
922 * immediately lockable, is not dirty or is missing, or we reach the
925 trace_netfs_folio(folio, netfs_folio_trace_store);
928 finfo = netfs_folio_info(folio);
930 start += finfo->dirty_offset;
931 if (finfo->dirty_offset + finfo->dirty_len != len) {
932 len = finfo->dirty_len;
935 len = finfo->dirty_len;
938 if (start < i_size) {
939 /* Trim the write to the EOF; the extra data is ignored. Also
940 * put an upper limit on the size of a single storedata op.
942 max_len = 65536 * 4096;
943 max_len = min_t(unsigned long long, max_len, end - start + 1);
944 max_len = min_t(unsigned long long, max_len, i_size - start);
947 netfs_extend_writeback(mapping, group, xas, &count, start,
948 max_len, caching, &len, &wreq->upper_len);
952 len = min_t(unsigned long long, len, i_size - start);
954 /* We now have a contiguous set of dirty pages, each with writeback
955 * set; the first page is still locked at this point, but all the rest
956 * have been unlocked.
962 if (start < i_size) {
963 _debug("write back %zx @%llx [%llx]", len, start, i_size);
965 /* Speculatively write to the cache. We have to fix this up
966 * later if the store fails.
968 wreq->cleanup = netfs_cleanup_buffered_write;
970 iov_iter_xarray(&wreq->iter, ITER_SOURCE, &mapping->i_pages, start,
972 __set_bit(NETFS_RREQ_UPLOAD_TO_SERVER, &wreq->flags);
973 ret = netfs_begin_write(wreq, true, netfs_write_trace_writeback);
974 if (ret == 0 || ret == -EIOCBQUEUED)
975 wbc->nr_to_write -= len / PAGE_SIZE;
977 _debug("write discard %zx @%llx [%llx]", len, start, i_size);
979 /* The dirty region was entirely beyond the EOF. */
980 fscache_clear_page_bits(mapping, start, len, caching);
981 netfs_pages_written_back(wreq);
985 netfs_put_request(wreq, false, netfs_rreq_trace_put_return);
991 * Write a region of pages back to the server
993 static ssize_t netfs_writepages_begin(struct address_space *mapping,
994 struct writeback_control *wbc,
995 struct netfs_group *group,
996 struct xa_state *xas,
997 unsigned long long *_start,
998 unsigned long long end)
1000 const struct netfs_folio *finfo;
1001 struct folio *folio;
1002 unsigned long long start = *_start;
1007 _enter("%llx,%llx,", start, end);
1010 /* Find the first dirty page in the group. */
1014 folio = xas_find_marked(xas, end / PAGE_SIZE, PAGECACHE_TAG_DIRTY);
1015 if (xas_retry(xas, folio) || xa_is_value(folio))
1020 if (!folio_try_get_rcu(folio)) {
1025 if (unlikely(folio != xas_reload(xas))) {
1031 /* Skip any dirty folio that's not in the group of interest. */
1032 priv = folio_get_private(folio);
1033 if ((const struct netfs_group *)priv != group) {
1034 finfo = netfs_folio_info(folio);
1035 if (finfo->netfs_group != group) {
1048 start = folio_pos(folio); /* May regress with THPs */
1050 _debug("wback %lx", folio_index(folio));
1052 /* At this point we hold neither the i_pages lock nor the page lock:
1053 * the page may be truncated or invalidated (changing page->mapping to
1054 * NULL), or even swizzled back from swapper_space to tmpfs file
1058 if (wbc->sync_mode != WB_SYNC_NONE) {
1059 ret = folio_lock_killable(folio);
1063 if (!folio_trylock(folio))
1067 if (folio->mapping != mapping ||
1068 !folio_test_dirty(folio)) {
1069 start += folio_size(folio);
1070 folio_unlock(folio);
1074 if (folio_test_writeback(folio) ||
1075 folio_test_fscache(folio)) {
1076 folio_unlock(folio);
1077 if (wbc->sync_mode != WB_SYNC_NONE) {
1078 folio_wait_writeback(folio);
1079 #ifdef CONFIG_FSCACHE
1080 folio_wait_fscache(folio);
1085 start += folio_size(folio);
1086 if (wbc->sync_mode == WB_SYNC_NONE) {
1087 if (skips >= 5 || need_resched()) {
1096 ret = netfs_write_back_from_locked_folio(mapping, wbc, group, xas,
1100 *_start = start + ret;
1101 _leave(" = %zd [%llx]", ret, *_start);
1106 * Write a region of pages back to the server
1108 static int netfs_writepages_region(struct address_space *mapping,
1109 struct writeback_control *wbc,
1110 struct netfs_group *group,
1111 unsigned long long *_start,
1112 unsigned long long end)
1116 XA_STATE(xas, &mapping->i_pages, *_start / PAGE_SIZE);
1119 ret = netfs_writepages_begin(mapping, wbc, group, &xas,
1121 if (ret > 0 && wbc->nr_to_write > 0)
1123 } while (ret > 0 && wbc->nr_to_write > 0);
1125 return ret > 0 ? 0 : ret;
1129 * write some of the pending data back to the server
1131 int netfs_writepages(struct address_space *mapping,
1132 struct writeback_control *wbc)
1134 struct netfs_group *group = NULL;
1140 /* We have to be careful as we can end up racing with setattr()
1141 * truncating the pagecache since the caller doesn't take a lock here
1145 if (wbc->range_cyclic && mapping->writeback_index) {
1146 start = mapping->writeback_index * PAGE_SIZE;
1147 ret = netfs_writepages_region(mapping, wbc, group,
1152 if (wbc->nr_to_write <= 0) {
1153 mapping->writeback_index = start / PAGE_SIZE;
1158 end = mapping->writeback_index * PAGE_SIZE;
1159 mapping->writeback_index = 0;
1160 ret = netfs_writepages_region(mapping, wbc, group, &start, end);
1162 mapping->writeback_index = start / PAGE_SIZE;
1163 } else if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) {
1165 ret = netfs_writepages_region(mapping, wbc, group,
1167 if (wbc->nr_to_write > 0 && ret == 0)
1168 mapping->writeback_index = start / PAGE_SIZE;
1170 start = wbc->range_start;
1171 ret = netfs_writepages_region(mapping, wbc, group,
1172 &start, wbc->range_end);
1176 _leave(" = %d", ret);
1179 EXPORT_SYMBOL(netfs_writepages);
1182 * Deal with the disposition of a laundered folio.
1184 static void netfs_cleanup_launder_folio(struct netfs_io_request *wreq)
1187 pr_notice("R=%08x Laundering error %d\n", wreq->debug_id, wreq->error);
1188 mapping_set_error(wreq->mapping, wreq->error);
1193 * netfs_launder_folio - Clean up a dirty folio that's being invalidated
1194 * @folio: The folio to clean
1196 * This is called to write back a folio that's being invalidated when an inode
1197 * is getting torn down. Ideally, writepages would be used instead.
1199 int netfs_launder_folio(struct folio *folio)
1201 struct netfs_io_request *wreq;
1202 struct address_space *mapping = folio->mapping;
1203 struct netfs_folio *finfo = netfs_folio_info(folio);
1204 struct netfs_group *group = netfs_folio_group(folio);
1205 struct bio_vec bvec;
1206 unsigned long long i_size = i_size_read(mapping->host);
1207 unsigned long long start = folio_pos(folio);
1208 size_t offset = 0, len;
1212 offset = finfo->dirty_offset;
1214 len = finfo->dirty_len;
1216 len = folio_size(folio);
1218 len = min_t(unsigned long long, len, i_size - start);
1220 wreq = netfs_alloc_request(mapping, NULL, start, len, NETFS_LAUNDER_WRITE);
1222 ret = PTR_ERR(wreq);
1226 if (!folio_clear_dirty_for_io(folio))
1229 trace_netfs_folio(folio, netfs_folio_trace_launder);
1231 _debug("launder %llx-%llx", start, start + len - 1);
1233 /* Speculatively write to the cache. We have to fix this up later if
1236 wreq->cleanup = netfs_cleanup_launder_folio;
1238 bvec_set_folio(&bvec, folio, len, offset);
1239 iov_iter_bvec(&wreq->iter, ITER_SOURCE, &bvec, 1, len);
1240 __set_bit(NETFS_RREQ_UPLOAD_TO_SERVER, &wreq->flags);
1241 ret = netfs_begin_write(wreq, true, netfs_write_trace_launder);
1244 folio_detach_private(folio);
1245 netfs_put_group(group);
1247 netfs_put_request(wreq, false, netfs_rreq_trace_put_return);
1249 folio_wait_fscache(folio);
1250 _leave(" = %d", ret);
1253 EXPORT_SYMBOL(netfs_launder_folio);