xfs_aops.c 48 KB

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