xfs_aops.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619
  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_log.h"
  20. #include "xfs_sb.h"
  21. #include "xfs_ag.h"
  22. #include "xfs_trans.h"
  23. #include "xfs_mount.h"
  24. #include "xfs_bmap_btree.h"
  25. #include "xfs_dinode.h"
  26. #include "xfs_inode.h"
  27. #include "xfs_inode_item.h"
  28. #include "xfs_alloc.h"
  29. #include "xfs_error.h"
  30. #include "xfs_iomap.h"
  31. #include "xfs_vnodeops.h"
  32. #include "xfs_trace.h"
  33. #include "xfs_bmap.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. if (ioend->io_iocb) {
  81. if (ioend->io_isasync) {
  82. aio_complete(ioend->io_iocb, ioend->io_error ?
  83. ioend->io_error : ioend->io_result, 0);
  84. }
  85. inode_dio_done(ioend->io_inode);
  86. }
  87. mempool_free(ioend, xfs_ioend_pool);
  88. }
  89. /*
  90. * Fast and loose check if this write could update the on-disk inode size.
  91. */
  92. static inline bool xfs_ioend_is_append(struct xfs_ioend *ioend)
  93. {
  94. return ioend->io_offset + ioend->io_size >
  95. XFS_I(ioend->io_inode)->i_d.di_size;
  96. }
  97. STATIC int
  98. xfs_setfilesize_trans_alloc(
  99. struct xfs_ioend *ioend)
  100. {
  101. struct xfs_mount *mp = XFS_I(ioend->io_inode)->i_mount;
  102. struct xfs_trans *tp;
  103. int error;
  104. tp = xfs_trans_alloc(mp, XFS_TRANS_FSYNC_TS);
  105. error = xfs_trans_reserve(tp, 0, XFS_FSYNC_TS_LOG_RES(mp), 0, 0, 0);
  106. if (error) {
  107. xfs_trans_cancel(tp, 0);
  108. return error;
  109. }
  110. ioend->io_append_trans = tp;
  111. /*
  112. * We hand off the transaction to the completion thread now, so
  113. * clear the flag here.
  114. */
  115. current_restore_flags_nested(&tp->t_pflags, PF_FSTRANS);
  116. return 0;
  117. }
  118. /*
  119. * Update on-disk file size now that data has been written to disk.
  120. */
  121. STATIC int
  122. xfs_setfilesize(
  123. struct xfs_ioend *ioend)
  124. {
  125. struct xfs_inode *ip = XFS_I(ioend->io_inode);
  126. struct xfs_trans *tp = ioend->io_append_trans;
  127. xfs_fsize_t isize;
  128. /*
  129. * The transaction was allocated in the I/O submission thread,
  130. * thus we need to mark ourselves as beeing in a transaction
  131. * manually.
  132. */
  133. current_set_flags_nested(&tp->t_pflags, PF_FSTRANS);
  134. xfs_ilock(ip, XFS_ILOCK_EXCL);
  135. isize = xfs_new_eof(ip, ioend->io_offset + ioend->io_size);
  136. if (!isize) {
  137. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  138. xfs_trans_cancel(tp, 0);
  139. return 0;
  140. }
  141. trace_xfs_setfilesize(ip, ioend->io_offset, ioend->io_size);
  142. ip->i_d.di_size = isize;
  143. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  144. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  145. return xfs_trans_commit(tp, 0);
  146. }
  147. /*
  148. * Schedule IO completion handling on the final put of an ioend.
  149. *
  150. * If there is no work to do we might as well call it a day and free the
  151. * ioend right now.
  152. */
  153. STATIC void
  154. xfs_finish_ioend(
  155. struct xfs_ioend *ioend)
  156. {
  157. if (atomic_dec_and_test(&ioend->io_remaining)) {
  158. struct xfs_mount *mp = XFS_I(ioend->io_inode)->i_mount;
  159. if (ioend->io_type == IO_UNWRITTEN)
  160. queue_work(mp->m_unwritten_workqueue, &ioend->io_work);
  161. else if (ioend->io_append_trans)
  162. queue_work(mp->m_data_workqueue, &ioend->io_work);
  163. else
  164. xfs_destroy_ioend(ioend);
  165. }
  166. }
  167. /*
  168. * IO write completion.
  169. */
  170. STATIC void
  171. xfs_end_io(
  172. struct work_struct *work)
  173. {
  174. xfs_ioend_t *ioend = container_of(work, xfs_ioend_t, io_work);
  175. struct xfs_inode *ip = XFS_I(ioend->io_inode);
  176. int error = 0;
  177. if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
  178. ioend->io_error = -EIO;
  179. goto done;
  180. }
  181. if (ioend->io_error)
  182. goto done;
  183. /*
  184. * For unwritten extents we need to issue transactions to convert a
  185. * range to normal written extens after the data I/O has finished.
  186. */
  187. if (ioend->io_type == IO_UNWRITTEN) {
  188. /*
  189. * For buffered I/O we never preallocate a transaction when
  190. * doing the unwritten extent conversion, but for direct I/O
  191. * we do not know if we are converting an unwritten extent
  192. * or not at the point where we preallocate the transaction.
  193. */
  194. if (ioend->io_append_trans) {
  195. ASSERT(ioend->io_isdirect);
  196. current_set_flags_nested(
  197. &ioend->io_append_trans->t_pflags, PF_FSTRANS);
  198. xfs_trans_cancel(ioend->io_append_trans, 0);
  199. }
  200. error = xfs_iomap_write_unwritten(ip, ioend->io_offset,
  201. ioend->io_size);
  202. if (error) {
  203. ioend->io_error = -error;
  204. goto done;
  205. }
  206. } else if (ioend->io_append_trans) {
  207. error = xfs_setfilesize(ioend);
  208. if (error)
  209. ioend->io_error = -error;
  210. } else {
  211. ASSERT(!xfs_ioend_is_append(ioend));
  212. }
  213. done:
  214. xfs_destroy_ioend(ioend);
  215. }
  216. /*
  217. * Call IO completion handling in caller context on the final put of an ioend.
  218. */
  219. STATIC void
  220. xfs_finish_ioend_sync(
  221. struct xfs_ioend *ioend)
  222. {
  223. if (atomic_dec_and_test(&ioend->io_remaining))
  224. xfs_end_io(&ioend->io_work);
  225. }
  226. /*
  227. * Allocate and initialise an IO completion structure.
  228. * We need to track unwritten extent write completion here initially.
  229. * We'll need to extend this for updating the ondisk inode size later
  230. * (vs. incore size).
  231. */
  232. STATIC xfs_ioend_t *
  233. xfs_alloc_ioend(
  234. struct inode *inode,
  235. unsigned int type)
  236. {
  237. xfs_ioend_t *ioend;
  238. ioend = mempool_alloc(xfs_ioend_pool, GFP_NOFS);
  239. /*
  240. * Set the count to 1 initially, which will prevent an I/O
  241. * completion callback from happening before we have started
  242. * all the I/O from calling the completion routine too early.
  243. */
  244. atomic_set(&ioend->io_remaining, 1);
  245. ioend->io_isasync = 0;
  246. ioend->io_isdirect = 0;
  247. ioend->io_error = 0;
  248. ioend->io_list = NULL;
  249. ioend->io_type = type;
  250. ioend->io_inode = inode;
  251. ioend->io_buffer_head = NULL;
  252. ioend->io_buffer_tail = NULL;
  253. ioend->io_offset = 0;
  254. ioend->io_size = 0;
  255. ioend->io_iocb = NULL;
  256. ioend->io_result = 0;
  257. ioend->io_append_trans = NULL;
  258. INIT_WORK(&ioend->io_work, xfs_end_io);
  259. return ioend;
  260. }
  261. STATIC int
  262. xfs_map_blocks(
  263. struct inode *inode,
  264. loff_t offset,
  265. struct xfs_bmbt_irec *imap,
  266. int type,
  267. int nonblocking)
  268. {
  269. struct xfs_inode *ip = XFS_I(inode);
  270. struct xfs_mount *mp = ip->i_mount;
  271. ssize_t count = 1 << inode->i_blkbits;
  272. xfs_fileoff_t offset_fsb, end_fsb;
  273. int error = 0;
  274. int bmapi_flags = XFS_BMAPI_ENTIRE;
  275. int nimaps = 1;
  276. if (XFS_FORCED_SHUTDOWN(mp))
  277. return -XFS_ERROR(EIO);
  278. if (type == IO_UNWRITTEN)
  279. bmapi_flags |= XFS_BMAPI_IGSTATE;
  280. if (!xfs_ilock_nowait(ip, XFS_ILOCK_SHARED)) {
  281. if (nonblocking)
  282. return -XFS_ERROR(EAGAIN);
  283. xfs_ilock(ip, XFS_ILOCK_SHARED);
  284. }
  285. ASSERT(ip->i_d.di_format != XFS_DINODE_FMT_BTREE ||
  286. (ip->i_df.if_flags & XFS_IFEXTENTS));
  287. ASSERT(offset <= mp->m_maxioffset);
  288. if (offset + count > mp->m_maxioffset)
  289. count = mp->m_maxioffset - offset;
  290. end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + count);
  291. offset_fsb = XFS_B_TO_FSBT(mp, offset);
  292. error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb,
  293. imap, &nimaps, bmapi_flags);
  294. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  295. if (error)
  296. return -XFS_ERROR(error);
  297. if (type == IO_DELALLOC &&
  298. (!nimaps || isnullstartblock(imap->br_startblock))) {
  299. error = xfs_iomap_write_allocate(ip, offset, count, imap);
  300. if (!error)
  301. trace_xfs_map_blocks_alloc(ip, offset, count, type, imap);
  302. return -XFS_ERROR(error);
  303. }
  304. #ifdef DEBUG
  305. if (type == IO_UNWRITTEN) {
  306. ASSERT(nimaps);
  307. ASSERT(imap->br_startblock != HOLESTARTBLOCK);
  308. ASSERT(imap->br_startblock != DELAYSTARTBLOCK);
  309. }
  310. #endif
  311. if (nimaps)
  312. trace_xfs_map_blocks_found(ip, offset, count, type, imap);
  313. return 0;
  314. }
  315. STATIC int
  316. xfs_imap_valid(
  317. struct inode *inode,
  318. struct xfs_bmbt_irec *imap,
  319. xfs_off_t offset)
  320. {
  321. offset >>= inode->i_blkbits;
  322. return offset >= imap->br_startoff &&
  323. offset < imap->br_startoff + imap->br_blockcount;
  324. }
  325. /*
  326. * BIO completion handler for buffered IO.
  327. */
  328. STATIC void
  329. xfs_end_bio(
  330. struct bio *bio,
  331. int error)
  332. {
  333. xfs_ioend_t *ioend = bio->bi_private;
  334. ASSERT(atomic_read(&bio->bi_cnt) >= 1);
  335. ioend->io_error = test_bit(BIO_UPTODATE, &bio->bi_flags) ? 0 : error;
  336. /* Toss bio and pass work off to an xfsdatad thread */
  337. bio->bi_private = NULL;
  338. bio->bi_end_io = NULL;
  339. bio_put(bio);
  340. xfs_finish_ioend(ioend);
  341. }
  342. STATIC void
  343. xfs_submit_ioend_bio(
  344. struct writeback_control *wbc,
  345. xfs_ioend_t *ioend,
  346. struct bio *bio)
  347. {
  348. atomic_inc(&ioend->io_remaining);
  349. bio->bi_private = ioend;
  350. bio->bi_end_io = xfs_end_bio;
  351. submit_bio(wbc->sync_mode == WB_SYNC_ALL ? WRITE_SYNC : WRITE, bio);
  352. }
  353. STATIC struct bio *
  354. xfs_alloc_ioend_bio(
  355. struct buffer_head *bh)
  356. {
  357. int nvecs = bio_get_nr_vecs(bh->b_bdev);
  358. struct bio *bio = bio_alloc(GFP_NOIO, nvecs);
  359. ASSERT(bio->bi_private == NULL);
  360. bio->bi_sector = bh->b_blocknr * (bh->b_size >> 9);
  361. bio->bi_bdev = bh->b_bdev;
  362. return bio;
  363. }
  364. STATIC void
  365. xfs_start_buffer_writeback(
  366. struct buffer_head *bh)
  367. {
  368. ASSERT(buffer_mapped(bh));
  369. ASSERT(buffer_locked(bh));
  370. ASSERT(!buffer_delay(bh));
  371. ASSERT(!buffer_unwritten(bh));
  372. mark_buffer_async_write(bh);
  373. set_buffer_uptodate(bh);
  374. clear_buffer_dirty(bh);
  375. }
  376. STATIC void
  377. xfs_start_page_writeback(
  378. struct page *page,
  379. int clear_dirty,
  380. int buffers)
  381. {
  382. ASSERT(PageLocked(page));
  383. ASSERT(!PageWriteback(page));
  384. if (clear_dirty)
  385. clear_page_dirty_for_io(page);
  386. set_page_writeback(page);
  387. unlock_page(page);
  388. /* If no buffers on the page are to be written, finish it here */
  389. if (!buffers)
  390. end_page_writeback(page);
  391. }
  392. static inline int bio_add_buffer(struct bio *bio, struct buffer_head *bh)
  393. {
  394. return bio_add_page(bio, bh->b_page, bh->b_size, bh_offset(bh));
  395. }
  396. /*
  397. * Submit all of the bios for all of the ioends we have saved up, covering the
  398. * initial writepage page and also any probed pages.
  399. *
  400. * Because we may have multiple ioends spanning a page, we need to start
  401. * writeback on all the buffers before we submit them for I/O. If we mark the
  402. * buffers as we got, then we can end up with a page that only has buffers
  403. * marked async write and I/O complete on can occur before we mark the other
  404. * buffers async write.
  405. *
  406. * The end result of this is that we trip a bug in end_page_writeback() because
  407. * we call it twice for the one page as the code in end_buffer_async_write()
  408. * assumes that all buffers on the page are started at the same time.
  409. *
  410. * The fix is two passes across the ioend list - one to start writeback on the
  411. * buffer_heads, and then submit them for I/O on the second pass.
  412. */
  413. STATIC void
  414. xfs_submit_ioend(
  415. struct writeback_control *wbc,
  416. xfs_ioend_t *ioend)
  417. {
  418. xfs_ioend_t *head = ioend;
  419. xfs_ioend_t *next;
  420. struct buffer_head *bh;
  421. struct bio *bio;
  422. sector_t lastblock = 0;
  423. /* Pass 1 - start writeback */
  424. do {
  425. next = ioend->io_list;
  426. for (bh = ioend->io_buffer_head; bh; bh = bh->b_private)
  427. xfs_start_buffer_writeback(bh);
  428. } while ((ioend = next) != NULL);
  429. /* Pass 2 - submit I/O */
  430. ioend = head;
  431. do {
  432. next = ioend->io_list;
  433. bio = NULL;
  434. for (bh = ioend->io_buffer_head; bh; bh = bh->b_private) {
  435. if (!bio) {
  436. retry:
  437. bio = xfs_alloc_ioend_bio(bh);
  438. } else if (bh->b_blocknr != lastblock + 1) {
  439. xfs_submit_ioend_bio(wbc, ioend, bio);
  440. goto retry;
  441. }
  442. if (bio_add_buffer(bio, bh) != bh->b_size) {
  443. xfs_submit_ioend_bio(wbc, ioend, bio);
  444. goto retry;
  445. }
  446. lastblock = bh->b_blocknr;
  447. }
  448. if (bio)
  449. xfs_submit_ioend_bio(wbc, ioend, bio);
  450. xfs_finish_ioend(ioend);
  451. } while ((ioend = next) != NULL);
  452. }
  453. /*
  454. * Cancel submission of all buffer_heads so far in this endio.
  455. * Toss the endio too. Only ever called for the initial page
  456. * in a writepage request, so only ever one page.
  457. */
  458. STATIC void
  459. xfs_cancel_ioend(
  460. xfs_ioend_t *ioend)
  461. {
  462. xfs_ioend_t *next;
  463. struct buffer_head *bh, *next_bh;
  464. do {
  465. next = ioend->io_list;
  466. bh = ioend->io_buffer_head;
  467. do {
  468. next_bh = bh->b_private;
  469. clear_buffer_async_write(bh);
  470. unlock_buffer(bh);
  471. } while ((bh = next_bh) != NULL);
  472. mempool_free(ioend, xfs_ioend_pool);
  473. } while ((ioend = next) != NULL);
  474. }
  475. /*
  476. * Test to see if we've been building up a completion structure for
  477. * earlier buffers -- if so, we try to append to this ioend if we
  478. * can, otherwise we finish off any current ioend and start another.
  479. * Return true if we've finished the given ioend.
  480. */
  481. STATIC void
  482. xfs_add_to_ioend(
  483. struct inode *inode,
  484. struct buffer_head *bh,
  485. xfs_off_t offset,
  486. unsigned int type,
  487. xfs_ioend_t **result,
  488. int need_ioend)
  489. {
  490. xfs_ioend_t *ioend = *result;
  491. if (!ioend || need_ioend || type != ioend->io_type) {
  492. xfs_ioend_t *previous = *result;
  493. ioend = xfs_alloc_ioend(inode, type);
  494. ioend->io_offset = offset;
  495. ioend->io_buffer_head = bh;
  496. ioend->io_buffer_tail = bh;
  497. if (previous)
  498. previous->io_list = ioend;
  499. *result = ioend;
  500. } else {
  501. ioend->io_buffer_tail->b_private = bh;
  502. ioend->io_buffer_tail = bh;
  503. }
  504. bh->b_private = NULL;
  505. ioend->io_size += bh->b_size;
  506. }
  507. STATIC void
  508. xfs_map_buffer(
  509. struct inode *inode,
  510. struct buffer_head *bh,
  511. struct xfs_bmbt_irec *imap,
  512. xfs_off_t offset)
  513. {
  514. sector_t bn;
  515. struct xfs_mount *m = XFS_I(inode)->i_mount;
  516. xfs_off_t iomap_offset = XFS_FSB_TO_B(m, imap->br_startoff);
  517. xfs_daddr_t iomap_bn = xfs_fsb_to_db(XFS_I(inode), imap->br_startblock);
  518. ASSERT(imap->br_startblock != HOLESTARTBLOCK);
  519. ASSERT(imap->br_startblock != DELAYSTARTBLOCK);
  520. bn = (iomap_bn >> (inode->i_blkbits - BBSHIFT)) +
  521. ((offset - iomap_offset) >> inode->i_blkbits);
  522. ASSERT(bn || XFS_IS_REALTIME_INODE(XFS_I(inode)));
  523. bh->b_blocknr = bn;
  524. set_buffer_mapped(bh);
  525. }
  526. STATIC void
  527. xfs_map_at_offset(
  528. struct inode *inode,
  529. struct buffer_head *bh,
  530. struct xfs_bmbt_irec *imap,
  531. xfs_off_t offset)
  532. {
  533. ASSERT(imap->br_startblock != HOLESTARTBLOCK);
  534. ASSERT(imap->br_startblock != DELAYSTARTBLOCK);
  535. xfs_map_buffer(inode, bh, imap, offset);
  536. set_buffer_mapped(bh);
  537. clear_buffer_delay(bh);
  538. clear_buffer_unwritten(bh);
  539. }
  540. /*
  541. * Test if a given page is suitable for writing as part of an unwritten
  542. * or delayed allocate extent.
  543. */
  544. STATIC int
  545. xfs_check_page_type(
  546. struct page *page,
  547. unsigned int type)
  548. {
  549. if (PageWriteback(page))
  550. return 0;
  551. if (page->mapping && page_has_buffers(page)) {
  552. struct buffer_head *bh, *head;
  553. int acceptable = 0;
  554. bh = head = page_buffers(page);
  555. do {
  556. if (buffer_unwritten(bh))
  557. acceptable += (type == IO_UNWRITTEN);
  558. else if (buffer_delay(bh))
  559. acceptable += (type == IO_DELALLOC);
  560. else if (buffer_dirty(bh) && buffer_mapped(bh))
  561. acceptable += (type == IO_OVERWRITE);
  562. else
  563. break;
  564. } while ((bh = bh->b_this_page) != head);
  565. if (acceptable)
  566. return 1;
  567. }
  568. return 0;
  569. }
  570. /*
  571. * Allocate & map buffers for page given the extent map. Write it out.
  572. * except for the original page of a writepage, this is called on
  573. * delalloc/unwritten pages only, for the original page it is possible
  574. * that the page has no mapping at all.
  575. */
  576. STATIC int
  577. xfs_convert_page(
  578. struct inode *inode,
  579. struct page *page,
  580. loff_t tindex,
  581. struct xfs_bmbt_irec *imap,
  582. xfs_ioend_t **ioendp,
  583. struct writeback_control *wbc)
  584. {
  585. struct buffer_head *bh, *head;
  586. xfs_off_t end_offset;
  587. unsigned long p_offset;
  588. unsigned int type;
  589. int len, page_dirty;
  590. int count = 0, done = 0, uptodate = 1;
  591. xfs_off_t offset = page_offset(page);
  592. if (page->index != tindex)
  593. goto fail;
  594. if (!trylock_page(page))
  595. goto fail;
  596. if (PageWriteback(page))
  597. goto fail_unlock_page;
  598. if (page->mapping != inode->i_mapping)
  599. goto fail_unlock_page;
  600. if (!xfs_check_page_type(page, (*ioendp)->io_type))
  601. goto fail_unlock_page;
  602. /*
  603. * page_dirty is initially a count of buffers on the page before
  604. * EOF and is decremented as we move each into a cleanable state.
  605. *
  606. * Derivation:
  607. *
  608. * End offset is the highest offset that this page should represent.
  609. * If we are on the last page, (end_offset & (PAGE_CACHE_SIZE - 1))
  610. * will evaluate non-zero and be less than PAGE_CACHE_SIZE and
  611. * hence give us the correct page_dirty count. On any other page,
  612. * it will be zero and in that case we need page_dirty to be the
  613. * count of buffers on the page.
  614. */
  615. end_offset = min_t(unsigned long long,
  616. (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT,
  617. i_size_read(inode));
  618. len = 1 << inode->i_blkbits;
  619. p_offset = min_t(unsigned long, end_offset & (PAGE_CACHE_SIZE - 1),
  620. PAGE_CACHE_SIZE);
  621. p_offset = p_offset ? roundup(p_offset, len) : PAGE_CACHE_SIZE;
  622. page_dirty = p_offset / len;
  623. bh = head = page_buffers(page);
  624. do {
  625. if (offset >= end_offset)
  626. break;
  627. if (!buffer_uptodate(bh))
  628. uptodate = 0;
  629. if (!(PageUptodate(page) || buffer_uptodate(bh))) {
  630. done = 1;
  631. continue;
  632. }
  633. if (buffer_unwritten(bh) || buffer_delay(bh) ||
  634. buffer_mapped(bh)) {
  635. if (buffer_unwritten(bh))
  636. type = IO_UNWRITTEN;
  637. else if (buffer_delay(bh))
  638. type = IO_DELALLOC;
  639. else
  640. type = IO_OVERWRITE;
  641. if (!xfs_imap_valid(inode, imap, offset)) {
  642. done = 1;
  643. continue;
  644. }
  645. lock_buffer(bh);
  646. if (type != IO_OVERWRITE)
  647. xfs_map_at_offset(inode, bh, imap, offset);
  648. xfs_add_to_ioend(inode, bh, offset, type,
  649. ioendp, done);
  650. page_dirty--;
  651. count++;
  652. } else {
  653. done = 1;
  654. }
  655. } while (offset += len, (bh = bh->b_this_page) != head);
  656. if (uptodate && bh == head)
  657. SetPageUptodate(page);
  658. if (count) {
  659. if (--wbc->nr_to_write <= 0 &&
  660. wbc->sync_mode == WB_SYNC_NONE)
  661. done = 1;
  662. }
  663. xfs_start_page_writeback(page, !page_dirty, count);
  664. return done;
  665. fail_unlock_page:
  666. unlock_page(page);
  667. fail:
  668. return 1;
  669. }
  670. /*
  671. * Convert & write out a cluster of pages in the same extent as defined
  672. * by mp and following the start page.
  673. */
  674. STATIC void
  675. xfs_cluster_write(
  676. struct inode *inode,
  677. pgoff_t tindex,
  678. struct xfs_bmbt_irec *imap,
  679. xfs_ioend_t **ioendp,
  680. struct writeback_control *wbc,
  681. pgoff_t tlast)
  682. {
  683. struct pagevec pvec;
  684. int done = 0, i;
  685. pagevec_init(&pvec, 0);
  686. while (!done && tindex <= tlast) {
  687. unsigned len = min_t(pgoff_t, PAGEVEC_SIZE, tlast - tindex + 1);
  688. if (!pagevec_lookup(&pvec, inode->i_mapping, tindex, len))
  689. break;
  690. for (i = 0; i < pagevec_count(&pvec); i++) {
  691. done = xfs_convert_page(inode, pvec.pages[i], tindex++,
  692. imap, ioendp, wbc);
  693. if (done)
  694. break;
  695. }
  696. pagevec_release(&pvec);
  697. cond_resched();
  698. }
  699. }
  700. STATIC void
  701. xfs_vm_invalidatepage(
  702. struct page *page,
  703. unsigned long offset)
  704. {
  705. trace_xfs_invalidatepage(page->mapping->host, page, offset);
  706. block_invalidatepage(page, offset);
  707. }
  708. /*
  709. * If the page has delalloc buffers on it, we need to punch them out before we
  710. * invalidate the page. If we don't, we leave a stale delalloc mapping on the
  711. * inode that can trip a BUG() in xfs_get_blocks() later on if a direct IO read
  712. * is done on that same region - the delalloc extent is returned when none is
  713. * supposed to be there.
  714. *
  715. * We prevent this by truncating away the delalloc regions on the page before
  716. * invalidating it. Because they are delalloc, we can do this without needing a
  717. * transaction. Indeed - if we get ENOSPC errors, we have to be able to do this
  718. * truncation without a transaction as there is no space left for block
  719. * reservation (typically why we see a ENOSPC in writeback).
  720. *
  721. * This is not a performance critical path, so for now just do the punching a
  722. * buffer head at a time.
  723. */
  724. STATIC void
  725. xfs_aops_discard_page(
  726. struct page *page)
  727. {
  728. struct inode *inode = page->mapping->host;
  729. struct xfs_inode *ip = XFS_I(inode);
  730. struct buffer_head *bh, *head;
  731. loff_t offset = page_offset(page);
  732. if (!xfs_check_page_type(page, IO_DELALLOC))
  733. goto out_invalidate;
  734. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  735. goto out_invalidate;
  736. xfs_alert(ip->i_mount,
  737. "page discard on page %p, inode 0x%llx, offset %llu.",
  738. page, ip->i_ino, offset);
  739. xfs_ilock(ip, XFS_ILOCK_EXCL);
  740. bh = head = page_buffers(page);
  741. do {
  742. int error;
  743. xfs_fileoff_t start_fsb;
  744. if (!buffer_delay(bh))
  745. goto next_buffer;
  746. start_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
  747. error = xfs_bmap_punch_delalloc_range(ip, start_fsb, 1);
  748. if (error) {
  749. /* something screwed, just bail */
  750. if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
  751. xfs_alert(ip->i_mount,
  752. "page discard unable to remove delalloc mapping.");
  753. }
  754. break;
  755. }
  756. next_buffer:
  757. offset += 1 << inode->i_blkbits;
  758. } while ((bh = bh->b_this_page) != head);
  759. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  760. out_invalidate:
  761. xfs_vm_invalidatepage(page, 0);
  762. return;
  763. }
  764. /*
  765. * Write out a dirty page.
  766. *
  767. * For delalloc space on the page we need to allocate space and flush it.
  768. * For unwritten space on the page we need to start the conversion to
  769. * regular allocated space.
  770. * For any other dirty buffer heads on the page we should flush them.
  771. */
  772. STATIC int
  773. xfs_vm_writepage(
  774. struct page *page,
  775. struct writeback_control *wbc)
  776. {
  777. struct inode *inode = page->mapping->host;
  778. struct buffer_head *bh, *head;
  779. struct xfs_bmbt_irec imap;
  780. xfs_ioend_t *ioend = NULL, *iohead = NULL;
  781. loff_t offset;
  782. unsigned int type;
  783. __uint64_t end_offset;
  784. pgoff_t end_index, last_index;
  785. ssize_t len;
  786. int err, imap_valid = 0, uptodate = 1;
  787. int count = 0;
  788. int nonblocking = 0;
  789. trace_xfs_writepage(inode, page, 0);
  790. ASSERT(page_has_buffers(page));
  791. /*
  792. * Refuse to write the page out if we are called from reclaim context.
  793. *
  794. * This avoids stack overflows when called from deeply used stacks in
  795. * random callers for direct reclaim or memcg reclaim. We explicitly
  796. * allow reclaim from kswapd as the stack usage there is relatively low.
  797. *
  798. * This should never happen except in the case of a VM regression so
  799. * warn about it.
  800. */
  801. if (WARN_ON_ONCE((current->flags & (PF_MEMALLOC|PF_KSWAPD)) ==
  802. PF_MEMALLOC))
  803. goto redirty;
  804. /*
  805. * Given that we do not allow direct reclaim to call us, we should
  806. * never be called while in a filesystem transaction.
  807. */
  808. if (WARN_ON(current->flags & PF_FSTRANS))
  809. goto redirty;
  810. /* Is this page beyond the end of the file? */
  811. offset = i_size_read(inode);
  812. end_index = offset >> PAGE_CACHE_SHIFT;
  813. last_index = (offset - 1) >> PAGE_CACHE_SHIFT;
  814. if (page->index >= end_index) {
  815. if ((page->index >= end_index + 1) ||
  816. !(i_size_read(inode) & (PAGE_CACHE_SIZE - 1))) {
  817. unlock_page(page);
  818. return 0;
  819. }
  820. }
  821. end_offset = min_t(unsigned long long,
  822. (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT,
  823. offset);
  824. len = 1 << inode->i_blkbits;
  825. bh = head = page_buffers(page);
  826. offset = page_offset(page);
  827. type = IO_OVERWRITE;
  828. if (wbc->sync_mode == WB_SYNC_NONE)
  829. nonblocking = 1;
  830. do {
  831. int new_ioend = 0;
  832. if (offset >= end_offset)
  833. break;
  834. if (!buffer_uptodate(bh))
  835. uptodate = 0;
  836. /*
  837. * set_page_dirty dirties all buffers in a page, independent
  838. * of their state. The dirty state however is entirely
  839. * meaningless for holes (!mapped && uptodate), so skip
  840. * buffers covering holes here.
  841. */
  842. if (!buffer_mapped(bh) && buffer_uptodate(bh)) {
  843. imap_valid = 0;
  844. continue;
  845. }
  846. if (buffer_unwritten(bh)) {
  847. if (type != IO_UNWRITTEN) {
  848. type = IO_UNWRITTEN;
  849. imap_valid = 0;
  850. }
  851. } else if (buffer_delay(bh)) {
  852. if (type != IO_DELALLOC) {
  853. type = IO_DELALLOC;
  854. imap_valid = 0;
  855. }
  856. } else if (buffer_uptodate(bh)) {
  857. if (type != IO_OVERWRITE) {
  858. type = IO_OVERWRITE;
  859. imap_valid = 0;
  860. }
  861. } else {
  862. if (PageUptodate(page)) {
  863. ASSERT(buffer_mapped(bh));
  864. imap_valid = 0;
  865. }
  866. continue;
  867. }
  868. if (imap_valid)
  869. imap_valid = xfs_imap_valid(inode, &imap, offset);
  870. if (!imap_valid) {
  871. /*
  872. * If we didn't have a valid mapping then we need to
  873. * put the new mapping into a separate ioend structure.
  874. * This ensures non-contiguous extents always have
  875. * separate ioends, which is particularly important
  876. * for unwritten extent conversion at I/O completion
  877. * time.
  878. */
  879. new_ioend = 1;
  880. err = xfs_map_blocks(inode, offset, &imap, type,
  881. nonblocking);
  882. if (err)
  883. goto error;
  884. imap_valid = xfs_imap_valid(inode, &imap, offset);
  885. }
  886. if (imap_valid) {
  887. lock_buffer(bh);
  888. if (type != IO_OVERWRITE)
  889. xfs_map_at_offset(inode, bh, &imap, offset);
  890. xfs_add_to_ioend(inode, bh, offset, type, &ioend,
  891. new_ioend);
  892. count++;
  893. }
  894. if (!iohead)
  895. iohead = ioend;
  896. } while (offset += len, ((bh = bh->b_this_page) != head));
  897. if (uptodate && bh == head)
  898. SetPageUptodate(page);
  899. xfs_start_page_writeback(page, 1, count);
  900. if (ioend && imap_valid) {
  901. xfs_off_t end_index;
  902. end_index = imap.br_startoff + imap.br_blockcount;
  903. /* to bytes */
  904. end_index <<= inode->i_blkbits;
  905. /* to pages */
  906. end_index = (end_index - 1) >> PAGE_CACHE_SHIFT;
  907. /* check against file size */
  908. if (end_index > last_index)
  909. end_index = last_index;
  910. xfs_cluster_write(inode, page->index + 1, &imap, &ioend,
  911. wbc, end_index);
  912. }
  913. if (iohead) {
  914. /*
  915. * Reserve log space if we might write beyond the on-disk
  916. * inode size.
  917. */
  918. if (ioend->io_type != IO_UNWRITTEN &&
  919. xfs_ioend_is_append(ioend)) {
  920. err = xfs_setfilesize_trans_alloc(ioend);
  921. if (err)
  922. goto error;
  923. }
  924. xfs_submit_ioend(wbc, iohead);
  925. }
  926. return 0;
  927. error:
  928. if (iohead)
  929. xfs_cancel_ioend(iohead);
  930. if (err == -EAGAIN)
  931. goto redirty;
  932. xfs_aops_discard_page(page);
  933. ClearPageUptodate(page);
  934. unlock_page(page);
  935. return err;
  936. redirty:
  937. redirty_page_for_writepage(wbc, page);
  938. unlock_page(page);
  939. return 0;
  940. }
  941. STATIC int
  942. xfs_vm_writepages(
  943. struct address_space *mapping,
  944. struct writeback_control *wbc)
  945. {
  946. xfs_iflags_clear(XFS_I(mapping->host), XFS_ITRUNCATED);
  947. return generic_writepages(mapping, wbc);
  948. }
  949. /*
  950. * Called to move a page into cleanable state - and from there
  951. * to be released. The page should already be clean. We always
  952. * have buffer heads in this call.
  953. *
  954. * Returns 1 if the page is ok to release, 0 otherwise.
  955. */
  956. STATIC int
  957. xfs_vm_releasepage(
  958. struct page *page,
  959. gfp_t gfp_mask)
  960. {
  961. int delalloc, unwritten;
  962. trace_xfs_releasepage(page->mapping->host, page, 0);
  963. xfs_count_page_state(page, &delalloc, &unwritten);
  964. if (WARN_ON(delalloc))
  965. return 0;
  966. if (WARN_ON(unwritten))
  967. return 0;
  968. return try_to_free_buffers(page);
  969. }
  970. STATIC int
  971. __xfs_get_blocks(
  972. struct inode *inode,
  973. sector_t iblock,
  974. struct buffer_head *bh_result,
  975. int create,
  976. int direct)
  977. {
  978. struct xfs_inode *ip = XFS_I(inode);
  979. struct xfs_mount *mp = ip->i_mount;
  980. xfs_fileoff_t offset_fsb, end_fsb;
  981. int error = 0;
  982. int lockmode = 0;
  983. struct xfs_bmbt_irec imap;
  984. int nimaps = 1;
  985. xfs_off_t offset;
  986. ssize_t size;
  987. int new = 0;
  988. if (XFS_FORCED_SHUTDOWN(mp))
  989. return -XFS_ERROR(EIO);
  990. offset = (xfs_off_t)iblock << inode->i_blkbits;
  991. ASSERT(bh_result->b_size >= (1 << inode->i_blkbits));
  992. size = bh_result->b_size;
  993. if (!create && direct && offset >= i_size_read(inode))
  994. return 0;
  995. /*
  996. * Direct I/O is usually done on preallocated files, so try getting
  997. * a block mapping without an exclusive lock first. For buffered
  998. * writes we already have the exclusive iolock anyway, so avoiding
  999. * a lock roundtrip here by taking the ilock exclusive from the
  1000. * beginning is a useful micro optimization.
  1001. */
  1002. if (create && !direct) {
  1003. lockmode = XFS_ILOCK_EXCL;
  1004. xfs_ilock(ip, lockmode);
  1005. } else {
  1006. lockmode = xfs_ilock_map_shared(ip);
  1007. }
  1008. ASSERT(offset <= mp->m_maxioffset);
  1009. if (offset + size > mp->m_maxioffset)
  1010. size = mp->m_maxioffset - offset;
  1011. end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + size);
  1012. offset_fsb = XFS_B_TO_FSBT(mp, offset);
  1013. error = xfs_bmapi_read(ip, offset_fsb, end_fsb - offset_fsb,
  1014. &imap, &nimaps, XFS_BMAPI_ENTIRE);
  1015. if (error)
  1016. goto out_unlock;
  1017. if (create &&
  1018. (!nimaps ||
  1019. (imap.br_startblock == HOLESTARTBLOCK ||
  1020. imap.br_startblock == DELAYSTARTBLOCK))) {
  1021. if (direct || xfs_get_extsz_hint(ip)) {
  1022. /*
  1023. * Drop the ilock in preparation for starting the block
  1024. * allocation transaction. It will be retaken
  1025. * exclusively inside xfs_iomap_write_direct for the
  1026. * actual allocation.
  1027. */
  1028. xfs_iunlock(ip, lockmode);
  1029. error = xfs_iomap_write_direct(ip, offset, size,
  1030. &imap, nimaps);
  1031. if (error)
  1032. return -error;
  1033. new = 1;
  1034. } else {
  1035. /*
  1036. * Delalloc reservations do not require a transaction,
  1037. * we can go on without dropping the lock here. If we
  1038. * are allocating a new delalloc block, make sure that
  1039. * we set the new flag so that we mark the buffer new so
  1040. * that we know that it is newly allocated if the write
  1041. * fails.
  1042. */
  1043. if (nimaps && imap.br_startblock == HOLESTARTBLOCK)
  1044. new = 1;
  1045. error = xfs_iomap_write_delay(ip, offset, size, &imap);
  1046. if (error)
  1047. goto out_unlock;
  1048. xfs_iunlock(ip, lockmode);
  1049. }
  1050. trace_xfs_get_blocks_alloc(ip, offset, size, 0, &imap);
  1051. } else if (nimaps) {
  1052. trace_xfs_get_blocks_found(ip, offset, size, 0, &imap);
  1053. xfs_iunlock(ip, lockmode);
  1054. } else {
  1055. trace_xfs_get_blocks_notfound(ip, offset, size);
  1056. goto out_unlock;
  1057. }
  1058. if (imap.br_startblock != HOLESTARTBLOCK &&
  1059. imap.br_startblock != DELAYSTARTBLOCK) {
  1060. /*
  1061. * For unwritten extents do not report a disk address on
  1062. * the read case (treat as if we're reading into a hole).
  1063. */
  1064. if (create || !ISUNWRITTEN(&imap))
  1065. xfs_map_buffer(inode, bh_result, &imap, offset);
  1066. if (create && ISUNWRITTEN(&imap)) {
  1067. if (direct)
  1068. bh_result->b_private = inode;
  1069. set_buffer_unwritten(bh_result);
  1070. }
  1071. }
  1072. /*
  1073. * If this is a realtime file, data may be on a different device.
  1074. * to that pointed to from the buffer_head b_bdev currently.
  1075. */
  1076. bh_result->b_bdev = xfs_find_bdev_for_inode(inode);
  1077. /*
  1078. * If we previously allocated a block out beyond eof and we are now
  1079. * coming back to use it then we will need to flag it as new even if it
  1080. * has a disk address.
  1081. *
  1082. * With sub-block writes into unwritten extents we also need to mark
  1083. * the buffer as new so that the unwritten parts of the buffer gets
  1084. * correctly zeroed.
  1085. */
  1086. if (create &&
  1087. ((!buffer_mapped(bh_result) && !buffer_uptodate(bh_result)) ||
  1088. (offset >= i_size_read(inode)) ||
  1089. (new || ISUNWRITTEN(&imap))))
  1090. set_buffer_new(bh_result);
  1091. if (imap.br_startblock == DELAYSTARTBLOCK) {
  1092. BUG_ON(direct);
  1093. if (create) {
  1094. set_buffer_uptodate(bh_result);
  1095. set_buffer_mapped(bh_result);
  1096. set_buffer_delay(bh_result);
  1097. }
  1098. }
  1099. /*
  1100. * If this is O_DIRECT or the mpage code calling tell them how large
  1101. * the mapping is, so that we can avoid repeated get_blocks calls.
  1102. */
  1103. if (direct || size > (1 << inode->i_blkbits)) {
  1104. xfs_off_t mapping_size;
  1105. mapping_size = imap.br_startoff + imap.br_blockcount - iblock;
  1106. mapping_size <<= inode->i_blkbits;
  1107. ASSERT(mapping_size > 0);
  1108. if (mapping_size > size)
  1109. mapping_size = size;
  1110. if (mapping_size > LONG_MAX)
  1111. mapping_size = LONG_MAX;
  1112. bh_result->b_size = mapping_size;
  1113. }
  1114. return 0;
  1115. out_unlock:
  1116. xfs_iunlock(ip, lockmode);
  1117. return -error;
  1118. }
  1119. int
  1120. xfs_get_blocks(
  1121. struct inode *inode,
  1122. sector_t iblock,
  1123. struct buffer_head *bh_result,
  1124. int create)
  1125. {
  1126. return __xfs_get_blocks(inode, iblock, bh_result, create, 0);
  1127. }
  1128. STATIC int
  1129. xfs_get_blocks_direct(
  1130. struct inode *inode,
  1131. sector_t iblock,
  1132. struct buffer_head *bh_result,
  1133. int create)
  1134. {
  1135. return __xfs_get_blocks(inode, iblock, bh_result, create, 1);
  1136. }
  1137. /*
  1138. * Complete a direct I/O write request.
  1139. *
  1140. * If the private argument is non-NULL __xfs_get_blocks signals us that we
  1141. * need to issue a transaction to convert the range from unwritten to written
  1142. * extents. In case this is regular synchronous I/O we just call xfs_end_io
  1143. * to do this and we are done. But in case this was a successful AIO
  1144. * request this handler is called from interrupt context, from which we
  1145. * can't start transactions. In that case offload the I/O completion to
  1146. * the workqueues we also use for buffered I/O completion.
  1147. */
  1148. STATIC void
  1149. xfs_end_io_direct_write(
  1150. struct kiocb *iocb,
  1151. loff_t offset,
  1152. ssize_t size,
  1153. void *private,
  1154. int ret,
  1155. bool is_async)
  1156. {
  1157. struct xfs_ioend *ioend = iocb->private;
  1158. /*
  1159. * While the generic direct I/O code updates the inode size, it does
  1160. * so only after the end_io handler is called, which means our
  1161. * end_io handler thinks the on-disk size is outside the in-core
  1162. * size. To prevent this just update it a little bit earlier here.
  1163. */
  1164. if (offset + size > i_size_read(ioend->io_inode))
  1165. i_size_write(ioend->io_inode, offset + size);
  1166. /*
  1167. * blockdev_direct_IO can return an error even after the I/O
  1168. * completion handler was called. Thus we need to protect
  1169. * against double-freeing.
  1170. */
  1171. iocb->private = NULL;
  1172. ioend->io_offset = offset;
  1173. ioend->io_size = size;
  1174. ioend->io_iocb = iocb;
  1175. ioend->io_result = ret;
  1176. if (private && size > 0)
  1177. ioend->io_type = IO_UNWRITTEN;
  1178. if (is_async) {
  1179. ioend->io_isasync = 1;
  1180. xfs_finish_ioend(ioend);
  1181. } else {
  1182. xfs_finish_ioend_sync(ioend);
  1183. }
  1184. }
  1185. STATIC ssize_t
  1186. xfs_vm_direct_IO(
  1187. int rw,
  1188. struct kiocb *iocb,
  1189. const struct iovec *iov,
  1190. loff_t offset,
  1191. unsigned long nr_segs)
  1192. {
  1193. struct inode *inode = iocb->ki_filp->f_mapping->host;
  1194. struct block_device *bdev = xfs_find_bdev_for_inode(inode);
  1195. struct xfs_ioend *ioend = NULL;
  1196. ssize_t ret;
  1197. if (rw & WRITE) {
  1198. size_t size = iov_length(iov, nr_segs);
  1199. /*
  1200. * We need to preallocate a transaction for a size update
  1201. * here. In the case that this write both updates the size
  1202. * and converts at least on unwritten extent we will cancel
  1203. * the still clean transaction after the I/O has finished.
  1204. */
  1205. iocb->private = ioend = xfs_alloc_ioend(inode, IO_DIRECT);
  1206. if (offset + size > XFS_I(inode)->i_d.di_size) {
  1207. ret = xfs_setfilesize_trans_alloc(ioend);
  1208. if (ret)
  1209. goto out_destroy_ioend;
  1210. ioend->io_isdirect = 1;
  1211. }
  1212. ret = __blockdev_direct_IO(rw, iocb, inode, bdev, iov,
  1213. offset, nr_segs,
  1214. xfs_get_blocks_direct,
  1215. xfs_end_io_direct_write, NULL, 0);
  1216. if (ret != -EIOCBQUEUED && iocb->private)
  1217. goto out_trans_cancel;
  1218. } else {
  1219. ret = __blockdev_direct_IO(rw, iocb, inode, bdev, iov,
  1220. offset, nr_segs,
  1221. xfs_get_blocks_direct,
  1222. NULL, NULL, 0);
  1223. }
  1224. return ret;
  1225. out_trans_cancel:
  1226. if (ioend->io_append_trans) {
  1227. current_set_flags_nested(&ioend->io_append_trans->t_pflags,
  1228. PF_FSTRANS);
  1229. xfs_trans_cancel(ioend->io_append_trans, 0);
  1230. }
  1231. out_destroy_ioend:
  1232. xfs_destroy_ioend(ioend);
  1233. return ret;
  1234. }
  1235. /*
  1236. * Punch out the delalloc blocks we have already allocated.
  1237. *
  1238. * Don't bother with xfs_setattr given that nothing can have made it to disk yet
  1239. * as the page is still locked at this point.
  1240. */
  1241. STATIC void
  1242. xfs_vm_kill_delalloc_range(
  1243. struct inode *inode,
  1244. loff_t start,
  1245. loff_t end)
  1246. {
  1247. struct xfs_inode *ip = XFS_I(inode);
  1248. xfs_fileoff_t start_fsb;
  1249. xfs_fileoff_t end_fsb;
  1250. int error;
  1251. start_fsb = XFS_B_TO_FSB(ip->i_mount, start);
  1252. end_fsb = XFS_B_TO_FSB(ip->i_mount, end);
  1253. if (end_fsb <= start_fsb)
  1254. return;
  1255. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1256. error = xfs_bmap_punch_delalloc_range(ip, start_fsb,
  1257. end_fsb - start_fsb);
  1258. if (error) {
  1259. /* something screwed, just bail */
  1260. if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
  1261. xfs_alert(ip->i_mount,
  1262. "xfs_vm_write_failed: unable to clean up ino %lld",
  1263. ip->i_ino);
  1264. }
  1265. }
  1266. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1267. }
  1268. STATIC void
  1269. xfs_vm_write_failed(
  1270. struct inode *inode,
  1271. struct page *page,
  1272. loff_t pos,
  1273. unsigned len)
  1274. {
  1275. loff_t block_offset = pos & PAGE_MASK;
  1276. loff_t block_start;
  1277. loff_t block_end;
  1278. loff_t from = pos & (PAGE_CACHE_SIZE - 1);
  1279. loff_t to = from + len;
  1280. struct buffer_head *bh, *head;
  1281. ASSERT(block_offset + from == pos);
  1282. head = page_buffers(page);
  1283. block_start = 0;
  1284. for (bh = head; bh != head || !block_start;
  1285. bh = bh->b_this_page, block_start = block_end,
  1286. block_offset += bh->b_size) {
  1287. block_end = block_start + bh->b_size;
  1288. /* skip buffers before the write */
  1289. if (block_end <= from)
  1290. continue;
  1291. /* if the buffer is after the write, we're done */
  1292. if (block_start >= to)
  1293. break;
  1294. if (!buffer_delay(bh))
  1295. continue;
  1296. if (!buffer_new(bh) && block_offset < i_size_read(inode))
  1297. continue;
  1298. xfs_vm_kill_delalloc_range(inode, block_offset,
  1299. block_offset + bh->b_size);
  1300. }
  1301. }
  1302. /*
  1303. * This used to call block_write_begin(), but it unlocks and releases the page
  1304. * on error, and we need that page to be able to punch stale delalloc blocks out
  1305. * on failure. hence we copy-n-waste it here and call xfs_vm_write_failed() at
  1306. * the appropriate point.
  1307. */
  1308. STATIC int
  1309. xfs_vm_write_begin(
  1310. struct file *file,
  1311. struct address_space *mapping,
  1312. loff_t pos,
  1313. unsigned len,
  1314. unsigned flags,
  1315. struct page **pagep,
  1316. void **fsdata)
  1317. {
  1318. pgoff_t index = pos >> PAGE_CACHE_SHIFT;
  1319. struct page *page;
  1320. int status;
  1321. ASSERT(len <= PAGE_CACHE_SIZE);
  1322. page = grab_cache_page_write_begin(mapping, index,
  1323. flags | AOP_FLAG_NOFS);
  1324. if (!page)
  1325. return -ENOMEM;
  1326. status = __block_write_begin(page, pos, len, xfs_get_blocks);
  1327. if (unlikely(status)) {
  1328. struct inode *inode = mapping->host;
  1329. xfs_vm_write_failed(inode, page, pos, len);
  1330. unlock_page(page);
  1331. if (pos + len > i_size_read(inode))
  1332. truncate_pagecache(inode, pos + len, i_size_read(inode));
  1333. page_cache_release(page);
  1334. page = NULL;
  1335. }
  1336. *pagep = page;
  1337. return status;
  1338. }
  1339. /*
  1340. * On failure, we only need to kill delalloc blocks beyond EOF because they
  1341. * will never be written. For blocks within EOF, generic_write_end() zeros them
  1342. * so they are safe to leave alone and be written with all the other valid data.
  1343. */
  1344. STATIC int
  1345. xfs_vm_write_end(
  1346. struct file *file,
  1347. struct address_space *mapping,
  1348. loff_t pos,
  1349. unsigned len,
  1350. unsigned copied,
  1351. struct page *page,
  1352. void *fsdata)
  1353. {
  1354. int ret;
  1355. ASSERT(len <= PAGE_CACHE_SIZE);
  1356. ret = generic_write_end(file, mapping, pos, len, copied, page, fsdata);
  1357. if (unlikely(ret < len)) {
  1358. struct inode *inode = mapping->host;
  1359. size_t isize = i_size_read(inode);
  1360. loff_t to = pos + len;
  1361. if (to > isize) {
  1362. truncate_pagecache(inode, to, isize);
  1363. xfs_vm_kill_delalloc_range(inode, isize, to);
  1364. }
  1365. }
  1366. return ret;
  1367. }
  1368. STATIC sector_t
  1369. xfs_vm_bmap(
  1370. struct address_space *mapping,
  1371. sector_t block)
  1372. {
  1373. struct inode *inode = (struct inode *)mapping->host;
  1374. struct xfs_inode *ip = XFS_I(inode);
  1375. trace_xfs_vm_bmap(XFS_I(inode));
  1376. xfs_ilock(ip, XFS_IOLOCK_SHARED);
  1377. xfs_flush_pages(ip, (xfs_off_t)0, -1, 0, FI_REMAPF);
  1378. xfs_iunlock(ip, XFS_IOLOCK_SHARED);
  1379. return generic_block_bmap(mapping, block, xfs_get_blocks);
  1380. }
  1381. STATIC int
  1382. xfs_vm_readpage(
  1383. struct file *unused,
  1384. struct page *page)
  1385. {
  1386. return mpage_readpage(page, xfs_get_blocks);
  1387. }
  1388. STATIC int
  1389. xfs_vm_readpages(
  1390. struct file *unused,
  1391. struct address_space *mapping,
  1392. struct list_head *pages,
  1393. unsigned nr_pages)
  1394. {
  1395. return mpage_readpages(mapping, pages, nr_pages, xfs_get_blocks);
  1396. }
  1397. const struct address_space_operations xfs_address_space_operations = {
  1398. .readpage = xfs_vm_readpage,
  1399. .readpages = xfs_vm_readpages,
  1400. .writepage = xfs_vm_writepage,
  1401. .writepages = xfs_vm_writepages,
  1402. .releasepage = xfs_vm_releasepage,
  1403. .invalidatepage = xfs_vm_invalidatepage,
  1404. .write_begin = xfs_vm_write_begin,
  1405. .write_end = xfs_vm_write_end,
  1406. .bmap = xfs_vm_bmap,
  1407. .direct_IO = xfs_vm_direct_IO,
  1408. .migratepage = buffer_migrate_page,
  1409. .is_partially_uptodate = block_is_partially_uptodate,
  1410. .error_remove_page = generic_error_remove_page,
  1411. };