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