xfs_aops.c 40 KB

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