|
6 | 6 | #include "common/messages.h"
|
7 | 7 | #include "tune/tune.h"
|
8 | 8 |
|
| 9 | +static int remove_quota_tree(struct btrfs_fs_info *fs_info) |
| 10 | +{ |
| 11 | + int ret; |
| 12 | + struct btrfs_root *quota_root = fs_info->quota_root; |
| 13 | + struct btrfs_root *tree_root = fs_info->tree_root; |
| 14 | + struct btrfs_super_block *sb = fs_info->super_copy; |
| 15 | + int super_flags = btrfs_super_incompat_flags(sb); |
| 16 | + struct btrfs_trans_handle *trans; |
| 17 | + |
| 18 | + trans = btrfs_start_transaction(quota_root, 0); |
| 19 | + ret = btrfs_clear_tree(trans, quota_root); |
| 20 | + if (ret) { |
| 21 | + btrfs_abort_transaction(trans, ret); |
| 22 | + return ret; |
| 23 | + } |
| 24 | + |
| 25 | + ret = btrfs_delete_and_free_root(trans, quota_root); |
| 26 | + if (ret) { |
| 27 | + btrfs_abort_transaction(trans, ret); |
| 28 | + return ret; |
| 29 | + } |
| 30 | + fs_info->quota_root = NULL; |
| 31 | + super_flags &= ~BTRFS_FEATURE_INCOMPAT_SIMPLE_QUOTA; |
| 32 | + btrfs_set_super_incompat_flags(sb, super_flags); |
| 33 | + btrfs_commit_transaction(trans, tree_root); |
| 34 | + return 0; |
| 35 | +} |
| 36 | + |
| 37 | +/* |
| 38 | + * Given a pointer (ptr) into DATAi (i = slot), and an amount to shift, |
| 39 | + * move all the data to the left (slots >= slot) of that ptr to the right by |
| 40 | + * the shift amount. This overwrites the shift bytes after ptr, effectively |
| 41 | + * removing them from the item data. We must update affected item sizes (only |
| 42 | + * at slot) and offsets (slots >= slot). |
| 43 | + * |
| 44 | + * Leaf view, using '-' to show shift scale: |
| 45 | + * Before: |
| 46 | + * [ITEM0,...,ITEMi,...,ITEMn,-------,DATAn,...,[---DATAi---],...,DATA0] |
| 47 | + * After: |
| 48 | + * [ITEM0,...,ITEMi,...,ITEMn,--------,DATAn,...,[--DATAi---],...,DATA0] |
| 49 | + * |
| 50 | + * Zooming in on DATAi |
| 51 | + * (ptr points at the start of the Ys, and shift is length of the Ys) |
| 52 | + * Before: |
| 53 | + * ...[DATAi+1][XXXXXXXXXXXXYYYYYYYYYYYYYYYYXXXXXXX][DATAi-1]... |
| 54 | + * After: |
| 55 | + * ...................[DATAi+1][XXXXXXXXXXXXXXXXXXX][DATAi-1]... |
| 56 | + * Note that DATAi-1 and smaller are not affected. |
| 57 | + */ |
| 58 | +static void shift_leaf_data(struct btrfs_trans_handle *trans, |
| 59 | + struct extent_buffer *leaf, int slot, |
| 60 | + unsigned long ptr, u32 shift) |
| 61 | +{ |
| 62 | + u32 nr = btrfs_header_nritems(leaf); |
| 63 | + u32 leaf_data_off = btrfs_item_ptr_offset(leaf, nr - 1); |
| 64 | + u32 len = ptr - leaf_data_off; |
| 65 | + u32 new_size = btrfs_item_size(leaf, slot) - shift; |
| 66 | + for (int i = slot; i < nr; i++) { |
| 67 | + u32 old_item_offset = btrfs_item_offset(leaf, i); |
| 68 | + btrfs_set_item_offset(leaf, i, old_item_offset + shift); |
| 69 | + } |
| 70 | + memmove_extent_buffer(leaf, leaf_data_off + shift, leaf_data_off, len); |
| 71 | + btrfs_set_item_size(leaf, slot, new_size); |
| 72 | + btrfs_set_header_generation(leaf, trans->transid); |
| 73 | + btrfs_mark_buffer_dirty(leaf); |
| 74 | +} |
| 75 | + |
| 76 | +/* |
| 77 | + * Iterate over the extent tree and for each EXTENT_DATA item that has an inline |
| 78 | + * ref of type OWNER_REF, shift that leaf to eliminate the owner ref. |
| 79 | + * |
| 80 | + * Note: we use a search_slot per leaf rather than find_next_leaf to get the |
| 81 | + * needed CoW-ing and rebalancing for each leaf and its path up to the root. |
| 82 | + */ |
| 83 | +static int remove_owner_refs(struct btrfs_fs_info *fs_info) |
| 84 | +{ |
| 85 | + struct btrfs_trans_handle *trans; |
| 86 | + struct btrfs_root *extent_root; |
| 87 | + struct btrfs_key key; |
| 88 | + struct extent_buffer *leaf; |
| 89 | + struct btrfs_path path = { 0 }; |
| 90 | + int slot; |
| 91 | + int ret; |
| 92 | + |
| 93 | + extent_root = btrfs_extent_root(fs_info, 0); |
| 94 | + |
| 95 | + trans = btrfs_start_transaction(extent_root, 0); |
| 96 | + |
| 97 | + key.objectid = 0; |
| 98 | + key.type = BTRFS_EXTENT_ITEM_KEY; |
| 99 | + key.offset = 0; |
| 100 | + |
| 101 | +search_slot: |
| 102 | + ret = btrfs_search_slot(trans, extent_root, &key, &path, 1, 1); |
| 103 | + if (ret < 0) |
| 104 | + return ret; |
| 105 | + leaf = path.nodes[0]; |
| 106 | + slot = path.slots[0]; |
| 107 | + |
| 108 | + while (1) { |
| 109 | + struct btrfs_key found_key; |
| 110 | + struct btrfs_extent_item *ei; |
| 111 | + struct btrfs_extent_inline_ref *iref; |
| 112 | + u8 type; |
| 113 | + unsigned long ptr; |
| 114 | + unsigned long item_end; |
| 115 | + |
| 116 | + if (slot >= btrfs_header_nritems(leaf)) { |
| 117 | + ret = btrfs_next_leaf(extent_root, &path); |
| 118 | + if (ret < 0) { |
| 119 | + break; |
| 120 | + } else if (ret) { |
| 121 | + ret = 0; |
| 122 | + break; |
| 123 | + } |
| 124 | + leaf = path.nodes[0]; |
| 125 | + slot = path.slots[0]; |
| 126 | + btrfs_item_key_to_cpu(leaf, &key, slot); |
| 127 | + btrfs_release_path(&path); |
| 128 | + goto search_slot; |
| 129 | + } |
| 130 | + |
| 131 | + btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 132 | + if (found_key.type != BTRFS_EXTENT_ITEM_KEY) |
| 133 | + goto next; |
| 134 | + ei = btrfs_item_ptr(leaf, slot, struct btrfs_extent_item); |
| 135 | + ptr = (unsigned long)(ei + 1); |
| 136 | + item_end = (unsigned long)ei + btrfs_item_size(leaf, slot); |
| 137 | + /* No inline extent references; accessing type is invalid. */ |
| 138 | + if (ptr > item_end) |
| 139 | + goto next; |
| 140 | + iref = (struct btrfs_extent_inline_ref *)ptr; |
| 141 | + type = btrfs_extent_inline_ref_type(leaf, iref); |
| 142 | + if (type == BTRFS_EXTENT_OWNER_REF_KEY) |
| 143 | + shift_leaf_data(trans, leaf, slot, ptr, sizeof(*iref)); |
| 144 | +next: |
| 145 | + slot++; |
| 146 | + } |
| 147 | + btrfs_release_path(&path); |
| 148 | + |
| 149 | + ret = btrfs_commit_transaction(trans, extent_root); |
| 150 | + if (ret < 0) { |
| 151 | + errno = -ret; |
| 152 | + error_msg(ERROR_MSG_COMMIT_TRANS, "%m"); |
| 153 | + return ret; |
| 154 | + } |
| 155 | + return 0; |
| 156 | +} |
| 157 | + |
| 158 | +int remove_squota(struct btrfs_fs_info *fs_info) |
| 159 | +{ |
| 160 | + int ret; |
| 161 | + |
| 162 | + ret = remove_owner_refs(fs_info); |
| 163 | + if (ret) |
| 164 | + return ret; |
| 165 | + |
| 166 | + return remove_quota_tree(fs_info); |
| 167 | +} |
| 168 | + |
9 | 169 | static int create_qgroup(struct btrfs_fs_info *fs_info,
|
10 | 170 | struct btrfs_trans_handle *trans,
|
11 | 171 | u64 qgroupid)
|
|
0 commit comments