misc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Miscellaneous routines.
  3. *
  4. * Copyright (C) 2023 Red Hat, Inc. All Rights Reserved.
  5. * Written by David Howells (dhowells@redhat.com)
  6. */
  7. #include <linux/swap.h>
  8. #include "internal.h"
  9. /**
  10. * netfs_alloc_folioq_buffer - Allocate buffer space into a folio queue
  11. * @mapping: Address space to set on the folio (or NULL).
  12. * @_buffer: Pointer to the folio queue to add to (may point to a NULL; updated).
  13. * @_cur_size: Current size of the buffer (updated).
  14. * @size: Target size of the buffer.
  15. * @gfp: The allocation constraints.
  16. */
  17. int netfs_alloc_folioq_buffer(struct address_space *mapping,
  18. struct folio_queue **_buffer,
  19. size_t *_cur_size, ssize_t size, gfp_t gfp)
  20. {
  21. struct folio_queue *tail = *_buffer, *p;
  22. size = round_up(size, PAGE_SIZE);
  23. if (*_cur_size >= size)
  24. return 0;
  25. if (tail)
  26. while (tail->next)
  27. tail = tail->next;
  28. do {
  29. struct folio *folio;
  30. int order = 0, slot;
  31. if (!tail || folioq_full(tail)) {
  32. p = netfs_folioq_alloc(0, GFP_NOFS, netfs_trace_folioq_alloc_buffer);
  33. if (!p)
  34. return -ENOMEM;
  35. if (tail) {
  36. tail->next = p;
  37. p->prev = tail;
  38. } else {
  39. *_buffer = p;
  40. }
  41. tail = p;
  42. }
  43. if (size - *_cur_size > PAGE_SIZE)
  44. order = umin(ilog2(size - *_cur_size) - PAGE_SHIFT,
  45. MAX_PAGECACHE_ORDER);
  46. folio = folio_alloc(gfp, order);
  47. if (!folio && order > 0)
  48. folio = folio_alloc(gfp, 0);
  49. if (!folio)
  50. return -ENOMEM;
  51. folio->mapping = mapping;
  52. folio->index = *_cur_size / PAGE_SIZE;
  53. trace_netfs_folio(folio, netfs_folio_trace_alloc_buffer);
  54. slot = folioq_append_mark(tail, folio);
  55. *_cur_size += folioq_folio_size(tail, slot);
  56. } while (*_cur_size < size);
  57. return 0;
  58. }
  59. EXPORT_SYMBOL(netfs_alloc_folioq_buffer);
  60. /**
  61. * netfs_free_folioq_buffer - Free a folio queue.
  62. * @fq: The start of the folio queue to free
  63. *
  64. * Free up a chain of folio_queues and, if marked, the marked folios they point
  65. * to.
  66. */
  67. void netfs_free_folioq_buffer(struct folio_queue *fq)
  68. {
  69. struct folio_queue *next;
  70. struct folio_batch fbatch;
  71. folio_batch_init(&fbatch);
  72. for (; fq; fq = next) {
  73. for (int slot = 0; slot < folioq_count(fq); slot++) {
  74. struct folio *folio = folioq_folio(fq, slot);
  75. if (!folio ||
  76. !folioq_is_marked(fq, slot))
  77. continue;
  78. trace_netfs_folio(folio, netfs_folio_trace_put);
  79. if (folio_batch_add(&fbatch, folio))
  80. folio_batch_release(&fbatch);
  81. }
  82. netfs_stat_d(&netfs_n_folioq);
  83. next = fq->next;
  84. kfree(fq);
  85. }
  86. folio_batch_release(&fbatch);
  87. }
  88. EXPORT_SYMBOL(netfs_free_folioq_buffer);
  89. /*
  90. * Reset the subrequest iterator to refer just to the region remaining to be
  91. * read. The iterator may or may not have been advanced by socket ops or
  92. * extraction ops to an extent that may or may not match the amount actually
  93. * read.
  94. */
  95. void netfs_reset_iter(struct netfs_io_subrequest *subreq)
  96. {
  97. struct iov_iter *io_iter = &subreq->io_iter;
  98. size_t remain = subreq->len - subreq->transferred;
  99. if (io_iter->count > remain)
  100. iov_iter_advance(io_iter, io_iter->count - remain);
  101. else if (io_iter->count < remain)
  102. iov_iter_revert(io_iter, remain - io_iter->count);
  103. iov_iter_truncate(&subreq->io_iter, remain);
  104. }
  105. /**
  106. * netfs_dirty_folio - Mark folio dirty and pin a cache object for writeback
  107. * @mapping: The mapping the folio belongs to.
  108. * @folio: The folio being dirtied.
  109. *
  110. * Set the dirty flag on a folio and pin an in-use cache object in memory so
  111. * that writeback can later write to it. This is intended to be called from
  112. * the filesystem's ->dirty_folio() method.
  113. *
  114. * Return: true if the dirty flag was set on the folio, false otherwise.
  115. */
  116. bool netfs_dirty_folio(struct address_space *mapping, struct folio *folio)
  117. {
  118. struct inode *inode = mapping->host;
  119. struct netfs_inode *ictx = netfs_inode(inode);
  120. struct fscache_cookie *cookie = netfs_i_cookie(ictx);
  121. bool need_use = false;
  122. _enter("");
  123. if (!filemap_dirty_folio(mapping, folio))
  124. return false;
  125. if (!fscache_cookie_valid(cookie))
  126. return true;
  127. if (!(inode_state_read_once(inode) & I_PINNING_NETFS_WB)) {
  128. spin_lock(&inode->i_lock);
  129. if (!(inode_state_read(inode) & I_PINNING_NETFS_WB)) {
  130. inode_state_set(inode, I_PINNING_NETFS_WB);
  131. need_use = true;
  132. }
  133. spin_unlock(&inode->i_lock);
  134. if (need_use)
  135. fscache_use_cookie(cookie, true);
  136. }
  137. return true;
  138. }
  139. EXPORT_SYMBOL(netfs_dirty_folio);
  140. /**
  141. * netfs_unpin_writeback - Unpin writeback resources
  142. * @inode: The inode on which the cookie resides
  143. * @wbc: The writeback control
  144. *
  145. * Unpin the writeback resources pinned by netfs_dirty_folio(). This is
  146. * intended to be called as/by the netfs's ->write_inode() method.
  147. */
  148. int netfs_unpin_writeback(struct inode *inode, struct writeback_control *wbc)
  149. {
  150. struct fscache_cookie *cookie = netfs_i_cookie(netfs_inode(inode));
  151. if (wbc->unpinned_netfs_wb)
  152. fscache_unuse_cookie(cookie, NULL, NULL);
  153. return 0;
  154. }
  155. EXPORT_SYMBOL(netfs_unpin_writeback);
  156. /**
  157. * netfs_clear_inode_writeback - Clear writeback resources pinned by an inode
  158. * @inode: The inode to clean up
  159. * @aux: Auxiliary data to apply to the inode
  160. *
  161. * Clear any writeback resources held by an inode when the inode is evicted.
  162. * This must be called before clear_inode() is called.
  163. */
  164. void netfs_clear_inode_writeback(struct inode *inode, const void *aux)
  165. {
  166. struct fscache_cookie *cookie = netfs_i_cookie(netfs_inode(inode));
  167. if (inode_state_read_once(inode) & I_PINNING_NETFS_WB) {
  168. loff_t i_size = i_size_read(inode);
  169. fscache_unuse_cookie(cookie, aux, &i_size);
  170. }
  171. }
  172. EXPORT_SYMBOL(netfs_clear_inode_writeback);
  173. /**
  174. * netfs_invalidate_folio - Invalidate or partially invalidate a folio
  175. * @folio: Folio proposed for release
  176. * @offset: Offset of the invalidated region
  177. * @length: Length of the invalidated region
  178. *
  179. * Invalidate part or all of a folio for a network filesystem. The folio will
  180. * be removed afterwards if the invalidated region covers the entire folio.
  181. */
  182. void netfs_invalidate_folio(struct folio *folio, size_t offset, size_t length)
  183. {
  184. struct netfs_folio *finfo;
  185. struct netfs_inode *ctx = netfs_inode(folio_inode(folio));
  186. size_t flen = folio_size(folio);
  187. _enter("{%lx},%zx,%zx", folio->index, offset, length);
  188. if (offset == 0 && length == flen) {
  189. unsigned long long i_size = i_size_read(&ctx->inode);
  190. unsigned long long fpos = folio_pos(folio), end;
  191. end = umin(fpos + flen, i_size);
  192. if (fpos < i_size && end > ctx->zero_point)
  193. ctx->zero_point = end;
  194. }
  195. folio_wait_private_2(folio); /* [DEPRECATED] */
  196. if (!folio_test_private(folio))
  197. return;
  198. finfo = netfs_folio_info(folio);
  199. if (offset == 0 && length >= flen)
  200. goto erase_completely;
  201. if (finfo) {
  202. /* We have a partially uptodate page from a streaming write. */
  203. unsigned int fstart = finfo->dirty_offset;
  204. unsigned int fend = fstart + finfo->dirty_len;
  205. unsigned int iend = offset + length;
  206. if (offset >= fend)
  207. return;
  208. if (iend <= fstart)
  209. return;
  210. /* The invalidation region overlaps the data. If the region
  211. * covers the start of the data, we either move along the start
  212. * or just erase the data entirely.
  213. */
  214. if (offset <= fstart) {
  215. if (iend >= fend)
  216. goto erase_completely;
  217. /* Move the start of the data. */
  218. finfo->dirty_len = fend - iend;
  219. finfo->dirty_offset = offset;
  220. return;
  221. }
  222. /* Reduce the length of the data if the invalidation region
  223. * covers the tail part.
  224. */
  225. if (iend >= fend) {
  226. finfo->dirty_len = offset - fstart;
  227. return;
  228. }
  229. /* A partial write was split. The caller has already zeroed
  230. * it, so just absorb the hole.
  231. */
  232. }
  233. return;
  234. erase_completely:
  235. netfs_put_group(netfs_folio_group(folio));
  236. folio_detach_private(folio);
  237. folio_clear_uptodate(folio);
  238. kfree(finfo);
  239. return;
  240. }
  241. EXPORT_SYMBOL(netfs_invalidate_folio);
  242. /**
  243. * netfs_release_folio - Try to release a folio
  244. * @folio: Folio proposed for release
  245. * @gfp: Flags qualifying the release
  246. *
  247. * Request release of a folio and clean up its private state if it's not busy.
  248. * Returns true if the folio can now be released, false if not
  249. */
  250. bool netfs_release_folio(struct folio *folio, gfp_t gfp)
  251. {
  252. struct netfs_inode *ctx = netfs_inode(folio_inode(folio));
  253. unsigned long long end;
  254. if (folio_test_dirty(folio))
  255. return false;
  256. end = umin(folio_next_pos(folio), i_size_read(&ctx->inode));
  257. if (end > ctx->zero_point)
  258. ctx->zero_point = end;
  259. if (folio_test_private(folio))
  260. return false;
  261. if (unlikely(folio_test_private_2(folio))) { /* [DEPRECATED] */
  262. if (current_is_kswapd() || !(gfp & __GFP_FS))
  263. return false;
  264. folio_wait_private_2(folio);
  265. }
  266. fscache_note_page_release(netfs_i_cookie(ctx));
  267. return true;
  268. }
  269. EXPORT_SYMBOL(netfs_release_folio);
  270. /*
  271. * Wake the collection work item.
  272. */
  273. void netfs_wake_collector(struct netfs_io_request *rreq)
  274. {
  275. if (test_bit(NETFS_RREQ_OFFLOAD_COLLECTION, &rreq->flags) &&
  276. !test_bit(NETFS_RREQ_RETRYING, &rreq->flags)) {
  277. queue_work(system_dfl_wq, &rreq->work);
  278. } else {
  279. trace_netfs_rreq(rreq, netfs_rreq_trace_wake_queue);
  280. wake_up(&rreq->waitq);
  281. }
  282. }
  283. /*
  284. * Mark a subrequest as no longer being in progress and, if need be, wake the
  285. * collector.
  286. */
  287. void netfs_subreq_clear_in_progress(struct netfs_io_subrequest *subreq)
  288. {
  289. struct netfs_io_request *rreq = subreq->rreq;
  290. struct netfs_io_stream *stream = &rreq->io_streams[subreq->stream_nr];
  291. clear_bit_unlock(NETFS_SREQ_IN_PROGRESS, &subreq->flags);
  292. smp_mb__after_atomic(); /* Clear IN_PROGRESS before task state */
  293. /* If we are at the head of the queue, wake up the collector. */
  294. if (list_is_first(&subreq->rreq_link, &stream->subrequests) ||
  295. test_bit(NETFS_RREQ_RETRYING, &rreq->flags))
  296. netfs_wake_collector(rreq);
  297. }
  298. /*
  299. * Wait for all outstanding I/O in a stream to quiesce.
  300. */
  301. void netfs_wait_for_in_progress_stream(struct netfs_io_request *rreq,
  302. struct netfs_io_stream *stream)
  303. {
  304. struct netfs_io_subrequest *subreq;
  305. DEFINE_WAIT(myself);
  306. list_for_each_entry(subreq, &stream->subrequests, rreq_link) {
  307. if (!netfs_check_subreq_in_progress(subreq))
  308. continue;
  309. trace_netfs_rreq(rreq, netfs_rreq_trace_wait_quiesce);
  310. for (;;) {
  311. prepare_to_wait(&rreq->waitq, &myself, TASK_UNINTERRUPTIBLE);
  312. if (!netfs_check_subreq_in_progress(subreq))
  313. break;
  314. trace_netfs_sreq(subreq, netfs_sreq_trace_wait_for);
  315. schedule();
  316. }
  317. }
  318. trace_netfs_rreq(rreq, netfs_rreq_trace_waited_quiesce);
  319. finish_wait(&rreq->waitq, &myself);
  320. }
  321. /*
  322. * Perform collection in app thread if not offloaded to workqueue.
  323. */
  324. static int netfs_collect_in_app(struct netfs_io_request *rreq,
  325. bool (*collector)(struct netfs_io_request *rreq))
  326. {
  327. bool need_collect = false, inactive = true, done = true;
  328. if (!netfs_check_rreq_in_progress(rreq)) {
  329. trace_netfs_rreq(rreq, netfs_rreq_trace_recollect);
  330. return 1; /* Done */
  331. }
  332. for (int i = 0; i < NR_IO_STREAMS; i++) {
  333. struct netfs_io_subrequest *subreq;
  334. struct netfs_io_stream *stream = &rreq->io_streams[i];
  335. if (!stream->active)
  336. continue;
  337. inactive = false;
  338. trace_netfs_collect_stream(rreq, stream);
  339. subreq = list_first_entry_or_null(&stream->subrequests,
  340. struct netfs_io_subrequest,
  341. rreq_link);
  342. if (subreq &&
  343. (!netfs_check_subreq_in_progress(subreq) ||
  344. test_bit(NETFS_SREQ_MADE_PROGRESS, &subreq->flags))) {
  345. need_collect = true;
  346. break;
  347. }
  348. if (subreq || !test_bit(NETFS_RREQ_ALL_QUEUED, &rreq->flags))
  349. done = false;
  350. }
  351. if (!need_collect && !inactive && !done)
  352. return 0; /* Sleep */
  353. __set_current_state(TASK_RUNNING);
  354. if (collector(rreq)) {
  355. /* Drop the ref from the NETFS_RREQ_IN_PROGRESS flag. */
  356. netfs_put_request(rreq, netfs_rreq_trace_put_work_ip);
  357. return 1; /* Done */
  358. }
  359. if (inactive) {
  360. WARN(true, "Failed to collect inactive req R=%08x\n",
  361. rreq->debug_id);
  362. cond_resched();
  363. }
  364. return 2; /* Again */
  365. }
  366. /*
  367. * Wait for a request to complete, successfully or otherwise.
  368. */
  369. static ssize_t netfs_wait_for_in_progress(struct netfs_io_request *rreq,
  370. bool (*collector)(struct netfs_io_request *rreq))
  371. {
  372. DEFINE_WAIT(myself);
  373. ssize_t ret;
  374. for (;;) {
  375. prepare_to_wait(&rreq->waitq, &myself, TASK_UNINTERRUPTIBLE);
  376. if (!test_bit(NETFS_RREQ_OFFLOAD_COLLECTION, &rreq->flags)) {
  377. switch (netfs_collect_in_app(rreq, collector)) {
  378. case 0:
  379. break;
  380. case 1:
  381. goto all_collected;
  382. case 2:
  383. if (!netfs_check_rreq_in_progress(rreq))
  384. break;
  385. cond_resched();
  386. continue;
  387. }
  388. }
  389. if (!netfs_check_rreq_in_progress(rreq))
  390. break;
  391. trace_netfs_rreq(rreq, netfs_rreq_trace_wait_ip);
  392. schedule();
  393. }
  394. all_collected:
  395. trace_netfs_rreq(rreq, netfs_rreq_trace_waited_ip);
  396. finish_wait(&rreq->waitq, &myself);
  397. ret = rreq->error;
  398. if (ret == 0) {
  399. ret = rreq->transferred;
  400. switch (rreq->origin) {
  401. case NETFS_DIO_READ:
  402. case NETFS_DIO_WRITE:
  403. case NETFS_READ_SINGLE:
  404. case NETFS_UNBUFFERED_READ:
  405. case NETFS_UNBUFFERED_WRITE:
  406. break;
  407. default:
  408. if (rreq->submitted < rreq->len) {
  409. trace_netfs_failure(rreq, NULL, ret, netfs_fail_short_read);
  410. ret = -EIO;
  411. }
  412. break;
  413. }
  414. }
  415. return ret;
  416. }
  417. ssize_t netfs_wait_for_read(struct netfs_io_request *rreq)
  418. {
  419. return netfs_wait_for_in_progress(rreq, netfs_read_collection);
  420. }
  421. ssize_t netfs_wait_for_write(struct netfs_io_request *rreq)
  422. {
  423. return netfs_wait_for_in_progress(rreq, netfs_write_collection);
  424. }
  425. /*
  426. * Wait for a paused operation to unpause or complete in some manner.
  427. */
  428. static void netfs_wait_for_pause(struct netfs_io_request *rreq,
  429. bool (*collector)(struct netfs_io_request *rreq))
  430. {
  431. DEFINE_WAIT(myself);
  432. for (;;) {
  433. trace_netfs_rreq(rreq, netfs_rreq_trace_wait_pause);
  434. prepare_to_wait(&rreq->waitq, &myself, TASK_UNINTERRUPTIBLE);
  435. if (!test_bit(NETFS_RREQ_OFFLOAD_COLLECTION, &rreq->flags)) {
  436. switch (netfs_collect_in_app(rreq, collector)) {
  437. case 0:
  438. break;
  439. case 1:
  440. goto all_collected;
  441. case 2:
  442. if (!netfs_check_rreq_in_progress(rreq) ||
  443. !test_bit(NETFS_RREQ_PAUSE, &rreq->flags))
  444. break;
  445. cond_resched();
  446. continue;
  447. }
  448. }
  449. if (!netfs_check_rreq_in_progress(rreq) ||
  450. !test_bit(NETFS_RREQ_PAUSE, &rreq->flags))
  451. break;
  452. schedule();
  453. }
  454. all_collected:
  455. trace_netfs_rreq(rreq, netfs_rreq_trace_waited_pause);
  456. finish_wait(&rreq->waitq, &myself);
  457. }
  458. void netfs_wait_for_paused_read(struct netfs_io_request *rreq)
  459. {
  460. return netfs_wait_for_pause(rreq, netfs_read_collection);
  461. }
  462. void netfs_wait_for_paused_write(struct netfs_io_request *rreq)
  463. {
  464. return netfs_wait_for_pause(rreq, netfs_write_collection);
  465. }