aboutsummaryrefslogtreecommitdiffstats
path: root/fs/ext4/extents.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/ext4/extents.c')
-rw-r--r--fs/ext4/extents.c110
1 files changed, 63 insertions, 47 deletions
diff --git a/fs/ext4/extents.c b/fs/ext4/extents.c
index 2608dce18f3e..dc2724fa7622 100644
--- a/fs/ext4/extents.c
+++ b/fs/ext4/extents.c
@@ -48,7 +48,7 @@
48 * ext_pblock: 48 * ext_pblock:
49 * combine low and high parts of physical block number into ext4_fsblk_t 49 * combine low and high parts of physical block number into ext4_fsblk_t
50 */ 50 */
51static inline ext4_fsblk_t ext_pblock(struct ext4_extent *ex) 51static ext4_fsblk_t ext_pblock(struct ext4_extent *ex)
52{ 52{
53 ext4_fsblk_t block; 53 ext4_fsblk_t block;
54 54
@@ -61,7 +61,7 @@ static inline ext4_fsblk_t ext_pblock(struct ext4_extent *ex)
61 * idx_pblock: 61 * idx_pblock:
62 * combine low and high parts of a leaf physical block number into ext4_fsblk_t 62 * combine low and high parts of a leaf physical block number into ext4_fsblk_t
63 */ 63 */
64static inline ext4_fsblk_t idx_pblock(struct ext4_extent_idx *ix) 64static ext4_fsblk_t idx_pblock(struct ext4_extent_idx *ix)
65{ 65{
66 ext4_fsblk_t block; 66 ext4_fsblk_t block;
67 67
@@ -75,7 +75,7 @@ static inline ext4_fsblk_t idx_pblock(struct ext4_extent_idx *ix)
75 * stores a large physical block number into an extent struct, 75 * stores a large physical block number into an extent struct,
76 * breaking it into parts 76 * breaking it into parts
77 */ 77 */
78static inline void ext4_ext_store_pblock(struct ext4_extent *ex, ext4_fsblk_t pb) 78static void ext4_ext_store_pblock(struct ext4_extent *ex, ext4_fsblk_t pb)
79{ 79{
80 ex->ee_start = cpu_to_le32((unsigned long) (pb & 0xffffffff)); 80 ex->ee_start = cpu_to_le32((unsigned long) (pb & 0xffffffff));
81 ex->ee_start_hi = cpu_to_le16((unsigned long) ((pb >> 31) >> 1) & 0xffff); 81 ex->ee_start_hi = cpu_to_le16((unsigned long) ((pb >> 31) >> 1) & 0xffff);
@@ -86,7 +86,7 @@ static inline void ext4_ext_store_pblock(struct ext4_extent *ex, ext4_fsblk_t pb
86 * stores a large physical block number into an index struct, 86 * stores a large physical block number into an index struct,
87 * breaking it into parts 87 * breaking it into parts
88 */ 88 */
89static inline void ext4_idx_store_pblock(struct ext4_extent_idx *ix, ext4_fsblk_t pb) 89static void ext4_idx_store_pblock(struct ext4_extent_idx *ix, ext4_fsblk_t pb)
90{ 90{
91 ix->ei_leaf = cpu_to_le32((unsigned long) (pb & 0xffffffff)); 91 ix->ei_leaf = cpu_to_le32((unsigned long) (pb & 0xffffffff));
92 ix->ei_leaf_hi = cpu_to_le16((unsigned long) ((pb >> 31) >> 1) & 0xffff); 92 ix->ei_leaf_hi = cpu_to_le16((unsigned long) ((pb >> 31) >> 1) & 0xffff);
@@ -186,7 +186,8 @@ static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode,
186 depth = path->p_depth; 186 depth = path->p_depth;
187 187
188 /* try to predict block placement */ 188 /* try to predict block placement */
189 if ((ex = path[depth].p_ext)) 189 ex = path[depth].p_ext;
190 if (ex)
190 return ext_pblock(ex)+(block-le32_to_cpu(ex->ee_block)); 191 return ext_pblock(ex)+(block-le32_to_cpu(ex->ee_block));
191 192
192 /* it looks like index is empty; 193 /* it looks like index is empty;
@@ -215,7 +216,7 @@ ext4_ext_new_block(handle_t *handle, struct inode *inode,
215 return newblock; 216 return newblock;
216} 217}
217 218
218static inline int ext4_ext_space_block(struct inode *inode) 219static int ext4_ext_space_block(struct inode *inode)
219{ 220{
220 int size; 221 int size;
221 222
@@ -228,7 +229,7 @@ static inline int ext4_ext_space_block(struct inode *inode)
228 return size; 229 return size;
229} 230}
230 231
231static inline int ext4_ext_space_block_idx(struct inode *inode) 232static int ext4_ext_space_block_idx(struct inode *inode)
232{ 233{
233 int size; 234 int size;
234 235
@@ -241,7 +242,7 @@ static inline int ext4_ext_space_block_idx(struct inode *inode)
241 return size; 242 return size;
242} 243}
243 244
244static inline int ext4_ext_space_root(struct inode *inode) 245static int ext4_ext_space_root(struct inode *inode)
245{ 246{
246 int size; 247 int size;
247 248
@@ -255,7 +256,7 @@ static inline int ext4_ext_space_root(struct inode *inode)
255 return size; 256 return size;
256} 257}
257 258
258static inline int ext4_ext_space_root_idx(struct inode *inode) 259static int ext4_ext_space_root_idx(struct inode *inode)
259{ 260{
260 int size; 261 int size;
261 262
@@ -476,13 +477,12 @@ ext4_ext_find_extent(struct inode *inode, int block, struct ext4_ext_path *path)
476 477
477 /* account possible depth increase */ 478 /* account possible depth increase */
478 if (!path) { 479 if (!path) {
479 path = kmalloc(sizeof(struct ext4_ext_path) * (depth + 2), 480 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 2),
480 GFP_NOFS); 481 GFP_NOFS);
481 if (!path) 482 if (!path)
482 return ERR_PTR(-ENOMEM); 483 return ERR_PTR(-ENOMEM);
483 alloc = 1; 484 alloc = 1;
484 } 485 }
485 memset(path, 0, sizeof(struct ext4_ext_path) * (depth + 1));
486 path[0].p_hdr = eh; 486 path[0].p_hdr = eh;
487 487
488 /* walk through the tree */ 488 /* walk through the tree */
@@ -543,7 +543,8 @@ static int ext4_ext_insert_index(handle_t *handle, struct inode *inode,
543 struct ext4_extent_idx *ix; 543 struct ext4_extent_idx *ix;
544 int len, err; 544 int len, err;
545 545
546 if ((err = ext4_ext_get_access(handle, inode, curp))) 546 err = ext4_ext_get_access(handle, inode, curp);
547 if (err)
547 return err; 548 return err;
548 549
549 BUG_ON(logical == le32_to_cpu(curp->p_idx->ei_block)); 550 BUG_ON(logical == le32_to_cpu(curp->p_idx->ei_block));
@@ -641,10 +642,9 @@ static int ext4_ext_split(handle_t *handle, struct inode *inode,
641 * We need this to handle errors and free blocks 642 * We need this to handle errors and free blocks
642 * upon them. 643 * upon them.
643 */ 644 */
644 ablocks = kmalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS); 645 ablocks = kzalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS);
645 if (!ablocks) 646 if (!ablocks)
646 return -ENOMEM; 647 return -ENOMEM;
647 memset(ablocks, 0, sizeof(ext4_fsblk_t) * depth);
648 648
649 /* allocate all needed blocks */ 649 /* allocate all needed blocks */
650 ext_debug("allocate %d blocks for indexes/leaf\n", depth - at); 650 ext_debug("allocate %d blocks for indexes/leaf\n", depth - at);
@@ -665,7 +665,8 @@ static int ext4_ext_split(handle_t *handle, struct inode *inode,
665 } 665 }
666 lock_buffer(bh); 666 lock_buffer(bh);
667 667
668 if ((err = ext4_journal_get_create_access(handle, bh))) 668 err = ext4_journal_get_create_access(handle, bh);
669 if (err)
669 goto cleanup; 670 goto cleanup;
670 671
671 neh = ext_block_hdr(bh); 672 neh = ext_block_hdr(bh);
@@ -702,18 +703,21 @@ static int ext4_ext_split(handle_t *handle, struct inode *inode,
702 set_buffer_uptodate(bh); 703 set_buffer_uptodate(bh);
703 unlock_buffer(bh); 704 unlock_buffer(bh);
704 705
705 if ((err = ext4_journal_dirty_metadata(handle, bh))) 706 err = ext4_journal_dirty_metadata(handle, bh);
707 if (err)
706 goto cleanup; 708 goto cleanup;
707 brelse(bh); 709 brelse(bh);
708 bh = NULL; 710 bh = NULL;
709 711
710 /* correct old leaf */ 712 /* correct old leaf */
711 if (m) { 713 if (m) {
712 if ((err = ext4_ext_get_access(handle, inode, path + depth))) 714 err = ext4_ext_get_access(handle, inode, path + depth);
715 if (err)
713 goto cleanup; 716 goto cleanup;
714 path[depth].p_hdr->eh_entries = 717 path[depth].p_hdr->eh_entries =
715 cpu_to_le16(le16_to_cpu(path[depth].p_hdr->eh_entries)-m); 718 cpu_to_le16(le16_to_cpu(path[depth].p_hdr->eh_entries)-m);
716 if ((err = ext4_ext_dirty(handle, inode, path + depth))) 719 err = ext4_ext_dirty(handle, inode, path + depth);
720 if (err)
717 goto cleanup; 721 goto cleanup;
718 722
719 } 723 }
@@ -736,7 +740,8 @@ static int ext4_ext_split(handle_t *handle, struct inode *inode,
736 } 740 }
737 lock_buffer(bh); 741 lock_buffer(bh);
738 742
739 if ((err = ext4_journal_get_create_access(handle, bh))) 743 err = ext4_journal_get_create_access(handle, bh);
744 if (err)
740 goto cleanup; 745 goto cleanup;
741 746
742 neh = ext_block_hdr(bh); 747 neh = ext_block_hdr(bh);
@@ -780,7 +785,8 @@ static int ext4_ext_split(handle_t *handle, struct inode *inode,
780 set_buffer_uptodate(bh); 785 set_buffer_uptodate(bh);
781 unlock_buffer(bh); 786 unlock_buffer(bh);
782 787
783 if ((err = ext4_journal_dirty_metadata(handle, bh))) 788 err = ext4_journal_dirty_metadata(handle, bh);
789 if (err)
784 goto cleanup; 790 goto cleanup;
785 brelse(bh); 791 brelse(bh);
786 bh = NULL; 792 bh = NULL;
@@ -800,9 +806,6 @@ static int ext4_ext_split(handle_t *handle, struct inode *inode,
800 } 806 }
801 807
802 /* insert new index */ 808 /* insert new index */
803 if (err)
804 goto cleanup;
805
806 err = ext4_ext_insert_index(handle, inode, path + at, 809 err = ext4_ext_insert_index(handle, inode, path + at,
807 le32_to_cpu(border), newblock); 810 le32_to_cpu(border), newblock);
808 811
@@ -857,7 +860,8 @@ static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode,
857 } 860 }
858 lock_buffer(bh); 861 lock_buffer(bh);
859 862
860 if ((err = ext4_journal_get_create_access(handle, bh))) { 863 err = ext4_journal_get_create_access(handle, bh);
864 if (err) {
861 unlock_buffer(bh); 865 unlock_buffer(bh);
862 goto out; 866 goto out;
863 } 867 }
@@ -877,11 +881,13 @@ static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode,
877 set_buffer_uptodate(bh); 881 set_buffer_uptodate(bh);
878 unlock_buffer(bh); 882 unlock_buffer(bh);
879 883
880 if ((err = ext4_journal_dirty_metadata(handle, bh))) 884 err = ext4_journal_dirty_metadata(handle, bh);
885 if (err)
881 goto out; 886 goto out;
882 887
883 /* create index in new top-level index: num,max,pointer */ 888 /* create index in new top-level index: num,max,pointer */
884 if ((err = ext4_ext_get_access(handle, inode, curp))) 889 err = ext4_ext_get_access(handle, inode, curp);
890 if (err)
885 goto out; 891 goto out;
886 892
887 curp->p_hdr->eh_magic = EXT4_EXT_MAGIC; 893 curp->p_hdr->eh_magic = EXT4_EXT_MAGIC;
@@ -1073,27 +1079,31 @@ int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode,
1073 */ 1079 */
1074 k = depth - 1; 1080 k = depth - 1;
1075 border = path[depth].p_ext->ee_block; 1081 border = path[depth].p_ext->ee_block;
1076 if ((err = ext4_ext_get_access(handle, inode, path + k))) 1082 err = ext4_ext_get_access(handle, inode, path + k);
1083 if (err)
1077 return err; 1084 return err;
1078 path[k].p_idx->ei_block = border; 1085 path[k].p_idx->ei_block = border;
1079 if ((err = ext4_ext_dirty(handle, inode, path + k))) 1086 err = ext4_ext_dirty(handle, inode, path + k);
1087 if (err)
1080 return err; 1088 return err;
1081 1089
1082 while (k--) { 1090 while (k--) {
1083 /* change all left-side indexes */ 1091 /* change all left-side indexes */
1084 if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr)) 1092 if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
1085 break; 1093 break;
1086 if ((err = ext4_ext_get_access(handle, inode, path + k))) 1094 err = ext4_ext_get_access(handle, inode, path + k);
1095 if (err)
1087 break; 1096 break;
1088 path[k].p_idx->ei_block = border; 1097 path[k].p_idx->ei_block = border;
1089 if ((err = ext4_ext_dirty(handle, inode, path + k))) 1098 err = ext4_ext_dirty(handle, inode, path + k);
1099 if (err)
1090 break; 1100 break;
1091 } 1101 }
1092 1102
1093 return err; 1103 return err;
1094} 1104}
1095 1105
1096static int inline 1106static int
1097ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1, 1107ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1,
1098 struct ext4_extent *ex2) 1108 struct ext4_extent *ex2)
1099{ 1109{
@@ -1145,7 +1155,8 @@ int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
1145 le16_to_cpu(newext->ee_len), 1155 le16_to_cpu(newext->ee_len),
1146 le32_to_cpu(ex->ee_block), 1156 le32_to_cpu(ex->ee_block),
1147 le16_to_cpu(ex->ee_len), ext_pblock(ex)); 1157 le16_to_cpu(ex->ee_len), ext_pblock(ex));
1148 if ((err = ext4_ext_get_access(handle, inode, path + depth))) 1158 err = ext4_ext_get_access(handle, inode, path + depth);
1159 if (err)
1149 return err; 1160 return err;
1150 ex->ee_len = cpu_to_le16(le16_to_cpu(ex->ee_len) 1161 ex->ee_len = cpu_to_le16(le16_to_cpu(ex->ee_len)
1151 + le16_to_cpu(newext->ee_len)); 1162 + le16_to_cpu(newext->ee_len));
@@ -1195,7 +1206,8 @@ repeat:
1195has_space: 1206has_space:
1196 nearex = path[depth].p_ext; 1207 nearex = path[depth].p_ext;
1197 1208
1198 if ((err = ext4_ext_get_access(handle, inode, path + depth))) 1209 err = ext4_ext_get_access(handle, inode, path + depth);
1210 if (err)
1199 goto cleanup; 1211 goto cleanup;
1200 1212
1201 if (!nearex) { 1213 if (!nearex) {
@@ -1383,7 +1395,7 @@ int ext4_ext_walk_space(struct inode *inode, unsigned long block,
1383 return err; 1395 return err;
1384} 1396}
1385 1397
1386static inline void 1398static void
1387ext4_ext_put_in_cache(struct inode *inode, __u32 block, 1399ext4_ext_put_in_cache(struct inode *inode, __u32 block,
1388 __u32 len, __u32 start, int type) 1400 __u32 len, __u32 start, int type)
1389{ 1401{
@@ -1401,7 +1413,7 @@ ext4_ext_put_in_cache(struct inode *inode, __u32 block,
1401 * calculate boundaries of the gap that the requested block fits into 1413 * calculate boundaries of the gap that the requested block fits into
1402 * and cache this gap 1414 * and cache this gap
1403 */ 1415 */
1404static inline void 1416static void
1405ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path, 1417ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path,
1406 unsigned long block) 1418 unsigned long block)
1407{ 1419{
@@ -1442,7 +1454,7 @@ ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path,
1442 ext4_ext_put_in_cache(inode, lblock, len, 0, EXT4_EXT_CACHE_GAP); 1454 ext4_ext_put_in_cache(inode, lblock, len, 0, EXT4_EXT_CACHE_GAP);
1443} 1455}
1444 1456
1445static inline int 1457static int
1446ext4_ext_in_cache(struct inode *inode, unsigned long block, 1458ext4_ext_in_cache(struct inode *inode, unsigned long block,
1447 struct ext4_extent *ex) 1459 struct ext4_extent *ex)
1448{ 1460{
@@ -1489,10 +1501,12 @@ int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
1489 path--; 1501 path--;
1490 leaf = idx_pblock(path->p_idx); 1502 leaf = idx_pblock(path->p_idx);
1491 BUG_ON(path->p_hdr->eh_entries == 0); 1503 BUG_ON(path->p_hdr->eh_entries == 0);
1492 if ((err = ext4_ext_get_access(handle, inode, path))) 1504 err = ext4_ext_get_access(handle, inode, path);
1505 if (err)
1493 return err; 1506 return err;
1494 path->p_hdr->eh_entries = cpu_to_le16(le16_to_cpu(path->p_hdr->eh_entries)-1); 1507 path->p_hdr->eh_entries = cpu_to_le16(le16_to_cpu(path->p_hdr->eh_entries)-1);
1495 if ((err = ext4_ext_dirty(handle, inode, path))) 1508 err = ext4_ext_dirty(handle, inode, path);
1509 if (err)
1496 return err; 1510 return err;
1497 ext_debug("index is empty, remove it, free block %llu\n", leaf); 1511 ext_debug("index is empty, remove it, free block %llu\n", leaf);
1498 bh = sb_find_get_block(inode->i_sb, leaf); 1512 bh = sb_find_get_block(inode->i_sb, leaf);
@@ -1509,7 +1523,7 @@ int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
1509 * the caller should calculate credits under truncate_mutex and 1523 * the caller should calculate credits under truncate_mutex and
1510 * pass the actual path. 1524 * pass the actual path.
1511 */ 1525 */
1512int inline ext4_ext_calc_credits_for_insert(struct inode *inode, 1526int ext4_ext_calc_credits_for_insert(struct inode *inode,
1513 struct ext4_ext_path *path) 1527 struct ext4_ext_path *path)
1514{ 1528{
1515 int depth, needed; 1529 int depth, needed;
@@ -1534,16 +1548,17 @@ int inline ext4_ext_calc_credits_for_insert(struct inode *inode,
1534 1548
1535 /* 1549 /*
1536 * tree can be full, so it would need to grow in depth: 1550 * tree can be full, so it would need to grow in depth:
1537 * allocation + old root + new root 1551 * we need one credit to modify old root, credits for
1552 * new root will be added in split accounting
1538 */ 1553 */
1539 needed += 2 + 1 + 1; 1554 needed += 1;
1540 1555
1541 /* 1556 /*
1542 * Index split can happen, we would need: 1557 * Index split can happen, we would need:
1543 * allocate intermediate indexes (bitmap + group) 1558 * allocate intermediate indexes (bitmap + group)
1544 * + change two blocks at each level, but root (already included) 1559 * + change two blocks at each level, but root (already included)
1545 */ 1560 */
1546 needed = (depth * 2) + (depth * 2); 1561 needed += (depth * 2) + (depth * 2);
1547 1562
1548 /* any allocation modifies superblock */ 1563 /* any allocation modifies superblock */
1549 needed += 1; 1564 needed += 1;
@@ -1718,7 +1733,7 @@ out:
1718 * ext4_ext_more_to_rm: 1733 * ext4_ext_more_to_rm:
1719 * returns 1 if current index has to be freed (even partial) 1734 * returns 1 if current index has to be freed (even partial)
1720 */ 1735 */
1721static int inline 1736static int
1722ext4_ext_more_to_rm(struct ext4_ext_path *path) 1737ext4_ext_more_to_rm(struct ext4_ext_path *path)
1723{ 1738{
1724 BUG_ON(path->p_idx == NULL); 1739 BUG_ON(path->p_idx == NULL);
@@ -1756,12 +1771,11 @@ int ext4_ext_remove_space(struct inode *inode, unsigned long start)
1756 * We start scanning from right side, freeing all the blocks 1771 * We start scanning from right side, freeing all the blocks
1757 * after i_size and walking into the tree depth-wise. 1772 * after i_size and walking into the tree depth-wise.
1758 */ 1773 */
1759 path = kmalloc(sizeof(struct ext4_ext_path) * (depth + 1), GFP_KERNEL); 1774 path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1), GFP_KERNEL);
1760 if (path == NULL) { 1775 if (path == NULL) {
1761 ext4_journal_stop(handle); 1776 ext4_journal_stop(handle);
1762 return -ENOMEM; 1777 return -ENOMEM;
1763 } 1778 }
1764 memset(path, 0, sizeof(struct ext4_ext_path) * (depth + 1));
1765 path[0].p_hdr = ext_inode_hdr(inode); 1779 path[0].p_hdr = ext_inode_hdr(inode);
1766 if (ext4_ext_check_header(__FUNCTION__, inode, path[0].p_hdr)) { 1780 if (ext4_ext_check_header(__FUNCTION__, inode, path[0].p_hdr)) {
1767 err = -EIO; 1781 err = -EIO;
@@ -1932,7 +1946,8 @@ int ext4_ext_get_blocks(handle_t *handle, struct inode *inode,
1932 mutex_lock(&EXT4_I(inode)->truncate_mutex); 1946 mutex_lock(&EXT4_I(inode)->truncate_mutex);
1933 1947
1934 /* check in cache */ 1948 /* check in cache */
1935 if ((goal = ext4_ext_in_cache(inode, iblock, &newex))) { 1949 goal = ext4_ext_in_cache(inode, iblock, &newex);
1950 if (goal) {
1936 if (goal == EXT4_EXT_CACHE_GAP) { 1951 if (goal == EXT4_EXT_CACHE_GAP) {
1937 if (!create) { 1952 if (!create) {
1938 /* block isn't allocated yet and 1953 /* block isn't allocated yet and
@@ -1971,7 +1986,8 @@ int ext4_ext_get_blocks(handle_t *handle, struct inode *inode,
1971 */ 1986 */
1972 BUG_ON(path[depth].p_ext == NULL && depth != 0); 1987 BUG_ON(path[depth].p_ext == NULL && depth != 0);
1973 1988
1974 if ((ex = path[depth].p_ext)) { 1989 ex = path[depth].p_ext;
1990 if (ex) {
1975 unsigned long ee_block = le32_to_cpu(ex->ee_block); 1991 unsigned long ee_block = le32_to_cpu(ex->ee_block);
1976 ext4_fsblk_t ee_start = ext_pblock(ex); 1992 ext4_fsblk_t ee_start = ext_pblock(ex);
1977 unsigned short ee_len = le16_to_cpu(ex->ee_len); 1993 unsigned short ee_len = le16_to_cpu(ex->ee_len);