xfs_aops.c 49 KB

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