xfs_aops.c 42 KB

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