xfs_aops.c 48 KB

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