xfs_aops.c 43 KB

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