Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

btrfs: remove dead snapshot-aware defrag code

Snapshot-aware defrag has been disabled since commit 8101c8dbf624
("Btrfs: disable snapshot aware defrag for now") almost 6 years ago.
Let's remove the dead code. If someone is up to the task of bringing it
back, they can dig it up from git.

This is logically a revert of commit 38c227d87c49 ("Btrfs:
snapshot-aware defrag") except that now we have to clear the
EXTENT_DEFRAG bit to avoid need_force_cow() returning true forever.

The reasons to disable were caused by runtime problems (like long stalls
or memory consumption) on heavily referenced extents (eg. thousands of
snapshots). There were attempts to fix that but never finished.

Current defrag breaks the extent references and some users prefer that
behaviour over the one implemented by snapshot aware (ie. keeping links
for defragmentation). To enable both usecases we'd need to extend
defrag ioctl but let's do that properly from scratch.

Reviewed-by: Nikolay Borisov <nborisov@suse.com>
Reviewed-by: Johannes Thumshirn <jthumshirn@suse.de>
Signed-off-by: Omar Sandoval <osandov@fb.com>
Reviewed-by: David Sterba <dsterba@suse.com>
[ enhance ]
Signed-off-by: David Sterba <dsterba@suse.com>

authored by

Omar Sandoval and committed by
David Sterba
313facc5 db72e47f

+11 -684
+11 -684
fs/btrfs/inode.c
··· 44 44 #include "locking.h" 45 45 #include "free-space-cache.h" 46 46 #include "inode-map.h" 47 - #include "backref.h" 48 47 #include "props.h" 49 48 #include "qgroup.h" 50 49 #include "delalloc-space.h" ··· 2392 2393 return ret; 2393 2394 } 2394 2395 2395 - /* snapshot-aware defrag */ 2396 - struct sa_defrag_extent_backref { 2397 - struct rb_node node; 2398 - struct old_sa_defrag_extent *old; 2399 - u64 root_id; 2400 - u64 inum; 2401 - u64 file_pos; 2402 - u64 extent_offset; 2403 - u64 num_bytes; 2404 - u64 generation; 2405 - }; 2406 - 2407 - struct old_sa_defrag_extent { 2408 - struct list_head list; 2409 - struct new_sa_defrag_extent *new; 2410 - 2411 - u64 extent_offset; 2412 - u64 bytenr; 2413 - u64 offset; 2414 - u64 len; 2415 - int count; 2416 - }; 2417 - 2418 - struct new_sa_defrag_extent { 2419 - struct rb_root root; 2420 - struct list_head head; 2421 - struct btrfs_path *path; 2422 - struct inode *inode; 2423 - u64 file_pos; 2424 - u64 len; 2425 - u64 bytenr; 2426 - u64 disk_len; 2427 - u8 compress_type; 2428 - }; 2429 - 2430 - static int backref_comp(struct sa_defrag_extent_backref *b1, 2431 - struct sa_defrag_extent_backref *b2) 2432 - { 2433 - if (b1->root_id < b2->root_id) 2434 - return -1; 2435 - else if (b1->root_id > b2->root_id) 2436 - return 1; 2437 - 2438 - if (b1->inum < b2->inum) 2439 - return -1; 2440 - else if (b1->inum > b2->inum) 2441 - return 1; 2442 - 2443 - if (b1->file_pos < b2->file_pos) 2444 - return -1; 2445 - else if (b1->file_pos > b2->file_pos) 2446 - return 1; 2447 - 2448 - /* 2449 - * [------------------------------] ===> (a range of space) 2450 - * |<--->| |<---->| =============> (fs/file tree A) 2451 - * |<---------------------------->| ===> (fs/file tree B) 2452 - * 2453 - * A range of space can refer to two file extents in one tree while 2454 - * refer to only one file extent in another tree. 2455 - * 2456 - * So we may process a disk offset more than one time(two extents in A) 2457 - * and locate at the same extent(one extent in B), then insert two same 2458 - * backrefs(both refer to the extent in B). 2459 - */ 2460 - return 0; 2461 - } 2462 - 2463 - static void backref_insert(struct rb_root *root, 2464 - struct sa_defrag_extent_backref *backref) 2465 - { 2466 - struct rb_node **p = &root->rb_node; 2467 - struct rb_node *parent = NULL; 2468 - struct sa_defrag_extent_backref *entry; 2469 - int ret; 2470 - 2471 - while (*p) { 2472 - parent = *p; 2473 - entry = rb_entry(parent, struct sa_defrag_extent_backref, node); 2474 - 2475 - ret = backref_comp(backref, entry); 2476 - if (ret < 0) 2477 - p = &(*p)->rb_left; 2478 - else 2479 - p = &(*p)->rb_right; 2480 - } 2481 - 2482 - rb_link_node(&backref->node, parent, p); 2483 - rb_insert_color(&backref->node, root); 2484 - } 2485 - 2486 - /* 2487 - * Note the backref might has changed, and in this case we just return 0. 2488 - */ 2489 - static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id, 2490 - void *ctx) 2491 - { 2492 - struct btrfs_file_extent_item *extent; 2493 - struct old_sa_defrag_extent *old = ctx; 2494 - struct new_sa_defrag_extent *new = old->new; 2495 - struct btrfs_path *path = new->path; 2496 - struct btrfs_key key; 2497 - struct btrfs_root *root; 2498 - struct sa_defrag_extent_backref *backref; 2499 - struct extent_buffer *leaf; 2500 - struct inode *inode = new->inode; 2501 - struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 2502 - int slot; 2503 - int ret; 2504 - u64 extent_offset; 2505 - u64 num_bytes; 2506 - 2507 - if (BTRFS_I(inode)->root->root_key.objectid == root_id && 2508 - inum == btrfs_ino(BTRFS_I(inode))) 2509 - return 0; 2510 - 2511 - key.objectid = root_id; 2512 - key.type = BTRFS_ROOT_ITEM_KEY; 2513 - key.offset = (u64)-1; 2514 - 2515 - root = btrfs_read_fs_root_no_name(fs_info, &key); 2516 - if (IS_ERR(root)) { 2517 - if (PTR_ERR(root) == -ENOENT) 2518 - return 0; 2519 - WARN_ON(1); 2520 - btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu", 2521 - inum, offset, root_id); 2522 - return PTR_ERR(root); 2523 - } 2524 - 2525 - key.objectid = inum; 2526 - key.type = BTRFS_EXTENT_DATA_KEY; 2527 - if (offset > (u64)-1 << 32) 2528 - key.offset = 0; 2529 - else 2530 - key.offset = offset; 2531 - 2532 - ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 2533 - if (WARN_ON(ret < 0)) 2534 - return ret; 2535 - ret = 0; 2536 - 2537 - while (1) { 2538 - cond_resched(); 2539 - 2540 - leaf = path->nodes[0]; 2541 - slot = path->slots[0]; 2542 - 2543 - if (slot >= btrfs_header_nritems(leaf)) { 2544 - ret = btrfs_next_leaf(root, path); 2545 - if (ret < 0) { 2546 - goto out; 2547 - } else if (ret > 0) { 2548 - ret = 0; 2549 - goto out; 2550 - } 2551 - continue; 2552 - } 2553 - 2554 - path->slots[0]++; 2555 - 2556 - btrfs_item_key_to_cpu(leaf, &key, slot); 2557 - 2558 - if (key.objectid > inum) 2559 - goto out; 2560 - 2561 - if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY) 2562 - continue; 2563 - 2564 - extent = btrfs_item_ptr(leaf, slot, 2565 - struct btrfs_file_extent_item); 2566 - 2567 - if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr) 2568 - continue; 2569 - 2570 - /* 2571 - * 'offset' refers to the exact key.offset, 2572 - * NOT the 'offset' field in btrfs_extent_data_ref, ie. 2573 - * (key.offset - extent_offset). 2574 - */ 2575 - if (key.offset != offset) 2576 - continue; 2577 - 2578 - extent_offset = btrfs_file_extent_offset(leaf, extent); 2579 - num_bytes = btrfs_file_extent_num_bytes(leaf, extent); 2580 - 2581 - if (extent_offset >= old->extent_offset + old->offset + 2582 - old->len || extent_offset + num_bytes <= 2583 - old->extent_offset + old->offset) 2584 - continue; 2585 - break; 2586 - } 2587 - 2588 - backref = kmalloc(sizeof(*backref), GFP_NOFS); 2589 - if (!backref) { 2590 - ret = -ENOENT; 2591 - goto out; 2592 - } 2593 - 2594 - backref->root_id = root_id; 2595 - backref->inum = inum; 2596 - backref->file_pos = offset; 2597 - backref->num_bytes = num_bytes; 2598 - backref->extent_offset = extent_offset; 2599 - backref->generation = btrfs_file_extent_generation(leaf, extent); 2600 - backref->old = old; 2601 - backref_insert(&new->root, backref); 2602 - old->count++; 2603 - out: 2604 - btrfs_release_path(path); 2605 - WARN_ON(ret); 2606 - return ret; 2607 - } 2608 - 2609 - static noinline bool record_extent_backrefs(struct btrfs_path *path, 2610 - struct new_sa_defrag_extent *new) 2611 - { 2612 - struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb); 2613 - struct old_sa_defrag_extent *old, *tmp; 2614 - int ret; 2615 - 2616 - new->path = path; 2617 - 2618 - list_for_each_entry_safe(old, tmp, &new->head, list) { 2619 - ret = iterate_inodes_from_logical(old->bytenr + 2620 - old->extent_offset, fs_info, 2621 - path, record_one_backref, 2622 - old, false); 2623 - if (ret < 0 && ret != -ENOENT) 2624 - return false; 2625 - 2626 - /* no backref to be processed for this extent */ 2627 - if (!old->count) { 2628 - list_del(&old->list); 2629 - kfree(old); 2630 - } 2631 - } 2632 - 2633 - if (list_empty(&new->head)) 2634 - return false; 2635 - 2636 - return true; 2637 - } 2638 - 2639 - static int relink_is_mergable(struct extent_buffer *leaf, 2640 - struct btrfs_file_extent_item *fi, 2641 - struct new_sa_defrag_extent *new) 2642 - { 2643 - if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr) 2644 - return 0; 2645 - 2646 - if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG) 2647 - return 0; 2648 - 2649 - if (btrfs_file_extent_compression(leaf, fi) != new->compress_type) 2650 - return 0; 2651 - 2652 - if (btrfs_file_extent_encryption(leaf, fi) || 2653 - btrfs_file_extent_other_encoding(leaf, fi)) 2654 - return 0; 2655 - 2656 - return 1; 2657 - } 2658 - 2659 - /* 2660 - * Note the backref might has changed, and in this case we just return 0. 2661 - */ 2662 - static noinline int relink_extent_backref(struct btrfs_path *path, 2663 - struct sa_defrag_extent_backref *prev, 2664 - struct sa_defrag_extent_backref *backref) 2665 - { 2666 - struct btrfs_file_extent_item *extent; 2667 - struct btrfs_file_extent_item *item; 2668 - struct btrfs_ordered_extent *ordered; 2669 - struct btrfs_trans_handle *trans; 2670 - struct btrfs_ref ref = { 0 }; 2671 - struct btrfs_root *root; 2672 - struct btrfs_key key; 2673 - struct extent_buffer *leaf; 2674 - struct old_sa_defrag_extent *old = backref->old; 2675 - struct new_sa_defrag_extent *new = old->new; 2676 - struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb); 2677 - struct inode *inode; 2678 - struct extent_state *cached = NULL; 2679 - int ret = 0; 2680 - u64 start; 2681 - u64 len; 2682 - u64 lock_start; 2683 - u64 lock_end; 2684 - bool merge = false; 2685 - int index; 2686 - 2687 - if (prev && prev->root_id == backref->root_id && 2688 - prev->inum == backref->inum && 2689 - prev->file_pos + prev->num_bytes == backref->file_pos) 2690 - merge = true; 2691 - 2692 - /* step 1: get root */ 2693 - key.objectid = backref->root_id; 2694 - key.type = BTRFS_ROOT_ITEM_KEY; 2695 - key.offset = (u64)-1; 2696 - 2697 - index = srcu_read_lock(&fs_info->subvol_srcu); 2698 - 2699 - root = btrfs_read_fs_root_no_name(fs_info, &key); 2700 - if (IS_ERR(root)) { 2701 - srcu_read_unlock(&fs_info->subvol_srcu, index); 2702 - if (PTR_ERR(root) == -ENOENT) 2703 - return 0; 2704 - return PTR_ERR(root); 2705 - } 2706 - 2707 - if (btrfs_root_readonly(root)) { 2708 - srcu_read_unlock(&fs_info->subvol_srcu, index); 2709 - return 0; 2710 - } 2711 - 2712 - /* step 2: get inode */ 2713 - key.objectid = backref->inum; 2714 - key.type = BTRFS_INODE_ITEM_KEY; 2715 - key.offset = 0; 2716 - 2717 - inode = btrfs_iget(fs_info->sb, &key, root); 2718 - if (IS_ERR(inode)) { 2719 - srcu_read_unlock(&fs_info->subvol_srcu, index); 2720 - return 0; 2721 - } 2722 - 2723 - srcu_read_unlock(&fs_info->subvol_srcu, index); 2724 - 2725 - /* step 3: relink backref */ 2726 - lock_start = backref->file_pos; 2727 - lock_end = backref->file_pos + backref->num_bytes - 1; 2728 - lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end, 2729 - &cached); 2730 - 2731 - ordered = btrfs_lookup_first_ordered_extent(inode, lock_end); 2732 - if (ordered) { 2733 - btrfs_put_ordered_extent(ordered); 2734 - goto out_unlock; 2735 - } 2736 - 2737 - trans = btrfs_join_transaction(root); 2738 - if (IS_ERR(trans)) { 2739 - ret = PTR_ERR(trans); 2740 - goto out_unlock; 2741 - } 2742 - 2743 - key.objectid = backref->inum; 2744 - key.type = BTRFS_EXTENT_DATA_KEY; 2745 - key.offset = backref->file_pos; 2746 - 2747 - ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 2748 - if (ret < 0) { 2749 - goto out_free_path; 2750 - } else if (ret > 0) { 2751 - ret = 0; 2752 - goto out_free_path; 2753 - } 2754 - 2755 - extent = btrfs_item_ptr(path->nodes[0], path->slots[0], 2756 - struct btrfs_file_extent_item); 2757 - 2758 - if (btrfs_file_extent_generation(path->nodes[0], extent) != 2759 - backref->generation) 2760 - goto out_free_path; 2761 - 2762 - btrfs_release_path(path); 2763 - 2764 - start = backref->file_pos; 2765 - if (backref->extent_offset < old->extent_offset + old->offset) 2766 - start += old->extent_offset + old->offset - 2767 - backref->extent_offset; 2768 - 2769 - len = min(backref->extent_offset + backref->num_bytes, 2770 - old->extent_offset + old->offset + old->len); 2771 - len -= max(backref->extent_offset, old->extent_offset + old->offset); 2772 - 2773 - ret = btrfs_drop_extents(trans, root, inode, start, 2774 - start + len, 1); 2775 - if (ret) 2776 - goto out_free_path; 2777 - again: 2778 - key.objectid = btrfs_ino(BTRFS_I(inode)); 2779 - key.type = BTRFS_EXTENT_DATA_KEY; 2780 - key.offset = start; 2781 - 2782 - path->leave_spinning = 1; 2783 - if (merge) { 2784 - struct btrfs_file_extent_item *fi; 2785 - u64 extent_len; 2786 - struct btrfs_key found_key; 2787 - 2788 - ret = btrfs_search_slot(trans, root, &key, path, 0, 1); 2789 - if (ret < 0) 2790 - goto out_free_path; 2791 - 2792 - path->slots[0]--; 2793 - leaf = path->nodes[0]; 2794 - btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); 2795 - 2796 - fi = btrfs_item_ptr(leaf, path->slots[0], 2797 - struct btrfs_file_extent_item); 2798 - extent_len = btrfs_file_extent_num_bytes(leaf, fi); 2799 - 2800 - if (extent_len + found_key.offset == start && 2801 - relink_is_mergable(leaf, fi, new)) { 2802 - btrfs_set_file_extent_num_bytes(leaf, fi, 2803 - extent_len + len); 2804 - btrfs_mark_buffer_dirty(leaf); 2805 - inode_add_bytes(inode, len); 2806 - 2807 - ret = 1; 2808 - goto out_free_path; 2809 - } else { 2810 - merge = false; 2811 - btrfs_release_path(path); 2812 - goto again; 2813 - } 2814 - } 2815 - 2816 - ret = btrfs_insert_empty_item(trans, root, path, &key, 2817 - sizeof(*extent)); 2818 - if (ret) { 2819 - btrfs_abort_transaction(trans, ret); 2820 - goto out_free_path; 2821 - } 2822 - 2823 - leaf = path->nodes[0]; 2824 - item = btrfs_item_ptr(leaf, path->slots[0], 2825 - struct btrfs_file_extent_item); 2826 - btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr); 2827 - btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len); 2828 - btrfs_set_file_extent_offset(leaf, item, start - new->file_pos); 2829 - btrfs_set_file_extent_num_bytes(leaf, item, len); 2830 - btrfs_set_file_extent_ram_bytes(leaf, item, new->len); 2831 - btrfs_set_file_extent_generation(leaf, item, trans->transid); 2832 - btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG); 2833 - btrfs_set_file_extent_compression(leaf, item, new->compress_type); 2834 - btrfs_set_file_extent_encryption(leaf, item, 0); 2835 - btrfs_set_file_extent_other_encoding(leaf, item, 0); 2836 - 2837 - btrfs_mark_buffer_dirty(leaf); 2838 - inode_add_bytes(inode, len); 2839 - btrfs_release_path(path); 2840 - 2841 - btrfs_init_generic_ref(&ref, BTRFS_ADD_DELAYED_REF, new->bytenr, 2842 - new->disk_len, 0); 2843 - btrfs_init_data_ref(&ref, backref->root_id, backref->inum, 2844 - new->file_pos); /* start - extent_offset */ 2845 - ret = btrfs_inc_extent_ref(trans, &ref); 2846 - if (ret) { 2847 - btrfs_abort_transaction(trans, ret); 2848 - goto out_free_path; 2849 - } 2850 - 2851 - ret = 1; 2852 - out_free_path: 2853 - btrfs_release_path(path); 2854 - path->leave_spinning = 0; 2855 - btrfs_end_transaction(trans); 2856 - out_unlock: 2857 - unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end, 2858 - &cached); 2859 - iput(inode); 2860 - return ret; 2861 - } 2862 - 2863 - static void free_sa_defrag_extent(struct new_sa_defrag_extent *new) 2864 - { 2865 - struct old_sa_defrag_extent *old, *tmp; 2866 - 2867 - if (!new) 2868 - return; 2869 - 2870 - list_for_each_entry_safe(old, tmp, &new->head, list) { 2871 - kfree(old); 2872 - } 2873 - kfree(new); 2874 - } 2875 - 2876 - static void relink_file_extents(struct new_sa_defrag_extent *new) 2877 - { 2878 - struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb); 2879 - struct btrfs_path *path; 2880 - struct sa_defrag_extent_backref *backref; 2881 - struct sa_defrag_extent_backref *prev = NULL; 2882 - struct rb_node *node; 2883 - int ret; 2884 - 2885 - path = btrfs_alloc_path(); 2886 - if (!path) 2887 - return; 2888 - 2889 - if (!record_extent_backrefs(path, new)) { 2890 - btrfs_free_path(path); 2891 - goto out; 2892 - } 2893 - btrfs_release_path(path); 2894 - 2895 - while (1) { 2896 - node = rb_first(&new->root); 2897 - if (!node) 2898 - break; 2899 - rb_erase(node, &new->root); 2900 - 2901 - backref = rb_entry(node, struct sa_defrag_extent_backref, node); 2902 - 2903 - ret = relink_extent_backref(path, prev, backref); 2904 - WARN_ON(ret < 0); 2905 - 2906 - kfree(prev); 2907 - 2908 - if (ret == 1) 2909 - prev = backref; 2910 - else 2911 - prev = NULL; 2912 - cond_resched(); 2913 - } 2914 - kfree(prev); 2915 - 2916 - btrfs_free_path(path); 2917 - out: 2918 - free_sa_defrag_extent(new); 2919 - 2920 - atomic_dec(&fs_info->defrag_running); 2921 - wake_up(&fs_info->transaction_wait); 2922 - } 2923 - 2924 - static struct new_sa_defrag_extent * 2925 - record_old_file_extents(struct inode *inode, 2926 - struct btrfs_ordered_extent *ordered) 2927 - { 2928 - struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); 2929 - struct btrfs_root *root = BTRFS_I(inode)->root; 2930 - struct btrfs_path *path; 2931 - struct btrfs_key key; 2932 - struct old_sa_defrag_extent *old; 2933 - struct new_sa_defrag_extent *new; 2934 - int ret; 2935 - 2936 - new = kmalloc(sizeof(*new), GFP_NOFS); 2937 - if (!new) 2938 - return NULL; 2939 - 2940 - new->inode = inode; 2941 - new->file_pos = ordered->file_offset; 2942 - new->len = ordered->len; 2943 - new->bytenr = ordered->start; 2944 - new->disk_len = ordered->disk_len; 2945 - new->compress_type = ordered->compress_type; 2946 - new->root = RB_ROOT; 2947 - INIT_LIST_HEAD(&new->head); 2948 - 2949 - path = btrfs_alloc_path(); 2950 - if (!path) 2951 - goto out_kfree; 2952 - 2953 - key.objectid = btrfs_ino(BTRFS_I(inode)); 2954 - key.type = BTRFS_EXTENT_DATA_KEY; 2955 - key.offset = new->file_pos; 2956 - 2957 - ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 2958 - if (ret < 0) 2959 - goto out_free_path; 2960 - if (ret > 0 && path->slots[0] > 0) 2961 - path->slots[0]--; 2962 - 2963 - /* find out all the old extents for the file range */ 2964 - while (1) { 2965 - struct btrfs_file_extent_item *extent; 2966 - struct extent_buffer *l; 2967 - int slot; 2968 - u64 num_bytes; 2969 - u64 offset; 2970 - u64 end; 2971 - u64 disk_bytenr; 2972 - u64 extent_offset; 2973 - 2974 - l = path->nodes[0]; 2975 - slot = path->slots[0]; 2976 - 2977 - if (slot >= btrfs_header_nritems(l)) { 2978 - ret = btrfs_next_leaf(root, path); 2979 - if (ret < 0) 2980 - goto out_free_path; 2981 - else if (ret > 0) 2982 - break; 2983 - continue; 2984 - } 2985 - 2986 - btrfs_item_key_to_cpu(l, &key, slot); 2987 - 2988 - if (key.objectid != btrfs_ino(BTRFS_I(inode))) 2989 - break; 2990 - if (key.type != BTRFS_EXTENT_DATA_KEY) 2991 - break; 2992 - if (key.offset >= new->file_pos + new->len) 2993 - break; 2994 - 2995 - extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item); 2996 - 2997 - num_bytes = btrfs_file_extent_num_bytes(l, extent); 2998 - if (key.offset + num_bytes < new->file_pos) 2999 - goto next; 3000 - 3001 - disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent); 3002 - if (!disk_bytenr) 3003 - goto next; 3004 - 3005 - extent_offset = btrfs_file_extent_offset(l, extent); 3006 - 3007 - old = kmalloc(sizeof(*old), GFP_NOFS); 3008 - if (!old) 3009 - goto out_free_path; 3010 - 3011 - offset = max(new->file_pos, key.offset); 3012 - end = min(new->file_pos + new->len, key.offset + num_bytes); 3013 - 3014 - old->bytenr = disk_bytenr; 3015 - old->extent_offset = extent_offset; 3016 - old->offset = offset - key.offset; 3017 - old->len = end - offset; 3018 - old->new = new; 3019 - old->count = 0; 3020 - list_add_tail(&old->list, &new->head); 3021 - next: 3022 - path->slots[0]++; 3023 - cond_resched(); 3024 - } 3025 - 3026 - btrfs_free_path(path); 3027 - atomic_inc(&fs_info->defrag_running); 3028 - 3029 - return new; 3030 - 3031 - out_free_path: 3032 - btrfs_free_path(path); 3033 - out_kfree: 3034 - free_sa_defrag_extent(new); 3035 - return NULL; 3036 - } 3037 - 3038 2396 static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info, 3039 2397 u64 start, u64 len) 3040 2398 { ··· 2419 3063 struct btrfs_trans_handle *trans = NULL; 2420 3064 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; 2421 3065 struct extent_state *cached_state = NULL; 2422 - struct new_sa_defrag_extent *new = NULL; 2423 3066 int compress_type = 0; 2424 3067 int ret = 0; 2425 3068 u64 logical_len = ordered_extent->len; ··· 2427 3072 bool range_locked = false; 2428 3073 bool clear_new_delalloc_bytes = false; 2429 3074 bool clear_reserved_extent = true; 3075 + unsigned int clear_bits; 2430 3076 2431 3077 if (!test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) && 2432 3078 !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags) && ··· 2485 3129 lock_extent_bits(io_tree, ordered_extent->file_offset, 2486 3130 ordered_extent->file_offset + ordered_extent->len - 1, 2487 3131 &cached_state); 2488 - 2489 - ret = test_range_bit(io_tree, ordered_extent->file_offset, 2490 - ordered_extent->file_offset + ordered_extent->len - 1, 2491 - EXTENT_DEFRAG, 0, cached_state); 2492 - if (ret) { 2493 - u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item); 2494 - if (0 && last_snapshot >= BTRFS_I(inode)->generation) 2495 - /* the inode is shared */ 2496 - new = record_old_file_extents(inode, ordered_extent); 2497 - 2498 - clear_extent_bit(io_tree, ordered_extent->file_offset, 2499 - ordered_extent->file_offset + ordered_extent->len - 1, 2500 - EXTENT_DEFRAG, 0, 0, &cached_state); 2501 - } 2502 3132 2503 3133 if (freespace_inode) 2504 3134 trans = btrfs_join_transaction_spacecache(root); ··· 2546 3204 } 2547 3205 ret = 0; 2548 3206 out: 2549 - if (range_locked || clear_new_delalloc_bytes) { 2550 - unsigned int clear_bits = 0; 2551 - 2552 - if (range_locked) 2553 - clear_bits |= EXTENT_LOCKED; 2554 - if (clear_new_delalloc_bytes) 2555 - clear_bits |= EXTENT_DELALLOC_NEW; 2556 - clear_extent_bit(&BTRFS_I(inode)->io_tree, 2557 - ordered_extent->file_offset, 2558 - ordered_extent->file_offset + 2559 - ordered_extent->len - 1, 2560 - clear_bits, 2561 - (clear_bits & EXTENT_LOCKED) ? 1 : 0, 2562 - 0, &cached_state); 2563 - } 3207 + clear_bits = EXTENT_DEFRAG; 3208 + if (range_locked) 3209 + clear_bits |= EXTENT_LOCKED; 3210 + if (clear_new_delalloc_bytes) 3211 + clear_bits |= EXTENT_DELALLOC_NEW; 3212 + clear_extent_bit(&BTRFS_I(inode)->io_tree, 3213 + ordered_extent->file_offset, 3214 + ordered_extent->file_offset + ordered_extent->len - 1, 3215 + clear_bits, (clear_bits & EXTENT_LOCKED) ? 1 : 0, 0, 3216 + &cached_state); 2564 3217 2565 3218 if (trans) 2566 3219 btrfs_end_transaction(trans); ··· 2607 3270 * updating everything for this ordered extent. 2608 3271 */ 2609 3272 btrfs_remove_ordered_extent(inode, ordered_extent); 2610 - 2611 - /* for snapshot-aware defrag */ 2612 - if (new) { 2613 - if (ret) { 2614 - free_sa_defrag_extent(new); 2615 - atomic_dec(&fs_info->defrag_running); 2616 - } else { 2617 - relink_file_extents(new); 2618 - } 2619 - } 2620 3273 2621 3274 /* once for us */ 2622 3275 btrfs_put_ordered_extent(ordered_extent);