1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * move_extents.c 4 * 5 * Copyright (C) 2011 Oracle. All rights reserved. 6 */ 7 #include <linux/fs.h> 8 #include <linux/types.h> 9 #include <linux/mount.h> 10 #include <linux/swap.h> 11 12 #include <cluster/masklog.h> 13 14 #include "ocfs2.h" 15 #include "ocfs2_ioctl.h" 16 17 #include "alloc.h" 18 #include "localalloc.h" 19 #include "aops.h" 20 #include "dlmglue.h" 21 #include "extent_map.h" 22 #include "inode.h" 23 #include "journal.h" 24 #include "suballoc.h" 25 #include "uptodate.h" 26 #include "super.h" 27 #include "dir.h" 28 #include "buffer_head_io.h" 29 #include "sysfile.h" 30 #include "refcounttree.h" 31 #include "move_extents.h" 32 33 struct ocfs2_move_extents_context { 34 struct inode *inode; 35 struct file *file; 36 int auto_defrag; 37 int partial; 38 int credits; 39 u32 new_phys_cpos; 40 u32 clusters_moved; 41 u64 refcount_loc; 42 struct ocfs2_move_extents *range; 43 struct ocfs2_extent_tree et; 44 struct ocfs2_alloc_context *meta_ac; 45 struct ocfs2_alloc_context *data_ac; 46 struct ocfs2_cached_dealloc_ctxt dealloc; 47 }; 48 49 static int __ocfs2_move_extent(handle_t *handle, 50 struct ocfs2_move_extents_context *context, 51 u32 cpos, u32 len, u32 p_cpos, u32 new_p_cpos, 52 int ext_flags) 53 { 54 int ret = 0, index; 55 struct inode *inode = context->inode; 56 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 57 struct ocfs2_extent_rec *rec, replace_rec; 58 struct ocfs2_path *path = NULL; 59 struct ocfs2_extent_list *el; 60 u64 ino = ocfs2_metadata_cache_owner(context->et.et_ci); 61 u64 old_blkno = ocfs2_clusters_to_blocks(inode->i_sb, p_cpos); 62 63 ret = ocfs2_duplicate_clusters_by_page(handle, inode, cpos, 64 p_cpos, new_p_cpos, len); 65 if (ret) { 66 mlog_errno(ret); 67 goto out; 68 } 69 70 memset(&replace_rec, 0, sizeof(replace_rec)); 71 replace_rec.e_cpos = cpu_to_le32(cpos); 72 replace_rec.e_leaf_clusters = cpu_to_le16(len); 73 replace_rec.e_blkno = cpu_to_le64(ocfs2_clusters_to_blocks(inode->i_sb, 74 new_p_cpos)); 75 76 path = ocfs2_new_path_from_et(&context->et); 77 if (!path) { 78 ret = -ENOMEM; 79 mlog_errno(ret); 80 goto out; 81 } 82 83 ret = ocfs2_find_path(INODE_CACHE(inode), path, cpos); 84 if (ret) { 85 mlog_errno(ret); 86 goto out; 87 } 88 89 el = path_leaf_el(path); 90 91 index = ocfs2_search_extent_list(el, cpos); 92 if (index == -1) { 93 ret = ocfs2_error(inode->i_sb, 94 "Inode %llu has an extent at cpos %u which can no longer be found\n", 95 (unsigned long long)ino, cpos); 96 goto out; 97 } 98 99 rec = &el->l_recs[index]; 100 101 if (ext_flags != rec->e_flags) { 102 ret = ocfs2_error(inode->i_sb, 103 "Inode %llu has corrupted extent %d with flags 0x%x at cpos %u\n", 104 (unsigned long long)ino, index, rec->e_flags, cpos); 105 goto out; 106 } 107 108 /* 109 * after moving/defraging to new location, the extent is not going 110 * to be refcounted anymore. 111 */ 112 replace_rec.e_flags = ext_flags & ~OCFS2_EXT_REFCOUNTED; 113 114 ret = ocfs2_split_extent(handle, &context->et, path, index, 115 &replace_rec, context->meta_ac, 116 &context->dealloc); 117 if (ret) { 118 mlog_errno(ret); 119 goto out; 120 } 121 122 context->new_phys_cpos = new_p_cpos; 123 124 /* 125 * need I to append truncate log for old clusters? 126 */ 127 if (old_blkno) { 128 if (ext_flags & OCFS2_EXT_REFCOUNTED) 129 ret = ocfs2_decrease_refcount(inode, handle, 130 ocfs2_blocks_to_clusters(osb->sb, 131 old_blkno), 132 len, context->meta_ac, 133 &context->dealloc, 1); 134 else 135 ret = ocfs2_truncate_log_append(osb, handle, 136 old_blkno, len); 137 } 138 139 ocfs2_update_inode_fsync_trans(handle, inode, 0); 140 out: 141 ocfs2_free_path(path); 142 return ret; 143 } 144 145 /* 146 * lock allocator, and reserve appropriate number of bits for 147 * meta blocks. 148 */ 149 static int ocfs2_lock_meta_allocator_move_extents(struct inode *inode, 150 struct ocfs2_extent_tree *et, 151 u32 clusters_to_move, 152 u32 extents_to_split, 153 struct ocfs2_alloc_context **meta_ac, 154 int extra_blocks, 155 int *credits) 156 { 157 int ret, num_free_extents; 158 unsigned int max_recs_needed = 2 * extents_to_split + clusters_to_move; 159 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 160 161 num_free_extents = ocfs2_num_free_extents(et); 162 if (num_free_extents < 0) { 163 ret = num_free_extents; 164 mlog_errno(ret); 165 goto out; 166 } 167 168 if (!num_free_extents || 169 (ocfs2_sparse_alloc(osb) && num_free_extents < max_recs_needed)) 170 extra_blocks += ocfs2_extend_meta_needed(et->et_root_el); 171 172 ret = ocfs2_reserve_new_metadata_blocks(osb, extra_blocks, meta_ac); 173 if (ret) { 174 mlog_errno(ret); 175 goto out; 176 } 177 178 179 *credits += ocfs2_calc_extend_credits(osb->sb, et->et_root_el); 180 181 mlog(0, "reserve metadata_blocks: %d, data_clusters: %u, credits: %d\n", 182 extra_blocks, clusters_to_move, *credits); 183 out: 184 if (ret) { 185 if (*meta_ac) { 186 ocfs2_free_alloc_context(*meta_ac); 187 *meta_ac = NULL; 188 } 189 } 190 191 return ret; 192 } 193 194 /* 195 * Using one journal handle to guarantee the data consistency in case 196 * crash happens anywhere. 197 * 198 * XXX: defrag can end up with finishing partial extent as requested, 199 * due to not enough contiguous clusters can be found in allocator. 200 */ 201 static int ocfs2_defrag_extent(struct ocfs2_move_extents_context *context, 202 u32 cpos, u32 phys_cpos, u32 *len, int ext_flags) 203 { 204 int ret, credits = 0, extra_blocks = 0, partial = context->partial; 205 handle_t *handle; 206 struct inode *inode = context->inode; 207 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 208 struct inode *tl_inode = osb->osb_tl_inode; 209 struct ocfs2_refcount_tree *ref_tree = NULL; 210 u32 new_phys_cpos, new_len; 211 u64 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos); 212 int need_free = 0; 213 214 if ((ext_flags & OCFS2_EXT_REFCOUNTED) && *len) { 215 BUG_ON(!ocfs2_is_refcount_inode(inode)); 216 BUG_ON(!context->refcount_loc); 217 218 ret = ocfs2_lock_refcount_tree(osb, context->refcount_loc, 1, 219 &ref_tree, NULL); 220 if (ret) { 221 mlog_errno(ret); 222 return ret; 223 } 224 225 ret = ocfs2_prepare_refcount_change_for_del(inode, 226 context->refcount_loc, 227 phys_blkno, 228 *len, 229 &credits, 230 &extra_blocks); 231 if (ret) { 232 mlog_errno(ret); 233 goto out; 234 } 235 } 236 237 ret = ocfs2_lock_meta_allocator_move_extents(inode, &context->et, 238 *len, 1, 239 &context->meta_ac, 240 extra_blocks, &credits); 241 if (ret) { 242 mlog_errno(ret); 243 goto out; 244 } 245 246 /* 247 * should be using allocation reservation strategy there? 248 * 249 * if (context->data_ac) 250 * context->data_ac->ac_resv = &OCFS2_I(inode)->ip_la_data_resv; 251 */ 252 253 inode_lock(tl_inode); 254 255 if (ocfs2_truncate_log_needs_flush(osb)) { 256 ret = __ocfs2_flush_truncate_log(osb); 257 if (ret < 0) { 258 mlog_errno(ret); 259 goto out_unlock_mutex; 260 } 261 } 262 263 /* 264 * Make sure ocfs2_reserve_cluster is called after 265 * __ocfs2_flush_truncate_log, otherwise, dead lock may happen. 266 * 267 * If ocfs2_reserve_cluster is called 268 * before __ocfs2_flush_truncate_log, dead lock on global bitmap 269 * may happen. 270 * 271 */ 272 ret = ocfs2_reserve_clusters(osb, *len, &context->data_ac); 273 if (ret) { 274 mlog_errno(ret); 275 goto out_unlock_mutex; 276 } 277 278 handle = ocfs2_start_trans(osb, credits); 279 if (IS_ERR(handle)) { 280 ret = PTR_ERR(handle); 281 mlog_errno(ret); 282 goto out_unlock_mutex; 283 } 284 285 ret = __ocfs2_claim_clusters(handle, context->data_ac, 1, *len, 286 &new_phys_cpos, &new_len); 287 if (ret) { 288 mlog_errno(ret); 289 goto out_commit; 290 } 291 292 /* 293 * allowing partial extent moving is kind of 'pros and cons', it makes 294 * whole defragmentation less likely to fail, on the contrary, the bad 295 * thing is it may make the fs even more fragmented after moving, let 296 * userspace make a good decision here. 297 */ 298 if (new_len != *len) { 299 mlog(0, "len_claimed: %u, len: %u\n", new_len, *len); 300 if (!partial) { 301 context->range->me_flags &= ~OCFS2_MOVE_EXT_FL_COMPLETE; 302 ret = -ENOSPC; 303 need_free = 1; 304 goto out_commit; 305 } 306 } 307 308 mlog(0, "cpos: %u, phys_cpos: %u, new_phys_cpos: %u\n", cpos, 309 phys_cpos, new_phys_cpos); 310 311 ret = __ocfs2_move_extent(handle, context, cpos, new_len, phys_cpos, 312 new_phys_cpos, ext_flags); 313 if (ret) 314 mlog_errno(ret); 315 316 if (partial && (new_len != *len)) 317 *len = new_len; 318 319 /* 320 * Here we should write the new page out first if we are 321 * in write-back mode. 322 */ 323 ret = ocfs2_cow_sync_writeback(inode->i_sb, context->inode, cpos, *len); 324 if (ret) 325 mlog_errno(ret); 326 327 out_commit: 328 if (need_free && context->data_ac) { 329 struct ocfs2_alloc_context *data_ac = context->data_ac; 330 331 if (context->data_ac->ac_which == OCFS2_AC_USE_LOCAL) 332 ocfs2_free_local_alloc_bits(osb, handle, data_ac, 333 new_phys_cpos, new_len); 334 else 335 ocfs2_free_clusters(handle, 336 data_ac->ac_inode, 337 data_ac->ac_bh, 338 ocfs2_clusters_to_blocks(osb->sb, new_phys_cpos), 339 new_len); 340 } 341 342 ocfs2_commit_trans(osb, handle); 343 344 out_unlock_mutex: 345 inode_unlock(tl_inode); 346 347 if (context->data_ac) { 348 ocfs2_free_alloc_context(context->data_ac); 349 context->data_ac = NULL; 350 } 351 352 if (context->meta_ac) { 353 ocfs2_free_alloc_context(context->meta_ac); 354 context->meta_ac = NULL; 355 } 356 357 out: 358 if (ref_tree) 359 ocfs2_unlock_refcount_tree(osb, ref_tree, 1); 360 361 return ret; 362 } 363 364 /* 365 * find the victim alloc group, where #blkno fits. 366 */ 367 static int ocfs2_find_victim_alloc_group(struct inode *inode, 368 u64 vict_blkno, 369 int type, int slot, 370 int *vict_bit, 371 struct buffer_head **ret_bh) 372 { 373 int ret, i, len, bits_per_unit = 0; 374 u64 blkno; 375 char namebuf[40]; 376 377 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 378 struct buffer_head *ac_bh = NULL, *gd_bh = NULL; 379 struct ocfs2_chain_list *cl; 380 struct ocfs2_chain_rec *rec; 381 struct ocfs2_dinode *ac_dinode; 382 struct ocfs2_group_desc *bg; 383 384 len = ocfs2_sprintf_system_inode_name(namebuf, sizeof(namebuf), type, slot); 385 ret = ocfs2_lookup_ino_from_name(osb->sys_root_inode, namebuf, len, &blkno); 386 387 if (ret) { 388 ret = -ENOENT; 389 goto out; 390 } 391 392 ret = ocfs2_read_blocks_sync(osb, blkno, 1, &ac_bh); 393 if (ret) { 394 mlog_errno(ret); 395 goto out; 396 } 397 398 ac_dinode = (struct ocfs2_dinode *)ac_bh->b_data; 399 cl = &(ac_dinode->id2.i_chain); 400 rec = &(cl->cl_recs[0]); 401 402 if (type == GLOBAL_BITMAP_SYSTEM_INODE) 403 bits_per_unit = osb->s_clustersize_bits - 404 inode->i_sb->s_blocksize_bits; 405 /* 406 * 'vict_blkno' was out of the valid range. 407 */ 408 if ((vict_blkno < le64_to_cpu(rec->c_blkno)) || 409 (vict_blkno >= ((u64)le32_to_cpu(ac_dinode->id1.bitmap1.i_total) << 410 bits_per_unit))) { 411 ret = -EINVAL; 412 goto out; 413 } 414 415 for (i = 0; i < le16_to_cpu(cl->cl_next_free_rec); i++) { 416 417 rec = &(cl->cl_recs[i]); 418 if (!rec) 419 continue; 420 421 bg = NULL; 422 423 do { 424 if (!bg) 425 blkno = le64_to_cpu(rec->c_blkno); 426 else 427 blkno = le64_to_cpu(bg->bg_next_group); 428 429 if (gd_bh) { 430 brelse(gd_bh); 431 gd_bh = NULL; 432 } 433 434 ret = ocfs2_read_blocks_sync(osb, blkno, 1, &gd_bh); 435 if (ret) { 436 mlog_errno(ret); 437 goto out; 438 } 439 440 bg = (struct ocfs2_group_desc *)gd_bh->b_data; 441 442 if (vict_blkno < (le64_to_cpu(bg->bg_blkno) + 443 (le16_to_cpu(bg->bg_bits) << bits_per_unit))) { 444 445 *ret_bh = gd_bh; 446 *vict_bit = (vict_blkno - blkno) >> 447 bits_per_unit; 448 mlog(0, "find the victim group: #%llu, " 449 "total_bits: %u, vict_bit: %u\n", 450 blkno, le16_to_cpu(bg->bg_bits), 451 *vict_bit); 452 goto out; 453 } 454 455 } while (le64_to_cpu(bg->bg_next_group)); 456 } 457 458 ret = -EINVAL; 459 out: 460 brelse(ac_bh); 461 462 /* 463 * caller has to release the gd_bh properly. 464 */ 465 return ret; 466 } 467 468 /* 469 * XXX: helper to validate and adjust moving goal. 470 */ 471 static int ocfs2_validate_and_adjust_move_goal(struct inode *inode, 472 struct ocfs2_move_extents *range) 473 { 474 int ret, goal_bit = 0; 475 476 struct buffer_head *gd_bh = NULL; 477 struct ocfs2_group_desc *bg; 478 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 479 int c_to_b = 1 << (osb->s_clustersize_bits - 480 inode->i_sb->s_blocksize_bits); 481 482 /* 483 * make goal become cluster aligned. 484 */ 485 range->me_goal = ocfs2_block_to_cluster_start(inode->i_sb, 486 range->me_goal); 487 /* 488 * validate goal sits within global_bitmap, and return the victim 489 * group desc 490 */ 491 ret = ocfs2_find_victim_alloc_group(inode, range->me_goal, 492 GLOBAL_BITMAP_SYSTEM_INODE, 493 OCFS2_INVALID_SLOT, 494 &goal_bit, &gd_bh); 495 if (ret) 496 goto out; 497 498 bg = (struct ocfs2_group_desc *)gd_bh->b_data; 499 500 /* 501 * moving goal is not allowed to start with a group desc blok(#0 blk) 502 * let's compromise to the latter cluster. 503 */ 504 if (range->me_goal == le64_to_cpu(bg->bg_blkno)) 505 range->me_goal += c_to_b; 506 507 /* 508 * movement is not gonna cross two groups. 509 */ 510 if ((le16_to_cpu(bg->bg_bits) - goal_bit) * osb->s_clustersize < 511 range->me_len) { 512 ret = -EINVAL; 513 goto out; 514 } 515 /* 516 * more exact validations/adjustments will be performed later during 517 * moving operation for each extent range. 518 */ 519 mlog(0, "extents get ready to be moved to #%llu block\n", 520 range->me_goal); 521 522 out: 523 brelse(gd_bh); 524 525 return ret; 526 } 527 528 static void ocfs2_probe_alloc_group(struct inode *inode, struct buffer_head *bh, 529 int *goal_bit, u32 move_len, u32 max_hop, 530 u32 *phys_cpos) 531 { 532 int i, used, last_free_bits = 0, base_bit = *goal_bit; 533 struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)bh->b_data; 534 u32 base_cpos = ocfs2_blocks_to_clusters(inode->i_sb, 535 le64_to_cpu(gd->bg_blkno)); 536 537 *phys_cpos = 0; 538 539 for (i = base_bit; i < le16_to_cpu(gd->bg_bits); i++) { 540 541 used = ocfs2_test_bit(i, (unsigned long *)gd->bg_bitmap); 542 if (used) { 543 /* 544 * we even tried searching the free chunk by jumping 545 * a 'max_hop' distance, but still failed. 546 */ 547 if ((i - base_bit) > max_hop) { 548 *phys_cpos = 0; 549 break; 550 } 551 552 if (last_free_bits) 553 last_free_bits = 0; 554 555 continue; 556 } else 557 last_free_bits++; 558 559 if (last_free_bits == move_len) { 560 i = i - move_len + 1; 561 *goal_bit = i; 562 *phys_cpos = base_cpos + i; 563 break; 564 } 565 } 566 567 mlog(0, "found phys_cpos: %u to fit the wanted moving.\n", *phys_cpos); 568 } 569 570 static int ocfs2_move_extent(struct ocfs2_move_extents_context *context, 571 u32 cpos, u32 phys_cpos, u32 *new_phys_cpos, 572 u32 len, int ext_flags) 573 { 574 int ret, credits = 0, extra_blocks = 0, goal_bit = 0; 575 handle_t *handle; 576 struct inode *inode = context->inode; 577 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 578 struct inode *tl_inode = osb->osb_tl_inode; 579 struct inode *gb_inode = NULL; 580 struct buffer_head *gb_bh = NULL; 581 struct buffer_head *gd_bh = NULL; 582 struct ocfs2_group_desc *gd; 583 struct ocfs2_refcount_tree *ref_tree = NULL; 584 u32 move_max_hop = ocfs2_blocks_to_clusters(inode->i_sb, 585 context->range->me_threshold); 586 u64 phys_blkno, new_phys_blkno; 587 588 phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, phys_cpos); 589 590 if ((ext_flags & OCFS2_EXT_REFCOUNTED) && len) { 591 BUG_ON(!ocfs2_is_refcount_inode(inode)); 592 BUG_ON(!context->refcount_loc); 593 594 ret = ocfs2_lock_refcount_tree(osb, context->refcount_loc, 1, 595 &ref_tree, NULL); 596 if (ret) { 597 mlog_errno(ret); 598 return ret; 599 } 600 601 ret = ocfs2_prepare_refcount_change_for_del(inode, 602 context->refcount_loc, 603 phys_blkno, 604 len, 605 &credits, 606 &extra_blocks); 607 if (ret) { 608 mlog_errno(ret); 609 goto out; 610 } 611 } 612 613 ret = ocfs2_lock_meta_allocator_move_extents(inode, &context->et, 614 len, 1, 615 &context->meta_ac, 616 extra_blocks, &credits); 617 if (ret) { 618 mlog_errno(ret); 619 goto out; 620 } 621 622 /* 623 * need to count 2 extra credits for global_bitmap inode and 624 * group descriptor. 625 */ 626 credits += OCFS2_INODE_UPDATE_CREDITS + 1; 627 628 inode_lock(tl_inode); 629 630 /* 631 * ocfs2_move_extent() didn't reserve any clusters in lock_allocators() 632 * logic, while we still need to lock the global_bitmap. 633 */ 634 gb_inode = ocfs2_get_system_file_inode(osb, GLOBAL_BITMAP_SYSTEM_INODE, 635 OCFS2_INVALID_SLOT); 636 if (!gb_inode) { 637 mlog(ML_ERROR, "unable to get global_bitmap inode\n"); 638 ret = -EIO; 639 goto out_unlock_tl_inode; 640 } 641 642 inode_lock(gb_inode); 643 644 ret = ocfs2_inode_lock(gb_inode, &gb_bh, 1); 645 if (ret) { 646 mlog_errno(ret); 647 goto out_unlock_gb_inode; 648 } 649 650 handle = ocfs2_start_trans(osb, credits); 651 if (IS_ERR(handle)) { 652 ret = PTR_ERR(handle); 653 mlog_errno(ret); 654 goto out_unlock; 655 } 656 657 new_phys_blkno = ocfs2_clusters_to_blocks(inode->i_sb, *new_phys_cpos); 658 ret = ocfs2_find_victim_alloc_group(inode, new_phys_blkno, 659 GLOBAL_BITMAP_SYSTEM_INODE, 660 OCFS2_INVALID_SLOT, 661 &goal_bit, &gd_bh); 662 if (ret) { 663 mlog_errno(ret); 664 goto out_commit; 665 } 666 667 gd = (struct ocfs2_group_desc *)gd_bh->b_data; 668 if (le16_to_cpu(gd->bg_free_bits_count) < len) { 669 ret = -ENOSPC; 670 goto out_commit; 671 } 672 673 /* 674 * probe the victim cluster group to find a proper 675 * region to fit wanted movement, it even will perform 676 * a best-effort attempt by compromising to a threshold 677 * around the goal. 678 */ 679 ocfs2_probe_alloc_group(inode, gd_bh, &goal_bit, len, move_max_hop, 680 new_phys_cpos); 681 if (!*new_phys_cpos) { 682 ret = -ENOSPC; 683 goto out_commit; 684 } 685 686 ret = __ocfs2_move_extent(handle, context, cpos, len, phys_cpos, 687 *new_phys_cpos, ext_flags); 688 if (ret) { 689 mlog_errno(ret); 690 goto out_commit; 691 } 692 693 ret = ocfs2_alloc_dinode_update_counts(gb_inode, handle, gb_bh, len, 694 le16_to_cpu(gd->bg_chain)); 695 if (ret) { 696 mlog_errno(ret); 697 goto out_commit; 698 } 699 700 ret = ocfs2_block_group_set_bits(handle, gb_inode, gd, gd_bh, 701 goal_bit, len, 0, 0); 702 if (ret) { 703 ocfs2_rollback_alloc_dinode_counts(gb_inode, gb_bh, len, 704 le16_to_cpu(gd->bg_chain)); 705 mlog_errno(ret); 706 } 707 708 /* 709 * Here we should write the new page out first if we are 710 * in write-back mode. 711 */ 712 ret = ocfs2_cow_sync_writeback(inode->i_sb, context->inode, cpos, len); 713 if (ret) 714 mlog_errno(ret); 715 716 out_commit: 717 ocfs2_commit_trans(osb, handle); 718 brelse(gd_bh); 719 out_unlock: 720 ocfs2_inode_unlock(gb_inode, 1); 721 out_unlock_gb_inode: 722 inode_unlock(gb_inode); 723 brelse(gb_bh); 724 iput(gb_inode); 725 out_unlock_tl_inode: 726 inode_unlock(tl_inode); 727 728 out: 729 if (context->meta_ac) { 730 ocfs2_free_alloc_context(context->meta_ac); 731 context->meta_ac = NULL; 732 } 733 734 if (ref_tree) 735 ocfs2_unlock_refcount_tree(osb, ref_tree, 1); 736 737 return ret; 738 } 739 740 /* 741 * Helper to calculate the defraging length in one run according to threshold. 742 */ 743 static void ocfs2_calc_extent_defrag_len(u32 *alloc_size, u32 *len_defraged, 744 u32 threshold, int *skip) 745 { 746 if ((*alloc_size + *len_defraged) < threshold) { 747 /* 748 * proceed defragmentation until we meet the thresh 749 */ 750 *len_defraged += *alloc_size; 751 } else if (*len_defraged == 0) { 752 /* 753 * XXX: skip a large extent. 754 */ 755 *skip = 1; 756 } else { 757 /* 758 * split this extent to coalesce with former pieces as 759 * to reach the threshold. 760 * 761 * we're done here with one cycle of defragmentation 762 * in a size of 'thresh', resetting 'len_defraged' 763 * forces a new defragmentation. 764 */ 765 *alloc_size = threshold - *len_defraged; 766 *len_defraged = 0; 767 } 768 } 769 770 static int __ocfs2_move_extents_range(struct buffer_head *di_bh, 771 struct ocfs2_move_extents_context *context) 772 { 773 int ret = 0, flags, do_defrag, skip = 0; 774 u32 cpos, phys_cpos, move_start, len_to_move, alloc_size; 775 u32 len_defraged = 0, defrag_thresh = 0, new_phys_cpos = 0; 776 777 struct inode *inode = context->inode; 778 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data; 779 struct ocfs2_move_extents *range = context->range; 780 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 781 782 if ((i_size_read(inode) == 0) || (range->me_len == 0)) 783 return 0; 784 785 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) 786 return 0; 787 788 context->refcount_loc = le64_to_cpu(di->i_refcount_loc); 789 790 ocfs2_init_dinode_extent_tree(&context->et, INODE_CACHE(inode), di_bh); 791 ocfs2_init_dealloc_ctxt(&context->dealloc); 792 793 /* 794 * TO-DO XXX: 795 * 796 * - xattr extents. 797 */ 798 799 do_defrag = context->auto_defrag; 800 801 /* 802 * extents moving happens in unit of clusters, for the sake 803 * of simplicity, we may ignore two clusters where 'byte_start' 804 * and 'byte_start + len' were within. 805 */ 806 move_start = ocfs2_clusters_for_bytes(osb->sb, range->me_start); 807 len_to_move = (range->me_start + range->me_len) >> 808 osb->s_clustersize_bits; 809 if (len_to_move >= move_start) 810 len_to_move -= move_start; 811 else 812 len_to_move = 0; 813 814 if (do_defrag) { 815 defrag_thresh = range->me_threshold >> osb->s_clustersize_bits; 816 if (defrag_thresh <= 1) 817 goto done; 818 } else 819 new_phys_cpos = ocfs2_blocks_to_clusters(inode->i_sb, 820 range->me_goal); 821 822 mlog(0, "Inode: %llu, start: %llu, len: %llu, cstart: %u, clen: %u, " 823 "thresh: %u\n", 824 (unsigned long long)OCFS2_I(inode)->ip_blkno, 825 (unsigned long long)range->me_start, 826 (unsigned long long)range->me_len, 827 move_start, len_to_move, defrag_thresh); 828 829 cpos = move_start; 830 while (len_to_move) { 831 ret = ocfs2_get_clusters(inode, cpos, &phys_cpos, &alloc_size, 832 &flags); 833 if (ret) { 834 mlog_errno(ret); 835 goto out; 836 } 837 838 if (alloc_size > len_to_move) 839 alloc_size = len_to_move; 840 841 /* 842 * XXX: how to deal with a hole: 843 * 844 * - skip the hole of course 845 * - force a new defragmentation 846 */ 847 if (!phys_cpos) { 848 if (do_defrag) 849 len_defraged = 0; 850 851 goto next; 852 } 853 854 if (do_defrag) { 855 ocfs2_calc_extent_defrag_len(&alloc_size, &len_defraged, 856 defrag_thresh, &skip); 857 /* 858 * skip large extents 859 */ 860 if (skip) { 861 skip = 0; 862 goto next; 863 } 864 865 mlog(0, "#Defrag: cpos: %u, phys_cpos: %u, " 866 "alloc_size: %u, len_defraged: %u\n", 867 cpos, phys_cpos, alloc_size, len_defraged); 868 869 ret = ocfs2_defrag_extent(context, cpos, phys_cpos, 870 &alloc_size, flags); 871 } else { 872 ret = ocfs2_move_extent(context, cpos, phys_cpos, 873 &new_phys_cpos, alloc_size, 874 flags); 875 876 new_phys_cpos += alloc_size; 877 } 878 879 if (ret < 0) { 880 mlog_errno(ret); 881 goto out; 882 } 883 /* 884 * Invalidate extent cache after moving/defragging to prevent 885 * stale cached data with outdated extent flags. 886 */ 887 ocfs2_extent_map_trunc(inode, cpos); 888 889 context->clusters_moved += alloc_size; 890 next: 891 cpos += alloc_size; 892 len_to_move -= alloc_size; 893 } 894 895 done: 896 range->me_flags |= OCFS2_MOVE_EXT_FL_COMPLETE; 897 898 out: 899 range->me_moved_len = ocfs2_clusters_to_bytes(osb->sb, 900 context->clusters_moved); 901 range->me_new_offset = ocfs2_clusters_to_bytes(osb->sb, 902 context->new_phys_cpos); 903 904 ocfs2_schedule_truncate_log_flush(osb, 1); 905 ocfs2_run_deallocs(osb, &context->dealloc); 906 907 return ret; 908 } 909 910 static int ocfs2_move_extents(struct ocfs2_move_extents_context *context) 911 { 912 int status; 913 handle_t *handle; 914 struct inode *inode = context->inode; 915 struct ocfs2_dinode *di; 916 struct buffer_head *di_bh = NULL; 917 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 918 919 if (unlikely(ocfs2_emergency_state(osb))) 920 return -EROFS; 921 922 inode_lock(inode); 923 924 /* 925 * This prevents concurrent writes from other nodes 926 */ 927 status = ocfs2_rw_lock(inode, 1); 928 if (status) { 929 mlog_errno(status); 930 goto out; 931 } 932 933 status = ocfs2_inode_lock(inode, &di_bh, 1); 934 if (status) { 935 mlog_errno(status); 936 goto out_rw_unlock; 937 } 938 939 /* 940 * remember ip_xattr_sem also needs to be held if necessary 941 */ 942 down_write(&OCFS2_I(inode)->ip_alloc_sem); 943 944 status = __ocfs2_move_extents_range(di_bh, context); 945 946 up_write(&OCFS2_I(inode)->ip_alloc_sem); 947 if (status) { 948 mlog_errno(status); 949 goto out_inode_unlock; 950 } 951 952 /* 953 * We update ctime for these changes 954 */ 955 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS); 956 if (IS_ERR(handle)) { 957 status = PTR_ERR(handle); 958 mlog_errno(status); 959 goto out_inode_unlock; 960 } 961 962 status = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh, 963 OCFS2_JOURNAL_ACCESS_WRITE); 964 if (status) { 965 mlog_errno(status); 966 goto out_commit; 967 } 968 969 di = (struct ocfs2_dinode *)di_bh->b_data; 970 inode_set_ctime_current(inode); 971 di->i_ctime = cpu_to_le64(inode_get_ctime_sec(inode)); 972 di->i_ctime_nsec = cpu_to_le32(inode_get_ctime_nsec(inode)); 973 ocfs2_update_inode_fsync_trans(handle, inode, 0); 974 975 ocfs2_journal_dirty(handle, di_bh); 976 977 out_commit: 978 ocfs2_commit_trans(osb, handle); 979 980 out_inode_unlock: 981 brelse(di_bh); 982 ocfs2_inode_unlock(inode, 1); 983 out_rw_unlock: 984 ocfs2_rw_unlock(inode, 1); 985 out: 986 inode_unlock(inode); 987 988 return status; 989 } 990 991 int ocfs2_ioctl_move_extents(struct file *filp, void __user *argp) 992 { 993 int status; 994 995 struct inode *inode = file_inode(filp); 996 struct ocfs2_move_extents range; 997 struct ocfs2_move_extents_context *context; 998 999 if (!argp) 1000 return -EINVAL; 1001 1002 status = mnt_want_write_file(filp); 1003 if (status) 1004 return status; 1005 1006 if ((!S_ISREG(inode->i_mode)) || !(filp->f_mode & FMODE_WRITE)) { 1007 status = -EPERM; 1008 goto out_drop; 1009 } 1010 1011 if (inode->i_flags & (S_IMMUTABLE|S_APPEND)) { 1012 status = -EPERM; 1013 goto out_drop; 1014 } 1015 1016 context = kzalloc_obj(struct ocfs2_move_extents_context, GFP_NOFS); 1017 if (!context) { 1018 status = -ENOMEM; 1019 mlog_errno(status); 1020 goto out_drop; 1021 } 1022 1023 context->inode = inode; 1024 context->file = filp; 1025 1026 if (copy_from_user(&range, argp, sizeof(range))) { 1027 status = -EFAULT; 1028 goto out_free; 1029 } 1030 1031 if (range.me_start > i_size_read(inode)) { 1032 status = -EINVAL; 1033 goto out_free; 1034 } 1035 1036 if (range.me_start + range.me_len > i_size_read(inode)) 1037 range.me_len = i_size_read(inode) - range.me_start; 1038 1039 context->range = ⦥ 1040 1041 /* 1042 * ok, the default threshold for the defragmentation 1043 * is 1M, since our maximum clustersize was 1M also. 1044 * any thought? 1045 */ 1046 if (!range.me_threshold) 1047 range.me_threshold = 1024 * 1024; 1048 1049 if (range.me_threshold > i_size_read(inode)) 1050 range.me_threshold = i_size_read(inode); 1051 1052 if (range.me_flags & ~(OCFS2_MOVE_EXT_FL_AUTO_DEFRAG | 1053 OCFS2_MOVE_EXT_FL_PART_DEFRAG)) { 1054 status = -EINVAL; 1055 goto out_free; 1056 } 1057 1058 if (range.me_flags & OCFS2_MOVE_EXT_FL_AUTO_DEFRAG) { 1059 context->auto_defrag = 1; 1060 1061 if (range.me_flags & OCFS2_MOVE_EXT_FL_PART_DEFRAG) 1062 context->partial = 1; 1063 } else { 1064 /* 1065 * first best-effort attempt to validate and adjust the goal 1066 * (physical address in block), while it can't guarantee later 1067 * operation can succeed all the time since global_bitmap may 1068 * change a bit over time. 1069 */ 1070 1071 status = ocfs2_validate_and_adjust_move_goal(inode, &range); 1072 if (status) 1073 goto out_copy; 1074 } 1075 1076 status = ocfs2_move_extents(context); 1077 if (status) 1078 mlog_errno(status); 1079 out_copy: 1080 /* 1081 * movement/defragmentation may end up being partially completed, 1082 * that's the reason why we need to return userspace the finished 1083 * length and new_offset even if failure happens somewhere. 1084 */ 1085 if (copy_to_user(argp, &range, sizeof(range))) 1086 status = -EFAULT; 1087 1088 out_free: 1089 kfree(context); 1090 out_drop: 1091 mnt_drop_write_file(filp); 1092 1093 return status; 1094 } 1095