xfs_aops.c 36 KB

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