xfs_aops.c 49 KB

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