xfs_aops.c 49 KB

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