xfs_aops.c 54 KB

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