buffered-io.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2010 Red Hat, Inc.
  4. * Copyright (C) 2016-2019 Christoph Hellwig.
  5. */
  6. #include <linux/module.h>
  7. #include <linux/compiler.h>
  8. #include <linux/fs.h>
  9. #include <linux/iomap.h>
  10. #include <linux/pagemap.h>
  11. #include <linux/uio.h>
  12. #include <linux/buffer_head.h>
  13. #include <linux/dax.h>
  14. #include <linux/writeback.h>
  15. #include <linux/list_sort.h>
  16. #include <linux/swap.h>
  17. #include <linux/bio.h>
  18. #include <linux/sched/signal.h>
  19. #include <linux/migrate.h>
  20. #include "trace.h"
  21. #include "../internal.h"
  22. #define IOEND_BATCH_SIZE 4096
  23. /*
  24. * Structure allocated for each folio when block size < folio size
  25. * to track sub-folio uptodate status and I/O completions.
  26. */
  27. struct iomap_page {
  28. atomic_t read_bytes_pending;
  29. atomic_t write_bytes_pending;
  30. spinlock_t uptodate_lock;
  31. unsigned long uptodate[];
  32. };
  33. static inline struct iomap_page *to_iomap_page(struct folio *folio)
  34. {
  35. if (folio_test_private(folio))
  36. return folio_get_private(folio);
  37. return NULL;
  38. }
  39. static struct bio_set iomap_ioend_bioset;
  40. static struct iomap_page *
  41. iomap_page_create(struct inode *inode, struct folio *folio, unsigned int flags)
  42. {
  43. struct iomap_page *iop = to_iomap_page(folio);
  44. unsigned int nr_blocks = i_blocks_per_folio(inode, folio);
  45. gfp_t gfp;
  46. if (iop || nr_blocks <= 1)
  47. return iop;
  48. if (flags & IOMAP_NOWAIT)
  49. gfp = GFP_NOWAIT;
  50. else
  51. gfp = GFP_NOFS | __GFP_NOFAIL;
  52. iop = kzalloc(struct_size(iop, uptodate, BITS_TO_LONGS(nr_blocks)),
  53. gfp);
  54. if (iop) {
  55. spin_lock_init(&iop->uptodate_lock);
  56. if (folio_test_uptodate(folio))
  57. bitmap_fill(iop->uptodate, nr_blocks);
  58. folio_attach_private(folio, iop);
  59. }
  60. return iop;
  61. }
  62. static void iomap_page_release(struct folio *folio)
  63. {
  64. struct iomap_page *iop = folio_detach_private(folio);
  65. struct inode *inode = folio->mapping->host;
  66. unsigned int nr_blocks = i_blocks_per_folio(inode, folio);
  67. if (!iop)
  68. return;
  69. WARN_ON_ONCE(atomic_read(&iop->read_bytes_pending));
  70. WARN_ON_ONCE(atomic_read(&iop->write_bytes_pending));
  71. WARN_ON_ONCE(bitmap_full(iop->uptodate, nr_blocks) !=
  72. folio_test_uptodate(folio));
  73. kfree(iop);
  74. }
  75. /*
  76. * Calculate the range inside the folio that we actually need to read.
  77. */
  78. static void iomap_adjust_read_range(struct inode *inode, struct folio *folio,
  79. loff_t *pos, loff_t length, size_t *offp, size_t *lenp)
  80. {
  81. struct iomap_page *iop = to_iomap_page(folio);
  82. loff_t orig_pos = *pos;
  83. loff_t isize = i_size_read(inode);
  84. unsigned block_bits = inode->i_blkbits;
  85. unsigned block_size = (1 << block_bits);
  86. size_t poff = offset_in_folio(folio, *pos);
  87. size_t plen = min_t(loff_t, folio_size(folio) - poff, length);
  88. unsigned first = poff >> block_bits;
  89. unsigned last = (poff + plen - 1) >> block_bits;
  90. /*
  91. * If the block size is smaller than the page size, we need to check the
  92. * per-block uptodate status and adjust the offset and length if needed
  93. * to avoid reading in already uptodate ranges.
  94. */
  95. if (iop) {
  96. unsigned int i;
  97. /* move forward for each leading block marked uptodate */
  98. for (i = first; i <= last; i++) {
  99. if (!test_bit(i, iop->uptodate))
  100. break;
  101. *pos += block_size;
  102. poff += block_size;
  103. plen -= block_size;
  104. first++;
  105. }
  106. /* truncate len if we find any trailing uptodate block(s) */
  107. for ( ; i <= last; i++) {
  108. if (test_bit(i, iop->uptodate)) {
  109. plen -= (last - i + 1) * block_size;
  110. last = i - 1;
  111. break;
  112. }
  113. }
  114. }
  115. /*
  116. * If the extent spans the block that contains the i_size, we need to
  117. * handle both halves separately so that we properly zero data in the
  118. * page cache for blocks that are entirely outside of i_size.
  119. */
  120. if (orig_pos <= isize && orig_pos + length > isize) {
  121. unsigned end = offset_in_folio(folio, isize - 1) >> block_bits;
  122. if (first <= end && last > end)
  123. plen -= (last - end) * block_size;
  124. }
  125. *offp = poff;
  126. *lenp = plen;
  127. }
  128. static void iomap_iop_set_range_uptodate(struct folio *folio,
  129. struct iomap_page *iop, size_t off, size_t len)
  130. {
  131. struct inode *inode = folio->mapping->host;
  132. unsigned first = off >> inode->i_blkbits;
  133. unsigned last = (off + len - 1) >> inode->i_blkbits;
  134. unsigned long flags;
  135. spin_lock_irqsave(&iop->uptodate_lock, flags);
  136. bitmap_set(iop->uptodate, first, last - first + 1);
  137. if (bitmap_full(iop->uptodate, i_blocks_per_folio(inode, folio)))
  138. folio_mark_uptodate(folio);
  139. spin_unlock_irqrestore(&iop->uptodate_lock, flags);
  140. }
  141. static void iomap_set_range_uptodate(struct folio *folio,
  142. struct iomap_page *iop, size_t off, size_t len)
  143. {
  144. if (iop)
  145. iomap_iop_set_range_uptodate(folio, iop, off, len);
  146. else
  147. folio_mark_uptodate(folio);
  148. }
  149. static void iomap_finish_folio_read(struct folio *folio, size_t offset,
  150. size_t len, int error)
  151. {
  152. struct iomap_page *iop = to_iomap_page(folio);
  153. if (unlikely(error)) {
  154. folio_clear_uptodate(folio);
  155. folio_set_error(folio);
  156. } else {
  157. iomap_set_range_uptodate(folio, iop, offset, len);
  158. }
  159. if (!iop || atomic_sub_and_test(len, &iop->read_bytes_pending))
  160. folio_unlock(folio);
  161. }
  162. static void iomap_read_end_io(struct bio *bio)
  163. {
  164. int error = blk_status_to_errno(bio->bi_status);
  165. struct folio_iter fi;
  166. bio_for_each_folio_all(fi, bio)
  167. iomap_finish_folio_read(fi.folio, fi.offset, fi.length, error);
  168. bio_put(bio);
  169. }
  170. struct iomap_readpage_ctx {
  171. struct folio *cur_folio;
  172. bool cur_folio_in_bio;
  173. struct bio *bio;
  174. struct readahead_control *rac;
  175. };
  176. /**
  177. * iomap_read_inline_data - copy inline data into the page cache
  178. * @iter: iteration structure
  179. * @folio: folio to copy to
  180. *
  181. * Copy the inline data in @iter into @folio and zero out the rest of the folio.
  182. * Only a single IOMAP_INLINE extent is allowed at the end of each file.
  183. * Returns zero for success to complete the read, or the usual negative errno.
  184. */
  185. static int iomap_read_inline_data(const struct iomap_iter *iter,
  186. struct folio *folio)
  187. {
  188. struct iomap_page *iop;
  189. const struct iomap *iomap = iomap_iter_srcmap(iter);
  190. size_t size = i_size_read(iter->inode) - iomap->offset;
  191. size_t poff = offset_in_page(iomap->offset);
  192. size_t offset = offset_in_folio(folio, iomap->offset);
  193. void *addr;
  194. if (folio_test_uptodate(folio))
  195. return 0;
  196. if (WARN_ON_ONCE(size > PAGE_SIZE - poff))
  197. return -EIO;
  198. if (WARN_ON_ONCE(size > PAGE_SIZE -
  199. offset_in_page(iomap->inline_data)))
  200. return -EIO;
  201. if (WARN_ON_ONCE(size > iomap->length))
  202. return -EIO;
  203. if (offset > 0)
  204. iop = iomap_page_create(iter->inode, folio, iter->flags);
  205. else
  206. iop = to_iomap_page(folio);
  207. addr = kmap_local_folio(folio, offset);
  208. memcpy(addr, iomap->inline_data, size);
  209. memset(addr + size, 0, PAGE_SIZE - poff - size);
  210. kunmap_local(addr);
  211. iomap_set_range_uptodate(folio, iop, offset, PAGE_SIZE - poff);
  212. return 0;
  213. }
  214. static inline bool iomap_block_needs_zeroing(const struct iomap_iter *iter,
  215. loff_t pos)
  216. {
  217. const struct iomap *srcmap = iomap_iter_srcmap(iter);
  218. return srcmap->type != IOMAP_MAPPED ||
  219. (srcmap->flags & IOMAP_F_NEW) ||
  220. pos >= i_size_read(iter->inode);
  221. }
  222. static loff_t iomap_readpage_iter(const struct iomap_iter *iter,
  223. struct iomap_readpage_ctx *ctx, loff_t offset)
  224. {
  225. const struct iomap *iomap = &iter->iomap;
  226. loff_t pos = iter->pos + offset;
  227. loff_t length = iomap_length(iter) - offset;
  228. struct folio *folio = ctx->cur_folio;
  229. struct iomap_page *iop;
  230. loff_t orig_pos = pos;
  231. size_t poff, plen;
  232. sector_t sector;
  233. if (iomap->type == IOMAP_INLINE)
  234. return iomap_read_inline_data(iter, folio);
  235. /* zero post-eof blocks as the page may be mapped */
  236. iop = iomap_page_create(iter->inode, folio, iter->flags);
  237. iomap_adjust_read_range(iter->inode, folio, &pos, length, &poff, &plen);
  238. if (plen == 0)
  239. goto done;
  240. if (iomap_block_needs_zeroing(iter, pos)) {
  241. folio_zero_range(folio, poff, plen);
  242. iomap_set_range_uptodate(folio, iop, poff, plen);
  243. goto done;
  244. }
  245. ctx->cur_folio_in_bio = true;
  246. if (iop)
  247. atomic_add(plen, &iop->read_bytes_pending);
  248. sector = iomap_sector(iomap, pos);
  249. if (!ctx->bio ||
  250. bio_end_sector(ctx->bio) != sector ||
  251. !bio_add_folio(ctx->bio, folio, plen, poff)) {
  252. gfp_t gfp = mapping_gfp_constraint(folio->mapping, GFP_KERNEL);
  253. gfp_t orig_gfp = gfp;
  254. unsigned int nr_vecs = DIV_ROUND_UP(length, PAGE_SIZE);
  255. if (ctx->bio)
  256. submit_bio(ctx->bio);
  257. if (ctx->rac) /* same as readahead_gfp_mask */
  258. gfp |= __GFP_NORETRY | __GFP_NOWARN;
  259. ctx->bio = bio_alloc(iomap->bdev, bio_max_segs(nr_vecs),
  260. REQ_OP_READ, gfp);
  261. /*
  262. * If the bio_alloc fails, try it again for a single page to
  263. * avoid having to deal with partial page reads. This emulates
  264. * what do_mpage_read_folio does.
  265. */
  266. if (!ctx->bio) {
  267. ctx->bio = bio_alloc(iomap->bdev, 1, REQ_OP_READ,
  268. orig_gfp);
  269. }
  270. if (ctx->rac)
  271. ctx->bio->bi_opf |= REQ_RAHEAD;
  272. ctx->bio->bi_iter.bi_sector = sector;
  273. ctx->bio->bi_end_io = iomap_read_end_io;
  274. bio_add_folio(ctx->bio, folio, plen, poff);
  275. }
  276. done:
  277. /*
  278. * Move the caller beyond our range so that it keeps making progress.
  279. * For that, we have to include any leading non-uptodate ranges, but
  280. * we can skip trailing ones as they will be handled in the next
  281. * iteration.
  282. */
  283. return pos - orig_pos + plen;
  284. }
  285. int iomap_read_folio(struct folio *folio, const struct iomap_ops *ops)
  286. {
  287. struct iomap_iter iter = {
  288. .inode = folio->mapping->host,
  289. .pos = folio_pos(folio),
  290. .len = folio_size(folio),
  291. };
  292. struct iomap_readpage_ctx ctx = {
  293. .cur_folio = folio,
  294. };
  295. int ret;
  296. trace_iomap_readpage(iter.inode, 1);
  297. while ((ret = iomap_iter(&iter, ops)) > 0)
  298. iter.processed = iomap_readpage_iter(&iter, &ctx, 0);
  299. if (ret < 0)
  300. folio_set_error(folio);
  301. if (ctx.bio) {
  302. submit_bio(ctx.bio);
  303. WARN_ON_ONCE(!ctx.cur_folio_in_bio);
  304. } else {
  305. WARN_ON_ONCE(ctx.cur_folio_in_bio);
  306. folio_unlock(folio);
  307. }
  308. /*
  309. * Just like mpage_readahead and block_read_full_folio, we always
  310. * return 0 and just set the folio error flag on errors. This
  311. * should be cleaned up throughout the stack eventually.
  312. */
  313. return 0;
  314. }
  315. EXPORT_SYMBOL_GPL(iomap_read_folio);
  316. static loff_t iomap_readahead_iter(const struct iomap_iter *iter,
  317. struct iomap_readpage_ctx *ctx)
  318. {
  319. loff_t length = iomap_length(iter);
  320. loff_t done, ret;
  321. for (done = 0; done < length; done += ret) {
  322. if (ctx->cur_folio &&
  323. offset_in_folio(ctx->cur_folio, iter->pos + done) == 0) {
  324. if (!ctx->cur_folio_in_bio)
  325. folio_unlock(ctx->cur_folio);
  326. ctx->cur_folio = NULL;
  327. }
  328. if (!ctx->cur_folio) {
  329. ctx->cur_folio = readahead_folio(ctx->rac);
  330. ctx->cur_folio_in_bio = false;
  331. }
  332. ret = iomap_readpage_iter(iter, ctx, done);
  333. if (ret <= 0)
  334. return ret;
  335. }
  336. return done;
  337. }
  338. /**
  339. * iomap_readahead - Attempt to read pages from a file.
  340. * @rac: Describes the pages to be read.
  341. * @ops: The operations vector for the filesystem.
  342. *
  343. * This function is for filesystems to call to implement their readahead
  344. * address_space operation.
  345. *
  346. * Context: The @ops callbacks may submit I/O (eg to read the addresses of
  347. * blocks from disc), and may wait for it. The caller may be trying to
  348. * access a different page, and so sleeping excessively should be avoided.
  349. * It may allocate memory, but should avoid costly allocations. This
  350. * function is called with memalloc_nofs set, so allocations will not cause
  351. * the filesystem to be reentered.
  352. */
  353. void iomap_readahead(struct readahead_control *rac, const struct iomap_ops *ops)
  354. {
  355. struct iomap_iter iter = {
  356. .inode = rac->mapping->host,
  357. .pos = readahead_pos(rac),
  358. .len = readahead_length(rac),
  359. };
  360. struct iomap_readpage_ctx ctx = {
  361. .rac = rac,
  362. };
  363. trace_iomap_readahead(rac->mapping->host, readahead_count(rac));
  364. while (iomap_iter(&iter, ops) > 0)
  365. iter.processed = iomap_readahead_iter(&iter, &ctx);
  366. if (ctx.bio)
  367. submit_bio(ctx.bio);
  368. if (ctx.cur_folio) {
  369. if (!ctx.cur_folio_in_bio)
  370. folio_unlock(ctx.cur_folio);
  371. }
  372. }
  373. EXPORT_SYMBOL_GPL(iomap_readahead);
  374. /*
  375. * iomap_is_partially_uptodate checks whether blocks within a folio are
  376. * uptodate or not.
  377. *
  378. * Returns true if all blocks which correspond to the specified part
  379. * of the folio are uptodate.
  380. */
  381. bool iomap_is_partially_uptodate(struct folio *folio, size_t from, size_t count)
  382. {
  383. struct iomap_page *iop = to_iomap_page(folio);
  384. struct inode *inode = folio->mapping->host;
  385. unsigned first, last, i;
  386. if (!iop)
  387. return false;
  388. /* Caller's range may extend past the end of this folio */
  389. count = min(folio_size(folio) - from, count);
  390. /* First and last blocks in range within folio */
  391. first = from >> inode->i_blkbits;
  392. last = (from + count - 1) >> inode->i_blkbits;
  393. for (i = first; i <= last; i++)
  394. if (!test_bit(i, iop->uptodate))
  395. return false;
  396. return true;
  397. }
  398. EXPORT_SYMBOL_GPL(iomap_is_partially_uptodate);
  399. bool iomap_release_folio(struct folio *folio, gfp_t gfp_flags)
  400. {
  401. trace_iomap_release_folio(folio->mapping->host, folio_pos(folio),
  402. folio_size(folio));
  403. /*
  404. * mm accommodates an old ext3 case where clean folios might
  405. * not have had the dirty bit cleared. Thus, it can send actual
  406. * dirty folios to ->release_folio() via shrink_active_list();
  407. * skip those here.
  408. */
  409. if (folio_test_dirty(folio) || folio_test_writeback(folio))
  410. return false;
  411. iomap_page_release(folio);
  412. return true;
  413. }
  414. EXPORT_SYMBOL_GPL(iomap_release_folio);
  415. void iomap_invalidate_folio(struct folio *folio, size_t offset, size_t len)
  416. {
  417. trace_iomap_invalidate_folio(folio->mapping->host,
  418. folio_pos(folio) + offset, len);
  419. /*
  420. * If we're invalidating the entire folio, clear the dirty state
  421. * from it and release it to avoid unnecessary buildup of the LRU.
  422. */
  423. if (offset == 0 && len == folio_size(folio)) {
  424. WARN_ON_ONCE(folio_test_writeback(folio));
  425. folio_cancel_dirty(folio);
  426. iomap_page_release(folio);
  427. }
  428. }
  429. EXPORT_SYMBOL_GPL(iomap_invalidate_folio);
  430. static void
  431. iomap_write_failed(struct inode *inode, loff_t pos, unsigned len)
  432. {
  433. loff_t i_size = i_size_read(inode);
  434. /*
  435. * Only truncate newly allocated pages beyoned EOF, even if the
  436. * write started inside the existing inode size.
  437. */
  438. if (pos + len > i_size)
  439. truncate_pagecache_range(inode, max(pos, i_size),
  440. pos + len - 1);
  441. }
  442. static int iomap_read_folio_sync(loff_t block_start, struct folio *folio,
  443. size_t poff, size_t plen, const struct iomap *iomap)
  444. {
  445. struct bio_vec bvec;
  446. struct bio bio;
  447. bio_init(&bio, iomap->bdev, &bvec, 1, REQ_OP_READ);
  448. bio.bi_iter.bi_sector = iomap_sector(iomap, block_start);
  449. bio_add_folio(&bio, folio, plen, poff);
  450. return submit_bio_wait(&bio);
  451. }
  452. static int __iomap_write_begin(const struct iomap_iter *iter, loff_t pos,
  453. size_t len, struct folio *folio)
  454. {
  455. const struct iomap *srcmap = iomap_iter_srcmap(iter);
  456. struct iomap_page *iop;
  457. loff_t block_size = i_blocksize(iter->inode);
  458. loff_t block_start = round_down(pos, block_size);
  459. loff_t block_end = round_up(pos + len, block_size);
  460. unsigned int nr_blocks = i_blocks_per_folio(iter->inode, folio);
  461. size_t from = offset_in_folio(folio, pos), to = from + len;
  462. size_t poff, plen;
  463. if (folio_test_uptodate(folio))
  464. return 0;
  465. folio_clear_error(folio);
  466. iop = iomap_page_create(iter->inode, folio, iter->flags);
  467. if ((iter->flags & IOMAP_NOWAIT) && !iop && nr_blocks > 1)
  468. return -EAGAIN;
  469. do {
  470. iomap_adjust_read_range(iter->inode, folio, &block_start,
  471. block_end - block_start, &poff, &plen);
  472. if (plen == 0)
  473. break;
  474. if (!(iter->flags & IOMAP_UNSHARE) &&
  475. (from <= poff || from >= poff + plen) &&
  476. (to <= poff || to >= poff + plen))
  477. continue;
  478. if (iomap_block_needs_zeroing(iter, block_start)) {
  479. if (WARN_ON_ONCE(iter->flags & IOMAP_UNSHARE))
  480. return -EIO;
  481. folio_zero_segments(folio, poff, from, to, poff + plen);
  482. } else {
  483. int status;
  484. if (iter->flags & IOMAP_NOWAIT)
  485. return -EAGAIN;
  486. status = iomap_read_folio_sync(block_start, folio,
  487. poff, plen, srcmap);
  488. if (status)
  489. return status;
  490. }
  491. iomap_set_range_uptodate(folio, iop, poff, plen);
  492. } while ((block_start += plen) < block_end);
  493. return 0;
  494. }
  495. static int iomap_write_begin_inline(const struct iomap_iter *iter,
  496. struct folio *folio)
  497. {
  498. /* needs more work for the tailpacking case; disable for now */
  499. if (WARN_ON_ONCE(iomap_iter_srcmap(iter)->offset != 0))
  500. return -EIO;
  501. return iomap_read_inline_data(iter, folio);
  502. }
  503. static int iomap_write_begin(const struct iomap_iter *iter, loff_t pos,
  504. size_t len, struct folio **foliop)
  505. {
  506. const struct iomap_page_ops *page_ops = iter->iomap.page_ops;
  507. const struct iomap *srcmap = iomap_iter_srcmap(iter);
  508. struct folio *folio;
  509. unsigned fgp = FGP_LOCK | FGP_WRITE | FGP_CREAT | FGP_STABLE | FGP_NOFS;
  510. int status = 0;
  511. if (iter->flags & IOMAP_NOWAIT)
  512. fgp |= FGP_NOWAIT;
  513. BUG_ON(pos + len > iter->iomap.offset + iter->iomap.length);
  514. if (srcmap != &iter->iomap)
  515. BUG_ON(pos + len > srcmap->offset + srcmap->length);
  516. if (fatal_signal_pending(current))
  517. return -EINTR;
  518. if (!mapping_large_folio_support(iter->inode->i_mapping))
  519. len = min_t(size_t, len, PAGE_SIZE - offset_in_page(pos));
  520. if (page_ops && page_ops->page_prepare) {
  521. status = page_ops->page_prepare(iter->inode, pos, len);
  522. if (status)
  523. return status;
  524. }
  525. folio = __filemap_get_folio(iter->inode->i_mapping, pos >> PAGE_SHIFT,
  526. fgp, mapping_gfp_mask(iter->inode->i_mapping));
  527. if (!folio) {
  528. status = (iter->flags & IOMAP_NOWAIT) ? -EAGAIN : -ENOMEM;
  529. goto out_no_page;
  530. }
  531. if (pos + len > folio_pos(folio) + folio_size(folio))
  532. len = folio_pos(folio) + folio_size(folio) - pos;
  533. if (srcmap->type == IOMAP_INLINE)
  534. status = iomap_write_begin_inline(iter, folio);
  535. else if (srcmap->flags & IOMAP_F_BUFFER_HEAD)
  536. status = __block_write_begin_int(folio, pos, len, NULL, srcmap);
  537. else
  538. status = __iomap_write_begin(iter, pos, len, folio);
  539. if (unlikely(status))
  540. goto out_unlock;
  541. *foliop = folio;
  542. return 0;
  543. out_unlock:
  544. folio_unlock(folio);
  545. folio_put(folio);
  546. iomap_write_failed(iter->inode, pos, len);
  547. out_no_page:
  548. if (page_ops && page_ops->page_done)
  549. page_ops->page_done(iter->inode, pos, 0, NULL);
  550. return status;
  551. }
  552. static size_t __iomap_write_end(struct inode *inode, loff_t pos, size_t len,
  553. size_t copied, struct folio *folio)
  554. {
  555. struct iomap_page *iop = to_iomap_page(folio);
  556. flush_dcache_folio(folio);
  557. /*
  558. * The blocks that were entirely written will now be uptodate, so we
  559. * don't have to worry about a read_folio reading them and overwriting a
  560. * partial write. However, if we've encountered a short write and only
  561. * partially written into a block, it will not be marked uptodate, so a
  562. * read_folio might come in and destroy our partial write.
  563. *
  564. * Do the simplest thing and just treat any short write to a
  565. * non-uptodate page as a zero-length write, and force the caller to
  566. * redo the whole thing.
  567. */
  568. if (unlikely(copied < len && !folio_test_uptodate(folio)))
  569. return 0;
  570. iomap_set_range_uptodate(folio, iop, offset_in_folio(folio, pos), len);
  571. filemap_dirty_folio(inode->i_mapping, folio);
  572. return copied;
  573. }
  574. static size_t iomap_write_end_inline(const struct iomap_iter *iter,
  575. struct folio *folio, loff_t pos, size_t copied)
  576. {
  577. const struct iomap *iomap = &iter->iomap;
  578. void *addr;
  579. WARN_ON_ONCE(!folio_test_uptodate(folio));
  580. BUG_ON(!iomap_inline_data_valid(iomap));
  581. flush_dcache_folio(folio);
  582. addr = kmap_local_folio(folio, pos);
  583. memcpy(iomap_inline_data(iomap, pos), addr, copied);
  584. kunmap_local(addr);
  585. mark_inode_dirty(iter->inode);
  586. return copied;
  587. }
  588. /* Returns the number of bytes copied. May be 0. Cannot be an errno. */
  589. static size_t iomap_write_end(struct iomap_iter *iter, loff_t pos, size_t len,
  590. size_t copied, struct folio *folio)
  591. {
  592. const struct iomap_page_ops *page_ops = iter->iomap.page_ops;
  593. const struct iomap *srcmap = iomap_iter_srcmap(iter);
  594. loff_t old_size = iter->inode->i_size;
  595. size_t ret;
  596. if (srcmap->type == IOMAP_INLINE) {
  597. ret = iomap_write_end_inline(iter, folio, pos, copied);
  598. } else if (srcmap->flags & IOMAP_F_BUFFER_HEAD) {
  599. ret = block_write_end(NULL, iter->inode->i_mapping, pos, len,
  600. copied, &folio->page, NULL);
  601. } else {
  602. ret = __iomap_write_end(iter->inode, pos, len, copied, folio);
  603. }
  604. /*
  605. * Update the in-memory inode size after copying the data into the page
  606. * cache. It's up to the file system to write the updated size to disk,
  607. * preferably after I/O completion so that no stale data is exposed.
  608. */
  609. if (pos + ret > old_size) {
  610. i_size_write(iter->inode, pos + ret);
  611. iter->iomap.flags |= IOMAP_F_SIZE_CHANGED;
  612. }
  613. folio_unlock(folio);
  614. if (old_size < pos)
  615. pagecache_isize_extended(iter->inode, old_size, pos);
  616. if (page_ops && page_ops->page_done)
  617. page_ops->page_done(iter->inode, pos, ret, &folio->page);
  618. folio_put(folio);
  619. if (ret < len)
  620. iomap_write_failed(iter->inode, pos + ret, len - ret);
  621. return ret;
  622. }
  623. static loff_t iomap_write_iter(struct iomap_iter *iter, struct iov_iter *i)
  624. {
  625. loff_t length = iomap_length(iter);
  626. loff_t pos = iter->pos;
  627. ssize_t written = 0;
  628. long status = 0;
  629. struct address_space *mapping = iter->inode->i_mapping;
  630. unsigned int bdp_flags = (iter->flags & IOMAP_NOWAIT) ? BDP_ASYNC : 0;
  631. do {
  632. struct folio *folio;
  633. struct page *page;
  634. unsigned long offset; /* Offset into pagecache page */
  635. unsigned long bytes; /* Bytes to write to page */
  636. size_t copied; /* Bytes copied from user */
  637. offset = offset_in_page(pos);
  638. bytes = min_t(unsigned long, PAGE_SIZE - offset,
  639. iov_iter_count(i));
  640. again:
  641. status = balance_dirty_pages_ratelimited_flags(mapping,
  642. bdp_flags);
  643. if (unlikely(status))
  644. break;
  645. if (bytes > length)
  646. bytes = length;
  647. /*
  648. * Bring in the user page that we'll copy from _first_.
  649. * Otherwise there's a nasty deadlock on copying from the
  650. * same page as we're writing to, without it being marked
  651. * up-to-date.
  652. *
  653. * For async buffered writes the assumption is that the user
  654. * page has already been faulted in. This can be optimized by
  655. * faulting the user page.
  656. */
  657. if (unlikely(fault_in_iov_iter_readable(i, bytes) == bytes)) {
  658. status = -EFAULT;
  659. break;
  660. }
  661. status = iomap_write_begin(iter, pos, bytes, &folio);
  662. if (unlikely(status))
  663. break;
  664. page = folio_file_page(folio, pos >> PAGE_SHIFT);
  665. if (mapping_writably_mapped(mapping))
  666. flush_dcache_page(page);
  667. copied = copy_page_from_iter_atomic(page, offset, bytes, i);
  668. status = iomap_write_end(iter, pos, bytes, copied, folio);
  669. if (unlikely(copied != status))
  670. iov_iter_revert(i, copied - status);
  671. cond_resched();
  672. if (unlikely(status == 0)) {
  673. /*
  674. * A short copy made iomap_write_end() reject the
  675. * thing entirely. Might be memory poisoning
  676. * halfway through, might be a race with munmap,
  677. * might be severe memory pressure.
  678. */
  679. if (copied)
  680. bytes = copied;
  681. goto again;
  682. }
  683. pos += status;
  684. written += status;
  685. length -= status;
  686. } while (iov_iter_count(i) && length);
  687. if (status == -EAGAIN) {
  688. iov_iter_revert(i, written);
  689. return -EAGAIN;
  690. }
  691. return written ? written : status;
  692. }
  693. ssize_t
  694. iomap_file_buffered_write(struct kiocb *iocb, struct iov_iter *i,
  695. const struct iomap_ops *ops)
  696. {
  697. struct iomap_iter iter = {
  698. .inode = iocb->ki_filp->f_mapping->host,
  699. .pos = iocb->ki_pos,
  700. .len = iov_iter_count(i),
  701. .flags = IOMAP_WRITE,
  702. };
  703. int ret;
  704. if (iocb->ki_flags & IOCB_NOWAIT)
  705. iter.flags |= IOMAP_NOWAIT;
  706. while ((ret = iomap_iter(&iter, ops)) > 0)
  707. iter.processed = iomap_write_iter(&iter, i);
  708. if (iter.pos == iocb->ki_pos)
  709. return ret;
  710. return iter.pos - iocb->ki_pos;
  711. }
  712. EXPORT_SYMBOL_GPL(iomap_file_buffered_write);
  713. static loff_t iomap_unshare_iter(struct iomap_iter *iter)
  714. {
  715. struct iomap *iomap = &iter->iomap;
  716. const struct iomap *srcmap = iomap_iter_srcmap(iter);
  717. loff_t pos = iter->pos;
  718. loff_t length = iomap_length(iter);
  719. long status = 0;
  720. loff_t written = 0;
  721. /* don't bother with blocks that are not shared to start with */
  722. if (!(iomap->flags & IOMAP_F_SHARED))
  723. return length;
  724. /* don't bother with holes or unwritten extents */
  725. if (srcmap->type == IOMAP_HOLE || srcmap->type == IOMAP_UNWRITTEN)
  726. return length;
  727. do {
  728. unsigned long offset = offset_in_page(pos);
  729. unsigned long bytes = min_t(loff_t, PAGE_SIZE - offset, length);
  730. struct folio *folio;
  731. status = iomap_write_begin(iter, pos, bytes, &folio);
  732. if (unlikely(status))
  733. return status;
  734. status = iomap_write_end(iter, pos, bytes, bytes, folio);
  735. if (WARN_ON_ONCE(status == 0))
  736. return -EIO;
  737. cond_resched();
  738. pos += status;
  739. written += status;
  740. length -= status;
  741. balance_dirty_pages_ratelimited(iter->inode->i_mapping);
  742. } while (length);
  743. return written;
  744. }
  745. int
  746. iomap_file_unshare(struct inode *inode, loff_t pos, loff_t len,
  747. const struct iomap_ops *ops)
  748. {
  749. struct iomap_iter iter = {
  750. .inode = inode,
  751. .pos = pos,
  752. .len = len,
  753. .flags = IOMAP_WRITE | IOMAP_UNSHARE,
  754. };
  755. int ret;
  756. while ((ret = iomap_iter(&iter, ops)) > 0)
  757. iter.processed = iomap_unshare_iter(&iter);
  758. return ret;
  759. }
  760. EXPORT_SYMBOL_GPL(iomap_file_unshare);
  761. static loff_t iomap_zero_iter(struct iomap_iter *iter, bool *did_zero)
  762. {
  763. const struct iomap *srcmap = iomap_iter_srcmap(iter);
  764. loff_t pos = iter->pos;
  765. loff_t length = iomap_length(iter);
  766. loff_t written = 0;
  767. /* already zeroed? we're done. */
  768. if (srcmap->type == IOMAP_HOLE || srcmap->type == IOMAP_UNWRITTEN)
  769. return length;
  770. do {
  771. struct folio *folio;
  772. int status;
  773. size_t offset;
  774. size_t bytes = min_t(u64, SIZE_MAX, length);
  775. status = iomap_write_begin(iter, pos, bytes, &folio);
  776. if (status)
  777. return status;
  778. offset = offset_in_folio(folio, pos);
  779. if (bytes > folio_size(folio) - offset)
  780. bytes = folio_size(folio) - offset;
  781. folio_zero_range(folio, offset, bytes);
  782. folio_mark_accessed(folio);
  783. bytes = iomap_write_end(iter, pos, bytes, bytes, folio);
  784. if (WARN_ON_ONCE(bytes == 0))
  785. return -EIO;
  786. pos += bytes;
  787. length -= bytes;
  788. written += bytes;
  789. } while (length > 0);
  790. if (did_zero)
  791. *did_zero = true;
  792. return written;
  793. }
  794. int
  795. iomap_zero_range(struct inode *inode, loff_t pos, loff_t len, bool *did_zero,
  796. const struct iomap_ops *ops)
  797. {
  798. struct iomap_iter iter = {
  799. .inode = inode,
  800. .pos = pos,
  801. .len = len,
  802. .flags = IOMAP_ZERO,
  803. };
  804. int ret;
  805. while ((ret = iomap_iter(&iter, ops)) > 0)
  806. iter.processed = iomap_zero_iter(&iter, did_zero);
  807. return ret;
  808. }
  809. EXPORT_SYMBOL_GPL(iomap_zero_range);
  810. int
  811. iomap_truncate_page(struct inode *inode, loff_t pos, bool *did_zero,
  812. const struct iomap_ops *ops)
  813. {
  814. unsigned int blocksize = i_blocksize(inode);
  815. unsigned int off = pos & (blocksize - 1);
  816. /* Block boundary? Nothing to do */
  817. if (!off)
  818. return 0;
  819. return iomap_zero_range(inode, pos, blocksize - off, did_zero, ops);
  820. }
  821. EXPORT_SYMBOL_GPL(iomap_truncate_page);
  822. static loff_t iomap_folio_mkwrite_iter(struct iomap_iter *iter,
  823. struct folio *folio)
  824. {
  825. loff_t length = iomap_length(iter);
  826. int ret;
  827. if (iter->iomap.flags & IOMAP_F_BUFFER_HEAD) {
  828. ret = __block_write_begin_int(folio, iter->pos, length, NULL,
  829. &iter->iomap);
  830. if (ret)
  831. return ret;
  832. block_commit_write(&folio->page, 0, length);
  833. } else {
  834. WARN_ON_ONCE(!folio_test_uptodate(folio));
  835. folio_mark_dirty(folio);
  836. }
  837. return length;
  838. }
  839. vm_fault_t iomap_page_mkwrite(struct vm_fault *vmf, const struct iomap_ops *ops)
  840. {
  841. struct iomap_iter iter = {
  842. .inode = file_inode(vmf->vma->vm_file),
  843. .flags = IOMAP_WRITE | IOMAP_FAULT,
  844. };
  845. struct folio *folio = page_folio(vmf->page);
  846. ssize_t ret;
  847. folio_lock(folio);
  848. ret = folio_mkwrite_check_truncate(folio, iter.inode);
  849. if (ret < 0)
  850. goto out_unlock;
  851. iter.pos = folio_pos(folio);
  852. iter.len = ret;
  853. while ((ret = iomap_iter(&iter, ops)) > 0)
  854. iter.processed = iomap_folio_mkwrite_iter(&iter, folio);
  855. if (ret < 0)
  856. goto out_unlock;
  857. folio_wait_stable(folio);
  858. return VM_FAULT_LOCKED;
  859. out_unlock:
  860. folio_unlock(folio);
  861. return block_page_mkwrite_return(ret);
  862. }
  863. EXPORT_SYMBOL_GPL(iomap_page_mkwrite);
  864. static void iomap_finish_folio_write(struct inode *inode, struct folio *folio,
  865. size_t len, int error)
  866. {
  867. struct iomap_page *iop = to_iomap_page(folio);
  868. if (error) {
  869. folio_set_error(folio);
  870. mapping_set_error(inode->i_mapping, error);
  871. }
  872. WARN_ON_ONCE(i_blocks_per_folio(inode, folio) > 1 && !iop);
  873. WARN_ON_ONCE(iop && atomic_read(&iop->write_bytes_pending) <= 0);
  874. if (!iop || atomic_sub_and_test(len, &iop->write_bytes_pending))
  875. folio_end_writeback(folio);
  876. }
  877. /*
  878. * We're now finished for good with this ioend structure. Update the page
  879. * state, release holds on bios, and finally free up memory. Do not use the
  880. * ioend after this.
  881. */
  882. static u32
  883. iomap_finish_ioend(struct iomap_ioend *ioend, int error)
  884. {
  885. struct inode *inode = ioend->io_inode;
  886. struct bio *bio = &ioend->io_inline_bio;
  887. struct bio *last = ioend->io_bio, *next;
  888. u64 start = bio->bi_iter.bi_sector;
  889. loff_t offset = ioend->io_offset;
  890. bool quiet = bio_flagged(bio, BIO_QUIET);
  891. u32 folio_count = 0;
  892. for (bio = &ioend->io_inline_bio; bio; bio = next) {
  893. struct folio_iter fi;
  894. /*
  895. * For the last bio, bi_private points to the ioend, so we
  896. * need to explicitly end the iteration here.
  897. */
  898. if (bio == last)
  899. next = NULL;
  900. else
  901. next = bio->bi_private;
  902. /* walk all folios in bio, ending page IO on them */
  903. bio_for_each_folio_all(fi, bio) {
  904. iomap_finish_folio_write(inode, fi.folio, fi.length,
  905. error);
  906. folio_count++;
  907. }
  908. bio_put(bio);
  909. }
  910. /* The ioend has been freed by bio_put() */
  911. if (unlikely(error && !quiet)) {
  912. printk_ratelimited(KERN_ERR
  913. "%s: writeback error on inode %lu, offset %lld, sector %llu",
  914. inode->i_sb->s_id, inode->i_ino, offset, start);
  915. }
  916. return folio_count;
  917. }
  918. /*
  919. * Ioend completion routine for merged bios. This can only be called from task
  920. * contexts as merged ioends can be of unbound length. Hence we have to break up
  921. * the writeback completions into manageable chunks to avoid long scheduler
  922. * holdoffs. We aim to keep scheduler holdoffs down below 10ms so that we get
  923. * good batch processing throughput without creating adverse scheduler latency
  924. * conditions.
  925. */
  926. void
  927. iomap_finish_ioends(struct iomap_ioend *ioend, int error)
  928. {
  929. struct list_head tmp;
  930. u32 completions;
  931. might_sleep();
  932. list_replace_init(&ioend->io_list, &tmp);
  933. completions = iomap_finish_ioend(ioend, error);
  934. while (!list_empty(&tmp)) {
  935. if (completions > IOEND_BATCH_SIZE * 8) {
  936. cond_resched();
  937. completions = 0;
  938. }
  939. ioend = list_first_entry(&tmp, struct iomap_ioend, io_list);
  940. list_del_init(&ioend->io_list);
  941. completions += iomap_finish_ioend(ioend, error);
  942. }
  943. }
  944. EXPORT_SYMBOL_GPL(iomap_finish_ioends);
  945. /*
  946. * We can merge two adjacent ioends if they have the same set of work to do.
  947. */
  948. static bool
  949. iomap_ioend_can_merge(struct iomap_ioend *ioend, struct iomap_ioend *next)
  950. {
  951. if (ioend->io_bio->bi_status != next->io_bio->bi_status)
  952. return false;
  953. if ((ioend->io_flags & IOMAP_F_SHARED) ^
  954. (next->io_flags & IOMAP_F_SHARED))
  955. return false;
  956. if ((ioend->io_type == IOMAP_UNWRITTEN) ^
  957. (next->io_type == IOMAP_UNWRITTEN))
  958. return false;
  959. if (ioend->io_offset + ioend->io_size != next->io_offset)
  960. return false;
  961. /*
  962. * Do not merge physically discontiguous ioends. The filesystem
  963. * completion functions will have to iterate the physical
  964. * discontiguities even if we merge the ioends at a logical level, so
  965. * we don't gain anything by merging physical discontiguities here.
  966. *
  967. * We cannot use bio->bi_iter.bi_sector here as it is modified during
  968. * submission so does not point to the start sector of the bio at
  969. * completion.
  970. */
  971. if (ioend->io_sector + (ioend->io_size >> 9) != next->io_sector)
  972. return false;
  973. return true;
  974. }
  975. void
  976. iomap_ioend_try_merge(struct iomap_ioend *ioend, struct list_head *more_ioends)
  977. {
  978. struct iomap_ioend *next;
  979. INIT_LIST_HEAD(&ioend->io_list);
  980. while ((next = list_first_entry_or_null(more_ioends, struct iomap_ioend,
  981. io_list))) {
  982. if (!iomap_ioend_can_merge(ioend, next))
  983. break;
  984. list_move_tail(&next->io_list, &ioend->io_list);
  985. ioend->io_size += next->io_size;
  986. }
  987. }
  988. EXPORT_SYMBOL_GPL(iomap_ioend_try_merge);
  989. static int
  990. iomap_ioend_compare(void *priv, const struct list_head *a,
  991. const struct list_head *b)
  992. {
  993. struct iomap_ioend *ia = container_of(a, struct iomap_ioend, io_list);
  994. struct iomap_ioend *ib = container_of(b, struct iomap_ioend, io_list);
  995. if (ia->io_offset < ib->io_offset)
  996. return -1;
  997. if (ia->io_offset > ib->io_offset)
  998. return 1;
  999. return 0;
  1000. }
  1001. void
  1002. iomap_sort_ioends(struct list_head *ioend_list)
  1003. {
  1004. list_sort(NULL, ioend_list, iomap_ioend_compare);
  1005. }
  1006. EXPORT_SYMBOL_GPL(iomap_sort_ioends);
  1007. static void iomap_writepage_end_bio(struct bio *bio)
  1008. {
  1009. struct iomap_ioend *ioend = bio->bi_private;
  1010. iomap_finish_ioend(ioend, blk_status_to_errno(bio->bi_status));
  1011. }
  1012. /*
  1013. * Submit the final bio for an ioend.
  1014. *
  1015. * If @error is non-zero, it means that we have a situation where some part of
  1016. * the submission process has failed after we've marked pages for writeback
  1017. * and unlocked them. In this situation, we need to fail the bio instead of
  1018. * submitting it. This typically only happens on a filesystem shutdown.
  1019. */
  1020. static int
  1021. iomap_submit_ioend(struct iomap_writepage_ctx *wpc, struct iomap_ioend *ioend,
  1022. int error)
  1023. {
  1024. ioend->io_bio->bi_private = ioend;
  1025. ioend->io_bio->bi_end_io = iomap_writepage_end_bio;
  1026. if (wpc->ops->prepare_ioend)
  1027. error = wpc->ops->prepare_ioend(ioend, error);
  1028. if (error) {
  1029. /*
  1030. * If we're failing the IO now, just mark the ioend with an
  1031. * error and finish it. This will run IO completion immediately
  1032. * as there is only one reference to the ioend at this point in
  1033. * time.
  1034. */
  1035. ioend->io_bio->bi_status = errno_to_blk_status(error);
  1036. bio_endio(ioend->io_bio);
  1037. return error;
  1038. }
  1039. submit_bio(ioend->io_bio);
  1040. return 0;
  1041. }
  1042. static struct iomap_ioend *
  1043. iomap_alloc_ioend(struct inode *inode, struct iomap_writepage_ctx *wpc,
  1044. loff_t offset, sector_t sector, struct writeback_control *wbc)
  1045. {
  1046. struct iomap_ioend *ioend;
  1047. struct bio *bio;
  1048. bio = bio_alloc_bioset(wpc->iomap.bdev, BIO_MAX_VECS,
  1049. REQ_OP_WRITE | wbc_to_write_flags(wbc),
  1050. GFP_NOFS, &iomap_ioend_bioset);
  1051. bio->bi_iter.bi_sector = sector;
  1052. wbc_init_bio(wbc, bio);
  1053. ioend = container_of(bio, struct iomap_ioend, io_inline_bio);
  1054. INIT_LIST_HEAD(&ioend->io_list);
  1055. ioend->io_type = wpc->iomap.type;
  1056. ioend->io_flags = wpc->iomap.flags;
  1057. ioend->io_inode = inode;
  1058. ioend->io_size = 0;
  1059. ioend->io_folios = 0;
  1060. ioend->io_offset = offset;
  1061. ioend->io_bio = bio;
  1062. ioend->io_sector = sector;
  1063. return ioend;
  1064. }
  1065. /*
  1066. * Allocate a new bio, and chain the old bio to the new one.
  1067. *
  1068. * Note that we have to perform the chaining in this unintuitive order
  1069. * so that the bi_private linkage is set up in the right direction for the
  1070. * traversal in iomap_finish_ioend().
  1071. */
  1072. static struct bio *
  1073. iomap_chain_bio(struct bio *prev)
  1074. {
  1075. struct bio *new;
  1076. new = bio_alloc(prev->bi_bdev, BIO_MAX_VECS, prev->bi_opf, GFP_NOFS);
  1077. bio_clone_blkg_association(new, prev);
  1078. new->bi_iter.bi_sector = bio_end_sector(prev);
  1079. bio_chain(prev, new);
  1080. bio_get(prev); /* for iomap_finish_ioend */
  1081. submit_bio(prev);
  1082. return new;
  1083. }
  1084. static bool
  1085. iomap_can_add_to_ioend(struct iomap_writepage_ctx *wpc, loff_t offset,
  1086. sector_t sector)
  1087. {
  1088. if ((wpc->iomap.flags & IOMAP_F_SHARED) !=
  1089. (wpc->ioend->io_flags & IOMAP_F_SHARED))
  1090. return false;
  1091. if (wpc->iomap.type != wpc->ioend->io_type)
  1092. return false;
  1093. if (offset != wpc->ioend->io_offset + wpc->ioend->io_size)
  1094. return false;
  1095. if (sector != bio_end_sector(wpc->ioend->io_bio))
  1096. return false;
  1097. /*
  1098. * Limit ioend bio chain lengths to minimise IO completion latency. This
  1099. * also prevents long tight loops ending page writeback on all the
  1100. * folios in the ioend.
  1101. */
  1102. if (wpc->ioend->io_folios >= IOEND_BATCH_SIZE)
  1103. return false;
  1104. return true;
  1105. }
  1106. /*
  1107. * Test to see if we have an existing ioend structure that we could append to
  1108. * first; otherwise finish off the current ioend and start another.
  1109. */
  1110. static void
  1111. iomap_add_to_ioend(struct inode *inode, loff_t pos, struct folio *folio,
  1112. struct iomap_page *iop, struct iomap_writepage_ctx *wpc,
  1113. struct writeback_control *wbc, struct list_head *iolist)
  1114. {
  1115. sector_t sector = iomap_sector(&wpc->iomap, pos);
  1116. unsigned len = i_blocksize(inode);
  1117. size_t poff = offset_in_folio(folio, pos);
  1118. if (!wpc->ioend || !iomap_can_add_to_ioend(wpc, pos, sector)) {
  1119. if (wpc->ioend)
  1120. list_add(&wpc->ioend->io_list, iolist);
  1121. wpc->ioend = iomap_alloc_ioend(inode, wpc, pos, sector, wbc);
  1122. }
  1123. if (!bio_add_folio(wpc->ioend->io_bio, folio, len, poff)) {
  1124. wpc->ioend->io_bio = iomap_chain_bio(wpc->ioend->io_bio);
  1125. bio_add_folio(wpc->ioend->io_bio, folio, len, poff);
  1126. }
  1127. if (iop)
  1128. atomic_add(len, &iop->write_bytes_pending);
  1129. wpc->ioend->io_size += len;
  1130. wbc_account_cgroup_owner(wbc, &folio->page, len);
  1131. }
  1132. /*
  1133. * We implement an immediate ioend submission policy here to avoid needing to
  1134. * chain multiple ioends and hence nest mempool allocations which can violate
  1135. * the forward progress guarantees we need to provide. The current ioend we're
  1136. * adding blocks to is cached in the writepage context, and if the new block
  1137. * doesn't append to the cached ioend, it will create a new ioend and cache that
  1138. * instead.
  1139. *
  1140. * If a new ioend is created and cached, the old ioend is returned and queued
  1141. * locally for submission once the entire page is processed or an error has been
  1142. * detected. While ioends are submitted immediately after they are completed,
  1143. * batching optimisations are provided by higher level block plugging.
  1144. *
  1145. * At the end of a writeback pass, there will be a cached ioend remaining on the
  1146. * writepage context that the caller will need to submit.
  1147. */
  1148. static int
  1149. iomap_writepage_map(struct iomap_writepage_ctx *wpc,
  1150. struct writeback_control *wbc, struct inode *inode,
  1151. struct folio *folio, u64 end_pos)
  1152. {
  1153. struct iomap_page *iop = iomap_page_create(inode, folio, 0);
  1154. struct iomap_ioend *ioend, *next;
  1155. unsigned len = i_blocksize(inode);
  1156. unsigned nblocks = i_blocks_per_folio(inode, folio);
  1157. u64 pos = folio_pos(folio);
  1158. int error = 0, count = 0, i;
  1159. LIST_HEAD(submit_list);
  1160. WARN_ON_ONCE(iop && atomic_read(&iop->write_bytes_pending) != 0);
  1161. /*
  1162. * Walk through the folio to find areas to write back. If we
  1163. * run off the end of the current map or find the current map
  1164. * invalid, grab a new one.
  1165. */
  1166. for (i = 0; i < nblocks && pos < end_pos; i++, pos += len) {
  1167. if (iop && !test_bit(i, iop->uptodate))
  1168. continue;
  1169. error = wpc->ops->map_blocks(wpc, inode, pos);
  1170. if (error)
  1171. break;
  1172. trace_iomap_writepage_map(inode, &wpc->iomap);
  1173. if (WARN_ON_ONCE(wpc->iomap.type == IOMAP_INLINE))
  1174. continue;
  1175. if (wpc->iomap.type == IOMAP_HOLE)
  1176. continue;
  1177. iomap_add_to_ioend(inode, pos, folio, iop, wpc, wbc,
  1178. &submit_list);
  1179. count++;
  1180. }
  1181. if (count)
  1182. wpc->ioend->io_folios++;
  1183. WARN_ON_ONCE(!wpc->ioend && !list_empty(&submit_list));
  1184. WARN_ON_ONCE(!folio_test_locked(folio));
  1185. WARN_ON_ONCE(folio_test_writeback(folio));
  1186. WARN_ON_ONCE(folio_test_dirty(folio));
  1187. /*
  1188. * We cannot cancel the ioend directly here on error. We may have
  1189. * already set other pages under writeback and hence we have to run I/O
  1190. * completion to mark the error state of the pages under writeback
  1191. * appropriately.
  1192. */
  1193. if (unlikely(error)) {
  1194. /*
  1195. * Let the filesystem know what portion of the current page
  1196. * failed to map. If the page hasn't been added to ioend, it
  1197. * won't be affected by I/O completion and we must unlock it
  1198. * now.
  1199. */
  1200. if (wpc->ops->discard_folio)
  1201. wpc->ops->discard_folio(folio, pos);
  1202. if (!count) {
  1203. folio_unlock(folio);
  1204. goto done;
  1205. }
  1206. }
  1207. folio_start_writeback(folio);
  1208. folio_unlock(folio);
  1209. /*
  1210. * Preserve the original error if there was one; catch
  1211. * submission errors here and propagate into subsequent ioend
  1212. * submissions.
  1213. */
  1214. list_for_each_entry_safe(ioend, next, &submit_list, io_list) {
  1215. int error2;
  1216. list_del_init(&ioend->io_list);
  1217. error2 = iomap_submit_ioend(wpc, ioend, error);
  1218. if (error2 && !error)
  1219. error = error2;
  1220. }
  1221. /*
  1222. * We can end up here with no error and nothing to write only if we race
  1223. * with a partial page truncate on a sub-page block sized filesystem.
  1224. */
  1225. if (!count)
  1226. folio_end_writeback(folio);
  1227. done:
  1228. mapping_set_error(inode->i_mapping, error);
  1229. return error;
  1230. }
  1231. /*
  1232. * Write out a dirty page.
  1233. *
  1234. * For delalloc space on the page, we need to allocate space and flush it.
  1235. * For unwritten space on the page, we need to start the conversion to
  1236. * regular allocated space.
  1237. */
  1238. static int
  1239. iomap_do_writepage(struct page *page, struct writeback_control *wbc, void *data)
  1240. {
  1241. struct folio *folio = page_folio(page);
  1242. struct iomap_writepage_ctx *wpc = data;
  1243. struct inode *inode = folio->mapping->host;
  1244. u64 end_pos, isize;
  1245. trace_iomap_writepage(inode, folio_pos(folio), folio_size(folio));
  1246. /*
  1247. * Refuse to write the folio out if we're called from reclaim context.
  1248. *
  1249. * This avoids stack overflows when called from deeply used stacks in
  1250. * random callers for direct reclaim or memcg reclaim. We explicitly
  1251. * allow reclaim from kswapd as the stack usage there is relatively low.
  1252. *
  1253. * This should never happen except in the case of a VM regression so
  1254. * warn about it.
  1255. */
  1256. if (WARN_ON_ONCE((current->flags & (PF_MEMALLOC|PF_KSWAPD)) ==
  1257. PF_MEMALLOC))
  1258. goto redirty;
  1259. /*
  1260. * Is this folio beyond the end of the file?
  1261. *
  1262. * The folio index is less than the end_index, adjust the end_pos
  1263. * to the highest offset that this folio should represent.
  1264. * -----------------------------------------------------
  1265. * | file mapping | <EOF> |
  1266. * -----------------------------------------------------
  1267. * | Page ... | Page N-2 | Page N-1 | Page N | |
  1268. * ^--------------------------------^----------|--------
  1269. * | desired writeback range | see else |
  1270. * ---------------------------------^------------------|
  1271. */
  1272. isize = i_size_read(inode);
  1273. end_pos = folio_pos(folio) + folio_size(folio);
  1274. if (end_pos > isize) {
  1275. /*
  1276. * Check whether the page to write out is beyond or straddles
  1277. * i_size or not.
  1278. * -------------------------------------------------------
  1279. * | file mapping | <EOF> |
  1280. * -------------------------------------------------------
  1281. * | Page ... | Page N-2 | Page N-1 | Page N | Beyond |
  1282. * ^--------------------------------^-----------|---------
  1283. * | | Straddles |
  1284. * ---------------------------------^-----------|--------|
  1285. */
  1286. size_t poff = offset_in_folio(folio, isize);
  1287. pgoff_t end_index = isize >> PAGE_SHIFT;
  1288. /*
  1289. * Skip the page if it's fully outside i_size, e.g.
  1290. * due to a truncate operation that's in progress. We've
  1291. * cleaned this page and truncate will finish things off for
  1292. * us.
  1293. *
  1294. * Note that the end_index is unsigned long. If the given
  1295. * offset is greater than 16TB on a 32-bit system then if we
  1296. * checked if the page is fully outside i_size with
  1297. * "if (page->index >= end_index + 1)", "end_index + 1" would
  1298. * overflow and evaluate to 0. Hence this page would be
  1299. * redirtied and written out repeatedly, which would result in
  1300. * an infinite loop; the user program performing this operation
  1301. * would hang. Instead, we can detect this situation by
  1302. * checking if the page is totally beyond i_size or if its
  1303. * offset is just equal to the EOF.
  1304. */
  1305. if (folio->index > end_index ||
  1306. (folio->index == end_index && poff == 0))
  1307. goto unlock;
  1308. /*
  1309. * The page straddles i_size. It must be zeroed out on each
  1310. * and every writepage invocation because it may be mmapped.
  1311. * "A file is mapped in multiples of the page size. For a file
  1312. * that is not a multiple of the page size, the remaining
  1313. * memory is zeroed when mapped, and writes to that region are
  1314. * not written out to the file."
  1315. */
  1316. folio_zero_segment(folio, poff, folio_size(folio));
  1317. end_pos = isize;
  1318. }
  1319. return iomap_writepage_map(wpc, wbc, inode, folio, end_pos);
  1320. redirty:
  1321. folio_redirty_for_writepage(wbc, folio);
  1322. unlock:
  1323. folio_unlock(folio);
  1324. return 0;
  1325. }
  1326. int
  1327. iomap_writepages(struct address_space *mapping, struct writeback_control *wbc,
  1328. struct iomap_writepage_ctx *wpc,
  1329. const struct iomap_writeback_ops *ops)
  1330. {
  1331. int ret;
  1332. wpc->ops = ops;
  1333. ret = write_cache_pages(mapping, wbc, iomap_do_writepage, wpc);
  1334. if (!wpc->ioend)
  1335. return ret;
  1336. return iomap_submit_ioend(wpc, wpc->ioend, ret);
  1337. }
  1338. EXPORT_SYMBOL_GPL(iomap_writepages);
  1339. static int __init iomap_init(void)
  1340. {
  1341. return bioset_init(&iomap_ioend_bioset, 4 * (PAGE_SIZE / SECTOR_SIZE),
  1342. offsetof(struct iomap_ioend, io_inline_bio),
  1343. BIOSET_NEED_BVECS);
  1344. }
  1345. fs_initcall(iomap_init);