xfs_aops.c 35 KB

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