xfs_aops.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734
  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/gfp.h>
  44. #include <linux/mpage.h>
  45. #include <linux/pagevec.h>
  46. #include <linux/writeback.h>
  47. /*
  48. * Types of I/O for bmap clustering and I/O completion tracking.
  49. */
  50. enum {
  51. IO_READ, /* mapping for a read */
  52. IO_DELAY, /* mapping covers delalloc region */
  53. IO_UNWRITTEN, /* mapping covers allocated but uninitialized data */
  54. IO_NEW /* just allocated */
  55. };
  56. /*
  57. * Prime number of hash buckets since address is used as the key.
  58. */
  59. #define NVSYNC 37
  60. #define to_ioend_wq(v) (&xfs_ioend_wq[((unsigned long)v) % NVSYNC])
  61. static wait_queue_head_t xfs_ioend_wq[NVSYNC];
  62. void __init
  63. xfs_ioend_init(void)
  64. {
  65. int i;
  66. for (i = 0; i < NVSYNC; i++)
  67. init_waitqueue_head(&xfs_ioend_wq[i]);
  68. }
  69. void
  70. xfs_ioend_wait(
  71. xfs_inode_t *ip)
  72. {
  73. wait_queue_head_t *wq = to_ioend_wq(ip);
  74. wait_event(*wq, (atomic_read(&ip->i_iocount) == 0));
  75. }
  76. STATIC void
  77. xfs_ioend_wake(
  78. xfs_inode_t *ip)
  79. {
  80. if (atomic_dec_and_test(&ip->i_iocount))
  81. wake_up(to_ioend_wq(ip));
  82. }
  83. void
  84. xfs_count_page_state(
  85. struct page *page,
  86. int *delalloc,
  87. int *unmapped,
  88. int *unwritten)
  89. {
  90. struct buffer_head *bh, *head;
  91. *delalloc = *unmapped = *unwritten = 0;
  92. bh = head = page_buffers(page);
  93. do {
  94. if (buffer_uptodate(bh) && !buffer_mapped(bh))
  95. (*unmapped) = 1;
  96. else if (buffer_unwritten(bh))
  97. (*unwritten) = 1;
  98. else if (buffer_delay(bh))
  99. (*delalloc) = 1;
  100. } while ((bh = bh->b_this_page) != head);
  101. }
  102. STATIC struct block_device *
  103. xfs_find_bdev_for_inode(
  104. struct inode *inode)
  105. {
  106. struct xfs_inode *ip = XFS_I(inode);
  107. struct xfs_mount *mp = ip->i_mount;
  108. if (XFS_IS_REALTIME_INODE(ip))
  109. return mp->m_rtdev_targp->bt_bdev;
  110. else
  111. return mp->m_ddev_targp->bt_bdev;
  112. }
  113. /*
  114. * We're now finished for good with this ioend structure.
  115. * Update the page state via the associated buffer_heads,
  116. * release holds on the inode and bio, and finally free
  117. * up memory. Do not use the ioend after this.
  118. */
  119. STATIC void
  120. xfs_destroy_ioend(
  121. xfs_ioend_t *ioend)
  122. {
  123. struct buffer_head *bh, *next;
  124. struct xfs_inode *ip = XFS_I(ioend->io_inode);
  125. for (bh = ioend->io_buffer_head; bh; bh = next) {
  126. next = bh->b_private;
  127. bh->b_end_io(bh, !ioend->io_error);
  128. }
  129. /*
  130. * Volume managers supporting multiple paths can send back ENODEV
  131. * when the final path disappears. In this case continuing to fill
  132. * the page cache with dirty data which cannot be written out is
  133. * evil, so prevent that.
  134. */
  135. if (unlikely(ioend->io_error == -ENODEV)) {
  136. xfs_do_force_shutdown(ip->i_mount, SHUTDOWN_DEVICE_REQ,
  137. __FILE__, __LINE__);
  138. }
  139. xfs_ioend_wake(ip);
  140. mempool_free(ioend, xfs_ioend_pool);
  141. }
  142. /*
  143. * If the end of the current ioend is beyond the current EOF,
  144. * return the new EOF value, otherwise zero.
  145. */
  146. STATIC xfs_fsize_t
  147. xfs_ioend_new_eof(
  148. xfs_ioend_t *ioend)
  149. {
  150. xfs_inode_t *ip = XFS_I(ioend->io_inode);
  151. xfs_fsize_t isize;
  152. xfs_fsize_t bsize;
  153. bsize = ioend->io_offset + ioend->io_size;
  154. isize = MAX(ip->i_size, ip->i_new_size);
  155. isize = MIN(isize, bsize);
  156. return isize > ip->i_d.di_size ? isize : 0;
  157. }
  158. /*
  159. * Update on-disk file size now that data has been written to disk. The
  160. * current in-memory file size is i_size. If a write is beyond eof i_new_size
  161. * will be the intended file size until i_size is updated. If this write does
  162. * not extend all the way to the valid file size then restrict this update to
  163. * the end of the write.
  164. *
  165. * This function does not block as blocking on the inode lock in IO completion
  166. * can lead to IO completion order dependency deadlocks.. If it can't get the
  167. * inode ilock it will return EAGAIN. Callers must handle this.
  168. */
  169. STATIC int
  170. xfs_setfilesize(
  171. xfs_ioend_t *ioend)
  172. {
  173. xfs_inode_t *ip = XFS_I(ioend->io_inode);
  174. xfs_fsize_t isize;
  175. ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFREG);
  176. ASSERT(ioend->io_type != IO_READ);
  177. if (unlikely(ioend->io_error))
  178. return 0;
  179. if (!xfs_ilock_nowait(ip, XFS_ILOCK_EXCL))
  180. return EAGAIN;
  181. isize = xfs_ioend_new_eof(ioend);
  182. if (isize) {
  183. ip->i_d.di_size = isize;
  184. xfs_mark_inode_dirty(ip);
  185. }
  186. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  187. return 0;
  188. }
  189. /*
  190. * Schedule IO completion handling on a xfsdatad if this was
  191. * the final hold on this ioend. If we are asked to wait,
  192. * flush the workqueue.
  193. */
  194. STATIC void
  195. xfs_finish_ioend(
  196. xfs_ioend_t *ioend,
  197. int wait)
  198. {
  199. if (atomic_dec_and_test(&ioend->io_remaining)) {
  200. struct workqueue_struct *wq;
  201. wq = (ioend->io_type == IO_UNWRITTEN) ?
  202. xfsconvertd_workqueue : xfsdatad_workqueue;
  203. queue_work(wq, &ioend->io_work);
  204. if (wait)
  205. flush_workqueue(wq);
  206. }
  207. }
  208. /*
  209. * IO write completion.
  210. */
  211. STATIC void
  212. xfs_end_io(
  213. struct work_struct *work)
  214. {
  215. xfs_ioend_t *ioend = container_of(work, xfs_ioend_t, io_work);
  216. struct xfs_inode *ip = XFS_I(ioend->io_inode);
  217. int error = 0;
  218. /*
  219. * For unwritten extents we need to issue transactions to convert a
  220. * range to normal written extens after the data I/O has finished.
  221. */
  222. if (ioend->io_type == IO_UNWRITTEN &&
  223. likely(!ioend->io_error && !XFS_FORCED_SHUTDOWN(ip->i_mount))) {
  224. error = xfs_iomap_write_unwritten(ip, ioend->io_offset,
  225. ioend->io_size);
  226. if (error)
  227. ioend->io_error = error;
  228. }
  229. /*
  230. * We might have to update the on-disk file size after extending
  231. * writes.
  232. */
  233. if (ioend->io_type != IO_READ) {
  234. error = xfs_setfilesize(ioend);
  235. ASSERT(!error || error == EAGAIN);
  236. }
  237. /*
  238. * If we didn't complete processing of the ioend, requeue it to the
  239. * tail of the workqueue for another attempt later. Otherwise destroy
  240. * it.
  241. */
  242. if (error == EAGAIN) {
  243. atomic_inc(&ioend->io_remaining);
  244. xfs_finish_ioend(ioend, 0);
  245. /* ensure we don't spin on blocked ioends */
  246. delay(1);
  247. } else
  248. xfs_destroy_ioend(ioend);
  249. }
  250. /*
  251. * Allocate and initialise an IO completion structure.
  252. * We need to track unwritten extent write completion here initially.
  253. * We'll need to extend this for updating the ondisk inode size later
  254. * (vs. incore size).
  255. */
  256. STATIC xfs_ioend_t *
  257. xfs_alloc_ioend(
  258. struct inode *inode,
  259. unsigned int type)
  260. {
  261. xfs_ioend_t *ioend;
  262. ioend = mempool_alloc(xfs_ioend_pool, GFP_NOFS);
  263. /*
  264. * Set the count to 1 initially, which will prevent an I/O
  265. * completion callback from happening before we have started
  266. * all the I/O from calling the completion routine too early.
  267. */
  268. atomic_set(&ioend->io_remaining, 1);
  269. ioend->io_error = 0;
  270. ioend->io_list = NULL;
  271. ioend->io_type = type;
  272. ioend->io_inode = inode;
  273. ioend->io_buffer_head = NULL;
  274. ioend->io_buffer_tail = NULL;
  275. atomic_inc(&XFS_I(ioend->io_inode)->i_iocount);
  276. ioend->io_offset = 0;
  277. ioend->io_size = 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, 0);
  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, 0);
  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. int mapped)
  544. {
  545. int ret = 0;
  546. if (PageWriteback(page))
  547. return 0;
  548. if (page->mapping && PageDirty(page)) {
  549. if (page_has_buffers(page)) {
  550. struct buffer_head *bh, *head;
  551. bh = head = page_buffers(page);
  552. do {
  553. if (!buffer_uptodate(bh))
  554. break;
  555. if (mapped != buffer_mapped(bh))
  556. break;
  557. ret += bh->b_size;
  558. if (ret >= pg_offset)
  559. break;
  560. } while ((bh = bh->b_this_page) != head);
  561. } else
  562. ret = mapped ? 0 : PAGE_CACHE_SIZE;
  563. }
  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. int mapped)
  573. {
  574. struct pagevec pvec;
  575. pgoff_t tindex, tlast, tloff;
  576. size_t total = 0;
  577. int done = 0, i;
  578. /* First sum forwards in this page */
  579. do {
  580. if (!buffer_uptodate(bh) || (mapped != buffer_mapped(bh)))
  581. return total;
  582. total += bh->b_size;
  583. } while ((bh = bh->b_this_page) != head);
  584. /* if we reached the end of the page, sum forwards in following pages */
  585. tlast = i_size_read(inode) >> PAGE_CACHE_SHIFT;
  586. tindex = startpage->index + 1;
  587. /* Prune this back to avoid pathological behavior */
  588. tloff = min(tlast, startpage->index + 64);
  589. pagevec_init(&pvec, 0);
  590. while (!done && tindex <= tloff) {
  591. unsigned len = min_t(pgoff_t, PAGEVEC_SIZE, tlast - tindex + 1);
  592. if (!pagevec_lookup(&pvec, inode->i_mapping, tindex, len))
  593. break;
  594. for (i = 0; i < pagevec_count(&pvec); i++) {
  595. struct page *page = pvec.pages[i];
  596. size_t pg_offset, pg_len = 0;
  597. if (tindex == tlast) {
  598. pg_offset =
  599. i_size_read(inode) & (PAGE_CACHE_SIZE - 1);
  600. if (!pg_offset) {
  601. done = 1;
  602. break;
  603. }
  604. } else
  605. pg_offset = PAGE_CACHE_SIZE;
  606. if (page->index == tindex && trylock_page(page)) {
  607. pg_len = xfs_probe_page(page, pg_offset, mapped);
  608. unlock_page(page);
  609. }
  610. if (!pg_len) {
  611. done = 1;
  612. break;
  613. }
  614. total += pg_len;
  615. tindex++;
  616. }
  617. pagevec_release(&pvec);
  618. cond_resched();
  619. }
  620. return total;
  621. }
  622. /*
  623. * Test if a given page is suitable for writing as part of an unwritten
  624. * or delayed allocate extent.
  625. */
  626. STATIC int
  627. xfs_is_delayed_page(
  628. struct page *page,
  629. unsigned int type)
  630. {
  631. if (PageWriteback(page))
  632. return 0;
  633. if (page->mapping && page_has_buffers(page)) {
  634. struct buffer_head *bh, *head;
  635. int acceptable = 0;
  636. bh = head = page_buffers(page);
  637. do {
  638. if (buffer_unwritten(bh))
  639. acceptable = (type == IO_UNWRITTEN);
  640. else if (buffer_delay(bh))
  641. acceptable = (type == IO_DELAY);
  642. else if (buffer_dirty(bh) && buffer_mapped(bh))
  643. acceptable = (type == IO_NEW);
  644. else
  645. break;
  646. } while ((bh = bh->b_this_page) != head);
  647. if (acceptable)
  648. return 1;
  649. }
  650. return 0;
  651. }
  652. /*
  653. * Allocate & map buffers for page given the extent map. Write it out.
  654. * except for the original page of a writepage, this is called on
  655. * delalloc/unwritten pages only, for the original page it is possible
  656. * that the page has no mapping at all.
  657. */
  658. STATIC int
  659. xfs_convert_page(
  660. struct inode *inode,
  661. struct page *page,
  662. loff_t tindex,
  663. struct xfs_bmbt_irec *imap,
  664. xfs_ioend_t **ioendp,
  665. struct writeback_control *wbc,
  666. int startio,
  667. int all_bh)
  668. {
  669. struct buffer_head *bh, *head;
  670. xfs_off_t end_offset;
  671. unsigned long p_offset;
  672. unsigned int type;
  673. int len, page_dirty;
  674. int count = 0, done = 0, uptodate = 1;
  675. xfs_off_t offset = page_offset(page);
  676. if (page->index != tindex)
  677. goto fail;
  678. if (!trylock_page(page))
  679. goto fail;
  680. if (PageWriteback(page))
  681. goto fail_unlock_page;
  682. if (page->mapping != inode->i_mapping)
  683. goto fail_unlock_page;
  684. if (!xfs_is_delayed_page(page, (*ioendp)->io_type))
  685. goto fail_unlock_page;
  686. /*
  687. * page_dirty is initially a count of buffers on the page before
  688. * EOF and is decremented as we move each into a cleanable state.
  689. *
  690. * Derivation:
  691. *
  692. * End offset is the highest offset that this page should represent.
  693. * If we are on the last page, (end_offset & (PAGE_CACHE_SIZE - 1))
  694. * will evaluate non-zero and be less than PAGE_CACHE_SIZE and
  695. * hence give us the correct page_dirty count. On any other page,
  696. * it will be zero and in that case we need page_dirty to be the
  697. * count of buffers on the page.
  698. */
  699. end_offset = min_t(unsigned long long,
  700. (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT,
  701. i_size_read(inode));
  702. len = 1 << inode->i_blkbits;
  703. p_offset = min_t(unsigned long, end_offset & (PAGE_CACHE_SIZE - 1),
  704. PAGE_CACHE_SIZE);
  705. p_offset = p_offset ? roundup(p_offset, len) : PAGE_CACHE_SIZE;
  706. page_dirty = p_offset / len;
  707. bh = head = page_buffers(page);
  708. do {
  709. if (offset >= end_offset)
  710. break;
  711. if (!buffer_uptodate(bh))
  712. uptodate = 0;
  713. if (!(PageUptodate(page) || buffer_uptodate(bh))) {
  714. done = 1;
  715. continue;
  716. }
  717. if (buffer_unwritten(bh) || buffer_delay(bh)) {
  718. if (buffer_unwritten(bh))
  719. type = IO_UNWRITTEN;
  720. else
  721. type = IO_DELAY;
  722. if (!xfs_imap_valid(inode, imap, offset)) {
  723. done = 1;
  724. continue;
  725. }
  726. ASSERT(imap->br_startblock != HOLESTARTBLOCK);
  727. ASSERT(imap->br_startblock != DELAYSTARTBLOCK);
  728. xfs_map_at_offset(inode, bh, imap, offset);
  729. if (startio) {
  730. xfs_add_to_ioend(inode, bh, offset,
  731. type, ioendp, done);
  732. } else {
  733. set_buffer_dirty(bh);
  734. unlock_buffer(bh);
  735. mark_buffer_dirty(bh);
  736. }
  737. page_dirty--;
  738. count++;
  739. } else {
  740. type = IO_NEW;
  741. if (buffer_mapped(bh) && all_bh && startio) {
  742. lock_buffer(bh);
  743. xfs_add_to_ioend(inode, bh, offset,
  744. type, ioendp, done);
  745. count++;
  746. page_dirty--;
  747. } else {
  748. done = 1;
  749. }
  750. }
  751. } while (offset += len, (bh = bh->b_this_page) != head);
  752. if (uptodate && bh == head)
  753. SetPageUptodate(page);
  754. if (startio) {
  755. if (count) {
  756. wbc->nr_to_write--;
  757. if (wbc->nr_to_write <= 0)
  758. done = 1;
  759. }
  760. xfs_start_page_writeback(page, !page_dirty, count);
  761. }
  762. return done;
  763. fail_unlock_page:
  764. unlock_page(page);
  765. fail:
  766. return 1;
  767. }
  768. /*
  769. * Convert & write out a cluster of pages in the same extent as defined
  770. * by mp and following the start page.
  771. */
  772. STATIC void
  773. xfs_cluster_write(
  774. struct inode *inode,
  775. pgoff_t tindex,
  776. struct xfs_bmbt_irec *imap,
  777. xfs_ioend_t **ioendp,
  778. struct writeback_control *wbc,
  779. int startio,
  780. int all_bh,
  781. pgoff_t tlast)
  782. {
  783. struct pagevec pvec;
  784. int done = 0, i;
  785. pagevec_init(&pvec, 0);
  786. while (!done && tindex <= tlast) {
  787. unsigned len = min_t(pgoff_t, PAGEVEC_SIZE, tlast - tindex + 1);
  788. if (!pagevec_lookup(&pvec, inode->i_mapping, tindex, len))
  789. break;
  790. for (i = 0; i < pagevec_count(&pvec); i++) {
  791. done = xfs_convert_page(inode, pvec.pages[i], tindex++,
  792. imap, ioendp, wbc, startio, all_bh);
  793. if (done)
  794. break;
  795. }
  796. pagevec_release(&pvec);
  797. cond_resched();
  798. }
  799. }
  800. STATIC void
  801. xfs_vm_invalidatepage(
  802. struct page *page,
  803. unsigned long offset)
  804. {
  805. trace_xfs_invalidatepage(page->mapping->host, page, offset);
  806. block_invalidatepage(page, offset);
  807. }
  808. /*
  809. * If the page has delalloc buffers on it, we need to punch them out before we
  810. * invalidate the page. If we don't, we leave a stale delalloc mapping on the
  811. * inode that can trip a BUG() in xfs_get_blocks() later on if a direct IO read
  812. * is done on that same region - the delalloc extent is returned when none is
  813. * supposed to be there.
  814. *
  815. * We prevent this by truncating away the delalloc regions on the page before
  816. * invalidating it. Because they are delalloc, we can do this without needing a
  817. * transaction. Indeed - if we get ENOSPC errors, we have to be able to do this
  818. * truncation without a transaction as there is no space left for block
  819. * reservation (typically why we see a ENOSPC in writeback).
  820. *
  821. * This is not a performance critical path, so for now just do the punching a
  822. * buffer head at a time.
  823. */
  824. STATIC void
  825. xfs_aops_discard_page(
  826. struct page *page)
  827. {
  828. struct inode *inode = page->mapping->host;
  829. struct xfs_inode *ip = XFS_I(inode);
  830. struct buffer_head *bh, *head;
  831. loff_t offset = page_offset(page);
  832. ssize_t len = 1 << inode->i_blkbits;
  833. if (!xfs_is_delayed_page(page, IO_DELAY))
  834. goto out_invalidate;
  835. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  836. goto out_invalidate;
  837. xfs_fs_cmn_err(CE_ALERT, ip->i_mount,
  838. "page discard on page %p, inode 0x%llx, offset %llu.",
  839. page, ip->i_ino, offset);
  840. xfs_ilock(ip, XFS_ILOCK_EXCL);
  841. bh = head = page_buffers(page);
  842. do {
  843. int done;
  844. xfs_fileoff_t offset_fsb;
  845. xfs_bmbt_irec_t imap;
  846. int nimaps = 1;
  847. int error;
  848. xfs_fsblock_t firstblock;
  849. xfs_bmap_free_t flist;
  850. if (!buffer_delay(bh))
  851. goto next_buffer;
  852. offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
  853. /*
  854. * Map the range first and check that it is a delalloc extent
  855. * before trying to unmap the range. Otherwise we will be
  856. * trying to remove a real extent (which requires a
  857. * transaction) or a hole, which is probably a bad idea...
  858. */
  859. error = xfs_bmapi(NULL, ip, offset_fsb, 1,
  860. XFS_BMAPI_ENTIRE, NULL, 0, &imap,
  861. &nimaps, NULL, NULL);
  862. if (error) {
  863. /* something screwed, just bail */
  864. if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
  865. xfs_fs_cmn_err(CE_ALERT, ip->i_mount,
  866. "page discard failed delalloc mapping lookup.");
  867. }
  868. break;
  869. }
  870. if (!nimaps) {
  871. /* nothing there */
  872. goto next_buffer;
  873. }
  874. if (imap.br_startblock != DELAYSTARTBLOCK) {
  875. /* been converted, ignore */
  876. goto next_buffer;
  877. }
  878. WARN_ON(imap.br_blockcount == 0);
  879. /*
  880. * Note: while we initialise the firstblock/flist pair, they
  881. * should never be used because blocks should never be
  882. * allocated or freed for a delalloc extent and hence we need
  883. * don't cancel or finish them after the xfs_bunmapi() call.
  884. */
  885. xfs_bmap_init(&flist, &firstblock);
  886. error = xfs_bunmapi(NULL, ip, offset_fsb, 1, 0, 1, &firstblock,
  887. &flist, NULL, &done);
  888. ASSERT(!flist.xbf_count && !flist.xbf_first);
  889. if (error) {
  890. /* something screwed, just bail */
  891. if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
  892. xfs_fs_cmn_err(CE_ALERT, ip->i_mount,
  893. "page discard unable to remove delalloc mapping.");
  894. }
  895. break;
  896. }
  897. next_buffer:
  898. offset += len;
  899. } while ((bh = bh->b_this_page) != head);
  900. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  901. out_invalidate:
  902. xfs_vm_invalidatepage(page, 0);
  903. return;
  904. }
  905. /*
  906. * Calling this without startio set means we are being asked to make a dirty
  907. * page ready for freeing it's buffers. When called with startio set then
  908. * we are coming from writepage.
  909. *
  910. * When called with startio set it is important that we write the WHOLE
  911. * page if possible.
  912. * The bh->b_state's cannot know if any of the blocks or which block for
  913. * that matter are dirty due to mmap writes, and therefore bh uptodate is
  914. * only valid if the page itself isn't completely uptodate. Some layers
  915. * may clear the page dirty flag prior to calling write page, under the
  916. * assumption the entire page will be written out; by not writing out the
  917. * whole page the page can be reused before all valid dirty data is
  918. * written out. Note: in the case of a page that has been dirty'd by
  919. * mapwrite and but partially setup by block_prepare_write the
  920. * bh->b_states's will not agree and only ones setup by BPW/BCW will have
  921. * valid state, thus the whole page must be written out thing.
  922. */
  923. STATIC int
  924. xfs_page_state_convert(
  925. struct inode *inode,
  926. struct page *page,
  927. struct writeback_control *wbc,
  928. int startio,
  929. int unmapped) /* also implies page uptodate */
  930. {
  931. struct buffer_head *bh, *head;
  932. struct xfs_bmbt_irec imap;
  933. xfs_ioend_t *ioend = NULL, *iohead = NULL;
  934. loff_t offset;
  935. unsigned long p_offset = 0;
  936. unsigned int type;
  937. __uint64_t end_offset;
  938. pgoff_t end_index, last_index;
  939. ssize_t size, len;
  940. int flags, err, imap_valid = 0, uptodate = 1;
  941. int page_dirty, count = 0;
  942. int trylock = 0;
  943. int all_bh = unmapped;
  944. if (startio) {
  945. if (wbc->sync_mode == WB_SYNC_NONE && wbc->nonblocking)
  946. trylock |= BMAPI_TRYLOCK;
  947. }
  948. /* Is this page beyond the end of the file? */
  949. offset = i_size_read(inode);
  950. end_index = offset >> PAGE_CACHE_SHIFT;
  951. last_index = (offset - 1) >> PAGE_CACHE_SHIFT;
  952. if (page->index >= end_index) {
  953. if ((page->index >= end_index + 1) ||
  954. !(i_size_read(inode) & (PAGE_CACHE_SIZE - 1))) {
  955. if (startio)
  956. unlock_page(page);
  957. return 0;
  958. }
  959. }
  960. /*
  961. * page_dirty is initially a count of buffers on the page before
  962. * EOF and is decremented as we move each into a cleanable state.
  963. *
  964. * Derivation:
  965. *
  966. * End offset is the highest offset that this page should represent.
  967. * If we are on the last page, (end_offset & (PAGE_CACHE_SIZE - 1))
  968. * will evaluate non-zero and be less than PAGE_CACHE_SIZE and
  969. * hence give us the correct page_dirty count. On any other page,
  970. * it will be zero and in that case we need page_dirty to be the
  971. * count of buffers on the page.
  972. */
  973. end_offset = min_t(unsigned long long,
  974. (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT, offset);
  975. len = 1 << inode->i_blkbits;
  976. p_offset = min_t(unsigned long, end_offset & (PAGE_CACHE_SIZE - 1),
  977. PAGE_CACHE_SIZE);
  978. p_offset = p_offset ? roundup(p_offset, len) : PAGE_CACHE_SIZE;
  979. page_dirty = p_offset / len;
  980. bh = head = page_buffers(page);
  981. offset = page_offset(page);
  982. flags = BMAPI_READ;
  983. type = IO_NEW;
  984. /* TODO: cleanup count and page_dirty */
  985. do {
  986. if (offset >= end_offset)
  987. break;
  988. if (!buffer_uptodate(bh))
  989. uptodate = 0;
  990. if (!(PageUptodate(page) || buffer_uptodate(bh)) && !startio) {
  991. /*
  992. * the iomap is actually still valid, but the ioend
  993. * isn't. shouldn't happen too often.
  994. */
  995. imap_valid = 0;
  996. continue;
  997. }
  998. if (imap_valid)
  999. imap_valid = xfs_imap_valid(inode, &imap, offset);
  1000. /*
  1001. * First case, map an unwritten extent and prepare for
  1002. * extent state conversion transaction on completion.
  1003. *
  1004. * Second case, allocate space for a delalloc buffer.
  1005. * We can return EAGAIN here in the release page case.
  1006. *
  1007. * Third case, an unmapped buffer was found, and we are
  1008. * in a path where we need to write the whole page out.
  1009. */
  1010. if (buffer_unwritten(bh) || buffer_delay(bh) ||
  1011. ((buffer_uptodate(bh) || PageUptodate(page)) &&
  1012. !buffer_mapped(bh) && (unmapped || startio))) {
  1013. int new_ioend = 0;
  1014. /*
  1015. * Make sure we don't use a read-only iomap
  1016. */
  1017. if (flags == BMAPI_READ)
  1018. imap_valid = 0;
  1019. if (buffer_unwritten(bh)) {
  1020. type = IO_UNWRITTEN;
  1021. flags = BMAPI_WRITE | BMAPI_IGNSTATE;
  1022. } else if (buffer_delay(bh)) {
  1023. type = IO_DELAY;
  1024. flags = BMAPI_ALLOCATE | trylock;
  1025. } else {
  1026. type = IO_NEW;
  1027. flags = BMAPI_WRITE | BMAPI_MMAP;
  1028. }
  1029. if (!imap_valid) {
  1030. /*
  1031. * if we didn't have a valid mapping then we
  1032. * need to ensure that we put the new mapping
  1033. * in a new ioend structure. This needs to be
  1034. * done to ensure that the ioends correctly
  1035. * reflect the block mappings at io completion
  1036. * for unwritten extent conversion.
  1037. */
  1038. new_ioend = 1;
  1039. if (type == IO_NEW) {
  1040. size = xfs_probe_cluster(inode,
  1041. page, bh, head, 0);
  1042. } else {
  1043. size = len;
  1044. }
  1045. err = xfs_map_blocks(inode, offset, size,
  1046. &imap, flags);
  1047. if (err)
  1048. goto error;
  1049. imap_valid = xfs_imap_valid(inode, &imap,
  1050. offset);
  1051. }
  1052. if (imap_valid) {
  1053. xfs_map_at_offset(inode, bh, &imap, offset);
  1054. if (startio) {
  1055. xfs_add_to_ioend(inode, bh, offset,
  1056. type, &ioend,
  1057. new_ioend);
  1058. } else {
  1059. set_buffer_dirty(bh);
  1060. unlock_buffer(bh);
  1061. mark_buffer_dirty(bh);
  1062. }
  1063. page_dirty--;
  1064. count++;
  1065. }
  1066. } else if (buffer_uptodate(bh) && startio) {
  1067. /*
  1068. * we got here because the buffer is already mapped.
  1069. * That means it must already have extents allocated
  1070. * underneath it. Map the extent by reading it.
  1071. */
  1072. if (!imap_valid || flags != BMAPI_READ) {
  1073. flags = BMAPI_READ;
  1074. size = xfs_probe_cluster(inode, page, bh,
  1075. head, 1);
  1076. err = xfs_map_blocks(inode, offset, size,
  1077. &imap, flags);
  1078. if (err)
  1079. goto error;
  1080. imap_valid = xfs_imap_valid(inode, &imap,
  1081. offset);
  1082. }
  1083. /*
  1084. * We set the type to IO_NEW in case we are doing a
  1085. * small write at EOF that is extending the file but
  1086. * without needing an allocation. We need to update the
  1087. * file size on I/O completion in this case so it is
  1088. * the same case as having just allocated a new extent
  1089. * that we are writing into for the first time.
  1090. */
  1091. type = IO_NEW;
  1092. if (trylock_buffer(bh)) {
  1093. ASSERT(buffer_mapped(bh));
  1094. if (imap_valid)
  1095. all_bh = 1;
  1096. xfs_add_to_ioend(inode, bh, offset, type,
  1097. &ioend, !imap_valid);
  1098. page_dirty--;
  1099. count++;
  1100. } else {
  1101. imap_valid = 0;
  1102. }
  1103. } else if ((buffer_uptodate(bh) || PageUptodate(page)) &&
  1104. (unmapped || startio)) {
  1105. imap_valid = 0;
  1106. }
  1107. if (!iohead)
  1108. iohead = ioend;
  1109. } while (offset += len, ((bh = bh->b_this_page) != head));
  1110. if (uptodate && bh == head)
  1111. SetPageUptodate(page);
  1112. if (startio)
  1113. xfs_start_page_writeback(page, 1, count);
  1114. if (ioend && imap_valid) {
  1115. xfs_off_t end_index;
  1116. end_index = imap.br_startoff + imap.br_blockcount;
  1117. /* to bytes */
  1118. end_index <<= inode->i_blkbits;
  1119. /* to pages */
  1120. end_index = (end_index - 1) >> PAGE_CACHE_SHIFT;
  1121. /* check against file size */
  1122. if (end_index > last_index)
  1123. end_index = last_index;
  1124. xfs_cluster_write(inode, page->index + 1, &imap, &ioend,
  1125. wbc, startio, all_bh, end_index);
  1126. }
  1127. if (iohead)
  1128. xfs_submit_ioend(wbc, iohead);
  1129. return page_dirty;
  1130. error:
  1131. if (iohead)
  1132. xfs_cancel_ioend(iohead);
  1133. /*
  1134. * If it's delalloc and we have nowhere to put it,
  1135. * throw it away, unless the lower layers told
  1136. * us to try again.
  1137. */
  1138. if (err != -EAGAIN) {
  1139. if (!unmapped)
  1140. xfs_aops_discard_page(page);
  1141. ClearPageUptodate(page);
  1142. }
  1143. return err;
  1144. }
  1145. /*
  1146. * writepage: Called from one of two places:
  1147. *
  1148. * 1. we are flushing a delalloc buffer head.
  1149. *
  1150. * 2. we are writing out a dirty page. Typically the page dirty
  1151. * state is cleared before we get here. In this case is it
  1152. * conceivable we have no buffer heads.
  1153. *
  1154. * For delalloc space on the page we need to allocate space and
  1155. * flush it. For unmapped buffer heads on the page we should
  1156. * allocate space if the page is uptodate. For any other dirty
  1157. * buffer heads on the page we should flush them.
  1158. *
  1159. * If we detect that a transaction would be required to flush
  1160. * the page, we have to check the process flags first, if we
  1161. * are already in a transaction or disk I/O during allocations
  1162. * is off, we need to fail the writepage and redirty the page.
  1163. */
  1164. STATIC int
  1165. xfs_vm_writepage(
  1166. struct page *page,
  1167. struct writeback_control *wbc)
  1168. {
  1169. int error;
  1170. int need_trans;
  1171. int delalloc, unmapped, unwritten;
  1172. struct inode *inode = page->mapping->host;
  1173. trace_xfs_writepage(inode, page, 0);
  1174. /*
  1175. * We need a transaction if:
  1176. * 1. There are delalloc buffers on the page
  1177. * 2. The page is uptodate and we have unmapped buffers
  1178. * 3. The page is uptodate and we have no buffers
  1179. * 4. There are unwritten buffers on the page
  1180. */
  1181. if (!page_has_buffers(page)) {
  1182. unmapped = 1;
  1183. need_trans = 1;
  1184. } else {
  1185. xfs_count_page_state(page, &delalloc, &unmapped, &unwritten);
  1186. if (!PageUptodate(page))
  1187. unmapped = 0;
  1188. need_trans = delalloc + unmapped + unwritten;
  1189. }
  1190. /*
  1191. * If we need a transaction and the process flags say
  1192. * we are already in a transaction, or no IO is allowed
  1193. * then mark the page dirty again and leave the page
  1194. * as is.
  1195. */
  1196. if (current_test_flags(PF_FSTRANS) && need_trans)
  1197. goto out_fail;
  1198. /*
  1199. * Delay hooking up buffer heads until we have
  1200. * made our go/no-go decision.
  1201. */
  1202. if (!page_has_buffers(page))
  1203. create_empty_buffers(page, 1 << inode->i_blkbits, 0);
  1204. /*
  1205. * VM calculation for nr_to_write seems off. Bump it way
  1206. * up, this gets simple streaming writes zippy again.
  1207. * To be reviewed again after Jens' writeback changes.
  1208. */
  1209. wbc->nr_to_write *= 4;
  1210. /*
  1211. * Convert delayed allocate, unwritten or unmapped space
  1212. * to real space and flush out to disk.
  1213. */
  1214. error = xfs_page_state_convert(inode, page, wbc, 1, unmapped);
  1215. if (error == -EAGAIN)
  1216. goto out_fail;
  1217. if (unlikely(error < 0))
  1218. goto out_unlock;
  1219. return 0;
  1220. out_fail:
  1221. redirty_page_for_writepage(wbc, page);
  1222. unlock_page(page);
  1223. return 0;
  1224. out_unlock:
  1225. unlock_page(page);
  1226. return error;
  1227. }
  1228. STATIC int
  1229. xfs_vm_writepages(
  1230. struct address_space *mapping,
  1231. struct writeback_control *wbc)
  1232. {
  1233. xfs_iflags_clear(XFS_I(mapping->host), XFS_ITRUNCATED);
  1234. return generic_writepages(mapping, wbc);
  1235. }
  1236. /*
  1237. * Called to move a page into cleanable state - and from there
  1238. * to be released. Possibly the page is already clean. We always
  1239. * have buffer heads in this call.
  1240. *
  1241. * Returns 0 if the page is ok to release, 1 otherwise.
  1242. *
  1243. * Possible scenarios are:
  1244. *
  1245. * 1. We are being called to release a page which has been written
  1246. * to via regular I/O. buffer heads will be dirty and possibly
  1247. * delalloc. If no delalloc buffer heads in this case then we
  1248. * can just return zero.
  1249. *
  1250. * 2. We are called to release a page which has been written via
  1251. * mmap, all we need to do is ensure there is no delalloc
  1252. * state in the buffer heads, if not we can let the caller
  1253. * free them and we should come back later via writepage.
  1254. */
  1255. STATIC int
  1256. xfs_vm_releasepage(
  1257. struct page *page,
  1258. gfp_t gfp_mask)
  1259. {
  1260. struct inode *inode = page->mapping->host;
  1261. int dirty, delalloc, unmapped, unwritten;
  1262. struct writeback_control wbc = {
  1263. .sync_mode = WB_SYNC_ALL,
  1264. .nr_to_write = 1,
  1265. };
  1266. trace_xfs_releasepage(inode, page, 0);
  1267. if (!page_has_buffers(page))
  1268. return 0;
  1269. xfs_count_page_state(page, &delalloc, &unmapped, &unwritten);
  1270. if (!delalloc && !unwritten)
  1271. goto free_buffers;
  1272. if (!(gfp_mask & __GFP_FS))
  1273. return 0;
  1274. /* If we are already inside a transaction or the thread cannot
  1275. * do I/O, we cannot release this page.
  1276. */
  1277. if (current_test_flags(PF_FSTRANS))
  1278. return 0;
  1279. /*
  1280. * Convert delalloc space to real space, do not flush the
  1281. * data out to disk, that will be done by the caller.
  1282. * Never need to allocate space here - we will always
  1283. * come back to writepage in that case.
  1284. */
  1285. dirty = xfs_page_state_convert(inode, page, &wbc, 0, 0);
  1286. if (dirty == 0 && !unwritten)
  1287. goto free_buffers;
  1288. return 0;
  1289. free_buffers:
  1290. return try_to_free_buffers(page);
  1291. }
  1292. STATIC int
  1293. __xfs_get_blocks(
  1294. struct inode *inode,
  1295. sector_t iblock,
  1296. struct buffer_head *bh_result,
  1297. int create,
  1298. int direct,
  1299. bmapi_flags_t flags)
  1300. {
  1301. struct xfs_bmbt_irec imap;
  1302. xfs_off_t offset;
  1303. ssize_t size;
  1304. int nimap = 1;
  1305. int new = 0;
  1306. int error;
  1307. offset = (xfs_off_t)iblock << inode->i_blkbits;
  1308. ASSERT(bh_result->b_size >= (1 << inode->i_blkbits));
  1309. size = bh_result->b_size;
  1310. if (!create && direct && offset >= i_size_read(inode))
  1311. return 0;
  1312. error = xfs_iomap(XFS_I(inode), offset, size,
  1313. create ? flags : BMAPI_READ, &imap, &nimap, &new);
  1314. if (error)
  1315. return -error;
  1316. if (nimap == 0)
  1317. return 0;
  1318. if (imap.br_startblock != HOLESTARTBLOCK &&
  1319. imap.br_startblock != DELAYSTARTBLOCK) {
  1320. /*
  1321. * For unwritten extents do not report a disk address on
  1322. * the read case (treat as if we're reading into a hole).
  1323. */
  1324. if (create || !ISUNWRITTEN(&imap))
  1325. xfs_map_buffer(inode, bh_result, &imap, offset);
  1326. if (create && ISUNWRITTEN(&imap)) {
  1327. if (direct)
  1328. bh_result->b_private = inode;
  1329. set_buffer_unwritten(bh_result);
  1330. }
  1331. }
  1332. /*
  1333. * If this is a realtime file, data may be on a different device.
  1334. * to that pointed to from the buffer_head b_bdev currently.
  1335. */
  1336. bh_result->b_bdev = xfs_find_bdev_for_inode(inode);
  1337. /*
  1338. * If we previously allocated a block out beyond eof and we are now
  1339. * coming back to use it then we will need to flag it as new even if it
  1340. * has a disk address.
  1341. *
  1342. * With sub-block writes into unwritten extents we also need to mark
  1343. * the buffer as new so that the unwritten parts of the buffer gets
  1344. * correctly zeroed.
  1345. */
  1346. if (create &&
  1347. ((!buffer_mapped(bh_result) && !buffer_uptodate(bh_result)) ||
  1348. (offset >= i_size_read(inode)) ||
  1349. (new || ISUNWRITTEN(&imap))))
  1350. set_buffer_new(bh_result);
  1351. if (imap.br_startblock == DELAYSTARTBLOCK) {
  1352. BUG_ON(direct);
  1353. if (create) {
  1354. set_buffer_uptodate(bh_result);
  1355. set_buffer_mapped(bh_result);
  1356. set_buffer_delay(bh_result);
  1357. }
  1358. }
  1359. /*
  1360. * If this is O_DIRECT or the mpage code calling tell them how large
  1361. * the mapping is, so that we can avoid repeated get_blocks calls.
  1362. */
  1363. if (direct || size > (1 << inode->i_blkbits)) {
  1364. xfs_off_t mapping_size;
  1365. mapping_size = imap.br_startoff + imap.br_blockcount - iblock;
  1366. mapping_size <<= inode->i_blkbits;
  1367. ASSERT(mapping_size > 0);
  1368. if (mapping_size > size)
  1369. mapping_size = size;
  1370. if (mapping_size > LONG_MAX)
  1371. mapping_size = LONG_MAX;
  1372. bh_result->b_size = mapping_size;
  1373. }
  1374. return 0;
  1375. }
  1376. int
  1377. xfs_get_blocks(
  1378. struct inode *inode,
  1379. sector_t iblock,
  1380. struct buffer_head *bh_result,
  1381. int create)
  1382. {
  1383. return __xfs_get_blocks(inode, iblock,
  1384. bh_result, create, 0, BMAPI_WRITE);
  1385. }
  1386. STATIC int
  1387. xfs_get_blocks_direct(
  1388. struct inode *inode,
  1389. sector_t iblock,
  1390. struct buffer_head *bh_result,
  1391. int create)
  1392. {
  1393. return __xfs_get_blocks(inode, iblock,
  1394. bh_result, create, 1, BMAPI_WRITE|BMAPI_DIRECT);
  1395. }
  1396. STATIC void
  1397. xfs_end_io_direct(
  1398. struct kiocb *iocb,
  1399. loff_t offset,
  1400. ssize_t size,
  1401. void *private)
  1402. {
  1403. xfs_ioend_t *ioend = iocb->private;
  1404. /*
  1405. * Non-NULL private data means we need to issue a transaction to
  1406. * convert a range from unwritten to written extents. This needs
  1407. * to happen from process context but aio+dio I/O completion
  1408. * happens from irq context so we need to defer it to a workqueue.
  1409. * This is not necessary for synchronous direct I/O, but we do
  1410. * it anyway to keep the code uniform and simpler.
  1411. *
  1412. * Well, if only it were that simple. Because synchronous direct I/O
  1413. * requires extent conversion to occur *before* we return to userspace,
  1414. * we have to wait for extent conversion to complete. Look at the
  1415. * iocb that has been passed to us to determine if this is AIO or
  1416. * not. If it is synchronous, tell xfs_finish_ioend() to kick the
  1417. * workqueue and wait for it to complete.
  1418. *
  1419. * The core direct I/O code might be changed to always call the
  1420. * completion handler in the future, in which case all this can
  1421. * go away.
  1422. */
  1423. ioend->io_offset = offset;
  1424. ioend->io_size = size;
  1425. if (ioend->io_type == IO_READ) {
  1426. xfs_finish_ioend(ioend, 0);
  1427. } else if (private && size > 0) {
  1428. xfs_finish_ioend(ioend, is_sync_kiocb(iocb));
  1429. } else {
  1430. /*
  1431. * A direct I/O write ioend starts it's life in unwritten
  1432. * state in case they map an unwritten extent. This write
  1433. * didn't map an unwritten extent so switch it's completion
  1434. * handler.
  1435. */
  1436. ioend->io_type = IO_NEW;
  1437. xfs_finish_ioend(ioend, 0);
  1438. }
  1439. /*
  1440. * blockdev_direct_IO can return an error even after the I/O
  1441. * completion handler was called. Thus we need to protect
  1442. * against double-freeing.
  1443. */
  1444. iocb->private = NULL;
  1445. }
  1446. STATIC ssize_t
  1447. xfs_vm_direct_IO(
  1448. int rw,
  1449. struct kiocb *iocb,
  1450. const struct iovec *iov,
  1451. loff_t offset,
  1452. unsigned long nr_segs)
  1453. {
  1454. struct file *file = iocb->ki_filp;
  1455. struct inode *inode = file->f_mapping->host;
  1456. struct block_device *bdev;
  1457. ssize_t ret;
  1458. bdev = xfs_find_bdev_for_inode(inode);
  1459. iocb->private = xfs_alloc_ioend(inode, rw == WRITE ?
  1460. IO_UNWRITTEN : IO_READ);
  1461. ret = blockdev_direct_IO_no_locking(rw, iocb, inode, bdev, iov,
  1462. offset, nr_segs,
  1463. xfs_get_blocks_direct,
  1464. xfs_end_io_direct);
  1465. if (unlikely(ret != -EIOCBQUEUED && iocb->private))
  1466. xfs_destroy_ioend(iocb->private);
  1467. return ret;
  1468. }
  1469. STATIC int
  1470. xfs_vm_write_begin(
  1471. struct file *file,
  1472. struct address_space *mapping,
  1473. loff_t pos,
  1474. unsigned len,
  1475. unsigned flags,
  1476. struct page **pagep,
  1477. void **fsdata)
  1478. {
  1479. *pagep = NULL;
  1480. return block_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
  1481. xfs_get_blocks);
  1482. }
  1483. STATIC sector_t
  1484. xfs_vm_bmap(
  1485. struct address_space *mapping,
  1486. sector_t block)
  1487. {
  1488. struct inode *inode = (struct inode *)mapping->host;
  1489. struct xfs_inode *ip = XFS_I(inode);
  1490. xfs_itrace_entry(XFS_I(inode));
  1491. xfs_ilock(ip, XFS_IOLOCK_SHARED);
  1492. xfs_flush_pages(ip, (xfs_off_t)0, -1, 0, FI_REMAPF);
  1493. xfs_iunlock(ip, XFS_IOLOCK_SHARED);
  1494. return generic_block_bmap(mapping, block, xfs_get_blocks);
  1495. }
  1496. STATIC int
  1497. xfs_vm_readpage(
  1498. struct file *unused,
  1499. struct page *page)
  1500. {
  1501. return mpage_readpage(page, xfs_get_blocks);
  1502. }
  1503. STATIC int
  1504. xfs_vm_readpages(
  1505. struct file *unused,
  1506. struct address_space *mapping,
  1507. struct list_head *pages,
  1508. unsigned nr_pages)
  1509. {
  1510. return mpage_readpages(mapping, pages, nr_pages, xfs_get_blocks);
  1511. }
  1512. const struct address_space_operations xfs_address_space_operations = {
  1513. .readpage = xfs_vm_readpage,
  1514. .readpages = xfs_vm_readpages,
  1515. .writepage = xfs_vm_writepage,
  1516. .writepages = xfs_vm_writepages,
  1517. .sync_page = block_sync_page,
  1518. .releasepage = xfs_vm_releasepage,
  1519. .invalidatepage = xfs_vm_invalidatepage,
  1520. .write_begin = xfs_vm_write_begin,
  1521. .write_end = generic_write_end,
  1522. .bmap = xfs_vm_bmap,
  1523. .direct_IO = xfs_vm_direct_IO,
  1524. .migratepage = buffer_migrate_page,
  1525. .is_partially_uptodate = block_is_partially_uptodate,
  1526. .error_remove_page = generic_error_remove_page,
  1527. };