xfs_aops.c 38 KB

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