xfs_aops.c 52 KB

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