Commit | Line | Data |
---|---|---|
c38f4e96 DH |
1 | // SPDX-License-Identifier: GPL-2.0-only |
2 | /* Network filesystem high-level write support. | |
3 | * | |
4 | * Copyright (C) 2023 Red Hat, Inc. All Rights Reserved. | |
5 | * Written by David Howells (dhowells@redhat.com) | |
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 | * Determined write method. Adjust netfs_folio_traces if this is changed. | |
18 | */ | |
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. */ | |
27 | }; | |
28 | ||
29 | static void netfs_set_group(struct folio *folio, struct netfs_group *netfs_group) | |
30 | { | |
31 | if (netfs_group && !folio_get_private(folio)) | |
32 | folio_attach_private(folio, netfs_get_group(netfs_group)); | |
33 | } | |
34 | ||
35 | /* | |
36 | * Decide how we should modify a folio. We might be attempting to do | |
37 | * write-streaming, in which case we don't want to a local RMW cycle if we can | |
38 | * avoid it. If we're doing local caching or content crypto, we award that | |
39 | * priority over avoiding RMW. If the file is open readably, then we also | |
40 | * assume that we may want to read what we wrote. | |
41 | */ | |
42 | static enum netfs_how_to_modify netfs_how_to_modify(struct netfs_inode *ctx, | |
43 | struct file *file, | |
44 | struct folio *folio, | |
45 | void *netfs_group, | |
46 | size_t flen, | |
47 | size_t offset, | |
48 | size_t len, | |
49 | bool maybe_trouble) | |
50 | { | |
51 | struct netfs_folio *finfo = netfs_folio_info(folio); | |
52 | loff_t pos = folio_file_pos(folio); | |
53 | ||
54 | _enter(""); | |
55 | ||
56 | if (netfs_folio_group(folio) != netfs_group) | |
57 | return NETFS_FLUSH_CONTENT; | |
58 | ||
59 | if (folio_test_uptodate(folio)) | |
60 | return NETFS_FOLIO_IS_UPTODATE; | |
61 | ||
62 | if (pos >= ctx->remote_i_size) | |
63 | return NETFS_MODIFY_AND_CLEAR; | |
64 | ||
65 | if (!maybe_trouble && offset == 0 && len >= flen) | |
66 | return NETFS_WHOLE_FOLIO_MODIFY; | |
67 | ||
68 | if (file->f_mode & FMODE_READ) | |
69 | return NETFS_JUST_PREFETCH; | |
70 | ||
71 | if (netfs_is_cache_enabled(ctx)) | |
72 | return NETFS_JUST_PREFETCH; | |
73 | ||
74 | if (!finfo) | |
75 | return NETFS_STREAMING_WRITE; | |
76 | ||
77 | /* We can continue a streaming write only if it continues on from the | |
78 | * previous. If it overlaps, we must flush lest we suffer a partial | |
79 | * copy and disjoint dirty regions. | |
80 | */ | |
81 | if (offset == finfo->dirty_offset + finfo->dirty_len) | |
82 | return NETFS_STREAMING_WRITE_CONT; | |
83 | return NETFS_FLUSH_CONTENT; | |
84 | } | |
85 | ||
86 | /* | |
e2e2e839 DH |
87 | * Grab a folio for writing and lock it. Attempt to allocate as large a folio |
88 | * as possible to hold as much of the remaining length as possible in one go. | |
c38f4e96 DH |
89 | */ |
90 | static struct folio *netfs_grab_folio_for_write(struct address_space *mapping, | |
91 | loff_t pos, size_t part) | |
92 | { | |
93 | pgoff_t index = pos / PAGE_SIZE; | |
e2e2e839 | 94 | fgf_t fgp_flags = FGP_WRITEBEGIN; |
c38f4e96 | 95 | |
e2e2e839 DH |
96 | if (mapping_large_folio_support(mapping)) |
97 | fgp_flags |= fgf_set_order(pos % PAGE_SIZE + part); | |
98 | ||
99 | return __filemap_get_folio(mapping, index, fgp_flags, | |
c38f4e96 DH |
100 | mapping_gfp_mask(mapping)); |
101 | } | |
102 | ||
103 | /** | |
104 | * netfs_perform_write - Copy data into the pagecache. | |
105 | * @iocb: The operation parameters | |
106 | * @iter: The source buffer | |
107 | * @netfs_group: Grouping for dirty pages (eg. ceph snaps). | |
108 | * | |
109 | * Copy data into pagecache pages attached to the inode specified by @iocb. | |
110 | * The caller must hold appropriate inode locks. | |
111 | * | |
112 | * Dirty pages are tagged with a netfs_folio struct if they're not up to date | |
113 | * to indicate the range modified. Dirty pages may also be tagged with a | |
114 | * netfs-specific grouping such that data from an old group gets flushed before | |
115 | * a new one is started. | |
116 | */ | |
117 | ssize_t netfs_perform_write(struct kiocb *iocb, struct iov_iter *iter, | |
118 | struct netfs_group *netfs_group) | |
119 | { | |
120 | struct file *file = iocb->ki_filp; | |
121 | struct inode *inode = file_inode(file); | |
122 | struct address_space *mapping = inode->i_mapping; | |
123 | struct netfs_inode *ctx = netfs_inode(inode); | |
124 | struct netfs_folio *finfo; | |
125 | struct folio *folio; | |
126 | enum netfs_how_to_modify howto; | |
127 | enum netfs_folio_trace trace; | |
128 | unsigned int bdp_flags = (iocb->ki_flags & IOCB_SYNC) ? 0: BDP_ASYNC; | |
129 | ssize_t written = 0, ret; | |
130 | loff_t i_size, pos = iocb->ki_pos, from, to; | |
131 | size_t max_chunk = PAGE_SIZE << MAX_PAGECACHE_ORDER; | |
132 | bool maybe_trouble = false; | |
133 | ||
134 | do { | |
135 | size_t flen; | |
136 | size_t offset; /* Offset into pagecache folio */ | |
137 | size_t part; /* Bytes to write to folio */ | |
138 | size_t copied; /* Bytes copied from user */ | |
139 | ||
140 | ret = balance_dirty_pages_ratelimited_flags(mapping, bdp_flags); | |
141 | if (unlikely(ret < 0)) | |
142 | break; | |
143 | ||
144 | offset = pos & (max_chunk - 1); | |
145 | part = min(max_chunk - offset, iov_iter_count(iter)); | |
146 | ||
147 | /* Bring in the user pages that we will copy from _first_ lest | |
148 | * we hit a nasty deadlock on copying from the same page as | |
149 | * we're writing to, without it being marked uptodate. | |
150 | * | |
151 | * Not only is this an optimisation, but it is also required to | |
152 | * check that the address is actually valid, when atomic | |
153 | * usercopies are used below. | |
154 | * | |
155 | * We rely on the page being held onto long enough by the LRU | |
156 | * that we can grab it below if this causes it to be read. | |
157 | */ | |
158 | ret = -EFAULT; | |
159 | if (unlikely(fault_in_iov_iter_readable(iter, part) == part)) | |
160 | break; | |
161 | ||
162 | ret = -ENOMEM; | |
163 | folio = netfs_grab_folio_for_write(mapping, pos, part); | |
164 | if (!folio) | |
165 | break; | |
166 | ||
167 | flen = folio_size(folio); | |
168 | offset = pos & (flen - 1); | |
169 | part = min_t(size_t, flen - offset, part); | |
170 | ||
171 | if (signal_pending(current)) { | |
172 | ret = written ? -EINTR : -ERESTARTSYS; | |
173 | goto error_folio_unlock; | |
174 | } | |
175 | ||
176 | /* See if we need to prefetch the area we're going to modify. | |
177 | * We need to do this before we get a lock on the folio in case | |
178 | * there's more than one writer competing for the same cache | |
179 | * block. | |
180 | */ | |
181 | howto = netfs_how_to_modify(ctx, file, folio, netfs_group, | |
182 | flen, offset, part, maybe_trouble); | |
183 | _debug("howto %u", howto); | |
184 | switch (howto) { | |
185 | case NETFS_JUST_PREFETCH: | |
186 | ret = netfs_prefetch_for_write(file, folio, offset, part); | |
187 | if (ret < 0) { | |
188 | _debug("prefetch = %zd", ret); | |
189 | goto error_folio_unlock; | |
190 | } | |
191 | break; | |
192 | case NETFS_FOLIO_IS_UPTODATE: | |
193 | case NETFS_WHOLE_FOLIO_MODIFY: | |
194 | case NETFS_STREAMING_WRITE_CONT: | |
195 | break; | |
196 | case NETFS_MODIFY_AND_CLEAR: | |
197 | zero_user_segment(&folio->page, 0, offset); | |
198 | break; | |
199 | case NETFS_STREAMING_WRITE: | |
200 | ret = -EIO; | |
201 | if (WARN_ON(folio_get_private(folio))) | |
202 | goto error_folio_unlock; | |
203 | break; | |
204 | case NETFS_FLUSH_CONTENT: | |
205 | trace_netfs_folio(folio, netfs_flush_content); | |
206 | from = folio_pos(folio); | |
207 | to = from + folio_size(folio) - 1; | |
208 | folio_unlock(folio); | |
209 | folio_put(folio); | |
210 | ret = filemap_write_and_wait_range(mapping, from, to); | |
211 | if (ret < 0) | |
212 | goto error_folio_unlock; | |
213 | continue; | |
214 | } | |
215 | ||
216 | if (mapping_writably_mapped(mapping)) | |
217 | flush_dcache_folio(folio); | |
218 | ||
219 | copied = copy_folio_from_iter_atomic(folio, offset, part, iter); | |
220 | ||
221 | flush_dcache_folio(folio); | |
222 | ||
223 | /* Deal with a (partially) failed copy */ | |
224 | if (copied == 0) { | |
225 | ret = -EFAULT; | |
226 | goto error_folio_unlock; | |
227 | } | |
228 | ||
229 | trace = (enum netfs_folio_trace)howto; | |
230 | switch (howto) { | |
231 | case NETFS_FOLIO_IS_UPTODATE: | |
232 | case NETFS_JUST_PREFETCH: | |
233 | netfs_set_group(folio, netfs_group); | |
234 | break; | |
235 | case NETFS_MODIFY_AND_CLEAR: | |
236 | zero_user_segment(&folio->page, offset + copied, flen); | |
237 | netfs_set_group(folio, netfs_group); | |
238 | folio_mark_uptodate(folio); | |
239 | break; | |
240 | case NETFS_WHOLE_FOLIO_MODIFY: | |
241 | if (unlikely(copied < part)) { | |
242 | maybe_trouble = true; | |
243 | iov_iter_revert(iter, copied); | |
244 | copied = 0; | |
245 | goto retry; | |
246 | } | |
247 | netfs_set_group(folio, netfs_group); | |
248 | folio_mark_uptodate(folio); | |
249 | break; | |
250 | case NETFS_STREAMING_WRITE: | |
251 | if (offset == 0 && copied == flen) { | |
252 | netfs_set_group(folio, netfs_group); | |
253 | folio_mark_uptodate(folio); | |
254 | trace = netfs_streaming_filled_page; | |
255 | break; | |
256 | } | |
257 | finfo = kzalloc(sizeof(*finfo), GFP_KERNEL); | |
258 | if (!finfo) { | |
259 | iov_iter_revert(iter, copied); | |
260 | ret = -ENOMEM; | |
261 | goto error_folio_unlock; | |
262 | } | |
263 | finfo->netfs_group = netfs_get_group(netfs_group); | |
264 | finfo->dirty_offset = offset; | |
265 | finfo->dirty_len = copied; | |
266 | folio_attach_private(folio, (void *)((unsigned long)finfo | | |
267 | NETFS_FOLIO_INFO)); | |
268 | break; | |
269 | case NETFS_STREAMING_WRITE_CONT: | |
270 | finfo = netfs_folio_info(folio); | |
271 | finfo->dirty_len += copied; | |
272 | if (finfo->dirty_offset == 0 && finfo->dirty_len == flen) { | |
273 | if (finfo->netfs_group) | |
274 | folio_change_private(folio, finfo->netfs_group); | |
275 | else | |
276 | folio_detach_private(folio); | |
277 | folio_mark_uptodate(folio); | |
278 | kfree(finfo); | |
279 | trace = netfs_streaming_cont_filled_page; | |
280 | } | |
281 | break; | |
282 | default: | |
283 | WARN(true, "Unexpected modify type %u ix=%lx\n", | |
284 | howto, folio_index(folio)); | |
285 | ret = -EIO; | |
286 | goto error_folio_unlock; | |
287 | } | |
288 | ||
289 | trace_netfs_folio(folio, trace); | |
290 | ||
291 | /* Update the inode size if we moved the EOF marker */ | |
292 | i_size = i_size_read(inode); | |
293 | pos += copied; | |
294 | if (pos > i_size) { | |
295 | if (ctx->ops->update_i_size) { | |
296 | ctx->ops->update_i_size(inode, pos); | |
297 | } else { | |
298 | i_size_write(inode, pos); | |
299 | #if IS_ENABLED(CONFIG_FSCACHE) | |
300 | fscache_update_cookie(ctx->cache, NULL, &pos); | |
301 | #endif | |
302 | } | |
303 | } | |
304 | written += copied; | |
305 | ||
306 | folio_mark_dirty(folio); | |
307 | retry: | |
308 | folio_unlock(folio); | |
309 | folio_put(folio); | |
310 | folio = NULL; | |
311 | ||
312 | cond_resched(); | |
313 | } while (iov_iter_count(iter)); | |
314 | ||
315 | out: | |
316 | if (likely(written)) { | |
317 | /* Flush and wait for a write that requires immediate synchronisation. */ | |
318 | if (iocb->ki_flags & (IOCB_DSYNC | IOCB_SYNC)) { | |
319 | _debug("dsync"); | |
320 | ret = filemap_fdatawait_range(mapping, iocb->ki_pos, | |
321 | iocb->ki_pos + written); | |
322 | } | |
323 | ||
324 | iocb->ki_pos += written; | |
325 | } | |
326 | ||
327 | _leave(" = %zd [%zd]", written, ret); | |
328 | return written ? written : ret; | |
329 | ||
330 | error_folio_unlock: | |
331 | folio_unlock(folio); | |
332 | folio_put(folio); | |
333 | goto out; | |
334 | } | |
335 | EXPORT_SYMBOL(netfs_perform_write); | |
938e13a7 DH |
336 | |
337 | /** | |
338 | * netfs_buffered_write_iter_locked - write data to a file | |
339 | * @iocb: IO state structure (file, offset, etc.) | |
340 | * @from: iov_iter with data to write | |
341 | * @netfs_group: Grouping for dirty pages (eg. ceph snaps). | |
342 | * | |
343 | * This function does all the work needed for actually writing data to a | |
344 | * file. It does all basic checks, removes SUID from the file, updates | |
345 | * modification times and calls proper subroutines depending on whether we | |
346 | * do direct IO or a standard buffered write. | |
347 | * | |
348 | * The caller must hold appropriate locks around this function and have called | |
349 | * generic_write_checks() already. The caller is also responsible for doing | |
350 | * any necessary syncing afterwards. | |
351 | * | |
352 | * This function does *not* take care of syncing data in case of O_SYNC write. | |
353 | * A caller has to handle it. This is mainly due to the fact that we want to | |
354 | * avoid syncing under i_rwsem. | |
355 | * | |
356 | * Return: | |
357 | * * number of bytes written, even for truncated writes | |
358 | * * negative error code if no data has been written at all | |
359 | */ | |
360 | ssize_t netfs_buffered_write_iter_locked(struct kiocb *iocb, struct iov_iter *from, | |
361 | struct netfs_group *netfs_group) | |
362 | { | |
363 | struct file *file = iocb->ki_filp; | |
364 | ssize_t ret; | |
365 | ||
366 | trace_netfs_write_iter(iocb, from); | |
367 | ||
368 | ret = file_remove_privs(file); | |
369 | if (ret) | |
370 | return ret; | |
371 | ||
372 | ret = file_update_time(file); | |
373 | if (ret) | |
374 | return ret; | |
375 | ||
376 | return netfs_perform_write(iocb, from, netfs_group); | |
377 | } | |
378 | EXPORT_SYMBOL(netfs_buffered_write_iter_locked); | |
379 | ||
380 | /** | |
381 | * netfs_file_write_iter - write data to a file | |
382 | * @iocb: IO state structure | |
383 | * @from: iov_iter with data to write | |
384 | * | |
385 | * Perform a write to a file, writing into the pagecache if possible and doing | |
386 | * an unbuffered write instead if not. | |
387 | * | |
388 | * Return: | |
389 | * * Negative error code if no data has been written at all of | |
390 | * vfs_fsync_range() failed for a synchronous write | |
391 | * * Number of bytes written, even for truncated writes | |
392 | */ | |
393 | ssize_t netfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from) | |
394 | { | |
395 | struct file *file = iocb->ki_filp; | |
396 | struct inode *inode = file->f_mapping->host; | |
397 | struct netfs_inode *ictx = netfs_inode(inode); | |
398 | ssize_t ret; | |
399 | ||
400 | _enter("%llx,%zx,%llx", iocb->ki_pos, iov_iter_count(from), i_size_read(inode)); | |
401 | ||
402 | if ((iocb->ki_flags & IOCB_DIRECT) || | |
403 | test_bit(NETFS_ICTX_UNBUFFERED, &ictx->flags)) | |
404 | return netfs_unbuffered_write_iter(iocb, from); | |
405 | ||
406 | ret = netfs_start_io_write(inode); | |
407 | if (ret < 0) | |
408 | return ret; | |
409 | ||
410 | ret = generic_write_checks(iocb, from); | |
411 | if (ret > 0) | |
412 | ret = netfs_buffered_write_iter_locked(iocb, from, NULL); | |
413 | netfs_end_io_write(inode); | |
414 | if (ret > 0) | |
415 | ret = generic_write_sync(iocb, ret); | |
416 | return ret; | |
417 | } | |
418 | EXPORT_SYMBOL(netfs_file_write_iter); |