aboutsummaryrefslogtreecommitdiffstats
path: root/fs/ext4/inode.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/ext4/inode.c')
-rw-r--r--fs/ext4/inode.c1791
1 files changed, 802 insertions, 989 deletions
diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
index d6382b89ecbd..dd32a2eacd0d 100644
--- a/fs/ext4/inode.c
+++ b/fs/ext4/inode.c
@@ -132,12 +132,12 @@ static inline int ext4_begin_ordered_truncate(struct inode *inode,
132 new_size); 132 new_size);
133} 133}
134 134
135static void ext4_invalidatepage(struct page *page, unsigned long offset); 135static void ext4_invalidatepage(struct page *page, unsigned int offset,
136 unsigned int length);
136static int __ext4_journalled_writepage(struct page *page, unsigned int len); 137static int __ext4_journalled_writepage(struct page *page, unsigned int len);
137static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh); 138static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh);
138static int ext4_discard_partial_page_buffers_no_lock(handle_t *handle, 139static int ext4_meta_trans_blocks(struct inode *inode, int lblocks,
139 struct inode *inode, struct page *page, loff_t from, 140 int pextents);
140 loff_t length, int flags);
141 141
142/* 142/*
143 * Test whether an inode is a fast symlink. 143 * Test whether an inode is a fast symlink.
@@ -215,7 +215,8 @@ void ext4_evict_inode(struct inode *inode)
215 filemap_write_and_wait(&inode->i_data); 215 filemap_write_and_wait(&inode->i_data);
216 } 216 }
217 truncate_inode_pages(&inode->i_data, 0); 217 truncate_inode_pages(&inode->i_data, 0);
218 ext4_ioend_shutdown(inode); 218
219 WARN_ON(atomic_read(&EXT4_I(inode)->i_ioend_count));
219 goto no_delete; 220 goto no_delete;
220 } 221 }
221 222
@@ -225,8 +226,8 @@ void ext4_evict_inode(struct inode *inode)
225 if (ext4_should_order_data(inode)) 226 if (ext4_should_order_data(inode))
226 ext4_begin_ordered_truncate(inode, 0); 227 ext4_begin_ordered_truncate(inode, 0);
227 truncate_inode_pages(&inode->i_data, 0); 228 truncate_inode_pages(&inode->i_data, 0);
228 ext4_ioend_shutdown(inode);
229 229
230 WARN_ON(atomic_read(&EXT4_I(inode)->i_ioend_count));
230 if (is_bad_inode(inode)) 231 if (is_bad_inode(inode))
231 goto no_delete; 232 goto no_delete;
232 233
@@ -423,66 +424,6 @@ static int __check_block_validity(struct inode *inode, const char *func,
423#define check_block_validity(inode, map) \ 424#define check_block_validity(inode, map) \
424 __check_block_validity((inode), __func__, __LINE__, (map)) 425 __check_block_validity((inode), __func__, __LINE__, (map))
425 426
426/*
427 * Return the number of contiguous dirty pages in a given inode
428 * starting at page frame idx.
429 */
430static pgoff_t ext4_num_dirty_pages(struct inode *inode, pgoff_t idx,
431 unsigned int max_pages)
432{
433 struct address_space *mapping = inode->i_mapping;
434 pgoff_t index;
435 struct pagevec pvec;
436 pgoff_t num = 0;
437 int i, nr_pages, done = 0;
438
439 if (max_pages == 0)
440 return 0;
441 pagevec_init(&pvec, 0);
442 while (!done) {
443 index = idx;
444 nr_pages = pagevec_lookup_tag(&pvec, mapping, &index,
445 PAGECACHE_TAG_DIRTY,
446 (pgoff_t)PAGEVEC_SIZE);
447 if (nr_pages == 0)
448 break;
449 for (i = 0; i < nr_pages; i++) {
450 struct page *page = pvec.pages[i];
451 struct buffer_head *bh, *head;
452
453 lock_page(page);
454 if (unlikely(page->mapping != mapping) ||
455 !PageDirty(page) ||
456 PageWriteback(page) ||
457 page->index != idx) {
458 done = 1;
459 unlock_page(page);
460 break;
461 }
462 if (page_has_buffers(page)) {
463 bh = head = page_buffers(page);
464 do {
465 if (!buffer_delay(bh) &&
466 !buffer_unwritten(bh))
467 done = 1;
468 bh = bh->b_this_page;
469 } while (!done && (bh != head));
470 }
471 unlock_page(page);
472 if (done)
473 break;
474 idx++;
475 num++;
476 if (num >= max_pages) {
477 done = 1;
478 break;
479 }
480 }
481 pagevec_release(&pvec);
482 }
483 return num;
484}
485
486#ifdef ES_AGGRESSIVE_TEST 427#ifdef ES_AGGRESSIVE_TEST
487static void ext4_map_blocks_es_recheck(handle_t *handle, 428static void ext4_map_blocks_es_recheck(handle_t *handle,
488 struct inode *inode, 429 struct inode *inode,
@@ -524,7 +465,7 @@ static void ext4_map_blocks_es_recheck(handle_t *handle,
524 if (es_map->m_lblk != map->m_lblk || 465 if (es_map->m_lblk != map->m_lblk ||
525 es_map->m_flags != map->m_flags || 466 es_map->m_flags != map->m_flags ||
526 es_map->m_pblk != map->m_pblk) { 467 es_map->m_pblk != map->m_pblk) {
527 printk("ES cache assertation failed for inode: %lu " 468 printk("ES cache assertion failed for inode: %lu "
528 "es_cached ex [%d/%d/%llu/%x] != " 469 "es_cached ex [%d/%d/%llu/%x] != "
529 "found ex [%d/%d/%llu/%x] retval %d flags %x\n", 470 "found ex [%d/%d/%llu/%x] retval %d flags %x\n",
530 inode->i_ino, es_map->m_lblk, es_map->m_len, 471 inode->i_ino, es_map->m_lblk, es_map->m_len,
@@ -575,6 +516,7 @@ int ext4_map_blocks(handle_t *handle, struct inode *inode,
575 516
576 /* Lookup extent status tree firstly */ 517 /* Lookup extent status tree firstly */
577 if (ext4_es_lookup_extent(inode, map->m_lblk, &es)) { 518 if (ext4_es_lookup_extent(inode, map->m_lblk, &es)) {
519 ext4_es_lru_add(inode);
578 if (ext4_es_is_written(&es) || ext4_es_is_unwritten(&es)) { 520 if (ext4_es_is_written(&es) || ext4_es_is_unwritten(&es)) {
579 map->m_pblk = ext4_es_pblock(&es) + 521 map->m_pblk = ext4_es_pblock(&es) +
580 map->m_lblk - es.es_lblk; 522 map->m_lblk - es.es_lblk;
@@ -613,14 +555,13 @@ int ext4_map_blocks(handle_t *handle, struct inode *inode,
613 int ret; 555 int ret;
614 unsigned long long status; 556 unsigned long long status;
615 557
616#ifdef ES_AGGRESSIVE_TEST 558 if (unlikely(retval != map->m_len)) {
617 if (retval != map->m_len) { 559 ext4_warning(inode->i_sb,
618 printk("ES len assertation failed for inode: %lu " 560 "ES len assertion failed for inode "
619 "retval %d != map->m_len %d " 561 "%lu: retval %d != map->m_len %d",
620 "in %s (lookup)\n", inode->i_ino, retval, 562 inode->i_ino, retval, map->m_len);
621 map->m_len, __func__); 563 WARN_ON(1);
622 } 564 }
623#endif
624 565
625 status = map->m_flags & EXT4_MAP_UNWRITTEN ? 566 status = map->m_flags & EXT4_MAP_UNWRITTEN ?
626 EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN; 567 EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN;
@@ -714,14 +655,13 @@ found:
714 int ret; 655 int ret;
715 unsigned long long status; 656 unsigned long long status;
716 657
717#ifdef ES_AGGRESSIVE_TEST 658 if (unlikely(retval != map->m_len)) {
718 if (retval != map->m_len) { 659 ext4_warning(inode->i_sb,
719 printk("ES len assertation failed for inode: %lu " 660 "ES len assertion failed for inode "
720 "retval %d != map->m_len %d " 661 "%lu: retval %d != map->m_len %d",
721 "in %s (allocation)\n", inode->i_ino, retval, 662 inode->i_ino, retval, map->m_len);
722 map->m_len, __func__); 663 WARN_ON(1);
723 } 664 }
724#endif
725 665
726 /* 666 /*
727 * If the extent has been zeroed out, we don't need to update 667 * If the extent has been zeroed out, we don't need to update
@@ -1118,10 +1058,13 @@ static int ext4_write_end(struct file *file,
1118 } 1058 }
1119 } 1059 }
1120 1060
1121 if (ext4_has_inline_data(inode)) 1061 if (ext4_has_inline_data(inode)) {
1122 copied = ext4_write_inline_data_end(inode, pos, len, 1062 ret = ext4_write_inline_data_end(inode, pos, len,
1123 copied, page); 1063 copied, page);
1124 else 1064 if (ret < 0)
1065 goto errout;
1066 copied = ret;
1067 } else
1125 copied = block_write_end(file, mapping, pos, 1068 copied = block_write_end(file, mapping, pos,
1126 len, copied, page, fsdata); 1069 len, copied, page, fsdata);
1127 1070
@@ -1157,8 +1100,6 @@ static int ext4_write_end(struct file *file,
1157 if (i_size_changed) 1100 if (i_size_changed)
1158 ext4_mark_inode_dirty(handle, inode); 1101 ext4_mark_inode_dirty(handle, inode);
1159 1102
1160 if (copied < 0)
1161 ret = copied;
1162 if (pos + len > inode->i_size && ext4_can_truncate(inode)) 1103 if (pos + len > inode->i_size && ext4_can_truncate(inode))
1163 /* if we have allocated more blocks and copied 1104 /* if we have allocated more blocks and copied
1164 * less. We will have blocks allocated outside 1105 * less. We will have blocks allocated outside
@@ -1415,21 +1356,28 @@ static void ext4_da_release_space(struct inode *inode, int to_free)
1415} 1356}
1416 1357
1417static void ext4_da_page_release_reservation(struct page *page, 1358static void ext4_da_page_release_reservation(struct page *page,
1418 unsigned long offset) 1359 unsigned int offset,
1360 unsigned int length)
1419{ 1361{
1420 int to_release = 0; 1362 int to_release = 0;
1421 struct buffer_head *head, *bh; 1363 struct buffer_head *head, *bh;
1422 unsigned int curr_off = 0; 1364 unsigned int curr_off = 0;
1423 struct inode *inode = page->mapping->host; 1365 struct inode *inode = page->mapping->host;
1424 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 1366 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
1367 unsigned int stop = offset + length;
1425 int num_clusters; 1368 int num_clusters;
1426 ext4_fsblk_t lblk; 1369 ext4_fsblk_t lblk;
1427 1370
1371 BUG_ON(stop > PAGE_CACHE_SIZE || stop < length);
1372
1428 head = page_buffers(page); 1373 head = page_buffers(page);
1429 bh = head; 1374 bh = head;
1430 do { 1375 do {
1431 unsigned int next_off = curr_off + bh->b_size; 1376 unsigned int next_off = curr_off + bh->b_size;
1432 1377
1378 if (next_off > stop)
1379 break;
1380
1433 if ((offset <= curr_off) && (buffer_delay(bh))) { 1381 if ((offset <= curr_off) && (buffer_delay(bh))) {
1434 to_release++; 1382 to_release++;
1435 clear_buffer_delay(bh); 1383 clear_buffer_delay(bh);
@@ -1460,140 +1408,43 @@ static void ext4_da_page_release_reservation(struct page *page,
1460 * Delayed allocation stuff 1408 * Delayed allocation stuff
1461 */ 1409 */
1462 1410
1463/* 1411struct mpage_da_data {
1464 * mpage_da_submit_io - walks through extent of pages and try to write 1412 struct inode *inode;
1465 * them with writepage() call back 1413 struct writeback_control *wbc;
1466 *
1467 * @mpd->inode: inode
1468 * @mpd->first_page: first page of the extent
1469 * @mpd->next_page: page after the last page of the extent
1470 *
1471 * By the time mpage_da_submit_io() is called we expect all blocks
1472 * to be allocated. this may be wrong if allocation failed.
1473 *
1474 * As pages are already locked by write_cache_pages(), we can't use it
1475 */
1476static int mpage_da_submit_io(struct mpage_da_data *mpd,
1477 struct ext4_map_blocks *map)
1478{
1479 struct pagevec pvec;
1480 unsigned long index, end;
1481 int ret = 0, err, nr_pages, i;
1482 struct inode *inode = mpd->inode;
1483 struct address_space *mapping = inode->i_mapping;
1484 loff_t size = i_size_read(inode);
1485 unsigned int len, block_start;
1486 struct buffer_head *bh, *page_bufs = NULL;
1487 sector_t pblock = 0, cur_logical = 0;
1488 struct ext4_io_submit io_submit;
1489 1414
1490 BUG_ON(mpd->next_page <= mpd->first_page); 1415 pgoff_t first_page; /* The first page to write */
1491 memset(&io_submit, 0, sizeof(io_submit)); 1416 pgoff_t next_page; /* Current page to examine */
1417 pgoff_t last_page; /* Last page to examine */
1492 /* 1418 /*
1493 * We need to start from the first_page to the next_page - 1 1419 * Extent to map - this can be after first_page because that can be
1494 * to make sure we also write the mapped dirty buffer_heads. 1420 * fully mapped. We somewhat abuse m_flags to store whether the extent
1495 * If we look at mpd->b_blocknr we would only be looking 1421 * is delalloc or unwritten.
1496 * at the currently mapped buffer_heads.
1497 */ 1422 */
1498 index = mpd->first_page; 1423 struct ext4_map_blocks map;
1499 end = mpd->next_page - 1; 1424 struct ext4_io_submit io_submit; /* IO submission data */
1500 1425};
1501 pagevec_init(&pvec, 0);
1502 while (index <= end) {
1503 nr_pages = pagevec_lookup(&pvec, mapping, index, PAGEVEC_SIZE);
1504 if (nr_pages == 0)
1505 break;
1506 for (i = 0; i < nr_pages; i++) {
1507 int skip_page = 0;
1508 struct page *page = pvec.pages[i];
1509
1510 index = page->index;
1511 if (index > end)
1512 break;
1513
1514 if (index == size >> PAGE_CACHE_SHIFT)
1515 len = size & ~PAGE_CACHE_MASK;
1516 else
1517 len = PAGE_CACHE_SIZE;
1518 if (map) {
1519 cur_logical = index << (PAGE_CACHE_SHIFT -
1520 inode->i_blkbits);
1521 pblock = map->m_pblk + (cur_logical -
1522 map->m_lblk);
1523 }
1524 index++;
1525
1526 BUG_ON(!PageLocked(page));
1527 BUG_ON(PageWriteback(page));
1528
1529 bh = page_bufs = page_buffers(page);
1530 block_start = 0;
1531 do {
1532 if (map && (cur_logical >= map->m_lblk) &&
1533 (cur_logical <= (map->m_lblk +
1534 (map->m_len - 1)))) {
1535 if (buffer_delay(bh)) {
1536 clear_buffer_delay(bh);
1537 bh->b_blocknr = pblock;
1538 }
1539 if (buffer_unwritten(bh) ||
1540 buffer_mapped(bh))
1541 BUG_ON(bh->b_blocknr != pblock);
1542 if (map->m_flags & EXT4_MAP_UNINIT)
1543 set_buffer_uninit(bh);
1544 clear_buffer_unwritten(bh);
1545 }
1546
1547 /*
1548 * skip page if block allocation undone and
1549 * block is dirty
1550 */
1551 if (ext4_bh_delay_or_unwritten(NULL, bh))
1552 skip_page = 1;
1553 bh = bh->b_this_page;
1554 block_start += bh->b_size;
1555 cur_logical++;
1556 pblock++;
1557 } while (bh != page_bufs);
1558
1559 if (skip_page) {
1560 unlock_page(page);
1561 continue;
1562 }
1563
1564 clear_page_dirty_for_io(page);
1565 err = ext4_bio_write_page(&io_submit, page, len,
1566 mpd->wbc);
1567 if (!err)
1568 mpd->pages_written++;
1569 /*
1570 * In error case, we have to continue because
1571 * remaining pages are still locked
1572 */
1573 if (ret == 0)
1574 ret = err;
1575 }
1576 pagevec_release(&pvec);
1577 }
1578 ext4_io_submit(&io_submit);
1579 return ret;
1580}
1581 1426
1582static void ext4_da_block_invalidatepages(struct mpage_da_data *mpd) 1427static void mpage_release_unused_pages(struct mpage_da_data *mpd,
1428 bool invalidate)
1583{ 1429{
1584 int nr_pages, i; 1430 int nr_pages, i;
1585 pgoff_t index, end; 1431 pgoff_t index, end;
1586 struct pagevec pvec; 1432 struct pagevec pvec;
1587 struct inode *inode = mpd->inode; 1433 struct inode *inode = mpd->inode;
1588 struct address_space *mapping = inode->i_mapping; 1434 struct address_space *mapping = inode->i_mapping;
1589 ext4_lblk_t start, last; 1435
1436 /* This is necessary when next_page == 0. */
1437 if (mpd->first_page >= mpd->next_page)
1438 return;
1590 1439
1591 index = mpd->first_page; 1440 index = mpd->first_page;
1592 end = mpd->next_page - 1; 1441 end = mpd->next_page - 1;
1593 1442 if (invalidate) {
1594 start = index << (PAGE_CACHE_SHIFT - inode->i_blkbits); 1443 ext4_lblk_t start, last;
1595 last = end << (PAGE_CACHE_SHIFT - inode->i_blkbits); 1444 start = index << (PAGE_CACHE_SHIFT - inode->i_blkbits);
1596 ext4_es_remove_extent(inode, start, last - start + 1); 1445 last = end << (PAGE_CACHE_SHIFT - inode->i_blkbits);
1446 ext4_es_remove_extent(inode, start, last - start + 1);
1447 }
1597 1448
1598 pagevec_init(&pvec, 0); 1449 pagevec_init(&pvec, 0);
1599 while (index <= end) { 1450 while (index <= end) {
@@ -1606,14 +1457,15 @@ static void ext4_da_block_invalidatepages(struct mpage_da_data *mpd)
1606 break; 1457 break;
1607 BUG_ON(!PageLocked(page)); 1458 BUG_ON(!PageLocked(page));
1608 BUG_ON(PageWriteback(page)); 1459 BUG_ON(PageWriteback(page));
1609 block_invalidatepage(page, 0); 1460 if (invalidate) {
1610 ClearPageUptodate(page); 1461 block_invalidatepage(page, 0, PAGE_CACHE_SIZE);
1462 ClearPageUptodate(page);
1463 }
1611 unlock_page(page); 1464 unlock_page(page);
1612 } 1465 }
1613 index = pvec.pages[nr_pages - 1]->index + 1; 1466 index = pvec.pages[nr_pages - 1]->index + 1;
1614 pagevec_release(&pvec); 1467 pagevec_release(&pvec);
1615 } 1468 }
1616 return;
1617} 1469}
1618 1470
1619static void ext4_print_free_blocks(struct inode *inode) 1471static void ext4_print_free_blocks(struct inode *inode)
@@ -1642,215 +1494,6 @@ static void ext4_print_free_blocks(struct inode *inode)
1642 return; 1494 return;
1643} 1495}
1644 1496
1645/*
1646 * mpage_da_map_and_submit - go through given space, map them
1647 * if necessary, and then submit them for I/O
1648 *
1649 * @mpd - bh describing space
1650 *
1651 * The function skips space we know is already mapped to disk blocks.
1652 *
1653 */
1654static void mpage_da_map_and_submit(struct mpage_da_data *mpd)
1655{
1656 int err, blks, get_blocks_flags;
1657 struct ext4_map_blocks map, *mapp = NULL;
1658 sector_t next = mpd->b_blocknr;
1659 unsigned max_blocks = mpd->b_size >> mpd->inode->i_blkbits;
1660 loff_t disksize = EXT4_I(mpd->inode)->i_disksize;
1661 handle_t *handle = NULL;
1662
1663 /*
1664 * If the blocks are mapped already, or we couldn't accumulate
1665 * any blocks, then proceed immediately to the submission stage.
1666 */
1667 if ((mpd->b_size == 0) ||
1668 ((mpd->b_state & (1 << BH_Mapped)) &&
1669 !(mpd->b_state & (1 << BH_Delay)) &&
1670 !(mpd->b_state & (1 << BH_Unwritten))))
1671 goto submit_io;
1672
1673 handle = ext4_journal_current_handle();
1674 BUG_ON(!handle);
1675
1676 /*
1677 * Call ext4_map_blocks() to allocate any delayed allocation
1678 * blocks, or to convert an uninitialized extent to be
1679 * initialized (in the case where we have written into
1680 * one or more preallocated blocks).
1681 *
1682 * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE to
1683 * indicate that we are on the delayed allocation path. This
1684 * affects functions in many different parts of the allocation
1685 * call path. This flag exists primarily because we don't
1686 * want to change *many* call functions, so ext4_map_blocks()
1687 * will set the EXT4_STATE_DELALLOC_RESERVED flag once the
1688 * inode's allocation semaphore is taken.
1689 *
1690 * If the blocks in questions were delalloc blocks, set
1691 * EXT4_GET_BLOCKS_DELALLOC_RESERVE so the delalloc accounting
1692 * variables are updated after the blocks have been allocated.
1693 */
1694 map.m_lblk = next;
1695 map.m_len = max_blocks;
1696 /*
1697 * We're in delalloc path and it is possible that we're going to
1698 * need more metadata blocks than previously reserved. However
1699 * we must not fail because we're in writeback and there is
1700 * nothing we can do about it so it might result in data loss.
1701 * So use reserved blocks to allocate metadata if possible.
1702 */
1703 get_blocks_flags = EXT4_GET_BLOCKS_CREATE |
1704 EXT4_GET_BLOCKS_METADATA_NOFAIL;
1705 if (ext4_should_dioread_nolock(mpd->inode))
1706 get_blocks_flags |= EXT4_GET_BLOCKS_IO_CREATE_EXT;
1707 if (mpd->b_state & (1 << BH_Delay))
1708 get_blocks_flags |= EXT4_GET_BLOCKS_DELALLOC_RESERVE;
1709
1710
1711 blks = ext4_map_blocks(handle, mpd->inode, &map, get_blocks_flags);
1712 if (blks < 0) {
1713 struct super_block *sb = mpd->inode->i_sb;
1714
1715 err = blks;
1716 /*
1717 * If get block returns EAGAIN or ENOSPC and there
1718 * appears to be free blocks we will just let
1719 * mpage_da_submit_io() unlock all of the pages.
1720 */
1721 if (err == -EAGAIN)
1722 goto submit_io;
1723
1724 if (err == -ENOSPC && ext4_count_free_clusters(sb)) {
1725 mpd->retval = err;
1726 goto submit_io;
1727 }
1728
1729 /*
1730 * get block failure will cause us to loop in
1731 * writepages, because a_ops->writepage won't be able
1732 * to make progress. The page will be redirtied by
1733 * writepage and writepages will again try to write
1734 * the same.
1735 */
1736 if (!(EXT4_SB(sb)->s_mount_flags & EXT4_MF_FS_ABORTED)) {
1737 ext4_msg(sb, KERN_CRIT,
1738 "delayed block allocation failed for inode %lu "
1739 "at logical offset %llu with max blocks %zd "
1740 "with error %d", mpd->inode->i_ino,
1741 (unsigned long long) next,
1742 mpd->b_size >> mpd->inode->i_blkbits, err);
1743 ext4_msg(sb, KERN_CRIT,
1744 "This should not happen!! Data will be lost");
1745 if (err == -ENOSPC)
1746 ext4_print_free_blocks(mpd->inode);
1747 }
1748 /* invalidate all the pages */
1749 ext4_da_block_invalidatepages(mpd);
1750
1751 /* Mark this page range as having been completed */
1752 mpd->io_done = 1;
1753 return;
1754 }
1755 BUG_ON(blks == 0);
1756
1757 mapp = &map;
1758 if (map.m_flags & EXT4_MAP_NEW) {
1759 struct block_device *bdev = mpd->inode->i_sb->s_bdev;
1760 int i;
1761
1762 for (i = 0; i < map.m_len; i++)
1763 unmap_underlying_metadata(bdev, map.m_pblk + i);
1764 }
1765
1766 /*
1767 * Update on-disk size along with block allocation.
1768 */
1769 disksize = ((loff_t) next + blks) << mpd->inode->i_blkbits;
1770 if (disksize > i_size_read(mpd->inode))
1771 disksize = i_size_read(mpd->inode);
1772 if (disksize > EXT4_I(mpd->inode)->i_disksize) {
1773 ext4_update_i_disksize(mpd->inode, disksize);
1774 err = ext4_mark_inode_dirty(handle, mpd->inode);
1775 if (err)
1776 ext4_error(mpd->inode->i_sb,
1777 "Failed to mark inode %lu dirty",
1778 mpd->inode->i_ino);
1779 }
1780
1781submit_io:
1782 mpage_da_submit_io(mpd, mapp);
1783 mpd->io_done = 1;
1784}
1785
1786#define BH_FLAGS ((1 << BH_Uptodate) | (1 << BH_Mapped) | \
1787 (1 << BH_Delay) | (1 << BH_Unwritten))
1788
1789/*
1790 * mpage_add_bh_to_extent - try to add one more block to extent of blocks
1791 *
1792 * @mpd->lbh - extent of blocks
1793 * @logical - logical number of the block in the file
1794 * @b_state - b_state of the buffer head added
1795 *
1796 * the function is used to collect contig. blocks in same state
1797 */
1798static void mpage_add_bh_to_extent(struct mpage_da_data *mpd, sector_t logical,
1799 unsigned long b_state)
1800{
1801 sector_t next;
1802 int blkbits = mpd->inode->i_blkbits;
1803 int nrblocks = mpd->b_size >> blkbits;
1804
1805 /*
1806 * XXX Don't go larger than mballoc is willing to allocate
1807 * This is a stopgap solution. We eventually need to fold
1808 * mpage_da_submit_io() into this function and then call
1809 * ext4_map_blocks() multiple times in a loop
1810 */
1811 if (nrblocks >= (8*1024*1024 >> blkbits))
1812 goto flush_it;
1813
1814 /* check if the reserved journal credits might overflow */
1815 if (!ext4_test_inode_flag(mpd->inode, EXT4_INODE_EXTENTS)) {
1816 if (nrblocks >= EXT4_MAX_TRANS_DATA) {
1817 /*
1818 * With non-extent format we are limited by the journal
1819 * credit available. Total credit needed to insert
1820 * nrblocks contiguous blocks is dependent on the
1821 * nrblocks. So limit nrblocks.
1822 */
1823 goto flush_it;
1824 }
1825 }
1826 /*
1827 * First block in the extent
1828 */
1829 if (mpd->b_size == 0) {
1830 mpd->b_blocknr = logical;
1831 mpd->b_size = 1 << blkbits;
1832 mpd->b_state = b_state & BH_FLAGS;
1833 return;
1834 }
1835
1836 next = mpd->b_blocknr + nrblocks;
1837 /*
1838 * Can we merge the block to our big extent?
1839 */
1840 if (logical == next && (b_state & BH_FLAGS) == mpd->b_state) {
1841 mpd->b_size += 1 << blkbits;
1842 return;
1843 }
1844
1845flush_it:
1846 /*
1847 * We couldn't merge the block to our extent, so we
1848 * need to flush current extent and start new one
1849 */
1850 mpage_da_map_and_submit(mpd);
1851 return;
1852}
1853
1854static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh) 1497static int ext4_bh_delay_or_unwritten(handle_t *handle, struct buffer_head *bh)
1855{ 1498{
1856 return (buffer_delay(bh) || buffer_unwritten(bh)) && buffer_dirty(bh); 1499 return (buffer_delay(bh) || buffer_unwritten(bh)) && buffer_dirty(bh);
@@ -1885,7 +1528,7 @@ static int ext4_da_map_blocks(struct inode *inode, sector_t iblock,
1885 1528
1886 /* Lookup extent status tree firstly */ 1529 /* Lookup extent status tree firstly */
1887 if (ext4_es_lookup_extent(inode, iblock, &es)) { 1530 if (ext4_es_lookup_extent(inode, iblock, &es)) {
1888 1531 ext4_es_lru_add(inode);
1889 if (ext4_es_is_hole(&es)) { 1532 if (ext4_es_is_hole(&es)) {
1890 retval = 0; 1533 retval = 0;
1891 down_read((&EXT4_I(inode)->i_data_sem)); 1534 down_read((&EXT4_I(inode)->i_data_sem));
@@ -1992,14 +1635,13 @@ add_delayed:
1992 int ret; 1635 int ret;
1993 unsigned long long status; 1636 unsigned long long status;
1994 1637
1995#ifdef ES_AGGRESSIVE_TEST 1638 if (unlikely(retval != map->m_len)) {
1996 if (retval != map->m_len) { 1639 ext4_warning(inode->i_sb,
1997 printk("ES len assertation failed for inode: %lu " 1640 "ES len assertion failed for inode "
1998 "retval %d != map->m_len %d " 1641 "%lu: retval %d != map->m_len %d",
1999 "in %s (lookup)\n", inode->i_ino, retval, 1642 inode->i_ino, retval, map->m_len);
2000 map->m_len, __func__); 1643 WARN_ON(1);
2001 } 1644 }
2002#endif
2003 1645
2004 status = map->m_flags & EXT4_MAP_UNWRITTEN ? 1646 status = map->m_flags & EXT4_MAP_UNWRITTEN ?
2005 EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN; 1647 EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN;
@@ -2156,7 +1798,7 @@ out:
2156 * lock so we have to do some magic. 1798 * lock so we have to do some magic.
2157 * 1799 *
2158 * This function can get called via... 1800 * This function can get called via...
2159 * - ext4_da_writepages after taking page lock (have journal handle) 1801 * - ext4_writepages after taking page lock (have journal handle)
2160 * - journal_submit_inode_data_buffers (no journal handle) 1802 * - journal_submit_inode_data_buffers (no journal handle)
2161 * - shrink_page_list via the kswapd/direct reclaim (no journal handle) 1803 * - shrink_page_list via the kswapd/direct reclaim (no journal handle)
2162 * - grab_page_cache when doing write_begin (have journal handle) 1804 * - grab_page_cache when doing write_begin (have journal handle)
@@ -2234,76 +1876,405 @@ static int ext4_writepage(struct page *page,
2234 */ 1876 */
2235 return __ext4_journalled_writepage(page, len); 1877 return __ext4_journalled_writepage(page, len);
2236 1878
2237 memset(&io_submit, 0, sizeof(io_submit)); 1879 ext4_io_submit_init(&io_submit, wbc);
1880 io_submit.io_end = ext4_init_io_end(inode, GFP_NOFS);
1881 if (!io_submit.io_end) {
1882 redirty_page_for_writepage(wbc, page);
1883 unlock_page(page);
1884 return -ENOMEM;
1885 }
2238 ret = ext4_bio_write_page(&io_submit, page, len, wbc); 1886 ret = ext4_bio_write_page(&io_submit, page, len, wbc);
2239 ext4_io_submit(&io_submit); 1887 ext4_io_submit(&io_submit);
1888 /* Drop io_end reference we got from init */
1889 ext4_put_io_end_defer(io_submit.io_end);
2240 return ret; 1890 return ret;
2241} 1891}
2242 1892
1893#define BH_FLAGS ((1 << BH_Unwritten) | (1 << BH_Delay))
1894
2243/* 1895/*
2244 * This is called via ext4_da_writepages() to 1896 * mballoc gives us at most this number of blocks...
2245 * calculate the total number of credits to reserve to fit 1897 * XXX: That seems to be only a limitation of ext4_mb_normalize_request().
2246 * a single extent allocation into a single transaction, 1898 * The rest of mballoc seems to handle chunks upto full group size.
2247 * ext4_da_writpeages() will loop calling this before
2248 * the block allocation.
2249 */ 1899 */
1900#define MAX_WRITEPAGES_EXTENT_LEN 2048
2250 1901
2251static int ext4_da_writepages_trans_blocks(struct inode *inode) 1902/*
1903 * mpage_add_bh_to_extent - try to add bh to extent of blocks to map
1904 *
1905 * @mpd - extent of blocks
1906 * @lblk - logical number of the block in the file
1907 * @b_state - b_state of the buffer head added
1908 *
1909 * the function is used to collect contig. blocks in same state
1910 */
1911static int mpage_add_bh_to_extent(struct mpage_da_data *mpd, ext4_lblk_t lblk,
1912 unsigned long b_state)
2252{ 1913{
2253 int max_blocks = EXT4_I(inode)->i_reserved_data_blocks; 1914 struct ext4_map_blocks *map = &mpd->map;
1915
1916 /* Don't go larger than mballoc is willing to allocate */
1917 if (map->m_len >= MAX_WRITEPAGES_EXTENT_LEN)
1918 return 0;
1919
1920 /* First block in the extent? */
1921 if (map->m_len == 0) {
1922 map->m_lblk = lblk;
1923 map->m_len = 1;
1924 map->m_flags = b_state & BH_FLAGS;
1925 return 1;
1926 }
1927
1928 /* Can we merge the block to our big extent? */
1929 if (lblk == map->m_lblk + map->m_len &&
1930 (b_state & BH_FLAGS) == map->m_flags) {
1931 map->m_len++;
1932 return 1;
1933 }
1934 return 0;
1935}
2254 1936
1937static bool add_page_bufs_to_extent(struct mpage_da_data *mpd,
1938 struct buffer_head *head,
1939 struct buffer_head *bh,
1940 ext4_lblk_t lblk)
1941{
1942 struct inode *inode = mpd->inode;
1943 ext4_lblk_t blocks = (i_size_read(inode) + (1 << inode->i_blkbits) - 1)
1944 >> inode->i_blkbits;
1945
1946 do {
1947 BUG_ON(buffer_locked(bh));
1948
1949 if (!buffer_dirty(bh) || !buffer_mapped(bh) ||
1950 (!buffer_delay(bh) && !buffer_unwritten(bh)) ||
1951 lblk >= blocks) {
1952 /* Found extent to map? */
1953 if (mpd->map.m_len)
1954 return false;
1955 if (lblk >= blocks)
1956 return true;
1957 continue;
1958 }
1959 if (!mpage_add_bh_to_extent(mpd, lblk, bh->b_state))
1960 return false;
1961 } while (lblk++, (bh = bh->b_this_page) != head);
1962 return true;
1963}
1964
1965static int mpage_submit_page(struct mpage_da_data *mpd, struct page *page)
1966{
1967 int len;
1968 loff_t size = i_size_read(mpd->inode);
1969 int err;
1970
1971 BUG_ON(page->index != mpd->first_page);
1972 if (page->index == size >> PAGE_CACHE_SHIFT)
1973 len = size & ~PAGE_CACHE_MASK;
1974 else
1975 len = PAGE_CACHE_SIZE;
1976 clear_page_dirty_for_io(page);
1977 err = ext4_bio_write_page(&mpd->io_submit, page, len, mpd->wbc);
1978 if (!err)
1979 mpd->wbc->nr_to_write--;
1980 mpd->first_page++;
1981
1982 return err;
1983}
1984
1985/*
1986 * mpage_map_buffers - update buffers corresponding to changed extent and
1987 * submit fully mapped pages for IO
1988 *
1989 * @mpd - description of extent to map, on return next extent to map
1990 *
1991 * Scan buffers corresponding to changed extent (we expect corresponding pages
1992 * to be already locked) and update buffer state according to new extent state.
1993 * We map delalloc buffers to their physical location, clear unwritten bits,
1994 * and mark buffers as uninit when we perform writes to uninitialized extents
1995 * and do extent conversion after IO is finished. If the last page is not fully
1996 * mapped, we update @map to the next extent in the last page that needs
1997 * mapping. Otherwise we submit the page for IO.
1998 */
1999static int mpage_map_and_submit_buffers(struct mpage_da_data *mpd)
2000{
2001 struct pagevec pvec;
2002 int nr_pages, i;
2003 struct inode *inode = mpd->inode;
2004 struct buffer_head *head, *bh;
2005 int bpp_bits = PAGE_CACHE_SHIFT - inode->i_blkbits;
2006 ext4_lblk_t blocks = (i_size_read(inode) + (1 << inode->i_blkbits) - 1)
2007 >> inode->i_blkbits;
2008 pgoff_t start, end;
2009 ext4_lblk_t lblk;
2010 sector_t pblock;
2011 int err;
2012
2013 start = mpd->map.m_lblk >> bpp_bits;
2014 end = (mpd->map.m_lblk + mpd->map.m_len - 1) >> bpp_bits;
2015 lblk = start << bpp_bits;
2016 pblock = mpd->map.m_pblk;
2017
2018 pagevec_init(&pvec, 0);
2019 while (start <= end) {
2020 nr_pages = pagevec_lookup(&pvec, inode->i_mapping, start,
2021 PAGEVEC_SIZE);
2022 if (nr_pages == 0)
2023 break;
2024 for (i = 0; i < nr_pages; i++) {
2025 struct page *page = pvec.pages[i];
2026
2027 if (page->index > end)
2028 break;
2029 /* Upto 'end' pages must be contiguous */
2030 BUG_ON(page->index != start);
2031 bh = head = page_buffers(page);
2032 do {
2033 if (lblk < mpd->map.m_lblk)
2034 continue;
2035 if (lblk >= mpd->map.m_lblk + mpd->map.m_len) {
2036 /*
2037 * Buffer after end of mapped extent.
2038 * Find next buffer in the page to map.
2039 */
2040 mpd->map.m_len = 0;
2041 mpd->map.m_flags = 0;
2042 add_page_bufs_to_extent(mpd, head, bh,
2043 lblk);
2044 pagevec_release(&pvec);
2045 return 0;
2046 }
2047 if (buffer_delay(bh)) {
2048 clear_buffer_delay(bh);
2049 bh->b_blocknr = pblock++;
2050 }
2051 clear_buffer_unwritten(bh);
2052 } while (++lblk < blocks &&
2053 (bh = bh->b_this_page) != head);
2054
2055 /*
2056 * FIXME: This is going to break if dioread_nolock
2057 * supports blocksize < pagesize as we will try to
2058 * convert potentially unmapped parts of inode.
2059 */
2060 mpd->io_submit.io_end->size += PAGE_CACHE_SIZE;
2061 /* Page fully mapped - let IO run! */
2062 err = mpage_submit_page(mpd, page);
2063 if (err < 0) {
2064 pagevec_release(&pvec);
2065 return err;
2066 }
2067 start++;
2068 }
2069 pagevec_release(&pvec);
2070 }
2071 /* Extent fully mapped and matches with page boundary. We are done. */
2072 mpd->map.m_len = 0;
2073 mpd->map.m_flags = 0;
2074 return 0;
2075}
2076
2077static int mpage_map_one_extent(handle_t *handle, struct mpage_da_data *mpd)
2078{
2079 struct inode *inode = mpd->inode;
2080 struct ext4_map_blocks *map = &mpd->map;
2081 int get_blocks_flags;
2082 int err;
2083
2084 trace_ext4_da_write_pages_extent(inode, map);
2255 /* 2085 /*
2256 * With non-extent format the journal credit needed to 2086 * Call ext4_map_blocks() to allocate any delayed allocation blocks, or
2257 * insert nrblocks contiguous block is dependent on 2087 * to convert an uninitialized extent to be initialized (in the case
2258 * number of contiguous block. So we will limit 2088 * where we have written into one or more preallocated blocks). It is
2259 * number of contiguous block to a sane value 2089 * possible that we're going to need more metadata blocks than
2090 * previously reserved. However we must not fail because we're in
2091 * writeback and there is nothing we can do about it so it might result
2092 * in data loss. So use reserved blocks to allocate metadata if
2093 * possible.
2094 *
2095 * We pass in the magic EXT4_GET_BLOCKS_DELALLOC_RESERVE if the blocks
2096 * in question are delalloc blocks. This affects functions in many
2097 * different parts of the allocation call path. This flag exists
2098 * primarily because we don't want to change *many* call functions, so
2099 * ext4_map_blocks() will set the EXT4_STATE_DELALLOC_RESERVED flag
2100 * once the inode's allocation semaphore is taken.
2260 */ 2101 */
2261 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) && 2102 get_blocks_flags = EXT4_GET_BLOCKS_CREATE |
2262 (max_blocks > EXT4_MAX_TRANS_DATA)) 2103 EXT4_GET_BLOCKS_METADATA_NOFAIL;
2263 max_blocks = EXT4_MAX_TRANS_DATA; 2104 if (ext4_should_dioread_nolock(inode))
2105 get_blocks_flags |= EXT4_GET_BLOCKS_IO_CREATE_EXT;
2106 if (map->m_flags & (1 << BH_Delay))
2107 get_blocks_flags |= EXT4_GET_BLOCKS_DELALLOC_RESERVE;
2264 2108
2265 return ext4_chunk_trans_blocks(inode, max_blocks); 2109 err = ext4_map_blocks(handle, inode, map, get_blocks_flags);
2110 if (err < 0)
2111 return err;
2112 if (map->m_flags & EXT4_MAP_UNINIT) {
2113 if (!mpd->io_submit.io_end->handle &&
2114 ext4_handle_valid(handle)) {
2115 mpd->io_submit.io_end->handle = handle->h_rsv_handle;
2116 handle->h_rsv_handle = NULL;
2117 }
2118 ext4_set_io_unwritten_flag(inode, mpd->io_submit.io_end);
2119 }
2120
2121 BUG_ON(map->m_len == 0);
2122 if (map->m_flags & EXT4_MAP_NEW) {
2123 struct block_device *bdev = inode->i_sb->s_bdev;
2124 int i;
2125
2126 for (i = 0; i < map->m_len; i++)
2127 unmap_underlying_metadata(bdev, map->m_pblk + i);
2128 }
2129 return 0;
2266} 2130}
2267 2131
2268/* 2132/*
2269 * write_cache_pages_da - walk the list of dirty pages of the given 2133 * mpage_map_and_submit_extent - map extent starting at mpd->lblk of length
2270 * address space and accumulate pages that need writing, and call 2134 * mpd->len and submit pages underlying it for IO
2271 * mpage_da_map_and_submit to map a single contiguous memory region 2135 *
2272 * and then write them. 2136 * @handle - handle for journal operations
2137 * @mpd - extent to map
2138 *
2139 * The function maps extent starting at mpd->lblk of length mpd->len. If it is
2140 * delayed, blocks are allocated, if it is unwritten, we may need to convert
2141 * them to initialized or split the described range from larger unwritten
2142 * extent. Note that we need not map all the described range since allocation
2143 * can return less blocks or the range is covered by more unwritten extents. We
2144 * cannot map more because we are limited by reserved transaction credits. On
2145 * the other hand we always make sure that the last touched page is fully
2146 * mapped so that it can be written out (and thus forward progress is
2147 * guaranteed). After mapping we submit all mapped pages for IO.
2273 */ 2148 */
2274static int write_cache_pages_da(handle_t *handle, 2149static int mpage_map_and_submit_extent(handle_t *handle,
2275 struct address_space *mapping, 2150 struct mpage_da_data *mpd,
2276 struct writeback_control *wbc, 2151 bool *give_up_on_write)
2277 struct mpage_da_data *mpd,
2278 pgoff_t *done_index)
2279{ 2152{
2280 struct buffer_head *bh, *head; 2153 struct inode *inode = mpd->inode;
2281 struct inode *inode = mapping->host; 2154 struct ext4_map_blocks *map = &mpd->map;
2282 struct pagevec pvec; 2155 int err;
2283 unsigned int nr_pages; 2156 loff_t disksize;
2284 sector_t logical;
2285 pgoff_t index, end;
2286 long nr_to_write = wbc->nr_to_write;
2287 int i, tag, ret = 0;
2288
2289 memset(mpd, 0, sizeof(struct mpage_da_data));
2290 mpd->wbc = wbc;
2291 mpd->inode = inode;
2292 pagevec_init(&pvec, 0);
2293 index = wbc->range_start >> PAGE_CACHE_SHIFT;
2294 end = wbc->range_end >> PAGE_CACHE_SHIFT;
2295 2157
2296 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) 2158 mpd->io_submit.io_end->offset =
2159 ((loff_t)map->m_lblk) << inode->i_blkbits;
2160 do {
2161 err = mpage_map_one_extent(handle, mpd);
2162 if (err < 0) {
2163 struct super_block *sb = inode->i_sb;
2164
2165 if (EXT4_SB(sb)->s_mount_flags & EXT4_MF_FS_ABORTED)
2166 goto invalidate_dirty_pages;
2167 /*
2168 * Let the uper layers retry transient errors.
2169 * In the case of ENOSPC, if ext4_count_free_blocks()
2170 * is non-zero, a commit should free up blocks.
2171 */
2172 if ((err == -ENOMEM) ||
2173 (err == -ENOSPC && ext4_count_free_clusters(sb)))
2174 return err;
2175 ext4_msg(sb, KERN_CRIT,
2176 "Delayed block allocation failed for "
2177 "inode %lu at logical offset %llu with"
2178 " max blocks %u with error %d",
2179 inode->i_ino,
2180 (unsigned long long)map->m_lblk,
2181 (unsigned)map->m_len, -err);
2182 ext4_msg(sb, KERN_CRIT,
2183 "This should not happen!! Data will "
2184 "be lost\n");
2185 if (err == -ENOSPC)
2186 ext4_print_free_blocks(inode);
2187 invalidate_dirty_pages:
2188 *give_up_on_write = true;
2189 return err;
2190 }
2191 /*
2192 * Update buffer state, submit mapped pages, and get us new
2193 * extent to map
2194 */
2195 err = mpage_map_and_submit_buffers(mpd);
2196 if (err < 0)
2197 return err;
2198 } while (map->m_len);
2199
2200 /* Update on-disk size after IO is submitted */
2201 disksize = ((loff_t)mpd->first_page) << PAGE_CACHE_SHIFT;
2202 if (disksize > i_size_read(inode))
2203 disksize = i_size_read(inode);
2204 if (disksize > EXT4_I(inode)->i_disksize) {
2205 int err2;
2206
2207 ext4_update_i_disksize(inode, disksize);
2208 err2 = ext4_mark_inode_dirty(handle, inode);
2209 if (err2)
2210 ext4_error(inode->i_sb,
2211 "Failed to mark inode %lu dirty",
2212 inode->i_ino);
2213 if (!err)
2214 err = err2;
2215 }
2216 return err;
2217}
2218
2219/*
2220 * Calculate the total number of credits to reserve for one writepages
2221 * iteration. This is called from ext4_writepages(). We map an extent of
2222 * upto MAX_WRITEPAGES_EXTENT_LEN blocks and then we go on and finish mapping
2223 * the last partial page. So in total we can map MAX_WRITEPAGES_EXTENT_LEN +
2224 * bpp - 1 blocks in bpp different extents.
2225 */
2226static int ext4_da_writepages_trans_blocks(struct inode *inode)
2227{
2228 int bpp = ext4_journal_blocks_per_page(inode);
2229
2230 return ext4_meta_trans_blocks(inode,
2231 MAX_WRITEPAGES_EXTENT_LEN + bpp - 1, bpp);
2232}
2233
2234/*
2235 * mpage_prepare_extent_to_map - find & lock contiguous range of dirty pages
2236 * and underlying extent to map
2237 *
2238 * @mpd - where to look for pages
2239 *
2240 * Walk dirty pages in the mapping. If they are fully mapped, submit them for
2241 * IO immediately. When we find a page which isn't mapped we start accumulating
2242 * extent of buffers underlying these pages that needs mapping (formed by
2243 * either delayed or unwritten buffers). We also lock the pages containing
2244 * these buffers. The extent found is returned in @mpd structure (starting at
2245 * mpd->lblk with length mpd->len blocks).
2246 *
2247 * Note that this function can attach bios to one io_end structure which are
2248 * neither logically nor physically contiguous. Although it may seem as an
2249 * unnecessary complication, it is actually inevitable in blocksize < pagesize
2250 * case as we need to track IO to all buffers underlying a page in one io_end.
2251 */
2252static int mpage_prepare_extent_to_map(struct mpage_da_data *mpd)
2253{
2254 struct address_space *mapping = mpd->inode->i_mapping;
2255 struct pagevec pvec;
2256 unsigned int nr_pages;
2257 pgoff_t index = mpd->first_page;
2258 pgoff_t end = mpd->last_page;
2259 int tag;
2260 int i, err = 0;
2261 int blkbits = mpd->inode->i_blkbits;
2262 ext4_lblk_t lblk;
2263 struct buffer_head *head;
2264
2265 if (mpd->wbc->sync_mode == WB_SYNC_ALL || mpd->wbc->tagged_writepages)
2297 tag = PAGECACHE_TAG_TOWRITE; 2266 tag = PAGECACHE_TAG_TOWRITE;
2298 else 2267 else
2299 tag = PAGECACHE_TAG_DIRTY; 2268 tag = PAGECACHE_TAG_DIRTY;
2300 2269
2301 *done_index = index; 2270 pagevec_init(&pvec, 0);
2271 mpd->map.m_len = 0;
2272 mpd->next_page = index;
2302 while (index <= end) { 2273 while (index <= end) {
2303 nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag, 2274 nr_pages = pagevec_lookup_tag(&pvec, mapping, &index, tag,
2304 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1); 2275 min(end - index, (pgoff_t)PAGEVEC_SIZE-1) + 1);
2305 if (nr_pages == 0) 2276 if (nr_pages == 0)
2306 return 0; 2277 goto out;
2307 2278
2308 for (i = 0; i < nr_pages; i++) { 2279 for (i = 0; i < nr_pages; i++) {
2309 struct page *page = pvec.pages[i]; 2280 struct page *page = pvec.pages[i];
@@ -2318,31 +2289,21 @@ static int write_cache_pages_da(handle_t *handle,
2318 if (page->index > end) 2289 if (page->index > end)
2319 goto out; 2290 goto out;
2320 2291
2321 *done_index = page->index + 1; 2292 /* If we can't merge this page, we are done. */
2322 2293 if (mpd->map.m_len > 0 && mpd->next_page != page->index)
2323 /* 2294 goto out;
2324 * If we can't merge this page, and we have
2325 * accumulated an contiguous region, write it
2326 */
2327 if ((mpd->next_page != page->index) &&
2328 (mpd->next_page != mpd->first_page)) {
2329 mpage_da_map_and_submit(mpd);
2330 goto ret_extent_tail;
2331 }
2332 2295
2333 lock_page(page); 2296 lock_page(page);
2334
2335 /* 2297 /*
2336 * If the page is no longer dirty, or its 2298 * If the page is no longer dirty, or its mapping no
2337 * mapping no longer corresponds to inode we 2299 * longer corresponds to inode we are writing (which
2338 * are writing (which means it has been 2300 * means it has been truncated or invalidated), or the
2339 * truncated or invalidated), or the page is 2301 * page is already under writeback and we are not doing
2340 * already under writeback and we are not 2302 * a data integrity writeback, skip the page
2341 * doing a data integrity writeback, skip the page
2342 */ 2303 */
2343 if (!PageDirty(page) || 2304 if (!PageDirty(page) ||
2344 (PageWriteback(page) && 2305 (PageWriteback(page) &&
2345 (wbc->sync_mode == WB_SYNC_NONE)) || 2306 (mpd->wbc->sync_mode == WB_SYNC_NONE)) ||
2346 unlikely(page->mapping != mapping)) { 2307 unlikely(page->mapping != mapping)) {
2347 unlock_page(page); 2308 unlock_page(page);
2348 continue; 2309 continue;
@@ -2351,106 +2312,70 @@ static int write_cache_pages_da(handle_t *handle,
2351 wait_on_page_writeback(page); 2312 wait_on_page_writeback(page);
2352 BUG_ON(PageWriteback(page)); 2313 BUG_ON(PageWriteback(page));
2353 2314
2354 /* 2315 if (mpd->map.m_len == 0)
2355 * If we have inline data and arrive here, it means that
2356 * we will soon create the block for the 1st page, so
2357 * we'd better clear the inline data here.
2358 */
2359 if (ext4_has_inline_data(inode)) {
2360 BUG_ON(ext4_test_inode_state(inode,
2361 EXT4_STATE_MAY_INLINE_DATA));
2362 ext4_destroy_inline_data(handle, inode);
2363 }
2364
2365 if (mpd->next_page != page->index)
2366 mpd->first_page = page->index; 2316 mpd->first_page = page->index;
2367 mpd->next_page = page->index + 1; 2317 mpd->next_page = page->index + 1;
2368 logical = (sector_t) page->index <<
2369 (PAGE_CACHE_SHIFT - inode->i_blkbits);
2370
2371 /* Add all dirty buffers to mpd */ 2318 /* Add all dirty buffers to mpd */
2319 lblk = ((ext4_lblk_t)page->index) <<
2320 (PAGE_CACHE_SHIFT - blkbits);
2372 head = page_buffers(page); 2321 head = page_buffers(page);
2373 bh = head; 2322 if (!add_page_bufs_to_extent(mpd, head, head, lblk))
2374 do { 2323 goto out;
2375 BUG_ON(buffer_locked(bh)); 2324 /* So far everything mapped? Submit the page for IO. */
2376 /* 2325 if (mpd->map.m_len == 0) {
2377 * We need to try to allocate unmapped blocks 2326 err = mpage_submit_page(mpd, page);
2378 * in the same page. Otherwise we won't make 2327 if (err < 0)
2379 * progress with the page in ext4_writepage
2380 */
2381 if (ext4_bh_delay_or_unwritten(NULL, bh)) {
2382 mpage_add_bh_to_extent(mpd, logical,
2383 bh->b_state);
2384 if (mpd->io_done)
2385 goto ret_extent_tail;
2386 } else if (buffer_dirty(bh) &&
2387 buffer_mapped(bh)) {
2388 /*
2389 * mapped dirty buffer. We need to
2390 * update the b_state because we look
2391 * at b_state in mpage_da_map_blocks.
2392 * We don't update b_size because if we
2393 * find an unmapped buffer_head later
2394 * we need to use the b_state flag of
2395 * that buffer_head.
2396 */
2397 if (mpd->b_size == 0)
2398 mpd->b_state =
2399 bh->b_state & BH_FLAGS;
2400 }
2401 logical++;
2402 } while ((bh = bh->b_this_page) != head);
2403
2404 if (nr_to_write > 0) {
2405 nr_to_write--;
2406 if (nr_to_write == 0 &&
2407 wbc->sync_mode == WB_SYNC_NONE)
2408 /*
2409 * We stop writing back only if we are
2410 * not doing integrity sync. In case of
2411 * integrity sync we have to keep going
2412 * because someone may be concurrently
2413 * dirtying pages, and we might have
2414 * synced a lot of newly appeared dirty
2415 * pages, but have not synced all of the
2416 * old dirty pages.
2417 */
2418 goto out; 2328 goto out;
2419 } 2329 }
2330
2331 /*
2332 * Accumulated enough dirty pages? This doesn't apply
2333 * to WB_SYNC_ALL mode. For integrity sync we have to
2334 * keep going because someone may be concurrently
2335 * dirtying pages, and we might have synced a lot of
2336 * newly appeared dirty pages, but have not synced all
2337 * of the old dirty pages.
2338 */
2339 if (mpd->wbc->sync_mode == WB_SYNC_NONE &&
2340 mpd->next_page - mpd->first_page >=
2341 mpd->wbc->nr_to_write)
2342 goto out;
2420 } 2343 }
2421 pagevec_release(&pvec); 2344 pagevec_release(&pvec);
2422 cond_resched(); 2345 cond_resched();
2423 } 2346 }
2424 return 0; 2347 return 0;
2425ret_extent_tail:
2426 ret = MPAGE_DA_EXTENT_TAIL;
2427out: 2348out:
2428 pagevec_release(&pvec); 2349 pagevec_release(&pvec);
2429 cond_resched(); 2350 return err;
2430 return ret;
2431} 2351}
2432 2352
2353static int __writepage(struct page *page, struct writeback_control *wbc,
2354 void *data)
2355{
2356 struct address_space *mapping = data;
2357 int ret = ext4_writepage(page, wbc);
2358 mapping_set_error(mapping, ret);
2359 return ret;
2360}
2433 2361
2434static int ext4_da_writepages(struct address_space *mapping, 2362static int ext4_writepages(struct address_space *mapping,
2435 struct writeback_control *wbc) 2363 struct writeback_control *wbc)
2436{ 2364{
2437 pgoff_t index; 2365 pgoff_t writeback_index = 0;
2366 long nr_to_write = wbc->nr_to_write;
2438 int range_whole = 0; 2367 int range_whole = 0;
2368 int cycled = 1;
2439 handle_t *handle = NULL; 2369 handle_t *handle = NULL;
2440 struct mpage_da_data mpd; 2370 struct mpage_da_data mpd;
2441 struct inode *inode = mapping->host; 2371 struct inode *inode = mapping->host;
2442 int pages_written = 0; 2372 int needed_blocks, rsv_blocks = 0, ret = 0;
2443 unsigned int max_pages;
2444 int range_cyclic, cycled = 1, io_done = 0;
2445 int needed_blocks, ret = 0;
2446 long desired_nr_to_write, nr_to_writebump = 0;
2447 loff_t range_start = wbc->range_start;
2448 struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb); 2373 struct ext4_sb_info *sbi = EXT4_SB(mapping->host->i_sb);
2449 pgoff_t done_index = 0; 2374 bool done;
2450 pgoff_t end;
2451 struct blk_plug plug; 2375 struct blk_plug plug;
2376 bool give_up_on_write = false;
2452 2377
2453 trace_ext4_da_writepages(inode, wbc); 2378 trace_ext4_writepages(inode, wbc);
2454 2379
2455 /* 2380 /*
2456 * No pages to write? This is mainly a kludge to avoid starting 2381 * No pages to write? This is mainly a kludge to avoid starting
@@ -2460,164 +2385,165 @@ static int ext4_da_writepages(struct address_space *mapping,
2460 if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) 2385 if (!mapping->nrpages || !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY))
2461 return 0; 2386 return 0;
2462 2387
2388 if (ext4_should_journal_data(inode)) {
2389 struct blk_plug plug;
2390 int ret;
2391
2392 blk_start_plug(&plug);
2393 ret = write_cache_pages(mapping, wbc, __writepage, mapping);
2394 blk_finish_plug(&plug);
2395 return ret;
2396 }
2397
2463 /* 2398 /*
2464 * If the filesystem has aborted, it is read-only, so return 2399 * If the filesystem has aborted, it is read-only, so return
2465 * right away instead of dumping stack traces later on that 2400 * right away instead of dumping stack traces later on that
2466 * will obscure the real source of the problem. We test 2401 * will obscure the real source of the problem. We test
2467 * EXT4_MF_FS_ABORTED instead of sb->s_flag's MS_RDONLY because 2402 * EXT4_MF_FS_ABORTED instead of sb->s_flag's MS_RDONLY because
2468 * the latter could be true if the filesystem is mounted 2403 * the latter could be true if the filesystem is mounted
2469 * read-only, and in that case, ext4_da_writepages should 2404 * read-only, and in that case, ext4_writepages should
2470 * *never* be called, so if that ever happens, we would want 2405 * *never* be called, so if that ever happens, we would want
2471 * the stack trace. 2406 * the stack trace.
2472 */ 2407 */
2473 if (unlikely(sbi->s_mount_flags & EXT4_MF_FS_ABORTED)) 2408 if (unlikely(sbi->s_mount_flags & EXT4_MF_FS_ABORTED))
2474 return -EROFS; 2409 return -EROFS;
2475 2410
2411 if (ext4_should_dioread_nolock(inode)) {
2412 /*
2413 * We may need to convert upto one extent per block in
2414 * the page and we may dirty the inode.
2415 */
2416 rsv_blocks = 1 + (PAGE_CACHE_SIZE >> inode->i_blkbits);
2417 }
2418
2419 /*
2420 * If we have inline data and arrive here, it means that
2421 * we will soon create the block for the 1st page, so
2422 * we'd better clear the inline data here.
2423 */
2424 if (ext4_has_inline_data(inode)) {
2425 /* Just inode will be modified... */
2426 handle = ext4_journal_start(inode, EXT4_HT_INODE, 1);
2427 if (IS_ERR(handle)) {
2428 ret = PTR_ERR(handle);
2429 goto out_writepages;
2430 }
2431 BUG_ON(ext4_test_inode_state(inode,
2432 EXT4_STATE_MAY_INLINE_DATA));
2433 ext4_destroy_inline_data(handle, inode);
2434 ext4_journal_stop(handle);
2435 }
2436
2476 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) 2437 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
2477 range_whole = 1; 2438 range_whole = 1;
2478 2439
2479 range_cyclic = wbc->range_cyclic;
2480 if (wbc->range_cyclic) { 2440 if (wbc->range_cyclic) {
2481 index = mapping->writeback_index; 2441 writeback_index = mapping->writeback_index;
2482 if (index) 2442 if (writeback_index)
2483 cycled = 0; 2443 cycled = 0;
2484 wbc->range_start = index << PAGE_CACHE_SHIFT; 2444 mpd.first_page = writeback_index;
2485 wbc->range_end = LLONG_MAX; 2445 mpd.last_page = -1;
2486 wbc->range_cyclic = 0;
2487 end = -1;
2488 } else { 2446 } else {
2489 index = wbc->range_start >> PAGE_CACHE_SHIFT; 2447 mpd.first_page = wbc->range_start >> PAGE_CACHE_SHIFT;
2490 end = wbc->range_end >> PAGE_CACHE_SHIFT; 2448 mpd.last_page = wbc->range_end >> PAGE_CACHE_SHIFT;
2491 }
2492
2493 /*
2494 * This works around two forms of stupidity. The first is in
2495 * the writeback code, which caps the maximum number of pages
2496 * written to be 1024 pages. This is wrong on multiple
2497 * levels; different architectues have a different page size,
2498 * which changes the maximum amount of data which gets
2499 * written. Secondly, 4 megabytes is way too small. XFS
2500 * forces this value to be 16 megabytes by multiplying
2501 * nr_to_write parameter by four, and then relies on its
2502 * allocator to allocate larger extents to make them
2503 * contiguous. Unfortunately this brings us to the second
2504 * stupidity, which is that ext4's mballoc code only allocates
2505 * at most 2048 blocks. So we force contiguous writes up to
2506 * the number of dirty blocks in the inode, or
2507 * sbi->max_writeback_mb_bump whichever is smaller.
2508 */
2509 max_pages = sbi->s_max_writeback_mb_bump << (20 - PAGE_CACHE_SHIFT);
2510 if (!range_cyclic && range_whole) {
2511 if (wbc->nr_to_write == LONG_MAX)
2512 desired_nr_to_write = wbc->nr_to_write;
2513 else
2514 desired_nr_to_write = wbc->nr_to_write * 8;
2515 } else
2516 desired_nr_to_write = ext4_num_dirty_pages(inode, index,
2517 max_pages);
2518 if (desired_nr_to_write > max_pages)
2519 desired_nr_to_write = max_pages;
2520
2521 if (wbc->nr_to_write < desired_nr_to_write) {
2522 nr_to_writebump = desired_nr_to_write - wbc->nr_to_write;
2523 wbc->nr_to_write = desired_nr_to_write;
2524 } 2449 }
2525 2450
2451 mpd.inode = inode;
2452 mpd.wbc = wbc;
2453 ext4_io_submit_init(&mpd.io_submit, wbc);
2526retry: 2454retry:
2527 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages) 2455 if (wbc->sync_mode == WB_SYNC_ALL || wbc->tagged_writepages)
2528 tag_pages_for_writeback(mapping, index, end); 2456 tag_pages_for_writeback(mapping, mpd.first_page, mpd.last_page);
2529 2457 done = false;
2530 blk_start_plug(&plug); 2458 blk_start_plug(&plug);
2531 while (!ret && wbc->nr_to_write > 0) { 2459 while (!done && mpd.first_page <= mpd.last_page) {
2460 /* For each extent of pages we use new io_end */
2461 mpd.io_submit.io_end = ext4_init_io_end(inode, GFP_KERNEL);
2462 if (!mpd.io_submit.io_end) {
2463 ret = -ENOMEM;
2464 break;
2465 }
2532 2466
2533 /* 2467 /*
2534 * we insert one extent at a time. So we need 2468 * We have two constraints: We find one extent to map and we
2535 * credit needed for single extent allocation. 2469 * must always write out whole page (makes a difference when
2536 * journalled mode is currently not supported 2470 * blocksize < pagesize) so that we don't block on IO when we
2537 * by delalloc 2471 * try to write out the rest of the page. Journalled mode is
2472 * not supported by delalloc.
2538 */ 2473 */
2539 BUG_ON(ext4_should_journal_data(inode)); 2474 BUG_ON(ext4_should_journal_data(inode));
2540 needed_blocks = ext4_da_writepages_trans_blocks(inode); 2475 needed_blocks = ext4_da_writepages_trans_blocks(inode);
2541 2476
2542 /* start a new transaction*/ 2477 /* start a new transaction */
2543 handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, 2478 handle = ext4_journal_start_with_reserve(inode,
2544 needed_blocks); 2479 EXT4_HT_WRITE_PAGE, needed_blocks, rsv_blocks);
2545 if (IS_ERR(handle)) { 2480 if (IS_ERR(handle)) {
2546 ret = PTR_ERR(handle); 2481 ret = PTR_ERR(handle);
2547 ext4_msg(inode->i_sb, KERN_CRIT, "%s: jbd2_start: " 2482 ext4_msg(inode->i_sb, KERN_CRIT, "%s: jbd2_start: "
2548 "%ld pages, ino %lu; err %d", __func__, 2483 "%ld pages, ino %lu; err %d", __func__,
2549 wbc->nr_to_write, inode->i_ino, ret); 2484 wbc->nr_to_write, inode->i_ino, ret);
2550 blk_finish_plug(&plug); 2485 /* Release allocated io_end */
2551 goto out_writepages; 2486 ext4_put_io_end(mpd.io_submit.io_end);
2487 break;
2552 } 2488 }
2553 2489
2554 /* 2490 trace_ext4_da_write_pages(inode, mpd.first_page, mpd.wbc);
2555 * Now call write_cache_pages_da() to find the next 2491 ret = mpage_prepare_extent_to_map(&mpd);
2556 * contiguous region of logical blocks that need 2492 if (!ret) {
2557 * blocks to be allocated by ext4 and submit them. 2493 if (mpd.map.m_len)
2558 */ 2494 ret = mpage_map_and_submit_extent(handle, &mpd,
2559 ret = write_cache_pages_da(handle, mapping, 2495 &give_up_on_write);
2560 wbc, &mpd, &done_index); 2496 else {
2561 /* 2497 /*
2562 * If we have a contiguous extent of pages and we 2498 * We scanned the whole range (or exhausted
2563 * haven't done the I/O yet, map the blocks and submit 2499 * nr_to_write), submitted what was mapped and
2564 * them for I/O. 2500 * didn't find anything needing mapping. We are
2565 */ 2501 * done.
2566 if (!mpd.io_done && mpd.next_page != mpd.first_page) { 2502 */
2567 mpage_da_map_and_submit(&mpd); 2503 done = true;
2568 ret = MPAGE_DA_EXTENT_TAIL; 2504 }
2569 } 2505 }
2570 trace_ext4_da_write_pages(inode, &mpd);
2571 wbc->nr_to_write -= mpd.pages_written;
2572
2573 ext4_journal_stop(handle); 2506 ext4_journal_stop(handle);
2574 2507 /* Submit prepared bio */
2575 if ((mpd.retval == -ENOSPC) && sbi->s_journal) { 2508 ext4_io_submit(&mpd.io_submit);
2576 /* commit the transaction which would 2509 /* Unlock pages we didn't use */
2510 mpage_release_unused_pages(&mpd, give_up_on_write);
2511 /* Drop our io_end reference we got from init */
2512 ext4_put_io_end(mpd.io_submit.io_end);
2513
2514 if (ret == -ENOSPC && sbi->s_journal) {
2515 /*
2516 * Commit the transaction which would
2577 * free blocks released in the transaction 2517 * free blocks released in the transaction
2578 * and try again 2518 * and try again
2579 */ 2519 */
2580 jbd2_journal_force_commit_nested(sbi->s_journal); 2520 jbd2_journal_force_commit_nested(sbi->s_journal);
2581 ret = 0; 2521 ret = 0;
2582 } else if (ret == MPAGE_DA_EXTENT_TAIL) { 2522 continue;
2583 /* 2523 }
2584 * Got one extent now try with rest of the pages. 2524 /* Fatal error - ENOMEM, EIO... */
2585 * If mpd.retval is set -EIO, journal is aborted. 2525 if (ret)
2586 * So we don't need to write any more.
2587 */
2588 pages_written += mpd.pages_written;
2589 ret = mpd.retval;
2590 io_done = 1;
2591 } else if (wbc->nr_to_write)
2592 /*
2593 * There is no more writeout needed
2594 * or we requested for a noblocking writeout
2595 * and we found the device congested
2596 */
2597 break; 2526 break;
2598 } 2527 }
2599 blk_finish_plug(&plug); 2528 blk_finish_plug(&plug);
2600 if (!io_done && !cycled) { 2529 if (!ret && !cycled) {
2601 cycled = 1; 2530 cycled = 1;
2602 index = 0; 2531 mpd.last_page = writeback_index - 1;
2603 wbc->range_start = index << PAGE_CACHE_SHIFT; 2532 mpd.first_page = 0;
2604 wbc->range_end = mapping->writeback_index - 1;
2605 goto retry; 2533 goto retry;
2606 } 2534 }
2607 2535
2608 /* Update index */ 2536 /* Update index */
2609 wbc->range_cyclic = range_cyclic;
2610 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) 2537 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
2611 /* 2538 /*
2612 * set the writeback_index so that range_cyclic 2539 * Set the writeback_index so that range_cyclic
2613 * mode will write it back later 2540 * mode will write it back later
2614 */ 2541 */
2615 mapping->writeback_index = done_index; 2542 mapping->writeback_index = mpd.first_page;
2616 2543
2617out_writepages: 2544out_writepages:
2618 wbc->nr_to_write -= nr_to_writebump; 2545 trace_ext4_writepages_result(inode, wbc, ret,
2619 wbc->range_start = range_start; 2546 nr_to_write - wbc->nr_to_write);
2620 trace_ext4_da_writepages_result(inode, wbc, ret, pages_written);
2621 return ret; 2547 return ret;
2622} 2548}
2623 2549
@@ -2829,7 +2755,8 @@ static int ext4_da_write_end(struct file *file,
2829 return ret ? ret : copied; 2755 return ret ? ret : copied;
2830} 2756}
2831 2757
2832static void ext4_da_invalidatepage(struct page *page, unsigned long offset) 2758static void ext4_da_invalidatepage(struct page *page, unsigned int offset,
2759 unsigned int length)
2833{ 2760{
2834 /* 2761 /*
2835 * Drop reserved blocks 2762 * Drop reserved blocks
@@ -2838,10 +2765,10 @@ static void ext4_da_invalidatepage(struct page *page, unsigned long offset)
2838 if (!page_has_buffers(page)) 2765 if (!page_has_buffers(page))
2839 goto out; 2766 goto out;
2840 2767
2841 ext4_da_page_release_reservation(page, offset); 2768 ext4_da_page_release_reservation(page, offset, length);
2842 2769
2843out: 2770out:
2844 ext4_invalidatepage(page, offset); 2771 ext4_invalidatepage(page, offset, length);
2845 2772
2846 return; 2773 return;
2847} 2774}
@@ -2864,7 +2791,7 @@ int ext4_alloc_da_blocks(struct inode *inode)
2864 * laptop_mode, not even desirable). However, to do otherwise 2791 * laptop_mode, not even desirable). However, to do otherwise
2865 * would require replicating code paths in: 2792 * would require replicating code paths in:
2866 * 2793 *
2867 * ext4_da_writepages() -> 2794 * ext4_writepages() ->
2868 * write_cache_pages() ---> (via passed in callback function) 2795 * write_cache_pages() ---> (via passed in callback function)
2869 * __mpage_da_writepage() --> 2796 * __mpage_da_writepage() -->
2870 * mpage_add_bh_to_extent() 2797 * mpage_add_bh_to_extent()
@@ -2989,37 +2916,40 @@ ext4_readpages(struct file *file, struct address_space *mapping,
2989 return mpage_readpages(mapping, pages, nr_pages, ext4_get_block); 2916 return mpage_readpages(mapping, pages, nr_pages, ext4_get_block);
2990} 2917}
2991 2918
2992static void ext4_invalidatepage(struct page *page, unsigned long offset) 2919static void ext4_invalidatepage(struct page *page, unsigned int offset,
2920 unsigned int length)
2993{ 2921{
2994 trace_ext4_invalidatepage(page, offset); 2922 trace_ext4_invalidatepage(page, offset, length);
2995 2923
2996 /* No journalling happens on data buffers when this function is used */ 2924 /* No journalling happens on data buffers when this function is used */
2997 WARN_ON(page_has_buffers(page) && buffer_jbd(page_buffers(page))); 2925 WARN_ON(page_has_buffers(page) && buffer_jbd(page_buffers(page)));
2998 2926
2999 block_invalidatepage(page, offset); 2927 block_invalidatepage(page, offset, length);
3000} 2928}
3001 2929
3002static int __ext4_journalled_invalidatepage(struct page *page, 2930static int __ext4_journalled_invalidatepage(struct page *page,
3003 unsigned long offset) 2931 unsigned int offset,
2932 unsigned int length)
3004{ 2933{
3005 journal_t *journal = EXT4_JOURNAL(page->mapping->host); 2934 journal_t *journal = EXT4_JOURNAL(page->mapping->host);
3006 2935
3007 trace_ext4_journalled_invalidatepage(page, offset); 2936 trace_ext4_journalled_invalidatepage(page, offset, length);
3008 2937
3009 /* 2938 /*
3010 * If it's a full truncate we just forget about the pending dirtying 2939 * If it's a full truncate we just forget about the pending dirtying
3011 */ 2940 */
3012 if (offset == 0) 2941 if (offset == 0 && length == PAGE_CACHE_SIZE)
3013 ClearPageChecked(page); 2942 ClearPageChecked(page);
3014 2943
3015 return jbd2_journal_invalidatepage(journal, page, offset); 2944 return jbd2_journal_invalidatepage(journal, page, offset, length);
3016} 2945}
3017 2946
3018/* Wrapper for aops... */ 2947/* Wrapper for aops... */
3019static void ext4_journalled_invalidatepage(struct page *page, 2948static void ext4_journalled_invalidatepage(struct page *page,
3020 unsigned long offset) 2949 unsigned int offset,
2950 unsigned int length)
3021{ 2951{
3022 WARN_ON(__ext4_journalled_invalidatepage(page, offset) < 0); 2952 WARN_ON(__ext4_journalled_invalidatepage(page, offset, length) < 0);
3023} 2953}
3024 2954
3025static int ext4_releasepage(struct page *page, gfp_t wait) 2955static int ext4_releasepage(struct page *page, gfp_t wait)
@@ -3067,9 +2997,13 @@ static void ext4_end_io_dio(struct kiocb *iocb, loff_t offset,
3067 struct inode *inode = file_inode(iocb->ki_filp); 2997 struct inode *inode = file_inode(iocb->ki_filp);
3068 ext4_io_end_t *io_end = iocb->private; 2998 ext4_io_end_t *io_end = iocb->private;
3069 2999
3070 /* if not async direct IO or dio with 0 bytes write, just return */ 3000 /* if not async direct IO just return */
3071 if (!io_end || !size) 3001 if (!io_end) {
3072 goto out; 3002 inode_dio_done(inode);
3003 if (is_async)
3004 aio_complete(iocb, ret, 0);
3005 return;
3006 }
3073 3007
3074 ext_debug("ext4_end_io_dio(): io_end 0x%p " 3008 ext_debug("ext4_end_io_dio(): io_end 0x%p "
3075 "for inode %lu, iocb 0x%p, offset %llu, size %zd\n", 3009 "for inode %lu, iocb 0x%p, offset %llu, size %zd\n",
@@ -3077,25 +3011,13 @@ static void ext4_end_io_dio(struct kiocb *iocb, loff_t offset,
3077 size); 3011 size);
3078 3012
3079 iocb->private = NULL; 3013 iocb->private = NULL;
3080
3081 /* if not aio dio with unwritten extents, just free io and return */
3082 if (!(io_end->flag & EXT4_IO_END_UNWRITTEN)) {
3083 ext4_free_io_end(io_end);
3084out:
3085 inode_dio_done(inode);
3086 if (is_async)
3087 aio_complete(iocb, ret, 0);
3088 return;
3089 }
3090
3091 io_end->offset = offset; 3014 io_end->offset = offset;
3092 io_end->size = size; 3015 io_end->size = size;
3093 if (is_async) { 3016 if (is_async) {
3094 io_end->iocb = iocb; 3017 io_end->iocb = iocb;
3095 io_end->result = ret; 3018 io_end->result = ret;
3096 } 3019 }
3097 3020 ext4_put_io_end_defer(io_end);
3098 ext4_add_complete_io(io_end);
3099} 3021}
3100 3022
3101/* 3023/*
@@ -3129,6 +3051,7 @@ static ssize_t ext4_ext_direct_IO(int rw, struct kiocb *iocb,
3129 get_block_t *get_block_func = NULL; 3051 get_block_t *get_block_func = NULL;
3130 int dio_flags = 0; 3052 int dio_flags = 0;
3131 loff_t final_size = offset + count; 3053 loff_t final_size = offset + count;
3054 ext4_io_end_t *io_end = NULL;
3132 3055
3133 /* Use the old path for reads and writes beyond i_size. */ 3056 /* Use the old path for reads and writes beyond i_size. */
3134 if (rw != WRITE || final_size > inode->i_size) 3057 if (rw != WRITE || final_size > inode->i_size)
@@ -3136,11 +3059,18 @@ static ssize_t ext4_ext_direct_IO(int rw, struct kiocb *iocb,
3136 3059
3137 BUG_ON(iocb->private == NULL); 3060 BUG_ON(iocb->private == NULL);
3138 3061
3062 /*
3063 * Make all waiters for direct IO properly wait also for extent
3064 * conversion. This also disallows race between truncate() and
3065 * overwrite DIO as i_dio_count needs to be incremented under i_mutex.
3066 */
3067 if (rw == WRITE)
3068 atomic_inc(&inode->i_dio_count);
3069
3139 /* If we do a overwrite dio, i_mutex locking can be released */ 3070 /* If we do a overwrite dio, i_mutex locking can be released */
3140 overwrite = *((int *)iocb->private); 3071 overwrite = *((int *)iocb->private);
3141 3072
3142 if (overwrite) { 3073 if (overwrite) {
3143 atomic_inc(&inode->i_dio_count);
3144 down_read(&EXT4_I(inode)->i_data_sem); 3074 down_read(&EXT4_I(inode)->i_data_sem);
3145 mutex_unlock(&inode->i_mutex); 3075 mutex_unlock(&inode->i_mutex);
3146 } 3076 }
@@ -3167,13 +3097,16 @@ static ssize_t ext4_ext_direct_IO(int rw, struct kiocb *iocb,
3167 iocb->private = NULL; 3097 iocb->private = NULL;
3168 ext4_inode_aio_set(inode, NULL); 3098 ext4_inode_aio_set(inode, NULL);
3169 if (!is_sync_kiocb(iocb)) { 3099 if (!is_sync_kiocb(iocb)) {
3170 ext4_io_end_t *io_end = ext4_init_io_end(inode, GFP_NOFS); 3100 io_end = ext4_init_io_end(inode, GFP_NOFS);
3171 if (!io_end) { 3101 if (!io_end) {
3172 ret = -ENOMEM; 3102 ret = -ENOMEM;
3173 goto retake_lock; 3103 goto retake_lock;
3174 } 3104 }
3175 io_end->flag |= EXT4_IO_END_DIRECT; 3105 io_end->flag |= EXT4_IO_END_DIRECT;
3176 iocb->private = io_end; 3106 /*
3107 * Grab reference for DIO. Will be dropped in ext4_end_io_dio()
3108 */
3109 iocb->private = ext4_get_io_end(io_end);
3177 /* 3110 /*
3178 * we save the io structure for current async direct 3111 * we save the io structure for current async direct
3179 * IO, so that later ext4_map_blocks() could flag the 3112 * IO, so that later ext4_map_blocks() could flag the
@@ -3197,33 +3130,42 @@ static ssize_t ext4_ext_direct_IO(int rw, struct kiocb *iocb,
3197 NULL, 3130 NULL,
3198 dio_flags); 3131 dio_flags);
3199 3132
3200 if (iocb->private)
3201 ext4_inode_aio_set(inode, NULL);
3202 /* 3133 /*
3203 * The io_end structure takes a reference to the inode, that 3134 * Put our reference to io_end. This can free the io_end structure e.g.
3204 * structure needs to be destroyed and the reference to the 3135 * in sync IO case or in case of error. It can even perform extent
3205 * inode need to be dropped, when IO is complete, even with 0 3136 * conversion if all bios we submitted finished before we got here.
3206 * byte write, or failed. 3137 * Note that in that case iocb->private can be already set to NULL
3207 * 3138 * here.
3208 * In the successful AIO DIO case, the io_end structure will
3209 * be destroyed and the reference to the inode will be dropped
3210 * after the end_io call back function is called.
3211 *
3212 * In the case there is 0 byte write, or error case, since VFS
3213 * direct IO won't invoke the end_io call back function, we
3214 * need to free the end_io structure here.
3215 */ 3139 */
3216 if (ret != -EIOCBQUEUED && ret <= 0 && iocb->private) { 3140 if (io_end) {
3217 ext4_free_io_end(iocb->private); 3141 ext4_inode_aio_set(inode, NULL);
3218 iocb->private = NULL; 3142 ext4_put_io_end(io_end);
3219 } else if (ret > 0 && !overwrite && ext4_test_inode_state(inode, 3143 /*
3144 * When no IO was submitted ext4_end_io_dio() was not
3145 * called so we have to put iocb's reference.
3146 */
3147 if (ret <= 0 && ret != -EIOCBQUEUED && iocb->private) {
3148 WARN_ON(iocb->private != io_end);
3149 WARN_ON(io_end->flag & EXT4_IO_END_UNWRITTEN);
3150 WARN_ON(io_end->iocb);
3151 /*
3152 * Generic code already did inode_dio_done() so we
3153 * have to clear EXT4_IO_END_DIRECT to not do it for
3154 * the second time.
3155 */
3156 io_end->flag = 0;
3157 ext4_put_io_end(io_end);
3158 iocb->private = NULL;
3159 }
3160 }
3161 if (ret > 0 && !overwrite && ext4_test_inode_state(inode,
3220 EXT4_STATE_DIO_UNWRITTEN)) { 3162 EXT4_STATE_DIO_UNWRITTEN)) {
3221 int err; 3163 int err;
3222 /* 3164 /*
3223 * for non AIO case, since the IO is already 3165 * for non AIO case, since the IO is already
3224 * completed, we could do the conversion right here 3166 * completed, we could do the conversion right here
3225 */ 3167 */
3226 err = ext4_convert_unwritten_extents(inode, 3168 err = ext4_convert_unwritten_extents(NULL, inode,
3227 offset, ret); 3169 offset, ret);
3228 if (err < 0) 3170 if (err < 0)
3229 ret = err; 3171 ret = err;
@@ -3231,9 +3173,10 @@ static ssize_t ext4_ext_direct_IO(int rw, struct kiocb *iocb,
3231 } 3173 }
3232 3174
3233retake_lock: 3175retake_lock:
3176 if (rw == WRITE)
3177 inode_dio_done(inode);
3234 /* take i_mutex locking again if we do a ovewrite dio */ 3178 /* take i_mutex locking again if we do a ovewrite dio */
3235 if (overwrite) { 3179 if (overwrite) {
3236 inode_dio_done(inode);
3237 up_read(&EXT4_I(inode)->i_data_sem); 3180 up_read(&EXT4_I(inode)->i_data_sem);
3238 mutex_lock(&inode->i_mutex); 3181 mutex_lock(&inode->i_mutex);
3239 } 3182 }
@@ -3292,6 +3235,7 @@ static const struct address_space_operations ext4_aops = {
3292 .readpage = ext4_readpage, 3235 .readpage = ext4_readpage,
3293 .readpages = ext4_readpages, 3236 .readpages = ext4_readpages,
3294 .writepage = ext4_writepage, 3237 .writepage = ext4_writepage,
3238 .writepages = ext4_writepages,
3295 .write_begin = ext4_write_begin, 3239 .write_begin = ext4_write_begin,
3296 .write_end = ext4_write_end, 3240 .write_end = ext4_write_end,
3297 .bmap = ext4_bmap, 3241 .bmap = ext4_bmap,
@@ -3307,6 +3251,7 @@ static const struct address_space_operations ext4_journalled_aops = {
3307 .readpage = ext4_readpage, 3251 .readpage = ext4_readpage,
3308 .readpages = ext4_readpages, 3252 .readpages = ext4_readpages,
3309 .writepage = ext4_writepage, 3253 .writepage = ext4_writepage,
3254 .writepages = ext4_writepages,
3310 .write_begin = ext4_write_begin, 3255 .write_begin = ext4_write_begin,
3311 .write_end = ext4_journalled_write_end, 3256 .write_end = ext4_journalled_write_end,
3312 .set_page_dirty = ext4_journalled_set_page_dirty, 3257 .set_page_dirty = ext4_journalled_set_page_dirty,
@@ -3322,7 +3267,7 @@ static const struct address_space_operations ext4_da_aops = {
3322 .readpage = ext4_readpage, 3267 .readpage = ext4_readpage,
3323 .readpages = ext4_readpages, 3268 .readpages = ext4_readpages,
3324 .writepage = ext4_writepage, 3269 .writepage = ext4_writepage,
3325 .writepages = ext4_da_writepages, 3270 .writepages = ext4_writepages,
3326 .write_begin = ext4_da_write_begin, 3271 .write_begin = ext4_da_write_begin,
3327 .write_end = ext4_da_write_end, 3272 .write_end = ext4_da_write_end,
3328 .bmap = ext4_bmap, 3273 .bmap = ext4_bmap,
@@ -3355,89 +3300,56 @@ void ext4_set_aops(struct inode *inode)
3355 inode->i_mapping->a_ops = &ext4_aops; 3300 inode->i_mapping->a_ops = &ext4_aops;
3356} 3301}
3357 3302
3358
3359/* 3303/*
3360 * ext4_discard_partial_page_buffers() 3304 * ext4_block_truncate_page() zeroes out a mapping from file offset `from'
3361 * Wrapper function for ext4_discard_partial_page_buffers_no_lock. 3305 * up to the end of the block which corresponds to `from'.
3362 * This function finds and locks the page containing the offset 3306 * This required during truncate. We need to physically zero the tail end
3363 * "from" and passes it to ext4_discard_partial_page_buffers_no_lock. 3307 * of that block so it doesn't yield old data if the file is later grown.
3364 * Calling functions that already have the page locked should call
3365 * ext4_discard_partial_page_buffers_no_lock directly.
3366 */ 3308 */
3367int ext4_discard_partial_page_buffers(handle_t *handle, 3309int ext4_block_truncate_page(handle_t *handle,
3368 struct address_space *mapping, loff_t from, 3310 struct address_space *mapping, loff_t from)
3369 loff_t length, int flags)
3370{ 3311{
3312 unsigned offset = from & (PAGE_CACHE_SIZE-1);
3313 unsigned length;
3314 unsigned blocksize;
3371 struct inode *inode = mapping->host; 3315 struct inode *inode = mapping->host;
3372 struct page *page;
3373 int err = 0;
3374 3316
3375 page = find_or_create_page(mapping, from >> PAGE_CACHE_SHIFT, 3317 blocksize = inode->i_sb->s_blocksize;
3376 mapping_gfp_mask(mapping) & ~__GFP_FS); 3318 length = blocksize - (offset & (blocksize - 1));
3377 if (!page)
3378 return -ENOMEM;
3379
3380 err = ext4_discard_partial_page_buffers_no_lock(handle, inode, page,
3381 from, length, flags);
3382 3319
3383 unlock_page(page); 3320 return ext4_block_zero_page_range(handle, mapping, from, length);
3384 page_cache_release(page);
3385 return err;
3386} 3321}
3387 3322
3388/* 3323/*
3389 * ext4_discard_partial_page_buffers_no_lock() 3324 * ext4_block_zero_page_range() zeros out a mapping of length 'length'
3390 * Zeros a page range of length 'length' starting from offset 'from'. 3325 * starting from file offset 'from'. The range to be zero'd must
3391 * Buffer heads that correspond to the block aligned regions of the 3326 * be contained with in one block. If the specified range exceeds
3392 * zeroed range will be unmapped. Unblock aligned regions 3327 * the end of the block it will be shortened to end of the block
3393 * will have the corresponding buffer head mapped if needed so that 3328 * that cooresponds to 'from'
3394 * that region of the page can be updated with the partial zero out.
3395 *
3396 * This function assumes that the page has already been locked. The
3397 * The range to be discarded must be contained with in the given page.
3398 * If the specified range exceeds the end of the page it will be shortened
3399 * to the end of the page that corresponds to 'from'. This function is
3400 * appropriate for updating a page and it buffer heads to be unmapped and
3401 * zeroed for blocks that have been either released, or are going to be
3402 * released.
3403 *
3404 * handle: The journal handle
3405 * inode: The files inode
3406 * page: A locked page that contains the offset "from"
3407 * from: The starting byte offset (from the beginning of the file)
3408 * to begin discarding
3409 * len: The length of bytes to discard
3410 * flags: Optional flags that may be used:
3411 *
3412 * EXT4_DISCARD_PARTIAL_PG_ZERO_UNMAPPED
3413 * Only zero the regions of the page whose buffer heads
3414 * have already been unmapped. This flag is appropriate
3415 * for updating the contents of a page whose blocks may
3416 * have already been released, and we only want to zero
3417 * out the regions that correspond to those released blocks.
3418 *
3419 * Returns zero on success or negative on failure.
3420 */ 3329 */
3421static int ext4_discard_partial_page_buffers_no_lock(handle_t *handle, 3330int ext4_block_zero_page_range(handle_t *handle,
3422 struct inode *inode, struct page *page, loff_t from, 3331 struct address_space *mapping, loff_t from, loff_t length)
3423 loff_t length, int flags)
3424{ 3332{
3425 ext4_fsblk_t index = from >> PAGE_CACHE_SHIFT; 3333 ext4_fsblk_t index = from >> PAGE_CACHE_SHIFT;
3426 unsigned int offset = from & (PAGE_CACHE_SIZE-1); 3334 unsigned offset = from & (PAGE_CACHE_SIZE-1);
3427 unsigned int blocksize, max, pos; 3335 unsigned blocksize, max, pos;
3428 ext4_lblk_t iblock; 3336 ext4_lblk_t iblock;
3337 struct inode *inode = mapping->host;
3429 struct buffer_head *bh; 3338 struct buffer_head *bh;
3339 struct page *page;
3430 int err = 0; 3340 int err = 0;
3431 3341
3432 blocksize = inode->i_sb->s_blocksize; 3342 page = find_or_create_page(mapping, from >> PAGE_CACHE_SHIFT,
3433 max = PAGE_CACHE_SIZE - offset; 3343 mapping_gfp_mask(mapping) & ~__GFP_FS);
3344 if (!page)
3345 return -ENOMEM;
3434 3346
3435 if (index != page->index) 3347 blocksize = inode->i_sb->s_blocksize;
3436 return -EINVAL; 3348 max = blocksize - (offset & (blocksize - 1));
3437 3349
3438 /* 3350 /*
3439 * correct length if it does not fall between 3351 * correct length if it does not fall between
3440 * 'from' and the end of the page 3352 * 'from' and the end of the block
3441 */ 3353 */
3442 if (length > max || length < 0) 3354 if (length > max || length < 0)
3443 length = max; 3355 length = max;
@@ -3455,106 +3367,91 @@ static int ext4_discard_partial_page_buffers_no_lock(handle_t *handle,
3455 iblock++; 3367 iblock++;
3456 pos += blocksize; 3368 pos += blocksize;
3457 } 3369 }
3458 3370 if (buffer_freed(bh)) {
3459 pos = offset; 3371 BUFFER_TRACE(bh, "freed: skip");
3460 while (pos < offset + length) { 3372 goto unlock;
3461 unsigned int end_of_block, range_to_discard; 3373 }
3462 3374 if (!buffer_mapped(bh)) {
3463 err = 0; 3375 BUFFER_TRACE(bh, "unmapped");
3464 3376 ext4_get_block(inode, iblock, bh, 0);
3465 /* The length of space left to zero and unmap */ 3377 /* unmapped? It's a hole - nothing to do */
3466 range_to_discard = offset + length - pos;
3467
3468 /* The length of space until the end of the block */
3469 end_of_block = blocksize - (pos & (blocksize-1));
3470
3471 /*
3472 * Do not unmap or zero past end of block
3473 * for this buffer head
3474 */
3475 if (range_to_discard > end_of_block)
3476 range_to_discard = end_of_block;
3477
3478
3479 /*
3480 * Skip this buffer head if we are only zeroing unampped
3481 * regions of the page
3482 */
3483 if (flags & EXT4_DISCARD_PARTIAL_PG_ZERO_UNMAPPED &&
3484 buffer_mapped(bh))
3485 goto next;
3486
3487 /* If the range is block aligned, unmap */
3488 if (range_to_discard == blocksize) {
3489 clear_buffer_dirty(bh);
3490 bh->b_bdev = NULL;
3491 clear_buffer_mapped(bh);
3492 clear_buffer_req(bh);
3493 clear_buffer_new(bh);
3494 clear_buffer_delay(bh);
3495 clear_buffer_unwritten(bh);
3496 clear_buffer_uptodate(bh);
3497 zero_user(page, pos, range_to_discard);
3498 BUFFER_TRACE(bh, "Buffer discarded");
3499 goto next;
3500 }
3501
3502 /*
3503 * If this block is not completely contained in the range
3504 * to be discarded, then it is not going to be released. Because
3505 * we need to keep this block, we need to make sure this part
3506 * of the page is uptodate before we modify it by writeing
3507 * partial zeros on it.
3508 */
3509 if (!buffer_mapped(bh)) { 3378 if (!buffer_mapped(bh)) {
3510 /* 3379 BUFFER_TRACE(bh, "still unmapped");
3511 * Buffer head must be mapped before we can read 3380 goto unlock;
3512 * from the block
3513 */
3514 BUFFER_TRACE(bh, "unmapped");
3515 ext4_get_block(inode, iblock, bh, 0);
3516 /* unmapped? It's a hole - nothing to do */
3517 if (!buffer_mapped(bh)) {
3518 BUFFER_TRACE(bh, "still unmapped");
3519 goto next;
3520 }
3521 } 3381 }
3382 }
3522 3383
3523 /* Ok, it's mapped. Make sure it's up-to-date */ 3384 /* Ok, it's mapped. Make sure it's up-to-date */
3524 if (PageUptodate(page)) 3385 if (PageUptodate(page))
3525 set_buffer_uptodate(bh); 3386 set_buffer_uptodate(bh);
3526 3387
3527 if (!buffer_uptodate(bh)) { 3388 if (!buffer_uptodate(bh)) {
3528 err = -EIO; 3389 err = -EIO;
3529 ll_rw_block(READ, 1, &bh); 3390 ll_rw_block(READ, 1, &bh);
3530 wait_on_buffer(bh); 3391 wait_on_buffer(bh);
3531 /* Uhhuh. Read error. Complain and punt.*/ 3392 /* Uhhuh. Read error. Complain and punt. */
3532 if (!buffer_uptodate(bh)) 3393 if (!buffer_uptodate(bh))
3533 goto next; 3394 goto unlock;
3534 } 3395 }
3396 if (ext4_should_journal_data(inode)) {
3397 BUFFER_TRACE(bh, "get write access");
3398 err = ext4_journal_get_write_access(handle, bh);
3399 if (err)
3400 goto unlock;
3401 }
3402 zero_user(page, offset, length);
3403 BUFFER_TRACE(bh, "zeroed end of block");
3535 3404
3536 if (ext4_should_journal_data(inode)) { 3405 if (ext4_should_journal_data(inode)) {
3537 BUFFER_TRACE(bh, "get write access"); 3406 err = ext4_handle_dirty_metadata(handle, inode, bh);
3538 err = ext4_journal_get_write_access(handle, bh); 3407 } else {
3539 if (err) 3408 err = 0;
3540 goto next; 3409 mark_buffer_dirty(bh);
3541 } 3410 if (ext4_test_inode_state(inode, EXT4_STATE_ORDERED_MODE))
3411 err = ext4_jbd2_file_inode(handle, inode);
3412 }
3542 3413
3543 zero_user(page, pos, range_to_discard); 3414unlock:
3415 unlock_page(page);
3416 page_cache_release(page);
3417 return err;
3418}
3544 3419
3545 err = 0; 3420int ext4_zero_partial_blocks(handle_t *handle, struct inode *inode,
3546 if (ext4_should_journal_data(inode)) { 3421 loff_t lstart, loff_t length)
3547 err = ext4_handle_dirty_metadata(handle, inode, bh); 3422{
3548 } else 3423 struct super_block *sb = inode->i_sb;
3549 mark_buffer_dirty(bh); 3424 struct address_space *mapping = inode->i_mapping;
3425 unsigned partial_start, partial_end;
3426 ext4_fsblk_t start, end;
3427 loff_t byte_end = (lstart + length - 1);
3428 int err = 0;
3550 3429
3551 BUFFER_TRACE(bh, "Partial buffer zeroed"); 3430 partial_start = lstart & (sb->s_blocksize - 1);
3552next: 3431 partial_end = byte_end & (sb->s_blocksize - 1);
3553 bh = bh->b_this_page;
3554 iblock++;
3555 pos += range_to_discard;
3556 }
3557 3432
3433 start = lstart >> sb->s_blocksize_bits;
3434 end = byte_end >> sb->s_blocksize_bits;
3435
3436 /* Handle partial zero within the single block */
3437 if (start == end &&
3438 (partial_start || (partial_end != sb->s_blocksize - 1))) {
3439 err = ext4_block_zero_page_range(handle, mapping,
3440 lstart, length);
3441 return err;
3442 }
3443 /* Handle partial zero out on the start of the range */
3444 if (partial_start) {
3445 err = ext4_block_zero_page_range(handle, mapping,
3446 lstart, sb->s_blocksize);
3447 if (err)
3448 return err;
3449 }
3450 /* Handle partial zero out on the end of the range */
3451 if (partial_end != sb->s_blocksize - 1)
3452 err = ext4_block_zero_page_range(handle, mapping,
3453 byte_end - partial_end,
3454 partial_end + 1);
3558 return err; 3455 return err;
3559} 3456}
3560 3457
@@ -3580,14 +3477,12 @@ int ext4_can_truncate(struct inode *inode)
3580 * Returns: 0 on success or negative on failure 3477 * Returns: 0 on success or negative on failure
3581 */ 3478 */
3582 3479
3583int ext4_punch_hole(struct file *file, loff_t offset, loff_t length) 3480int ext4_punch_hole(struct inode *inode, loff_t offset, loff_t length)
3584{ 3481{
3585 struct inode *inode = file_inode(file);
3586 struct super_block *sb = inode->i_sb; 3482 struct super_block *sb = inode->i_sb;
3587 ext4_lblk_t first_block, stop_block; 3483 ext4_lblk_t first_block, stop_block;
3588 struct address_space *mapping = inode->i_mapping; 3484 struct address_space *mapping = inode->i_mapping;
3589 loff_t first_page, last_page, page_len; 3485 loff_t first_block_offset, last_block_offset;
3590 loff_t first_page_offset, last_page_offset;
3591 handle_t *handle; 3486 handle_t *handle;
3592 unsigned int credits; 3487 unsigned int credits;
3593 int ret = 0; 3488 int ret = 0;
@@ -3638,23 +3533,16 @@ int ext4_punch_hole(struct file *file, loff_t offset, loff_t length)
3638 offset; 3533 offset;
3639 } 3534 }
3640 3535
3641 first_page = (offset + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; 3536 first_block_offset = round_up(offset, sb->s_blocksize);
3642 last_page = (offset + length) >> PAGE_CACHE_SHIFT; 3537 last_block_offset = round_down((offset + length), sb->s_blocksize) - 1;
3643 3538
3644 first_page_offset = first_page << PAGE_CACHE_SHIFT; 3539 /* Now release the pages and zero block aligned part of pages*/
3645 last_page_offset = last_page << PAGE_CACHE_SHIFT; 3540 if (last_block_offset > first_block_offset)
3646 3541 truncate_pagecache_range(inode, first_block_offset,
3647 /* Now release the pages */ 3542 last_block_offset);
3648 if (last_page_offset > first_page_offset) {
3649 truncate_pagecache_range(inode, first_page_offset,
3650 last_page_offset - 1);
3651 }
3652 3543
3653 /* Wait all existing dio workers, newcomers will block on i_mutex */ 3544 /* Wait all existing dio workers, newcomers will block on i_mutex */
3654 ext4_inode_block_unlocked_dio(inode); 3545 ext4_inode_block_unlocked_dio(inode);
3655 ret = ext4_flush_unwritten_io(inode);
3656 if (ret)
3657 goto out_dio;
3658 inode_dio_wait(inode); 3546 inode_dio_wait(inode);
3659 3547
3660 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 3548 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
@@ -3668,66 +3556,10 @@ int ext4_punch_hole(struct file *file, loff_t offset, loff_t length)
3668 goto out_dio; 3556 goto out_dio;
3669 } 3557 }
3670 3558
3671 /* 3559 ret = ext4_zero_partial_blocks(handle, inode, offset,
3672 * Now we need to zero out the non-page-aligned data in the 3560 length);
3673 * pages at the start and tail of the hole, and unmap the 3561 if (ret)
3674 * buffer heads for the block aligned regions of the page that 3562 goto out_stop;
3675 * were completely zeroed.
3676 */
3677 if (first_page > last_page) {
3678 /*
3679 * If the file space being truncated is contained
3680 * within a page just zero out and unmap the middle of
3681 * that page
3682 */
3683 ret = ext4_discard_partial_page_buffers(handle,
3684 mapping, offset, length, 0);
3685
3686 if (ret)
3687 goto out_stop;
3688 } else {
3689 /*
3690 * zero out and unmap the partial page that contains
3691 * the start of the hole
3692 */
3693 page_len = first_page_offset - offset;
3694 if (page_len > 0) {
3695 ret = ext4_discard_partial_page_buffers(handle, mapping,
3696 offset, page_len, 0);
3697 if (ret)
3698 goto out_stop;
3699 }
3700
3701 /*
3702 * zero out and unmap the partial page that contains
3703 * the end of the hole
3704 */
3705 page_len = offset + length - last_page_offset;
3706 if (page_len > 0) {
3707 ret = ext4_discard_partial_page_buffers(handle, mapping,
3708 last_page_offset, page_len, 0);
3709 if (ret)
3710 goto out_stop;
3711 }
3712 }
3713
3714 /*
3715 * If i_size is contained in the last page, we need to
3716 * unmap and zero the partial page after i_size
3717 */
3718 if (inode->i_size >> PAGE_CACHE_SHIFT == last_page &&
3719 inode->i_size % PAGE_CACHE_SIZE != 0) {
3720 page_len = PAGE_CACHE_SIZE -
3721 (inode->i_size & (PAGE_CACHE_SIZE - 1));
3722
3723 if (page_len > 0) {
3724 ret = ext4_discard_partial_page_buffers(handle,
3725 mapping, inode->i_size, page_len, 0);
3726
3727 if (ret)
3728 goto out_stop;
3729 }
3730 }
3731 3563
3732 first_block = (offset + sb->s_blocksize - 1) >> 3564 first_block = (offset + sb->s_blocksize - 1) >>
3733 EXT4_BLOCK_SIZE_BITS(sb); 3565 EXT4_BLOCK_SIZE_BITS(sb);
@@ -3803,7 +3635,6 @@ void ext4_truncate(struct inode *inode)
3803 unsigned int credits; 3635 unsigned int credits;
3804 handle_t *handle; 3636 handle_t *handle;
3805 struct address_space *mapping = inode->i_mapping; 3637 struct address_space *mapping = inode->i_mapping;
3806 loff_t page_len;
3807 3638
3808 /* 3639 /*
3809 * There is a possibility that we're either freeing the inode 3640 * There is a possibility that we're either freeing the inode
@@ -3830,12 +3661,6 @@ void ext4_truncate(struct inode *inode)
3830 return; 3661 return;
3831 } 3662 }
3832 3663
3833 /*
3834 * finish any pending end_io work so we won't run the risk of
3835 * converting any truncated blocks to initialized later
3836 */
3837 ext4_flush_unwritten_io(inode);
3838
3839 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 3664 if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
3840 credits = ext4_writepage_trans_blocks(inode); 3665 credits = ext4_writepage_trans_blocks(inode);
3841 else 3666 else
@@ -3847,14 +3672,8 @@ void ext4_truncate(struct inode *inode)
3847 return; 3672 return;
3848 } 3673 }
3849 3674
3850 if (inode->i_size % PAGE_CACHE_SIZE != 0) { 3675 if (inode->i_size & (inode->i_sb->s_blocksize - 1))
3851 page_len = PAGE_CACHE_SIZE - 3676 ext4_block_truncate_page(handle, mapping, inode->i_size);
3852 (inode->i_size & (PAGE_CACHE_SIZE - 1));
3853
3854 if (ext4_discard_partial_page_buffers(handle,
3855 mapping, inode->i_size, page_len, 0))
3856 goto out_stop;
3857 }
3858 3677
3859 /* 3678 /*
3860 * We add the inode to the orphan list, so that if this 3679 * We add the inode to the orphan list, so that if this
@@ -4623,7 +4442,8 @@ static void ext4_wait_for_tail_page_commit(struct inode *inode)
4623 inode->i_size >> PAGE_CACHE_SHIFT); 4442 inode->i_size >> PAGE_CACHE_SHIFT);
4624 if (!page) 4443 if (!page)
4625 return; 4444 return;
4626 ret = __ext4_journalled_invalidatepage(page, offset); 4445 ret = __ext4_journalled_invalidatepage(page, offset,
4446 PAGE_CACHE_SIZE - offset);
4627 unlock_page(page); 4447 unlock_page(page);
4628 page_cache_release(page); 4448 page_cache_release(page);
4629 if (ret != -EBUSY) 4449 if (ret != -EBUSY)
@@ -4805,7 +4625,7 @@ int ext4_getattr(struct vfsmount *mnt, struct dentry *dentry,
4805 struct kstat *stat) 4625 struct kstat *stat)
4806{ 4626{
4807 struct inode *inode; 4627 struct inode *inode;
4808 unsigned long delalloc_blocks; 4628 unsigned long long delalloc_blocks;
4809 4629
4810 inode = dentry->d_inode; 4630 inode = dentry->d_inode;
4811 generic_fillattr(inode, stat); 4631 generic_fillattr(inode, stat);
@@ -4823,15 +4643,16 @@ int ext4_getattr(struct vfsmount *mnt, struct dentry *dentry,
4823 delalloc_blocks = EXT4_C2B(EXT4_SB(inode->i_sb), 4643 delalloc_blocks = EXT4_C2B(EXT4_SB(inode->i_sb),
4824 EXT4_I(inode)->i_reserved_data_blocks); 4644 EXT4_I(inode)->i_reserved_data_blocks);
4825 4645
4826 stat->blocks += (delalloc_blocks << inode->i_sb->s_blocksize_bits)>>9; 4646 stat->blocks += delalloc_blocks << (inode->i_sb->s_blocksize_bits-9);
4827 return 0; 4647 return 0;
4828} 4648}
4829 4649
4830static int ext4_index_trans_blocks(struct inode *inode, int nrblocks, int chunk) 4650static int ext4_index_trans_blocks(struct inode *inode, int lblocks,
4651 int pextents)
4831{ 4652{
4832 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) 4653 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
4833 return ext4_ind_trans_blocks(inode, nrblocks, chunk); 4654 return ext4_ind_trans_blocks(inode, lblocks);
4834 return ext4_ext_index_trans_blocks(inode, nrblocks, chunk); 4655 return ext4_ext_index_trans_blocks(inode, pextents);
4835} 4656}
4836 4657
4837/* 4658/*
@@ -4845,7 +4666,8 @@ static int ext4_index_trans_blocks(struct inode *inode, int nrblocks, int chunk)
4845 * 4666 *
4846 * Also account for superblock, inode, quota and xattr blocks 4667 * Also account for superblock, inode, quota and xattr blocks
4847 */ 4668 */
4848static int ext4_meta_trans_blocks(struct inode *inode, int nrblocks, int chunk) 4669static int ext4_meta_trans_blocks(struct inode *inode, int lblocks,
4670 int pextents)
4849{ 4671{
4850 ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb); 4672 ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb);
4851 int gdpblocks; 4673 int gdpblocks;
@@ -4853,14 +4675,10 @@ static int ext4_meta_trans_blocks(struct inode *inode, int nrblocks, int chunk)
4853 int ret = 0; 4675 int ret = 0;
4854 4676
4855 /* 4677 /*
4856 * How many index blocks need to touch to modify nrblocks? 4678 * How many index blocks need to touch to map @lblocks logical blocks
4857 * The "Chunk" flag indicating whether the nrblocks is 4679 * to @pextents physical extents?
4858 * physically contiguous on disk
4859 *
4860 * For Direct IO and fallocate, they calls get_block to allocate
4861 * one single extent at a time, so they could set the "Chunk" flag
4862 */ 4680 */
4863 idxblocks = ext4_index_trans_blocks(inode, nrblocks, chunk); 4681 idxblocks = ext4_index_trans_blocks(inode, lblocks, pextents);
4864 4682
4865 ret = idxblocks; 4683 ret = idxblocks;
4866 4684
@@ -4868,12 +4686,7 @@ static int ext4_meta_trans_blocks(struct inode *inode, int nrblocks, int chunk)
4868 * Now let's see how many group bitmaps and group descriptors need 4686 * Now let's see how many group bitmaps and group descriptors need
4869 * to account 4687 * to account
4870 */ 4688 */
4871 groups = idxblocks; 4689 groups = idxblocks + pextents;
4872 if (chunk)
4873 groups += 1;
4874 else
4875 groups += nrblocks;
4876
4877 gdpblocks = groups; 4690 gdpblocks = groups;
4878 if (groups > ngroups) 4691 if (groups > ngroups)
4879 groups = ngroups; 4692 groups = ngroups;
@@ -4904,7 +4717,7 @@ int ext4_writepage_trans_blocks(struct inode *inode)
4904 int bpp = ext4_journal_blocks_per_page(inode); 4717 int bpp = ext4_journal_blocks_per_page(inode);
4905 int ret; 4718 int ret;
4906 4719
4907 ret = ext4_meta_trans_blocks(inode, bpp, 0); 4720 ret = ext4_meta_trans_blocks(inode, bpp, bpp);
4908 4721
4909 /* Account for data blocks for journalled mode */ 4722 /* Account for data blocks for journalled mode */
4910 if (ext4_should_journal_data(inode)) 4723 if (ext4_should_journal_data(inode))