blob: fe4fe709c312fc60732cf93a0afac9f1961a5b98 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
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 Brownec6b9102007-07-11 10:00:37 -040018#include <linux/sched.h>
Chris Masonedbd8d42007-12-21 16:27:24 -050019#include <linux/pagemap.h>
Chris Mason74493f72007-12-11 09:25:06 -050020#include "hash.h"
Miguela5eb62e2008-04-11 15:45:51 -040021#include "crc32c.h"
Chris Masonfec577f2007-02-26 10:40:21 -050022#include "ctree.h"
23#include "disk-io.h"
24#include "print-tree.h"
Chris Masone089f052007-03-16 16:20:31 -040025#include "transaction.h"
Chris Mason0b86a832008-03-24 15:01:56 -040026#include "volumes.h"
Chris Masonfec577f2007-02-26 10:40:21 -050027
Chris Mason0b86a832008-03-24 15:01:56 -040028#define BLOCK_GROUP_DATA EXTENT_WRITEBACK
Chris Mason96b51792007-10-15 16:15:19 -040029#define BLOCK_GROUP_METADATA EXTENT_UPTODATE
Chris Mason0b86a832008-03-24 15:01:56 -040030#define BLOCK_GROUP_SYSTEM EXTENT_NEW
31
Chris Mason96b51792007-10-15 16:15:19 -040032#define BLOCK_GROUP_DIRTY EXTENT_DIRTY
33
Chris Masone089f052007-03-16 16:20:31 -040034static int finish_current_insert(struct btrfs_trans_handle *trans, struct
35 btrfs_root *extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -040036static int del_pending_extents(struct btrfs_trans_handle *trans, struct
37 btrfs_root *extent_root);
Yand548ee52008-01-03 13:56:30 -050038
Chris Masonfec577f2007-02-26 10:40:21 -050039
Chris Masone37c9e62007-05-09 20:13:14 -040040static 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 Mason5f39d392007-10-15 16:14:19 -040046 struct extent_buffer *leaf;
Chris Masond1310b22008-01-24 16:13:08 -050047 struct extent_io_tree *free_space_cache;
Chris Masone37c9e62007-05-09 20:13:14 -040048 int slot;
Chris Masone37c9e62007-05-09 20:13:14 -040049 u64 last = 0;
50 u64 hole_size;
Yan7d7d6062007-09-14 16:15:28 -040051 u64 first_free;
Chris Masone37c9e62007-05-09 20:13:14 -040052 int found = 0;
53
Chris Mason00f5c792007-11-30 10:09:33 -050054 if (!block_group)
55 return 0;
56
Chris Masone37c9e62007-05-09 20:13:14 -040057 root = root->fs_info->extent_root;
Chris Masonf510cfe2007-10-15 16:14:48 -040058 free_space_cache = &root->fs_info->free_space_cache;
Chris Masone37c9e62007-05-09 20:13:14 -040059
60 if (block_group->cached)
61 return 0;
Chris Masonf510cfe2007-10-15 16:14:48 -040062
Chris Masone37c9e62007-05-09 20:13:14 -040063 path = btrfs_alloc_path();
64 if (!path)
65 return -ENOMEM;
Yan7d7d6062007-09-14 16:15:28 -040066
Chris Mason2cc58cf2007-08-27 16:49:44 -040067 path->reada = 2;
Yan7d7d6062007-09-14 16:15:28 -040068 first_free = block_group->key.objectid;
Chris Masone37c9e62007-05-09 20:13:14 -040069 key.objectid = block_group->key.objectid;
Chris Masone37c9e62007-05-09 20:13:14 -040070 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 Mason0b86a832008-03-24 15:01:56 -040075 ret = btrfs_previous_item(root, path, 0, BTRFS_EXTENT_ITEM_KEY);
Yand548ee52008-01-03 13:56:30 -050076 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 Masone37c9e62007-05-09 20:13:14 -040084 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -040085 leaf = path->nodes[0];
Chris Masone37c9e62007-05-09 20:13:14 -040086 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -040087 if (slot >= btrfs_header_nritems(leaf)) {
Chris Masone37c9e62007-05-09 20:13:14 -040088 ret = btrfs_next_leaf(root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -040089 if (ret < 0)
90 goto err;
Chris Masonde428b62007-05-18 13:28:27 -040091 if (ret == 0) {
Chris Masone37c9e62007-05-09 20:13:14 -040092 continue;
Chris Masonde428b62007-05-18 13:28:27 -040093 } else {
Chris Masone37c9e62007-05-09 20:13:14 -040094 break;
95 }
96 }
Chris Mason5f39d392007-10-15 16:14:19 -040097 btrfs_item_key_to_cpu(leaf, &key, slot);
Yan7d7d6062007-09-14 16:15:28 -040098 if (key.objectid < block_group->key.objectid) {
Yan7d7d6062007-09-14 16:15:28 -040099 goto next;
100 }
Chris Masone37c9e62007-05-09 20:13:14 -0400101 if (key.objectid >= block_group->key.objectid +
102 block_group->key.offset) {
Yan7d7d6062007-09-14 16:15:28 -0400103 break;
104 }
105
106 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
107 if (!found) {
108 last = first_free;
109 found = 1;
Chris Masone37c9e62007-05-09 20:13:14 -0400110 }
Chris Masonf510cfe2007-10-15 16:14:48 -0400111 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 Masone37c9e62007-05-09 20:13:14 -0400116 }
Yan7d7d6062007-09-14 16:15:28 -0400117 last = key.objectid + key.offset;
Chris Masone37c9e62007-05-09 20:13:14 -0400118 }
Yan7d7d6062007-09-14 16:15:28 -0400119next:
Chris Masone37c9e62007-05-09 20:13:14 -0400120 path->slots[0]++;
121 }
122
Yan7d7d6062007-09-14 16:15:28 -0400123 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 Masonf510cfe2007-10-15 16:14:48 -0400129 set_extent_dirty(free_space_cache, last,
130 last + hole_size - 1, GFP_NOFS);
Yan7d7d6062007-09-14 16:15:28 -0400131 }
Chris Masone37c9e62007-05-09 20:13:14 -0400132 block_group->cached = 1;
Chris Mason54aa1f42007-06-22 14:16:25 -0400133err:
Chris Masone37c9e62007-05-09 20:13:14 -0400134 btrfs_free_path(path);
135 return 0;
136}
137
Chris Mason5276aed2007-06-11 21:33:38 -0400138struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
139 btrfs_fs_info *info,
Chris Masondb945352007-10-15 16:15:53 -0400140 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400141{
Chris Masond1310b22008-01-24 16:13:08 -0500142 struct extent_io_tree *block_group_cache;
Chris Mason96b51792007-10-15 16:15:19 -0400143 struct btrfs_block_group_cache *block_group = NULL;
144 u64 ptr;
145 u64 start;
146 u64 end;
Chris Masonbe744172007-05-06 10:15:01 -0400147 int ret;
148
Chris Mason96b51792007-10-15 16:15:19 -0400149 block_group_cache = &info->block_group_cache;
150 ret = find_first_extent_bit(block_group_cache,
Chris Masondb945352007-10-15 16:15:53 -0400151 bytenr, &start, &end,
Chris Mason0b86a832008-03-24 15:01:56 -0400152 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA |
153 BLOCK_GROUP_SYSTEM);
Chris Masonbe744172007-05-06 10:15:01 -0400154 if (ret) {
Chris Mason96b51792007-10-15 16:15:19 -0400155 return NULL;
Chris Masonbe744172007-05-06 10:15:01 -0400156 }
Chris Mason96b51792007-10-15 16:15:19 -0400157 ret = get_state_private(block_group_cache, start, &ptr);
158 if (ret)
159 return NULL;
160
Jens Axboeae2f5412007-10-19 09:22:59 -0400161 block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
Yan5cf66422007-11-16 14:57:09 -0500162 if (block_group->key.objectid <= bytenr && bytenr <
Chris Mason96b51792007-10-15 16:15:19 -0400163 block_group->key.objectid + block_group->key.offset)
164 return block_group;
Chris Masonbe744172007-05-06 10:15:01 -0400165 return NULL;
166}
Chris Mason0b86a832008-03-24 15:01:56 -0400167
168static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
169{
Chris Mason593060d2008-03-25 16:50:33 -0400170 return (cache->flags & bits) == bits;
Chris Mason0b86a832008-03-24 15:01:56 -0400171}
172
173static int noinline find_search_start(struct btrfs_root *root,
Chris Mason98ed5172008-01-03 10:01:48 -0500174 struct btrfs_block_group_cache **cache_ret,
Chris Mason0b86a832008-03-24 15:01:56 -0400175 u64 *start_ret, int num, int data)
Chris Masone37c9e62007-05-09 20:13:14 -0400176{
Chris Masone37c9e62007-05-09 20:13:14 -0400177 int ret;
178 struct btrfs_block_group_cache *cache = *cache_ret;
Chris Masond7fc6402008-02-18 12:12:38 -0500179 struct extent_io_tree *free_space_cache;
Chris Mason7d1660d2008-03-24 15:02:03 -0400180 struct extent_state *state;
Chris Masone19caa52007-10-15 16:17:44 -0400181 u64 last;
Chris Masonf510cfe2007-10-15 16:14:48 -0400182 u64 start = 0;
Chris Mason257d0ce2007-11-07 21:08:16 -0500183 u64 cache_miss = 0;
Chris Masonc31f8832008-01-08 15:46:31 -0500184 u64 total_fs_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -0400185 u64 search_start = *start_ret;
Chris Masonf84a8b32007-11-06 10:26:29 -0500186 int wrapped = 0;
Chris Masone37c9e62007-05-09 20:13:14 -0400187
Chris Mason0b86a832008-03-24 15:01:56 -0400188 if (!cache)
Chris Mason1a2b2ac2007-12-04 13:18:24 -0500189 goto out;
Chris Mason8f18cf12008-04-25 16:53:30 -0400190
Chris Masonc31f8832008-01-08 15:46:31 -0500191 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
Chris Masond7fc6402008-02-18 12:12:38 -0500192 free_space_cache = &root->fs_info->free_space_cache;
193
Chris Masone37c9e62007-05-09 20:13:14 -0400194again:
Chris Mason54aa1f42007-06-22 14:16:25 -0400195 ret = cache_block_group(root, cache);
196 if (ret)
197 goto out;
Chris Masonf84a8b32007-11-06 10:26:29 -0500198
Chris Masone19caa52007-10-15 16:17:44 -0400199 last = max(search_start, cache->key.objectid);
Chris Mason8f18cf12008-04-25 16:53:30 -0400200 if (!block_group_bits(cache, data) || cache->ro) {
Chris Mason0b86a832008-03-24 15:01:56 -0400201 goto new_group;
202 }
Chris Masone19caa52007-10-15 16:17:44 -0400203
Chris Mason7d1660d2008-03-24 15:02:03 -0400204 spin_lock_irq(&free_space_cache->lock);
205 state = find_first_extent_bit_state(free_space_cache, last, EXTENT_DIRTY);
Chris Masone37c9e62007-05-09 20:13:14 -0400206 while(1) {
Chris Mason7d1660d2008-03-24 15:02:03 -0400207 if (!state) {
Chris Mason257d0ce2007-11-07 21:08:16 -0500208 if (!cache_miss)
209 cache_miss = last;
Chris Mason7d1660d2008-03-24 15:02:03 -0400210 spin_unlock_irq(&free_space_cache->lock);
Chris Masone19caa52007-10-15 16:17:44 -0400211 goto new_group;
212 }
Chris Masonf510cfe2007-10-15 16:14:48 -0400213
Chris Mason7d1660d2008-03-24 15:02:03 -0400214 start = max(last, state->start);
215 last = state->end + 1;
Chris Mason257d0ce2007-11-07 21:08:16 -0500216 if (last - start < num) {
217 if (last == cache->key.objectid + cache->key.offset)
218 cache_miss = start;
Chris Mason7d1660d2008-03-24 15:02:03 -0400219 do {
220 state = extent_state_next(state);
221 } while(state && !(state->state & EXTENT_DIRTY));
Chris Masonf510cfe2007-10-15 16:14:48 -0400222 continue;
Chris Mason257d0ce2007-11-07 21:08:16 -0500223 }
Chris Mason7d1660d2008-03-24 15:02:03 -0400224 spin_unlock_irq(&free_space_cache->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -0400225 if (cache->ro)
226 goto new_group;
Chris Mason0b86a832008-03-24 15:01:56 -0400227 if (start + num > cache->key.objectid + cache->key.offset)
Chris Masone37c9e62007-05-09 20:13:14 -0400228 goto new_group;
Chris Masonc31f8832008-01-08 15:46:31 -0500229 if (start + num > total_fs_bytes)
230 goto new_group;
Chris Mason8790d502008-04-03 16:29:03 -0400231 if (!block_group_bits(cache, data)) {
Chris Mason611f0e02008-04-03 16:29:03 -0400232 printk("block group bits don't match %Lu %d\n", cache->flags, data);
Chris Mason8790d502008-04-03 16:29:03 -0400233 }
Chris Mason0b86a832008-03-24 15:01:56 -0400234 *start_ret = start;
235 return 0;
Chris Mason8790d502008-04-03 16:29:03 -0400236 }
237out:
Chris Mason1a2b2ac2007-12-04 13:18:24 -0500238 cache = btrfs_lookup_block_group(root->fs_info, search_start);
239 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -0400240 printk("Unable to find block group for %Lu\n", search_start);
Chris Mason1a2b2ac2007-12-04 13:18:24 -0500241 WARN_ON(1);
Chris Mason1a2b2ac2007-12-04 13:18:24 -0500242 }
Chris Mason0b86a832008-03-24 15:01:56 -0400243 return -ENOSPC;
Chris Masone37c9e62007-05-09 20:13:14 -0400244
245new_group:
Chris Masone19caa52007-10-15 16:17:44 -0400246 last = cache->key.objectid + cache->key.offset;
Chris Masonf84a8b32007-11-06 10:26:29 -0500247wrapped:
Chris Masone19caa52007-10-15 16:17:44 -0400248 cache = btrfs_lookup_block_group(root->fs_info, last);
Chris Masonc31f8832008-01-08 15:46:31 -0500249 if (!cache || cache->key.objectid >= total_fs_bytes) {
Chris Mason0e4de582007-11-26 10:55:49 -0500250no_cache:
Chris Masonf84a8b32007-11-06 10:26:29 -0500251 if (!wrapped) {
252 wrapped = 1;
253 last = search_start;
Chris Masonf84a8b32007-11-06 10:26:29 -0500254 goto wrapped;
255 }
Chris Mason1a2b2ac2007-12-04 13:18:24 -0500256 goto out;
Chris Masone37c9e62007-05-09 20:13:14 -0400257 }
Chris Mason257d0ce2007-11-07 21:08:16 -0500258 if (cache_miss && !cache->cached) {
259 cache_block_group(root, cache);
260 last = cache_miss;
Chris Mason257d0ce2007-11-07 21:08:16 -0500261 cache = btrfs_lookup_block_group(root->fs_info, last);
262 }
Chris Mason1a2b2ac2007-12-04 13:18:24 -0500263 cache = btrfs_find_block_group(root, cache, last, data, 0);
Chris Mason0e4de582007-11-26 10:55:49 -0500264 if (!cache)
265 goto no_cache;
Chris Masone37c9e62007-05-09 20:13:14 -0400266 *cache_ret = cache;
Chris Mason257d0ce2007-11-07 21:08:16 -0500267 cache_miss = 0;
Chris Masone37c9e62007-05-09 20:13:14 -0400268 goto again;
269}
270
Chris Mason84f54cf2007-06-12 07:43:08 -0400271static u64 div_factor(u64 num, int factor)
272{
Chris Mason257d0ce2007-11-07 21:08:16 -0500273 if (factor == 10)
274 return num;
Chris Mason84f54cf2007-06-12 07:43:08 -0400275 num *= factor;
276 do_div(num, 10);
277 return num;
278}
279
Chris Mason6324fbf2008-03-24 15:01:59 -0400280static 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 Mason31f3c992007-04-30 15:25:45 -0400292struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
293 struct btrfs_block_group_cache
Chris Masonbe744172007-05-06 10:15:01 -0400294 *hint, u64 search_start,
Chris Masonde428b62007-05-18 13:28:27 -0400295 int data, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400296{
Chris Mason96b51792007-10-15 16:15:19 -0400297 struct btrfs_block_group_cache *cache;
Chris Masond1310b22008-01-24 16:13:08 -0500298 struct extent_io_tree *block_group_cache;
Chris Mason31f3c992007-04-30 15:25:45 -0400299 struct btrfs_block_group_cache *found_group = NULL;
Chris Masoncd1bc462007-04-27 10:08:34 -0400300 struct btrfs_fs_info *info = root->fs_info;
301 u64 used;
Chris Mason31f3c992007-04-30 15:25:45 -0400302 u64 last = 0;
303 u64 hint_last;
Chris Mason96b51792007-10-15 16:15:19 -0400304 u64 start;
305 u64 end;
306 u64 free_check;
307 u64 ptr;
Chris Masonc31f8832008-01-08 15:46:31 -0500308 u64 total_fs_bytes;
Chris Mason96b51792007-10-15 16:15:19 -0400309 int bit;
Chris Masoncd1bc462007-04-27 10:08:34 -0400310 int ret;
Chris Mason31f3c992007-04-30 15:25:45 -0400311 int full_search = 0;
Chris Masonbce4eae2008-04-24 14:42:46 -0400312 int factor = 10;
Chris Masonde428b62007-05-18 13:28:27 -0400313
Chris Mason96b51792007-10-15 16:15:19 -0400314 block_group_cache = &info->block_group_cache;
Chris Masonc31f8832008-01-08 15:46:31 -0500315 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
Chris Mason96b51792007-10-15 16:15:19 -0400316
Chris Masonde428b62007-05-18 13:28:27 -0400317 if (!owner)
Chris Masonbce4eae2008-04-24 14:42:46 -0400318 factor = 10;
Chris Masonbe744172007-05-06 10:15:01 -0400319
Chris Mason6324fbf2008-03-24 15:01:59 -0400320 bit = block_group_state_bits(data);
Chris Masonbe744172007-05-06 10:15:01 -0400321
Chris Masonc31f8832008-01-08 15:46:31 -0500322 if (search_start && search_start < total_fs_bytes) {
Chris Masonbe744172007-05-06 10:15:01 -0400323 struct btrfs_block_group_cache *shint;
Chris Mason5276aed2007-06-11 21:33:38 -0400324 shint = btrfs_lookup_block_group(info, search_start);
Chris Mason8f18cf12008-04-25 16:53:30 -0400325 if (shint && block_group_bits(shint, data) && !shint->ro) {
Chris Masonbe744172007-05-06 10:15:01 -0400326 used = btrfs_block_group_used(&shint->item);
Yan324ae4d2007-11-16 14:57:08 -0500327 if (used + shint->pinned <
328 div_factor(shint->key.offset, factor)) {
Chris Masonbe744172007-05-06 10:15:01 -0400329 return shint;
330 }
331 }
332 }
Chris Mason8f18cf12008-04-25 16:53:30 -0400333 if (hint && !hint->ro && block_group_bits(hint, data) &&
Chris Mason0b86a832008-03-24 15:01:56 -0400334 hint->key.objectid < total_fs_bytes) {
Chris Mason31f3c992007-04-30 15:25:45 -0400335 used = btrfs_block_group_used(&hint->item);
Yan324ae4d2007-11-16 14:57:08 -0500336 if (used + hint->pinned <
337 div_factor(hint->key.offset, factor)) {
Chris Mason31f3c992007-04-30 15:25:45 -0400338 return hint;
339 }
Chris Masone19caa52007-10-15 16:17:44 -0400340 last = hint->key.objectid + hint->key.offset;
Chris Mason31f3c992007-04-30 15:25:45 -0400341 hint_last = last;
342 } else {
Chris Masone37c9e62007-05-09 20:13:14 -0400343 if (hint)
344 hint_last = max(hint->key.objectid, search_start);
345 else
346 hint_last = search_start;
347
Chris Masonc31f8832008-01-08 15:46:31 -0500348 if (hint_last >= total_fs_bytes)
349 hint_last = search_start;
Chris Masone37c9e62007-05-09 20:13:14 -0400350 last = hint_last;
Chris Mason31f3c992007-04-30 15:25:45 -0400351 }
Chris Mason31f3c992007-04-30 15:25:45 -0400352again:
Chris Masoncd1bc462007-04-27 10:08:34 -0400353 while(1) {
Chris Mason96b51792007-10-15 16:15:19 -0400354 ret = find_first_extent_bit(block_group_cache, last,
355 &start, &end, bit);
356 if (ret)
Chris Masoncd1bc462007-04-27 10:08:34 -0400357 break;
Chris Mason96b51792007-10-15 16:15:19 -0400358
359 ret = get_state_private(block_group_cache, start, &ptr);
360 if (ret)
361 break;
362
Jens Axboeae2f5412007-10-19 09:22:59 -0400363 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
Chris Mason96b51792007-10-15 16:15:19 -0400364 last = cache->key.objectid + cache->key.offset;
365 used = btrfs_block_group_used(&cache->item);
366
Chris Masonc31f8832008-01-08 15:46:31 -0500367 if (cache->key.objectid > total_fs_bytes)
368 break;
369
Chris Mason8f18cf12008-04-25 16:53:30 -0400370 if (!cache->ro && block_group_bits(cache, data)) {
Chris Mason8790d502008-04-03 16:29:03 -0400371 if (full_search)
372 free_check = cache->key.offset;
373 else
374 free_check = div_factor(cache->key.offset,
375 factor);
Chris Mason6324fbf2008-03-24 15:01:59 -0400376
Chris Mason8790d502008-04-03 16:29:03 -0400377 if (used + cache->pinned < free_check) {
378 found_group = cache;
379 goto found;
380 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400381 }
Chris Masonde428b62007-05-18 13:28:27 -0400382 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400383 }
Chris Mason31f3c992007-04-30 15:25:45 -0400384 if (!full_search) {
Chris Masonbe744172007-05-06 10:15:01 -0400385 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400386 full_search = 1;
387 goto again;
388 }
Chris Masonbe744172007-05-06 10:15:01 -0400389found:
Chris Mason31f3c992007-04-30 15:25:45 -0400390 return found_group;
Chris Masoncd1bc462007-04-27 10:08:34 -0400391}
392
Chris Mason7bb86312007-12-11 09:25:06 -0500393static u64 hash_extent_ref(u64 root_objectid, u64 ref_generation,
Chris Mason74493f72007-12-11 09:25:06 -0500394 u64 owner, u64 owner_offset)
395{
396 u32 high_crc = ~(u32)0;
397 u32 low_crc = ~(u32)0;
398 __le64 lenum;
Chris Mason74493f72007-12-11 09:25:06 -0500399 lenum = cpu_to_le64(root_objectid);
Miguela5eb62e2008-04-11 15:45:51 -0400400 high_crc = btrfs_crc32c(high_crc, &lenum, sizeof(lenum));
Chris Mason7bb86312007-12-11 09:25:06 -0500401 lenum = cpu_to_le64(ref_generation);
Miguela5eb62e2008-04-11 15:45:51 -0400402 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
Chris Mason21a49892008-02-01 14:51:59 -0500403 if (owner >= BTRFS_FIRST_FREE_OBJECTID) {
404 lenum = cpu_to_le64(owner);
Miguela5eb62e2008-04-11 15:45:51 -0400405 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
Chris Mason21a49892008-02-01 14:51:59 -0500406 lenum = cpu_to_le64(owner_offset);
Miguela5eb62e2008-04-11 15:45:51 -0400407 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
Chris Mason21a49892008-02-01 14:51:59 -0500408 }
Chris Mason74493f72007-12-11 09:25:06 -0500409 return ((u64)high_crc << 32) | (u64)low_crc;
410}
411
Chris Mason7bb86312007-12-11 09:25:06 -0500412static 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 Mason98ed5172008-01-03 10:01:48 -0500428static 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 Mason7bb86312007-12-11 09:25:06 -0500434{
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 }
490out:
491 return ret;
492}
493
Chris Masond8d5f3e2007-12-11 12:42:00 -0500494/*
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 Masonf6dbff52007-12-13 11:13:32 -0500557 * (root->root_key.objectid, trans->transid or zero, level, lowest_key_objectid)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500558 *
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 Masonf6dbff52007-12-13 11:13:32 -0500563 * (root->root_key.objectid, trans->transid, level, lowest_key_objectid)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500564 *
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 Mason7bb86312007-12-11 09:25:06 -0500582int 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 Mason74493f72007-12-11 09:25:06 -0500587{
588 u64 hash;
589 struct btrfs_key key;
590 struct btrfs_extent_ref ref;
Chris Mason7bb86312007-12-11 09:25:06 -0500591 struct btrfs_extent_ref *disk_ref;
Chris Mason74493f72007-12-11 09:25:06 -0500592 int ret;
593
594 btrfs_set_stack_ref_root(&ref, root_objectid);
Chris Mason7bb86312007-12-11 09:25:06 -0500595 btrfs_set_stack_ref_generation(&ref, ref_generation);
Chris Mason74493f72007-12-11 09:25:06 -0500596 btrfs_set_stack_ref_objectid(&ref, owner);
597 btrfs_set_stack_ref_offset(&ref, owner_offset);
598
Chris Mason7bb86312007-12-11 09:25:06 -0500599 hash = hash_extent_ref(root_objectid, ref_generation, owner,
600 owner_offset);
Chris Mason74493f72007-12-11 09:25:06 -0500601 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 Mason7bb86312007-12-11 09:25:06 -0500607 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 Mason74493f72007-12-11 09:25:06 -0500615 }
Chris Mason7bb86312007-12-11 09:25:06 -0500616 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]);
623out:
624 btrfs_release_path(root, path);
625 return ret;
Chris Mason74493f72007-12-11 09:25:06 -0500626}
627
Chris Masonb18c6682007-04-17 13:26:50 -0400628int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
629 struct btrfs_root *root,
Chris Mason74493f72007-12-11 09:25:06 -0500630 u64 bytenr, u64 num_bytes,
Chris Mason7bb86312007-12-11 09:25:06 -0500631 u64 root_objectid, u64 ref_generation,
Chris Mason74493f72007-12-11 09:25:06 -0500632 u64 owner, u64 owner_offset)
Chris Mason02217ed2007-03-02 16:08:05 -0500633{
Chris Mason5caf2a02007-04-02 11:20:42 -0400634 struct btrfs_path *path;
Chris Mason02217ed2007-03-02 16:08:05 -0500635 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400636 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400637 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -0400638 struct btrfs_extent_item *item;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400639 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500640
Chris Masondb945352007-10-15 16:15:53 -0400641 WARN_ON(num_bytes < root->sectorsize);
Chris Mason5caf2a02007-04-02 11:20:42 -0400642 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400643 if (!path)
644 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -0400645
Chris Mason3c12ac72008-04-21 12:01:38 -0400646 path->reada = 1;
Chris Masondb945352007-10-15 16:15:53 -0400647 key.objectid = bytenr;
Chris Mason62e27492007-03-15 12:56:47 -0400648 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masondb945352007-10-15 16:15:53 -0400649 key.offset = num_bytes;
Chris Mason5caf2a02007-04-02 11:20:42 -0400650 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400651 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400652 if (ret < 0)
653 return ret;
Chris Masona429e512007-04-18 16:15:28 -0400654 if (ret != 0) {
Chris Masona28ec192007-03-06 20:08:01 -0500655 BUG();
Chris Masona429e512007-04-18 16:15:28 -0400656 }
Chris Mason02217ed2007-03-02 16:08:05 -0500657 BUG_ON(ret != 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400658 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -0400659 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400660 refs = btrfs_extent_refs(l, item);
661 btrfs_set_extent_refs(l, item, refs + 1);
Chris Mason5caf2a02007-04-02 11:20:42 -0400662 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masona28ec192007-03-06 20:08:01 -0500663
Chris Mason5caf2a02007-04-02 11:20:42 -0400664 btrfs_release_path(root->fs_info->extent_root, path);
Chris Mason7bb86312007-12-11 09:25:06 -0500665
Chris Mason3c12ac72008-04-21 12:01:38 -0400666 path->reada = 1;
Chris Mason7bb86312007-12-11 09:25:06 -0500667 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 Mason9f5fae22007-03-20 14:38:32 -0400671 finish_current_insert(trans, root->fs_info->extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400672 del_pending_extents(trans, root->fs_info->extent_root);
Chris Mason74493f72007-12-11 09:25:06 -0500673
674 btrfs_free_path(path);
Chris Mason02217ed2007-03-02 16:08:05 -0500675 return 0;
676}
677
Chris Masone9d0b132007-08-10 14:06:19 -0400678int 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 Masonb18c6682007-04-17 13:26:50 -0400686static int lookup_extent_ref(struct btrfs_trans_handle *trans,
Chris Masondb945352007-10-15 16:15:53 -0400687 struct btrfs_root *root, u64 bytenr,
688 u64 num_bytes, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -0500689{
Chris Mason5caf2a02007-04-02 11:20:42 -0400690 struct btrfs_path *path;
Chris Masona28ec192007-03-06 20:08:01 -0500691 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400692 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400693 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -0400694 struct btrfs_extent_item *item;
Chris Mason5caf2a02007-04-02 11:20:42 -0400695
Chris Masondb945352007-10-15 16:15:53 -0400696 WARN_ON(num_bytes < root->sectorsize);
Chris Mason5caf2a02007-04-02 11:20:42 -0400697 path = btrfs_alloc_path();
Chris Mason3c12ac72008-04-21 12:01:38 -0400698 path->reada = 1;
Chris Masondb945352007-10-15 16:15:53 -0400699 key.objectid = bytenr;
700 key.offset = num_bytes;
Chris Mason62e27492007-03-15 12:56:47 -0400701 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5caf2a02007-04-02 11:20:42 -0400702 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400703 0, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400704 if (ret < 0)
705 goto out;
Chris Mason5f39d392007-10-15 16:14:19 -0400706 if (ret != 0) {
707 btrfs_print_leaf(root, path->nodes[0]);
Chris Masondb945352007-10-15 16:15:53 -0400708 printk("failed to find block number %Lu\n", bytenr);
Chris Masona28ec192007-03-06 20:08:01 -0500709 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -0400710 }
711 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -0400712 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400713 *refs = btrfs_extent_refs(l, item);
Chris Mason54aa1f42007-06-22 14:16:25 -0400714out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400715 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -0500716 return 0;
717}
718
Chris Masonbe20aa92007-12-17 20:14:01 -0500719u32 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 Mason56b453c2008-01-03 09:08:27 -0500727 u64 root_objectid = root->root_key.objectid;
Chris Masonbe20aa92007-12-17 20:14:01 -0500728 u32 total_count = 0;
729 u32 cur_count;
Chris Masonbe20aa92007-12-17 20:14:01 -0500730 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();
740again:
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 Masonbe20aa92007-12-17 20:14:01 -0500765 while (1) {
Chris Masonbd098352008-01-03 13:23:19 -0500766 l = path->nodes[0];
Chris Masonbe20aa92007-12-17 20:14:01 -0500767 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 Masonbd098352008-01-03 13:23:19 -0500777
Chris Masonbe20aa92007-12-17 20:14:01 -0500778 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 Mason56b453c2008-01-03 09:08:27 -0500788 if (found_objectid != root_objectid) {
789 total_count = 2;
Chris Mason4313b392008-01-03 09:08:48 -0500790 goto out;
Chris Mason56b453c2008-01-03 09:08:27 -0500791 }
792 total_count = 1;
Chris Masonbe20aa92007-12-17 20:14:01 -0500793 path->slots[0]++;
794 }
795 if (cur_count == 0) {
796 total_count = 0;
797 goto out;
798 }
Chris Masonbe20aa92007-12-17 20:14:01 -0500799 if (level >= 0 && root->node == count_path->nodes[level])
800 goto out;
801 level++;
802 btrfs_release_path(root, path);
803 goto again;
804
805out:
806 btrfs_free_path(path);
807 return total_count;
Chris Masonbe20aa92007-12-17 20:14:01 -0500808}
Chris Masonc5739bb2007-04-10 09:27:04 -0400809int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
Chris Mason7bb86312007-12-11 09:25:06 -0500810 struct btrfs_root *root, u64 owner_objectid)
Chris Masonc5739bb2007-04-10 09:27:04 -0400811{
Chris Mason7bb86312007-12-11 09:25:06 -0500812 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 Masondb945352007-10-15 16:15:53 -0400830 return btrfs_inc_extent_ref(trans, root, root->node->start,
Chris Mason7bb86312007-12-11 09:25:06 -0500831 root->node->len, owner_objectid,
Chris Masonf6dbff52007-12-13 11:13:32 -0500832 generation, level, key_objectid);
Chris Masonc5739bb2007-04-10 09:27:04 -0400833}
834
Chris Masone089f052007-03-16 16:20:31 -0400835int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400836 struct extent_buffer *buf)
Chris Mason02217ed2007-03-02 16:08:05 -0500837{
Chris Masondb945352007-10-15 16:15:53 -0400838 u64 bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -0400839 u32 nritems;
840 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -0400841 struct btrfs_file_extent_item *fi;
Chris Mason02217ed2007-03-02 16:08:05 -0500842 int i;
Chris Masondb945352007-10-15 16:15:53 -0400843 int level;
Chris Mason6407bf62007-03-27 06:33:00 -0400844 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400845 int faili;
Chris Masona28ec192007-03-06 20:08:01 -0500846
Chris Mason3768f362007-03-13 16:47:54 -0400847 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -0500848 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400849
Chris Masondb945352007-10-15 16:15:53 -0400850 level = btrfs_header_level(buf);
Chris Mason5f39d392007-10-15 16:14:19 -0400851 nritems = btrfs_header_nritems(buf);
852 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -0400853 if (level == 0) {
854 u64 disk_bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -0400855 btrfs_item_key_to_cpu(buf, &key, i);
856 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -0400857 continue;
Chris Mason5f39d392007-10-15 16:14:19 -0400858 fi = btrfs_item_ptr(buf, i,
Chris Mason6407bf62007-03-27 06:33:00 -0400859 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400860 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason236454df2007-04-19 13:37:44 -0400861 BTRFS_FILE_EXTENT_INLINE)
862 continue;
Chris Masondb945352007-10-15 16:15:53 -0400863 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
864 if (disk_bytenr == 0)
Chris Mason3a686372007-05-24 13:35:57 -0400865 continue;
Chris Masondb945352007-10-15 16:15:53 -0400866 ret = btrfs_inc_extent_ref(trans, root, disk_bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -0500867 btrfs_file_extent_disk_num_bytes(buf, fi),
868 root->root_key.objectid, trans->transid,
869 key.objectid, key.offset);
Chris Mason54aa1f42007-06-22 14:16:25 -0400870 if (ret) {
871 faili = i;
872 goto fail;
873 }
Chris Mason6407bf62007-03-27 06:33:00 -0400874 } else {
Chris Masondb945352007-10-15 16:15:53 -0400875 bytenr = btrfs_node_blockptr(buf, i);
Chris Mason6caab482007-12-13 09:48:07 -0500876 btrfs_node_key_to_cpu(buf, &key, i);
Chris Masondb945352007-10-15 16:15:53 -0400877 ret = btrfs_inc_extent_ref(trans, root, bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -0500878 btrfs_level_size(root, level - 1),
879 root->root_key.objectid,
Chris Masonf6dbff52007-12-13 11:13:32 -0500880 trans->transid,
881 level - 1, key.objectid);
Chris Mason54aa1f42007-06-22 14:16:25 -0400882 if (ret) {
883 faili = i;
884 goto fail;
885 }
Chris Mason6407bf62007-03-27 06:33:00 -0400886 }
Chris Mason02217ed2007-03-02 16:08:05 -0500887 }
888 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -0400889fail:
Chris Masonccd467d2007-06-28 15:57:36 -0400890 WARN_ON(1);
Chris Mason7bb86312007-12-11 09:25:06 -0500891#if 0
Chris Mason54aa1f42007-06-22 14:16:25 -0400892 for (i =0; i < faili; i++) {
Chris Masondb945352007-10-15 16:15:53 -0400893 if (level == 0) {
894 u64 disk_bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -0400895 btrfs_item_key_to_cpu(buf, &key, i);
896 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -0400897 continue;
Chris Mason5f39d392007-10-15 16:14:19 -0400898 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -0400899 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400900 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -0400901 BTRFS_FILE_EXTENT_INLINE)
902 continue;
Chris Masondb945352007-10-15 16:15:53 -0400903 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
904 if (disk_bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -0400905 continue;
Chris Masondb945352007-10-15 16:15:53 -0400906 err = btrfs_free_extent(trans, root, disk_bytenr,
907 btrfs_file_extent_disk_num_bytes(buf,
Chris Mason5f39d392007-10-15 16:14:19 -0400908 fi), 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400909 BUG_ON(err);
910 } else {
Chris Masondb945352007-10-15 16:15:53 -0400911 bytenr = btrfs_node_blockptr(buf, i);
912 err = btrfs_free_extent(trans, root, bytenr,
913 btrfs_level_size(root, level - 1), 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400914 BUG_ON(err);
915 }
916 }
Chris Mason7bb86312007-12-11 09:25:06 -0500917#endif
Chris Mason54aa1f42007-06-22 14:16:25 -0400918 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -0500919}
920
Chris Mason9078a3e2007-04-26 16:46:15 -0400921static 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 Mason5f39d392007-10-15 16:14:19 -0400929 unsigned long bi;
930 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -0400931
Chris Mason9078a3e2007-04-26 16:46:15 -0400932 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400933 if (ret < 0)
934 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -0400935 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -0400936
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 Mason9078a3e2007-04-26 16:46:15 -0400941 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400942fail:
Chris Mason9078a3e2007-04-26 16:46:15 -0400943 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 Mason96b51792007-10-15 16:15:19 -0400953int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
954 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -0400955{
Chris Masond1310b22008-01-24 16:13:08 -0500956 struct extent_io_tree *block_group_cache;
Chris Mason96b51792007-10-15 16:15:19 -0400957 struct btrfs_block_group_cache *cache;
Chris Mason9078a3e2007-04-26 16:46:15 -0400958 int ret;
959 int err = 0;
960 int werr = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -0400961 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -0400962 u64 last = 0;
963 u64 start;
964 u64 end;
965 u64 ptr;
Chris Mason9078a3e2007-04-26 16:46:15 -0400966
Chris Mason96b51792007-10-15 16:15:19 -0400967 block_group_cache = &root->fs_info->block_group_cache;
Chris Mason9078a3e2007-04-26 16:46:15 -0400968 path = btrfs_alloc_path();
969 if (!path)
970 return -ENOMEM;
971
972 while(1) {
Chris Mason96b51792007-10-15 16:15:19 -0400973 ret = find_first_extent_bit(block_group_cache, last,
974 &start, &end, BLOCK_GROUP_DIRTY);
975 if (ret)
Chris Mason9078a3e2007-04-26 16:46:15 -0400976 break;
Chris Mason54aa1f42007-06-22 14:16:25 -0400977
Chris Mason96b51792007-10-15 16:15:19 -0400978 last = end + 1;
979 ret = get_state_private(block_group_cache, start, &ptr);
980 if (ret)
981 break;
Jens Axboeae2f5412007-10-19 09:22:59 -0400982 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
Chris Mason96b51792007-10-15 16:15:19 -0400983 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 Mason9078a3e2007-04-26 16:46:15 -0400993 }
Chris Mason96b51792007-10-15 16:15:19 -0400994 clear_extent_bits(block_group_cache, start, end,
995 BLOCK_GROUP_DIRTY, GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -0400996 }
997 btrfs_free_path(path);
998 return werr;
999}
1000
Chris Mason6324fbf2008-03-24 15:01:59 -04001001static 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 Mason593060d2008-03-25 16:50:33 -04001016static 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 Mason8f18cf12008-04-25 16:53:30 -04001026 found->full = 0;
Chris Mason593060d2008-03-25 16:50:33 -04001027 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 Mason8790d502008-04-03 16:29:03 -04001045static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
1046{
1047 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
Chris Mason611f0e02008-04-03 16:29:03 -04001048 BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04001049 BTRFS_BLOCK_GROUP_RAID10 |
Chris Mason611f0e02008-04-03 16:29:03 -04001050 BTRFS_BLOCK_GROUP_DUP);
Chris Mason8790d502008-04-03 16:29:03 -04001051 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 Mason593060d2008-03-25 16:50:33 -04001060
Chris Mason6324fbf2008-03-24 15:01:59 -04001061static 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 Mason593060d2008-03-25 16:50:33 -04001072 if (!space_info) {
1073 ret = update_space_info(extent_root->fs_info, flags,
1074 0, 0, &space_info);
1075 BUG_ON(ret);
1076 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001077 BUG_ON(!space_info);
1078
1079 if (space_info->full)
1080 return 0;
1081
Chris Mason8790d502008-04-03 16:29:03 -04001082 thresh = div_factor(space_info->total_bytes, 6);
Chris Mason6324fbf2008-03-24 15:01:59 -04001083 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) {
1089printk("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 Masone17cade2008-04-15 15:41:47 -04001097 BTRFS_FIRST_CHUNK_TREE_OBJECTID, start, num_bytes);
Chris Mason6324fbf2008-03-24 15:01:59 -04001098 BUG_ON(ret);
Chris Mason593060d2008-03-25 16:50:33 -04001099
Chris Mason6324fbf2008-03-24 15:01:59 -04001100 return 0;
1101}
1102
Chris Mason9078a3e2007-04-26 16:46:15 -04001103static int update_block_group(struct btrfs_trans_handle *trans,
1104 struct btrfs_root *root,
Chris Masondb945352007-10-15 16:15:53 -04001105 u64 bytenr, u64 num_bytes, int alloc,
Chris Mason0b86a832008-03-24 15:01:56 -04001106 int mark_free)
Chris Mason9078a3e2007-04-26 16:46:15 -04001107{
1108 struct btrfs_block_group_cache *cache;
1109 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04001110 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04001111 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04001112 u64 byte_in_group;
Chris Mason96b51792007-10-15 16:15:19 -04001113 u64 start;
1114 u64 end;
Chris Mason3e1ad542007-05-07 20:03:49 -04001115
Chris Mason9078a3e2007-04-26 16:46:15 -04001116 while(total) {
Chris Masondb945352007-10-15 16:15:53 -04001117 cache = btrfs_lookup_block_group(info, bytenr);
Chris Mason3e1ad542007-05-07 20:03:49 -04001118 if (!cache) {
Chris Mason9078a3e2007-04-26 16:46:15 -04001119 return -1;
Chris Masoncd1bc462007-04-27 10:08:34 -04001120 }
Chris Masondb945352007-10-15 16:15:53 -04001121 byte_in_group = bytenr - cache->key.objectid;
1122 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason96b51792007-10-15 16:15:19 -04001123 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 Mason9078a3e2007-04-26 16:46:15 -04001127
1128 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04001129 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04001130 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04001131 old_val += num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04001132 cache->space_info->bytes_used += num_bytes;
Chris Masoncd1bc462007-04-27 10:08:34 -04001133 } else {
Chris Masondb945352007-10-15 16:15:53 -04001134 old_val -= num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04001135 cache->space_info->bytes_used -= num_bytes;
Chris Masonf510cfe2007-10-15 16:14:48 -04001136 if (mark_free) {
1137 set_extent_dirty(&info->free_space_cache,
Chris Masondb945352007-10-15 16:15:53 -04001138 bytenr, bytenr + num_bytes - 1,
Chris Masonf510cfe2007-10-15 16:14:48 -04001139 GFP_NOFS);
Chris Masone37c9e62007-05-09 20:13:14 -04001140 }
Chris Masoncd1bc462007-04-27 10:08:34 -04001141 }
Chris Mason9078a3e2007-04-26 16:46:15 -04001142 btrfs_set_block_group_used(&cache->item, old_val);
Chris Masondb945352007-10-15 16:15:53 -04001143 total -= num_bytes;
1144 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04001145 }
1146 return 0;
1147}
Chris Mason6324fbf2008-03-24 15:01:59 -04001148
Yan324ae4d2007-11-16 14:57:08 -05001149static 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 Mason6324fbf2008-03-24 15:01:59 -04001170 cache->space_info->bytes_pinned += len;
Yan324ae4d2007-11-16 14:57:08 -05001171 fs_info->total_pinned += len;
1172 } else {
1173 cache->pinned -= len;
Chris Mason6324fbf2008-03-24 15:01:59 -04001174 cache->space_info->bytes_pinned -= len;
Yan324ae4d2007-11-16 14:57:08 -05001175 fs_info->total_pinned -= len;
1176 }
1177 bytenr += len;
1178 num -= len;
1179 }
1180 return 0;
1181}
Chris Mason9078a3e2007-04-26 16:46:15 -04001182
Chris Masond1310b22008-01-24 16:13:08 -05001183int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
Chris Masonccd467d2007-06-28 15:57:36 -04001184{
Chris Masonccd467d2007-06-28 15:57:36 -04001185 u64 last = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -04001186 u64 start;
1187 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -05001188 struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
Chris Masonccd467d2007-06-28 15:57:36 -04001189 int ret;
Chris Masonccd467d2007-06-28 15:57:36 -04001190
1191 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001192 ret = find_first_extent_bit(pinned_extents, last,
1193 &start, &end, EXTENT_DIRTY);
1194 if (ret)
Chris Masonccd467d2007-06-28 15:57:36 -04001195 break;
Chris Mason1a5bc162007-10-15 16:15:26 -04001196 set_extent_dirty(copy, start, end, GFP_NOFS);
1197 last = end + 1;
Chris Masonccd467d2007-06-28 15:57:36 -04001198 }
1199 return 0;
1200}
1201
1202int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
1203 struct btrfs_root *root,
Chris Masond1310b22008-01-24 16:13:08 -05001204 struct extent_io_tree *unpin)
Chris Masona28ec192007-03-06 20:08:01 -05001205{
Chris Mason1a5bc162007-10-15 16:15:26 -04001206 u64 start;
1207 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05001208 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05001209 struct extent_io_tree *free_space_cache;
Chris Masonf510cfe2007-10-15 16:14:48 -04001210 free_space_cache = &root->fs_info->free_space_cache;
Chris Masona28ec192007-03-06 20:08:01 -05001211
1212 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001213 ret = find_first_extent_bit(unpin, 0, &start, &end,
1214 EXTENT_DIRTY);
1215 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05001216 break;
Yan324ae4d2007-11-16 14:57:08 -05001217 update_pinned_extents(root, start, end + 1 - start, 0);
Chris Mason1a5bc162007-10-15 16:15:26 -04001218 clear_extent_dirty(unpin, start, end, GFP_NOFS);
1219 set_extent_dirty(free_space_cache, start, end, GFP_NOFS);
Chris Masona28ec192007-03-06 20:08:01 -05001220 }
1221 return 0;
1222}
1223
Chris Mason98ed5172008-01-03 10:01:48 -05001224static int finish_current_insert(struct btrfs_trans_handle *trans,
1225 struct btrfs_root *extent_root)
Chris Mason037e6392007-03-07 11:50:24 -05001226{
Chris Mason7bb86312007-12-11 09:25:06 -05001227 u64 start;
1228 u64 end;
1229 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Masond8d5f3e2007-12-11 12:42:00 -05001230 struct extent_buffer *eb;
Chris Mason7bb86312007-12-11 09:25:06 -05001231 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -04001232 struct btrfs_key ins;
Chris Masond8d5f3e2007-12-11 12:42:00 -05001233 struct btrfs_disk_key first;
Chris Mason234b63a2007-03-13 10:46:10 -04001234 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -05001235 int ret;
Chris Masond8d5f3e2007-12-11 12:42:00 -05001236 int level;
Chris Mason1a5bc162007-10-15 16:15:26 -04001237 int err = 0;
Chris Mason037e6392007-03-07 11:50:24 -05001238
Chris Mason5f39d392007-10-15 16:14:19 -04001239 btrfs_set_stack_extent_refs(&extent_item, 1);
Chris Mason62e27492007-03-15 12:56:47 -04001240 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
Chris Mason7bb86312007-12-11 09:25:06 -05001241 path = btrfs_alloc_path();
Chris Mason037e6392007-03-07 11:50:24 -05001242
Chris Mason26b80032007-08-08 20:17:12 -04001243 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001244 ret = find_first_extent_bit(&info->extent_ins, 0, &start,
1245 &end, EXTENT_LOCKED);
1246 if (ret)
Chris Mason26b80032007-08-08 20:17:12 -04001247 break;
1248
Chris Mason1a5bc162007-10-15 16:15:26 -04001249 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 Masond8d5f3e2007-12-11 12:42:00 -05001255 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 Mason7bb86312007-12-11 09:25:06 -05001262 err = btrfs_insert_extent_backref(trans, extent_root, path,
1263 start, extent_root->root_key.objectid,
Chris Masonf6dbff52007-12-13 11:13:32 -05001264 0, level,
1265 btrfs_disk_key_objectid(&first));
Chris Mason7bb86312007-12-11 09:25:06 -05001266 BUG_ON(err);
Chris Masond8d5f3e2007-12-11 12:42:00 -05001267 free_extent_buffer(eb);
Chris Mason037e6392007-03-07 11:50:24 -05001268 }
Chris Mason7bb86312007-12-11 09:25:06 -05001269 btrfs_free_path(path);
Chris Mason037e6392007-03-07 11:50:24 -05001270 return 0;
1271}
1272
Chris Masondb945352007-10-15 16:15:53 -04001273static int pin_down_bytes(struct btrfs_root *root, u64 bytenr, u32 num_bytes,
1274 int pending)
Chris Masone20d96d2007-03-22 12:13:20 -04001275{
Chris Mason1a5bc162007-10-15 16:15:26 -04001276 int err = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001277 struct extent_buffer *buf;
Chris Mason78fae272007-03-25 11:35:08 -04001278
Chris Masonf4b9aa82007-03-27 11:05:53 -04001279 if (!pending) {
Chris Masondb945352007-10-15 16:15:53 -04001280 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
Chris Mason5f39d392007-10-15 16:14:19 -04001281 if (buf) {
1282 if (btrfs_buffer_uptodate(buf)) {
Chris Mason2c90e5d2007-04-02 10:50:19 -04001283 u64 transid =
1284 root->fs_info->running_transaction->transid;
Chris Masondc17ff82008-01-08 15:46:30 -05001285 u64 header_transid =
1286 btrfs_header_generation(buf);
Chris Mason6bc34672008-04-04 15:40:00 -04001287 if (header_transid == transid &&
1288 !btrfs_header_flag(buf,
1289 BTRFS_HEADER_FLAG_WRITTEN)) {
Chris Mason55c69072008-01-09 15:55:33 -05001290 clean_tree_block(NULL, root, buf);
Chris Mason5f39d392007-10-15 16:14:19 -04001291 free_extent_buffer(buf);
Yanc5492282007-11-06 10:25:25 -05001292 return 1;
Chris Mason2c90e5d2007-04-02 10:50:19 -04001293 }
Chris Masonf4b9aa82007-03-27 11:05:53 -04001294 }
Chris Mason5f39d392007-10-15 16:14:19 -04001295 free_extent_buffer(buf);
Chris Mason8ef97622007-03-26 10:15:30 -04001296 }
Yan324ae4d2007-11-16 14:57:08 -05001297 update_pinned_extents(root, bytenr, num_bytes, 1);
Chris Masonf4b9aa82007-03-27 11:05:53 -04001298 } else {
Chris Mason1a5bc162007-10-15 16:15:26 -04001299 set_extent_bits(&root->fs_info->pending_del,
Chris Masondb945352007-10-15 16:15:53 -04001300 bytenr, bytenr + num_bytes - 1,
1301 EXTENT_LOCKED, GFP_NOFS);
Chris Masonf4b9aa82007-03-27 11:05:53 -04001302 }
Chris Masonbe744172007-05-06 10:15:01 -04001303 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -04001304 return 0;
1305}
1306
Chris Masona28ec192007-03-06 20:08:01 -05001307/*
1308 * remove an extent from the root, returns 0 on success
1309 */
Chris Masone089f052007-03-16 16:20:31 -04001310static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason7bb86312007-12-11 09:25:06 -05001311 *root, u64 bytenr, u64 num_bytes,
1312 u64 root_objectid, u64 ref_generation,
1313 u64 owner_objectid, u64 owner_offset, int pin,
Chris Masone37c9e62007-05-09 20:13:14 -04001314 int mark_free)
Chris Masona28ec192007-03-06 20:08:01 -05001315{
Chris Mason5caf2a02007-04-02 11:20:42 -04001316 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -04001317 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -04001318 struct btrfs_fs_info *info = root->fs_info;
1319 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04001320 struct extent_buffer *leaf;
Chris Masona28ec192007-03-06 20:08:01 -05001321 int ret;
Chris Mason952fcca2008-02-18 16:33:44 -05001322 int extent_slot = 0;
1323 int found_extent = 0;
1324 int num_to_del = 1;
Chris Mason234b63a2007-03-13 10:46:10 -04001325 struct btrfs_extent_item *ei;
Chris Masoncf27e1e2007-03-13 09:49:06 -04001326 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -05001327
Chris Masondb945352007-10-15 16:15:53 -04001328 key.objectid = bytenr;
Chris Mason62e27492007-03-15 12:56:47 -04001329 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masondb945352007-10-15 16:15:53 -04001330 key.offset = num_bytes;
Chris Mason5caf2a02007-04-02 11:20:42 -04001331 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001332 if (!path)
1333 return -ENOMEM;
1334
Chris Mason3c12ac72008-04-21 12:01:38 -04001335 path->reada = 1;
Chris Mason7bb86312007-12-11 09:25:06 -05001336 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 Mason952fcca2008-02-18 16:33:44 -05001341 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 Mason7bb86312007-12-11 09:25:06 -05001359 } 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 Mason952fcca2008-02-18 16:33:44 -05001367 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 Mason5f39d392007-10-15 16:14:19 -04001375
1376 leaf = path->nodes[0];
Chris Mason952fcca2008-02-18 16:33:44 -05001377 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04001378 struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001379 refs = btrfs_extent_refs(leaf, ei);
1380 BUG_ON(refs == 0);
1381 refs -= 1;
1382 btrfs_set_extent_refs(leaf, ei, refs);
Chris Mason952fcca2008-02-18 16:33:44 -05001383
Chris Mason5f39d392007-10-15 16:14:19 -04001384 btrfs_mark_buffer_dirty(leaf);
1385
Chris Mason952fcca2008-02-18 16:33:44 -05001386 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 Masoncf27e1e2007-03-13 09:49:06 -04001407 if (refs == 0) {
Chris Masondb945352007-10-15 16:15:53 -04001408 u64 super_used;
1409 u64 root_used;
Chris Mason78fae272007-03-25 11:35:08 -04001410
1411 if (pin) {
Chris Masondb945352007-10-15 16:15:53 -04001412 ret = pin_down_bytes(root, bytenr, num_bytes, 0);
Yanc5492282007-11-06 10:25:25 -05001413 if (ret > 0)
1414 mark_free = 1;
1415 BUG_ON(ret < 0);
Chris Mason78fae272007-03-25 11:35:08 -04001416 }
1417
Josef Bacik58176a92007-08-29 15:47:34 -04001418 /* block accounting for super block */
Chris Masondb945352007-10-15 16:15:53 -04001419 super_used = btrfs_super_bytes_used(&info->super_copy);
1420 btrfs_set_super_bytes_used(&info->super_copy,
1421 super_used - num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -04001422
1423 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04001424 root_used = btrfs_root_used(&root->root_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001425 btrfs_set_root_used(&root->root_item,
Chris Masondb945352007-10-15 16:15:53 -04001426 root_used - num_bytes);
Chris Mason952fcca2008-02-18 16:33:44 -05001427 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
1428 num_to_del);
Chris Mason54aa1f42007-06-22 14:16:25 -04001429 if (ret) {
1430 return ret;
1431 }
Chris Masondb945352007-10-15 16:15:53 -04001432 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
Chris Mason0b86a832008-03-24 15:01:56 -04001433 mark_free);
Chris Mason9078a3e2007-04-26 16:46:15 -04001434 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -05001435 }
Chris Mason5caf2a02007-04-02 11:20:42 -04001436 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -04001437 finish_current_insert(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -05001438 return ret;
1439}
1440
1441/*
Chris Masonfec577f2007-02-26 10:40:21 -05001442 * find all the blocks marked as pending in the radix tree and remove
1443 * them from the extent map
1444 */
Chris Masone089f052007-03-16 16:20:31 -04001445static int del_pending_extents(struct btrfs_trans_handle *trans, struct
1446 btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -05001447{
1448 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -04001449 int err = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -04001450 u64 start;
1451 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -05001452 struct extent_io_tree *pending_del;
1453 struct extent_io_tree *pinned_extents;
Chris Mason8ef97622007-03-26 10:15:30 -04001454
Chris Mason1a5bc162007-10-15 16:15:26 -04001455 pending_del = &extent_root->fs_info->pending_del;
1456 pinned_extents = &extent_root->fs_info->pinned_extents;
Chris Masonfec577f2007-02-26 10:40:21 -05001457
1458 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001459 ret = find_first_extent_bit(pending_del, 0, &start, &end,
1460 EXTENT_LOCKED);
1461 if (ret)
Chris Masonfec577f2007-02-26 10:40:21 -05001462 break;
Yan324ae4d2007-11-16 14:57:08 -05001463 update_pinned_extents(extent_root, start, end + 1 - start, 1);
Chris Mason1a5bc162007-10-15 16:15:26 -04001464 clear_extent_bits(pending_del, start, end, EXTENT_LOCKED,
1465 GFP_NOFS);
1466 ret = __free_extent(trans, extent_root,
Chris Mason7bb86312007-12-11 09:25:06 -05001467 start, end + 1 - start,
1468 extent_root->root_key.objectid,
1469 0, 0, 0, 0, 0);
Chris Mason1a5bc162007-10-15 16:15:26 -04001470 if (ret)
1471 err = ret;
Chris Masonfec577f2007-02-26 10:40:21 -05001472 }
Chris Masone20d96d2007-03-22 12:13:20 -04001473 return err;
Chris Masonfec577f2007-02-26 10:40:21 -05001474}
1475
1476/*
1477 * remove an extent from the root, returns 0 on success
1478 */
Chris Masone089f052007-03-16 16:20:31 -04001479int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason7bb86312007-12-11 09:25:06 -05001480 *root, u64 bytenr, u64 num_bytes,
1481 u64 root_objectid, u64 ref_generation,
1482 u64 owner_objectid, u64 owner_offset, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -05001483{
Chris Mason9f5fae22007-03-20 14:38:32 -04001484 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -05001485 int pending_ret;
1486 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05001487
Chris Masondb945352007-10-15 16:15:53 -04001488 WARN_ON(num_bytes < root->sectorsize);
Chris Mason7bb86312007-12-11 09:25:06 -05001489 if (!root->ref_cows)
1490 ref_generation = 0;
1491
Chris Masona28ec192007-03-06 20:08:01 -05001492 if (root == extent_root) {
Chris Masondb945352007-10-15 16:15:53 -04001493 pin_down_bytes(root, bytenr, num_bytes, 1);
Chris Masona28ec192007-03-06 20:08:01 -05001494 return 0;
1495 }
Chris Mason7bb86312007-12-11 09:25:06 -05001496 ret = __free_extent(trans, root, bytenr, num_bytes, root_objectid,
1497 ref_generation, owner_objectid, owner_offset,
1498 pin, pin == 0);
Chris Masone20d96d2007-03-22 12:13:20 -04001499 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -05001500 return ret ? ret : pending_ret;
1501}
1502
Chris Mason87ee04e2007-11-30 11:30:34 -05001503static 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 Masonfec577f2007-02-26 10:40:21 -05001510/*
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 Mason62e27492007-03-15 12:56:47 -04001514 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05001515 * ins->offset == number of blocks
1516 * Any available blocks before search_start are skipped.
1517 */
Chris Mason98ed5172008-01-03 10:01:48 -05001518static 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 Masonfec577f2007-02-26 10:40:21 -05001525{
Chris Mason87ee04e2007-11-30 11:30:34 -05001526 int ret;
Chris Masonbe744172007-05-06 10:15:01 -04001527 u64 orig_search_start = search_start;
Chris Mason9f5fae22007-03-20 14:38:32 -04001528 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Masonf2458e12007-04-25 15:52:25 -04001529 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04001530 u64 total_needed = num_bytes;
Chris Mason239b14b2008-03-24 15:02:07 -04001531 u64 *last_ptr = NULL;
Chris Masonbe08c1b2007-05-03 09:06:49 -04001532 struct btrfs_block_group_cache *block_group;
Chris Masonbe744172007-05-06 10:15:01 -04001533 int full_scan = 0;
Chris Masonfbdc7622007-05-30 10:22:12 -04001534 int wrapped = 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001535 int empty_cluster = 2 * 1024 * 1024;
Chris Masonfec577f2007-02-26 10:40:21 -05001536
Chris Masondb945352007-10-15 16:15:53 -04001537 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04001538 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
1539
Chris Mason239b14b2008-03-24 15:02:07 -04001540 if (data & BTRFS_BLOCK_GROUP_METADATA) {
1541 last_ptr = &root->fs_info->last_alloc;
Chris Mason8790d502008-04-03 16:29:03 -04001542 empty_cluster = 256 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04001543 }
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 Mason7f93bf82008-03-24 15:01:28 -04001557 if (search_end == (u64)-1)
1558 search_end = btrfs_super_total_bytes(&info->super_copy);
Chris Mason0b86a832008-03-24 15:01:56 -04001559
Chris Masondb945352007-10-15 16:15:53 -04001560 if (hint_byte) {
1561 block_group = btrfs_lookup_block_group(info, hint_byte);
Chris Mason1a2b2ac2007-12-04 13:18:24 -05001562 if (!block_group)
1563 hint_byte = search_start;
Chris Masonbe744172007-05-06 10:15:01 -04001564 block_group = btrfs_find_block_group(root, block_group,
Chris Masondb945352007-10-15 16:15:53 -04001565 hint_byte, data, 1);
Chris Mason239b14b2008-03-24 15:02:07 -04001566 if (last_ptr && *last_ptr == 0 && block_group)
1567 hint_byte = block_group->key.objectid;
Chris Masonbe744172007-05-06 10:15:01 -04001568 } else {
1569 block_group = btrfs_find_block_group(root,
Chris Mason1a2b2ac2007-12-04 13:18:24 -05001570 trans->block_group,
1571 search_start, data, 1);
Chris Masonbe744172007-05-06 10:15:01 -04001572 }
Chris Mason239b14b2008-03-24 15:02:07 -04001573 search_start = max(search_start, hint_byte);
Chris Masonbe744172007-05-06 10:15:01 -04001574
Chris Mason6702ed42007-08-07 16:15:09 -04001575 total_needed += empty_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001576
Chris Masonbe744172007-05-06 10:15:01 -04001577check_failed:
Chris Mason70b043f2007-12-13 09:02:46 -05001578 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 Mason0b86a832008-03-24 15:01:56 -04001584 ret = find_search_start(root, &block_group, &search_start,
1585 total_needed, data);
Chris Mason239b14b2008-03-24 15:02:07 -04001586 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 Mason0b86a832008-03-24 15:01:56 -04001596 if (ret)
1597 goto error;
1598
Chris Mason239b14b2008-03-24 15:02:07 -04001599 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 Mason87ee04e2007-11-30 11:30:34 -05001616 search_start = stripe_align(root, search_start);
Chris Masonfec577f2007-02-26 10:40:21 -05001617 ins->objectid = search_start;
Chris Mason0b86a832008-03-24 15:01:56 -04001618 ins->offset = num_bytes;
Chris Masone37c9e62007-05-09 20:13:14 -04001619
Chris Masondb945352007-10-15 16:15:53 -04001620 if (ins->objectid + num_bytes >= search_end)
Chris Masoncf675822007-09-17 11:00:51 -04001621 goto enospc;
Chris Mason0b86a832008-03-24 15:01:56 -04001622
1623 if (ins->objectid + num_bytes >
1624 block_group->key.objectid + block_group->key.offset) {
Chris Masone19caa52007-10-15 16:17:44 -04001625 search_start = block_group->key.objectid +
1626 block_group->key.offset;
1627 goto new_group;
1628 }
Chris Mason0b86a832008-03-24 15:01:56 -04001629
Chris Mason1a5bc162007-10-15 16:15:26 -04001630 if (test_range_bit(&info->extent_ins, ins->objectid,
Chris Masondb945352007-10-15 16:15:53 -04001631 ins->objectid + num_bytes -1, EXTENT_LOCKED, 0)) {
1632 search_start = ins->objectid + num_bytes;
Chris Mason1a5bc162007-10-15 16:15:26 -04001633 goto new_group;
1634 }
Chris Mason0b86a832008-03-24 15:01:56 -04001635
Chris Mason1a5bc162007-10-15 16:15:26 -04001636 if (test_range_bit(&info->pinned_extents, ins->objectid,
Chris Masondb945352007-10-15 16:15:53 -04001637 ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) {
1638 search_start = ins->objectid + num_bytes;
Chris Mason1a5bc162007-10-15 16:15:26 -04001639 goto new_group;
Chris Masonfec577f2007-02-26 10:40:21 -05001640 }
Chris Mason0b86a832008-03-24 15:01:56 -04001641
Chris Masondb945352007-10-15 16:15:53 -04001642 if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
Chris Masonf2654de2007-06-26 12:20:46 -04001643 ins->objectid < exclude_start + exclude_nr)) {
1644 search_start = exclude_start + exclude_nr;
1645 goto new_group;
1646 }
Chris Mason0b86a832008-03-24 15:01:56 -04001647
Chris Mason6324fbf2008-03-24 15:01:59 -04001648 if (!(data & BTRFS_BLOCK_GROUP_DATA)) {
Chris Mason5276aed2007-06-11 21:33:38 -04001649 block_group = btrfs_lookup_block_group(info, ins->objectid);
Chris Mason26b80032007-08-08 20:17:12 -04001650 if (block_group)
1651 trans->block_group = block_group;
Chris Masoncd1bc462007-04-27 10:08:34 -04001652 }
Chris Masondb945352007-10-15 16:15:53 -04001653 ins->offset = num_bytes;
Chris Mason239b14b2008-03-24 15:02:07 -04001654 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 Masonfec577f2007-02-26 10:40:21 -05001661 return 0;
Chris Masonbe744172007-05-06 10:15:01 -04001662
1663new_group:
Chris Masondb945352007-10-15 16:15:53 -04001664 if (search_start + num_bytes >= search_end) {
Chris Masoncf675822007-09-17 11:00:51 -04001665enospc:
Chris Masonbe744172007-05-06 10:15:01 -04001666 search_start = orig_search_start;
Chris Masonfbdc7622007-05-30 10:22:12 -04001667 if (full_scan) {
1668 ret = -ENOSPC;
1669 goto error;
1670 }
Chris Mason6702ed42007-08-07 16:15:09 -04001671 if (wrapped) {
1672 if (!full_scan)
1673 total_needed -= empty_size;
Chris Masonfbdc7622007-05-30 10:22:12 -04001674 full_scan = 1;
Chris Mason6702ed42007-08-07 16:15:09 -04001675 } else
Chris Masonfbdc7622007-05-30 10:22:12 -04001676 wrapped = 1;
Chris Masonbe744172007-05-06 10:15:01 -04001677 }
Chris Mason5276aed2007-06-11 21:33:38 -04001678 block_group = btrfs_lookup_block_group(info, search_start);
Chris Masonfbdc7622007-05-30 10:22:12 -04001679 cond_resched();
Chris Mason1a2b2ac2007-12-04 13:18:24 -05001680 block_group = btrfs_find_block_group(root, block_group,
1681 search_start, data, 0);
Chris Masonbe744172007-05-06 10:15:01 -04001682 goto check_failed;
1683
Chris Mason0f70abe2007-02-28 16:46:22 -05001684error:
Chris Mason0f70abe2007-02-28 16:46:22 -05001685 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05001686}
Chris Masonfec577f2007-02-26 10:40:21 -05001687/*
Chris Masonfec577f2007-02-26 10:40:21 -05001688 * 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 Mason4d775672007-04-20 20:23:12 -04001694int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
Chris Mason7bb86312007-12-11 09:25:06 -05001695 struct btrfs_root *root,
Chris Mason98d20f62008-04-14 09:46:10 -04001696 u64 num_bytes, u64 min_alloc_size,
1697 u64 root_objectid, u64 ref_generation,
Chris Mason7bb86312007-12-11 09:25:06 -05001698 u64 owner, u64 owner_offset,
1699 u64 empty_size, u64 hint_byte,
Chris Masonbe08c1b2007-05-03 09:06:49 -04001700 u64 search_end, struct btrfs_key *ins, int data)
Chris Masonfec577f2007-02-26 10:40:21 -05001701{
1702 int ret;
1703 int pending_ret;
Chris Masonc31f8832008-01-08 15:46:31 -05001704 u64 super_used;
1705 u64 root_used;
Chris Masonfbdc7622007-05-30 10:22:12 -04001706 u64 search_start = 0;
Chris Mason8790d502008-04-03 16:29:03 -04001707 u64 alloc_profile;
Chris Mason47e4bb92008-02-01 14:51:59 -05001708 u32 sizes[2];
Chris Mason1261ec42007-03-20 20:35:03 -04001709 struct btrfs_fs_info *info = root->fs_info;
1710 struct btrfs_root *extent_root = info->extent_root;
Chris Mason47e4bb92008-02-01 14:51:59 -05001711 struct btrfs_extent_item *extent_item;
1712 struct btrfs_extent_ref *ref;
Chris Mason7bb86312007-12-11 09:25:06 -05001713 struct btrfs_path *path;
Chris Mason47e4bb92008-02-01 14:51:59 -05001714 struct btrfs_key keys[2];
Chris Mason8f662a72008-01-02 10:01:11 -05001715
Chris Mason6324fbf2008-03-24 15:01:59 -04001716 if (data) {
Chris Mason8790d502008-04-03 16:29:03 -04001717 alloc_profile = info->avail_data_alloc_bits &
1718 info->data_alloc_profile;
1719 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04001720 } else if (root == root->fs_info->chunk_root) {
Chris Mason8790d502008-04-03 16:29:03 -04001721 alloc_profile = info->avail_system_alloc_bits &
1722 info->system_alloc_profile;
1723 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04001724 } else {
Chris Mason8790d502008-04-03 16:29:03 -04001725 alloc_profile = info->avail_metadata_alloc_bits &
1726 info->metadata_alloc_profile;
1727 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04001728 }
Chris Mason98d20f62008-04-14 09:46:10 -04001729again:
Chris Mason3bf3d9e2008-04-26 11:03:32 -04001730 if (root->ref_cows) {
Chris Mason593060d2008-03-25 16:50:33 -04001731 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
Chris Mason6324fbf2008-03-24 15:01:59 -04001732 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason239b14b2008-03-24 15:02:07 -04001733 2 * 1024 * 1024,
Chris Mason593060d2008-03-25 16:50:33 -04001734 BTRFS_BLOCK_GROUP_METADATA |
Chris Mason8790d502008-04-03 16:29:03 -04001735 (info->metadata_alloc_profile &
1736 info->avail_metadata_alloc_bits));
Chris Mason6324fbf2008-03-24 15:01:59 -04001737 BUG_ON(ret);
1738 }
1739 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason8790d502008-04-03 16:29:03 -04001740 num_bytes + 2 * 1024 * 1024, data);
Chris Mason6324fbf2008-03-24 15:01:59 -04001741 BUG_ON(ret);
1742 }
Chris Mason0b86a832008-03-24 15:01:56 -04001743
Chris Masondb945352007-10-15 16:15:53 -04001744 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 Mason26b80032007-08-08 20:17:12 -04001747 trans->alloc_exclude_start,
1748 trans->alloc_exclude_nr, data);
Chris Mason3b951512008-04-17 11:29:12 -04001749
Chris Mason98d20f62008-04-14 09:46:10 -04001750 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 Masonccd467d2007-06-28 15:57:36 -04001755 BUG_ON(ret);
Chris Masonf2654de2007-06-26 12:20:46 -04001756 if (ret)
1757 return ret;
1758
Josef Bacik58176a92007-08-29 15:47:34 -04001759 /* block accounting for super block */
Chris Masondb945352007-10-15 16:15:53 -04001760 super_used = btrfs_super_bytes_used(&info->super_copy);
1761 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
Chris Mason26b80032007-08-08 20:17:12 -04001762
Josef Bacik58176a92007-08-29 15:47:34 -04001763 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04001764 root_used = btrfs_root_used(&root->root_item);
1765 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -04001766
Chris Masonf510cfe2007-10-15 16:14:48 -04001767 clear_extent_dirty(&root->fs_info->free_space_cache,
1768 ins->objectid, ins->objectid + ins->offset - 1,
1769 GFP_NOFS);
1770
Chris Mason26b80032007-08-08 20:17:12 -04001771 if (root == extent_root) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001772 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
1773 ins->objectid + ins->offset - 1,
1774 EXTENT_LOCKED, GFP_NOFS);
Chris Mason26b80032007-08-08 20:17:12 -04001775 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 Mason037e6392007-03-07 11:50:24 -05001781
Chris Mason47e4bb92008-02-01 14:51:59 -05001782 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 Mason7bb86312007-12-11 09:25:06 -05001789
1790 path = btrfs_alloc_path();
1791 BUG_ON(!path);
Chris Mason47e4bb92008-02-01 14:51:59 -05001792
1793 ret = btrfs_insert_empty_items(trans, extent_root, path, keys,
1794 sizes, 2);
Chris Mason26b80032007-08-08 20:17:12 -04001795
Chris Masonccd467d2007-06-28 15:57:36 -04001796 BUG_ON(ret);
Chris Mason47e4bb92008-02-01 14:51:59 -05001797 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 Mason7bb86312007-12-11 09:25:06 -05001812 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -04001813 finish_current_insert(trans, extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -04001814 pending_ret = del_pending_extents(trans, extent_root);
Chris Masonf510cfe2007-10-15 16:14:48 -04001815
Chris Masone37c9e62007-05-09 20:13:14 -04001816 if (ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001817 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001818 }
1819 if (pending_ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001820 return pending_ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001821 }
Chris Mason26b80032007-08-08 20:17:12 -04001822
1823update_block:
Chris Mason0b86a832008-03-24 15:01:56 -04001824 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0);
Chris Masonf5947062008-02-04 10:10:13 -05001825 if (ret) {
1826 printk("update block group failed for %Lu %Lu\n",
1827 ins->objectid, ins->offset);
1828 BUG();
1829 }
Chris Mason037e6392007-03-07 11:50:24 -05001830 return 0;
Chris Masonfec577f2007-02-26 10:40:21 -05001831}
1832
1833/*
1834 * helper function to allocate a block for a given tree
1835 * returns the tree buffer or NULL.
1836 */
Chris Mason5f39d392007-10-15 16:14:19 -04001837struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Masondb945352007-10-15 16:15:53 -04001838 struct btrfs_root *root,
Chris Mason7bb86312007-12-11 09:25:06 -05001839 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 */
1859struct 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 Mason5f39d392007-10-15 16:14:19 -04001867 u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05001868{
Chris Masone2fa7222007-03-12 16:22:34 -04001869 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05001870 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04001871 struct extent_buffer *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05001872
Chris Mason98d20f62008-04-14 09:46:10 -04001873 ret = btrfs_alloc_extent(trans, root, blocksize, blocksize,
Chris Mason7bb86312007-12-11 09:25:06 -05001874 root_objectid, ref_generation,
Chris Masonf6dbff52007-12-13 11:13:32 -05001875 level, first_objectid, empty_size, hint,
Chris Masondb945352007-10-15 16:15:53 -04001876 (u64)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05001877 if (ret) {
Chris Mason54aa1f42007-06-22 14:16:25 -04001878 BUG_ON(ret > 0);
1879 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05001880 }
Chris Masondb945352007-10-15 16:15:53 -04001881 buf = btrfs_find_create_tree_block(root, ins.objectid, blocksize);
Chris Mason54aa1f42007-06-22 14:16:25 -04001882 if (!buf) {
Chris Mason7bb86312007-12-11 09:25:06 -05001883 btrfs_free_extent(trans, root, ins.objectid, blocksize,
1884 root->root_key.objectid, ref_generation,
1885 0, 0, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001886 return ERR_PTR(-ENOMEM);
1887 }
Chris Mason55c69072008-01-09 15:55:33 -05001888 btrfs_set_header_generation(buf, trans->transid);
1889 clean_tree_block(trans, root, buf);
Chris Mason5f39d392007-10-15 16:14:19 -04001890 btrfs_set_buffer_uptodate(buf);
Chris Mason55c69072008-01-09 15:55:33 -05001891
1892 if (PageDirty(buf->first_page)) {
1893 printk("page %lu dirty\n", buf->first_page->index);
1894 WARN_ON(1);
1895 }
1896
Chris Mason5f39d392007-10-15 16:14:19 -04001897 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
1898 buf->start + buf->len - 1, GFP_NOFS);
Chris Mason9afbb0b2008-02-15 13:19:35 -05001899 if (!btrfs_test_opt(root, SSD))
1900 btrfs_set_buffer_defrag(buf);
Chris Masond3c2fdcf2007-09-17 10:58:06 -04001901 trans->blocks_used++;
Chris Masonfec577f2007-02-26 10:40:21 -05001902 return buf;
1903}
Chris Masona28ec192007-03-06 20:08:01 -05001904
Chris Mason98ed5172008-01-03 10:01:48 -05001905static int noinline drop_leaf_ref(struct btrfs_trans_handle *trans,
1906 struct btrfs_root *root,
1907 struct extent_buffer *leaf)
Chris Mason6407bf62007-03-27 06:33:00 -04001908{
Chris Mason7bb86312007-12-11 09:25:06 -05001909 u64 leaf_owner;
1910 u64 leaf_generation;
Chris Mason5f39d392007-10-15 16:14:19 -04001911 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04001912 struct btrfs_file_extent_item *fi;
1913 int i;
1914 int nritems;
1915 int ret;
1916
Chris Mason5f39d392007-10-15 16:14:19 -04001917 BUG_ON(!btrfs_is_leaf(leaf));
1918 nritems = btrfs_header_nritems(leaf);
Chris Mason7bb86312007-12-11 09:25:06 -05001919 leaf_owner = btrfs_header_owner(leaf);
1920 leaf_generation = btrfs_header_generation(leaf);
1921
Chris Mason6407bf62007-03-27 06:33:00 -04001922 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04001923 u64 disk_bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -04001924
1925 btrfs_item_key_to_cpu(leaf, &key, i);
1926 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -04001927 continue;
1928 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001929 if (btrfs_file_extent_type(leaf, fi) ==
1930 BTRFS_FILE_EXTENT_INLINE)
Chris Mason236454df2007-04-19 13:37:44 -04001931 continue;
Chris Mason6407bf62007-03-27 06:33:00 -04001932 /*
1933 * FIXME make sure to insert a trans record that
1934 * repeats the snapshot del on crash
1935 */
Chris Masondb945352007-10-15 16:15:53 -04001936 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1937 if (disk_bytenr == 0)
Chris Mason3a686372007-05-24 13:35:57 -04001938 continue;
Chris Masondb945352007-10-15 16:15:53 -04001939 ret = btrfs_free_extent(trans, root, disk_bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -05001940 btrfs_file_extent_disk_num_bytes(leaf, fi),
1941 leaf_owner, leaf_generation,
1942 key.objectid, key.offset, 0);
Chris Mason6407bf62007-03-27 06:33:00 -04001943 BUG_ON(ret);
1944 }
1945 return 0;
1946}
1947
Chris Mason98ed5172008-01-03 10:01:48 -05001948static void noinline reada_walk_down(struct btrfs_root *root,
Chris Masonbea495e2008-01-24 16:13:14 -05001949 struct extent_buffer *node,
1950 int slot)
Chris Masone0115992007-06-19 16:23:05 -04001951{
Chris Masondb945352007-10-15 16:15:53 -04001952 u64 bytenr;
Chris Masonbea495e2008-01-24 16:13:14 -05001953 u64 last = 0;
1954 u32 nritems;
Chris Masone0115992007-06-19 16:23:05 -04001955 u32 refs;
Chris Masondb945352007-10-15 16:15:53 -04001956 u32 blocksize;
Chris Masonbea495e2008-01-24 16:13:14 -05001957 int ret;
1958 int i;
1959 int level;
1960 int skipped = 0;
Chris Masone0115992007-06-19 16:23:05 -04001961
Chris Mason5f39d392007-10-15 16:14:19 -04001962 nritems = btrfs_header_nritems(node);
Chris Masondb945352007-10-15 16:15:53 -04001963 level = btrfs_header_level(node);
Chris Masonbea495e2008-01-24 16:13:14 -05001964 if (level)
1965 return;
1966
1967 for (i = slot; i < nritems && skipped < 32; i++) {
Chris Masondb945352007-10-15 16:15:53 -04001968 bytenr = btrfs_node_blockptr(node, i);
Chris Masonbea495e2008-01-24 16:13:14 -05001969 if (last && ((bytenr > last && bytenr - last > 32 * 1024) ||
1970 (last > bytenr && last - bytenr > 32 * 1024))) {
1971 skipped++;
Chris Masone0115992007-06-19 16:23:05 -04001972 continue;
Chris Masonbea495e2008-01-24 16:13:14 -05001973 }
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 Mason409eb952007-08-08 20:17:12 -04001984 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masondb945352007-10-15 16:15:53 -04001985 ret = readahead_tree_block(root, bytenr, blocksize);
Chris Masonbea495e2008-01-24 16:13:14 -05001986 last = bytenr + blocksize;
Chris Mason409eb952007-08-08 20:17:12 -04001987 cond_resched();
1988 mutex_lock(&root->fs_info->fs_mutex);
Chris Masone0115992007-06-19 16:23:05 -04001989 if (ret)
1990 break;
1991 }
1992}
1993
Chris Mason9aca1d52007-03-13 11:09:37 -04001994/*
1995 * helper function for drop_snapshot, this walks down the tree dropping ref
1996 * counts as it goes.
1997 */
Chris Mason98ed5172008-01-03 10:01:48 -05001998static int noinline walk_down_tree(struct btrfs_trans_handle *trans,
1999 struct btrfs_root *root,
2000 struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05002001{
Chris Mason7bb86312007-12-11 09:25:06 -05002002 u64 root_owner;
2003 u64 root_gen;
2004 u64 bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -04002005 struct extent_buffer *next;
2006 struct extent_buffer *cur;
Chris Mason7bb86312007-12-11 09:25:06 -05002007 struct extent_buffer *parent;
Chris Masondb945352007-10-15 16:15:53 -04002008 u32 blocksize;
Chris Mason20524f02007-03-10 06:35:47 -05002009 int ret;
2010 u32 refs;
2011
Chris Mason5caf2a02007-04-02 11:20:42 -04002012 WARN_ON(*level < 0);
2013 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason5f39d392007-10-15 16:14:19 -04002014 ret = lookup_extent_ref(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04002015 path->nodes[*level]->start,
2016 path->nodes[*level]->len, &refs);
Chris Mason20524f02007-03-10 06:35:47 -05002017 BUG_ON(ret);
2018 if (refs > 1)
2019 goto out;
Chris Masone0115992007-06-19 16:23:05 -04002020
Chris Mason9aca1d52007-03-13 11:09:37 -04002021 /*
2022 * walk down to the last node level and free all the leaves
2023 */
Chris Mason6407bf62007-03-27 06:33:00 -04002024 while(*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04002025 WARN_ON(*level < 0);
2026 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -05002027 cur = path->nodes[*level];
Chris Masone0115992007-06-19 16:23:05 -04002028
Chris Mason5f39d392007-10-15 16:14:19 -04002029 if (btrfs_header_level(cur) != *level)
Chris Mason2c90e5d2007-04-02 10:50:19 -04002030 WARN_ON(1);
Chris Masone0115992007-06-19 16:23:05 -04002031
Chris Mason7518a232007-03-12 12:01:18 -04002032 if (path->slots[*level] >=
Chris Mason5f39d392007-10-15 16:14:19 -04002033 btrfs_header_nritems(cur))
Chris Mason20524f02007-03-10 06:35:47 -05002034 break;
Chris Mason6407bf62007-03-27 06:33:00 -04002035 if (*level == 0) {
2036 ret = drop_leaf_ref(trans, root, cur);
2037 BUG_ON(ret);
2038 break;
2039 }
Chris Masondb945352007-10-15 16:15:53 -04002040 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 Mason6407bf62007-03-27 06:33:00 -04002043 BUG_ON(ret);
2044 if (refs != 1) {
Chris Mason7bb86312007-12-11 09:25:06 -05002045 parent = path->nodes[*level];
2046 root_owner = btrfs_header_owner(parent);
2047 root_gen = btrfs_header_generation(parent);
Chris Mason20524f02007-03-10 06:35:47 -05002048 path->slots[*level]++;
Chris Masondb945352007-10-15 16:15:53 -04002049 ret = btrfs_free_extent(trans, root, bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -05002050 blocksize, root_owner,
2051 root_gen, 0, 0, 1);
Chris Mason20524f02007-03-10 06:35:47 -05002052 BUG_ON(ret);
2053 continue;
2054 }
Chris Masondb945352007-10-15 16:15:53 -04002055 next = btrfs_find_tree_block(root, bytenr, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04002056 if (!next || !btrfs_buffer_uptodate(next)) {
2057 free_extent_buffer(next);
Chris Masonbea495e2008-01-24 16:13:14 -05002058 reada_walk_down(root, cur, path->slots[*level]);
Chris Masone9d0b132007-08-10 14:06:19 -04002059
Chris Mason8790d502008-04-03 16:29:03 -04002060 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 Masondb945352007-10-15 16:15:53 -04002065 ret = lookup_extent_ref(trans, root, bytenr,
2066 blocksize, &refs);
Chris Masone9d0b132007-08-10 14:06:19 -04002067 BUG_ON(ret);
2068 if (refs != 1) {
Chris Mason7bb86312007-12-11 09:25:06 -05002069 parent = path->nodes[*level];
2070 root_owner = btrfs_header_owner(parent);
2071 root_gen = btrfs_header_generation(parent);
2072
Chris Masone9d0b132007-08-10 14:06:19 -04002073 path->slots[*level]++;
Chris Mason5f39d392007-10-15 16:14:19 -04002074 free_extent_buffer(next);
Chris Mason7bb86312007-12-11 09:25:06 -05002075 ret = btrfs_free_extent(trans, root, bytenr,
2076 blocksize,
2077 root_owner,
2078 root_gen, 0, 0, 1);
Chris Masone9d0b132007-08-10 14:06:19 -04002079 BUG_ON(ret);
2080 continue;
2081 }
Chris Mason0999df52008-04-01 13:48:14 -04002082 } else if (next) {
2083 btrfs_verify_block_csum(root, next);
Chris Masone9d0b132007-08-10 14:06:19 -04002084 }
Chris Mason5caf2a02007-04-02 11:20:42 -04002085 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -04002086 if (path->nodes[*level-1])
Chris Mason5f39d392007-10-15 16:14:19 -04002087 free_extent_buffer(path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -05002088 path->nodes[*level-1] = next;
Chris Mason5f39d392007-10-15 16:14:19 -04002089 *level = btrfs_header_level(next);
Chris Mason20524f02007-03-10 06:35:47 -05002090 path->slots[*level] = 0;
2091 }
2092out:
Chris Mason5caf2a02007-04-02 11:20:42 -04002093 WARN_ON(*level < 0);
2094 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason7bb86312007-12-11 09:25:06 -05002095
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 Masondb945352007-10-15 16:15:53 -04002105 ret = btrfs_free_extent(trans, root, path->nodes[*level]->start,
Chris Mason7bb86312007-12-11 09:25:06 -05002106 path->nodes[*level]->len,
2107 root_owner, root_gen, 0, 0, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002108 free_extent_buffer(path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -05002109 path->nodes[*level] = NULL;
2110 *level += 1;
2111 BUG_ON(ret);
2112 return 0;
2113}
2114
Chris Mason9aca1d52007-03-13 11:09:37 -04002115/*
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 Mason98ed5172008-01-03 10:01:48 -05002120static int noinline walk_up_tree(struct btrfs_trans_handle *trans,
2121 struct btrfs_root *root,
2122 struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05002123{
Chris Mason7bb86312007-12-11 09:25:06 -05002124 u64 root_owner;
2125 u64 root_gen;
2126 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05002127 int i;
2128 int slot;
2129 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04002130
Chris Mason234b63a2007-03-13 10:46:10 -04002131 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -05002132 slot = path->slots[i];
Chris Mason5f39d392007-10-15 16:14:19 -04002133 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 Mason20524f02007-03-10 06:35:47 -05002137 path->slots[i]++;
2138 *level = i;
Chris Mason9f3a7422007-08-07 15:52:19 -04002139 WARN_ON(*level == 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002140 btrfs_node_key(node, &disk_key, path->slots[i]);
Chris Mason9f3a7422007-08-07 15:52:19 -04002141 memcpy(&root_item->drop_progress,
Chris Mason5f39d392007-10-15 16:14:19 -04002142 &disk_key, sizeof(disk_key));
Chris Mason9f3a7422007-08-07 15:52:19 -04002143 root_item->drop_level = i;
Chris Mason20524f02007-03-10 06:35:47 -05002144 return 0;
2145 } else {
Chris Mason7bb86312007-12-11 09:25:06 -05002146 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 Masone089f052007-03-16 16:20:31 -04002156 ret = btrfs_free_extent(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04002157 path->nodes[*level]->start,
Chris Mason7bb86312007-12-11 09:25:06 -05002158 path->nodes[*level]->len,
2159 root_owner, root_gen, 0, 0, 1);
Chris Mason6407bf62007-03-27 06:33:00 -04002160 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04002161 free_extent_buffer(path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -04002162 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -05002163 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -05002164 }
2165 }
2166 return 1;
2167}
2168
Chris Mason9aca1d52007-03-13 11:09:37 -04002169/*
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 Masone089f052007-03-16 16:20:31 -04002174int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason9f3a7422007-08-07 15:52:19 -04002175 *root)
Chris Mason20524f02007-03-10 06:35:47 -05002176{
Chris Mason3768f362007-03-13 16:47:54 -04002177 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -04002178 int wret;
Chris Mason20524f02007-03-10 06:35:47 -05002179 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -04002180 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -05002181 int i;
2182 int orig_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04002183 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05002184
Chris Mason5caf2a02007-04-02 11:20:42 -04002185 path = btrfs_alloc_path();
2186 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05002187
Chris Mason5f39d392007-10-15 16:14:19 -04002188 level = btrfs_header_level(root->node);
Chris Mason20524f02007-03-10 06:35:47 -05002189 orig_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04002190 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
2191 path->nodes[level] = root->node;
Chris Masonf510cfe2007-10-15 16:14:48 -04002192 extent_buffer_get(root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -04002193 path->slots[level] = 0;
2194 } else {
2195 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04002196 struct btrfs_disk_key found_key;
2197 struct extent_buffer *node;
Chris Mason6702ed42007-08-07 16:15:09 -04002198
Chris Mason9f3a7422007-08-07 15:52:19 -04002199 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Chris Mason6702ed42007-08-07 16:15:09 -04002200 level = root_item->drop_level;
2201 path->lowest_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04002202 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason6702ed42007-08-07 16:15:09 -04002203 if (wret < 0) {
Chris Mason9f3a7422007-08-07 15:52:19 -04002204 ret = wret;
2205 goto out;
2206 }
Chris Mason5f39d392007-10-15 16:14:19 -04002207 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 Mason9f3a7422007-08-07 15:52:19 -04002211 }
Chris Mason20524f02007-03-10 06:35:47 -05002212 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -04002213 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04002214 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05002215 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04002216 if (wret < 0)
2217 ret = wret;
2218
Chris Mason5caf2a02007-04-02 11:20:42 -04002219 wret = walk_up_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04002220 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05002221 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04002222 if (wret < 0)
2223 ret = wret;
Chris Mason409eb952007-08-08 20:17:12 -04002224 ret = -EAGAIN;
Chris Mason409eb952007-08-08 20:17:12 -04002225 break;
Chris Mason20524f02007-03-10 06:35:47 -05002226 }
Chris Mason83e15a22007-03-12 09:03:27 -04002227 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -04002228 if (path->nodes[i]) {
Chris Mason5f39d392007-10-15 16:14:19 -04002229 free_extent_buffer(path->nodes[i]);
Chris Mason0f827312007-10-15 16:18:56 -04002230 path->nodes[i] = NULL;
Chris Mason83e15a22007-03-12 09:03:27 -04002231 }
Chris Mason20524f02007-03-10 06:35:47 -05002232 }
Chris Mason9f3a7422007-08-07 15:52:19 -04002233out:
Chris Mason5caf2a02007-04-02 11:20:42 -04002234 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -04002235 return ret;
Chris Mason20524f02007-03-10 06:35:47 -05002236}
Chris Mason9078a3e2007-04-26 16:46:15 -04002237
Chris Masonbe744172007-05-06 10:15:01 -04002238int btrfs_free_block_groups(struct btrfs_fs_info *info)
2239{
Chris Masonf510cfe2007-10-15 16:14:48 -04002240 u64 start;
2241 u64 end;
Yanb97f9202007-11-01 11:28:41 -04002242 u64 ptr;
Chris Mason96b51792007-10-15 16:15:19 -04002243 int ret;
Chris Mason96b51792007-10-15 16:15:19 -04002244 while(1) {
2245 ret = find_first_extent_bit(&info->block_group_cache, 0,
2246 &start, &end, (unsigned int)-1);
2247 if (ret)
2248 break;
Yanb97f9202007-11-01 11:28:41 -04002249 ret = get_state_private(&info->block_group_cache, start, &ptr);
2250 if (!ret)
2251 kfree((void *)(unsigned long)ptr);
Chris Mason96b51792007-10-15 16:15:19 -04002252 clear_extent_bits(&info->block_group_cache, start,
2253 end, (unsigned int)-1, GFP_NOFS);
2254 }
Chris Masone37c9e62007-05-09 20:13:14 -04002255 while(1) {
Chris Masonf510cfe2007-10-15 16:14:48 -04002256 ret = find_first_extent_bit(&info->free_space_cache, 0,
2257 &start, &end, EXTENT_DIRTY);
2258 if (ret)
Chris Masone37c9e62007-05-09 20:13:14 -04002259 break;
Chris Masonf510cfe2007-10-15 16:14:48 -04002260 clear_extent_dirty(&info->free_space_cache, start,
2261 end, GFP_NOFS);
Chris Masone37c9e62007-05-09 20:13:14 -04002262 }
Chris Masonbe744172007-05-06 10:15:01 -04002263 return 0;
2264}
2265
Chris Mason8e7bf942008-04-28 09:02:36 -04002266static 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 Mason98ed5172008-01-03 10:01:48 -05002272static int noinline relocate_inode_pages(struct inode *inode, u64 start,
2273 u64 len)
Chris Masonedbd8d42007-12-21 16:27:24 -05002274{
2275 u64 page_start;
2276 u64 page_end;
2277 u64 delalloc_start;
2278 u64 existing_delalloc;
2279 unsigned long last_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05002280 unsigned long i;
2281 struct page *page;
Chris Masond1310b22008-01-24 16:13:08 -05002282 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason4313b392008-01-03 09:08:48 -05002283 struct file_ra_state *ra;
Chris Mason8e7bf942008-04-28 09:02:36 -04002284 unsigned long total_read = 0;
2285 unsigned long ra_pages;
Chris Mason4313b392008-01-03 09:08:48 -05002286
2287 ra = kzalloc(sizeof(*ra), GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05002288
2289 mutex_lock(&inode->i_mutex);
Chris Mason4313b392008-01-03 09:08:48 -05002290 i = start >> PAGE_CACHE_SHIFT;
Chris Masonedbd8d42007-12-21 16:27:24 -05002291 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
2292
Chris Mason8e7bf942008-04-28 09:02:36 -04002293 ra_pages = BTRFS_I(inode)->root->fs_info->bdi.ra_pages;
2294
Chris Mason4313b392008-01-03 09:08:48 -05002295 file_ra_state_init(ra, inode->i_mapping);
Chris Mason4313b392008-01-03 09:08:48 -05002296 kfree(ra);
Chris Masonedbd8d42007-12-21 16:27:24 -05002297
Chris Mason4313b392008-01-03 09:08:48 -05002298 for (; i <= last_index; i++) {
Chris Mason8e7bf942008-04-28 09:02:36 -04002299 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 Masonedbd8d42007-12-21 16:27:24 -05002304 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 Masond1310b22008-01-24 16:13:08 -05002319 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05002320
2321 delalloc_start = page_start;
Chris Masond1310b22008-01-24 16:13:08 -05002322 existing_delalloc = count_range_bits(io_tree,
2323 &delalloc_start, page_end,
2324 PAGE_CACHE_SIZE, EXTENT_DELALLOC);
Chris Masonedbd8d42007-12-21 16:27:24 -05002325
Chris Masond1310b22008-01-24 16:13:08 -05002326 set_extent_delalloc(io_tree, page_start,
Chris Masonedbd8d42007-12-21 16:27:24 -05002327 page_end, GFP_NOFS);
2328
Chris Masond1310b22008-01-24 16:13:08 -05002329 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05002330 set_page_dirty(page);
2331 unlock_page(page);
2332 page_cache_release(page);
2333 }
2334
2335out_unlock:
2336 mutex_unlock(&inode->i_mutex);
2337 return 0;
2338}
2339
Chris Mason4313b392008-01-03 09:08:48 -05002340/*
2341 * note, this releases the path
2342 */
Chris Mason98ed5172008-01-03 10:01:48 -05002343static int noinline relocate_one_reference(struct btrfs_root *extent_root,
Chris Masonedbd8d42007-12-21 16:27:24 -05002344 struct btrfs_path *path,
Chris Mason4313b392008-01-03 09:08:48 -05002345 struct btrfs_key *extent_key)
Chris Masonedbd8d42007-12-21 16:27:24 -05002346{
2347 struct inode *inode;
2348 struct btrfs_root *found_root;
Chris Mason4313b392008-01-03 09:08:48 -05002349 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 Masonedbd8d42007-12-21 16:27:24 -05002355 int ret;
2356
Chris Mason4313b392008-01-03 09:08:48 -05002357 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 Masonedbd8d42007-12-21 16:27:24 -05002367 if (ref_gen == 0)
Chris Mason4313b392008-01-03 09:08:48 -05002368 root_location->offset = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05002369 else
Chris Mason4313b392008-01-03 09:08:48 -05002370 root_location->offset = (u64)-1;
2371 root_location->type = BTRFS_ROOT_ITEM_KEY;
Chris Masonedbd8d42007-12-21 16:27:24 -05002372
2373 found_root = btrfs_read_fs_root_no_name(extent_root->fs_info,
Chris Mason4313b392008-01-03 09:08:48 -05002374 root_location);
Chris Masonedbd8d42007-12-21 16:27:24 -05002375 BUG_ON(!found_root);
Chris Mason4313b392008-01-03 09:08:48 -05002376 kfree(root_location);
Chris Masonedbd8d42007-12-21 16:27:24 -05002377
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 Mason8f662a72008-01-02 10:01:11 -05002424 path->reada = 2;
Chris Masonedbd8d42007-12-21 16:27:24 -05002425 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
2438out:
2439 return 0;
2440}
2441
Chris Mason98ed5172008-01-03 10:01:48 -05002442static int noinline relocate_one_extent(struct btrfs_root *extent_root,
2443 struct btrfs_path *path,
2444 struct btrfs_key *extent_key)
Chris Masonedbd8d42007-12-21 16:27:24 -05002445{
2446 struct btrfs_key key;
2447 struct btrfs_key found_key;
Chris Masonedbd8d42007-12-21 16:27:24 -05002448 struct extent_buffer *leaf;
Chris Masonedbd8d42007-12-21 16:27:24 -05002449 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 Masonedbd8d42007-12-21 16:27:24 -05002460 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 Mason4313b392008-01-03 09:08:48 -05002479 ret = relocate_one_reference(extent_root, path, extent_key);
Chris Masonedbd8d42007-12-21 16:27:24 -05002480 if (ret)
2481 goto out;
2482 }
2483 ret = 0;
2484out:
2485 btrfs_release_path(extent_root, path);
2486 return ret;
2487}
2488
Chris Mason8f18cf12008-04-25 16:53:30 -04002489int btrfs_shrink_extent_tree(struct btrfs_root *root, u64 shrink_start)
Chris Masonedbd8d42007-12-21 16:27:24 -05002490{
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 Mason8f18cf12008-04-25 16:53:30 -04002496 u64 shrink_last_byte;
2497 struct btrfs_block_group_cache *shrink_block_group;
Chris Masonedbd8d42007-12-21 16:27:24 -05002498 struct btrfs_fs_info *info = root->fs_info;
Chris Masonedbd8d42007-12-21 16:27:24 -05002499 struct btrfs_key key;
Yan73e48b22008-01-03 14:14:39 -05002500 struct btrfs_key found_key;
Chris Masonedbd8d42007-12-21 16:27:24 -05002501 struct extent_buffer *leaf;
2502 u32 nritems;
2503 int ret;
Chris Mason725c8462008-01-04 16:47:16 -05002504 int progress = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05002505
Chris Mason8f18cf12008-04-25 16:53:30 -04002506 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;
2514printk("shrink_extent_tree %Lu -> %Lu type %Lu\n", shrink_start, shrink_last_byte, shrink_block_group->flags);
Chris Masonedbd8d42007-12-21 16:27:24 -05002515 path = btrfs_alloc_path();
2516 root = root->fs_info->extent_root;
Chris Mason8f662a72008-01-02 10:01:11 -05002517 path->reada = 2;
Chris Masonedbd8d42007-12-21 16:27:24 -05002518
2519again:
Chris Mason8f18cf12008-04-25 16:53:30 -04002520 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 Masonedbd8d42007-12-21 16:27:24 -05002527 total_found = 0;
Chris Mason8f18cf12008-04-25 16:53:30 -04002528 key.objectid = shrink_start;
Chris Masonedbd8d42007-12-21 16:27:24 -05002529 key.offset = 0;
2530 key.type = 0;
Yan73e48b22008-01-03 14:14:39 -05002531 cur_byte = key.objectid;
Chris Mason4313b392008-01-03 09:08:48 -05002532
Yan73e48b22008-01-03 14:14:39 -05002533 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2534 if (ret < 0)
2535 goto out;
2536
Chris Mason0b86a832008-03-24 15:01:56 -04002537 ret = btrfs_previous_item(root, path, 0, BTRFS_EXTENT_ITEM_KEY);
Yan73e48b22008-01-03 14:14:39 -05002538 if (ret < 0)
2539 goto out;
Chris Mason8f18cf12008-04-25 16:53:30 -04002540
Yan73e48b22008-01-03 14:14:39 -05002541 if (ret == 0) {
2542 leaf = path->nodes[0];
2543 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04002544 if (found_key.objectid + found_key.offset > shrink_start &&
2545 found_key.objectid < shrink_last_byte) {
Yan73e48b22008-01-03 14:14:39 -05002546 cur_byte = found_key.objectid;
2547 key.objectid = cur_byte;
2548 }
2549 }
2550 btrfs_release_path(root, path);
2551
2552 while(1) {
Chris Masonedbd8d42007-12-21 16:27:24 -05002553 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2554 if (ret < 0)
2555 goto out;
Yan73e48b22008-01-03 14:14:39 -05002556
Chris Masonedbd8d42007-12-21 16:27:24 -05002557 leaf = path->nodes[0];
Chris Masonedbd8d42007-12-21 16:27:24 -05002558 nritems = btrfs_header_nritems(leaf);
Yan73e48b22008-01-03 14:14:39 -05002559next:
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 Masonedbd8d42007-12-21 16:27:24 -05002567 }
Yan73e48b22008-01-03 14:14:39 -05002568 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 Mason725c8462008-01-04 16:47:16 -05002573
Chris Mason8f18cf12008-04-25 16:53:30 -04002574 if (found_key.objectid >= shrink_last_byte)
2575 break;
2576
Chris Mason725c8462008-01-04 16:47:16 -05002577 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
Yan73e48b22008-01-03 14:14:39 -05002589 if (btrfs_key_type(&found_key) != BTRFS_EXTENT_ITEM_KEY ||
2590 found_key.objectid + found_key.offset <= cur_byte) {
2591 path->slots[0]++;
Chris Masonedbd8d42007-12-21 16:27:24 -05002592 goto next;
2593 }
Yan73e48b22008-01-03 14:14:39 -05002594
Chris Masonedbd8d42007-12-21 16:27:24 -05002595 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 Mason8f18cf12008-04-25 16:53:30 -04002617 /*
2618 * we've freed all the extents, now remove the block
2619 * group item from the tree
2620 */
Chris Masonedbd8d42007-12-21 16:27:24 -05002621 trans = btrfs_start_transaction(root, 1);
Chris Mason8f18cf12008-04-25 16:53:30 -04002622 memcpy(&key, &shrink_block_group->key, sizeof(key));
Chris Mason4313b392008-01-03 09:08:48 -05002623
Chris Mason8f18cf12008-04-25 16:53:30 -04002624 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2625 if (ret > 0)
2626 ret = -EIO;
2627 if (ret < 0)
2628 goto out;
Yan73e48b22008-01-03 14:14:39 -05002629
Chris Mason8f18cf12008-04-25 16:53:30 -04002630 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 Mason1372f8e2008-01-04 09:34:54 -05002634
Chris Mason8f18cf12008-04-25 16:53:30 -04002635 clear_extent_bits(&info->block_group_cache, found_key.objectid,
2636 found_key.objectid + found_key.offset - 1,
2637 (unsigned int)-1, GFP_NOFS);
Yan73e48b22008-01-03 14:14:39 -05002638
Chris Mason8f18cf12008-04-25 16:53:30 -04002639 btrfs_del_item(trans, root, path);
2640 clear_extent_dirty(&info->free_space_cache,
2641 shrink_start, shrink_last_byte - 1,
Chris Masonedbd8d42007-12-21 16:27:24 -05002642 GFP_NOFS);
2643 btrfs_commit_transaction(trans, root);
2644out:
2645 btrfs_free_path(path);
2646 return ret;
2647}
2648
Chris Mason0b86a832008-03-24 15:01:56 -04002649int 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;
2679error:
2680 return ret;
2681}
2682
Chris Mason9078a3e2007-04-26 16:46:15 -04002683int btrfs_read_block_groups(struct btrfs_root *root)
2684{
2685 struct btrfs_path *path;
2686 int ret;
Chris Mason96b51792007-10-15 16:15:19 -04002687 int bit;
Chris Mason9078a3e2007-04-26 16:46:15 -04002688 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04002689 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04002690 struct btrfs_space_info *space_info;
Chris Masond1310b22008-01-24 16:13:08 -05002691 struct extent_io_tree *block_group_cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04002692 struct btrfs_key key;
2693 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04002694 struct extent_buffer *leaf;
Chris Mason96b51792007-10-15 16:15:19 -04002695
2696 block_group_cache = &info->block_group_cache;
Chris Masonbe744172007-05-06 10:15:01 -04002697 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04002698 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04002699 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002700 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04002701 path = btrfs_alloc_path();
2702 if (!path)
2703 return -ENOMEM;
2704
2705 while(1) {
Chris Mason0b86a832008-03-24 15:01:56 -04002706 ret = find_first_block_group(root, path, &key);
2707 if (ret > 0) {
2708 ret = 0;
2709 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04002710 }
Chris Mason0b86a832008-03-24 15:01:56 -04002711 if (ret != 0)
2712 goto error;
2713
Chris Mason5f39d392007-10-15 16:14:19 -04002714 leaf = path->nodes[0];
2715 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04002716 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04002717 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04002718 ret = -ENOMEM;
Chris Mason9078a3e2007-04-26 16:46:15 -04002719 break;
2720 }
Chris Mason3e1ad542007-05-07 20:03:49 -04002721
Chris Mason5f39d392007-10-15 16:14:19 -04002722 read_extent_buffer(leaf, &cache->item,
2723 btrfs_item_ptr_offset(leaf, path->slots[0]),
2724 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04002725 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04002726
Chris Mason9078a3e2007-04-26 16:46:15 -04002727 key.objectid = found_key.objectid + found_key.offset;
2728 btrfs_release_path(root, path);
Chris Mason0b86a832008-03-24 15:01:56 -04002729 cache->flags = btrfs_block_group_flags(&cache->item);
2730 bit = 0;
2731 if (cache->flags & BTRFS_BLOCK_GROUP_DATA) {
Chris Mason96b51792007-10-15 16:15:19 -04002732 bit = BLOCK_GROUP_DATA;
Chris Mason0b86a832008-03-24 15:01:56 -04002733 } else if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) {
2734 bit = BLOCK_GROUP_SYSTEM;
2735 } else if (cache->flags & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason96b51792007-10-15 16:15:19 -04002736 bit = BLOCK_GROUP_METADATA;
Chris Mason31f3c992007-04-30 15:25:45 -04002737 }
Chris Mason8790d502008-04-03 16:29:03 -04002738 set_avail_alloc_bits(info, cache->flags);
Chris Mason96b51792007-10-15 16:15:19 -04002739
Chris Mason6324fbf2008-03-24 15:01:59 -04002740 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 Mason96b51792007-10-15 16:15:19 -04002746 /* 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 Axboeae2f5412007-10-19 09:22:59 -04002751 (unsigned long)cache);
Chris Mason96b51792007-10-15 16:15:19 -04002752
Chris Mason9078a3e2007-04-26 16:46:15 -04002753 if (key.objectid >=
Chris Masondb945352007-10-15 16:15:53 -04002754 btrfs_super_total_bytes(&info->super_copy))
Chris Mason9078a3e2007-04-26 16:46:15 -04002755 break;
2756 }
Chris Mason0b86a832008-03-24 15:01:56 -04002757 ret = 0;
2758error:
Chris Mason9078a3e2007-04-26 16:46:15 -04002759 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04002760 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04002761}
Chris Mason6324fbf2008-03-24 15:01:59 -04002762
2763int btrfs_make_block_group(struct btrfs_trans_handle *trans,
2764 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04002765 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04002766 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 Mason8f18cf12008-04-25 16:53:30 -04002777 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason6324fbf2008-03-24 15:01:59 -04002778 BUG_ON(!cache);
Chris Masone17cade2008-04-15 15:41:47 -04002779 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04002780 cache->key.offset = size;
Chris Masone17cade2008-04-15 15:41:47 -04002781
Chris Mason6324fbf2008-03-24 15:01:59 -04002782 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 Mason6324fbf2008-03-24 15:01:59 -04002785 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 Masond18a2c42008-04-04 15:40:00 -04002793 bit = block_group_state_bits(type);
Chris Masone17cade2008-04-15 15:41:47 -04002794 set_extent_bits(block_group_cache, chunk_offset,
2795 chunk_offset + size - 1,
Chris Mason6324fbf2008-03-24 15:01:59 -04002796 bit | EXTENT_LOCKED, GFP_NOFS);
Chris Mason6324fbf2008-03-24 15:01:59 -04002797
Chris Masone17cade2008-04-15 15:41:47 -04002798 set_state_private(block_group_cache, chunk_offset,
2799 (unsigned long)cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04002800 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 Masond18a2c42008-04-04 15:40:00 -04002807 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason6324fbf2008-03-24 15:01:59 -04002808 return 0;
2809}