xfs_aops.c 36 KB

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