1 /* 2 * Copyright IBM Corporation, 2007 3 * Author Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com> 4 * 5 * This program is free software; you can redistribute it and/or modify it 6 * under the terms of version 2.1 of the GNU Lesser General Public License 7 * as published by the Free Software Foundation. 8 * 9 * This program is distributed in the hope that it would be useful, but 10 * WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 12 * 13 */ 14 15 #include <linux/module.h> 16 #include "ext4_jbd2.h" 17 #include "ext4_extents.h" 18 19 /* 20 * The contiguous blocks details which can be 21 * represented by a single extent 22 */ 23 struct list_blocks_struct { 24 ext4_lblk_t first_block, last_block; 25 ext4_fsblk_t first_pblock, last_pblock; 26 }; 27 28 static int finish_range(handle_t *handle, struct inode *inode, 29 struct list_blocks_struct *lb) 30 31 { 32 int retval = 0, needed; 33 struct ext4_extent newext; 34 struct ext4_ext_path *path; 35 if (lb->first_pblock == 0) 36 return 0; 37 38 /* Add the extent to temp inode*/ 39 newext.ee_block = cpu_to_le32(lb->first_block); 40 newext.ee_len = cpu_to_le16(lb->last_block - lb->first_block + 1); 41 ext4_ext_store_pblock(&newext, lb->first_pblock); 42 path = ext4_ext_find_extent(inode, lb->first_block, NULL); 43 44 if (IS_ERR(path)) { 45 retval = PTR_ERR(path); 46 path = NULL; 47 goto err_out; 48 } 49 50 /* 51 * Calculate the credit needed to inserting this extent 52 * Since we are doing this in loop we may accumalate extra 53 * credit. But below we try to not accumalate too much 54 * of them by restarting the journal. 55 */ 56 needed = ext4_ext_calc_credits_for_insert(inode, path); 57 58 /* 59 * Make sure the credit we accumalated is not really high 60 */ 61 if (needed && handle->h_buffer_credits >= EXT4_RESERVE_TRANS_BLOCKS) { 62 retval = ext4_journal_restart(handle, needed); 63 if (retval) 64 goto err_out; 65 } else if (needed) { 66 retval = ext4_journal_extend(handle, needed); 67 if (retval) { 68 /* 69 * IF not able to extend the journal restart the journal 70 */ 71 retval = ext4_journal_restart(handle, needed); 72 if (retval) 73 goto err_out; 74 } 75 } 76 retval = ext4_ext_insert_extent(handle, inode, path, &newext); 77 err_out: 78 if (path) { 79 ext4_ext_drop_refs(path); 80 kfree(path); 81 } 82 lb->first_pblock = 0; 83 return retval; 84 } 85 86 static int update_extent_range(handle_t *handle, struct inode *inode, 87 ext4_fsblk_t pblock, ext4_lblk_t blk_num, 88 struct list_blocks_struct *lb) 89 { 90 int retval; 91 /* 92 * See if we can add on to the existing range (if it exists) 93 */ 94 if (lb->first_pblock && 95 (lb->last_pblock+1 == pblock) && 96 (lb->last_block+1 == blk_num)) { 97 lb->last_pblock = pblock; 98 lb->last_block = blk_num; 99 return 0; 100 } 101 /* 102 * Start a new range. 103 */ 104 retval = finish_range(handle, inode, lb); 105 lb->first_pblock = lb->last_pblock = pblock; 106 lb->first_block = lb->last_block = blk_num; 107 108 return retval; 109 } 110 111 static int update_ind_extent_range(handle_t *handle, struct inode *inode, 112 ext4_fsblk_t pblock, ext4_lblk_t *blk_nump, 113 struct list_blocks_struct *lb) 114 { 115 struct buffer_head *bh; 116 __le32 *i_data; 117 int i, retval = 0; 118 ext4_lblk_t blk_count = *blk_nump; 119 unsigned long max_entries = inode->i_sb->s_blocksize >> 2; 120 121 if (!pblock) { 122 /* Only update the file block number */ 123 *blk_nump += max_entries; 124 return 0; 125 } 126 127 bh = sb_bread(inode->i_sb, pblock); 128 if (!bh) 129 return -EIO; 130 131 i_data = (__le32 *)bh->b_data; 132 for (i = 0; i < max_entries; i++, blk_count++) { 133 if (i_data[i]) { 134 retval = update_extent_range(handle, inode, 135 le32_to_cpu(i_data[i]), 136 blk_count, lb); 137 if (retval) 138 break; 139 } 140 } 141 142 /* Update the file block number */ 143 *blk_nump = blk_count; 144 put_bh(bh); 145 return retval; 146 147 } 148 149 static int update_dind_extent_range(handle_t *handle, struct inode *inode, 150 ext4_fsblk_t pblock, ext4_lblk_t *blk_nump, 151 struct list_blocks_struct *lb) 152 { 153 struct buffer_head *bh; 154 __le32 *i_data; 155 int i, retval = 0; 156 ext4_lblk_t blk_count = *blk_nump; 157 unsigned long max_entries = inode->i_sb->s_blocksize >> 2; 158 159 if (!pblock) { 160 /* Only update the file block number */ 161 *blk_nump += max_entries * max_entries; 162 return 0; 163 } 164 bh = sb_bread(inode->i_sb, pblock); 165 if (!bh) 166 return -EIO; 167 168 i_data = (__le32 *)bh->b_data; 169 for (i = 0; i < max_entries; i++) { 170 if (i_data[i]) { 171 retval = update_ind_extent_range(handle, inode, 172 le32_to_cpu(i_data[i]), 173 &blk_count, lb); 174 if (retval) 175 break; 176 } else { 177 /* Only update the file block number */ 178 blk_count += max_entries; 179 } 180 } 181 182 /* Update the file block number */ 183 *blk_nump = blk_count; 184 put_bh(bh); 185 return retval; 186 187 } 188 189 static int update_tind_extent_range(handle_t *handle, struct inode *inode, 190 ext4_fsblk_t pblock, ext4_lblk_t *blk_nump, 191 struct list_blocks_struct *lb) 192 { 193 struct buffer_head *bh; 194 __le32 *i_data; 195 int i, retval = 0; 196 ext4_lblk_t blk_count = *blk_nump; 197 unsigned long max_entries = inode->i_sb->s_blocksize >> 2; 198 199 if (!pblock) { 200 /* Only update the file block number */ 201 *blk_nump += max_entries * max_entries * max_entries; 202 return 0; 203 } 204 bh = sb_bread(inode->i_sb, pblock); 205 if (!bh) 206 return -EIO; 207 208 i_data = (__le32 *)bh->b_data; 209 for (i = 0; i < max_entries; i++) { 210 if (i_data[i]) { 211 retval = update_dind_extent_range(handle, inode, 212 le32_to_cpu(i_data[i]), 213 &blk_count, lb); 214 if (retval) 215 break; 216 } else 217 /* Only update the file block number */ 218 blk_count += max_entries * max_entries; 219 } 220 /* Update the file block number */ 221 *blk_nump = blk_count; 222 put_bh(bh); 223 return retval; 224 225 } 226 227 static int extend_credit_for_blkdel(handle_t *handle, struct inode *inode) 228 { 229 int retval = 0, needed; 230 231 if (handle->h_buffer_credits > EXT4_RESERVE_TRANS_BLOCKS) 232 return 0; 233 /* 234 * We are freeing a blocks. During this we touch 235 * superblock, group descriptor and block bitmap. 236 * So allocate a credit of 3. We may update 237 * quota (user and group). 238 */ 239 needed = 3 + 2*EXT4_QUOTA_TRANS_BLOCKS(inode->i_sb); 240 241 if (ext4_journal_extend(handle, needed) != 0) 242 retval = ext4_journal_restart(handle, needed); 243 244 return retval; 245 } 246 247 static int free_dind_blocks(handle_t *handle, 248 struct inode *inode, __le32 i_data) 249 { 250 int i; 251 __le32 *tmp_idata; 252 struct buffer_head *bh; 253 unsigned long max_entries = inode->i_sb->s_blocksize >> 2; 254 255 bh = sb_bread(inode->i_sb, le32_to_cpu(i_data)); 256 if (!bh) 257 return -EIO; 258 259 tmp_idata = (__le32 *)bh->b_data; 260 for (i = 0; i < max_entries; i++) { 261 if (tmp_idata[i]) { 262 extend_credit_for_blkdel(handle, inode); 263 ext4_free_blocks(handle, inode, 264 le32_to_cpu(tmp_idata[i]), 1, 1); 265 } 266 } 267 put_bh(bh); 268 extend_credit_for_blkdel(handle, inode); 269 ext4_free_blocks(handle, inode, le32_to_cpu(i_data), 1, 1); 270 return 0; 271 } 272 273 static int free_tind_blocks(handle_t *handle, 274 struct inode *inode, __le32 i_data) 275 { 276 int i, retval = 0; 277 __le32 *tmp_idata; 278 struct buffer_head *bh; 279 unsigned long max_entries = inode->i_sb->s_blocksize >> 2; 280 281 bh = sb_bread(inode->i_sb, le32_to_cpu(i_data)); 282 if (!bh) 283 return -EIO; 284 285 tmp_idata = (__le32 *)bh->b_data; 286 for (i = 0; i < max_entries; i++) { 287 if (tmp_idata[i]) { 288 retval = free_dind_blocks(handle, 289 inode, tmp_idata[i]); 290 if (retval) { 291 put_bh(bh); 292 return retval; 293 } 294 } 295 } 296 put_bh(bh); 297 extend_credit_for_blkdel(handle, inode); 298 ext4_free_blocks(handle, inode, le32_to_cpu(i_data), 1, 1); 299 return 0; 300 } 301 302 static int free_ind_block(handle_t *handle, struct inode *inode, __le32 *i_data) 303 { 304 int retval; 305 306 /* ei->i_data[EXT4_IND_BLOCK] */ 307 if (i_data[0]) { 308 extend_credit_for_blkdel(handle, inode); 309 ext4_free_blocks(handle, inode, 310 le32_to_cpu(i_data[0]), 1, 1); 311 } 312 313 /* ei->i_data[EXT4_DIND_BLOCK] */ 314 if (i_data[1]) { 315 retval = free_dind_blocks(handle, inode, i_data[1]); 316 if (retval) 317 return retval; 318 } 319 320 /* ei->i_data[EXT4_TIND_BLOCK] */ 321 if (i_data[2]) { 322 retval = free_tind_blocks(handle, inode, i_data[2]); 323 if (retval) 324 return retval; 325 } 326 return 0; 327 } 328 329 static int ext4_ext_swap_inode_data(handle_t *handle, struct inode *inode, 330 struct inode *tmp_inode) 331 { 332 int retval; 333 __le32 i_data[3]; 334 struct ext4_inode_info *ei = EXT4_I(inode); 335 struct ext4_inode_info *tmp_ei = EXT4_I(tmp_inode); 336 337 /* 338 * One credit accounted for writing the 339 * i_data field of the original inode 340 */ 341 retval = ext4_journal_extend(handle, 1); 342 if (retval) { 343 retval = ext4_journal_restart(handle, 1); 344 if (retval) 345 goto err_out; 346 } 347 348 i_data[0] = ei->i_data[EXT4_IND_BLOCK]; 349 i_data[1] = ei->i_data[EXT4_DIND_BLOCK]; 350 i_data[2] = ei->i_data[EXT4_TIND_BLOCK]; 351 352 down_write(&EXT4_I(inode)->i_data_sem); 353 /* 354 * if EXT4_EXT_MIGRATE is cleared a block allocation 355 * happened after we started the migrate. We need to 356 * fail the migrate 357 */ 358 if (!(EXT4_I(inode)->i_flags & EXT4_EXT_MIGRATE)) { 359 retval = -EAGAIN; 360 up_write(&EXT4_I(inode)->i_data_sem); 361 goto err_out; 362 } else 363 EXT4_I(inode)->i_flags = EXT4_I(inode)->i_flags & 364 ~EXT4_EXT_MIGRATE; 365 /* 366 * We have the extent map build with the tmp inode. 367 * Now copy the i_data across 368 */ 369 ei->i_flags |= EXT4_EXTENTS_FL; 370 memcpy(ei->i_data, tmp_ei->i_data, sizeof(ei->i_data)); 371 372 /* 373 * Update i_blocks with the new blocks that got 374 * allocated while adding extents for extent index 375 * blocks. 376 * 377 * While converting to extents we need not 378 * update the orignal inode i_blocks for extent blocks 379 * via quota APIs. The quota update happened via tmp_inode already. 380 */ 381 spin_lock(&inode->i_lock); 382 inode->i_blocks += tmp_inode->i_blocks; 383 spin_unlock(&inode->i_lock); 384 up_write(&EXT4_I(inode)->i_data_sem); 385 386 /* 387 * We mark the inode dirty after, because we decrement the 388 * i_blocks when freeing the indirect meta-data blocks 389 */ 390 retval = free_ind_block(handle, inode, i_data); 391 ext4_mark_inode_dirty(handle, inode); 392 393 err_out: 394 return retval; 395 } 396 397 static int free_ext_idx(handle_t *handle, struct inode *inode, 398 struct ext4_extent_idx *ix) 399 { 400 int i, retval = 0; 401 ext4_fsblk_t block; 402 struct buffer_head *bh; 403 struct ext4_extent_header *eh; 404 405 block = idx_pblock(ix); 406 bh = sb_bread(inode->i_sb, block); 407 if (!bh) 408 return -EIO; 409 410 eh = (struct ext4_extent_header *)bh->b_data; 411 if (eh->eh_depth != 0) { 412 ix = EXT_FIRST_INDEX(eh); 413 for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ix++) { 414 retval = free_ext_idx(handle, inode, ix); 415 if (retval) 416 break; 417 } 418 } 419 put_bh(bh); 420 extend_credit_for_blkdel(handle, inode); 421 ext4_free_blocks(handle, inode, block, 1, 1); 422 return retval; 423 } 424 425 /* 426 * Free the extent meta data blocks only 427 */ 428 static int free_ext_block(handle_t *handle, struct inode *inode) 429 { 430 int i, retval = 0; 431 struct ext4_inode_info *ei = EXT4_I(inode); 432 struct ext4_extent_header *eh = (struct ext4_extent_header *)ei->i_data; 433 struct ext4_extent_idx *ix; 434 if (eh->eh_depth == 0) 435 /* 436 * No extra blocks allocated for extent meta data 437 */ 438 return 0; 439 ix = EXT_FIRST_INDEX(eh); 440 for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ix++) { 441 retval = free_ext_idx(handle, inode, ix); 442 if (retval) 443 return retval; 444 } 445 return retval; 446 447 } 448 449 int ext4_ext_migrate(struct inode *inode, struct file *filp, 450 unsigned int cmd, unsigned long arg) 451 { 452 handle_t *handle; 453 int retval = 0, i; 454 __le32 *i_data; 455 ext4_lblk_t blk_count = 0; 456 struct ext4_inode_info *ei; 457 struct inode *tmp_inode = NULL; 458 struct list_blocks_struct lb; 459 unsigned long max_entries; 460 461 if (!test_opt(inode->i_sb, EXTENTS)) 462 /* 463 * if mounted with noextents we don't allow the migrate 464 */ 465 return -EINVAL; 466 467 if ((EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL)) 468 return -EINVAL; 469 470 if (S_ISLNK(inode->i_mode) && inode->i_blocks == 0) 471 /* 472 * don't migrate fast symlink 473 */ 474 return retval; 475 476 handle = ext4_journal_start(inode, 477 EXT4_DATA_TRANS_BLOCKS(inode->i_sb) + 478 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 + 479 2 * EXT4_QUOTA_INIT_BLOCKS(inode->i_sb) 480 + 1); 481 if (IS_ERR(handle)) { 482 retval = PTR_ERR(handle); 483 goto err_out; 484 } 485 tmp_inode = ext4_new_inode(handle, 486 inode->i_sb->s_root->d_inode, 487 S_IFREG); 488 if (IS_ERR(tmp_inode)) { 489 retval = -ENOMEM; 490 ext4_journal_stop(handle); 491 tmp_inode = NULL; 492 goto err_out; 493 } 494 i_size_write(tmp_inode, i_size_read(inode)); 495 /* 496 * We don't want the inode to be reclaimed 497 * if we got interrupted in between. We have 498 * this tmp inode carrying reference to the 499 * data blocks of the original file. We set 500 * the i_nlink to zero at the last stage after 501 * switching the original file to extent format 502 */ 503 tmp_inode->i_nlink = 1; 504 505 ext4_ext_tree_init(handle, tmp_inode); 506 ext4_orphan_add(handle, tmp_inode); 507 ext4_journal_stop(handle); 508 509 /* 510 * start with one credit accounted for 511 * superblock modification. 512 * 513 * For the tmp_inode we already have commited the 514 * trascation that created the inode. Later as and 515 * when we add extents we extent the journal 516 */ 517 /* 518 * inode_mutex prevent write and truncate on the file. Read still goes 519 * through. We take i_data_sem in ext4_ext_swap_inode_data before we 520 * switch the inode format to prevent read. 521 */ 522 mutex_lock(&(inode->i_mutex)); 523 /* 524 * Even though we take i_mutex we can still cause block allocation 525 * via mmap write to holes. If we have allocated new blocks we fail 526 * migrate. New block allocation will clear EXT4_EXT_MIGRATE flag. 527 * The flag is updated with i_data_sem held to prevent racing with 528 * block allocation. 529 */ 530 down_read((&EXT4_I(inode)->i_data_sem)); 531 EXT4_I(inode)->i_flags = EXT4_I(inode)->i_flags | EXT4_EXT_MIGRATE; 532 up_read((&EXT4_I(inode)->i_data_sem)); 533 534 handle = ext4_journal_start(inode, 1); 535 536 ei = EXT4_I(inode); 537 i_data = ei->i_data; 538 memset(&lb, 0, sizeof(lb)); 539 540 /* 32 bit block address 4 bytes */ 541 max_entries = inode->i_sb->s_blocksize >> 2; 542 for (i = 0; i < EXT4_NDIR_BLOCKS; i++, blk_count++) { 543 if (i_data[i]) { 544 retval = update_extent_range(handle, tmp_inode, 545 le32_to_cpu(i_data[i]), 546 blk_count, &lb); 547 if (retval) 548 goto err_out; 549 } 550 } 551 if (i_data[EXT4_IND_BLOCK]) { 552 retval = update_ind_extent_range(handle, tmp_inode, 553 le32_to_cpu(i_data[EXT4_IND_BLOCK]), 554 &blk_count, &lb); 555 if (retval) 556 goto err_out; 557 } else 558 blk_count += max_entries; 559 if (i_data[EXT4_DIND_BLOCK]) { 560 retval = update_dind_extent_range(handle, tmp_inode, 561 le32_to_cpu(i_data[EXT4_DIND_BLOCK]), 562 &blk_count, &lb); 563 if (retval) 564 goto err_out; 565 } else 566 blk_count += max_entries * max_entries; 567 if (i_data[EXT4_TIND_BLOCK]) { 568 retval = update_tind_extent_range(handle, tmp_inode, 569 le32_to_cpu(i_data[EXT4_TIND_BLOCK]), 570 &blk_count, &lb); 571 if (retval) 572 goto err_out; 573 } 574 /* 575 * Build the last extent 576 */ 577 retval = finish_range(handle, tmp_inode, &lb); 578 err_out: 579 if (retval) 580 /* 581 * Failure case delete the extent information with the 582 * tmp_inode 583 */ 584 free_ext_block(handle, tmp_inode); 585 else { 586 retval = ext4_ext_swap_inode_data(handle, inode, tmp_inode); 587 if (retval) 588 /* 589 * if we fail to swap inode data free the extent 590 * details of the tmp inode 591 */ 592 free_ext_block(handle, tmp_inode); 593 } 594 595 /* We mark the tmp_inode dirty via ext4_ext_tree_init. */ 596 if (ext4_journal_extend(handle, 1) != 0) 597 ext4_journal_restart(handle, 1); 598 599 /* 600 * Mark the tmp_inode as of size zero 601 */ 602 i_size_write(tmp_inode, 0); 603 604 /* 605 * set the i_blocks count to zero 606 * so that the ext4_delete_inode does the 607 * right job 608 * 609 * We don't need to take the i_lock because 610 * the inode is not visible to user space. 611 */ 612 tmp_inode->i_blocks = 0; 613 614 /* Reset the extent details */ 615 ext4_ext_tree_init(handle, tmp_inode); 616 617 /* 618 * Set the i_nlink to zero so that 619 * generic_drop_inode really deletes the 620 * inode 621 */ 622 tmp_inode->i_nlink = 0; 623 624 ext4_journal_stop(handle); 625 mutex_unlock(&(inode->i_mutex)); 626 627 if (tmp_inode) 628 iput(tmp_inode); 629 630 return retval; 631 } 632