xfs_aops.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541
  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. lock_buffer(bh);
  551. xfs_map_buffer(inode, bh, imap, offset);
  552. bh->b_bdev = xfs_find_bdev_for_inode(inode);
  553. set_buffer_mapped(bh);
  554. clear_buffer_delay(bh);
  555. clear_buffer_unwritten(bh);
  556. }
  557. /*
  558. * Test if a given page is suitable for writing as part of an unwritten
  559. * or delayed allocate extent.
  560. */
  561. STATIC int
  562. xfs_is_delayed_page(
  563. struct page *page,
  564. unsigned int type)
  565. {
  566. if (PageWriteback(page))
  567. return 0;
  568. if (page->mapping && page_has_buffers(page)) {
  569. struct buffer_head *bh, *head;
  570. int acceptable = 0;
  571. bh = head = page_buffers(page);
  572. do {
  573. if (buffer_unwritten(bh))
  574. acceptable = (type == IO_UNWRITTEN);
  575. else if (buffer_delay(bh))
  576. acceptable = (type == IO_DELALLOC);
  577. else if (buffer_dirty(bh) && buffer_mapped(bh))
  578. acceptable = (type == IO_OVERWRITE);
  579. else
  580. break;
  581. } while ((bh = bh->b_this_page) != head);
  582. if (acceptable)
  583. return 1;
  584. }
  585. return 0;
  586. }
  587. /*
  588. * Allocate & map buffers for page given the extent map. Write it out.
  589. * except for the original page of a writepage, this is called on
  590. * delalloc/unwritten pages only, for the original page it is possible
  591. * that the page has no mapping at all.
  592. */
  593. STATIC int
  594. xfs_convert_page(
  595. struct inode *inode,
  596. struct page *page,
  597. loff_t tindex,
  598. struct xfs_bmbt_irec *imap,
  599. xfs_ioend_t **ioendp,
  600. struct writeback_control *wbc,
  601. int all_bh)
  602. {
  603. struct buffer_head *bh, *head;
  604. xfs_off_t end_offset;
  605. unsigned long p_offset;
  606. unsigned int type;
  607. int len, page_dirty;
  608. int count = 0, done = 0, uptodate = 1;
  609. xfs_off_t offset = page_offset(page);
  610. if (page->index != tindex)
  611. goto fail;
  612. if (!trylock_page(page))
  613. goto fail;
  614. if (PageWriteback(page))
  615. goto fail_unlock_page;
  616. if (page->mapping != inode->i_mapping)
  617. goto fail_unlock_page;
  618. if (!xfs_is_delayed_page(page, (*ioendp)->io_type))
  619. goto fail_unlock_page;
  620. /*
  621. * page_dirty is initially a count of buffers on the page before
  622. * EOF and is decremented as we move each into a cleanable state.
  623. *
  624. * Derivation:
  625. *
  626. * End offset is the highest offset that this page should represent.
  627. * If we are on the last page, (end_offset & (PAGE_CACHE_SIZE - 1))
  628. * will evaluate non-zero and be less than PAGE_CACHE_SIZE and
  629. * hence give us the correct page_dirty count. On any other page,
  630. * it will be zero and in that case we need page_dirty to be the
  631. * count of buffers on the page.
  632. */
  633. end_offset = min_t(unsigned long long,
  634. (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT,
  635. i_size_read(inode));
  636. len = 1 << inode->i_blkbits;
  637. p_offset = min_t(unsigned long, end_offset & (PAGE_CACHE_SIZE - 1),
  638. PAGE_CACHE_SIZE);
  639. p_offset = p_offset ? roundup(p_offset, len) : PAGE_CACHE_SIZE;
  640. page_dirty = p_offset / len;
  641. bh = head = page_buffers(page);
  642. do {
  643. if (offset >= end_offset)
  644. break;
  645. if (!buffer_uptodate(bh))
  646. uptodate = 0;
  647. if (!(PageUptodate(page) || buffer_uptodate(bh))) {
  648. done = 1;
  649. continue;
  650. }
  651. if (buffer_unwritten(bh) || buffer_delay(bh)) {
  652. if (buffer_unwritten(bh))
  653. type = IO_UNWRITTEN;
  654. else
  655. type = IO_DELALLOC;
  656. if (!xfs_imap_valid(inode, imap, offset)) {
  657. done = 1;
  658. continue;
  659. }
  660. ASSERT(imap->br_startblock != HOLESTARTBLOCK);
  661. ASSERT(imap->br_startblock != DELAYSTARTBLOCK);
  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. type = IO_OVERWRITE;
  669. if (buffer_mapped(bh) && all_bh) {
  670. lock_buffer(bh);
  671. xfs_add_to_ioend(inode, bh, offset,
  672. type, ioendp, done);
  673. count++;
  674. page_dirty--;
  675. } else {
  676. done = 1;
  677. }
  678. }
  679. } while (offset += len, (bh = bh->b_this_page) != head);
  680. if (uptodate && bh == head)
  681. SetPageUptodate(page);
  682. if (count) {
  683. if (--wbc->nr_to_write <= 0 &&
  684. wbc->sync_mode == WB_SYNC_NONE)
  685. done = 1;
  686. }
  687. xfs_start_page_writeback(page, !page_dirty, count);
  688. return done;
  689. fail_unlock_page:
  690. unlock_page(page);
  691. fail:
  692. return 1;
  693. }
  694. /*
  695. * Convert & write out a cluster of pages in the same extent as defined
  696. * by mp and following the start page.
  697. */
  698. STATIC void
  699. xfs_cluster_write(
  700. struct inode *inode,
  701. pgoff_t tindex,
  702. struct xfs_bmbt_irec *imap,
  703. xfs_ioend_t **ioendp,
  704. struct writeback_control *wbc,
  705. int all_bh,
  706. pgoff_t tlast)
  707. {
  708. struct pagevec pvec;
  709. int done = 0, i;
  710. pagevec_init(&pvec, 0);
  711. while (!done && tindex <= tlast) {
  712. unsigned len = min_t(pgoff_t, PAGEVEC_SIZE, tlast - tindex + 1);
  713. if (!pagevec_lookup(&pvec, inode->i_mapping, tindex, len))
  714. break;
  715. for (i = 0; i < pagevec_count(&pvec); i++) {
  716. done = xfs_convert_page(inode, pvec.pages[i], tindex++,
  717. imap, ioendp, wbc, all_bh);
  718. if (done)
  719. break;
  720. }
  721. pagevec_release(&pvec);
  722. cond_resched();
  723. }
  724. }
  725. STATIC void
  726. xfs_vm_invalidatepage(
  727. struct page *page,
  728. unsigned long offset)
  729. {
  730. trace_xfs_invalidatepage(page->mapping->host, page, offset);
  731. block_invalidatepage(page, offset);
  732. }
  733. /*
  734. * If the page has delalloc buffers on it, we need to punch them out before we
  735. * invalidate the page. If we don't, we leave a stale delalloc mapping on the
  736. * inode that can trip a BUG() in xfs_get_blocks() later on if a direct IO read
  737. * is done on that same region - the delalloc extent is returned when none is
  738. * supposed to be there.
  739. *
  740. * We prevent this by truncating away the delalloc regions on the page before
  741. * invalidating it. Because they are delalloc, we can do this without needing a
  742. * transaction. Indeed - if we get ENOSPC errors, we have to be able to do this
  743. * truncation without a transaction as there is no space left for block
  744. * reservation (typically why we see a ENOSPC in writeback).
  745. *
  746. * This is not a performance critical path, so for now just do the punching a
  747. * buffer head at a time.
  748. */
  749. STATIC void
  750. xfs_aops_discard_page(
  751. struct page *page)
  752. {
  753. struct inode *inode = page->mapping->host;
  754. struct xfs_inode *ip = XFS_I(inode);
  755. struct buffer_head *bh, *head;
  756. loff_t offset = page_offset(page);
  757. if (!xfs_is_delayed_page(page, IO_DELALLOC))
  758. goto out_invalidate;
  759. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  760. goto out_invalidate;
  761. xfs_fs_cmn_err(CE_ALERT, ip->i_mount,
  762. "page discard on page %p, inode 0x%llx, offset %llu.",
  763. page, ip->i_ino, offset);
  764. xfs_ilock(ip, XFS_ILOCK_EXCL);
  765. bh = head = page_buffers(page);
  766. do {
  767. int error;
  768. xfs_fileoff_t start_fsb;
  769. if (!buffer_delay(bh))
  770. goto next_buffer;
  771. start_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
  772. error = xfs_bmap_punch_delalloc_range(ip, start_fsb, 1);
  773. if (error) {
  774. /* something screwed, just bail */
  775. if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
  776. xfs_fs_cmn_err(CE_ALERT, ip->i_mount,
  777. "page discard unable to remove delalloc mapping.");
  778. }
  779. break;
  780. }
  781. next_buffer:
  782. offset += 1 << inode->i_blkbits;
  783. } while ((bh = bh->b_this_page) != head);
  784. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  785. out_invalidate:
  786. xfs_vm_invalidatepage(page, 0);
  787. return;
  788. }
  789. /*
  790. * Write out a dirty page.
  791. *
  792. * For delalloc space on the page we need to allocate space and flush it.
  793. * For unwritten space on the page we need to start the conversion to
  794. * regular allocated space.
  795. * For any other dirty buffer heads on the page we should flush them.
  796. *
  797. * If we detect that a transaction would be required to flush the page, we
  798. * have to check the process flags first, if we are already in a transaction
  799. * or disk I/O during allocations is off, we need to fail the writepage and
  800. * redirty the page.
  801. */
  802. STATIC int
  803. xfs_vm_writepage(
  804. struct page *page,
  805. struct writeback_control *wbc)
  806. {
  807. struct inode *inode = page->mapping->host;
  808. int delalloc, unwritten;
  809. struct buffer_head *bh, *head;
  810. struct xfs_bmbt_irec imap;
  811. xfs_ioend_t *ioend = NULL, *iohead = NULL;
  812. loff_t offset;
  813. unsigned int type;
  814. __uint64_t end_offset;
  815. pgoff_t end_index, last_index;
  816. ssize_t len;
  817. int err, imap_valid = 0, uptodate = 1;
  818. int count = 0;
  819. int all_bh = 0;
  820. int nonblocking = 0;
  821. trace_xfs_writepage(inode, page, 0);
  822. ASSERT(page_has_buffers(page));
  823. /*
  824. * Refuse to write the page out if we are called from reclaim context.
  825. *
  826. * This avoids stack overflows when called from deeply used stacks in
  827. * random callers for direct reclaim or memcg reclaim. We explicitly
  828. * allow reclaim from kswapd as the stack usage there is relatively low.
  829. *
  830. * This should really be done by the core VM, but until that happens
  831. * filesystems like XFS, btrfs and ext4 have to take care of this
  832. * by themselves.
  833. */
  834. if ((current->flags & (PF_MEMALLOC|PF_KSWAPD)) == PF_MEMALLOC)
  835. goto redirty;
  836. /*
  837. * We need a transaction if there are delalloc or unwritten buffers
  838. * on the page.
  839. *
  840. * If we need a transaction and the process flags say we are already
  841. * in a transaction, or no IO is allowed then mark the page dirty
  842. * again and leave the page as is.
  843. */
  844. xfs_count_page_state(page, &delalloc, &unwritten);
  845. if ((current->flags & PF_FSTRANS) && (delalloc || unwritten))
  846. goto redirty;
  847. /* Is this page beyond the end of the file? */
  848. offset = i_size_read(inode);
  849. end_index = offset >> PAGE_CACHE_SHIFT;
  850. last_index = (offset - 1) >> PAGE_CACHE_SHIFT;
  851. if (page->index >= end_index) {
  852. if ((page->index >= end_index + 1) ||
  853. !(i_size_read(inode) & (PAGE_CACHE_SIZE - 1))) {
  854. unlock_page(page);
  855. return 0;
  856. }
  857. }
  858. end_offset = min_t(unsigned long long,
  859. (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT,
  860. offset);
  861. len = 1 << inode->i_blkbits;
  862. bh = head = page_buffers(page);
  863. offset = page_offset(page);
  864. type = IO_OVERWRITE;
  865. if (wbc->sync_mode == WB_SYNC_NONE && wbc->nonblocking)
  866. nonblocking = 1;
  867. do {
  868. int new_ioend = 0;
  869. if (offset >= end_offset)
  870. break;
  871. if (!buffer_uptodate(bh))
  872. uptodate = 0;
  873. /*
  874. * set_page_dirty dirties all buffers in a page, independent
  875. * of their state. The dirty state however is entirely
  876. * meaningless for holes (!mapped && uptodate), so skip
  877. * buffers covering holes here.
  878. */
  879. if (!buffer_mapped(bh) && buffer_uptodate(bh)) {
  880. imap_valid = 0;
  881. continue;
  882. }
  883. if (imap_valid)
  884. imap_valid = xfs_imap_valid(inode, &imap, offset);
  885. if (buffer_unwritten(bh) || buffer_delay(bh)) {
  886. if (buffer_unwritten(bh)) {
  887. if (type != IO_UNWRITTEN) {
  888. type = IO_UNWRITTEN;
  889. imap_valid = 0;
  890. }
  891. } else if (buffer_delay(bh)) {
  892. if (type != IO_DELALLOC) {
  893. type = IO_DELALLOC;
  894. imap_valid = 0;
  895. }
  896. }
  897. if (!imap_valid) {
  898. /*
  899. * If we didn't have a valid mapping then we
  900. * need to ensure that we put the new mapping
  901. * in a new ioend structure. This needs to be
  902. * done to ensure that the ioends correctly
  903. * reflect the block mappings at io completion
  904. * for unwritten extent conversion.
  905. */
  906. new_ioend = 1;
  907. err = xfs_map_blocks(inode, offset, &imap,
  908. type, nonblocking);
  909. if (err)
  910. goto error;
  911. imap_valid = xfs_imap_valid(inode, &imap,
  912. offset);
  913. }
  914. if (imap_valid) {
  915. xfs_map_at_offset(inode, bh, &imap, offset);
  916. xfs_add_to_ioend(inode, bh, offset, type,
  917. &ioend, new_ioend);
  918. count++;
  919. }
  920. } else if (buffer_uptodate(bh)) {
  921. /*
  922. * we got here because the buffer is already mapped.
  923. * That means it must already have extents allocated
  924. * underneath it. Map the extent by reading it.
  925. */
  926. if (type != IO_OVERWRITE) {
  927. type = IO_OVERWRITE;
  928. imap_valid = 0;
  929. }
  930. if (!imap_valid) {
  931. new_ioend = 1;
  932. err = xfs_map_blocks(inode, offset,
  933. &imap, type, nonblocking);
  934. if (err)
  935. goto error;
  936. imap_valid = xfs_imap_valid(inode, &imap,
  937. offset);
  938. }
  939. if (imap_valid) {
  940. all_bh = 1;
  941. lock_buffer(bh);
  942. xfs_add_to_ioend(inode, bh, offset, type,
  943. &ioend, new_ioend);
  944. count++;
  945. }
  946. } else if (PageUptodate(page)) {
  947. ASSERT(buffer_mapped(bh));
  948. imap_valid = 0;
  949. }
  950. if (!iohead)
  951. iohead = ioend;
  952. } while (offset += len, ((bh = bh->b_this_page) != head));
  953. if (uptodate && bh == head)
  954. SetPageUptodate(page);
  955. xfs_start_page_writeback(page, 1, count);
  956. if (ioend && imap_valid) {
  957. xfs_off_t end_index;
  958. end_index = imap.br_startoff + imap.br_blockcount;
  959. /* to bytes */
  960. end_index <<= inode->i_blkbits;
  961. /* to pages */
  962. end_index = (end_index - 1) >> PAGE_CACHE_SHIFT;
  963. /* check against file size */
  964. if (end_index > last_index)
  965. end_index = last_index;
  966. xfs_cluster_write(inode, page->index + 1, &imap, &ioend,
  967. wbc, all_bh, end_index);
  968. }
  969. if (iohead)
  970. xfs_submit_ioend(wbc, iohead);
  971. return 0;
  972. error:
  973. if (iohead)
  974. xfs_cancel_ioend(iohead);
  975. if (err == -EAGAIN)
  976. goto redirty;
  977. xfs_aops_discard_page(page);
  978. ClearPageUptodate(page);
  979. unlock_page(page);
  980. return err;
  981. redirty:
  982. redirty_page_for_writepage(wbc, page);
  983. unlock_page(page);
  984. return 0;
  985. }
  986. STATIC int
  987. xfs_vm_writepages(
  988. struct address_space *mapping,
  989. struct writeback_control *wbc)
  990. {
  991. xfs_iflags_clear(XFS_I(mapping->host), XFS_ITRUNCATED);
  992. return generic_writepages(mapping, wbc);
  993. }
  994. /*
  995. * Called to move a page into cleanable state - and from there
  996. * to be released. The page should already be clean. We always
  997. * have buffer heads in this call.
  998. *
  999. * Returns 1 if the page is ok to release, 0 otherwise.
  1000. */
  1001. STATIC int
  1002. xfs_vm_releasepage(
  1003. struct page *page,
  1004. gfp_t gfp_mask)
  1005. {
  1006. int delalloc, unwritten;
  1007. trace_xfs_releasepage(page->mapping->host, page, 0);
  1008. xfs_count_page_state(page, &delalloc, &unwritten);
  1009. if (WARN_ON(delalloc))
  1010. return 0;
  1011. if (WARN_ON(unwritten))
  1012. return 0;
  1013. return try_to_free_buffers(page);
  1014. }
  1015. STATIC int
  1016. __xfs_get_blocks(
  1017. struct inode *inode,
  1018. sector_t iblock,
  1019. struct buffer_head *bh_result,
  1020. int create,
  1021. int direct)
  1022. {
  1023. struct xfs_inode *ip = XFS_I(inode);
  1024. struct xfs_mount *mp = ip->i_mount;
  1025. xfs_fileoff_t offset_fsb, end_fsb;
  1026. int error = 0;
  1027. int lockmode = 0;
  1028. struct xfs_bmbt_irec imap;
  1029. int nimaps = 1;
  1030. xfs_off_t offset;
  1031. ssize_t size;
  1032. int new = 0;
  1033. if (XFS_FORCED_SHUTDOWN(mp))
  1034. return -XFS_ERROR(EIO);
  1035. offset = (xfs_off_t)iblock << inode->i_blkbits;
  1036. ASSERT(bh_result->b_size >= (1 << inode->i_blkbits));
  1037. size = bh_result->b_size;
  1038. if (!create && direct && offset >= i_size_read(inode))
  1039. return 0;
  1040. if (create) {
  1041. lockmode = XFS_ILOCK_EXCL;
  1042. xfs_ilock(ip, lockmode);
  1043. } else {
  1044. lockmode = xfs_ilock_map_shared(ip);
  1045. }
  1046. ASSERT(offset <= mp->m_maxioffset);
  1047. if (offset + size > mp->m_maxioffset)
  1048. size = mp->m_maxioffset - offset;
  1049. end_fsb = XFS_B_TO_FSB(mp, (xfs_ufsize_t)offset + size);
  1050. offset_fsb = XFS_B_TO_FSBT(mp, offset);
  1051. error = xfs_bmapi(NULL, ip, offset_fsb, end_fsb - offset_fsb,
  1052. XFS_BMAPI_ENTIRE, NULL, 0, &imap, &nimaps, NULL);
  1053. if (error)
  1054. goto out_unlock;
  1055. if (create &&
  1056. (!nimaps ||
  1057. (imap.br_startblock == HOLESTARTBLOCK ||
  1058. imap.br_startblock == DELAYSTARTBLOCK))) {
  1059. if (direct) {
  1060. error = xfs_iomap_write_direct(ip, offset, size,
  1061. &imap, nimaps);
  1062. } else {
  1063. error = xfs_iomap_write_delay(ip, offset, size, &imap);
  1064. }
  1065. if (error)
  1066. goto out_unlock;
  1067. trace_xfs_get_blocks_alloc(ip, offset, size, 0, &imap);
  1068. } else if (nimaps) {
  1069. trace_xfs_get_blocks_found(ip, offset, size, 0, &imap);
  1070. } else {
  1071. trace_xfs_get_blocks_notfound(ip, offset, size);
  1072. goto out_unlock;
  1073. }
  1074. xfs_iunlock(ip, lockmode);
  1075. if (imap.br_startblock != HOLESTARTBLOCK &&
  1076. imap.br_startblock != DELAYSTARTBLOCK) {
  1077. /*
  1078. * For unwritten extents do not report a disk address on
  1079. * the read case (treat as if we're reading into a hole).
  1080. */
  1081. if (create || !ISUNWRITTEN(&imap))
  1082. xfs_map_buffer(inode, bh_result, &imap, offset);
  1083. if (create && ISUNWRITTEN(&imap)) {
  1084. if (direct)
  1085. bh_result->b_private = inode;
  1086. set_buffer_unwritten(bh_result);
  1087. }
  1088. }
  1089. /*
  1090. * If this is a realtime file, data may be on a different device.
  1091. * to that pointed to from the buffer_head b_bdev currently.
  1092. */
  1093. bh_result->b_bdev = xfs_find_bdev_for_inode(inode);
  1094. /*
  1095. * If we previously allocated a block out beyond eof and we are now
  1096. * coming back to use it then we will need to flag it as new even if it
  1097. * has a disk address.
  1098. *
  1099. * With sub-block writes into unwritten extents we also need to mark
  1100. * the buffer as new so that the unwritten parts of the buffer gets
  1101. * correctly zeroed.
  1102. */
  1103. if (create &&
  1104. ((!buffer_mapped(bh_result) && !buffer_uptodate(bh_result)) ||
  1105. (offset >= i_size_read(inode)) ||
  1106. (new || ISUNWRITTEN(&imap))))
  1107. set_buffer_new(bh_result);
  1108. if (imap.br_startblock == DELAYSTARTBLOCK) {
  1109. BUG_ON(direct);
  1110. if (create) {
  1111. set_buffer_uptodate(bh_result);
  1112. set_buffer_mapped(bh_result);
  1113. set_buffer_delay(bh_result);
  1114. }
  1115. }
  1116. /*
  1117. * If this is O_DIRECT or the mpage code calling tell them how large
  1118. * the mapping is, so that we can avoid repeated get_blocks calls.
  1119. */
  1120. if (direct || size > (1 << inode->i_blkbits)) {
  1121. xfs_off_t mapping_size;
  1122. mapping_size = imap.br_startoff + imap.br_blockcount - iblock;
  1123. mapping_size <<= inode->i_blkbits;
  1124. ASSERT(mapping_size > 0);
  1125. if (mapping_size > size)
  1126. mapping_size = size;
  1127. if (mapping_size > LONG_MAX)
  1128. mapping_size = LONG_MAX;
  1129. bh_result->b_size = mapping_size;
  1130. }
  1131. return 0;
  1132. out_unlock:
  1133. xfs_iunlock(ip, lockmode);
  1134. return -error;
  1135. }
  1136. int
  1137. xfs_get_blocks(
  1138. struct inode *inode,
  1139. sector_t iblock,
  1140. struct buffer_head *bh_result,
  1141. int create)
  1142. {
  1143. return __xfs_get_blocks(inode, iblock, bh_result, create, 0);
  1144. }
  1145. STATIC int
  1146. xfs_get_blocks_direct(
  1147. struct inode *inode,
  1148. sector_t iblock,
  1149. struct buffer_head *bh_result,
  1150. int create)
  1151. {
  1152. return __xfs_get_blocks(inode, iblock, bh_result, create, 1);
  1153. }
  1154. /*
  1155. * Complete a direct I/O write request.
  1156. *
  1157. * If the private argument is non-NULL __xfs_get_blocks signals us that we
  1158. * need to issue a transaction to convert the range from unwritten to written
  1159. * extents. In case this is regular synchronous I/O we just call xfs_end_io
  1160. * to do this and we are done. But in case this was a successfull AIO
  1161. * request this handler is called from interrupt context, from which we
  1162. * can't start transactions. In that case offload the I/O completion to
  1163. * the workqueues we also use for buffered I/O completion.
  1164. */
  1165. STATIC void
  1166. xfs_end_io_direct_write(
  1167. struct kiocb *iocb,
  1168. loff_t offset,
  1169. ssize_t size,
  1170. void *private,
  1171. int ret,
  1172. bool is_async)
  1173. {
  1174. struct xfs_ioend *ioend = iocb->private;
  1175. /*
  1176. * blockdev_direct_IO can return an error even after the I/O
  1177. * completion handler was called. Thus we need to protect
  1178. * against double-freeing.
  1179. */
  1180. iocb->private = NULL;
  1181. ioend->io_offset = offset;
  1182. ioend->io_size = size;
  1183. if (private && size > 0)
  1184. ioend->io_type = IO_UNWRITTEN;
  1185. if (is_async) {
  1186. /*
  1187. * If we are converting an unwritten extent we need to delay
  1188. * the AIO completion until after the unwrittent extent
  1189. * conversion has completed, otherwise do it ASAP.
  1190. */
  1191. if (ioend->io_type == IO_UNWRITTEN) {
  1192. ioend->io_iocb = iocb;
  1193. ioend->io_result = ret;
  1194. } else {
  1195. aio_complete(iocb, ret, 0);
  1196. }
  1197. xfs_finish_ioend(ioend);
  1198. } else {
  1199. xfs_finish_ioend_sync(ioend);
  1200. }
  1201. }
  1202. STATIC ssize_t
  1203. xfs_vm_direct_IO(
  1204. int rw,
  1205. struct kiocb *iocb,
  1206. const struct iovec *iov,
  1207. loff_t offset,
  1208. unsigned long nr_segs)
  1209. {
  1210. struct inode *inode = iocb->ki_filp->f_mapping->host;
  1211. struct block_device *bdev = xfs_find_bdev_for_inode(inode);
  1212. ssize_t ret;
  1213. if (rw & WRITE) {
  1214. iocb->private = xfs_alloc_ioend(inode, IO_DIRECT);
  1215. ret = __blockdev_direct_IO(rw, iocb, inode, bdev, iov,
  1216. offset, nr_segs,
  1217. xfs_get_blocks_direct,
  1218. xfs_end_io_direct_write, NULL, 0);
  1219. if (ret != -EIOCBQUEUED && iocb->private)
  1220. xfs_destroy_ioend(iocb->private);
  1221. } else {
  1222. ret = __blockdev_direct_IO(rw, iocb, inode, bdev, iov,
  1223. offset, nr_segs,
  1224. xfs_get_blocks_direct,
  1225. NULL, NULL, 0);
  1226. }
  1227. return ret;
  1228. }
  1229. STATIC void
  1230. xfs_vm_write_failed(
  1231. struct address_space *mapping,
  1232. loff_t to)
  1233. {
  1234. struct inode *inode = mapping->host;
  1235. if (to > inode->i_size) {
  1236. /*
  1237. * punch out the delalloc blocks we have already allocated. We
  1238. * don't call xfs_setattr() to do this as we may be in the
  1239. * middle of a multi-iovec write and so the vfs inode->i_size
  1240. * will not match the xfs ip->i_size and so it will zero too
  1241. * much. Hence we jus truncate the page cache to zero what is
  1242. * necessary and punch the delalloc blocks directly.
  1243. */
  1244. struct xfs_inode *ip = XFS_I(inode);
  1245. xfs_fileoff_t start_fsb;
  1246. xfs_fileoff_t end_fsb;
  1247. int error;
  1248. truncate_pagecache(inode, to, inode->i_size);
  1249. /*
  1250. * Check if there are any blocks that are outside of i_size
  1251. * that need to be trimmed back.
  1252. */
  1253. start_fsb = XFS_B_TO_FSB(ip->i_mount, inode->i_size) + 1;
  1254. end_fsb = XFS_B_TO_FSB(ip->i_mount, to);
  1255. if (end_fsb <= start_fsb)
  1256. return;
  1257. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1258. error = xfs_bmap_punch_delalloc_range(ip, start_fsb,
  1259. end_fsb - start_fsb);
  1260. if (error) {
  1261. /* something screwed, just bail */
  1262. if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
  1263. xfs_fs_cmn_err(CE_ALERT, ip->i_mount,
  1264. "xfs_vm_write_failed: unable to clean up ino %lld",
  1265. ip->i_ino);
  1266. }
  1267. }
  1268. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  1269. }
  1270. }
  1271. STATIC int
  1272. xfs_vm_write_begin(
  1273. struct file *file,
  1274. struct address_space *mapping,
  1275. loff_t pos,
  1276. unsigned len,
  1277. unsigned flags,
  1278. struct page **pagep,
  1279. void **fsdata)
  1280. {
  1281. int ret;
  1282. ret = block_write_begin(mapping, pos, len, flags | AOP_FLAG_NOFS,
  1283. pagep, xfs_get_blocks);
  1284. if (unlikely(ret))
  1285. xfs_vm_write_failed(mapping, pos + len);
  1286. return ret;
  1287. }
  1288. STATIC int
  1289. xfs_vm_write_end(
  1290. struct file *file,
  1291. struct address_space *mapping,
  1292. loff_t pos,
  1293. unsigned len,
  1294. unsigned copied,
  1295. struct page *page,
  1296. void *fsdata)
  1297. {
  1298. int ret;
  1299. ret = generic_write_end(file, mapping, pos, len, copied, page, fsdata);
  1300. if (unlikely(ret < len))
  1301. xfs_vm_write_failed(mapping, pos + len);
  1302. return ret;
  1303. }
  1304. STATIC sector_t
  1305. xfs_vm_bmap(
  1306. struct address_space *mapping,
  1307. sector_t block)
  1308. {
  1309. struct inode *inode = (struct inode *)mapping->host;
  1310. struct xfs_inode *ip = XFS_I(inode);
  1311. trace_xfs_vm_bmap(XFS_I(inode));
  1312. xfs_ilock(ip, XFS_IOLOCK_SHARED);
  1313. xfs_flush_pages(ip, (xfs_off_t)0, -1, 0, FI_REMAPF);
  1314. xfs_iunlock(ip, XFS_IOLOCK_SHARED);
  1315. return generic_block_bmap(mapping, block, xfs_get_blocks);
  1316. }
  1317. STATIC int
  1318. xfs_vm_readpage(
  1319. struct file *unused,
  1320. struct page *page)
  1321. {
  1322. return mpage_readpage(page, xfs_get_blocks);
  1323. }
  1324. STATIC int
  1325. xfs_vm_readpages(
  1326. struct file *unused,
  1327. struct address_space *mapping,
  1328. struct list_head *pages,
  1329. unsigned nr_pages)
  1330. {
  1331. return mpage_readpages(mapping, pages, nr_pages, xfs_get_blocks);
  1332. }
  1333. const struct address_space_operations xfs_address_space_operations = {
  1334. .readpage = xfs_vm_readpage,
  1335. .readpages = xfs_vm_readpages,
  1336. .writepage = xfs_vm_writepage,
  1337. .writepages = xfs_vm_writepages,
  1338. .sync_page = block_sync_page,
  1339. .releasepage = xfs_vm_releasepage,
  1340. .invalidatepage = xfs_vm_invalidatepage,
  1341. .write_begin = xfs_vm_write_begin,
  1342. .write_end = xfs_vm_write_end,
  1343. .bmap = xfs_vm_bmap,
  1344. .direct_IO = xfs_vm_direct_IO,
  1345. .migratepage = buffer_migrate_page,
  1346. .is_partially_uptodate = block_is_partially_uptodate,
  1347. .error_remove_page = generic_error_remove_page,
  1348. };