write_issue.c 20 KB

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