PageRenderTime 1317ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 1ms

/fs/xfs/xfs_aops.c

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