xfs_aops.c 41 KB

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