diff options
Diffstat (limited to 'fs/ext4/extents.c')
-rw-r--r-- | fs/ext4/extents.c | 131 |
1 files changed, 114 insertions, 17 deletions
diff --git a/fs/ext4/extents.c b/fs/ext4/extents.c index e2eab196875f..ac77d8b8251d 100644 --- a/fs/ext4/extents.c +++ b/fs/ext4/extents.c | |||
@@ -152,6 +152,8 @@ static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode, | |||
152 | ext4_fsblk_t bg_start; | 152 | ext4_fsblk_t bg_start; |
153 | ext4_fsblk_t last_block; | 153 | ext4_fsblk_t last_block; |
154 | ext4_grpblk_t colour; | 154 | ext4_grpblk_t colour; |
155 | ext4_group_t block_group; | ||
156 | int flex_size = ext4_flex_bg_size(EXT4_SB(inode->i_sb)); | ||
155 | int depth; | 157 | int depth; |
156 | 158 | ||
157 | if (path) { | 159 | if (path) { |
@@ -170,10 +172,31 @@ static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode, | |||
170 | } | 172 | } |
171 | 173 | ||
172 | /* OK. use inode's group */ | 174 | /* OK. use inode's group */ |
173 | bg_start = (ei->i_block_group * EXT4_BLOCKS_PER_GROUP(inode->i_sb)) + | 175 | block_group = ei->i_block_group; |
176 | if (flex_size >= EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME) { | ||
177 | /* | ||
178 | * If there are at least EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME | ||
179 | * block groups per flexgroup, reserve the first block | ||
180 | * group for directories and special files. Regular | ||
181 | * files will start at the second block group. This | ||
182 | * tends to speed up directory access and improves | ||
183 | * fsck times. | ||
184 | */ | ||
185 | block_group &= ~(flex_size-1); | ||
186 | if (S_ISREG(inode->i_mode)) | ||
187 | block_group++; | ||
188 | } | ||
189 | bg_start = (block_group * EXT4_BLOCKS_PER_GROUP(inode->i_sb)) + | ||
174 | le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_first_data_block); | 190 | le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_first_data_block); |
175 | last_block = ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es) - 1; | 191 | last_block = ext4_blocks_count(EXT4_SB(inode->i_sb)->s_es) - 1; |
176 | 192 | ||
193 | /* | ||
194 | * If we are doing delayed allocation, we don't need take | ||
195 | * colour into account. | ||
196 | */ | ||
197 | if (test_opt(inode->i_sb, DELALLOC)) | ||
198 | return bg_start; | ||
199 | |||
177 | if (bg_start + EXT4_BLOCKS_PER_GROUP(inode->i_sb) <= last_block) | 200 | if (bg_start + EXT4_BLOCKS_PER_GROUP(inode->i_sb) <= last_block) |
178 | colour = (current->pid % 16) * | 201 | colour = (current->pid % 16) * |
179 | (EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16); | 202 | (EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16); |
@@ -301,7 +324,64 @@ ext4_ext_max_entries(struct inode *inode, int depth) | |||
301 | return max; | 324 | return max; |
302 | } | 325 | } |
303 | 326 | ||
304 | static int __ext4_ext_check_header(const char *function, struct inode *inode, | 327 | static int ext4_valid_extent(struct inode *inode, struct ext4_extent *ext) |
328 | { | ||
329 | ext4_fsblk_t block = ext_pblock(ext); | ||
330 | int len = ext4_ext_get_actual_len(ext); | ||
331 | struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es; | ||
332 | if (unlikely(block < le32_to_cpu(es->s_first_data_block) || | ||
333 | ((block + len) > ext4_blocks_count(es)))) | ||
334 | return 0; | ||
335 | else | ||
336 | return 1; | ||
337 | } | ||
338 | |||
339 | static int ext4_valid_extent_idx(struct inode *inode, | ||
340 | struct ext4_extent_idx *ext_idx) | ||
341 | { | ||
342 | ext4_fsblk_t block = idx_pblock(ext_idx); | ||
343 | struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es; | ||
344 | if (unlikely(block < le32_to_cpu(es->s_first_data_block) || | ||
345 | (block > ext4_blocks_count(es)))) | ||
346 | return 0; | ||
347 | else | ||
348 | return 1; | ||
349 | } | ||
350 | |||
351 | static int ext4_valid_extent_entries(struct inode *inode, | ||
352 | struct ext4_extent_header *eh, | ||
353 | int depth) | ||
354 | { | ||
355 | struct ext4_extent *ext; | ||
356 | struct ext4_extent_idx *ext_idx; | ||
357 | unsigned short entries; | ||
358 | if (eh->eh_entries == 0) | ||
359 | return 1; | ||
360 | |||
361 | entries = le16_to_cpu(eh->eh_entries); | ||
362 | |||
363 | if (depth == 0) { | ||
364 | /* leaf entries */ | ||
365 | ext = EXT_FIRST_EXTENT(eh); | ||
366 | while (entries) { | ||
367 | if (!ext4_valid_extent(inode, ext)) | ||
368 | return 0; | ||
369 | ext++; | ||
370 | entries--; | ||
371 | } | ||
372 | } else { | ||
373 | ext_idx = EXT_FIRST_INDEX(eh); | ||
374 | while (entries) { | ||
375 | if (!ext4_valid_extent_idx(inode, ext_idx)) | ||
376 | return 0; | ||
377 | ext_idx++; | ||
378 | entries--; | ||
379 | } | ||
380 | } | ||
381 | return 1; | ||
382 | } | ||
383 | |||
384 | static int __ext4_ext_check(const char *function, struct inode *inode, | ||
305 | struct ext4_extent_header *eh, | 385 | struct ext4_extent_header *eh, |
306 | int depth) | 386 | int depth) |
307 | { | 387 | { |
@@ -329,11 +409,15 @@ static int __ext4_ext_check_header(const char *function, struct inode *inode, | |||
329 | error_msg = "invalid eh_entries"; | 409 | error_msg = "invalid eh_entries"; |
330 | goto corrupted; | 410 | goto corrupted; |
331 | } | 411 | } |
412 | if (!ext4_valid_extent_entries(inode, eh, depth)) { | ||
413 | error_msg = "invalid extent entries"; | ||
414 | goto corrupted; | ||
415 | } | ||
332 | return 0; | 416 | return 0; |
333 | 417 | ||
334 | corrupted: | 418 | corrupted: |
335 | ext4_error(inode->i_sb, function, | 419 | ext4_error(inode->i_sb, function, |
336 | "bad header in inode #%lu: %s - magic %x, " | 420 | "bad header/extent in inode #%lu: %s - magic %x, " |
337 | "entries %u, max %u(%u), depth %u(%u)", | 421 | "entries %u, max %u(%u), depth %u(%u)", |
338 | inode->i_ino, error_msg, le16_to_cpu(eh->eh_magic), | 422 | inode->i_ino, error_msg, le16_to_cpu(eh->eh_magic), |
339 | le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max), | 423 | le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max), |
@@ -342,8 +426,13 @@ corrupted: | |||
342 | return -EIO; | 426 | return -EIO; |
343 | } | 427 | } |
344 | 428 | ||
345 | #define ext4_ext_check_header(inode, eh, depth) \ | 429 | #define ext4_ext_check(inode, eh, depth) \ |
346 | __ext4_ext_check_header(__func__, inode, eh, depth) | 430 | __ext4_ext_check(__func__, inode, eh, depth) |
431 | |||
432 | int ext4_ext_check_inode(struct inode *inode) | ||
433 | { | ||
434 | return ext4_ext_check(inode, ext_inode_hdr(inode), ext_depth(inode)); | ||
435 | } | ||
347 | 436 | ||
348 | #ifdef EXT_DEBUG | 437 | #ifdef EXT_DEBUG |
349 | static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path) | 438 | static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path) |
@@ -547,9 +636,6 @@ ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block, | |||
547 | 636 | ||
548 | eh = ext_inode_hdr(inode); | 637 | eh = ext_inode_hdr(inode); |
549 | depth = ext_depth(inode); | 638 | depth = ext_depth(inode); |
550 | if (ext4_ext_check_header(inode, eh, depth)) | ||
551 | return ERR_PTR(-EIO); | ||
552 | |||
553 | 639 | ||
554 | /* account possible depth increase */ | 640 | /* account possible depth increase */ |
555 | if (!path) { | 641 | if (!path) { |
@@ -565,6 +651,8 @@ ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block, | |||
565 | i = depth; | 651 | i = depth; |
566 | /* walk through the tree */ | 652 | /* walk through the tree */ |
567 | while (i) { | 653 | while (i) { |
654 | int need_to_validate = 0; | ||
655 | |||
568 | ext_debug("depth %d: num %d, max %d\n", | 656 | ext_debug("depth %d: num %d, max %d\n", |
569 | ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max)); | 657 | ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max)); |
570 | 658 | ||
@@ -573,10 +661,17 @@ ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block, | |||
573 | path[ppos].p_depth = i; | 661 | path[ppos].p_depth = i; |
574 | path[ppos].p_ext = NULL; | 662 | path[ppos].p_ext = NULL; |
575 | 663 | ||
576 | bh = sb_bread(inode->i_sb, path[ppos].p_block); | 664 | bh = sb_getblk(inode->i_sb, path[ppos].p_block); |
577 | if (!bh) | 665 | if (unlikely(!bh)) |
578 | goto err; | 666 | goto err; |
579 | 667 | if (!bh_uptodate_or_lock(bh)) { | |
668 | if (bh_submit_read(bh) < 0) { | ||
669 | put_bh(bh); | ||
670 | goto err; | ||
671 | } | ||
672 | /* validate the extent entries */ | ||
673 | need_to_validate = 1; | ||
674 | } | ||
580 | eh = ext_block_hdr(bh); | 675 | eh = ext_block_hdr(bh); |
581 | ppos++; | 676 | ppos++; |
582 | BUG_ON(ppos > depth); | 677 | BUG_ON(ppos > depth); |
@@ -584,7 +679,7 @@ ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block, | |||
584 | path[ppos].p_hdr = eh; | 679 | path[ppos].p_hdr = eh; |
585 | i--; | 680 | i--; |
586 | 681 | ||
587 | if (ext4_ext_check_header(inode, eh, i)) | 682 | if (need_to_validate && ext4_ext_check(inode, eh, i)) |
588 | goto err; | 683 | goto err; |
589 | } | 684 | } |
590 | 685 | ||
@@ -1122,7 +1217,8 @@ ext4_ext_search_right(struct inode *inode, struct ext4_ext_path *path, | |||
1122 | struct ext4_extent_idx *ix; | 1217 | struct ext4_extent_idx *ix; |
1123 | struct ext4_extent *ex; | 1218 | struct ext4_extent *ex; |
1124 | ext4_fsblk_t block; | 1219 | ext4_fsblk_t block; |
1125 | int depth, ee_len; | 1220 | int depth; /* Note, NOT eh_depth; depth from top of tree */ |
1221 | int ee_len; | ||
1126 | 1222 | ||
1127 | BUG_ON(path == NULL); | 1223 | BUG_ON(path == NULL); |
1128 | depth = path->p_depth; | 1224 | depth = path->p_depth; |
@@ -1179,7 +1275,8 @@ got_index: | |||
1179 | if (bh == NULL) | 1275 | if (bh == NULL) |
1180 | return -EIO; | 1276 | return -EIO; |
1181 | eh = ext_block_hdr(bh); | 1277 | eh = ext_block_hdr(bh); |
1182 | if (ext4_ext_check_header(inode, eh, depth)) { | 1278 | /* subtract from p_depth to get proper eh_depth */ |
1279 | if (ext4_ext_check(inode, eh, path->p_depth - depth)) { | ||
1183 | put_bh(bh); | 1280 | put_bh(bh); |
1184 | return -EIO; | 1281 | return -EIO; |
1185 | } | 1282 | } |
@@ -1192,7 +1289,7 @@ got_index: | |||
1192 | if (bh == NULL) | 1289 | if (bh == NULL) |
1193 | return -EIO; | 1290 | return -EIO; |
1194 | eh = ext_block_hdr(bh); | 1291 | eh = ext_block_hdr(bh); |
1195 | if (ext4_ext_check_header(inode, eh, path->p_depth - depth)) { | 1292 | if (ext4_ext_check(inode, eh, path->p_depth - depth)) { |
1196 | put_bh(bh); | 1293 | put_bh(bh); |
1197 | return -EIO; | 1294 | return -EIO; |
1198 | } | 1295 | } |
@@ -2135,7 +2232,7 @@ static int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start) | |||
2135 | return -ENOMEM; | 2232 | return -ENOMEM; |
2136 | } | 2233 | } |
2137 | path[0].p_hdr = ext_inode_hdr(inode); | 2234 | path[0].p_hdr = ext_inode_hdr(inode); |
2138 | if (ext4_ext_check_header(inode, path[0].p_hdr, depth)) { | 2235 | if (ext4_ext_check(inode, path[0].p_hdr, depth)) { |
2139 | err = -EIO; | 2236 | err = -EIO; |
2140 | goto out; | 2237 | goto out; |
2141 | } | 2238 | } |
@@ -2189,7 +2286,7 @@ static int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start) | |||
2189 | err = -EIO; | 2286 | err = -EIO; |
2190 | break; | 2287 | break; |
2191 | } | 2288 | } |
2192 | if (ext4_ext_check_header(inode, ext_block_hdr(bh), | 2289 | if (ext4_ext_check(inode, ext_block_hdr(bh), |
2193 | depth - i - 1)) { | 2290 | depth - i - 1)) { |
2194 | err = -EIO; | 2291 | err = -EIO; |
2195 | break; | 2292 | break; |