xfs_aops.c 43 KB

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