xfs_aops.c 42 KB

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