xfs_aops.c 48 KB

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