xfs_aops.c 38 KB

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