xfs_aops.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2000-2005 Silicon Graphics, Inc.
  4. * Copyright (c) 2016-2018 Christoph Hellwig.
  5. * All Rights Reserved.
  6. */
  7. #include "xfs.h"
  8. #include "xfs_shared.h"
  9. #include "xfs_format.h"
  10. #include "xfs_log_format.h"
  11. #include "xfs_trans_resv.h"
  12. #include "xfs_mount.h"
  13. #include "xfs_inode.h"
  14. #include "xfs_trans.h"
  15. #include "xfs_iomap.h"
  16. #include "xfs_trace.h"
  17. #include "xfs_bmap.h"
  18. #include "xfs_bmap_util.h"
  19. #include "xfs_reflink.h"
  20. struct xfs_writepage_ctx {
  21. struct iomap_writepage_ctx ctx;
  22. unsigned int data_seq;
  23. unsigned int cow_seq;
  24. };
  25. static inline struct xfs_writepage_ctx *
  26. XFS_WPC(struct iomap_writepage_ctx *ctx)
  27. {
  28. return container_of(ctx, struct xfs_writepage_ctx, ctx);
  29. }
  30. /*
  31. * Fast and loose check if this write could update the on-disk inode size.
  32. */
  33. static inline bool xfs_ioend_is_append(struct iomap_ioend *ioend)
  34. {
  35. return ioend->io_offset + ioend->io_size >
  36. XFS_I(ioend->io_inode)->i_d.di_size;
  37. }
  38. STATIC int
  39. xfs_setfilesize_trans_alloc(
  40. struct iomap_ioend *ioend)
  41. {
  42. struct xfs_mount *mp = XFS_I(ioend->io_inode)->i_mount;
  43. struct xfs_trans *tp;
  44. int error;
  45. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_fsyncts, 0, 0, 0, &tp);
  46. if (error)
  47. return error;
  48. ioend->io_private = tp;
  49. /*
  50. * We may pass freeze protection with a transaction. So tell lockdep
  51. * we released it.
  52. */
  53. __sb_writers_release(ioend->io_inode->i_sb, SB_FREEZE_FS);
  54. /*
  55. * We hand off the transaction to the completion thread now, so
  56. * clear the flag here.
  57. */
  58. current_restore_flags_nested(&tp->t_pflags, PF_MEMALLOC_NOFS);
  59. return 0;
  60. }
  61. /*
  62. * Update on-disk file size now that data has been written to disk.
  63. */
  64. STATIC int
  65. __xfs_setfilesize(
  66. struct xfs_inode *ip,
  67. struct xfs_trans *tp,
  68. xfs_off_t offset,
  69. size_t size)
  70. {
  71. xfs_fsize_t isize;
  72. xfs_ilock(ip, XFS_ILOCK_EXCL);
  73. isize = xfs_new_eof(ip, offset + size);
  74. if (!isize) {
  75. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  76. xfs_trans_cancel(tp);
  77. return 0;
  78. }
  79. trace_xfs_setfilesize(ip, offset, size);
  80. ip->i_d.di_size = isize;
  81. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  82. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  83. return xfs_trans_commit(tp);
  84. }
  85. int
  86. xfs_setfilesize(
  87. struct xfs_inode *ip,
  88. xfs_off_t offset,
  89. size_t size)
  90. {
  91. struct xfs_mount *mp = ip->i_mount;
  92. struct xfs_trans *tp;
  93. int error;
  94. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_fsyncts, 0, 0, 0, &tp);
  95. if (error)
  96. return error;
  97. return __xfs_setfilesize(ip, tp, offset, size);
  98. }
  99. STATIC int
  100. xfs_setfilesize_ioend(
  101. struct iomap_ioend *ioend,
  102. int error)
  103. {
  104. struct xfs_inode *ip = XFS_I(ioend->io_inode);
  105. struct xfs_trans *tp = ioend->io_private;
  106. /*
  107. * The transaction may have been allocated in the I/O submission thread,
  108. * thus we need to mark ourselves as being in a transaction manually.
  109. * Similarly for freeze protection.
  110. */
  111. current_set_flags_nested(&tp->t_pflags, PF_MEMALLOC_NOFS);
  112. __sb_writers_acquired(VFS_I(ip)->i_sb, SB_FREEZE_FS);
  113. /* we abort the update if there was an IO error */
  114. if (error) {
  115. xfs_trans_cancel(tp);
  116. return error;
  117. }
  118. return __xfs_setfilesize(ip, tp, ioend->io_offset, ioend->io_size);
  119. }
  120. /*
  121. * IO write completion.
  122. */
  123. STATIC void
  124. xfs_end_ioend(
  125. struct iomap_ioend *ioend)
  126. {
  127. struct xfs_inode *ip = XFS_I(ioend->io_inode);
  128. xfs_off_t offset = ioend->io_offset;
  129. size_t size = ioend->io_size;
  130. unsigned int nofs_flag;
  131. int error;
  132. /*
  133. * We can allocate memory here while doing writeback on behalf of
  134. * memory reclaim. To avoid memory allocation deadlocks set the
  135. * task-wide nofs context for the following operations.
  136. */
  137. nofs_flag = memalloc_nofs_save();
  138. /*
  139. * Just clean up the in-memory strutures if the fs has been shut down.
  140. */
  141. if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
  142. error = -EIO;
  143. goto done;
  144. }
  145. /*
  146. * Clean up any COW blocks on an I/O error.
  147. */
  148. error = blk_status_to_errno(ioend->io_bio->bi_status);
  149. if (unlikely(error)) {
  150. if (ioend->io_flags & IOMAP_F_SHARED)
  151. xfs_reflink_cancel_cow_range(ip, offset, size, true);
  152. goto done;
  153. }
  154. /*
  155. * Success: commit the COW or unwritten blocks if needed.
  156. */
  157. if (ioend->io_flags & IOMAP_F_SHARED)
  158. error = xfs_reflink_end_cow(ip, offset, size);
  159. else if (ioend->io_type == IOMAP_UNWRITTEN)
  160. error = xfs_iomap_write_unwritten(ip, offset, size, false);
  161. else
  162. ASSERT(!xfs_ioend_is_append(ioend) || ioend->io_private);
  163. done:
  164. if (ioend->io_private)
  165. error = xfs_setfilesize_ioend(ioend, error);
  166. iomap_finish_ioends(ioend, error);
  167. memalloc_nofs_restore(nofs_flag);
  168. }
  169. /*
  170. * If the to be merged ioend has a preallocated transaction for file
  171. * size updates we need to ensure the ioend it is merged into also
  172. * has one. If it already has one we can simply cancel the transaction
  173. * as it is guaranteed to be clean.
  174. */
  175. static void
  176. xfs_ioend_merge_private(
  177. struct iomap_ioend *ioend,
  178. struct iomap_ioend *next)
  179. {
  180. if (!ioend->io_private) {
  181. ioend->io_private = next->io_private;
  182. next->io_private = NULL;
  183. } else {
  184. xfs_setfilesize_ioend(next, -ECANCELED);
  185. }
  186. }
  187. /* Finish all pending io completions. */
  188. void
  189. xfs_end_io(
  190. struct work_struct *work)
  191. {
  192. struct xfs_inode *ip =
  193. container_of(work, struct xfs_inode, i_ioend_work);
  194. struct iomap_ioend *ioend;
  195. struct list_head tmp;
  196. unsigned long flags;
  197. spin_lock_irqsave(&ip->i_ioend_lock, flags);
  198. list_replace_init(&ip->i_ioend_list, &tmp);
  199. spin_unlock_irqrestore(&ip->i_ioend_lock, flags);
  200. iomap_sort_ioends(&tmp);
  201. while ((ioend = list_first_entry_or_null(&tmp, struct iomap_ioend,
  202. io_list))) {
  203. list_del_init(&ioend->io_list);
  204. iomap_ioend_try_merge(ioend, &tmp, xfs_ioend_merge_private);
  205. xfs_end_ioend(ioend);
  206. }
  207. }
  208. static inline bool xfs_ioend_needs_workqueue(struct iomap_ioend *ioend)
  209. {
  210. return ioend->io_private ||
  211. ioend->io_type == IOMAP_UNWRITTEN ||
  212. (ioend->io_flags & IOMAP_F_SHARED);
  213. }
  214. STATIC void
  215. xfs_end_bio(
  216. struct bio *bio)
  217. {
  218. struct iomap_ioend *ioend = bio->bi_private;
  219. struct xfs_inode *ip = XFS_I(ioend->io_inode);
  220. unsigned long flags;
  221. ASSERT(xfs_ioend_needs_workqueue(ioend));
  222. spin_lock_irqsave(&ip->i_ioend_lock, flags);
  223. if (list_empty(&ip->i_ioend_list))
  224. WARN_ON_ONCE(!queue_work(ip->i_mount->m_unwritten_workqueue,
  225. &ip->i_ioend_work));
  226. list_add_tail(&ioend->io_list, &ip->i_ioend_list);
  227. spin_unlock_irqrestore(&ip->i_ioend_lock, flags);
  228. }
  229. /*
  230. * Fast revalidation of the cached writeback mapping. Return true if the current
  231. * mapping is valid, false otherwise.
  232. */
  233. static bool
  234. xfs_imap_valid(
  235. struct iomap_writepage_ctx *wpc,
  236. struct xfs_inode *ip,
  237. loff_t offset)
  238. {
  239. if (offset < wpc->iomap.offset ||
  240. offset >= wpc->iomap.offset + wpc->iomap.length)
  241. return false;
  242. /*
  243. * If this is a COW mapping, it is sufficient to check that the mapping
  244. * covers the offset. Be careful to check this first because the caller
  245. * can revalidate a COW mapping without updating the data seqno.
  246. */
  247. if (wpc->iomap.flags & IOMAP_F_SHARED)
  248. return true;
  249. /*
  250. * This is not a COW mapping. Check the sequence number of the data fork
  251. * because concurrent changes could have invalidated the extent. Check
  252. * the COW fork because concurrent changes since the last time we
  253. * checked (and found nothing at this offset) could have added
  254. * overlapping blocks.
  255. */
  256. if (XFS_WPC(wpc)->data_seq != READ_ONCE(ip->i_df.if_seq))
  257. return false;
  258. if (xfs_inode_has_cow_data(ip) &&
  259. XFS_WPC(wpc)->cow_seq != READ_ONCE(ip->i_cowfp->if_seq))
  260. return false;
  261. return true;
  262. }
  263. /*
  264. * Pass in a dellalloc extent and convert it to real extents, return the real
  265. * extent that maps offset_fsb in wpc->iomap.
  266. *
  267. * The current page is held locked so nothing could have removed the block
  268. * backing offset_fsb, although it could have moved from the COW to the data
  269. * fork by another thread.
  270. */
  271. static int
  272. xfs_convert_blocks(
  273. struct iomap_writepage_ctx *wpc,
  274. struct xfs_inode *ip,
  275. int whichfork,
  276. loff_t offset)
  277. {
  278. int error;
  279. unsigned *seq;
  280. if (whichfork == XFS_COW_FORK)
  281. seq = &XFS_WPC(wpc)->cow_seq;
  282. else
  283. seq = &XFS_WPC(wpc)->data_seq;
  284. /*
  285. * Attempt to allocate whatever delalloc extent currently backs offset
  286. * and put the result into wpc->iomap. Allocate in a loop because it
  287. * may take several attempts to allocate real blocks for a contiguous
  288. * delalloc extent if free space is sufficiently fragmented.
  289. */
  290. do {
  291. error = xfs_bmapi_convert_delalloc(ip, whichfork, offset,
  292. &wpc->iomap, seq);
  293. if (error)
  294. return error;
  295. } while (wpc->iomap.offset + wpc->iomap.length <= offset);
  296. return 0;
  297. }
  298. static int
  299. xfs_map_blocks(
  300. struct iomap_writepage_ctx *wpc,
  301. struct inode *inode,
  302. loff_t offset)
  303. {
  304. struct xfs_inode *ip = XFS_I(inode);
  305. struct xfs_mount *mp = ip->i_mount;
  306. ssize_t count = i_blocksize(inode);
  307. xfs_fileoff_t offset_fsb = XFS_B_TO_FSBT(mp, offset);
  308. xfs_fileoff_t end_fsb = XFS_B_TO_FSB(mp, offset + count);
  309. xfs_fileoff_t cow_fsb;
  310. int whichfork;
  311. struct xfs_bmbt_irec imap;
  312. struct xfs_iext_cursor icur;
  313. int retries = 0;
  314. int error = 0;
  315. if (XFS_FORCED_SHUTDOWN(mp))
  316. return -EIO;
  317. /*
  318. * COW fork blocks can overlap data fork blocks even if the blocks
  319. * aren't shared. COW I/O always takes precedent, so we must always
  320. * check for overlap on reflink inodes unless the mapping is already a
  321. * COW one, or the COW fork hasn't changed from the last time we looked
  322. * at it.
  323. *
  324. * It's safe to check the COW fork if_seq here without the ILOCK because
  325. * we've indirectly protected against concurrent updates: writeback has
  326. * the page locked, which prevents concurrent invalidations by reflink
  327. * and directio and prevents concurrent buffered writes to the same
  328. * page. Changes to if_seq always happen under i_lock, which protects
  329. * against concurrent updates and provides a memory barrier on the way
  330. * out that ensures that we always see the current value.
  331. */
  332. if (xfs_imap_valid(wpc, ip, offset))
  333. return 0;
  334. /*
  335. * If we don't have a valid map, now it's time to get a new one for this
  336. * offset. This will convert delayed allocations (including COW ones)
  337. * into real extents. If we return without a valid map, it means we
  338. * landed in a hole and we skip the block.
  339. */
  340. retry:
  341. cow_fsb = NULLFILEOFF;
  342. whichfork = XFS_DATA_FORK;
  343. xfs_ilock(ip, XFS_ILOCK_SHARED);
  344. ASSERT(ip->i_df.if_format != XFS_DINODE_FMT_BTREE ||
  345. (ip->i_df.if_flags & XFS_IFEXTENTS));
  346. /*
  347. * Check if this is offset is covered by a COW extents, and if yes use
  348. * it directly instead of looking up anything in the data fork.
  349. */
  350. if (xfs_inode_has_cow_data(ip) &&
  351. xfs_iext_lookup_extent(ip, ip->i_cowfp, offset_fsb, &icur, &imap))
  352. cow_fsb = imap.br_startoff;
  353. if (cow_fsb != NULLFILEOFF && cow_fsb <= offset_fsb) {
  354. XFS_WPC(wpc)->cow_seq = READ_ONCE(ip->i_cowfp->if_seq);
  355. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  356. whichfork = XFS_COW_FORK;
  357. goto allocate_blocks;
  358. }
  359. /*
  360. * No COW extent overlap. Revalidate now that we may have updated
  361. * ->cow_seq. If the data mapping is still valid, we're done.
  362. */
  363. if (xfs_imap_valid(wpc, ip, offset)) {
  364. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  365. return 0;
  366. }
  367. /*
  368. * If we don't have a valid map, now it's time to get a new one for this
  369. * offset. This will convert delayed allocations (including COW ones)
  370. * into real extents.
  371. */
  372. if (!xfs_iext_lookup_extent(ip, &ip->i_df, offset_fsb, &icur, &imap))
  373. imap.br_startoff = end_fsb; /* fake a hole past EOF */
  374. XFS_WPC(wpc)->data_seq = READ_ONCE(ip->i_df.if_seq);
  375. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  376. /* landed in a hole or beyond EOF? */
  377. if (imap.br_startoff > offset_fsb) {
  378. imap.br_blockcount = imap.br_startoff - offset_fsb;
  379. imap.br_startoff = offset_fsb;
  380. imap.br_startblock = HOLESTARTBLOCK;
  381. imap.br_state = XFS_EXT_NORM;
  382. }
  383. /*
  384. * Truncate to the next COW extent if there is one. This is the only
  385. * opportunity to do this because we can skip COW fork lookups for the
  386. * subsequent blocks in the mapping; however, the requirement to treat
  387. * the COW range separately remains.
  388. */
  389. if (cow_fsb != NULLFILEOFF &&
  390. cow_fsb < imap.br_startoff + imap.br_blockcount)
  391. imap.br_blockcount = cow_fsb - imap.br_startoff;
  392. /* got a delalloc extent? */
  393. if (imap.br_startblock != HOLESTARTBLOCK &&
  394. isnullstartblock(imap.br_startblock))
  395. goto allocate_blocks;
  396. xfs_bmbt_to_iomap(ip, &wpc->iomap, &imap, 0);
  397. trace_xfs_map_blocks_found(ip, offset, count, whichfork, &imap);
  398. return 0;
  399. allocate_blocks:
  400. error = xfs_convert_blocks(wpc, ip, whichfork, offset);
  401. if (error) {
  402. /*
  403. * If we failed to find the extent in the COW fork we might have
  404. * raced with a COW to data fork conversion or truncate.
  405. * Restart the lookup to catch the extent in the data fork for
  406. * the former case, but prevent additional retries to avoid
  407. * looping forever for the latter case.
  408. */
  409. if (error == -EAGAIN && whichfork == XFS_COW_FORK && !retries++)
  410. goto retry;
  411. ASSERT(error != -EAGAIN);
  412. return error;
  413. }
  414. /*
  415. * Due to merging the return real extent might be larger than the
  416. * original delalloc one. Trim the return extent to the next COW
  417. * boundary again to force a re-lookup.
  418. */
  419. if (whichfork != XFS_COW_FORK && cow_fsb != NULLFILEOFF) {
  420. loff_t cow_offset = XFS_FSB_TO_B(mp, cow_fsb);
  421. if (cow_offset < wpc->iomap.offset + wpc->iomap.length)
  422. wpc->iomap.length = cow_offset - wpc->iomap.offset;
  423. }
  424. ASSERT(wpc->iomap.offset <= offset);
  425. ASSERT(wpc->iomap.offset + wpc->iomap.length > offset);
  426. trace_xfs_map_blocks_alloc(ip, offset, count, whichfork, &imap);
  427. return 0;
  428. }
  429. static int
  430. xfs_prepare_ioend(
  431. struct iomap_ioend *ioend,
  432. int status)
  433. {
  434. unsigned int nofs_flag;
  435. /*
  436. * We can allocate memory here while doing writeback on behalf of
  437. * memory reclaim. To avoid memory allocation deadlocks set the
  438. * task-wide nofs context for the following operations.
  439. */
  440. nofs_flag = memalloc_nofs_save();
  441. /* Convert CoW extents to regular */
  442. if (!status && (ioend->io_flags & IOMAP_F_SHARED)) {
  443. status = xfs_reflink_convert_cow(XFS_I(ioend->io_inode),
  444. ioend->io_offset, ioend->io_size);
  445. }
  446. /* Reserve log space if we might write beyond the on-disk inode size. */
  447. if (!status &&
  448. ((ioend->io_flags & IOMAP_F_SHARED) ||
  449. ioend->io_type != IOMAP_UNWRITTEN) &&
  450. xfs_ioend_is_append(ioend) &&
  451. !ioend->io_private)
  452. status = xfs_setfilesize_trans_alloc(ioend);
  453. memalloc_nofs_restore(nofs_flag);
  454. if (xfs_ioend_needs_workqueue(ioend))
  455. ioend->io_bio->bi_end_io = xfs_end_bio;
  456. return status;
  457. }
  458. /*
  459. * If the page has delalloc blocks on it, we need to punch them out before we
  460. * invalidate the page. If we don't, we leave a stale delalloc mapping on the
  461. * inode that can trip up a later direct I/O read operation on the same region.
  462. *
  463. * We prevent this by truncating away the delalloc regions on the page. Because
  464. * they are delalloc, we can do this without needing a transaction. Indeed - if
  465. * we get ENOSPC errors, we have to be able to do this truncation without a
  466. * transaction as there is no space left for block reservation (typically why we
  467. * see a ENOSPC in writeback).
  468. */
  469. static void
  470. xfs_discard_page(
  471. struct page *page,
  472. loff_t fileoff)
  473. {
  474. struct inode *inode = page->mapping->host;
  475. struct xfs_inode *ip = XFS_I(inode);
  476. struct xfs_mount *mp = ip->i_mount;
  477. unsigned int pageoff = offset_in_page(fileoff);
  478. xfs_fileoff_t start_fsb = XFS_B_TO_FSBT(mp, fileoff);
  479. xfs_fileoff_t pageoff_fsb = XFS_B_TO_FSBT(mp, pageoff);
  480. int error;
  481. if (XFS_FORCED_SHUTDOWN(mp))
  482. goto out_invalidate;
  483. xfs_alert_ratelimited(mp,
  484. "page discard on page "PTR_FMT", inode 0x%llx, offset %llu.",
  485. page, ip->i_ino, fileoff);
  486. error = xfs_bmap_punch_delalloc_range(ip, start_fsb,
  487. i_blocks_per_page(inode, page) - pageoff_fsb);
  488. if (error && !XFS_FORCED_SHUTDOWN(mp))
  489. xfs_alert(mp, "page discard unable to remove delalloc mapping.");
  490. out_invalidate:
  491. iomap_invalidatepage(page, pageoff, PAGE_SIZE - pageoff);
  492. }
  493. static const struct iomap_writeback_ops xfs_writeback_ops = {
  494. .map_blocks = xfs_map_blocks,
  495. .prepare_ioend = xfs_prepare_ioend,
  496. .discard_page = xfs_discard_page,
  497. };
  498. STATIC int
  499. xfs_vm_writepage(
  500. struct page *page,
  501. struct writeback_control *wbc)
  502. {
  503. struct xfs_writepage_ctx wpc = { };
  504. return iomap_writepage(page, wbc, &wpc.ctx, &xfs_writeback_ops);
  505. }
  506. STATIC int
  507. xfs_vm_writepages(
  508. struct address_space *mapping,
  509. struct writeback_control *wbc)
  510. {
  511. struct xfs_writepage_ctx wpc = { };
  512. xfs_iflags_clear(XFS_I(mapping->host), XFS_ITRUNCATED);
  513. return iomap_writepages(mapping, wbc, &wpc.ctx, &xfs_writeback_ops);
  514. }
  515. STATIC int
  516. xfs_dax_writepages(
  517. struct address_space *mapping,
  518. struct writeback_control *wbc)
  519. {
  520. struct xfs_inode *ip = XFS_I(mapping->host);
  521. xfs_iflags_clear(ip, XFS_ITRUNCATED);
  522. return dax_writeback_mapping_range(mapping,
  523. xfs_inode_buftarg(ip)->bt_daxdev, wbc);
  524. }
  525. STATIC sector_t
  526. xfs_vm_bmap(
  527. struct address_space *mapping,
  528. sector_t block)
  529. {
  530. struct xfs_inode *ip = XFS_I(mapping->host);
  531. trace_xfs_vm_bmap(ip);
  532. /*
  533. * The swap code (ab-)uses ->bmap to get a block mapping and then
  534. * bypasses the file system for actual I/O. We really can't allow
  535. * that on reflinks inodes, so we have to skip out here. And yes,
  536. * 0 is the magic code for a bmap error.
  537. *
  538. * Since we don't pass back blockdev info, we can't return bmap
  539. * information for rt files either.
  540. */
  541. if (xfs_is_cow_inode(ip) || XFS_IS_REALTIME_INODE(ip))
  542. return 0;
  543. return iomap_bmap(mapping, block, &xfs_read_iomap_ops);
  544. }
  545. STATIC int
  546. xfs_vm_readpage(
  547. struct file *unused,
  548. struct page *page)
  549. {
  550. return iomap_readpage(page, &xfs_read_iomap_ops);
  551. }
  552. STATIC void
  553. xfs_vm_readahead(
  554. struct readahead_control *rac)
  555. {
  556. iomap_readahead(rac, &xfs_read_iomap_ops);
  557. }
  558. static int
  559. xfs_iomap_swapfile_activate(
  560. struct swap_info_struct *sis,
  561. struct file *swap_file,
  562. sector_t *span)
  563. {
  564. sis->bdev = xfs_inode_buftarg(XFS_I(file_inode(swap_file)))->bt_bdev;
  565. return iomap_swapfile_activate(sis, swap_file, span,
  566. &xfs_read_iomap_ops);
  567. }
  568. const struct address_space_operations xfs_address_space_operations = {
  569. .readpage = xfs_vm_readpage,
  570. .readahead = xfs_vm_readahead,
  571. .writepage = xfs_vm_writepage,
  572. .writepages = xfs_vm_writepages,
  573. .set_page_dirty = iomap_set_page_dirty,
  574. .releasepage = iomap_releasepage,
  575. .invalidatepage = iomap_invalidatepage,
  576. .bmap = xfs_vm_bmap,
  577. .direct_IO = noop_direct_IO,
  578. .migratepage = iomap_migrate_page,
  579. .is_partially_uptodate = iomap_is_partially_uptodate,
  580. .error_remove_page = generic_error_remove_page,
  581. .swap_activate = xfs_iomap_swapfile_activate,
  582. };
  583. const struct address_space_operations xfs_dax_aops = {
  584. .writepages = xfs_dax_writepages,
  585. .direct_IO = noop_direct_IO,
  586. .set_page_dirty = noop_set_page_dirty,
  587. .invalidatepage = noop_invalidatepage,
  588. .swap_activate = xfs_iomap_swapfile_activate,
  589. };