xfs_aops.c 39 KB

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