xfs_aops.c 42 KB

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