xfs_aops.c 39 KB

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