Chris Mason | 6cbd557 | 2007-06-12 09:07:21 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 Oracle. All rights reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public |
| 6 | * License v2 as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | * General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public |
| 14 | * License along with this program; if not, write to the |
| 15 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 16 | * Boston, MA 021110-1307, USA. |
| 17 | */ |
Zach Brown | ec6b910 | 2007-07-11 10:00:37 -0400 | [diff] [blame] | 18 | #include <linux/sched.h> |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 19 | #include <linux/pagemap.h> |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 20 | #include "hash.h" |
Miguel | a5eb62e | 2008-04-11 15:45:51 -0400 | [diff] [blame] | 21 | #include "crc32c.h" |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 22 | #include "ctree.h" |
| 23 | #include "disk-io.h" |
| 24 | #include "print-tree.h" |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 25 | #include "transaction.h" |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 26 | #include "volumes.h" |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 27 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 28 | #define BLOCK_GROUP_DATA EXTENT_WRITEBACK |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 29 | #define BLOCK_GROUP_METADATA EXTENT_UPTODATE |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 30 | #define BLOCK_GROUP_SYSTEM EXTENT_NEW |
| 31 | |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 32 | #define BLOCK_GROUP_DIRTY EXTENT_DIRTY |
| 33 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 34 | static int finish_current_insert(struct btrfs_trans_handle *trans, struct |
| 35 | btrfs_root *extent_root); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 36 | static int del_pending_extents(struct btrfs_trans_handle *trans, struct |
| 37 | btrfs_root *extent_root); |
Yan | d548ee5 | 2008-01-03 13:56:30 -0500 | [diff] [blame] | 38 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 39 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 40 | static int cache_block_group(struct btrfs_root *root, |
| 41 | struct btrfs_block_group_cache *block_group) |
| 42 | { |
| 43 | struct btrfs_path *path; |
| 44 | int ret; |
| 45 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 46 | struct extent_buffer *leaf; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 47 | struct extent_io_tree *free_space_cache; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 48 | int slot; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 49 | u64 last = 0; |
| 50 | u64 hole_size; |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 51 | u64 first_free; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 52 | int found = 0; |
| 53 | |
Chris Mason | 00f5c79 | 2007-11-30 10:09:33 -0500 | [diff] [blame] | 54 | if (!block_group) |
| 55 | return 0; |
| 56 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 57 | root = root->fs_info->extent_root; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 58 | free_space_cache = &root->fs_info->free_space_cache; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 59 | |
| 60 | if (block_group->cached) |
| 61 | return 0; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 62 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 63 | path = btrfs_alloc_path(); |
| 64 | if (!path) |
| 65 | return -ENOMEM; |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 66 | |
Chris Mason | 2cc58cf | 2007-08-27 16:49:44 -0400 | [diff] [blame] | 67 | path->reada = 2; |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 68 | first_free = block_group->key.objectid; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 69 | key.objectid = block_group->key.objectid; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 70 | key.offset = 0; |
| 71 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
| 72 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 73 | if (ret < 0) |
| 74 | return ret; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 75 | ret = btrfs_previous_item(root, path, 0, BTRFS_EXTENT_ITEM_KEY); |
Yan | d548ee5 | 2008-01-03 13:56:30 -0500 | [diff] [blame] | 76 | if (ret < 0) |
| 77 | return ret; |
| 78 | if (ret == 0) { |
| 79 | leaf = path->nodes[0]; |
| 80 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); |
| 81 | if (key.objectid + key.offset > first_free) |
| 82 | first_free = key.objectid + key.offset; |
| 83 | } |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 84 | while(1) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 85 | leaf = path->nodes[0]; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 86 | slot = path->slots[0]; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 87 | if (slot >= btrfs_header_nritems(leaf)) { |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 88 | ret = btrfs_next_leaf(root, path); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 89 | if (ret < 0) |
| 90 | goto err; |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 91 | if (ret == 0) { |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 92 | continue; |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 93 | } else { |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 94 | break; |
| 95 | } |
| 96 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 97 | btrfs_item_key_to_cpu(leaf, &key, slot); |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 98 | if (key.objectid < block_group->key.objectid) { |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 99 | goto next; |
| 100 | } |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 101 | if (key.objectid >= block_group->key.objectid + |
| 102 | block_group->key.offset) { |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 103 | break; |
| 104 | } |
| 105 | |
| 106 | if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) { |
| 107 | if (!found) { |
| 108 | last = first_free; |
| 109 | found = 1; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 110 | } |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 111 | if (key.objectid > last) { |
| 112 | hole_size = key.objectid - last; |
| 113 | set_extent_dirty(free_space_cache, last, |
| 114 | last + hole_size - 1, |
| 115 | GFP_NOFS); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 116 | } |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 117 | last = key.objectid + key.offset; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 118 | } |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 119 | next: |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 120 | path->slots[0]++; |
| 121 | } |
| 122 | |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 123 | if (!found) |
| 124 | last = first_free; |
| 125 | if (block_group->key.objectid + |
| 126 | block_group->key.offset > last) { |
| 127 | hole_size = block_group->key.objectid + |
| 128 | block_group->key.offset - last; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 129 | set_extent_dirty(free_space_cache, last, |
| 130 | last + hole_size - 1, GFP_NOFS); |
Yan | 7d7d606 | 2007-09-14 16:15:28 -0400 | [diff] [blame] | 131 | } |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 132 | block_group->cached = 1; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 133 | err: |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 134 | btrfs_free_path(path); |
| 135 | return 0; |
| 136 | } |
| 137 | |
Chris Mason | 5276aed | 2007-06-11 21:33:38 -0400 | [diff] [blame] | 138 | struct btrfs_block_group_cache *btrfs_lookup_block_group(struct |
| 139 | btrfs_fs_info *info, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 140 | u64 bytenr) |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 141 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 142 | struct extent_io_tree *block_group_cache; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 143 | struct btrfs_block_group_cache *block_group = NULL; |
| 144 | u64 ptr; |
| 145 | u64 start; |
| 146 | u64 end; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 147 | int ret; |
| 148 | |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 149 | block_group_cache = &info->block_group_cache; |
| 150 | ret = find_first_extent_bit(block_group_cache, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 151 | bytenr, &start, &end, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 152 | BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA | |
| 153 | BLOCK_GROUP_SYSTEM); |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 154 | if (ret) { |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 155 | return NULL; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 156 | } |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 157 | ret = get_state_private(block_group_cache, start, &ptr); |
| 158 | if (ret) |
| 159 | return NULL; |
| 160 | |
Jens Axboe | ae2f541 | 2007-10-19 09:22:59 -0400 | [diff] [blame] | 161 | block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr; |
Yan | 5cf6642 | 2007-11-16 14:57:09 -0500 | [diff] [blame] | 162 | if (block_group->key.objectid <= bytenr && bytenr < |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 163 | block_group->key.objectid + block_group->key.offset) |
| 164 | return block_group; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 165 | return NULL; |
| 166 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 167 | |
| 168 | static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits) |
| 169 | { |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 170 | return (cache->flags & bits) == bits; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | static int noinline find_search_start(struct btrfs_root *root, |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 174 | struct btrfs_block_group_cache **cache_ret, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 175 | u64 *start_ret, int num, int data) |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 176 | { |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 177 | int ret; |
| 178 | struct btrfs_block_group_cache *cache = *cache_ret; |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 179 | struct extent_io_tree *free_space_cache; |
Chris Mason | 7d1660d | 2008-03-24 15:02:03 -0400 | [diff] [blame] | 180 | struct extent_state *state; |
Chris Mason | e19caa5 | 2007-10-15 16:17:44 -0400 | [diff] [blame] | 181 | u64 last; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 182 | u64 start = 0; |
Chris Mason | 257d0ce | 2007-11-07 21:08:16 -0500 | [diff] [blame] | 183 | u64 cache_miss = 0; |
Chris Mason | c31f883 | 2008-01-08 15:46:31 -0500 | [diff] [blame] | 184 | u64 total_fs_bytes; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 185 | u64 search_start = *start_ret; |
Chris Mason | f84a8b3 | 2007-11-06 10:26:29 -0500 | [diff] [blame] | 186 | int wrapped = 0; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 187 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 188 | if (!cache) |
Chris Mason | 1a2b2ac | 2007-12-04 13:18:24 -0500 | [diff] [blame] | 189 | goto out; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 190 | |
Chris Mason | c31f883 | 2008-01-08 15:46:31 -0500 | [diff] [blame] | 191 | total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy); |
Chris Mason | d7fc640 | 2008-02-18 12:12:38 -0500 | [diff] [blame] | 192 | free_space_cache = &root->fs_info->free_space_cache; |
| 193 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 194 | again: |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 195 | ret = cache_block_group(root, cache); |
| 196 | if (ret) |
| 197 | goto out; |
Chris Mason | f84a8b3 | 2007-11-06 10:26:29 -0500 | [diff] [blame] | 198 | |
Chris Mason | e19caa5 | 2007-10-15 16:17:44 -0400 | [diff] [blame] | 199 | last = max(search_start, cache->key.objectid); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 200 | if (!block_group_bits(cache, data) || cache->ro) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 201 | goto new_group; |
| 202 | } |
Chris Mason | e19caa5 | 2007-10-15 16:17:44 -0400 | [diff] [blame] | 203 | |
Chris Mason | 7d1660d | 2008-03-24 15:02:03 -0400 | [diff] [blame] | 204 | spin_lock_irq(&free_space_cache->lock); |
| 205 | state = find_first_extent_bit_state(free_space_cache, last, EXTENT_DIRTY); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 206 | while(1) { |
Chris Mason | 7d1660d | 2008-03-24 15:02:03 -0400 | [diff] [blame] | 207 | if (!state) { |
Chris Mason | 257d0ce | 2007-11-07 21:08:16 -0500 | [diff] [blame] | 208 | if (!cache_miss) |
| 209 | cache_miss = last; |
Chris Mason | 7d1660d | 2008-03-24 15:02:03 -0400 | [diff] [blame] | 210 | spin_unlock_irq(&free_space_cache->lock); |
Chris Mason | e19caa5 | 2007-10-15 16:17:44 -0400 | [diff] [blame] | 211 | goto new_group; |
| 212 | } |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 213 | |
Chris Mason | 7d1660d | 2008-03-24 15:02:03 -0400 | [diff] [blame] | 214 | start = max(last, state->start); |
| 215 | last = state->end + 1; |
Chris Mason | 257d0ce | 2007-11-07 21:08:16 -0500 | [diff] [blame] | 216 | if (last - start < num) { |
| 217 | if (last == cache->key.objectid + cache->key.offset) |
| 218 | cache_miss = start; |
Chris Mason | 7d1660d | 2008-03-24 15:02:03 -0400 | [diff] [blame] | 219 | do { |
| 220 | state = extent_state_next(state); |
| 221 | } while(state && !(state->state & EXTENT_DIRTY)); |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 222 | continue; |
Chris Mason | 257d0ce | 2007-11-07 21:08:16 -0500 | [diff] [blame] | 223 | } |
Chris Mason | 7d1660d | 2008-03-24 15:02:03 -0400 | [diff] [blame] | 224 | spin_unlock_irq(&free_space_cache->lock); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 225 | if (cache->ro) |
| 226 | goto new_group; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 227 | if (start + num > cache->key.objectid + cache->key.offset) |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 228 | goto new_group; |
Chris Mason | c31f883 | 2008-01-08 15:46:31 -0500 | [diff] [blame] | 229 | if (start + num > total_fs_bytes) |
| 230 | goto new_group; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 231 | if (!block_group_bits(cache, data)) { |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 232 | printk("block group bits don't match %Lu %d\n", cache->flags, data); |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 233 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 234 | *start_ret = start; |
| 235 | return 0; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 236 | } |
| 237 | out: |
Chris Mason | 1a2b2ac | 2007-12-04 13:18:24 -0500 | [diff] [blame] | 238 | cache = btrfs_lookup_block_group(root->fs_info, search_start); |
| 239 | if (!cache) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 240 | printk("Unable to find block group for %Lu\n", search_start); |
Chris Mason | 1a2b2ac | 2007-12-04 13:18:24 -0500 | [diff] [blame] | 241 | WARN_ON(1); |
Chris Mason | 1a2b2ac | 2007-12-04 13:18:24 -0500 | [diff] [blame] | 242 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 243 | return -ENOSPC; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 244 | |
| 245 | new_group: |
Chris Mason | e19caa5 | 2007-10-15 16:17:44 -0400 | [diff] [blame] | 246 | last = cache->key.objectid + cache->key.offset; |
Chris Mason | f84a8b3 | 2007-11-06 10:26:29 -0500 | [diff] [blame] | 247 | wrapped: |
Chris Mason | e19caa5 | 2007-10-15 16:17:44 -0400 | [diff] [blame] | 248 | cache = btrfs_lookup_block_group(root->fs_info, last); |
Chris Mason | c31f883 | 2008-01-08 15:46:31 -0500 | [diff] [blame] | 249 | if (!cache || cache->key.objectid >= total_fs_bytes) { |
Chris Mason | 0e4de58 | 2007-11-26 10:55:49 -0500 | [diff] [blame] | 250 | no_cache: |
Chris Mason | f84a8b3 | 2007-11-06 10:26:29 -0500 | [diff] [blame] | 251 | if (!wrapped) { |
| 252 | wrapped = 1; |
| 253 | last = search_start; |
Chris Mason | f84a8b3 | 2007-11-06 10:26:29 -0500 | [diff] [blame] | 254 | goto wrapped; |
| 255 | } |
Chris Mason | 1a2b2ac | 2007-12-04 13:18:24 -0500 | [diff] [blame] | 256 | goto out; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 257 | } |
Chris Mason | 257d0ce | 2007-11-07 21:08:16 -0500 | [diff] [blame] | 258 | if (cache_miss && !cache->cached) { |
| 259 | cache_block_group(root, cache); |
| 260 | last = cache_miss; |
Chris Mason | 257d0ce | 2007-11-07 21:08:16 -0500 | [diff] [blame] | 261 | cache = btrfs_lookup_block_group(root->fs_info, last); |
| 262 | } |
Chris Mason | 1a2b2ac | 2007-12-04 13:18:24 -0500 | [diff] [blame] | 263 | cache = btrfs_find_block_group(root, cache, last, data, 0); |
Chris Mason | 0e4de58 | 2007-11-26 10:55:49 -0500 | [diff] [blame] | 264 | if (!cache) |
| 265 | goto no_cache; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 266 | *cache_ret = cache; |
Chris Mason | 257d0ce | 2007-11-07 21:08:16 -0500 | [diff] [blame] | 267 | cache_miss = 0; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 268 | goto again; |
| 269 | } |
| 270 | |
Chris Mason | 84f54cf | 2007-06-12 07:43:08 -0400 | [diff] [blame] | 271 | static u64 div_factor(u64 num, int factor) |
| 272 | { |
Chris Mason | 257d0ce | 2007-11-07 21:08:16 -0500 | [diff] [blame] | 273 | if (factor == 10) |
| 274 | return num; |
Chris Mason | 84f54cf | 2007-06-12 07:43:08 -0400 | [diff] [blame] | 275 | num *= factor; |
| 276 | do_div(num, 10); |
| 277 | return num; |
| 278 | } |
| 279 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 280 | static int block_group_state_bits(u64 flags) |
| 281 | { |
| 282 | int bits = 0; |
| 283 | if (flags & BTRFS_BLOCK_GROUP_DATA) |
| 284 | bits |= BLOCK_GROUP_DATA; |
| 285 | if (flags & BTRFS_BLOCK_GROUP_METADATA) |
| 286 | bits |= BLOCK_GROUP_METADATA; |
| 287 | if (flags & BTRFS_BLOCK_GROUP_SYSTEM) |
| 288 | bits |= BLOCK_GROUP_SYSTEM; |
| 289 | return bits; |
| 290 | } |
| 291 | |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 292 | struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root, |
| 293 | struct btrfs_block_group_cache |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 294 | *hint, u64 search_start, |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 295 | int data, int owner) |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 296 | { |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 297 | struct btrfs_block_group_cache *cache; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 298 | struct extent_io_tree *block_group_cache; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 299 | struct btrfs_block_group_cache *found_group = NULL; |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 300 | struct btrfs_fs_info *info = root->fs_info; |
| 301 | u64 used; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 302 | u64 last = 0; |
| 303 | u64 hint_last; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 304 | u64 start; |
| 305 | u64 end; |
| 306 | u64 free_check; |
| 307 | u64 ptr; |
Chris Mason | c31f883 | 2008-01-08 15:46:31 -0500 | [diff] [blame] | 308 | u64 total_fs_bytes; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 309 | int bit; |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 310 | int ret; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 311 | int full_search = 0; |
Chris Mason | bce4eae | 2008-04-24 14:42:46 -0400 | [diff] [blame] | 312 | int factor = 10; |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 313 | |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 314 | block_group_cache = &info->block_group_cache; |
Chris Mason | c31f883 | 2008-01-08 15:46:31 -0500 | [diff] [blame] | 315 | total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy); |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 316 | |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 317 | if (!owner) |
Chris Mason | bce4eae | 2008-04-24 14:42:46 -0400 | [diff] [blame] | 318 | factor = 10; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 319 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 320 | bit = block_group_state_bits(data); |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 321 | |
Chris Mason | c31f883 | 2008-01-08 15:46:31 -0500 | [diff] [blame] | 322 | if (search_start && search_start < total_fs_bytes) { |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 323 | struct btrfs_block_group_cache *shint; |
Chris Mason | 5276aed | 2007-06-11 21:33:38 -0400 | [diff] [blame] | 324 | shint = btrfs_lookup_block_group(info, search_start); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 325 | if (shint && block_group_bits(shint, data) && !shint->ro) { |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 326 | used = btrfs_block_group_used(&shint->item); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 327 | if (used + shint->pinned < |
| 328 | div_factor(shint->key.offset, factor)) { |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 329 | return shint; |
| 330 | } |
| 331 | } |
| 332 | } |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 333 | if (hint && !hint->ro && block_group_bits(hint, data) && |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 334 | hint->key.objectid < total_fs_bytes) { |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 335 | used = btrfs_block_group_used(&hint->item); |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 336 | if (used + hint->pinned < |
| 337 | div_factor(hint->key.offset, factor)) { |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 338 | return hint; |
| 339 | } |
Chris Mason | e19caa5 | 2007-10-15 16:17:44 -0400 | [diff] [blame] | 340 | last = hint->key.objectid + hint->key.offset; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 341 | hint_last = last; |
| 342 | } else { |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 343 | if (hint) |
| 344 | hint_last = max(hint->key.objectid, search_start); |
| 345 | else |
| 346 | hint_last = search_start; |
| 347 | |
Chris Mason | c31f883 | 2008-01-08 15:46:31 -0500 | [diff] [blame] | 348 | if (hint_last >= total_fs_bytes) |
| 349 | hint_last = search_start; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 350 | last = hint_last; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 351 | } |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 352 | again: |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 353 | while(1) { |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 354 | ret = find_first_extent_bit(block_group_cache, last, |
| 355 | &start, &end, bit); |
| 356 | if (ret) |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 357 | break; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 358 | |
| 359 | ret = get_state_private(block_group_cache, start, &ptr); |
| 360 | if (ret) |
| 361 | break; |
| 362 | |
Jens Axboe | ae2f541 | 2007-10-19 09:22:59 -0400 | [diff] [blame] | 363 | cache = (struct btrfs_block_group_cache *)(unsigned long)ptr; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 364 | last = cache->key.objectid + cache->key.offset; |
| 365 | used = btrfs_block_group_used(&cache->item); |
| 366 | |
Chris Mason | c31f883 | 2008-01-08 15:46:31 -0500 | [diff] [blame] | 367 | if (cache->key.objectid > total_fs_bytes) |
| 368 | break; |
| 369 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 370 | if (!cache->ro && block_group_bits(cache, data)) { |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 371 | if (full_search) |
| 372 | free_check = cache->key.offset; |
| 373 | else |
| 374 | free_check = div_factor(cache->key.offset, |
| 375 | factor); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 376 | |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 377 | if (used + cache->pinned < free_check) { |
| 378 | found_group = cache; |
| 379 | goto found; |
| 380 | } |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 381 | } |
Chris Mason | de428b6 | 2007-05-18 13:28:27 -0400 | [diff] [blame] | 382 | cond_resched(); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 383 | } |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 384 | if (!full_search) { |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 385 | last = search_start; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 386 | full_search = 1; |
| 387 | goto again; |
| 388 | } |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 389 | found: |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 390 | return found_group; |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 391 | } |
| 392 | |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 393 | static u64 hash_extent_ref(u64 root_objectid, u64 ref_generation, |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 394 | u64 owner, u64 owner_offset) |
| 395 | { |
| 396 | u32 high_crc = ~(u32)0; |
| 397 | u32 low_crc = ~(u32)0; |
| 398 | __le64 lenum; |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 399 | lenum = cpu_to_le64(root_objectid); |
Miguel | a5eb62e | 2008-04-11 15:45:51 -0400 | [diff] [blame] | 400 | high_crc = btrfs_crc32c(high_crc, &lenum, sizeof(lenum)); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 401 | lenum = cpu_to_le64(ref_generation); |
Miguel | a5eb62e | 2008-04-11 15:45:51 -0400 | [diff] [blame] | 402 | low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum)); |
Chris Mason | 21a4989 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 403 | if (owner >= BTRFS_FIRST_FREE_OBJECTID) { |
| 404 | lenum = cpu_to_le64(owner); |
Miguel | a5eb62e | 2008-04-11 15:45:51 -0400 | [diff] [blame] | 405 | low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum)); |
Chris Mason | 21a4989 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 406 | lenum = cpu_to_le64(owner_offset); |
Miguel | a5eb62e | 2008-04-11 15:45:51 -0400 | [diff] [blame] | 407 | low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum)); |
Chris Mason | 21a4989 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 408 | } |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 409 | return ((u64)high_crc << 32) | (u64)low_crc; |
| 410 | } |
| 411 | |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 412 | static int match_extent_ref(struct extent_buffer *leaf, |
| 413 | struct btrfs_extent_ref *disk_ref, |
| 414 | struct btrfs_extent_ref *cpu_ref) |
| 415 | { |
| 416 | int ret; |
| 417 | int len; |
| 418 | |
| 419 | if (cpu_ref->objectid) |
| 420 | len = sizeof(*cpu_ref); |
| 421 | else |
| 422 | len = 2 * sizeof(u64); |
| 423 | ret = memcmp_extent_buffer(leaf, cpu_ref, (unsigned long)disk_ref, |
| 424 | len); |
| 425 | return ret == 0; |
| 426 | } |
| 427 | |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 428 | static int noinline lookup_extent_backref(struct btrfs_trans_handle *trans, |
| 429 | struct btrfs_root *root, |
| 430 | struct btrfs_path *path, u64 bytenr, |
| 431 | u64 root_objectid, |
| 432 | u64 ref_generation, u64 owner, |
| 433 | u64 owner_offset, int del) |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 434 | { |
| 435 | u64 hash; |
| 436 | struct btrfs_key key; |
| 437 | struct btrfs_key found_key; |
| 438 | struct btrfs_extent_ref ref; |
| 439 | struct extent_buffer *leaf; |
| 440 | struct btrfs_extent_ref *disk_ref; |
| 441 | int ret; |
| 442 | int ret2; |
| 443 | |
| 444 | btrfs_set_stack_ref_root(&ref, root_objectid); |
| 445 | btrfs_set_stack_ref_generation(&ref, ref_generation); |
| 446 | btrfs_set_stack_ref_objectid(&ref, owner); |
| 447 | btrfs_set_stack_ref_offset(&ref, owner_offset); |
| 448 | |
| 449 | hash = hash_extent_ref(root_objectid, ref_generation, owner, |
| 450 | owner_offset); |
| 451 | key.offset = hash; |
| 452 | key.objectid = bytenr; |
| 453 | key.type = BTRFS_EXTENT_REF_KEY; |
| 454 | |
| 455 | while (1) { |
| 456 | ret = btrfs_search_slot(trans, root, &key, path, |
| 457 | del ? -1 : 0, del); |
| 458 | if (ret < 0) |
| 459 | goto out; |
| 460 | leaf = path->nodes[0]; |
| 461 | if (ret != 0) { |
| 462 | u32 nritems = btrfs_header_nritems(leaf); |
| 463 | if (path->slots[0] >= nritems) { |
| 464 | ret2 = btrfs_next_leaf(root, path); |
| 465 | if (ret2) |
| 466 | goto out; |
| 467 | leaf = path->nodes[0]; |
| 468 | } |
| 469 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 470 | if (found_key.objectid != bytenr || |
| 471 | found_key.type != BTRFS_EXTENT_REF_KEY) |
| 472 | goto out; |
| 473 | key.offset = found_key.offset; |
| 474 | if (del) { |
| 475 | btrfs_release_path(root, path); |
| 476 | continue; |
| 477 | } |
| 478 | } |
| 479 | disk_ref = btrfs_item_ptr(path->nodes[0], |
| 480 | path->slots[0], |
| 481 | struct btrfs_extent_ref); |
| 482 | if (match_extent_ref(path->nodes[0], disk_ref, &ref)) { |
| 483 | ret = 0; |
| 484 | goto out; |
| 485 | } |
| 486 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 487 | key.offset = found_key.offset + 1; |
| 488 | btrfs_release_path(root, path); |
| 489 | } |
| 490 | out: |
| 491 | return ret; |
| 492 | } |
| 493 | |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 494 | /* |
| 495 | * Back reference rules. Back refs have three main goals: |
| 496 | * |
| 497 | * 1) differentiate between all holders of references to an extent so that |
| 498 | * when a reference is dropped we can make sure it was a valid reference |
| 499 | * before freeing the extent. |
| 500 | * |
| 501 | * 2) Provide enough information to quickly find the holders of an extent |
| 502 | * if we notice a given block is corrupted or bad. |
| 503 | * |
| 504 | * 3) Make it easy to migrate blocks for FS shrinking or storage pool |
| 505 | * maintenance. This is actually the same as #2, but with a slightly |
| 506 | * different use case. |
| 507 | * |
| 508 | * File extents can be referenced by: |
| 509 | * |
| 510 | * - multiple snapshots, subvolumes, or different generations in one subvol |
| 511 | * - different files inside a single subvolume (in theory, not implemented yet) |
| 512 | * - different offsets inside a file (bookend extents in file.c) |
| 513 | * |
| 514 | * The extent ref structure has fields for: |
| 515 | * |
| 516 | * - Objectid of the subvolume root |
| 517 | * - Generation number of the tree holding the reference |
| 518 | * - objectid of the file holding the reference |
| 519 | * - offset in the file corresponding to the key holding the reference |
| 520 | * |
| 521 | * When a file extent is allocated the fields are filled in: |
| 522 | * (root_key.objectid, trans->transid, inode objectid, offset in file) |
| 523 | * |
| 524 | * When a leaf is cow'd new references are added for every file extent found |
| 525 | * in the leaf. It looks the same as the create case, but trans->transid |
| 526 | * will be different when the block is cow'd. |
| 527 | * |
| 528 | * (root_key.objectid, trans->transid, inode objectid, offset in file) |
| 529 | * |
| 530 | * When a file extent is removed either during snapshot deletion or file |
| 531 | * truncation, the corresponding back reference is found |
| 532 | * by searching for: |
| 533 | * |
| 534 | * (btrfs_header_owner(leaf), btrfs_header_generation(leaf), |
| 535 | * inode objectid, offset in file) |
| 536 | * |
| 537 | * Btree extents can be referenced by: |
| 538 | * |
| 539 | * - Different subvolumes |
| 540 | * - Different generations of the same subvolume |
| 541 | * |
| 542 | * Storing sufficient information for a full reverse mapping of a btree |
| 543 | * block would require storing the lowest key of the block in the backref, |
| 544 | * and it would require updating that lowest key either before write out or |
| 545 | * every time it changed. Instead, the objectid of the lowest key is stored |
| 546 | * along with the level of the tree block. This provides a hint |
| 547 | * about where in the btree the block can be found. Searches through the |
| 548 | * btree only need to look for a pointer to that block, so they stop one |
| 549 | * level higher than the level recorded in the backref. |
| 550 | * |
| 551 | * Some btrees do not do reference counting on their extents. These |
| 552 | * include the extent tree and the tree of tree roots. Backrefs for these |
| 553 | * trees always have a generation of zero. |
| 554 | * |
| 555 | * When a tree block is created, back references are inserted: |
| 556 | * |
Chris Mason | f6dbff5 | 2007-12-13 11:13:32 -0500 | [diff] [blame] | 557 | * (root->root_key.objectid, trans->transid or zero, level, lowest_key_objectid) |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 558 | * |
| 559 | * When a tree block is cow'd in a reference counted root, |
| 560 | * new back references are added for all the blocks it points to. |
| 561 | * These are of the form (trans->transid will have increased since creation): |
| 562 | * |
Chris Mason | f6dbff5 | 2007-12-13 11:13:32 -0500 | [diff] [blame] | 563 | * (root->root_key.objectid, trans->transid, level, lowest_key_objectid) |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 564 | * |
| 565 | * Because the lowest_key_objectid and the level are just hints |
| 566 | * they are not used when backrefs are deleted. When a backref is deleted: |
| 567 | * |
| 568 | * if backref was for a tree root: |
| 569 | * root_objectid = root->root_key.objectid |
| 570 | * else |
| 571 | * root_objectid = btrfs_header_owner(parent) |
| 572 | * |
| 573 | * (root_objectid, btrfs_header_generation(parent) or zero, 0, 0) |
| 574 | * |
| 575 | * Back Reference Key hashing: |
| 576 | * |
| 577 | * Back references have four fields, each 64 bits long. Unfortunately, |
| 578 | * This is hashed into a single 64 bit number and placed into the key offset. |
| 579 | * The key objectid corresponds to the first byte in the extent, and the |
| 580 | * key type is set to BTRFS_EXTENT_REF_KEY |
| 581 | */ |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 582 | int btrfs_insert_extent_backref(struct btrfs_trans_handle *trans, |
| 583 | struct btrfs_root *root, |
| 584 | struct btrfs_path *path, u64 bytenr, |
| 585 | u64 root_objectid, u64 ref_generation, |
| 586 | u64 owner, u64 owner_offset) |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 587 | { |
| 588 | u64 hash; |
| 589 | struct btrfs_key key; |
| 590 | struct btrfs_extent_ref ref; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 591 | struct btrfs_extent_ref *disk_ref; |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 592 | int ret; |
| 593 | |
| 594 | btrfs_set_stack_ref_root(&ref, root_objectid); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 595 | btrfs_set_stack_ref_generation(&ref, ref_generation); |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 596 | btrfs_set_stack_ref_objectid(&ref, owner); |
| 597 | btrfs_set_stack_ref_offset(&ref, owner_offset); |
| 598 | |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 599 | hash = hash_extent_ref(root_objectid, ref_generation, owner, |
| 600 | owner_offset); |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 601 | key.offset = hash; |
| 602 | key.objectid = bytenr; |
| 603 | key.type = BTRFS_EXTENT_REF_KEY; |
| 604 | |
| 605 | ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(ref)); |
| 606 | while (ret == -EEXIST) { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 607 | disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 608 | struct btrfs_extent_ref); |
| 609 | if (match_extent_ref(path->nodes[0], disk_ref, &ref)) |
| 610 | goto out; |
| 611 | key.offset++; |
| 612 | btrfs_release_path(root, path); |
| 613 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 614 | sizeof(ref)); |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 615 | } |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 616 | if (ret) |
| 617 | goto out; |
| 618 | disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 619 | struct btrfs_extent_ref); |
| 620 | write_extent_buffer(path->nodes[0], &ref, (unsigned long)disk_ref, |
| 621 | sizeof(ref)); |
| 622 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 623 | out: |
| 624 | btrfs_release_path(root, path); |
| 625 | return ret; |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 626 | } |
| 627 | |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 628 | int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
| 629 | struct btrfs_root *root, |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 630 | u64 bytenr, u64 num_bytes, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 631 | u64 root_objectid, u64 ref_generation, |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 632 | u64 owner, u64 owner_offset) |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 633 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 634 | struct btrfs_path *path; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 635 | int ret; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 636 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 637 | struct extent_buffer *l; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 638 | struct btrfs_extent_item *item; |
Chris Mason | cf27e1e | 2007-03-13 09:49:06 -0400 | [diff] [blame] | 639 | u32 refs; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 640 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 641 | WARN_ON(num_bytes < root->sectorsize); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 642 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 643 | if (!path) |
| 644 | return -ENOMEM; |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 645 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 646 | path->reada = 1; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 647 | key.objectid = bytenr; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 648 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 649 | key.offset = num_bytes; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 650 | ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path, |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 651 | 0, 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 652 | if (ret < 0) |
| 653 | return ret; |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 654 | if (ret != 0) { |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 655 | BUG(); |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 656 | } |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 657 | BUG_ON(ret != 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 658 | l = path->nodes[0]; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 659 | item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 660 | refs = btrfs_extent_refs(l, item); |
| 661 | btrfs_set_extent_refs(l, item, refs + 1); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 662 | btrfs_mark_buffer_dirty(path->nodes[0]); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 663 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 664 | btrfs_release_path(root->fs_info->extent_root, path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 665 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 666 | path->reada = 1; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 667 | ret = btrfs_insert_extent_backref(trans, root->fs_info->extent_root, |
| 668 | path, bytenr, root_objectid, |
| 669 | ref_generation, owner, owner_offset); |
| 670 | BUG_ON(ret); |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 671 | finish_current_insert(trans, root->fs_info->extent_root); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 672 | del_pending_extents(trans, root->fs_info->extent_root); |
Chris Mason | 74493f7 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 673 | |
| 674 | btrfs_free_path(path); |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 675 | return 0; |
| 676 | } |
| 677 | |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 678 | int btrfs_extent_post_op(struct btrfs_trans_handle *trans, |
| 679 | struct btrfs_root *root) |
| 680 | { |
| 681 | finish_current_insert(trans, root->fs_info->extent_root); |
| 682 | del_pending_extents(trans, root->fs_info->extent_root); |
| 683 | return 0; |
| 684 | } |
| 685 | |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 686 | static int lookup_extent_ref(struct btrfs_trans_handle *trans, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 687 | struct btrfs_root *root, u64 bytenr, |
| 688 | u64 num_bytes, u32 *refs) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 689 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 690 | struct btrfs_path *path; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 691 | int ret; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 692 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 693 | struct extent_buffer *l; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 694 | struct btrfs_extent_item *item; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 695 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 696 | WARN_ON(num_bytes < root->sectorsize); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 697 | path = btrfs_alloc_path(); |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 698 | path->reada = 1; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 699 | key.objectid = bytenr; |
| 700 | key.offset = num_bytes; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 701 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 702 | ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path, |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 703 | 0, 0); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 704 | if (ret < 0) |
| 705 | goto out; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 706 | if (ret != 0) { |
| 707 | btrfs_print_leaf(root, path->nodes[0]); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 708 | printk("failed to find block number %Lu\n", bytenr); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 709 | BUG(); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 710 | } |
| 711 | l = path->nodes[0]; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 712 | item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 713 | *refs = btrfs_extent_refs(l, item); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 714 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 715 | btrfs_free_path(path); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 716 | return 0; |
| 717 | } |
| 718 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 719 | u32 btrfs_count_snapshots_in_path(struct btrfs_root *root, |
| 720 | struct btrfs_path *count_path, |
| 721 | u64 first_extent) |
| 722 | { |
| 723 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
| 724 | struct btrfs_path *path; |
| 725 | u64 bytenr; |
| 726 | u64 found_objectid; |
Chris Mason | 56b453c | 2008-01-03 09:08:27 -0500 | [diff] [blame] | 727 | u64 root_objectid = root->root_key.objectid; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 728 | u32 total_count = 0; |
| 729 | u32 cur_count; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 730 | u32 nritems; |
| 731 | int ret; |
| 732 | struct btrfs_key key; |
| 733 | struct btrfs_key found_key; |
| 734 | struct extent_buffer *l; |
| 735 | struct btrfs_extent_item *item; |
| 736 | struct btrfs_extent_ref *ref_item; |
| 737 | int level = -1; |
| 738 | |
| 739 | path = btrfs_alloc_path(); |
| 740 | again: |
| 741 | if (level == -1) |
| 742 | bytenr = first_extent; |
| 743 | else |
| 744 | bytenr = count_path->nodes[level]->start; |
| 745 | |
| 746 | cur_count = 0; |
| 747 | key.objectid = bytenr; |
| 748 | key.offset = 0; |
| 749 | |
| 750 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
| 751 | ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); |
| 752 | if (ret < 0) |
| 753 | goto out; |
| 754 | BUG_ON(ret == 0); |
| 755 | |
| 756 | l = path->nodes[0]; |
| 757 | btrfs_item_key_to_cpu(l, &found_key, path->slots[0]); |
| 758 | |
| 759 | if (found_key.objectid != bytenr || |
| 760 | found_key.type != BTRFS_EXTENT_ITEM_KEY) { |
| 761 | goto out; |
| 762 | } |
| 763 | |
| 764 | item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item); |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 765 | while (1) { |
Chris Mason | bd09835 | 2008-01-03 13:23:19 -0500 | [diff] [blame] | 766 | l = path->nodes[0]; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 767 | nritems = btrfs_header_nritems(l); |
| 768 | if (path->slots[0] >= nritems) { |
| 769 | ret = btrfs_next_leaf(extent_root, path); |
| 770 | if (ret == 0) |
| 771 | continue; |
| 772 | break; |
| 773 | } |
| 774 | btrfs_item_key_to_cpu(l, &found_key, path->slots[0]); |
| 775 | if (found_key.objectid != bytenr) |
| 776 | break; |
Chris Mason | bd09835 | 2008-01-03 13:23:19 -0500 | [diff] [blame] | 777 | |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 778 | if (found_key.type != BTRFS_EXTENT_REF_KEY) { |
| 779 | path->slots[0]++; |
| 780 | continue; |
| 781 | } |
| 782 | |
| 783 | cur_count++; |
| 784 | ref_item = btrfs_item_ptr(l, path->slots[0], |
| 785 | struct btrfs_extent_ref); |
| 786 | found_objectid = btrfs_ref_root(l, ref_item); |
| 787 | |
Chris Mason | 56b453c | 2008-01-03 09:08:27 -0500 | [diff] [blame] | 788 | if (found_objectid != root_objectid) { |
| 789 | total_count = 2; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 790 | goto out; |
Chris Mason | 56b453c | 2008-01-03 09:08:27 -0500 | [diff] [blame] | 791 | } |
| 792 | total_count = 1; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 793 | path->slots[0]++; |
| 794 | } |
| 795 | if (cur_count == 0) { |
| 796 | total_count = 0; |
| 797 | goto out; |
| 798 | } |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 799 | if (level >= 0 && root->node == count_path->nodes[level]) |
| 800 | goto out; |
| 801 | level++; |
| 802 | btrfs_release_path(root, path); |
| 803 | goto again; |
| 804 | |
| 805 | out: |
| 806 | btrfs_free_path(path); |
| 807 | return total_count; |
Chris Mason | be20aa9 | 2007-12-17 20:14:01 -0500 | [diff] [blame] | 808 | } |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 809 | int btrfs_inc_root_ref(struct btrfs_trans_handle *trans, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 810 | struct btrfs_root *root, u64 owner_objectid) |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 811 | { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 812 | u64 generation; |
| 813 | u64 key_objectid; |
| 814 | u64 level; |
| 815 | u32 nritems; |
| 816 | struct btrfs_disk_key disk_key; |
| 817 | |
| 818 | level = btrfs_header_level(root->node); |
| 819 | generation = trans->transid; |
| 820 | nritems = btrfs_header_nritems(root->node); |
| 821 | if (nritems > 0) { |
| 822 | if (level == 0) |
| 823 | btrfs_item_key(root->node, &disk_key, 0); |
| 824 | else |
| 825 | btrfs_node_key(root->node, &disk_key, 0); |
| 826 | key_objectid = btrfs_disk_key_objectid(&disk_key); |
| 827 | } else { |
| 828 | key_objectid = 0; |
| 829 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 830 | return btrfs_inc_extent_ref(trans, root, root->node->start, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 831 | root->node->len, owner_objectid, |
Chris Mason | f6dbff5 | 2007-12-13 11:13:32 -0500 | [diff] [blame] | 832 | generation, level, key_objectid); |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 833 | } |
| 834 | |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 835 | int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 836 | struct extent_buffer *buf) |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 837 | { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 838 | u64 bytenr; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 839 | u32 nritems; |
| 840 | struct btrfs_key key; |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 841 | struct btrfs_file_extent_item *fi; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 842 | int i; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 843 | int level; |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 844 | int ret; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 845 | int faili; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 846 | |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 847 | if (!root->ref_cows) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 848 | return 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 849 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 850 | level = btrfs_header_level(buf); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 851 | nritems = btrfs_header_nritems(buf); |
| 852 | for (i = 0; i < nritems; i++) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 853 | if (level == 0) { |
| 854 | u64 disk_bytenr; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 855 | btrfs_item_key_to_cpu(buf, &key, i); |
| 856 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 857 | continue; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 858 | fi = btrfs_item_ptr(buf, i, |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 859 | struct btrfs_file_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 860 | if (btrfs_file_extent_type(buf, fi) == |
Chris Mason | 236454d | 2007-04-19 13:37:44 -0400 | [diff] [blame] | 861 | BTRFS_FILE_EXTENT_INLINE) |
| 862 | continue; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 863 | disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi); |
| 864 | if (disk_bytenr == 0) |
Chris Mason | 3a68637 | 2007-05-24 13:35:57 -0400 | [diff] [blame] | 865 | continue; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 866 | ret = btrfs_inc_extent_ref(trans, root, disk_bytenr, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 867 | btrfs_file_extent_disk_num_bytes(buf, fi), |
| 868 | root->root_key.objectid, trans->transid, |
| 869 | key.objectid, key.offset); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 870 | if (ret) { |
| 871 | faili = i; |
| 872 | goto fail; |
| 873 | } |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 874 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 875 | bytenr = btrfs_node_blockptr(buf, i); |
Chris Mason | 6caab48 | 2007-12-13 09:48:07 -0500 | [diff] [blame] | 876 | btrfs_node_key_to_cpu(buf, &key, i); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 877 | ret = btrfs_inc_extent_ref(trans, root, bytenr, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 878 | btrfs_level_size(root, level - 1), |
| 879 | root->root_key.objectid, |
Chris Mason | f6dbff5 | 2007-12-13 11:13:32 -0500 | [diff] [blame] | 880 | trans->transid, |
| 881 | level - 1, key.objectid); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 882 | if (ret) { |
| 883 | faili = i; |
| 884 | goto fail; |
| 885 | } |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 886 | } |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 887 | } |
| 888 | return 0; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 889 | fail: |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 890 | WARN_ON(1); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 891 | #if 0 |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 892 | for (i =0; i < faili; i++) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 893 | if (level == 0) { |
| 894 | u64 disk_bytenr; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 895 | btrfs_item_key_to_cpu(buf, &key, i); |
| 896 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 897 | continue; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 898 | fi = btrfs_item_ptr(buf, i, |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 899 | struct btrfs_file_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 900 | if (btrfs_file_extent_type(buf, fi) == |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 901 | BTRFS_FILE_EXTENT_INLINE) |
| 902 | continue; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 903 | disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi); |
| 904 | if (disk_bytenr == 0) |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 905 | continue; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 906 | err = btrfs_free_extent(trans, root, disk_bytenr, |
| 907 | btrfs_file_extent_disk_num_bytes(buf, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 908 | fi), 0); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 909 | BUG_ON(err); |
| 910 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 911 | bytenr = btrfs_node_blockptr(buf, i); |
| 912 | err = btrfs_free_extent(trans, root, bytenr, |
| 913 | btrfs_level_size(root, level - 1), 0); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 914 | BUG_ON(err); |
| 915 | } |
| 916 | } |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 917 | #endif |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 918 | return ret; |
Chris Mason | 02217ed | 2007-03-02 16:08:05 -0500 | [diff] [blame] | 919 | } |
| 920 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 921 | static int write_one_cache_group(struct btrfs_trans_handle *trans, |
| 922 | struct btrfs_root *root, |
| 923 | struct btrfs_path *path, |
| 924 | struct btrfs_block_group_cache *cache) |
| 925 | { |
| 926 | int ret; |
| 927 | int pending_ret; |
| 928 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 929 | unsigned long bi; |
| 930 | struct extent_buffer *leaf; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 931 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 932 | ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 933 | if (ret < 0) |
| 934 | goto fail; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 935 | BUG_ON(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 936 | |
| 937 | leaf = path->nodes[0]; |
| 938 | bi = btrfs_item_ptr_offset(leaf, path->slots[0]); |
| 939 | write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item)); |
| 940 | btrfs_mark_buffer_dirty(leaf); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 941 | btrfs_release_path(extent_root, path); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 942 | fail: |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 943 | finish_current_insert(trans, extent_root); |
| 944 | pending_ret = del_pending_extents(trans, extent_root); |
| 945 | if (ret) |
| 946 | return ret; |
| 947 | if (pending_ret) |
| 948 | return pending_ret; |
| 949 | return 0; |
| 950 | |
| 951 | } |
| 952 | |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 953 | int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans, |
| 954 | struct btrfs_root *root) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 955 | { |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 956 | struct extent_io_tree *block_group_cache; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 957 | struct btrfs_block_group_cache *cache; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 958 | int ret; |
| 959 | int err = 0; |
| 960 | int werr = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 961 | struct btrfs_path *path; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 962 | u64 last = 0; |
| 963 | u64 start; |
| 964 | u64 end; |
| 965 | u64 ptr; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 966 | |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 967 | block_group_cache = &root->fs_info->block_group_cache; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 968 | path = btrfs_alloc_path(); |
| 969 | if (!path) |
| 970 | return -ENOMEM; |
| 971 | |
| 972 | while(1) { |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 973 | ret = find_first_extent_bit(block_group_cache, last, |
| 974 | &start, &end, BLOCK_GROUP_DIRTY); |
| 975 | if (ret) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 976 | break; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 977 | |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 978 | last = end + 1; |
| 979 | ret = get_state_private(block_group_cache, start, &ptr); |
| 980 | if (ret) |
| 981 | break; |
Jens Axboe | ae2f541 | 2007-10-19 09:22:59 -0400 | [diff] [blame] | 982 | cache = (struct btrfs_block_group_cache *)(unsigned long)ptr; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 983 | err = write_one_cache_group(trans, root, |
| 984 | path, cache); |
| 985 | /* |
| 986 | * if we fail to write the cache group, we want |
| 987 | * to keep it marked dirty in hopes that a later |
| 988 | * write will work |
| 989 | */ |
| 990 | if (err) { |
| 991 | werr = err; |
| 992 | continue; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 993 | } |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 994 | clear_extent_bits(block_group_cache, start, end, |
| 995 | BLOCK_GROUP_DIRTY, GFP_NOFS); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 996 | } |
| 997 | btrfs_free_path(path); |
| 998 | return werr; |
| 999 | } |
| 1000 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1001 | static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info, |
| 1002 | u64 flags) |
| 1003 | { |
| 1004 | struct list_head *head = &info->space_info; |
| 1005 | struct list_head *cur; |
| 1006 | struct btrfs_space_info *found; |
| 1007 | list_for_each(cur, head) { |
| 1008 | found = list_entry(cur, struct btrfs_space_info, list); |
| 1009 | if (found->flags == flags) |
| 1010 | return found; |
| 1011 | } |
| 1012 | return NULL; |
| 1013 | |
| 1014 | } |
| 1015 | |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1016 | static int update_space_info(struct btrfs_fs_info *info, u64 flags, |
| 1017 | u64 total_bytes, u64 bytes_used, |
| 1018 | struct btrfs_space_info **space_info) |
| 1019 | { |
| 1020 | struct btrfs_space_info *found; |
| 1021 | |
| 1022 | found = __find_space_info(info, flags); |
| 1023 | if (found) { |
| 1024 | found->total_bytes += total_bytes; |
| 1025 | found->bytes_used += bytes_used; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 1026 | found->full = 0; |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1027 | WARN_ON(found->total_bytes < found->bytes_used); |
| 1028 | *space_info = found; |
| 1029 | return 0; |
| 1030 | } |
| 1031 | found = kmalloc(sizeof(*found), GFP_NOFS); |
| 1032 | if (!found) |
| 1033 | return -ENOMEM; |
| 1034 | |
| 1035 | list_add(&found->list, &info->space_info); |
| 1036 | found->flags = flags; |
| 1037 | found->total_bytes = total_bytes; |
| 1038 | found->bytes_used = bytes_used; |
| 1039 | found->bytes_pinned = 0; |
| 1040 | found->full = 0; |
| 1041 | *space_info = found; |
| 1042 | return 0; |
| 1043 | } |
| 1044 | |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1045 | static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags) |
| 1046 | { |
| 1047 | u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 | |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1048 | BTRFS_BLOCK_GROUP_RAID1 | |
Chris Mason | 321aecc | 2008-04-16 10:49:51 -0400 | [diff] [blame] | 1049 | BTRFS_BLOCK_GROUP_RAID10 | |
Chris Mason | 611f0e0 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1050 | BTRFS_BLOCK_GROUP_DUP); |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1051 | if (extra_flags) { |
| 1052 | if (flags & BTRFS_BLOCK_GROUP_DATA) |
| 1053 | fs_info->avail_data_alloc_bits |= extra_flags; |
| 1054 | if (flags & BTRFS_BLOCK_GROUP_METADATA) |
| 1055 | fs_info->avail_metadata_alloc_bits |= extra_flags; |
| 1056 | if (flags & BTRFS_BLOCK_GROUP_SYSTEM) |
| 1057 | fs_info->avail_system_alloc_bits |= extra_flags; |
| 1058 | } |
| 1059 | } |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1060 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1061 | static int do_chunk_alloc(struct btrfs_trans_handle *trans, |
| 1062 | struct btrfs_root *extent_root, u64 alloc_bytes, |
| 1063 | u64 flags) |
| 1064 | { |
| 1065 | struct btrfs_space_info *space_info; |
| 1066 | u64 thresh; |
| 1067 | u64 start; |
| 1068 | u64 num_bytes; |
| 1069 | int ret; |
| 1070 | |
| 1071 | space_info = __find_space_info(extent_root->fs_info, flags); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1072 | if (!space_info) { |
| 1073 | ret = update_space_info(extent_root->fs_info, flags, |
| 1074 | 0, 0, &space_info); |
| 1075 | BUG_ON(ret); |
| 1076 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1077 | BUG_ON(!space_info); |
| 1078 | |
| 1079 | if (space_info->full) |
| 1080 | return 0; |
| 1081 | |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1082 | thresh = div_factor(space_info->total_bytes, 6); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1083 | if ((space_info->bytes_used + space_info->bytes_pinned + alloc_bytes) < |
| 1084 | thresh) |
| 1085 | return 0; |
| 1086 | |
| 1087 | ret = btrfs_alloc_chunk(trans, extent_root, &start, &num_bytes, flags); |
| 1088 | if (ret == -ENOSPC) { |
| 1089 | printk("space info full %Lu\n", flags); |
| 1090 | space_info->full = 1; |
| 1091 | return 0; |
| 1092 | } |
| 1093 | |
| 1094 | BUG_ON(ret); |
| 1095 | |
| 1096 | ret = btrfs_make_block_group(trans, extent_root, 0, flags, |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 1097 | BTRFS_FIRST_CHUNK_TREE_OBJECTID, start, num_bytes); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1098 | BUG_ON(ret); |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1099 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1100 | return 0; |
| 1101 | } |
| 1102 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1103 | static int update_block_group(struct btrfs_trans_handle *trans, |
| 1104 | struct btrfs_root *root, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1105 | u64 bytenr, u64 num_bytes, int alloc, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1106 | int mark_free) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1107 | { |
| 1108 | struct btrfs_block_group_cache *cache; |
| 1109 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1110 | u64 total = num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1111 | u64 old_val; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1112 | u64 byte_in_group; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 1113 | u64 start; |
| 1114 | u64 end; |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 1115 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1116 | while(total) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1117 | cache = btrfs_lookup_block_group(info, bytenr); |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 1118 | if (!cache) { |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1119 | return -1; |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 1120 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1121 | byte_in_group = bytenr - cache->key.objectid; |
| 1122 | WARN_ON(byte_in_group > cache->key.offset); |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 1123 | start = cache->key.objectid; |
| 1124 | end = start + cache->key.offset - 1; |
| 1125 | set_extent_bits(&info->block_group_cache, start, end, |
| 1126 | BLOCK_GROUP_DIRTY, GFP_NOFS); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1127 | |
| 1128 | old_val = btrfs_block_group_used(&cache->item); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1129 | num_bytes = min(total, cache->key.offset - byte_in_group); |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 1130 | if (alloc) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1131 | old_val += num_bytes; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1132 | cache->space_info->bytes_used += num_bytes; |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 1133 | } else { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1134 | old_val -= num_bytes; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1135 | cache->space_info->bytes_used -= num_bytes; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 1136 | if (mark_free) { |
| 1137 | set_extent_dirty(&info->free_space_cache, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1138 | bytenr, bytenr + num_bytes - 1, |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 1139 | GFP_NOFS); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 1140 | } |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 1141 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1142 | btrfs_set_block_group_used(&cache->item, old_val); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1143 | total -= num_bytes; |
| 1144 | bytenr += num_bytes; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1145 | } |
| 1146 | return 0; |
| 1147 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1148 | |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 1149 | static int update_pinned_extents(struct btrfs_root *root, |
| 1150 | u64 bytenr, u64 num, int pin) |
| 1151 | { |
| 1152 | u64 len; |
| 1153 | struct btrfs_block_group_cache *cache; |
| 1154 | struct btrfs_fs_info *fs_info = root->fs_info; |
| 1155 | |
| 1156 | if (pin) { |
| 1157 | set_extent_dirty(&fs_info->pinned_extents, |
| 1158 | bytenr, bytenr + num - 1, GFP_NOFS); |
| 1159 | } else { |
| 1160 | clear_extent_dirty(&fs_info->pinned_extents, |
| 1161 | bytenr, bytenr + num - 1, GFP_NOFS); |
| 1162 | } |
| 1163 | while (num > 0) { |
| 1164 | cache = btrfs_lookup_block_group(fs_info, bytenr); |
| 1165 | WARN_ON(!cache); |
| 1166 | len = min(num, cache->key.offset - |
| 1167 | (bytenr - cache->key.objectid)); |
| 1168 | if (pin) { |
| 1169 | cache->pinned += len; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1170 | cache->space_info->bytes_pinned += len; |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 1171 | fs_info->total_pinned += len; |
| 1172 | } else { |
| 1173 | cache->pinned -= len; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1174 | cache->space_info->bytes_pinned -= len; |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 1175 | fs_info->total_pinned -= len; |
| 1176 | } |
| 1177 | bytenr += len; |
| 1178 | num -= len; |
| 1179 | } |
| 1180 | return 0; |
| 1181 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1182 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1183 | int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy) |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 1184 | { |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 1185 | u64 last = 0; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1186 | u64 start; |
| 1187 | u64 end; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1188 | struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents; |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 1189 | int ret; |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 1190 | |
| 1191 | while(1) { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1192 | ret = find_first_extent_bit(pinned_extents, last, |
| 1193 | &start, &end, EXTENT_DIRTY); |
| 1194 | if (ret) |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 1195 | break; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1196 | set_extent_dirty(copy, start, end, GFP_NOFS); |
| 1197 | last = end + 1; |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 1198 | } |
| 1199 | return 0; |
| 1200 | } |
| 1201 | |
| 1202 | int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, |
| 1203 | struct btrfs_root *root, |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1204 | struct extent_io_tree *unpin) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1205 | { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1206 | u64 start; |
| 1207 | u64 end; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1208 | int ret; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1209 | struct extent_io_tree *free_space_cache; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 1210 | free_space_cache = &root->fs_info->free_space_cache; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1211 | |
| 1212 | while(1) { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1213 | ret = find_first_extent_bit(unpin, 0, &start, &end, |
| 1214 | EXTENT_DIRTY); |
| 1215 | if (ret) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1216 | break; |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 1217 | update_pinned_extents(root, start, end + 1 - start, 0); |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1218 | clear_extent_dirty(unpin, start, end, GFP_NOFS); |
| 1219 | set_extent_dirty(free_space_cache, start, end, GFP_NOFS); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1220 | } |
| 1221 | return 0; |
| 1222 | } |
| 1223 | |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 1224 | static int finish_current_insert(struct btrfs_trans_handle *trans, |
| 1225 | struct btrfs_root *extent_root) |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1226 | { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1227 | u64 start; |
| 1228 | u64 end; |
| 1229 | struct btrfs_fs_info *info = extent_root->fs_info; |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 1230 | struct extent_buffer *eb; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1231 | struct btrfs_path *path; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 1232 | struct btrfs_key ins; |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 1233 | struct btrfs_disk_key first; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1234 | struct btrfs_extent_item extent_item; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1235 | int ret; |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 1236 | int level; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1237 | int err = 0; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1238 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1239 | btrfs_set_stack_extent_refs(&extent_item, 1); |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 1240 | btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1241 | path = btrfs_alloc_path(); |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1242 | |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 1243 | while(1) { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1244 | ret = find_first_extent_bit(&info->extent_ins, 0, &start, |
| 1245 | &end, EXTENT_LOCKED); |
| 1246 | if (ret) |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 1247 | break; |
| 1248 | |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1249 | ins.objectid = start; |
| 1250 | ins.offset = end + 1 - start; |
| 1251 | err = btrfs_insert_item(trans, extent_root, &ins, |
| 1252 | &extent_item, sizeof(extent_item)); |
| 1253 | clear_extent_bits(&info->extent_ins, start, end, EXTENT_LOCKED, |
| 1254 | GFP_NOFS); |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 1255 | eb = read_tree_block(extent_root, ins.objectid, ins.offset); |
| 1256 | level = btrfs_header_level(eb); |
| 1257 | if (level == 0) { |
| 1258 | btrfs_item_key(eb, &first, 0); |
| 1259 | } else { |
| 1260 | btrfs_node_key(eb, &first, 0); |
| 1261 | } |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1262 | err = btrfs_insert_extent_backref(trans, extent_root, path, |
| 1263 | start, extent_root->root_key.objectid, |
Chris Mason | f6dbff5 | 2007-12-13 11:13:32 -0500 | [diff] [blame] | 1264 | 0, level, |
| 1265 | btrfs_disk_key_objectid(&first)); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1266 | BUG_ON(err); |
Chris Mason | d8d5f3e | 2007-12-11 12:42:00 -0500 | [diff] [blame] | 1267 | free_extent_buffer(eb); |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1268 | } |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1269 | btrfs_free_path(path); |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1270 | return 0; |
| 1271 | } |
| 1272 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1273 | static int pin_down_bytes(struct btrfs_root *root, u64 bytenr, u32 num_bytes, |
| 1274 | int pending) |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1275 | { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1276 | int err = 0; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1277 | struct extent_buffer *buf; |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 1278 | |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 1279 | if (!pending) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1280 | buf = btrfs_find_tree_block(root, bytenr, num_bytes); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1281 | if (buf) { |
| 1282 | if (btrfs_buffer_uptodate(buf)) { |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 1283 | u64 transid = |
| 1284 | root->fs_info->running_transaction->transid; |
Chris Mason | dc17ff8 | 2008-01-08 15:46:30 -0500 | [diff] [blame] | 1285 | u64 header_transid = |
| 1286 | btrfs_header_generation(buf); |
Chris Mason | 6bc3467 | 2008-04-04 15:40:00 -0400 | [diff] [blame] | 1287 | if (header_transid == transid && |
| 1288 | !btrfs_header_flag(buf, |
| 1289 | BTRFS_HEADER_FLAG_WRITTEN)) { |
Chris Mason | 55c6907 | 2008-01-09 15:55:33 -0500 | [diff] [blame] | 1290 | clean_tree_block(NULL, root, buf); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1291 | free_extent_buffer(buf); |
Yan | c549228 | 2007-11-06 10:25:25 -0500 | [diff] [blame] | 1292 | return 1; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 1293 | } |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 1294 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1295 | free_extent_buffer(buf); |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 1296 | } |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 1297 | update_pinned_extents(root, bytenr, num_bytes, 1); |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 1298 | } else { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1299 | set_extent_bits(&root->fs_info->pending_del, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1300 | bytenr, bytenr + num_bytes - 1, |
| 1301 | EXTENT_LOCKED, GFP_NOFS); |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 1302 | } |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 1303 | BUG_ON(err < 0); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1304 | return 0; |
| 1305 | } |
| 1306 | |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1307 | /* |
| 1308 | * remove an extent from the root, returns 0 on success |
| 1309 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1310 | static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1311 | *root, u64 bytenr, u64 num_bytes, |
| 1312 | u64 root_objectid, u64 ref_generation, |
| 1313 | u64 owner_objectid, u64 owner_offset, int pin, |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 1314 | int mark_free) |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1315 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1316 | struct btrfs_path *path; |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 1317 | struct btrfs_key key; |
Chris Mason | 1261ec4 | 2007-03-20 20:35:03 -0400 | [diff] [blame] | 1318 | struct btrfs_fs_info *info = root->fs_info; |
| 1319 | struct btrfs_root *extent_root = info->extent_root; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1320 | struct extent_buffer *leaf; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1321 | int ret; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 1322 | int extent_slot = 0; |
| 1323 | int found_extent = 0; |
| 1324 | int num_to_del = 1; |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 1325 | struct btrfs_extent_item *ei; |
Chris Mason | cf27e1e | 2007-03-13 09:49:06 -0400 | [diff] [blame] | 1326 | u32 refs; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1327 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1328 | key.objectid = bytenr; |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 1329 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1330 | key.offset = num_bytes; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1331 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1332 | if (!path) |
| 1333 | return -ENOMEM; |
| 1334 | |
Chris Mason | 3c12ac7 | 2008-04-21 12:01:38 -0400 | [diff] [blame] | 1335 | path->reada = 1; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1336 | ret = lookup_extent_backref(trans, extent_root, path, |
| 1337 | bytenr, root_objectid, |
| 1338 | ref_generation, |
| 1339 | owner_objectid, owner_offset, 1); |
| 1340 | if (ret == 0) { |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 1341 | struct btrfs_key found_key; |
| 1342 | extent_slot = path->slots[0]; |
| 1343 | while(extent_slot > 0) { |
| 1344 | extent_slot--; |
| 1345 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, |
| 1346 | extent_slot); |
| 1347 | if (found_key.objectid != bytenr) |
| 1348 | break; |
| 1349 | if (found_key.type == BTRFS_EXTENT_ITEM_KEY && |
| 1350 | found_key.offset == num_bytes) { |
| 1351 | found_extent = 1; |
| 1352 | break; |
| 1353 | } |
| 1354 | if (path->slots[0] - extent_slot > 5) |
| 1355 | break; |
| 1356 | } |
| 1357 | if (!found_extent) |
| 1358 | ret = btrfs_del_item(trans, extent_root, path); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1359 | } else { |
| 1360 | btrfs_print_leaf(extent_root, path->nodes[0]); |
| 1361 | WARN_ON(1); |
| 1362 | printk("Unable to find ref byte nr %Lu root %Lu " |
| 1363 | " gen %Lu owner %Lu offset %Lu\n", bytenr, |
| 1364 | root_objectid, ref_generation, owner_objectid, |
| 1365 | owner_offset); |
| 1366 | } |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 1367 | if (!found_extent) { |
| 1368 | btrfs_release_path(extent_root, path); |
| 1369 | ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1); |
| 1370 | if (ret < 0) |
| 1371 | return ret; |
| 1372 | BUG_ON(ret); |
| 1373 | extent_slot = path->slots[0]; |
| 1374 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1375 | |
| 1376 | leaf = path->nodes[0]; |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 1377 | ei = btrfs_item_ptr(leaf, extent_slot, |
Chris Mason | 123abc8 | 2007-03-14 14:14:43 -0400 | [diff] [blame] | 1378 | struct btrfs_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1379 | refs = btrfs_extent_refs(leaf, ei); |
| 1380 | BUG_ON(refs == 0); |
| 1381 | refs -= 1; |
| 1382 | btrfs_set_extent_refs(leaf, ei, refs); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 1383 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1384 | btrfs_mark_buffer_dirty(leaf); |
| 1385 | |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 1386 | if (refs == 0 && found_extent && path->slots[0] == extent_slot + 1) { |
| 1387 | /* if the back ref and the extent are next to each other |
| 1388 | * they get deleted below in one shot |
| 1389 | */ |
| 1390 | path->slots[0] = extent_slot; |
| 1391 | num_to_del = 2; |
| 1392 | } else if (found_extent) { |
| 1393 | /* otherwise delete the extent back ref */ |
| 1394 | ret = btrfs_del_item(trans, extent_root, path); |
| 1395 | BUG_ON(ret); |
| 1396 | /* if refs are 0, we need to setup the path for deletion */ |
| 1397 | if (refs == 0) { |
| 1398 | btrfs_release_path(extent_root, path); |
| 1399 | ret = btrfs_search_slot(trans, extent_root, &key, path, |
| 1400 | -1, 1); |
| 1401 | if (ret < 0) |
| 1402 | return ret; |
| 1403 | BUG_ON(ret); |
| 1404 | } |
| 1405 | } |
| 1406 | |
Chris Mason | cf27e1e | 2007-03-13 09:49:06 -0400 | [diff] [blame] | 1407 | if (refs == 0) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1408 | u64 super_used; |
| 1409 | u64 root_used; |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 1410 | |
| 1411 | if (pin) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1412 | ret = pin_down_bytes(root, bytenr, num_bytes, 0); |
Yan | c549228 | 2007-11-06 10:25:25 -0500 | [diff] [blame] | 1413 | if (ret > 0) |
| 1414 | mark_free = 1; |
| 1415 | BUG_ON(ret < 0); |
Chris Mason | 78fae27 | 2007-03-25 11:35:08 -0400 | [diff] [blame] | 1416 | } |
| 1417 | |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 1418 | /* block accounting for super block */ |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1419 | super_used = btrfs_super_bytes_used(&info->super_copy); |
| 1420 | btrfs_set_super_bytes_used(&info->super_copy, |
| 1421 | super_used - num_bytes); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 1422 | |
| 1423 | /* block accounting for root item */ |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1424 | root_used = btrfs_root_used(&root->root_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1425 | btrfs_set_root_used(&root->root_item, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1426 | root_used - num_bytes); |
Chris Mason | 952fcca | 2008-02-18 16:33:44 -0500 | [diff] [blame] | 1427 | ret = btrfs_del_items(trans, extent_root, path, path->slots[0], |
| 1428 | num_to_del); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1429 | if (ret) { |
| 1430 | return ret; |
| 1431 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1432 | ret = update_block_group(trans, root, bytenr, num_bytes, 0, |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1433 | mark_free); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 1434 | BUG_ON(ret); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1435 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1436 | btrfs_free_path(path); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1437 | finish_current_insert(trans, extent_root); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1438 | return ret; |
| 1439 | } |
| 1440 | |
| 1441 | /* |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1442 | * find all the blocks marked as pending in the radix tree and remove |
| 1443 | * them from the extent map |
| 1444 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1445 | static int del_pending_extents(struct btrfs_trans_handle *trans, struct |
| 1446 | btrfs_root *extent_root) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1447 | { |
| 1448 | int ret; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1449 | int err = 0; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1450 | u64 start; |
| 1451 | u64 end; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 1452 | struct extent_io_tree *pending_del; |
| 1453 | struct extent_io_tree *pinned_extents; |
Chris Mason | 8ef9762 | 2007-03-26 10:15:30 -0400 | [diff] [blame] | 1454 | |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1455 | pending_del = &extent_root->fs_info->pending_del; |
| 1456 | pinned_extents = &extent_root->fs_info->pinned_extents; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1457 | |
| 1458 | while(1) { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1459 | ret = find_first_extent_bit(pending_del, 0, &start, &end, |
| 1460 | EXTENT_LOCKED); |
| 1461 | if (ret) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1462 | break; |
Yan | 324ae4d | 2007-11-16 14:57:08 -0500 | [diff] [blame] | 1463 | update_pinned_extents(extent_root, start, end + 1 - start, 1); |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1464 | clear_extent_bits(pending_del, start, end, EXTENT_LOCKED, |
| 1465 | GFP_NOFS); |
| 1466 | ret = __free_extent(trans, extent_root, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1467 | start, end + 1 - start, |
| 1468 | extent_root->root_key.objectid, |
| 1469 | 0, 0, 0, 0, 0); |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1470 | if (ret) |
| 1471 | err = ret; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1472 | } |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1473 | return err; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1474 | } |
| 1475 | |
| 1476 | /* |
| 1477 | * remove an extent from the root, returns 0 on success |
| 1478 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1479 | int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1480 | *root, u64 bytenr, u64 num_bytes, |
| 1481 | u64 root_objectid, u64 ref_generation, |
| 1482 | u64 owner_objectid, u64 owner_offset, int pin) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1483 | { |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 1484 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1485 | int pending_ret; |
| 1486 | int ret; |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1487 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1488 | WARN_ON(num_bytes < root->sectorsize); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1489 | if (!root->ref_cows) |
| 1490 | ref_generation = 0; |
| 1491 | |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1492 | if (root == extent_root) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1493 | pin_down_bytes(root, bytenr, num_bytes, 1); |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1494 | return 0; |
| 1495 | } |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1496 | ret = __free_extent(trans, root, bytenr, num_bytes, root_objectid, |
| 1497 | ref_generation, owner_objectid, owner_offset, |
| 1498 | pin, pin == 0); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1499 | pending_ret = del_pending_extents(trans, root->fs_info->extent_root); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1500 | return ret ? ret : pending_ret; |
| 1501 | } |
| 1502 | |
Chris Mason | 87ee04e | 2007-11-30 11:30:34 -0500 | [diff] [blame] | 1503 | static u64 stripe_align(struct btrfs_root *root, u64 val) |
| 1504 | { |
| 1505 | u64 mask = ((u64)root->stripesize - 1); |
| 1506 | u64 ret = (val + mask) & ~mask; |
| 1507 | return ret; |
| 1508 | } |
| 1509 | |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1510 | /* |
| 1511 | * walks the btree of allocated extents and find a hole of a given size. |
| 1512 | * The key ins is changed to record the hole: |
| 1513 | * ins->objectid == block start |
Chris Mason | 62e2749 | 2007-03-15 12:56:47 -0400 | [diff] [blame] | 1514 | * ins->flags = BTRFS_EXTENT_ITEM_KEY |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1515 | * ins->offset == number of blocks |
| 1516 | * Any available blocks before search_start are skipped. |
| 1517 | */ |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 1518 | static int noinline find_free_extent(struct btrfs_trans_handle *trans, |
| 1519 | struct btrfs_root *orig_root, |
| 1520 | u64 num_bytes, u64 empty_size, |
| 1521 | u64 search_start, u64 search_end, |
| 1522 | u64 hint_byte, struct btrfs_key *ins, |
| 1523 | u64 exclude_start, u64 exclude_nr, |
| 1524 | int data) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1525 | { |
Chris Mason | 87ee04e | 2007-11-30 11:30:34 -0500 | [diff] [blame] | 1526 | int ret; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 1527 | u64 orig_search_start = search_start; |
Chris Mason | 9f5fae2 | 2007-03-20 14:38:32 -0400 | [diff] [blame] | 1528 | struct btrfs_root * root = orig_root->fs_info->extent_root; |
Chris Mason | f2458e1 | 2007-04-25 15:52:25 -0400 | [diff] [blame] | 1529 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1530 | u64 total_needed = num_bytes; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1531 | u64 *last_ptr = NULL; |
Chris Mason | be08c1b | 2007-05-03 09:06:49 -0400 | [diff] [blame] | 1532 | struct btrfs_block_group_cache *block_group; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 1533 | int full_scan = 0; |
Chris Mason | fbdc762 | 2007-05-30 10:22:12 -0400 | [diff] [blame] | 1534 | int wrapped = 0; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1535 | int empty_cluster = 2 * 1024 * 1024; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1536 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1537 | WARN_ON(num_bytes < root->sectorsize); |
Chris Mason | b1a4d96 | 2007-04-04 15:27:52 -0400 | [diff] [blame] | 1538 | btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY); |
| 1539 | |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1540 | if (data & BTRFS_BLOCK_GROUP_METADATA) { |
| 1541 | last_ptr = &root->fs_info->last_alloc; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1542 | empty_cluster = 256 * 1024; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1543 | } |
| 1544 | |
| 1545 | if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD)) { |
| 1546 | last_ptr = &root->fs_info->last_data_alloc; |
| 1547 | } |
| 1548 | |
| 1549 | if (last_ptr) { |
| 1550 | if (*last_ptr) |
| 1551 | hint_byte = *last_ptr; |
| 1552 | else { |
| 1553 | empty_size += empty_cluster; |
| 1554 | } |
| 1555 | } |
| 1556 | |
Chris Mason | 7f93bf8 | 2008-03-24 15:01:28 -0400 | [diff] [blame] | 1557 | if (search_end == (u64)-1) |
| 1558 | search_end = btrfs_super_total_bytes(&info->super_copy); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1559 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1560 | if (hint_byte) { |
| 1561 | block_group = btrfs_lookup_block_group(info, hint_byte); |
Chris Mason | 1a2b2ac | 2007-12-04 13:18:24 -0500 | [diff] [blame] | 1562 | if (!block_group) |
| 1563 | hint_byte = search_start; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 1564 | block_group = btrfs_find_block_group(root, block_group, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1565 | hint_byte, data, 1); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1566 | if (last_ptr && *last_ptr == 0 && block_group) |
| 1567 | hint_byte = block_group->key.objectid; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 1568 | } else { |
| 1569 | block_group = btrfs_find_block_group(root, |
Chris Mason | 1a2b2ac | 2007-12-04 13:18:24 -0500 | [diff] [blame] | 1570 | trans->block_group, |
| 1571 | search_start, data, 1); |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 1572 | } |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1573 | search_start = max(search_start, hint_byte); |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 1574 | |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1575 | total_needed += empty_size; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1576 | |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 1577 | check_failed: |
Chris Mason | 70b043f | 2007-12-13 09:02:46 -0500 | [diff] [blame] | 1578 | if (!block_group) { |
| 1579 | block_group = btrfs_lookup_block_group(info, search_start); |
| 1580 | if (!block_group) |
| 1581 | block_group = btrfs_lookup_block_group(info, |
| 1582 | orig_search_start); |
| 1583 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1584 | ret = find_search_start(root, &block_group, &search_start, |
| 1585 | total_needed, data); |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1586 | if (ret == -ENOSPC && last_ptr && *last_ptr) { |
| 1587 | *last_ptr = 0; |
| 1588 | block_group = btrfs_lookup_block_group(info, |
| 1589 | orig_search_start); |
| 1590 | search_start = orig_search_start; |
| 1591 | ret = find_search_start(root, &block_group, &search_start, |
| 1592 | total_needed, data); |
| 1593 | } |
| 1594 | if (ret == -ENOSPC) |
| 1595 | goto enospc; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1596 | if (ret) |
| 1597 | goto error; |
| 1598 | |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1599 | if (last_ptr && *last_ptr && search_start != *last_ptr) { |
| 1600 | *last_ptr = 0; |
| 1601 | if (!empty_size) { |
| 1602 | empty_size += empty_cluster; |
| 1603 | total_needed += empty_size; |
| 1604 | } |
| 1605 | block_group = btrfs_lookup_block_group(info, |
| 1606 | orig_search_start); |
| 1607 | search_start = orig_search_start; |
| 1608 | ret = find_search_start(root, &block_group, |
| 1609 | &search_start, total_needed, data); |
| 1610 | if (ret == -ENOSPC) |
| 1611 | goto enospc; |
| 1612 | if (ret) |
| 1613 | goto error; |
| 1614 | } |
| 1615 | |
Chris Mason | 87ee04e | 2007-11-30 11:30:34 -0500 | [diff] [blame] | 1616 | search_start = stripe_align(root, search_start); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1617 | ins->objectid = search_start; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1618 | ins->offset = num_bytes; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 1619 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1620 | if (ins->objectid + num_bytes >= search_end) |
Chris Mason | cf67582 | 2007-09-17 11:00:51 -0400 | [diff] [blame] | 1621 | goto enospc; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1622 | |
| 1623 | if (ins->objectid + num_bytes > |
| 1624 | block_group->key.objectid + block_group->key.offset) { |
Chris Mason | e19caa5 | 2007-10-15 16:17:44 -0400 | [diff] [blame] | 1625 | search_start = block_group->key.objectid + |
| 1626 | block_group->key.offset; |
| 1627 | goto new_group; |
| 1628 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1629 | |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1630 | if (test_range_bit(&info->extent_ins, ins->objectid, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1631 | ins->objectid + num_bytes -1, EXTENT_LOCKED, 0)) { |
| 1632 | search_start = ins->objectid + num_bytes; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1633 | goto new_group; |
| 1634 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1635 | |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1636 | if (test_range_bit(&info->pinned_extents, ins->objectid, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1637 | ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) { |
| 1638 | search_start = ins->objectid + num_bytes; |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1639 | goto new_group; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1640 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1641 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1642 | if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start && |
Chris Mason | f2654de | 2007-06-26 12:20:46 -0400 | [diff] [blame] | 1643 | ins->objectid < exclude_start + exclude_nr)) { |
| 1644 | search_start = exclude_start + exclude_nr; |
| 1645 | goto new_group; |
| 1646 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1647 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1648 | if (!(data & BTRFS_BLOCK_GROUP_DATA)) { |
Chris Mason | 5276aed | 2007-06-11 21:33:38 -0400 | [diff] [blame] | 1649 | block_group = btrfs_lookup_block_group(info, ins->objectid); |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 1650 | if (block_group) |
| 1651 | trans->block_group = block_group; |
Chris Mason | cd1bc46 | 2007-04-27 10:08:34 -0400 | [diff] [blame] | 1652 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1653 | ins->offset = num_bytes; |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1654 | if (last_ptr) { |
| 1655 | *last_ptr = ins->objectid + ins->offset; |
| 1656 | if (*last_ptr == |
| 1657 | btrfs_super_total_bytes(&root->fs_info->super_copy)) { |
| 1658 | *last_ptr = 0; |
| 1659 | } |
| 1660 | } |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1661 | return 0; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 1662 | |
| 1663 | new_group: |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1664 | if (search_start + num_bytes >= search_end) { |
Chris Mason | cf67582 | 2007-09-17 11:00:51 -0400 | [diff] [blame] | 1665 | enospc: |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 1666 | search_start = orig_search_start; |
Chris Mason | fbdc762 | 2007-05-30 10:22:12 -0400 | [diff] [blame] | 1667 | if (full_scan) { |
| 1668 | ret = -ENOSPC; |
| 1669 | goto error; |
| 1670 | } |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1671 | if (wrapped) { |
| 1672 | if (!full_scan) |
| 1673 | total_needed -= empty_size; |
Chris Mason | fbdc762 | 2007-05-30 10:22:12 -0400 | [diff] [blame] | 1674 | full_scan = 1; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 1675 | } else |
Chris Mason | fbdc762 | 2007-05-30 10:22:12 -0400 | [diff] [blame] | 1676 | wrapped = 1; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 1677 | } |
Chris Mason | 5276aed | 2007-06-11 21:33:38 -0400 | [diff] [blame] | 1678 | block_group = btrfs_lookup_block_group(info, search_start); |
Chris Mason | fbdc762 | 2007-05-30 10:22:12 -0400 | [diff] [blame] | 1679 | cond_resched(); |
Chris Mason | 1a2b2ac | 2007-12-04 13:18:24 -0500 | [diff] [blame] | 1680 | block_group = btrfs_find_block_group(root, block_group, |
| 1681 | search_start, data, 0); |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 1682 | goto check_failed; |
| 1683 | |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 1684 | error: |
Chris Mason | 0f70abe | 2007-02-28 16:46:22 -0500 | [diff] [blame] | 1685 | return ret; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1686 | } |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1687 | /* |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1688 | * finds a free extent and does all the dirty work required for allocation |
| 1689 | * returns the key for the extent through ins, and a tree buffer for |
| 1690 | * the first block of the extent through buf. |
| 1691 | * |
| 1692 | * returns 0 if everything worked, non-zero otherwise. |
| 1693 | */ |
Chris Mason | 4d77567 | 2007-04-20 20:23:12 -0400 | [diff] [blame] | 1694 | int btrfs_alloc_extent(struct btrfs_trans_handle *trans, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1695 | struct btrfs_root *root, |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 1696 | u64 num_bytes, u64 min_alloc_size, |
| 1697 | u64 root_objectid, u64 ref_generation, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1698 | u64 owner, u64 owner_offset, |
| 1699 | u64 empty_size, u64 hint_byte, |
Chris Mason | be08c1b | 2007-05-03 09:06:49 -0400 | [diff] [blame] | 1700 | u64 search_end, struct btrfs_key *ins, int data) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1701 | { |
| 1702 | int ret; |
| 1703 | int pending_ret; |
Chris Mason | c31f883 | 2008-01-08 15:46:31 -0500 | [diff] [blame] | 1704 | u64 super_used; |
| 1705 | u64 root_used; |
Chris Mason | fbdc762 | 2007-05-30 10:22:12 -0400 | [diff] [blame] | 1706 | u64 search_start = 0; |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1707 | u64 alloc_profile; |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 1708 | u32 sizes[2]; |
Chris Mason | 1261ec4 | 2007-03-20 20:35:03 -0400 | [diff] [blame] | 1709 | struct btrfs_fs_info *info = root->fs_info; |
| 1710 | struct btrfs_root *extent_root = info->extent_root; |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 1711 | struct btrfs_extent_item *extent_item; |
| 1712 | struct btrfs_extent_ref *ref; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1713 | struct btrfs_path *path; |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 1714 | struct btrfs_key keys[2]; |
Chris Mason | 8f662a7 | 2008-01-02 10:01:11 -0500 | [diff] [blame] | 1715 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1716 | if (data) { |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1717 | alloc_profile = info->avail_data_alloc_bits & |
| 1718 | info->data_alloc_profile; |
| 1719 | data = BTRFS_BLOCK_GROUP_DATA | alloc_profile; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1720 | } else if (root == root->fs_info->chunk_root) { |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1721 | alloc_profile = info->avail_system_alloc_bits & |
| 1722 | info->system_alloc_profile; |
| 1723 | data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1724 | } else { |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1725 | alloc_profile = info->avail_metadata_alloc_bits & |
| 1726 | info->metadata_alloc_profile; |
| 1727 | data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1728 | } |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 1729 | again: |
Chris Mason | 3bf3d9e | 2008-04-26 11:03:32 -0400 | [diff] [blame] | 1730 | if (root->ref_cows) { |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1731 | if (!(data & BTRFS_BLOCK_GROUP_METADATA)) { |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1732 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, |
Chris Mason | 239b14b | 2008-03-24 15:02:07 -0400 | [diff] [blame] | 1733 | 2 * 1024 * 1024, |
Chris Mason | 593060d | 2008-03-25 16:50:33 -0400 | [diff] [blame] | 1734 | BTRFS_BLOCK_GROUP_METADATA | |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1735 | (info->metadata_alloc_profile & |
| 1736 | info->avail_metadata_alloc_bits)); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1737 | BUG_ON(ret); |
| 1738 | } |
| 1739 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 1740 | num_bytes + 2 * 1024 * 1024, data); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 1741 | BUG_ON(ret); |
| 1742 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1743 | |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1744 | WARN_ON(num_bytes < root->sectorsize); |
| 1745 | ret = find_free_extent(trans, root, num_bytes, empty_size, |
| 1746 | search_start, search_end, hint_byte, ins, |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 1747 | trans->alloc_exclude_start, |
| 1748 | trans->alloc_exclude_nr, data); |
Chris Mason | 3b95151 | 2008-04-17 11:29:12 -0400 | [diff] [blame] | 1749 | |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 1750 | if (ret == -ENOSPC && num_bytes > min_alloc_size) { |
| 1751 | num_bytes = num_bytes >> 1; |
| 1752 | num_bytes = max(num_bytes, min_alloc_size); |
| 1753 | goto again; |
| 1754 | } |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 1755 | BUG_ON(ret); |
Chris Mason | f2654de | 2007-06-26 12:20:46 -0400 | [diff] [blame] | 1756 | if (ret) |
| 1757 | return ret; |
| 1758 | |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 1759 | /* block accounting for super block */ |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1760 | super_used = btrfs_super_bytes_used(&info->super_copy); |
| 1761 | btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes); |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 1762 | |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 1763 | /* block accounting for root item */ |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1764 | root_used = btrfs_root_used(&root->root_item); |
| 1765 | btrfs_set_root_used(&root->root_item, root_used + num_bytes); |
Josef Bacik | 58176a9 | 2007-08-29 15:47:34 -0400 | [diff] [blame] | 1766 | |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 1767 | clear_extent_dirty(&root->fs_info->free_space_cache, |
| 1768 | ins->objectid, ins->objectid + ins->offset - 1, |
| 1769 | GFP_NOFS); |
| 1770 | |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 1771 | if (root == extent_root) { |
Chris Mason | 1a5bc16 | 2007-10-15 16:15:26 -0400 | [diff] [blame] | 1772 | set_extent_bits(&root->fs_info->extent_ins, ins->objectid, |
| 1773 | ins->objectid + ins->offset - 1, |
| 1774 | EXTENT_LOCKED, GFP_NOFS); |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 1775 | goto update_block; |
| 1776 | } |
| 1777 | |
| 1778 | WARN_ON(trans->alloc_exclude_nr); |
| 1779 | trans->alloc_exclude_start = ins->objectid; |
| 1780 | trans->alloc_exclude_nr = ins->offset; |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1781 | |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 1782 | memcpy(&keys[0], ins, sizeof(*ins)); |
| 1783 | keys[1].offset = hash_extent_ref(root_objectid, ref_generation, |
| 1784 | owner, owner_offset); |
| 1785 | keys[1].objectid = ins->objectid; |
| 1786 | keys[1].type = BTRFS_EXTENT_REF_KEY; |
| 1787 | sizes[0] = sizeof(*extent_item); |
| 1788 | sizes[1] = sizeof(*ref); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1789 | |
| 1790 | path = btrfs_alloc_path(); |
| 1791 | BUG_ON(!path); |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 1792 | |
| 1793 | ret = btrfs_insert_empty_items(trans, extent_root, path, keys, |
| 1794 | sizes, 2); |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 1795 | |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 1796 | BUG_ON(ret); |
Chris Mason | 47e4bb9 | 2008-02-01 14:51:59 -0500 | [diff] [blame] | 1797 | extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 1798 | struct btrfs_extent_item); |
| 1799 | btrfs_set_extent_refs(path->nodes[0], extent_item, 1); |
| 1800 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, |
| 1801 | struct btrfs_extent_ref); |
| 1802 | |
| 1803 | btrfs_set_ref_root(path->nodes[0], ref, root_objectid); |
| 1804 | btrfs_set_ref_generation(path->nodes[0], ref, ref_generation); |
| 1805 | btrfs_set_ref_objectid(path->nodes[0], ref, owner); |
| 1806 | btrfs_set_ref_offset(path->nodes[0], ref, owner_offset); |
| 1807 | |
| 1808 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 1809 | |
| 1810 | trans->alloc_exclude_start = 0; |
| 1811 | trans->alloc_exclude_nr = 0; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1812 | btrfs_free_path(path); |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 1813 | finish_current_insert(trans, extent_root); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 1814 | pending_ret = del_pending_extents(trans, extent_root); |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 1815 | |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 1816 | if (ret) { |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1817 | return ret; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 1818 | } |
| 1819 | if (pending_ret) { |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1820 | return pending_ret; |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 1821 | } |
Chris Mason | 26b8003 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 1822 | |
| 1823 | update_block: |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 1824 | ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0); |
Chris Mason | f594706 | 2008-02-04 10:10:13 -0500 | [diff] [blame] | 1825 | if (ret) { |
| 1826 | printk("update block group failed for %Lu %Lu\n", |
| 1827 | ins->objectid, ins->offset); |
| 1828 | BUG(); |
| 1829 | } |
Chris Mason | 037e639 | 2007-03-07 11:50:24 -0500 | [diff] [blame] | 1830 | return 0; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1831 | } |
| 1832 | |
| 1833 | /* |
| 1834 | * helper function to allocate a block for a given tree |
| 1835 | * returns the tree buffer or NULL. |
| 1836 | */ |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1837 | struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1838 | struct btrfs_root *root, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1839 | u32 blocksize, |
| 1840 | u64 root_objectid, u64 hint, |
| 1841 | u64 empty_size) |
| 1842 | { |
| 1843 | u64 ref_generation; |
| 1844 | |
| 1845 | if (root->ref_cows) |
| 1846 | ref_generation = trans->transid; |
| 1847 | else |
| 1848 | ref_generation = 0; |
| 1849 | |
| 1850 | |
| 1851 | return __btrfs_alloc_free_block(trans, root, blocksize, root_objectid, |
| 1852 | ref_generation, 0, 0, hint, empty_size); |
| 1853 | } |
| 1854 | |
| 1855 | /* |
| 1856 | * helper function to allocate a block for a given tree |
| 1857 | * returns the tree buffer or NULL. |
| 1858 | */ |
| 1859 | struct extent_buffer *__btrfs_alloc_free_block(struct btrfs_trans_handle *trans, |
| 1860 | struct btrfs_root *root, |
| 1861 | u32 blocksize, |
| 1862 | u64 root_objectid, |
| 1863 | u64 ref_generation, |
| 1864 | u64 first_objectid, |
| 1865 | int level, |
| 1866 | u64 hint, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1867 | u64 empty_size) |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1868 | { |
Chris Mason | e2fa722 | 2007-03-12 16:22:34 -0400 | [diff] [blame] | 1869 | struct btrfs_key ins; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1870 | int ret; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1871 | struct extent_buffer *buf; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1872 | |
Chris Mason | 98d20f6 | 2008-04-14 09:46:10 -0400 | [diff] [blame] | 1873 | ret = btrfs_alloc_extent(trans, root, blocksize, blocksize, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1874 | root_objectid, ref_generation, |
Chris Mason | f6dbff5 | 2007-12-13 11:13:32 -0500 | [diff] [blame] | 1875 | level, first_objectid, empty_size, hint, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1876 | (u64)-1, &ins, 0); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1877 | if (ret) { |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1878 | BUG_ON(ret > 0); |
| 1879 | return ERR_PTR(ret); |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1880 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1881 | buf = btrfs_find_create_tree_block(root, ins.objectid, blocksize); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1882 | if (!buf) { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1883 | btrfs_free_extent(trans, root, ins.objectid, blocksize, |
| 1884 | root->root_key.objectid, ref_generation, |
| 1885 | 0, 0, 0); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1886 | return ERR_PTR(-ENOMEM); |
| 1887 | } |
Chris Mason | 55c6907 | 2008-01-09 15:55:33 -0500 | [diff] [blame] | 1888 | btrfs_set_header_generation(buf, trans->transid); |
| 1889 | clean_tree_block(trans, root, buf); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1890 | btrfs_set_buffer_uptodate(buf); |
Chris Mason | 55c6907 | 2008-01-09 15:55:33 -0500 | [diff] [blame] | 1891 | |
| 1892 | if (PageDirty(buf->first_page)) { |
| 1893 | printk("page %lu dirty\n", buf->first_page->index); |
| 1894 | WARN_ON(1); |
| 1895 | } |
| 1896 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1897 | set_extent_dirty(&trans->transaction->dirty_pages, buf->start, |
| 1898 | buf->start + buf->len - 1, GFP_NOFS); |
Chris Mason | 9afbb0b | 2008-02-15 13:19:35 -0500 | [diff] [blame] | 1899 | if (!btrfs_test_opt(root, SSD)) |
| 1900 | btrfs_set_buffer_defrag(buf); |
Chris Mason | d3c2fdcf | 2007-09-17 10:58:06 -0400 | [diff] [blame] | 1901 | trans->blocks_used++; |
Chris Mason | fec577f | 2007-02-26 10:40:21 -0500 | [diff] [blame] | 1902 | return buf; |
| 1903 | } |
Chris Mason | a28ec19 | 2007-03-06 20:08:01 -0500 | [diff] [blame] | 1904 | |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 1905 | static int noinline drop_leaf_ref(struct btrfs_trans_handle *trans, |
| 1906 | struct btrfs_root *root, |
| 1907 | struct extent_buffer *leaf) |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 1908 | { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1909 | u64 leaf_owner; |
| 1910 | u64 leaf_generation; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1911 | struct btrfs_key key; |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 1912 | struct btrfs_file_extent_item *fi; |
| 1913 | int i; |
| 1914 | int nritems; |
| 1915 | int ret; |
| 1916 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1917 | BUG_ON(!btrfs_is_leaf(leaf)); |
| 1918 | nritems = btrfs_header_nritems(leaf); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1919 | leaf_owner = btrfs_header_owner(leaf); |
| 1920 | leaf_generation = btrfs_header_generation(leaf); |
| 1921 | |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 1922 | for (i = 0; i < nritems; i++) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1923 | u64 disk_bytenr; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1924 | |
| 1925 | btrfs_item_key_to_cpu(leaf, &key, i); |
| 1926 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 1927 | continue; |
| 1928 | fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1929 | if (btrfs_file_extent_type(leaf, fi) == |
| 1930 | BTRFS_FILE_EXTENT_INLINE) |
Chris Mason | 236454d | 2007-04-19 13:37:44 -0400 | [diff] [blame] | 1931 | continue; |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 1932 | /* |
| 1933 | * FIXME make sure to insert a trans record that |
| 1934 | * repeats the snapshot del on crash |
| 1935 | */ |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1936 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
| 1937 | if (disk_bytenr == 0) |
Chris Mason | 3a68637 | 2007-05-24 13:35:57 -0400 | [diff] [blame] | 1938 | continue; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1939 | ret = btrfs_free_extent(trans, root, disk_bytenr, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 1940 | btrfs_file_extent_disk_num_bytes(leaf, fi), |
| 1941 | leaf_owner, leaf_generation, |
| 1942 | key.objectid, key.offset, 0); |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 1943 | BUG_ON(ret); |
| 1944 | } |
| 1945 | return 0; |
| 1946 | } |
| 1947 | |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 1948 | static void noinline reada_walk_down(struct btrfs_root *root, |
Chris Mason | bea495e | 2008-01-24 16:13:14 -0500 | [diff] [blame] | 1949 | struct extent_buffer *node, |
| 1950 | int slot) |
Chris Mason | e011599 | 2007-06-19 16:23:05 -0400 | [diff] [blame] | 1951 | { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1952 | u64 bytenr; |
Chris Mason | bea495e | 2008-01-24 16:13:14 -0500 | [diff] [blame] | 1953 | u64 last = 0; |
| 1954 | u32 nritems; |
Chris Mason | e011599 | 2007-06-19 16:23:05 -0400 | [diff] [blame] | 1955 | u32 refs; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1956 | u32 blocksize; |
Chris Mason | bea495e | 2008-01-24 16:13:14 -0500 | [diff] [blame] | 1957 | int ret; |
| 1958 | int i; |
| 1959 | int level; |
| 1960 | int skipped = 0; |
Chris Mason | e011599 | 2007-06-19 16:23:05 -0400 | [diff] [blame] | 1961 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 1962 | nritems = btrfs_header_nritems(node); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1963 | level = btrfs_header_level(node); |
Chris Mason | bea495e | 2008-01-24 16:13:14 -0500 | [diff] [blame] | 1964 | if (level) |
| 1965 | return; |
| 1966 | |
| 1967 | for (i = slot; i < nritems && skipped < 32; i++) { |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1968 | bytenr = btrfs_node_blockptr(node, i); |
Chris Mason | bea495e | 2008-01-24 16:13:14 -0500 | [diff] [blame] | 1969 | if (last && ((bytenr > last && bytenr - last > 32 * 1024) || |
| 1970 | (last > bytenr && last - bytenr > 32 * 1024))) { |
| 1971 | skipped++; |
Chris Mason | e011599 | 2007-06-19 16:23:05 -0400 | [diff] [blame] | 1972 | continue; |
Chris Mason | bea495e | 2008-01-24 16:13:14 -0500 | [diff] [blame] | 1973 | } |
| 1974 | blocksize = btrfs_level_size(root, level - 1); |
| 1975 | if (i != slot) { |
| 1976 | ret = lookup_extent_ref(NULL, root, bytenr, |
| 1977 | blocksize, &refs); |
| 1978 | BUG_ON(ret); |
| 1979 | if (refs != 1) { |
| 1980 | skipped++; |
| 1981 | continue; |
| 1982 | } |
| 1983 | } |
Chris Mason | 409eb95 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 1984 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 1985 | ret = readahead_tree_block(root, bytenr, blocksize); |
Chris Mason | bea495e | 2008-01-24 16:13:14 -0500 | [diff] [blame] | 1986 | last = bytenr + blocksize; |
Chris Mason | 409eb95 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 1987 | cond_resched(); |
| 1988 | mutex_lock(&root->fs_info->fs_mutex); |
Chris Mason | e011599 | 2007-06-19 16:23:05 -0400 | [diff] [blame] | 1989 | if (ret) |
| 1990 | break; |
| 1991 | } |
| 1992 | } |
| 1993 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 1994 | /* |
| 1995 | * helper function for drop_snapshot, this walks down the tree dropping ref |
| 1996 | * counts as it goes. |
| 1997 | */ |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 1998 | static int noinline walk_down_tree(struct btrfs_trans_handle *trans, |
| 1999 | struct btrfs_root *root, |
| 2000 | struct btrfs_path *path, int *level) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2001 | { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2002 | u64 root_owner; |
| 2003 | u64 root_gen; |
| 2004 | u64 bytenr; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2005 | struct extent_buffer *next; |
| 2006 | struct extent_buffer *cur; |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2007 | struct extent_buffer *parent; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2008 | u32 blocksize; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2009 | int ret; |
| 2010 | u32 refs; |
| 2011 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 2012 | WARN_ON(*level < 0); |
| 2013 | WARN_ON(*level >= BTRFS_MAX_LEVEL); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2014 | ret = lookup_extent_ref(trans, root, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2015 | path->nodes[*level]->start, |
| 2016 | path->nodes[*level]->len, &refs); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2017 | BUG_ON(ret); |
| 2018 | if (refs > 1) |
| 2019 | goto out; |
Chris Mason | e011599 | 2007-06-19 16:23:05 -0400 | [diff] [blame] | 2020 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 2021 | /* |
| 2022 | * walk down to the last node level and free all the leaves |
| 2023 | */ |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 2024 | while(*level >= 0) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 2025 | WARN_ON(*level < 0); |
| 2026 | WARN_ON(*level >= BTRFS_MAX_LEVEL); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2027 | cur = path->nodes[*level]; |
Chris Mason | e011599 | 2007-06-19 16:23:05 -0400 | [diff] [blame] | 2028 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2029 | if (btrfs_header_level(cur) != *level) |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 2030 | WARN_ON(1); |
Chris Mason | e011599 | 2007-06-19 16:23:05 -0400 | [diff] [blame] | 2031 | |
Chris Mason | 7518a23 | 2007-03-12 12:01:18 -0400 | [diff] [blame] | 2032 | if (path->slots[*level] >= |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2033 | btrfs_header_nritems(cur)) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2034 | break; |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 2035 | if (*level == 0) { |
| 2036 | ret = drop_leaf_ref(trans, root, cur); |
| 2037 | BUG_ON(ret); |
| 2038 | break; |
| 2039 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2040 | bytenr = btrfs_node_blockptr(cur, path->slots[*level]); |
| 2041 | blocksize = btrfs_level_size(root, *level - 1); |
| 2042 | ret = lookup_extent_ref(trans, root, bytenr, blocksize, &refs); |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 2043 | BUG_ON(ret); |
| 2044 | if (refs != 1) { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2045 | parent = path->nodes[*level]; |
| 2046 | root_owner = btrfs_header_owner(parent); |
| 2047 | root_gen = btrfs_header_generation(parent); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2048 | path->slots[*level]++; |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2049 | ret = btrfs_free_extent(trans, root, bytenr, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2050 | blocksize, root_owner, |
| 2051 | root_gen, 0, 0, 1); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2052 | BUG_ON(ret); |
| 2053 | continue; |
| 2054 | } |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2055 | next = btrfs_find_tree_block(root, bytenr, blocksize); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2056 | if (!next || !btrfs_buffer_uptodate(next)) { |
| 2057 | free_extent_buffer(next); |
Chris Mason | bea495e | 2008-01-24 16:13:14 -0500 | [diff] [blame] | 2058 | reada_walk_down(root, cur, path->slots[*level]); |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 2059 | |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2060 | mutex_unlock(&root->fs_info->fs_mutex); |
| 2061 | next = read_tree_block(root, bytenr, blocksize); |
| 2062 | mutex_lock(&root->fs_info->fs_mutex); |
| 2063 | |
| 2064 | /* we've dropped the lock, double check */ |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2065 | ret = lookup_extent_ref(trans, root, bytenr, |
| 2066 | blocksize, &refs); |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 2067 | BUG_ON(ret); |
| 2068 | if (refs != 1) { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2069 | parent = path->nodes[*level]; |
| 2070 | root_owner = btrfs_header_owner(parent); |
| 2071 | root_gen = btrfs_header_generation(parent); |
| 2072 | |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 2073 | path->slots[*level]++; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2074 | free_extent_buffer(next); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2075 | ret = btrfs_free_extent(trans, root, bytenr, |
| 2076 | blocksize, |
| 2077 | root_owner, |
| 2078 | root_gen, 0, 0, 1); |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 2079 | BUG_ON(ret); |
| 2080 | continue; |
| 2081 | } |
Chris Mason | 0999df5 | 2008-04-01 13:48:14 -0400 | [diff] [blame] | 2082 | } else if (next) { |
| 2083 | btrfs_verify_block_csum(root, next); |
Chris Mason | e9d0b13 | 2007-08-10 14:06:19 -0400 | [diff] [blame] | 2084 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 2085 | WARN_ON(*level <= 0); |
Chris Mason | 83e15a2 | 2007-03-12 09:03:27 -0400 | [diff] [blame] | 2086 | if (path->nodes[*level-1]) |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2087 | free_extent_buffer(path->nodes[*level-1]); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2088 | path->nodes[*level-1] = next; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2089 | *level = btrfs_header_level(next); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2090 | path->slots[*level] = 0; |
| 2091 | } |
| 2092 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 2093 | WARN_ON(*level < 0); |
| 2094 | WARN_ON(*level >= BTRFS_MAX_LEVEL); |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2095 | |
| 2096 | if (path->nodes[*level] == root->node) { |
| 2097 | root_owner = root->root_key.objectid; |
| 2098 | parent = path->nodes[*level]; |
| 2099 | } else { |
| 2100 | parent = path->nodes[*level + 1]; |
| 2101 | root_owner = btrfs_header_owner(parent); |
| 2102 | } |
| 2103 | |
| 2104 | root_gen = btrfs_header_generation(parent); |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2105 | ret = btrfs_free_extent(trans, root, path->nodes[*level]->start, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2106 | path->nodes[*level]->len, |
| 2107 | root_owner, root_gen, 0, 0, 1); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2108 | free_extent_buffer(path->nodes[*level]); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2109 | path->nodes[*level] = NULL; |
| 2110 | *level += 1; |
| 2111 | BUG_ON(ret); |
| 2112 | return 0; |
| 2113 | } |
| 2114 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 2115 | /* |
| 2116 | * helper for dropping snapshots. This walks back up the tree in the path |
| 2117 | * to find the first node higher up where we haven't yet gone through |
| 2118 | * all the slots |
| 2119 | */ |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 2120 | static int noinline walk_up_tree(struct btrfs_trans_handle *trans, |
| 2121 | struct btrfs_root *root, |
| 2122 | struct btrfs_path *path, int *level) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2123 | { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2124 | u64 root_owner; |
| 2125 | u64 root_gen; |
| 2126 | struct btrfs_root_item *root_item = &root->root_item; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2127 | int i; |
| 2128 | int slot; |
| 2129 | int ret; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 2130 | |
Chris Mason | 234b63a | 2007-03-13 10:46:10 -0400 | [diff] [blame] | 2131 | for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) { |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2132 | slot = path->slots[i]; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2133 | if (slot < btrfs_header_nritems(path->nodes[i]) - 1) { |
| 2134 | struct extent_buffer *node; |
| 2135 | struct btrfs_disk_key disk_key; |
| 2136 | node = path->nodes[i]; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2137 | path->slots[i]++; |
| 2138 | *level = i; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 2139 | WARN_ON(*level == 0); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2140 | btrfs_node_key(node, &disk_key, path->slots[i]); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 2141 | memcpy(&root_item->drop_progress, |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2142 | &disk_key, sizeof(disk_key)); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 2143 | root_item->drop_level = i; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2144 | return 0; |
| 2145 | } else { |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2146 | if (path->nodes[*level] == root->node) { |
| 2147 | root_owner = root->root_key.objectid; |
| 2148 | root_gen = |
| 2149 | btrfs_header_generation(path->nodes[*level]); |
| 2150 | } else { |
| 2151 | struct extent_buffer *node; |
| 2152 | node = path->nodes[*level + 1]; |
| 2153 | root_owner = btrfs_header_owner(node); |
| 2154 | root_gen = btrfs_header_generation(node); |
| 2155 | } |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 2156 | ret = btrfs_free_extent(trans, root, |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2157 | path->nodes[*level]->start, |
Chris Mason | 7bb8631 | 2007-12-11 09:25:06 -0500 | [diff] [blame] | 2158 | path->nodes[*level]->len, |
| 2159 | root_owner, root_gen, 0, 0, 1); |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 2160 | BUG_ON(ret); |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2161 | free_extent_buffer(path->nodes[*level]); |
Chris Mason | 83e15a2 | 2007-03-12 09:03:27 -0400 | [diff] [blame] | 2162 | path->nodes[*level] = NULL; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2163 | *level = i + 1; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2164 | } |
| 2165 | } |
| 2166 | return 1; |
| 2167 | } |
| 2168 | |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 2169 | /* |
| 2170 | * drop the reference count on the tree rooted at 'snap'. This traverses |
| 2171 | * the tree freeing any blocks that have a ref count of zero after being |
| 2172 | * decremented. |
| 2173 | */ |
Chris Mason | e089f05 | 2007-03-16 16:20:31 -0400 | [diff] [blame] | 2174 | int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 2175 | *root) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2176 | { |
Chris Mason | 3768f36 | 2007-03-13 16:47:54 -0400 | [diff] [blame] | 2177 | int ret = 0; |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 2178 | int wret; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2179 | int level; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 2180 | struct btrfs_path *path; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2181 | int i; |
| 2182 | int orig_level; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 2183 | struct btrfs_root_item *root_item = &root->root_item; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2184 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 2185 | path = btrfs_alloc_path(); |
| 2186 | BUG_ON(!path); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2187 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2188 | level = btrfs_header_level(root->node); |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2189 | orig_level = level; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 2190 | if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) { |
| 2191 | path->nodes[level] = root->node; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 2192 | extent_buffer_get(root->node); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 2193 | path->slots[level] = 0; |
| 2194 | } else { |
| 2195 | struct btrfs_key key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2196 | struct btrfs_disk_key found_key; |
| 2197 | struct extent_buffer *node; |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 2198 | |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 2199 | btrfs_disk_key_to_cpu(&key, &root_item->drop_progress); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 2200 | level = root_item->drop_level; |
| 2201 | path->lowest_level = level; |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 2202 | wret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Chris Mason | 6702ed4 | 2007-08-07 16:15:09 -0400 | [diff] [blame] | 2203 | if (wret < 0) { |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 2204 | ret = wret; |
| 2205 | goto out; |
| 2206 | } |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2207 | node = path->nodes[level]; |
| 2208 | btrfs_node_key(node, &found_key, path->slots[level]); |
| 2209 | WARN_ON(memcmp(&found_key, &root_item->drop_progress, |
| 2210 | sizeof(found_key))); |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 2211 | } |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2212 | while(1) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 2213 | wret = walk_down_tree(trans, root, path, &level); |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 2214 | if (wret > 0) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2215 | break; |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 2216 | if (wret < 0) |
| 2217 | ret = wret; |
| 2218 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 2219 | wret = walk_up_tree(trans, root, path, &level); |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 2220 | if (wret > 0) |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2221 | break; |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 2222 | if (wret < 0) |
| 2223 | ret = wret; |
Chris Mason | 409eb95 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 2224 | ret = -EAGAIN; |
Chris Mason | 409eb95 | 2007-08-08 20:17:12 -0400 | [diff] [blame] | 2225 | break; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2226 | } |
Chris Mason | 83e15a2 | 2007-03-12 09:03:27 -0400 | [diff] [blame] | 2227 | for (i = 0; i <= orig_level; i++) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 2228 | if (path->nodes[i]) { |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2229 | free_extent_buffer(path->nodes[i]); |
Chris Mason | 0f82731 | 2007-10-15 16:18:56 -0400 | [diff] [blame] | 2230 | path->nodes[i] = NULL; |
Chris Mason | 83e15a2 | 2007-03-12 09:03:27 -0400 | [diff] [blame] | 2231 | } |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2232 | } |
Chris Mason | 9f3a742 | 2007-08-07 15:52:19 -0400 | [diff] [blame] | 2233 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 2234 | btrfs_free_path(path); |
Chris Mason | 9aca1d5 | 2007-03-13 11:09:37 -0400 | [diff] [blame] | 2235 | return ret; |
Chris Mason | 20524f0 | 2007-03-10 06:35:47 -0500 | [diff] [blame] | 2236 | } |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2237 | |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 2238 | int btrfs_free_block_groups(struct btrfs_fs_info *info) |
| 2239 | { |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 2240 | u64 start; |
| 2241 | u64 end; |
Yan | b97f920 | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 2242 | u64 ptr; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 2243 | int ret; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 2244 | while(1) { |
| 2245 | ret = find_first_extent_bit(&info->block_group_cache, 0, |
| 2246 | &start, &end, (unsigned int)-1); |
| 2247 | if (ret) |
| 2248 | break; |
Yan | b97f920 | 2007-11-01 11:28:41 -0400 | [diff] [blame] | 2249 | ret = get_state_private(&info->block_group_cache, start, &ptr); |
| 2250 | if (!ret) |
| 2251 | kfree((void *)(unsigned long)ptr); |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 2252 | clear_extent_bits(&info->block_group_cache, start, |
| 2253 | end, (unsigned int)-1, GFP_NOFS); |
| 2254 | } |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 2255 | while(1) { |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 2256 | ret = find_first_extent_bit(&info->free_space_cache, 0, |
| 2257 | &start, &end, EXTENT_DIRTY); |
| 2258 | if (ret) |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 2259 | break; |
Chris Mason | f510cfe | 2007-10-15 16:14:48 -0400 | [diff] [blame] | 2260 | clear_extent_dirty(&info->free_space_cache, start, |
| 2261 | end, GFP_NOFS); |
Chris Mason | e37c9e6 | 2007-05-09 20:13:14 -0400 | [diff] [blame] | 2262 | } |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 2263 | return 0; |
| 2264 | } |
| 2265 | |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame^] | 2266 | static unsigned long calc_ra(unsigned long start, unsigned long last, |
| 2267 | unsigned long nr) |
| 2268 | { |
| 2269 | return min(last, start + nr - 1); |
| 2270 | } |
| 2271 | |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 2272 | static int noinline relocate_inode_pages(struct inode *inode, u64 start, |
| 2273 | u64 len) |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2274 | { |
| 2275 | u64 page_start; |
| 2276 | u64 page_end; |
| 2277 | u64 delalloc_start; |
| 2278 | u64 existing_delalloc; |
| 2279 | unsigned long last_index; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2280 | unsigned long i; |
| 2281 | struct page *page; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2282 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 2283 | struct file_ra_state *ra; |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame^] | 2284 | unsigned long total_read = 0; |
| 2285 | unsigned long ra_pages; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 2286 | |
| 2287 | ra = kzalloc(sizeof(*ra), GFP_NOFS); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2288 | |
| 2289 | mutex_lock(&inode->i_mutex); |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 2290 | i = start >> PAGE_CACHE_SHIFT; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2291 | last_index = (start + len - 1) >> PAGE_CACHE_SHIFT; |
| 2292 | |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame^] | 2293 | ra_pages = BTRFS_I(inode)->root->fs_info->bdi.ra_pages; |
| 2294 | |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 2295 | file_ra_state_init(ra, inode->i_mapping); |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 2296 | kfree(ra); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2297 | |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 2298 | for (; i <= last_index; i++) { |
Chris Mason | 8e7bf94 | 2008-04-28 09:02:36 -0400 | [diff] [blame^] | 2299 | if (total_read % ra_pages == 0) { |
| 2300 | btrfs_force_ra(inode->i_mapping, ra, NULL, i, |
| 2301 | calc_ra(i, last_index, ra_pages)); |
| 2302 | } |
| 2303 | total_read++; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2304 | page = grab_cache_page(inode->i_mapping, i); |
| 2305 | if (!page) |
| 2306 | goto out_unlock; |
| 2307 | if (!PageUptodate(page)) { |
| 2308 | btrfs_readpage(NULL, page); |
| 2309 | lock_page(page); |
| 2310 | if (!PageUptodate(page)) { |
| 2311 | unlock_page(page); |
| 2312 | page_cache_release(page); |
| 2313 | goto out_unlock; |
| 2314 | } |
| 2315 | } |
| 2316 | page_start = (u64)page->index << PAGE_CACHE_SHIFT; |
| 2317 | page_end = page_start + PAGE_CACHE_SIZE - 1; |
| 2318 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2319 | lock_extent(io_tree, page_start, page_end, GFP_NOFS); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2320 | |
| 2321 | delalloc_start = page_start; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2322 | existing_delalloc = count_range_bits(io_tree, |
| 2323 | &delalloc_start, page_end, |
| 2324 | PAGE_CACHE_SIZE, EXTENT_DELALLOC); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2325 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2326 | set_extent_delalloc(io_tree, page_start, |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2327 | page_end, GFP_NOFS); |
| 2328 | |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2329 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2330 | set_page_dirty(page); |
| 2331 | unlock_page(page); |
| 2332 | page_cache_release(page); |
| 2333 | } |
| 2334 | |
| 2335 | out_unlock: |
| 2336 | mutex_unlock(&inode->i_mutex); |
| 2337 | return 0; |
| 2338 | } |
| 2339 | |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 2340 | /* |
| 2341 | * note, this releases the path |
| 2342 | */ |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 2343 | static int noinline relocate_one_reference(struct btrfs_root *extent_root, |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2344 | struct btrfs_path *path, |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 2345 | struct btrfs_key *extent_key) |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2346 | { |
| 2347 | struct inode *inode; |
| 2348 | struct btrfs_root *found_root; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 2349 | struct btrfs_key *root_location; |
| 2350 | struct btrfs_extent_ref *ref; |
| 2351 | u64 ref_root; |
| 2352 | u64 ref_gen; |
| 2353 | u64 ref_objectid; |
| 2354 | u64 ref_offset; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2355 | int ret; |
| 2356 | |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 2357 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0], |
| 2358 | struct btrfs_extent_ref); |
| 2359 | ref_root = btrfs_ref_root(path->nodes[0], ref); |
| 2360 | ref_gen = btrfs_ref_generation(path->nodes[0], ref); |
| 2361 | ref_objectid = btrfs_ref_objectid(path->nodes[0], ref); |
| 2362 | ref_offset = btrfs_ref_offset(path->nodes[0], ref); |
| 2363 | btrfs_release_path(extent_root, path); |
| 2364 | |
| 2365 | root_location = kmalloc(sizeof(*root_location), GFP_NOFS); |
| 2366 | root_location->objectid = ref_root; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2367 | if (ref_gen == 0) |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 2368 | root_location->offset = 0; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2369 | else |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 2370 | root_location->offset = (u64)-1; |
| 2371 | root_location->type = BTRFS_ROOT_ITEM_KEY; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2372 | |
| 2373 | found_root = btrfs_read_fs_root_no_name(extent_root->fs_info, |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 2374 | root_location); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2375 | BUG_ON(!found_root); |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 2376 | kfree(root_location); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2377 | |
| 2378 | if (ref_objectid >= BTRFS_FIRST_FREE_OBJECTID) { |
| 2379 | mutex_unlock(&extent_root->fs_info->fs_mutex); |
| 2380 | inode = btrfs_iget_locked(extent_root->fs_info->sb, |
| 2381 | ref_objectid, found_root); |
| 2382 | if (inode->i_state & I_NEW) { |
| 2383 | /* the inode and parent dir are two different roots */ |
| 2384 | BTRFS_I(inode)->root = found_root; |
| 2385 | BTRFS_I(inode)->location.objectid = ref_objectid; |
| 2386 | BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY; |
| 2387 | BTRFS_I(inode)->location.offset = 0; |
| 2388 | btrfs_read_locked_inode(inode); |
| 2389 | unlock_new_inode(inode); |
| 2390 | |
| 2391 | } |
| 2392 | /* this can happen if the reference is not against |
| 2393 | * the latest version of the tree root |
| 2394 | */ |
| 2395 | if (is_bad_inode(inode)) { |
| 2396 | mutex_lock(&extent_root->fs_info->fs_mutex); |
| 2397 | goto out; |
| 2398 | } |
| 2399 | relocate_inode_pages(inode, ref_offset, extent_key->offset); |
| 2400 | /* FIXME, data=ordered will help get rid of this */ |
| 2401 | filemap_fdatawrite(inode->i_mapping); |
| 2402 | iput(inode); |
| 2403 | mutex_lock(&extent_root->fs_info->fs_mutex); |
| 2404 | } else { |
| 2405 | struct btrfs_trans_handle *trans; |
| 2406 | struct btrfs_key found_key; |
| 2407 | struct extent_buffer *eb; |
| 2408 | int level; |
| 2409 | int i; |
| 2410 | |
| 2411 | trans = btrfs_start_transaction(found_root, 1); |
| 2412 | eb = read_tree_block(found_root, extent_key->objectid, |
| 2413 | extent_key->offset); |
| 2414 | level = btrfs_header_level(eb); |
| 2415 | |
| 2416 | if (level == 0) |
| 2417 | btrfs_item_key_to_cpu(eb, &found_key, 0); |
| 2418 | else |
| 2419 | btrfs_node_key_to_cpu(eb, &found_key, 0); |
| 2420 | |
| 2421 | free_extent_buffer(eb); |
| 2422 | |
| 2423 | path->lowest_level = level; |
Chris Mason | 8f662a7 | 2008-01-02 10:01:11 -0500 | [diff] [blame] | 2424 | path->reada = 2; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2425 | ret = btrfs_search_slot(trans, found_root, &found_key, path, |
| 2426 | 0, 1); |
| 2427 | path->lowest_level = 0; |
| 2428 | for (i = level; i < BTRFS_MAX_LEVEL; i++) { |
| 2429 | if (!path->nodes[i]) |
| 2430 | break; |
| 2431 | free_extent_buffer(path->nodes[i]); |
| 2432 | path->nodes[i] = NULL; |
| 2433 | } |
| 2434 | btrfs_release_path(found_root, path); |
| 2435 | btrfs_end_transaction(trans, found_root); |
| 2436 | } |
| 2437 | |
| 2438 | out: |
| 2439 | return 0; |
| 2440 | } |
| 2441 | |
Chris Mason | 98ed517 | 2008-01-03 10:01:48 -0500 | [diff] [blame] | 2442 | static int noinline relocate_one_extent(struct btrfs_root *extent_root, |
| 2443 | struct btrfs_path *path, |
| 2444 | struct btrfs_key *extent_key) |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2445 | { |
| 2446 | struct btrfs_key key; |
| 2447 | struct btrfs_key found_key; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2448 | struct extent_buffer *leaf; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2449 | u32 nritems; |
| 2450 | u32 item_size; |
| 2451 | int ret = 0; |
| 2452 | |
| 2453 | key.objectid = extent_key->objectid; |
| 2454 | key.type = BTRFS_EXTENT_REF_KEY; |
| 2455 | key.offset = 0; |
| 2456 | |
| 2457 | while(1) { |
| 2458 | ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); |
| 2459 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2460 | if (ret < 0) |
| 2461 | goto out; |
| 2462 | |
| 2463 | ret = 0; |
| 2464 | leaf = path->nodes[0]; |
| 2465 | nritems = btrfs_header_nritems(leaf); |
| 2466 | if (path->slots[0] == nritems) |
| 2467 | goto out; |
| 2468 | |
| 2469 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 2470 | if (found_key.objectid != extent_key->objectid) |
| 2471 | break; |
| 2472 | |
| 2473 | if (found_key.type != BTRFS_EXTENT_REF_KEY) |
| 2474 | break; |
| 2475 | |
| 2476 | key.offset = found_key.offset + 1; |
| 2477 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); |
| 2478 | |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 2479 | ret = relocate_one_reference(extent_root, path, extent_key); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2480 | if (ret) |
| 2481 | goto out; |
| 2482 | } |
| 2483 | ret = 0; |
| 2484 | out: |
| 2485 | btrfs_release_path(extent_root, path); |
| 2486 | return ret; |
| 2487 | } |
| 2488 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2489 | int btrfs_shrink_extent_tree(struct btrfs_root *root, u64 shrink_start) |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2490 | { |
| 2491 | struct btrfs_trans_handle *trans; |
| 2492 | struct btrfs_root *tree_root = root->fs_info->tree_root; |
| 2493 | struct btrfs_path *path; |
| 2494 | u64 cur_byte; |
| 2495 | u64 total_found; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2496 | u64 shrink_last_byte; |
| 2497 | struct btrfs_block_group_cache *shrink_block_group; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2498 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2499 | struct btrfs_key key; |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 2500 | struct btrfs_key found_key; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2501 | struct extent_buffer *leaf; |
| 2502 | u32 nritems; |
| 2503 | int ret; |
Chris Mason | 725c846 | 2008-01-04 16:47:16 -0500 | [diff] [blame] | 2504 | int progress = 0; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2505 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2506 | shrink_block_group = btrfs_lookup_block_group(root->fs_info, |
| 2507 | shrink_start); |
| 2508 | BUG_ON(!shrink_block_group); |
| 2509 | |
| 2510 | shrink_last_byte = shrink_start + shrink_block_group->key.offset; |
| 2511 | |
| 2512 | shrink_block_group->space_info->total_bytes -= |
| 2513 | shrink_block_group->key.offset; |
| 2514 | printk("shrink_extent_tree %Lu -> %Lu type %Lu\n", shrink_start, shrink_last_byte, shrink_block_group->flags); |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2515 | path = btrfs_alloc_path(); |
| 2516 | root = root->fs_info->extent_root; |
Chris Mason | 8f662a7 | 2008-01-02 10:01:11 -0500 | [diff] [blame] | 2517 | path->reada = 2; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2518 | |
| 2519 | again: |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2520 | trans = btrfs_start_transaction(root, 1); |
| 2521 | do_chunk_alloc(trans, root->fs_info->extent_root, |
| 2522 | btrfs_block_group_used(&shrink_block_group->item) + |
| 2523 | 2 * 1024 * 1024, shrink_block_group->flags); |
| 2524 | btrfs_end_transaction(trans, root); |
| 2525 | shrink_block_group->ro = 1; |
| 2526 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2527 | total_found = 0; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2528 | key.objectid = shrink_start; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2529 | key.offset = 0; |
| 2530 | key.type = 0; |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 2531 | cur_byte = key.objectid; |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 2532 | |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 2533 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 2534 | if (ret < 0) |
| 2535 | goto out; |
| 2536 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2537 | ret = btrfs_previous_item(root, path, 0, BTRFS_EXTENT_ITEM_KEY); |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 2538 | if (ret < 0) |
| 2539 | goto out; |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2540 | |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 2541 | if (ret == 0) { |
| 2542 | leaf = path->nodes[0]; |
| 2543 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2544 | if (found_key.objectid + found_key.offset > shrink_start && |
| 2545 | found_key.objectid < shrink_last_byte) { |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 2546 | cur_byte = found_key.objectid; |
| 2547 | key.objectid = cur_byte; |
| 2548 | } |
| 2549 | } |
| 2550 | btrfs_release_path(root, path); |
| 2551 | |
| 2552 | while(1) { |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2553 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 2554 | if (ret < 0) |
| 2555 | goto out; |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 2556 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2557 | leaf = path->nodes[0]; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2558 | nritems = btrfs_header_nritems(leaf); |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 2559 | next: |
| 2560 | if (path->slots[0] >= nritems) { |
| 2561 | ret = btrfs_next_leaf(root, path); |
| 2562 | if (ret < 0) |
| 2563 | goto out; |
| 2564 | if (ret == 1) { |
| 2565 | ret = 0; |
| 2566 | break; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2567 | } |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 2568 | leaf = path->nodes[0]; |
| 2569 | nritems = btrfs_header_nritems(leaf); |
| 2570 | } |
| 2571 | |
| 2572 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | 725c846 | 2008-01-04 16:47:16 -0500 | [diff] [blame] | 2573 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2574 | if (found_key.objectid >= shrink_last_byte) |
| 2575 | break; |
| 2576 | |
Chris Mason | 725c846 | 2008-01-04 16:47:16 -0500 | [diff] [blame] | 2577 | if (progress && need_resched()) { |
| 2578 | memcpy(&key, &found_key, sizeof(key)); |
| 2579 | mutex_unlock(&root->fs_info->fs_mutex); |
| 2580 | cond_resched(); |
| 2581 | mutex_lock(&root->fs_info->fs_mutex); |
| 2582 | btrfs_release_path(root, path); |
| 2583 | btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 2584 | progress = 0; |
| 2585 | goto next; |
| 2586 | } |
| 2587 | progress = 1; |
| 2588 | |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 2589 | if (btrfs_key_type(&found_key) != BTRFS_EXTENT_ITEM_KEY || |
| 2590 | found_key.objectid + found_key.offset <= cur_byte) { |
| 2591 | path->slots[0]++; |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2592 | goto next; |
| 2593 | } |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 2594 | |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2595 | total_found++; |
| 2596 | cur_byte = found_key.objectid + found_key.offset; |
| 2597 | key.objectid = cur_byte; |
| 2598 | btrfs_release_path(root, path); |
| 2599 | ret = relocate_one_extent(root, path, &found_key); |
| 2600 | } |
| 2601 | |
| 2602 | btrfs_release_path(root, path); |
| 2603 | |
| 2604 | if (total_found > 0) { |
| 2605 | trans = btrfs_start_transaction(tree_root, 1); |
| 2606 | btrfs_commit_transaction(trans, tree_root); |
| 2607 | |
| 2608 | mutex_unlock(&root->fs_info->fs_mutex); |
| 2609 | btrfs_clean_old_snapshots(tree_root); |
| 2610 | mutex_lock(&root->fs_info->fs_mutex); |
| 2611 | |
| 2612 | trans = btrfs_start_transaction(tree_root, 1); |
| 2613 | btrfs_commit_transaction(trans, tree_root); |
| 2614 | goto again; |
| 2615 | } |
| 2616 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2617 | /* |
| 2618 | * we've freed all the extents, now remove the block |
| 2619 | * group item from the tree |
| 2620 | */ |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2621 | trans = btrfs_start_transaction(root, 1); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2622 | memcpy(&key, &shrink_block_group->key, sizeof(key)); |
Chris Mason | 4313b39 | 2008-01-03 09:08:48 -0500 | [diff] [blame] | 2623 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2624 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 2625 | if (ret > 0) |
| 2626 | ret = -EIO; |
| 2627 | if (ret < 0) |
| 2628 | goto out; |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 2629 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2630 | leaf = path->nodes[0]; |
| 2631 | nritems = btrfs_header_nritems(leaf); |
| 2632 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 2633 | kfree(shrink_block_group); |
Chris Mason | 1372f8e | 2008-01-04 09:34:54 -0500 | [diff] [blame] | 2634 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2635 | clear_extent_bits(&info->block_group_cache, found_key.objectid, |
| 2636 | found_key.objectid + found_key.offset - 1, |
| 2637 | (unsigned int)-1, GFP_NOFS); |
Yan | 73e48b2 | 2008-01-03 14:14:39 -0500 | [diff] [blame] | 2638 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2639 | btrfs_del_item(trans, root, path); |
| 2640 | clear_extent_dirty(&info->free_space_cache, |
| 2641 | shrink_start, shrink_last_byte - 1, |
Chris Mason | edbd8d4 | 2007-12-21 16:27:24 -0500 | [diff] [blame] | 2642 | GFP_NOFS); |
| 2643 | btrfs_commit_transaction(trans, root); |
| 2644 | out: |
| 2645 | btrfs_free_path(path); |
| 2646 | return ret; |
| 2647 | } |
| 2648 | |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2649 | int find_first_block_group(struct btrfs_root *root, struct btrfs_path *path, |
| 2650 | struct btrfs_key *key) |
| 2651 | { |
| 2652 | int ret; |
| 2653 | struct btrfs_key found_key; |
| 2654 | struct extent_buffer *leaf; |
| 2655 | int slot; |
| 2656 | |
| 2657 | ret = btrfs_search_slot(NULL, root, key, path, 0, 0); |
| 2658 | if (ret < 0) |
| 2659 | return ret; |
| 2660 | while(1) { |
| 2661 | slot = path->slots[0]; |
| 2662 | leaf = path->nodes[0]; |
| 2663 | if (slot >= btrfs_header_nritems(leaf)) { |
| 2664 | ret = btrfs_next_leaf(root, path); |
| 2665 | if (ret == 0) |
| 2666 | continue; |
| 2667 | if (ret < 0) |
| 2668 | goto error; |
| 2669 | break; |
| 2670 | } |
| 2671 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
| 2672 | |
| 2673 | if (found_key.objectid >= key->objectid && |
| 2674 | found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) |
| 2675 | return 0; |
| 2676 | path->slots[0]++; |
| 2677 | } |
| 2678 | ret = -ENOENT; |
| 2679 | error: |
| 2680 | return ret; |
| 2681 | } |
| 2682 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2683 | int btrfs_read_block_groups(struct btrfs_root *root) |
| 2684 | { |
| 2685 | struct btrfs_path *path; |
| 2686 | int ret; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 2687 | int bit; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2688 | struct btrfs_block_group_cache *cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 2689 | struct btrfs_fs_info *info = root->fs_info; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2690 | struct btrfs_space_info *space_info; |
Chris Mason | d1310b2 | 2008-01-24 16:13:08 -0500 | [diff] [blame] | 2691 | struct extent_io_tree *block_group_cache; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2692 | struct btrfs_key key; |
| 2693 | struct btrfs_key found_key; |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2694 | struct extent_buffer *leaf; |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 2695 | |
| 2696 | block_group_cache = &info->block_group_cache; |
Chris Mason | be74417 | 2007-05-06 10:15:01 -0400 | [diff] [blame] | 2697 | root = info->extent_root; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2698 | key.objectid = 0; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2699 | key.offset = 0; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2700 | btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2701 | path = btrfs_alloc_path(); |
| 2702 | if (!path) |
| 2703 | return -ENOMEM; |
| 2704 | |
| 2705 | while(1) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2706 | ret = find_first_block_group(root, path, &key); |
| 2707 | if (ret > 0) { |
| 2708 | ret = 0; |
| 2709 | goto error; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2710 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2711 | if (ret != 0) |
| 2712 | goto error; |
| 2713 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2714 | leaf = path->nodes[0]; |
| 2715 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2716 | cache = kzalloc(sizeof(*cache), GFP_NOFS); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2717 | if (!cache) { |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2718 | ret = -ENOMEM; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2719 | break; |
| 2720 | } |
Chris Mason | 3e1ad54 | 2007-05-07 20:03:49 -0400 | [diff] [blame] | 2721 | |
Chris Mason | 5f39d39 | 2007-10-15 16:14:19 -0400 | [diff] [blame] | 2722 | read_extent_buffer(leaf, &cache->item, |
| 2723 | btrfs_item_ptr_offset(leaf, path->slots[0]), |
| 2724 | sizeof(cache->item)); |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2725 | memcpy(&cache->key, &found_key, sizeof(found_key)); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2726 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2727 | key.objectid = found_key.objectid + found_key.offset; |
| 2728 | btrfs_release_path(root, path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2729 | cache->flags = btrfs_block_group_flags(&cache->item); |
| 2730 | bit = 0; |
| 2731 | if (cache->flags & BTRFS_BLOCK_GROUP_DATA) { |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 2732 | bit = BLOCK_GROUP_DATA; |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2733 | } else if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) { |
| 2734 | bit = BLOCK_GROUP_SYSTEM; |
| 2735 | } else if (cache->flags & BTRFS_BLOCK_GROUP_METADATA) { |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 2736 | bit = BLOCK_GROUP_METADATA; |
Chris Mason | 31f3c99 | 2007-04-30 15:25:45 -0400 | [diff] [blame] | 2737 | } |
Chris Mason | 8790d50 | 2008-04-03 16:29:03 -0400 | [diff] [blame] | 2738 | set_avail_alloc_bits(info, cache->flags); |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 2739 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2740 | ret = update_space_info(info, cache->flags, found_key.offset, |
| 2741 | btrfs_block_group_used(&cache->item), |
| 2742 | &space_info); |
| 2743 | BUG_ON(ret); |
| 2744 | cache->space_info = space_info; |
| 2745 | |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 2746 | /* use EXTENT_LOCKED to prevent merging */ |
| 2747 | set_extent_bits(block_group_cache, found_key.objectid, |
| 2748 | found_key.objectid + found_key.offset - 1, |
| 2749 | bit | EXTENT_LOCKED, GFP_NOFS); |
| 2750 | set_state_private(block_group_cache, found_key.objectid, |
Jens Axboe | ae2f541 | 2007-10-19 09:22:59 -0400 | [diff] [blame] | 2751 | (unsigned long)cache); |
Chris Mason | 96b5179 | 2007-10-15 16:15:19 -0400 | [diff] [blame] | 2752 | |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2753 | if (key.objectid >= |
Chris Mason | db94535 | 2007-10-15 16:15:53 -0400 | [diff] [blame] | 2754 | btrfs_super_total_bytes(&info->super_copy)) |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2755 | break; |
| 2756 | } |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2757 | ret = 0; |
| 2758 | error: |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2759 | btrfs_free_path(path); |
Chris Mason | 0b86a83 | 2008-03-24 15:01:56 -0400 | [diff] [blame] | 2760 | return ret; |
Chris Mason | 9078a3e | 2007-04-26 16:46:15 -0400 | [diff] [blame] | 2761 | } |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2762 | |
| 2763 | int btrfs_make_block_group(struct btrfs_trans_handle *trans, |
| 2764 | struct btrfs_root *root, u64 bytes_used, |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 2765 | u64 type, u64 chunk_objectid, u64 chunk_offset, |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2766 | u64 size) |
| 2767 | { |
| 2768 | int ret; |
| 2769 | int bit = 0; |
| 2770 | struct btrfs_root *extent_root; |
| 2771 | struct btrfs_block_group_cache *cache; |
| 2772 | struct extent_io_tree *block_group_cache; |
| 2773 | |
| 2774 | extent_root = root->fs_info->extent_root; |
| 2775 | block_group_cache = &root->fs_info->block_group_cache; |
| 2776 | |
Chris Mason | 8f18cf1 | 2008-04-25 16:53:30 -0400 | [diff] [blame] | 2777 | cache = kzalloc(sizeof(*cache), GFP_NOFS); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2778 | BUG_ON(!cache); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 2779 | cache->key.objectid = chunk_offset; |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2780 | cache->key.offset = size; |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 2781 | |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2782 | btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY); |
| 2783 | memset(&cache->item, 0, sizeof(cache->item)); |
| 2784 | btrfs_set_block_group_used(&cache->item, bytes_used); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2785 | btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid); |
| 2786 | cache->flags = type; |
| 2787 | btrfs_set_block_group_flags(&cache->item, type); |
| 2788 | |
| 2789 | ret = update_space_info(root->fs_info, cache->flags, size, bytes_used, |
| 2790 | &cache->space_info); |
| 2791 | BUG_ON(ret); |
| 2792 | |
Chris Mason | d18a2c4 | 2008-04-04 15:40:00 -0400 | [diff] [blame] | 2793 | bit = block_group_state_bits(type); |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 2794 | set_extent_bits(block_group_cache, chunk_offset, |
| 2795 | chunk_offset + size - 1, |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2796 | bit | EXTENT_LOCKED, GFP_NOFS); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2797 | |
Chris Mason | e17cade | 2008-04-15 15:41:47 -0400 | [diff] [blame] | 2798 | set_state_private(block_group_cache, chunk_offset, |
| 2799 | (unsigned long)cache); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2800 | ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item, |
| 2801 | sizeof(cache->item)); |
| 2802 | BUG_ON(ret); |
| 2803 | |
| 2804 | finish_current_insert(trans, extent_root); |
| 2805 | ret = del_pending_extents(trans, extent_root); |
| 2806 | BUG_ON(ret); |
Chris Mason | d18a2c4 | 2008-04-04 15:40:00 -0400 | [diff] [blame] | 2807 | set_avail_alloc_bits(extent_root->fs_info, type); |
Chris Mason | 6324fbf | 2008-03-24 15:01:59 -0400 | [diff] [blame] | 2808 | return 0; |
| 2809 | } |