xfs_aops.c 38 KB

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