xfs_aops.c 41 KB

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