Debian Codegraph @

fs/netfs/buffered_write.c

raw
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Network filesystem high-level buffered write support.
3 *
4 * Copyright (C) 2023 Red Hat, Inc. All Rights Reserved.
5 * Written by David Howells ([email protected])
6 */
7
8 #include <linux/export.h>
9 #include <linux/fs.h>
10 #include <linux/mm.h>
11 #include <linux/pagemap.h>
12 #include <linux/slab.h>
13 #include <linux/pagevec.h>
14 #include "internal.h"
15
16 /*
17 * Grab a folio for writing and lock it. Attempt to allocate as large a folio
18 * as possible to hold as much of the remaining length as possible in one go.
19 */
20 static struct folio *netfs_grab_folio_for_write(struct address_space *mapping,
21 loff_t pos, size_t part)
22 {
23 pgoff_t index = pos / PAGE_SIZE;
24 fgf_t fgp_flags = FGP_WRITEBEGIN;
25
26 if (mapping_large_folio_support(mapping))
27 fgp_flags |= fgf_set_order(pos % PAGE_SIZE + part);
28
29 return __filemap_get_folio(mapping, index, fgp_flags,
30 mapping_gfp_mask(mapping));
31 }
32
33 /*
34 * Update i_size and estimate the update to i_blocks to reflect the additional
35 * data written into the pagecache until we can find out from the server what
36 * the values actually are.
37 */
38 void netfs_update_i_size(struct netfs_inode *ctx, struct inode *inode,
39 loff_t pos, size_t copied)
40 {
41 loff_t i_size, end = pos + copied;
42 blkcnt_t add;
43 size_t gap;
44
45 if (end <= i_size_read(inode))
46 return;
47
48 if (ctx->ops->update_i_size) {
49 ctx->ops->update_i_size(inode, end);
50 return;
51 }
52
53 spin_lock(&inode->i_lock);
54
55 i_size = i_size_read(inode);
56 if (end > i_size) {
57 i_size_write(inode, end);
58 #if IS_ENABLED(CONFIG_FSCACHE)
59 fscache_update_cookie(ctx->cache, NULL, &end);
60 #endif
61
62 gap = SECTOR_SIZE - (i_size & (SECTOR_SIZE - 1));
63 if (copied > gap) {
64 add = DIV_ROUND_UP(copied - gap, SECTOR_SIZE);
65
66 inode->i_blocks = min_t(blkcnt_t,
67 DIV_ROUND_UP(end, SECTOR_SIZE),
68 inode->i_blocks + add);
69 }
70 }
71 spin_unlock(&inode->i_lock);
72 }
73
74 /**
75 * netfs_perform_write - Copy data into the pagecache.
76 * @iocb: The operation parameters
77 * @iter: The source buffer
78 * @netfs_group: Grouping for dirty folios (eg. ceph snaps).
79 *
80 * Copy data into pagecache folios attached to the inode specified by @iocb.
81 * The caller must hold appropriate inode locks.
82 *
83 * Dirty folios are tagged with a netfs_folio struct if they're not up to date
84 * to indicate the range modified. Dirty folios may also be tagged with a
85 * netfs-specific grouping such that data from an old group gets flushed before
86 * a new one is started.
87 */
88 ssize_t netfs_perform_write(struct kiocb *iocb, struct iov_iter *iter,
89 struct netfs_group *netfs_group)
90 {
91 struct file *file = iocb->ki_filp;
92 struct inode *inode = file_inode(file);
93 struct address_space *mapping = inode->i_mapping;
94 struct netfs_inode *ctx = netfs_inode(inode);
95 struct writeback_control wbc = {
96 .sync_mode = WB_SYNC_NONE,
97 .for_sync = true,
98 .nr_to_write = LONG_MAX,
99 .range_start = iocb->ki_pos,
100 .range_end = iocb->ki_pos + iter->count,
101 };
102 struct netfs_io_request *wreq = NULL;
103 struct folio *folio = NULL, *writethrough = NULL;
104 unsigned int bdp_flags = (iocb->ki_flags & IOCB_NOWAIT) ? BDP_ASYNC : 0;
105 ssize_t written = 0, ret, ret2;
106 loff_t pos = iocb->ki_pos;
107 size_t max_chunk = mapping_max_folio_size(mapping);
108 bool maybe_trouble = false;
109
110 if (unlikely(iocb->ki_flags & (IOCB_DSYNC | IOCB_SYNC))
111 ) {
112 wbc_attach_fdatawrite_inode(&wbc, mapping->host);
113
114 ret = filemap_write_and_wait_range(mapping, pos, pos + iter->count);
115 if (ret < 0) {
116 wbc_detach_inode(&wbc);
117 goto out;
118 }
119
120 wreq = netfs_begin_writethrough(iocb, iter->count);
121 if (IS_ERR(wreq)) {
122 wbc_detach_inode(&wbc);
123 ret = PTR_ERR(wreq);
124 wreq = NULL;
125 goto out;
126 }
127 if (!is_sync_kiocb(iocb))
128 wreq->iocb = iocb;
129 netfs_stat(&netfs_n_wh_writethrough);
130 } else {
131 netfs_stat(&netfs_n_wh_buffered_write);
132 }
133
134 do {
135 enum netfs_folio_trace trace;
136 struct netfs_folio *finfo;
137 struct netfs_group *group;
138 unsigned long long fpos;
139 size_t flen;
140 size_t offset; /* Offset into pagecache folio */
141 size_t part; /* Bytes to write to folio */
142 size_t copied; /* Bytes copied from user */
143 void *priv;
144
145 offset = pos & (max_chunk - 1);
146 part = min(max_chunk - offset, iov_iter_count(iter));
147
148 /* Bring in the user pages that we will copy from _first_ lest
149 * we hit a nasty deadlock on copying from the same page as
150 * we're writing to, without it being marked uptodate.
151 *
152 * Not only is this an optimisation, but it is also required to
153 * check that the address is actually valid, when atomic
154 * usercopies are used below.
155 *
156 * We rely on the page being held onto long enough by the LRU
157 * that we can grab it below if this causes it to be read.
158 */
159 ret = -EFAULT;
160 if (unlikely(fault_in_iov_iter_readable(iter, part) == part))
161 break;
162
163 folio = netfs_grab_folio_for_write(mapping, pos, part);
164 if (IS_ERR(folio)) {
165 ret = PTR_ERR(folio);
166 break;
167 }
168
169 flen = folio_size(folio);
170 fpos = folio_pos(folio);
171 offset = pos - fpos;
172 part = min_t(size_t, flen - offset, part);
173
174 /* Wait for writeback to complete. The writeback engine owns
175 * the info in folio->private and may change it until it
176 * removes the WB mark.
177 */
178 if (folio_get_private(folio) &&
179 folio_wait_writeback_killable(folio)) {
180 ret = written ? -EINTR : -ERESTARTSYS;
181 goto error_folio_unlock;
182 }
183
184 if (signal_pending(current)) {
185 ret = written ? -EINTR : -ERESTARTSYS;
186 goto error_folio_unlock;
187 }
188
189 finfo = netfs_folio_info(folio);
190 group = netfs_folio_group(folio);
191
192 /* If the requested group differs from the group set on the
193 * page, then we need to flush out the folio if it has a group
194 * set (ie. is non-NULL). Note that COPY_TO_CACHE is a special
195 * case, being a netfs annotation rather than an actual group.
196 *
197 * The filesystem isn't permitted to mix writes with groups and
198 * writes without groups as the NULL group is used to indicate
199 * that no group is set.
200 */
201 if (unlikely(group != netfs_group) &&
202 group != NETFS_FOLIO_COPY_TO_CACHE &&
203 group) {
204 WARN_ON_ONCE(!netfs_group);
205 goto flush_content;
206 }
207
208 /* Decide how we should modify a folio. We might be attempting
209 * to do write-streaming, as we don't want to a local RMW cycle
210 * if we can avoid it. If we're doing local caching or content
211 * crypto, we award that priority over avoiding RMW. If the
212 * file is open readably, then we let ->read_folio() fill in
213 * the gaps.
214 */
215 if (folio_test_uptodate(folio)) {
216 if (mapping_writably_mapped(mapping))
217 flush_dcache_folio(folio);
218 copied = copy_folio_from_iter_atomic(folio, offset, part, iter);
219 if (unlikely(copied == 0))
220 goto copy_failed;
221 trace = netfs_folio_is_uptodate;
222 goto copied_uptodate;
223 }
224
225 /* If the page is above the zero-point then we assume that the
226 * server would just return a block of zeros or a short read if
227 * we try to read it.
228 */
229 if (fpos >= netfs_read_zero_point(inode)) {
230 folio_zero_segment(folio, 0, offset);
231 copied = copy_folio_from_iter_atomic(folio, offset, part, iter);
232 if (unlikely(copied == 0))
233 goto copy_failed;
234 folio_zero_segment(folio, offset + copied, flen);
235 if (finfo)
236 trace = netfs_modify_and_clear_rm_finfo;
237 else
238 trace = netfs_modify_and_clear;
239 goto mark_uptodate;
240 }
241
242 /* See if we can write a whole folio in one go. */
243 if (!maybe_trouble && offset == 0 && part >= flen) {
244 copied = copy_folio_from_iter_atomic(folio, offset, part, iter);
245 if (likely(copied == part)) {
246 if (finfo)
247 trace = netfs_whole_folio_modify_filled;
248 else
249 trace = netfs_whole_folio_modify;
250 goto mark_uptodate;
251 }
252 if (copied == 0)
253 goto copy_failed;
254 if (!finfo || copied <= finfo->dirty_offset) {
255 maybe_trouble = true;
256 iov_iter_revert(iter, copied);
257 copied = 0;
258 folio_unlock(folio);
259 goto retry;
260 }
261
262 /* We overwrote some existing dirty data, so we have to
263 * accept the partial write.
264 */
265 finfo->dirty_len += finfo->dirty_offset;
266 if (finfo->dirty_len == flen) {
267 trace = netfs_whole_folio_modify_filled_efault;
268 goto mark_uptodate;
269 }
270 if (copied > finfo->dirty_len)
271 finfo->dirty_len = copied;
272 finfo->dirty_offset = 0;
273 trace = netfs_whole_folio_modify_efault;
274 goto copied;
275 }
276
277 /* We don't want to do a streaming write on a file that loses
278 * caching service temporarily because the backing store got
279 * culled.
280 */
281 if (netfs_is_cache_enabled(ctx)) {
282 if (finfo) {
283 netfs_stat(&netfs_n_wh_wstream_conflict);
284 goto flush_content;
285 }
286 ret = netfs_prefetch_for_write(file, folio, offset, part);
287 if (ret < 0) {
288 _debug("prefetch = %zd", ret);
289 goto error_folio_unlock;
290 }
291 /* Note that copy-to-cache may have been set. */
292
293 copied = copy_folio_from_iter_atomic(folio, offset, part, iter);
294 if (unlikely(copied == 0))
295 goto copy_failed;
296 trace = netfs_just_prefetch;
297 goto copied_uptodate;
298 }
299
300 /* Do a streaming write on a folio that has nothing in it yet. */
301 if (!finfo) {
302 ret = -EIO;
303 if (WARN_ON(folio_get_private(folio)))
304 goto error_folio_unlock;
305 copied = copy_folio_from_iter_atomic(folio, offset, part, iter);
306 if (unlikely(copied == 0))
307 goto copy_failed;
308 if (offset == 0 && copied == flen) {
309 trace = netfs_streaming_filled_page;
310 goto mark_uptodate;
311 }
312
313 finfo = kzalloc_obj(*finfo);
314 if (!finfo) {
315 iov_iter_revert(iter, copied);
316 ret = -ENOMEM;
317 goto error_folio_unlock;
318 }
319 finfo->netfs_group = netfs_get_group(netfs_group);
320 finfo->dirty_offset = offset;
321 finfo->dirty_len = copied;
322 folio_attach_private(folio, (void *)((unsigned long)finfo |
323 NETFS_FOLIO_INFO));
324 trace = netfs_streaming_write;
325 goto copied;
326 }
327
328 /* We can continue a streaming write only if it continues on
329 * from the previous. If it overlaps, we must flush lest we
330 * suffer a partial copy and disjoint dirty regions.
331 */
332 if (offset == finfo->dirty_offset + finfo->dirty_len) {
333 copied = copy_folio_from_iter_atomic(folio, offset, part, iter);
334 if (unlikely(copied == 0))
335 goto copy_failed;
336 finfo->dirty_len += copied;
337 if (finfo->dirty_offset == 0 && finfo->dirty_len == flen) {
338 trace = netfs_streaming_cont_filled_page;
339 goto mark_uptodate;
340 }
341 trace = netfs_streaming_write_cont;
342 goto copied;
343 }
344
345 /* Incompatible write; flush the folio and try again. */
346 flush_content:
347 trace_netfs_folio(folio, netfs_flush_content);
348 folio_unlock(folio);
349 folio_put(folio);
350 ret = filemap_write_and_wait_range(mapping, fpos, fpos + flen - 1);
351 if (ret < 0)
352 goto out;
353 continue;
354
355 /* Mark a folio as being up to data when we've filled it
356 * completely. If the folio has a group attached, then it must
357 * be the same group, otherwise we should have flushed it out
358 * above. We have to get rid of the netfs_folio struct if
359 * there was one.
360 */
361 mark_uptodate:
362 folio_mark_uptodate(folio);
363
364 copied_uptodate:
365 priv = folio_get_private(folio);
366 if (likely(priv == netfs_group)) {
367 /* Already set correctly; no change required. */
368 } else if (priv == NETFS_FOLIO_COPY_TO_CACHE) {
369 if (!netfs_group)
370 folio_detach_private(folio);
371 else
372 folio_change_private(folio, netfs_get_group(netfs_group));
373 } else if (!priv) {
374 folio_attach_private(folio, netfs_get_group(netfs_group));
375 } else {
376 WARN_ON_ONCE(!finfo);
377 if (netfs_group)
378 /* finfo->netfs_group has a ref */
379 folio_change_private(folio, netfs_group);
380 else
381 folio_detach_private(folio);
382 kfree(finfo);
383 }
384
385 copied:
386 trace_netfs_folio(folio, trace);
387 flush_dcache_folio(folio);
388
389 /* Update the inode size if we moved the EOF marker */
390 netfs_update_i_size(ctx, inode, pos, copied);
391 pos += copied;
392 written += copied;
393
394 if (likely(!wreq)) {
395 folio_mark_dirty(folio);
396 folio_unlock(folio);
397 } else {
398 netfs_advance_writethrough(wreq, &wbc, folio, copied,
399 offset + copied == flen,
400 &writethrough);
401 /* Folio unlocked */
402 }
403 retry:
404 folio_put(folio);
405 folio = NULL;
406
407 ret = balance_dirty_pages_ratelimited_flags(mapping, bdp_flags);
408 if (unlikely(ret < 0))
409 break;
410
411 cond_resched();
412 } while (iov_iter_count(iter));
413
414 out:
415 if (likely(written)) {
416 /* Set indication that ctime and mtime got updated in case
417 * close is deferred.
418 */
419 set_bit(NETFS_ICTX_MODIFIED_ATTR, &ctx->flags);
420 if (unlikely(ctx->ops->post_modify))
421 ctx->ops->post_modify(inode);
422 }
423
424 if (unlikely(wreq)) {
425 ret2 = netfs_end_writethrough(wreq, &wbc, writethrough);
426 wbc_detach_inode(&wbc);
427 if (ret2 == -EIOCBQUEUED)
428 return ret2;
429 if (ret == 0 && ret2 < 0)
430 ret = ret2;
431 }
432
433 iocb->ki_pos += written;
434 _leave(" = %zd [%zd]", written, ret);
435 return written ? written : ret;
436
437 copy_failed:
438 ret = -EFAULT;
439 error_folio_unlock:
440 folio_unlock(folio);
441 folio_put(folio);
442 goto out;
443 }
444 EXPORT_SYMBOL(netfs_perform_write);
445
446 /**
447 * netfs_buffered_write_iter_locked - write data to a file
448 * @iocb: IO state structure (file, offset, etc.)
449 * @from: iov_iter with data to write
450 * @netfs_group: Grouping for dirty folios (eg. ceph snaps).
451 *
452 * This function does all the work needed for actually writing data to a
453 * file. It does all basic checks, removes SUID from the file, updates
454 * modification times and calls proper subroutines depending on whether we
455 * do direct IO or a standard buffered write.
456 *
457 * The caller must hold appropriate locks around this function and have called
458 * generic_write_checks() already. The caller is also responsible for doing
459 * any necessary syncing afterwards.
460 *
461 * This function does *not* take care of syncing data in case of O_SYNC write.
462 * A caller has to handle it. This is mainly due to the fact that we want to
463 * avoid syncing under i_rwsem.
464 *
465 * Return:
466 * * number of bytes written, even for truncated writes
467 * * negative error code if no data has been written at all
468 */
469 ssize_t netfs_buffered_write_iter_locked(struct kiocb *iocb, struct iov_iter *from,
470 struct netfs_group *netfs_group)
471 {
472 struct file *file = iocb->ki_filp;
473 ssize_t ret;
474
475 trace_netfs_write_iter(iocb, from);
476
477 ret = file_remove_privs(file);
478 if (ret)
479 return ret;
480
481 ret = file_update_time(file);
482 if (ret)
483 return ret;
484
485 return netfs_perform_write(iocb, from, netfs_group);
486 }
487 EXPORT_SYMBOL(netfs_buffered_write_iter_locked);
488
489 /**
490 * netfs_file_write_iter - write data to a file
491 * @iocb: IO state structure
492 * @from: iov_iter with data to write
493 *
494 * Perform a write to a file, writing into the pagecache if possible and doing
495 * an unbuffered write instead if not.
496 *
497 * Return:
498 * * Negative error code if no data has been written at all of
499 * vfs_fsync_range() failed for a synchronous write
500 * * Number of bytes written, even for truncated writes
501 */
502 ssize_t netfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
503 {
504 struct file *file = iocb->ki_filp;
505 struct inode *inode = file->f_mapping->host;
506 struct netfs_inode *ictx = netfs_inode(inode);
507 ssize_t ret;
508
509 _enter("%llx,%zx,%llx", iocb->ki_pos, iov_iter_count(from), i_size_read(inode));
510
511 if (!iov_iter_count(from))
512 return 0;
513
514 if ((iocb->ki_flags & IOCB_DIRECT) ||
515 test_bit(NETFS_ICTX_UNBUFFERED, &ictx->flags))
516 return netfs_unbuffered_write_iter(iocb, from);
517
518 ret = netfs_start_io_write(inode);
519 if (ret < 0)
520 return ret;
521
522 ret = generic_write_checks(iocb, from);
523 if (ret > 0)
524 ret = netfs_buffered_write_iter_locked(iocb, from, NULL);
525 netfs_end_io_write(inode);
526 if (ret > 0)
527 ret = generic_write_sync(iocb, ret);
528 return ret;
529 }
530 EXPORT_SYMBOL(netfs_file_write_iter);
531
532 /*
533 * Notification that a previously read-only page is about to become writable.
534 * The caller indicates the precise page that needs to be written to, but
535 * we only track group on a per-folio basis, so we block more often than
536 * we might otherwise.
537 */
538 vm_fault_t netfs_page_mkwrite(struct vm_fault *vmf, struct netfs_group *netfs_group)
539 {
540 struct netfs_group *group;
541 struct folio *folio = page_folio(vmf->page);
542 struct file *file = vmf->vma->vm_file;
543 struct address_space *mapping = file->f_mapping;
544 struct inode *inode = file_inode(file);
545 struct netfs_inode *ictx = netfs_inode(inode);
546 vm_fault_t ret = VM_FAULT_NOPAGE;
547 void *priv;
548 int err;
549
550 _enter("%lx", folio->index);
551
552 sb_start_pagefault(inode->i_sb);
553
554 if (folio_lock_killable(folio) < 0)
555 goto out;
556 if (folio->mapping != mapping)
557 goto unlock;
558 if (folio_wait_writeback_killable(folio) < 0)
559 goto unlock;
560
561 /* Can we see a streaming write here? */
562 if (WARN_ON(!folio_test_uptodate(folio))) {
563 ret = VM_FAULT_SIGBUS;
564 goto unlock;
565 }
566
567 group = netfs_folio_group(folio);
568 if (group &&
569 group != netfs_group &&
570 group != NETFS_FOLIO_COPY_TO_CACHE) {
571 folio_unlock(folio);
572 err = filemap_fdatawrite_range(mapping,
573 folio_pos(folio),
574 folio_next_pos(folio));
575 switch (err) {
576 case 0:
577 ret = VM_FAULT_RETRY;
578 goto out;
579 case -ENOMEM:
580 ret = VM_FAULT_OOM;
581 goto out;
582 default:
583 ret = VM_FAULT_SIGBUS;
584 goto out;
585 }
586 }
587
588 if (folio_test_dirty(folio))
589 trace_netfs_folio(folio, netfs_folio_trace_mkwrite_plus);
590 else
591 trace_netfs_folio(folio, netfs_folio_trace_mkwrite);
592
593 priv = folio_get_private(folio);
594 if (priv != netfs_group) {
595 if (!netfs_group && priv == NETFS_FOLIO_COPY_TO_CACHE)
596 folio_detach_private(folio);
597 else if (netfs_group && priv == NETFS_FOLIO_COPY_TO_CACHE)
598 folio_change_private(folio, netfs_get_group(netfs_group));
599 else if (netfs_group && !priv)
600 folio_attach_private(folio, netfs_get_group(netfs_group));
601 else
602 WARN_ON_ONCE(1);
603 }
604
605 file_update_time(file);
606 set_bit(NETFS_ICTX_MODIFIED_ATTR, &ictx->flags);
607 if (ictx->ops->post_modify)
608 ictx->ops->post_modify(inode);
609 ret = VM_FAULT_LOCKED;
610 out:
611 sb_end_pagefault(inode->i_sb);
612 return ret;
613 unlock:
614 folio_unlock(folio);
615 goto out;
616 }
617 EXPORT_SYMBOL(netfs_page_mkwrite);