xfs_aops.c 39 KB

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