xfs_aops.c 44 KB

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