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