xfs_aops.c 38 KB

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