xfs_aops.c 36 KB

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