buffered-io.c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2010 Red Hat, Inc.
  4. * Copyright (C) 2016-2023 Christoph Hellwig.
  5. */
  6. #include <linux/iomap.h>
  7. #include <linux/buffer_head.h>
  8. #include <linux/writeback.h>
  9. #include <linux/swap.h>
  10. #include <linux/migrate.h>
  11. #include <linux/fserror.h>
  12. #include "internal.h"
  13. #include "trace.h"
  14. #include "../internal.h"
  15. /*
  16. * Structure allocated for each folio to track per-block uptodate, dirty state
  17. * and I/O completions.
  18. */
  19. struct iomap_folio_state {
  20. spinlock_t state_lock;
  21. unsigned int read_bytes_pending;
  22. atomic_t write_bytes_pending;
  23. /*
  24. * Each block has two bits in this bitmap:
  25. * Bits [0..blocks_per_folio) has the uptodate status.
  26. * Bits [b_p_f...(2*b_p_f)) has the dirty status.
  27. */
  28. unsigned long state[];
  29. };
  30. static inline bool ifs_is_fully_uptodate(struct folio *folio,
  31. struct iomap_folio_state *ifs)
  32. {
  33. struct inode *inode = folio->mapping->host;
  34. return bitmap_full(ifs->state, i_blocks_per_folio(inode, folio));
  35. }
  36. /*
  37. * Find the next uptodate block in the folio. end_blk is inclusive.
  38. * If no uptodate block is found, this will return end_blk + 1.
  39. */
  40. static unsigned ifs_next_uptodate_block(struct folio *folio,
  41. unsigned start_blk, unsigned end_blk)
  42. {
  43. struct iomap_folio_state *ifs = folio->private;
  44. return find_next_bit(ifs->state, end_blk + 1, start_blk);
  45. }
  46. /*
  47. * Find the next non-uptodate block in the folio. end_blk is inclusive.
  48. * If no non-uptodate block is found, this will return end_blk + 1.
  49. */
  50. static unsigned ifs_next_nonuptodate_block(struct folio *folio,
  51. unsigned start_blk, unsigned end_blk)
  52. {
  53. struct iomap_folio_state *ifs = folio->private;
  54. return find_next_zero_bit(ifs->state, end_blk + 1, start_blk);
  55. }
  56. static bool ifs_set_range_uptodate(struct folio *folio,
  57. struct iomap_folio_state *ifs, size_t off, size_t len)
  58. {
  59. struct inode *inode = folio->mapping->host;
  60. unsigned int first_blk = off >> inode->i_blkbits;
  61. unsigned int last_blk = (off + len - 1) >> inode->i_blkbits;
  62. unsigned int nr_blks = last_blk - first_blk + 1;
  63. bitmap_set(ifs->state, first_blk, nr_blks);
  64. return ifs_is_fully_uptodate(folio, ifs);
  65. }
  66. static void iomap_set_range_uptodate(struct folio *folio, size_t off,
  67. size_t len)
  68. {
  69. struct iomap_folio_state *ifs = folio->private;
  70. unsigned long flags;
  71. bool mark_uptodate = true;
  72. if (folio_test_uptodate(folio))
  73. return;
  74. if (ifs) {
  75. spin_lock_irqsave(&ifs->state_lock, flags);
  76. /*
  77. * If a read with bytes pending is in progress, we must not call
  78. * folio_mark_uptodate(). The read completion path
  79. * (iomap_read_end()) will call folio_end_read(), which uses XOR
  80. * semantics to set the uptodate bit. If we set it here, the XOR
  81. * in folio_end_read() will clear it, leaving the folio not
  82. * uptodate.
  83. */
  84. mark_uptodate = ifs_set_range_uptodate(folio, ifs, off, len) &&
  85. !ifs->read_bytes_pending;
  86. spin_unlock_irqrestore(&ifs->state_lock, flags);
  87. }
  88. if (mark_uptodate)
  89. folio_mark_uptodate(folio);
  90. }
  91. /*
  92. * Find the next dirty block in the folio. end_blk is inclusive.
  93. * If no dirty block is found, this will return end_blk + 1.
  94. */
  95. static unsigned ifs_next_dirty_block(struct folio *folio,
  96. unsigned start_blk, unsigned end_blk)
  97. {
  98. struct iomap_folio_state *ifs = folio->private;
  99. struct inode *inode = folio->mapping->host;
  100. unsigned int blks = i_blocks_per_folio(inode, folio);
  101. return find_next_bit(ifs->state, blks + end_blk + 1,
  102. blks + start_blk) - blks;
  103. }
  104. /*
  105. * Find the next clean block in the folio. end_blk is inclusive.
  106. * If no clean block is found, this will return end_blk + 1.
  107. */
  108. static unsigned ifs_next_clean_block(struct folio *folio,
  109. unsigned start_blk, unsigned end_blk)
  110. {
  111. struct iomap_folio_state *ifs = folio->private;
  112. struct inode *inode = folio->mapping->host;
  113. unsigned int blks = i_blocks_per_folio(inode, folio);
  114. return find_next_zero_bit(ifs->state, blks + end_blk + 1,
  115. blks + start_blk) - blks;
  116. }
  117. static unsigned ifs_find_dirty_range(struct folio *folio,
  118. struct iomap_folio_state *ifs, u64 *range_start, u64 range_end)
  119. {
  120. struct inode *inode = folio->mapping->host;
  121. unsigned start_blk =
  122. offset_in_folio(folio, *range_start) >> inode->i_blkbits;
  123. unsigned end_blk = min_not_zero(
  124. offset_in_folio(folio, range_end) >> inode->i_blkbits,
  125. i_blocks_per_folio(inode, folio)) - 1;
  126. unsigned nblks;
  127. start_blk = ifs_next_dirty_block(folio, start_blk, end_blk);
  128. if (start_blk > end_blk)
  129. return 0;
  130. if (start_blk == end_blk)
  131. nblks = 1;
  132. else
  133. nblks = ifs_next_clean_block(folio, start_blk + 1, end_blk) -
  134. start_blk;
  135. *range_start = folio_pos(folio) + (start_blk << inode->i_blkbits);
  136. return nblks << inode->i_blkbits;
  137. }
  138. static unsigned iomap_find_dirty_range(struct folio *folio, u64 *range_start,
  139. u64 range_end)
  140. {
  141. struct iomap_folio_state *ifs = folio->private;
  142. if (*range_start >= range_end)
  143. return 0;
  144. if (ifs)
  145. return ifs_find_dirty_range(folio, ifs, range_start, range_end);
  146. return range_end - *range_start;
  147. }
  148. static void ifs_clear_range_dirty(struct folio *folio,
  149. struct iomap_folio_state *ifs, size_t off, size_t len)
  150. {
  151. struct inode *inode = folio->mapping->host;
  152. unsigned int blks_per_folio = i_blocks_per_folio(inode, folio);
  153. unsigned int first_blk = (off >> inode->i_blkbits);
  154. unsigned int last_blk = (off + len - 1) >> inode->i_blkbits;
  155. unsigned int nr_blks = last_blk - first_blk + 1;
  156. unsigned long flags;
  157. spin_lock_irqsave(&ifs->state_lock, flags);
  158. bitmap_clear(ifs->state, first_blk + blks_per_folio, nr_blks);
  159. spin_unlock_irqrestore(&ifs->state_lock, flags);
  160. }
  161. static void iomap_clear_range_dirty(struct folio *folio, size_t off, size_t len)
  162. {
  163. struct iomap_folio_state *ifs = folio->private;
  164. if (ifs)
  165. ifs_clear_range_dirty(folio, ifs, off, len);
  166. }
  167. static void ifs_set_range_dirty(struct folio *folio,
  168. struct iomap_folio_state *ifs, size_t off, size_t len)
  169. {
  170. struct inode *inode = folio->mapping->host;
  171. unsigned int blks_per_folio = i_blocks_per_folio(inode, folio);
  172. unsigned int first_blk = (off >> inode->i_blkbits);
  173. unsigned int last_blk = (off + len - 1) >> inode->i_blkbits;
  174. unsigned int nr_blks = last_blk - first_blk + 1;
  175. unsigned long flags;
  176. spin_lock_irqsave(&ifs->state_lock, flags);
  177. bitmap_set(ifs->state, first_blk + blks_per_folio, nr_blks);
  178. spin_unlock_irqrestore(&ifs->state_lock, flags);
  179. }
  180. static void iomap_set_range_dirty(struct folio *folio, size_t off, size_t len)
  181. {
  182. struct iomap_folio_state *ifs = folio->private;
  183. if (ifs)
  184. ifs_set_range_dirty(folio, ifs, off, len);
  185. }
  186. static struct iomap_folio_state *ifs_alloc(struct inode *inode,
  187. struct folio *folio, unsigned int flags)
  188. {
  189. struct iomap_folio_state *ifs = folio->private;
  190. unsigned int nr_blocks = i_blocks_per_folio(inode, folio);
  191. gfp_t gfp;
  192. if (ifs || nr_blocks <= 1)
  193. return ifs;
  194. if (flags & IOMAP_NOWAIT)
  195. gfp = GFP_NOWAIT;
  196. else
  197. gfp = GFP_NOFS | __GFP_NOFAIL;
  198. /*
  199. * ifs->state tracks two sets of state flags when the
  200. * filesystem block size is smaller than the folio size.
  201. * The first state tracks per-block uptodate and the
  202. * second tracks per-block dirty state.
  203. */
  204. ifs = kzalloc_flex(*ifs, state, BITS_TO_LONGS(2 * nr_blocks), gfp);
  205. if (!ifs)
  206. return ifs;
  207. spin_lock_init(&ifs->state_lock);
  208. if (folio_test_uptodate(folio))
  209. bitmap_set(ifs->state, 0, nr_blocks);
  210. if (folio_test_dirty(folio))
  211. bitmap_set(ifs->state, nr_blocks, nr_blocks);
  212. folio_attach_private(folio, ifs);
  213. return ifs;
  214. }
  215. static void ifs_free(struct folio *folio)
  216. {
  217. struct iomap_folio_state *ifs = folio_detach_private(folio);
  218. if (!ifs)
  219. return;
  220. WARN_ON_ONCE(ifs->read_bytes_pending != 0);
  221. WARN_ON_ONCE(atomic_read(&ifs->write_bytes_pending));
  222. WARN_ON_ONCE(ifs_is_fully_uptodate(folio, ifs) !=
  223. folio_test_uptodate(folio));
  224. kfree(ifs);
  225. }
  226. /*
  227. * Calculate how many bytes to truncate based off the number of blocks to
  228. * truncate and the end position to start truncating from.
  229. */
  230. static size_t iomap_bytes_to_truncate(loff_t end_pos, unsigned block_bits,
  231. unsigned blocks_truncated)
  232. {
  233. unsigned block_size = 1 << block_bits;
  234. unsigned block_offset = end_pos & (block_size - 1);
  235. if (!block_offset)
  236. return blocks_truncated << block_bits;
  237. return ((blocks_truncated - 1) << block_bits) + block_offset;
  238. }
  239. /*
  240. * Calculate the range inside the folio that we actually need to read.
  241. */
  242. static void iomap_adjust_read_range(struct inode *inode, struct folio *folio,
  243. loff_t *pos, loff_t length, size_t *offp, size_t *lenp)
  244. {
  245. struct iomap_folio_state *ifs = folio->private;
  246. loff_t orig_pos = *pos;
  247. loff_t isize = i_size_read(inode);
  248. unsigned block_bits = inode->i_blkbits;
  249. unsigned block_size = (1 << block_bits);
  250. size_t poff = offset_in_folio(folio, *pos);
  251. size_t plen = min_t(loff_t, folio_size(folio) - poff, length);
  252. size_t orig_plen = plen;
  253. unsigned first = poff >> block_bits;
  254. unsigned last = (poff + plen - 1) >> block_bits;
  255. /*
  256. * If the block size is smaller than the page size, we need to check the
  257. * per-block uptodate status and adjust the offset and length if needed
  258. * to avoid reading in already uptodate ranges.
  259. */
  260. if (ifs) {
  261. unsigned int next, blocks_skipped;
  262. next = ifs_next_nonuptodate_block(folio, first, last);
  263. blocks_skipped = next - first;
  264. if (blocks_skipped) {
  265. unsigned long block_offset = *pos & (block_size - 1);
  266. unsigned bytes_skipped =
  267. (blocks_skipped << block_bits) - block_offset;
  268. *pos += bytes_skipped;
  269. poff += bytes_skipped;
  270. plen -= bytes_skipped;
  271. }
  272. first = next;
  273. /* truncate len if we find any trailing uptodate block(s) */
  274. if (++next <= last) {
  275. next = ifs_next_uptodate_block(folio, next, last);
  276. if (next <= last) {
  277. plen -= iomap_bytes_to_truncate(*pos + plen,
  278. block_bits, last - next + 1);
  279. last = next - 1;
  280. }
  281. }
  282. }
  283. /*
  284. * If the extent spans the block that contains the i_size, we need to
  285. * handle both halves separately so that we properly zero data in the
  286. * page cache for blocks that are entirely outside of i_size.
  287. */
  288. if (orig_pos <= isize && orig_pos + orig_plen > isize) {
  289. unsigned end = offset_in_folio(folio, isize - 1) >> block_bits;
  290. if (first <= end && last > end)
  291. plen -= iomap_bytes_to_truncate(*pos + plen, block_bits,
  292. last - end);
  293. }
  294. *offp = poff;
  295. *lenp = plen;
  296. }
  297. static inline bool iomap_block_needs_zeroing(const struct iomap_iter *iter,
  298. loff_t pos)
  299. {
  300. const struct iomap *srcmap = iomap_iter_srcmap(iter);
  301. return srcmap->type != IOMAP_MAPPED ||
  302. (srcmap->flags & IOMAP_F_NEW) ||
  303. pos >= i_size_read(iter->inode);
  304. }
  305. /**
  306. * iomap_read_inline_data - copy inline data into the page cache
  307. * @iter: iteration structure
  308. * @folio: folio to copy to
  309. *
  310. * Copy the inline data in @iter into @folio and zero out the rest of the folio.
  311. * Only a single IOMAP_INLINE extent is allowed at the end of each file.
  312. * Returns zero for success to complete the read, or the usual negative errno.
  313. */
  314. static int iomap_read_inline_data(const struct iomap_iter *iter,
  315. struct folio *folio)
  316. {
  317. const struct iomap *iomap = iomap_iter_srcmap(iter);
  318. size_t size = i_size_read(iter->inode) - iomap->offset;
  319. size_t offset = offset_in_folio(folio, iomap->offset);
  320. if (WARN_ON_ONCE(!iomap->inline_data))
  321. return -EIO;
  322. if (folio_test_uptodate(folio))
  323. return 0;
  324. if (WARN_ON_ONCE(size > iomap->length)) {
  325. fserror_report_io(iter->inode, FSERR_BUFFERED_READ,
  326. iomap->offset, size, -EIO, GFP_NOFS);
  327. return -EIO;
  328. }
  329. if (offset > 0)
  330. ifs_alloc(iter->inode, folio, iter->flags);
  331. folio_fill_tail(folio, offset, iomap->inline_data, size);
  332. iomap_set_range_uptodate(folio, offset, folio_size(folio) - offset);
  333. return 0;
  334. }
  335. void iomap_finish_folio_read(struct folio *folio, size_t off, size_t len,
  336. int error)
  337. {
  338. struct iomap_folio_state *ifs = folio->private;
  339. bool uptodate = !error;
  340. bool finished = true;
  341. if (ifs) {
  342. unsigned long flags;
  343. spin_lock_irqsave(&ifs->state_lock, flags);
  344. if (!error)
  345. uptodate = ifs_set_range_uptodate(folio, ifs, off, len);
  346. ifs->read_bytes_pending -= len;
  347. finished = !ifs->read_bytes_pending;
  348. spin_unlock_irqrestore(&ifs->state_lock, flags);
  349. }
  350. if (error)
  351. fserror_report_io(folio->mapping->host, FSERR_BUFFERED_READ,
  352. folio_pos(folio) + off, len, error,
  353. GFP_ATOMIC);
  354. if (finished)
  355. folio_end_read(folio, uptodate);
  356. }
  357. EXPORT_SYMBOL_GPL(iomap_finish_folio_read);
  358. static void iomap_read_init(struct folio *folio)
  359. {
  360. struct iomap_folio_state *ifs = folio->private;
  361. if (ifs) {
  362. /*
  363. * ifs->read_bytes_pending is used to track how many bytes are
  364. * read in asynchronously by the IO helper. We need to track
  365. * this so that we can know when the IO helper has finished
  366. * reading in all the necessary ranges of the folio and can end
  367. * the read.
  368. *
  369. * Increase ->read_bytes_pending by the folio size to start.
  370. * We'll subtract any uptodate / zeroed ranges that did not
  371. * require IO in iomap_read_end() after we're done processing
  372. * the folio.
  373. *
  374. * We do this because otherwise, we would have to increment
  375. * ifs->read_bytes_pending every time a range in the folio needs
  376. * to be read in, which can get expensive since the spinlock
  377. * needs to be held whenever modifying ifs->read_bytes_pending.
  378. */
  379. spin_lock_irq(&ifs->state_lock);
  380. WARN_ON_ONCE(ifs->read_bytes_pending != 0);
  381. ifs->read_bytes_pending = folio_size(folio);
  382. spin_unlock_irq(&ifs->state_lock);
  383. }
  384. }
  385. /*
  386. * This ends IO if no bytes were submitted to an IO helper.
  387. *
  388. * Otherwise, this calibrates ifs->read_bytes_pending to represent only the
  389. * submitted bytes (see comment in iomap_read_init()). If all bytes submitted
  390. * have already been completed by the IO helper, then this will end the read.
  391. * Else the IO helper will end the read after all submitted ranges have been
  392. * read.
  393. */
  394. static void iomap_read_end(struct folio *folio, size_t bytes_submitted)
  395. {
  396. struct iomap_folio_state *ifs = folio->private;
  397. if (ifs) {
  398. bool end_read, uptodate;
  399. spin_lock_irq(&ifs->state_lock);
  400. if (!ifs->read_bytes_pending) {
  401. WARN_ON_ONCE(bytes_submitted);
  402. spin_unlock_irq(&ifs->state_lock);
  403. folio_unlock(folio);
  404. return;
  405. }
  406. /*
  407. * Subtract any bytes that were initially accounted to
  408. * read_bytes_pending but skipped for IO.
  409. */
  410. ifs->read_bytes_pending -= folio_size(folio) - bytes_submitted;
  411. /*
  412. * If !ifs->read_bytes_pending, this means all pending reads by
  413. * the IO helper have already completed, which means we need to
  414. * end the folio read here. If ifs->read_bytes_pending != 0,
  415. * the IO helper will end the folio read.
  416. */
  417. end_read = !ifs->read_bytes_pending;
  418. if (end_read)
  419. uptodate = ifs_is_fully_uptodate(folio, ifs);
  420. spin_unlock_irq(&ifs->state_lock);
  421. if (end_read)
  422. folio_end_read(folio, uptodate);
  423. } else {
  424. /*
  425. * If a folio without an ifs is submitted to the IO helper, the
  426. * read must be on the entire folio and the IO helper takes
  427. * ownership of the folio. This means we should only enter
  428. * iomap_read_end() for the !ifs case if no bytes were submitted
  429. * to the IO helper, in which case we are responsible for
  430. * unlocking the folio here.
  431. */
  432. WARN_ON_ONCE(bytes_submitted);
  433. folio_unlock(folio);
  434. }
  435. }
  436. static int iomap_read_folio_iter(struct iomap_iter *iter,
  437. struct iomap_read_folio_ctx *ctx, size_t *bytes_submitted)
  438. {
  439. const struct iomap *iomap = &iter->iomap;
  440. loff_t pos = iter->pos;
  441. loff_t length = iomap_length(iter);
  442. struct folio *folio = ctx->cur_folio;
  443. size_t folio_len = folio_size(folio);
  444. struct iomap_folio_state *ifs;
  445. size_t poff, plen;
  446. loff_t pos_diff;
  447. int ret;
  448. if (iomap->type == IOMAP_INLINE) {
  449. ret = iomap_read_inline_data(iter, folio);
  450. if (ret)
  451. return ret;
  452. return iomap_iter_advance(iter, length);
  453. }
  454. ifs = ifs_alloc(iter->inode, folio, iter->flags);
  455. length = min_t(loff_t, length, folio_len - offset_in_folio(folio, pos));
  456. while (length) {
  457. iomap_adjust_read_range(iter->inode, folio, &pos, length, &poff,
  458. &plen);
  459. pos_diff = pos - iter->pos;
  460. if (WARN_ON_ONCE(pos_diff + plen > length))
  461. return -EIO;
  462. ret = iomap_iter_advance(iter, pos_diff);
  463. if (ret)
  464. return ret;
  465. if (plen == 0)
  466. return 0;
  467. /* zero post-eof blocks as the page may be mapped */
  468. if (iomap_block_needs_zeroing(iter, pos)) {
  469. folio_zero_range(folio, poff, plen);
  470. iomap_set_range_uptodate(folio, poff, plen);
  471. } else {
  472. if (!*bytes_submitted)
  473. iomap_read_init(folio);
  474. ret = ctx->ops->read_folio_range(iter, ctx, plen);
  475. if (ret < 0)
  476. fserror_report_io(iter->inode,
  477. FSERR_BUFFERED_READ, pos,
  478. plen, ret, GFP_NOFS);
  479. if (ret)
  480. return ret;
  481. *bytes_submitted += plen;
  482. /*
  483. * Hand off folio ownership to the IO helper when:
  484. * 1) The entire folio has been submitted for IO, or
  485. * 2) There is no ifs attached to the folio
  486. *
  487. * Case (2) occurs when 1 << i_blkbits matches the folio
  488. * size but the underlying filesystem or block device
  489. * uses a smaller granularity for IO.
  490. */
  491. if (*bytes_submitted == folio_len || !ifs)
  492. ctx->cur_folio = NULL;
  493. }
  494. ret = iomap_iter_advance(iter, plen);
  495. if (ret)
  496. return ret;
  497. length -= pos_diff + plen;
  498. pos = iter->pos;
  499. }
  500. return 0;
  501. }
  502. void iomap_read_folio(const struct iomap_ops *ops,
  503. struct iomap_read_folio_ctx *ctx, void *private)
  504. {
  505. struct folio *folio = ctx->cur_folio;
  506. struct iomap_iter iter = {
  507. .inode = folio->mapping->host,
  508. .pos = folio_pos(folio),
  509. .len = folio_size(folio),
  510. .private = private,
  511. };
  512. size_t bytes_submitted = 0;
  513. int ret;
  514. trace_iomap_readpage(iter.inode, 1);
  515. while ((ret = iomap_iter(&iter, ops)) > 0)
  516. iter.status = iomap_read_folio_iter(&iter, ctx,
  517. &bytes_submitted);
  518. if (ctx->ops->submit_read)
  519. ctx->ops->submit_read(ctx);
  520. if (ctx->cur_folio)
  521. iomap_read_end(ctx->cur_folio, bytes_submitted);
  522. }
  523. EXPORT_SYMBOL_GPL(iomap_read_folio);
  524. static int iomap_readahead_iter(struct iomap_iter *iter,
  525. struct iomap_read_folio_ctx *ctx, size_t *cur_bytes_submitted)
  526. {
  527. int ret;
  528. while (iomap_length(iter)) {
  529. if (ctx->cur_folio &&
  530. offset_in_folio(ctx->cur_folio, iter->pos) == 0) {
  531. iomap_read_end(ctx->cur_folio, *cur_bytes_submitted);
  532. ctx->cur_folio = NULL;
  533. }
  534. if (!ctx->cur_folio) {
  535. ctx->cur_folio = readahead_folio(ctx->rac);
  536. if (WARN_ON_ONCE(!ctx->cur_folio))
  537. return -EINVAL;
  538. *cur_bytes_submitted = 0;
  539. }
  540. ret = iomap_read_folio_iter(iter, ctx, cur_bytes_submitted);
  541. if (ret)
  542. return ret;
  543. }
  544. return 0;
  545. }
  546. /**
  547. * iomap_readahead - Attempt to read pages from a file.
  548. * @ops: The operations vector for the filesystem.
  549. * @ctx: The ctx used for issuing readahead.
  550. * @private: The filesystem-specific information for issuing iomap_iter.
  551. *
  552. * This function is for filesystems to call to implement their readahead
  553. * address_space operation.
  554. *
  555. * Context: The @ops callbacks may submit I/O (eg to read the addresses of
  556. * blocks from disc), and may wait for it. The caller may be trying to
  557. * access a different page, and so sleeping excessively should be avoided.
  558. * It may allocate memory, but should avoid costly allocations. This
  559. * function is called with memalloc_nofs set, so allocations will not cause
  560. * the filesystem to be reentered.
  561. */
  562. void iomap_readahead(const struct iomap_ops *ops,
  563. struct iomap_read_folio_ctx *ctx, void *private)
  564. {
  565. struct readahead_control *rac = ctx->rac;
  566. struct iomap_iter iter = {
  567. .inode = rac->mapping->host,
  568. .pos = readahead_pos(rac),
  569. .len = readahead_length(rac),
  570. .private = private,
  571. };
  572. size_t cur_bytes_submitted;
  573. trace_iomap_readahead(rac->mapping->host, readahead_count(rac));
  574. while (iomap_iter(&iter, ops) > 0)
  575. iter.status = iomap_readahead_iter(&iter, ctx,
  576. &cur_bytes_submitted);
  577. if (ctx->ops->submit_read)
  578. ctx->ops->submit_read(ctx);
  579. if (ctx->cur_folio)
  580. iomap_read_end(ctx->cur_folio, cur_bytes_submitted);
  581. }
  582. EXPORT_SYMBOL_GPL(iomap_readahead);
  583. /*
  584. * iomap_is_partially_uptodate checks whether blocks within a folio are
  585. * uptodate or not.
  586. *
  587. * Returns true if all blocks which correspond to the specified part
  588. * of the folio are uptodate.
  589. */
  590. bool iomap_is_partially_uptodate(struct folio *folio, size_t from, size_t count)
  591. {
  592. struct iomap_folio_state *ifs = folio->private;
  593. struct inode *inode = folio->mapping->host;
  594. unsigned first, last;
  595. if (!ifs)
  596. return false;
  597. /* Caller's range may extend past the end of this folio */
  598. count = min(folio_size(folio) - from, count);
  599. /* First and last blocks in range within folio */
  600. first = from >> inode->i_blkbits;
  601. last = (from + count - 1) >> inode->i_blkbits;
  602. return ifs_next_nonuptodate_block(folio, first, last) > last;
  603. }
  604. EXPORT_SYMBOL_GPL(iomap_is_partially_uptodate);
  605. /**
  606. * iomap_get_folio - get a folio reference for writing
  607. * @iter: iteration structure
  608. * @pos: start offset of write
  609. * @len: Suggested size of folio to create.
  610. *
  611. * Returns a locked reference to the folio at @pos, or an error pointer if the
  612. * folio could not be obtained.
  613. */
  614. struct folio *iomap_get_folio(struct iomap_iter *iter, loff_t pos, size_t len)
  615. {
  616. fgf_t fgp = FGP_WRITEBEGIN | FGP_NOFS;
  617. if (iter->flags & IOMAP_NOWAIT)
  618. fgp |= FGP_NOWAIT;
  619. if (iter->flags & IOMAP_DONTCACHE)
  620. fgp |= FGP_DONTCACHE;
  621. fgp |= fgf_set_order(len);
  622. return __filemap_get_folio(iter->inode->i_mapping, pos >> PAGE_SHIFT,
  623. fgp, mapping_gfp_mask(iter->inode->i_mapping));
  624. }
  625. EXPORT_SYMBOL_GPL(iomap_get_folio);
  626. bool iomap_release_folio(struct folio *folio, gfp_t gfp_flags)
  627. {
  628. trace_iomap_release_folio(folio->mapping->host, folio_pos(folio),
  629. folio_size(folio));
  630. /*
  631. * If the folio is dirty, we refuse to release our metadata because
  632. * it may be partially dirty. Once we track per-block dirty state,
  633. * we can release the metadata if every block is dirty.
  634. */
  635. if (folio_test_dirty(folio))
  636. return false;
  637. ifs_free(folio);
  638. return true;
  639. }
  640. EXPORT_SYMBOL_GPL(iomap_release_folio);
  641. void iomap_invalidate_folio(struct folio *folio, size_t offset, size_t len)
  642. {
  643. trace_iomap_invalidate_folio(folio->mapping->host,
  644. folio_pos(folio) + offset, len);
  645. /*
  646. * If we're invalidating the entire folio, clear the dirty state
  647. * from it and release it to avoid unnecessary buildup of the LRU.
  648. */
  649. if (offset == 0 && len == folio_size(folio)) {
  650. WARN_ON_ONCE(folio_test_writeback(folio));
  651. folio_cancel_dirty(folio);
  652. ifs_free(folio);
  653. }
  654. }
  655. EXPORT_SYMBOL_GPL(iomap_invalidate_folio);
  656. bool iomap_dirty_folio(struct address_space *mapping, struct folio *folio)
  657. {
  658. struct inode *inode = mapping->host;
  659. size_t len = folio_size(folio);
  660. ifs_alloc(inode, folio, 0);
  661. iomap_set_range_dirty(folio, 0, len);
  662. return filemap_dirty_folio(mapping, folio);
  663. }
  664. EXPORT_SYMBOL_GPL(iomap_dirty_folio);
  665. static void
  666. iomap_write_failed(struct inode *inode, loff_t pos, unsigned len)
  667. {
  668. loff_t i_size = i_size_read(inode);
  669. /*
  670. * Only truncate newly allocated pages beyoned EOF, even if the
  671. * write started inside the existing inode size.
  672. */
  673. if (pos + len > i_size)
  674. truncate_pagecache_range(inode, max(pos, i_size),
  675. pos + len - 1);
  676. }
  677. static int __iomap_write_begin(const struct iomap_iter *iter,
  678. const struct iomap_write_ops *write_ops, size_t len,
  679. struct folio *folio)
  680. {
  681. struct iomap_folio_state *ifs;
  682. loff_t pos = iter->pos;
  683. loff_t block_size = i_blocksize(iter->inode);
  684. loff_t block_start = round_down(pos, block_size);
  685. loff_t block_end = round_up(pos + len, block_size);
  686. unsigned int nr_blocks = i_blocks_per_folio(iter->inode, folio);
  687. size_t from = offset_in_folio(folio, pos), to = from + len;
  688. size_t poff, plen;
  689. /*
  690. * If the write or zeroing completely overlaps the current folio, then
  691. * entire folio will be dirtied so there is no need for
  692. * per-block state tracking structures to be attached to this folio.
  693. * For the unshare case, we must read in the ondisk contents because we
  694. * are not changing pagecache contents.
  695. */
  696. if (!(iter->flags & IOMAP_UNSHARE) && pos <= folio_pos(folio) &&
  697. pos + len >= folio_next_pos(folio))
  698. return 0;
  699. ifs = ifs_alloc(iter->inode, folio, iter->flags);
  700. if ((iter->flags & IOMAP_NOWAIT) && !ifs && nr_blocks > 1)
  701. return -EAGAIN;
  702. if (folio_test_uptodate(folio))
  703. return 0;
  704. do {
  705. iomap_adjust_read_range(iter->inode, folio, &block_start,
  706. block_end - block_start, &poff, &plen);
  707. if (plen == 0)
  708. break;
  709. /*
  710. * If the read range will be entirely overwritten by the write,
  711. * we can skip having to zero/read it in.
  712. */
  713. if (!(iter->flags & IOMAP_UNSHARE) && from <= poff &&
  714. to >= poff + plen)
  715. continue;
  716. if (iomap_block_needs_zeroing(iter, block_start)) {
  717. if (WARN_ON_ONCE(iter->flags & IOMAP_UNSHARE))
  718. return -EIO;
  719. folio_zero_segments(folio, poff, from, to, poff + plen);
  720. } else {
  721. int status;
  722. if (iter->flags & IOMAP_NOWAIT)
  723. return -EAGAIN;
  724. if (write_ops && write_ops->read_folio_range)
  725. status = write_ops->read_folio_range(iter,
  726. folio, block_start, plen);
  727. else
  728. status = iomap_bio_read_folio_range_sync(iter,
  729. folio, block_start, plen);
  730. if (status < 0)
  731. fserror_report_io(iter->inode,
  732. FSERR_BUFFERED_READ, pos,
  733. len, status, GFP_NOFS);
  734. if (status)
  735. return status;
  736. }
  737. iomap_set_range_uptodate(folio, poff, plen);
  738. } while ((block_start += plen) < block_end);
  739. return 0;
  740. }
  741. static struct folio *__iomap_get_folio(struct iomap_iter *iter,
  742. const struct iomap_write_ops *write_ops, size_t len)
  743. {
  744. loff_t pos = iter->pos;
  745. if (!mapping_large_folio_support(iter->inode->i_mapping))
  746. len = min_t(size_t, len, PAGE_SIZE - offset_in_page(pos));
  747. if (iter->iomap.flags & IOMAP_F_FOLIO_BATCH) {
  748. struct folio *folio = folio_batch_next(iter->fbatch);
  749. if (!folio)
  750. return NULL;
  751. /*
  752. * The folio mapping generally shouldn't have changed based on
  753. * fs locks, but be consistent with filemap lookup and retry
  754. * the iter if it does.
  755. */
  756. folio_lock(folio);
  757. if (unlikely(folio->mapping != iter->inode->i_mapping)) {
  758. iter->iomap.flags |= IOMAP_F_STALE;
  759. folio_unlock(folio);
  760. return NULL;
  761. }
  762. folio_get(folio);
  763. folio_wait_stable(folio);
  764. return folio;
  765. }
  766. if (write_ops && write_ops->get_folio)
  767. return write_ops->get_folio(iter, pos, len);
  768. return iomap_get_folio(iter, pos, len);
  769. }
  770. static void __iomap_put_folio(struct iomap_iter *iter,
  771. const struct iomap_write_ops *write_ops, size_t ret,
  772. struct folio *folio)
  773. {
  774. loff_t pos = iter->pos;
  775. if (write_ops && write_ops->put_folio) {
  776. write_ops->put_folio(iter->inode, pos, ret, folio);
  777. } else {
  778. folio_unlock(folio);
  779. folio_put(folio);
  780. }
  781. }
  782. /* trim pos and bytes to within a given folio */
  783. static loff_t iomap_trim_folio_range(struct iomap_iter *iter,
  784. struct folio *folio, size_t *offset, u64 *bytes)
  785. {
  786. loff_t pos = iter->pos;
  787. size_t fsize = folio_size(folio);
  788. WARN_ON_ONCE(pos < folio_pos(folio));
  789. WARN_ON_ONCE(pos >= folio_pos(folio) + fsize);
  790. *offset = offset_in_folio(folio, pos);
  791. *bytes = min(*bytes, fsize - *offset);
  792. return pos;
  793. }
  794. static int iomap_write_begin_inline(const struct iomap_iter *iter,
  795. struct folio *folio)
  796. {
  797. /* needs more work for the tailpacking case; disable for now */
  798. if (WARN_ON_ONCE(iomap_iter_srcmap(iter)->offset != 0))
  799. return -EIO;
  800. return iomap_read_inline_data(iter, folio);
  801. }
  802. /*
  803. * Grab and prepare a folio for write based on iter state. Returns the folio,
  804. * offset, and length. Callers can optionally pass a max length *plen,
  805. * otherwise init to zero.
  806. */
  807. static int iomap_write_begin(struct iomap_iter *iter,
  808. const struct iomap_write_ops *write_ops, struct folio **foliop,
  809. size_t *poffset, u64 *plen)
  810. {
  811. const struct iomap *srcmap = iomap_iter_srcmap(iter);
  812. loff_t pos;
  813. u64 len = min_t(u64, SIZE_MAX, iomap_length(iter));
  814. struct folio *folio;
  815. int status = 0;
  816. len = min_not_zero(len, *plen);
  817. *foliop = NULL;
  818. *plen = 0;
  819. if (fatal_signal_pending(current))
  820. return -EINTR;
  821. folio = __iomap_get_folio(iter, write_ops, len);
  822. if (IS_ERR(folio))
  823. return PTR_ERR(folio);
  824. /*
  825. * No folio means we're done with a batch. We still have range to
  826. * process so return and let the caller iterate and refill the batch.
  827. */
  828. if (!folio) {
  829. WARN_ON_ONCE(!(iter->iomap.flags & IOMAP_F_FOLIO_BATCH));
  830. return 0;
  831. }
  832. /*
  833. * Now we have a locked folio, before we do anything with it we need to
  834. * check that the iomap we have cached is not stale. The inode extent
  835. * mapping can change due to concurrent IO in flight (e.g.
  836. * IOMAP_UNWRITTEN state can change and memory reclaim could have
  837. * reclaimed a previously partially written page at this index after IO
  838. * completion before this write reaches this file offset) and hence we
  839. * could do the wrong thing here (zero a page range incorrectly or fail
  840. * to zero) and corrupt data.
  841. */
  842. if (write_ops && write_ops->iomap_valid) {
  843. bool iomap_valid = write_ops->iomap_valid(iter->inode,
  844. &iter->iomap);
  845. if (!iomap_valid) {
  846. iter->iomap.flags |= IOMAP_F_STALE;
  847. status = 0;
  848. goto out_unlock;
  849. }
  850. }
  851. /*
  852. * The folios in a batch may not be contiguous. If we've skipped
  853. * forward, advance the iter to the pos of the current folio. If the
  854. * folio starts beyond the end of the mapping, it may have been trimmed
  855. * since the lookup for whatever reason. Return a NULL folio to
  856. * terminate the op.
  857. */
  858. if (folio_pos(folio) > iter->pos) {
  859. len = min_t(u64, folio_pos(folio) - iter->pos,
  860. iomap_length(iter));
  861. status = iomap_iter_advance(iter, len);
  862. len = iomap_length(iter);
  863. if (status || !len)
  864. goto out_unlock;
  865. }
  866. pos = iomap_trim_folio_range(iter, folio, poffset, &len);
  867. if (srcmap->type == IOMAP_INLINE)
  868. status = iomap_write_begin_inline(iter, folio);
  869. else if (srcmap->flags & IOMAP_F_BUFFER_HEAD)
  870. status = __block_write_begin_int(folio, pos, len, NULL, srcmap);
  871. else
  872. status = __iomap_write_begin(iter, write_ops, len, folio);
  873. if (unlikely(status))
  874. goto out_unlock;
  875. *foliop = folio;
  876. *plen = len;
  877. return 0;
  878. out_unlock:
  879. __iomap_put_folio(iter, write_ops, 0, folio);
  880. return status;
  881. }
  882. static bool __iomap_write_end(struct inode *inode, loff_t pos, size_t len,
  883. size_t copied, struct folio *folio)
  884. {
  885. flush_dcache_folio(folio);
  886. /*
  887. * The blocks that were entirely written will now be uptodate, so we
  888. * don't have to worry about a read_folio reading them and overwriting a
  889. * partial write. However, if we've encountered a short write and only
  890. * partially written into a block, it will not be marked uptodate, so a
  891. * read_folio might come in and destroy our partial write.
  892. *
  893. * Do the simplest thing and just treat any short write to a
  894. * non-uptodate page as a zero-length write, and force the caller to
  895. * redo the whole thing.
  896. */
  897. if (unlikely(copied < len && !folio_test_uptodate(folio)))
  898. return false;
  899. iomap_set_range_uptodate(folio, offset_in_folio(folio, pos), len);
  900. iomap_set_range_dirty(folio, offset_in_folio(folio, pos), copied);
  901. filemap_dirty_folio(inode->i_mapping, folio);
  902. return true;
  903. }
  904. static bool iomap_write_end_inline(const struct iomap_iter *iter,
  905. struct folio *folio, loff_t pos, size_t copied)
  906. {
  907. const struct iomap *iomap = &iter->iomap;
  908. void *addr;
  909. WARN_ON_ONCE(!folio_test_uptodate(folio));
  910. BUG_ON(!iomap_inline_data_valid(iomap));
  911. if (WARN_ON_ONCE(!iomap->inline_data))
  912. return false;
  913. flush_dcache_folio(folio);
  914. addr = kmap_local_folio(folio, pos);
  915. memcpy(iomap_inline_data(iomap, pos), addr, copied);
  916. kunmap_local(addr);
  917. mark_inode_dirty(iter->inode);
  918. return true;
  919. }
  920. /*
  921. * Returns true if all copied bytes have been written to the pagecache,
  922. * otherwise return false.
  923. */
  924. static bool iomap_write_end(struct iomap_iter *iter, size_t len, size_t copied,
  925. struct folio *folio)
  926. {
  927. const struct iomap *srcmap = iomap_iter_srcmap(iter);
  928. loff_t pos = iter->pos;
  929. if (srcmap->type == IOMAP_INLINE)
  930. return iomap_write_end_inline(iter, folio, pos, copied);
  931. if (srcmap->flags & IOMAP_F_BUFFER_HEAD) {
  932. size_t bh_written;
  933. bh_written = block_write_end(pos, len, copied, folio);
  934. WARN_ON_ONCE(bh_written != copied && bh_written != 0);
  935. return bh_written == copied;
  936. }
  937. return __iomap_write_end(iter->inode, pos, len, copied, folio);
  938. }
  939. static int iomap_write_iter(struct iomap_iter *iter, struct iov_iter *i,
  940. const struct iomap_write_ops *write_ops)
  941. {
  942. ssize_t total_written = 0;
  943. int status = 0;
  944. struct address_space *mapping = iter->inode->i_mapping;
  945. size_t chunk = mapping_max_folio_size(mapping);
  946. unsigned int bdp_flags = (iter->flags & IOMAP_NOWAIT) ? BDP_ASYNC : 0;
  947. do {
  948. struct folio *folio;
  949. loff_t old_size;
  950. size_t offset; /* Offset into folio */
  951. u64 bytes; /* Bytes to write to folio */
  952. size_t copied; /* Bytes copied from user */
  953. u64 written; /* Bytes have been written */
  954. loff_t pos;
  955. bytes = iov_iter_count(i);
  956. retry:
  957. offset = iter->pos & (chunk - 1);
  958. bytes = min(chunk - offset, bytes);
  959. status = balance_dirty_pages_ratelimited_flags(mapping,
  960. bdp_flags);
  961. if (unlikely(status))
  962. break;
  963. if (bytes > iomap_length(iter))
  964. bytes = iomap_length(iter);
  965. /*
  966. * Bring in the user page that we'll copy from _first_.
  967. * Otherwise there's a nasty deadlock on copying from the
  968. * same page as we're writing to, without it being marked
  969. * up-to-date.
  970. *
  971. * For async buffered writes the assumption is that the user
  972. * page has already been faulted in. This can be optimized by
  973. * faulting the user page.
  974. */
  975. if (unlikely(fault_in_iov_iter_readable(i, bytes) == bytes)) {
  976. status = -EFAULT;
  977. break;
  978. }
  979. status = iomap_write_begin(iter, write_ops, &folio, &offset,
  980. &bytes);
  981. if (unlikely(status)) {
  982. iomap_write_failed(iter->inode, iter->pos, bytes);
  983. break;
  984. }
  985. if (iter->iomap.flags & IOMAP_F_STALE)
  986. break;
  987. pos = iter->pos;
  988. if (mapping_writably_mapped(mapping))
  989. flush_dcache_folio(folio);
  990. copied = copy_folio_from_iter_atomic(folio, offset, bytes, i);
  991. written = iomap_write_end(iter, bytes, copied, folio) ?
  992. copied : 0;
  993. /*
  994. * Update the in-memory inode size after copying the data into
  995. * the page cache. It's up to the file system to write the
  996. * updated size to disk, preferably after I/O completion so that
  997. * no stale data is exposed. Only once that's done can we
  998. * unlock and release the folio.
  999. */
  1000. old_size = iter->inode->i_size;
  1001. if (pos + written > old_size) {
  1002. i_size_write(iter->inode, pos + written);
  1003. iter->iomap.flags |= IOMAP_F_SIZE_CHANGED;
  1004. }
  1005. __iomap_put_folio(iter, write_ops, written, folio);
  1006. if (old_size < pos)
  1007. pagecache_isize_extended(iter->inode, old_size, pos);
  1008. cond_resched();
  1009. if (unlikely(written == 0)) {
  1010. /*
  1011. * A short copy made iomap_write_end() reject the
  1012. * thing entirely. Might be memory poisoning
  1013. * halfway through, might be a race with munmap,
  1014. * might be severe memory pressure.
  1015. */
  1016. iomap_write_failed(iter->inode, pos, bytes);
  1017. iov_iter_revert(i, copied);
  1018. if (chunk > PAGE_SIZE)
  1019. chunk /= 2;
  1020. if (copied) {
  1021. bytes = copied;
  1022. goto retry;
  1023. }
  1024. } else {
  1025. total_written += written;
  1026. iomap_iter_advance(iter, written);
  1027. }
  1028. } while (iov_iter_count(i) && iomap_length(iter));
  1029. return total_written ? 0 : status;
  1030. }
  1031. ssize_t
  1032. iomap_file_buffered_write(struct kiocb *iocb, struct iov_iter *i,
  1033. const struct iomap_ops *ops,
  1034. const struct iomap_write_ops *write_ops, void *private)
  1035. {
  1036. struct iomap_iter iter = {
  1037. .inode = iocb->ki_filp->f_mapping->host,
  1038. .pos = iocb->ki_pos,
  1039. .len = iov_iter_count(i),
  1040. .flags = IOMAP_WRITE,
  1041. .private = private,
  1042. };
  1043. ssize_t ret;
  1044. if (iocb->ki_flags & IOCB_NOWAIT)
  1045. iter.flags |= IOMAP_NOWAIT;
  1046. if (iocb->ki_flags & IOCB_DONTCACHE)
  1047. iter.flags |= IOMAP_DONTCACHE;
  1048. while ((ret = iomap_iter(&iter, ops)) > 0)
  1049. iter.status = iomap_write_iter(&iter, i, write_ops);
  1050. if (unlikely(iter.pos == iocb->ki_pos))
  1051. return ret;
  1052. ret = iter.pos - iocb->ki_pos;
  1053. iocb->ki_pos = iter.pos;
  1054. return ret;
  1055. }
  1056. EXPORT_SYMBOL_GPL(iomap_file_buffered_write);
  1057. static void iomap_write_delalloc_ifs_punch(struct inode *inode,
  1058. struct folio *folio, loff_t start_byte, loff_t end_byte,
  1059. struct iomap *iomap, iomap_punch_t punch)
  1060. {
  1061. unsigned int first_blk, last_blk;
  1062. loff_t last_byte;
  1063. u8 blkbits = inode->i_blkbits;
  1064. struct iomap_folio_state *ifs;
  1065. /*
  1066. * When we have per-block dirty tracking, there can be
  1067. * blocks within a folio which are marked uptodate
  1068. * but not dirty. In that case it is necessary to punch
  1069. * out such blocks to avoid leaking any delalloc blocks.
  1070. */
  1071. ifs = folio->private;
  1072. if (!ifs)
  1073. return;
  1074. last_byte = min_t(loff_t, end_byte - 1, folio_next_pos(folio) - 1);
  1075. first_blk = offset_in_folio(folio, start_byte) >> blkbits;
  1076. last_blk = offset_in_folio(folio, last_byte) >> blkbits;
  1077. while ((first_blk = ifs_next_clean_block(folio, first_blk, last_blk))
  1078. <= last_blk) {
  1079. punch(inode, folio_pos(folio) + (first_blk << blkbits),
  1080. 1 << blkbits, iomap);
  1081. first_blk++;
  1082. }
  1083. }
  1084. static void iomap_write_delalloc_punch(struct inode *inode, struct folio *folio,
  1085. loff_t *punch_start_byte, loff_t start_byte, loff_t end_byte,
  1086. struct iomap *iomap, iomap_punch_t punch)
  1087. {
  1088. if (!folio_test_dirty(folio))
  1089. return;
  1090. /* if dirty, punch up to offset */
  1091. if (start_byte > *punch_start_byte) {
  1092. punch(inode, *punch_start_byte, start_byte - *punch_start_byte,
  1093. iomap);
  1094. }
  1095. /* Punch non-dirty blocks within folio */
  1096. iomap_write_delalloc_ifs_punch(inode, folio, start_byte, end_byte,
  1097. iomap, punch);
  1098. /*
  1099. * Make sure the next punch start is correctly bound to
  1100. * the end of this data range, not the end of the folio.
  1101. */
  1102. *punch_start_byte = min_t(loff_t, end_byte, folio_next_pos(folio));
  1103. }
  1104. /*
  1105. * Scan the data range passed to us for dirty page cache folios. If we find a
  1106. * dirty folio, punch out the preceding range and update the offset from which
  1107. * the next punch will start from.
  1108. *
  1109. * We can punch out storage reservations under clean pages because they either
  1110. * contain data that has been written back - in which case the delalloc punch
  1111. * over that range is a no-op - or they have been read faults in which case they
  1112. * contain zeroes and we can remove the delalloc backing range and any new
  1113. * writes to those pages will do the normal hole filling operation...
  1114. *
  1115. * This makes the logic simple: we only need to keep the delalloc extents only
  1116. * over the dirty ranges of the page cache.
  1117. *
  1118. * This function uses [start_byte, end_byte) intervals (i.e. open ended) to
  1119. * simplify range iterations.
  1120. */
  1121. static void iomap_write_delalloc_scan(struct inode *inode,
  1122. loff_t *punch_start_byte, loff_t start_byte, loff_t end_byte,
  1123. struct iomap *iomap, iomap_punch_t punch)
  1124. {
  1125. while (start_byte < end_byte) {
  1126. struct folio *folio;
  1127. /* grab locked page */
  1128. folio = filemap_lock_folio(inode->i_mapping,
  1129. start_byte >> PAGE_SHIFT);
  1130. if (IS_ERR(folio)) {
  1131. start_byte = ALIGN_DOWN(start_byte, PAGE_SIZE) +
  1132. PAGE_SIZE;
  1133. continue;
  1134. }
  1135. iomap_write_delalloc_punch(inode, folio, punch_start_byte,
  1136. start_byte, end_byte, iomap, punch);
  1137. /* move offset to start of next folio in range */
  1138. start_byte = folio_next_pos(folio);
  1139. folio_unlock(folio);
  1140. folio_put(folio);
  1141. }
  1142. }
  1143. /*
  1144. * When a short write occurs, the filesystem might need to use ->iomap_end
  1145. * to remove space reservations created in ->iomap_begin.
  1146. *
  1147. * For filesystems that use delayed allocation, there can be dirty pages over
  1148. * the delalloc extent outside the range of a short write but still within the
  1149. * delalloc extent allocated for this iomap if the write raced with page
  1150. * faults.
  1151. *
  1152. * Punch out all the delalloc blocks in the range given except for those that
  1153. * have dirty data still pending in the page cache - those are going to be
  1154. * written and so must still retain the delalloc backing for writeback.
  1155. *
  1156. * The punch() callback *must* only punch delalloc extents in the range passed
  1157. * to it. It must skip over all other types of extents in the range and leave
  1158. * them completely unchanged. It must do this punch atomically with respect to
  1159. * other extent modifications.
  1160. *
  1161. * The punch() callback may be called with a folio locked to prevent writeback
  1162. * extent allocation racing at the edge of the range we are currently punching.
  1163. * The locked folio may or may not cover the range being punched, so it is not
  1164. * safe for the punch() callback to lock folios itself.
  1165. *
  1166. * Lock order is:
  1167. *
  1168. * inode->i_rwsem (shared or exclusive)
  1169. * inode->i_mapping->invalidate_lock (exclusive)
  1170. * folio_lock()
  1171. * ->punch
  1172. * internal filesystem allocation lock
  1173. *
  1174. * As we are scanning the page cache for data, we don't need to reimplement the
  1175. * wheel - mapping_seek_hole_data() does exactly what we need to identify the
  1176. * start and end of data ranges correctly even for sub-folio block sizes. This
  1177. * byte range based iteration is especially convenient because it means we
  1178. * don't have to care about variable size folios, nor where the start or end of
  1179. * the data range lies within a folio, if they lie within the same folio or even
  1180. * if there are multiple discontiguous data ranges within the folio.
  1181. *
  1182. * It should be noted that mapping_seek_hole_data() is not aware of EOF, and so
  1183. * can return data ranges that exist in the cache beyond EOF. e.g. a page fault
  1184. * spanning EOF will initialise the post-EOF data to zeroes and mark it up to
  1185. * date. A write page fault can then mark it dirty. If we then fail a write()
  1186. * beyond EOF into that up to date cached range, we allocate a delalloc block
  1187. * beyond EOF and then have to punch it out. Because the range is up to date,
  1188. * mapping_seek_hole_data() will return it, and we will skip the punch because
  1189. * the folio is dirty. THis is incorrect - we always need to punch out delalloc
  1190. * beyond EOF in this case as writeback will never write back and covert that
  1191. * delalloc block beyond EOF. Hence we limit the cached data scan range to EOF,
  1192. * resulting in always punching out the range from the EOF to the end of the
  1193. * range the iomap spans.
  1194. *
  1195. * Intervals are of the form [start_byte, end_byte) (i.e. open ended) because it
  1196. * matches the intervals returned by mapping_seek_hole_data(). i.e. SEEK_DATA
  1197. * returns the start of a data range (start_byte), and SEEK_HOLE(start_byte)
  1198. * returns the end of the data range (data_end). Using closed intervals would
  1199. * require sprinkling this code with magic "+ 1" and "- 1" arithmetic and expose
  1200. * the code to subtle off-by-one bugs....
  1201. */
  1202. void iomap_write_delalloc_release(struct inode *inode, loff_t start_byte,
  1203. loff_t end_byte, unsigned flags, struct iomap *iomap,
  1204. iomap_punch_t punch)
  1205. {
  1206. loff_t punch_start_byte = start_byte;
  1207. loff_t scan_end_byte = min(i_size_read(inode), end_byte);
  1208. /*
  1209. * The caller must hold invalidate_lock to avoid races with page faults
  1210. * re-instantiating folios and dirtying them via ->page_mkwrite whilst
  1211. * we walk the cache and perform delalloc extent removal. Failing to do
  1212. * this can leave dirty pages with no space reservation in the cache.
  1213. */
  1214. lockdep_assert_held_write(&inode->i_mapping->invalidate_lock);
  1215. while (start_byte < scan_end_byte) {
  1216. loff_t data_end;
  1217. start_byte = mapping_seek_hole_data(inode->i_mapping,
  1218. start_byte, scan_end_byte, SEEK_DATA);
  1219. /*
  1220. * If there is no more data to scan, all that is left is to
  1221. * punch out the remaining range.
  1222. *
  1223. * Note that mapping_seek_hole_data is only supposed to return
  1224. * either an offset or -ENXIO, so WARN on any other error as
  1225. * that would be an API change without updating the callers.
  1226. */
  1227. if (start_byte == -ENXIO || start_byte == scan_end_byte)
  1228. break;
  1229. if (WARN_ON_ONCE(start_byte < 0))
  1230. return;
  1231. WARN_ON_ONCE(start_byte < punch_start_byte);
  1232. WARN_ON_ONCE(start_byte > scan_end_byte);
  1233. /*
  1234. * We find the end of this contiguous cached data range by
  1235. * seeking from start_byte to the beginning of the next hole.
  1236. */
  1237. data_end = mapping_seek_hole_data(inode->i_mapping, start_byte,
  1238. scan_end_byte, SEEK_HOLE);
  1239. if (WARN_ON_ONCE(data_end < 0))
  1240. return;
  1241. /*
  1242. * If we race with post-direct I/O invalidation of the page cache,
  1243. * there might be no data left at start_byte.
  1244. */
  1245. if (data_end == start_byte)
  1246. continue;
  1247. WARN_ON_ONCE(data_end < start_byte);
  1248. WARN_ON_ONCE(data_end > scan_end_byte);
  1249. iomap_write_delalloc_scan(inode, &punch_start_byte, start_byte,
  1250. data_end, iomap, punch);
  1251. /* The next data search starts at the end of this one. */
  1252. start_byte = data_end;
  1253. }
  1254. if (punch_start_byte < end_byte)
  1255. punch(inode, punch_start_byte, end_byte - punch_start_byte,
  1256. iomap);
  1257. }
  1258. EXPORT_SYMBOL_GPL(iomap_write_delalloc_release);
  1259. static int iomap_unshare_iter(struct iomap_iter *iter,
  1260. const struct iomap_write_ops *write_ops)
  1261. {
  1262. struct iomap *iomap = &iter->iomap;
  1263. u64 bytes = iomap_length(iter);
  1264. int status;
  1265. if (!iomap_want_unshare_iter(iter))
  1266. return iomap_iter_advance(iter, bytes);
  1267. do {
  1268. struct folio *folio;
  1269. size_t offset;
  1270. bool ret;
  1271. bytes = min_t(u64, SIZE_MAX, bytes);
  1272. status = iomap_write_begin(iter, write_ops, &folio, &offset,
  1273. &bytes);
  1274. if (unlikely(status))
  1275. return status;
  1276. if (iomap->flags & IOMAP_F_STALE)
  1277. break;
  1278. ret = iomap_write_end(iter, bytes, bytes, folio);
  1279. __iomap_put_folio(iter, write_ops, bytes, folio);
  1280. if (WARN_ON_ONCE(!ret))
  1281. return -EIO;
  1282. cond_resched();
  1283. balance_dirty_pages_ratelimited(iter->inode->i_mapping);
  1284. status = iomap_iter_advance(iter, bytes);
  1285. if (status)
  1286. break;
  1287. } while ((bytes = iomap_length(iter)) > 0);
  1288. return status;
  1289. }
  1290. int
  1291. iomap_file_unshare(struct inode *inode, loff_t pos, loff_t len,
  1292. const struct iomap_ops *ops,
  1293. const struct iomap_write_ops *write_ops)
  1294. {
  1295. struct iomap_iter iter = {
  1296. .inode = inode,
  1297. .pos = pos,
  1298. .flags = IOMAP_WRITE | IOMAP_UNSHARE,
  1299. };
  1300. loff_t size = i_size_read(inode);
  1301. int ret;
  1302. if (pos < 0 || pos >= size)
  1303. return 0;
  1304. iter.len = min(len, size - pos);
  1305. while ((ret = iomap_iter(&iter, ops)) > 0)
  1306. iter.status = iomap_unshare_iter(&iter, write_ops);
  1307. return ret;
  1308. }
  1309. EXPORT_SYMBOL_GPL(iomap_file_unshare);
  1310. /*
  1311. * Flush the remaining range of the iter and mark the current mapping stale.
  1312. * This is used when zero range sees an unwritten mapping that may have had
  1313. * dirty pagecache over it.
  1314. */
  1315. static inline int iomap_zero_iter_flush_and_stale(struct iomap_iter *i)
  1316. {
  1317. struct address_space *mapping = i->inode->i_mapping;
  1318. loff_t end = i->pos + i->len - 1;
  1319. i->iomap.flags |= IOMAP_F_STALE;
  1320. return filemap_write_and_wait_range(mapping, i->pos, end);
  1321. }
  1322. static int iomap_zero_iter(struct iomap_iter *iter, bool *did_zero,
  1323. const struct iomap_write_ops *write_ops)
  1324. {
  1325. u64 bytes = iomap_length(iter);
  1326. int status;
  1327. do {
  1328. struct folio *folio;
  1329. size_t offset;
  1330. bool ret;
  1331. bytes = min_t(u64, SIZE_MAX, bytes);
  1332. status = iomap_write_begin(iter, write_ops, &folio, &offset,
  1333. &bytes);
  1334. if (status)
  1335. return status;
  1336. if (iter->iomap.flags & IOMAP_F_STALE)
  1337. break;
  1338. /* a NULL folio means we're done with a folio batch */
  1339. if (!folio) {
  1340. status = iomap_iter_advance_full(iter);
  1341. break;
  1342. }
  1343. /* warn about zeroing folios beyond eof that won't write back */
  1344. WARN_ON_ONCE(folio_pos(folio) > iter->inode->i_size);
  1345. trace_iomap_zero_iter(iter->inode, folio_pos(folio) + offset,
  1346. bytes);
  1347. folio_zero_range(folio, offset, bytes);
  1348. folio_mark_accessed(folio);
  1349. ret = iomap_write_end(iter, bytes, bytes, folio);
  1350. __iomap_put_folio(iter, write_ops, bytes, folio);
  1351. if (WARN_ON_ONCE(!ret))
  1352. return -EIO;
  1353. status = iomap_iter_advance(iter, bytes);
  1354. if (status)
  1355. break;
  1356. } while ((bytes = iomap_length(iter)) > 0);
  1357. if (did_zero)
  1358. *did_zero = true;
  1359. return status;
  1360. }
  1361. /**
  1362. * iomap_fill_dirty_folios - fill a folio batch with dirty folios
  1363. * @iter: Iteration structure
  1364. * @start: Start offset of range. Updated based on lookup progress.
  1365. * @end: End offset of range
  1366. * @iomap_flags: Flags to set on the associated iomap to track the batch.
  1367. *
  1368. * Returns the folio count directly. Also returns the associated control flag if
  1369. * the the batch lookup is performed and the expected offset of a subsequent
  1370. * lookup via out params. The caller is responsible to set the flag on the
  1371. * associated iomap.
  1372. */
  1373. unsigned int
  1374. iomap_fill_dirty_folios(
  1375. struct iomap_iter *iter,
  1376. loff_t *start,
  1377. loff_t end,
  1378. unsigned int *iomap_flags)
  1379. {
  1380. struct address_space *mapping = iter->inode->i_mapping;
  1381. pgoff_t pstart = *start >> PAGE_SHIFT;
  1382. pgoff_t pend = (end - 1) >> PAGE_SHIFT;
  1383. unsigned int count;
  1384. if (!iter->fbatch) {
  1385. *start = end;
  1386. return 0;
  1387. }
  1388. count = filemap_get_folios_dirty(mapping, &pstart, pend, iter->fbatch);
  1389. *start = (pstart << PAGE_SHIFT);
  1390. *iomap_flags |= IOMAP_F_FOLIO_BATCH;
  1391. return count;
  1392. }
  1393. EXPORT_SYMBOL_GPL(iomap_fill_dirty_folios);
  1394. int
  1395. iomap_zero_range(struct inode *inode, loff_t pos, loff_t len, bool *did_zero,
  1396. const struct iomap_ops *ops,
  1397. const struct iomap_write_ops *write_ops, void *private)
  1398. {
  1399. struct folio_batch fbatch;
  1400. struct iomap_iter iter = {
  1401. .inode = inode,
  1402. .pos = pos,
  1403. .len = len,
  1404. .flags = IOMAP_ZERO,
  1405. .private = private,
  1406. .fbatch = &fbatch,
  1407. };
  1408. struct address_space *mapping = inode->i_mapping;
  1409. int ret;
  1410. bool range_dirty;
  1411. folio_batch_init(&fbatch);
  1412. /*
  1413. * To avoid an unconditional flush, check pagecache state and only flush
  1414. * if dirty and the fs returns a mapping that might convert on
  1415. * writeback.
  1416. */
  1417. range_dirty = filemap_range_needs_writeback(mapping, iter.pos,
  1418. iter.pos + iter.len - 1);
  1419. while ((ret = iomap_iter(&iter, ops)) > 0) {
  1420. const struct iomap *srcmap = iomap_iter_srcmap(&iter);
  1421. if (WARN_ON_ONCE((iter.iomap.flags & IOMAP_F_FOLIO_BATCH) &&
  1422. srcmap->type != IOMAP_UNWRITTEN))
  1423. return -EIO;
  1424. if (!(iter.iomap.flags & IOMAP_F_FOLIO_BATCH) &&
  1425. (srcmap->type == IOMAP_HOLE ||
  1426. srcmap->type == IOMAP_UNWRITTEN)) {
  1427. s64 status;
  1428. if (range_dirty) {
  1429. range_dirty = false;
  1430. status = iomap_zero_iter_flush_and_stale(&iter);
  1431. } else {
  1432. status = iomap_iter_advance_full(&iter);
  1433. }
  1434. iter.status = status;
  1435. continue;
  1436. }
  1437. iter.status = iomap_zero_iter(&iter, did_zero, write_ops);
  1438. }
  1439. return ret;
  1440. }
  1441. EXPORT_SYMBOL_GPL(iomap_zero_range);
  1442. int
  1443. iomap_truncate_page(struct inode *inode, loff_t pos, bool *did_zero,
  1444. const struct iomap_ops *ops,
  1445. const struct iomap_write_ops *write_ops, void *private)
  1446. {
  1447. unsigned int blocksize = i_blocksize(inode);
  1448. unsigned int off = pos & (blocksize - 1);
  1449. /* Block boundary? Nothing to do */
  1450. if (!off)
  1451. return 0;
  1452. return iomap_zero_range(inode, pos, blocksize - off, did_zero, ops,
  1453. write_ops, private);
  1454. }
  1455. EXPORT_SYMBOL_GPL(iomap_truncate_page);
  1456. static int iomap_folio_mkwrite_iter(struct iomap_iter *iter,
  1457. struct folio *folio)
  1458. {
  1459. loff_t length = iomap_length(iter);
  1460. int ret;
  1461. if (iter->iomap.flags & IOMAP_F_BUFFER_HEAD) {
  1462. ret = __block_write_begin_int(folio, iter->pos, length, NULL,
  1463. &iter->iomap);
  1464. if (ret)
  1465. return ret;
  1466. block_commit_write(folio, 0, length);
  1467. } else {
  1468. WARN_ON_ONCE(!folio_test_uptodate(folio));
  1469. folio_mark_dirty(folio);
  1470. }
  1471. return iomap_iter_advance(iter, length);
  1472. }
  1473. vm_fault_t iomap_page_mkwrite(struct vm_fault *vmf, const struct iomap_ops *ops,
  1474. void *private)
  1475. {
  1476. struct iomap_iter iter = {
  1477. .inode = file_inode(vmf->vma->vm_file),
  1478. .flags = IOMAP_WRITE | IOMAP_FAULT,
  1479. .private = private,
  1480. };
  1481. struct folio *folio = page_folio(vmf->page);
  1482. ssize_t ret;
  1483. folio_lock(folio);
  1484. ret = folio_mkwrite_check_truncate(folio, iter.inode);
  1485. if (ret < 0)
  1486. goto out_unlock;
  1487. iter.pos = folio_pos(folio);
  1488. iter.len = ret;
  1489. while ((ret = iomap_iter(&iter, ops)) > 0)
  1490. iter.status = iomap_folio_mkwrite_iter(&iter, folio);
  1491. if (ret < 0)
  1492. goto out_unlock;
  1493. folio_wait_stable(folio);
  1494. return VM_FAULT_LOCKED;
  1495. out_unlock:
  1496. folio_unlock(folio);
  1497. return vmf_fs_error(ret);
  1498. }
  1499. EXPORT_SYMBOL_GPL(iomap_page_mkwrite);
  1500. static void iomap_writeback_init(struct inode *inode, struct folio *folio)
  1501. {
  1502. struct iomap_folio_state *ifs = folio->private;
  1503. WARN_ON_ONCE(i_blocks_per_folio(inode, folio) > 1 && !ifs);
  1504. if (ifs) {
  1505. WARN_ON_ONCE(atomic_read(&ifs->write_bytes_pending) != 0);
  1506. /*
  1507. * Set this to the folio size. After processing the folio for
  1508. * writeback in iomap_writeback_folio(), we'll subtract any
  1509. * ranges not written back.
  1510. *
  1511. * We do this because otherwise, we would have to atomically
  1512. * increment ifs->write_bytes_pending every time a range in the
  1513. * folio needs to be written back.
  1514. */
  1515. atomic_set(&ifs->write_bytes_pending, folio_size(folio));
  1516. }
  1517. }
  1518. void iomap_finish_folio_write(struct inode *inode, struct folio *folio,
  1519. size_t len)
  1520. {
  1521. struct iomap_folio_state *ifs = folio->private;
  1522. WARN_ON_ONCE(i_blocks_per_folio(inode, folio) > 1 && !ifs);
  1523. WARN_ON_ONCE(ifs && atomic_read(&ifs->write_bytes_pending) <= 0);
  1524. if (!ifs || atomic_sub_and_test(len, &ifs->write_bytes_pending))
  1525. folio_end_writeback(folio);
  1526. }
  1527. EXPORT_SYMBOL_GPL(iomap_finish_folio_write);
  1528. static int iomap_writeback_range(struct iomap_writepage_ctx *wpc,
  1529. struct folio *folio, u64 pos, u32 rlen, u64 end_pos,
  1530. size_t *bytes_submitted)
  1531. {
  1532. do {
  1533. ssize_t ret;
  1534. ret = wpc->ops->writeback_range(wpc, folio, pos, rlen, end_pos);
  1535. if (WARN_ON_ONCE(ret == 0 || ret > rlen))
  1536. return -EIO;
  1537. if (ret < 0)
  1538. return ret;
  1539. rlen -= ret;
  1540. pos += ret;
  1541. /*
  1542. * Holes are not written back by ->writeback_range, so track
  1543. * if we did handle anything that is not a hole here.
  1544. */
  1545. if (wpc->iomap.type != IOMAP_HOLE)
  1546. *bytes_submitted += ret;
  1547. } while (rlen);
  1548. return 0;
  1549. }
  1550. /*
  1551. * Check interaction of the folio with the file end.
  1552. *
  1553. * If the folio is entirely beyond i_size, return false. If it straddles
  1554. * i_size, adjust end_pos and zero all data beyond i_size.
  1555. */
  1556. static bool iomap_writeback_handle_eof(struct folio *folio, struct inode *inode,
  1557. u64 *end_pos)
  1558. {
  1559. u64 isize = i_size_read(inode);
  1560. if (*end_pos > isize) {
  1561. size_t poff = offset_in_folio(folio, isize);
  1562. pgoff_t end_index = isize >> PAGE_SHIFT;
  1563. /*
  1564. * If the folio is entirely ouside of i_size, skip it.
  1565. *
  1566. * This can happen due to a truncate operation that is in
  1567. * progress and in that case truncate will finish it off once
  1568. * we've dropped the folio lock.
  1569. *
  1570. * Note that the pgoff_t used for end_index is an unsigned long.
  1571. * If the given offset is greater than 16TB on a 32-bit system,
  1572. * then if we checked if the folio is fully outside i_size with
  1573. * "if (folio->index >= end_index + 1)", "end_index + 1" would
  1574. * overflow and evaluate to 0. Hence this folio would be
  1575. * redirtied and written out repeatedly, which would result in
  1576. * an infinite loop; the user program performing this operation
  1577. * would hang. Instead, we can detect this situation by
  1578. * checking if the folio is totally beyond i_size or if its
  1579. * offset is just equal to the EOF.
  1580. */
  1581. if (folio->index > end_index ||
  1582. (folio->index == end_index && poff == 0))
  1583. return false;
  1584. /*
  1585. * The folio straddles i_size.
  1586. *
  1587. * It must be zeroed out on each and every writepage invocation
  1588. * because it may be mmapped:
  1589. *
  1590. * A file is mapped in multiples of the page size. For a
  1591. * file that is not a multiple of the page size, the
  1592. * remaining memory is zeroed when mapped, and writes to that
  1593. * region are not written out to the file.
  1594. *
  1595. * Also adjust the end_pos to the end of file and skip writeback
  1596. * for all blocks entirely beyond i_size.
  1597. */
  1598. folio_zero_segment(folio, poff, folio_size(folio));
  1599. *end_pos = isize;
  1600. }
  1601. return true;
  1602. }
  1603. int iomap_writeback_folio(struct iomap_writepage_ctx *wpc, struct folio *folio)
  1604. {
  1605. struct iomap_folio_state *ifs = folio->private;
  1606. struct inode *inode = wpc->inode;
  1607. u64 pos = folio_pos(folio);
  1608. u64 end_pos = pos + folio_size(folio);
  1609. u64 end_aligned = 0;
  1610. loff_t orig_pos = pos;
  1611. size_t bytes_submitted = 0;
  1612. int error = 0;
  1613. u32 rlen;
  1614. WARN_ON_ONCE(!folio_test_locked(folio));
  1615. WARN_ON_ONCE(folio_test_dirty(folio));
  1616. WARN_ON_ONCE(folio_test_writeback(folio));
  1617. trace_iomap_writeback_folio(inode, pos, folio_size(folio));
  1618. if (!iomap_writeback_handle_eof(folio, inode, &end_pos))
  1619. return 0;
  1620. WARN_ON_ONCE(end_pos <= pos);
  1621. if (i_blocks_per_folio(inode, folio) > 1) {
  1622. if (!ifs) {
  1623. ifs = ifs_alloc(inode, folio, 0);
  1624. iomap_set_range_dirty(folio, 0, end_pos - pos);
  1625. }
  1626. iomap_writeback_init(inode, folio);
  1627. }
  1628. /*
  1629. * Set the writeback bit ASAP, as the I/O completion for the single
  1630. * block per folio case happen hit as soon as we're submitting the bio.
  1631. */
  1632. folio_start_writeback(folio);
  1633. /*
  1634. * Walk through the folio to find dirty areas to write back.
  1635. */
  1636. end_aligned = round_up(end_pos, i_blocksize(inode));
  1637. while ((rlen = iomap_find_dirty_range(folio, &pos, end_aligned))) {
  1638. error = iomap_writeback_range(wpc, folio, pos, rlen, end_pos,
  1639. &bytes_submitted);
  1640. if (error)
  1641. break;
  1642. pos += rlen;
  1643. }
  1644. if (bytes_submitted)
  1645. wpc->nr_folios++;
  1646. if (error && pos > orig_pos)
  1647. fserror_report_io(inode, FSERR_BUFFERED_WRITE, orig_pos, 0,
  1648. error, GFP_NOFS);
  1649. /*
  1650. * We can have dirty bits set past end of file in page_mkwrite path
  1651. * while mapping the last partial folio. Hence it's better to clear
  1652. * all the dirty bits in the folio here.
  1653. */
  1654. iomap_clear_range_dirty(folio, 0, folio_size(folio));
  1655. /*
  1656. * Usually the writeback bit is cleared by the I/O completion handler.
  1657. * But we may end up either not actually writing any blocks, or (when
  1658. * there are multiple blocks in a folio) all I/O might have finished
  1659. * already at this point. In that case we need to clear the writeback
  1660. * bit ourselves right after unlocking the page.
  1661. */
  1662. if (ifs) {
  1663. /*
  1664. * Subtract any bytes that were initially accounted to
  1665. * write_bytes_pending but skipped for writeback.
  1666. */
  1667. size_t bytes_not_submitted = folio_size(folio) -
  1668. bytes_submitted;
  1669. if (bytes_not_submitted)
  1670. iomap_finish_folio_write(inode, folio,
  1671. bytes_not_submitted);
  1672. } else if (!bytes_submitted) {
  1673. folio_end_writeback(folio);
  1674. }
  1675. mapping_set_error(inode->i_mapping, error);
  1676. return error;
  1677. }
  1678. EXPORT_SYMBOL_GPL(iomap_writeback_folio);
  1679. int
  1680. iomap_writepages(struct iomap_writepage_ctx *wpc)
  1681. {
  1682. struct address_space *mapping = wpc->inode->i_mapping;
  1683. struct folio *folio = NULL;
  1684. int error;
  1685. /*
  1686. * Writeback from reclaim context should never happen except in the case
  1687. * of a VM regression so warn about it and refuse to write the data.
  1688. */
  1689. if (WARN_ON_ONCE((current->flags & (PF_MEMALLOC | PF_KSWAPD)) ==
  1690. PF_MEMALLOC))
  1691. return -EIO;
  1692. while ((folio = writeback_iter(mapping, wpc->wbc, folio, &error))) {
  1693. error = iomap_writeback_folio(wpc, folio);
  1694. folio_unlock(folio);
  1695. }
  1696. /*
  1697. * If @error is non-zero, it means that we have a situation where some
  1698. * part of the submission process has failed after we've marked pages
  1699. * for writeback.
  1700. *
  1701. * We cannot cancel the writeback directly in that case, so always call
  1702. * ->writeback_submit to run the I/O completion handler to clear the
  1703. * writeback bit and let the file system proess the errors.
  1704. */
  1705. if (wpc->wb_ctx)
  1706. return wpc->ops->writeback_submit(wpc, error);
  1707. return error;
  1708. }
  1709. EXPORT_SYMBOL_GPL(iomap_writepages);