1 // SPDX-License-Identifier: LGPL-2.1 2 /* 3 * Copyright IBM Corporation, 2007 4 * Author Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com> 5 * 6 */ 7 8 #include <linux/slab.h> 9 #include "ext4_jbd2.h" 10 #include "ext4_extents.h" 11 12 /* 13 * The contiguous blocks details which can be 14 * represented by a single extent 15 */ 16 struct migrate_struct { 17 u64 first_block, last_block, curr_block; 18 ext4_fsblk_t first_pblock, last_pblock; 19 }; 20 21 static int finish_range(handle_t *handle, struct inode *inode, 22 struct migrate_struct *lb) 23 24 { 25 int retval = 0, needed; 26 struct ext4_extent newext; 27 struct ext4_ext_path *path; 28 if (lb->first_pblock == 0) 29 return 0; 30 31 /* Add the extent to temp inode*/ 32 newext.ee_block = cpu_to_le32(lb->first_block); 33 newext.ee_len = cpu_to_le16(lb->last_block - lb->first_block + 1); 34 ext4_ext_store_pblock(&newext, lb->first_pblock); 35 /* Locking only for convenience since we are operating on temp inode */ 36 down_write(&EXT4_I(inode)->i_data_sem); 37 path = ext4_find_extent(inode, lb->first_block, NULL, 0); 38 if (IS_ERR(path)) { 39 retval = PTR_ERR(path); 40 goto err_out; 41 } 42 43 /* 44 * Calculate the credit needed to inserting this extent 45 * Since we are doing this in loop we may accumulate extra 46 * credit. But below we try to not accumulate too much 47 * of them by restarting the journal. 48 */ 49 needed = ext4_ext_calc_credits_for_single_extent(inode, 50 lb->last_block - lb->first_block + 1, path); 51 52 retval = ext4_datasem_ensure_credits(handle, inode, needed, needed, 0); 53 if (retval < 0) 54 goto err_out; 55 path = ext4_ext_insert_extent(handle, inode, path, &newext, 0); 56 retval = PTR_ERR_OR_ZERO(path); 57 err_out: 58 up_write((&EXT4_I(inode)->i_data_sem)); 59 ext4_free_ext_path(path); 60 lb->first_pblock = 0; 61 return retval; 62 } 63 64 static int update_extent_range(handle_t *handle, struct inode *inode, 65 ext4_fsblk_t pblock, struct migrate_struct *lb) 66 { 67 int retval; 68 69 if (lb->curr_block >= EXT_MAX_BLOCKS) 70 return -EOPNOTSUPP; 71 72 /* 73 * See if we can add on to the existing range (if it exists) 74 */ 75 if (lb->first_pblock && 76 (lb->last_pblock+1 == pblock) && 77 (lb->last_block+1 == lb->curr_block) && 78 (lb->last_block - lb->first_block + 1 < EXT_INIT_MAX_LEN)) { 79 lb->last_pblock = pblock; 80 lb->last_block = lb->curr_block; 81 lb->curr_block++; 82 return 0; 83 } 84 /* 85 * Start a new range. 86 */ 87 retval = finish_range(handle, inode, lb); 88 lb->first_pblock = lb->last_pblock = pblock; 89 lb->first_block = lb->last_block = lb->curr_block; 90 lb->curr_block++; 91 return retval; 92 } 93 94 static int update_ind_extent_range(handle_t *handle, struct inode *inode, 95 ext4_fsblk_t pblock, 96 struct migrate_struct *lb) 97 { 98 struct buffer_head *bh; 99 __le32 *i_data; 100 int i, retval = 0; 101 unsigned long max_entries = inode->i_sb->s_blocksize >> 2; 102 103 bh = ext4_sb_bread(inode->i_sb, pblock, 0); 104 if (IS_ERR(bh)) 105 return PTR_ERR(bh); 106 107 i_data = (__le32 *)bh->b_data; 108 for (i = 0; i < max_entries; i++) { 109 if (i_data[i]) { 110 retval = update_extent_range(handle, inode, 111 le32_to_cpu(i_data[i]), lb); 112 if (retval) 113 break; 114 } else { 115 lb->curr_block++; 116 } 117 } 118 put_bh(bh); 119 return retval; 120 121 } 122 123 static int update_dind_extent_range(handle_t *handle, struct inode *inode, 124 ext4_fsblk_t pblock, 125 struct migrate_struct *lb) 126 { 127 struct buffer_head *bh; 128 __le32 *i_data; 129 int i, retval = 0; 130 unsigned long max_entries = inode->i_sb->s_blocksize >> 2; 131 132 bh = ext4_sb_bread(inode->i_sb, pblock, 0); 133 if (IS_ERR(bh)) 134 return PTR_ERR(bh); 135 136 i_data = (__le32 *)bh->b_data; 137 for (i = 0; i < max_entries; i++) { 138 if (i_data[i]) { 139 retval = update_ind_extent_range(handle, inode, 140 le32_to_cpu(i_data[i]), lb); 141 if (retval) 142 break; 143 } else { 144 /* Only update the file block number */ 145 lb->curr_block += max_entries; 146 } 147 } 148 put_bh(bh); 149 return retval; 150 151 } 152 153 static int update_tind_extent_range(handle_t *handle, struct inode *inode, 154 ext4_fsblk_t pblock, 155 struct migrate_struct *lb) 156 { 157 struct buffer_head *bh; 158 __le32 *i_data; 159 int i, retval = 0; 160 unsigned long max_entries = inode->i_sb->s_blocksize >> 2; 161 162 bh = ext4_sb_bread(inode->i_sb, pblock, 0); 163 if (IS_ERR(bh)) 164 return PTR_ERR(bh); 165 166 i_data = (__le32 *)bh->b_data; 167 for (i = 0; i < max_entries; i++) { 168 if (i_data[i]) { 169 retval = update_dind_extent_range(handle, inode, 170 le32_to_cpu(i_data[i]), lb); 171 if (retval) 172 break; 173 } else { 174 /* Only update the file block number */ 175 lb->curr_block += max_entries * max_entries; 176 } 177 } 178 put_bh(bh); 179 return retval; 180 181 } 182 183 static int free_dind_blocks(handle_t *handle, 184 struct inode *inode, __le32 i_data) 185 { 186 int i; 187 __le32 *tmp_idata; 188 struct buffer_head *bh; 189 struct super_block *sb = inode->i_sb; 190 unsigned long max_entries = inode->i_sb->s_blocksize >> 2; 191 int err; 192 193 bh = ext4_sb_bread(sb, le32_to_cpu(i_data), 0); 194 if (IS_ERR(bh)) 195 return PTR_ERR(bh); 196 197 tmp_idata = (__le32 *)bh->b_data; 198 for (i = 0; i < max_entries; i++) { 199 if (tmp_idata[i]) { 200 err = ext4_journal_ensure_credits(handle, 201 EXT4_RESERVE_TRANS_BLOCKS, 202 ext4_free_metadata_revoke_credits(sb, 1)); 203 if (err < 0) { 204 put_bh(bh); 205 return err; 206 } 207 ext4_free_blocks(handle, inode, NULL, 208 le32_to_cpu(tmp_idata[i]), 1, 209 EXT4_FREE_BLOCKS_METADATA | 210 EXT4_FREE_BLOCKS_FORGET); 211 } 212 } 213 put_bh(bh); 214 err = ext4_journal_ensure_credits(handle, EXT4_RESERVE_TRANS_BLOCKS, 215 ext4_free_metadata_revoke_credits(sb, 1)); 216 if (err < 0) 217 return err; 218 ext4_free_blocks(handle, inode, NULL, le32_to_cpu(i_data), 1, 219 EXT4_FREE_BLOCKS_METADATA | 220 EXT4_FREE_BLOCKS_FORGET); 221 return 0; 222 } 223 224 static int free_tind_blocks(handle_t *handle, 225 struct inode *inode, __le32 i_data) 226 { 227 int i, retval = 0; 228 __le32 *tmp_idata; 229 struct buffer_head *bh; 230 unsigned long max_entries = inode->i_sb->s_blocksize >> 2; 231 232 bh = ext4_sb_bread(inode->i_sb, le32_to_cpu(i_data), 0); 233 if (IS_ERR(bh)) 234 return PTR_ERR(bh); 235 236 tmp_idata = (__le32 *)bh->b_data; 237 for (i = 0; i < max_entries; i++) { 238 if (tmp_idata[i]) { 239 retval = free_dind_blocks(handle, 240 inode, tmp_idata[i]); 241 if (retval) { 242 put_bh(bh); 243 return retval; 244 } 245 } 246 } 247 put_bh(bh); 248 retval = ext4_journal_ensure_credits(handle, EXT4_RESERVE_TRANS_BLOCKS, 249 ext4_free_metadata_revoke_credits(inode->i_sb, 1)); 250 if (retval < 0) 251 return retval; 252 ext4_free_blocks(handle, inode, NULL, le32_to_cpu(i_data), 1, 253 EXT4_FREE_BLOCKS_METADATA | 254 EXT4_FREE_BLOCKS_FORGET); 255 return 0; 256 } 257 258 static int free_ind_block(handle_t *handle, struct inode *inode, __le32 *i_data) 259 { 260 int retval; 261 262 /* ei->i_data[EXT4_IND_BLOCK] */ 263 if (i_data[0]) { 264 retval = ext4_journal_ensure_credits(handle, 265 EXT4_RESERVE_TRANS_BLOCKS, 266 ext4_free_metadata_revoke_credits(inode->i_sb, 1)); 267 if (retval < 0) 268 return retval; 269 ext4_free_blocks(handle, inode, NULL, 270 le32_to_cpu(i_data[0]), 1, 271 EXT4_FREE_BLOCKS_METADATA | 272 EXT4_FREE_BLOCKS_FORGET); 273 } 274 275 /* ei->i_data[EXT4_DIND_BLOCK] */ 276 if (i_data[1]) { 277 retval = free_dind_blocks(handle, inode, i_data[1]); 278 if (retval) 279 return retval; 280 } 281 282 /* ei->i_data[EXT4_TIND_BLOCK] */ 283 if (i_data[2]) { 284 retval = free_tind_blocks(handle, inode, i_data[2]); 285 if (retval) 286 return retval; 287 } 288 return 0; 289 } 290 291 static int ext4_ext_swap_inode_data(handle_t *handle, struct inode *inode, 292 struct inode *tmp_inode) 293 { 294 int retval, retval2 = 0; 295 __le32 i_data[3]; 296 struct ext4_inode_info *ei = EXT4_I(inode); 297 struct ext4_inode_info *tmp_ei = EXT4_I(tmp_inode); 298 299 /* 300 * One credit accounted for writing the 301 * i_data field of the original inode 302 */ 303 retval = ext4_journal_ensure_credits(handle, 1, 0); 304 if (retval < 0) 305 goto err_out; 306 307 i_data[0] = ei->i_data[EXT4_IND_BLOCK]; 308 i_data[1] = ei->i_data[EXT4_DIND_BLOCK]; 309 i_data[2] = ei->i_data[EXT4_TIND_BLOCK]; 310 311 down_write(&EXT4_I(inode)->i_data_sem); 312 /* 313 * if EXT4_STATE_EXT_MIGRATE is cleared a block allocation 314 * happened after we started the migrate. We need to 315 * fail the migrate 316 */ 317 if (!ext4_test_inode_state(inode, EXT4_STATE_EXT_MIGRATE)) { 318 retval = -EAGAIN; 319 up_write(&EXT4_I(inode)->i_data_sem); 320 goto err_out; 321 } else 322 ext4_clear_inode_state(inode, EXT4_STATE_EXT_MIGRATE); 323 /* 324 * We have the extent map build with the tmp inode. 325 * Now copy the i_data across 326 */ 327 ext4_set_inode_flag(inode, EXT4_INODE_EXTENTS); 328 memcpy(ei->i_data, tmp_ei->i_data, sizeof(ei->i_data)); 329 /* 330 * The extent tree belongs to the inode now. Make tmp_inode's extent 331 * tree empty, so that cleaning up tmp_inode can't free the tree 332 * blocks, if we fail to finish the migration. 333 */ 334 ext4_ext_init_root(tmp_inode); 335 336 /* 337 * Update i_blocks with the new blocks that got 338 * allocated while adding extents for extent index 339 * blocks. 340 * 341 * While converting to extents we need not 342 * update the original inode i_blocks for extent blocks 343 * via quota APIs. The quota update happened via tmp_inode already. 344 * Clear tmp_inode i_blocks to avoid truncating original data blocks 345 * in case we fail to finish the migration. 346 */ 347 spin_lock(&inode->i_lock); 348 inode->i_blocks += tmp_inode->i_blocks; 349 spin_unlock(&inode->i_lock); 350 tmp_inode->i_blocks = 0; 351 up_write(&EXT4_I(inode)->i_data_sem); 352 353 /* 354 * We mark the inode dirty after, because we decrement the 355 * i_blocks when freeing the indirect meta-data blocks 356 */ 357 retval = free_ind_block(handle, inode, i_data); 358 retval2 = ext4_mark_inode_dirty(handle, inode); 359 if (unlikely(retval2 && !retval)) 360 retval = retval2; 361 362 err_out: 363 return retval; 364 } 365 366 static int free_ext_idx(handle_t *handle, struct inode *inode, 367 struct ext4_extent_idx *ix) 368 { 369 int i, retval = 0; 370 ext4_fsblk_t block; 371 struct buffer_head *bh; 372 struct ext4_extent_header *eh; 373 374 block = ext4_idx_pblock(ix); 375 bh = ext4_sb_bread(inode->i_sb, block, 0); 376 if (IS_ERR(bh)) 377 return PTR_ERR(bh); 378 379 eh = (struct ext4_extent_header *)bh->b_data; 380 if (eh->eh_depth != 0) { 381 ix = EXT_FIRST_INDEX(eh); 382 for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ix++) { 383 retval = free_ext_idx(handle, inode, ix); 384 if (retval) { 385 put_bh(bh); 386 return retval; 387 } 388 } 389 } 390 put_bh(bh); 391 retval = ext4_journal_ensure_credits(handle, EXT4_RESERVE_TRANS_BLOCKS, 392 ext4_free_metadata_revoke_credits(inode->i_sb, 1)); 393 if (retval < 0) 394 return retval; 395 ext4_free_blocks(handle, inode, NULL, block, 1, 396 EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET); 397 return 0; 398 } 399 400 /* 401 * Free the extent meta data blocks only 402 */ 403 static int free_ext_block(handle_t *handle, struct inode *inode) 404 { 405 int i, retval = 0; 406 struct ext4_inode_info *ei = EXT4_I(inode); 407 struct ext4_extent_header *eh = (struct ext4_extent_header *)ei->i_data; 408 struct ext4_extent_idx *ix; 409 if (eh->eh_depth == 0) 410 /* 411 * No extra blocks allocated for extent meta data 412 */ 413 return 0; 414 ix = EXT_FIRST_INDEX(eh); 415 for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ix++) { 416 retval = free_ext_idx(handle, inode, ix); 417 if (retval) 418 return retval; 419 } 420 return retval; 421 } 422 423 int ext4_ext_migrate(struct inode *inode) 424 { 425 handle_t *handle; 426 int retval = 0, err, i; 427 __le32 *i_data; 428 struct ext4_inode_info *ei; 429 struct inode *tmp_inode = NULL; 430 struct migrate_struct lb; 431 unsigned long max_entries; 432 __u32 goal, tmp_csum_seed; 433 uid_t owner[2]; 434 int alloc_ctx; 435 436 /* 437 * If the filesystem does not support extents, or the inode 438 * already is extent-based, error out. 439 */ 440 if (!ext4_has_feature_extents(inode->i_sb) || 441 ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS) || 442 ext4_has_inline_data(inode)) 443 return -EINVAL; 444 445 if (S_ISLNK(inode->i_mode) && inode->i_blocks == 0) 446 /* 447 * don't migrate fast symlink 448 */ 449 return retval; 450 451 alloc_ctx = ext4_writepages_down_write(inode->i_sb); 452 453 /* 454 * Worst case we can touch the allocation bitmaps and a block 455 * group descriptor block. We do need to worry about 456 * credits for modifying the quota inode. 457 */ 458 handle = ext4_journal_start(inode, EXT4_HT_MIGRATE, 459 3 + EXT4_MAXQUOTAS_TRANS_BLOCKS(inode->i_sb)); 460 461 if (IS_ERR(handle)) { 462 retval = PTR_ERR(handle); 463 goto out_unlock; 464 } 465 /* 466 * This operation rewrites the inode's block mapping layout 467 * (indirect to extents) and is not tracked in the fast commit 468 * log, so disable fast commits for this transaction. 469 */ 470 ext4_fc_mark_ineligible(inode->i_sb, EXT4_FC_REASON_MIGRATE, handle); 471 goal = ((((u32)inode->i_ino - 1) / EXT4_INODES_PER_GROUP(inode->i_sb)) * 472 EXT4_INODES_PER_GROUP(inode->i_sb)) + 1; 473 owner[0] = i_uid_read(inode); 474 owner[1] = i_gid_read(inode); 475 tmp_inode = ext4_new_inode(handle, d_inode(inode->i_sb->s_root), 476 S_IFREG, NULL, goal, owner, 0); 477 if (IS_ERR(tmp_inode)) { 478 retval = PTR_ERR(tmp_inode); 479 ext4_journal_stop(handle); 480 tmp_inode = NULL; 481 goto out_unlock; 482 } 483 /* 484 * Use the correct seed for checksum (i.e. the seed from 'inode'). This 485 * is so that the metadata blocks will have the correct checksum after 486 * the migration. 487 */ 488 ei = EXT4_I(inode); 489 tmp_csum_seed = EXT4_I(tmp_inode)->i_csum_seed; 490 EXT4_I(tmp_inode)->i_csum_seed = ei->i_csum_seed; 491 i_size_write(tmp_inode, i_size_read(inode)); 492 /* 493 * Set the i_nlink to zero so it will be deleted later 494 * when we drop inode reference. 495 */ 496 clear_nlink(tmp_inode); 497 498 ext4_ext_tree_init(handle, tmp_inode); 499 ext4_journal_stop(handle); 500 501 /* 502 * start with one credit accounted for 503 * superblock modification. 504 * 505 * For the tmp_inode we already have committed the 506 * transaction that created the inode. Later as and 507 * when we add extents we extent the journal 508 */ 509 /* 510 * Even though we take i_rwsem we can still cause block 511 * allocation via mmap write to holes. If we have allocated 512 * new blocks we fail migrate. New block allocation will 513 * clear EXT4_STATE_EXT_MIGRATE flag. The flag is updated 514 * with i_data_sem held to prevent racing with block 515 * allocation. 516 */ 517 down_read(&EXT4_I(inode)->i_data_sem); 518 ext4_set_inode_state(inode, EXT4_STATE_EXT_MIGRATE); 519 up_read((&EXT4_I(inode)->i_data_sem)); 520 521 handle = ext4_journal_start(inode, EXT4_HT_MIGRATE, 1); 522 if (IS_ERR(handle)) { 523 retval = PTR_ERR(handle); 524 goto out_tmp_inode; 525 } 526 527 i_data = ei->i_data; 528 memset(&lb, 0, sizeof(lb)); 529 530 /* 32 bit block address 4 bytes */ 531 max_entries = inode->i_sb->s_blocksize >> 2; 532 for (i = 0; i < EXT4_NDIR_BLOCKS; i++) { 533 if (i_data[i]) { 534 retval = update_extent_range(handle, tmp_inode, 535 le32_to_cpu(i_data[i]), &lb); 536 if (retval) 537 goto err_out; 538 } else 539 lb.curr_block++; 540 } 541 if (i_data[EXT4_IND_BLOCK]) { 542 retval = update_ind_extent_range(handle, tmp_inode, 543 le32_to_cpu(i_data[EXT4_IND_BLOCK]), &lb); 544 if (retval) 545 goto err_out; 546 } else 547 lb.curr_block += max_entries; 548 if (i_data[EXT4_DIND_BLOCK]) { 549 retval = update_dind_extent_range(handle, tmp_inode, 550 le32_to_cpu(i_data[EXT4_DIND_BLOCK]), &lb); 551 if (retval) 552 goto err_out; 553 } else 554 lb.curr_block += max_entries * max_entries; 555 if (i_data[EXT4_TIND_BLOCK]) { 556 retval = update_tind_extent_range(handle, tmp_inode, 557 le32_to_cpu(i_data[EXT4_TIND_BLOCK]), &lb); 558 if (retval) 559 goto err_out; 560 } 561 /* 562 * Build the last extent 563 */ 564 retval = finish_range(handle, tmp_inode, &lb); 565 err_out: 566 if (retval) 567 /* 568 * Failure case delete the extent information with the 569 * tmp_inode 570 */ 571 free_ext_block(handle, tmp_inode); 572 else { 573 retval = ext4_ext_swap_inode_data(handle, inode, tmp_inode); 574 if (retval && !ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) 575 /* 576 * if we fail to swap inode data free the extent 577 * details of the tmp inode. If the tree was already 578 * moved to the original inode, the tmp_inode has an 579 * empty tree and there is nothing to free. 580 */ 581 free_ext_block(handle, tmp_inode); 582 } 583 584 /* 585 * Mark the tmp_inode as of size zero 586 */ 587 i_size_write(tmp_inode, 0); 588 589 /* 590 * set the i_blocks count to zero 591 * so that the ext4_evict_inode() does the 592 * right job 593 * 594 * We don't need to take the i_lock because 595 * the inode is not visible to user space. 596 */ 597 tmp_inode->i_blocks = 0; 598 EXT4_I(tmp_inode)->i_csum_seed = tmp_csum_seed; 599 600 /* We mark the tmp_inode dirty via ext4_ext_tree_init. */ 601 err = ext4_journal_ensure_credits(handle, 1, 0); 602 if (err < 0) { 603 if (!retval) 604 retval = err; 605 goto out_stop; 606 } 607 /* Reset the extent details */ 608 ext4_ext_tree_init(handle, tmp_inode); 609 out_stop: 610 ext4_journal_stop(handle); 611 out_tmp_inode: 612 unlock_new_inode(tmp_inode); 613 out_unlock: 614 ext4_writepages_up_write(inode->i_sb, alloc_ctx); 615 iput(tmp_inode); 616 return retval; 617 } 618 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_ext_migrate); 619 620 /* 621 * Migrate a simple extent-based inode to use the i_blocks[] array 622 */ 623 int ext4_ind_migrate(struct inode *inode) 624 { 625 struct ext4_extent_header *eh; 626 struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 627 struct ext4_super_block *es = sbi->s_es; 628 struct ext4_inode_info *ei = EXT4_I(inode); 629 struct ext4_extent *ex; 630 unsigned int i, len; 631 ext4_lblk_t start, end; 632 ext4_fsblk_t blk; 633 handle_t *handle; 634 int ret, ret2 = 0; 635 int alloc_ctx; 636 637 if (!ext4_has_feature_extents(inode->i_sb) || 638 (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))) 639 return -EINVAL; 640 641 if (ext4_has_feature_bigalloc(inode->i_sb)) 642 return -EOPNOTSUPP; 643 if (ext4_inode_buffered_iomap(inode)) 644 return -EOPNOTSUPP; 645 646 /* 647 * In order to get correct extent info, force all delayed allocation 648 * blocks to be allocated, otherwise delayed allocation blocks may not 649 * be reflected and bypass the checks on extent header. 650 */ 651 if (test_opt(inode->i_sb, DELALLOC)) 652 ext4_alloc_da_blocks(inode); 653 654 alloc_ctx = ext4_writepages_down_write(inode->i_sb); 655 656 handle = ext4_journal_start(inode, EXT4_HT_MIGRATE, 1); 657 if (IS_ERR(handle)) { 658 ret = PTR_ERR(handle); 659 goto out_unlock; 660 } 661 /* 662 * This operation rewrites the inode's block mapping layout 663 * (extents to indirect blocks) and is not tracked in the fast 664 * commit log, so disable fast commits for this transaction. 665 */ 666 ext4_fc_mark_ineligible(inode->i_sb, EXT4_FC_REASON_MIGRATE, handle); 667 668 down_write(&EXT4_I(inode)->i_data_sem); 669 ret = ext4_ext_check_inode(inode); 670 if (ret) 671 goto errout; 672 673 eh = ext_inode_hdr(inode); 674 ex = EXT_FIRST_EXTENT(eh); 675 if (ext4_blocks_count(es) > EXT4_MAX_BLOCK_FILE_PHYS || 676 eh->eh_depth != 0 || le16_to_cpu(eh->eh_entries) > 1) { 677 ret = -EOPNOTSUPP; 678 goto errout; 679 } 680 if (eh->eh_entries == 0) 681 blk = len = start = end = 0; 682 else { 683 len = le16_to_cpu(ex->ee_len); 684 blk = ext4_ext_pblock(ex); 685 start = le32_to_cpu(ex->ee_block); 686 end = start + len - 1; 687 if (end >= EXT4_NDIR_BLOCKS) { 688 ret = -EOPNOTSUPP; 689 goto errout; 690 } 691 } 692 693 ext4_clear_inode_flag(inode, EXT4_INODE_EXTENTS); 694 memset(ei->i_data, 0, sizeof(ei->i_data)); 695 for (i = start; i <= end; i++) 696 ei->i_data[i] = cpu_to_le32(blk++); 697 ret2 = ext4_mark_inode_dirty(handle, inode); 698 if (unlikely(ret2 && !ret)) 699 ret = ret2; 700 errout: 701 up_write(&EXT4_I(inode)->i_data_sem); 702 ext4_journal_stop(handle); 703 out_unlock: 704 ext4_writepages_up_write(inode->i_sb, alloc_ctx); 705 return ret; 706 } 707 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_ind_migrate); 708 709 #if IS_ENABLED(CONFIG_EXT4_KUNIT_TESTS) 710 int ext4_ext_swap_inode_data_test(handle_t *handle, struct inode *inode, 711 struct inode *tmp_inode) 712 { 713 return ext4_ext_swap_inode_data(handle, inode, tmp_inode); 714 } 715 EXPORT_SYMBOL_FOR_EXT4_TEST(ext4_ext_swap_inode_data_test); 716 #endif 717