1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (c) 2003-2006, Cluster File Systems, Inc, info@clusterfs.com 4 * Written by Alex Tomas <alex@clusterfs.com> 5 * 6 * Architecture independence: 7 * Copyright (c) 2005, Bull S.A. 8 * Written by Pierre Peiffer <pierre.peiffer@bull.net> 9 */ 10 11 /* 12 * Extents support for EXT4 13 * 14 * TODO: 15 * - ext4*_error() should be used in some situations 16 * - analyze all BUG()/BUG_ON(), use -EIO where appropriate 17 * - smart tree reduction 18 */ 19 20 #include <linux/fs.h> 21 #include <linux/time.h> 22 #include <linux/jbd2.h> 23 #include <linux/highuid.h> 24 #include <linux/pagemap.h> 25 #include <linux/quotaops.h> 26 #include <linux/string.h> 27 #include <linux/slab.h> 28 #include <linux/uaccess.h> 29 #include <linux/fiemap.h> 30 #include <linux/iomap.h> 31 #include <linux/sched/mm.h> 32 #include "ext4_jbd2.h" 33 #include "ext4_extents.h" 34 #include "xattr.h" 35 #include <kunit/static_stub.h> 36 37 #include <trace/events/ext4.h> 38 39 /* 40 * used by extent splitting. 41 */ 42 #define EXT4_EXT_MAY_ZEROOUT 0x1 /* safe to zeroout if split fails \ 43 due to ENOSPC */ 44 static struct ext4_ext_path *ext4_split_convert_extents( 45 handle_t *handle, struct inode *inode, struct ext4_map_blocks *map, 46 struct ext4_ext_path *path, int flags, unsigned int *allocated); 47 48 static __le32 ext4_extent_block_csum(struct inode *inode, 49 struct ext4_extent_header *eh) 50 { 51 struct ext4_inode_info *ei = EXT4_I(inode); 52 __u32 csum; 53 54 csum = ext4_chksum(ei->i_csum_seed, (__u8 *)eh, 55 EXT4_EXTENT_TAIL_OFFSET(eh)); 56 return cpu_to_le32(csum); 57 } 58 59 static int ext4_extent_block_csum_verify(struct inode *inode, 60 struct ext4_extent_header *eh) 61 { 62 struct ext4_extent_tail *et; 63 64 if (!ext4_has_feature_metadata_csum(inode->i_sb)) 65 return 1; 66 67 et = find_ext4_extent_tail(eh); 68 if (et->et_checksum != ext4_extent_block_csum(inode, eh)) 69 return 0; 70 return 1; 71 } 72 73 static void ext4_extent_block_csum_set(struct inode *inode, 74 struct ext4_extent_header *eh) 75 { 76 struct ext4_extent_tail *et; 77 78 if (!ext4_has_feature_metadata_csum(inode->i_sb)) 79 return; 80 81 et = find_ext4_extent_tail(eh); 82 et->et_checksum = ext4_extent_block_csum(inode, eh); 83 } 84 85 static struct ext4_ext_path *ext4_split_extent_at(handle_t *handle, 86 struct inode *inode, 87 struct ext4_ext_path *path, 88 ext4_lblk_t split, int flags); 89 90 static int ext4_ext_trunc_restart_fn(struct inode *inode, int *dropped) 91 { 92 /* 93 * Drop i_data_sem to avoid deadlock with ext4_map_blocks. At this 94 * moment, get_block can be called only for blocks inside i_size since 95 * page cache has been already dropped and writes are blocked by 96 * i_rwsem. So we can safely drop the i_data_sem here. 97 */ 98 BUG_ON(EXT4_JOURNAL(inode) == NULL); 99 ext4_discard_preallocations(inode); 100 up_write(&EXT4_I(inode)->i_data_sem); 101 *dropped = 1; 102 return 0; 103 } 104 105 static inline void ext4_ext_path_brelse(struct ext4_ext_path *path) 106 { 107 brelse(path->p_bh); 108 path->p_bh = NULL; 109 } 110 111 static void ext4_ext_drop_refs(struct ext4_ext_path *path) 112 { 113 int depth, i; 114 115 if (IS_ERR_OR_NULL(path)) 116 return; 117 depth = path->p_depth; 118 for (i = 0; i <= depth; i++, path++) 119 ext4_ext_path_brelse(path); 120 } 121 122 void ext4_free_ext_path(struct ext4_ext_path *path) 123 { 124 if (IS_ERR_OR_NULL(path)) 125 return; 126 ext4_ext_drop_refs(path); 127 kfree(path); 128 } 129 130 /* 131 * Make sure 'handle' has at least 'check_cred' credits. If not, restart 132 * transaction with 'restart_cred' credits. The function drops i_data_sem 133 * when restarting transaction and gets it after transaction is restarted. 134 * 135 * The function returns 0 on success, 1 if transaction had to be restarted, 136 * and < 0 in case of fatal error. 137 */ 138 int ext4_datasem_ensure_credits(handle_t *handle, struct inode *inode, 139 int check_cred, int restart_cred, 140 int revoke_cred) 141 { 142 int ret; 143 int dropped = 0; 144 145 ret = ext4_journal_ensure_credits_fn(handle, check_cred, restart_cred, 146 revoke_cred, ext4_ext_trunc_restart_fn(inode, &dropped)); 147 if (dropped) 148 down_write(&EXT4_I(inode)->i_data_sem); 149 return ret; 150 } 151 152 /* 153 * could return: 154 * - EROFS 155 * - ENOMEM 156 */ 157 static int ext4_ext_get_access(handle_t *handle, struct inode *inode, 158 struct ext4_ext_path *path) 159 { 160 int err = 0; 161 162 if (path->p_bh) { 163 /* path points to block */ 164 BUFFER_TRACE(path->p_bh, "get_write_access"); 165 err = ext4_journal_get_write_access(handle, inode->i_sb, 166 path->p_bh, EXT4_JTR_NONE); 167 /* 168 * The extent buffer's verified bit will be set again in 169 * __ext4_ext_dirty(). We could leave an inconsistent 170 * buffer if the extents updating procudure break off du 171 * to some error happens, force to check it again. 172 */ 173 if (!err) 174 clear_buffer_verified(path->p_bh); 175 } 176 /* path points to leaf/index in inode body */ 177 /* we use in-core data, no need to protect them */ 178 return err; 179 } 180 181 /* 182 * could return: 183 * - EROFS 184 * - ENOMEM 185 * - EIO 186 */ 187 int __ext4_ext_dirty(const char *where, unsigned int line, 188 handle_t *handle, struct inode *inode, 189 struct ext4_ext_path *path) 190 { 191 int err; 192 193 KUNIT_STATIC_STUB_REDIRECT(__ext4_ext_dirty, where, line, handle, inode, 194 path); 195 196 WARN_ON(!rwsem_is_locked(&EXT4_I(inode)->i_data_sem)); 197 if (path->p_bh) { 198 ext4_extent_block_csum_set(inode, ext_block_hdr(path->p_bh)); 199 /* path points to block */ 200 err = __ext4_handle_dirty_metadata(where, line, handle, 201 inode, path->p_bh); 202 /* Extents updating done, re-set verified flag */ 203 if (!err) 204 set_buffer_verified(path->p_bh); 205 } else { 206 /* path points to leaf/index in inode body */ 207 err = ext4_mark_inode_dirty(handle, inode); 208 } 209 return err; 210 } 211 212 #define ext4_ext_dirty(handle, inode, path) \ 213 __ext4_ext_dirty(__func__, __LINE__, (handle), (inode), (path)) 214 215 static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode, 216 struct ext4_ext_path *path, 217 ext4_lblk_t block) 218 { 219 if (path) { 220 int depth = path->p_depth; 221 struct ext4_extent *ex; 222 223 /* 224 * Try to predict block placement assuming that we are 225 * filling in a file which will eventually be 226 * non-sparse --- i.e., in the case of libbfd writing 227 * an ELF object sections out-of-order but in a way 228 * the eventually results in a contiguous object or 229 * executable file, or some database extending a table 230 * space file. However, this is actually somewhat 231 * non-ideal if we are writing a sparse file such as 232 * qemu or KVM writing a raw image file that is going 233 * to stay fairly sparse, since it will end up 234 * fragmenting the file system's free space. Maybe we 235 * should have some hueristics or some way to allow 236 * userspace to pass a hint to file system, 237 * especially if the latter case turns out to be 238 * common. 239 */ 240 ex = path[depth].p_ext; 241 if (ex) { 242 ext4_fsblk_t ext_pblk = ext4_ext_pblock(ex); 243 ext4_lblk_t ext_block = le32_to_cpu(ex->ee_block); 244 245 if (block > ext_block) 246 return ext_pblk + (block - ext_block); 247 else 248 return ext_pblk - (ext_block - block); 249 } 250 251 /* it looks like index is empty; 252 * try to find starting block from index itself */ 253 if (path[depth].p_bh) 254 return path[depth].p_bh->b_blocknr; 255 } 256 257 /* OK. use inode's group */ 258 return ext4_inode_to_goal_block(inode); 259 } 260 261 /* 262 * Allocation for a meta data block 263 */ 264 static ext4_fsblk_t 265 ext4_ext_new_meta_block(handle_t *handle, struct inode *inode, 266 struct ext4_ext_path *path, 267 struct ext4_extent *ex, int *err, unsigned int flags) 268 { 269 ext4_fsblk_t goal, newblock; 270 271 goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block)); 272 newblock = ext4_new_meta_blocks(handle, inode, goal, flags, 273 NULL, err); 274 return newblock; 275 } 276 277 static inline int ext4_ext_space_block(struct inode *inode, int check) 278 { 279 int size; 280 281 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header)) 282 / sizeof(struct ext4_extent); 283 #ifdef AGGRESSIVE_TEST 284 if (!check && size > 6) 285 size = 6; 286 #endif 287 return size; 288 } 289 290 static inline int ext4_ext_space_block_idx(struct inode *inode, int check) 291 { 292 int size; 293 294 size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header)) 295 / sizeof(struct ext4_extent_idx); 296 #ifdef AGGRESSIVE_TEST 297 if (!check && size > 5) 298 size = 5; 299 #endif 300 return size; 301 } 302 303 static inline int ext4_ext_space_root(struct inode *inode, int check) 304 { 305 int size; 306 307 size = sizeof(EXT4_I(inode)->i_data); 308 size -= sizeof(struct ext4_extent_header); 309 size /= sizeof(struct ext4_extent); 310 #ifdef AGGRESSIVE_TEST 311 if (!check && size > 3) 312 size = 3; 313 #endif 314 return size; 315 } 316 317 static inline int ext4_ext_space_root_idx(struct inode *inode, int check) 318 { 319 int size; 320 321 size = sizeof(EXT4_I(inode)->i_data); 322 size -= sizeof(struct ext4_extent_header); 323 size /= sizeof(struct ext4_extent_idx); 324 #ifdef AGGRESSIVE_TEST 325 if (!check && size > 4) 326 size = 4; 327 #endif 328 return size; 329 } 330 331 static inline struct ext4_ext_path * 332 ext4_force_split_extent_at(handle_t *handle, struct inode *inode, 333 struct ext4_ext_path *path, ext4_lblk_t lblk, 334 int nofail) 335 { 336 int flags = EXT4_EX_NOCACHE | EXT4_GET_BLOCKS_SPLIT_NOMERGE; 337 338 if (nofail) 339 flags |= EXT4_GET_BLOCKS_METADATA_NOFAIL | EXT4_EX_NOFAIL; 340 341 return ext4_split_extent_at(handle, inode, path, lblk, flags); 342 } 343 344 static int 345 ext4_ext_max_entries(struct inode *inode, int depth) 346 { 347 int max; 348 349 if (depth == ext_depth(inode)) { 350 if (depth == 0) 351 max = ext4_ext_space_root(inode, 1); 352 else 353 max = ext4_ext_space_root_idx(inode, 1); 354 } else { 355 if (depth == 0) 356 max = ext4_ext_space_block(inode, 1); 357 else 358 max = ext4_ext_space_block_idx(inode, 1); 359 } 360 361 return max; 362 } 363 364 static int ext4_valid_extent(struct inode *inode, struct ext4_extent *ext) 365 { 366 ext4_fsblk_t block = ext4_ext_pblock(ext); 367 int len = ext4_ext_get_actual_len(ext); 368 ext4_lblk_t lblock = le32_to_cpu(ext->ee_block); 369 370 /* 371 * We allow neither: 372 * - zero length 373 * - overflow/wrap-around 374 */ 375 if (lblock + len <= lblock) 376 return 0; 377 return ext4_inode_block_valid(inode, block, len); 378 } 379 380 static int ext4_valid_extent_idx(struct inode *inode, 381 struct ext4_extent_idx *ext_idx) 382 { 383 ext4_fsblk_t block = ext4_idx_pblock(ext_idx); 384 385 return ext4_inode_block_valid(inode, block, 1); 386 } 387 388 static int ext4_valid_extent_entries(struct inode *inode, 389 struct ext4_extent_header *eh, 390 ext4_lblk_t lblk, ext4_fsblk_t *pblk, 391 int depth) 392 { 393 unsigned short entries; 394 ext4_lblk_t lblock = 0; 395 ext4_lblk_t cur = 0; 396 397 if (eh->eh_entries == 0) 398 return 1; 399 400 entries = le16_to_cpu(eh->eh_entries); 401 402 if (depth == 0) { 403 /* leaf entries */ 404 struct ext4_extent *ext = EXT_FIRST_EXTENT(eh); 405 406 /* 407 * The logical block in the first entry should equal to 408 * the number in the index block. 409 */ 410 if (depth != ext_depth(inode) && 411 lblk != le32_to_cpu(ext->ee_block)) 412 return 0; 413 while (entries) { 414 if (!ext4_valid_extent(inode, ext)) 415 return 0; 416 417 /* Check for overlapping extents */ 418 lblock = le32_to_cpu(ext->ee_block); 419 if (lblock < cur) { 420 *pblk = ext4_ext_pblock(ext); 421 return 0; 422 } 423 cur = lblock + ext4_ext_get_actual_len(ext); 424 ext++; 425 entries--; 426 } 427 } else { 428 struct ext4_extent_idx *ext_idx = EXT_FIRST_INDEX(eh); 429 430 /* 431 * The logical block in the first entry should equal to 432 * the number in the parent index block. 433 */ 434 if (depth != ext_depth(inode) && 435 lblk != le32_to_cpu(ext_idx->ei_block)) 436 return 0; 437 while (entries) { 438 if (!ext4_valid_extent_idx(inode, ext_idx)) 439 return 0; 440 441 /* Check for overlapping index extents */ 442 lblock = le32_to_cpu(ext_idx->ei_block); 443 if (lblock < cur) { 444 *pblk = ext4_idx_pblock(ext_idx); 445 return 0; 446 } 447 ext_idx++; 448 entries--; 449 cur = lblock + 1; 450 } 451 } 452 return 1; 453 } 454 455 static int __ext4_ext_check(const char *function, unsigned int line, 456 struct inode *inode, struct ext4_extent_header *eh, 457 int depth, ext4_fsblk_t pblk, ext4_lblk_t lblk) 458 { 459 const char *error_msg; 460 int max = 0, err = -EFSCORRUPTED; 461 462 if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) { 463 error_msg = "invalid magic"; 464 goto corrupted; 465 } 466 if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) { 467 error_msg = "unexpected eh_depth"; 468 goto corrupted; 469 } 470 if (unlikely(eh->eh_max == 0)) { 471 error_msg = "invalid eh_max"; 472 goto corrupted; 473 } 474 max = ext4_ext_max_entries(inode, depth); 475 if (unlikely(le16_to_cpu(eh->eh_max) > max)) { 476 error_msg = "too large eh_max"; 477 goto corrupted; 478 } 479 if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) { 480 error_msg = "invalid eh_entries"; 481 goto corrupted; 482 } 483 if (unlikely((eh->eh_entries == 0) && (depth > 0))) { 484 error_msg = "eh_entries is 0 but eh_depth is > 0"; 485 goto corrupted; 486 } 487 if (!ext4_valid_extent_entries(inode, eh, lblk, &pblk, depth)) { 488 error_msg = "invalid extent entries"; 489 goto corrupted; 490 } 491 if (unlikely(depth > 32)) { 492 error_msg = "too large eh_depth"; 493 goto corrupted; 494 } 495 /* Verify checksum on non-root extent tree nodes */ 496 if (ext_depth(inode) != depth && 497 !ext4_extent_block_csum_verify(inode, eh)) { 498 error_msg = "extent tree corrupted"; 499 err = -EFSBADCRC; 500 goto corrupted; 501 } 502 return 0; 503 504 corrupted: 505 ext4_error_inode_err(inode, function, line, 0, -err, 506 "pblk %llu bad header/extent: %s - magic %x, " 507 "entries %u, max %u(%u), depth %u(%u)", 508 (unsigned long long) pblk, error_msg, 509 le16_to_cpu(eh->eh_magic), 510 le16_to_cpu(eh->eh_entries), 511 le16_to_cpu(eh->eh_max), 512 max, le16_to_cpu(eh->eh_depth), depth); 513 return err; 514 } 515 516 #define ext4_ext_check(inode, eh, depth, pblk) \ 517 __ext4_ext_check(__func__, __LINE__, (inode), (eh), (depth), (pblk), 0) 518 519 int ext4_ext_check_inode(struct inode *inode) 520 { 521 return ext4_ext_check(inode, ext_inode_hdr(inode), ext_depth(inode), 0); 522 } 523 524 static void ext4_cache_extents(struct inode *inode, 525 struct ext4_extent_header *eh) 526 { 527 struct ext4_extent *ex = EXT_FIRST_EXTENT(eh); 528 ext4_lblk_t prev = 0; 529 int i; 530 531 KUNIT_STATIC_STUB_REDIRECT(ext4_cache_extents, inode, eh); 532 533 for (i = le16_to_cpu(eh->eh_entries); i > 0; i--, ex++) { 534 unsigned int status = EXTENT_STATUS_WRITTEN; 535 ext4_lblk_t lblk = le32_to_cpu(ex->ee_block); 536 int len = ext4_ext_get_actual_len(ex); 537 538 if (prev && (prev != lblk)) 539 ext4_es_cache_extent(inode, prev, lblk - prev, ~0, 540 EXTENT_STATUS_HOLE); 541 542 if (ext4_ext_is_unwritten(ex)) 543 status = EXTENT_STATUS_UNWRITTEN; 544 ext4_es_cache_extent(inode, lblk, len, 545 ext4_ext_pblock(ex), status); 546 prev = lblk + len; 547 } 548 } 549 550 static struct buffer_head * 551 __read_extent_tree_block(const char *function, unsigned int line, 552 struct inode *inode, struct ext4_extent_idx *idx, 553 int depth, int flags) 554 { 555 struct buffer_head *bh; 556 int err; 557 gfp_t gfp_flags = __GFP_MOVABLE | GFP_NOFS; 558 ext4_fsblk_t pblk; 559 560 if (flags & EXT4_EX_NOFAIL) 561 gfp_flags |= __GFP_NOFAIL; 562 563 pblk = ext4_idx_pblock(idx); 564 bh = sb_getblk_gfp(inode->i_sb, pblk, gfp_flags); 565 if (unlikely(!bh)) 566 return ERR_PTR(-ENOMEM); 567 568 if (!bh_uptodate_or_lock(bh)) { 569 trace_ext4_ext_load_extent(inode, pblk, _RET_IP_); 570 err = ext4_read_bh(bh, 0, NULL, false); 571 if (err < 0) 572 goto errout; 573 } 574 if (buffer_verified(bh) && !(flags & EXT4_EX_FORCE_CACHE)) 575 return bh; 576 err = __ext4_ext_check(function, line, inode, ext_block_hdr(bh), 577 depth, pblk, le32_to_cpu(idx->ei_block)); 578 if (err) 579 goto errout; 580 set_buffer_verified(bh); 581 /* 582 * If this is a leaf block, cache all of its entries 583 */ 584 if (!(flags & EXT4_EX_NOCACHE) && depth == 0) { 585 struct ext4_extent_header *eh = ext_block_hdr(bh); 586 ext4_cache_extents(inode, eh); 587 } 588 return bh; 589 errout: 590 put_bh(bh); 591 return ERR_PTR(err); 592 593 } 594 595 #define read_extent_tree_block(inode, idx, depth, flags) \ 596 __read_extent_tree_block(__func__, __LINE__, (inode), (idx), \ 597 (depth), (flags)) 598 599 /* 600 * This function is called to cache a file's extent information in the 601 * extent status tree 602 */ 603 int ext4_ext_precache(struct inode *inode) 604 { 605 struct ext4_inode_info *ei = EXT4_I(inode); 606 struct ext4_ext_path *path = NULL; 607 struct buffer_head *bh; 608 int i = 0, depth, ret = 0; 609 610 if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 611 return 0; /* not an extent-mapped inode */ 612 613 ext4_check_map_extents_env(inode); 614 615 down_read(&ei->i_data_sem); 616 depth = ext_depth(inode); 617 618 /* Don't cache anything if there are no external extent blocks */ 619 if (!depth) { 620 up_read(&ei->i_data_sem); 621 return ret; 622 } 623 624 path = kzalloc_objs(struct ext4_ext_path, depth + 1, GFP_NOFS); 625 if (path == NULL) { 626 up_read(&ei->i_data_sem); 627 return -ENOMEM; 628 } 629 630 path[0].p_hdr = ext_inode_hdr(inode); 631 ret = ext4_ext_check(inode, path[0].p_hdr, depth, 0); 632 if (ret) 633 goto out; 634 path[0].p_idx = EXT_FIRST_INDEX(path[0].p_hdr); 635 while (i >= 0) { 636 /* 637 * If this is a leaf block or we've reached the end of 638 * the index block, go up 639 */ 640 if ((i == depth) || 641 path[i].p_idx > EXT_LAST_INDEX(path[i].p_hdr)) { 642 ext4_ext_path_brelse(path + i); 643 i--; 644 continue; 645 } 646 bh = read_extent_tree_block(inode, path[i].p_idx++, 647 depth - i - 1, 648 EXT4_EX_FORCE_CACHE); 649 if (IS_ERR(bh)) { 650 ret = PTR_ERR(bh); 651 break; 652 } 653 i++; 654 path[i].p_bh = bh; 655 path[i].p_hdr = ext_block_hdr(bh); 656 path[i].p_idx = EXT_FIRST_INDEX(path[i].p_hdr); 657 } 658 ext4_set_inode_state(inode, EXT4_STATE_EXT_PRECACHED); 659 out: 660 up_read(&ei->i_data_sem); 661 ext4_free_ext_path(path); 662 return ret; 663 } 664 665 #ifdef EXT_DEBUG 666 static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path) 667 { 668 int k, l = path->p_depth; 669 670 ext_debug(inode, "path:"); 671 for (k = 0; k <= l; k++, path++) { 672 if (path->p_idx) { 673 ext_debug(inode, " %d->%llu", 674 le32_to_cpu(path->p_idx->ei_block), 675 ext4_idx_pblock(path->p_idx)); 676 } else if (path->p_ext) { 677 ext_debug(inode, " %d:[%d]%d:%llu ", 678 le32_to_cpu(path->p_ext->ee_block), 679 ext4_ext_is_unwritten(path->p_ext), 680 ext4_ext_get_actual_len(path->p_ext), 681 ext4_ext_pblock(path->p_ext)); 682 } else 683 ext_debug(inode, " []"); 684 } 685 ext_debug(inode, "\n"); 686 } 687 688 static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path) 689 { 690 int depth = ext_depth(inode); 691 struct ext4_extent_header *eh; 692 struct ext4_extent *ex; 693 int i; 694 695 if (IS_ERR_OR_NULL(path)) 696 return; 697 698 eh = path[depth].p_hdr; 699 ex = EXT_FIRST_EXTENT(eh); 700 701 ext_debug(inode, "Displaying leaf extents\n"); 702 703 for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) { 704 ext_debug(inode, "%d:[%d]%d:%llu ", le32_to_cpu(ex->ee_block), 705 ext4_ext_is_unwritten(ex), 706 ext4_ext_get_actual_len(ex), ext4_ext_pblock(ex)); 707 } 708 ext_debug(inode, "\n"); 709 } 710 711 static void ext4_ext_show_move(struct inode *inode, struct ext4_ext_path *path, 712 ext4_fsblk_t newblock, int level) 713 { 714 int depth = ext_depth(inode); 715 struct ext4_extent *ex; 716 717 if (depth != level) { 718 struct ext4_extent_idx *idx; 719 idx = path[level].p_idx; 720 while (idx <= EXT_MAX_INDEX(path[level].p_hdr)) { 721 ext_debug(inode, "%d: move %d:%llu in new index %llu\n", 722 level, le32_to_cpu(idx->ei_block), 723 ext4_idx_pblock(idx), newblock); 724 idx++; 725 } 726 727 return; 728 } 729 730 ex = path[depth].p_ext; 731 while (ex <= EXT_MAX_EXTENT(path[depth].p_hdr)) { 732 ext_debug(inode, "move %d:%llu:[%d]%d in new leaf %llu\n", 733 le32_to_cpu(ex->ee_block), 734 ext4_ext_pblock(ex), 735 ext4_ext_is_unwritten(ex), 736 ext4_ext_get_actual_len(ex), 737 newblock); 738 ex++; 739 } 740 } 741 742 #else 743 #define ext4_ext_show_path(inode, path) 744 #define ext4_ext_show_leaf(inode, path) 745 #define ext4_ext_show_move(inode, path, newblock, level) 746 #endif 747 748 /* 749 * ext4_ext_binsearch_idx: 750 * binary search for the closest index of the given block 751 * the header must be checked before calling this 752 */ 753 static void 754 ext4_ext_binsearch_idx(struct inode *inode, 755 struct ext4_ext_path *path, ext4_lblk_t block) 756 { 757 struct ext4_extent_header *eh = path->p_hdr; 758 struct ext4_extent_idx *r, *l, *m; 759 760 761 ext_debug(inode, "binsearch for %u(idx): ", block); 762 763 l = EXT_FIRST_INDEX(eh) + 1; 764 r = EXT_LAST_INDEX(eh); 765 while (l <= r) { 766 m = l + (r - l) / 2; 767 ext_debug(inode, "%p(%u):%p(%u):%p(%u) ", l, 768 le32_to_cpu(l->ei_block), m, le32_to_cpu(m->ei_block), 769 r, le32_to_cpu(r->ei_block)); 770 771 if (block < le32_to_cpu(m->ei_block)) 772 r = m - 1; 773 else 774 l = m + 1; 775 } 776 777 path->p_idx = l - 1; 778 ext_debug(inode, " -> %u->%lld ", le32_to_cpu(path->p_idx->ei_block), 779 ext4_idx_pblock(path->p_idx)); 780 781 #ifdef CHECK_BINSEARCH 782 { 783 struct ext4_extent_idx *chix, *ix; 784 int k; 785 786 chix = ix = EXT_FIRST_INDEX(eh); 787 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) { 788 if (k != 0 && le32_to_cpu(ix->ei_block) <= 789 le32_to_cpu(ix[-1].ei_block)) { 790 printk(KERN_DEBUG "k=%d, ix=0x%p, " 791 "first=0x%p\n", k, 792 ix, EXT_FIRST_INDEX(eh)); 793 printk(KERN_DEBUG "%u <= %u\n", 794 le32_to_cpu(ix->ei_block), 795 le32_to_cpu(ix[-1].ei_block)); 796 } 797 BUG_ON(k && le32_to_cpu(ix->ei_block) 798 <= le32_to_cpu(ix[-1].ei_block)); 799 if (block < le32_to_cpu(ix->ei_block)) 800 break; 801 chix = ix; 802 } 803 BUG_ON(chix != path->p_idx); 804 } 805 #endif 806 807 } 808 809 /* 810 * ext4_ext_binsearch: 811 * binary search for closest extent of the given block 812 * the header must be checked before calling this 813 */ 814 static void 815 ext4_ext_binsearch(struct inode *inode, 816 struct ext4_ext_path *path, ext4_lblk_t block) 817 { 818 struct ext4_extent_header *eh = path->p_hdr; 819 struct ext4_extent *r, *l, *m; 820 821 if (eh->eh_entries == 0) { 822 /* 823 * this leaf is empty: 824 * we get such a leaf in split/add case 825 */ 826 return; 827 } 828 829 ext_debug(inode, "binsearch for %u: ", block); 830 831 l = EXT_FIRST_EXTENT(eh) + 1; 832 r = EXT_LAST_EXTENT(eh); 833 834 while (l <= r) { 835 m = l + (r - l) / 2; 836 ext_debug(inode, "%p(%u):%p(%u):%p(%u) ", l, 837 le32_to_cpu(l->ee_block), m, le32_to_cpu(m->ee_block), 838 r, le32_to_cpu(r->ee_block)); 839 840 if (block < le32_to_cpu(m->ee_block)) 841 r = m - 1; 842 else 843 l = m + 1; 844 } 845 846 path->p_ext = l - 1; 847 ext_debug(inode, " -> %d:%llu:[%d]%d ", 848 le32_to_cpu(path->p_ext->ee_block), 849 ext4_ext_pblock(path->p_ext), 850 ext4_ext_is_unwritten(path->p_ext), 851 ext4_ext_get_actual_len(path->p_ext)); 852 853 #ifdef CHECK_BINSEARCH 854 { 855 struct ext4_extent *chex, *ex; 856 int k; 857 858 chex = ex = EXT_FIRST_EXTENT(eh); 859 for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) { 860 BUG_ON(k && le32_to_cpu(ex->ee_block) 861 <= le32_to_cpu(ex[-1].ee_block)); 862 if (block < le32_to_cpu(ex->ee_block)) 863 break; 864 chex = ex; 865 } 866 BUG_ON(chex != path->p_ext); 867 } 868 #endif 869 870 } 871 872 void ext4_ext_tree_init(handle_t *handle, struct inode *inode) 873 { 874 struct ext4_extent_header *eh; 875 876 eh = ext_inode_hdr(inode); 877 eh->eh_depth = 0; 878 eh->eh_entries = 0; 879 eh->eh_magic = EXT4_EXT_MAGIC; 880 eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode, 0)); 881 eh->eh_generation = 0; 882 ext4_mark_inode_dirty(handle, inode); 883 } 884 885 struct ext4_ext_path * 886 ext4_find_extent(struct inode *inode, ext4_lblk_t block, 887 struct ext4_ext_path *path, int flags) 888 { 889 struct ext4_extent_header *eh; 890 struct buffer_head *bh; 891 short int depth, i, ppos = 0; 892 int ret; 893 gfp_t gfp_flags = GFP_NOFS; 894 895 KUNIT_STATIC_STUB_REDIRECT(ext4_find_extent, inode, block, path, flags); 896 897 if (flags & EXT4_EX_NOFAIL) 898 gfp_flags |= __GFP_NOFAIL; 899 900 eh = ext_inode_hdr(inode); 901 depth = ext_depth(inode); 902 if (depth < 0 || depth > EXT4_MAX_EXTENT_DEPTH) { 903 EXT4_ERROR_INODE(inode, "inode has invalid extent depth: %d", 904 depth); 905 ret = -EFSCORRUPTED; 906 goto err; 907 } 908 909 if (path) { 910 ext4_ext_drop_refs(path); 911 if (depth > path[0].p_maxdepth) { 912 kfree(path); 913 path = NULL; 914 } 915 } 916 if (!path) { 917 /* account possible depth increase */ 918 path = kzalloc_objs(struct ext4_ext_path, depth + 2, gfp_flags); 919 if (unlikely(!path)) 920 return ERR_PTR(-ENOMEM); 921 path[0].p_maxdepth = depth + 1; 922 } 923 path[0].p_hdr = eh; 924 path[0].p_bh = NULL; 925 926 i = depth; 927 if (!(flags & EXT4_EX_NOCACHE) && depth == 0) 928 ext4_cache_extents(inode, eh); 929 /* walk through the tree */ 930 while (i) { 931 ext_debug(inode, "depth %d: num %d, max %d\n", 932 ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max)); 933 934 ext4_ext_binsearch_idx(inode, path + ppos, block); 935 path[ppos].p_block = ext4_idx_pblock(path[ppos].p_idx); 936 path[ppos].p_depth = i; 937 path[ppos].p_ext = NULL; 938 939 bh = read_extent_tree_block(inode, path[ppos].p_idx, --i, flags); 940 if (IS_ERR(bh)) { 941 ret = PTR_ERR(bh); 942 goto err; 943 } 944 945 eh = ext_block_hdr(bh); 946 ppos++; 947 path[ppos].p_bh = bh; 948 path[ppos].p_hdr = eh; 949 } 950 951 path[ppos].p_depth = i; 952 path[ppos].p_ext = NULL; 953 path[ppos].p_idx = NULL; 954 955 /* find extent */ 956 ext4_ext_binsearch(inode, path + ppos, block); 957 /* if not an empty leaf */ 958 if (path[ppos].p_ext) 959 path[ppos].p_block = ext4_ext_pblock(path[ppos].p_ext); 960 961 ext4_ext_show_path(inode, path); 962 963 return path; 964 965 err: 966 ext4_free_ext_path(path); 967 return ERR_PTR(ret); 968 } 969 970 /* 971 * ext4_ext_insert_index: 972 * insert new index [@logical;@ptr] into the block at @curp; 973 * check where to insert: before @curp or after @curp 974 */ 975 static int ext4_ext_insert_index(handle_t *handle, struct inode *inode, 976 struct ext4_ext_path *curp, 977 int logical, ext4_fsblk_t ptr) 978 { 979 struct ext4_extent_idx *ix; 980 int len, err; 981 982 err = ext4_ext_get_access(handle, inode, curp); 983 if (err) 984 return err; 985 986 if (unlikely(logical == le32_to_cpu(curp->p_idx->ei_block))) { 987 EXT4_ERROR_INODE(inode, 988 "logical %d == ei_block %d!", 989 logical, le32_to_cpu(curp->p_idx->ei_block)); 990 return -EFSCORRUPTED; 991 } 992 993 if (unlikely(le16_to_cpu(curp->p_hdr->eh_entries) 994 >= le16_to_cpu(curp->p_hdr->eh_max))) { 995 EXT4_ERROR_INODE(inode, 996 "eh_entries %d >= eh_max %d!", 997 le16_to_cpu(curp->p_hdr->eh_entries), 998 le16_to_cpu(curp->p_hdr->eh_max)); 999 return -EFSCORRUPTED; 1000 } 1001 1002 if (logical > le32_to_cpu(curp->p_idx->ei_block)) { 1003 /* insert after */ 1004 ext_debug(inode, "insert new index %d after: %llu\n", 1005 logical, ptr); 1006 ix = curp->p_idx + 1; 1007 } else { 1008 /* insert before */ 1009 ext_debug(inode, "insert new index %d before: %llu\n", 1010 logical, ptr); 1011 ix = curp->p_idx; 1012 } 1013 1014 if (unlikely(ix > EXT_MAX_INDEX(curp->p_hdr))) { 1015 EXT4_ERROR_INODE(inode, "ix > EXT_MAX_INDEX!"); 1016 return -EFSCORRUPTED; 1017 } 1018 1019 len = EXT_LAST_INDEX(curp->p_hdr) - ix + 1; 1020 BUG_ON(len < 0); 1021 if (len > 0) { 1022 ext_debug(inode, "insert new index %d: " 1023 "move %d indices from 0x%p to 0x%p\n", 1024 logical, len, ix, ix + 1); 1025 memmove(ix + 1, ix, len * sizeof(struct ext4_extent_idx)); 1026 } 1027 1028 ix->ei_block = cpu_to_le32(logical); 1029 ext4_idx_store_pblock(ix, ptr); 1030 le16_add_cpu(&curp->p_hdr->eh_entries, 1); 1031 1032 if (unlikely(ix > EXT_LAST_INDEX(curp->p_hdr))) { 1033 EXT4_ERROR_INODE(inode, "ix > EXT_LAST_INDEX!"); 1034 return -EFSCORRUPTED; 1035 } 1036 1037 err = ext4_ext_dirty(handle, inode, curp); 1038 ext4_std_error(inode->i_sb, err); 1039 1040 return err; 1041 } 1042 1043 /* 1044 * ext4_ext_split: 1045 * inserts new subtree into the path, using free index entry 1046 * at depth @at: 1047 * - allocates all needed blocks (new leaf and all intermediate index blocks) 1048 * - makes decision where to split 1049 * - moves remaining extents and index entries (right to the split point) 1050 * into the newly allocated blocks 1051 * - initializes subtree 1052 */ 1053 static int ext4_ext_split(handle_t *handle, struct inode *inode, 1054 unsigned int flags, 1055 struct ext4_ext_path *path, 1056 struct ext4_extent *newext, int at) 1057 { 1058 struct buffer_head *bh = NULL; 1059 int depth = ext_depth(inode); 1060 struct ext4_extent_header *neh; 1061 struct ext4_extent_idx *fidx; 1062 int i = at, k, m, a; 1063 ext4_fsblk_t newblock, oldblock; 1064 __le32 border; 1065 ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */ 1066 gfp_t gfp_flags = GFP_NOFS; 1067 int err = 0; 1068 size_t ext_size = 0; 1069 1070 if (flags & EXT4_EX_NOFAIL) 1071 gfp_flags |= __GFP_NOFAIL; 1072 1073 /* make decision: where to split? */ 1074 /* FIXME: now decision is simplest: at current extent */ 1075 1076 /* if current leaf will be split, then we should use 1077 * border from split point */ 1078 if (unlikely(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr))) { 1079 EXT4_ERROR_INODE(inode, "p_ext > EXT_MAX_EXTENT!"); 1080 return -EFSCORRUPTED; 1081 } 1082 if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) { 1083 border = path[depth].p_ext[1].ee_block; 1084 ext_debug(inode, "leaf will be split." 1085 " next leaf starts at %d\n", 1086 le32_to_cpu(border)); 1087 } else { 1088 border = newext->ee_block; 1089 ext_debug(inode, "leaf will be added." 1090 " next leaf starts at %d\n", 1091 le32_to_cpu(border)); 1092 } 1093 1094 /* 1095 * If error occurs, then we break processing 1096 * and mark filesystem read-only. index won't 1097 * be inserted and tree will be in consistent 1098 * state. Next mount will repair buffers too. 1099 */ 1100 1101 /* 1102 * Get array to track all allocated blocks. 1103 * We need this to handle errors and free blocks 1104 * upon them. 1105 */ 1106 ablocks = kzalloc_objs(ext4_fsblk_t, depth, gfp_flags); 1107 if (!ablocks) 1108 return -ENOMEM; 1109 1110 /* allocate all needed blocks */ 1111 ext_debug(inode, "allocate %d blocks for indexes/leaf\n", depth - at); 1112 for (a = 0; a < depth - at; a++) { 1113 newblock = ext4_ext_new_meta_block(handle, inode, path, 1114 newext, &err, flags); 1115 if (newblock == 0) 1116 goto cleanup; 1117 ablocks[a] = newblock; 1118 } 1119 1120 /* initialize new leaf */ 1121 newblock = ablocks[--a]; 1122 if (unlikely(newblock == 0)) { 1123 EXT4_ERROR_INODE(inode, "newblock == 0!"); 1124 err = -EFSCORRUPTED; 1125 goto cleanup; 1126 } 1127 bh = sb_getblk_gfp(inode->i_sb, newblock, __GFP_MOVABLE | GFP_NOFS); 1128 if (unlikely(!bh)) { 1129 err = -ENOMEM; 1130 goto cleanup; 1131 } 1132 lock_buffer(bh); 1133 1134 err = ext4_journal_get_create_access(handle, inode->i_sb, bh, 1135 EXT4_JTR_NONE); 1136 if (err) 1137 goto cleanup; 1138 1139 neh = ext_block_hdr(bh); 1140 neh->eh_entries = 0; 1141 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0)); 1142 neh->eh_magic = EXT4_EXT_MAGIC; 1143 neh->eh_depth = 0; 1144 neh->eh_generation = 0; 1145 1146 /* move remainder of path[depth] to the new leaf */ 1147 if (unlikely(path[depth].p_hdr->eh_entries != 1148 path[depth].p_hdr->eh_max)) { 1149 EXT4_ERROR_INODE(inode, "eh_entries %d != eh_max %d!", 1150 path[depth].p_hdr->eh_entries, 1151 path[depth].p_hdr->eh_max); 1152 err = -EFSCORRUPTED; 1153 goto cleanup; 1154 } 1155 /* start copy from next extent */ 1156 m = EXT_MAX_EXTENT(path[depth].p_hdr) - path[depth].p_ext++; 1157 ext4_ext_show_move(inode, path, newblock, depth); 1158 if (m) { 1159 struct ext4_extent *ex; 1160 ex = EXT_FIRST_EXTENT(neh); 1161 memmove(ex, path[depth].p_ext, sizeof(struct ext4_extent) * m); 1162 le16_add_cpu(&neh->eh_entries, m); 1163 } 1164 1165 /* zero out unused area in the extent block */ 1166 ext_size = sizeof(struct ext4_extent_header) + 1167 sizeof(struct ext4_extent) * le16_to_cpu(neh->eh_entries); 1168 memset(bh->b_data + ext_size, 0, inode->i_sb->s_blocksize - ext_size); 1169 ext4_extent_block_csum_set(inode, neh); 1170 set_buffer_uptodate(bh); 1171 unlock_buffer(bh); 1172 1173 err = ext4_handle_dirty_metadata(handle, inode, bh); 1174 if (err) 1175 goto cleanup; 1176 brelse(bh); 1177 bh = NULL; 1178 1179 /* correct old leaf */ 1180 if (m) { 1181 err = ext4_ext_get_access(handle, inode, path + depth); 1182 if (err) 1183 goto cleanup; 1184 le16_add_cpu(&path[depth].p_hdr->eh_entries, -m); 1185 err = ext4_ext_dirty(handle, inode, path + depth); 1186 if (err) 1187 goto cleanup; 1188 1189 } 1190 1191 /* create intermediate indexes */ 1192 k = depth - at - 1; 1193 if (unlikely(k < 0)) { 1194 EXT4_ERROR_INODE(inode, "k %d < 0!", k); 1195 err = -EFSCORRUPTED; 1196 goto cleanup; 1197 } 1198 if (k) 1199 ext_debug(inode, "create %d intermediate indices\n", k); 1200 /* insert new index into current index block */ 1201 /* current depth stored in i var */ 1202 i = depth - 1; 1203 while (k--) { 1204 oldblock = newblock; 1205 newblock = ablocks[--a]; 1206 bh = sb_getblk(inode->i_sb, newblock); 1207 if (unlikely(!bh)) { 1208 err = -ENOMEM; 1209 goto cleanup; 1210 } 1211 lock_buffer(bh); 1212 1213 err = ext4_journal_get_create_access(handle, inode->i_sb, bh, 1214 EXT4_JTR_NONE); 1215 if (err) 1216 goto cleanup; 1217 1218 neh = ext_block_hdr(bh); 1219 neh->eh_entries = cpu_to_le16(1); 1220 neh->eh_magic = EXT4_EXT_MAGIC; 1221 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0)); 1222 neh->eh_depth = cpu_to_le16(depth - i); 1223 neh->eh_generation = 0; 1224 fidx = EXT_FIRST_INDEX(neh); 1225 fidx->ei_block = border; 1226 ext4_idx_store_pblock(fidx, oldblock); 1227 1228 ext_debug(inode, "int.index at %d (block %llu): %u -> %llu\n", 1229 i, newblock, le32_to_cpu(border), oldblock); 1230 1231 /* move remainder of path[i] to the new index block */ 1232 if (unlikely(EXT_MAX_INDEX(path[i].p_hdr) != 1233 EXT_LAST_INDEX(path[i].p_hdr))) { 1234 EXT4_ERROR_INODE(inode, 1235 "EXT_MAX_INDEX != EXT_LAST_INDEX ee_block %d!", 1236 le32_to_cpu(path[i].p_ext->ee_block)); 1237 err = -EFSCORRUPTED; 1238 goto cleanup; 1239 } 1240 /* start copy indexes */ 1241 m = EXT_MAX_INDEX(path[i].p_hdr) - path[i].p_idx++; 1242 ext_debug(inode, "cur 0x%p, last 0x%p\n", path[i].p_idx, 1243 EXT_MAX_INDEX(path[i].p_hdr)); 1244 ext4_ext_show_move(inode, path, newblock, i); 1245 if (m) { 1246 memmove(++fidx, path[i].p_idx, 1247 sizeof(struct ext4_extent_idx) * m); 1248 le16_add_cpu(&neh->eh_entries, m); 1249 } 1250 /* zero out unused area in the extent block */ 1251 ext_size = sizeof(struct ext4_extent_header) + 1252 (sizeof(struct ext4_extent) * le16_to_cpu(neh->eh_entries)); 1253 memset(bh->b_data + ext_size, 0, 1254 inode->i_sb->s_blocksize - ext_size); 1255 ext4_extent_block_csum_set(inode, neh); 1256 set_buffer_uptodate(bh); 1257 unlock_buffer(bh); 1258 1259 err = ext4_handle_dirty_metadata(handle, inode, bh); 1260 if (err) 1261 goto cleanup; 1262 brelse(bh); 1263 bh = NULL; 1264 1265 /* correct old index */ 1266 if (m) { 1267 err = ext4_ext_get_access(handle, inode, path + i); 1268 if (err) 1269 goto cleanup; 1270 le16_add_cpu(&path[i].p_hdr->eh_entries, -m); 1271 err = ext4_ext_dirty(handle, inode, path + i); 1272 if (err) 1273 goto cleanup; 1274 } 1275 1276 i--; 1277 } 1278 1279 /* insert new index */ 1280 err = ext4_ext_insert_index(handle, inode, path + at, 1281 le32_to_cpu(border), newblock); 1282 1283 cleanup: 1284 if (bh) { 1285 if (buffer_locked(bh)) 1286 unlock_buffer(bh); 1287 brelse(bh); 1288 } 1289 1290 if (err) { 1291 /* free all allocated blocks in error case */ 1292 for (i = 0; i < depth; i++) { 1293 if (!ablocks[i]) 1294 continue; 1295 ext4_free_blocks(handle, inode, NULL, ablocks[i], 1, 1296 EXT4_FREE_BLOCKS_METADATA); 1297 } 1298 } 1299 kfree(ablocks); 1300 1301 return err; 1302 } 1303 1304 /* 1305 * ext4_ext_grow_indepth: 1306 * implements tree growing procedure: 1307 * - allocates new block 1308 * - moves top-level data (index block or leaf) into the new block 1309 * - initializes new top-level, creating index that points to the 1310 * just created block 1311 */ 1312 static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode, 1313 unsigned int flags) 1314 { 1315 struct ext4_extent_header *neh; 1316 struct buffer_head *bh; 1317 ext4_fsblk_t newblock, goal = 0; 1318 struct ext4_super_block *es = EXT4_SB(inode->i_sb)->s_es; 1319 int err = 0; 1320 size_t ext_size = 0; 1321 1322 /* Try to prepend new index to old one */ 1323 if (ext_depth(inode)) 1324 goal = ext4_idx_pblock(EXT_FIRST_INDEX(ext_inode_hdr(inode))); 1325 if (goal > le32_to_cpu(es->s_first_data_block)) { 1326 flags |= EXT4_MB_HINT_TRY_GOAL; 1327 goal--; 1328 } else 1329 goal = ext4_inode_to_goal_block(inode); 1330 newblock = ext4_new_meta_blocks(handle, inode, goal, flags, 1331 NULL, &err); 1332 if (newblock == 0) 1333 return err; 1334 1335 bh = sb_getblk_gfp(inode->i_sb, newblock, __GFP_MOVABLE | GFP_NOFS); 1336 if (unlikely(!bh)) 1337 return -ENOMEM; 1338 lock_buffer(bh); 1339 1340 err = ext4_journal_get_create_access(handle, inode->i_sb, bh, 1341 EXT4_JTR_NONE); 1342 if (err) { 1343 unlock_buffer(bh); 1344 goto out; 1345 } 1346 1347 ext_size = sizeof(EXT4_I(inode)->i_data); 1348 /* move top-level index/leaf into new block */ 1349 memmove(bh->b_data, EXT4_I(inode)->i_data, ext_size); 1350 /* zero out unused area in the extent block */ 1351 memset(bh->b_data + ext_size, 0, inode->i_sb->s_blocksize - ext_size); 1352 1353 /* set size of new block */ 1354 neh = ext_block_hdr(bh); 1355 /* old root could have indexes or leaves 1356 * so calculate e_max right way */ 1357 if (ext_depth(inode)) 1358 neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0)); 1359 else 1360 neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0)); 1361 neh->eh_magic = EXT4_EXT_MAGIC; 1362 ext4_extent_block_csum_set(inode, neh); 1363 set_buffer_uptodate(bh); 1364 set_buffer_verified(bh); 1365 unlock_buffer(bh); 1366 1367 err = ext4_handle_dirty_metadata(handle, inode, bh); 1368 if (err) 1369 goto out; 1370 1371 /* Update top-level index: num,max,pointer */ 1372 neh = ext_inode_hdr(inode); 1373 neh->eh_entries = cpu_to_le16(1); 1374 ext4_idx_store_pblock(EXT_FIRST_INDEX(neh), newblock); 1375 if (neh->eh_depth == 0) { 1376 /* Root extent block becomes index block */ 1377 neh->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode, 0)); 1378 EXT_FIRST_INDEX(neh)->ei_block = 1379 EXT_FIRST_EXTENT(neh)->ee_block; 1380 } 1381 ext_debug(inode, "new root: num %d(%d), lblock %d, ptr %llu\n", 1382 le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max), 1383 le32_to_cpu(EXT_FIRST_INDEX(neh)->ei_block), 1384 ext4_idx_pblock(EXT_FIRST_INDEX(neh))); 1385 1386 le16_add_cpu(&neh->eh_depth, 1); 1387 err = ext4_mark_inode_dirty(handle, inode); 1388 out: 1389 brelse(bh); 1390 1391 return err; 1392 } 1393 1394 /* 1395 * ext4_ext_create_new_leaf: 1396 * finds empty index and adds new leaf. 1397 * if no free index is found, then it requests in-depth growing. 1398 */ 1399 static struct ext4_ext_path * 1400 ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode, 1401 unsigned int mb_flags, unsigned int gb_flags, 1402 struct ext4_ext_path *path, 1403 struct ext4_extent *newext) 1404 { 1405 struct ext4_ext_path *curp; 1406 int depth, i, err = 0; 1407 ext4_lblk_t ee_block = le32_to_cpu(newext->ee_block); 1408 1409 repeat: 1410 i = depth = ext_depth(inode); 1411 1412 /* walk up to the tree and look for free index entry */ 1413 curp = path + depth; 1414 while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) { 1415 i--; 1416 curp--; 1417 } 1418 1419 /* we use already allocated block for index block, 1420 * so subsequent data blocks should be contiguous */ 1421 if (EXT_HAS_FREE_INDEX(curp)) { 1422 /* if we found index with free entry, then use that 1423 * entry: create all needed subtree and add new leaf */ 1424 err = ext4_ext_split(handle, inode, mb_flags, path, newext, i); 1425 if (err) 1426 goto errout; 1427 1428 /* refill path */ 1429 path = ext4_find_extent(inode, ee_block, path, gb_flags); 1430 return path; 1431 } 1432 1433 /* tree is full, time to grow in depth */ 1434 err = ext4_ext_grow_indepth(handle, inode, mb_flags); 1435 if (err) 1436 goto errout; 1437 1438 /* refill path */ 1439 path = ext4_find_extent(inode, ee_block, path, gb_flags); 1440 if (IS_ERR(path)) 1441 return path; 1442 1443 /* 1444 * only first (depth 0 -> 1) produces free space; 1445 * in all other cases we have to split the grown tree 1446 */ 1447 depth = ext_depth(inode); 1448 if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) { 1449 /* now we need to split */ 1450 goto repeat; 1451 } 1452 1453 return path; 1454 1455 errout: 1456 ext4_free_ext_path(path); 1457 return ERR_PTR(err); 1458 } 1459 1460 /* 1461 * search the closest allocated block to the left for *logical 1462 * and returns it at @logical + it's physical address at @phys 1463 * if *logical is the smallest allocated block, the function 1464 * returns 0 at @phys 1465 * return value contains 0 (success) or error code 1466 */ 1467 static int ext4_ext_search_left(struct inode *inode, 1468 struct ext4_ext_path *path, 1469 ext4_lblk_t *logical, ext4_fsblk_t *phys) 1470 { 1471 struct ext4_extent_idx *ix; 1472 struct ext4_extent *ex; 1473 int depth, ee_len; 1474 1475 if (unlikely(path == NULL)) { 1476 EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical); 1477 return -EFSCORRUPTED; 1478 } 1479 depth = path->p_depth; 1480 *phys = 0; 1481 1482 if (depth == 0 && path->p_ext == NULL) 1483 return 0; 1484 1485 /* usually extent in the path covers blocks smaller 1486 * then *logical, but it can be that extent is the 1487 * first one in the file */ 1488 1489 ex = path[depth].p_ext; 1490 ee_len = ext4_ext_get_actual_len(ex); 1491 if (*logical < le32_to_cpu(ex->ee_block)) { 1492 if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) { 1493 EXT4_ERROR_INODE(inode, 1494 "EXT_FIRST_EXTENT != ex *logical %d ee_block %d!", 1495 *logical, le32_to_cpu(ex->ee_block)); 1496 return -EFSCORRUPTED; 1497 } 1498 while (--depth >= 0) { 1499 ix = path[depth].p_idx; 1500 if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) { 1501 EXT4_ERROR_INODE(inode, 1502 "ix (%d) != EXT_FIRST_INDEX (%d) (depth %d)!", 1503 ix != NULL ? le32_to_cpu(ix->ei_block) : 0, 1504 le32_to_cpu(EXT_FIRST_INDEX(path[depth].p_hdr)->ei_block), 1505 depth); 1506 return -EFSCORRUPTED; 1507 } 1508 } 1509 return 0; 1510 } 1511 1512 if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) { 1513 EXT4_ERROR_INODE(inode, 1514 "logical %d < ee_block %d + ee_len %d!", 1515 *logical, le32_to_cpu(ex->ee_block), ee_len); 1516 return -EFSCORRUPTED; 1517 } 1518 1519 *logical = le32_to_cpu(ex->ee_block) + ee_len - 1; 1520 *phys = ext4_ext_pblock(ex) + ee_len - 1; 1521 return 0; 1522 } 1523 1524 /* 1525 * Search the closest allocated block to the right for *logical 1526 * and returns it at @logical + it's physical address at @phys. 1527 * If not exists, return 0 and @phys is set to 0. We will return 1528 * 1 which means we found an allocated block and ret_ex is valid. 1529 * Or return a (< 0) error code. 1530 */ 1531 static int ext4_ext_search_right(struct inode *inode, 1532 struct ext4_ext_path *path, 1533 ext4_lblk_t *logical, ext4_fsblk_t *phys, 1534 struct ext4_extent *ret_ex, int flags) 1535 { 1536 struct buffer_head *bh = NULL; 1537 struct ext4_extent_header *eh; 1538 struct ext4_extent_idx *ix; 1539 struct ext4_extent *ex; 1540 int depth; /* Note, NOT eh_depth; depth from top of tree */ 1541 int ee_len; 1542 1543 if (unlikely(path == NULL)) { 1544 EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical); 1545 return -EFSCORRUPTED; 1546 } 1547 depth = path->p_depth; 1548 *phys = 0; 1549 1550 if (depth == 0 && path->p_ext == NULL) 1551 return 0; 1552 1553 /* usually extent in the path covers blocks smaller 1554 * then *logical, but it can be that extent is the 1555 * first one in the file */ 1556 1557 ex = path[depth].p_ext; 1558 ee_len = ext4_ext_get_actual_len(ex); 1559 if (*logical < le32_to_cpu(ex->ee_block)) { 1560 if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) { 1561 EXT4_ERROR_INODE(inode, 1562 "first_extent(path[%d].p_hdr) != ex", 1563 depth); 1564 return -EFSCORRUPTED; 1565 } 1566 while (--depth >= 0) { 1567 ix = path[depth].p_idx; 1568 if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) { 1569 EXT4_ERROR_INODE(inode, 1570 "ix != EXT_FIRST_INDEX *logical %d!", 1571 *logical); 1572 return -EFSCORRUPTED; 1573 } 1574 } 1575 goto found_extent; 1576 } 1577 1578 if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) { 1579 EXT4_ERROR_INODE(inode, 1580 "logical %d < ee_block %d + ee_len %d!", 1581 *logical, le32_to_cpu(ex->ee_block), ee_len); 1582 return -EFSCORRUPTED; 1583 } 1584 1585 if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) { 1586 /* next allocated block in this leaf */ 1587 ex++; 1588 goto found_extent; 1589 } 1590 1591 /* go up and search for index to the right */ 1592 while (--depth >= 0) { 1593 ix = path[depth].p_idx; 1594 if (ix != EXT_LAST_INDEX(path[depth].p_hdr)) 1595 goto got_index; 1596 } 1597 1598 /* we've gone up to the root and found no index to the right */ 1599 return 0; 1600 1601 got_index: 1602 /* we've found index to the right, let's 1603 * follow it and find the closest allocated 1604 * block to the right */ 1605 ix++; 1606 while (++depth < path->p_depth) { 1607 /* subtract from p_depth to get proper eh_depth */ 1608 bh = read_extent_tree_block(inode, ix, path->p_depth - depth, 1609 flags); 1610 if (IS_ERR(bh)) 1611 return PTR_ERR(bh); 1612 eh = ext_block_hdr(bh); 1613 ix = EXT_FIRST_INDEX(eh); 1614 put_bh(bh); 1615 } 1616 1617 bh = read_extent_tree_block(inode, ix, path->p_depth - depth, flags); 1618 if (IS_ERR(bh)) 1619 return PTR_ERR(bh); 1620 eh = ext_block_hdr(bh); 1621 ex = EXT_FIRST_EXTENT(eh); 1622 found_extent: 1623 *logical = le32_to_cpu(ex->ee_block); 1624 *phys = ext4_ext_pblock(ex); 1625 if (ret_ex) 1626 *ret_ex = *ex; 1627 if (bh) 1628 put_bh(bh); 1629 return 1; 1630 } 1631 1632 /* 1633 * ext4_ext_next_allocated_block: 1634 * returns allocated block in subsequent extent or EXT_MAX_BLOCKS. 1635 * NOTE: it considers block number from index entry as 1636 * allocated block. Thus, index entries have to be consistent 1637 * with leaves. 1638 */ 1639 ext4_lblk_t 1640 ext4_ext_next_allocated_block(struct ext4_ext_path *path) 1641 { 1642 int depth; 1643 1644 BUG_ON(path == NULL); 1645 depth = path->p_depth; 1646 1647 if (depth == 0 && path->p_ext == NULL) 1648 return EXT_MAX_BLOCKS; 1649 1650 while (depth >= 0) { 1651 struct ext4_ext_path *p = &path[depth]; 1652 1653 if (depth == path->p_depth) { 1654 /* leaf */ 1655 if (p->p_ext && p->p_ext != EXT_LAST_EXTENT(p->p_hdr)) 1656 return le32_to_cpu(p->p_ext[1].ee_block); 1657 } else { 1658 /* index */ 1659 if (p->p_idx != EXT_LAST_INDEX(p->p_hdr)) 1660 return le32_to_cpu(p->p_idx[1].ei_block); 1661 } 1662 depth--; 1663 } 1664 1665 return EXT_MAX_BLOCKS; 1666 } 1667 1668 /* 1669 * ext4_ext_next_leaf_block: 1670 * returns first allocated block from next leaf or EXT_MAX_BLOCKS 1671 */ 1672 static ext4_lblk_t ext4_ext_next_leaf_block(struct ext4_ext_path *path) 1673 { 1674 int depth; 1675 1676 BUG_ON(path == NULL); 1677 depth = path->p_depth; 1678 1679 /* zero-tree has no leaf blocks at all */ 1680 if (depth == 0) 1681 return EXT_MAX_BLOCKS; 1682 1683 /* go to index block */ 1684 depth--; 1685 1686 while (depth >= 0) { 1687 if (path[depth].p_idx != 1688 EXT_LAST_INDEX(path[depth].p_hdr)) 1689 return (ext4_lblk_t) 1690 le32_to_cpu(path[depth].p_idx[1].ei_block); 1691 depth--; 1692 } 1693 1694 return EXT_MAX_BLOCKS; 1695 } 1696 1697 /* 1698 * ext4_ext_correct_indexes: 1699 * if leaf gets modified and modified extent is first in the leaf, 1700 * then we have to correct all indexes above. 1701 * TODO: do we need to correct tree in all cases? 1702 */ 1703 static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode, 1704 struct ext4_ext_path *path) 1705 { 1706 struct ext4_extent_header *eh; 1707 int depth = ext_depth(inode); 1708 struct ext4_extent *ex; 1709 __le32 border; 1710 int k, err = 0; 1711 1712 eh = path[depth].p_hdr; 1713 ex = path[depth].p_ext; 1714 1715 if (unlikely(ex == NULL || eh == NULL)) { 1716 EXT4_ERROR_INODE(inode, 1717 "ex %p == NULL or eh %p == NULL", ex, eh); 1718 return -EFSCORRUPTED; 1719 } 1720 1721 if (depth == 0) { 1722 /* there is no tree at all */ 1723 return 0; 1724 } 1725 1726 if (ex != EXT_FIRST_EXTENT(eh)) { 1727 /* we correct tree if first leaf got modified only */ 1728 return 0; 1729 } 1730 1731 /* 1732 * TODO: we need correction if border is smaller than current one 1733 */ 1734 k = depth - 1; 1735 border = path[depth].p_ext->ee_block; 1736 err = ext4_ext_get_access(handle, inode, path + k); 1737 if (err) 1738 return err; 1739 if (unlikely(path[k].p_idx > EXT_LAST_INDEX(path[k].p_hdr))) { 1740 EXT4_ERROR_INODE(inode, 1741 "path[%d].p_idx %p > EXT_LAST_INDEX %p", 1742 k, path[k].p_idx, 1743 EXT_LAST_INDEX(path[k].p_hdr)); 1744 return -EFSCORRUPTED; 1745 } 1746 path[k].p_idx->ei_block = border; 1747 err = ext4_ext_dirty(handle, inode, path + k); 1748 if (err) 1749 return err; 1750 1751 while (k--) { 1752 /* change all left-side indexes */ 1753 if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr)) 1754 break; 1755 err = ext4_ext_get_access(handle, inode, path + k); 1756 if (err) 1757 goto clean; 1758 if (unlikely(path[k].p_idx > EXT_LAST_INDEX(path[k].p_hdr))) { 1759 EXT4_ERROR_INODE(inode, 1760 "path[%d].p_idx %p > EXT_LAST_INDEX %p", 1761 k, path[k].p_idx, 1762 EXT_LAST_INDEX(path[k].p_hdr)); 1763 err = -EFSCORRUPTED; 1764 goto clean; 1765 } 1766 path[k].p_idx->ei_block = border; 1767 err = ext4_ext_dirty(handle, inode, path + k); 1768 if (err) 1769 goto clean; 1770 } 1771 return 0; 1772 1773 clean: 1774 /* 1775 * The path[k].p_bh is either unmodified or with no verified bit 1776 * set (see ext4_ext_get_access()). So just clear the verified bit 1777 * of the successfully modified extents buffers, which will force 1778 * these extents to be checked to avoid using inconsistent data. 1779 */ 1780 while (++k < depth) 1781 clear_buffer_verified(path[k].p_bh); 1782 1783 return err; 1784 } 1785 1786 static int ext4_can_extents_be_merged(struct inode *inode, 1787 struct ext4_extent *ex1, 1788 struct ext4_extent *ex2) 1789 { 1790 unsigned short ext1_ee_len, ext2_ee_len; 1791 1792 if (ext4_ext_is_unwritten(ex1) != ext4_ext_is_unwritten(ex2)) 1793 return 0; 1794 1795 ext1_ee_len = ext4_ext_get_actual_len(ex1); 1796 ext2_ee_len = ext4_ext_get_actual_len(ex2); 1797 1798 if (le32_to_cpu(ex1->ee_block) + ext1_ee_len != 1799 le32_to_cpu(ex2->ee_block)) 1800 return 0; 1801 1802 if (ext1_ee_len + ext2_ee_len > EXT_INIT_MAX_LEN) 1803 return 0; 1804 1805 if (ext4_ext_is_unwritten(ex1) && 1806 ext1_ee_len + ext2_ee_len > EXT_UNWRITTEN_MAX_LEN) 1807 return 0; 1808 #ifdef AGGRESSIVE_TEST 1809 if (ext1_ee_len >= 4) 1810 return 0; 1811 #endif 1812 1813 if (ext4_ext_pblock(ex1) + ext1_ee_len == ext4_ext_pblock(ex2)) 1814 return 1; 1815 return 0; 1816 } 1817 1818 /* 1819 * This function tries to merge the "ex" extent to the next extent in the tree. 1820 * It always tries to merge towards right. If you want to merge towards 1821 * left, pass "ex - 1" as argument instead of "ex". 1822 * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns 1823 * 1 if they got merged. 1824 */ 1825 static int ext4_ext_try_to_merge_right(struct inode *inode, 1826 struct ext4_ext_path *path, 1827 struct ext4_extent *ex) 1828 { 1829 struct ext4_extent_header *eh; 1830 unsigned int depth, len; 1831 int merge_done = 0, unwritten; 1832 1833 depth = ext_depth(inode); 1834 BUG_ON(path[depth].p_hdr == NULL); 1835 eh = path[depth].p_hdr; 1836 1837 while (ex < EXT_LAST_EXTENT(eh)) { 1838 if (!ext4_can_extents_be_merged(inode, ex, ex + 1)) 1839 break; 1840 /* merge with next extent! */ 1841 unwritten = ext4_ext_is_unwritten(ex); 1842 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex) 1843 + ext4_ext_get_actual_len(ex + 1)); 1844 if (unwritten) 1845 ext4_ext_mark_unwritten(ex); 1846 1847 if (ex + 1 < EXT_LAST_EXTENT(eh)) { 1848 len = (EXT_LAST_EXTENT(eh) - ex - 1) 1849 * sizeof(struct ext4_extent); 1850 memmove(ex + 1, ex + 2, len); 1851 } 1852 le16_add_cpu(&eh->eh_entries, -1); 1853 merge_done = 1; 1854 WARN_ON(eh->eh_entries == 0); 1855 if (!eh->eh_entries) 1856 EXT4_ERROR_INODE(inode, "eh->eh_entries = 0!"); 1857 } 1858 1859 return merge_done; 1860 } 1861 1862 /* 1863 * This function does a very simple check to see if we can collapse 1864 * an extent tree with a single extent tree leaf block into the inode. 1865 */ 1866 static void ext4_ext_try_to_merge_up(handle_t *handle, 1867 struct inode *inode, 1868 struct ext4_ext_path *path) 1869 { 1870 size_t s; 1871 unsigned max_root = ext4_ext_space_root(inode, 0); 1872 ext4_fsblk_t blk; 1873 1874 if ((path[0].p_depth != 1) || 1875 (le16_to_cpu(path[0].p_hdr->eh_entries) != 1) || 1876 (le16_to_cpu(path[1].p_hdr->eh_entries) > max_root)) 1877 return; 1878 1879 /* 1880 * We need to modify the block allocation bitmap and the block 1881 * group descriptor to release the extent tree block. If we 1882 * can't get the journal credits, give up. 1883 */ 1884 if (ext4_journal_extend(handle, 2, 1885 ext4_free_metadata_revoke_credits(inode->i_sb, 1))) 1886 return; 1887 1888 /* 1889 * Copy the extent data up to the inode 1890 */ 1891 blk = ext4_idx_pblock(path[0].p_idx); 1892 s = le16_to_cpu(path[1].p_hdr->eh_entries) * 1893 sizeof(struct ext4_extent_idx); 1894 s += sizeof(struct ext4_extent_header); 1895 1896 path[1].p_maxdepth = path[0].p_maxdepth; 1897 memcpy(path[0].p_hdr, path[1].p_hdr, s); 1898 path[0].p_depth = 0; 1899 path[0].p_ext = EXT_FIRST_EXTENT(path[0].p_hdr) + 1900 (path[1].p_ext - EXT_FIRST_EXTENT(path[1].p_hdr)); 1901 path[0].p_hdr->eh_max = cpu_to_le16(max_root); 1902 1903 ext4_ext_path_brelse(path + 1); 1904 ext4_free_blocks(handle, inode, NULL, blk, 1, 1905 EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET); 1906 } 1907 1908 /* 1909 * This function tries to merge the @ex extent to neighbours in the tree, then 1910 * tries to collapse the extent tree into the inode. 1911 */ 1912 static void ext4_ext_try_to_merge(handle_t *handle, 1913 struct inode *inode, 1914 struct ext4_ext_path *path, 1915 struct ext4_extent *ex) 1916 { 1917 struct ext4_extent_header *eh; 1918 unsigned int depth; 1919 int merge_done = 0; 1920 1921 depth = ext_depth(inode); 1922 BUG_ON(path[depth].p_hdr == NULL); 1923 eh = path[depth].p_hdr; 1924 1925 if (ex > EXT_FIRST_EXTENT(eh)) 1926 merge_done = ext4_ext_try_to_merge_right(inode, path, ex - 1); 1927 1928 if (!merge_done) 1929 (void) ext4_ext_try_to_merge_right(inode, path, ex); 1930 1931 ext4_ext_try_to_merge_up(handle, inode, path); 1932 } 1933 1934 /* 1935 * check if a portion of the "newext" extent overlaps with an 1936 * existing extent. 1937 * 1938 * If there is an overlap discovered, it updates the length of the newext 1939 * such that there will be no overlap, and then returns 1. 1940 * If there is no overlap found, it returns 0. 1941 */ 1942 static unsigned int ext4_ext_check_overlap(struct ext4_sb_info *sbi, 1943 struct inode *inode, 1944 struct ext4_extent *newext, 1945 struct ext4_ext_path *path) 1946 { 1947 ext4_lblk_t b1, b2; 1948 unsigned int depth, len1; 1949 unsigned int ret = 0; 1950 1951 b1 = le32_to_cpu(newext->ee_block); 1952 len1 = ext4_ext_get_actual_len(newext); 1953 depth = ext_depth(inode); 1954 if (!path[depth].p_ext) 1955 goto out; 1956 b2 = EXT4_LBLK_CMASK(sbi, le32_to_cpu(path[depth].p_ext->ee_block)); 1957 1958 /* 1959 * get the next allocated block if the extent in the path 1960 * is before the requested block(s) 1961 */ 1962 if (b2 < b1) { 1963 b2 = ext4_ext_next_allocated_block(path); 1964 if (b2 == EXT_MAX_BLOCKS) 1965 goto out; 1966 b2 = EXT4_LBLK_CMASK(sbi, b2); 1967 } 1968 1969 /* check for wrap through zero on extent logical start block*/ 1970 if (b1 + len1 < b1) { 1971 len1 = EXT_MAX_BLOCKS - b1; 1972 newext->ee_len = cpu_to_le16(len1); 1973 ret = 1; 1974 } 1975 1976 /* check for overlap */ 1977 if (b1 + len1 > b2) { 1978 newext->ee_len = cpu_to_le16(b2 - b1); 1979 ret = 1; 1980 } 1981 out: 1982 return ret; 1983 } 1984 1985 /* 1986 * ext4_ext_insert_extent: 1987 * tries to merge requested extent into the existing extent or 1988 * inserts requested extent as new one into the tree, 1989 * creating new leaf in the no-space case. 1990 */ 1991 struct ext4_ext_path * 1992 ext4_ext_insert_extent(handle_t *handle, struct inode *inode, 1993 struct ext4_ext_path *path, 1994 struct ext4_extent *newext, int gb_flags) 1995 { 1996 struct ext4_extent_header *eh; 1997 struct ext4_extent *ex, *fex; 1998 struct ext4_extent *nearex; /* nearest extent */ 1999 int depth, len, err = 0; 2000 ext4_lblk_t next; 2001 int mb_flags = 0, unwritten; 2002 2003 KUNIT_STATIC_STUB_REDIRECT(ext4_ext_insert_extent, handle, inode, path, 2004 newext, gb_flags); 2005 2006 if (gb_flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) 2007 mb_flags |= EXT4_MB_DELALLOC_RESERVED; 2008 if (unlikely(ext4_ext_get_actual_len(newext) == 0)) { 2009 EXT4_ERROR_INODE(inode, "ext4_ext_get_actual_len(newext) == 0"); 2010 err = -EFSCORRUPTED; 2011 goto errout; 2012 } 2013 depth = ext_depth(inode); 2014 ex = path[depth].p_ext; 2015 eh = path[depth].p_hdr; 2016 if (unlikely(path[depth].p_hdr == NULL)) { 2017 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth); 2018 err = -EFSCORRUPTED; 2019 goto errout; 2020 } 2021 2022 /* try to insert block into found extent and return */ 2023 if (ex && !(gb_flags & EXT4_GET_BLOCKS_SPLIT_NOMERGE)) { 2024 2025 /* 2026 * Try to see whether we should rather test the extent on 2027 * right from ex, or from the left of ex. This is because 2028 * ext4_find_extent() can return either extent on the 2029 * left, or on the right from the searched position. This 2030 * will make merging more effective. 2031 */ 2032 if (ex < EXT_LAST_EXTENT(eh) && 2033 (le32_to_cpu(ex->ee_block) + 2034 ext4_ext_get_actual_len(ex) < 2035 le32_to_cpu(newext->ee_block))) { 2036 ex += 1; 2037 goto prepend; 2038 } else if ((ex > EXT_FIRST_EXTENT(eh)) && 2039 (le32_to_cpu(newext->ee_block) + 2040 ext4_ext_get_actual_len(newext) < 2041 le32_to_cpu(ex->ee_block))) 2042 ex -= 1; 2043 2044 /* Try to append newex to the ex */ 2045 if (ext4_can_extents_be_merged(inode, ex, newext)) { 2046 ext_debug(inode, "append [%d]%d block to %u:[%d]%d" 2047 "(from %llu)\n", 2048 ext4_ext_is_unwritten(newext), 2049 ext4_ext_get_actual_len(newext), 2050 le32_to_cpu(ex->ee_block), 2051 ext4_ext_is_unwritten(ex), 2052 ext4_ext_get_actual_len(ex), 2053 ext4_ext_pblock(ex)); 2054 err = ext4_ext_get_access(handle, inode, 2055 path + depth); 2056 if (err) 2057 goto errout; 2058 unwritten = ext4_ext_is_unwritten(ex); 2059 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex) 2060 + ext4_ext_get_actual_len(newext)); 2061 if (unwritten) 2062 ext4_ext_mark_unwritten(ex); 2063 nearex = ex; 2064 goto merge; 2065 } 2066 2067 prepend: 2068 /* Try to prepend newex to the ex */ 2069 if (ext4_can_extents_be_merged(inode, newext, ex)) { 2070 ext_debug(inode, "prepend %u[%d]%d block to %u:[%d]%d" 2071 "(from %llu)\n", 2072 le32_to_cpu(newext->ee_block), 2073 ext4_ext_is_unwritten(newext), 2074 ext4_ext_get_actual_len(newext), 2075 le32_to_cpu(ex->ee_block), 2076 ext4_ext_is_unwritten(ex), 2077 ext4_ext_get_actual_len(ex), 2078 ext4_ext_pblock(ex)); 2079 err = ext4_ext_get_access(handle, inode, 2080 path + depth); 2081 if (err) 2082 goto errout; 2083 2084 unwritten = ext4_ext_is_unwritten(ex); 2085 ex->ee_block = newext->ee_block; 2086 ext4_ext_store_pblock(ex, ext4_ext_pblock(newext)); 2087 ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex) 2088 + ext4_ext_get_actual_len(newext)); 2089 if (unwritten) 2090 ext4_ext_mark_unwritten(ex); 2091 nearex = ex; 2092 goto merge; 2093 } 2094 } 2095 2096 depth = ext_depth(inode); 2097 eh = path[depth].p_hdr; 2098 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) 2099 goto has_space; 2100 2101 /* probably next leaf has space for us? */ 2102 fex = EXT_LAST_EXTENT(eh); 2103 next = EXT_MAX_BLOCKS; 2104 if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block)) 2105 next = ext4_ext_next_leaf_block(path); 2106 if (next != EXT_MAX_BLOCKS) { 2107 struct ext4_ext_path *npath; 2108 2109 ext_debug(inode, "next leaf block - %u\n", next); 2110 npath = ext4_find_extent(inode, next, NULL, gb_flags); 2111 if (IS_ERR(npath)) { 2112 err = PTR_ERR(npath); 2113 goto errout; 2114 } 2115 BUG_ON(npath->p_depth != path->p_depth); 2116 eh = npath[depth].p_hdr; 2117 if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) { 2118 ext_debug(inode, "next leaf isn't full(%d)\n", 2119 le16_to_cpu(eh->eh_entries)); 2120 ext4_free_ext_path(path); 2121 path = npath; 2122 goto has_space; 2123 } 2124 ext_debug(inode, "next leaf has no free space(%d,%d)\n", 2125 le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max)); 2126 ext4_free_ext_path(npath); 2127 } 2128 2129 /* 2130 * There is no free space in the found leaf. 2131 * We're gonna add a new leaf in the tree. 2132 */ 2133 if (gb_flags & EXT4_GET_BLOCKS_METADATA_NOFAIL) 2134 mb_flags |= EXT4_MB_USE_RESERVED; 2135 path = ext4_ext_create_new_leaf(handle, inode, mb_flags, gb_flags, 2136 path, newext); 2137 if (IS_ERR(path)) 2138 return path; 2139 depth = ext_depth(inode); 2140 eh = path[depth].p_hdr; 2141 2142 has_space: 2143 nearex = path[depth].p_ext; 2144 2145 err = ext4_ext_get_access(handle, inode, path + depth); 2146 if (err) 2147 goto errout; 2148 2149 if (!nearex) { 2150 /* there is no extent in this leaf, create first one */ 2151 ext_debug(inode, "first extent in the leaf: %u:%llu:[%d]%d\n", 2152 le32_to_cpu(newext->ee_block), 2153 ext4_ext_pblock(newext), 2154 ext4_ext_is_unwritten(newext), 2155 ext4_ext_get_actual_len(newext)); 2156 nearex = EXT_FIRST_EXTENT(eh); 2157 } else { 2158 if (le32_to_cpu(newext->ee_block) 2159 > le32_to_cpu(nearex->ee_block)) { 2160 /* Insert after */ 2161 ext_debug(inode, "insert %u:%llu:[%d]%d before: " 2162 "nearest %p\n", 2163 le32_to_cpu(newext->ee_block), 2164 ext4_ext_pblock(newext), 2165 ext4_ext_is_unwritten(newext), 2166 ext4_ext_get_actual_len(newext), 2167 nearex); 2168 nearex++; 2169 } else { 2170 /* Insert before */ 2171 BUG_ON(newext->ee_block == nearex->ee_block); 2172 ext_debug(inode, "insert %u:%llu:[%d]%d after: " 2173 "nearest %p\n", 2174 le32_to_cpu(newext->ee_block), 2175 ext4_ext_pblock(newext), 2176 ext4_ext_is_unwritten(newext), 2177 ext4_ext_get_actual_len(newext), 2178 nearex); 2179 } 2180 len = EXT_LAST_EXTENT(eh) - nearex + 1; 2181 if (len > 0) { 2182 ext_debug(inode, "insert %u:%llu:[%d]%d: " 2183 "move %d extents from 0x%p to 0x%p\n", 2184 le32_to_cpu(newext->ee_block), 2185 ext4_ext_pblock(newext), 2186 ext4_ext_is_unwritten(newext), 2187 ext4_ext_get_actual_len(newext), 2188 len, nearex, nearex + 1); 2189 memmove(nearex + 1, nearex, 2190 len * sizeof(struct ext4_extent)); 2191 } 2192 } 2193 2194 le16_add_cpu(&eh->eh_entries, 1); 2195 path[depth].p_ext = nearex; 2196 nearex->ee_block = newext->ee_block; 2197 ext4_ext_store_pblock(nearex, ext4_ext_pblock(newext)); 2198 nearex->ee_len = newext->ee_len; 2199 2200 merge: 2201 /* try to merge extents */ 2202 if (!(gb_flags & EXT4_GET_BLOCKS_SPLIT_NOMERGE)) 2203 ext4_ext_try_to_merge(handle, inode, path, nearex); 2204 2205 /* time to correct all indexes above */ 2206 err = ext4_ext_correct_indexes(handle, inode, path); 2207 if (err) 2208 goto errout; 2209 2210 err = ext4_ext_dirty(handle, inode, path + path->p_depth); 2211 if (err) 2212 goto errout; 2213 2214 return path; 2215 2216 errout: 2217 ext4_free_ext_path(path); 2218 return ERR_PTR(err); 2219 } 2220 2221 static int ext4_fill_es_cache_info(struct inode *inode, 2222 ext4_lblk_t block, ext4_lblk_t num, 2223 struct fiemap_extent_info *fieinfo) 2224 { 2225 ext4_lblk_t next, end = block + num - 1; 2226 struct extent_status es; 2227 unsigned char blksize_bits = inode->i_sb->s_blocksize_bits; 2228 unsigned int flags; 2229 int err; 2230 2231 while (block <= end) { 2232 next = 0; 2233 flags = 0; 2234 if (!ext4_es_lookup_extent(inode, block, &next, &es, NULL)) 2235 break; 2236 if (ext4_es_is_unwritten(&es)) 2237 flags |= FIEMAP_EXTENT_UNWRITTEN; 2238 if (ext4_es_is_delayed(&es)) 2239 flags |= (FIEMAP_EXTENT_DELALLOC | 2240 FIEMAP_EXTENT_UNKNOWN); 2241 if (ext4_es_is_hole(&es)) 2242 flags |= EXT4_FIEMAP_EXTENT_HOLE; 2243 if (next == 0) 2244 flags |= FIEMAP_EXTENT_LAST; 2245 if (flags & (FIEMAP_EXTENT_DELALLOC| 2246 EXT4_FIEMAP_EXTENT_HOLE)) 2247 es.es_pblk = 0; 2248 else 2249 es.es_pblk = ext4_es_pblock(&es); 2250 err = fiemap_fill_next_extent(fieinfo, 2251 (__u64)es.es_lblk << blksize_bits, 2252 (__u64)es.es_pblk << blksize_bits, 2253 (__u64)es.es_len << blksize_bits, 2254 flags); 2255 if (next == 0) 2256 break; 2257 block = next; 2258 if (err < 0) 2259 return err; 2260 if (err == 1) 2261 return 0; 2262 } 2263 return 0; 2264 } 2265 2266 2267 /* 2268 * ext4_ext_find_hole - find hole around given block according to the given path 2269 * @inode: inode we lookup in 2270 * @path: path in extent tree to @lblk 2271 * @lblk: pointer to logical block around which we want to determine hole 2272 * 2273 * Determine hole length (and start if easily possible) around given logical 2274 * block. We don't try too hard to find the beginning of the hole but @path 2275 * actually points to extent before @lblk, we provide it. 2276 * 2277 * The function returns the length of a hole starting at @lblk. We update @lblk 2278 * to the beginning of the hole if we managed to find it. 2279 */ 2280 static ext4_lblk_t ext4_ext_find_hole(struct inode *inode, 2281 struct ext4_ext_path *path, 2282 ext4_lblk_t *lblk) 2283 { 2284 int depth = ext_depth(inode); 2285 struct ext4_extent *ex; 2286 ext4_lblk_t len; 2287 2288 ex = path[depth].p_ext; 2289 if (ex == NULL) { 2290 /* there is no extent yet, so gap is [0;-] */ 2291 *lblk = 0; 2292 len = EXT_MAX_BLOCKS; 2293 } else if (*lblk < le32_to_cpu(ex->ee_block)) { 2294 len = le32_to_cpu(ex->ee_block) - *lblk; 2295 } else if (*lblk >= le32_to_cpu(ex->ee_block) 2296 + ext4_ext_get_actual_len(ex)) { 2297 ext4_lblk_t next; 2298 2299 *lblk = le32_to_cpu(ex->ee_block) + ext4_ext_get_actual_len(ex); 2300 next = ext4_ext_next_allocated_block(path); 2301 BUG_ON(next == *lblk); 2302 len = next - *lblk; 2303 } else { 2304 BUG(); 2305 } 2306 return len; 2307 } 2308 2309 /* 2310 * ext4_ext_rm_idx: 2311 * removes index from the index block. 2312 */ 2313 static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode, 2314 struct ext4_ext_path *path, int depth) 2315 { 2316 int err; 2317 ext4_fsblk_t leaf; 2318 int k = depth - 1; 2319 2320 /* free index block */ 2321 leaf = ext4_idx_pblock(path[k].p_idx); 2322 if (unlikely(path[k].p_hdr->eh_entries == 0)) { 2323 EXT4_ERROR_INODE(inode, "path[%d].p_hdr->eh_entries == 0", k); 2324 return -EFSCORRUPTED; 2325 } 2326 err = ext4_ext_get_access(handle, inode, path + k); 2327 if (err) 2328 return err; 2329 2330 if (path[k].p_idx != EXT_LAST_INDEX(path[k].p_hdr)) { 2331 int len = EXT_LAST_INDEX(path[k].p_hdr) - path[k].p_idx; 2332 len *= sizeof(struct ext4_extent_idx); 2333 memmove(path[k].p_idx, path[k].p_idx + 1, len); 2334 } 2335 2336 le16_add_cpu(&path[k].p_hdr->eh_entries, -1); 2337 err = ext4_ext_dirty(handle, inode, path + k); 2338 if (err) 2339 return err; 2340 ext_debug(inode, "index is empty, remove it, free block %llu\n", leaf); 2341 trace_ext4_ext_rm_idx(inode, leaf); 2342 2343 ext4_free_blocks(handle, inode, NULL, leaf, 1, 2344 EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET); 2345 2346 while (--k >= 0) { 2347 if (path[k + 1].p_idx != EXT_FIRST_INDEX(path[k + 1].p_hdr)) 2348 break; 2349 err = ext4_ext_get_access(handle, inode, path + k); 2350 if (err) 2351 goto clean; 2352 path[k].p_idx->ei_block = path[k + 1].p_idx->ei_block; 2353 err = ext4_ext_dirty(handle, inode, path + k); 2354 if (err) 2355 goto clean; 2356 } 2357 return 0; 2358 2359 clean: 2360 /* 2361 * The path[k].p_bh is either unmodified or with no verified bit 2362 * set (see ext4_ext_get_access()). So just clear the verified bit 2363 * of the successfully modified extents buffers, which will force 2364 * these extents to be checked to avoid using inconsistent data. 2365 */ 2366 while (++k < depth) 2367 clear_buffer_verified(path[k].p_bh); 2368 2369 return err; 2370 } 2371 2372 /* 2373 * ext4_ext_calc_credits_for_single_extent: 2374 * This routine returns max. credits that needed to insert an extent 2375 * to the extent tree. 2376 * When pass the actual path, the caller should calculate credits 2377 * under i_data_sem. 2378 */ 2379 int ext4_ext_calc_credits_for_single_extent(struct inode *inode, int nrblocks, 2380 struct ext4_ext_path *path) 2381 { 2382 if (path) { 2383 int depth = ext_depth(inode); 2384 int ret = 0; 2385 2386 /* probably there is space in leaf? */ 2387 if (le16_to_cpu(path[depth].p_hdr->eh_entries) 2388 < le16_to_cpu(path[depth].p_hdr->eh_max)) { 2389 2390 /* 2391 * There are some space in the leaf tree, no 2392 * need to account for leaf block credit 2393 * 2394 * bitmaps and block group descriptor blocks 2395 * and other metadata blocks still need to be 2396 * accounted. 2397 */ 2398 /* 1 bitmap, 1 block group descriptor */ 2399 ret = 2 + EXT4_META_TRANS_BLOCKS(inode->i_sb); 2400 return ret; 2401 } 2402 } 2403 2404 return ext4_chunk_trans_blocks(inode, nrblocks); 2405 } 2406 2407 /* 2408 * How many index/leaf blocks need to change/allocate to add @extents extents? 2409 * 2410 * If we add a single extent, then in the worse case, each tree level 2411 * index/leaf need to be changed in case of the tree split. 2412 * 2413 * If more extents are inserted, they could cause the whole tree split more 2414 * than once, but this is really rare. 2415 */ 2416 int ext4_ext_index_trans_blocks(struct inode *inode, int extents) 2417 { 2418 int index; 2419 2420 /* If we are converting the inline data, only one is needed here. */ 2421 if (ext4_has_inline_data(inode)) 2422 return 1; 2423 2424 /* 2425 * Extent tree can change between the time we estimate credits and 2426 * the time we actually modify the tree. Assume the worst case. 2427 */ 2428 if (extents <= 1) 2429 index = (EXT4_MAX_EXTENT_DEPTH * 2) + extents; 2430 else { 2431 int ext_max = ext4_ext_space_block(inode, 0); 2432 2433 index = EXT4_MAX_EXTENT_DEPTH * 3; 2434 /* 2435 * Modified extents need not start at the beginning of the 2436 * leaf. Already two extents may need two leaf block 2437 * modifications... 2438 */ 2439 index += DIV_ROUND_UP(extents + ext_max - 1, ext_max); 2440 } 2441 2442 return index; 2443 } 2444 2445 static inline int get_default_free_blocks_flags(struct inode *inode) 2446 { 2447 if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode) || 2448 ext4_test_inode_flag(inode, EXT4_INODE_EA_INODE)) 2449 return EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET; 2450 else if (ext4_should_journal_data(inode)) 2451 return EXT4_FREE_BLOCKS_FORGET; 2452 return 0; 2453 } 2454 2455 /* 2456 * ext4_rereserve_cluster - increment the reserved cluster count when 2457 * freeing a cluster with a pending reservation 2458 * 2459 * @inode - file containing the cluster 2460 * @lblk - logical block in cluster to be reserved 2461 * 2462 * Increments the reserved cluster count and adjusts quota in a bigalloc 2463 * file system when freeing a partial cluster containing at least one 2464 * delayed and unwritten block. A partial cluster meeting that 2465 * requirement will have a pending reservation. If so, the 2466 * RERESERVE_CLUSTER flag is used when calling ext4_free_blocks() to 2467 * defer reserved and allocated space accounting to a subsequent call 2468 * to this function. 2469 */ 2470 static void ext4_rereserve_cluster(struct inode *inode, ext4_lblk_t lblk) 2471 { 2472 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 2473 struct ext4_inode_info *ei = EXT4_I(inode); 2474 2475 dquot_reclaim_block(inode, EXT4_C2B(sbi, 1)); 2476 2477 spin_lock(&ei->i_block_reservation_lock); 2478 ei->i_reserved_data_blocks++; 2479 percpu_counter_add(&sbi->s_dirtyclusters_counter, 1); 2480 spin_unlock(&ei->i_block_reservation_lock); 2481 2482 percpu_counter_add(&sbi->s_freeclusters_counter, 1); 2483 ext4_remove_pending(inode, lblk); 2484 } 2485 2486 static int ext4_remove_blocks(handle_t *handle, struct inode *inode, 2487 struct ext4_extent *ex, 2488 struct partial_cluster *partial, 2489 ext4_lblk_t from, ext4_lblk_t to) 2490 { 2491 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 2492 unsigned short ee_len = ext4_ext_get_actual_len(ex); 2493 ext4_fsblk_t last_pblk, pblk; 2494 ext4_lblk_t num; 2495 int flags; 2496 2497 /* only extent tail removal is allowed */ 2498 if (from < le32_to_cpu(ex->ee_block) || 2499 to != le32_to_cpu(ex->ee_block) + ee_len - 1) { 2500 ext4_error(sbi->s_sb, 2501 "strange request: removal(2) %u-%u from %u:%u", 2502 from, to, le32_to_cpu(ex->ee_block), ee_len); 2503 return 0; 2504 } 2505 2506 #ifdef EXTENTS_STATS 2507 spin_lock(&sbi->s_ext_stats_lock); 2508 sbi->s_ext_blocks += ee_len; 2509 sbi->s_ext_extents++; 2510 if (ee_len < sbi->s_ext_min) 2511 sbi->s_ext_min = ee_len; 2512 if (ee_len > sbi->s_ext_max) 2513 sbi->s_ext_max = ee_len; 2514 if (ext_depth(inode) > sbi->s_depth_max) 2515 sbi->s_depth_max = ext_depth(inode); 2516 spin_unlock(&sbi->s_ext_stats_lock); 2517 #endif 2518 2519 trace_ext4_remove_blocks(inode, ex, from, to, partial); 2520 2521 /* 2522 * if we have a partial cluster, and it's different from the 2523 * cluster of the last block in the extent, we free it 2524 */ 2525 last_pblk = ext4_ext_pblock(ex) + ee_len - 1; 2526 2527 if (partial->state != initial && 2528 partial->pclu != EXT4_B2C(sbi, last_pblk)) { 2529 if (partial->state == tofree) { 2530 flags = get_default_free_blocks_flags(inode); 2531 if (ext4_is_pending(inode, partial->lblk)) 2532 flags |= EXT4_FREE_BLOCKS_RERESERVE_CLUSTER; 2533 ext4_free_blocks(handle, inode, NULL, 2534 EXT4_C2B(sbi, partial->pclu), 2535 sbi->s_cluster_ratio, flags); 2536 if (flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER) 2537 ext4_rereserve_cluster(inode, partial->lblk); 2538 } 2539 partial->state = initial; 2540 } 2541 2542 num = le32_to_cpu(ex->ee_block) + ee_len - from; 2543 pblk = ext4_ext_pblock(ex) + ee_len - num; 2544 2545 /* 2546 * We free the partial cluster at the end of the extent (if any), 2547 * unless the cluster is used by another extent (partial_cluster 2548 * state is nofree). If a partial cluster exists here, it must be 2549 * shared with the last block in the extent. 2550 */ 2551 flags = get_default_free_blocks_flags(inode); 2552 2553 /* partial, left end cluster aligned, right end unaligned */ 2554 if ((EXT4_LBLK_COFF(sbi, to) != sbi->s_cluster_ratio - 1) && 2555 (EXT4_LBLK_CMASK(sbi, to) >= from) && 2556 (partial->state != nofree)) { 2557 if (ext4_is_pending(inode, to)) 2558 flags |= EXT4_FREE_BLOCKS_RERESERVE_CLUSTER; 2559 ext4_free_blocks(handle, inode, NULL, 2560 EXT4_PBLK_CMASK(sbi, last_pblk), 2561 sbi->s_cluster_ratio, flags); 2562 if (flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER) 2563 ext4_rereserve_cluster(inode, to); 2564 partial->state = initial; 2565 flags = get_default_free_blocks_flags(inode); 2566 } 2567 2568 flags |= EXT4_FREE_BLOCKS_NOFREE_LAST_CLUSTER; 2569 2570 /* 2571 * For bigalloc file systems, we never free a partial cluster 2572 * at the beginning of the extent. Instead, we check to see if we 2573 * need to free it on a subsequent call to ext4_remove_blocks, 2574 * or at the end of ext4_ext_rm_leaf or ext4_ext_remove_space. 2575 */ 2576 flags |= EXT4_FREE_BLOCKS_NOFREE_FIRST_CLUSTER; 2577 ext4_free_blocks(handle, inode, NULL, pblk, num, flags); 2578 2579 /* reset the partial cluster if we've freed past it */ 2580 if (partial->state != initial && partial->pclu != EXT4_B2C(sbi, pblk)) 2581 partial->state = initial; 2582 2583 /* 2584 * If we've freed the entire extent but the beginning is not left 2585 * cluster aligned and is not marked as ineligible for freeing we 2586 * record the partial cluster at the beginning of the extent. It 2587 * wasn't freed by the preceding ext4_free_blocks() call, and we 2588 * need to look farther to the left to determine if it's to be freed 2589 * (not shared with another extent). Else, reset the partial 2590 * cluster - we're either done freeing or the beginning of the 2591 * extent is left cluster aligned. 2592 */ 2593 if (EXT4_LBLK_COFF(sbi, from) && num == ee_len) { 2594 if (partial->state == initial) { 2595 partial->pclu = EXT4_B2C(sbi, pblk); 2596 partial->lblk = from; 2597 partial->state = tofree; 2598 } 2599 } else { 2600 partial->state = initial; 2601 } 2602 2603 return 0; 2604 } 2605 2606 /* 2607 * ext4_ext_rm_leaf() Removes the extents associated with the 2608 * blocks appearing between "start" and "end". Both "start" 2609 * and "end" must appear in the same extent or EIO is returned. 2610 * 2611 * @handle: The journal handle 2612 * @inode: The files inode 2613 * @path: The path to the leaf 2614 * @partial_cluster: The cluster which we'll have to free if all extents 2615 * has been released from it. However, if this value is 2616 * negative, it's a cluster just to the right of the 2617 * punched region and it must not be freed. 2618 * @start: The first block to remove 2619 * @end: The last block to remove 2620 */ 2621 static int 2622 ext4_ext_rm_leaf(handle_t *handle, struct inode *inode, 2623 struct ext4_ext_path *path, 2624 struct partial_cluster *partial, 2625 ext4_lblk_t start, ext4_lblk_t end) 2626 { 2627 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 2628 int err = 0, correct_index = 0; 2629 int depth = ext_depth(inode), credits, revoke_credits; 2630 struct ext4_extent_header *eh; 2631 ext4_lblk_t a, b; 2632 unsigned num; 2633 ext4_lblk_t ex_ee_block; 2634 unsigned short ex_ee_len; 2635 unsigned unwritten = 0; 2636 struct ext4_extent *ex; 2637 ext4_fsblk_t pblk; 2638 2639 /* the header must be checked already in ext4_ext_remove_space() */ 2640 ext_debug(inode, "truncate since %u in leaf to %u\n", start, end); 2641 if (!path[depth].p_hdr) 2642 path[depth].p_hdr = ext_block_hdr(path[depth].p_bh); 2643 eh = path[depth].p_hdr; 2644 if (unlikely(path[depth].p_hdr == NULL)) { 2645 EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth); 2646 return -EFSCORRUPTED; 2647 } 2648 /* find where to start removing */ 2649 ex = path[depth].p_ext; 2650 if (!ex) 2651 ex = EXT_LAST_EXTENT(eh); 2652 2653 ex_ee_block = le32_to_cpu(ex->ee_block); 2654 ex_ee_len = ext4_ext_get_actual_len(ex); 2655 2656 trace_ext4_ext_rm_leaf(inode, start, ex, partial); 2657 2658 while (ex >= EXT_FIRST_EXTENT(eh) && 2659 ex_ee_block + ex_ee_len > start) { 2660 2661 if (ext4_ext_is_unwritten(ex)) 2662 unwritten = 1; 2663 else 2664 unwritten = 0; 2665 2666 ext_debug(inode, "remove ext %u:[%d]%d\n", ex_ee_block, 2667 unwritten, ex_ee_len); 2668 path[depth].p_ext = ex; 2669 2670 a = max(ex_ee_block, start); 2671 b = min(ex_ee_block + ex_ee_len - 1, end); 2672 2673 ext_debug(inode, " border %u:%u\n", a, b); 2674 2675 /* If this extent is beyond the end of the hole, skip it */ 2676 if (end < ex_ee_block) { 2677 /* 2678 * We're going to skip this extent and move to another, 2679 * so note that its first cluster is in use to avoid 2680 * freeing it when removing blocks. Eventually, the 2681 * right edge of the truncated/punched region will 2682 * be just to the left. 2683 */ 2684 if (sbi->s_cluster_ratio > 1) { 2685 pblk = ext4_ext_pblock(ex); 2686 partial->pclu = EXT4_B2C(sbi, pblk); 2687 partial->state = nofree; 2688 } 2689 ex--; 2690 ex_ee_block = le32_to_cpu(ex->ee_block); 2691 ex_ee_len = ext4_ext_get_actual_len(ex); 2692 continue; 2693 } else if (b != ex_ee_block + ex_ee_len - 1) { 2694 EXT4_ERROR_INODE(inode, 2695 "can not handle truncate %u:%u " 2696 "on extent %u:%u", 2697 start, end, ex_ee_block, 2698 ex_ee_block + ex_ee_len - 1); 2699 err = -EFSCORRUPTED; 2700 goto out; 2701 } else if (a != ex_ee_block) { 2702 /* remove tail of the extent */ 2703 num = a - ex_ee_block; 2704 } else { 2705 /* remove whole extent: excellent! */ 2706 num = 0; 2707 } 2708 /* 2709 * 3 for leaf, sb, and inode plus 2 (bmap and group 2710 * descriptor) for each block group; assume two block 2711 * groups plus ex_ee_len/blocks_per_block_group for 2712 * the worst case 2713 */ 2714 credits = 7 + 2*(ex_ee_len/EXT4_BLOCKS_PER_GROUP(inode->i_sb)); 2715 if (ex == EXT_FIRST_EXTENT(eh)) { 2716 correct_index = 1; 2717 credits += (ext_depth(inode)) + 1; 2718 } 2719 credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(inode->i_sb); 2720 /* 2721 * We may end up freeing some index blocks and data from the 2722 * punched range. Note that partial clusters are accounted for 2723 * by ext4_free_data_revoke_credits(). 2724 */ 2725 revoke_credits = 2726 ext4_free_metadata_revoke_credits(inode->i_sb, 2727 ext_depth(inode)) + 2728 ext4_free_data_revoke_credits(inode, b - a + 1); 2729 2730 err = ext4_datasem_ensure_credits(handle, inode, credits, 2731 credits, revoke_credits); 2732 if (err) { 2733 if (err > 0) 2734 err = -EAGAIN; 2735 goto out; 2736 } 2737 2738 err = ext4_ext_get_access(handle, inode, path + depth); 2739 if (err) 2740 goto out; 2741 2742 err = ext4_remove_blocks(handle, inode, ex, partial, a, b); 2743 if (err) 2744 goto out; 2745 2746 if (num == 0) 2747 /* this extent is removed; mark slot entirely unused */ 2748 ext4_ext_store_pblock(ex, 0); 2749 2750 ex->ee_len = cpu_to_le16(num); 2751 /* 2752 * Do not mark unwritten if all the blocks in the 2753 * extent have been removed. 2754 */ 2755 if (unwritten && num) 2756 ext4_ext_mark_unwritten(ex); 2757 /* 2758 * If the extent was completely released, 2759 * we need to remove it from the leaf 2760 */ 2761 if (num == 0) { 2762 if (end != EXT_MAX_BLOCKS - 1) { 2763 /* 2764 * For hole punching, we need to scoot all the 2765 * extents up when an extent is removed so that 2766 * we dont have blank extents in the middle 2767 */ 2768 memmove(ex, ex+1, (EXT_LAST_EXTENT(eh) - ex) * 2769 sizeof(struct ext4_extent)); 2770 2771 /* Now get rid of the one at the end */ 2772 memset(EXT_LAST_EXTENT(eh), 0, 2773 sizeof(struct ext4_extent)); 2774 } 2775 le16_add_cpu(&eh->eh_entries, -1); 2776 } 2777 2778 err = ext4_ext_dirty(handle, inode, path + depth); 2779 if (err) 2780 goto out; 2781 2782 ext_debug(inode, "new extent: %u:%u:%llu\n", ex_ee_block, num, 2783 ext4_ext_pblock(ex)); 2784 ex--; 2785 ex_ee_block = le32_to_cpu(ex->ee_block); 2786 ex_ee_len = ext4_ext_get_actual_len(ex); 2787 } 2788 2789 if (correct_index && eh->eh_entries) 2790 err = ext4_ext_correct_indexes(handle, inode, path); 2791 2792 /* 2793 * If there's a partial cluster and at least one extent remains in 2794 * the leaf, free the partial cluster if it isn't shared with the 2795 * current extent. If it is shared with the current extent 2796 * we reset the partial cluster because we've reached the start of the 2797 * truncated/punched region and we're done removing blocks. 2798 */ 2799 if (partial->state == tofree && ex >= EXT_FIRST_EXTENT(eh)) { 2800 pblk = ext4_ext_pblock(ex) + ex_ee_len - 1; 2801 if (partial->pclu != EXT4_B2C(sbi, pblk)) { 2802 int flags = get_default_free_blocks_flags(inode); 2803 2804 if (ext4_is_pending(inode, partial->lblk)) 2805 flags |= EXT4_FREE_BLOCKS_RERESERVE_CLUSTER; 2806 ext4_free_blocks(handle, inode, NULL, 2807 EXT4_C2B(sbi, partial->pclu), 2808 sbi->s_cluster_ratio, flags); 2809 if (flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER) 2810 ext4_rereserve_cluster(inode, partial->lblk); 2811 } 2812 partial->state = initial; 2813 } 2814 2815 /* if this leaf is free, then we should 2816 * remove it from index block above */ 2817 if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL) 2818 err = ext4_ext_rm_idx(handle, inode, path, depth); 2819 2820 out: 2821 return err; 2822 } 2823 2824 /* 2825 * ext4_ext_more_to_rm: 2826 * returns 1 if current index has to be freed (even partial) 2827 */ 2828 static int 2829 ext4_ext_more_to_rm(struct ext4_ext_path *path) 2830 { 2831 BUG_ON(path->p_idx == NULL); 2832 2833 if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr)) 2834 return 0; 2835 2836 /* 2837 * if truncate on deeper level happened, it wasn't partial, 2838 * so we have to consider current index for truncation 2839 */ 2840 if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block) 2841 return 0; 2842 return 1; 2843 } 2844 2845 int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start, 2846 ext4_lblk_t end) 2847 { 2848 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 2849 int depth = ext_depth(inode); 2850 struct ext4_ext_path *path = NULL; 2851 struct partial_cluster partial; 2852 handle_t *handle; 2853 int i = 0, err = 0; 2854 int flags = EXT4_EX_NOCACHE | EXT4_EX_NOFAIL; 2855 2856 partial.pclu = 0; 2857 partial.lblk = 0; 2858 partial.state = initial; 2859 2860 ext_debug(inode, "truncate since %u to %u\n", start, end); 2861 2862 /* probably first extent we're gonna free will be last in block */ 2863 handle = ext4_journal_start_with_revoke(inode, EXT4_HT_TRUNCATE, 2864 depth + 1, 2865 ext4_free_metadata_revoke_credits(inode->i_sb, depth)); 2866 if (IS_ERR(handle)) 2867 return PTR_ERR(handle); 2868 2869 again: 2870 trace_ext4_ext_remove_space(inode, start, end, depth); 2871 2872 /* 2873 * Check if we are removing extents inside the extent tree. If that 2874 * is the case, we are going to punch a hole inside the extent tree 2875 * so we have to check whether we need to split the extent covering 2876 * the last block to remove so we can easily remove the part of it 2877 * in ext4_ext_rm_leaf(). 2878 */ 2879 if (end < EXT_MAX_BLOCKS - 1) { 2880 struct ext4_extent *ex; 2881 ext4_lblk_t ee_block, ex_end, lblk; 2882 ext4_fsblk_t pblk; 2883 2884 /* find extent for or closest extent to this block */ 2885 path = ext4_find_extent(inode, end, NULL, flags); 2886 if (IS_ERR(path)) { 2887 ext4_journal_stop(handle); 2888 return PTR_ERR(path); 2889 } 2890 depth = ext_depth(inode); 2891 /* Leaf not may not exist only if inode has no blocks at all */ 2892 ex = path[depth].p_ext; 2893 if (!ex) { 2894 if (depth) { 2895 EXT4_ERROR_INODE(inode, 2896 "path[%d].p_hdr == NULL", 2897 depth); 2898 err = -EFSCORRUPTED; 2899 } 2900 goto out; 2901 } 2902 2903 ee_block = le32_to_cpu(ex->ee_block); 2904 ex_end = ee_block + ext4_ext_get_actual_len(ex) - 1; 2905 2906 /* 2907 * See if the last block is inside the extent, if so split 2908 * the extent at 'end' block so we can easily remove the 2909 * tail of the first part of the split extent in 2910 * ext4_ext_rm_leaf(). 2911 */ 2912 if (end >= ee_block && end < ex_end) { 2913 2914 /* 2915 * If we're going to split the extent, note that 2916 * the cluster containing the block after 'end' is 2917 * in use to avoid freeing it when removing blocks. 2918 */ 2919 if (sbi->s_cluster_ratio > 1) { 2920 pblk = ext4_ext_pblock(ex) + end - ee_block + 1; 2921 partial.pclu = EXT4_B2C(sbi, pblk); 2922 partial.state = nofree; 2923 } 2924 2925 /* 2926 * Split the extent in two so that 'end' is the last 2927 * block in the first new extent. Also we should not 2928 * fail removing space due to ENOSPC so try to use 2929 * reserved block if that happens. 2930 */ 2931 path = ext4_force_split_extent_at(handle, inode, path, 2932 end + 1, 1); 2933 if (IS_ERR(path)) { 2934 err = PTR_ERR(path); 2935 goto out; 2936 } 2937 } else if (sbi->s_cluster_ratio > 1 && end >= ex_end && 2938 partial.state == initial) { 2939 /* 2940 * If we're punching, there's an extent to the right. 2941 * If the partial cluster hasn't been set, set it to 2942 * that extent's first cluster and its state to nofree 2943 * so it won't be freed should it contain blocks to be 2944 * removed. If it's already set (tofree/nofree), we're 2945 * retrying and keep the original partial cluster info 2946 * so a cluster marked tofree as a result of earlier 2947 * extent removal is not lost. 2948 */ 2949 lblk = ex_end + 1; 2950 err = ext4_ext_search_right(inode, path, &lblk, &pblk, 2951 NULL, flags); 2952 if (err < 0) 2953 goto out; 2954 if (pblk) { 2955 partial.pclu = EXT4_B2C(sbi, pblk); 2956 partial.state = nofree; 2957 } 2958 } 2959 } 2960 /* 2961 * We start scanning from right side, freeing all the blocks 2962 * after i_size and walking into the tree depth-wise. 2963 */ 2964 depth = ext_depth(inode); 2965 if (path) { 2966 int k = i = depth; 2967 while (--k > 0) 2968 path[k].p_block = 2969 le16_to_cpu(path[k].p_hdr->eh_entries)+1; 2970 } else { 2971 path = kzalloc_objs(struct ext4_ext_path, depth + 1, 2972 GFP_NOFS | __GFP_NOFAIL); 2973 path[0].p_maxdepth = path[0].p_depth = depth; 2974 path[0].p_hdr = ext_inode_hdr(inode); 2975 i = 0; 2976 2977 if (ext4_ext_check(inode, path[0].p_hdr, depth, 0)) { 2978 err = -EFSCORRUPTED; 2979 goto out; 2980 } 2981 } 2982 err = 0; 2983 2984 while (i >= 0 && err == 0) { 2985 if (i == depth) { 2986 /* this is leaf block */ 2987 err = ext4_ext_rm_leaf(handle, inode, path, 2988 &partial, start, end); 2989 /* root level has p_bh == NULL, brelse() eats this */ 2990 ext4_ext_path_brelse(path + i); 2991 i--; 2992 continue; 2993 } 2994 2995 /* this is index block */ 2996 if (!path[i].p_hdr) { 2997 ext_debug(inode, "initialize header\n"); 2998 path[i].p_hdr = ext_block_hdr(path[i].p_bh); 2999 } 3000 3001 if (!path[i].p_idx) { 3002 /* this level hasn't been touched yet */ 3003 path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr); 3004 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1; 3005 ext_debug(inode, "init index ptr: hdr 0x%p, num %d\n", 3006 path[i].p_hdr, 3007 le16_to_cpu(path[i].p_hdr->eh_entries)); 3008 } else { 3009 /* we were already here, see at next index */ 3010 path[i].p_idx--; 3011 } 3012 3013 ext_debug(inode, "level %d - index, first 0x%p, cur 0x%p\n", 3014 i, EXT_FIRST_INDEX(path[i].p_hdr), 3015 path[i].p_idx); 3016 if (ext4_ext_more_to_rm(path + i)) { 3017 struct buffer_head *bh; 3018 /* go to the next level */ 3019 ext_debug(inode, "move to level %d (block %llu)\n", 3020 i + 1, ext4_idx_pblock(path[i].p_idx)); 3021 memset(path + i + 1, 0, sizeof(*path)); 3022 bh = read_extent_tree_block(inode, path[i].p_idx, 3023 depth - i - 1, flags); 3024 if (IS_ERR(bh)) { 3025 /* should we reset i_size? */ 3026 err = PTR_ERR(bh); 3027 break; 3028 } 3029 /* Yield here to deal with large extent trees. 3030 * Should be a no-op if we did IO above. */ 3031 cond_resched(); 3032 if (WARN_ON(i + 1 > depth)) { 3033 err = -EFSCORRUPTED; 3034 break; 3035 } 3036 path[i + 1].p_bh = bh; 3037 3038 /* save actual number of indexes since this 3039 * number is changed at the next iteration */ 3040 path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries); 3041 i++; 3042 } else { 3043 /* we finished processing this index, go up */ 3044 if (path[i].p_hdr->eh_entries == 0 && i > 0) { 3045 /* index is empty, remove it; 3046 * handle must be already prepared by the 3047 * truncatei_leaf() */ 3048 err = ext4_ext_rm_idx(handle, inode, path, i); 3049 } 3050 /* root level has p_bh == NULL, brelse() eats this */ 3051 ext4_ext_path_brelse(path + i); 3052 i--; 3053 ext_debug(inode, "return to level %d\n", i); 3054 } 3055 } 3056 3057 trace_ext4_ext_remove_space_done(inode, start, end, depth, &partial, 3058 path->p_hdr->eh_entries); 3059 3060 /* 3061 * if there's a partial cluster and we have removed the first extent 3062 * in the file, then we also free the partial cluster, if any 3063 */ 3064 if (partial.state == tofree && err == 0) { 3065 int flags = get_default_free_blocks_flags(inode); 3066 3067 if (ext4_is_pending(inode, partial.lblk)) 3068 flags |= EXT4_FREE_BLOCKS_RERESERVE_CLUSTER; 3069 ext4_free_blocks(handle, inode, NULL, 3070 EXT4_C2B(sbi, partial.pclu), 3071 sbi->s_cluster_ratio, flags); 3072 if (flags & EXT4_FREE_BLOCKS_RERESERVE_CLUSTER) 3073 ext4_rereserve_cluster(inode, partial.lblk); 3074 partial.state = initial; 3075 } 3076 3077 /* TODO: flexible tree reduction should be here */ 3078 if (path->p_hdr->eh_entries == 0) { 3079 /* 3080 * truncate to zero freed all the tree, 3081 * so we need to correct eh_depth 3082 */ 3083 err = ext4_ext_get_access(handle, inode, path); 3084 if (err == 0) { 3085 ext_inode_hdr(inode)->eh_depth = 0; 3086 ext_inode_hdr(inode)->eh_max = 3087 cpu_to_le16(ext4_ext_space_root(inode, 0)); 3088 err = ext4_ext_dirty(handle, inode, path); 3089 } 3090 } 3091 out: 3092 ext4_free_ext_path(path); 3093 path = NULL; 3094 if (err == -EAGAIN) 3095 goto again; 3096 ext4_journal_stop(handle); 3097 3098 return err; 3099 } 3100 3101 /* 3102 * called at mount time 3103 */ 3104 void ext4_ext_init(struct super_block *sb) 3105 { 3106 /* 3107 * possible initialization would be here 3108 */ 3109 3110 if (ext4_has_feature_extents(sb)) { 3111 #if defined(AGGRESSIVE_TEST) || defined(CHECK_BINSEARCH) || defined(EXTENTS_STATS) 3112 printk(KERN_INFO "EXT4-fs: file extents enabled" 3113 #ifdef AGGRESSIVE_TEST 3114 ", aggressive tests" 3115 #endif 3116 #ifdef CHECK_BINSEARCH 3117 ", check binsearch" 3118 #endif 3119 #ifdef EXTENTS_STATS 3120 ", stats" 3121 #endif 3122 "\n"); 3123 #endif 3124 #ifdef EXTENTS_STATS 3125 spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock); 3126 EXT4_SB(sb)->s_ext_min = 1 << 30; 3127 EXT4_SB(sb)->s_ext_max = 0; 3128 #endif 3129 } 3130 } 3131 3132 /* 3133 * called at umount time 3134 */ 3135 void ext4_ext_release(struct super_block *sb) 3136 { 3137 if (!ext4_has_feature_extents(sb)) 3138 return; 3139 3140 #ifdef EXTENTS_STATS 3141 if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) { 3142 struct ext4_sb_info *sbi = EXT4_SB(sb); 3143 printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n", 3144 sbi->s_ext_blocks, sbi->s_ext_extents, 3145 sbi->s_ext_blocks / sbi->s_ext_extents); 3146 printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n", 3147 sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max); 3148 } 3149 #endif 3150 } 3151 3152 static void ext4_zeroout_es(struct inode *inode, struct ext4_extent *ex) 3153 { 3154 ext4_lblk_t ee_block; 3155 ext4_fsblk_t ee_pblock; 3156 unsigned int ee_len; 3157 3158 ee_block = le32_to_cpu(ex->ee_block); 3159 ee_len = ext4_ext_get_actual_len(ex); 3160 ee_pblock = ext4_ext_pblock(ex); 3161 3162 if (ee_len == 0) 3163 return; 3164 3165 ext4_es_insert_extent(inode, ee_block, ee_len, ee_pblock, 3166 EXTENT_STATUS_WRITTEN, false); 3167 } 3168 3169 /* FIXME!! we need to try to merge to left or right after zero-out */ 3170 int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex) 3171 { 3172 ext4_fsblk_t ee_pblock; 3173 unsigned int ee_len; 3174 3175 KUNIT_STATIC_STUB_REDIRECT(ext4_ext_zeroout, inode, ex); 3176 3177 ee_len = ext4_ext_get_actual_len(ex); 3178 ee_pblock = ext4_ext_pblock(ex); 3179 return ext4_issue_zeroout(inode, le32_to_cpu(ex->ee_block), ee_pblock, 3180 ee_len); 3181 } 3182 3183 /* 3184 * ext4_split_extent_at() splits an extent at given block. 3185 * 3186 * @handle: the journal handle 3187 * @inode: the file inode 3188 * @path: the path to the extent 3189 * @split: the logical block where the extent is splitted. 3190 * @flags: flags used to insert new extent to extent tree. 3191 * 3192 * 3193 * Splits extent [a, b] into two extents [a, @split) and [@split, b], states 3194 * of which are same as the original extent. No conversion is performed. 3195 * 3196 * Return an extent path pointer on success, or an error pointer on failure. On 3197 * failure, the extent is restored to original state. 3198 */ 3199 static struct ext4_ext_path *ext4_split_extent_at(handle_t *handle, 3200 struct inode *inode, 3201 struct ext4_ext_path *path, 3202 ext4_lblk_t split, 3203 int flags) 3204 { 3205 ext4_fsblk_t newblock; 3206 ext4_lblk_t ee_block; 3207 struct ext4_extent *ex, newex, orig_ex; 3208 struct ext4_extent *ex2 = NULL; 3209 unsigned int ee_len, depth; 3210 int err = 0, insert_err = 0, is_unwrit = 0; 3211 3212 /* Do not cache extents that are in the process of being modified. */ 3213 flags |= EXT4_EX_NOCACHE; 3214 3215 ext_debug(inode, "logical block %llu\n", (unsigned long long)split); 3216 3217 ext4_ext_show_leaf(inode, path); 3218 3219 depth = ext_depth(inode); 3220 ex = path[depth].p_ext; 3221 ee_block = le32_to_cpu(ex->ee_block); 3222 ee_len = ext4_ext_get_actual_len(ex); 3223 newblock = split - ee_block + ext4_ext_pblock(ex); 3224 is_unwrit = ext4_ext_is_unwritten(ex); 3225 3226 BUG_ON(split < ee_block || split >= (ee_block + ee_len)); 3227 3228 /* 3229 * No split needed 3230 */ 3231 if (split == ee_block) 3232 goto out; 3233 3234 err = ext4_ext_get_access(handle, inode, path + depth); 3235 if (err) 3236 goto out; 3237 3238 /* case a */ 3239 memcpy(&orig_ex, ex, sizeof(orig_ex)); 3240 ex->ee_len = cpu_to_le16(split - ee_block); 3241 if (is_unwrit) 3242 ext4_ext_mark_unwritten(ex); 3243 3244 /* 3245 * path may lead to new leaf, not to original leaf any more 3246 * after ext4_ext_insert_extent() returns, 3247 */ 3248 err = ext4_ext_dirty(handle, inode, path + depth); 3249 if (err) 3250 goto fix_extent_len; 3251 3252 ex2 = &newex; 3253 ex2->ee_block = cpu_to_le32(split); 3254 ex2->ee_len = cpu_to_le16(ee_len - (split - ee_block)); 3255 ext4_ext_store_pblock(ex2, newblock); 3256 if (is_unwrit) 3257 ext4_ext_mark_unwritten(ex2); 3258 3259 path = ext4_ext_insert_extent(handle, inode, path, &newex, flags); 3260 if (!IS_ERR(path)) 3261 return path; 3262 3263 insert_err = PTR_ERR(path); 3264 err = 0; 3265 if (insert_err != -ENOSPC && insert_err != -EDQUOT && 3266 insert_err != -ENOMEM) 3267 goto out_path; 3268 3269 /* 3270 * Get a new path to try to zeroout or fix the extent length. 3271 * Using EXT4_EX_NOFAIL guarantees that ext4_find_extent() 3272 * will not return -ENOMEM, otherwise -ENOMEM will cause a 3273 * retry in do_writepages(), and a WARN_ON may be triggered 3274 * in ext4_da_update_reserve_space() due to an incorrect 3275 * ee_len causing the i_reserved_data_blocks exception. 3276 */ 3277 path = ext4_find_extent(inode, ee_block, NULL, flags | EXT4_EX_NOFAIL); 3278 if (IS_ERR(path)) { 3279 EXT4_ERROR_INODE(inode, "Failed split extent on %u, err %pe", 3280 split, path); 3281 goto out_path; 3282 } 3283 3284 depth = ext_depth(inode); 3285 ex = path[depth].p_ext; 3286 if (!ex) { 3287 EXT4_ERROR_INODE(inode, 3288 "bad extent address lblock: %lu, depth: %d pblock %llu", 3289 (unsigned long)ee_block, depth, path[depth].p_block); 3290 err = -EFSCORRUPTED; 3291 goto out; 3292 } 3293 3294 err = ext4_ext_get_access(handle, inode, path + depth); 3295 if (err) 3296 goto out; 3297 3298 fix_extent_len: 3299 ex->ee_len = orig_ex.ee_len; 3300 err = ext4_ext_dirty(handle, inode, path + path->p_depth); 3301 out: 3302 if (err || insert_err) { 3303 ext4_free_ext_path(path); 3304 path = err ? ERR_PTR(err) : ERR_PTR(insert_err); 3305 } 3306 out_path: 3307 if (IS_ERR(path)) 3308 /* Remove all remaining potentially stale extents. */ 3309 ext4_es_remove_extent(inode, ee_block, ee_len); 3310 ext4_ext_show_leaf(inode, path); 3311 return path; 3312 } 3313 3314 static int ext4_split_extent_zeroout(handle_t *handle, struct inode *inode, 3315 struct ext4_ext_path *path, 3316 struct ext4_map_blocks *map, int flags) 3317 { 3318 struct ext4_extent *ex; 3319 unsigned int ee_len, depth; 3320 ext4_lblk_t ee_block; 3321 uint64_t lblk, pblk, len; 3322 int is_unwrit; 3323 int err = 0; 3324 3325 depth = ext_depth(inode); 3326 ex = path[depth].p_ext; 3327 ee_block = le32_to_cpu(ex->ee_block); 3328 ee_len = ext4_ext_get_actual_len(ex); 3329 is_unwrit = ext4_ext_is_unwritten(ex); 3330 3331 if (flags & EXT4_GET_BLOCKS_CONVERT) { 3332 /* 3333 * EXT4_GET_BLOCKS_CONVERT: Caller wants the range specified by 3334 * map to be initialized. Zeroout everything except the map 3335 * range. 3336 */ 3337 3338 loff_t map_end = (loff_t) map->m_lblk + map->m_len; 3339 loff_t ex_end = (loff_t) ee_block + ee_len; 3340 3341 if (!is_unwrit) 3342 /* Shouldn't happen. Just exit */ 3343 return -EINVAL; 3344 3345 /* zeroout left */ 3346 if (map->m_lblk > ee_block) { 3347 lblk = ee_block; 3348 len = map->m_lblk - ee_block; 3349 pblk = ext4_ext_pblock(ex); 3350 err = ext4_issue_zeroout(inode, lblk, pblk, len); 3351 if (err) 3352 /* ZEROOUT failed, just return original error */ 3353 return err; 3354 } 3355 3356 /* zeroout right */ 3357 if (map_end < ex_end) { 3358 lblk = map_end; 3359 len = ex_end - map_end; 3360 pblk = ext4_ext_pblock(ex) + (map_end - ee_block); 3361 err = ext4_issue_zeroout(inode, lblk, pblk, len); 3362 if (err) 3363 /* ZEROOUT failed, just return original error */ 3364 return err; 3365 } 3366 } else if (flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN) { 3367 /* 3368 * EXT4_GET_BLOCKS_CONVERT_UNWRITTEN: Caller wants the 3369 * range specified by map to be marked unwritten. 3370 * Zeroout the map range leaving rest as it is. 3371 */ 3372 3373 if (is_unwrit) 3374 /* Shouldn't happen. Just exit */ 3375 return -EINVAL; 3376 3377 lblk = map->m_lblk; 3378 len = map->m_len; 3379 pblk = ext4_ext_pblock(ex) + (map->m_lblk - ee_block); 3380 err = ext4_issue_zeroout(inode, lblk, pblk, len); 3381 if (err) 3382 /* ZEROOUT failed, just return original error */ 3383 return err; 3384 } else { 3385 /* 3386 * We no longer perform unwritten to unwritten splits in IO paths. 3387 * Hence this should not happen. 3388 */ 3389 WARN_ON_ONCE(true); 3390 return -EINVAL; 3391 } 3392 3393 err = ext4_ext_get_access(handle, inode, path + depth); 3394 if (err) 3395 return err; 3396 3397 ext4_ext_mark_initialized(ex); 3398 3399 err = ext4_ext_dirty(handle, inode, path + depth); 3400 if (err) 3401 return err; 3402 3403 return 0; 3404 } 3405 3406 /* 3407 * ext4_split_extent() splits an extent and mark extent which is covered 3408 * by @map as split_flags indicates 3409 * 3410 * It may result in splitting the extent into multiple extents (up to three) 3411 * There are three possibilities: 3412 * a> There is no split required 3413 * b> Splits in two extents: Split is happening at either end of the extent 3414 * c> Splits in three extents: Somone is splitting in middle of the extent 3415 * 3416 */ 3417 static struct ext4_ext_path *ext4_split_extent(handle_t *handle, 3418 struct inode *inode, 3419 struct ext4_ext_path *path, 3420 struct ext4_map_blocks *map, 3421 int split_flag, int flags, 3422 unsigned int *allocated, bool *did_zeroout) 3423 { 3424 ext4_lblk_t ee_block, orig_ee_block; 3425 struct ext4_extent *ex; 3426 unsigned int ee_len, orig_ee_len, depth; 3427 int unwritten, orig_unwritten; 3428 int orig_err = 0; 3429 3430 depth = ext_depth(inode); 3431 ex = path[depth].p_ext; 3432 ee_block = le32_to_cpu(ex->ee_block); 3433 ee_len = ext4_ext_get_actual_len(ex); 3434 unwritten = ext4_ext_is_unwritten(ex); 3435 3436 orig_ee_block = ee_block; 3437 orig_ee_len = ee_len; 3438 orig_unwritten = unwritten; 3439 3440 /* Do not cache extents that are in the process of being modified. */ 3441 flags |= EXT4_EX_NOCACHE; 3442 3443 if (map->m_lblk + map->m_len < ee_block + ee_len) { 3444 path = ext4_split_extent_at(handle, inode, path, 3445 map->m_lblk + map->m_len, flags); 3446 if (IS_ERR(path)) 3447 goto try_zeroout; 3448 3449 /* 3450 * Update path is required because previous ext4_split_extent_at 3451 * may result in split of original leaf or extent zeroout. 3452 */ 3453 path = ext4_find_extent(inode, map->m_lblk, path, flags); 3454 if (IS_ERR(path)) 3455 goto try_zeroout; 3456 3457 depth = ext_depth(inode); 3458 ex = path[depth].p_ext; 3459 if (!ex) { 3460 EXT4_ERROR_INODE(inode, "unexpected hole at %lu", 3461 (unsigned long) map->m_lblk); 3462 ext4_free_ext_path(path); 3463 return ERR_PTR(-EFSCORRUPTED); 3464 } 3465 3466 /* extent would have changed so update original values */ 3467 orig_ee_block = le32_to_cpu(ex->ee_block); 3468 orig_ee_len = ext4_ext_get_actual_len(ex); 3469 orig_unwritten = ext4_ext_is_unwritten(ex); 3470 } 3471 3472 if (map->m_lblk >= ee_block) { 3473 path = ext4_split_extent_at(handle, inode, path, map->m_lblk, 3474 flags); 3475 if (IS_ERR(path)) 3476 goto try_zeroout; 3477 } 3478 3479 goto success; 3480 3481 try_zeroout: 3482 /* 3483 * There was an error in splitting the extent. So instead, just zeroout 3484 * unwritten portions and convert it to initialized as a last resort. If 3485 * there is any failure here we just return the original error 3486 */ 3487 3488 orig_err = PTR_ERR(path); 3489 if (orig_err != -ENOSPC && orig_err != -EDQUOT && orig_err != -ENOMEM) 3490 goto out_orig_err; 3491 3492 /* we can't zeroout? just return the original err */ 3493 if (!(split_flag & EXT4_EXT_MAY_ZEROOUT)) 3494 goto out_orig_err; 3495 3496 if (flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN) { 3497 int max_zeroout_blks = 3498 EXT4_SB(inode->i_sb)->s_extent_max_zeroout_kb >> 3499 (inode->i_sb->s_blocksize_bits - 10); 3500 3501 if (map->m_len > max_zeroout_blks) 3502 goto out_orig_err; 3503 } 3504 3505 path = ext4_find_extent(inode, map->m_lblk, NULL, flags); 3506 if (IS_ERR(path)) 3507 goto out_orig_err; 3508 3509 depth = ext_depth(inode); 3510 ex = path[depth].p_ext; 3511 ee_block = le32_to_cpu(ex->ee_block); 3512 ee_len = ext4_ext_get_actual_len(ex); 3513 unwritten = ext4_ext_is_unwritten(ex); 3514 3515 /* extent to zeroout should have been unchanged but its not */ 3516 if (WARN_ON(ee_block != orig_ee_block || ee_len != orig_ee_len || 3517 unwritten != orig_unwritten)) 3518 goto out_free_path; 3519 3520 if (ext4_split_extent_zeroout(handle, inode, path, map, flags)) 3521 goto out_free_path; 3522 3523 /* zeroout succeeded */ 3524 if (did_zeroout) 3525 *did_zeroout = true; 3526 3527 success: 3528 if (allocated) { 3529 if (map->m_lblk + map->m_len > ee_block + ee_len) 3530 *allocated = ee_len - (map->m_lblk - ee_block); 3531 else 3532 *allocated = map->m_len; 3533 } 3534 ext4_ext_show_leaf(inode, path); 3535 return path; 3536 3537 out_free_path: 3538 ext4_free_ext_path(path); 3539 out_orig_err: 3540 return ERR_PTR(orig_err); 3541 3542 } 3543 3544 /* 3545 * This function is called by ext4_ext_map_blocks() if someone tries to write 3546 * to an unwritten extent. It may result in splitting the unwritten 3547 * extent into multiple extents (up to three - one initialized and two 3548 * unwritten). 3549 * There are three possibilities: 3550 * a> There is no split required: Entire extent should be initialized 3551 * b> Splits in two extents: Write is happening at either end of the extent 3552 * c> Splits in three extents: Somone is writing in middle of the extent 3553 * 3554 * Pre-conditions: 3555 * - The extent pointed to by 'path' is unwritten. 3556 * - The extent pointed to by 'path' contains a superset 3557 * of the logical span [map->m_lblk, map->m_lblk + map->m_len). 3558 * 3559 * Post-conditions on success: 3560 * - the returned value is the number of blocks beyond map->l_lblk 3561 * that are allocated and initialized. 3562 * It is guaranteed to be >= map->m_len. 3563 */ 3564 static struct ext4_ext_path * 3565 ext4_ext_convert_to_initialized(handle_t *handle, struct inode *inode, 3566 struct ext4_map_blocks *map, struct ext4_ext_path *path, 3567 int flags, unsigned int *allocated) 3568 { 3569 struct ext4_sb_info *sbi; 3570 struct ext4_extent_header *eh; 3571 struct ext4_map_blocks split_map; 3572 struct ext4_extent zero_ex1, zero_ex2; 3573 struct ext4_extent *ex, *abut_ex; 3574 ext4_lblk_t ee_block, eof_block; 3575 unsigned int ee_len, depth, map_len = map->m_len; 3576 int err = 0; 3577 unsigned int max_zeroout = 0; 3578 3579 ext_debug(inode, "logical block %llu, max_blocks %u\n", 3580 (unsigned long long)map->m_lblk, map_len); 3581 3582 sbi = EXT4_SB(inode->i_sb); 3583 eof_block = (EXT4_I(inode)->i_disksize + inode->i_sb->s_blocksize - 1) 3584 >> inode->i_sb->s_blocksize_bits; 3585 if (eof_block < map->m_lblk + map_len) 3586 eof_block = map->m_lblk + map_len; 3587 3588 depth = ext_depth(inode); 3589 eh = path[depth].p_hdr; 3590 ex = path[depth].p_ext; 3591 ee_block = le32_to_cpu(ex->ee_block); 3592 ee_len = ext4_ext_get_actual_len(ex); 3593 zero_ex1.ee_len = 0; 3594 zero_ex2.ee_len = 0; 3595 3596 trace_ext4_ext_convert_to_initialized_enter(inode, map, ex); 3597 3598 /* Pre-conditions */ 3599 BUG_ON(!ext4_ext_is_unwritten(ex)); 3600 BUG_ON(!in_range(map->m_lblk, ee_block, ee_len)); 3601 3602 /* 3603 * Attempt to transfer newly initialized blocks from the currently 3604 * unwritten extent to its neighbor. This is much cheaper 3605 * than an insertion followed by a merge as those involve costly 3606 * memmove() calls. Transferring to the left is the common case in 3607 * steady state for workloads doing fallocate(FALLOC_FL_KEEP_SIZE) 3608 * followed by append writes. 3609 * 3610 * Limitations of the current logic: 3611 * - L1: we do not deal with writes covering the whole extent. 3612 * This would require removing the extent if the transfer 3613 * is possible. 3614 * - L2: we only attempt to merge with an extent stored in the 3615 * same extent tree node. 3616 */ 3617 *allocated = 0; 3618 if ((map->m_lblk == ee_block) && 3619 /* See if we can merge left */ 3620 (map_len < ee_len) && /*L1*/ 3621 (ex > EXT_FIRST_EXTENT(eh))) { /*L2*/ 3622 ext4_lblk_t prev_lblk; 3623 ext4_fsblk_t prev_pblk, ee_pblk; 3624 unsigned int prev_len; 3625 3626 abut_ex = ex - 1; 3627 prev_lblk = le32_to_cpu(abut_ex->ee_block); 3628 prev_len = ext4_ext_get_actual_len(abut_ex); 3629 prev_pblk = ext4_ext_pblock(abut_ex); 3630 ee_pblk = ext4_ext_pblock(ex); 3631 3632 /* 3633 * A transfer of blocks from 'ex' to 'abut_ex' is allowed 3634 * upon those conditions: 3635 * - C1: abut_ex is initialized, 3636 * - C2: abut_ex is logically abutting ex, 3637 * - C3: abut_ex is physically abutting ex, 3638 * - C4: abut_ex can receive the additional blocks without 3639 * overflowing the (initialized) length limit. 3640 */ 3641 if ((!ext4_ext_is_unwritten(abut_ex)) && /*C1*/ 3642 ((prev_lblk + prev_len) == ee_block) && /*C2*/ 3643 ((prev_pblk + prev_len) == ee_pblk) && /*C3*/ 3644 (prev_len < (EXT_INIT_MAX_LEN - map_len))) { /*C4*/ 3645 err = ext4_ext_get_access(handle, inode, path + depth); 3646 if (err) 3647 goto errout; 3648 3649 trace_ext4_ext_convert_to_initialized_fastpath(inode, 3650 map, ex, abut_ex); 3651 3652 /* Shift the start of ex by 'map_len' blocks */ 3653 ex->ee_block = cpu_to_le32(ee_block + map_len); 3654 ext4_ext_store_pblock(ex, ee_pblk + map_len); 3655 ex->ee_len = cpu_to_le16(ee_len - map_len); 3656 ext4_ext_mark_unwritten(ex); /* Restore the flag */ 3657 3658 /* Extend abut_ex by 'map_len' blocks */ 3659 abut_ex->ee_len = cpu_to_le16(prev_len + map_len); 3660 3661 /* Result: number of initialized blocks past m_lblk */ 3662 *allocated = map_len; 3663 } 3664 } else if (((map->m_lblk + map_len) == (ee_block + ee_len)) && 3665 (map_len < ee_len) && /*L1*/ 3666 ex < EXT_LAST_EXTENT(eh)) { /*L2*/ 3667 /* See if we can merge right */ 3668 ext4_lblk_t next_lblk; 3669 ext4_fsblk_t next_pblk, ee_pblk; 3670 unsigned int next_len; 3671 3672 abut_ex = ex + 1; 3673 next_lblk = le32_to_cpu(abut_ex->ee_block); 3674 next_len = ext4_ext_get_actual_len(abut_ex); 3675 next_pblk = ext4_ext_pblock(abut_ex); 3676 ee_pblk = ext4_ext_pblock(ex); 3677 3678 /* 3679 * A transfer of blocks from 'ex' to 'abut_ex' is allowed 3680 * upon those conditions: 3681 * - C1: abut_ex is initialized, 3682 * - C2: abut_ex is logically abutting ex, 3683 * - C3: abut_ex is physically abutting ex, 3684 * - C4: abut_ex can receive the additional blocks without 3685 * overflowing the (initialized) length limit. 3686 */ 3687 if ((!ext4_ext_is_unwritten(abut_ex)) && /*C1*/ 3688 ((map->m_lblk + map_len) == next_lblk) && /*C2*/ 3689 ((ee_pblk + ee_len) == next_pblk) && /*C3*/ 3690 (next_len < (EXT_INIT_MAX_LEN - map_len))) { /*C4*/ 3691 err = ext4_ext_get_access(handle, inode, path + depth); 3692 if (err) 3693 goto errout; 3694 3695 trace_ext4_ext_convert_to_initialized_fastpath(inode, 3696 map, ex, abut_ex); 3697 3698 /* Shift the start of abut_ex by 'map_len' blocks */ 3699 abut_ex->ee_block = cpu_to_le32(next_lblk - map_len); 3700 ext4_ext_store_pblock(abut_ex, next_pblk - map_len); 3701 ex->ee_len = cpu_to_le16(ee_len - map_len); 3702 ext4_ext_mark_unwritten(ex); /* Restore the flag */ 3703 3704 /* Extend abut_ex by 'map_len' blocks */ 3705 abut_ex->ee_len = cpu_to_le16(next_len + map_len); 3706 3707 /* Result: number of initialized blocks past m_lblk */ 3708 *allocated = map_len; 3709 } 3710 } 3711 if (*allocated) { 3712 /* Mark the block containing both extents as dirty */ 3713 err = ext4_ext_dirty(handle, inode, path + depth); 3714 3715 /* Update path to point to the right extent */ 3716 path[depth].p_ext = abut_ex; 3717 if (err) 3718 goto errout; 3719 goto out; 3720 } else 3721 *allocated = ee_len - (map->m_lblk - ee_block); 3722 3723 WARN_ON(map->m_lblk < ee_block); 3724 /* 3725 * It is safe to convert extent to initialized via explicit 3726 * zeroout only if extent is fully inside i_size or new_size. 3727 */ 3728 if (ee_block + ee_len <= eof_block) 3729 max_zeroout = sbi->s_extent_max_zeroout_kb >> 3730 (inode->i_sb->s_blocksize_bits - 10); 3731 3732 /* 3733 * five cases: 3734 * 1. split the extent into three extents. 3735 * 2. split the extent into two extents, zeroout the head of the first 3736 * extent. 3737 * 3. split the extent into two extents, zeroout the tail of the second 3738 * extent. 3739 * 4. split the extent into two extents with out zeroout. 3740 * 5. no splitting needed, just possibly zeroout the head and / or the 3741 * tail of the extent. 3742 */ 3743 split_map.m_lblk = map->m_lblk; 3744 split_map.m_len = map->m_len; 3745 3746 if (max_zeroout && (*allocated > split_map.m_len)) { 3747 if (*allocated <= max_zeroout) { 3748 /* case 3 or 5 */ 3749 zero_ex1.ee_block = 3750 cpu_to_le32(split_map.m_lblk + 3751 split_map.m_len); 3752 zero_ex1.ee_len = 3753 cpu_to_le16(*allocated - split_map.m_len); 3754 ext4_ext_store_pblock(&zero_ex1, 3755 ext4_ext_pblock(ex) + split_map.m_lblk + 3756 split_map.m_len - ee_block); 3757 err = ext4_ext_zeroout(inode, &zero_ex1); 3758 if (err) 3759 goto fallback; 3760 split_map.m_len = *allocated; 3761 } 3762 if (split_map.m_lblk - ee_block + split_map.m_len < 3763 max_zeroout) { 3764 /* case 2 or 5 */ 3765 if (split_map.m_lblk != ee_block) { 3766 zero_ex2.ee_block = ex->ee_block; 3767 zero_ex2.ee_len = cpu_to_le16(split_map.m_lblk - 3768 ee_block); 3769 ext4_ext_store_pblock(&zero_ex2, 3770 ext4_ext_pblock(ex)); 3771 err = ext4_ext_zeroout(inode, &zero_ex2); 3772 if (err) 3773 goto fallback; 3774 } 3775 3776 split_map.m_len += split_map.m_lblk - ee_block; 3777 split_map.m_lblk = ee_block; 3778 *allocated = map->m_len; 3779 } 3780 } 3781 3782 fallback: 3783 path = ext4_split_convert_extents(handle, inode, &split_map, path, 3784 flags | EXT4_GET_BLOCKS_CONVERT, NULL); 3785 if (IS_ERR(path)) 3786 return path; 3787 out: 3788 /* If we have gotten a failure, don't zero out status tree */ 3789 ext4_zeroout_es(inode, &zero_ex1); 3790 ext4_zeroout_es(inode, &zero_ex2); 3791 return path; 3792 3793 errout: 3794 ext4_free_ext_path(path); 3795 return ERR_PTR(err); 3796 } 3797 3798 /* 3799 * This function is called by ext4_ext_map_blocks() from 3800 * ext4_get_blocks_dio_write() when DIO to write 3801 * to an unwritten extent. 3802 * 3803 * Writing to an unwritten extent may result in splitting the unwritten 3804 * extent into multiple initialized/unwritten extents (up to three) 3805 * There are three possibilities: 3806 * a> There is no split required: Entire extent should be unwritten 3807 * b> Splits in two extents: Write is happening at either end of the extent 3808 * c> Splits in three extents: Somone is writing in middle of the extent 3809 * 3810 * This works the same way in the case of initialized -> unwritten conversion. 3811 * 3812 * One of more index blocks maybe needed if the extent tree grow after 3813 * the unwritten extent split. To prevent ENOSPC occur at the IO 3814 * complete, we need to split the unwritten extent before DIO submit 3815 * the IO. The unwritten extent called at this time will be split 3816 * into three unwritten extent(at most). After IO complete, the part 3817 * being filled will be convert to initialized by the end_io callback function 3818 * via ext4_convert_unwritten_extents(). 3819 * 3820 * The size of unwritten extent to be written is passed to the caller via the 3821 * allocated pointer. Return an extent path pointer on success, or an error 3822 * pointer on failure. 3823 */ 3824 static struct ext4_ext_path *ext4_split_convert_extents(handle_t *handle, 3825 struct inode *inode, 3826 struct ext4_map_blocks *map, 3827 struct ext4_ext_path *path, 3828 int flags, unsigned int *allocated) 3829 { 3830 ext4_lblk_t eof_block; 3831 ext4_lblk_t ee_block; 3832 struct ext4_extent *ex; 3833 unsigned int ee_len; 3834 int split_flag = 0, depth, err = 0; 3835 bool did_zeroout = false; 3836 3837 ext_debug(inode, "logical block %llu, max_blocks %u\n", 3838 (unsigned long long)map->m_lblk, map->m_len); 3839 3840 eof_block = (EXT4_I(inode)->i_disksize + inode->i_sb->s_blocksize - 1) 3841 >> inode->i_sb->s_blocksize_bits; 3842 if (eof_block < map->m_lblk + map->m_len) 3843 eof_block = map->m_lblk + map->m_len; 3844 depth = ext_depth(inode); 3845 ex = path[depth].p_ext; 3846 ee_block = le32_to_cpu(ex->ee_block); 3847 ee_len = ext4_ext_get_actual_len(ex); 3848 3849 /* No split needed */ 3850 if (ee_block == map->m_lblk && ee_len == map->m_len) 3851 goto convert; 3852 3853 /* 3854 * It is only safe to convert extent to initialized via explicit 3855 * zeroout only if extent is fully inside i_size or new_size. 3856 */ 3857 split_flag |= ee_block + ee_len <= eof_block ? EXT4_EXT_MAY_ZEROOUT : 0; 3858 3859 /* 3860 * pass SPLIT_NOMERGE explicitly so we don't end up merging extents we 3861 * just split. 3862 */ 3863 path = ext4_split_extent(handle, inode, path, map, split_flag, 3864 flags | EXT4_GET_BLOCKS_SPLIT_NOMERGE, 3865 allocated, &did_zeroout); 3866 if (IS_ERR(path)) 3867 return path; 3868 3869 convert: 3870 path = ext4_find_extent(inode, map->m_lblk, path, flags); 3871 if (IS_ERR(path)) 3872 return path; 3873 3874 depth = ext_depth(inode); 3875 ex = path[depth].p_ext; 3876 3877 /* 3878 * Conversion is already handled in case of zeroout 3879 */ 3880 if (!did_zeroout) { 3881 err = ext4_ext_get_access(handle, inode, path + depth); 3882 if (err) 3883 goto err; 3884 3885 if (flags & EXT4_GET_BLOCKS_CONVERT) 3886 ext4_ext_mark_initialized(ex); 3887 else if (flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN) 3888 ext4_ext_mark_unwritten(ex); 3889 3890 if (!(flags & EXT4_GET_BLOCKS_SPLIT_NOMERGE)) 3891 /* 3892 * note: ext4_ext_correct_indexes() isn't needed here because 3893 * borders are not changed 3894 */ 3895 ext4_ext_try_to_merge(handle, inode, path, ex); 3896 3897 err = ext4_ext_dirty(handle, inode, path + depth); 3898 if (err) 3899 goto err; 3900 } 3901 3902 /* Lets update the extent status tree after conversion */ 3903 if (!(flags & EXT4_EX_NOCACHE)) 3904 ext4_es_insert_extent(inode, le32_to_cpu(ex->ee_block), 3905 ext4_ext_get_actual_len(ex), 3906 ext4_ext_pblock(ex), 3907 ext4_ext_is_unwritten(ex) ? 3908 EXTENT_STATUS_UNWRITTEN : 3909 EXTENT_STATUS_WRITTEN, 3910 false); 3911 3912 err: 3913 if (err) { 3914 ext4_free_ext_path(path); 3915 return ERR_PTR(err); 3916 } 3917 3918 return path; 3919 } 3920 3921 static struct ext4_ext_path * 3922 ext4_convert_unwritten_extents_endio(handle_t *handle, struct inode *inode, 3923 struct ext4_map_blocks *map, 3924 struct ext4_ext_path *path, int flags) 3925 { 3926 struct ext4_extent *ex; 3927 ext4_lblk_t ee_block; 3928 unsigned int ee_len; 3929 int depth; 3930 3931 depth = ext_depth(inode); 3932 ex = path[depth].p_ext; 3933 ee_block = le32_to_cpu(ex->ee_block); 3934 ee_len = ext4_ext_get_actual_len(ex); 3935 3936 ext_debug(inode, "logical block %llu, max_blocks %u\n", 3937 (unsigned long long)ee_block, ee_len); 3938 3939 return ext4_split_convert_extents(handle, inode, map, path, flags, 3940 NULL); 3941 } 3942 3943 static struct ext4_ext_path * 3944 convert_initialized_extent(handle_t *handle, struct inode *inode, 3945 struct ext4_map_blocks *map, 3946 struct ext4_ext_path *path, 3947 int flags, 3948 unsigned int *allocated) 3949 { 3950 struct ext4_extent *ex; 3951 ext4_lblk_t ee_block; 3952 unsigned int ee_len; 3953 int depth; 3954 3955 /* 3956 * Make sure that the extent is no bigger than we support with 3957 * unwritten extent 3958 */ 3959 if (map->m_len > EXT_UNWRITTEN_MAX_LEN) 3960 map->m_len = EXT_UNWRITTEN_MAX_LEN / 2; 3961 3962 depth = ext_depth(inode); 3963 ex = path[depth].p_ext; 3964 ee_block = le32_to_cpu(ex->ee_block); 3965 ee_len = ext4_ext_get_actual_len(ex); 3966 3967 ext_debug(inode, "logical block %llu, max_blocks %u\n", 3968 (unsigned long long)ee_block, ee_len); 3969 3970 path = ext4_split_convert_extents(handle, inode, map, path, flags, 3971 NULL); 3972 if (IS_ERR(path)) 3973 return path; 3974 3975 ext4_ext_show_leaf(inode, path); 3976 3977 ext4_update_inode_fsync_trans(handle, inode, 1); 3978 3979 /* 3980 * The extent might be initialized in case of zeroout. 3981 */ 3982 path = ext4_find_extent(inode, map->m_lblk, path, flags); 3983 if (IS_ERR(path)) 3984 return path; 3985 3986 depth = ext_depth(inode); 3987 ex = path[depth].p_ext; 3988 3989 if (ext4_ext_is_unwritten(ex)) 3990 map->m_flags |= EXT4_MAP_UNWRITTEN; 3991 else 3992 map->m_flags |= EXT4_MAP_MAPPED; 3993 if (*allocated > map->m_len) 3994 *allocated = map->m_len; 3995 map->m_len = *allocated; 3996 return path; 3997 } 3998 3999 static struct ext4_ext_path * 4000 ext4_ext_handle_unwritten_extents(handle_t *handle, struct inode *inode, 4001 struct ext4_map_blocks *map, 4002 struct ext4_ext_path *path, int flags, 4003 unsigned int *allocated, ext4_fsblk_t newblock) 4004 { 4005 int err = 0; 4006 4007 ext_debug(inode, "logical block %llu, max_blocks %u, flags 0x%x, allocated %u\n", 4008 (unsigned long long)map->m_lblk, map->m_len, flags, 4009 *allocated); 4010 ext4_ext_show_leaf(inode, path); 4011 4012 /* 4013 * When writing into unwritten space, we should not fail to 4014 * allocate metadata blocks for the new extent block if needed. 4015 */ 4016 flags |= EXT4_GET_BLOCKS_METADATA_NOFAIL; 4017 4018 trace_ext4_ext_handle_unwritten_extents(inode, map, flags, 4019 *allocated, newblock); 4020 4021 /* IO end_io complete, convert the filled extent to written */ 4022 if (flags & EXT4_GET_BLOCKS_CONVERT) { 4023 path = ext4_convert_unwritten_extents_endio(handle, inode, 4024 map, path, flags); 4025 if (IS_ERR(path)) 4026 return path; 4027 ext4_update_inode_fsync_trans(handle, inode, 1); 4028 goto map_out; 4029 } 4030 /* buffered IO cases */ 4031 /* 4032 * repeat fallocate creation request 4033 * we already have an unwritten extent 4034 */ 4035 if (flags & EXT4_GET_BLOCKS_UNWRIT_EXT) { 4036 map->m_flags |= EXT4_MAP_UNWRITTEN; 4037 goto map_out; 4038 } 4039 4040 /* buffered READ or buffered write_begin() lookup */ 4041 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) { 4042 /* 4043 * We have blocks reserved already. We 4044 * return allocated blocks so that delalloc 4045 * won't do block reservation for us. But 4046 * the buffer head will be unmapped so that 4047 * a read from the block returns 0s. 4048 */ 4049 map->m_flags |= EXT4_MAP_UNWRITTEN; 4050 goto out1; 4051 } 4052 4053 /* 4054 * Default case when (flags & EXT4_GET_BLOCKS_CREATE) == 1. 4055 * For buffered writes, at writepage time, etc. Convert a 4056 * discovered unwritten extent to written. 4057 */ 4058 path = ext4_ext_convert_to_initialized(handle, inode, map, path, 4059 flags, allocated); 4060 if (IS_ERR(path)) 4061 return path; 4062 ext4_update_inode_fsync_trans(handle, inode, 1); 4063 /* 4064 * shouldn't get a 0 allocated when converting an unwritten extent 4065 * unless m_len is 0 (bug) or extent has been corrupted 4066 */ 4067 if (unlikely(*allocated == 0)) { 4068 EXT4_ERROR_INODE(inode, "unexpected allocated == 0, m_len = %u", 4069 map->m_len); 4070 err = -EFSCORRUPTED; 4071 goto errout; 4072 } 4073 4074 map->m_flags |= EXT4_MAP_NEW; 4075 map_out: 4076 map->m_flags |= EXT4_MAP_MAPPED; 4077 out1: 4078 map->m_pblk = newblock; 4079 if (*allocated > map->m_len) 4080 *allocated = map->m_len; 4081 map->m_len = *allocated; 4082 ext4_ext_show_leaf(inode, path); 4083 return path; 4084 4085 errout: 4086 ext4_free_ext_path(path); 4087 return ERR_PTR(err); 4088 } 4089 4090 /* 4091 * get_implied_cluster_alloc - check to see if the requested 4092 * allocation (in the map structure) overlaps with a cluster already 4093 * allocated in an extent. 4094 * @sb The filesystem superblock structure 4095 * @map The requested lblk->pblk mapping 4096 * @ex The extent structure which might contain an implied 4097 * cluster allocation 4098 * 4099 * This function is called by ext4_ext_map_blocks() after we failed to 4100 * find blocks that were already in the inode's extent tree. Hence, 4101 * we know that the beginning of the requested region cannot overlap 4102 * the extent from the inode's extent tree. There are three cases we 4103 * want to catch. The first is this case: 4104 * 4105 * |--- cluster # N--| 4106 * |--- extent ---| |---- requested region ---| 4107 * |==========| 4108 * 4109 * The second case that we need to test for is this one: 4110 * 4111 * |--------- cluster # N ----------------| 4112 * |--- requested region --| |------- extent ----| 4113 * |=======================| 4114 * 4115 * The third case is when the requested region lies between two extents 4116 * within the same cluster: 4117 * |------------- cluster # N-------------| 4118 * |----- ex -----| |---- ex_right ----| 4119 * |------ requested region ------| 4120 * |================| 4121 * 4122 * In each of the above cases, we need to set the map->m_pblk and 4123 * map->m_len so it corresponds to the return the extent labelled as 4124 * "|====|" from cluster #N, since it is already in use for data in 4125 * cluster EXT4_B2C(sbi, map->m_lblk). We will then return 1 to 4126 * signal to ext4_ext_map_blocks() that map->m_pblk should be treated 4127 * as a new "allocated" block region. Otherwise, we will return 0 and 4128 * ext4_ext_map_blocks() will then allocate one or more new clusters 4129 * by calling ext4_mb_new_blocks(). 4130 */ 4131 static int get_implied_cluster_alloc(struct super_block *sb, 4132 struct ext4_map_blocks *map, 4133 struct ext4_extent *ex, 4134 struct ext4_ext_path *path) 4135 { 4136 struct ext4_sb_info *sbi = EXT4_SB(sb); 4137 ext4_lblk_t c_offset = EXT4_LBLK_COFF(sbi, map->m_lblk); 4138 ext4_lblk_t ex_cluster_start, ex_cluster_end; 4139 ext4_lblk_t rr_cluster_start; 4140 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block); 4141 ext4_fsblk_t ee_start = ext4_ext_pblock(ex); 4142 unsigned short ee_len = ext4_ext_get_actual_len(ex); 4143 4144 /* The extent passed in that we are trying to match */ 4145 ex_cluster_start = EXT4_B2C(sbi, ee_block); 4146 ex_cluster_end = EXT4_B2C(sbi, ee_block + ee_len - 1); 4147 4148 /* The requested region passed into ext4_map_blocks() */ 4149 rr_cluster_start = EXT4_B2C(sbi, map->m_lblk); 4150 4151 if ((rr_cluster_start == ex_cluster_end) || 4152 (rr_cluster_start == ex_cluster_start)) { 4153 if (rr_cluster_start == ex_cluster_end) 4154 ee_start += ee_len - 1; 4155 map->m_pblk = EXT4_PBLK_CMASK(sbi, ee_start) + c_offset; 4156 map->m_len = min(map->m_len, 4157 (unsigned) sbi->s_cluster_ratio - c_offset); 4158 /* 4159 * Check for and handle this case: 4160 * 4161 * |--------- cluster # N-------------| 4162 * |------- extent ----| 4163 * |--- requested region ---| 4164 * |===========| 4165 */ 4166 4167 if (map->m_lblk < ee_block) 4168 map->m_len = min(map->m_len, ee_block - map->m_lblk); 4169 4170 /* 4171 * Check for the case where there is already another allocated 4172 * block to the right of 'ex' but before the end of the cluster. 4173 * 4174 * |------------- cluster # N-------------| 4175 * |----- ex -----| |---- ex_right ----| 4176 * |------ requested region ------| 4177 * |================| 4178 */ 4179 if (map->m_lblk > ee_block) { 4180 ext4_lblk_t next = ext4_ext_next_allocated_block(path); 4181 map->m_len = min(map->m_len, next - map->m_lblk); 4182 } 4183 4184 trace_ext4_get_implied_cluster_alloc_exit(sb, map, 1); 4185 return 1; 4186 } 4187 4188 trace_ext4_get_implied_cluster_alloc_exit(sb, map, 0); 4189 return 0; 4190 } 4191 4192 /* 4193 * Determine hole length around the given logical block, first try to 4194 * locate and expand the hole from the given @path, and then adjust it 4195 * if it's partially or completely converted to delayed extents, insert 4196 * it into the extent cache tree if it's indeed a hole, finally return 4197 * the length of the determined extent. 4198 */ 4199 static ext4_lblk_t ext4_ext_determine_insert_hole(struct inode *inode, 4200 struct ext4_ext_path *path, 4201 ext4_lblk_t lblk) 4202 { 4203 ext4_lblk_t hole_start, len; 4204 struct extent_status es; 4205 4206 hole_start = lblk; 4207 len = ext4_ext_find_hole(inode, path, &hole_start); 4208 again: 4209 ext4_es_find_extent_range(inode, &ext4_es_is_delayed, hole_start, 4210 hole_start + len - 1, &es); 4211 if (!es.es_len) 4212 goto insert_hole; 4213 4214 /* 4215 * There's a delalloc extent in the hole, handle it if the delalloc 4216 * extent is in front of, behind and straddle the queried range. 4217 */ 4218 if (lblk >= es.es_lblk + es.es_len) { 4219 /* 4220 * The delalloc extent is in front of the queried range, 4221 * find again from the queried start block. 4222 */ 4223 len -= lblk - hole_start; 4224 hole_start = lblk; 4225 goto again; 4226 } else if (in_range(lblk, es.es_lblk, es.es_len)) { 4227 /* 4228 * The delalloc extent containing lblk, it must have been 4229 * added after ext4_map_blocks() checked the extent status 4230 * tree so we are not holding i_rwsem and delalloc info is 4231 * only stabilized by i_data_sem we are going to release 4232 * soon. Don't modify the extent status tree and report 4233 * extent as a hole, just adjust the length to the delalloc 4234 * extent's after lblk. 4235 */ 4236 len = es.es_lblk + es.es_len - lblk; 4237 return len; 4238 } else { 4239 /* 4240 * The delalloc extent is partially or completely behind 4241 * the queried range, update hole length until the 4242 * beginning of the delalloc extent. 4243 */ 4244 len = min(es.es_lblk - hole_start, len); 4245 } 4246 4247 insert_hole: 4248 /* Put just found gap into cache to speed up subsequent requests */ 4249 ext_debug(inode, " -> %u:%u\n", hole_start, len); 4250 ext4_es_cache_extent(inode, hole_start, len, ~0, EXTENT_STATUS_HOLE); 4251 4252 /* Update hole_len to reflect hole size after lblk */ 4253 if (hole_start != lblk) 4254 len -= lblk - hole_start; 4255 4256 return len; 4257 } 4258 4259 /* 4260 * Block allocation/map/preallocation routine for extents based files 4261 * 4262 * 4263 * Need to be called with 4264 * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block 4265 * (ie, flags is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem) 4266 * 4267 * return > 0, number of blocks already mapped/allocated 4268 * if flags doesn't contain EXT4_GET_BLOCKS_CREATE and these are pre-allocated blocks 4269 * buffer head is unmapped 4270 * otherwise blocks are mapped 4271 * 4272 * return = 0, if plain look up failed (blocks have not been allocated) 4273 * buffer head is unmapped 4274 * 4275 * return < 0, error case. 4276 */ 4277 int ext4_ext_map_blocks(handle_t *handle, struct inode *inode, 4278 struct ext4_map_blocks *map, int flags) 4279 { 4280 struct ext4_ext_path *path = NULL; 4281 struct ext4_extent newex, *ex, ex2; 4282 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 4283 ext4_fsblk_t newblock = 0, pblk; 4284 int err = 0, depth; 4285 unsigned int allocated = 0, offset = 0; 4286 unsigned int allocated_clusters = 0; 4287 struct ext4_allocation_request ar; 4288 ext4_lblk_t cluster_offset; 4289 4290 ext_debug(inode, "blocks %u/%u requested\n", map->m_lblk, map->m_len); 4291 trace_ext4_ext_map_blocks_enter(inode, map->m_lblk, map->m_len, flags); 4292 4293 /* find extent for this block */ 4294 path = ext4_find_extent(inode, map->m_lblk, NULL, flags); 4295 if (IS_ERR(path)) { 4296 err = PTR_ERR(path); 4297 goto out; 4298 } 4299 4300 depth = ext_depth(inode); 4301 4302 /* 4303 * consistent leaf must not be empty; 4304 * this situation is possible, though, _during_ tree modification; 4305 * this is why assert can't be put in ext4_find_extent() 4306 */ 4307 if (unlikely(path[depth].p_ext == NULL && depth != 0)) { 4308 EXT4_ERROR_INODE(inode, "bad extent address " 4309 "lblock: %lu, depth: %d pblock %lld", 4310 (unsigned long) map->m_lblk, depth, 4311 path[depth].p_block); 4312 err = -EFSCORRUPTED; 4313 goto out; 4314 } 4315 4316 ex = path[depth].p_ext; 4317 if (ex) { 4318 ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block); 4319 ext4_fsblk_t ee_start = ext4_ext_pblock(ex); 4320 unsigned short ee_len; 4321 4322 4323 /* 4324 * unwritten extents are treated as holes, except that 4325 * we split out initialized portions during a write. 4326 */ 4327 ee_len = ext4_ext_get_actual_len(ex); 4328 4329 trace_ext4_ext_show_extent(inode, ee_block, ee_start, ee_len); 4330 4331 /* if found extent covers block, simply return it */ 4332 if (in_range(map->m_lblk, ee_block, ee_len)) { 4333 newblock = map->m_lblk - ee_block + ee_start; 4334 /* number of remaining blocks in the extent */ 4335 allocated = ee_len - (map->m_lblk - ee_block); 4336 ext_debug(inode, "%u fit into %u:%d -> %llu\n", 4337 map->m_lblk, ee_block, ee_len, newblock); 4338 4339 /* 4340 * If the extent is initialized check whether the 4341 * caller wants to convert it to unwritten. 4342 */ 4343 if ((!ext4_ext_is_unwritten(ex)) && 4344 (flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN)) { 4345 path = convert_initialized_extent(handle, 4346 inode, map, path, flags, &allocated); 4347 if (IS_ERR(path)) 4348 err = PTR_ERR(path); 4349 goto out; 4350 } else if (!ext4_ext_is_unwritten(ex)) { 4351 map->m_flags |= EXT4_MAP_MAPPED; 4352 map->m_pblk = newblock; 4353 if (allocated > map->m_len) 4354 allocated = map->m_len; 4355 map->m_len = allocated; 4356 ext4_ext_show_leaf(inode, path); 4357 goto out; 4358 } 4359 4360 path = ext4_ext_handle_unwritten_extents( 4361 handle, inode, map, path, flags, 4362 &allocated, newblock); 4363 if (IS_ERR(path)) 4364 err = PTR_ERR(path); 4365 goto out; 4366 } 4367 } 4368 4369 /* 4370 * requested block isn't allocated yet; 4371 * we couldn't try to create block if flags doesn't contain EXT4_GET_BLOCKS_CREATE 4372 */ 4373 if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) { 4374 ext4_lblk_t len; 4375 4376 len = ext4_ext_determine_insert_hole(inode, path, map->m_lblk); 4377 4378 map->m_pblk = 0; 4379 map->m_len = min_t(unsigned int, map->m_len, len); 4380 goto out; 4381 } 4382 4383 /* 4384 * Okay, we need to do block allocation. 4385 */ 4386 newex.ee_block = cpu_to_le32(map->m_lblk); 4387 cluster_offset = EXT4_LBLK_COFF(sbi, map->m_lblk); 4388 4389 /* 4390 * If we are doing bigalloc, check to see if the extent returned 4391 * by ext4_find_extent() implies a cluster we can use. 4392 */ 4393 if (cluster_offset && ex && 4394 get_implied_cluster_alloc(inode->i_sb, map, ex, path)) { 4395 ar.len = allocated = map->m_len; 4396 newblock = map->m_pblk; 4397 goto got_allocated_blocks; 4398 } 4399 4400 /* find neighbour allocated blocks */ 4401 ar.lleft = map->m_lblk; 4402 err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft); 4403 if (err) 4404 goto out; 4405 ar.lright = map->m_lblk; 4406 err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright, 4407 &ex2, flags); 4408 if (err < 0) 4409 goto out; 4410 4411 /* Check if the extent after searching to the right implies a 4412 * cluster we can use. */ 4413 if ((sbi->s_cluster_ratio > 1) && err && 4414 get_implied_cluster_alloc(inode->i_sb, map, &ex2, path)) { 4415 ar.len = allocated = map->m_len; 4416 newblock = map->m_pblk; 4417 err = 0; 4418 goto got_allocated_blocks; 4419 } 4420 4421 /* 4422 * See if request is beyond maximum number of blocks we can have in 4423 * a single extent. For an initialized extent this limit is 4424 * EXT_INIT_MAX_LEN and for an unwritten extent this limit is 4425 * EXT_UNWRITTEN_MAX_LEN. 4426 */ 4427 if (map->m_len > EXT_INIT_MAX_LEN && 4428 !(flags & EXT4_GET_BLOCKS_UNWRIT_EXT)) 4429 map->m_len = EXT_INIT_MAX_LEN; 4430 else if (map->m_len > EXT_UNWRITTEN_MAX_LEN && 4431 (flags & EXT4_GET_BLOCKS_UNWRIT_EXT)) 4432 map->m_len = EXT_UNWRITTEN_MAX_LEN; 4433 4434 /* Check if we can really insert (m_lblk)::(m_lblk + m_len) extent */ 4435 newex.ee_len = cpu_to_le16(map->m_len); 4436 err = ext4_ext_check_overlap(sbi, inode, &newex, path); 4437 if (err) 4438 allocated = ext4_ext_get_actual_len(&newex); 4439 else 4440 allocated = map->m_len; 4441 4442 /* allocate new block */ 4443 ar.inode = inode; 4444 ar.goal = ext4_ext_find_goal(inode, path, map->m_lblk); 4445 ar.logical = map->m_lblk; 4446 /* 4447 * We calculate the offset from the beginning of the cluster 4448 * for the logical block number, since when we allocate a 4449 * physical cluster, the physical block should start at the 4450 * same offset from the beginning of the cluster. This is 4451 * needed so that future calls to get_implied_cluster_alloc() 4452 * work correctly. 4453 */ 4454 offset = EXT4_LBLK_COFF(sbi, map->m_lblk); 4455 ar.len = EXT4_NUM_B2C(sbi, offset+allocated); 4456 ar.goal -= offset; 4457 ar.logical -= offset; 4458 if (S_ISREG(inode->i_mode)) 4459 ar.flags = EXT4_MB_HINT_DATA; 4460 else 4461 /* disable in-core preallocation for non-regular files */ 4462 ar.flags = 0; 4463 if (flags & EXT4_GET_BLOCKS_NO_NORMALIZE) 4464 ar.flags |= EXT4_MB_HINT_NOPREALLOC; 4465 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) 4466 ar.flags |= EXT4_MB_DELALLOC_RESERVED; 4467 if (flags & EXT4_GET_BLOCKS_METADATA_NOFAIL) 4468 ar.flags |= EXT4_MB_USE_RESERVED; 4469 newblock = ext4_mb_new_blocks(handle, &ar, &err); 4470 if (!newblock) 4471 goto out; 4472 allocated_clusters = ar.len; 4473 ar.len = EXT4_C2B(sbi, ar.len) - offset; 4474 ext_debug(inode, "allocate new block: goal %llu, found %llu/%u, requested %u\n", 4475 ar.goal, newblock, ar.len, allocated); 4476 if (ar.len > allocated) 4477 ar.len = allocated; 4478 4479 got_allocated_blocks: 4480 /* try to insert new extent into found leaf and return */ 4481 pblk = newblock + offset; 4482 ext4_ext_store_pblock(&newex, pblk); 4483 newex.ee_len = cpu_to_le16(ar.len); 4484 /* Mark unwritten */ 4485 if (flags & EXT4_GET_BLOCKS_UNWRIT_EXT) { 4486 ext4_ext_mark_unwritten(&newex); 4487 map->m_flags |= EXT4_MAP_UNWRITTEN; 4488 } 4489 4490 path = ext4_ext_insert_extent(handle, inode, path, &newex, flags); 4491 if (IS_ERR(path)) { 4492 err = PTR_ERR(path); 4493 /* 4494 * Gracefully handle out of space conditions. If the filesystem 4495 * is inconsistent, we'll just leak allocated blocks to avoid 4496 * causing even more damage. 4497 */ 4498 if (allocated_clusters && (err == -EDQUOT || err == -ENOSPC)) { 4499 int fb_flags = 0; 4500 /* 4501 * free data blocks we just allocated. 4502 * not a good idea to call discard here directly, 4503 * but otherwise we'd need to call it every free(). 4504 */ 4505 ext4_discard_preallocations(inode); 4506 if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) 4507 fb_flags = EXT4_FREE_BLOCKS_NO_QUOT_UPDATE; 4508 ext4_free_blocks(handle, inode, NULL, newblock, 4509 EXT4_C2B(sbi, allocated_clusters), 4510 fb_flags); 4511 } 4512 goto out; 4513 } 4514 4515 /* 4516 * Cache the extent and update transaction to commit on fdatasync only 4517 * when it is _not_ an unwritten extent. 4518 */ 4519 if ((flags & EXT4_GET_BLOCKS_UNWRIT_EXT) == 0) 4520 ext4_update_inode_fsync_trans(handle, inode, 1); 4521 else 4522 ext4_update_inode_fsync_trans(handle, inode, 0); 4523 4524 map->m_flags |= (EXT4_MAP_NEW | EXT4_MAP_MAPPED); 4525 map->m_pblk = pblk; 4526 map->m_len = ar.len; 4527 allocated = map->m_len; 4528 ext4_ext_show_leaf(inode, path); 4529 out: 4530 /* 4531 * We never use EXT4_GET_BLOCKS_QUERY_LAST_IN_LEAF with CREATE flag. 4532 * So we know that the depth used here is correct, since there was no 4533 * block allocation done if EXT4_GET_BLOCKS_QUERY_LAST_IN_LEAF is set. 4534 * If tomorrow we start using this QUERY flag with CREATE, then we will 4535 * need to re-calculate the depth as it might have changed due to block 4536 * allocation. 4537 */ 4538 if (flags & EXT4_GET_BLOCKS_QUERY_LAST_IN_LEAF) { 4539 WARN_ON_ONCE(flags & EXT4_GET_BLOCKS_CREATE); 4540 if (!err && ex && (ex == EXT_LAST_EXTENT(path[depth].p_hdr))) 4541 map->m_flags |= EXT4_MAP_QUERY_LAST_IN_LEAF; 4542 } 4543 4544 ext4_free_ext_path(path); 4545 4546 trace_ext4_ext_map_blocks_exit(inode, flags, map, 4547 err ? err : allocated); 4548 return err ? err : allocated; 4549 } 4550 4551 int ext4_ext_truncate(handle_t *handle, struct inode *inode) 4552 { 4553 struct super_block *sb = inode->i_sb; 4554 ext4_lblk_t last_block; 4555 int err = 0; 4556 4557 /* 4558 * TODO: optimization is possible here. 4559 * Probably we need not scan at all, 4560 * because page truncation is enough. 4561 */ 4562 4563 /* we have to know where to truncate from in crash case */ 4564 EXT4_I(inode)->i_disksize = inode->i_size; 4565 err = ext4_mark_inode_dirty(handle, inode); 4566 if (err) 4567 return err; 4568 4569 last_block = (inode->i_size + sb->s_blocksize - 1) 4570 >> EXT4_BLOCK_SIZE_BITS(sb); 4571 ext4_es_remove_extent(inode, last_block, EXT_MAX_BLOCKS - last_block); 4572 4573 retry_remove_space: 4574 err = ext4_ext_remove_space(inode, last_block, EXT_MAX_BLOCKS - 1); 4575 if (err == -ENOMEM) { 4576 memalloc_retry_wait(GFP_ATOMIC); 4577 goto retry_remove_space; 4578 } 4579 return err; 4580 } 4581 4582 /* 4583 * Pre-allocate blocks for the range [@offset, @offset + @len). Allocated 4584 * blocks are marked as unwritten by default. If EXT4_GET_BLOCKS_ZERO is 4585 * set, the allocated blocks are zeroed on disk and their extents are 4586 * converted to written state. 4587 * 4588 * When @new_size is nonzero, the caller intends to extend the file, and 4589 * the file size should be updated to the end of the allocated blocks. 4590 * 4591 * Allocation may partially succeed due to some non-fatal issues. In that 4592 * case, i_disksize (and i_size) is advanced up to the successfully 4593 * processed portion of the range. 4594 * 4595 * Return 0 on success, or a negative error code on failure or partial 4596 * failure. 4597 */ 4598 static int ext4_alloc_file_blocks(struct file *file, loff_t offset, loff_t len, 4599 loff_t new_size, int flags) 4600 { 4601 struct inode *inode = file_inode(file); 4602 handle_t *handle; 4603 int ret = 0, ret2 = 0, ret3 = 0; 4604 int retries = 0; 4605 int depth = 0; 4606 ext4_lblk_t len_lblk; 4607 struct ext4_map_blocks map; 4608 unsigned int credits; 4609 loff_t epos = 0, old_size = i_size_read(inode); 4610 unsigned int blkbits = inode->i_blkbits; 4611 bool alloc_zero = false; 4612 bool orphan = false; 4613 4614 BUG_ON(!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)); 4615 map.m_lblk = offset >> blkbits; 4616 map.m_len = len_lblk = EXT4_MAX_BLOCKS(len, offset, blkbits); 4617 /* 4618 * Don't normalize the request if it can fit in one extent so 4619 * that it doesn't get unnecessarily split into multiple 4620 * extents. 4621 */ 4622 if (len_lblk <= EXT_UNWRITTEN_MAX_LEN) 4623 flags |= EXT4_GET_BLOCKS_NO_NORMALIZE; 4624 4625 /* 4626 * Do the actual write zero during a running journal transaction 4627 * costs a lot. First allocate an unwritten extent and then 4628 * convert it to written after zeroing it out. 4629 */ 4630 if (flags & EXT4_GET_BLOCKS_ZERO) { 4631 flags &= ~EXT4_GET_BLOCKS_ZERO; 4632 flags |= EXT4_GET_BLOCKS_UNWRIT_EXT; 4633 alloc_zero = true; 4634 } 4635 4636 /* 4637 * credits to insert 1 extent into extent tree 4638 */ 4639 credits = ext4_chunk_trans_blocks(inode, len_lblk); 4640 depth = ext_depth(inode); 4641 4642 /* Zero to the end of the block containing i_size */ 4643 if (new_size > old_size) { 4644 ret = ext4_block_zero_eof(inode, old_size, LLONG_MAX); 4645 if (ret) 4646 return ret; 4647 } 4648 4649 retry: 4650 while (len_lblk) { 4651 /* 4652 * Recalculate credits when extent tree depth changes. 4653 */ 4654 if (depth != ext_depth(inode)) { 4655 credits = ext4_chunk_trans_blocks(inode, len_lblk); 4656 depth = ext_depth(inode); 4657 } 4658 4659 handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS, 4660 credits); 4661 if (IS_ERR(handle)) { 4662 ret = PTR_ERR(handle); 4663 break; 4664 } 4665 ret = ext4_map_blocks(handle, inode, &map, flags); 4666 if (ret <= 0) { 4667 ext4_debug("inode #%llu: block %u: len %u: " 4668 "ext4_ext_map_blocks returned %d", 4669 inode->i_ino, map.m_lblk, 4670 map.m_len, ret); 4671 ext4_mark_inode_dirty(handle, inode); 4672 ext4_journal_stop(handle); 4673 break; 4674 } 4675 ext4_update_inode_fsync_trans(handle, inode, 1); 4676 ret = ext4_journal_stop(handle); 4677 if (unlikely(ret)) 4678 break; 4679 4680 /* 4681 * allow a full retry cycle for any remaining allocations 4682 */ 4683 retries = 0; 4684 4685 if (alloc_zero && 4686 (map.m_flags & (EXT4_MAP_MAPPED | EXT4_MAP_UNWRITTEN))) { 4687 ext4_lblk_t converted; 4688 4689 WARN_ON_ONCE(map.m_lblk + map.m_len > 4690 EXT4_B_TO_LBLK(inode, new_size ?: old_size)); 4691 4692 ret = ext4_issue_zeroout(inode, map.m_lblk, map.m_pblk, 4693 map.m_len); 4694 if (unlikely(ret)) 4695 break; 4696 4697 handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS, 4698 credits); 4699 if (IS_ERR(handle)) { 4700 ret = PTR_ERR(handle); 4701 break; 4702 } 4703 4704 ret = ext4_convert_unwritten_extents(handle, 4705 inode, (loff_t)map.m_lblk << blkbits, 4706 (loff_t)map.m_len << blkbits, 4707 &converted); 4708 if (ret) 4709 map.m_len = converted; 4710 4711 /* 4712 * If blocks beyond i_disksize are converted, add 4713 * the inode to the orphan list and advance the epos. 4714 */ 4715 if (new_size && converted) { 4716 ret2 = ext4_orphan_add(handle, inode); 4717 ret = ret ? ret : ret2; 4718 orphan = true; 4719 } 4720 4721 ret3 = ext4_journal_stop(handle); 4722 ret = ret ? ret : ret3; 4723 } 4724 4725 map.m_lblk += map.m_len; 4726 map.m_len = len_lblk = len_lblk - map.m_len; 4727 epos = EXT4_LBLK_TO_B(inode, map.m_lblk); 4728 if (ret) 4729 break; 4730 } 4731 4732 if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) 4733 goto retry; 4734 4735 if (!epos || !new_size) 4736 return ret; 4737 4738 /* 4739 * Allocate blocks, update the file size to match the size of the 4740 * already successfully allocated blocks. 4741 */ 4742 if (epos > new_size) 4743 epos = new_size; 4744 4745 handle = ext4_journal_start(inode, EXT4_HT_MISC, 2); 4746 if (IS_ERR(handle)) { 4747 /* 4748 * The conversion has successfully completed. Not much to 4749 * do with the error here so just cleanup the orphan list 4750 * and hope for the best. 4751 */ 4752 if (orphan && inode->i_nlink) 4753 ext4_orphan_del(NULL, inode); 4754 ret2 = PTR_ERR(handle); 4755 goto out; 4756 } 4757 4758 ext4_update_inode_size(inode, epos); 4759 if (orphan && inode->i_nlink) 4760 ext4_orphan_del(handle, inode); 4761 4762 ret2 = ext4_mark_inode_dirty(handle, inode); 4763 ext4_update_inode_fsync_trans(handle, inode, 1); 4764 ret3 = ext4_journal_stop(handle); 4765 ret2 = ret3 ? ret3 : ret2; 4766 4767 if (epos > old_size) 4768 pagecache_isize_extended(inode, old_size, epos); 4769 out: 4770 if (ret2) 4771 ext4_std_error(inode->i_sb, ret2); 4772 4773 return ret ? ret : ret2; 4774 } 4775 4776 static int ext4_collapse_range(struct file *file, loff_t offset, loff_t len); 4777 4778 static int ext4_insert_range(struct file *file, loff_t offset, loff_t len); 4779 4780 static long ext4_zero_range(struct file *file, loff_t offset, 4781 loff_t len, int mode) 4782 { 4783 struct inode *inode = file_inode(file); 4784 handle_t *handle = NULL; 4785 loff_t align_start, align_end, new_size = 0; 4786 loff_t end = offset + len; 4787 unsigned int blocksize = i_blocksize(inode); 4788 unsigned int partial_zeroed = 0; 4789 int ret, flags; 4790 4791 trace_ext4_zero_range(inode, offset, len, mode); 4792 WARN_ON_ONCE(!inode_is_locked(inode)); 4793 4794 /* Indirect files do not support unwritten extents */ 4795 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) 4796 return -EOPNOTSUPP; 4797 4798 if (!(mode & FALLOC_FL_KEEP_SIZE) && 4799 (end > inode->i_size || end > EXT4_I(inode)->i_disksize)) { 4800 new_size = end; 4801 ret = inode_newsize_ok(inode, new_size); 4802 if (ret) 4803 return ret; 4804 } 4805 4806 flags = EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT; 4807 /* 4808 * Preallocate the range including the unaligned edges, and zero 4809 * out partial blocks if they already contain data. 4810 */ 4811 if (!IS_ALIGNED(offset | end, blocksize)) { 4812 ret = ext4_alloc_file_blocks(file, offset, len, new_size, 4813 flags); 4814 if (!ret) 4815 ret = ext4_zero_partial_blocks(inode, offset, len, 4816 &partial_zeroed); 4817 if (ret) 4818 return ret; 4819 } 4820 4821 ret = ext4_update_disksize_before_punch(inode, offset, len); 4822 if (ret) 4823 return ret; 4824 4825 /* Now release the pages and zero block aligned part of pages */ 4826 ret = ext4_truncate_page_cache_block_range(inode, offset, end); 4827 if (ret) 4828 return ret; 4829 4830 /* Zero range excluding the unaligned edges */ 4831 align_start = round_up(offset, blocksize); 4832 align_end = round_down(end, blocksize); 4833 4834 /* 4835 * In WRITE_ZEROES mode, edges that were not partial-zeroed (clean 4836 * unwritten or hole) must be allocated and zeroed as whole blocks. 4837 * Expand the aligned range outward to cover them. 4838 */ 4839 if (mode & FALLOC_FL_WRITE_ZEROES) { 4840 if (!IS_ALIGNED(offset, blocksize) && 4841 !(partial_zeroed & EXT4_PARTIAL_ZERO_START)) 4842 align_start = round_down(offset, blocksize); 4843 if (!IS_ALIGNED(end, blocksize) && 4844 !(partial_zeroed & EXT4_PARTIAL_ZERO_END)) 4845 align_end = round_up(end, blocksize); 4846 } 4847 4848 if (align_end > align_start) { 4849 if (mode & FALLOC_FL_WRITE_ZEROES) 4850 flags = EXT4_GET_BLOCKS_CREATE_ZERO | EXT4_EX_NOCACHE; 4851 else 4852 flags |= (EXT4_GET_BLOCKS_CONVERT_UNWRITTEN | 4853 EXT4_EX_NOCACHE); 4854 ret = ext4_alloc_file_blocks(file, align_start, 4855 align_end - align_start, new_size, 4856 flags); 4857 if (ret) 4858 return ret; 4859 } 4860 /* Finish zeroing out if it doesn't contain partial block */ 4861 if (IS_ALIGNED(offset | end, blocksize)) 4862 return ret; 4863 4864 /* 4865 * In FALLOC_FL_WRITE_ZEROES mode, edges that have been partially 4866 * zeroed must be written back to ensure the entire zeroed range 4867 * is converted to the written state. In SYNC mode, writeback is 4868 * also required to persist the zeroed data to disk. 4869 */ 4870 if (partial_zeroed && 4871 ((mode & FALLOC_FL_WRITE_ZEROES) || 4872 (file->f_flags & O_SYNC) || IS_SYNC(inode))) { 4873 ret = filemap_write_and_wait_range(inode->i_mapping, offset, 4874 end - 1); 4875 if (ret) 4876 return ret; 4877 } 4878 4879 handle = ext4_journal_start(inode, EXT4_HT_MISC, 1); 4880 if (IS_ERR(handle)) { 4881 ret = PTR_ERR(handle); 4882 ext4_std_error(inode->i_sb, ret); 4883 return ret; 4884 } 4885 4886 if (new_size) 4887 ext4_update_inode_size(inode, new_size); 4888 ret = ext4_mark_inode_dirty(handle, inode); 4889 if (unlikely(ret)) 4890 goto out_handle; 4891 4892 ext4_update_inode_fsync_trans(handle, inode, 1); 4893 if ((file->f_flags & O_SYNC) || IS_SYNC(inode)) 4894 ext4_handle_sync(handle); 4895 4896 out_handle: 4897 ext4_journal_stop(handle); 4898 return ret; 4899 } 4900 4901 static long ext4_do_fallocate(struct file *file, loff_t offset, 4902 loff_t len, int mode) 4903 { 4904 struct inode *inode = file_inode(file); 4905 loff_t end = offset + len; 4906 loff_t new_size = 0; 4907 int ret; 4908 4909 trace_ext4_fallocate_enter(inode, offset, len, mode); 4910 WARN_ON_ONCE(!inode_is_locked(inode)); 4911 4912 /* We only support preallocation for extent-based files only. */ 4913 if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) { 4914 ret = -EOPNOTSUPP; 4915 goto out; 4916 } 4917 4918 if (!(mode & FALLOC_FL_KEEP_SIZE) && 4919 (end > inode->i_size || end > EXT4_I(inode)->i_disksize)) { 4920 new_size = end; 4921 ret = inode_newsize_ok(inode, new_size); 4922 if (ret) 4923 goto out; 4924 } 4925 4926 ret = ext4_alloc_file_blocks(file, offset, len, new_size, 4927 EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT); 4928 if (ret) 4929 goto out; 4930 4931 if (((file->f_flags & O_SYNC) || IS_SYNC(inode)) && 4932 EXT4_SB(inode->i_sb)->s_journal) { 4933 ret = ext4_fc_commit(EXT4_SB(inode->i_sb)->s_journal, 4934 EXT4_I(inode)->i_sync_tid); 4935 } 4936 out: 4937 trace_ext4_fallocate_exit(inode, offset, 4938 EXT4_MAX_BLOCKS(len, offset, inode->i_blkbits), ret); 4939 return ret; 4940 } 4941 4942 /* 4943 * preallocate space for a file. This implements ext4's fallocate file 4944 * operation, which gets called from sys_fallocate system call. 4945 * For block-mapped files, posix_fallocate should fall back to the method 4946 * of writing zeroes to the required new blocks (the same behavior which is 4947 * expected for file systems which do not support fallocate() system call). 4948 */ 4949 long ext4_fallocate(struct file *file, int mode, loff_t offset, loff_t len) 4950 { 4951 struct inode *inode = file_inode(file); 4952 struct address_space *mapping = file->f_mapping; 4953 int ret; 4954 4955 /* 4956 * Encrypted inodes can't handle collapse range or insert 4957 * range since we would need to re-encrypt blocks with a 4958 * different IV or XTS tweak (which are based on the logical 4959 * block number). 4960 */ 4961 if (IS_ENCRYPTED(inode) && 4962 (mode & (FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE))) 4963 return -EOPNOTSUPP; 4964 /* 4965 * Don't allow writing zeroes if the underlying device does not 4966 * enable the unmap write zeroes operation. 4967 */ 4968 if ((mode & FALLOC_FL_WRITE_ZEROES) && 4969 !bdev_write_zeroes_unmap_sectors(inode->i_sb->s_bdev)) 4970 return -EOPNOTSUPP; 4971 4972 /* Return error if mode is not supported */ 4973 if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE | 4974 FALLOC_FL_ZERO_RANGE | FALLOC_FL_COLLAPSE_RANGE | 4975 FALLOC_FL_INSERT_RANGE | FALLOC_FL_WRITE_ZEROES)) 4976 return -EOPNOTSUPP; 4977 4978 inode_lock(inode); 4979 ret = ext4_convert_inline_data(inode); 4980 if (ret) 4981 goto out_inode_lock; 4982 4983 /* Wait all existing dio workers, newcomers will block on i_rwsem */ 4984 inode_dio_wait(inode); 4985 4986 ret = file_modified(file); 4987 if (ret) 4988 goto out_inode_lock; 4989 4990 if ((mode & FALLOC_FL_MODE_MASK) == FALLOC_FL_ALLOCATE_RANGE) { 4991 ret = ext4_do_fallocate(file, offset, len, mode); 4992 goto out_inode_lock; 4993 } 4994 4995 /* 4996 * Follow-up operations will drop page cache, hold invalidate lock 4997 * to prevent page faults from reinstantiating pages we have 4998 * released from page cache. 4999 */ 5000 filemap_invalidate_lock(mapping); 5001 5002 ret = ext4_break_layouts(inode); 5003 if (ret) 5004 goto out_invalidate_lock; 5005 5006 switch (mode & FALLOC_FL_MODE_MASK) { 5007 case FALLOC_FL_PUNCH_HOLE: 5008 ret = ext4_punch_hole(file, offset, len); 5009 break; 5010 case FALLOC_FL_COLLAPSE_RANGE: 5011 ret = ext4_collapse_range(file, offset, len); 5012 break; 5013 case FALLOC_FL_INSERT_RANGE: 5014 ret = ext4_insert_range(file, offset, len); 5015 break; 5016 case FALLOC_FL_ZERO_RANGE: 5017 case FALLOC_FL_WRITE_ZEROES: 5018 ret = ext4_zero_range(file, offset, len, mode); 5019 break; 5020 default: 5021 ret = -EOPNOTSUPP; 5022 } 5023 5024 out_invalidate_lock: 5025 filemap_invalidate_unlock(mapping); 5026 out_inode_lock: 5027 inode_unlock(inode); 5028 return ret; 5029 } 5030 5031 /* 5032 * This function converts a range of blocks to written extents. The caller of 5033 * this function will pass the start offset and the size. all unwritten extents 5034 * within this range will be converted to written extents. 5035 * 5036 * This function is called from the direct IO end io call back function for 5037 * atomic writes, to convert the unwritten extents after IO is completed. 5038 * 5039 * Note that the requirement for atomic writes is that all conversion should 5040 * happen atomically in a single fs journal transaction. We mainly only allocate 5041 * unwritten extents either on a hole on a pre-exiting unwritten extent range in 5042 * ext4_map_blocks_atomic_write(). The only case where we can have multiple 5043 * unwritten extents in a range [offset, offset+len) is when there is a split 5044 * unwritten extent between two leaf nodes which was cached in extent status 5045 * cache during ext4_iomap_alloc() time. That will allow 5046 * ext4_map_blocks_atomic_write() to return the unwritten extent range w/o going 5047 * into the slow path. That means we might need a loop for conversion of this 5048 * unwritten extent split across leaf block within a single journal transaction. 5049 * Split extents across leaf nodes is a rare case, but let's still handle that 5050 * to meet the requirements of multi-fsblock atomic writes. 5051 * 5052 * Returns 0 on success. 5053 */ 5054 int ext4_convert_unwritten_extents_atomic(handle_t *handle, struct inode *inode, 5055 loff_t offset, ssize_t len) 5056 { 5057 unsigned int max_blocks; 5058 int ret = 0, ret2 = 0, ret3 = 0; 5059 struct ext4_map_blocks map; 5060 unsigned int blkbits = inode->i_blkbits; 5061 unsigned int credits = 0; 5062 int flags = EXT4_GET_BLOCKS_IO_CONVERT_EXT | EXT4_EX_NOCACHE; 5063 5064 map.m_lblk = offset >> blkbits; 5065 max_blocks = EXT4_MAX_BLOCKS(len, offset, blkbits); 5066 5067 if (!handle) { 5068 /* 5069 * TODO: An optimization can be added later by having an extent 5070 * status flag e.g. EXTENT_STATUS_SPLIT_LEAF. If we query that 5071 * it can tell if the extent in the cache is a split extent. 5072 * But for now let's assume pextents as 2 always. 5073 */ 5074 credits = ext4_meta_trans_blocks(inode, max_blocks, 2, 0); 5075 } 5076 5077 if (credits) { 5078 handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS, credits); 5079 if (IS_ERR(handle)) { 5080 ret = PTR_ERR(handle); 5081 return ret; 5082 } 5083 } 5084 5085 while (ret >= 0 && ret < max_blocks) { 5086 map.m_lblk += ret; 5087 map.m_len = (max_blocks -= ret); 5088 ret = ext4_map_blocks(handle, inode, &map, flags); 5089 if (ret != max_blocks) 5090 ext4_msg(inode->i_sb, KERN_INFO, 5091 "inode #%llu: block %u: len %u: " 5092 "split block mapping found for atomic write, " 5093 "ret = %d", 5094 inode->i_ino, map.m_lblk, 5095 map.m_len, ret); 5096 if (ret <= 0) 5097 break; 5098 } 5099 5100 ret2 = ext4_mark_inode_dirty(handle, inode); 5101 5102 if (credits) { 5103 ret3 = ext4_journal_stop(handle); 5104 if (unlikely(ret3)) 5105 ret2 = ret3; 5106 } 5107 5108 if (ret <= 0 || ret2) 5109 ext4_warning(inode->i_sb, 5110 "inode #%llu: block %u: len %u: " 5111 "returned %d or %d", 5112 inode->i_ino, map.m_lblk, 5113 map.m_len, ret, ret2); 5114 5115 return ret > 0 ? ret2 : ret; 5116 } 5117 5118 /* 5119 * This function convert a range of blocks to written extents 5120 * The caller of this function will pass the start offset and the size. 5121 * all unwritten extents within this range will be converted to 5122 * written extents. 5123 * 5124 * This function is called from the direct/buffered I/O end io call back 5125 * function and FALLOC_FL_WRITE_ZEROES, to convert the fallocated 5126 * unwritten extents after data I/O is completed. 5127 * 5128 * Returns 0 on full success, or a negative error code on partial 5129 * success or failure. The number of blocks converted is returned via 5130 * @converted. 5131 */ 5132 int ext4_convert_unwritten_extents(handle_t *handle, struct inode *inode, 5133 loff_t offset, ssize_t len, 5134 ext4_lblk_t *converted) 5135 { 5136 ext4_lblk_t max_blocks, conv_blocks = 0; 5137 int ret = 0, ret2 = 0, ret3 = 0; 5138 struct ext4_map_blocks map; 5139 unsigned int blkbits = inode->i_blkbits; 5140 unsigned int credits = 0; 5141 5142 map.m_lblk = offset >> blkbits; 5143 map.m_len = max_blocks = EXT4_MAX_BLOCKS(len, offset, blkbits); 5144 5145 if (!handle) { 5146 /* 5147 * credits to insert 1 extent into extent tree 5148 */ 5149 credits = ext4_chunk_trans_blocks(inode, max_blocks); 5150 } 5151 5152 while (max_blocks) { 5153 if (credits) { 5154 handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS, 5155 credits); 5156 if (IS_ERR(handle)) { 5157 ret = PTR_ERR(handle); 5158 break; 5159 } 5160 } 5161 /* 5162 * Do not cache any unrelated extents, as it does not hold the 5163 * i_rwsem or invalidate_lock, which could corrupt the extent 5164 * status tree. 5165 */ 5166 ret = ext4_map_blocks(handle, inode, &map, 5167 EXT4_GET_BLOCKS_IO_CONVERT_EXT | 5168 EXT4_EX_NOCACHE); 5169 if (ret <= 0) { 5170 ext4_warning(inode->i_sb, 5171 "inode #%llu: block %u: len %u: ext4_map_blocks returned %d", 5172 inode->i_ino, map.m_lblk, map.m_len, ret); 5173 if (unlikely(ret == 0)) 5174 ret = -EINVAL; 5175 } else { 5176 conv_blocks += map.m_len; 5177 } 5178 5179 ret2 = ext4_mark_inode_dirty(handle, inode); 5180 if (credits) { 5181 ret3 = ext4_journal_stop(handle); 5182 if (unlikely(ret3)) 5183 ret2 = ret3; 5184 } 5185 ret = ret < 0 ? ret : ret2; 5186 if (ret) 5187 break; 5188 5189 map.m_lblk += map.m_len; 5190 map.m_len = (max_blocks -= map.m_len); 5191 } 5192 /* Converted some or all blocks successfully? */ 5193 if (converted) 5194 *converted = conv_blocks; 5195 5196 return ret; 5197 } 5198 5199 int ext4_convert_unwritten_io_end_vec(handle_t *handle, ext4_io_end_t *io_end) 5200 { 5201 int ret = 0, err = 0; 5202 struct ext4_io_end_vec *io_end_vec; 5203 5204 /* 5205 * This is somewhat ugly but the idea is clear: When transaction is 5206 * reserved, everything goes into it. Otherwise we rather start several 5207 * smaller transactions for conversion of each extent separately. 5208 */ 5209 if (handle) { 5210 handle = ext4_journal_start_reserved(handle, 5211 EXT4_HT_EXT_CONVERT); 5212 if (IS_ERR(handle)) 5213 return PTR_ERR(handle); 5214 } 5215 5216 list_for_each_entry(io_end_vec, &io_end->list_vec, list) { 5217 ret = ext4_convert_unwritten_extents(handle, io_end->inode, 5218 io_end_vec->offset, 5219 io_end_vec->size, NULL); 5220 if (ret) 5221 break; 5222 } 5223 5224 if (handle) 5225 err = ext4_journal_stop(handle); 5226 5227 return ret < 0 ? ret : err; 5228 } 5229 5230 static int ext4_iomap_xattr_fiemap(struct inode *inode, struct iomap *iomap) 5231 { 5232 __u64 physical = 0; 5233 __u64 length = 0; 5234 int blockbits = inode->i_sb->s_blocksize_bits; 5235 int error = 0; 5236 u16 iomap_type; 5237 5238 /* in-inode? */ 5239 if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) { 5240 struct ext4_iloc iloc; 5241 int offset; /* offset of xattr in inode */ 5242 5243 error = ext4_get_inode_loc(inode, &iloc); 5244 if (error) 5245 return error; 5246 physical = (__u64)iloc.bh->b_blocknr << blockbits; 5247 offset = EXT4_GOOD_OLD_INODE_SIZE + 5248 EXT4_I(inode)->i_extra_isize; 5249 physical += offset; 5250 length = EXT4_SB(inode->i_sb)->s_inode_size - offset; 5251 brelse(iloc.bh); 5252 iomap_type = IOMAP_INLINE; 5253 } else if (EXT4_I(inode)->i_file_acl) { /* external block */ 5254 physical = (__u64)EXT4_I(inode)->i_file_acl << blockbits; 5255 length = inode->i_sb->s_blocksize; 5256 iomap_type = IOMAP_MAPPED; 5257 } else { 5258 /* no in-inode or external block for xattr, so return -ENOENT */ 5259 error = -ENOENT; 5260 goto out; 5261 } 5262 5263 iomap->addr = physical; 5264 iomap->offset = 0; 5265 iomap->length = length; 5266 iomap->type = iomap_type; 5267 iomap->flags = 0; 5268 out: 5269 return error; 5270 } 5271 5272 static int ext4_iomap_xattr_begin(struct inode *inode, loff_t offset, 5273 loff_t length, unsigned flags, 5274 struct iomap *iomap, struct iomap *srcmap) 5275 { 5276 int error; 5277 5278 error = ext4_iomap_xattr_fiemap(inode, iomap); 5279 if (error == 0 && (offset >= iomap->length)) 5280 error = -ENOENT; 5281 return error; 5282 } 5283 5284 static DEFINE_IOMAP_ITER_NEXT(ext4_iomap_xattr_next, ext4_iomap_xattr_begin); 5285 5286 static const struct iomap_ops ext4_iomap_xattr_ops = { 5287 .iomap_next = ext4_iomap_xattr_next, 5288 }; 5289 5290 static int ext4_fiemap_check_ranges(struct inode *inode, u64 start, u64 *len) 5291 { 5292 u64 maxbytes = ext4_get_maxbytes(inode); 5293 5294 if (*len == 0) 5295 return -EINVAL; 5296 if (start > maxbytes) 5297 return -EFBIG; 5298 5299 /* 5300 * Shrink request scope to what the fs can actually handle. 5301 */ 5302 if (*len > maxbytes || (maxbytes - *len) < start) 5303 *len = maxbytes - start; 5304 return 0; 5305 } 5306 5307 int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, 5308 u64 start, u64 len) 5309 { 5310 int error = 0; 5311 5312 inode_lock_shared(inode); 5313 if (fieinfo->fi_flags & FIEMAP_FLAG_CACHE) { 5314 error = ext4_ext_precache(inode); 5315 if (error) 5316 goto unlock; 5317 fieinfo->fi_flags &= ~FIEMAP_FLAG_CACHE; 5318 } 5319 5320 /* 5321 * For bitmap files the maximum size limit could be smaller than 5322 * s_maxbytes, so check len here manually instead of just relying on the 5323 * generic check. 5324 */ 5325 error = ext4_fiemap_check_ranges(inode, start, &len); 5326 if (error) 5327 goto unlock; 5328 5329 if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) { 5330 fieinfo->fi_flags &= ~FIEMAP_FLAG_XATTR; 5331 error = iomap_fiemap(inode, fieinfo, start, len, 5332 &ext4_iomap_xattr_ops); 5333 } else { 5334 error = iomap_fiemap(inode, fieinfo, start, len, 5335 &ext4_iomap_report_ops); 5336 } 5337 unlock: 5338 inode_unlock_shared(inode); 5339 return error; 5340 } 5341 5342 int ext4_get_es_cache(struct inode *inode, struct fiemap_extent_info *fieinfo, 5343 __u64 start, __u64 len) 5344 { 5345 ext4_lblk_t start_blk, len_blks; 5346 __u64 last_blk; 5347 int error = 0; 5348 5349 if (ext4_has_inline_data(inode)) { 5350 int has_inline; 5351 5352 down_read(&EXT4_I(inode)->xattr_sem); 5353 has_inline = ext4_has_inline_data(inode); 5354 up_read(&EXT4_I(inode)->xattr_sem); 5355 if (has_inline) 5356 return 0; 5357 } 5358 5359 if (fieinfo->fi_flags & FIEMAP_FLAG_CACHE) { 5360 inode_lock_shared(inode); 5361 error = ext4_ext_precache(inode); 5362 inode_unlock_shared(inode); 5363 if (error) 5364 return error; 5365 fieinfo->fi_flags &= ~FIEMAP_FLAG_CACHE; 5366 } 5367 5368 error = fiemap_prep(inode, fieinfo, start, &len, 0); 5369 if (error) 5370 return error; 5371 5372 error = ext4_fiemap_check_ranges(inode, start, &len); 5373 if (error) 5374 return error; 5375 5376 start_blk = start >> inode->i_sb->s_blocksize_bits; 5377 last_blk = (start + len - 1) >> inode->i_sb->s_blocksize_bits; 5378 if (last_blk >= EXT_MAX_BLOCKS) 5379 last_blk = EXT_MAX_BLOCKS-1; 5380 len_blks = ((ext4_lblk_t) last_blk) - start_blk + 1; 5381 5382 /* 5383 * Walk the extent tree gathering extent information 5384 * and pushing extents back to the user. 5385 */ 5386 return ext4_fill_es_cache_info(inode, start_blk, len_blks, fieinfo); 5387 } 5388 5389 /* 5390 * ext4_ext_shift_path_extents: 5391 * Shift the extents of a path structure lying between path[depth].p_ext 5392 * and EXT_LAST_EXTENT(path[depth].p_hdr), by @shift blocks. @SHIFT tells 5393 * if it is right shift or left shift operation. 5394 */ 5395 static int 5396 ext4_ext_shift_path_extents(struct ext4_ext_path *path, ext4_lblk_t shift, 5397 struct inode *inode, handle_t *handle, 5398 enum SHIFT_DIRECTION SHIFT) 5399 { 5400 int depth, err = 0; 5401 struct ext4_extent *ex_start, *ex_last; 5402 bool update = false; 5403 int credits, restart_credits; 5404 depth = path->p_depth; 5405 5406 while (depth >= 0) { 5407 if (depth == path->p_depth) { 5408 ex_start = path[depth].p_ext; 5409 if (!ex_start) 5410 return -EFSCORRUPTED; 5411 5412 ex_last = EXT_LAST_EXTENT(path[depth].p_hdr); 5413 /* leaf + sb + inode */ 5414 credits = 3; 5415 if (ex_start == EXT_FIRST_EXTENT(path[depth].p_hdr)) { 5416 update = true; 5417 /* extent tree + sb + inode */ 5418 credits = depth + 2; 5419 } 5420 5421 restart_credits = ext4_chunk_trans_extent(inode, 0); 5422 err = ext4_datasem_ensure_credits(handle, inode, credits, 5423 restart_credits, 0); 5424 if (err) { 5425 if (err > 0) 5426 err = -EAGAIN; 5427 goto out; 5428 } 5429 5430 err = ext4_ext_get_access(handle, inode, path + depth); 5431 if (err) 5432 goto out; 5433 5434 while (ex_start <= ex_last) { 5435 if (SHIFT == SHIFT_LEFT) { 5436 le32_add_cpu(&ex_start->ee_block, 5437 -shift); 5438 /* Try to merge to the left. */ 5439 if ((ex_start > 5440 EXT_FIRST_EXTENT(path[depth].p_hdr)) 5441 && 5442 ext4_ext_try_to_merge_right(inode, 5443 path, ex_start - 1)) 5444 ex_last--; 5445 else 5446 ex_start++; 5447 } else { 5448 le32_add_cpu(&ex_last->ee_block, shift); 5449 ext4_ext_try_to_merge_right(inode, path, 5450 ex_last); 5451 ex_last--; 5452 } 5453 } 5454 err = ext4_ext_dirty(handle, inode, path + depth); 5455 if (err) 5456 goto out; 5457 5458 if (--depth < 0 || !update) 5459 break; 5460 } 5461 5462 /* Update index too */ 5463 err = ext4_ext_get_access(handle, inode, path + depth); 5464 if (err) 5465 goto out; 5466 5467 if (SHIFT == SHIFT_LEFT) 5468 le32_add_cpu(&path[depth].p_idx->ei_block, -shift); 5469 else 5470 le32_add_cpu(&path[depth].p_idx->ei_block, shift); 5471 err = ext4_ext_dirty(handle, inode, path + depth); 5472 if (err) 5473 goto out; 5474 5475 /* we are done if current index is not a starting index */ 5476 if (path[depth].p_idx != EXT_FIRST_INDEX(path[depth].p_hdr)) 5477 break; 5478 5479 depth--; 5480 } 5481 5482 out: 5483 return err; 5484 } 5485 5486 /* 5487 * ext4_ext_shift_extents: 5488 * All the extents which lies in the range from @start to the last allocated 5489 * block for the @inode are shifted either towards left or right (depending 5490 * upon @SHIFT) by @shift blocks. 5491 * On success, 0 is returned, error otherwise. 5492 */ 5493 static int 5494 ext4_ext_shift_extents(struct inode *inode, handle_t *handle, 5495 ext4_lblk_t start, ext4_lblk_t shift, 5496 enum SHIFT_DIRECTION SHIFT) 5497 { 5498 struct ext4_ext_path *path; 5499 int ret = 0, depth; 5500 struct ext4_extent *extent; 5501 ext4_lblk_t stop, *iterator, ex_start, ex_end; 5502 ext4_lblk_t tmp = EXT_MAX_BLOCKS; 5503 5504 /* Let path point to the last extent */ 5505 path = ext4_find_extent(inode, EXT_MAX_BLOCKS - 1, NULL, 5506 EXT4_EX_NOCACHE); 5507 if (IS_ERR(path)) 5508 return PTR_ERR(path); 5509 5510 depth = path->p_depth; 5511 extent = path[depth].p_ext; 5512 if (!extent) 5513 goto out; 5514 5515 stop = le32_to_cpu(extent->ee_block); 5516 5517 /* 5518 * For left shifts, make sure the hole on the left is big enough to 5519 * accommodate the shift. For right shifts, make sure the last extent 5520 * won't be shifted beyond EXT_MAX_BLOCKS. 5521 */ 5522 if (SHIFT == SHIFT_LEFT) { 5523 path = ext4_find_extent(inode, start - 1, path, 5524 EXT4_EX_NOCACHE); 5525 if (IS_ERR(path)) 5526 return PTR_ERR(path); 5527 depth = path->p_depth; 5528 extent = path[depth].p_ext; 5529 if (extent) { 5530 ex_start = le32_to_cpu(extent->ee_block); 5531 ex_end = le32_to_cpu(extent->ee_block) + 5532 ext4_ext_get_actual_len(extent); 5533 } else { 5534 ex_start = 0; 5535 ex_end = 0; 5536 } 5537 5538 if ((start == ex_start && shift > ex_start) || 5539 (shift > start - ex_end)) { 5540 ret = -EINVAL; 5541 goto out; 5542 } 5543 } else { 5544 if (shift > EXT_MAX_BLOCKS - 5545 (stop + ext4_ext_get_actual_len(extent))) { 5546 ret = -EINVAL; 5547 goto out; 5548 } 5549 } 5550 5551 /* 5552 * In case of left shift, iterator points to start and it is increased 5553 * till we reach stop. In case of right shift, iterator points to stop 5554 * and it is decreased till we reach start. 5555 */ 5556 again: 5557 ret = 0; 5558 if (SHIFT == SHIFT_LEFT) 5559 iterator = &start; 5560 else 5561 iterator = &stop; 5562 5563 if (tmp != EXT_MAX_BLOCKS) 5564 *iterator = tmp; 5565 5566 /* 5567 * Its safe to start updating extents. Start and stop are unsigned, so 5568 * in case of right shift if extent with 0 block is reached, iterator 5569 * becomes NULL to indicate the end of the loop. 5570 */ 5571 while (iterator && start <= stop) { 5572 path = ext4_find_extent(inode, *iterator, path, 5573 EXT4_EX_NOCACHE); 5574 if (IS_ERR(path)) 5575 return PTR_ERR(path); 5576 depth = path->p_depth; 5577 extent = path[depth].p_ext; 5578 if (!extent) { 5579 EXT4_ERROR_INODE(inode, "unexpected hole at %lu", 5580 (unsigned long) *iterator); 5581 ret = -EFSCORRUPTED; 5582 goto out; 5583 } 5584 if (SHIFT == SHIFT_LEFT && *iterator > 5585 le32_to_cpu(extent->ee_block)) { 5586 /* Hole, move to the next extent */ 5587 if (extent < EXT_LAST_EXTENT(path[depth].p_hdr)) { 5588 path[depth].p_ext++; 5589 } else { 5590 *iterator = ext4_ext_next_allocated_block(path); 5591 continue; 5592 } 5593 } 5594 5595 tmp = *iterator; 5596 if (SHIFT == SHIFT_LEFT) { 5597 extent = EXT_LAST_EXTENT(path[depth].p_hdr); 5598 *iterator = le32_to_cpu(extent->ee_block) + 5599 ext4_ext_get_actual_len(extent); 5600 } else { 5601 extent = EXT_FIRST_EXTENT(path[depth].p_hdr); 5602 if (le32_to_cpu(extent->ee_block) > start) 5603 *iterator = le32_to_cpu(extent->ee_block) - 1; 5604 else if (le32_to_cpu(extent->ee_block) == start) 5605 iterator = NULL; 5606 else { 5607 extent = EXT_LAST_EXTENT(path[depth].p_hdr); 5608 while (le32_to_cpu(extent->ee_block) >= start) 5609 extent--; 5610 5611 if (extent == EXT_LAST_EXTENT(path[depth].p_hdr)) 5612 break; 5613 5614 extent++; 5615 iterator = NULL; 5616 } 5617 path[depth].p_ext = extent; 5618 } 5619 ret = ext4_ext_shift_path_extents(path, shift, inode, 5620 handle, SHIFT); 5621 /* iterator can be NULL which means we should break */ 5622 if (ret == -EAGAIN) 5623 goto again; 5624 if (ret) 5625 break; 5626 } 5627 out: 5628 ext4_free_ext_path(path); 5629 return ret; 5630 } 5631 5632 /* 5633 * ext4_collapse_range: 5634 * This implements the fallocate's collapse range functionality for ext4 5635 * Returns: 0 and non-zero on error. 5636 */ 5637 static int ext4_collapse_range(struct file *file, loff_t offset, loff_t len) 5638 { 5639 struct inode *inode = file_inode(file); 5640 struct super_block *sb = inode->i_sb; 5641 struct address_space *mapping = inode->i_mapping; 5642 loff_t end = offset + len; 5643 ext4_lblk_t start_lblk, end_lblk; 5644 handle_t *handle; 5645 unsigned int credits; 5646 loff_t start, new_size; 5647 int ret; 5648 5649 trace_ext4_collapse_range(inode, offset, len); 5650 WARN_ON_ONCE(!inode_is_locked(inode)); 5651 5652 /* Currently just for extent based files */ 5653 if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 5654 return -EOPNOTSUPP; 5655 /* Collapse range works only on fs cluster size aligned regions. */ 5656 if (!IS_ALIGNED(offset | len, EXT4_CLUSTER_SIZE(sb))) 5657 return -EINVAL; 5658 /* 5659 * There is no need to overlap collapse range with EOF, in which case 5660 * it is effectively a truncate operation 5661 */ 5662 if (end >= inode->i_size) 5663 return -EINVAL; 5664 5665 /* 5666 * Write tail of the last page before removed range and data that 5667 * will be shifted since they will get removed from the page cache 5668 * below. We are also protected from pages becoming dirty by 5669 * i_rwsem and invalidate_lock. 5670 * Need to round down offset to be aligned with page size boundary 5671 * for page size > block size. 5672 */ 5673 start = round_down(offset, PAGE_SIZE); 5674 ret = filemap_write_and_wait_range(mapping, start, offset); 5675 if (!ret) 5676 ret = filemap_write_and_wait_range(mapping, end, LLONG_MAX); 5677 if (ret) 5678 return ret; 5679 5680 truncate_pagecache(inode, start); 5681 5682 credits = ext4_chunk_trans_extent(inode, 0); 5683 handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits); 5684 if (IS_ERR(handle)) 5685 return PTR_ERR(handle); 5686 5687 ext4_fc_mark_ineligible(sb, EXT4_FC_REASON_FALLOC_RANGE, handle); 5688 5689 start_lblk = offset >> inode->i_blkbits; 5690 end_lblk = (offset + len) >> inode->i_blkbits; 5691 5692 ext4_check_map_extents_env(inode); 5693 5694 down_write(&EXT4_I(inode)->i_data_sem); 5695 ext4_discard_preallocations(inode); 5696 ext4_es_remove_extent(inode, start_lblk, EXT_MAX_BLOCKS - start_lblk); 5697 5698 ret = ext4_ext_remove_space(inode, start_lblk, end_lblk - 1); 5699 if (ret) { 5700 up_write(&EXT4_I(inode)->i_data_sem); 5701 goto out_handle; 5702 } 5703 ext4_discard_preallocations(inode); 5704 5705 ret = ext4_ext_shift_extents(inode, handle, end_lblk, 5706 end_lblk - start_lblk, SHIFT_LEFT); 5707 if (ret) { 5708 up_write(&EXT4_I(inode)->i_data_sem); 5709 goto out_handle; 5710 } 5711 5712 new_size = inode->i_size - len; 5713 i_size_write(inode, new_size); 5714 EXT4_I(inode)->i_disksize = new_size; 5715 5716 up_write(&EXT4_I(inode)->i_data_sem); 5717 ret = ext4_mark_inode_dirty(handle, inode); 5718 if (ret) 5719 goto out_handle; 5720 5721 ext4_update_inode_fsync_trans(handle, inode, 1); 5722 if ((file->f_flags & O_SYNC) || IS_SYNC(inode)) 5723 ext4_handle_sync(handle); 5724 5725 out_handle: 5726 ext4_journal_stop(handle); 5727 return ret; 5728 } 5729 5730 /* 5731 * ext4_insert_range: 5732 * This function implements the FALLOC_FL_INSERT_RANGE flag of fallocate. 5733 * The data blocks starting from @offset to the EOF are shifted by @len 5734 * towards right to create a hole in the @inode. Inode size is increased 5735 * by len bytes. 5736 * Returns 0 on success, error otherwise. 5737 */ 5738 static int ext4_insert_range(struct file *file, loff_t offset, loff_t len) 5739 { 5740 struct inode *inode = file_inode(file); 5741 struct super_block *sb = inode->i_sb; 5742 struct address_space *mapping = inode->i_mapping; 5743 handle_t *handle; 5744 struct ext4_ext_path *path; 5745 struct ext4_extent *extent; 5746 ext4_lblk_t start_lblk, len_lblk, ee_start_lblk = 0; 5747 unsigned int credits, ee_len; 5748 int ret, depth; 5749 loff_t start; 5750 5751 trace_ext4_insert_range(inode, offset, len); 5752 WARN_ON_ONCE(!inode_is_locked(inode)); 5753 5754 /* Currently just for extent based files */ 5755 if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 5756 return -EOPNOTSUPP; 5757 /* Insert range works only on fs cluster size aligned regions. */ 5758 if (!IS_ALIGNED(offset | len, EXT4_CLUSTER_SIZE(sb))) 5759 return -EINVAL; 5760 /* Offset must be less than i_size */ 5761 if (offset >= inode->i_size) 5762 return -EINVAL; 5763 /* Check whether the maximum file size would be exceeded */ 5764 if (len > inode->i_sb->s_maxbytes - inode->i_size) 5765 return -EFBIG; 5766 5767 /* 5768 * Write out all dirty pages. Need to round down to align start offset 5769 * to page size boundary for page size > block size. 5770 */ 5771 start = round_down(offset, PAGE_SIZE); 5772 ret = filemap_write_and_wait_range(mapping, start, LLONG_MAX); 5773 if (ret) 5774 return ret; 5775 5776 truncate_pagecache(inode, start); 5777 5778 credits = ext4_chunk_trans_extent(inode, 0); 5779 handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, credits); 5780 if (IS_ERR(handle)) 5781 return PTR_ERR(handle); 5782 5783 ext4_fc_mark_ineligible(sb, EXT4_FC_REASON_FALLOC_RANGE, handle); 5784 5785 /* Expand file to avoid data loss if there is error while shifting */ 5786 inode->i_size += len; 5787 EXT4_I(inode)->i_disksize += len; 5788 ret = ext4_mark_inode_dirty(handle, inode); 5789 if (ret) 5790 goto out_handle; 5791 5792 start_lblk = offset >> inode->i_blkbits; 5793 len_lblk = len >> inode->i_blkbits; 5794 5795 ext4_check_map_extents_env(inode); 5796 5797 down_write(&EXT4_I(inode)->i_data_sem); 5798 ext4_discard_preallocations(inode); 5799 5800 path = ext4_find_extent(inode, start_lblk, NULL, 0); 5801 if (IS_ERR(path)) { 5802 up_write(&EXT4_I(inode)->i_data_sem); 5803 ret = PTR_ERR(path); 5804 goto out_handle; 5805 } 5806 5807 depth = ext_depth(inode); 5808 extent = path[depth].p_ext; 5809 if (extent) { 5810 ee_start_lblk = le32_to_cpu(extent->ee_block); 5811 ee_len = ext4_ext_get_actual_len(extent); 5812 5813 /* 5814 * If start_lblk is not the starting block of extent, split 5815 * the extent @start_lblk 5816 */ 5817 if ((start_lblk > ee_start_lblk) && 5818 (start_lblk < (ee_start_lblk + ee_len))) { 5819 path = ext4_split_extent_at(handle, inode, path, 5820 start_lblk, EXT4_EX_NOCACHE | 5821 EXT4_GET_BLOCKS_SPLIT_NOMERGE | 5822 EXT4_GET_BLOCKS_METADATA_NOFAIL); 5823 } 5824 5825 if (IS_ERR(path)) { 5826 up_write(&EXT4_I(inode)->i_data_sem); 5827 ret = PTR_ERR(path); 5828 goto out_handle; 5829 } 5830 } 5831 5832 ext4_free_ext_path(path); 5833 ext4_es_remove_extent(inode, start_lblk, EXT_MAX_BLOCKS - start_lblk); 5834 5835 /* 5836 * if start_lblk lies in a hole which is at start of file, use 5837 * ee_start_lblk to shift extents 5838 */ 5839 ret = ext4_ext_shift_extents(inode, handle, 5840 max(ee_start_lblk, start_lblk), len_lblk, SHIFT_RIGHT); 5841 up_write(&EXT4_I(inode)->i_data_sem); 5842 if (ret) 5843 goto out_handle; 5844 5845 ext4_update_inode_fsync_trans(handle, inode, 1); 5846 if ((file->f_flags & O_SYNC) || IS_SYNC(inode)) 5847 ext4_handle_sync(handle); 5848 5849 out_handle: 5850 ext4_journal_stop(handle); 5851 return ret; 5852 } 5853 5854 /** 5855 * ext4_swap_extents() - Swap extents between two inodes 5856 * @handle: handle for this transaction 5857 * @inode1: First inode 5858 * @inode2: Second inode 5859 * @lblk1: Start block for first inode 5860 * @lblk2: Start block for second inode 5861 * @count: Number of blocks to swap 5862 * @unwritten: Mark second inode's extents as unwritten after swap 5863 * @erp: Pointer to save error value 5864 * 5865 * This helper routine does exactly what is promise "swap extents". All other 5866 * stuff such as page-cache locking consistency, bh mapping consistency or 5867 * extent's data copying must be performed by caller. 5868 * Locking: 5869 * i_rwsem is held for both inodes 5870 * i_data_sem is locked for write for both inodes 5871 * Assumptions: 5872 * All pages from requested range are locked for both inodes 5873 */ 5874 int 5875 ext4_swap_extents(handle_t *handle, struct inode *inode1, 5876 struct inode *inode2, ext4_lblk_t lblk1, ext4_lblk_t lblk2, 5877 ext4_lblk_t count, int unwritten, int *erp) 5878 { 5879 struct ext4_ext_path *path1 = NULL; 5880 struct ext4_ext_path *path2 = NULL; 5881 int replaced_count = 0; 5882 5883 BUG_ON(!rwsem_is_locked(&EXT4_I(inode1)->i_data_sem)); 5884 BUG_ON(!rwsem_is_locked(&EXT4_I(inode2)->i_data_sem)); 5885 BUG_ON(!inode_is_locked(inode1)); 5886 BUG_ON(!inode_is_locked(inode2)); 5887 5888 ext4_es_remove_extent(inode1, lblk1, count); 5889 ext4_es_remove_extent(inode2, lblk2, count); 5890 5891 while (count) { 5892 struct ext4_extent *ex1, *ex2, tmp_ex; 5893 ext4_lblk_t e1_blk, e2_blk; 5894 int e1_len, e2_len, len; 5895 int split = 0; 5896 5897 path1 = ext4_find_extent(inode1, lblk1, path1, EXT4_EX_NOCACHE); 5898 if (IS_ERR(path1)) { 5899 *erp = PTR_ERR(path1); 5900 goto errout; 5901 } 5902 path2 = ext4_find_extent(inode2, lblk2, path2, EXT4_EX_NOCACHE); 5903 if (IS_ERR(path2)) { 5904 *erp = PTR_ERR(path2); 5905 goto errout; 5906 } 5907 ex1 = path1[path1->p_depth].p_ext; 5908 ex2 = path2[path2->p_depth].p_ext; 5909 /* Do we have something to swap ? */ 5910 if (unlikely(!ex2 || !ex1)) 5911 goto errout; 5912 5913 e1_blk = le32_to_cpu(ex1->ee_block); 5914 e2_blk = le32_to_cpu(ex2->ee_block); 5915 e1_len = ext4_ext_get_actual_len(ex1); 5916 e2_len = ext4_ext_get_actual_len(ex2); 5917 5918 /* Hole handling */ 5919 if (!in_range(lblk1, e1_blk, e1_len) || 5920 !in_range(lblk2, e2_blk, e2_len)) { 5921 ext4_lblk_t next1, next2; 5922 5923 /* if hole after extent, then go to next extent */ 5924 next1 = ext4_ext_next_allocated_block(path1); 5925 next2 = ext4_ext_next_allocated_block(path2); 5926 /* If hole before extent, then shift to that extent */ 5927 if (e1_blk > lblk1) 5928 next1 = e1_blk; 5929 if (e2_blk > lblk2) 5930 next2 = e2_blk; 5931 /* Do we have something to swap */ 5932 if (next1 == EXT_MAX_BLOCKS || next2 == EXT_MAX_BLOCKS) 5933 goto errout; 5934 /* Move to the rightest boundary */ 5935 len = next1 - lblk1; 5936 if (len < next2 - lblk2) 5937 len = next2 - lblk2; 5938 if (len > count) 5939 len = count; 5940 lblk1 += len; 5941 lblk2 += len; 5942 count -= len; 5943 continue; 5944 } 5945 5946 /* Prepare left boundary */ 5947 if (e1_blk < lblk1) { 5948 split = 1; 5949 path1 = ext4_force_split_extent_at(handle, inode1, 5950 path1, lblk1, 0); 5951 if (IS_ERR(path1)) { 5952 *erp = PTR_ERR(path1); 5953 goto errout; 5954 } 5955 } 5956 if (e2_blk < lblk2) { 5957 split = 1; 5958 path2 = ext4_force_split_extent_at(handle, inode2, 5959 path2, lblk2, 0); 5960 if (IS_ERR(path2)) { 5961 *erp = PTR_ERR(path2); 5962 goto errout; 5963 } 5964 } 5965 /* ext4_split_extent_at() may result in leaf extent split, 5966 * path must to be revalidated. */ 5967 if (split) 5968 continue; 5969 5970 /* Prepare right boundary */ 5971 len = count; 5972 if (len > e1_blk + e1_len - lblk1) 5973 len = e1_blk + e1_len - lblk1; 5974 if (len > e2_blk + e2_len - lblk2) 5975 len = e2_blk + e2_len - lblk2; 5976 5977 if (len != e1_len) { 5978 split = 1; 5979 path1 = ext4_force_split_extent_at(handle, inode1, 5980 path1, lblk1 + len, 0); 5981 if (IS_ERR(path1)) { 5982 *erp = PTR_ERR(path1); 5983 goto errout; 5984 } 5985 } 5986 if (len != e2_len) { 5987 split = 1; 5988 path2 = ext4_force_split_extent_at(handle, inode2, 5989 path2, lblk2 + len, 0); 5990 if (IS_ERR(path2)) { 5991 *erp = PTR_ERR(path2); 5992 goto errout; 5993 } 5994 } 5995 /* ext4_split_extent_at() may result in leaf extent split, 5996 * path must to be revalidated. */ 5997 if (split) 5998 continue; 5999 6000 BUG_ON(e2_len != e1_len); 6001 *erp = ext4_ext_get_access(handle, inode1, path1 + path1->p_depth); 6002 if (unlikely(*erp)) 6003 goto errout; 6004 *erp = ext4_ext_get_access(handle, inode2, path2 + path2->p_depth); 6005 if (unlikely(*erp)) 6006 goto errout; 6007 6008 /* Both extents are fully inside boundaries. Swap it now */ 6009 tmp_ex = *ex1; 6010 ext4_ext_store_pblock(ex1, ext4_ext_pblock(ex2)); 6011 ext4_ext_store_pblock(ex2, ext4_ext_pblock(&tmp_ex)); 6012 ex1->ee_len = cpu_to_le16(e2_len); 6013 ex2->ee_len = cpu_to_le16(e1_len); 6014 if (unwritten) 6015 ext4_ext_mark_unwritten(ex2); 6016 if (ext4_ext_is_unwritten(&tmp_ex)) 6017 ext4_ext_mark_unwritten(ex1); 6018 6019 ext4_ext_try_to_merge(handle, inode2, path2, ex2); 6020 ext4_ext_try_to_merge(handle, inode1, path1, ex1); 6021 *erp = ext4_ext_dirty(handle, inode2, path2 + 6022 path2->p_depth); 6023 if (unlikely(*erp)) 6024 goto errout; 6025 *erp = ext4_ext_dirty(handle, inode1, path1 + 6026 path1->p_depth); 6027 /* 6028 * Looks scarry ah..? second inode already points to new blocks, 6029 * and it was successfully dirtied. But luckily error may happen 6030 * only due to journal error, so full transaction will be 6031 * aborted anyway. 6032 */ 6033 if (unlikely(*erp)) 6034 goto errout; 6035 6036 lblk1 += len; 6037 lblk2 += len; 6038 replaced_count += len; 6039 count -= len; 6040 } 6041 6042 errout: 6043 ext4_free_ext_path(path1); 6044 ext4_free_ext_path(path2); 6045 return replaced_count; 6046 } 6047 6048 /* 6049 * ext4_clu_mapped - determine whether any block in a logical cluster has 6050 * been mapped to a physical cluster 6051 * 6052 * @inode - file containing the logical cluster 6053 * @lclu - logical cluster of interest 6054 * 6055 * Returns 1 if any block in the logical cluster is mapped, signifying 6056 * that a physical cluster has been allocated for it. Otherwise, 6057 * returns 0. Can also return negative error codes. Derived from 6058 * ext4_ext_map_blocks(). 6059 */ 6060 int ext4_clu_mapped(struct inode *inode, ext4_lblk_t lclu) 6061 { 6062 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 6063 struct ext4_ext_path *path; 6064 int depth, mapped = 0, err = 0; 6065 struct ext4_extent *extent; 6066 ext4_lblk_t first_lblk, first_lclu, last_lclu; 6067 6068 /* 6069 * if data can be stored inline, the logical cluster isn't 6070 * mapped - no physical clusters have been allocated, and the 6071 * file has no extents 6072 */ 6073 if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA) || 6074 ext4_has_inline_data(inode)) 6075 return 0; 6076 6077 /* search for the extent closest to the first block in the cluster */ 6078 path = ext4_find_extent(inode, EXT4_C2B(sbi, lclu), NULL, 0); 6079 if (IS_ERR(path)) 6080 return PTR_ERR(path); 6081 6082 depth = ext_depth(inode); 6083 6084 /* 6085 * A consistent leaf must not be empty. This situation is possible, 6086 * though, _during_ tree modification, and it's why an assert can't 6087 * be put in ext4_find_extent(). 6088 */ 6089 if (unlikely(path[depth].p_ext == NULL && depth != 0)) { 6090 EXT4_ERROR_INODE(inode, 6091 "bad extent address - lblock: %lu, depth: %d, pblock: %lld", 6092 (unsigned long) EXT4_C2B(sbi, lclu), 6093 depth, path[depth].p_block); 6094 err = -EFSCORRUPTED; 6095 goto out; 6096 } 6097 6098 extent = path[depth].p_ext; 6099 6100 /* can't be mapped if the extent tree is empty */ 6101 if (extent == NULL) 6102 goto out; 6103 6104 first_lblk = le32_to_cpu(extent->ee_block); 6105 first_lclu = EXT4_B2C(sbi, first_lblk); 6106 6107 /* 6108 * Three possible outcomes at this point - found extent spanning 6109 * the target cluster, to the left of the target cluster, or to the 6110 * right of the target cluster. The first two cases are handled here. 6111 * The last case indicates the target cluster is not mapped. 6112 */ 6113 if (lclu >= first_lclu) { 6114 last_lclu = EXT4_B2C(sbi, first_lblk + 6115 ext4_ext_get_actual_len(extent) - 1); 6116 if (lclu <= last_lclu) { 6117 mapped = 1; 6118 } else { 6119 first_lblk = ext4_ext_next_allocated_block(path); 6120 first_lclu = EXT4_B2C(sbi, first_lblk); 6121 if (lclu == first_lclu) 6122 mapped = 1; 6123 } 6124 } 6125 6126 out: 6127 ext4_free_ext_path(path); 6128 6129 return err ? err : mapped; 6130 } 6131 6132 /* 6133 * Updates physical block address and unwritten status of extent 6134 * starting at lblk start and of len. If such an extent doesn't exist, 6135 * this function splits the extent tree appropriately to create an 6136 * extent like this. This function is called in the fast commit 6137 * replay path. Returns 0 on success and error on failure. 6138 */ 6139 int ext4_ext_replay_update_ex(struct inode *inode, ext4_lblk_t start, 6140 int len, int unwritten, ext4_fsblk_t pblk) 6141 { 6142 struct ext4_ext_path *path; 6143 struct ext4_extent *ex; 6144 int ret; 6145 6146 path = ext4_find_extent(inode, start, NULL, 0); 6147 if (IS_ERR(path)) 6148 return PTR_ERR(path); 6149 ex = path[path->p_depth].p_ext; 6150 if (!ex) { 6151 ret = -EFSCORRUPTED; 6152 goto out; 6153 } 6154 6155 if (le32_to_cpu(ex->ee_block) != start || 6156 ext4_ext_get_actual_len(ex) != len) { 6157 /* We need to split this extent to match our extent first */ 6158 down_write(&EXT4_I(inode)->i_data_sem); 6159 path = ext4_force_split_extent_at(NULL, inode, path, start, 1); 6160 up_write(&EXT4_I(inode)->i_data_sem); 6161 if (IS_ERR(path)) { 6162 ret = PTR_ERR(path); 6163 goto out; 6164 } 6165 6166 path = ext4_find_extent(inode, start, path, 0); 6167 if (IS_ERR(path)) 6168 return PTR_ERR(path); 6169 6170 ex = path[path->p_depth].p_ext; 6171 WARN_ON(le32_to_cpu(ex->ee_block) != start); 6172 6173 if (ext4_ext_get_actual_len(ex) != len) { 6174 down_write(&EXT4_I(inode)->i_data_sem); 6175 path = ext4_force_split_extent_at(NULL, inode, path, 6176 start + len, 1); 6177 up_write(&EXT4_I(inode)->i_data_sem); 6178 if (IS_ERR(path)) { 6179 ret = PTR_ERR(path); 6180 goto out; 6181 } 6182 6183 path = ext4_find_extent(inode, start, path, 0); 6184 if (IS_ERR(path)) 6185 return PTR_ERR(path); 6186 ex = path[path->p_depth].p_ext; 6187 } 6188 } 6189 if (unwritten) 6190 ext4_ext_mark_unwritten(ex); 6191 else 6192 ext4_ext_mark_initialized(ex); 6193 ext4_ext_store_pblock(ex, pblk); 6194 down_write(&EXT4_I(inode)->i_data_sem); 6195 ret = ext4_ext_dirty(NULL, inode, &path[path->p_depth]); 6196 up_write(&EXT4_I(inode)->i_data_sem); 6197 out: 6198 ext4_free_ext_path(path); 6199 ext4_mark_inode_dirty(NULL, inode); 6200 return ret; 6201 } 6202 6203 /* Try to shrink the extent tree */ 6204 void ext4_ext_replay_shrink_inode(struct inode *inode, ext4_lblk_t end) 6205 { 6206 struct ext4_ext_path *path = NULL; 6207 struct ext4_extent *ex; 6208 ext4_lblk_t old_cur, cur = 0; 6209 6210 while (cur < end) { 6211 path = ext4_find_extent(inode, cur, NULL, 0); 6212 if (IS_ERR(path)) 6213 return; 6214 ex = path[path->p_depth].p_ext; 6215 if (!ex) { 6216 ext4_free_ext_path(path); 6217 ext4_mark_inode_dirty(NULL, inode); 6218 return; 6219 } 6220 old_cur = cur; 6221 cur = le32_to_cpu(ex->ee_block) + ext4_ext_get_actual_len(ex); 6222 if (cur <= old_cur) 6223 cur = old_cur + 1; 6224 ext4_ext_try_to_merge(NULL, inode, path, ex); 6225 down_write(&EXT4_I(inode)->i_data_sem); 6226 ext4_ext_dirty(NULL, inode, &path[path->p_depth]); 6227 up_write(&EXT4_I(inode)->i_data_sem); 6228 ext4_mark_inode_dirty(NULL, inode); 6229 ext4_free_ext_path(path); 6230 } 6231 } 6232 6233 /* Check if *cur is a hole and if it is, skip it */ 6234 static int skip_hole(struct inode *inode, ext4_lblk_t *cur) 6235 { 6236 int ret; 6237 struct ext4_map_blocks map; 6238 6239 map.m_lblk = *cur; 6240 map.m_len = ((inode->i_size) >> inode->i_sb->s_blocksize_bits) - *cur; 6241 6242 ret = ext4_map_blocks(NULL, inode, &map, 0); 6243 if (ret < 0) 6244 return ret; 6245 if (ret != 0) 6246 return 0; 6247 *cur = *cur + map.m_len; 6248 return 0; 6249 } 6250 6251 /* Count number of blocks used by this inode and update i_blocks */ 6252 int ext4_ext_replay_set_iblocks(struct inode *inode) 6253 { 6254 struct ext4_ext_path *path = NULL, *path2 = NULL; 6255 struct ext4_extent *ex; 6256 ext4_lblk_t cur = 0, end; 6257 int numblks = 0, i, ret = 0; 6258 ext4_fsblk_t cmp1, cmp2; 6259 struct ext4_map_blocks map; 6260 6261 /* Determin the size of the file first */ 6262 path = ext4_find_extent(inode, EXT_MAX_BLOCKS - 1, NULL, 6263 EXT4_EX_NOCACHE); 6264 if (IS_ERR(path)) 6265 return PTR_ERR(path); 6266 ex = path[path->p_depth].p_ext; 6267 if (!ex) 6268 goto out; 6269 end = le32_to_cpu(ex->ee_block) + ext4_ext_get_actual_len(ex); 6270 6271 /* Count the number of data blocks */ 6272 cur = 0; 6273 while (cur < end) { 6274 map.m_lblk = cur; 6275 map.m_len = end - cur; 6276 ret = ext4_map_blocks(NULL, inode, &map, 0); 6277 if (ret < 0) 6278 break; 6279 if (ret > 0) 6280 numblks += ret; 6281 cur = cur + map.m_len; 6282 } 6283 6284 /* 6285 * Count the number of extent tree blocks. We do it by looking up 6286 * two successive extents and determining the difference between 6287 * their paths. When path is different for 2 successive extents 6288 * we compare the blocks in the path at each level and increment 6289 * iblocks by total number of differences found. 6290 */ 6291 cur = 0; 6292 ret = skip_hole(inode, &cur); 6293 if (ret < 0) 6294 goto out; 6295 path = ext4_find_extent(inode, cur, path, 0); 6296 if (IS_ERR(path)) 6297 goto out; 6298 numblks += path->p_depth; 6299 while (cur < end) { 6300 path = ext4_find_extent(inode, cur, path, 0); 6301 if (IS_ERR(path)) 6302 break; 6303 ex = path[path->p_depth].p_ext; 6304 if (!ex) 6305 goto cleanup; 6306 6307 cur = max(cur + 1, le32_to_cpu(ex->ee_block) + 6308 ext4_ext_get_actual_len(ex)); 6309 ret = skip_hole(inode, &cur); 6310 if (ret < 0) 6311 break; 6312 6313 path2 = ext4_find_extent(inode, cur, path2, 0); 6314 if (IS_ERR(path2)) 6315 break; 6316 6317 for (i = 0; i <= max(path->p_depth, path2->p_depth); i++) { 6318 cmp1 = cmp2 = 0; 6319 if (i <= path->p_depth) 6320 cmp1 = path[i].p_bh ? 6321 path[i].p_bh->b_blocknr : 0; 6322 if (i <= path2->p_depth) 6323 cmp2 = path2[i].p_bh ? 6324 path2[i].p_bh->b_blocknr : 0; 6325 if (cmp1 != cmp2 && cmp2 != 0) 6326 numblks++; 6327 } 6328 } 6329 6330 out: 6331 inode->i_blocks = numblks << (inode->i_sb->s_blocksize_bits - 9); 6332 ext4_mark_inode_dirty(NULL, inode); 6333 cleanup: 6334 ext4_free_ext_path(path); 6335 ext4_free_ext_path(path2); 6336 return 0; 6337 } 6338 6339 int ext4_ext_clear_bb(struct inode *inode) 6340 { 6341 struct ext4_ext_path *path = NULL; 6342 struct ext4_extent *ex; 6343 ext4_lblk_t cur = 0, end; 6344 int j, ret = 0; 6345 struct ext4_map_blocks map; 6346 6347 if (ext4_test_inode_flag(inode, EXT4_INODE_INLINE_DATA)) 6348 return 0; 6349 6350 /* Determin the size of the file first */ 6351 path = ext4_find_extent(inode, EXT_MAX_BLOCKS - 1, NULL, 6352 EXT4_EX_NOCACHE); 6353 if (IS_ERR(path)) 6354 return PTR_ERR(path); 6355 ex = path[path->p_depth].p_ext; 6356 if (!ex) 6357 goto out; 6358 end = le32_to_cpu(ex->ee_block) + ext4_ext_get_actual_len(ex); 6359 6360 cur = 0; 6361 while (cur < end) { 6362 map.m_lblk = cur; 6363 map.m_len = end - cur; 6364 ret = ext4_map_blocks(NULL, inode, &map, 0); 6365 if (ret < 0) 6366 break; 6367 if (ret > 0) { 6368 path = ext4_find_extent(inode, map.m_lblk, path, 0); 6369 if (!IS_ERR(path)) { 6370 for (j = 0; j < path->p_depth; j++) { 6371 ext4_mb_mark_bb(inode->i_sb, 6372 path[j].p_block, 1, false); 6373 ext4_fc_record_regions(inode->i_sb, inode->i_ino, 6374 0, path[j].p_block, 1, 1); 6375 } 6376 } else { 6377 path = NULL; 6378 } 6379 ext4_mb_mark_bb(inode->i_sb, map.m_pblk, map.m_len, false); 6380 ext4_fc_record_regions(inode->i_sb, inode->i_ino, 6381 map.m_lblk, map.m_pblk, map.m_len, 1); 6382 } 6383 cur = cur + map.m_len; 6384 } 6385 6386 out: 6387 ext4_free_ext_path(path); 6388 return 0; 6389 } 6390 6391 #if IS_ENABLED(CONFIG_EXT4_KUNIT_TESTS) 6392 int ext4_ext_space_root_idx_test(struct inode *inode, int check) 6393 { 6394 return ext4_ext_space_root_idx(inode, check); 6395 } 6396 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_ext_space_root_idx_test); 6397 6398 struct ext4_ext_path *ext4_split_convert_extents_test(handle_t *handle, 6399 struct inode *inode, struct ext4_map_blocks *map, 6400 struct ext4_ext_path *path, int flags, 6401 unsigned int *allocated) 6402 { 6403 return ext4_split_convert_extents(handle, inode, map, path, 6404 flags, allocated); 6405 } 6406 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_split_convert_extents_test); 6407 6408 EXPORT_SYMBOL_FOR_EXT4_TEST(__ext4_ext_dirty); 6409 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_ext_zeroout); 6410 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_es_register_shrinker); 6411 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_es_unregister_shrinker); 6412 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_map_create_blocks); 6413 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_es_init_tree); 6414 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_es_lookup_extent); 6415 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_es_insert_extent); 6416 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_ext_insert_extent); 6417 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_find_extent); 6418 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_issue_zeroout); 6419 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_map_query_blocks); 6420 #endif 6421