xfs_aops.c 43 KB

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