xfs_aops.c 41 KB

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