xfs_aops.c 38 KB

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