xfs_aops.c 35 KB

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