xfs_aops.c 40 KB

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