xfs_aops.c 48 KB

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