buffered_read.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* Network filesystem high-level buffered read support.
  3. *
  4. * Copyright (C) 2021 Red Hat, Inc. All Rights Reserved.
  5. * Written by David Howells (dhowells@redhat.com)
  6. */
  7. #include <linux/export.h>
  8. #include <linux/task_io_accounting_ops.h>
  9. #include "internal.h"
  10. static void netfs_cache_expand_readahead(struct netfs_io_request *rreq,
  11. unsigned long long *_start,
  12. unsigned long long *_len,
  13. unsigned long long i_size)
  14. {
  15. struct netfs_cache_resources *cres = &rreq->cache_resources;
  16. if (cres->ops && cres->ops->expand_readahead)
  17. cres->ops->expand_readahead(cres, _start, _len, i_size);
  18. }
  19. static void netfs_rreq_expand(struct netfs_io_request *rreq,
  20. struct readahead_control *ractl)
  21. {
  22. /* Give the cache a chance to change the request parameters. The
  23. * resultant request must contain the original region.
  24. */
  25. netfs_cache_expand_readahead(rreq, &rreq->start, &rreq->len, rreq->i_size);
  26. /* Give the netfs a chance to change the request parameters. The
  27. * resultant request must contain the original region.
  28. */
  29. if (rreq->netfs_ops->expand_readahead)
  30. rreq->netfs_ops->expand_readahead(rreq);
  31. /* Expand the request if the cache wants it to start earlier. Note
  32. * that the expansion may get further extended if the VM wishes to
  33. * insert THPs and the preferred start and/or end wind up in the middle
  34. * of THPs.
  35. *
  36. * If this is the case, however, the THP size should be an integer
  37. * multiple of the cache granule size, so we get a whole number of
  38. * granules to deal with.
  39. */
  40. if (rreq->start != readahead_pos(ractl) ||
  41. rreq->len != readahead_length(ractl)) {
  42. readahead_expand(ractl, rreq->start, rreq->len);
  43. rreq->start = readahead_pos(ractl);
  44. rreq->len = readahead_length(ractl);
  45. trace_netfs_read(rreq, readahead_pos(ractl), readahead_length(ractl),
  46. netfs_read_trace_expanded);
  47. }
  48. }
  49. /*
  50. * Begin an operation, and fetch the stored zero point value from the cookie if
  51. * available.
  52. */
  53. static int netfs_begin_cache_read(struct netfs_io_request *rreq, struct netfs_inode *ctx)
  54. {
  55. return fscache_begin_read_operation(&rreq->cache_resources, netfs_i_cookie(ctx));
  56. }
  57. /*
  58. * netfs_prepare_read_iterator - Prepare the subreq iterator for I/O
  59. * @subreq: The subrequest to be set up
  60. *
  61. * Prepare the I/O iterator representing the read buffer on a subrequest for
  62. * the filesystem to use for I/O (it can be passed directly to a socket). This
  63. * is intended to be called from the ->issue_read() method once the filesystem
  64. * has trimmed the request to the size it wants.
  65. *
  66. * Returns the limited size if successful and -ENOMEM if insufficient memory
  67. * available.
  68. *
  69. * [!] NOTE: This must be run in the same thread as ->issue_read() was called
  70. * in as we access the readahead_control struct.
  71. */
  72. static ssize_t netfs_prepare_read_iterator(struct netfs_io_subrequest *subreq,
  73. struct readahead_control *ractl)
  74. {
  75. struct netfs_io_request *rreq = subreq->rreq;
  76. size_t rsize = subreq->len;
  77. if (subreq->source == NETFS_DOWNLOAD_FROM_SERVER)
  78. rsize = umin(rsize, rreq->io_streams[0].sreq_max_len);
  79. if (ractl) {
  80. /* If we don't have sufficient folios in the rolling buffer,
  81. * extract a folioq's worth from the readahead region at a time
  82. * into the buffer. Note that this acquires a ref on each page
  83. * that we will need to release later - but we don't want to do
  84. * that until after we've started the I/O.
  85. */
  86. struct folio_batch put_batch;
  87. folio_batch_init(&put_batch);
  88. while (rreq->submitted < subreq->start + rsize) {
  89. ssize_t added;
  90. added = rolling_buffer_load_from_ra(&rreq->buffer, ractl,
  91. &put_batch);
  92. if (added < 0)
  93. return added;
  94. rreq->submitted += added;
  95. }
  96. folio_batch_release(&put_batch);
  97. }
  98. subreq->len = rsize;
  99. if (unlikely(rreq->io_streams[0].sreq_max_segs)) {
  100. size_t limit = netfs_limit_iter(&rreq->buffer.iter, 0, rsize,
  101. rreq->io_streams[0].sreq_max_segs);
  102. if (limit < rsize) {
  103. subreq->len = limit;
  104. trace_netfs_sreq(subreq, netfs_sreq_trace_limited);
  105. }
  106. }
  107. subreq->io_iter = rreq->buffer.iter;
  108. iov_iter_truncate(&subreq->io_iter, subreq->len);
  109. rolling_buffer_advance(&rreq->buffer, subreq->len);
  110. return subreq->len;
  111. }
  112. static enum netfs_io_source netfs_cache_prepare_read(struct netfs_io_request *rreq,
  113. struct netfs_io_subrequest *subreq,
  114. loff_t i_size)
  115. {
  116. struct netfs_cache_resources *cres = &rreq->cache_resources;
  117. enum netfs_io_source source;
  118. if (!cres->ops)
  119. return NETFS_DOWNLOAD_FROM_SERVER;
  120. source = cres->ops->prepare_read(subreq, i_size);
  121. trace_netfs_sreq(subreq, netfs_sreq_trace_prepare);
  122. return source;
  123. }
  124. /*
  125. * Issue a read against the cache.
  126. * - Eats the caller's ref on subreq.
  127. */
  128. static void netfs_read_cache_to_pagecache(struct netfs_io_request *rreq,
  129. struct netfs_io_subrequest *subreq)
  130. {
  131. struct netfs_cache_resources *cres = &rreq->cache_resources;
  132. netfs_stat(&netfs_n_rh_read);
  133. cres->ops->read(cres, subreq->start, &subreq->io_iter, NETFS_READ_HOLE_IGNORE,
  134. netfs_cache_read_terminated, subreq);
  135. }
  136. static void netfs_queue_read(struct netfs_io_request *rreq,
  137. struct netfs_io_subrequest *subreq,
  138. bool last_subreq)
  139. {
  140. struct netfs_io_stream *stream = &rreq->io_streams[0];
  141. __set_bit(NETFS_SREQ_IN_PROGRESS, &subreq->flags);
  142. /* We add to the end of the list whilst the collector may be walking
  143. * the list. The collector only goes nextwards and uses the lock to
  144. * remove entries off of the front.
  145. */
  146. spin_lock(&rreq->lock);
  147. list_add_tail(&subreq->rreq_link, &stream->subrequests);
  148. if (list_is_first(&subreq->rreq_link, &stream->subrequests)) {
  149. if (!stream->active) {
  150. stream->collected_to = subreq->start;
  151. /* Store list pointers before active flag */
  152. smp_store_release(&stream->active, true);
  153. }
  154. }
  155. if (last_subreq) {
  156. smp_wmb(); /* Write lists before ALL_QUEUED. */
  157. set_bit(NETFS_RREQ_ALL_QUEUED, &rreq->flags);
  158. }
  159. spin_unlock(&rreq->lock);
  160. }
  161. static void netfs_issue_read(struct netfs_io_request *rreq,
  162. struct netfs_io_subrequest *subreq)
  163. {
  164. switch (subreq->source) {
  165. case NETFS_DOWNLOAD_FROM_SERVER:
  166. rreq->netfs_ops->issue_read(subreq);
  167. break;
  168. case NETFS_READ_FROM_CACHE:
  169. netfs_read_cache_to_pagecache(rreq, subreq);
  170. break;
  171. default:
  172. __set_bit(NETFS_SREQ_CLEAR_TAIL, &subreq->flags);
  173. subreq->error = 0;
  174. iov_iter_zero(subreq->len, &subreq->io_iter);
  175. subreq->transferred = subreq->len;
  176. netfs_read_subreq_terminated(subreq);
  177. break;
  178. }
  179. }
  180. /*
  181. * Perform a read to the pagecache from a series of sources of different types,
  182. * slicing up the region to be read according to available cache blocks and
  183. * network rsize.
  184. */
  185. static void netfs_read_to_pagecache(struct netfs_io_request *rreq,
  186. struct readahead_control *ractl)
  187. {
  188. struct netfs_inode *ictx = netfs_inode(rreq->inode);
  189. unsigned long long start = rreq->start;
  190. ssize_t size = rreq->len;
  191. int ret = 0;
  192. do {
  193. struct netfs_io_subrequest *subreq;
  194. enum netfs_io_source source = NETFS_SOURCE_UNKNOWN;
  195. ssize_t slice;
  196. subreq = netfs_alloc_subrequest(rreq);
  197. if (!subreq) {
  198. ret = -ENOMEM;
  199. break;
  200. }
  201. subreq->start = start;
  202. subreq->len = size;
  203. source = netfs_cache_prepare_read(rreq, subreq, rreq->i_size);
  204. subreq->source = source;
  205. if (source == NETFS_DOWNLOAD_FROM_SERVER) {
  206. unsigned long long zp = umin(ictx->zero_point, rreq->i_size);
  207. size_t len = subreq->len;
  208. if (unlikely(rreq->origin == NETFS_READ_SINGLE))
  209. zp = rreq->i_size;
  210. if (subreq->start >= zp) {
  211. subreq->source = source = NETFS_FILL_WITH_ZEROES;
  212. goto fill_with_zeroes;
  213. }
  214. if (len > zp - subreq->start)
  215. len = zp - subreq->start;
  216. if (len == 0) {
  217. pr_err("ZERO-LEN READ: R=%08x[%x] l=%zx/%zx s=%llx z=%llx i=%llx",
  218. rreq->debug_id, subreq->debug_index,
  219. subreq->len, size,
  220. subreq->start, ictx->zero_point, rreq->i_size);
  221. break;
  222. }
  223. subreq->len = len;
  224. netfs_stat(&netfs_n_rh_download);
  225. if (rreq->netfs_ops->prepare_read) {
  226. ret = rreq->netfs_ops->prepare_read(subreq);
  227. if (ret < 0) {
  228. subreq->error = ret;
  229. /* Not queued - release both refs. */
  230. netfs_put_subrequest(subreq,
  231. netfs_sreq_trace_put_cancel);
  232. netfs_put_subrequest(subreq,
  233. netfs_sreq_trace_put_cancel);
  234. break;
  235. }
  236. trace_netfs_sreq(subreq, netfs_sreq_trace_prepare);
  237. }
  238. goto issue;
  239. }
  240. fill_with_zeroes:
  241. if (source == NETFS_FILL_WITH_ZEROES) {
  242. subreq->source = NETFS_FILL_WITH_ZEROES;
  243. trace_netfs_sreq(subreq, netfs_sreq_trace_submit);
  244. netfs_stat(&netfs_n_rh_zero);
  245. goto issue;
  246. }
  247. if (source == NETFS_READ_FROM_CACHE) {
  248. trace_netfs_sreq(subreq, netfs_sreq_trace_submit);
  249. goto issue;
  250. }
  251. pr_err("Unexpected read source %u\n", source);
  252. WARN_ON_ONCE(1);
  253. break;
  254. issue:
  255. slice = netfs_prepare_read_iterator(subreq, ractl);
  256. if (slice < 0) {
  257. ret = slice;
  258. subreq->error = ret;
  259. trace_netfs_sreq(subreq, netfs_sreq_trace_cancel);
  260. /* Not queued - release both refs. */
  261. netfs_put_subrequest(subreq, netfs_sreq_trace_put_cancel);
  262. netfs_put_subrequest(subreq, netfs_sreq_trace_put_cancel);
  263. break;
  264. }
  265. size -= slice;
  266. start += slice;
  267. netfs_queue_read(rreq, subreq, size <= 0);
  268. netfs_issue_read(rreq, subreq);
  269. cond_resched();
  270. } while (size > 0);
  271. if (unlikely(size > 0)) {
  272. smp_wmb(); /* Write lists before ALL_QUEUED. */
  273. set_bit(NETFS_RREQ_ALL_QUEUED, &rreq->flags);
  274. netfs_wake_collector(rreq);
  275. }
  276. /* Defer error return as we may need to wait for outstanding I/O. */
  277. cmpxchg(&rreq->error, 0, ret);
  278. }
  279. /**
  280. * netfs_readahead - Helper to manage a read request
  281. * @ractl: The description of the readahead request
  282. *
  283. * Fulfil a readahead request by drawing data from the cache if possible, or
  284. * the netfs if not. Space beyond the EOF is zero-filled. Multiple I/O
  285. * requests from different sources will get munged together. If necessary, the
  286. * readahead window can be expanded in either direction to a more convenient
  287. * alighment for RPC efficiency or to make storage in the cache feasible.
  288. *
  289. * The calling netfs must initialise a netfs context contiguous to the vfs
  290. * inode before calling this.
  291. *
  292. * This is usable whether or not caching is enabled.
  293. */
  294. void netfs_readahead(struct readahead_control *ractl)
  295. {
  296. struct netfs_io_request *rreq;
  297. struct netfs_inode *ictx = netfs_inode(ractl->mapping->host);
  298. unsigned long long start = readahead_pos(ractl);
  299. size_t size = readahead_length(ractl);
  300. int ret;
  301. rreq = netfs_alloc_request(ractl->mapping, ractl->file, start, size,
  302. NETFS_READAHEAD);
  303. if (IS_ERR(rreq))
  304. return;
  305. __set_bit(NETFS_RREQ_OFFLOAD_COLLECTION, &rreq->flags);
  306. ret = netfs_begin_cache_read(rreq, ictx);
  307. if (ret == -ENOMEM || ret == -EINTR || ret == -ERESTARTSYS)
  308. goto cleanup_free;
  309. netfs_stat(&netfs_n_rh_readahead);
  310. trace_netfs_read(rreq, readahead_pos(ractl), readahead_length(ractl),
  311. netfs_read_trace_readahead);
  312. netfs_rreq_expand(rreq, ractl);
  313. rreq->submitted = rreq->start;
  314. if (rolling_buffer_init(&rreq->buffer, rreq->debug_id, ITER_DEST) < 0)
  315. goto cleanup_free;
  316. netfs_read_to_pagecache(rreq, ractl);
  317. return netfs_put_request(rreq, netfs_rreq_trace_put_return);
  318. cleanup_free:
  319. return netfs_put_failed_request(rreq);
  320. }
  321. EXPORT_SYMBOL(netfs_readahead);
  322. /*
  323. * Create a rolling buffer with a single occupying folio.
  324. */
  325. static int netfs_create_singular_buffer(struct netfs_io_request *rreq, struct folio *folio,
  326. unsigned int rollbuf_flags)
  327. {
  328. ssize_t added;
  329. if (rolling_buffer_init(&rreq->buffer, rreq->debug_id, ITER_DEST) < 0)
  330. return -ENOMEM;
  331. added = rolling_buffer_append(&rreq->buffer, folio, rollbuf_flags);
  332. if (added < 0)
  333. return added;
  334. rreq->submitted = rreq->start + added;
  335. return 0;
  336. }
  337. /*
  338. * Read into gaps in a folio partially filled by a streaming write.
  339. */
  340. static int netfs_read_gaps(struct file *file, struct folio *folio)
  341. {
  342. struct netfs_io_request *rreq;
  343. struct address_space *mapping = folio->mapping;
  344. struct netfs_folio *finfo = netfs_folio_info(folio);
  345. struct netfs_inode *ctx = netfs_inode(mapping->host);
  346. struct folio *sink = NULL;
  347. struct bio_vec *bvec;
  348. unsigned int from = finfo->dirty_offset;
  349. unsigned int to = from + finfo->dirty_len;
  350. unsigned int off = 0, i = 0;
  351. size_t flen = folio_size(folio);
  352. size_t nr_bvec = flen / PAGE_SIZE + 2;
  353. size_t part;
  354. int ret;
  355. _enter("%lx", folio->index);
  356. rreq = netfs_alloc_request(mapping, file, folio_pos(folio), flen, NETFS_READ_GAPS);
  357. if (IS_ERR(rreq)) {
  358. ret = PTR_ERR(rreq);
  359. goto alloc_error;
  360. }
  361. ret = netfs_begin_cache_read(rreq, ctx);
  362. if (ret == -ENOMEM || ret == -EINTR || ret == -ERESTARTSYS)
  363. goto discard;
  364. netfs_stat(&netfs_n_rh_read_folio);
  365. trace_netfs_read(rreq, rreq->start, rreq->len, netfs_read_trace_read_gaps);
  366. /* Fiddle the buffer so that a gap at the beginning and/or a gap at the
  367. * end get copied to, but the middle is discarded.
  368. */
  369. ret = -ENOMEM;
  370. bvec = kmalloc_objs(*bvec, nr_bvec);
  371. if (!bvec)
  372. goto discard;
  373. sink = folio_alloc(GFP_KERNEL, 0);
  374. if (!sink) {
  375. kfree(bvec);
  376. goto discard;
  377. }
  378. trace_netfs_folio(folio, netfs_folio_trace_read_gaps);
  379. rreq->direct_bv = bvec;
  380. rreq->direct_bv_count = nr_bvec;
  381. if (from > 0) {
  382. bvec_set_folio(&bvec[i++], folio, from, 0);
  383. off = from;
  384. }
  385. while (off < to) {
  386. part = min_t(size_t, to - off, PAGE_SIZE);
  387. bvec_set_folio(&bvec[i++], sink, part, 0);
  388. off += part;
  389. }
  390. if (to < flen)
  391. bvec_set_folio(&bvec[i++], folio, flen - to, to);
  392. iov_iter_bvec(&rreq->buffer.iter, ITER_DEST, bvec, i, rreq->len);
  393. rreq->submitted = rreq->start + flen;
  394. netfs_read_to_pagecache(rreq, NULL);
  395. if (sink)
  396. folio_put(sink);
  397. ret = netfs_wait_for_read(rreq);
  398. if (ret >= 0) {
  399. flush_dcache_folio(folio);
  400. folio_mark_uptodate(folio);
  401. }
  402. folio_unlock(folio);
  403. netfs_put_request(rreq, netfs_rreq_trace_put_return);
  404. return ret < 0 ? ret : 0;
  405. discard:
  406. netfs_put_failed_request(rreq);
  407. alloc_error:
  408. folio_unlock(folio);
  409. return ret;
  410. }
  411. /**
  412. * netfs_read_folio - Helper to manage a read_folio request
  413. * @file: The file to read from
  414. * @folio: The folio to read
  415. *
  416. * Fulfil a read_folio request by drawing data from the cache if
  417. * possible, or the netfs if not. Space beyond the EOF is zero-filled.
  418. * Multiple I/O requests from different sources will get munged together.
  419. *
  420. * The calling netfs must initialise a netfs context contiguous to the vfs
  421. * inode before calling this.
  422. *
  423. * This is usable whether or not caching is enabled.
  424. */
  425. int netfs_read_folio(struct file *file, struct folio *folio)
  426. {
  427. struct address_space *mapping = folio->mapping;
  428. struct netfs_io_request *rreq;
  429. struct netfs_inode *ctx = netfs_inode(mapping->host);
  430. int ret;
  431. if (folio_test_dirty(folio)) {
  432. trace_netfs_folio(folio, netfs_folio_trace_read_gaps);
  433. return netfs_read_gaps(file, folio);
  434. }
  435. _enter("%lx", folio->index);
  436. rreq = netfs_alloc_request(mapping, file,
  437. folio_pos(folio), folio_size(folio),
  438. NETFS_READPAGE);
  439. if (IS_ERR(rreq)) {
  440. ret = PTR_ERR(rreq);
  441. goto alloc_error;
  442. }
  443. ret = netfs_begin_cache_read(rreq, ctx);
  444. if (ret == -ENOMEM || ret == -EINTR || ret == -ERESTARTSYS)
  445. goto discard;
  446. netfs_stat(&netfs_n_rh_read_folio);
  447. trace_netfs_read(rreq, rreq->start, rreq->len, netfs_read_trace_readpage);
  448. /* Set up the output buffer */
  449. ret = netfs_create_singular_buffer(rreq, folio, 0);
  450. if (ret < 0)
  451. goto discard;
  452. netfs_read_to_pagecache(rreq, NULL);
  453. ret = netfs_wait_for_read(rreq);
  454. netfs_put_request(rreq, netfs_rreq_trace_put_return);
  455. return ret < 0 ? ret : 0;
  456. discard:
  457. netfs_put_failed_request(rreq);
  458. alloc_error:
  459. folio_unlock(folio);
  460. return ret;
  461. }
  462. EXPORT_SYMBOL(netfs_read_folio);
  463. /*
  464. * Prepare a folio for writing without reading first
  465. * @folio: The folio being prepared
  466. * @pos: starting position for the write
  467. * @len: length of write
  468. * @always_fill: T if the folio should always be completely filled/cleared
  469. *
  470. * In some cases, write_begin doesn't need to read at all:
  471. * - full folio write
  472. * - write that lies in a folio that is completely beyond EOF
  473. * - write that covers the folio from start to EOF or beyond it
  474. *
  475. * If any of these criteria are met, then zero out the unwritten parts
  476. * of the folio and return true. Otherwise, return false.
  477. */
  478. static bool netfs_skip_folio_read(struct folio *folio, loff_t pos, size_t len,
  479. bool always_fill)
  480. {
  481. struct inode *inode = folio_inode(folio);
  482. loff_t i_size = i_size_read(inode);
  483. size_t offset = offset_in_folio(folio, pos);
  484. size_t plen = folio_size(folio);
  485. if (unlikely(always_fill)) {
  486. if (pos - offset + len <= i_size)
  487. return false; /* Page entirely before EOF */
  488. folio_zero_segment(folio, 0, plen);
  489. folio_mark_uptodate(folio);
  490. return true;
  491. }
  492. /* Full folio write */
  493. if (offset == 0 && len >= plen)
  494. return true;
  495. /* Page entirely beyond the end of the file */
  496. if (pos - offset >= i_size)
  497. goto zero_out;
  498. /* Write that covers from the start of the folio to EOF or beyond */
  499. if (offset == 0 && (pos + len) >= i_size)
  500. goto zero_out;
  501. return false;
  502. zero_out:
  503. folio_zero_segments(folio, 0, offset, offset + len, plen);
  504. return true;
  505. }
  506. /**
  507. * netfs_write_begin - Helper to prepare for writing [DEPRECATED]
  508. * @ctx: The netfs context
  509. * @file: The file to read from
  510. * @mapping: The mapping to read from
  511. * @pos: File position at which the write will begin
  512. * @len: The length of the write (may extend beyond the end of the folio chosen)
  513. * @_folio: Where to put the resultant folio
  514. * @_fsdata: Place for the netfs to store a cookie
  515. *
  516. * Pre-read data for a write-begin request by drawing data from the cache if
  517. * possible, or the netfs if not. Space beyond the EOF is zero-filled.
  518. * Multiple I/O requests from different sources will get munged together.
  519. *
  520. * The calling netfs must provide a table of operations, only one of which,
  521. * issue_read, is mandatory.
  522. *
  523. * The check_write_begin() operation can be provided to check for and flush
  524. * conflicting writes once the folio is grabbed and locked. It is passed a
  525. * pointer to the fsdata cookie that gets returned to the VM to be passed to
  526. * write_end. It is permitted to sleep. It should return 0 if the request
  527. * should go ahead or it may return an error. It may also unlock and put the
  528. * folio, provided it sets ``*foliop`` to NULL, in which case a return of 0
  529. * will cause the folio to be re-got and the process to be retried.
  530. *
  531. * The calling netfs must initialise a netfs context contiguous to the vfs
  532. * inode before calling this.
  533. *
  534. * This is usable whether or not caching is enabled.
  535. *
  536. * Note that this should be considered deprecated and netfs_perform_write()
  537. * used instead.
  538. */
  539. int netfs_write_begin(struct netfs_inode *ctx,
  540. struct file *file, struct address_space *mapping,
  541. loff_t pos, unsigned int len, struct folio **_folio,
  542. void **_fsdata)
  543. {
  544. struct netfs_io_request *rreq;
  545. struct folio *folio;
  546. pgoff_t index = pos >> PAGE_SHIFT;
  547. int ret;
  548. retry:
  549. folio = __filemap_get_folio(mapping, index, FGP_WRITEBEGIN,
  550. mapping_gfp_mask(mapping));
  551. if (IS_ERR(folio))
  552. return PTR_ERR(folio);
  553. if (ctx->ops->check_write_begin) {
  554. /* Allow the netfs (eg. ceph) to flush conflicts. */
  555. ret = ctx->ops->check_write_begin(file, pos, len, &folio, _fsdata);
  556. if (ret < 0) {
  557. trace_netfs_failure(NULL, NULL, ret, netfs_fail_check_write_begin);
  558. goto error;
  559. }
  560. if (!folio)
  561. goto retry;
  562. }
  563. if (folio_test_uptodate(folio))
  564. goto have_folio;
  565. /* If the folio is beyond the EOF, we want to clear it - unless it's
  566. * within the cache granule containing the EOF, in which case we need
  567. * to preload the granule.
  568. */
  569. if (!netfs_is_cache_enabled(ctx) &&
  570. netfs_skip_folio_read(folio, pos, len, false)) {
  571. netfs_stat(&netfs_n_rh_write_zskip);
  572. goto have_folio_no_wait;
  573. }
  574. rreq = netfs_alloc_request(mapping, file,
  575. folio_pos(folio), folio_size(folio),
  576. NETFS_READ_FOR_WRITE);
  577. if (IS_ERR(rreq)) {
  578. ret = PTR_ERR(rreq);
  579. goto error;
  580. }
  581. rreq->no_unlock_folio = folio->index;
  582. __set_bit(NETFS_RREQ_NO_UNLOCK_FOLIO, &rreq->flags);
  583. ret = netfs_begin_cache_read(rreq, ctx);
  584. if (ret == -ENOMEM || ret == -EINTR || ret == -ERESTARTSYS)
  585. goto error_put;
  586. netfs_stat(&netfs_n_rh_write_begin);
  587. trace_netfs_read(rreq, pos, len, netfs_read_trace_write_begin);
  588. /* Set up the output buffer */
  589. ret = netfs_create_singular_buffer(rreq, folio, 0);
  590. if (ret < 0)
  591. goto error_put;
  592. netfs_read_to_pagecache(rreq, NULL);
  593. ret = netfs_wait_for_read(rreq);
  594. if (ret < 0)
  595. goto error;
  596. netfs_put_request(rreq, netfs_rreq_trace_put_return);
  597. have_folio:
  598. ret = folio_wait_private_2_killable(folio);
  599. if (ret < 0)
  600. goto error;
  601. have_folio_no_wait:
  602. *_folio = folio;
  603. _leave(" = 0");
  604. return 0;
  605. error_put:
  606. netfs_put_failed_request(rreq);
  607. error:
  608. if (folio) {
  609. folio_unlock(folio);
  610. folio_put(folio);
  611. }
  612. _leave(" = %d", ret);
  613. return ret;
  614. }
  615. EXPORT_SYMBOL(netfs_write_begin);
  616. /*
  617. * Preload the data into a folio we're proposing to write into.
  618. */
  619. int netfs_prefetch_for_write(struct file *file, struct folio *folio,
  620. size_t offset, size_t len)
  621. {
  622. struct netfs_io_request *rreq;
  623. struct address_space *mapping = folio->mapping;
  624. struct netfs_inode *ctx = netfs_inode(mapping->host);
  625. unsigned long long start = folio_pos(folio);
  626. size_t flen = folio_size(folio);
  627. int ret;
  628. _enter("%zx @%llx", flen, start);
  629. ret = -ENOMEM;
  630. rreq = netfs_alloc_request(mapping, file, start, flen,
  631. NETFS_READ_FOR_WRITE);
  632. if (IS_ERR(rreq)) {
  633. ret = PTR_ERR(rreq);
  634. goto error;
  635. }
  636. rreq->no_unlock_folio = folio->index;
  637. __set_bit(NETFS_RREQ_NO_UNLOCK_FOLIO, &rreq->flags);
  638. ret = netfs_begin_cache_read(rreq, ctx);
  639. if (ret == -ENOMEM || ret == -EINTR || ret == -ERESTARTSYS)
  640. goto error_put;
  641. netfs_stat(&netfs_n_rh_write_begin);
  642. trace_netfs_read(rreq, start, flen, netfs_read_trace_prefetch_for_write);
  643. /* Set up the output buffer */
  644. ret = netfs_create_singular_buffer(rreq, folio, NETFS_ROLLBUF_PAGECACHE_MARK);
  645. if (ret < 0)
  646. goto error_put;
  647. netfs_read_to_pagecache(rreq, NULL);
  648. ret = netfs_wait_for_read(rreq);
  649. netfs_put_request(rreq, netfs_rreq_trace_put_return);
  650. return ret < 0 ? ret : 0;
  651. error_put:
  652. netfs_put_failed_request(rreq);
  653. error:
  654. _leave(" = %d", ret);
  655. return ret;
  656. }
  657. /**
  658. * netfs_buffered_read_iter - Filesystem buffered I/O read routine
  659. * @iocb: kernel I/O control block
  660. * @iter: destination for the data read
  661. *
  662. * This is the ->read_iter() routine for all filesystems that can use the page
  663. * cache directly.
  664. *
  665. * The IOCB_NOWAIT flag in iocb->ki_flags indicates that -EAGAIN shall be
  666. * returned when no data can be read without waiting for I/O requests to
  667. * complete; it doesn't prevent readahead.
  668. *
  669. * The IOCB_NOIO flag in iocb->ki_flags indicates that no new I/O requests
  670. * shall be made for the read or for readahead. When no data can be read,
  671. * -EAGAIN shall be returned. When readahead would be triggered, a partial,
  672. * possibly empty read shall be returned.
  673. *
  674. * Return:
  675. * * number of bytes copied, even for partial reads
  676. * * negative error code (or 0 if IOCB_NOIO) if nothing was read
  677. */
  678. ssize_t netfs_buffered_read_iter(struct kiocb *iocb, struct iov_iter *iter)
  679. {
  680. struct inode *inode = file_inode(iocb->ki_filp);
  681. struct netfs_inode *ictx = netfs_inode(inode);
  682. ssize_t ret;
  683. if (WARN_ON_ONCE((iocb->ki_flags & IOCB_DIRECT) ||
  684. test_bit(NETFS_ICTX_UNBUFFERED, &ictx->flags)))
  685. return -EINVAL;
  686. ret = netfs_start_io_read(inode);
  687. if (ret == 0) {
  688. ret = filemap_read(iocb, iter, 0);
  689. netfs_end_io_read(inode);
  690. }
  691. return ret;
  692. }
  693. EXPORT_SYMBOL(netfs_buffered_read_iter);
  694. /**
  695. * netfs_file_read_iter - Generic filesystem read routine
  696. * @iocb: kernel I/O control block
  697. * @iter: destination for the data read
  698. *
  699. * This is the ->read_iter() routine for all filesystems that can use the page
  700. * cache directly.
  701. *
  702. * The IOCB_NOWAIT flag in iocb->ki_flags indicates that -EAGAIN shall be
  703. * returned when no data can be read without waiting for I/O requests to
  704. * complete; it doesn't prevent readahead.
  705. *
  706. * The IOCB_NOIO flag in iocb->ki_flags indicates that no new I/O requests
  707. * shall be made for the read or for readahead. When no data can be read,
  708. * -EAGAIN shall be returned. When readahead would be triggered, a partial,
  709. * possibly empty read shall be returned.
  710. *
  711. * Return:
  712. * * number of bytes copied, even for partial reads
  713. * * negative error code (or 0 if IOCB_NOIO) if nothing was read
  714. */
  715. ssize_t netfs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
  716. {
  717. struct netfs_inode *ictx = netfs_inode(iocb->ki_filp->f_mapping->host);
  718. if ((iocb->ki_flags & IOCB_DIRECT) ||
  719. test_bit(NETFS_ICTX_UNBUFFERED, &ictx->flags))
  720. return netfs_unbuffered_read_iter(iocb, iter);
  721. return netfs_buffered_read_iter(iocb, iter);
  722. }
  723. EXPORT_SYMBOL(netfs_file_read_iter);