xfs_aops.c 43 KB

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