1
0

write_issue.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891
  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_cacheable = (origin == NETFS_WRITEBACK ||
  89. origin == NETFS_WRITEBACK_SINGLE ||
  90. origin == NETFS_WRITETHROUGH ||
  91. origin == NETFS_PGPRIV2_COPY_TO_CACHE);
  92. wreq = netfs_alloc_request(mapping, file, start, 0, origin);
  93. if (IS_ERR(wreq))
  94. return wreq;
  95. _enter("R=%x", wreq->debug_id);
  96. ictx = netfs_inode(wreq->inode);
  97. if (is_cacheable && netfs_is_cache_enabled(ictx))
  98. fscache_begin_write_operation(&wreq->cache_resources, netfs_i_cookie(ictx));
  99. if (rolling_buffer_init(&wreq->buffer, wreq->debug_id, ITER_SOURCE) < 0)
  100. goto nomem;
  101. wreq->cleaned_to = wreq->start;
  102. wreq->io_streams[0].stream_nr = 0;
  103. wreq->io_streams[0].source = NETFS_UPLOAD_TO_SERVER;
  104. wreq->io_streams[0].prepare_write = ictx->ops->prepare_write;
  105. wreq->io_streams[0].issue_write = ictx->ops->issue_write;
  106. wreq->io_streams[0].collected_to = start;
  107. wreq->io_streams[0].transferred = 0;
  108. wreq->io_streams[1].stream_nr = 1;
  109. wreq->io_streams[1].source = NETFS_WRITE_TO_CACHE;
  110. wreq->io_streams[1].collected_to = start;
  111. wreq->io_streams[1].transferred = 0;
  112. if (fscache_resources_valid(&wreq->cache_resources)) {
  113. wreq->io_streams[1].avail = true;
  114. wreq->io_streams[1].active = true;
  115. wreq->io_streams[1].prepare_write = wreq->cache_resources.ops->prepare_write_subreq;
  116. wreq->io_streams[1].issue_write = wreq->cache_resources.ops->issue_write;
  117. }
  118. return wreq;
  119. nomem:
  120. netfs_put_failed_request(wreq);
  121. return ERR_PTR(-ENOMEM);
  122. }
  123. /**
  124. * netfs_prepare_write_failed - Note write preparation failed
  125. * @subreq: The subrequest to mark
  126. *
  127. * Mark a subrequest to note that preparation for write failed.
  128. */
  129. void netfs_prepare_write_failed(struct netfs_io_subrequest *subreq)
  130. {
  131. __set_bit(NETFS_SREQ_FAILED, &subreq->flags);
  132. trace_netfs_sreq(subreq, netfs_sreq_trace_prep_failed);
  133. }
  134. EXPORT_SYMBOL(netfs_prepare_write_failed);
  135. /*
  136. * Prepare a write subrequest. We need to allocate a new subrequest
  137. * if we don't have one.
  138. */
  139. void netfs_prepare_write(struct netfs_io_request *wreq,
  140. struct netfs_io_stream *stream,
  141. loff_t start)
  142. {
  143. struct netfs_io_subrequest *subreq;
  144. struct iov_iter *wreq_iter = &wreq->buffer.iter;
  145. /* Make sure we don't point the iterator at a used-up folio_queue
  146. * struct being used as a placeholder to prevent the queue from
  147. * collapsing. In such a case, extend the queue.
  148. */
  149. if (iov_iter_is_folioq(wreq_iter) &&
  150. wreq_iter->folioq_slot >= folioq_nr_slots(wreq_iter->folioq))
  151. rolling_buffer_make_space(&wreq->buffer);
  152. subreq = netfs_alloc_subrequest(wreq);
  153. subreq->source = stream->source;
  154. subreq->start = start;
  155. subreq->stream_nr = stream->stream_nr;
  156. subreq->io_iter = *wreq_iter;
  157. _enter("R=%x[%x]", wreq->debug_id, subreq->debug_index);
  158. trace_netfs_sreq(subreq, netfs_sreq_trace_prepare);
  159. stream->sreq_max_len = UINT_MAX;
  160. stream->sreq_max_segs = INT_MAX;
  161. switch (stream->source) {
  162. case NETFS_UPLOAD_TO_SERVER:
  163. netfs_stat(&netfs_n_wh_upload);
  164. stream->sreq_max_len = wreq->wsize;
  165. break;
  166. case NETFS_WRITE_TO_CACHE:
  167. netfs_stat(&netfs_n_wh_write);
  168. break;
  169. default:
  170. WARN_ON_ONCE(1);
  171. break;
  172. }
  173. if (stream->prepare_write)
  174. stream->prepare_write(subreq);
  175. __set_bit(NETFS_SREQ_IN_PROGRESS, &subreq->flags);
  176. /* We add to the end of the list whilst the collector may be walking
  177. * the list. The collector only goes nextwards and uses the lock to
  178. * remove entries off of the front.
  179. */
  180. spin_lock(&wreq->lock);
  181. list_add_tail(&subreq->rreq_link, &stream->subrequests);
  182. if (list_is_first(&subreq->rreq_link, &stream->subrequests)) {
  183. if (!stream->active) {
  184. stream->collected_to = subreq->start;
  185. /* Write list pointers before active flag */
  186. smp_store_release(&stream->active, true);
  187. }
  188. }
  189. spin_unlock(&wreq->lock);
  190. stream->construct = subreq;
  191. }
  192. /*
  193. * Set the I/O iterator for the filesystem/cache to use and dispatch the I/O
  194. * operation. The operation may be asynchronous and should call
  195. * netfs_write_subrequest_terminated() when complete.
  196. */
  197. static void netfs_do_issue_write(struct netfs_io_stream *stream,
  198. struct netfs_io_subrequest *subreq)
  199. {
  200. struct netfs_io_request *wreq = subreq->rreq;
  201. _enter("R=%x[%x],%zx", wreq->debug_id, subreq->debug_index, subreq->len);
  202. if (test_bit(NETFS_SREQ_FAILED, &subreq->flags))
  203. return netfs_write_subrequest_terminated(subreq, subreq->error);
  204. trace_netfs_sreq(subreq, netfs_sreq_trace_submit);
  205. stream->issue_write(subreq);
  206. }
  207. void netfs_reissue_write(struct netfs_io_stream *stream,
  208. struct netfs_io_subrequest *subreq,
  209. struct iov_iter *source)
  210. {
  211. size_t size = subreq->len - subreq->transferred;
  212. // TODO: Use encrypted buffer
  213. subreq->io_iter = *source;
  214. iov_iter_advance(source, size);
  215. iov_iter_truncate(&subreq->io_iter, size);
  216. subreq->retry_count++;
  217. subreq->error = 0;
  218. __clear_bit(NETFS_SREQ_MADE_PROGRESS, &subreq->flags);
  219. __set_bit(NETFS_SREQ_IN_PROGRESS, &subreq->flags);
  220. netfs_stat(&netfs_n_wh_retry_write_subreq);
  221. netfs_do_issue_write(stream, subreq);
  222. }
  223. void netfs_issue_write(struct netfs_io_request *wreq,
  224. struct netfs_io_stream *stream)
  225. {
  226. struct netfs_io_subrequest *subreq = stream->construct;
  227. if (!subreq)
  228. return;
  229. stream->construct = NULL;
  230. subreq->io_iter.count = subreq->len;
  231. netfs_do_issue_write(stream, subreq);
  232. }
  233. /*
  234. * Add data to the write subrequest, dispatching each as we fill it up or if it
  235. * is discontiguous with the previous. We only fill one part at a time so that
  236. * we can avoid overrunning the credits obtained (cifs) and try to parallelise
  237. * content-crypto preparation with network writes.
  238. */
  239. size_t netfs_advance_write(struct netfs_io_request *wreq,
  240. struct netfs_io_stream *stream,
  241. loff_t start, size_t len, bool to_eof)
  242. {
  243. struct netfs_io_subrequest *subreq = stream->construct;
  244. size_t part;
  245. if (!stream->avail) {
  246. _leave("no write");
  247. return len;
  248. }
  249. _enter("R=%x[%x]", wreq->debug_id, subreq ? subreq->debug_index : 0);
  250. if (subreq && start != subreq->start + subreq->len) {
  251. netfs_issue_write(wreq, stream);
  252. subreq = NULL;
  253. }
  254. if (!stream->construct)
  255. netfs_prepare_write(wreq, stream, start);
  256. subreq = stream->construct;
  257. part = umin(stream->sreq_max_len - subreq->len, len);
  258. _debug("part %zx/%zx %zx/%zx", subreq->len, stream->sreq_max_len, part, len);
  259. subreq->len += part;
  260. subreq->nr_segs++;
  261. stream->submit_extendable_to -= part;
  262. if (subreq->len >= stream->sreq_max_len ||
  263. subreq->nr_segs >= stream->sreq_max_segs ||
  264. to_eof) {
  265. netfs_issue_write(wreq, stream);
  266. subreq = NULL;
  267. }
  268. return part;
  269. }
  270. /*
  271. * Write some of a pending folio data back to the server.
  272. */
  273. static int netfs_write_folio(struct netfs_io_request *wreq,
  274. struct writeback_control *wbc,
  275. struct folio *folio)
  276. {
  277. struct netfs_io_stream *upload = &wreq->io_streams[0];
  278. struct netfs_io_stream *cache = &wreq->io_streams[1];
  279. struct netfs_io_stream *stream;
  280. struct netfs_group *fgroup; /* TODO: Use this with ceph */
  281. struct netfs_folio *finfo;
  282. size_t iter_off = 0;
  283. size_t fsize = folio_size(folio), flen = fsize, foff = 0;
  284. loff_t fpos = folio_pos(folio), i_size;
  285. bool to_eof = false, streamw = false;
  286. bool debug = false;
  287. _enter("");
  288. if (rolling_buffer_make_space(&wreq->buffer) < 0)
  289. return -ENOMEM;
  290. /* netfs_perform_write() may shift i_size around the page or from out
  291. * of the page to beyond it, but cannot move i_size into or through the
  292. * page since we have it locked.
  293. */
  294. i_size = i_size_read(wreq->inode);
  295. if (fpos >= i_size) {
  296. /* mmap beyond eof. */
  297. _debug("beyond eof");
  298. folio_start_writeback(folio);
  299. folio_unlock(folio);
  300. wreq->nr_group_rel += netfs_folio_written_back(folio);
  301. netfs_put_group_many(wreq->group, wreq->nr_group_rel);
  302. wreq->nr_group_rel = 0;
  303. return 0;
  304. }
  305. if (fpos + fsize > wreq->i_size)
  306. wreq->i_size = i_size;
  307. fgroup = netfs_folio_group(folio);
  308. finfo = netfs_folio_info(folio);
  309. if (finfo) {
  310. foff = finfo->dirty_offset;
  311. flen = foff + finfo->dirty_len;
  312. streamw = true;
  313. }
  314. if (wreq->origin == NETFS_WRITETHROUGH) {
  315. to_eof = false;
  316. if (flen > i_size - fpos)
  317. flen = i_size - fpos;
  318. } else if (flen > i_size - fpos) {
  319. flen = i_size - fpos;
  320. if (!streamw)
  321. folio_zero_segment(folio, flen, fsize);
  322. to_eof = true;
  323. } else if (flen == i_size - fpos) {
  324. to_eof = true;
  325. }
  326. flen -= foff;
  327. _debug("folio %zx %zx %zx", foff, flen, fsize);
  328. /* Deal with discontinuities in the stream of dirty pages. These can
  329. * arise from a number of sources:
  330. *
  331. * (1) Intervening non-dirty pages from random-access writes, multiple
  332. * flushers writing back different parts simultaneously and manual
  333. * syncing.
  334. *
  335. * (2) Partially-written pages from write-streaming.
  336. *
  337. * (3) Pages that belong to a different write-back group (eg. Ceph
  338. * snapshots).
  339. *
  340. * (4) Actually-clean pages that were marked for write to the cache
  341. * when they were read. Note that these appear as a special
  342. * write-back group.
  343. */
  344. if (fgroup == NETFS_FOLIO_COPY_TO_CACHE) {
  345. netfs_issue_write(wreq, upload);
  346. } else if (fgroup != wreq->group) {
  347. /* We can't write this page to the server yet. */
  348. kdebug("wrong group");
  349. folio_redirty_for_writepage(wbc, folio);
  350. folio_unlock(folio);
  351. netfs_issue_write(wreq, upload);
  352. netfs_issue_write(wreq, cache);
  353. return 0;
  354. }
  355. if (foff > 0)
  356. netfs_issue_write(wreq, upload);
  357. if (streamw)
  358. netfs_issue_write(wreq, cache);
  359. /* Flip the page to the writeback state and unlock. If we're called
  360. * from write-through, then the page has already been put into the wb
  361. * state.
  362. */
  363. if (wreq->origin == NETFS_WRITEBACK)
  364. folio_start_writeback(folio);
  365. folio_unlock(folio);
  366. if (fgroup == NETFS_FOLIO_COPY_TO_CACHE) {
  367. if (!cache->avail) {
  368. trace_netfs_folio(folio, netfs_folio_trace_cancel_copy);
  369. netfs_issue_write(wreq, upload);
  370. netfs_folio_written_back(folio);
  371. return 0;
  372. }
  373. trace_netfs_folio(folio, netfs_folio_trace_store_copy);
  374. } else if (!upload->avail && !cache->avail) {
  375. trace_netfs_folio(folio, netfs_folio_trace_cancel_store);
  376. netfs_folio_written_back(folio);
  377. return 0;
  378. } else if (!upload->construct) {
  379. trace_netfs_folio(folio, netfs_folio_trace_store);
  380. } else {
  381. trace_netfs_folio(folio, netfs_folio_trace_store_plus);
  382. }
  383. /* Attach the folio to the rolling buffer. */
  384. rolling_buffer_append(&wreq->buffer, folio, 0);
  385. /* Move the submission point forward to allow for write-streaming data
  386. * not starting at the front of the page. We don't do write-streaming
  387. * with the cache as the cache requires DIO alignment.
  388. *
  389. * Also skip uploading for data that's been read and just needs copying
  390. * to the cache.
  391. */
  392. for (int s = 0; s < NR_IO_STREAMS; s++) {
  393. stream = &wreq->io_streams[s];
  394. stream->submit_off = foff;
  395. stream->submit_len = flen;
  396. if (!stream->avail ||
  397. (stream->source == NETFS_WRITE_TO_CACHE && streamw) ||
  398. (stream->source == NETFS_UPLOAD_TO_SERVER &&
  399. fgroup == NETFS_FOLIO_COPY_TO_CACHE)) {
  400. stream->submit_off = UINT_MAX;
  401. stream->submit_len = 0;
  402. }
  403. }
  404. /* Attach the folio to one or more subrequests. For a big folio, we
  405. * could end up with thousands of subrequests if the wsize is small -
  406. * but we might need to wait during the creation of subrequests for
  407. * network resources (eg. SMB credits).
  408. */
  409. for (;;) {
  410. ssize_t part;
  411. size_t lowest_off = ULONG_MAX;
  412. int choose_s = -1;
  413. /* Always add to the lowest-submitted stream first. */
  414. for (int s = 0; s < NR_IO_STREAMS; s++) {
  415. stream = &wreq->io_streams[s];
  416. if (stream->submit_len > 0 &&
  417. stream->submit_off < lowest_off) {
  418. lowest_off = stream->submit_off;
  419. choose_s = s;
  420. }
  421. }
  422. if (choose_s < 0)
  423. break;
  424. stream = &wreq->io_streams[choose_s];
  425. /* Advance the iterator(s). */
  426. if (stream->submit_off > iter_off) {
  427. rolling_buffer_advance(&wreq->buffer, stream->submit_off - iter_off);
  428. iter_off = stream->submit_off;
  429. }
  430. atomic64_set(&wreq->issued_to, fpos + stream->submit_off);
  431. stream->submit_extendable_to = fsize - stream->submit_off;
  432. part = netfs_advance_write(wreq, stream, fpos + stream->submit_off,
  433. stream->submit_len, to_eof);
  434. stream->submit_off += part;
  435. if (part > stream->submit_len)
  436. stream->submit_len = 0;
  437. else
  438. stream->submit_len -= part;
  439. if (part > 0)
  440. debug = true;
  441. }
  442. if (fsize > iter_off)
  443. rolling_buffer_advance(&wreq->buffer, fsize - iter_off);
  444. atomic64_set(&wreq->issued_to, fpos + fsize);
  445. if (!debug)
  446. kdebug("R=%x: No submit", wreq->debug_id);
  447. if (foff + flen < fsize)
  448. for (int s = 0; s < NR_IO_STREAMS; s++)
  449. netfs_issue_write(wreq, &wreq->io_streams[s]);
  450. _leave(" = 0");
  451. return 0;
  452. }
  453. /*
  454. * End the issuing of writes, letting the collector know we're done.
  455. */
  456. static void netfs_end_issue_write(struct netfs_io_request *wreq)
  457. {
  458. bool needs_poke = true;
  459. smp_wmb(); /* Write subreq lists before ALL_QUEUED. */
  460. set_bit(NETFS_RREQ_ALL_QUEUED, &wreq->flags);
  461. for (int s = 0; s < NR_IO_STREAMS; s++) {
  462. struct netfs_io_stream *stream = &wreq->io_streams[s];
  463. if (!stream->active)
  464. continue;
  465. if (!list_empty(&stream->subrequests))
  466. needs_poke = false;
  467. netfs_issue_write(wreq, stream);
  468. }
  469. if (needs_poke)
  470. netfs_wake_collector(wreq);
  471. }
  472. /*
  473. * Write some of the pending data back to the server
  474. */
  475. int netfs_writepages(struct address_space *mapping,
  476. struct writeback_control *wbc)
  477. {
  478. struct netfs_inode *ictx = netfs_inode(mapping->host);
  479. struct netfs_io_request *wreq = NULL;
  480. struct folio *folio;
  481. int error = 0;
  482. if (!mutex_trylock(&ictx->wb_lock)) {
  483. if (wbc->sync_mode == WB_SYNC_NONE) {
  484. netfs_stat(&netfs_n_wb_lock_skip);
  485. return 0;
  486. }
  487. netfs_stat(&netfs_n_wb_lock_wait);
  488. mutex_lock(&ictx->wb_lock);
  489. }
  490. /* Need the first folio to be able to set up the op. */
  491. folio = writeback_iter(mapping, wbc, NULL, &error);
  492. if (!folio)
  493. goto out;
  494. wreq = netfs_create_write_req(mapping, NULL, folio_pos(folio), NETFS_WRITEBACK);
  495. if (IS_ERR(wreq)) {
  496. error = PTR_ERR(wreq);
  497. goto couldnt_start;
  498. }
  499. __set_bit(NETFS_RREQ_OFFLOAD_COLLECTION, &wreq->flags);
  500. trace_netfs_write(wreq, netfs_write_trace_writeback);
  501. netfs_stat(&netfs_n_wh_writepages);
  502. do {
  503. _debug("wbiter %lx %llx", folio->index, atomic64_read(&wreq->issued_to));
  504. /* It appears we don't have to handle cyclic writeback wrapping. */
  505. WARN_ON_ONCE(wreq && folio_pos(folio) < atomic64_read(&wreq->issued_to));
  506. if (netfs_folio_group(folio) != NETFS_FOLIO_COPY_TO_CACHE &&
  507. unlikely(!test_bit(NETFS_RREQ_UPLOAD_TO_SERVER, &wreq->flags))) {
  508. set_bit(NETFS_RREQ_UPLOAD_TO_SERVER, &wreq->flags);
  509. wreq->netfs_ops->begin_writeback(wreq);
  510. }
  511. error = netfs_write_folio(wreq, wbc, folio);
  512. if (error < 0)
  513. break;
  514. } while ((folio = writeback_iter(mapping, wbc, folio, &error)));
  515. netfs_end_issue_write(wreq);
  516. mutex_unlock(&ictx->wb_lock);
  517. netfs_wake_collector(wreq);
  518. netfs_put_request(wreq, netfs_rreq_trace_put_return);
  519. _leave(" = %d", error);
  520. return error;
  521. couldnt_start:
  522. netfs_kill_dirty_pages(mapping, wbc, folio);
  523. out:
  524. mutex_unlock(&ictx->wb_lock);
  525. _leave(" = %d", error);
  526. return error;
  527. }
  528. EXPORT_SYMBOL(netfs_writepages);
  529. /*
  530. * Begin a write operation for writing through the pagecache.
  531. */
  532. struct netfs_io_request *netfs_begin_writethrough(struct kiocb *iocb, size_t len)
  533. {
  534. struct netfs_io_request *wreq = NULL;
  535. struct netfs_inode *ictx = netfs_inode(file_inode(iocb->ki_filp));
  536. mutex_lock(&ictx->wb_lock);
  537. wreq = netfs_create_write_req(iocb->ki_filp->f_mapping, iocb->ki_filp,
  538. iocb->ki_pos, NETFS_WRITETHROUGH);
  539. if (IS_ERR(wreq)) {
  540. mutex_unlock(&ictx->wb_lock);
  541. return wreq;
  542. }
  543. wreq->io_streams[0].avail = true;
  544. trace_netfs_write(wreq, netfs_write_trace_writethrough);
  545. return wreq;
  546. }
  547. /*
  548. * Advance the state of the write operation used when writing through the
  549. * pagecache. Data has been copied into the pagecache that we need to append
  550. * to the request. If we've added more than wsize then we need to create a new
  551. * subrequest.
  552. */
  553. int netfs_advance_writethrough(struct netfs_io_request *wreq, struct writeback_control *wbc,
  554. struct folio *folio, size_t copied, bool to_page_end,
  555. struct folio **writethrough_cache)
  556. {
  557. _enter("R=%x ic=%zu ws=%u cp=%zu tp=%u",
  558. wreq->debug_id, wreq->buffer.iter.count, wreq->wsize, copied, to_page_end);
  559. if (!*writethrough_cache) {
  560. if (folio_test_dirty(folio))
  561. /* Sigh. mmap. */
  562. folio_clear_dirty_for_io(folio);
  563. /* We can make multiple writes to the folio... */
  564. folio_start_writeback(folio);
  565. if (wreq->len == 0)
  566. trace_netfs_folio(folio, netfs_folio_trace_wthru);
  567. else
  568. trace_netfs_folio(folio, netfs_folio_trace_wthru_plus);
  569. *writethrough_cache = folio;
  570. }
  571. wreq->len += copied;
  572. if (!to_page_end)
  573. return 0;
  574. *writethrough_cache = NULL;
  575. return netfs_write_folio(wreq, wbc, folio);
  576. }
  577. /*
  578. * End a write operation used when writing through the pagecache.
  579. */
  580. ssize_t netfs_end_writethrough(struct netfs_io_request *wreq, struct writeback_control *wbc,
  581. struct folio *writethrough_cache)
  582. {
  583. struct netfs_inode *ictx = netfs_inode(wreq->inode);
  584. ssize_t ret;
  585. _enter("R=%x", wreq->debug_id);
  586. if (writethrough_cache)
  587. netfs_write_folio(wreq, wbc, writethrough_cache);
  588. netfs_end_issue_write(wreq);
  589. mutex_unlock(&ictx->wb_lock);
  590. if (wreq->iocb)
  591. ret = -EIOCBQUEUED;
  592. else
  593. ret = netfs_wait_for_write(wreq);
  594. netfs_put_request(wreq, netfs_rreq_trace_put_return);
  595. return ret;
  596. }
  597. /*
  598. * Write some of a pending folio data back to the server and/or the cache.
  599. */
  600. static int netfs_write_folio_single(struct netfs_io_request *wreq,
  601. struct folio *folio)
  602. {
  603. struct netfs_io_stream *upload = &wreq->io_streams[0];
  604. struct netfs_io_stream *cache = &wreq->io_streams[1];
  605. struct netfs_io_stream *stream;
  606. size_t iter_off = 0;
  607. size_t fsize = folio_size(folio), flen;
  608. loff_t fpos = folio_pos(folio);
  609. bool to_eof = false;
  610. bool no_debug = false;
  611. _enter("");
  612. flen = folio_size(folio);
  613. if (flen > wreq->i_size - fpos) {
  614. flen = wreq->i_size - fpos;
  615. folio_zero_segment(folio, flen, fsize);
  616. to_eof = true;
  617. } else if (flen == wreq->i_size - fpos) {
  618. to_eof = true;
  619. }
  620. _debug("folio %zx/%zx", flen, fsize);
  621. if (!upload->avail && !cache->avail) {
  622. trace_netfs_folio(folio, netfs_folio_trace_cancel_store);
  623. return 0;
  624. }
  625. if (!upload->construct)
  626. trace_netfs_folio(folio, netfs_folio_trace_store);
  627. else
  628. trace_netfs_folio(folio, netfs_folio_trace_store_plus);
  629. /* Attach the folio to the rolling buffer. */
  630. folio_get(folio);
  631. rolling_buffer_append(&wreq->buffer, folio, NETFS_ROLLBUF_PUT_MARK);
  632. /* Move the submission point forward to allow for write-streaming data
  633. * not starting at the front of the page. We don't do write-streaming
  634. * with the cache as the cache requires DIO alignment.
  635. *
  636. * Also skip uploading for data that's been read and just needs copying
  637. * to the cache.
  638. */
  639. for (int s = 0; s < NR_IO_STREAMS; s++) {
  640. stream = &wreq->io_streams[s];
  641. stream->submit_off = 0;
  642. stream->submit_len = flen;
  643. if (!stream->avail) {
  644. stream->submit_off = UINT_MAX;
  645. stream->submit_len = 0;
  646. }
  647. }
  648. /* Attach the folio to one or more subrequests. For a big folio, we
  649. * could end up with thousands of subrequests if the wsize is small -
  650. * but we might need to wait during the creation of subrequests for
  651. * network resources (eg. SMB credits).
  652. */
  653. for (;;) {
  654. ssize_t part;
  655. size_t lowest_off = ULONG_MAX;
  656. int choose_s = -1;
  657. /* Always add to the lowest-submitted stream first. */
  658. for (int s = 0; s < NR_IO_STREAMS; s++) {
  659. stream = &wreq->io_streams[s];
  660. if (stream->submit_len > 0 &&
  661. stream->submit_off < lowest_off) {
  662. lowest_off = stream->submit_off;
  663. choose_s = s;
  664. }
  665. }
  666. if (choose_s < 0)
  667. break;
  668. stream = &wreq->io_streams[choose_s];
  669. /* Advance the iterator(s). */
  670. if (stream->submit_off > iter_off) {
  671. rolling_buffer_advance(&wreq->buffer, stream->submit_off - iter_off);
  672. iter_off = stream->submit_off;
  673. }
  674. atomic64_set(&wreq->issued_to, fpos + stream->submit_off);
  675. stream->submit_extendable_to = fsize - stream->submit_off;
  676. part = netfs_advance_write(wreq, stream, fpos + stream->submit_off,
  677. stream->submit_len, to_eof);
  678. stream->submit_off += part;
  679. if (part > stream->submit_len)
  680. stream->submit_len = 0;
  681. else
  682. stream->submit_len -= part;
  683. if (part > 0)
  684. no_debug = true;
  685. }
  686. wreq->buffer.iter.iov_offset = 0;
  687. if (fsize > iter_off)
  688. rolling_buffer_advance(&wreq->buffer, fsize - iter_off);
  689. atomic64_set(&wreq->issued_to, fpos + fsize);
  690. if (!no_debug)
  691. kdebug("R=%x: No submit", wreq->debug_id);
  692. _leave(" = 0");
  693. return 0;
  694. }
  695. /**
  696. * netfs_writeback_single - Write back a monolithic payload
  697. * @mapping: The mapping to write from
  698. * @wbc: Hints from the VM
  699. * @iter: Data to write, must be ITER_FOLIOQ.
  700. *
  701. * Write a monolithic, non-pagecache object back to the server and/or
  702. * the cache.
  703. */
  704. int netfs_writeback_single(struct address_space *mapping,
  705. struct writeback_control *wbc,
  706. struct iov_iter *iter)
  707. {
  708. struct netfs_io_request *wreq;
  709. struct netfs_inode *ictx = netfs_inode(mapping->host);
  710. struct folio_queue *fq;
  711. size_t size = iov_iter_count(iter);
  712. int ret;
  713. if (WARN_ON_ONCE(!iov_iter_is_folioq(iter)))
  714. return -EIO;
  715. if (!mutex_trylock(&ictx->wb_lock)) {
  716. if (wbc->sync_mode == WB_SYNC_NONE) {
  717. netfs_stat(&netfs_n_wb_lock_skip);
  718. return 0;
  719. }
  720. netfs_stat(&netfs_n_wb_lock_wait);
  721. mutex_lock(&ictx->wb_lock);
  722. }
  723. wreq = netfs_create_write_req(mapping, NULL, 0, NETFS_WRITEBACK_SINGLE);
  724. if (IS_ERR(wreq)) {
  725. ret = PTR_ERR(wreq);
  726. goto couldnt_start;
  727. }
  728. __set_bit(NETFS_RREQ_OFFLOAD_COLLECTION, &wreq->flags);
  729. trace_netfs_write(wreq, netfs_write_trace_writeback_single);
  730. netfs_stat(&netfs_n_wh_writepages);
  731. if (__test_and_set_bit(NETFS_RREQ_UPLOAD_TO_SERVER, &wreq->flags))
  732. wreq->netfs_ops->begin_writeback(wreq);
  733. for (fq = (struct folio_queue *)iter->folioq; fq; fq = fq->next) {
  734. for (int slot = 0; slot < folioq_count(fq); slot++) {
  735. struct folio *folio = folioq_folio(fq, slot);
  736. size_t part = umin(folioq_folio_size(fq, slot), size);
  737. _debug("wbiter %lx %llx", folio->index, atomic64_read(&wreq->issued_to));
  738. ret = netfs_write_folio_single(wreq, folio);
  739. if (ret < 0)
  740. goto stop;
  741. size -= part;
  742. if (size <= 0)
  743. goto stop;
  744. }
  745. }
  746. stop:
  747. for (int s = 0; s < NR_IO_STREAMS; s++)
  748. netfs_issue_write(wreq, &wreq->io_streams[s]);
  749. smp_wmb(); /* Write lists before ALL_QUEUED. */
  750. set_bit(NETFS_RREQ_ALL_QUEUED, &wreq->flags);
  751. mutex_unlock(&ictx->wb_lock);
  752. netfs_wake_collector(wreq);
  753. netfs_put_request(wreq, netfs_rreq_trace_put_return);
  754. _leave(" = %d", ret);
  755. return ret;
  756. couldnt_start:
  757. mutex_unlock(&ictx->wb_lock);
  758. _leave(" = %d", ret);
  759. return ret;
  760. }
  761. EXPORT_SYMBOL(netfs_writeback_single);