xref: /linux/fs/netfs/write_issue.c (revision c94cd9508b1335b949fd13ebd269313c65492df0)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Network filesystem high-level (buffered) writeback.
3  *
4  * Copyright (C) 2024 Red Hat, Inc. All Rights Reserved.
5  * Written by David Howells (dhowells@redhat.com)
6  *
7  *
8  * To support network filesystems with local caching, we manage a situation
9  * that can be envisioned like the following:
10  *
11  *               +---+---+-----+-----+---+----------+
12  *    Folios:    |   |   |     |     |   |          |
13  *               +---+---+-----+-----+---+----------+
14  *
15  *                 +------+------+     +----+----+
16  *    Upload:      |      |      |.....|    |    |
17  *  (Stream 0)     +------+------+     +----+----+
18  *
19  *               +------+------+------+------+------+
20  *    Cache:     |      |      |      |      |      |
21  *  (Stream 1)   +------+------+------+------+------+
22  *
23  * Where we have a sequence of folios of varying sizes that we need to overlay
24  * with multiple parallel streams of I/O requests, where the I/O requests in a
25  * stream may also be of various sizes (in cifs, for example, the sizes are
26  * negotiated with the server; in something like ceph, they may represent the
27  * sizes of storage objects).
28  *
29  * The sequence in each stream may contain gaps and noncontiguous subrequests
30  * may be glued together into single vectored write RPCs.
31  */
32 
33 #include <linux/export.h>
34 #include <linux/fs.h>
35 #include <linux/mm.h>
36 #include <linux/pagemap.h>
37 #include "internal.h"
38 
39 /*
40  * Kill all dirty folios in the event of an unrecoverable error, starting with
41  * a locked folio we've already obtained from writeback_iter().
42  */
43 static void netfs_kill_dirty_pages(struct address_space *mapping,
44 				   struct writeback_control *wbc,
45 				   struct folio *folio)
46 {
47 	int error = 0;
48 
49 	do {
50 		enum netfs_folio_trace why = netfs_folio_trace_kill;
51 		struct netfs_group *group = NULL;
52 		struct netfs_folio *finfo = NULL;
53 		void *priv;
54 
55 		priv = folio_detach_private(folio);
56 		if (priv) {
57 			finfo = __netfs_folio_info(priv);
58 			if (finfo) {
59 				/* Kill folio from streaming write. */
60 				group = finfo->netfs_group;
61 				why = netfs_folio_trace_kill_s;
62 			} else {
63 				group = priv;
64 				if (group == NETFS_FOLIO_COPY_TO_CACHE) {
65 					/* Kill copy-to-cache folio */
66 					why = netfs_folio_trace_kill_cc;
67 					group = NULL;
68 				} else {
69 					/* Kill folio with group */
70 					why = netfs_folio_trace_kill_g;
71 				}
72 			}
73 		}
74 
75 		trace_netfs_folio(folio, why);
76 
77 		folio_start_writeback(folio);
78 		folio_unlock(folio);
79 		folio_end_writeback(folio);
80 
81 		netfs_put_group(group);
82 		kfree(finfo);
83 
84 	} while ((folio = writeback_iter(mapping, wbc, folio, &error)));
85 }
86 
87 /*
88  * Create a write request and set it up appropriately for the origin type.
89  */
90 struct netfs_io_request *netfs_create_write_req(struct address_space *mapping,
91 						struct file *file,
92 						loff_t start,
93 						enum netfs_io_origin origin)
94 {
95 	struct netfs_io_request *wreq;
96 	struct netfs_inode *ictx;
97 	bool is_buffered = (origin == NETFS_WRITEBACK ||
98 			    origin == NETFS_WRITETHROUGH ||
99 			    origin == NETFS_PGPRIV2_COPY_TO_CACHE);
100 
101 	wreq = netfs_alloc_request(mapping, file, start, 0, origin);
102 	if (IS_ERR(wreq))
103 		return wreq;
104 
105 	_enter("R=%x", wreq->debug_id);
106 
107 	ictx = netfs_inode(wreq->inode);
108 	if (is_buffered && netfs_is_cache_enabled(ictx))
109 		fscache_begin_write_operation(&wreq->cache_resources, netfs_i_cookie(ictx));
110 
111 	wreq->cleaned_to = wreq->start;
112 
113 	wreq->io_streams[0].stream_nr		= 0;
114 	wreq->io_streams[0].source		= NETFS_UPLOAD_TO_SERVER;
115 	wreq->io_streams[0].prepare_write	= ictx->ops->prepare_write;
116 	wreq->io_streams[0].issue_write		= ictx->ops->issue_write;
117 	wreq->io_streams[0].collected_to	= start;
118 	wreq->io_streams[0].transferred		= LONG_MAX;
119 
120 	wreq->io_streams[1].stream_nr		= 1;
121 	wreq->io_streams[1].source		= NETFS_WRITE_TO_CACHE;
122 	wreq->io_streams[1].collected_to	= start;
123 	wreq->io_streams[1].transferred		= LONG_MAX;
124 	if (fscache_resources_valid(&wreq->cache_resources)) {
125 		wreq->io_streams[1].avail	= true;
126 		wreq->io_streams[1].active	= true;
127 		wreq->io_streams[1].prepare_write = wreq->cache_resources.ops->prepare_write_subreq;
128 		wreq->io_streams[1].issue_write = wreq->cache_resources.ops->issue_write;
129 	}
130 
131 	return wreq;
132 }
133 
134 /**
135  * netfs_prepare_write_failed - Note write preparation failed
136  * @subreq: The subrequest to mark
137  *
138  * Mark a subrequest to note that preparation for write failed.
139  */
140 void netfs_prepare_write_failed(struct netfs_io_subrequest *subreq)
141 {
142 	__set_bit(NETFS_SREQ_FAILED, &subreq->flags);
143 	trace_netfs_sreq(subreq, netfs_sreq_trace_prep_failed);
144 }
145 EXPORT_SYMBOL(netfs_prepare_write_failed);
146 
147 /*
148  * Prepare a write subrequest.  We need to allocate a new subrequest
149  * if we don't have one.
150  */
151 static void netfs_prepare_write(struct netfs_io_request *wreq,
152 				struct netfs_io_stream *stream,
153 				loff_t start)
154 {
155 	struct netfs_io_subrequest *subreq;
156 
157 	subreq = netfs_alloc_subrequest(wreq);
158 	subreq->source		= stream->source;
159 	subreq->start		= start;
160 	subreq->stream_nr	= stream->stream_nr;
161 	subreq->io_iter		= wreq->io_iter;
162 
163 	_enter("R=%x[%x]", wreq->debug_id, subreq->debug_index);
164 
165 	trace_netfs_sreq(subreq, netfs_sreq_trace_prepare);
166 
167 	stream->sreq_max_len	= UINT_MAX;
168 	stream->sreq_max_segs	= INT_MAX;
169 	switch (stream->source) {
170 	case NETFS_UPLOAD_TO_SERVER:
171 		netfs_stat(&netfs_n_wh_upload);
172 		stream->sreq_max_len = wreq->wsize;
173 		break;
174 	case NETFS_WRITE_TO_CACHE:
175 		netfs_stat(&netfs_n_wh_write);
176 		break;
177 	default:
178 		WARN_ON_ONCE(1);
179 		break;
180 	}
181 
182 	if (stream->prepare_write)
183 		stream->prepare_write(subreq);
184 
185 	__set_bit(NETFS_SREQ_IN_PROGRESS, &subreq->flags);
186 
187 	/* We add to the end of the list whilst the collector may be walking
188 	 * the list.  The collector only goes nextwards and uses the lock to
189 	 * remove entries off of the front.
190 	 */
191 	spin_lock_bh(&wreq->lock);
192 	list_add_tail(&subreq->rreq_link, &stream->subrequests);
193 	if (list_is_first(&subreq->rreq_link, &stream->subrequests)) {
194 		stream->front = subreq;
195 		if (!stream->active) {
196 			stream->collected_to = stream->front->start;
197 			/* Write list pointers before active flag */
198 			smp_store_release(&stream->active, true);
199 		}
200 	}
201 
202 	spin_unlock_bh(&wreq->lock);
203 
204 	stream->construct = subreq;
205 }
206 
207 /*
208  * Set the I/O iterator for the filesystem/cache to use and dispatch the I/O
209  * operation.  The operation may be asynchronous and should call
210  * netfs_write_subrequest_terminated() when complete.
211  */
212 static void netfs_do_issue_write(struct netfs_io_stream *stream,
213 				 struct netfs_io_subrequest *subreq)
214 {
215 	struct netfs_io_request *wreq = subreq->rreq;
216 
217 	_enter("R=%x[%x],%zx", wreq->debug_id, subreq->debug_index, subreq->len);
218 
219 	if (test_bit(NETFS_SREQ_FAILED, &subreq->flags))
220 		return netfs_write_subrequest_terminated(subreq, subreq->error, false);
221 
222 	trace_netfs_sreq(subreq, netfs_sreq_trace_submit);
223 	stream->issue_write(subreq);
224 }
225 
226 void netfs_reissue_write(struct netfs_io_stream *stream,
227 			 struct netfs_io_subrequest *subreq,
228 			 struct iov_iter *source)
229 {
230 	size_t size = subreq->len - subreq->transferred;
231 
232 	// TODO: Use encrypted buffer
233 	subreq->io_iter = *source;
234 	iov_iter_advance(source, size);
235 	iov_iter_truncate(&subreq->io_iter, size);
236 
237 	__set_bit(NETFS_SREQ_IN_PROGRESS, &subreq->flags);
238 	netfs_do_issue_write(stream, subreq);
239 }
240 
241 void netfs_issue_write(struct netfs_io_request *wreq,
242 		       struct netfs_io_stream *stream)
243 {
244 	struct netfs_io_subrequest *subreq = stream->construct;
245 
246 	if (!subreq)
247 		return;
248 	stream->construct = NULL;
249 	subreq->io_iter.count = subreq->len;
250 	netfs_do_issue_write(stream, subreq);
251 }
252 
253 /*
254  * Add data to the write subrequest, dispatching each as we fill it up or if it
255  * is discontiguous with the previous.  We only fill one part at a time so that
256  * we can avoid overrunning the credits obtained (cifs) and try to parallelise
257  * content-crypto preparation with network writes.
258  */
259 int netfs_advance_write(struct netfs_io_request *wreq,
260 			struct netfs_io_stream *stream,
261 			loff_t start, size_t len, bool to_eof)
262 {
263 	struct netfs_io_subrequest *subreq = stream->construct;
264 	size_t part;
265 
266 	if (!stream->avail) {
267 		_leave("no write");
268 		return len;
269 	}
270 
271 	_enter("R=%x[%x]", wreq->debug_id, subreq ? subreq->debug_index : 0);
272 
273 	if (subreq && start != subreq->start + subreq->len) {
274 		netfs_issue_write(wreq, stream);
275 		subreq = NULL;
276 	}
277 
278 	if (!stream->construct)
279 		netfs_prepare_write(wreq, stream, start);
280 	subreq = stream->construct;
281 
282 	part = umin(stream->sreq_max_len - subreq->len, len);
283 	_debug("part %zx/%zx %zx/%zx", subreq->len, stream->sreq_max_len, part, len);
284 	subreq->len += part;
285 	subreq->nr_segs++;
286 	stream->submit_extendable_to -= part;
287 
288 	if (subreq->len >= stream->sreq_max_len ||
289 	    subreq->nr_segs >= stream->sreq_max_segs ||
290 	    to_eof) {
291 		netfs_issue_write(wreq, stream);
292 		subreq = NULL;
293 	}
294 
295 	return part;
296 }
297 
298 /*
299  * Write some of a pending folio data back to the server.
300  */
301 static int netfs_write_folio(struct netfs_io_request *wreq,
302 			     struct writeback_control *wbc,
303 			     struct folio *folio)
304 {
305 	struct netfs_io_stream *upload = &wreq->io_streams[0];
306 	struct netfs_io_stream *cache  = &wreq->io_streams[1];
307 	struct netfs_io_stream *stream;
308 	struct netfs_group *fgroup; /* TODO: Use this with ceph */
309 	struct netfs_folio *finfo;
310 	size_t fsize = folio_size(folio), flen = fsize, foff = 0;
311 	loff_t fpos = folio_pos(folio), i_size;
312 	bool to_eof = false, streamw = false;
313 	bool debug = false;
314 
315 	_enter("");
316 
317 	/* netfs_perform_write() may shift i_size around the page or from out
318 	 * of the page to beyond it, but cannot move i_size into or through the
319 	 * page since we have it locked.
320 	 */
321 	i_size = i_size_read(wreq->inode);
322 
323 	if (fpos >= i_size) {
324 		/* mmap beyond eof. */
325 		_debug("beyond eof");
326 		folio_start_writeback(folio);
327 		folio_unlock(folio);
328 		wreq->nr_group_rel += netfs_folio_written_back(folio);
329 		netfs_put_group_many(wreq->group, wreq->nr_group_rel);
330 		wreq->nr_group_rel = 0;
331 		return 0;
332 	}
333 
334 	if (fpos + fsize > wreq->i_size)
335 		wreq->i_size = i_size;
336 
337 	fgroup = netfs_folio_group(folio);
338 	finfo = netfs_folio_info(folio);
339 	if (finfo) {
340 		foff = finfo->dirty_offset;
341 		flen = foff + finfo->dirty_len;
342 		streamw = true;
343 	}
344 
345 	if (wreq->origin == NETFS_WRITETHROUGH) {
346 		to_eof = false;
347 		if (flen > i_size - fpos)
348 			flen = i_size - fpos;
349 	} else if (flen > i_size - fpos) {
350 		flen = i_size - fpos;
351 		if (!streamw)
352 			folio_zero_segment(folio, flen, fsize);
353 		to_eof = true;
354 	} else if (flen == i_size - fpos) {
355 		to_eof = true;
356 	}
357 	flen -= foff;
358 
359 	_debug("folio %zx %zx %zx", foff, flen, fsize);
360 
361 	/* Deal with discontinuities in the stream of dirty pages.  These can
362 	 * arise from a number of sources:
363 	 *
364 	 * (1) Intervening non-dirty pages from random-access writes, multiple
365 	 *     flushers writing back different parts simultaneously and manual
366 	 *     syncing.
367 	 *
368 	 * (2) Partially-written pages from write-streaming.
369 	 *
370 	 * (3) Pages that belong to a different write-back group (eg.  Ceph
371 	 *     snapshots).
372 	 *
373 	 * (4) Actually-clean pages that were marked for write to the cache
374 	 *     when they were read.  Note that these appear as a special
375 	 *     write-back group.
376 	 */
377 	if (fgroup == NETFS_FOLIO_COPY_TO_CACHE) {
378 		netfs_issue_write(wreq, upload);
379 	} else if (fgroup != wreq->group) {
380 		/* We can't write this page to the server yet. */
381 		kdebug("wrong group");
382 		folio_redirty_for_writepage(wbc, folio);
383 		folio_unlock(folio);
384 		netfs_issue_write(wreq, upload);
385 		netfs_issue_write(wreq, cache);
386 		return 0;
387 	}
388 
389 	if (foff > 0)
390 		netfs_issue_write(wreq, upload);
391 	if (streamw)
392 		netfs_issue_write(wreq, cache);
393 
394 	/* Flip the page to the writeback state and unlock.  If we're called
395 	 * from write-through, then the page has already been put into the wb
396 	 * state.
397 	 */
398 	if (wreq->origin == NETFS_WRITEBACK)
399 		folio_start_writeback(folio);
400 	folio_unlock(folio);
401 
402 	if (fgroup == NETFS_FOLIO_COPY_TO_CACHE) {
403 		if (!cache->avail) {
404 			trace_netfs_folio(folio, netfs_folio_trace_cancel_copy);
405 			netfs_issue_write(wreq, upload);
406 			netfs_folio_written_back(folio);
407 			return 0;
408 		}
409 		trace_netfs_folio(folio, netfs_folio_trace_store_copy);
410 	} else if (!upload->avail && !cache->avail) {
411 		trace_netfs_folio(folio, netfs_folio_trace_cancel_store);
412 		netfs_folio_written_back(folio);
413 		return 0;
414 	} else if (!upload->construct) {
415 		trace_netfs_folio(folio, netfs_folio_trace_store);
416 	} else {
417 		trace_netfs_folio(folio, netfs_folio_trace_store_plus);
418 	}
419 
420 	/* Attach the folio to the rolling buffer. */
421 	netfs_buffer_append_folio(wreq, folio, false);
422 
423 	/* Move the submission point forward to allow for write-streaming data
424 	 * not starting at the front of the page.  We don't do write-streaming
425 	 * with the cache as the cache requires DIO alignment.
426 	 *
427 	 * Also skip uploading for data that's been read and just needs copying
428 	 * to the cache.
429 	 */
430 	for (int s = 0; s < NR_IO_STREAMS; s++) {
431 		stream = &wreq->io_streams[s];
432 		stream->submit_off = foff;
433 		stream->submit_len = flen;
434 		if ((stream->source == NETFS_WRITE_TO_CACHE && streamw) ||
435 		    (stream->source == NETFS_UPLOAD_TO_SERVER &&
436 		     fgroup == NETFS_FOLIO_COPY_TO_CACHE)) {
437 			stream->submit_off = UINT_MAX;
438 			stream->submit_len = 0;
439 		}
440 	}
441 
442 	/* Attach the folio to one or more subrequests.  For a big folio, we
443 	 * could end up with thousands of subrequests if the wsize is small -
444 	 * but we might need to wait during the creation of subrequests for
445 	 * network resources (eg. SMB credits).
446 	 */
447 	for (;;) {
448 		ssize_t part;
449 		size_t lowest_off = ULONG_MAX;
450 		int choose_s = -1;
451 
452 		/* Always add to the lowest-submitted stream first. */
453 		for (int s = 0; s < NR_IO_STREAMS; s++) {
454 			stream = &wreq->io_streams[s];
455 			if (stream->submit_len > 0 &&
456 			    stream->submit_off < lowest_off) {
457 				lowest_off = stream->submit_off;
458 				choose_s = s;
459 			}
460 		}
461 
462 		if (choose_s < 0)
463 			break;
464 		stream = &wreq->io_streams[choose_s];
465 		wreq->io_iter.iov_offset = stream->submit_off;
466 
467 		atomic64_set(&wreq->issued_to, fpos + stream->submit_off);
468 		stream->submit_extendable_to = fsize - stream->submit_off;
469 		part = netfs_advance_write(wreq, stream, fpos + stream->submit_off,
470 					   stream->submit_len, to_eof);
471 		stream->submit_off += part;
472 		if (part > stream->submit_len)
473 			stream->submit_len = 0;
474 		else
475 			stream->submit_len -= part;
476 		if (part > 0)
477 			debug = true;
478 	}
479 
480 	wreq->io_iter.iov_offset = 0;
481 	iov_iter_advance(&wreq->io_iter, fsize);
482 	atomic64_set(&wreq->issued_to, fpos + fsize);
483 
484 	if (!debug)
485 		kdebug("R=%x: No submit", wreq->debug_id);
486 
487 	if (foff + flen < fsize)
488 		for (int s = 0; s < NR_IO_STREAMS; s++)
489 			netfs_issue_write(wreq, &wreq->io_streams[s]);
490 
491 	_leave(" = 0");
492 	return 0;
493 }
494 
495 /*
496  * Write some of the pending data back to the server
497  */
498 int netfs_writepages(struct address_space *mapping,
499 		     struct writeback_control *wbc)
500 {
501 	struct netfs_inode *ictx = netfs_inode(mapping->host);
502 	struct netfs_io_request *wreq = NULL;
503 	struct folio *folio;
504 	int error = 0;
505 
506 	if (!mutex_trylock(&ictx->wb_lock)) {
507 		if (wbc->sync_mode == WB_SYNC_NONE) {
508 			netfs_stat(&netfs_n_wb_lock_skip);
509 			return 0;
510 		}
511 		netfs_stat(&netfs_n_wb_lock_wait);
512 		mutex_lock(&ictx->wb_lock);
513 	}
514 
515 	/* Need the first folio to be able to set up the op. */
516 	folio = writeback_iter(mapping, wbc, NULL, &error);
517 	if (!folio)
518 		goto out;
519 
520 	wreq = netfs_create_write_req(mapping, NULL, folio_pos(folio), NETFS_WRITEBACK);
521 	if (IS_ERR(wreq)) {
522 		error = PTR_ERR(wreq);
523 		goto couldnt_start;
524 	}
525 
526 	trace_netfs_write(wreq, netfs_write_trace_writeback);
527 	netfs_stat(&netfs_n_wh_writepages);
528 
529 	do {
530 		_debug("wbiter %lx %llx", folio->index, atomic64_read(&wreq->issued_to));
531 
532 		/* It appears we don't have to handle cyclic writeback wrapping. */
533 		WARN_ON_ONCE(wreq && folio_pos(folio) < atomic64_read(&wreq->issued_to));
534 
535 		if (netfs_folio_group(folio) != NETFS_FOLIO_COPY_TO_CACHE &&
536 		    unlikely(!test_bit(NETFS_RREQ_UPLOAD_TO_SERVER, &wreq->flags))) {
537 			set_bit(NETFS_RREQ_UPLOAD_TO_SERVER, &wreq->flags);
538 			wreq->netfs_ops->begin_writeback(wreq);
539 		}
540 
541 		error = netfs_write_folio(wreq, wbc, folio);
542 		if (error < 0)
543 			break;
544 	} while ((folio = writeback_iter(mapping, wbc, folio, &error)));
545 
546 	for (int s = 0; s < NR_IO_STREAMS; s++)
547 		netfs_issue_write(wreq, &wreq->io_streams[s]);
548 	smp_wmb(); /* Write lists before ALL_QUEUED. */
549 	set_bit(NETFS_RREQ_ALL_QUEUED, &wreq->flags);
550 
551 	mutex_unlock(&ictx->wb_lock);
552 
553 	netfs_put_request(wreq, false, netfs_rreq_trace_put_return);
554 	_leave(" = %d", error);
555 	return error;
556 
557 couldnt_start:
558 	netfs_kill_dirty_pages(mapping, wbc, folio);
559 out:
560 	mutex_unlock(&ictx->wb_lock);
561 	_leave(" = %d", error);
562 	return error;
563 }
564 EXPORT_SYMBOL(netfs_writepages);
565 
566 /*
567  * Begin a write operation for writing through the pagecache.
568  */
569 struct netfs_io_request *netfs_begin_writethrough(struct kiocb *iocb, size_t len)
570 {
571 	struct netfs_io_request *wreq = NULL;
572 	struct netfs_inode *ictx = netfs_inode(file_inode(iocb->ki_filp));
573 
574 	mutex_lock(&ictx->wb_lock);
575 
576 	wreq = netfs_create_write_req(iocb->ki_filp->f_mapping, iocb->ki_filp,
577 				      iocb->ki_pos, NETFS_WRITETHROUGH);
578 	if (IS_ERR(wreq)) {
579 		mutex_unlock(&ictx->wb_lock);
580 		return wreq;
581 	}
582 
583 	wreq->io_streams[0].avail = true;
584 	trace_netfs_write(wreq, netfs_write_trace_writethrough);
585 	return wreq;
586 }
587 
588 /*
589  * Advance the state of the write operation used when writing through the
590  * pagecache.  Data has been copied into the pagecache that we need to append
591  * to the request.  If we've added more than wsize then we need to create a new
592  * subrequest.
593  */
594 int netfs_advance_writethrough(struct netfs_io_request *wreq, struct writeback_control *wbc,
595 			       struct folio *folio, size_t copied, bool to_page_end,
596 			       struct folio **writethrough_cache)
597 {
598 	_enter("R=%x ic=%zu ws=%u cp=%zu tp=%u",
599 	       wreq->debug_id, wreq->iter.count, wreq->wsize, copied, to_page_end);
600 
601 	if (!*writethrough_cache) {
602 		if (folio_test_dirty(folio))
603 			/* Sigh.  mmap. */
604 			folio_clear_dirty_for_io(folio);
605 
606 		/* We can make multiple writes to the folio... */
607 		folio_start_writeback(folio);
608 		if (wreq->len == 0)
609 			trace_netfs_folio(folio, netfs_folio_trace_wthru);
610 		else
611 			trace_netfs_folio(folio, netfs_folio_trace_wthru_plus);
612 		*writethrough_cache = folio;
613 	}
614 
615 	wreq->len += copied;
616 	if (!to_page_end)
617 		return 0;
618 
619 	*writethrough_cache = NULL;
620 	return netfs_write_folio(wreq, wbc, folio);
621 }
622 
623 /*
624  * End a write operation used when writing through the pagecache.
625  */
626 int netfs_end_writethrough(struct netfs_io_request *wreq, struct writeback_control *wbc,
627 			   struct folio *writethrough_cache)
628 {
629 	struct netfs_inode *ictx = netfs_inode(wreq->inode);
630 	int ret;
631 
632 	_enter("R=%x", wreq->debug_id);
633 
634 	if (writethrough_cache)
635 		netfs_write_folio(wreq, wbc, writethrough_cache);
636 
637 	netfs_issue_write(wreq, &wreq->io_streams[0]);
638 	netfs_issue_write(wreq, &wreq->io_streams[1]);
639 	smp_wmb(); /* Write lists before ALL_QUEUED. */
640 	set_bit(NETFS_RREQ_ALL_QUEUED, &wreq->flags);
641 
642 	mutex_unlock(&ictx->wb_lock);
643 
644 	if (wreq->iocb) {
645 		ret = -EIOCBQUEUED;
646 	} else {
647 		wait_on_bit(&wreq->flags, NETFS_RREQ_IN_PROGRESS, TASK_UNINTERRUPTIBLE);
648 		ret = wreq->error;
649 	}
650 	netfs_put_request(wreq, false, netfs_rreq_trace_put_return);
651 	return ret;
652 }
653 
654 /*
655  * Write data to the server without going through the pagecache and without
656  * writing it to the local cache.
657  */
658 int netfs_unbuffered_write(struct netfs_io_request *wreq, bool may_wait, size_t len)
659 {
660 	struct netfs_io_stream *upload = &wreq->io_streams[0];
661 	ssize_t part;
662 	loff_t start = wreq->start;
663 	int error = 0;
664 
665 	_enter("%zx", len);
666 
667 	if (wreq->origin == NETFS_DIO_WRITE)
668 		inode_dio_begin(wreq->inode);
669 
670 	while (len) {
671 		// TODO: Prepare content encryption
672 
673 		_debug("unbuffered %zx", len);
674 		part = netfs_advance_write(wreq, upload, start, len, false);
675 		start += part;
676 		len -= part;
677 		iov_iter_advance(&wreq->io_iter, part);
678 		if (test_bit(NETFS_RREQ_PAUSE, &wreq->flags)) {
679 			trace_netfs_rreq(wreq, netfs_rreq_trace_wait_pause);
680 			wait_on_bit(&wreq->flags, NETFS_RREQ_PAUSE, TASK_UNINTERRUPTIBLE);
681 		}
682 		if (test_bit(NETFS_RREQ_FAILED, &wreq->flags))
683 			break;
684 	}
685 
686 	netfs_issue_write(wreq, upload);
687 
688 	smp_wmb(); /* Write lists before ALL_QUEUED. */
689 	set_bit(NETFS_RREQ_ALL_QUEUED, &wreq->flags);
690 	if (list_empty(&upload->subrequests))
691 		netfs_wake_write_collector(wreq, false);
692 
693 	_leave(" = %d", error);
694 	return error;
695 }
696