xfs_aops.c 51 KB

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