xfs_aops.c 41 KB

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