xfs_aops.c 46 KB

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