xfs_aops.c 41 KB

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