xfs_aops.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508
  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_bit.h"
  20. #include "xfs_log.h"
  21. #include "xfs_inum.h"
  22. #include "xfs_sb.h"
  23. #include "xfs_ag.h"
  24. #include "xfs_trans.h"
  25. #include "xfs_mount.h"
  26. #include "xfs_bmap_btree.h"
  27. #include "xfs_dinode.h"
  28. #include "xfs_inode.h"
  29. #include "xfs_alloc.h"
  30. #include "xfs_error.h"
  31. #include "xfs_rw.h"
  32. #include "xfs_iomap.h"
  33. #include "xfs_vnodeops.h"
  34. #include "xfs_trace.h"
  35. #include "xfs_bmap.h"
  36. #include <linux/gfp.h>
  37. #include <linux/mpage.h>
  38. #include <linux/pagevec.h>
  39. #include <linux/writeback.h>
  40. /*
  41. * Prime number of hash buckets since address is used as the key.
  42. */
  43. #define NVSYNC 37
  44. #define to_ioend_wq(v) (&xfs_ioend_wq[((unsigned long)v) % NVSYNC])
  45. static wait_queue_head_t xfs_ioend_wq[NVSYNC];
  46. void __init
  47. xfs_ioend_init(void)
  48. {
  49. int i;
  50. for (i = 0; i < NVSYNC; i++)
  51. init_waitqueue_head(&xfs_ioend_wq[i]);
  52. }
  53. void
  54. xfs_ioend_wait(
  55. xfs_inode_t *ip)
  56. {
  57. wait_queue_head_t *wq = to_ioend_wq(ip);
  58. wait_event(*wq, (atomic_read(&ip->i_iocount) == 0));
  59. }
  60. STATIC void
  61. xfs_ioend_wake(
  62. xfs_inode_t *ip)
  63. {
  64. if (atomic_dec_and_test(&ip->i_iocount))
  65. wake_up(to_ioend_wq(ip));
  66. }
  67. void
  68. xfs_count_page_state(
  69. struct page *page,
  70. int *delalloc,
  71. int *unwritten)
  72. {
  73. struct buffer_head *bh, *head;
  74. *delalloc = *unwritten = 0;
  75. bh = head = page_buffers(page);
  76. do {
  77. if (buffer_unwritten(bh))
  78. (*unwritten) = 1;
  79. else if (buffer_delay(bh))
  80. (*delalloc) = 1;
  81. } while ((bh = bh->b_this_page) != head);
  82. }
  83. STATIC struct block_device *
  84. xfs_find_bdev_for_inode(
  85. struct inode *inode)
  86. {
  87. struct xfs_inode *ip = XFS_I(inode);
  88. struct xfs_mount *mp = ip->i_mount;
  89. if (XFS_IS_REALTIME_INODE(ip))
  90. return mp->m_rtdev_targp->bt_bdev;
  91. else
  92. return mp->m_ddev_targp->bt_bdev;
  93. }
  94. /*
  95. * We're now finished for good with this ioend structure.
  96. * Update the page state via the associated buffer_heads,
  97. * release holds on the inode and bio, and finally free
  98. * up memory. Do not use the ioend after this.
  99. */
  100. STATIC void
  101. xfs_destroy_ioend(
  102. xfs_ioend_t *ioend)
  103. {
  104. struct buffer_head *bh, *next;
  105. struct xfs_inode *ip = XFS_I(ioend->io_inode);
  106. for (bh = ioend->io_buffer_head; bh; bh = next) {
  107. next = bh->b_private;
  108. bh->b_end_io(bh, !ioend->io_error);
  109. }
  110. /*
  111. * Volume managers supporting multiple paths can send back ENODEV
  112. * when the final path disappears. In this case continuing to fill
  113. * the page cache with dirty data which cannot be written out is
  114. * evil, so prevent that.
  115. */
  116. if (unlikely(ioend->io_error == -ENODEV)) {
  117. xfs_do_force_shutdown(ip->i_mount, SHUTDOWN_DEVICE_REQ,
  118. __FILE__, __LINE__);
  119. }
  120. xfs_ioend_wake(ip);
  121. mempool_free(ioend, xfs_ioend_pool);
  122. }
  123. /*
  124. * If the end of the current ioend is beyond the current EOF,
  125. * return the new EOF value, otherwise zero.
  126. */
  127. STATIC xfs_fsize_t
  128. xfs_ioend_new_eof(
  129. xfs_ioend_t *ioend)
  130. {
  131. xfs_inode_t *ip = XFS_I(ioend->io_inode);
  132. xfs_fsize_t isize;
  133. xfs_fsize_t bsize;
  134. bsize = ioend->io_offset + ioend->io_size;
  135. isize = MAX(ip->i_size, ip->i_new_size);
  136. isize = MIN(isize, bsize);
  137. return isize > ip->i_d.di_size ? isize : 0;
  138. }
  139. /*
  140. * Update on-disk file size now that data has been written to disk. The
  141. * current in-memory file size is i_size. If a write is beyond eof i_new_size
  142. * will be the intended file size until i_size is updated. If this write does
  143. * not extend all the way to the valid file size then restrict this update to
  144. * the end of the write.
  145. *
  146. * This function does not block as blocking on the inode lock in IO completion
  147. * can lead to IO completion order dependency deadlocks.. If it can't get the
  148. * inode ilock it will return EAGAIN. Callers must handle this.
  149. */
  150. STATIC int
  151. xfs_setfilesize(
  152. xfs_ioend_t *ioend)
  153. {
  154. xfs_inode_t *ip = XFS_I(ioend->io_inode);
  155. xfs_fsize_t isize;
  156. if (unlikely(ioend->io_error))
  157. return 0;
  158. if (!xfs_ilock_nowait(ip, XFS_ILOCK_EXCL))
  159. return EAGAIN;
  160. isize = xfs_ioend_new_eof(ioend);
  161. if (isize) {
  162. ip->i_d.di_size = isize;
  163. xfs_mark_inode_dirty(ip);
  164. }
  165. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  166. return 0;
  167. }
  168. /*
  169. * Schedule IO completion handling on the final put of an ioend.
  170. */
  171. STATIC void
  172. xfs_finish_ioend(
  173. struct xfs_ioend *ioend)
  174. {
  175. if (atomic_dec_and_test(&ioend->io_remaining)) {
  176. if (ioend->io_type == IO_UNWRITTEN)
  177. queue_work(xfsconvertd_workqueue, &ioend->io_work);
  178. else
  179. queue_work(xfsdatad_workqueue, &ioend->io_work);
  180. }
  181. }
  182. /*
  183. * IO write completion.
  184. */
  185. STATIC void
  186. xfs_end_io(
  187. struct work_struct *work)
  188. {
  189. xfs_ioend_t *ioend = container_of(work, xfs_ioend_t, io_work);
  190. struct xfs_inode *ip = XFS_I(ioend->io_inode);
  191. int error = 0;
  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 == IO_UNWRITTEN &&
  197. likely(!ioend->io_error && !XFS_FORCED_SHUTDOWN(ip->i_mount))) {
  198. error = xfs_iomap_write_unwritten(ip, ioend->io_offset,
  199. ioend->io_size);
  200. if (error)
  201. ioend->io_error = error;
  202. }
  203. /*
  204. * We might have to update the on-disk file size after extending
  205. * writes.
  206. */
  207. error = xfs_setfilesize(ioend);
  208. ASSERT(!error || error == EAGAIN);
  209. /*
  210. * If we didn't complete processing of the ioend, requeue it to the
  211. * tail of the workqueue for another attempt later. Otherwise destroy
  212. * it.
  213. */
  214. if (error == EAGAIN) {
  215. atomic_inc(&ioend->io_remaining);
  216. xfs_finish_ioend(ioend);
  217. /* ensure we don't spin on blocked ioends */
  218. delay(1);
  219. } else {
  220. if (ioend->io_iocb)
  221. aio_complete(ioend->io_iocb, ioend->io_result, 0);
  222. xfs_destroy_ioend(ioend);
  223. }
  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_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. atomic_inc(&XFS_I(ioend->io_inode)->i_iocount);
  261. ioend->io_offset = 0;
  262. ioend->io_size = 0;
  263. ioend->io_iocb = NULL;
  264. ioend->io_result = 0;
  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 == 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_maxioffset);
  295. if (offset + count > mp->m_maxioffset)
  296. count = mp->m_maxioffset - 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(NULL, ip, offset_fsb, end_fsb - offset_fsb,
  300. bmapi_flags, NULL, 0, imap, &nimaps, NULL);
  301. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  302. if (error)
  303. return -XFS_ERROR(error);
  304. if (type == 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 == 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. /*
  359. * If the I/O is beyond EOF we mark the inode dirty immediately
  360. * but don't update the inode size until I/O completion.
  361. */
  362. if (xfs_ioend_new_eof(ioend))
  363. xfs_mark_inode_dirty(XFS_I(ioend->io_inode));
  364. submit_bio(wbc->sync_mode == WB_SYNC_ALL ?
  365. WRITE_SYNC_PLUG : WRITE, bio);
  366. }
  367. STATIC struct bio *
  368. xfs_alloc_ioend_bio(
  369. struct buffer_head *bh)
  370. {
  371. int nvecs = bio_get_nr_vecs(bh->b_bdev);
  372. struct bio *bio = bio_alloc(GFP_NOIO, nvecs);
  373. ASSERT(bio->bi_private == NULL);
  374. bio->bi_sector = bh->b_blocknr * (bh->b_size >> 9);
  375. bio->bi_bdev = bh->b_bdev;
  376. return bio;
  377. }
  378. STATIC void
  379. xfs_start_buffer_writeback(
  380. struct buffer_head *bh)
  381. {
  382. ASSERT(buffer_mapped(bh));
  383. ASSERT(buffer_locked(bh));
  384. ASSERT(!buffer_delay(bh));
  385. ASSERT(!buffer_unwritten(bh));
  386. mark_buffer_async_write(bh);
  387. set_buffer_uptodate(bh);
  388. clear_buffer_dirty(bh);
  389. }
  390. STATIC void
  391. xfs_start_page_writeback(
  392. struct page *page,
  393. int clear_dirty,
  394. int buffers)
  395. {
  396. ASSERT(PageLocked(page));
  397. ASSERT(!PageWriteback(page));
  398. if (clear_dirty)
  399. clear_page_dirty_for_io(page);
  400. set_page_writeback(page);
  401. unlock_page(page);
  402. /* If no buffers on the page are to be written, finish it here */
  403. if (!buffers)
  404. end_page_writeback(page);
  405. }
  406. static inline int bio_add_buffer(struct bio *bio, struct buffer_head *bh)
  407. {
  408. return bio_add_page(bio, bh->b_page, bh->b_size, bh_offset(bh));
  409. }
  410. /*
  411. * Submit all of the bios for all of the ioends we have saved up, covering the
  412. * initial writepage page and also any probed pages.
  413. *
  414. * Because we may have multiple ioends spanning a page, we need to start
  415. * writeback on all the buffers before we submit them for I/O. If we mark the
  416. * buffers as we got, then we can end up with a page that only has buffers
  417. * marked async write and I/O complete on can occur before we mark the other
  418. * buffers async write.
  419. *
  420. * The end result of this is that we trip a bug in end_page_writeback() because
  421. * we call it twice for the one page as the code in end_buffer_async_write()
  422. * assumes that all buffers on the page are started at the same time.
  423. *
  424. * The fix is two passes across the ioend list - one to start writeback on the
  425. * buffer_heads, and then submit them for I/O on the second pass.
  426. */
  427. STATIC void
  428. xfs_submit_ioend(
  429. struct writeback_control *wbc,
  430. xfs_ioend_t *ioend)
  431. {
  432. xfs_ioend_t *head = ioend;
  433. xfs_ioend_t *next;
  434. struct buffer_head *bh;
  435. struct bio *bio;
  436. sector_t lastblock = 0;
  437. /* Pass 1 - start writeback */
  438. do {
  439. next = ioend->io_list;
  440. for (bh = ioend->io_buffer_head; bh; bh = bh->b_private)
  441. xfs_start_buffer_writeback(bh);
  442. } while ((ioend = next) != NULL);
  443. /* Pass 2 - submit I/O */
  444. ioend = head;
  445. do {
  446. next = ioend->io_list;
  447. bio = NULL;
  448. for (bh = ioend->io_buffer_head; bh; bh = bh->b_private) {
  449. if (!bio) {
  450. retry:
  451. bio = xfs_alloc_ioend_bio(bh);
  452. } else if (bh->b_blocknr != lastblock + 1) {
  453. xfs_submit_ioend_bio(wbc, ioend, bio);
  454. goto retry;
  455. }
  456. if (bio_add_buffer(bio, bh) != bh->b_size) {
  457. xfs_submit_ioend_bio(wbc, ioend, bio);
  458. goto retry;
  459. }
  460. lastblock = bh->b_blocknr;
  461. }
  462. if (bio)
  463. xfs_submit_ioend_bio(wbc, ioend, bio);
  464. xfs_finish_ioend(ioend);
  465. } while ((ioend = next) != NULL);
  466. }
  467. /*
  468. * Cancel submission of all buffer_heads so far in this endio.
  469. * Toss the endio too. Only ever called for the initial page
  470. * in a writepage request, so only ever one page.
  471. */
  472. STATIC void
  473. xfs_cancel_ioend(
  474. xfs_ioend_t *ioend)
  475. {
  476. xfs_ioend_t *next;
  477. struct buffer_head *bh, *next_bh;
  478. do {
  479. next = ioend->io_list;
  480. bh = ioend->io_buffer_head;
  481. do {
  482. next_bh = bh->b_private;
  483. clear_buffer_async_write(bh);
  484. unlock_buffer(bh);
  485. } while ((bh = next_bh) != NULL);
  486. xfs_ioend_wake(XFS_I(ioend->io_inode));
  487. mempool_free(ioend, xfs_ioend_pool);
  488. } while ((ioend = next) != NULL);
  489. }
  490. /*
  491. * Test to see if we've been building up a completion structure for
  492. * earlier buffers -- if so, we try to append to this ioend if we
  493. * can, otherwise we finish off any current ioend and start another.
  494. * Return true if we've finished the given ioend.
  495. */
  496. STATIC void
  497. xfs_add_to_ioend(
  498. struct inode *inode,
  499. struct buffer_head *bh,
  500. xfs_off_t offset,
  501. unsigned int type,
  502. xfs_ioend_t **result,
  503. int need_ioend)
  504. {
  505. xfs_ioend_t *ioend = *result;
  506. if (!ioend || need_ioend || type != ioend->io_type) {
  507. xfs_ioend_t *previous = *result;
  508. ioend = xfs_alloc_ioend(inode, type);
  509. ioend->io_offset = offset;
  510. ioend->io_buffer_head = bh;
  511. ioend->io_buffer_tail = bh;
  512. if (previous)
  513. previous->io_list = ioend;
  514. *result = ioend;
  515. } else {
  516. ioend->io_buffer_tail->b_private = bh;
  517. ioend->io_buffer_tail = bh;
  518. }
  519. bh->b_private = NULL;
  520. ioend->io_size += bh->b_size;
  521. }
  522. STATIC void
  523. xfs_map_buffer(
  524. struct inode *inode,
  525. struct buffer_head *bh,
  526. struct xfs_bmbt_irec *imap,
  527. xfs_off_t offset)
  528. {
  529. sector_t bn;
  530. struct xfs_mount *m = XFS_I(inode)->i_mount;
  531. xfs_off_t iomap_offset = XFS_FSB_TO_B(m, imap->br_startoff);
  532. xfs_daddr_t iomap_bn = xfs_fsb_to_db(XFS_I(inode), imap->br_startblock);
  533. ASSERT(imap->br_startblock != HOLESTARTBLOCK);
  534. ASSERT(imap->br_startblock != DELAYSTARTBLOCK);
  535. bn = (iomap_bn >> (inode->i_blkbits - BBSHIFT)) +
  536. ((offset - iomap_offset) >> inode->i_blkbits);
  537. ASSERT(bn || XFS_IS_REALTIME_INODE(XFS_I(inode)));
  538. bh->b_blocknr = bn;
  539. set_buffer_mapped(bh);
  540. }
  541. STATIC void
  542. xfs_map_at_offset(
  543. struct inode *inode,
  544. struct buffer_head *bh,
  545. struct xfs_bmbt_irec *imap,
  546. xfs_off_t offset)
  547. {
  548. ASSERT(imap->br_startblock != HOLESTARTBLOCK);
  549. ASSERT(imap->br_startblock != DELAYSTARTBLOCK);
  550. xfs_map_buffer(inode, bh, imap, offset);
  551. set_buffer_mapped(bh);
  552. clear_buffer_delay(bh);
  553. clear_buffer_unwritten(bh);
  554. }
  555. /*
  556. * Test if a given page is suitable for writing as part of an unwritten
  557. * or delayed allocate extent.
  558. */
  559. STATIC int
  560. xfs_is_delayed_page(
  561. struct page *page,
  562. unsigned int type)
  563. {
  564. if (PageWriteback(page))
  565. return 0;
  566. if (page->mapping && page_has_buffers(page)) {
  567. struct buffer_head *bh, *head;
  568. int acceptable = 0;
  569. bh = head = page_buffers(page);
  570. do {
  571. if (buffer_unwritten(bh))
  572. acceptable = (type == IO_UNWRITTEN);
  573. else if (buffer_delay(bh))
  574. acceptable = (type == IO_DELALLOC);
  575. else if (buffer_dirty(bh) && buffer_mapped(bh))
  576. acceptable = (type == IO_OVERWRITE);
  577. else
  578. break;
  579. } while ((bh = bh->b_this_page) != head);
  580. if (acceptable)
  581. return 1;
  582. }
  583. return 0;
  584. }
  585. /*
  586. * Allocate & map buffers for page given the extent map. Write it out.
  587. * except for the original page of a writepage, this is called on
  588. * delalloc/unwritten pages only, for the original page it is possible
  589. * that the page has no mapping at all.
  590. */
  591. STATIC int
  592. xfs_convert_page(
  593. struct inode *inode,
  594. struct page *page,
  595. loff_t tindex,
  596. struct xfs_bmbt_irec *imap,
  597. xfs_ioend_t **ioendp,
  598. struct writeback_control *wbc)
  599. {
  600. struct buffer_head *bh, *head;
  601. xfs_off_t end_offset;
  602. unsigned long p_offset;
  603. unsigned int type;
  604. int len, page_dirty;
  605. int count = 0, done = 0, uptodate = 1;
  606. xfs_off_t offset = page_offset(page);
  607. if (page->index != tindex)
  608. goto fail;
  609. if (!trylock_page(page))
  610. goto fail;
  611. if (PageWriteback(page))
  612. goto fail_unlock_page;
  613. if (page->mapping != inode->i_mapping)
  614. goto fail_unlock_page;
  615. if (!xfs_is_delayed_page(page, (*ioendp)->io_type))
  616. goto fail_unlock_page;
  617. /*
  618. * page_dirty is initially a count of buffers on the page before
  619. * EOF and is decremented as we move each into a cleanable state.
  620. *
  621. * Derivation:
  622. *
  623. * End offset is the highest offset that this page should represent.
  624. * If we are on the last page, (end_offset & (PAGE_CACHE_SIZE - 1))
  625. * will evaluate non-zero and be less than PAGE_CACHE_SIZE and
  626. * hence give us the correct page_dirty count. On any other page,
  627. * it will be zero and in that case we need page_dirty to be the
  628. * count of buffers on the page.
  629. */
  630. end_offset = min_t(unsigned long long,
  631. (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT,
  632. i_size_read(inode));
  633. len = 1 << inode->i_blkbits;
  634. p_offset = min_t(unsigned long, end_offset & (PAGE_CACHE_SIZE - 1),
  635. PAGE_CACHE_SIZE);
  636. p_offset = p_offset ? roundup(p_offset, len) : PAGE_CACHE_SIZE;
  637. page_dirty = p_offset / len;
  638. bh = head = page_buffers(page);
  639. do {
  640. if (offset >= end_offset)
  641. break;
  642. if (!buffer_uptodate(bh))
  643. uptodate = 0;
  644. if (!(PageUptodate(page) || buffer_uptodate(bh))) {
  645. done = 1;
  646. continue;
  647. }
  648. if (buffer_unwritten(bh) || buffer_delay(bh) ||
  649. buffer_mapped(bh)) {
  650. if (buffer_unwritten(bh))
  651. type = IO_UNWRITTEN;
  652. else if (buffer_delay(bh))
  653. type = IO_DELALLOC;
  654. else
  655. type = IO_OVERWRITE;
  656. if (!xfs_imap_valid(inode, imap, offset)) {
  657. done = 1;
  658. continue;
  659. }
  660. lock_buffer(bh);
  661. if (type != IO_OVERWRITE)
  662. xfs_map_at_offset(inode, bh, imap, offset);
  663. xfs_add_to_ioend(inode, bh, offset, type,
  664. ioendp, done);
  665. page_dirty--;
  666. count++;
  667. } else {
  668. done = 1;
  669. }
  670. } while (offset += len, (bh = bh->b_this_page) != head);
  671. if (uptodate && bh == head)
  672. SetPageUptodate(page);
  673. if (count) {
  674. if (--wbc->nr_to_write <= 0 &&
  675. wbc->sync_mode == WB_SYNC_NONE)
  676. done = 1;
  677. }
  678. xfs_start_page_writeback(page, !page_dirty, count);
  679. return done;
  680. fail_unlock_page:
  681. unlock_page(page);
  682. fail:
  683. return 1;
  684. }
  685. /*
  686. * Convert & write out a cluster of pages in the same extent as defined
  687. * by mp and following the start page.
  688. */
  689. STATIC void
  690. xfs_cluster_write(
  691. struct inode *inode,
  692. pgoff_t tindex,
  693. struct xfs_bmbt_irec *imap,
  694. xfs_ioend_t **ioendp,
  695. struct writeback_control *wbc,
  696. pgoff_t tlast)
  697. {
  698. struct pagevec pvec;
  699. int done = 0, i;
  700. pagevec_init(&pvec, 0);
  701. while (!done && tindex <= tlast) {
  702. unsigned len = min_t(pgoff_t, PAGEVEC_SIZE, tlast - tindex + 1);
  703. if (!pagevec_lookup(&pvec, inode->i_mapping, tindex, len))
  704. break;
  705. for (i = 0; i < pagevec_count(&pvec); i++) {
  706. done = xfs_convert_page(inode, pvec.pages[i], tindex++,
  707. imap, ioendp, wbc);
  708. if (done)
  709. break;
  710. }
  711. pagevec_release(&pvec);
  712. cond_resched();
  713. }
  714. }
  715. STATIC void
  716. xfs_vm_invalidatepage(
  717. struct page *page,
  718. unsigned long offset)
  719. {
  720. trace_xfs_invalidatepage(page->mapping->host, page, offset);
  721. block_invalidatepage(page, offset);
  722. }
  723. /*
  724. * If the page has delalloc buffers on it, we need to punch them out before we
  725. * invalidate the page. If we don't, we leave a stale delalloc mapping on the
  726. * inode that can trip a BUG() in xfs_get_blocks() later on if a direct IO read
  727. * is done on that same region - the delalloc extent is returned when none is
  728. * supposed to be there.
  729. *
  730. * We prevent this by truncating away the delalloc regions on the page before
  731. * invalidating it. Because they are delalloc, we can do this without needing a
  732. * transaction. Indeed - if we get ENOSPC errors, we have to be able to do this
  733. * truncation without a transaction as there is no space left for block
  734. * reservation (typically why we see a ENOSPC in writeback).
  735. *
  736. * This is not a performance critical path, so for now just do the punching a
  737. * buffer head at a time.
  738. */
  739. STATIC void
  740. xfs_aops_discard_page(
  741. struct page *page)
  742. {
  743. struct inode *inode = page->mapping->host;
  744. struct xfs_inode *ip = XFS_I(inode);
  745. struct buffer_head *bh, *head;
  746. loff_t offset = page_offset(page);
  747. if (!xfs_is_delayed_page(page, IO_DELALLOC))
  748. goto out_invalidate;
  749. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  750. goto out_invalidate;
  751. xfs_fs_cmn_err(CE_ALERT, ip->i_mount,
  752. "page discard on page %p, inode 0x%llx, offset %llu.",
  753. page, ip->i_ino, offset);
  754. xfs_ilock(ip, XFS_ILOCK_EXCL);
  755. bh = head = page_buffers(page);
  756. do {
  757. int error;
  758. xfs_fileoff_t start_fsb;
  759. if (!buffer_delay(bh))
  760. goto next_buffer;
  761. start_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
  762. error = xfs_bmap_punch_delalloc_range(ip, start_fsb, 1);
  763. if (error) {
  764. /* something screwed, just bail */
  765. if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
  766. xfs_fs_cmn_err(CE_ALERT, ip->i_mount,
  767. "page discard unable to remove delalloc mapping.");
  768. }
  769. break;
  770. }
  771. next_buffer:
  772. offset += 1 << inode->i_blkbits;
  773. } while ((bh = bh->b_this_page) != head);
  774. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  775. out_invalidate:
  776. xfs_vm_invalidatepage(page, 0);
  777. return;
  778. }
  779. /*
  780. * Write out a dirty page.
  781. *
  782. * For delalloc space on the page we need to allocate space and flush it.
  783. * For unwritten space on the page we need to start the conversion to
  784. * regular allocated space.
  785. * For any other dirty buffer heads on the page we should flush them.
  786. *
  787. * If we detect that a transaction would be required to flush the page, we
  788. * have to check the process flags first, if we are already in a transaction
  789. * or disk I/O during allocations is off, we need to fail the writepage and
  790. * redirty the page.
  791. */
  792. STATIC int
  793. xfs_vm_writepage(
  794. struct page *page,
  795. struct writeback_control *wbc)
  796. {
  797. struct inode *inode = page->mapping->host;
  798. int delalloc, unwritten;
  799. struct buffer_head *bh, *head;
  800. struct xfs_bmbt_irec imap;
  801. xfs_ioend_t *ioend = NULL, *iohead = NULL;
  802. loff_t offset;
  803. unsigned int type;
  804. __uint64_t end_offset;
  805. pgoff_t end_index, last_index;
  806. ssize_t len;
  807. int err, imap_valid = 0, uptodate = 1;
  808. int count = 0;
  809. int nonblocking = 0;
  810. trace_xfs_writepage(inode, page, 0);
  811. ASSERT(page_has_buffers(page));
  812. /*
  813. * Refuse to write the page out if we are called from reclaim context.
  814. *
  815. * This avoids stack overflows when called from deeply used stacks in
  816. * random callers for direct reclaim or memcg reclaim. We explicitly
  817. * allow reclaim from kswapd as the stack usage there is relatively low.
  818. *
  819. * This should really be done by the core VM, but until that happens
  820. * filesystems like XFS, btrfs and ext4 have to take care of this
  821. * by themselves.
  822. */
  823. if ((current->flags & (PF_MEMALLOC|PF_KSWAPD)) == PF_MEMALLOC)
  824. goto redirty;
  825. /*
  826. * We need a transaction if there are delalloc or unwritten buffers
  827. * on the page.
  828. *
  829. * If we need a transaction and the process flags say we are already
  830. * in a transaction, or no IO is allowed then mark the page dirty
  831. * again and leave the page as is.
  832. */
  833. xfs_count_page_state(page, &delalloc, &unwritten);
  834. if ((current->flags & PF_FSTRANS) && (delalloc || unwritten))
  835. goto redirty;
  836. /* Is this page beyond the end of the file? */
  837. offset = i_size_read(inode);
  838. end_index = offset >> PAGE_CACHE_SHIFT;
  839. last_index = (offset - 1) >> PAGE_CACHE_SHIFT;
  840. if (page->index >= end_index) {
  841. if ((page->index >= end_index + 1) ||
  842. !(i_size_read(inode) & (PAGE_CACHE_SIZE - 1))) {
  843. unlock_page(page);
  844. return 0;
  845. }
  846. }
  847. end_offset = min_t(unsigned long long,
  848. (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT,
  849. offset);
  850. len = 1 << inode->i_blkbits;
  851. bh = head = page_buffers(page);
  852. offset = page_offset(page);
  853. type = IO_OVERWRITE;
  854. if (wbc->sync_mode == WB_SYNC_NONE && wbc->nonblocking)
  855. nonblocking = 1;
  856. do {
  857. int new_ioend = 0;
  858. if (offset >= end_offset)
  859. break;
  860. if (!buffer_uptodate(bh))
  861. uptodate = 0;
  862. /*
  863. * set_page_dirty dirties all buffers in a page, independent
  864. * of their state. The dirty state however is entirely
  865. * meaningless for holes (!mapped && uptodate), so skip
  866. * buffers covering holes here.
  867. */
  868. if (!buffer_mapped(bh) && buffer_uptodate(bh)) {
  869. imap_valid = 0;
  870. continue;
  871. }
  872. if (buffer_unwritten(bh)) {
  873. if (type != IO_UNWRITTEN) {
  874. type = IO_UNWRITTEN;
  875. imap_valid = 0;
  876. }
  877. } else if (buffer_delay(bh)) {
  878. if (type != IO_DELALLOC) {
  879. type = IO_DELALLOC;
  880. imap_valid = 0;
  881. }
  882. } else if (buffer_uptodate(bh)) {
  883. if (type != IO_OVERWRITE) {
  884. type = IO_OVERWRITE;
  885. imap_valid = 0;
  886. }
  887. } else {
  888. if (PageUptodate(page)) {
  889. ASSERT(buffer_mapped(bh));
  890. imap_valid = 0;
  891. }
  892. continue;
  893. }
  894. if (imap_valid)
  895. imap_valid = xfs_imap_valid(inode, &imap, offset);
  896. if (!imap_valid) {
  897. /*
  898. * If we didn't have a valid mapping then we need to
  899. * put the new mapping into a separate ioend structure.
  900. * This ensures non-contiguous extents always have
  901. * separate ioends, which is particularly important
  902. * for unwritten extent conversion at I/O completion
  903. * time.
  904. */
  905. new_ioend = 1;
  906. err = xfs_map_blocks(inode, offset, &imap, type,
  907. nonblocking);
  908. if (err)
  909. goto error;
  910. imap_valid = xfs_imap_valid(inode, &imap, offset);
  911. }
  912. if (imap_valid) {
  913. lock_buffer(bh);
  914. if (type != IO_OVERWRITE)
  915. xfs_map_at_offset(inode, bh, &imap, offset);
  916. xfs_add_to_ioend(inode, bh, offset, type, &ioend,
  917. new_ioend);
  918. count++;
  919. }
  920. if (!iohead)
  921. iohead = ioend;
  922. } while (offset += len, ((bh = bh->b_this_page) != head));
  923. if (uptodate && bh == head)
  924. SetPageUptodate(page);
  925. xfs_start_page_writeback(page, 1, count);
  926. if (ioend && imap_valid) {
  927. xfs_off_t end_index;
  928. end_index = imap.br_startoff + imap.br_blockcount;
  929. /* to bytes */
  930. end_index <<= inode->i_blkbits;
  931. /* to pages */
  932. end_index = (end_index - 1) >> PAGE_CACHE_SHIFT;
  933. /* check against file size */
  934. if (end_index > last_index)
  935. end_index = last_index;
  936. xfs_cluster_write(inode, page->index + 1, &imap, &ioend,
  937. wbc, end_index);
  938. }
  939. if (iohead)
  940. xfs_submit_ioend(wbc, iohead);
  941. return 0;
  942. error:
  943. if (iohead)
  944. xfs_cancel_ioend(iohead);
  945. if (err == -EAGAIN)
  946. goto redirty;
  947. xfs_aops_discard_page(page);
  948. ClearPageUptodate(page);
  949. unlock_page(page);
  950. return err;
  951. redirty:
  952. redirty_page_for_writepage(wbc, page);
  953. unlock_page(page);
  954. return 0;
  955. }
  956. STATIC int
  957. xfs_vm_writepages(
  958. struct address_space *mapping,
  959. struct writeback_control *wbc)
  960. {
  961. xfs_iflags_clear(XFS_I(mapping->host), XFS_ITRUNCATED);
  962. return generic_writepages(mapping, wbc);
  963. }
  964. /*
  965. * Called to move a page into cleanable state - and from there
  966. * to be released. The page should already be clean. We always
  967. * have buffer heads in this call.
  968. *
  969. * Returns 1 if the page is ok to release, 0 otherwise.
  970. */
  971. STATIC int
  972. xfs_vm_releasepage(
  973. struct page *page,
  974. gfp_t gfp_mask)
  975. {
  976. int delalloc, unwritten;
  977. trace_xfs_releasepage(page->mapping->host, page, 0);
  978. xfs_count_page_state(page, &delalloc, &unwritten);
  979. if (WARN_ON(delalloc))
  980. return 0;
  981. if (WARN_ON(unwritten))
  982. return 0;
  983. return try_to_free_buffers(page);
  984. }
  985. STATIC int
  986. __xfs_get_blocks(
  987. struct inode *inode,
  988. sector_t iblock,
  989. struct buffer_head *bh_result,
  990. int create,
  991. int direct)
  992. {
  993. struct xfs_inode *ip = XFS_I(inode);
  994. struct xfs_mount *mp = ip->i_mount;
  995. xfs_fileoff_t offset_fsb, end_fsb;
  996. int error = 0;
  997. int lockmode = 0;
  998. struct xfs_bmbt_irec imap;
  999. int nimaps = 1;
  1000. xfs_off_t offset;
  1001. ssize_t size;
  1002. int new = 0;
  1003. if (XFS_FORCED_SHUTDOWN(mp))
  1004. return -XFS_ERROR(EIO);
  1005. offset = (xfs_off_t)iblock << inode->i_blkbits;
  1006. ASSERT(bh_result->b_size >= (1 << inode->i_blkbits));
  1007. size = bh_result->b_size;
  1008. if (!create && direct && offset >= i_size_read(inode))
  1009. return 0;
  1010. if (create) {
  1011. lockmode = XFS_ILOCK_EXCL;
  1012. xfs_ilock(ip, lockmode);
  1013. } else {
  1014. lockmode = xfs_ilock_map_shared(ip);
  1015. }
  1016. ASSERT(offset <= mp->m_maxioffset);
  1017. if (offset + size > mp->m_maxioffset)
  1018. size = mp->m_maxioffset - offset;
  1019. end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + size);
  1020. offset_fsb = XFS_B_TO_FSBT(mp, offset);
  1021. error = xfs_bmapi(NULL, ip, offset_fsb, end_fsb - offset_fsb,
  1022. XFS_BMAPI_ENTIRE, NULL, 0, &imap, &nimaps, NULL);
  1023. if (error)
  1024. goto out_unlock;
  1025. if (create &&
  1026. (!nimaps ||
  1027. (imap.br_startblock == HOLESTARTBLOCK ||
  1028. imap.br_startblock == DELAYSTARTBLOCK))) {
  1029. if (direct) {
  1030. error = xfs_iomap_write_direct(ip, offset, size,
  1031. &imap, nimaps);
  1032. } else {
  1033. error = xfs_iomap_write_delay(ip, offset, size, &imap);
  1034. }
  1035. if (error)
  1036. goto out_unlock;
  1037. trace_xfs_get_blocks_alloc(ip, offset, size, 0, &imap);
  1038. } else if (nimaps) {
  1039. trace_xfs_get_blocks_found(ip, offset, size, 0, &imap);
  1040. } else {
  1041. trace_xfs_get_blocks_notfound(ip, offset, size);
  1042. goto out_unlock;
  1043. }
  1044. xfs_iunlock(ip, lockmode);
  1045. if (imap.br_startblock != HOLESTARTBLOCK &&
  1046. imap.br_startblock != DELAYSTARTBLOCK) {
  1047. /*
  1048. * For unwritten extents do not report a disk address on
  1049. * the read case (treat as if we're reading into a hole).
  1050. */
  1051. if (create || !ISUNWRITTEN(&imap))
  1052. xfs_map_buffer(inode, bh_result, &imap, offset);
  1053. if (create && ISUNWRITTEN(&imap)) {
  1054. if (direct)
  1055. bh_result->b_private = inode;
  1056. set_buffer_unwritten(bh_result);
  1057. }
  1058. }
  1059. /*
  1060. * If this is a realtime file, data may be on a different device.
  1061. * to that pointed to from the buffer_head b_bdev currently.
  1062. */
  1063. bh_result->b_bdev = xfs_find_bdev_for_inode(inode);
  1064. /*
  1065. * If we previously allocated a block out beyond eof and we are now
  1066. * coming back to use it then we will need to flag it as new even if it
  1067. * has a disk address.
  1068. *
  1069. * With sub-block writes into unwritten extents we also need to mark
  1070. * the buffer as new so that the unwritten parts of the buffer gets
  1071. * correctly zeroed.
  1072. */
  1073. if (create &&
  1074. ((!buffer_mapped(bh_result) && !buffer_uptodate(bh_result)) ||
  1075. (offset >= i_size_read(inode)) ||
  1076. (new || ISUNWRITTEN(&imap))))
  1077. set_buffer_new(bh_result);
  1078. if (imap.br_startblock == DELAYSTARTBLOCK) {
  1079. BUG_ON(direct);
  1080. if (create) {
  1081. set_buffer_uptodate(bh_result);
  1082. set_buffer_mapped(bh_result);
  1083. set_buffer_delay(bh_result);
  1084. }
  1085. }
  1086. /*
  1087. * If this is O_DIRECT or the mpage code calling tell them how large
  1088. * the mapping is, so that we can avoid repeated get_blocks calls.
  1089. */
  1090. if (direct || size > (1 << inode->i_blkbits)) {
  1091. xfs_off_t mapping_size;
  1092. mapping_size = imap.br_startoff + imap.br_blockcount - iblock;
  1093. mapping_size <<= inode->i_blkbits;
  1094. ASSERT(mapping_size > 0);
  1095. if (mapping_size > size)
  1096. mapping_size = size;
  1097. if (mapping_size > LONG_MAX)
  1098. mapping_size = LONG_MAX;
  1099. bh_result->b_size = mapping_size;
  1100. }
  1101. return 0;
  1102. out_unlock:
  1103. xfs_iunlock(ip, lockmode);
  1104. return -error;
  1105. }
  1106. int
  1107. xfs_get_blocks(
  1108. struct inode *inode,
  1109. sector_t iblock,
  1110. struct buffer_head *bh_result,
  1111. int create)
  1112. {
  1113. return __xfs_get_blocks(inode, iblock, bh_result, create, 0);
  1114. }
  1115. STATIC int
  1116. xfs_get_blocks_direct(
  1117. struct inode *inode,
  1118. sector_t iblock,
  1119. struct buffer_head *bh_result,
  1120. int create)
  1121. {
  1122. return __xfs_get_blocks(inode, iblock, bh_result, create, 1);
  1123. }
  1124. /*
  1125. * Complete a direct I/O write request.
  1126. *
  1127. * If the private argument is non-NULL __xfs_get_blocks signals us that we
  1128. * need to issue a transaction to convert the range from unwritten to written
  1129. * extents. In case this is regular synchronous I/O we just call xfs_end_io
  1130. * to do this and we are done. But in case this was a successfull AIO
  1131. * request this handler is called from interrupt context, from which we
  1132. * can't start transactions. In that case offload the I/O completion to
  1133. * the workqueues we also use for buffered I/O completion.
  1134. */
  1135. STATIC void
  1136. xfs_end_io_direct_write(
  1137. struct kiocb *iocb,
  1138. loff_t offset,
  1139. ssize_t size,
  1140. void *private,
  1141. int ret,
  1142. bool is_async)
  1143. {
  1144. struct xfs_ioend *ioend = iocb->private;
  1145. /*
  1146. * blockdev_direct_IO can return an error even after the I/O
  1147. * completion handler was called. Thus we need to protect
  1148. * against double-freeing.
  1149. */
  1150. iocb->private = NULL;
  1151. ioend->io_offset = offset;
  1152. ioend->io_size = size;
  1153. if (private && size > 0)
  1154. ioend->io_type = IO_UNWRITTEN;
  1155. if (is_async) {
  1156. /*
  1157. * If we are converting an unwritten extent we need to delay
  1158. * the AIO completion until after the unwrittent extent
  1159. * conversion has completed, otherwise do it ASAP.
  1160. */
  1161. if (ioend->io_type == IO_UNWRITTEN) {
  1162. ioend->io_iocb = iocb;
  1163. ioend->io_result = ret;
  1164. } else {
  1165. aio_complete(iocb, ret, 0);
  1166. }
  1167. xfs_finish_ioend(ioend);
  1168. } else {
  1169. xfs_finish_ioend_sync(ioend);
  1170. }
  1171. }
  1172. STATIC ssize_t
  1173. xfs_vm_direct_IO(
  1174. int rw,
  1175. struct kiocb *iocb,
  1176. const struct iovec *iov,
  1177. loff_t offset,
  1178. unsigned long nr_segs)
  1179. {
  1180. struct inode *inode = iocb->ki_filp->f_mapping->host;
  1181. struct block_device *bdev = xfs_find_bdev_for_inode(inode);
  1182. ssize_t ret;
  1183. if (rw & WRITE) {
  1184. iocb->private = xfs_alloc_ioend(inode, IO_DIRECT);
  1185. ret = __blockdev_direct_IO(rw, iocb, inode, bdev, iov,
  1186. offset, nr_segs,
  1187. xfs_get_blocks_direct,
  1188. xfs_end_io_direct_write, NULL, 0);
  1189. if (ret != -EIOCBQUEUED && iocb->private)
  1190. xfs_destroy_ioend(iocb->private);
  1191. } else {
  1192. ret = __blockdev_direct_IO(rw, iocb, inode, bdev, iov,
  1193. offset, nr_segs,
  1194. xfs_get_blocks_direct,
  1195. NULL, NULL, 0);
  1196. }
  1197. return ret;
  1198. }
  1199. STATIC void
  1200. xfs_vm_write_failed(
  1201. struct address_space *mapping,
  1202. loff_t to)
  1203. {
  1204. struct inode *inode = mapping->host;
  1205. if (to > inode->i_size) {
  1206. /*
  1207. * punch out the delalloc blocks we have already allocated. We
  1208. * don't call xfs_setattr() to do this as we may be in the
  1209. * middle of a multi-iovec write and so the vfs inode->i_size
  1210. * will not match the xfs ip->i_size and so it will zero too
  1211. * much. Hence we jus truncate the page cache to zero what is
  1212. * necessary and punch the delalloc blocks directly.
  1213. */
  1214. struct xfs_inode *ip = XFS_I(inode);
  1215. xfs_fileoff_t start_fsb;
  1216. xfs_fileoff_t end_fsb;
  1217. int error;
  1218. truncate_pagecache(inode, to, inode->i_size);
  1219. /*
  1220. * Check if there are any blocks that are outside of i_size
  1221. * that need to be trimmed back.
  1222. */
  1223. start_fsb = XFS_B_TO_FSB(ip->i_mount, inode->i_size) + 1;
  1224. end_fsb = XFS_B_TO_FSB(ip->i_mount, to);
  1225. if (end_fsb <= start_fsb)
  1226. return;
  1227. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1228. error = xfs_bmap_punch_delalloc_range(ip, start_fsb,
  1229. end_fsb - start_fsb);
  1230. if (error) {
  1231. /* something screwed, just bail */
  1232. if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
  1233. xfs_fs_cmn_err(CE_ALERT, ip->i_mount,
  1234. "xfs_vm_write_failed: unable to clean up ino %lld",
  1235. ip->i_ino);
  1236. }
  1237. }
  1238. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1239. }
  1240. }
  1241. STATIC int
  1242. xfs_vm_write_begin(
  1243. struct file *file,
  1244. struct address_space *mapping,
  1245. loff_t pos,
  1246. unsigned len,
  1247. unsigned flags,
  1248. struct page **pagep,
  1249. void **fsdata)
  1250. {
  1251. int ret;
  1252. ret = block_write_begin(mapping, pos, len, flags | AOP_FLAG_NOFS,
  1253. pagep, xfs_get_blocks);
  1254. if (unlikely(ret))
  1255. xfs_vm_write_failed(mapping, pos + len);
  1256. return ret;
  1257. }
  1258. STATIC int
  1259. xfs_vm_write_end(
  1260. struct file *file,
  1261. struct address_space *mapping,
  1262. loff_t pos,
  1263. unsigned len,
  1264. unsigned copied,
  1265. struct page *page,
  1266. void *fsdata)
  1267. {
  1268. int ret;
  1269. ret = generic_write_end(file, mapping, pos, len, copied, page, fsdata);
  1270. if (unlikely(ret < len))
  1271. xfs_vm_write_failed(mapping, pos + len);
  1272. return ret;
  1273. }
  1274. STATIC sector_t
  1275. xfs_vm_bmap(
  1276. struct address_space *mapping,
  1277. sector_t block)
  1278. {
  1279. struct inode *inode = (struct inode *)mapping->host;
  1280. struct xfs_inode *ip = XFS_I(inode);
  1281. trace_xfs_vm_bmap(XFS_I(inode));
  1282. xfs_ilock(ip, XFS_IOLOCK_SHARED);
  1283. xfs_flush_pages(ip, (xfs_off_t)0, -1, 0, FI_REMAPF);
  1284. xfs_iunlock(ip, XFS_IOLOCK_SHARED);
  1285. return generic_block_bmap(mapping, block, xfs_get_blocks);
  1286. }
  1287. STATIC int
  1288. xfs_vm_readpage(
  1289. struct file *unused,
  1290. struct page *page)
  1291. {
  1292. return mpage_readpage(page, xfs_get_blocks);
  1293. }
  1294. STATIC int
  1295. xfs_vm_readpages(
  1296. struct file *unused,
  1297. struct address_space *mapping,
  1298. struct list_head *pages,
  1299. unsigned nr_pages)
  1300. {
  1301. return mpage_readpages(mapping, pages, nr_pages, xfs_get_blocks);
  1302. }
  1303. const struct address_space_operations xfs_address_space_operations = {
  1304. .readpage = xfs_vm_readpage,
  1305. .readpages = xfs_vm_readpages,
  1306. .writepage = xfs_vm_writepage,
  1307. .writepages = xfs_vm_writepages,
  1308. .sync_page = block_sync_page,
  1309. .releasepage = xfs_vm_releasepage,
  1310. .invalidatepage = xfs_vm_invalidatepage,
  1311. .write_begin = xfs_vm_write_begin,
  1312. .write_end = xfs_vm_write_end,
  1313. .bmap = xfs_vm_bmap,
  1314. .direct_IO = xfs_vm_direct_IO,
  1315. .migratepage = buffer_migrate_page,
  1316. .is_partially_uptodate = block_is_partially_uptodate,
  1317. .error_remove_page = generic_error_remove_page,
  1318. };