xfs_aops.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740
  1. /*
  2. * Copyright (c) 2000-2005 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "xfs.h"
  19. #include "xfs_shared.h"
  20. #include "xfs_format.h"
  21. #include "xfs_log_format.h"
  22. #include "xfs_trans_resv.h"
  23. #include "xfs_mount.h"
  24. #include "xfs_inode.h"
  25. #include "xfs_trans.h"
  26. #include "xfs_inode_item.h"
  27. #include "xfs_alloc.h"
  28. #include "xfs_error.h"
  29. #include "xfs_iomap.h"
  30. #include "xfs_trace.h"
  31. #include "xfs_bmap.h"
  32. #include "xfs_bmap_util.h"
  33. #include "xfs_bmap_btree.h"
  34. #include "xfs_reflink.h"
  35. #include <linux/gfp.h>
  36. #include <linux/mpage.h>
  37. #include <linux/pagevec.h>
  38. #include <linux/writeback.h>
  39. /* flags for direct write completions */
  40. #define XFS_DIO_FLAG_UNWRITTEN (1 << 0)
  41. #define XFS_DIO_FLAG_APPEND (1 << 1)
  42. #define XFS_DIO_FLAG_COW (1 << 2)
  43. /*
  44. * structure owned by writepages passed to individual writepage calls
  45. */
  46. struct xfs_writepage_ctx {
  47. struct xfs_bmbt_irec imap;
  48. bool imap_valid;
  49. unsigned int io_type;
  50. struct xfs_ioend *ioend;
  51. sector_t last_block;
  52. };
  53. void
  54. xfs_count_page_state(
  55. struct page *page,
  56. int *delalloc,
  57. int *unwritten)
  58. {
  59. struct buffer_head *bh, *head;
  60. *delalloc = *unwritten = 0;
  61. bh = head = page_buffers(page);
  62. do {
  63. if (buffer_unwritten(bh))
  64. (*unwritten) = 1;
  65. else if (buffer_delay(bh))
  66. (*delalloc) = 1;
  67. } while ((bh = bh->b_this_page) != head);
  68. }
  69. struct block_device *
  70. xfs_find_bdev_for_inode(
  71. struct inode *inode)
  72. {
  73. struct xfs_inode *ip = XFS_I(inode);
  74. struct xfs_mount *mp = ip->i_mount;
  75. if (XFS_IS_REALTIME_INODE(ip))
  76. return mp->m_rtdev_targp->bt_bdev;
  77. else
  78. return mp->m_ddev_targp->bt_bdev;
  79. }
  80. /*
  81. * We're now finished for good with this page. Update the page state via the
  82. * associated buffer_heads, paying attention to the start and end offsets that
  83. * we need to process on the page.
  84. *
  85. * Note that we open code the action in end_buffer_async_write here so that we
  86. * only have to iterate over the buffers attached to the page once. This is not
  87. * only more efficient, but also ensures that we only calls end_page_writeback
  88. * at the end of the iteration, and thus avoids the pitfall of having the page
  89. * and buffers potentially freed after every call to end_buffer_async_write.
  90. */
  91. static void
  92. xfs_finish_page_writeback(
  93. struct inode *inode,
  94. struct bio_vec *bvec,
  95. int error)
  96. {
  97. struct buffer_head *head = page_buffers(bvec->bv_page), *bh = head;
  98. bool busy = false;
  99. unsigned int off = 0;
  100. unsigned long flags;
  101. ASSERT(bvec->bv_offset < PAGE_SIZE);
  102. ASSERT((bvec->bv_offset & (i_blocksize(inode) - 1)) == 0);
  103. ASSERT(bvec->bv_offset + bvec->bv_len <= PAGE_SIZE);
  104. ASSERT((bvec->bv_len & (i_blocksize(inode) - 1)) == 0);
  105. local_irq_save(flags);
  106. bit_spin_lock(BH_Uptodate_Lock, &head->b_state);
  107. do {
  108. if (off >= bvec->bv_offset &&
  109. off < bvec->bv_offset + bvec->bv_len) {
  110. ASSERT(buffer_async_write(bh));
  111. ASSERT(bh->b_end_io == NULL);
  112. if (error) {
  113. mapping_set_error(bvec->bv_page->mapping, -EIO);
  114. set_buffer_write_io_error(bh);
  115. clear_buffer_uptodate(bh);
  116. SetPageError(bvec->bv_page);
  117. } else {
  118. set_buffer_uptodate(bh);
  119. }
  120. clear_buffer_async_write(bh);
  121. unlock_buffer(bh);
  122. } else if (buffer_async_write(bh)) {
  123. ASSERT(buffer_locked(bh));
  124. busy = true;
  125. }
  126. off += bh->b_size;
  127. } while ((bh = bh->b_this_page) != head);
  128. bit_spin_unlock(BH_Uptodate_Lock, &head->b_state);
  129. local_irq_restore(flags);
  130. if (!busy)
  131. end_page_writeback(bvec->bv_page);
  132. }
  133. /*
  134. * We're now finished for good with this ioend structure. Update the page
  135. * state, release holds on bios, and finally free up memory. Do not use the
  136. * ioend after this.
  137. */
  138. STATIC void
  139. xfs_destroy_ioend(
  140. struct xfs_ioend *ioend,
  141. int error)
  142. {
  143. struct inode *inode = ioend->io_inode;
  144. struct bio *bio = &ioend->io_inline_bio;
  145. struct bio *last = ioend->io_bio, *next;
  146. u64 start = bio->bi_iter.bi_sector;
  147. bool quiet = bio_flagged(bio, BIO_QUIET);
  148. for (bio = &ioend->io_inline_bio; bio; bio = next) {
  149. struct bio_vec *bvec;
  150. int i;
  151. /*
  152. * For the last bio, bi_private points to the ioend, so we
  153. * need to explicitly end the iteration here.
  154. */
  155. if (bio == last)
  156. next = NULL;
  157. else
  158. next = bio->bi_private;
  159. /* walk each page on bio, ending page IO on them */
  160. bio_for_each_segment_all(bvec, bio, i)
  161. xfs_finish_page_writeback(inode, bvec, error);
  162. bio_put(bio);
  163. }
  164. if (unlikely(error && !quiet)) {
  165. xfs_err_ratelimited(XFS_I(inode)->i_mount,
  166. "writeback error on sector %llu", start);
  167. }
  168. }
  169. /*
  170. * Fast and loose check if this write could update the on-disk inode size.
  171. */
  172. static inline bool xfs_ioend_is_append(struct xfs_ioend *ioend)
  173. {
  174. return ioend->io_offset + ioend->io_size >
  175. XFS_I(ioend->io_inode)->i_d.di_size;
  176. }
  177. STATIC int
  178. xfs_setfilesize_trans_alloc(
  179. struct xfs_ioend *ioend)
  180. {
  181. struct xfs_mount *mp = XFS_I(ioend->io_inode)->i_mount;
  182. struct xfs_trans *tp;
  183. int error;
  184. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_fsyncts, 0, 0, 0, &tp);
  185. if (error)
  186. return error;
  187. ioend->io_append_trans = tp;
  188. /*
  189. * We may pass freeze protection with a transaction. So tell lockdep
  190. * we released it.
  191. */
  192. __sb_writers_release(ioend->io_inode->i_sb, SB_FREEZE_FS);
  193. /*
  194. * We hand off the transaction to the completion thread now, so
  195. * clear the flag here.
  196. */
  197. current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
  198. return 0;
  199. }
  200. /*
  201. * Update on-disk file size now that data has been written to disk.
  202. */
  203. STATIC int
  204. __xfs_setfilesize(
  205. struct xfs_inode *ip,
  206. struct xfs_trans *tp,
  207. xfs_off_t offset,
  208. size_t size)
  209. {
  210. xfs_fsize_t isize;
  211. xfs_ilock(ip, XFS_ILOCK_EXCL);
  212. isize = xfs_new_eof(ip, offset + size);
  213. if (!isize) {
  214. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  215. xfs_trans_cancel(tp);
  216. return 0;
  217. }
  218. trace_xfs_setfilesize(ip, offset, size);
  219. ip->i_d.di_size = isize;
  220. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  221. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  222. return xfs_trans_commit(tp);
  223. }
  224. int
  225. xfs_setfilesize(
  226. struct xfs_inode *ip,
  227. xfs_off_t offset,
  228. size_t size)
  229. {
  230. struct xfs_mount *mp = ip->i_mount;
  231. struct xfs_trans *tp;
  232. int error;
  233. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_fsyncts, 0, 0, 0, &tp);
  234. if (error)
  235. return error;
  236. return __xfs_setfilesize(ip, tp, offset, size);
  237. }
  238. STATIC int
  239. xfs_setfilesize_ioend(
  240. struct xfs_ioend *ioend,
  241. int error)
  242. {
  243. struct xfs_inode *ip = XFS_I(ioend->io_inode);
  244. struct xfs_trans *tp = ioend->io_append_trans;
  245. /*
  246. * The transaction may have been allocated in the I/O submission thread,
  247. * thus we need to mark ourselves as being in a transaction manually.
  248. * Similarly for freeze protection.
  249. */
  250. current_set_flags_nested(&tp->t_pflags, PF_FSTRANS);
  251. __sb_writers_acquired(VFS_I(ip)->i_sb, SB_FREEZE_FS);
  252. /* we abort the update if there was an IO error */
  253. if (error) {
  254. xfs_trans_cancel(tp);
  255. return error;
  256. }
  257. return __xfs_setfilesize(ip, tp, ioend->io_offset, ioend->io_size);
  258. }
  259. /*
  260. * IO write completion.
  261. */
  262. STATIC void
  263. xfs_end_io(
  264. struct work_struct *work)
  265. {
  266. struct xfs_ioend *ioend =
  267. container_of(work, struct xfs_ioend, io_work);
  268. struct xfs_inode *ip = XFS_I(ioend->io_inode);
  269. xfs_off_t offset = ioend->io_offset;
  270. size_t size = ioend->io_size;
  271. int error = ioend->io_bio->bi_error;
  272. /*
  273. * Just clean up the in-memory strutures if the fs has been shut down.
  274. */
  275. if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
  276. error = -EIO;
  277. goto done;
  278. }
  279. /*
  280. * Clean up any COW blocks on an I/O error.
  281. */
  282. if (unlikely(error)) {
  283. switch (ioend->io_type) {
  284. case XFS_IO_COW:
  285. xfs_reflink_cancel_cow_range(ip, offset, size, true);
  286. break;
  287. }
  288. goto done;
  289. }
  290. /*
  291. * Success: commit the COW or unwritten blocks if needed.
  292. */
  293. switch (ioend->io_type) {
  294. case XFS_IO_COW:
  295. error = xfs_reflink_end_cow(ip, offset, size);
  296. break;
  297. case XFS_IO_UNWRITTEN:
  298. /* writeback should never update isize */
  299. error = xfs_iomap_write_unwritten(ip, offset, size, false);
  300. break;
  301. default:
  302. ASSERT(!xfs_ioend_is_append(ioend) || ioend->io_append_trans);
  303. break;
  304. }
  305. done:
  306. if (ioend->io_append_trans)
  307. error = xfs_setfilesize_ioend(ioend, error);
  308. xfs_destroy_ioend(ioend, error);
  309. }
  310. STATIC void
  311. xfs_end_bio(
  312. struct bio *bio)
  313. {
  314. struct xfs_ioend *ioend = bio->bi_private;
  315. struct xfs_mount *mp = XFS_I(ioend->io_inode)->i_mount;
  316. if (ioend->io_type == XFS_IO_UNWRITTEN || ioend->io_type == XFS_IO_COW)
  317. queue_work(mp->m_unwritten_workqueue, &ioend->io_work);
  318. else if (ioend->io_append_trans)
  319. queue_work(mp->m_data_workqueue, &ioend->io_work);
  320. else
  321. xfs_destroy_ioend(ioend, bio->bi_error);
  322. }
  323. STATIC int
  324. xfs_map_blocks(
  325. struct inode *inode,
  326. loff_t offset,
  327. struct xfs_bmbt_irec *imap,
  328. int type)
  329. {
  330. struct xfs_inode *ip = XFS_I(inode);
  331. struct xfs_mount *mp = ip->i_mount;
  332. ssize_t count = i_blocksize(inode);
  333. xfs_fileoff_t offset_fsb, end_fsb;
  334. int error = 0;
  335. int bmapi_flags = XFS_BMAPI_ENTIRE;
  336. int nimaps = 1;
  337. if (XFS_FORCED_SHUTDOWN(mp))
  338. return -EIO;
  339. ASSERT(type != XFS_IO_COW);
  340. if (type == XFS_IO_UNWRITTEN)
  341. bmapi_flags |= XFS_BMAPI_IGSTATE;
  342. xfs_ilock(ip, XFS_ILOCK_SHARED);
  343. ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
  344. (ip->i_df.if_flags & XFS_IFEXTENTS));
  345. ASSERT(offset <= mp->m_super->s_maxbytes);
  346. if ((xfs_ufsize_t)offset + count > mp->m_super->s_maxbytes)
  347. count = mp->m_super->s_maxbytes - offset;
  348. end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
  349. offset_fsb = XFS_B_TO_FSBT(mp, offset);
  350. error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb,
  351. imap, &nimaps, bmapi_flags);
  352. /*
  353. * Truncate an overwrite extent if there's a pending CoW
  354. * reservation before the end of this extent. This forces us
  355. * to come back to writepage to take care of the CoW.
  356. */
  357. if (nimaps && type == XFS_IO_OVERWRITE)
  358. xfs_reflink_trim_irec_to_next_cow(ip, offset_fsb, imap);
  359. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  360. if (error)
  361. return error;
  362. if (type == XFS_IO_DELALLOC &&
  363. (!nimaps || isnullstartblock(imap->br_startblock))) {
  364. error = xfs_iomap_write_allocate(ip, XFS_DATA_FORK, offset,
  365. imap);
  366. if (!error)
  367. trace_xfs_map_blocks_alloc(ip, offset, count, type, imap);
  368. return error;
  369. }
  370. #ifdef DEBUG
  371. if (type == XFS_IO_UNWRITTEN) {
  372. ASSERT(nimaps);
  373. ASSERT(imap->br_startblock != HOLESTARTBLOCK);
  374. ASSERT(imap->br_startblock != DELAYSTARTBLOCK);
  375. }
  376. #endif
  377. if (nimaps)
  378. trace_xfs_map_blocks_found(ip, offset, count, type, imap);
  379. return 0;
  380. }
  381. STATIC bool
  382. xfs_imap_valid(
  383. struct inode *inode,
  384. struct xfs_bmbt_irec *imap,
  385. xfs_off_t offset)
  386. {
  387. offset >>= inode->i_blkbits;
  388. /*
  389. * We have to make sure the cached mapping is within EOF to protect
  390. * against eofblocks trimming on file release leaving us with a stale
  391. * mapping. Otherwise, a page for a subsequent file extending buffered
  392. * write could get picked up by this writeback cycle and written to the
  393. * wrong blocks.
  394. *
  395. * Note that what we really want here is a generic mapping invalidation
  396. * mechanism to protect us from arbitrary extent modifying contexts, not
  397. * just eofblocks.
  398. */
  399. xfs_trim_extent_eof(imap, XFS_I(inode));
  400. return offset >= imap->br_startoff &&
  401. offset < imap->br_startoff + imap->br_blockcount;
  402. }
  403. STATIC void
  404. xfs_start_buffer_writeback(
  405. struct buffer_head *bh)
  406. {
  407. ASSERT(buffer_mapped(bh));
  408. ASSERT(buffer_locked(bh));
  409. ASSERT(!buffer_delay(bh));
  410. ASSERT(!buffer_unwritten(bh));
  411. bh->b_end_io = NULL;
  412. set_buffer_async_write(bh);
  413. set_buffer_uptodate(bh);
  414. clear_buffer_dirty(bh);
  415. }
  416. STATIC void
  417. xfs_start_page_writeback(
  418. struct page *page,
  419. int clear_dirty)
  420. {
  421. ASSERT(PageLocked(page));
  422. ASSERT(!PageWriteback(page));
  423. /*
  424. * if the page was not fully cleaned, we need to ensure that the higher
  425. * layers come back to it correctly. That means we need to keep the page
  426. * dirty, and for WB_SYNC_ALL writeback we need to ensure the
  427. * PAGECACHE_TAG_TOWRITE index mark is not removed so another attempt to
  428. * write this page in this writeback sweep will be made.
  429. */
  430. if (clear_dirty) {
  431. clear_page_dirty_for_io(page);
  432. set_page_writeback(page);
  433. } else
  434. set_page_writeback_keepwrite(page);
  435. unlock_page(page);
  436. }
  437. static inline int xfs_bio_add_buffer(struct bio *bio, struct buffer_head *bh)
  438. {
  439. return bio_add_page(bio, bh->b_page, bh->b_size, bh_offset(bh));
  440. }
  441. /*
  442. * Submit the bio for an ioend. We are passed an ioend with a bio attached to
  443. * it, and we submit that bio. The ioend may be used for multiple bio
  444. * submissions, so we only want to allocate an append transaction for the ioend
  445. * once. In the case of multiple bio submission, each bio will take an IO
  446. * reference to the ioend to ensure that the ioend completion is only done once
  447. * all bios have been submitted and the ioend is really done.
  448. *
  449. * If @fail is non-zero, it means that we have a situation where some part of
  450. * the submission process has failed after we have marked paged for writeback
  451. * and unlocked them. In this situation, we need to fail the bio and ioend
  452. * rather than submit it to IO. This typically only happens on a filesystem
  453. * shutdown.
  454. */
  455. STATIC int
  456. xfs_submit_ioend(
  457. struct writeback_control *wbc,
  458. struct xfs_ioend *ioend,
  459. int status)
  460. {
  461. /* Convert CoW extents to regular */
  462. if (!status && ioend->io_type == XFS_IO_COW) {
  463. status = xfs_reflink_convert_cow(XFS_I(ioend->io_inode),
  464. ioend->io_offset, ioend->io_size);
  465. }
  466. /* Reserve log space if we might write beyond the on-disk inode size. */
  467. if (!status &&
  468. ioend->io_type != XFS_IO_UNWRITTEN &&
  469. xfs_ioend_is_append(ioend) &&
  470. !ioend->io_append_trans)
  471. status = xfs_setfilesize_trans_alloc(ioend);
  472. ioend->io_bio->bi_private = ioend;
  473. ioend->io_bio->bi_end_io = xfs_end_bio;
  474. bio_set_op_attrs(ioend->io_bio, REQ_OP_WRITE,
  475. (wbc->sync_mode == WB_SYNC_ALL) ? WRITE_SYNC : 0);
  476. /*
  477. * If we are failing the IO now, just mark the ioend with an
  478. * error and finish it. This will run IO completion immediately
  479. * as there is only one reference to the ioend at this point in
  480. * time.
  481. */
  482. if (status) {
  483. ioend->io_bio->bi_error = status;
  484. bio_endio(ioend->io_bio);
  485. return status;
  486. }
  487. submit_bio(ioend->io_bio);
  488. return 0;
  489. }
  490. static void
  491. xfs_init_bio_from_bh(
  492. struct bio *bio,
  493. struct buffer_head *bh)
  494. {
  495. bio->bi_iter.bi_sector = bh->b_blocknr * (bh->b_size >> 9);
  496. bio->bi_bdev = bh->b_bdev;
  497. }
  498. static struct xfs_ioend *
  499. xfs_alloc_ioend(
  500. struct inode *inode,
  501. unsigned int type,
  502. xfs_off_t offset,
  503. struct buffer_head *bh)
  504. {
  505. struct xfs_ioend *ioend;
  506. struct bio *bio;
  507. bio = bio_alloc_bioset(GFP_NOFS, BIO_MAX_PAGES, xfs_ioend_bioset);
  508. xfs_init_bio_from_bh(bio, bh);
  509. ioend = container_of(bio, struct xfs_ioend, io_inline_bio);
  510. INIT_LIST_HEAD(&ioend->io_list);
  511. ioend->io_type = type;
  512. ioend->io_inode = inode;
  513. ioend->io_size = 0;
  514. ioend->io_offset = offset;
  515. INIT_WORK(&ioend->io_work, xfs_end_io);
  516. ioend->io_append_trans = NULL;
  517. ioend->io_bio = bio;
  518. return ioend;
  519. }
  520. /*
  521. * Allocate a new bio, and chain the old bio to the new one.
  522. *
  523. * Note that we have to do perform the chaining in this unintuitive order
  524. * so that the bi_private linkage is set up in the right direction for the
  525. * traversal in xfs_destroy_ioend().
  526. */
  527. static void
  528. xfs_chain_bio(
  529. struct xfs_ioend *ioend,
  530. struct writeback_control *wbc,
  531. struct buffer_head *bh)
  532. {
  533. struct bio *new;
  534. new = bio_alloc(GFP_NOFS, BIO_MAX_PAGES);
  535. xfs_init_bio_from_bh(new, bh);
  536. bio_chain(ioend->io_bio, new);
  537. bio_get(ioend->io_bio); /* for xfs_destroy_ioend */
  538. bio_set_op_attrs(ioend->io_bio, REQ_OP_WRITE,
  539. (wbc->sync_mode == WB_SYNC_ALL) ? WRITE_SYNC : 0);
  540. submit_bio(ioend->io_bio);
  541. ioend->io_bio = new;
  542. }
  543. /*
  544. * Test to see if we've been building up a completion structure for
  545. * earlier buffers -- if so, we try to append to this ioend if we
  546. * can, otherwise we finish off any current ioend and start another.
  547. * Return the ioend we finished off so that the caller can submit it
  548. * once it has finished processing the dirty page.
  549. */
  550. STATIC void
  551. xfs_add_to_ioend(
  552. struct inode *inode,
  553. struct buffer_head *bh,
  554. xfs_off_t offset,
  555. struct xfs_writepage_ctx *wpc,
  556. struct writeback_control *wbc,
  557. struct list_head *iolist)
  558. {
  559. if (!wpc->ioend || wpc->io_type != wpc->ioend->io_type ||
  560. bh->b_blocknr != wpc->last_block + 1 ||
  561. offset != wpc->ioend->io_offset + wpc->ioend->io_size) {
  562. if (wpc->ioend)
  563. list_add(&wpc->ioend->io_list, iolist);
  564. wpc->ioend = xfs_alloc_ioend(inode, wpc->io_type, offset, bh);
  565. }
  566. /*
  567. * If the buffer doesn't fit into the bio we need to allocate a new
  568. * one. This shouldn't happen more than once for a given buffer.
  569. */
  570. while (xfs_bio_add_buffer(wpc->ioend->io_bio, bh) != bh->b_size)
  571. xfs_chain_bio(wpc->ioend, wbc, bh);
  572. wpc->ioend->io_size += bh->b_size;
  573. wpc->last_block = bh->b_blocknr;
  574. xfs_start_buffer_writeback(bh);
  575. }
  576. STATIC void
  577. xfs_map_buffer(
  578. struct inode *inode,
  579. struct buffer_head *bh,
  580. struct xfs_bmbt_irec *imap,
  581. xfs_off_t offset)
  582. {
  583. sector_t bn;
  584. struct xfs_mount *m = XFS_I(inode)->i_mount;
  585. xfs_off_t iomap_offset = XFS_FSB_TO_B(m, imap->br_startoff);
  586. xfs_daddr_t iomap_bn = xfs_fsb_to_db(XFS_I(inode), imap->br_startblock);
  587. ASSERT(imap->br_startblock != HOLESTARTBLOCK);
  588. ASSERT(imap->br_startblock != DELAYSTARTBLOCK);
  589. bn = (iomap_bn >> (inode->i_blkbits - BBSHIFT)) +
  590. ((offset - iomap_offset) >> inode->i_blkbits);
  591. ASSERT(bn || XFS_IS_REALTIME_INODE(XFS_I(inode)));
  592. bh->b_blocknr = bn;
  593. set_buffer_mapped(bh);
  594. }
  595. STATIC void
  596. xfs_map_at_offset(
  597. struct inode *inode,
  598. struct buffer_head *bh,
  599. struct xfs_bmbt_irec *imap,
  600. xfs_off_t offset)
  601. {
  602. ASSERT(imap->br_startblock != HOLESTARTBLOCK);
  603. ASSERT(imap->br_startblock != DELAYSTARTBLOCK);
  604. xfs_map_buffer(inode, bh, imap, offset);
  605. set_buffer_mapped(bh);
  606. clear_buffer_delay(bh);
  607. clear_buffer_unwritten(bh);
  608. }
  609. /*
  610. * Test if a given page contains at least one buffer of a given @type.
  611. * If @check_all_buffers is true, then we walk all the buffers in the page to
  612. * try to find one of the type passed in. If it is not set, then the caller only
  613. * needs to check the first buffer on the page for a match.
  614. */
  615. STATIC bool
  616. xfs_check_page_type(
  617. struct page *page,
  618. unsigned int type,
  619. bool check_all_buffers)
  620. {
  621. struct buffer_head *bh;
  622. struct buffer_head *head;
  623. if (PageWriteback(page))
  624. return false;
  625. if (!page->mapping)
  626. return false;
  627. if (!page_has_buffers(page))
  628. return false;
  629. bh = head = page_buffers(page);
  630. do {
  631. if (buffer_unwritten(bh)) {
  632. if (type == XFS_IO_UNWRITTEN)
  633. return true;
  634. } else if (buffer_delay(bh)) {
  635. if (type == XFS_IO_DELALLOC)
  636. return true;
  637. } else if (buffer_dirty(bh) && buffer_mapped(bh)) {
  638. if (type == XFS_IO_OVERWRITE)
  639. return true;
  640. }
  641. /* If we are only checking the first buffer, we are done now. */
  642. if (!check_all_buffers)
  643. break;
  644. } while ((bh = bh->b_this_page) != head);
  645. return false;
  646. }
  647. STATIC void
  648. xfs_vm_invalidatepage(
  649. struct page *page,
  650. unsigned int offset,
  651. unsigned int length)
  652. {
  653. trace_xfs_invalidatepage(page->mapping->host, page, offset,
  654. length);
  655. /*
  656. * If we are invalidating the entire page, clear the dirty state from it
  657. * so that we can check for attempts to release dirty cached pages in
  658. * xfs_vm_releasepage().
  659. */
  660. if (offset == 0 && length >= PAGE_SIZE)
  661. cancel_dirty_page(page);
  662. block_invalidatepage(page, offset, length);
  663. }
  664. /*
  665. * If the page has delalloc buffers on it, we need to punch them out before we
  666. * invalidate the page. If we don't, we leave a stale delalloc mapping on the
  667. * inode that can trip a BUG() in xfs_get_blocks() later on if a direct IO read
  668. * is done on that same region - the delalloc extent is returned when none is
  669. * supposed to be there.
  670. *
  671. * We prevent this by truncating away the delalloc regions on the page before
  672. * invalidating it. Because they are delalloc, we can do this without needing a
  673. * transaction. Indeed - if we get ENOSPC errors, we have to be able to do this
  674. * truncation without a transaction as there is no space left for block
  675. * reservation (typically why we see a ENOSPC in writeback).
  676. *
  677. * This is not a performance critical path, so for now just do the punching a
  678. * buffer head at a time.
  679. */
  680. STATIC void
  681. xfs_aops_discard_page(
  682. struct page *page)
  683. {
  684. struct inode *inode = page->mapping->host;
  685. struct xfs_inode *ip = XFS_I(inode);
  686. struct buffer_head *bh, *head;
  687. loff_t offset = page_offset(page);
  688. if (!xfs_check_page_type(page, XFS_IO_DELALLOC, true))
  689. goto out_invalidate;
  690. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  691. goto out_invalidate;
  692. xfs_alert(ip->i_mount,
  693. "page discard on page %p, inode 0x%llx, offset %llu.",
  694. page, ip->i_ino, offset);
  695. xfs_ilock(ip, XFS_ILOCK_EXCL);
  696. bh = head = page_buffers(page);
  697. do {
  698. int error;
  699. xfs_fileoff_t start_fsb;
  700. if (!buffer_delay(bh))
  701. goto next_buffer;
  702. start_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
  703. error = xfs_bmap_punch_delalloc_range(ip, start_fsb, 1);
  704. if (error) {
  705. /* something screwed, just bail */
  706. if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
  707. xfs_alert(ip->i_mount,
  708. "page discard unable to remove delalloc mapping.");
  709. }
  710. break;
  711. }
  712. next_buffer:
  713. offset += i_blocksize(inode);
  714. } while ((bh = bh->b_this_page) != head);
  715. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  716. out_invalidate:
  717. xfs_vm_invalidatepage(page, 0, PAGE_SIZE);
  718. return;
  719. }
  720. static int
  721. xfs_map_cow(
  722. struct xfs_writepage_ctx *wpc,
  723. struct inode *inode,
  724. loff_t offset,
  725. unsigned int *new_type)
  726. {
  727. struct xfs_inode *ip = XFS_I(inode);
  728. struct xfs_bmbt_irec imap;
  729. bool is_cow = false, need_alloc = false;
  730. int error;
  731. /*
  732. * If we already have a valid COW mapping keep using it.
  733. */
  734. if (wpc->io_type == XFS_IO_COW) {
  735. wpc->imap_valid = xfs_imap_valid(inode, &wpc->imap, offset);
  736. if (wpc->imap_valid) {
  737. *new_type = XFS_IO_COW;
  738. return 0;
  739. }
  740. }
  741. /*
  742. * Else we need to check if there is a COW mapping at this offset.
  743. */
  744. xfs_ilock(ip, XFS_ILOCK_SHARED);
  745. is_cow = xfs_reflink_find_cow_mapping(ip, offset, &imap, &need_alloc);
  746. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  747. if (!is_cow)
  748. return 0;
  749. /*
  750. * And if the COW mapping has a delayed extent here we need to
  751. * allocate real space for it now.
  752. */
  753. if (need_alloc) {
  754. error = xfs_iomap_write_allocate(ip, XFS_COW_FORK, offset,
  755. &imap);
  756. if (error)
  757. return error;
  758. }
  759. wpc->io_type = *new_type = XFS_IO_COW;
  760. wpc->imap_valid = true;
  761. wpc->imap = imap;
  762. return 0;
  763. }
  764. /*
  765. * We implement an immediate ioend submission policy here to avoid needing to
  766. * chain multiple ioends and hence nest mempool allocations which can violate
  767. * forward progress guarantees we need to provide. The current ioend we are
  768. * adding buffers to is cached on the writepage context, and if the new buffer
  769. * does not append to the cached ioend it will create a new ioend and cache that
  770. * instead.
  771. *
  772. * If a new ioend is created and cached, the old ioend is returned and queued
  773. * locally for submission once the entire page is processed or an error has been
  774. * detected. While ioends are submitted immediately after they are completed,
  775. * batching optimisations are provided by higher level block plugging.
  776. *
  777. * At the end of a writeback pass, there will be a cached ioend remaining on the
  778. * writepage context that the caller will need to submit.
  779. */
  780. static int
  781. xfs_writepage_map(
  782. struct xfs_writepage_ctx *wpc,
  783. struct writeback_control *wbc,
  784. struct inode *inode,
  785. struct page *page,
  786. loff_t offset,
  787. __uint64_t end_offset)
  788. {
  789. LIST_HEAD(submit_list);
  790. struct xfs_ioend *ioend, *next;
  791. struct buffer_head *bh, *head;
  792. ssize_t len = i_blocksize(inode);
  793. int error = 0;
  794. int count = 0;
  795. int uptodate = 1;
  796. unsigned int new_type;
  797. bh = head = page_buffers(page);
  798. offset = page_offset(page);
  799. do {
  800. if (offset >= end_offset)
  801. break;
  802. if (!buffer_uptodate(bh))
  803. uptodate = 0;
  804. /*
  805. * set_page_dirty dirties all buffers in a page, independent
  806. * of their state. The dirty state however is entirely
  807. * meaningless for holes (!mapped && uptodate), so skip
  808. * buffers covering holes here.
  809. */
  810. if (!buffer_mapped(bh) && buffer_uptodate(bh)) {
  811. wpc->imap_valid = false;
  812. continue;
  813. }
  814. if (buffer_unwritten(bh))
  815. new_type = XFS_IO_UNWRITTEN;
  816. else if (buffer_delay(bh))
  817. new_type = XFS_IO_DELALLOC;
  818. else if (buffer_uptodate(bh))
  819. new_type = XFS_IO_OVERWRITE;
  820. else {
  821. if (PageUptodate(page))
  822. ASSERT(buffer_mapped(bh));
  823. /*
  824. * This buffer is not uptodate and will not be
  825. * written to disk. Ensure that we will put any
  826. * subsequent writeable buffers into a new
  827. * ioend.
  828. */
  829. wpc->imap_valid = false;
  830. continue;
  831. }
  832. if (xfs_is_reflink_inode(XFS_I(inode))) {
  833. error = xfs_map_cow(wpc, inode, offset, &new_type);
  834. if (error)
  835. goto out;
  836. }
  837. if (wpc->io_type != new_type) {
  838. wpc->io_type = new_type;
  839. wpc->imap_valid = false;
  840. }
  841. if (wpc->imap_valid)
  842. wpc->imap_valid = xfs_imap_valid(inode, &wpc->imap,
  843. offset);
  844. if (!wpc->imap_valid) {
  845. error = xfs_map_blocks(inode, offset, &wpc->imap,
  846. wpc->io_type);
  847. if (error)
  848. goto out;
  849. wpc->imap_valid = xfs_imap_valid(inode, &wpc->imap,
  850. offset);
  851. }
  852. if (wpc->imap_valid) {
  853. lock_buffer(bh);
  854. if (wpc->io_type != XFS_IO_OVERWRITE)
  855. xfs_map_at_offset(inode, bh, &wpc->imap, offset);
  856. xfs_add_to_ioend(inode, bh, offset, wpc, wbc, &submit_list);
  857. count++;
  858. }
  859. } while (offset += len, ((bh = bh->b_this_page) != head));
  860. if (uptodate && bh == head)
  861. SetPageUptodate(page);
  862. ASSERT(wpc->ioend || list_empty(&submit_list));
  863. out:
  864. /*
  865. * On error, we have to fail the ioend here because we have locked
  866. * buffers in the ioend. If we don't do this, we'll deadlock
  867. * invalidating the page as that tries to lock the buffers on the page.
  868. * Also, because we may have set pages under writeback, we have to make
  869. * sure we run IO completion to mark the error state of the IO
  870. * appropriately, so we can't cancel the ioend directly here. That means
  871. * we have to mark this page as under writeback if we included any
  872. * buffers from it in the ioend chain so that completion treats it
  873. * correctly.
  874. *
  875. * If we didn't include the page in the ioend, the on error we can
  876. * simply discard and unlock it as there are no other users of the page
  877. * or it's buffers right now. The caller will still need to trigger
  878. * submission of outstanding ioends on the writepage context so they are
  879. * treated correctly on error.
  880. */
  881. if (count) {
  882. xfs_start_page_writeback(page, !error);
  883. /*
  884. * Preserve the original error if there was one, otherwise catch
  885. * submission errors here and propagate into subsequent ioend
  886. * submissions.
  887. */
  888. list_for_each_entry_safe(ioend, next, &submit_list, io_list) {
  889. int error2;
  890. list_del_init(&ioend->io_list);
  891. error2 = xfs_submit_ioend(wbc, ioend, error);
  892. if (error2 && !error)
  893. error = error2;
  894. }
  895. } else if (error) {
  896. xfs_aops_discard_page(page);
  897. ClearPageUptodate(page);
  898. unlock_page(page);
  899. } else {
  900. /*
  901. * We can end up here with no error and nothing to write if we
  902. * race with a partial page truncate on a sub-page block sized
  903. * filesystem. In that case we need to mark the page clean.
  904. */
  905. xfs_start_page_writeback(page, 1);
  906. end_page_writeback(page);
  907. }
  908. mapping_set_error(page->mapping, error);
  909. return error;
  910. }
  911. /*
  912. * Write out a dirty page.
  913. *
  914. * For delalloc space on the page we need to allocate space and flush it.
  915. * For unwritten space on the page we need to start the conversion to
  916. * regular allocated space.
  917. * For any other dirty buffer heads on the page we should flush them.
  918. */
  919. STATIC int
  920. xfs_do_writepage(
  921. struct page *page,
  922. struct writeback_control *wbc,
  923. void *data)
  924. {
  925. struct xfs_writepage_ctx *wpc = data;
  926. struct inode *inode = page->mapping->host;
  927. loff_t offset;
  928. __uint64_t end_offset;
  929. pgoff_t end_index;
  930. trace_xfs_writepage(inode, page, 0, 0);
  931. ASSERT(page_has_buffers(page));
  932. /*
  933. * Refuse to write the page out if we are called from reclaim context.
  934. *
  935. * This avoids stack overflows when called from deeply used stacks in
  936. * random callers for direct reclaim or memcg reclaim. We explicitly
  937. * allow reclaim from kswapd as the stack usage there is relatively low.
  938. *
  939. * This should never happen except in the case of a VM regression so
  940. * warn about it.
  941. */
  942. if (WARN_ON_ONCE((current->flags & (PF_MEMALLOC|PF_KSWAPD)) ==
  943. PF_MEMALLOC))
  944. goto redirty;
  945. /*
  946. * Given that we do not allow direct reclaim to call us, we should
  947. * never be called while in a filesystem transaction.
  948. */
  949. if (WARN_ON_ONCE(current->flags & PF_FSTRANS))
  950. goto redirty;
  951. /*
  952. * Is this page beyond the end of the file?
  953. *
  954. * The page index is less than the end_index, adjust the end_offset
  955. * to the highest offset that this page should represent.
  956. * -----------------------------------------------------
  957. * | file mapping | <EOF> |
  958. * -----------------------------------------------------
  959. * | Page ... | Page N-2 | Page N-1 | Page N | |
  960. * ^--------------------------------^----------|--------
  961. * | desired writeback range | see else |
  962. * ---------------------------------^------------------|
  963. */
  964. offset = i_size_read(inode);
  965. end_index = offset >> PAGE_SHIFT;
  966. if (page->index < end_index)
  967. end_offset = (xfs_off_t)(page->index + 1) << PAGE_SHIFT;
  968. else {
  969. /*
  970. * Check whether the page to write out is beyond or straddles
  971. * i_size or not.
  972. * -------------------------------------------------------
  973. * | file mapping | <EOF> |
  974. * -------------------------------------------------------
  975. * | Page ... | Page N-2 | Page N-1 | Page N | Beyond |
  976. * ^--------------------------------^-----------|---------
  977. * | | Straddles |
  978. * ---------------------------------^-----------|--------|
  979. */
  980. unsigned offset_into_page = offset & (PAGE_SIZE - 1);
  981. /*
  982. * Skip the page if it is fully outside i_size, e.g. due to a
  983. * truncate operation that is in progress. We must redirty the
  984. * page so that reclaim stops reclaiming it. Otherwise
  985. * xfs_vm_releasepage() is called on it and gets confused.
  986. *
  987. * Note that the end_index is unsigned long, it would overflow
  988. * if the given offset is greater than 16TB on 32-bit system
  989. * and if we do check the page is fully outside i_size or not
  990. * via "if (page->index >= end_index + 1)" as "end_index + 1"
  991. * will be evaluated to 0. Hence this page will be redirtied
  992. * and be written out repeatedly which would result in an
  993. * infinite loop, the user program that perform this operation
  994. * will hang. Instead, we can verify this situation by checking
  995. * if the page to write is totally beyond the i_size or if it's
  996. * offset is just equal to the EOF.
  997. */
  998. if (page->index > end_index ||
  999. (page->index == end_index && offset_into_page == 0))
  1000. goto redirty;
  1001. /*
  1002. * The page straddles i_size. It must be zeroed out on each
  1003. * and every writepage invocation because it may be mmapped.
  1004. * "A file is mapped in multiples of the page size. For a file
  1005. * that is not a multiple of the page size, the remaining
  1006. * memory is zeroed when mapped, and writes to that region are
  1007. * not written out to the file."
  1008. */
  1009. zero_user_segment(page, offset_into_page, PAGE_SIZE);
  1010. /* Adjust the end_offset to the end of file */
  1011. end_offset = offset;
  1012. }
  1013. return xfs_writepage_map(wpc, wbc, inode, page, offset, end_offset);
  1014. redirty:
  1015. redirty_page_for_writepage(wbc, page);
  1016. unlock_page(page);
  1017. return 0;
  1018. }
  1019. STATIC int
  1020. xfs_vm_writepage(
  1021. struct page *page,
  1022. struct writeback_control *wbc)
  1023. {
  1024. struct xfs_writepage_ctx wpc = {
  1025. .io_type = XFS_IO_INVALID,
  1026. };
  1027. int ret;
  1028. ret = xfs_do_writepage(page, wbc, &wpc);
  1029. if (wpc.ioend)
  1030. ret = xfs_submit_ioend(wbc, wpc.ioend, ret);
  1031. return ret;
  1032. }
  1033. STATIC int
  1034. xfs_vm_writepages(
  1035. struct address_space *mapping,
  1036. struct writeback_control *wbc)
  1037. {
  1038. struct xfs_writepage_ctx wpc = {
  1039. .io_type = XFS_IO_INVALID,
  1040. };
  1041. int ret;
  1042. xfs_iflags_clear(XFS_I(mapping->host), XFS_ITRUNCATED);
  1043. if (dax_mapping(mapping))
  1044. return dax_writeback_mapping_range(mapping,
  1045. xfs_find_bdev_for_inode(mapping->host), wbc);
  1046. ret = write_cache_pages(mapping, wbc, xfs_do_writepage, &wpc);
  1047. if (wpc.ioend)
  1048. ret = xfs_submit_ioend(wbc, wpc.ioend, ret);
  1049. return ret;
  1050. }
  1051. /*
  1052. * Called to move a page into cleanable state - and from there
  1053. * to be released. The page should already be clean. We always
  1054. * have buffer heads in this call.
  1055. *
  1056. * Returns 1 if the page is ok to release, 0 otherwise.
  1057. */
  1058. STATIC int
  1059. xfs_vm_releasepage(
  1060. struct page *page,
  1061. gfp_t gfp_mask)
  1062. {
  1063. int delalloc, unwritten;
  1064. trace_xfs_releasepage(page->mapping->host, page, 0, 0);
  1065. /*
  1066. * mm accommodates an old ext3 case where clean pages might not have had
  1067. * the dirty bit cleared. Thus, it can send actual dirty pages to
  1068. * ->releasepage() via shrink_active_list(). Conversely,
  1069. * block_invalidatepage() can send pages that are still marked dirty but
  1070. * otherwise have invalidated buffers.
  1071. *
  1072. * We want to release the latter to avoid unnecessary buildup of the
  1073. * LRU, so xfs_vm_invalidatepage() clears the page dirty flag on pages
  1074. * that are entirely invalidated and need to be released. Hence the
  1075. * only time we should get dirty pages here is through
  1076. * shrink_active_list() and so we can simply skip those now.
  1077. *
  1078. * warn if we've left any lingering delalloc/unwritten buffers on clean
  1079. * or invalidated pages we are about to release.
  1080. */
  1081. if (PageDirty(page))
  1082. return 0;
  1083. xfs_count_page_state(page, &delalloc, &unwritten);
  1084. if (WARN_ON_ONCE(delalloc))
  1085. return 0;
  1086. if (WARN_ON_ONCE(unwritten))
  1087. return 0;
  1088. return try_to_free_buffers(page);
  1089. }
  1090. /*
  1091. * When we map a DIO buffer, we may need to pass flags to
  1092. * xfs_end_io_direct_write to tell it what kind of write IO we are doing.
  1093. *
  1094. * Note that for DIO, an IO to the highest supported file block offset (i.e.
  1095. * 2^63 - 1FSB bytes) will result in the offset + count overflowing a signed 64
  1096. * bit variable. Hence if we see this overflow, we have to assume that the IO is
  1097. * extending the file size. We won't know for sure until IO completion is run
  1098. * and the actual max write offset is communicated to the IO completion
  1099. * routine.
  1100. */
  1101. static void
  1102. xfs_map_direct(
  1103. struct inode *inode,
  1104. struct buffer_head *bh_result,
  1105. struct xfs_bmbt_irec *imap,
  1106. xfs_off_t offset,
  1107. bool is_cow)
  1108. {
  1109. uintptr_t *flags = (uintptr_t *)&bh_result->b_private;
  1110. xfs_off_t size = bh_result->b_size;
  1111. trace_xfs_get_blocks_map_direct(XFS_I(inode), offset, size,
  1112. ISUNWRITTEN(imap) ? XFS_IO_UNWRITTEN : is_cow ? XFS_IO_COW :
  1113. XFS_IO_OVERWRITE, imap);
  1114. if (ISUNWRITTEN(imap)) {
  1115. *flags |= XFS_DIO_FLAG_UNWRITTEN;
  1116. set_buffer_defer_completion(bh_result);
  1117. } else if (is_cow) {
  1118. *flags |= XFS_DIO_FLAG_COW;
  1119. set_buffer_defer_completion(bh_result);
  1120. }
  1121. if (offset + size > i_size_read(inode) || offset + size < 0) {
  1122. *flags |= XFS_DIO_FLAG_APPEND;
  1123. set_buffer_defer_completion(bh_result);
  1124. }
  1125. }
  1126. /*
  1127. * If this is O_DIRECT or the mpage code calling tell them how large the mapping
  1128. * is, so that we can avoid repeated get_blocks calls.
  1129. *
  1130. * If the mapping spans EOF, then we have to break the mapping up as the mapping
  1131. * for blocks beyond EOF must be marked new so that sub block regions can be
  1132. * correctly zeroed. We can't do this for mappings within EOF unless the mapping
  1133. * was just allocated or is unwritten, otherwise the callers would overwrite
  1134. * existing data with zeros. Hence we have to split the mapping into a range up
  1135. * to and including EOF, and a second mapping for beyond EOF.
  1136. */
  1137. static void
  1138. xfs_map_trim_size(
  1139. struct inode *inode,
  1140. sector_t iblock,
  1141. struct buffer_head *bh_result,
  1142. struct xfs_bmbt_irec *imap,
  1143. xfs_off_t offset,
  1144. ssize_t size)
  1145. {
  1146. xfs_off_t mapping_size;
  1147. mapping_size = imap->br_startoff + imap->br_blockcount - iblock;
  1148. mapping_size <<= inode->i_blkbits;
  1149. ASSERT(mapping_size > 0);
  1150. if (mapping_size > size)
  1151. mapping_size = size;
  1152. if (offset < i_size_read(inode) &&
  1153. (xfs_ufsize_t)offset + mapping_size >= i_size_read(inode)) {
  1154. /* limit mapping to block that spans EOF */
  1155. mapping_size = roundup_64(i_size_read(inode) - offset,
  1156. i_blocksize(inode));
  1157. }
  1158. if (mapping_size > LONG_MAX)
  1159. mapping_size = LONG_MAX;
  1160. bh_result->b_size = mapping_size;
  1161. }
  1162. STATIC int
  1163. __xfs_get_blocks(
  1164. struct inode *inode,
  1165. sector_t iblock,
  1166. struct buffer_head *bh_result,
  1167. int create,
  1168. bool direct,
  1169. bool dax_fault)
  1170. {
  1171. struct xfs_inode *ip = XFS_I(inode);
  1172. struct xfs_mount *mp = ip->i_mount;
  1173. xfs_fileoff_t offset_fsb, end_fsb;
  1174. int error = 0;
  1175. int lockmode = 0;
  1176. struct xfs_bmbt_irec imap;
  1177. int nimaps = 1;
  1178. xfs_off_t offset;
  1179. ssize_t size;
  1180. int new = 0;
  1181. bool is_cow = false;
  1182. bool need_alloc = false;
  1183. BUG_ON(create && !direct);
  1184. if (XFS_FORCED_SHUTDOWN(mp))
  1185. return -EIO;
  1186. offset = (xfs_off_t)iblock << inode->i_blkbits;
  1187. ASSERT(bh_result->b_size >= i_blocksize(inode));
  1188. size = bh_result->b_size;
  1189. if (!create && offset >= i_size_read(inode))
  1190. return 0;
  1191. /*
  1192. * Direct I/O is usually done on preallocated files, so try getting
  1193. * a block mapping without an exclusive lock first.
  1194. */
  1195. lockmode = xfs_ilock_data_map_shared(ip);
  1196. ASSERT(offset <= mp->m_super->s_maxbytes);
  1197. if ((xfs_ufsize_t)offset + size > mp->m_super->s_maxbytes)
  1198. size = mp->m_super->s_maxbytes - offset;
  1199. end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + size);
  1200. offset_fsb = XFS_B_TO_FSBT(mp, offset);
  1201. if (create && direct && xfs_is_reflink_inode(ip))
  1202. is_cow = xfs_reflink_find_cow_mapping(ip, offset, &imap,
  1203. &need_alloc);
  1204. if (!is_cow) {
  1205. error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb,
  1206. &imap, &nimaps, XFS_BMAPI_ENTIRE);
  1207. /*
  1208. * Truncate an overwrite extent if there's a pending CoW
  1209. * reservation before the end of this extent. This
  1210. * forces us to come back to get_blocks to take care of
  1211. * the CoW.
  1212. */
  1213. if (create && direct && nimaps &&
  1214. imap.br_startblock != HOLESTARTBLOCK &&
  1215. imap.br_startblock != DELAYSTARTBLOCK &&
  1216. !ISUNWRITTEN(&imap))
  1217. xfs_reflink_trim_irec_to_next_cow(ip, offset_fsb,
  1218. &imap);
  1219. }
  1220. ASSERT(!need_alloc);
  1221. if (error)
  1222. goto out_unlock;
  1223. /*
  1224. * The only time we can ever safely find delalloc blocks on direct I/O
  1225. * is a dio write to post-eof speculative preallocation. All other
  1226. * scenarios are indicative of a problem or misuse (such as mixing
  1227. * direct and mapped I/O).
  1228. *
  1229. * The file may be unmapped by the time we get here so we cannot
  1230. * reliably fail the I/O based on mapping. Instead, fail the I/O if this
  1231. * is a read or a write within eof. Otherwise, carry on but warn as a
  1232. * precuation if the file happens to be mapped.
  1233. */
  1234. if (direct && imap.br_startblock == DELAYSTARTBLOCK) {
  1235. if (!create || offset < i_size_read(VFS_I(ip))) {
  1236. WARN_ON_ONCE(1);
  1237. error = -EIO;
  1238. goto out_unlock;
  1239. }
  1240. WARN_ON_ONCE(mapping_mapped(VFS_I(ip)->i_mapping));
  1241. }
  1242. /* for DAX, we convert unwritten extents directly */
  1243. if (create &&
  1244. (!nimaps ||
  1245. (imap.br_startblock == HOLESTARTBLOCK ||
  1246. imap.br_startblock == DELAYSTARTBLOCK) ||
  1247. (IS_DAX(inode) && ISUNWRITTEN(&imap)))) {
  1248. /*
  1249. * xfs_iomap_write_direct() expects the shared lock. It
  1250. * is unlocked on return.
  1251. */
  1252. if (lockmode == XFS_ILOCK_EXCL)
  1253. xfs_ilock_demote(ip, lockmode);
  1254. error = xfs_iomap_write_direct(ip, offset, size,
  1255. &imap, nimaps);
  1256. if (error)
  1257. return error;
  1258. new = 1;
  1259. trace_xfs_get_blocks_alloc(ip, offset, size,
  1260. ISUNWRITTEN(&imap) ? XFS_IO_UNWRITTEN
  1261. : XFS_IO_DELALLOC, &imap);
  1262. } else if (nimaps) {
  1263. trace_xfs_get_blocks_found(ip, offset, size,
  1264. ISUNWRITTEN(&imap) ? XFS_IO_UNWRITTEN
  1265. : XFS_IO_OVERWRITE, &imap);
  1266. xfs_iunlock(ip, lockmode);
  1267. } else {
  1268. trace_xfs_get_blocks_notfound(ip, offset, size);
  1269. goto out_unlock;
  1270. }
  1271. if (IS_DAX(inode) && create) {
  1272. ASSERT(!ISUNWRITTEN(&imap));
  1273. /* zeroing is not needed at a higher layer */
  1274. new = 0;
  1275. }
  1276. /* trim mapping down to size requested */
  1277. xfs_map_trim_size(inode, iblock, bh_result, &imap, offset, size);
  1278. /*
  1279. * For unwritten extents do not report a disk address in the buffered
  1280. * read case (treat as if we're reading into a hole).
  1281. */
  1282. if (imap.br_startblock != HOLESTARTBLOCK &&
  1283. imap.br_startblock != DELAYSTARTBLOCK &&
  1284. (create || !ISUNWRITTEN(&imap))) {
  1285. xfs_map_buffer(inode, bh_result, &imap, offset);
  1286. if (ISUNWRITTEN(&imap))
  1287. set_buffer_unwritten(bh_result);
  1288. /* direct IO needs special help */
  1289. if (create) {
  1290. if (dax_fault)
  1291. ASSERT(!ISUNWRITTEN(&imap));
  1292. else
  1293. xfs_map_direct(inode, bh_result, &imap, offset,
  1294. is_cow);
  1295. }
  1296. }
  1297. /*
  1298. * If this is a realtime file, data may be on a different device.
  1299. * to that pointed to from the buffer_head b_bdev currently.
  1300. */
  1301. bh_result->b_bdev = xfs_find_bdev_for_inode(inode);
  1302. /*
  1303. * If we previously allocated a block out beyond eof and we are now
  1304. * coming back to use it then we will need to flag it as new even if it
  1305. * has a disk address.
  1306. *
  1307. * With sub-block writes into unwritten extents we also need to mark
  1308. * the buffer as new so that the unwritten parts of the buffer gets
  1309. * correctly zeroed.
  1310. */
  1311. if (create &&
  1312. ((!buffer_mapped(bh_result) && !buffer_uptodate(bh_result)) ||
  1313. (offset >= i_size_read(inode)) ||
  1314. (new || ISUNWRITTEN(&imap))))
  1315. set_buffer_new(bh_result);
  1316. return 0;
  1317. out_unlock:
  1318. xfs_iunlock(ip, lockmode);
  1319. return error;
  1320. }
  1321. int
  1322. xfs_get_blocks(
  1323. struct inode *inode,
  1324. sector_t iblock,
  1325. struct buffer_head *bh_result,
  1326. int create)
  1327. {
  1328. return __xfs_get_blocks(inode, iblock, bh_result, create, false, false);
  1329. }
  1330. int
  1331. xfs_get_blocks_direct(
  1332. struct inode *inode,
  1333. sector_t iblock,
  1334. struct buffer_head *bh_result,
  1335. int create)
  1336. {
  1337. return __xfs_get_blocks(inode, iblock, bh_result, create, true, false);
  1338. }
  1339. int
  1340. xfs_get_blocks_dax_fault(
  1341. struct inode *inode,
  1342. sector_t iblock,
  1343. struct buffer_head *bh_result,
  1344. int create)
  1345. {
  1346. return __xfs_get_blocks(inode, iblock, bh_result, create, true, true);
  1347. }
  1348. /*
  1349. * Complete a direct I/O write request.
  1350. *
  1351. * xfs_map_direct passes us some flags in the private data to tell us what to
  1352. * do. If no flags are set, then the write IO is an overwrite wholly within
  1353. * the existing allocated file size and so there is nothing for us to do.
  1354. *
  1355. * Note that in this case the completion can be called in interrupt context,
  1356. * whereas if we have flags set we will always be called in task context
  1357. * (i.e. from a workqueue).
  1358. */
  1359. int
  1360. xfs_end_io_direct_write(
  1361. struct kiocb *iocb,
  1362. loff_t offset,
  1363. ssize_t size,
  1364. void *private)
  1365. {
  1366. struct inode *inode = file_inode(iocb->ki_filp);
  1367. struct xfs_inode *ip = XFS_I(inode);
  1368. uintptr_t flags = (uintptr_t)private;
  1369. int error = 0;
  1370. trace_xfs_end_io_direct_write(ip, offset, size);
  1371. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  1372. return -EIO;
  1373. if (size <= 0)
  1374. return size;
  1375. /*
  1376. * The flags tell us whether we are doing unwritten extent conversions
  1377. * or an append transaction that updates the on-disk file size. These
  1378. * cases are the only cases where we should *potentially* be needing
  1379. * to update the VFS inode size.
  1380. */
  1381. if (flags == 0) {
  1382. ASSERT(offset + size <= i_size_read(inode));
  1383. return 0;
  1384. }
  1385. if (flags & XFS_DIO_FLAG_COW)
  1386. error = xfs_reflink_end_cow(ip, offset, size);
  1387. /*
  1388. * Unwritten conversion updates the in-core isize after extent
  1389. * conversion but before updating the on-disk size. Updating isize any
  1390. * earlier allows a racing dio read to find unwritten extents before
  1391. * they are converted.
  1392. */
  1393. if (flags & XFS_DIO_FLAG_UNWRITTEN) {
  1394. trace_xfs_end_io_direct_write_unwritten(ip, offset, size);
  1395. return xfs_iomap_write_unwritten(ip, offset, size, true);
  1396. }
  1397. /*
  1398. * We need to update the in-core inode size here so that we don't end up
  1399. * with the on-disk inode size being outside the in-core inode size. We
  1400. * have no other method of updating EOF for AIO, so always do it here
  1401. * if necessary.
  1402. *
  1403. * We need to lock the test/set EOF update as we can be racing with
  1404. * other IO completions here to update the EOF. Failing to serialise
  1405. * here can result in EOF moving backwards and Bad Things Happen when
  1406. * that occurs.
  1407. */
  1408. spin_lock(&ip->i_flags_lock);
  1409. if (offset + size > i_size_read(inode))
  1410. i_size_write(inode, offset + size);
  1411. spin_unlock(&ip->i_flags_lock);
  1412. if (flags & XFS_DIO_FLAG_APPEND) {
  1413. trace_xfs_end_io_direct_write_append(ip, offset, size);
  1414. error = xfs_setfilesize(ip, offset, size);
  1415. }
  1416. return error;
  1417. }
  1418. STATIC ssize_t
  1419. xfs_vm_direct_IO(
  1420. struct kiocb *iocb,
  1421. struct iov_iter *iter)
  1422. {
  1423. /*
  1424. * We just need the method present so that open/fcntl allow direct I/O.
  1425. */
  1426. return -EINVAL;
  1427. }
  1428. STATIC sector_t
  1429. xfs_vm_bmap(
  1430. struct address_space *mapping,
  1431. sector_t block)
  1432. {
  1433. struct inode *inode = (struct inode *)mapping->host;
  1434. struct xfs_inode *ip = XFS_I(inode);
  1435. trace_xfs_vm_bmap(XFS_I(inode));
  1436. xfs_ilock(ip, XFS_IOLOCK_SHARED);
  1437. /*
  1438. * The swap code (ab-)uses ->bmap to get a block mapping and then
  1439. * bypasseѕ the file system for actual I/O. We really can't allow
  1440. * that on reflinks inodes, so we have to skip out here. And yes,
  1441. * 0 is the magic code for a bmap error.
  1442. *
  1443. * Since we don't pass back blockdev info, we can't return bmap
  1444. * information for rt files either.
  1445. */
  1446. if (xfs_is_reflink_inode(ip) || XFS_IS_REALTIME_INODE(ip)) {
  1447. xfs_iunlock(ip, XFS_IOLOCK_SHARED);
  1448. return 0;
  1449. }
  1450. filemap_write_and_wait(mapping);
  1451. xfs_iunlock(ip, XFS_IOLOCK_SHARED);
  1452. return generic_block_bmap(mapping, block, xfs_get_blocks);
  1453. }
  1454. STATIC int
  1455. xfs_vm_readpage(
  1456. struct file *unused,
  1457. struct page *page)
  1458. {
  1459. trace_xfs_vm_readpage(page->mapping->host, 1);
  1460. return mpage_readpage(page, xfs_get_blocks);
  1461. }
  1462. STATIC int
  1463. xfs_vm_readpages(
  1464. struct file *unused,
  1465. struct address_space *mapping,
  1466. struct list_head *pages,
  1467. unsigned nr_pages)
  1468. {
  1469. trace_xfs_vm_readpages(mapping->host, nr_pages);
  1470. return mpage_readpages(mapping, pages, nr_pages, xfs_get_blocks);
  1471. }
  1472. /*
  1473. * This is basically a copy of __set_page_dirty_buffers() with one
  1474. * small tweak: buffers beyond EOF do not get marked dirty. If we mark them
  1475. * dirty, we'll never be able to clean them because we don't write buffers
  1476. * beyond EOF, and that means we can't invalidate pages that span EOF
  1477. * that have been marked dirty. Further, the dirty state can leak into
  1478. * the file interior if the file is extended, resulting in all sorts of
  1479. * bad things happening as the state does not match the underlying data.
  1480. *
  1481. * XXX: this really indicates that bufferheads in XFS need to die. Warts like
  1482. * this only exist because of bufferheads and how the generic code manages them.
  1483. */
  1484. STATIC int
  1485. xfs_vm_set_page_dirty(
  1486. struct page *page)
  1487. {
  1488. struct address_space *mapping = page->mapping;
  1489. struct inode *inode = mapping->host;
  1490. loff_t end_offset;
  1491. loff_t offset;
  1492. int newly_dirty;
  1493. if (unlikely(!mapping))
  1494. return !TestSetPageDirty(page);
  1495. end_offset = i_size_read(inode);
  1496. offset = page_offset(page);
  1497. spin_lock(&mapping->private_lock);
  1498. if (page_has_buffers(page)) {
  1499. struct buffer_head *head = page_buffers(page);
  1500. struct buffer_head *bh = head;
  1501. do {
  1502. if (offset < end_offset)
  1503. set_buffer_dirty(bh);
  1504. bh = bh->b_this_page;
  1505. offset += i_blocksize(inode);
  1506. } while (bh != head);
  1507. }
  1508. /*
  1509. * Lock out page->mem_cgroup migration to keep PageDirty
  1510. * synchronized with per-memcg dirty page counters.
  1511. */
  1512. lock_page_memcg(page);
  1513. newly_dirty = !TestSetPageDirty(page);
  1514. spin_unlock(&mapping->private_lock);
  1515. if (newly_dirty) {
  1516. /* sigh - __set_page_dirty() is static, so copy it here, too */
  1517. unsigned long flags;
  1518. spin_lock_irqsave(&mapping->tree_lock, flags);
  1519. if (page->mapping) { /* Race with truncate? */
  1520. WARN_ON_ONCE(!PageUptodate(page));
  1521. account_page_dirtied(page, mapping);
  1522. radix_tree_tag_set(&mapping->page_tree,
  1523. page_index(page), PAGECACHE_TAG_DIRTY);
  1524. }
  1525. spin_unlock_irqrestore(&mapping->tree_lock, flags);
  1526. }
  1527. unlock_page_memcg(page);
  1528. if (newly_dirty)
  1529. __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
  1530. return newly_dirty;
  1531. }
  1532. const struct address_space_operations xfs_address_space_operations = {
  1533. .readpage = xfs_vm_readpage,
  1534. .readpages = xfs_vm_readpages,
  1535. .writepage = xfs_vm_writepage,
  1536. .writepages = xfs_vm_writepages,
  1537. .set_page_dirty = xfs_vm_set_page_dirty,
  1538. .releasepage = xfs_vm_releasepage,
  1539. .invalidatepage = xfs_vm_invalidatepage,
  1540. .bmap = xfs_vm_bmap,
  1541. .direct_IO = xfs_vm_direct_IO,
  1542. .migratepage = buffer_migrate_page,
  1543. .is_partially_uptodate = block_is_partially_uptodate,
  1544. .error_remove_page = generic_error_remove_page,
  1545. };