blob: 22820f91d2b01247a353f06c2f59c84f348a80a2 [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 Masonec44a352008-04-28 15:29:52 -040020#include <linux/writeback.h>
David Woodhouse21af8042008-08-12 14:13:26 +010021#include <linux/blkdev.h>
Chris Mason74493f72007-12-11 09:25:06 -050022#include "hash.h"
Miguela5eb62e2008-04-11 15:45:51 -040023#include "crc32c.h"
Chris Masonfec577f2007-02-26 10:40:21 -050024#include "ctree.h"
25#include "disk-io.h"
26#include "print-tree.h"
Chris Masone089f052007-03-16 16:20:31 -040027#include "transaction.h"
Chris Mason0b86a832008-03-24 15:01:56 -040028#include "volumes.h"
Chris Mason925baed2008-06-25 16:01:30 -040029#include "locking.h"
Yan Zheng31153d82008-07-28 15:32:19 -040030#include "ref-cache.h"
Chris Masonfec577f2007-02-26 10:40:21 -050031
Zheng Yan31840ae2008-09-23 13:14:14 -040032#define PENDING_EXTENT_INSERT 0
33#define PENDING_EXTENT_DELETE 1
34#define PENDING_BACKREF_UPDATE 2
35
36struct pending_extent_op {
37 int type;
38 u64 bytenr;
39 u64 num_bytes;
40 u64 parent;
41 u64 orig_parent;
42 u64 generation;
43 u64 orig_generation;
44 int level;
45};
46
Chris Masone089f052007-03-16 16:20:31 -040047static int finish_current_insert(struct btrfs_trans_handle *trans, struct
Chris Mason87ef2bb2008-10-30 11:23:27 -040048 btrfs_root *extent_root, int all);
Chris Masone20d96d2007-03-22 12:13:20 -040049static int del_pending_extents(struct btrfs_trans_handle *trans, struct
Chris Mason87ef2bb2008-10-30 11:23:27 -040050 btrfs_root *extent_root, int all);
Chris Mason925baed2008-06-25 16:01:30 -040051static struct btrfs_block_group_cache *
52__btrfs_find_block_group(struct btrfs_root *root,
53 struct btrfs_block_group_cache *hint,
54 u64 search_start, int data, int owner);
Yand548ee52008-01-03 13:56:30 -050055
Josef Bacik0f9dd462008-09-23 13:14:11 -040056static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
57{
58 return (cache->flags & bits) == bits;
59}
60
61/*
62 * this adds the block group to the fs_info rb tree for the block group
63 * cache
64 */
65int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
66 struct btrfs_block_group_cache *block_group)
67{
68 struct rb_node **p;
69 struct rb_node *parent = NULL;
70 struct btrfs_block_group_cache *cache;
71
72 spin_lock(&info->block_group_cache_lock);
73 p = &info->block_group_cache_tree.rb_node;
74
75 while (*p) {
76 parent = *p;
77 cache = rb_entry(parent, struct btrfs_block_group_cache,
78 cache_node);
79 if (block_group->key.objectid < cache->key.objectid) {
80 p = &(*p)->rb_left;
81 } else if (block_group->key.objectid > cache->key.objectid) {
82 p = &(*p)->rb_right;
83 } else {
84 spin_unlock(&info->block_group_cache_lock);
85 return -EEXIST;
86 }
87 }
88
89 rb_link_node(&block_group->cache_node, parent, p);
90 rb_insert_color(&block_group->cache_node,
91 &info->block_group_cache_tree);
92 spin_unlock(&info->block_group_cache_lock);
93
94 return 0;
95}
96
97/*
98 * This will return the block group at or after bytenr if contains is 0, else
99 * it will return the block group that contains the bytenr
100 */
101static struct btrfs_block_group_cache *
102block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
103 int contains)
104{
105 struct btrfs_block_group_cache *cache, *ret = NULL;
106 struct rb_node *n;
107 u64 end, start;
108
109 spin_lock(&info->block_group_cache_lock);
110 n = info->block_group_cache_tree.rb_node;
111
112 while (n) {
113 cache = rb_entry(n, struct btrfs_block_group_cache,
114 cache_node);
115 end = cache->key.objectid + cache->key.offset - 1;
116 start = cache->key.objectid;
117
118 if (bytenr < start) {
119 if (!contains && (!ret || start < ret->key.objectid))
120 ret = cache;
121 n = n->rb_left;
122 } else if (bytenr > start) {
123 if (contains && bytenr <= end) {
124 ret = cache;
125 break;
126 }
127 n = n->rb_right;
128 } else {
129 ret = cache;
130 break;
131 }
132 }
133 spin_unlock(&info->block_group_cache_lock);
134
135 return ret;
136}
137
138/*
139 * this is only called by cache_block_group, since we could have freed extents
140 * we need to check the pinned_extents for any extents that can't be used yet
141 * since their free space will be released as soon as the transaction commits.
142 */
143static int add_new_free_space(struct btrfs_block_group_cache *block_group,
144 struct btrfs_fs_info *info, u64 start, u64 end)
145{
146 u64 extent_start, extent_end, size;
147 int ret;
148
Josef Bacik25179202008-10-29 14:49:05 -0400149 mutex_lock(&info->pinned_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400150 while (start < end) {
151 ret = find_first_extent_bit(&info->pinned_extents, start,
152 &extent_start, &extent_end,
153 EXTENT_DIRTY);
154 if (ret)
155 break;
156
157 if (extent_start == start) {
158 start = extent_end + 1;
159 } else if (extent_start > start && extent_start < end) {
160 size = extent_start - start;
Josef Bacik25179202008-10-29 14:49:05 -0400161 ret = btrfs_add_free_space_lock(block_group, start,
162 size);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400163 BUG_ON(ret);
164 start = extent_end + 1;
165 } else {
166 break;
167 }
168 }
169
170 if (start < end) {
171 size = end - start;
Josef Bacik25179202008-10-29 14:49:05 -0400172 ret = btrfs_add_free_space_lock(block_group, start, size);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400173 BUG_ON(ret);
174 }
Josef Bacik25179202008-10-29 14:49:05 -0400175 mutex_unlock(&info->pinned_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400176
177 return 0;
178}
179
Chris Masone37c9e62007-05-09 20:13:14 -0400180static int cache_block_group(struct btrfs_root *root,
181 struct btrfs_block_group_cache *block_group)
182{
183 struct btrfs_path *path;
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400184 int ret = 0;
Chris Masone37c9e62007-05-09 20:13:14 -0400185 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400186 struct extent_buffer *leaf;
Chris Masone37c9e62007-05-09 20:13:14 -0400187 int slot;
Chris Masone37c9e62007-05-09 20:13:14 -0400188 u64 last = 0;
Yan7d7d6062007-09-14 16:15:28 -0400189 u64 first_free;
Chris Masone37c9e62007-05-09 20:13:14 -0400190 int found = 0;
191
Chris Mason00f5c792007-11-30 10:09:33 -0500192 if (!block_group)
193 return 0;
194
Chris Masone37c9e62007-05-09 20:13:14 -0400195 root = root->fs_info->extent_root;
Chris Masone37c9e62007-05-09 20:13:14 -0400196
197 if (block_group->cached)
198 return 0;
Chris Masonf510cfe2007-10-15 16:14:48 -0400199
Chris Masone37c9e62007-05-09 20:13:14 -0400200 path = btrfs_alloc_path();
201 if (!path)
202 return -ENOMEM;
Yan7d7d6062007-09-14 16:15:28 -0400203
Chris Mason2cc58cf2007-08-27 16:49:44 -0400204 path->reada = 2;
Chris Mason5cd57b22008-06-25 16:01:30 -0400205 /*
206 * we get into deadlocks with paths held by callers of this function.
207 * since the alloc_mutex is protecting things right now, just
208 * skip the locking here
209 */
210 path->skip_locking = 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400211 first_free = max_t(u64, block_group->key.objectid,
212 BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE);
Chris Masone37c9e62007-05-09 20:13:14 -0400213 key.objectid = block_group->key.objectid;
Chris Masone37c9e62007-05-09 20:13:14 -0400214 key.offset = 0;
215 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
216 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
217 if (ret < 0)
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400218 goto err;
Chris Mason0b86a832008-03-24 15:01:56 -0400219 ret = btrfs_previous_item(root, path, 0, BTRFS_EXTENT_ITEM_KEY);
Yand548ee52008-01-03 13:56:30 -0500220 if (ret < 0)
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400221 goto err;
Yand548ee52008-01-03 13:56:30 -0500222 if (ret == 0) {
223 leaf = path->nodes[0];
224 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
225 if (key.objectid + key.offset > first_free)
226 first_free = key.objectid + key.offset;
227 }
Chris Masone37c9e62007-05-09 20:13:14 -0400228 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400229 leaf = path->nodes[0];
Chris Masone37c9e62007-05-09 20:13:14 -0400230 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -0400231 if (slot >= btrfs_header_nritems(leaf)) {
Chris Masone37c9e62007-05-09 20:13:14 -0400232 ret = btrfs_next_leaf(root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400233 if (ret < 0)
234 goto err;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400235 if (ret == 0)
Chris Masone37c9e62007-05-09 20:13:14 -0400236 continue;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400237 else
Chris Masone37c9e62007-05-09 20:13:14 -0400238 break;
Chris Masone37c9e62007-05-09 20:13:14 -0400239 }
Chris Mason5f39d392007-10-15 16:14:19 -0400240 btrfs_item_key_to_cpu(leaf, &key, slot);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400241 if (key.objectid < block_group->key.objectid)
Yan7d7d6062007-09-14 16:15:28 -0400242 goto next;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400243
Chris Masone37c9e62007-05-09 20:13:14 -0400244 if (key.objectid >= block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -0400245 block_group->key.offset)
Yan7d7d6062007-09-14 16:15:28 -0400246 break;
Yan7d7d6062007-09-14 16:15:28 -0400247
248 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
249 if (!found) {
250 last = first_free;
251 found = 1;
Chris Masone37c9e62007-05-09 20:13:14 -0400252 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400253
254 add_new_free_space(block_group, root->fs_info, last,
255 key.objectid);
256
Yan7d7d6062007-09-14 16:15:28 -0400257 last = key.objectid + key.offset;
Chris Masone37c9e62007-05-09 20:13:14 -0400258 }
Yan7d7d6062007-09-14 16:15:28 -0400259next:
Chris Masone37c9e62007-05-09 20:13:14 -0400260 path->slots[0]++;
261 }
262
Yan7d7d6062007-09-14 16:15:28 -0400263 if (!found)
264 last = first_free;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400265
266 add_new_free_space(block_group, root->fs_info, last,
267 block_group->key.objectid +
268 block_group->key.offset);
269
Chris Masone37c9e62007-05-09 20:13:14 -0400270 block_group->cached = 1;
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400271 ret = 0;
Chris Mason54aa1f42007-06-22 14:16:25 -0400272err:
Chris Masone37c9e62007-05-09 20:13:14 -0400273 btrfs_free_path(path);
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400274 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -0400275}
276
Josef Bacik0f9dd462008-09-23 13:14:11 -0400277/*
278 * return the block group that starts at or after bytenr
279 */
Chris Mason0ef3e662008-05-24 14:04:53 -0400280struct btrfs_block_group_cache *btrfs_lookup_first_block_group(struct
281 btrfs_fs_info *info,
282 u64 bytenr)
283{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400284 struct btrfs_block_group_cache *cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400285
Josef Bacik0f9dd462008-09-23 13:14:11 -0400286 cache = block_group_cache_tree_search(info, bytenr, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -0400287
Josef Bacik0f9dd462008-09-23 13:14:11 -0400288 return cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400289}
290
Josef Bacik0f9dd462008-09-23 13:14:11 -0400291/*
292 * return the block group that contains teh given bytenr
293 */
Chris Mason5276aed2007-06-11 21:33:38 -0400294struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
295 btrfs_fs_info *info,
Chris Masondb945352007-10-15 16:15:53 -0400296 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400297{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400298 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -0400299
Josef Bacik0f9dd462008-09-23 13:14:11 -0400300 cache = block_group_cache_tree_search(info, bytenr, 1);
Chris Mason96b51792007-10-15 16:15:19 -0400301
Josef Bacik0f9dd462008-09-23 13:14:11 -0400302 return cache;
Chris Masonbe744172007-05-06 10:15:01 -0400303}
Chris Mason0b86a832008-03-24 15:01:56 -0400304
Josef Bacik0f9dd462008-09-23 13:14:11 -0400305static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
306 u64 flags)
Chris Mason6324fbf2008-03-24 15:01:59 -0400307{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400308 struct list_head *head = &info->space_info;
309 struct list_head *cur;
310 struct btrfs_space_info *found;
311 list_for_each(cur, head) {
312 found = list_entry(cur, struct btrfs_space_info, list);
313 if (found->flags == flags)
314 return found;
315 }
316 return NULL;
Chris Mason6324fbf2008-03-24 15:01:59 -0400317}
318
Josef Bacik80eb2342008-10-29 14:49:05 -0400319static u64 div_factor(u64 num, int factor)
320{
321 if (factor == 10)
322 return num;
323 num *= factor;
324 do_div(num, 10);
325 return num;
326}
327
Chris Mason925baed2008-06-25 16:01:30 -0400328static struct btrfs_block_group_cache *
329__btrfs_find_block_group(struct btrfs_root *root,
330 struct btrfs_block_group_cache *hint,
331 u64 search_start, int data, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400332{
Chris Mason96b51792007-10-15 16:15:19 -0400333 struct btrfs_block_group_cache *cache;
Chris Mason31f3c992007-04-30 15:25:45 -0400334 struct btrfs_block_group_cache *found_group = NULL;
Chris Masoncd1bc462007-04-27 10:08:34 -0400335 struct btrfs_fs_info *info = root->fs_info;
336 u64 used;
Chris Mason31f3c992007-04-30 15:25:45 -0400337 u64 last = 0;
Chris Mason96b51792007-10-15 16:15:19 -0400338 u64 free_check;
Chris Mason31f3c992007-04-30 15:25:45 -0400339 int full_search = 0;
Chris Masonbce4eae2008-04-24 14:42:46 -0400340 int factor = 10;
Chris Mason0ef3e662008-05-24 14:04:53 -0400341 int wrapped = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400342
Chris Masona236aed2008-04-29 09:38:00 -0400343 if (data & BTRFS_BLOCK_GROUP_METADATA)
344 factor = 9;
Chris Masonbe744172007-05-06 10:15:01 -0400345
Chris Mason0ef3e662008-05-24 14:04:53 -0400346 if (search_start) {
Chris Masonbe744172007-05-06 10:15:01 -0400347 struct btrfs_block_group_cache *shint;
Chris Mason0ef3e662008-05-24 14:04:53 -0400348 shint = btrfs_lookup_first_block_group(info, search_start);
Chris Mason8f18cf12008-04-25 16:53:30 -0400349 if (shint && block_group_bits(shint, data) && !shint->ro) {
Chris Masonc286ac42008-07-22 23:06:41 -0400350 spin_lock(&shint->lock);
Chris Masonbe744172007-05-06 10:15:01 -0400351 used = btrfs_block_group_used(&shint->item);
Zheng Yane8569812008-09-26 10:05:48 -0400352 if (used + shint->pinned + shint->reserved <
Yan324ae4d2007-11-16 14:57:08 -0500353 div_factor(shint->key.offset, factor)) {
Chris Masonc286ac42008-07-22 23:06:41 -0400354 spin_unlock(&shint->lock);
Chris Masonbe744172007-05-06 10:15:01 -0400355 return shint;
356 }
Chris Masonc286ac42008-07-22 23:06:41 -0400357 spin_unlock(&shint->lock);
Chris Masonbe744172007-05-06 10:15:01 -0400358 }
359 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400360 if (hint && !hint->ro && block_group_bits(hint, data)) {
Chris Masonc286ac42008-07-22 23:06:41 -0400361 spin_lock(&hint->lock);
Chris Mason31f3c992007-04-30 15:25:45 -0400362 used = btrfs_block_group_used(&hint->item);
Zheng Yane8569812008-09-26 10:05:48 -0400363 if (used + hint->pinned + hint->reserved <
Yan324ae4d2007-11-16 14:57:08 -0500364 div_factor(hint->key.offset, factor)) {
Chris Masonc286ac42008-07-22 23:06:41 -0400365 spin_unlock(&hint->lock);
Chris Mason31f3c992007-04-30 15:25:45 -0400366 return hint;
367 }
Chris Masonc286ac42008-07-22 23:06:41 -0400368 spin_unlock(&hint->lock);
Chris Masone19caa52007-10-15 16:17:44 -0400369 last = hint->key.objectid + hint->key.offset;
Chris Mason31f3c992007-04-30 15:25:45 -0400370 } else {
Chris Masone37c9e62007-05-09 20:13:14 -0400371 if (hint)
Chris Mason0ef3e662008-05-24 14:04:53 -0400372 last = max(hint->key.objectid, search_start);
Chris Masone37c9e62007-05-09 20:13:14 -0400373 else
Chris Mason0ef3e662008-05-24 14:04:53 -0400374 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400375 }
Chris Mason31f3c992007-04-30 15:25:45 -0400376again:
Zheng Yane8569812008-09-26 10:05:48 -0400377 while (1) {
378 cache = btrfs_lookup_first_block_group(root->fs_info, last);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400379 if (!cache)
Chris Masoncd1bc462007-04-27 10:08:34 -0400380 break;
Chris Mason96b51792007-10-15 16:15:19 -0400381
Chris Masonc286ac42008-07-22 23:06:41 -0400382 spin_lock(&cache->lock);
Chris Mason96b51792007-10-15 16:15:19 -0400383 last = cache->key.objectid + cache->key.offset;
384 used = btrfs_block_group_used(&cache->item);
385
Chris Mason8f18cf12008-04-25 16:53:30 -0400386 if (!cache->ro && block_group_bits(cache, data)) {
Chris Mason0ef3e662008-05-24 14:04:53 -0400387 free_check = div_factor(cache->key.offset, factor);
Zheng Yane8569812008-09-26 10:05:48 -0400388 if (used + cache->pinned + cache->reserved <
389 free_check) {
Chris Mason8790d502008-04-03 16:29:03 -0400390 found_group = cache;
Chris Masonc286ac42008-07-22 23:06:41 -0400391 spin_unlock(&cache->lock);
Chris Mason8790d502008-04-03 16:29:03 -0400392 goto found;
393 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400394 }
Chris Masonc286ac42008-07-22 23:06:41 -0400395 spin_unlock(&cache->lock);
Chris Masonde428b62007-05-18 13:28:27 -0400396 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400397 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400398 if (!wrapped) {
399 last = search_start;
400 wrapped = 1;
401 goto again;
402 }
403 if (!full_search && factor < 10) {
Chris Masonbe744172007-05-06 10:15:01 -0400404 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400405 full_search = 1;
Chris Mason0ef3e662008-05-24 14:04:53 -0400406 factor = 10;
Chris Mason31f3c992007-04-30 15:25:45 -0400407 goto again;
408 }
Chris Masonbe744172007-05-06 10:15:01 -0400409found:
Chris Mason31f3c992007-04-30 15:25:45 -0400410 return found_group;
Chris Masoncd1bc462007-04-27 10:08:34 -0400411}
412
Chris Mason925baed2008-06-25 16:01:30 -0400413struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
414 struct btrfs_block_group_cache
415 *hint, u64 search_start,
416 int data, int owner)
417{
418
419 struct btrfs_block_group_cache *ret;
Chris Mason925baed2008-06-25 16:01:30 -0400420 ret = __btrfs_find_block_group(root, hint, search_start, data, owner);
Chris Mason925baed2008-06-25 16:01:30 -0400421 return ret;
422}
Josef Bacik0f9dd462008-09-23 13:14:11 -0400423
Chris Masone02119d2008-09-05 16:13:11 -0400424/* simple helper to search for an existing extent at a given offset */
Zheng Yan31840ae2008-09-23 13:14:14 -0400425int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
Chris Masone02119d2008-09-05 16:13:11 -0400426{
427 int ret;
428 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400429 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -0400430
Zheng Yan31840ae2008-09-23 13:14:14 -0400431 path = btrfs_alloc_path();
432 BUG_ON(!path);
Chris Masone02119d2008-09-05 16:13:11 -0400433 key.objectid = start;
434 key.offset = len;
435 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
436 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
437 0, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400438 btrfs_free_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500439 return ret;
440}
441
Chris Masond8d5f3e2007-12-11 12:42:00 -0500442/*
443 * Back reference rules. Back refs have three main goals:
444 *
445 * 1) differentiate between all holders of references to an extent so that
446 * when a reference is dropped we can make sure it was a valid reference
447 * before freeing the extent.
448 *
449 * 2) Provide enough information to quickly find the holders of an extent
450 * if we notice a given block is corrupted or bad.
451 *
452 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
453 * maintenance. This is actually the same as #2, but with a slightly
454 * different use case.
455 *
456 * File extents can be referenced by:
457 *
458 * - multiple snapshots, subvolumes, or different generations in one subvol
Zheng Yan31840ae2008-09-23 13:14:14 -0400459 * - different files inside a single subvolume
Chris Masond8d5f3e2007-12-11 12:42:00 -0500460 * - different offsets inside a file (bookend extents in file.c)
461 *
462 * The extent ref structure has fields for:
463 *
464 * - Objectid of the subvolume root
465 * - Generation number of the tree holding the reference
466 * - objectid of the file holding the reference
Zheng Yan31840ae2008-09-23 13:14:14 -0400467 * - number of references holding by parent node (alway 1 for tree blocks)
468 *
469 * Btree leaf may hold multiple references to a file extent. In most cases,
470 * these references are from same file and the corresponding offsets inside
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400471 * the file are close together.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500472 *
473 * When a file extent is allocated the fields are filled in:
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400474 * (root_key.objectid, trans->transid, inode objectid, 1)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500475 *
476 * When a leaf is cow'd new references are added for every file extent found
Zheng Yan31840ae2008-09-23 13:14:14 -0400477 * in the leaf. It looks similar to the create case, but trans->transid will
478 * be different when the block is cow'd.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500479 *
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400480 * (root_key.objectid, trans->transid, inode objectid,
Zheng Yan31840ae2008-09-23 13:14:14 -0400481 * number of references in the leaf)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500482 *
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400483 * When a file extent is removed either during snapshot deletion or
484 * file truncation, we find the corresponding back reference and check
485 * the following fields:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500486 *
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400487 * (btrfs_header_owner(leaf), btrfs_header_generation(leaf),
488 * inode objectid)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500489 *
490 * Btree extents can be referenced by:
491 *
492 * - Different subvolumes
493 * - Different generations of the same subvolume
494 *
Chris Masond8d5f3e2007-12-11 12:42:00 -0500495 * When a tree block is created, back references are inserted:
496 *
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400497 * (root->root_key.objectid, trans->transid, level, 1)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500498 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400499 * When a tree block is cow'd, new back references are added for all the
500 * blocks it points to. If the tree block isn't in reference counted root,
501 * the old back references are removed. These new back references are of
502 * the form (trans->transid will have increased since creation):
Chris Masond8d5f3e2007-12-11 12:42:00 -0500503 *
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400504 * (root->root_key.objectid, trans->transid, level, 1)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500505 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400506 * When a backref is in deleting, the following fields are checked:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500507 *
508 * if backref was for a tree root:
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400509 * (btrfs_header_owner(itself), btrfs_header_generation(itself), level)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500510 * else
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400511 * (btrfs_header_owner(parent), btrfs_header_generation(parent), level)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500512 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400513 * Back Reference Key composing:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500514 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400515 * The key objectid corresponds to the first byte in the extent, the key
516 * type is set to BTRFS_EXTENT_REF_KEY, and the key offset is the first
517 * byte of parent extent. If a extent is tree root, the key offset is set
518 * to the key objectid.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500519 */
Zheng Yan31840ae2008-09-23 13:14:14 -0400520
521static int noinline lookup_extent_backref(struct btrfs_trans_handle *trans,
522 struct btrfs_root *root,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400523 struct btrfs_path *path,
524 u64 bytenr, u64 parent,
525 u64 ref_root, u64 ref_generation,
526 u64 owner_objectid, int del)
Chris Mason74493f72007-12-11 09:25:06 -0500527{
Chris Mason74493f72007-12-11 09:25:06 -0500528 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400529 struct btrfs_extent_ref *ref;
530 struct extent_buffer *leaf;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400531 u64 ref_objectid;
Chris Mason74493f72007-12-11 09:25:06 -0500532 int ret;
533
Chris Mason74493f72007-12-11 09:25:06 -0500534 key.objectid = bytenr;
535 key.type = BTRFS_EXTENT_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -0400536 key.offset = parent;
Chris Mason74493f72007-12-11 09:25:06 -0500537
Zheng Yan31840ae2008-09-23 13:14:14 -0400538 ret = btrfs_search_slot(trans, root, &key, path, del ? -1 : 0, 1);
539 if (ret < 0)
Chris Mason7bb86312007-12-11 09:25:06 -0500540 goto out;
Zheng Yan31840ae2008-09-23 13:14:14 -0400541 if (ret > 0) {
542 ret = -ENOENT;
543 goto out;
544 }
545
546 leaf = path->nodes[0];
547 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400548 ref_objectid = btrfs_ref_objectid(leaf, ref);
Zheng Yan31840ae2008-09-23 13:14:14 -0400549 if (btrfs_ref_root(leaf, ref) != ref_root ||
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400550 btrfs_ref_generation(leaf, ref) != ref_generation ||
551 (ref_objectid != owner_objectid &&
552 ref_objectid != BTRFS_MULTIPLE_OBJECTIDS)) {
Zheng Yan31840ae2008-09-23 13:14:14 -0400553 ret = -EIO;
554 WARN_ON(1);
555 goto out;
556 }
557 ret = 0;
558out:
559 return ret;
560}
561
562static int noinline insert_extent_backref(struct btrfs_trans_handle *trans,
563 struct btrfs_root *root,
564 struct btrfs_path *path,
565 u64 bytenr, u64 parent,
566 u64 ref_root, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400567 u64 owner_objectid)
Zheng Yan31840ae2008-09-23 13:14:14 -0400568{
569 struct btrfs_key key;
570 struct extent_buffer *leaf;
571 struct btrfs_extent_ref *ref;
572 u32 num_refs;
573 int ret;
574
575 key.objectid = bytenr;
576 key.type = BTRFS_EXTENT_REF_KEY;
577 key.offset = parent;
578
579 ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(*ref));
580 if (ret == 0) {
581 leaf = path->nodes[0];
582 ref = btrfs_item_ptr(leaf, path->slots[0],
583 struct btrfs_extent_ref);
584 btrfs_set_ref_root(leaf, ref, ref_root);
585 btrfs_set_ref_generation(leaf, ref, ref_generation);
586 btrfs_set_ref_objectid(leaf, ref, owner_objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -0400587 btrfs_set_ref_num_refs(leaf, ref, 1);
588 } else if (ret == -EEXIST) {
589 u64 existing_owner;
590 BUG_ON(owner_objectid < BTRFS_FIRST_FREE_OBJECTID);
591 leaf = path->nodes[0];
592 ref = btrfs_item_ptr(leaf, path->slots[0],
593 struct btrfs_extent_ref);
594 if (btrfs_ref_root(leaf, ref) != ref_root ||
595 btrfs_ref_generation(leaf, ref) != ref_generation) {
596 ret = -EIO;
597 WARN_ON(1);
598 goto out;
599 }
600
601 num_refs = btrfs_ref_num_refs(leaf, ref);
602 BUG_ON(num_refs == 0);
603 btrfs_set_ref_num_refs(leaf, ref, num_refs + 1);
604
605 existing_owner = btrfs_ref_objectid(leaf, ref);
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400606 if (existing_owner != owner_objectid &&
607 existing_owner != BTRFS_MULTIPLE_OBJECTIDS) {
Zheng Yan31840ae2008-09-23 13:14:14 -0400608 btrfs_set_ref_objectid(leaf, ref,
609 BTRFS_MULTIPLE_OBJECTIDS);
Zheng Yan31840ae2008-09-23 13:14:14 -0400610 }
611 ret = 0;
612 } else {
613 goto out;
614 }
Chris Mason7bb86312007-12-11 09:25:06 -0500615 btrfs_mark_buffer_dirty(path->nodes[0]);
616out:
617 btrfs_release_path(root, path);
618 return ret;
Chris Mason74493f72007-12-11 09:25:06 -0500619}
620
Zheng Yan31840ae2008-09-23 13:14:14 -0400621static int noinline remove_extent_backref(struct btrfs_trans_handle *trans,
622 struct btrfs_root *root,
623 struct btrfs_path *path)
624{
625 struct extent_buffer *leaf;
626 struct btrfs_extent_ref *ref;
627 u32 num_refs;
628 int ret = 0;
629
630 leaf = path->nodes[0];
631 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
632 num_refs = btrfs_ref_num_refs(leaf, ref);
633 BUG_ON(num_refs == 0);
634 num_refs -= 1;
635 if (num_refs == 0) {
636 ret = btrfs_del_item(trans, root, path);
637 } else {
638 btrfs_set_ref_num_refs(leaf, ref, num_refs);
639 btrfs_mark_buffer_dirty(leaf);
640 }
641 btrfs_release_path(root, path);
642 return ret;
643}
644
645static int __btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
646 struct btrfs_root *root, u64 bytenr,
647 u64 orig_parent, u64 parent,
648 u64 orig_root, u64 ref_root,
649 u64 orig_generation, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400650 u64 owner_objectid)
Zheng Yan31840ae2008-09-23 13:14:14 -0400651{
652 int ret;
653 struct btrfs_root *extent_root = root->fs_info->extent_root;
654 struct btrfs_path *path;
655
656 if (root == root->fs_info->extent_root) {
657 struct pending_extent_op *extent_op;
658 u64 num_bytes;
659
660 BUG_ON(owner_objectid >= BTRFS_MAX_LEVEL);
661 num_bytes = btrfs_level_size(root, (int)owner_objectid);
Josef Bacik25179202008-10-29 14:49:05 -0400662 mutex_lock(&root->fs_info->extent_ins_mutex);
Zheng Yan31840ae2008-09-23 13:14:14 -0400663 if (test_range_bit(&root->fs_info->extent_ins, bytenr,
Josef Bacik25179202008-10-29 14:49:05 -0400664 bytenr + num_bytes - 1, EXTENT_WRITEBACK, 0)) {
Zheng Yan31840ae2008-09-23 13:14:14 -0400665 u64 priv;
666 ret = get_state_private(&root->fs_info->extent_ins,
667 bytenr, &priv);
668 BUG_ON(ret);
669 extent_op = (struct pending_extent_op *)
670 (unsigned long)priv;
671 BUG_ON(extent_op->parent != orig_parent);
672 BUG_ON(extent_op->generation != orig_generation);
Josef Bacik25179202008-10-29 14:49:05 -0400673
Zheng Yan31840ae2008-09-23 13:14:14 -0400674 extent_op->parent = parent;
675 extent_op->generation = ref_generation;
676 } else {
677 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
678 BUG_ON(!extent_op);
679
680 extent_op->type = PENDING_BACKREF_UPDATE;
681 extent_op->bytenr = bytenr;
682 extent_op->num_bytes = num_bytes;
683 extent_op->parent = parent;
684 extent_op->orig_parent = orig_parent;
685 extent_op->generation = ref_generation;
686 extent_op->orig_generation = orig_generation;
687 extent_op->level = (int)owner_objectid;
688
689 set_extent_bits(&root->fs_info->extent_ins,
690 bytenr, bytenr + num_bytes - 1,
Josef Bacik25179202008-10-29 14:49:05 -0400691 EXTENT_WRITEBACK, GFP_NOFS);
Zheng Yan31840ae2008-09-23 13:14:14 -0400692 set_state_private(&root->fs_info->extent_ins,
693 bytenr, (unsigned long)extent_op);
694 }
Josef Bacik25179202008-10-29 14:49:05 -0400695 mutex_unlock(&root->fs_info->extent_ins_mutex);
Zheng Yan31840ae2008-09-23 13:14:14 -0400696 return 0;
697 }
698
699 path = btrfs_alloc_path();
700 if (!path)
701 return -ENOMEM;
702 ret = lookup_extent_backref(trans, extent_root, path,
703 bytenr, orig_parent, orig_root,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400704 orig_generation, owner_objectid, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -0400705 if (ret)
706 goto out;
707 ret = remove_extent_backref(trans, extent_root, path);
708 if (ret)
709 goto out;
710 ret = insert_extent_backref(trans, extent_root, path, bytenr,
711 parent, ref_root, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400712 owner_objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -0400713 BUG_ON(ret);
Chris Mason87ef2bb2008-10-30 11:23:27 -0400714 finish_current_insert(trans, extent_root, 0);
715 del_pending_extents(trans, extent_root, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400716out:
717 btrfs_free_path(path);
718 return ret;
719}
720
721int btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
722 struct btrfs_root *root, u64 bytenr,
723 u64 orig_parent, u64 parent,
724 u64 ref_root, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400725 u64 owner_objectid)
Zheng Yan31840ae2008-09-23 13:14:14 -0400726{
727 int ret;
728 if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
729 owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
730 return 0;
Zheng Yan31840ae2008-09-23 13:14:14 -0400731 ret = __btrfs_update_extent_ref(trans, root, bytenr, orig_parent,
732 parent, ref_root, ref_root,
733 ref_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400734 owner_objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -0400735 return ret;
736}
737
Chris Mason925baed2008-06-25 16:01:30 -0400738static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -0400739 struct btrfs_root *root, u64 bytenr,
740 u64 orig_parent, u64 parent,
741 u64 orig_root, u64 ref_root,
742 u64 orig_generation, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400743 u64 owner_objectid)
Chris Mason02217ed2007-03-02 16:08:05 -0500744{
Chris Mason5caf2a02007-04-02 11:20:42 -0400745 struct btrfs_path *path;
Chris Mason02217ed2007-03-02 16:08:05 -0500746 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400747 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400748 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -0400749 struct btrfs_extent_item *item;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400750 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500751
Chris Mason5caf2a02007-04-02 11:20:42 -0400752 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400753 if (!path)
754 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -0400755
Chris Mason3c12ac72008-04-21 12:01:38 -0400756 path->reada = 1;
Chris Masondb945352007-10-15 16:15:53 -0400757 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -0400758 key.type = BTRFS_EXTENT_ITEM_KEY;
759 key.offset = (u64)-1;
760
Chris Mason5caf2a02007-04-02 11:20:42 -0400761 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400762 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400763 if (ret < 0)
764 return ret;
Zheng Yan31840ae2008-09-23 13:14:14 -0400765 BUG_ON(ret == 0 || path->slots[0] == 0);
766
767 path->slots[0]--;
Chris Mason5f39d392007-10-15 16:14:19 -0400768 l = path->nodes[0];
Zheng Yan31840ae2008-09-23 13:14:14 -0400769
770 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
Chris Mason771ed682008-11-06 22:02:51 -0500771 if (key.objectid != bytenr) {
772 btrfs_print_leaf(root->fs_info->extent_root, path->nodes[0]);
773 printk("wanted %Lu found %Lu\n", bytenr, key.objectid);
774 BUG();
775 }
Zheng Yan31840ae2008-09-23 13:14:14 -0400776 BUG_ON(key.type != BTRFS_EXTENT_ITEM_KEY);
777
Chris Mason5caf2a02007-04-02 11:20:42 -0400778 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400779 refs = btrfs_extent_refs(l, item);
780 btrfs_set_extent_refs(l, item, refs + 1);
Chris Mason5caf2a02007-04-02 11:20:42 -0400781 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masona28ec192007-03-06 20:08:01 -0500782
Chris Mason5caf2a02007-04-02 11:20:42 -0400783 btrfs_release_path(root->fs_info->extent_root, path);
Chris Mason7bb86312007-12-11 09:25:06 -0500784
Chris Mason3c12ac72008-04-21 12:01:38 -0400785 path->reada = 1;
Zheng Yan31840ae2008-09-23 13:14:14 -0400786 ret = insert_extent_backref(trans, root->fs_info->extent_root,
787 path, bytenr, parent,
788 ref_root, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400789 owner_objectid);
Chris Mason7bb86312007-12-11 09:25:06 -0500790 BUG_ON(ret);
Chris Mason87ef2bb2008-10-30 11:23:27 -0400791 finish_current_insert(trans, root->fs_info->extent_root, 0);
792 del_pending_extents(trans, root->fs_info->extent_root, 0);
Chris Mason74493f72007-12-11 09:25:06 -0500793
794 btrfs_free_path(path);
Chris Mason02217ed2007-03-02 16:08:05 -0500795 return 0;
796}
797
Chris Mason925baed2008-06-25 16:01:30 -0400798int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -0400799 struct btrfs_root *root,
800 u64 bytenr, u64 num_bytes, u64 parent,
801 u64 ref_root, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400802 u64 owner_objectid)
Chris Mason925baed2008-06-25 16:01:30 -0400803{
804 int ret;
Zheng Yan31840ae2008-09-23 13:14:14 -0400805 if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
806 owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
807 return 0;
Zheng Yan31840ae2008-09-23 13:14:14 -0400808 ret = __btrfs_inc_extent_ref(trans, root, bytenr, 0, parent,
809 0, ref_root, 0, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400810 owner_objectid);
Chris Mason925baed2008-06-25 16:01:30 -0400811 return ret;
812}
813
Chris Masone9d0b132007-08-10 14:06:19 -0400814int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
815 struct btrfs_root *root)
816{
Chris Mason87ef2bb2008-10-30 11:23:27 -0400817 finish_current_insert(trans, root->fs_info->extent_root, 1);
818 del_pending_extents(trans, root->fs_info->extent_root, 1);
Chris Masone9d0b132007-08-10 14:06:19 -0400819 return 0;
820}
821
Zheng Yan31840ae2008-09-23 13:14:14 -0400822int btrfs_lookup_extent_ref(struct btrfs_trans_handle *trans,
823 struct btrfs_root *root, u64 bytenr,
824 u64 num_bytes, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -0500825{
Chris Mason5caf2a02007-04-02 11:20:42 -0400826 struct btrfs_path *path;
Chris Masona28ec192007-03-06 20:08:01 -0500827 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400828 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400829 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -0400830 struct btrfs_extent_item *item;
Chris Mason5caf2a02007-04-02 11:20:42 -0400831
Chris Masondb945352007-10-15 16:15:53 -0400832 WARN_ON(num_bytes < root->sectorsize);
Chris Mason5caf2a02007-04-02 11:20:42 -0400833 path = btrfs_alloc_path();
Chris Mason3c12ac72008-04-21 12:01:38 -0400834 path->reada = 1;
Chris Masondb945352007-10-15 16:15:53 -0400835 key.objectid = bytenr;
836 key.offset = num_bytes;
Chris Mason62e27492007-03-15 12:56:47 -0400837 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5caf2a02007-04-02 11:20:42 -0400838 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400839 0, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400840 if (ret < 0)
841 goto out;
Chris Mason5f39d392007-10-15 16:14:19 -0400842 if (ret != 0) {
843 btrfs_print_leaf(root, path->nodes[0]);
Chris Masondb945352007-10-15 16:15:53 -0400844 printk("failed to find block number %Lu\n", bytenr);
Chris Masona28ec192007-03-06 20:08:01 -0500845 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -0400846 }
847 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -0400848 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400849 *refs = btrfs_extent_refs(l, item);
Chris Mason54aa1f42007-06-22 14:16:25 -0400850out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400851 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -0500852 return 0;
853}
854
Yan Zheng80ff3852008-10-30 14:20:02 -0400855int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
856 struct btrfs_root *root, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -0500857{
858 struct btrfs_root *extent_root = root->fs_info->extent_root;
859 struct btrfs_path *path;
Yan Zhengf321e492008-07-30 09:26:11 -0400860 struct extent_buffer *leaf;
861 struct btrfs_extent_ref *ref_item;
Chris Masonbe20aa92007-12-17 20:14:01 -0500862 struct btrfs_key key;
863 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -0400864 u64 ref_root;
865 u64 last_snapshot;
Yan Zhengf321e492008-07-30 09:26:11 -0400866 u32 nritems;
867 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -0500868
Chris Masonbe20aa92007-12-17 20:14:01 -0500869 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -0400870 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -0400871 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -0500872
Yan Zhengf321e492008-07-30 09:26:11 -0400873 path = btrfs_alloc_path();
Chris Masonbe20aa92007-12-17 20:14:01 -0500874 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
875 if (ret < 0)
876 goto out;
877 BUG_ON(ret == 0);
Yan Zheng80ff3852008-10-30 14:20:02 -0400878
879 ret = -ENOENT;
880 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -0400881 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -0500882
Zheng Yan31840ae2008-09-23 13:14:14 -0400883 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -0400884 leaf = path->nodes[0];
885 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -0500886
887 if (found_key.objectid != bytenr ||
Yan Zheng80ff3852008-10-30 14:20:02 -0400888 found_key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -0500889 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -0500890
Yan Zheng80ff3852008-10-30 14:20:02 -0400891 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Chris Masonbe20aa92007-12-17 20:14:01 -0500892 while (1) {
Yan Zhengf321e492008-07-30 09:26:11 -0400893 leaf = path->nodes[0];
894 nritems = btrfs_header_nritems(leaf);
Chris Masonbe20aa92007-12-17 20:14:01 -0500895 if (path->slots[0] >= nritems) {
896 ret = btrfs_next_leaf(extent_root, path);
Yan Zhengf321e492008-07-30 09:26:11 -0400897 if (ret < 0)
898 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -0500899 if (ret == 0)
900 continue;
901 break;
902 }
Yan Zhengf321e492008-07-30 09:26:11 -0400903 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -0500904 if (found_key.objectid != bytenr)
905 break;
Chris Masonbd098352008-01-03 13:23:19 -0500906
Chris Masonbe20aa92007-12-17 20:14:01 -0500907 if (found_key.type != BTRFS_EXTENT_REF_KEY) {
908 path->slots[0]++;
909 continue;
910 }
911
Yan Zhengf321e492008-07-30 09:26:11 -0400912 ref_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masonbe20aa92007-12-17 20:14:01 -0500913 struct btrfs_extent_ref);
Yan Zheng80ff3852008-10-30 14:20:02 -0400914 ref_root = btrfs_ref_root(leaf, ref_item);
915 if (ref_root != root->root_key.objectid &&
916 ref_root != BTRFS_TREE_LOG_OBJECTID) {
917 ret = 1;
918 goto out;
Yan Zhengf321e492008-07-30 09:26:11 -0400919 }
Yan Zheng80ff3852008-10-30 14:20:02 -0400920 if (btrfs_ref_generation(leaf, ref_item) <= last_snapshot) {
921 ret = 1;
922 goto out;
923 }
Yan Zhengf321e492008-07-30 09:26:11 -0400924
Chris Masonbe20aa92007-12-17 20:14:01 -0500925 path->slots[0]++;
926 }
Yan Zhengf321e492008-07-30 09:26:11 -0400927 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -0500928out:
Yan Zhengf321e492008-07-30 09:26:11 -0400929 btrfs_free_path(path);
930 return ret;
931}
932
Zheng Yan31840ae2008-09-23 13:14:14 -0400933int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
934 struct extent_buffer *buf, u32 nr_extents)
Chris Mason02217ed2007-03-02 16:08:05 -0500935{
Chris Mason5f39d392007-10-15 16:14:19 -0400936 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -0400937 struct btrfs_file_extent_item *fi;
Zheng Yane4657682008-09-26 10:04:53 -0400938 u64 root_gen;
939 u32 nritems;
Chris Mason02217ed2007-03-02 16:08:05 -0500940 int i;
Chris Masondb945352007-10-15 16:15:53 -0400941 int level;
Zheng Yan31840ae2008-09-23 13:14:14 -0400942 int ret = 0;
Zheng Yane4657682008-09-26 10:04:53 -0400943 int shared = 0;
Chris Masona28ec192007-03-06 20:08:01 -0500944
Chris Mason3768f362007-03-13 16:47:54 -0400945 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -0500946 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400947
Zheng Yane4657682008-09-26 10:04:53 -0400948 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
949 shared = 0;
950 root_gen = root->root_key.offset;
951 } else {
952 shared = 1;
953 root_gen = trans->transid - 1;
954 }
955
Chris Masondb945352007-10-15 16:15:53 -0400956 level = btrfs_header_level(buf);
Chris Mason5f39d392007-10-15 16:14:19 -0400957 nritems = btrfs_header_nritems(buf);
Chris Mason4a096752008-07-21 10:29:44 -0400958
Zheng Yan31840ae2008-09-23 13:14:14 -0400959 if (level == 0) {
Yan Zheng31153d82008-07-28 15:32:19 -0400960 struct btrfs_leaf_ref *ref;
961 struct btrfs_extent_info *info;
962
Zheng Yan31840ae2008-09-23 13:14:14 -0400963 ref = btrfs_alloc_leaf_ref(root, nr_extents);
Yan Zheng31153d82008-07-28 15:32:19 -0400964 if (!ref) {
Zheng Yan31840ae2008-09-23 13:14:14 -0400965 ret = -ENOMEM;
Yan Zheng31153d82008-07-28 15:32:19 -0400966 goto out;
967 }
968
Zheng Yane4657682008-09-26 10:04:53 -0400969 ref->root_gen = root_gen;
Yan Zheng31153d82008-07-28 15:32:19 -0400970 ref->bytenr = buf->start;
971 ref->owner = btrfs_header_owner(buf);
972 ref->generation = btrfs_header_generation(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -0400973 ref->nritems = nr_extents;
Yan Zheng31153d82008-07-28 15:32:19 -0400974 info = ref->extents;
Yanbcc63ab2008-07-30 16:29:20 -0400975
Zheng Yan31840ae2008-09-23 13:14:14 -0400976 for (i = 0; nr_extents > 0 && i < nritems; i++) {
Yan Zheng31153d82008-07-28 15:32:19 -0400977 u64 disk_bytenr;
978 btrfs_item_key_to_cpu(buf, &key, i);
979 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
980 continue;
981 fi = btrfs_item_ptr(buf, i,
982 struct btrfs_file_extent_item);
983 if (btrfs_file_extent_type(buf, fi) ==
984 BTRFS_FILE_EXTENT_INLINE)
985 continue;
986 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
987 if (disk_bytenr == 0)
988 continue;
989
990 info->bytenr = disk_bytenr;
991 info->num_bytes =
992 btrfs_file_extent_disk_num_bytes(buf, fi);
993 info->objectid = key.objectid;
994 info->offset = key.offset;
995 info++;
996 }
997
Zheng Yane4657682008-09-26 10:04:53 -0400998 ret = btrfs_add_leaf_ref(root, ref, shared);
Yan Zheng5b84e8d2008-10-09 11:46:19 -0400999 if (ret == -EEXIST && shared) {
1000 struct btrfs_leaf_ref *old;
1001 old = btrfs_lookup_leaf_ref(root, ref->bytenr);
1002 BUG_ON(!old);
1003 btrfs_remove_leaf_ref(root, old);
1004 btrfs_free_leaf_ref(root, old);
1005 ret = btrfs_add_leaf_ref(root, ref, shared);
1006 }
Yan Zheng31153d82008-07-28 15:32:19 -04001007 WARN_ON(ret);
Yanbcc63ab2008-07-30 16:29:20 -04001008 btrfs_free_leaf_ref(root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04001009 }
1010out:
Zheng Yan31840ae2008-09-23 13:14:14 -04001011 return ret;
1012}
1013
1014int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1015 struct extent_buffer *orig_buf, struct extent_buffer *buf,
1016 u32 *nr_extents)
1017{
1018 u64 bytenr;
1019 u64 ref_root;
1020 u64 orig_root;
1021 u64 ref_generation;
1022 u64 orig_generation;
1023 u32 nritems;
1024 u32 nr_file_extents = 0;
1025 struct btrfs_key key;
1026 struct btrfs_file_extent_item *fi;
1027 int i;
1028 int level;
1029 int ret = 0;
1030 int faili = 0;
1031 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001032 u64, u64, u64, u64, u64, u64, u64, u64);
Zheng Yan31840ae2008-09-23 13:14:14 -04001033
1034 ref_root = btrfs_header_owner(buf);
1035 ref_generation = btrfs_header_generation(buf);
1036 orig_root = btrfs_header_owner(orig_buf);
1037 orig_generation = btrfs_header_generation(orig_buf);
1038
1039 nritems = btrfs_header_nritems(buf);
1040 level = btrfs_header_level(buf);
1041
1042 if (root->ref_cows) {
1043 process_func = __btrfs_inc_extent_ref;
1044 } else {
1045 if (level == 0 &&
1046 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1047 goto out;
1048 if (level != 0 &&
1049 root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
1050 goto out;
1051 process_func = __btrfs_update_extent_ref;
1052 }
1053
1054 for (i = 0; i < nritems; i++) {
1055 cond_resched();
Chris Masondb945352007-10-15 16:15:53 -04001056 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001057 btrfs_item_key_to_cpu(buf, &key, i);
1058 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04001059 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001060 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04001061 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001062 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04001063 BTRFS_FILE_EXTENT_INLINE)
1064 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04001065 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1066 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04001067 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04001068
1069 nr_file_extents++;
1070
Zheng Yan31840ae2008-09-23 13:14:14 -04001071 ret = process_func(trans, root, bytenr,
1072 orig_buf->start, buf->start,
1073 orig_root, ref_root,
1074 orig_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001075 key.objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -04001076
1077 if (ret) {
1078 faili = i;
1079 WARN_ON(1);
1080 goto fail;
1081 }
Chris Mason54aa1f42007-06-22 14:16:25 -04001082 } else {
Chris Masondb945352007-10-15 16:15:53 -04001083 bytenr = btrfs_node_blockptr(buf, i);
Zheng Yan31840ae2008-09-23 13:14:14 -04001084 ret = process_func(trans, root, bytenr,
1085 orig_buf->start, buf->start,
1086 orig_root, ref_root,
1087 orig_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001088 level - 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001089 if (ret) {
1090 faili = i;
1091 WARN_ON(1);
1092 goto fail;
1093 }
Chris Mason54aa1f42007-06-22 14:16:25 -04001094 }
1095 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001096out:
1097 if (nr_extents) {
1098 if (level == 0)
1099 *nr_extents = nr_file_extents;
1100 else
1101 *nr_extents = nritems;
1102 }
1103 return 0;
1104fail:
1105 WARN_ON(1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001106 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05001107}
1108
Zheng Yan31840ae2008-09-23 13:14:14 -04001109int btrfs_update_ref(struct btrfs_trans_handle *trans,
1110 struct btrfs_root *root, struct extent_buffer *orig_buf,
1111 struct extent_buffer *buf, int start_slot, int nr)
1112
1113{
1114 u64 bytenr;
1115 u64 ref_root;
1116 u64 orig_root;
1117 u64 ref_generation;
1118 u64 orig_generation;
1119 struct btrfs_key key;
1120 struct btrfs_file_extent_item *fi;
1121 int i;
1122 int ret;
1123 int slot;
1124 int level;
1125
1126 BUG_ON(start_slot < 0);
1127 BUG_ON(start_slot + nr > btrfs_header_nritems(buf));
1128
1129 ref_root = btrfs_header_owner(buf);
1130 ref_generation = btrfs_header_generation(buf);
1131 orig_root = btrfs_header_owner(orig_buf);
1132 orig_generation = btrfs_header_generation(orig_buf);
1133 level = btrfs_header_level(buf);
1134
1135 if (!root->ref_cows) {
1136 if (level == 0 &&
1137 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1138 return 0;
1139 if (level != 0 &&
1140 root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
1141 return 0;
1142 }
1143
1144 for (i = 0, slot = start_slot; i < nr; i++, slot++) {
1145 cond_resched();
1146 if (level == 0) {
1147 btrfs_item_key_to_cpu(buf, &key, slot);
1148 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1149 continue;
1150 fi = btrfs_item_ptr(buf, slot,
1151 struct btrfs_file_extent_item);
1152 if (btrfs_file_extent_type(buf, fi) ==
1153 BTRFS_FILE_EXTENT_INLINE)
1154 continue;
1155 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1156 if (bytenr == 0)
1157 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04001158 ret = __btrfs_update_extent_ref(trans, root, bytenr,
1159 orig_buf->start, buf->start,
1160 orig_root, ref_root,
1161 orig_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001162 key.objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -04001163 if (ret)
1164 goto fail;
1165 } else {
1166 bytenr = btrfs_node_blockptr(buf, slot);
Zheng Yan31840ae2008-09-23 13:14:14 -04001167 ret = __btrfs_update_extent_ref(trans, root, bytenr,
1168 orig_buf->start, buf->start,
1169 orig_root, ref_root,
1170 orig_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001171 level - 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001172 if (ret)
1173 goto fail;
1174 }
1175 }
1176 return 0;
1177fail:
1178 WARN_ON(1);
1179 return -1;
1180}
1181
Chris Mason9078a3e2007-04-26 16:46:15 -04001182static int write_one_cache_group(struct btrfs_trans_handle *trans,
1183 struct btrfs_root *root,
1184 struct btrfs_path *path,
1185 struct btrfs_block_group_cache *cache)
1186{
1187 int ret;
1188 int pending_ret;
1189 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04001190 unsigned long bi;
1191 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04001192
Chris Mason9078a3e2007-04-26 16:46:15 -04001193 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001194 if (ret < 0)
1195 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -04001196 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001197
1198 leaf = path->nodes[0];
1199 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
1200 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
1201 btrfs_mark_buffer_dirty(leaf);
Chris Mason9078a3e2007-04-26 16:46:15 -04001202 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -04001203fail:
Chris Mason87ef2bb2008-10-30 11:23:27 -04001204 finish_current_insert(trans, extent_root, 0);
1205 pending_ret = del_pending_extents(trans, extent_root, 0);
Chris Mason9078a3e2007-04-26 16:46:15 -04001206 if (ret)
1207 return ret;
1208 if (pending_ret)
1209 return pending_ret;
1210 return 0;
1211
1212}
1213
Chris Mason96b51792007-10-15 16:15:19 -04001214int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
1215 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04001216{
Josef Bacik0f9dd462008-09-23 13:14:11 -04001217 struct btrfs_block_group_cache *cache, *entry;
1218 struct rb_node *n;
Chris Mason9078a3e2007-04-26 16:46:15 -04001219 int err = 0;
1220 int werr = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04001221 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04001222 u64 last = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04001223
1224 path = btrfs_alloc_path();
1225 if (!path)
1226 return -ENOMEM;
1227
1228 while(1) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001229 cache = NULL;
1230 spin_lock(&root->fs_info->block_group_cache_lock);
1231 for (n = rb_first(&root->fs_info->block_group_cache_tree);
1232 n; n = rb_next(n)) {
1233 entry = rb_entry(n, struct btrfs_block_group_cache,
1234 cache_node);
1235 if (entry->dirty) {
1236 cache = entry;
1237 break;
1238 }
1239 }
1240 spin_unlock(&root->fs_info->block_group_cache_lock);
1241
1242 if (!cache)
Chris Mason9078a3e2007-04-26 16:46:15 -04001243 break;
Chris Mason54aa1f42007-06-22 14:16:25 -04001244
Zheng Yane8569812008-09-26 10:05:48 -04001245 cache->dirty = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001246 last += cache->key.offset;
1247
Chris Mason96b51792007-10-15 16:15:19 -04001248 err = write_one_cache_group(trans, root,
1249 path, cache);
1250 /*
1251 * if we fail to write the cache group, we want
1252 * to keep it marked dirty in hopes that a later
1253 * write will work
1254 */
1255 if (err) {
1256 werr = err;
1257 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04001258 }
1259 }
1260 btrfs_free_path(path);
1261 return werr;
1262}
1263
Chris Mason593060d2008-03-25 16:50:33 -04001264static int update_space_info(struct btrfs_fs_info *info, u64 flags,
1265 u64 total_bytes, u64 bytes_used,
1266 struct btrfs_space_info **space_info)
1267{
1268 struct btrfs_space_info *found;
1269
1270 found = __find_space_info(info, flags);
1271 if (found) {
Josef Bacik25179202008-10-29 14:49:05 -04001272 spin_lock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04001273 found->total_bytes += total_bytes;
1274 found->bytes_used += bytes_used;
Chris Mason8f18cf12008-04-25 16:53:30 -04001275 found->full = 0;
Josef Bacik25179202008-10-29 14:49:05 -04001276 spin_unlock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04001277 *space_info = found;
1278 return 0;
1279 }
1280 found = kmalloc(sizeof(*found), GFP_NOFS);
1281 if (!found)
1282 return -ENOMEM;
1283
1284 list_add(&found->list, &info->space_info);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001285 INIT_LIST_HEAD(&found->block_groups);
Josef Bacik80eb2342008-10-29 14:49:05 -04001286 init_rwsem(&found->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001287 spin_lock_init(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04001288 found->flags = flags;
1289 found->total_bytes = total_bytes;
1290 found->bytes_used = bytes_used;
1291 found->bytes_pinned = 0;
Zheng Yane8569812008-09-26 10:05:48 -04001292 found->bytes_reserved = 0;
Chris Mason593060d2008-03-25 16:50:33 -04001293 found->full = 0;
Chris Mason0ef3e662008-05-24 14:04:53 -04001294 found->force_alloc = 0;
Chris Mason593060d2008-03-25 16:50:33 -04001295 *space_info = found;
1296 return 0;
1297}
1298
Chris Mason8790d502008-04-03 16:29:03 -04001299static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
1300{
1301 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
Chris Mason611f0e02008-04-03 16:29:03 -04001302 BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04001303 BTRFS_BLOCK_GROUP_RAID10 |
Chris Mason611f0e02008-04-03 16:29:03 -04001304 BTRFS_BLOCK_GROUP_DUP);
Chris Mason8790d502008-04-03 16:29:03 -04001305 if (extra_flags) {
1306 if (flags & BTRFS_BLOCK_GROUP_DATA)
1307 fs_info->avail_data_alloc_bits |= extra_flags;
1308 if (flags & BTRFS_BLOCK_GROUP_METADATA)
1309 fs_info->avail_metadata_alloc_bits |= extra_flags;
1310 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
1311 fs_info->avail_system_alloc_bits |= extra_flags;
1312 }
1313}
Chris Mason593060d2008-03-25 16:50:33 -04001314
Chris Masona061fc82008-05-07 11:43:44 -04001315static u64 reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04001316{
Chris Masona061fc82008-05-07 11:43:44 -04001317 u64 num_devices = root->fs_info->fs_devices->num_devices;
1318
1319 if (num_devices == 1)
1320 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
1321 if (num_devices < 4)
1322 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
1323
Chris Masonec44a352008-04-28 15:29:52 -04001324 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
1325 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
Chris Masona061fc82008-05-07 11:43:44 -04001326 BTRFS_BLOCK_GROUP_RAID10))) {
Chris Masonec44a352008-04-28 15:29:52 -04001327 flags &= ~BTRFS_BLOCK_GROUP_DUP;
Chris Masona061fc82008-05-07 11:43:44 -04001328 }
Chris Masonec44a352008-04-28 15:29:52 -04001329
1330 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
Chris Masona061fc82008-05-07 11:43:44 -04001331 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
Chris Masonec44a352008-04-28 15:29:52 -04001332 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
Chris Masona061fc82008-05-07 11:43:44 -04001333 }
Chris Masonec44a352008-04-28 15:29:52 -04001334
1335 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
1336 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
1337 (flags & BTRFS_BLOCK_GROUP_RAID10) |
1338 (flags & BTRFS_BLOCK_GROUP_DUP)))
1339 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
1340 return flags;
1341}
1342
Chris Mason6324fbf2008-03-24 15:01:59 -04001343static int do_chunk_alloc(struct btrfs_trans_handle *trans,
1344 struct btrfs_root *extent_root, u64 alloc_bytes,
Chris Mason0ef3e662008-05-24 14:04:53 -04001345 u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04001346{
1347 struct btrfs_space_info *space_info;
1348 u64 thresh;
1349 u64 start;
1350 u64 num_bytes;
Josef Bacikcf749822008-10-01 19:11:18 -04001351 int ret = 0, waited = 0;
Chris Mason6324fbf2008-03-24 15:01:59 -04001352
Chris Masona061fc82008-05-07 11:43:44 -04001353 flags = reduce_alloc_profile(extent_root, flags);
Chris Masonec44a352008-04-28 15:29:52 -04001354
Chris Mason6324fbf2008-03-24 15:01:59 -04001355 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04001356 if (!space_info) {
1357 ret = update_space_info(extent_root->fs_info, flags,
1358 0, 0, &space_info);
1359 BUG_ON(ret);
1360 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001361 BUG_ON(!space_info);
1362
Josef Bacik25179202008-10-29 14:49:05 -04001363 spin_lock(&space_info->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04001364 if (space_info->force_alloc) {
1365 force = 1;
1366 space_info->force_alloc = 0;
1367 }
Josef Bacik25179202008-10-29 14:49:05 -04001368 if (space_info->full) {
1369 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04001370 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04001371 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001372
Chris Mason8790d502008-04-03 16:29:03 -04001373 thresh = div_factor(space_info->total_bytes, 6);
Chris Mason0ef3e662008-05-24 14:04:53 -04001374 if (!force &&
Zheng Yane8569812008-09-26 10:05:48 -04001375 (space_info->bytes_used + space_info->bytes_pinned +
Josef Bacik25179202008-10-29 14:49:05 -04001376 space_info->bytes_reserved + alloc_bytes) < thresh) {
1377 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04001378 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04001379 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001380
Josef Bacik25179202008-10-29 14:49:05 -04001381 spin_unlock(&space_info->lock);
1382
1383 ret = mutex_trylock(&extent_root->fs_info->chunk_mutex);
1384 if (!ret && !force) {
1385 goto out;
1386 } else if (!ret) {
1387 mutex_lock(&extent_root->fs_info->chunk_mutex);
Josef Bacikcf749822008-10-01 19:11:18 -04001388 waited = 1;
1389 }
1390
Josef Bacik25179202008-10-29 14:49:05 -04001391 if (waited) {
1392 spin_lock(&space_info->lock);
1393 if (space_info->full) {
1394 spin_unlock(&space_info->lock);
1395 goto out_unlock;
1396 }
1397 spin_unlock(&space_info->lock);
1398 }
Josef Bacikcf749822008-10-01 19:11:18 -04001399
Chris Mason6324fbf2008-03-24 15:01:59 -04001400 ret = btrfs_alloc_chunk(trans, extent_root, &start, &num_bytes, flags);
Josef Bacik25179202008-10-29 14:49:05 -04001401 if (ret) {
Chris Mason6324fbf2008-03-24 15:01:59 -04001402printk("space info full %Lu\n", flags);
1403 space_info->full = 1;
Chris Masona74a4b92008-06-25 16:01:31 -04001404 goto out_unlock;
Chris Mason6324fbf2008-03-24 15:01:59 -04001405 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001406
1407 ret = btrfs_make_block_group(trans, extent_root, 0, flags,
Chris Masone17cade2008-04-15 15:41:47 -04001408 BTRFS_FIRST_CHUNK_TREE_OBJECTID, start, num_bytes);
Chris Mason6324fbf2008-03-24 15:01:59 -04001409 BUG_ON(ret);
Chris Masona74a4b92008-06-25 16:01:31 -04001410out_unlock:
Chris Mason333db942008-06-25 16:01:30 -04001411 mutex_unlock(&extent_root->fs_info->chunk_mutex);
Chris Masona74a4b92008-06-25 16:01:31 -04001412out:
Josef Bacik0f9dd462008-09-23 13:14:11 -04001413 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04001414}
1415
Chris Mason9078a3e2007-04-26 16:46:15 -04001416static int update_block_group(struct btrfs_trans_handle *trans,
1417 struct btrfs_root *root,
Chris Masondb945352007-10-15 16:15:53 -04001418 u64 bytenr, u64 num_bytes, int alloc,
Chris Mason0b86a832008-03-24 15:01:56 -04001419 int mark_free)
Chris Mason9078a3e2007-04-26 16:46:15 -04001420{
1421 struct btrfs_block_group_cache *cache;
1422 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04001423 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04001424 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04001425 u64 byte_in_group;
Chris Mason3e1ad542007-05-07 20:03:49 -04001426
Chris Mason9078a3e2007-04-26 16:46:15 -04001427 while(total) {
Chris Masondb945352007-10-15 16:15:53 -04001428 cache = btrfs_lookup_block_group(info, bytenr);
Chris Mason3e1ad542007-05-07 20:03:49 -04001429 if (!cache) {
Chris Mason9078a3e2007-04-26 16:46:15 -04001430 return -1;
Chris Masoncd1bc462007-04-27 10:08:34 -04001431 }
Chris Masondb945352007-10-15 16:15:53 -04001432 byte_in_group = bytenr - cache->key.objectid;
1433 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04001434
Josef Bacik25179202008-10-29 14:49:05 -04001435 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04001436 spin_lock(&cache->lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001437 cache->dirty = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04001438 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04001439 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04001440 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04001441 old_val += num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04001442 cache->space_info->bytes_used += num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04001443 btrfs_set_block_group_used(&cache->item, old_val);
1444 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04001445 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04001446 } else {
Chris Masondb945352007-10-15 16:15:53 -04001447 old_val -= num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04001448 cache->space_info->bytes_used -= num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04001449 btrfs_set_block_group_used(&cache->item, old_val);
1450 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04001451 spin_unlock(&cache->space_info->lock);
Chris Masonf510cfe2007-10-15 16:14:48 -04001452 if (mark_free) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001453 int ret;
1454 ret = btrfs_add_free_space(cache, bytenr,
1455 num_bytes);
1456 if (ret)
1457 return -1;
Chris Masone37c9e62007-05-09 20:13:14 -04001458 }
Chris Masoncd1bc462007-04-27 10:08:34 -04001459 }
Chris Masondb945352007-10-15 16:15:53 -04001460 total -= num_bytes;
1461 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04001462 }
1463 return 0;
1464}
Chris Mason6324fbf2008-03-24 15:01:59 -04001465
Chris Masona061fc82008-05-07 11:43:44 -04001466static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
1467{
Josef Bacik0f9dd462008-09-23 13:14:11 -04001468 struct btrfs_block_group_cache *cache;
1469
1470 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
1471 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04001472 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001473
1474 return cache->key.objectid;
Chris Masona061fc82008-05-07 11:43:44 -04001475}
1476
Chris Masone02119d2008-09-05 16:13:11 -04001477int btrfs_update_pinned_extents(struct btrfs_root *root,
Yan324ae4d2007-11-16 14:57:08 -05001478 u64 bytenr, u64 num, int pin)
1479{
1480 u64 len;
1481 struct btrfs_block_group_cache *cache;
1482 struct btrfs_fs_info *fs_info = root->fs_info;
1483
Josef Bacik25179202008-10-29 14:49:05 -04001484 WARN_ON(!mutex_is_locked(&root->fs_info->pinned_mutex));
Yan324ae4d2007-11-16 14:57:08 -05001485 if (pin) {
1486 set_extent_dirty(&fs_info->pinned_extents,
1487 bytenr, bytenr + num - 1, GFP_NOFS);
1488 } else {
1489 clear_extent_dirty(&fs_info->pinned_extents,
1490 bytenr, bytenr + num - 1, GFP_NOFS);
1491 }
1492 while (num > 0) {
1493 cache = btrfs_lookup_block_group(fs_info, bytenr);
Zheng Yane8569812008-09-26 10:05:48 -04001494 BUG_ON(!cache);
1495 len = min(num, cache->key.offset -
1496 (bytenr - cache->key.objectid));
Yan324ae4d2007-11-16 14:57:08 -05001497 if (pin) {
Josef Bacik25179202008-10-29 14:49:05 -04001498 spin_lock(&cache->space_info->lock);
Zheng Yane8569812008-09-26 10:05:48 -04001499 spin_lock(&cache->lock);
1500 cache->pinned += len;
1501 cache->space_info->bytes_pinned += len;
1502 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04001503 spin_unlock(&cache->space_info->lock);
Yan324ae4d2007-11-16 14:57:08 -05001504 fs_info->total_pinned += len;
1505 } else {
Josef Bacik25179202008-10-29 14:49:05 -04001506 spin_lock(&cache->space_info->lock);
Zheng Yane8569812008-09-26 10:05:48 -04001507 spin_lock(&cache->lock);
1508 cache->pinned -= len;
1509 cache->space_info->bytes_pinned -= len;
1510 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04001511 spin_unlock(&cache->space_info->lock);
Yan324ae4d2007-11-16 14:57:08 -05001512 fs_info->total_pinned -= len;
1513 }
1514 bytenr += len;
1515 num -= len;
1516 }
1517 return 0;
1518}
Chris Mason9078a3e2007-04-26 16:46:15 -04001519
Zheng Yane8569812008-09-26 10:05:48 -04001520static int update_reserved_extents(struct btrfs_root *root,
1521 u64 bytenr, u64 num, int reserve)
1522{
1523 u64 len;
1524 struct btrfs_block_group_cache *cache;
1525 struct btrfs_fs_info *fs_info = root->fs_info;
1526
Zheng Yane8569812008-09-26 10:05:48 -04001527 while (num > 0) {
1528 cache = btrfs_lookup_block_group(fs_info, bytenr);
1529 BUG_ON(!cache);
1530 len = min(num, cache->key.offset -
1531 (bytenr - cache->key.objectid));
Josef Bacik25179202008-10-29 14:49:05 -04001532
1533 spin_lock(&cache->space_info->lock);
1534 spin_lock(&cache->lock);
Zheng Yane8569812008-09-26 10:05:48 -04001535 if (reserve) {
Zheng Yane8569812008-09-26 10:05:48 -04001536 cache->reserved += len;
1537 cache->space_info->bytes_reserved += len;
Zheng Yane8569812008-09-26 10:05:48 -04001538 } else {
Zheng Yane8569812008-09-26 10:05:48 -04001539 cache->reserved -= len;
1540 cache->space_info->bytes_reserved -= len;
Zheng Yane8569812008-09-26 10:05:48 -04001541 }
Josef Bacik25179202008-10-29 14:49:05 -04001542 spin_unlock(&cache->lock);
1543 spin_unlock(&cache->space_info->lock);
Zheng Yane8569812008-09-26 10:05:48 -04001544 bytenr += len;
1545 num -= len;
1546 }
1547 return 0;
1548}
1549
Chris Masond1310b22008-01-24 16:13:08 -05001550int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
Chris Masonccd467d2007-06-28 15:57:36 -04001551{
Chris Masonccd467d2007-06-28 15:57:36 -04001552 u64 last = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -04001553 u64 start;
1554 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -05001555 struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
Chris Masonccd467d2007-06-28 15:57:36 -04001556 int ret;
Chris Masonccd467d2007-06-28 15:57:36 -04001557
Josef Bacik25179202008-10-29 14:49:05 -04001558 mutex_lock(&root->fs_info->pinned_mutex);
Chris Masonccd467d2007-06-28 15:57:36 -04001559 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001560 ret = find_first_extent_bit(pinned_extents, last,
1561 &start, &end, EXTENT_DIRTY);
1562 if (ret)
Chris Masonccd467d2007-06-28 15:57:36 -04001563 break;
Chris Mason1a5bc162007-10-15 16:15:26 -04001564 set_extent_dirty(copy, start, end, GFP_NOFS);
1565 last = end + 1;
Chris Masonccd467d2007-06-28 15:57:36 -04001566 }
Josef Bacik25179202008-10-29 14:49:05 -04001567 mutex_unlock(&root->fs_info->pinned_mutex);
Chris Masonccd467d2007-06-28 15:57:36 -04001568 return 0;
1569}
1570
1571int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
1572 struct btrfs_root *root,
Chris Masond1310b22008-01-24 16:13:08 -05001573 struct extent_io_tree *unpin)
Chris Masona28ec192007-03-06 20:08:01 -05001574{
Chris Mason1a5bc162007-10-15 16:15:26 -04001575 u64 start;
1576 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05001577 int ret;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001578 struct btrfs_block_group_cache *cache;
Chris Masona28ec192007-03-06 20:08:01 -05001579
Josef Bacik25179202008-10-29 14:49:05 -04001580 mutex_lock(&root->fs_info->pinned_mutex);
Chris Masona28ec192007-03-06 20:08:01 -05001581 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001582 ret = find_first_extent_bit(unpin, 0, &start, &end,
1583 EXTENT_DIRTY);
1584 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05001585 break;
Chris Masone02119d2008-09-05 16:13:11 -04001586 btrfs_update_pinned_extents(root, start, end + 1 - start, 0);
Chris Mason1a5bc162007-10-15 16:15:26 -04001587 clear_extent_dirty(unpin, start, end, GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001588 cache = btrfs_lookup_block_group(root->fs_info, start);
1589 if (cache->cached)
1590 btrfs_add_free_space(cache, start, end - start + 1);
Chris Masonc286ac42008-07-22 23:06:41 -04001591 if (need_resched()) {
Josef Bacik25179202008-10-29 14:49:05 -04001592 mutex_unlock(&root->fs_info->pinned_mutex);
Chris Masonc286ac42008-07-22 23:06:41 -04001593 cond_resched();
Josef Bacik25179202008-10-29 14:49:05 -04001594 mutex_lock(&root->fs_info->pinned_mutex);
Chris Masonc286ac42008-07-22 23:06:41 -04001595 }
Chris Masona28ec192007-03-06 20:08:01 -05001596 }
Josef Bacik25179202008-10-29 14:49:05 -04001597 mutex_unlock(&root->fs_info->pinned_mutex);
Chris Masona28ec192007-03-06 20:08:01 -05001598 return 0;
1599}
1600
Chris Mason98ed5172008-01-03 10:01:48 -05001601static int finish_current_insert(struct btrfs_trans_handle *trans,
Chris Mason87ef2bb2008-10-30 11:23:27 -04001602 struct btrfs_root *extent_root, int all)
Chris Mason037e6392007-03-07 11:50:24 -05001603{
Chris Mason7bb86312007-12-11 09:25:06 -05001604 u64 start;
1605 u64 end;
Zheng Yan31840ae2008-09-23 13:14:14 -04001606 u64 priv;
Josef Bacik25179202008-10-29 14:49:05 -04001607 u64 search = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05001608 struct btrfs_fs_info *info = extent_root->fs_info;
1609 struct btrfs_path *path;
Zheng Yan31840ae2008-09-23 13:14:14 -04001610 struct btrfs_extent_ref *ref;
1611 struct pending_extent_op *extent_op;
1612 struct btrfs_key key;
Chris Mason234b63a2007-03-13 10:46:10 -04001613 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -05001614 int ret;
Chris Mason1a5bc162007-10-15 16:15:26 -04001615 int err = 0;
Chris Mason037e6392007-03-07 11:50:24 -05001616
Chris Mason5f39d392007-10-15 16:14:19 -04001617 btrfs_set_stack_extent_refs(&extent_item, 1);
Chris Mason7bb86312007-12-11 09:25:06 -05001618 path = btrfs_alloc_path();
Chris Mason037e6392007-03-07 11:50:24 -05001619
Chris Mason26b80032007-08-08 20:17:12 -04001620 while(1) {
Josef Bacik25179202008-10-29 14:49:05 -04001621 mutex_lock(&info->extent_ins_mutex);
1622 ret = find_first_extent_bit(&info->extent_ins, search, &start,
1623 &end, EXTENT_WRITEBACK);
1624 if (ret) {
1625 mutex_unlock(&info->extent_ins_mutex);
Chris Mason87ef2bb2008-10-30 11:23:27 -04001626 if (search && all) {
Josef Bacik25179202008-10-29 14:49:05 -04001627 search = 0;
1628 continue;
1629 }
Chris Mason26b80032007-08-08 20:17:12 -04001630 break;
Josef Bacik25179202008-10-29 14:49:05 -04001631 }
1632
1633 ret = try_lock_extent(&info->extent_ins, start, end, GFP_NOFS);
1634 if (!ret) {
Chris Mason87ef2bb2008-10-30 11:23:27 -04001635 search = end + 1;
Josef Bacik25179202008-10-29 14:49:05 -04001636 mutex_unlock(&info->extent_ins_mutex);
1637 cond_resched();
1638 continue;
1639 }
1640 BUG_ON(ret < 0);
Chris Mason26b80032007-08-08 20:17:12 -04001641
Zheng Yan31840ae2008-09-23 13:14:14 -04001642 ret = get_state_private(&info->extent_ins, start, &priv);
1643 BUG_ON(ret);
1644 extent_op = (struct pending_extent_op *)(unsigned long)priv;
1645
Josef Bacik25179202008-10-29 14:49:05 -04001646 mutex_unlock(&info->extent_ins_mutex);
1647
Zheng Yan31840ae2008-09-23 13:14:14 -04001648 if (extent_op->type == PENDING_EXTENT_INSERT) {
1649 key.objectid = start;
1650 key.offset = end + 1 - start;
1651 key.type = BTRFS_EXTENT_ITEM_KEY;
1652 err = btrfs_insert_item(trans, extent_root, &key,
Chris Mason1a5bc162007-10-15 16:15:26 -04001653 &extent_item, sizeof(extent_item));
Zheng Yan31840ae2008-09-23 13:14:14 -04001654 BUG_ON(err);
Chris Masonc286ac42008-07-22 23:06:41 -04001655
Josef Bacik25179202008-10-29 14:49:05 -04001656 mutex_lock(&info->extent_ins_mutex);
Zheng Yan31840ae2008-09-23 13:14:14 -04001657 clear_extent_bits(&info->extent_ins, start, end,
Josef Bacik25179202008-10-29 14:49:05 -04001658 EXTENT_WRITEBACK, GFP_NOFS);
1659 mutex_unlock(&info->extent_ins_mutex);
Chris Masonc286ac42008-07-22 23:06:41 -04001660
Zheng Yan31840ae2008-09-23 13:14:14 -04001661 err = insert_extent_backref(trans, extent_root, path,
1662 start, extent_op->parent,
1663 extent_root->root_key.objectid,
1664 extent_op->generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001665 extent_op->level);
Zheng Yan31840ae2008-09-23 13:14:14 -04001666 BUG_ON(err);
1667 } else if (extent_op->type == PENDING_BACKREF_UPDATE) {
1668 err = lookup_extent_backref(trans, extent_root, path,
1669 start, extent_op->orig_parent,
1670 extent_root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001671 extent_op->orig_generation,
1672 extent_op->level, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04001673 BUG_ON(err);
Chris Masonc286ac42008-07-22 23:06:41 -04001674
Josef Bacik25179202008-10-29 14:49:05 -04001675 mutex_lock(&info->extent_ins_mutex);
Zheng Yan31840ae2008-09-23 13:14:14 -04001676 clear_extent_bits(&info->extent_ins, start, end,
Josef Bacik25179202008-10-29 14:49:05 -04001677 EXTENT_WRITEBACK, GFP_NOFS);
1678 mutex_unlock(&info->extent_ins_mutex);
Zheng Yan31840ae2008-09-23 13:14:14 -04001679
1680 key.objectid = start;
1681 key.offset = extent_op->parent;
1682 key.type = BTRFS_EXTENT_REF_KEY;
1683 err = btrfs_set_item_key_safe(trans, extent_root, path,
1684 &key);
1685 BUG_ON(err);
1686 ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
1687 struct btrfs_extent_ref);
1688 btrfs_set_ref_generation(path->nodes[0], ref,
1689 extent_op->generation);
1690 btrfs_mark_buffer_dirty(path->nodes[0]);
1691 btrfs_release_path(extent_root, path);
Chris Masond8d5f3e2007-12-11 12:42:00 -05001692 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04001693 BUG_ON(1);
Chris Masond8d5f3e2007-12-11 12:42:00 -05001694 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001695 kfree(extent_op);
Josef Bacik25179202008-10-29 14:49:05 -04001696 unlock_extent(&info->extent_ins, start, end, GFP_NOFS);
Chris Mason87ef2bb2008-10-30 11:23:27 -04001697 if (all)
1698 search = 0;
1699 else
1700 search = end + 1;
Zheng Yan31840ae2008-09-23 13:14:14 -04001701
Josef Bacik25179202008-10-29 14:49:05 -04001702 cond_resched();
Chris Mason037e6392007-03-07 11:50:24 -05001703 }
Chris Mason7bb86312007-12-11 09:25:06 -05001704 btrfs_free_path(path);
Chris Mason037e6392007-03-07 11:50:24 -05001705 return 0;
1706}
1707
Zheng Yan31840ae2008-09-23 13:14:14 -04001708static int pin_down_bytes(struct btrfs_trans_handle *trans,
1709 struct btrfs_root *root,
1710 u64 bytenr, u64 num_bytes, int is_data)
Chris Masone20d96d2007-03-22 12:13:20 -04001711{
Chris Mason1a5bc162007-10-15 16:15:26 -04001712 int err = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001713 struct extent_buffer *buf;
Chris Mason78fae272007-03-25 11:35:08 -04001714
Zheng Yan31840ae2008-09-23 13:14:14 -04001715 if (is_data)
1716 goto pinit;
Chris Mason4bef0842008-09-08 11:18:08 -04001717
Zheng Yan31840ae2008-09-23 13:14:14 -04001718 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
1719 if (!buf)
1720 goto pinit;
Chris Mason4bef0842008-09-08 11:18:08 -04001721
Zheng Yan31840ae2008-09-23 13:14:14 -04001722 /* we can reuse a block if it hasn't been written
1723 * and it is from this transaction. We can't
1724 * reuse anything from the tree log root because
1725 * it has tiny sub-transactions.
1726 */
1727 if (btrfs_buffer_uptodate(buf, 0) &&
1728 btrfs_try_tree_lock(buf)) {
1729 u64 header_owner = btrfs_header_owner(buf);
1730 u64 header_transid = btrfs_header_generation(buf);
1731 if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
Zheng Yan1a40e232008-09-26 10:09:34 -04001732 header_owner != BTRFS_TREE_RELOC_OBJECTID &&
Zheng Yan31840ae2008-09-23 13:14:14 -04001733 header_transid == trans->transid &&
1734 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
1735 clean_tree_block(NULL, root, buf);
1736 btrfs_tree_unlock(buf);
Chris Mason5f39d392007-10-15 16:14:19 -04001737 free_extent_buffer(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04001738 return 1;
Chris Mason8ef97622007-03-26 10:15:30 -04001739 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001740 btrfs_tree_unlock(buf);
Chris Masonf4b9aa82007-03-27 11:05:53 -04001741 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001742 free_extent_buffer(buf);
1743pinit:
1744 btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
1745
Chris Masonbe744172007-05-06 10:15:01 -04001746 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -04001747 return 0;
1748}
1749
Chris Masona28ec192007-03-06 20:08:01 -05001750/*
1751 * remove an extent from the root, returns 0 on success
1752 */
Zheng Yan31840ae2008-09-23 13:14:14 -04001753static int __free_extent(struct btrfs_trans_handle *trans,
1754 struct btrfs_root *root,
1755 u64 bytenr, u64 num_bytes, u64 parent,
Chris Mason7bb86312007-12-11 09:25:06 -05001756 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001757 u64 owner_objectid, int pin, int mark_free)
Chris Masona28ec192007-03-06 20:08:01 -05001758{
Chris Mason5caf2a02007-04-02 11:20:42 -04001759 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -04001760 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -04001761 struct btrfs_fs_info *info = root->fs_info;
1762 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04001763 struct extent_buffer *leaf;
Chris Masona28ec192007-03-06 20:08:01 -05001764 int ret;
Chris Mason952fcca2008-02-18 16:33:44 -05001765 int extent_slot = 0;
1766 int found_extent = 0;
1767 int num_to_del = 1;
Chris Mason234b63a2007-03-13 10:46:10 -04001768 struct btrfs_extent_item *ei;
Chris Masoncf27e1e2007-03-13 09:49:06 -04001769 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -05001770
Chris Masondb945352007-10-15 16:15:53 -04001771 key.objectid = bytenr;
Chris Mason62e27492007-03-15 12:56:47 -04001772 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masondb945352007-10-15 16:15:53 -04001773 key.offset = num_bytes;
Chris Mason5caf2a02007-04-02 11:20:42 -04001774 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001775 if (!path)
1776 return -ENOMEM;
1777
Chris Mason3c12ac72008-04-21 12:01:38 -04001778 path->reada = 1;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001779 ret = lookup_extent_backref(trans, extent_root, path,
1780 bytenr, parent, root_objectid,
1781 ref_generation, owner_objectid, 1);
Chris Mason7bb86312007-12-11 09:25:06 -05001782 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05001783 struct btrfs_key found_key;
1784 extent_slot = path->slots[0];
1785 while(extent_slot > 0) {
1786 extent_slot--;
1787 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1788 extent_slot);
1789 if (found_key.objectid != bytenr)
1790 break;
1791 if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
1792 found_key.offset == num_bytes) {
1793 found_extent = 1;
1794 break;
1795 }
1796 if (path->slots[0] - extent_slot > 5)
1797 break;
1798 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001799 if (!found_extent) {
1800 ret = remove_extent_backref(trans, extent_root, path);
1801 BUG_ON(ret);
1802 btrfs_release_path(extent_root, path);
1803 ret = btrfs_search_slot(trans, extent_root,
1804 &key, path, -1, 1);
1805 BUG_ON(ret);
1806 extent_slot = path->slots[0];
1807 }
Chris Mason7bb86312007-12-11 09:25:06 -05001808 } else {
1809 btrfs_print_leaf(extent_root, path->nodes[0]);
1810 WARN_ON(1);
1811 printk("Unable to find ref byte nr %Lu root %Lu "
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001812 "gen %Lu owner %Lu\n", bytenr,
1813 root_objectid, ref_generation, owner_objectid);
Chris Mason7bb86312007-12-11 09:25:06 -05001814 }
Chris Mason5f39d392007-10-15 16:14:19 -04001815
1816 leaf = path->nodes[0];
Chris Mason952fcca2008-02-18 16:33:44 -05001817 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04001818 struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001819 refs = btrfs_extent_refs(leaf, ei);
1820 BUG_ON(refs == 0);
1821 refs -= 1;
1822 btrfs_set_extent_refs(leaf, ei, refs);
Chris Mason952fcca2008-02-18 16:33:44 -05001823
Chris Mason5f39d392007-10-15 16:14:19 -04001824 btrfs_mark_buffer_dirty(leaf);
1825
Chris Mason952fcca2008-02-18 16:33:44 -05001826 if (refs == 0 && found_extent && path->slots[0] == extent_slot + 1) {
Zheng Yan31840ae2008-09-23 13:14:14 -04001827 struct btrfs_extent_ref *ref;
1828 ref = btrfs_item_ptr(leaf, path->slots[0],
1829 struct btrfs_extent_ref);
1830 BUG_ON(btrfs_ref_num_refs(leaf, ref) != 1);
Chris Mason952fcca2008-02-18 16:33:44 -05001831 /* if the back ref and the extent are next to each other
1832 * they get deleted below in one shot
1833 */
1834 path->slots[0] = extent_slot;
1835 num_to_del = 2;
1836 } else if (found_extent) {
1837 /* otherwise delete the extent back ref */
Zheng Yan31840ae2008-09-23 13:14:14 -04001838 ret = remove_extent_backref(trans, extent_root, path);
Chris Mason952fcca2008-02-18 16:33:44 -05001839 BUG_ON(ret);
1840 /* if refs are 0, we need to setup the path for deletion */
1841 if (refs == 0) {
1842 btrfs_release_path(extent_root, path);
1843 ret = btrfs_search_slot(trans, extent_root, &key, path,
1844 -1, 1);
Chris Mason952fcca2008-02-18 16:33:44 -05001845 BUG_ON(ret);
1846 }
1847 }
1848
Chris Masoncf27e1e2007-03-13 09:49:06 -04001849 if (refs == 0) {
Chris Masondb945352007-10-15 16:15:53 -04001850 u64 super_used;
1851 u64 root_used;
David Woodhouse21af8042008-08-12 14:13:26 +01001852#ifdef BIO_RW_DISCARD
1853 u64 map_length = num_bytes;
1854 struct btrfs_multi_bio *multi = NULL;
1855#endif
Chris Mason78fae272007-03-25 11:35:08 -04001856
1857 if (pin) {
Josef Bacik25179202008-10-29 14:49:05 -04001858 mutex_lock(&root->fs_info->pinned_mutex);
Zheng Yan31840ae2008-09-23 13:14:14 -04001859 ret = pin_down_bytes(trans, root, bytenr, num_bytes,
1860 owner_objectid >= BTRFS_FIRST_FREE_OBJECTID);
Josef Bacik25179202008-10-29 14:49:05 -04001861 mutex_unlock(&root->fs_info->pinned_mutex);
Yanc5492282007-11-06 10:25:25 -05001862 if (ret > 0)
1863 mark_free = 1;
1864 BUG_ON(ret < 0);
Chris Mason78fae272007-03-25 11:35:08 -04001865 }
1866
Josef Bacik58176a92007-08-29 15:47:34 -04001867 /* block accounting for super block */
Chris Masona2135012008-06-25 16:01:30 -04001868 spin_lock_irq(&info->delalloc_lock);
Chris Masondb945352007-10-15 16:15:53 -04001869 super_used = btrfs_super_bytes_used(&info->super_copy);
1870 btrfs_set_super_bytes_used(&info->super_copy,
1871 super_used - num_bytes);
Chris Masona2135012008-06-25 16:01:30 -04001872 spin_unlock_irq(&info->delalloc_lock);
Josef Bacik58176a92007-08-29 15:47:34 -04001873
1874 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04001875 root_used = btrfs_root_used(&root->root_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001876 btrfs_set_root_used(&root->root_item,
Chris Masondb945352007-10-15 16:15:53 -04001877 root_used - num_bytes);
Chris Mason952fcca2008-02-18 16:33:44 -05001878 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
1879 num_to_del);
Zheng Yan31840ae2008-09-23 13:14:14 -04001880 BUG_ON(ret);
Josef Bacik25179202008-10-29 14:49:05 -04001881 btrfs_release_path(extent_root, path);
Chris Masondb945352007-10-15 16:15:53 -04001882 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
Chris Mason0b86a832008-03-24 15:01:56 -04001883 mark_free);
Chris Mason9078a3e2007-04-26 16:46:15 -04001884 BUG_ON(ret);
David Woodhouse21af8042008-08-12 14:13:26 +01001885
1886#ifdef BIO_RW_DISCARD
1887 /* Tell the block device(s) that the sectors can be discarded */
1888 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
1889 bytenr, &map_length, &multi, 0);
1890 if (!ret) {
1891 struct btrfs_bio_stripe *stripe = multi->stripes;
1892 int i;
1893
1894 if (map_length > num_bytes)
1895 map_length = num_bytes;
1896
1897 for (i = 0; i < multi->num_stripes; i++, stripe++) {
1898 blkdev_issue_discard(stripe->dev->bdev,
1899 stripe->physical >> 9,
1900 map_length >> 9);
1901 }
1902 kfree(multi);
1903 }
1904#endif
Chris Masona28ec192007-03-06 20:08:01 -05001905 }
Chris Mason5caf2a02007-04-02 11:20:42 -04001906 btrfs_free_path(path);
Chris Mason87ef2bb2008-10-30 11:23:27 -04001907 finish_current_insert(trans, extent_root, 0);
Chris Masona28ec192007-03-06 20:08:01 -05001908 return ret;
1909}
1910
1911/*
Chris Masonfec577f2007-02-26 10:40:21 -05001912 * find all the blocks marked as pending in the radix tree and remove
1913 * them from the extent map
1914 */
Chris Masone089f052007-03-16 16:20:31 -04001915static int del_pending_extents(struct btrfs_trans_handle *trans, struct
Chris Mason87ef2bb2008-10-30 11:23:27 -04001916 btrfs_root *extent_root, int all)
Chris Masonfec577f2007-02-26 10:40:21 -05001917{
1918 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -04001919 int err = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -04001920 u64 start;
1921 u64 end;
Zheng Yan31840ae2008-09-23 13:14:14 -04001922 u64 priv;
Josef Bacik25179202008-10-29 14:49:05 -04001923 u64 search = 0;
Chris Masond1310b22008-01-24 16:13:08 -05001924 struct extent_io_tree *pending_del;
Zheng Yan31840ae2008-09-23 13:14:14 -04001925 struct extent_io_tree *extent_ins;
1926 struct pending_extent_op *extent_op;
Josef Bacik25179202008-10-29 14:49:05 -04001927 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Mason8ef97622007-03-26 10:15:30 -04001928
Zheng Yan31840ae2008-09-23 13:14:14 -04001929 extent_ins = &extent_root->fs_info->extent_ins;
Chris Mason1a5bc162007-10-15 16:15:26 -04001930 pending_del = &extent_root->fs_info->pending_del;
Chris Masonfec577f2007-02-26 10:40:21 -05001931
1932 while(1) {
Josef Bacik25179202008-10-29 14:49:05 -04001933 mutex_lock(&info->extent_ins_mutex);
1934 ret = find_first_extent_bit(pending_del, search, &start, &end,
1935 EXTENT_WRITEBACK);
1936 if (ret) {
1937 mutex_unlock(&info->extent_ins_mutex);
Chris Mason87ef2bb2008-10-30 11:23:27 -04001938 if (all && search) {
Josef Bacik25179202008-10-29 14:49:05 -04001939 search = 0;
1940 continue;
1941 }
Chris Masonfec577f2007-02-26 10:40:21 -05001942 break;
Josef Bacik25179202008-10-29 14:49:05 -04001943 }
1944
1945 ret = try_lock_extent(extent_ins, start, end, GFP_NOFS);
1946 if (!ret) {
1947 search = end+1;
1948 mutex_unlock(&info->extent_ins_mutex);
1949 cond_resched();
1950 continue;
1951 }
1952 BUG_ON(ret < 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04001953
1954 ret = get_state_private(pending_del, start, &priv);
1955 BUG_ON(ret);
1956 extent_op = (struct pending_extent_op *)(unsigned long)priv;
1957
Josef Bacik25179202008-10-29 14:49:05 -04001958 clear_extent_bits(pending_del, start, end, EXTENT_WRITEBACK,
Chris Mason1a5bc162007-10-15 16:15:26 -04001959 GFP_NOFS);
Zheng Yan31840ae2008-09-23 13:14:14 -04001960 if (!test_range_bit(extent_ins, start, end,
Josef Bacik25179202008-10-29 14:49:05 -04001961 EXTENT_WRITEBACK, 0)) {
1962 mutex_unlock(&info->extent_ins_mutex);
Zheng Yan31840ae2008-09-23 13:14:14 -04001963free_extent:
Chris Masonc286ac42008-07-22 23:06:41 -04001964 ret = __free_extent(trans, extent_root,
Zheng Yan31840ae2008-09-23 13:14:14 -04001965 start, end + 1 - start,
1966 extent_op->orig_parent,
1967 extent_root->root_key.objectid,
1968 extent_op->orig_generation,
Josef Bacik25179202008-10-29 14:49:05 -04001969 extent_op->level, 1, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04001970 kfree(extent_op);
Chris Masonc286ac42008-07-22 23:06:41 -04001971 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04001972 kfree(extent_op);
Josef Bacik25179202008-10-29 14:49:05 -04001973
1974 ret = get_state_private(&info->extent_ins, start,
1975 &priv);
Zheng Yan31840ae2008-09-23 13:14:14 -04001976 BUG_ON(ret);
1977 extent_op = (struct pending_extent_op *)
Josef Bacik25179202008-10-29 14:49:05 -04001978 (unsigned long)priv;
Zheng Yan31840ae2008-09-23 13:14:14 -04001979
Josef Bacik25179202008-10-29 14:49:05 -04001980 clear_extent_bits(&info->extent_ins, start, end,
1981 EXTENT_WRITEBACK, GFP_NOFS);
1982
1983 mutex_unlock(&info->extent_ins_mutex);
Zheng Yan31840ae2008-09-23 13:14:14 -04001984
1985 if (extent_op->type == PENDING_BACKREF_UPDATE)
1986 goto free_extent;
1987
Josef Bacik25179202008-10-29 14:49:05 -04001988 mutex_lock(&extent_root->fs_info->pinned_mutex);
1989 ret = pin_down_bytes(trans, extent_root, start,
1990 end + 1 - start, 0);
1991 mutex_unlock(&extent_root->fs_info->pinned_mutex);
1992
Zheng Yan31840ae2008-09-23 13:14:14 -04001993 ret = update_block_group(trans, extent_root, start,
Josef Bacik25179202008-10-29 14:49:05 -04001994 end + 1 - start, 0, ret > 0);
1995
Zheng Yan31840ae2008-09-23 13:14:14 -04001996 BUG_ON(ret);
1997 kfree(extent_op);
Chris Masonc286ac42008-07-22 23:06:41 -04001998 }
Chris Mason1a5bc162007-10-15 16:15:26 -04001999 if (ret)
2000 err = ret;
Josef Bacik25179202008-10-29 14:49:05 -04002001 unlock_extent(extent_ins, start, end, GFP_NOFS);
Chris Masonc286ac42008-07-22 23:06:41 -04002002
Chris Mason87ef2bb2008-10-30 11:23:27 -04002003 if (all)
2004 search = 0;
2005 else
2006 search = end + 1;
Josef Bacik25179202008-10-29 14:49:05 -04002007 cond_resched();
Chris Masonfec577f2007-02-26 10:40:21 -05002008 }
Chris Masone20d96d2007-03-22 12:13:20 -04002009 return err;
Chris Masonfec577f2007-02-26 10:40:21 -05002010}
2011
2012/*
2013 * remove an extent from the root, returns 0 on success
2014 */
Chris Mason925baed2008-06-25 16:01:30 -04002015static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04002016 struct btrfs_root *root,
2017 u64 bytenr, u64 num_bytes, u64 parent,
2018 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002019 u64 owner_objectid, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -05002020{
Chris Mason9f5fae22007-03-20 14:38:32 -04002021 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -05002022 int pending_ret;
2023 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05002024
Chris Masondb945352007-10-15 16:15:53 -04002025 WARN_ON(num_bytes < root->sectorsize);
Chris Masona28ec192007-03-06 20:08:01 -05002026 if (root == extent_root) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002027 struct pending_extent_op *extent_op;
2028
2029 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2030 BUG_ON(!extent_op);
2031
2032 extent_op->type = PENDING_EXTENT_DELETE;
2033 extent_op->bytenr = bytenr;
2034 extent_op->num_bytes = num_bytes;
2035 extent_op->parent = parent;
2036 extent_op->orig_parent = parent;
2037 extent_op->generation = ref_generation;
2038 extent_op->orig_generation = ref_generation;
2039 extent_op->level = (int)owner_objectid;
2040
Josef Bacik25179202008-10-29 14:49:05 -04002041 mutex_lock(&root->fs_info->extent_ins_mutex);
Zheng Yan31840ae2008-09-23 13:14:14 -04002042 set_extent_bits(&root->fs_info->pending_del,
2043 bytenr, bytenr + num_bytes - 1,
Josef Bacik25179202008-10-29 14:49:05 -04002044 EXTENT_WRITEBACK, GFP_NOFS);
Zheng Yan31840ae2008-09-23 13:14:14 -04002045 set_state_private(&root->fs_info->pending_del,
2046 bytenr, (unsigned long)extent_op);
Josef Bacik25179202008-10-29 14:49:05 -04002047 mutex_unlock(&root->fs_info->extent_ins_mutex);
Chris Masona28ec192007-03-06 20:08:01 -05002048 return 0;
2049 }
Chris Mason4bef0842008-09-08 11:18:08 -04002050 /* if metadata always pin */
Chris Masond00aff02008-09-11 15:54:42 -04002051 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
2052 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04002053 struct btrfs_block_group_cache *cache;
2054
Chris Masond00aff02008-09-11 15:54:42 -04002055 /* btrfs_free_reserved_extent */
Josef Bacik0f9dd462008-09-23 13:14:11 -04002056 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
2057 BUG_ON(!cache);
2058 btrfs_add_free_space(cache, bytenr, num_bytes);
Zheng Yane8569812008-09-26 10:05:48 -04002059 update_reserved_extents(root, bytenr, num_bytes, 0);
Chris Masond00aff02008-09-11 15:54:42 -04002060 return 0;
2061 }
Chris Mason4bef0842008-09-08 11:18:08 -04002062 pin = 1;
Chris Masond00aff02008-09-11 15:54:42 -04002063 }
Chris Mason4bef0842008-09-08 11:18:08 -04002064
2065 /* if data pin when any transaction has committed this */
2066 if (ref_generation != trans->transid)
2067 pin = 1;
2068
Zheng Yan31840ae2008-09-23 13:14:14 -04002069 ret = __free_extent(trans, root, bytenr, num_bytes, parent,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002070 root_objectid, ref_generation,
2071 owner_objectid, pin, pin == 0);
Chris Masonee6e6502008-07-17 12:54:40 -04002072
Chris Mason87ef2bb2008-10-30 11:23:27 -04002073 finish_current_insert(trans, root->fs_info->extent_root, 0);
2074 pending_ret = del_pending_extents(trans, root->fs_info->extent_root, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05002075 return ret ? ret : pending_ret;
2076}
2077
Chris Mason925baed2008-06-25 16:01:30 -04002078int btrfs_free_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04002079 struct btrfs_root *root,
2080 u64 bytenr, u64 num_bytes, u64 parent,
2081 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002082 u64 owner_objectid, int pin)
Chris Mason925baed2008-06-25 16:01:30 -04002083{
2084 int ret;
2085
Zheng Yan31840ae2008-09-23 13:14:14 -04002086 ret = __btrfs_free_extent(trans, root, bytenr, num_bytes, parent,
Chris Mason925baed2008-06-25 16:01:30 -04002087 root_objectid, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002088 owner_objectid, pin);
Chris Mason925baed2008-06-25 16:01:30 -04002089 return ret;
2090}
2091
Chris Mason87ee04e2007-11-30 11:30:34 -05002092static u64 stripe_align(struct btrfs_root *root, u64 val)
2093{
2094 u64 mask = ((u64)root->stripesize - 1);
2095 u64 ret = (val + mask) & ~mask;
2096 return ret;
2097}
2098
Chris Masonfec577f2007-02-26 10:40:21 -05002099/*
2100 * walks the btree of allocated extents and find a hole of a given size.
2101 * The key ins is changed to record the hole:
2102 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04002103 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05002104 * ins->offset == number of blocks
2105 * Any available blocks before search_start are skipped.
2106 */
Chris Mason98ed5172008-01-03 10:01:48 -05002107static int noinline find_free_extent(struct btrfs_trans_handle *trans,
2108 struct btrfs_root *orig_root,
2109 u64 num_bytes, u64 empty_size,
2110 u64 search_start, u64 search_end,
2111 u64 hint_byte, struct btrfs_key *ins,
2112 u64 exclude_start, u64 exclude_nr,
2113 int data)
Chris Masonfec577f2007-02-26 10:40:21 -05002114{
Josef Bacik80eb2342008-10-29 14:49:05 -04002115 int ret = 0;
Chris Mason9f5fae22007-03-20 14:38:32 -04002116 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Masondb945352007-10-15 16:15:53 -04002117 u64 total_needed = num_bytes;
Chris Mason239b14b2008-03-24 15:02:07 -04002118 u64 *last_ptr = NULL;
Chris Mason43662112008-11-07 09:06:11 -05002119 u64 last_wanted = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04002120 struct btrfs_block_group_cache *block_group = NULL;
Chris Mason0ef3e662008-05-24 14:04:53 -04002121 int chunk_alloc_done = 0;
Chris Mason239b14b2008-03-24 15:02:07 -04002122 int empty_cluster = 2 * 1024 * 1024;
Chris Mason0ef3e662008-05-24 14:04:53 -04002123 int allowed_chunk_alloc = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04002124 struct list_head *head = NULL, *cur = NULL;
2125 int loop = 0;
Chris Masonf5a31e12008-11-10 11:47:09 -05002126 int extra_loop = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04002127 struct btrfs_space_info *space_info;
Chris Masonfec577f2007-02-26 10:40:21 -05002128
Chris Masondb945352007-10-15 16:15:53 -04002129 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04002130 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
Josef Bacik80eb2342008-10-29 14:49:05 -04002131 ins->objectid = 0;
2132 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04002133
Chris Mason0ef3e662008-05-24 14:04:53 -04002134 if (orig_root->ref_cows || empty_size)
2135 allowed_chunk_alloc = 1;
2136
Chris Mason239b14b2008-03-24 15:02:07 -04002137 if (data & BTRFS_BLOCK_GROUP_METADATA) {
2138 last_ptr = &root->fs_info->last_alloc;
Chris Mason43662112008-11-07 09:06:11 -05002139 empty_cluster = 64 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04002140 }
2141
Josef Bacik0f9dd462008-09-23 13:14:11 -04002142 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD))
Chris Mason239b14b2008-03-24 15:02:07 -04002143 last_ptr = &root->fs_info->last_data_alloc;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002144
Chris Mason239b14b2008-03-24 15:02:07 -04002145 if (last_ptr) {
Chris Mason43662112008-11-07 09:06:11 -05002146 if (*last_ptr) {
Chris Mason239b14b2008-03-24 15:02:07 -04002147 hint_byte = *last_ptr;
Chris Mason43662112008-11-07 09:06:11 -05002148 last_wanted = *last_ptr;
2149 } else
Chris Mason239b14b2008-03-24 15:02:07 -04002150 empty_size += empty_cluster;
Chris Mason43662112008-11-07 09:06:11 -05002151 } else {
2152 empty_cluster = 0;
Chris Mason239b14b2008-03-24 15:02:07 -04002153 }
Chris Masona061fc82008-05-07 11:43:44 -04002154 search_start = max(search_start, first_logical_byte(root, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04002155 search_start = max(search_start, hint_byte);
Chris Mason0b86a832008-03-24 15:01:56 -04002156
Chris Mason42e70e72008-11-07 18:17:11 -05002157 if (last_wanted && search_start != last_wanted) {
Chris Mason43662112008-11-07 09:06:11 -05002158 last_wanted = 0;
Chris Mason42e70e72008-11-07 18:17:11 -05002159 empty_size += empty_cluster;
2160 }
Chris Mason43662112008-11-07 09:06:11 -05002161
Chris Mason42e70e72008-11-07 18:17:11 -05002162 total_needed += empty_size;
Josef Bacik80eb2342008-10-29 14:49:05 -04002163 block_group = btrfs_lookup_block_group(root->fs_info, search_start);
Yan Zhengd899e052008-10-30 14:25:28 -04002164 if (!block_group)
2165 block_group = btrfs_lookup_first_block_group(root->fs_info,
2166 search_start);
Josef Bacik80eb2342008-10-29 14:49:05 -04002167 space_info = __find_space_info(root->fs_info, data);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002168
Josef Bacik80eb2342008-10-29 14:49:05 -04002169 down_read(&space_info->groups_sem);
2170 while (1) {
2171 struct btrfs_free_space *free_space;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002172 /*
Josef Bacik80eb2342008-10-29 14:49:05 -04002173 * the only way this happens if our hint points to a block
2174 * group thats not of the proper type, while looping this
2175 * should never happen
Josef Bacik0f9dd462008-09-23 13:14:11 -04002176 */
Chris Mason8a1413a2008-11-10 16:13:54 -05002177 if (empty_size)
2178 extra_loop = 1;
2179
Chris Mason42e70e72008-11-07 18:17:11 -05002180 if (!block_group)
2181 goto new_group_no_lock;
2182
Josef Bacik25179202008-10-29 14:49:05 -04002183 mutex_lock(&block_group->alloc_mutex);
Josef Bacik80eb2342008-10-29 14:49:05 -04002184 if (unlikely(!block_group_bits(block_group, data)))
Josef Bacik0f9dd462008-09-23 13:14:11 -04002185 goto new_group;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002186
Josef Bacik80eb2342008-10-29 14:49:05 -04002187 ret = cache_block_group(root, block_group);
Josef Bacik25179202008-10-29 14:49:05 -04002188 if (ret) {
2189 mutex_unlock(&block_group->alloc_mutex);
Josef Bacik80eb2342008-10-29 14:49:05 -04002190 break;
Josef Bacik25179202008-10-29 14:49:05 -04002191 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002192
Josef Bacik80eb2342008-10-29 14:49:05 -04002193 if (block_group->ro)
2194 goto new_group;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002195
Josef Bacik80eb2342008-10-29 14:49:05 -04002196 free_space = btrfs_find_free_space(block_group, search_start,
2197 total_needed);
2198 if (free_space) {
2199 u64 start = block_group->key.objectid;
2200 u64 end = block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -04002201 block_group->key.offset;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002202
Josef Bacik80eb2342008-10-29 14:49:05 -04002203 search_start = stripe_align(root, free_space->offset);
Chris Mason0b86a832008-03-24 15:01:56 -04002204
Josef Bacik80eb2342008-10-29 14:49:05 -04002205 /* move on to the next group */
2206 if (search_start + num_bytes >= search_end)
2207 goto new_group;
Chris Masone37c9e62007-05-09 20:13:14 -04002208
Josef Bacik80eb2342008-10-29 14:49:05 -04002209 /* move on to the next group */
2210 if (search_start + num_bytes > end)
2211 goto new_group;
2212
Chris Mason43662112008-11-07 09:06:11 -05002213 if (last_wanted && search_start != last_wanted) {
Chris Mason3b7885b2008-11-06 21:48:27 -05002214 total_needed += empty_cluster;
Chris Mason8a1413a2008-11-10 16:13:54 -05002215 empty_size += empty_cluster;
Chris Mason43662112008-11-07 09:06:11 -05002216 last_wanted = 0;
Chris Mason3b7885b2008-11-06 21:48:27 -05002217 /*
2218 * if search_start is still in this block group
2219 * then we just re-search this block group
2220 */
2221 if (search_start >= start &&
2222 search_start < end) {
2223 mutex_unlock(&block_group->alloc_mutex);
2224 continue;
2225 }
2226
2227 /* else we go to the next block group */
2228 goto new_group;
2229 }
2230
Josef Bacik80eb2342008-10-29 14:49:05 -04002231 if (exclude_nr > 0 &&
2232 (search_start + num_bytes > exclude_start &&
2233 search_start < exclude_start + exclude_nr)) {
2234 search_start = exclude_start + exclude_nr;
2235 /*
2236 * if search_start is still in this block group
2237 * then we just re-search this block group
2238 */
2239 if (search_start >= start &&
Josef Bacik25179202008-10-29 14:49:05 -04002240 search_start < end) {
2241 mutex_unlock(&block_group->alloc_mutex);
Chris Mason43662112008-11-07 09:06:11 -05002242 last_wanted = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04002243 continue;
Josef Bacik25179202008-10-29 14:49:05 -04002244 }
Josef Bacik80eb2342008-10-29 14:49:05 -04002245
2246 /* else we go to the next block group */
2247 goto new_group;
2248 }
2249
2250 ins->objectid = search_start;
2251 ins->offset = num_bytes;
Josef Bacik25179202008-10-29 14:49:05 -04002252
2253 btrfs_remove_free_space_lock(block_group, search_start,
2254 num_bytes);
Josef Bacik80eb2342008-10-29 14:49:05 -04002255 /* we are all good, lets return */
Josef Bacik25179202008-10-29 14:49:05 -04002256 mutex_unlock(&block_group->alloc_mutex);
Josef Bacik80eb2342008-10-29 14:49:05 -04002257 break;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002258 }
Josef Bacik80eb2342008-10-29 14:49:05 -04002259new_group:
Chris Mason42e70e72008-11-07 18:17:11 -05002260 mutex_unlock(&block_group->alloc_mutex);
2261new_group_no_lock:
Chris Masonf5a31e12008-11-10 11:47:09 -05002262 /* don't try to compare new allocations against the
2263 * last allocation any more
2264 */
Chris Mason43662112008-11-07 09:06:11 -05002265 last_wanted = 0;
Chris Masonf5a31e12008-11-10 11:47:09 -05002266
Josef Bacik80eb2342008-10-29 14:49:05 -04002267 /*
2268 * Here's how this works.
2269 * loop == 0: we were searching a block group via a hint
2270 * and didn't find anything, so we start at
2271 * the head of the block groups and keep searching
2272 * loop == 1: we're searching through all of the block groups
2273 * if we hit the head again we have searched
2274 * all of the block groups for this space and we
2275 * need to try and allocate, if we cant error out.
2276 * loop == 2: we allocated more space and are looping through
2277 * all of the block groups again.
2278 */
2279 if (loop == 0) {
2280 head = &space_info->block_groups;
2281 cur = head->next;
Josef Bacik80eb2342008-10-29 14:49:05 -04002282 loop++;
2283 } else if (loop == 1 && cur == head) {
Chris Masonf5a31e12008-11-10 11:47:09 -05002284 int keep_going;
Chris Mason42e70e72008-11-07 18:17:11 -05002285
Chris Masonf5a31e12008-11-10 11:47:09 -05002286 /* at this point we give up on the empty_size
2287 * allocations and just try to allocate the min
2288 * space.
2289 *
2290 * The extra_loop field was set if an empty_size
2291 * allocation was attempted above, and if this
2292 * is try we need to try the loop again without
2293 * the additional empty_size.
2294 */
Chris Mason5b7c3fc2008-11-10 07:26:33 -05002295 total_needed -= empty_size;
2296 empty_size = 0;
Chris Masonf5a31e12008-11-10 11:47:09 -05002297 keep_going = extra_loop;
2298 loop++;
Chris Mason42e70e72008-11-07 18:17:11 -05002299
Josef Bacik80eb2342008-10-29 14:49:05 -04002300 if (allowed_chunk_alloc && !chunk_alloc_done) {
2301 up_read(&space_info->groups_sem);
2302 ret = do_chunk_alloc(trans, root, num_bytes +
2303 2 * 1024 * 1024, data, 1);
Josef Bacik80eb2342008-10-29 14:49:05 -04002304 down_read(&space_info->groups_sem);
Chris Mason2ed6d662008-11-13 09:59:33 -05002305 if (ret < 0)
2306 goto loop_check;
Josef Bacik80eb2342008-10-29 14:49:05 -04002307 head = &space_info->block_groups;
Chris Masonf5a31e12008-11-10 11:47:09 -05002308 /*
2309 * we've allocated a new chunk, keep
2310 * trying
2311 */
2312 keep_going = 1;
Josef Bacik80eb2342008-10-29 14:49:05 -04002313 chunk_alloc_done = 1;
2314 } else if (!allowed_chunk_alloc) {
2315 space_info->force_alloc = 1;
Chris Masonf5a31e12008-11-10 11:47:09 -05002316 }
Chris Mason2ed6d662008-11-13 09:59:33 -05002317loop_check:
Chris Masonf5a31e12008-11-10 11:47:09 -05002318 if (keep_going) {
2319 cur = head->next;
2320 extra_loop = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04002321 } else {
2322 break;
2323 }
2324 } else if (cur == head) {
2325 break;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002326 }
Josef Bacik80eb2342008-10-29 14:49:05 -04002327
2328 block_group = list_entry(cur, struct btrfs_block_group_cache,
2329 list);
2330 search_start = block_group->key.objectid;
2331 cur = cur->next;
Chris Masone19caa52007-10-15 16:17:44 -04002332 }
Chris Mason0b86a832008-03-24 15:01:56 -04002333
Josef Bacik80eb2342008-10-29 14:49:05 -04002334 /* we found what we needed */
2335 if (ins->objectid) {
2336 if (!(data & BTRFS_BLOCK_GROUP_DATA))
2337 trans->block_group = block_group;
2338
2339 if (last_ptr)
2340 *last_ptr = ins->objectid + ins->offset;
2341 ret = 0;
2342 } else if (!ret) {
2343 ret = -ENOSPC;
Chris Masonf2654de2007-06-26 12:20:46 -04002344 }
Chris Mason0b86a832008-03-24 15:01:56 -04002345
Josef Bacik80eb2342008-10-29 14:49:05 -04002346 up_read(&space_info->groups_sem);
Chris Mason0f70abe2007-02-28 16:46:22 -05002347 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05002348}
Chris Masonec44a352008-04-28 15:29:52 -04002349
Josef Bacik0f9dd462008-09-23 13:14:11 -04002350static void dump_space_info(struct btrfs_space_info *info, u64 bytes)
2351{
2352 struct btrfs_block_group_cache *cache;
2353 struct list_head *l;
2354
2355 printk(KERN_INFO "space_info has %Lu free, is %sfull\n",
Zheng Yane8569812008-09-26 10:05:48 -04002356 info->total_bytes - info->bytes_used - info->bytes_pinned -
2357 info->bytes_reserved, (info->full) ? "" : "not ");
Josef Bacik0f9dd462008-09-23 13:14:11 -04002358
Josef Bacik80eb2342008-10-29 14:49:05 -04002359 down_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002360 list_for_each(l, &info->block_groups) {
2361 cache = list_entry(l, struct btrfs_block_group_cache, list);
2362 spin_lock(&cache->lock);
2363 printk(KERN_INFO "block group %Lu has %Lu bytes, %Lu used "
Zheng Yane8569812008-09-26 10:05:48 -04002364 "%Lu pinned %Lu reserved\n",
Josef Bacik0f9dd462008-09-23 13:14:11 -04002365 cache->key.objectid, cache->key.offset,
Zheng Yane8569812008-09-26 10:05:48 -04002366 btrfs_block_group_used(&cache->item),
2367 cache->pinned, cache->reserved);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002368 btrfs_dump_free_space(cache, bytes);
2369 spin_unlock(&cache->lock);
2370 }
Josef Bacik80eb2342008-10-29 14:49:05 -04002371 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002372}
Zheng Yane8569812008-09-26 10:05:48 -04002373
Chris Masone6dcd2d2008-07-17 12:53:50 -04002374static int __btrfs_reserve_extent(struct btrfs_trans_handle *trans,
2375 struct btrfs_root *root,
2376 u64 num_bytes, u64 min_alloc_size,
2377 u64 empty_size, u64 hint_byte,
2378 u64 search_end, struct btrfs_key *ins,
2379 u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05002380{
2381 int ret;
Chris Masonfbdc7622007-05-30 10:22:12 -04002382 u64 search_start = 0;
Chris Mason8790d502008-04-03 16:29:03 -04002383 u64 alloc_profile;
Chris Mason1261ec42007-03-20 20:35:03 -04002384 struct btrfs_fs_info *info = root->fs_info;
Chris Mason925baed2008-06-25 16:01:30 -04002385
Chris Mason6324fbf2008-03-24 15:01:59 -04002386 if (data) {
Chris Mason8790d502008-04-03 16:29:03 -04002387 alloc_profile = info->avail_data_alloc_bits &
2388 info->data_alloc_profile;
2389 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04002390 } else if (root == root->fs_info->chunk_root) {
Chris Mason8790d502008-04-03 16:29:03 -04002391 alloc_profile = info->avail_system_alloc_bits &
2392 info->system_alloc_profile;
2393 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04002394 } else {
Chris Mason8790d502008-04-03 16:29:03 -04002395 alloc_profile = info->avail_metadata_alloc_bits &
2396 info->metadata_alloc_profile;
2397 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04002398 }
Chris Mason98d20f62008-04-14 09:46:10 -04002399again:
Chris Masona061fc82008-05-07 11:43:44 -04002400 data = reduce_alloc_profile(root, data);
Chris Mason0ef3e662008-05-24 14:04:53 -04002401 /*
2402 * the only place that sets empty_size is btrfs_realloc_node, which
2403 * is not called recursively on allocations
2404 */
2405 if (empty_size || root->ref_cows) {
Chris Mason593060d2008-03-25 16:50:33 -04002406 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
Chris Mason6324fbf2008-03-24 15:01:59 -04002407 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04002408 2 * 1024 * 1024,
2409 BTRFS_BLOCK_GROUP_METADATA |
2410 (info->metadata_alloc_profile &
2411 info->avail_metadata_alloc_bits), 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04002412 }
2413 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04002414 num_bytes + 2 * 1024 * 1024, data, 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04002415 }
Chris Mason0b86a832008-03-24 15:01:56 -04002416
Chris Masondb945352007-10-15 16:15:53 -04002417 WARN_ON(num_bytes < root->sectorsize);
2418 ret = find_free_extent(trans, root, num_bytes, empty_size,
2419 search_start, search_end, hint_byte, ins,
Chris Mason26b80032007-08-08 20:17:12 -04002420 trans->alloc_exclude_start,
2421 trans->alloc_exclude_nr, data);
Chris Mason3b951512008-04-17 11:29:12 -04002422
Chris Mason98d20f62008-04-14 09:46:10 -04002423 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
2424 num_bytes = num_bytes >> 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002425 num_bytes = num_bytes & ~(root->sectorsize - 1);
Chris Mason98d20f62008-04-14 09:46:10 -04002426 num_bytes = max(num_bytes, min_alloc_size);
Chris Mason0ef3e662008-05-24 14:04:53 -04002427 do_chunk_alloc(trans, root->fs_info->extent_root,
2428 num_bytes, data, 1);
Chris Mason98d20f62008-04-14 09:46:10 -04002429 goto again;
2430 }
Chris Masonec44a352008-04-28 15:29:52 -04002431 if (ret) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04002432 struct btrfs_space_info *sinfo;
2433
2434 sinfo = __find_space_info(root->fs_info, data);
2435 printk("allocation failed flags %Lu, wanted %Lu\n",
2436 data, num_bytes);
2437 dump_space_info(sinfo, num_bytes);
Chris Mason925baed2008-06-25 16:01:30 -04002438 BUG();
Chris Mason925baed2008-06-25 16:01:30 -04002439 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002440
2441 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002442}
2443
Chris Mason65b51a02008-08-01 15:11:20 -04002444int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
2445{
Josef Bacik0f9dd462008-09-23 13:14:11 -04002446 struct btrfs_block_group_cache *cache;
2447
Josef Bacik0f9dd462008-09-23 13:14:11 -04002448 cache = btrfs_lookup_block_group(root->fs_info, start);
2449 if (!cache) {
2450 printk(KERN_ERR "Unable to find block group for %Lu\n", start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002451 return -ENOSPC;
2452 }
2453 btrfs_add_free_space(cache, start, len);
Zheng Yan1a40e232008-09-26 10:09:34 -04002454 update_reserved_extents(root, start, len, 0);
Chris Mason65b51a02008-08-01 15:11:20 -04002455 return 0;
2456}
2457
Chris Masone6dcd2d2008-07-17 12:53:50 -04002458int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
2459 struct btrfs_root *root,
2460 u64 num_bytes, u64 min_alloc_size,
2461 u64 empty_size, u64 hint_byte,
2462 u64 search_end, struct btrfs_key *ins,
2463 u64 data)
2464{
2465 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002466 ret = __btrfs_reserve_extent(trans, root, num_bytes, min_alloc_size,
2467 empty_size, hint_byte, search_end, ins,
2468 data);
Zheng Yane8569812008-09-26 10:05:48 -04002469 update_reserved_extents(root, ins->objectid, ins->offset, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002470 return ret;
2471}
2472
2473static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04002474 struct btrfs_root *root, u64 parent,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002475 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002476 u64 owner, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002477{
2478 int ret;
2479 int pending_ret;
2480 u64 super_used;
2481 u64 root_used;
2482 u64 num_bytes = ins->offset;
2483 u32 sizes[2];
2484 struct btrfs_fs_info *info = root->fs_info;
2485 struct btrfs_root *extent_root = info->extent_root;
2486 struct btrfs_extent_item *extent_item;
2487 struct btrfs_extent_ref *ref;
2488 struct btrfs_path *path;
2489 struct btrfs_key keys[2];
Chris Masonf2654de2007-06-26 12:20:46 -04002490
Zheng Yan31840ae2008-09-23 13:14:14 -04002491 if (parent == 0)
2492 parent = ins->objectid;
2493
Josef Bacik58176a92007-08-29 15:47:34 -04002494 /* block accounting for super block */
Chris Masona2135012008-06-25 16:01:30 -04002495 spin_lock_irq(&info->delalloc_lock);
Chris Masondb945352007-10-15 16:15:53 -04002496 super_used = btrfs_super_bytes_used(&info->super_copy);
2497 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
Chris Masona2135012008-06-25 16:01:30 -04002498 spin_unlock_irq(&info->delalloc_lock);
Chris Mason26b80032007-08-08 20:17:12 -04002499
Josef Bacik58176a92007-08-29 15:47:34 -04002500 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04002501 root_used = btrfs_root_used(&root->root_item);
2502 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -04002503
Chris Mason26b80032007-08-08 20:17:12 -04002504 if (root == extent_root) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002505 struct pending_extent_op *extent_op;
2506
2507 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2508 BUG_ON(!extent_op);
2509
2510 extent_op->type = PENDING_EXTENT_INSERT;
2511 extent_op->bytenr = ins->objectid;
2512 extent_op->num_bytes = ins->offset;
2513 extent_op->parent = parent;
2514 extent_op->orig_parent = 0;
2515 extent_op->generation = ref_generation;
2516 extent_op->orig_generation = 0;
2517 extent_op->level = (int)owner;
2518
Josef Bacik25179202008-10-29 14:49:05 -04002519 mutex_lock(&root->fs_info->extent_ins_mutex);
Chris Mason1a5bc162007-10-15 16:15:26 -04002520 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
2521 ins->objectid + ins->offset - 1,
Josef Bacik25179202008-10-29 14:49:05 -04002522 EXTENT_WRITEBACK, GFP_NOFS);
Zheng Yan31840ae2008-09-23 13:14:14 -04002523 set_state_private(&root->fs_info->extent_ins,
2524 ins->objectid, (unsigned long)extent_op);
Josef Bacik25179202008-10-29 14:49:05 -04002525 mutex_unlock(&root->fs_info->extent_ins_mutex);
Chris Mason26b80032007-08-08 20:17:12 -04002526 goto update_block;
2527 }
2528
Chris Mason47e4bb92008-02-01 14:51:59 -05002529 memcpy(&keys[0], ins, sizeof(*ins));
Chris Mason47e4bb92008-02-01 14:51:59 -05002530 keys[1].objectid = ins->objectid;
2531 keys[1].type = BTRFS_EXTENT_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04002532 keys[1].offset = parent;
Chris Mason47e4bb92008-02-01 14:51:59 -05002533 sizes[0] = sizeof(*extent_item);
2534 sizes[1] = sizeof(*ref);
Chris Mason7bb86312007-12-11 09:25:06 -05002535
2536 path = btrfs_alloc_path();
2537 BUG_ON(!path);
Chris Mason47e4bb92008-02-01 14:51:59 -05002538
2539 ret = btrfs_insert_empty_items(trans, extent_root, path, keys,
2540 sizes, 2);
Chris Masonccd467d2007-06-28 15:57:36 -04002541 BUG_ON(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002542
Chris Mason47e4bb92008-02-01 14:51:59 -05002543 extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2544 struct btrfs_extent_item);
2545 btrfs_set_extent_refs(path->nodes[0], extent_item, 1);
2546 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
2547 struct btrfs_extent_ref);
2548
2549 btrfs_set_ref_root(path->nodes[0], ref, root_objectid);
2550 btrfs_set_ref_generation(path->nodes[0], ref, ref_generation);
2551 btrfs_set_ref_objectid(path->nodes[0], ref, owner);
Zheng Yan31840ae2008-09-23 13:14:14 -04002552 btrfs_set_ref_num_refs(path->nodes[0], ref, 1);
Chris Mason47e4bb92008-02-01 14:51:59 -05002553
2554 btrfs_mark_buffer_dirty(path->nodes[0]);
2555
2556 trans->alloc_exclude_start = 0;
2557 trans->alloc_exclude_nr = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05002558 btrfs_free_path(path);
Chris Mason87ef2bb2008-10-30 11:23:27 -04002559 finish_current_insert(trans, extent_root, 0);
2560 pending_ret = del_pending_extents(trans, extent_root, 0);
Chris Masonf510cfe2007-10-15 16:14:48 -04002561
Chris Mason925baed2008-06-25 16:01:30 -04002562 if (ret)
2563 goto out;
Chris Masone37c9e62007-05-09 20:13:14 -04002564 if (pending_ret) {
Chris Mason925baed2008-06-25 16:01:30 -04002565 ret = pending_ret;
2566 goto out;
Chris Masone37c9e62007-05-09 20:13:14 -04002567 }
Chris Mason26b80032007-08-08 20:17:12 -04002568
2569update_block:
Chris Mason0b86a832008-03-24 15:01:56 -04002570 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0);
Chris Masonf5947062008-02-04 10:10:13 -05002571 if (ret) {
2572 printk("update block group failed for %Lu %Lu\n",
2573 ins->objectid, ins->offset);
2574 BUG();
2575 }
Chris Mason925baed2008-06-25 16:01:30 -04002576out:
Chris Masone6dcd2d2008-07-17 12:53:50 -04002577 return ret;
2578}
2579
2580int btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04002581 struct btrfs_root *root, u64 parent,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002582 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002583 u64 owner, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002584{
2585 int ret;
Chris Mason1c2308f2008-09-23 13:14:13 -04002586
2587 if (root_objectid == BTRFS_TREE_LOG_OBJECTID)
2588 return 0;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002589 ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid,
2590 ref_generation, owner, ins);
Zheng Yane8569812008-09-26 10:05:48 -04002591 update_reserved_extents(root, ins->objectid, ins->offset, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002592 return ret;
2593}
Chris Masone02119d2008-09-05 16:13:11 -04002594
2595/*
2596 * this is used by the tree logging recovery code. It records that
2597 * an extent has been allocated and makes sure to clear the free
2598 * space cache bits as well
2599 */
2600int btrfs_alloc_logged_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04002601 struct btrfs_root *root, u64 parent,
Chris Masone02119d2008-09-05 16:13:11 -04002602 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002603 u64 owner, struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04002604{
2605 int ret;
2606 struct btrfs_block_group_cache *block_group;
2607
Chris Masone02119d2008-09-05 16:13:11 -04002608 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
Josef Bacik25179202008-10-29 14:49:05 -04002609 mutex_lock(&block_group->alloc_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04002610 cache_block_group(root, block_group);
2611
Josef Bacik25179202008-10-29 14:49:05 -04002612 ret = btrfs_remove_free_space_lock(block_group, ins->objectid,
2613 ins->offset);
2614 mutex_unlock(&block_group->alloc_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002615 BUG_ON(ret);
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002616 ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid,
2617 ref_generation, owner, ins);
Chris Masone02119d2008-09-05 16:13:11 -04002618 return ret;
2619}
2620
Chris Masone6dcd2d2008-07-17 12:53:50 -04002621/*
2622 * finds a free extent and does all the dirty work required for allocation
2623 * returns the key for the extent through ins, and a tree buffer for
2624 * the first block of the extent through buf.
2625 *
2626 * returns 0 if everything worked, non-zero otherwise.
2627 */
2628int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
2629 struct btrfs_root *root,
Zheng Yan31840ae2008-09-23 13:14:14 -04002630 u64 num_bytes, u64 parent, u64 min_alloc_size,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002631 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002632 u64 owner_objectid, u64 empty_size, u64 hint_byte,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002633 u64 search_end, struct btrfs_key *ins, u64 data)
2634{
2635 int ret;
2636
Chris Masone6dcd2d2008-07-17 12:53:50 -04002637 ret = __btrfs_reserve_extent(trans, root, num_bytes,
2638 min_alloc_size, empty_size, hint_byte,
2639 search_end, ins, data);
2640 BUG_ON(ret);
Chris Masond00aff02008-09-11 15:54:42 -04002641 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002642 ret = __btrfs_alloc_reserved_extent(trans, root, parent,
2643 root_objectid, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002644 owner_objectid, ins);
Chris Masond00aff02008-09-11 15:54:42 -04002645 BUG_ON(ret);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002646
Zheng Yane8569812008-09-26 10:05:48 -04002647 } else {
2648 update_reserved_extents(root, ins->objectid, ins->offset, 1);
Chris Masond00aff02008-09-11 15:54:42 -04002649 }
Chris Mason925baed2008-06-25 16:01:30 -04002650 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05002651}
Chris Mason65b51a02008-08-01 15:11:20 -04002652
2653struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
2654 struct btrfs_root *root,
2655 u64 bytenr, u32 blocksize)
2656{
2657 struct extent_buffer *buf;
2658
2659 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
2660 if (!buf)
2661 return ERR_PTR(-ENOMEM);
2662 btrfs_set_header_generation(buf, trans->transid);
2663 btrfs_tree_lock(buf);
2664 clean_tree_block(trans, root, buf);
2665 btrfs_set_buffer_uptodate(buf);
Chris Masond0c803c2008-09-11 16:17:57 -04002666 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
2667 set_extent_dirty(&root->dirty_log_pages, buf->start,
Chris Mason65b51a02008-08-01 15:11:20 -04002668 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04002669 } else {
2670 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
2671 buf->start + buf->len - 1, GFP_NOFS);
2672 }
Chris Mason65b51a02008-08-01 15:11:20 -04002673 trans->blocks_used++;
2674 return buf;
2675}
2676
Chris Masonfec577f2007-02-26 10:40:21 -05002677/*
2678 * helper function to allocate a block for a given tree
2679 * returns the tree buffer or NULL.
2680 */
Chris Mason5f39d392007-10-15 16:14:19 -04002681struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Masondb945352007-10-15 16:15:53 -04002682 struct btrfs_root *root,
Zheng Yan31840ae2008-09-23 13:14:14 -04002683 u32 blocksize, u64 parent,
Chris Mason7bb86312007-12-11 09:25:06 -05002684 u64 root_objectid,
2685 u64 ref_generation,
Chris Mason7bb86312007-12-11 09:25:06 -05002686 int level,
2687 u64 hint,
Chris Mason5f39d392007-10-15 16:14:19 -04002688 u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05002689{
Chris Masone2fa7222007-03-12 16:22:34 -04002690 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05002691 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04002692 struct extent_buffer *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05002693
Zheng Yan31840ae2008-09-23 13:14:14 -04002694 ret = btrfs_alloc_extent(trans, root, blocksize, parent, blocksize,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002695 root_objectid, ref_generation, level,
Zheng Yan31840ae2008-09-23 13:14:14 -04002696 empty_size, hint, (u64)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05002697 if (ret) {
Chris Mason54aa1f42007-06-22 14:16:25 -04002698 BUG_ON(ret > 0);
2699 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05002700 }
Chris Mason55c69072008-01-09 15:55:33 -05002701
Chris Mason65b51a02008-08-01 15:11:20 -04002702 buf = btrfs_init_new_buffer(trans, root, ins.objectid, blocksize);
Chris Masonfec577f2007-02-26 10:40:21 -05002703 return buf;
2704}
Chris Masona28ec192007-03-06 20:08:01 -05002705
Chris Masone02119d2008-09-05 16:13:11 -04002706int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans,
2707 struct btrfs_root *root, struct extent_buffer *leaf)
Chris Mason6407bf62007-03-27 06:33:00 -04002708{
Chris Mason7bb86312007-12-11 09:25:06 -05002709 u64 leaf_owner;
2710 u64 leaf_generation;
Chris Mason5f39d392007-10-15 16:14:19 -04002711 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04002712 struct btrfs_file_extent_item *fi;
2713 int i;
2714 int nritems;
2715 int ret;
2716
Chris Mason5f39d392007-10-15 16:14:19 -04002717 BUG_ON(!btrfs_is_leaf(leaf));
2718 nritems = btrfs_header_nritems(leaf);
Chris Mason7bb86312007-12-11 09:25:06 -05002719 leaf_owner = btrfs_header_owner(leaf);
2720 leaf_generation = btrfs_header_generation(leaf);
2721
Chris Mason6407bf62007-03-27 06:33:00 -04002722 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04002723 u64 disk_bytenr;
Chris Masone34a5b42008-07-22 12:08:37 -04002724 cond_resched();
Chris Mason5f39d392007-10-15 16:14:19 -04002725
2726 btrfs_item_key_to_cpu(leaf, &key, i);
2727 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -04002728 continue;
2729 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002730 if (btrfs_file_extent_type(leaf, fi) ==
2731 BTRFS_FILE_EXTENT_INLINE)
Chris Mason236454df2007-04-19 13:37:44 -04002732 continue;
Chris Mason6407bf62007-03-27 06:33:00 -04002733 /*
2734 * FIXME make sure to insert a trans record that
2735 * repeats the snapshot del on crash
2736 */
Chris Masondb945352007-10-15 16:15:53 -04002737 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
2738 if (disk_bytenr == 0)
Chris Mason3a686372007-05-24 13:35:57 -04002739 continue;
Chris Mason4a096752008-07-21 10:29:44 -04002740
Chris Mason925baed2008-06-25 16:01:30 -04002741 ret = __btrfs_free_extent(trans, root, disk_bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -05002742 btrfs_file_extent_disk_num_bytes(leaf, fi),
Zheng Yan31840ae2008-09-23 13:14:14 -04002743 leaf->start, leaf_owner, leaf_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002744 key.objectid, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002745 BUG_ON(ret);
Chris Mason2dd3e672008-08-04 08:20:15 -04002746
2747 atomic_inc(&root->fs_info->throttle_gen);
2748 wake_up(&root->fs_info->transaction_throttle);
2749 cond_resched();
Chris Mason6407bf62007-03-27 06:33:00 -04002750 }
2751 return 0;
2752}
2753
Chris Masone02119d2008-09-05 16:13:11 -04002754static int noinline cache_drop_leaf_ref(struct btrfs_trans_handle *trans,
2755 struct btrfs_root *root,
2756 struct btrfs_leaf_ref *ref)
Yan Zheng31153d82008-07-28 15:32:19 -04002757{
2758 int i;
2759 int ret;
2760 struct btrfs_extent_info *info = ref->extents;
2761
Yan Zheng31153d82008-07-28 15:32:19 -04002762 for (i = 0; i < ref->nritems; i++) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002763 ret = __btrfs_free_extent(trans, root, info->bytenr,
2764 info->num_bytes, ref->bytenr,
2765 ref->owner, ref->generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002766 info->objectid, 0);
Chris Mason2dd3e672008-08-04 08:20:15 -04002767
2768 atomic_inc(&root->fs_info->throttle_gen);
2769 wake_up(&root->fs_info->transaction_throttle);
2770 cond_resched();
2771
Yan Zheng31153d82008-07-28 15:32:19 -04002772 BUG_ON(ret);
2773 info++;
2774 }
Yan Zheng31153d82008-07-28 15:32:19 -04002775
2776 return 0;
2777}
2778
Chris Mason333db942008-06-25 16:01:30 -04002779int drop_snap_lookup_refcount(struct btrfs_root *root, u64 start, u64 len,
2780 u32 *refs)
2781{
Chris Mason017e5362008-07-28 15:32:51 -04002782 int ret;
Chris Masonf87f0572008-08-01 11:27:23 -04002783
Zheng Yan31840ae2008-09-23 13:14:14 -04002784 ret = btrfs_lookup_extent_ref(NULL, root, start, len, refs);
Chris Masonf87f0572008-08-01 11:27:23 -04002785 BUG_ON(ret);
2786
2787#if 0 // some debugging code in case we see problems here
2788 /* if the refs count is one, it won't get increased again. But
2789 * if the ref count is > 1, someone may be decreasing it at
2790 * the same time we are.
2791 */
2792 if (*refs != 1) {
2793 struct extent_buffer *eb = NULL;
2794 eb = btrfs_find_create_tree_block(root, start, len);
2795 if (eb)
2796 btrfs_tree_lock(eb);
2797
2798 mutex_lock(&root->fs_info->alloc_mutex);
2799 ret = lookup_extent_ref(NULL, root, start, len, refs);
2800 BUG_ON(ret);
2801 mutex_unlock(&root->fs_info->alloc_mutex);
2802
2803 if (eb) {
2804 btrfs_tree_unlock(eb);
2805 free_extent_buffer(eb);
2806 }
2807 if (*refs == 1) {
2808 printk("block %llu went down to one during drop_snap\n",
2809 (unsigned long long)start);
2810 }
2811
2812 }
2813#endif
2814
Chris Masone7a84562008-06-25 16:01:31 -04002815 cond_resched();
Chris Mason017e5362008-07-28 15:32:51 -04002816 return ret;
Chris Mason333db942008-06-25 16:01:30 -04002817}
2818
2819/*
Chris Mason9aca1d52007-03-13 11:09:37 -04002820 * helper function for drop_snapshot, this walks down the tree dropping ref
2821 * counts as it goes.
2822 */
Chris Mason98ed5172008-01-03 10:01:48 -05002823static int noinline walk_down_tree(struct btrfs_trans_handle *trans,
2824 struct btrfs_root *root,
2825 struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05002826{
Chris Mason7bb86312007-12-11 09:25:06 -05002827 u64 root_owner;
2828 u64 root_gen;
2829 u64 bytenr;
Chris Masonca7a79a2008-05-12 12:59:19 -04002830 u64 ptr_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04002831 struct extent_buffer *next;
2832 struct extent_buffer *cur;
Chris Mason7bb86312007-12-11 09:25:06 -05002833 struct extent_buffer *parent;
Yan Zheng31153d82008-07-28 15:32:19 -04002834 struct btrfs_leaf_ref *ref;
Chris Masondb945352007-10-15 16:15:53 -04002835 u32 blocksize;
Chris Mason20524f02007-03-10 06:35:47 -05002836 int ret;
2837 u32 refs;
2838
Chris Mason5caf2a02007-04-02 11:20:42 -04002839 WARN_ON(*level < 0);
2840 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason333db942008-06-25 16:01:30 -04002841 ret = drop_snap_lookup_refcount(root, path->nodes[*level]->start,
Chris Masondb945352007-10-15 16:15:53 -04002842 path->nodes[*level]->len, &refs);
Chris Mason20524f02007-03-10 06:35:47 -05002843 BUG_ON(ret);
2844 if (refs > 1)
2845 goto out;
Chris Masone0115992007-06-19 16:23:05 -04002846
Chris Mason9aca1d52007-03-13 11:09:37 -04002847 /*
2848 * walk down to the last node level and free all the leaves
2849 */
Chris Mason6407bf62007-03-27 06:33:00 -04002850 while(*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04002851 WARN_ON(*level < 0);
2852 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -05002853 cur = path->nodes[*level];
Chris Masone0115992007-06-19 16:23:05 -04002854
Chris Mason5f39d392007-10-15 16:14:19 -04002855 if (btrfs_header_level(cur) != *level)
Chris Mason2c90e5d2007-04-02 10:50:19 -04002856 WARN_ON(1);
Chris Masone0115992007-06-19 16:23:05 -04002857
Chris Mason7518a232007-03-12 12:01:18 -04002858 if (path->slots[*level] >=
Chris Mason5f39d392007-10-15 16:14:19 -04002859 btrfs_header_nritems(cur))
Chris Mason20524f02007-03-10 06:35:47 -05002860 break;
Chris Mason6407bf62007-03-27 06:33:00 -04002861 if (*level == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002862 ret = btrfs_drop_leaf_ref(trans, root, cur);
Chris Mason6407bf62007-03-27 06:33:00 -04002863 BUG_ON(ret);
2864 break;
2865 }
Chris Masondb945352007-10-15 16:15:53 -04002866 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
Chris Masonca7a79a2008-05-12 12:59:19 -04002867 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
Chris Masondb945352007-10-15 16:15:53 -04002868 blocksize = btrfs_level_size(root, *level - 1);
Chris Mason925baed2008-06-25 16:01:30 -04002869
Chris Mason333db942008-06-25 16:01:30 -04002870 ret = drop_snap_lookup_refcount(root, bytenr, blocksize, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -04002871 BUG_ON(ret);
2872 if (refs != 1) {
Chris Mason7bb86312007-12-11 09:25:06 -05002873 parent = path->nodes[*level];
2874 root_owner = btrfs_header_owner(parent);
2875 root_gen = btrfs_header_generation(parent);
Chris Mason20524f02007-03-10 06:35:47 -05002876 path->slots[*level]++;
Chris Masonf87f0572008-08-01 11:27:23 -04002877
Chris Mason925baed2008-06-25 16:01:30 -04002878 ret = __btrfs_free_extent(trans, root, bytenr,
Zheng Yan31840ae2008-09-23 13:14:14 -04002879 blocksize, parent->start,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002880 root_owner, root_gen,
2881 *level - 1, 1);
Chris Mason20524f02007-03-10 06:35:47 -05002882 BUG_ON(ret);
Chris Mason18e35e0a2008-08-01 13:11:41 -04002883
2884 atomic_inc(&root->fs_info->throttle_gen);
2885 wake_up(&root->fs_info->transaction_throttle);
Chris Mason2dd3e672008-08-04 08:20:15 -04002886 cond_resched();
Chris Mason18e35e0a2008-08-01 13:11:41 -04002887
Chris Mason20524f02007-03-10 06:35:47 -05002888 continue;
2889 }
Chris Masonf87f0572008-08-01 11:27:23 -04002890 /*
2891 * at this point, we have a single ref, and since the
2892 * only place referencing this extent is a dead root
2893 * the reference count should never go higher.
2894 * So, we don't need to check it again
2895 */
Yan Zheng31153d82008-07-28 15:32:19 -04002896 if (*level == 1) {
Chris Mason017e5362008-07-28 15:32:51 -04002897 ref = btrfs_lookup_leaf_ref(root, bytenr);
Zheng Yan1a40e232008-09-26 10:09:34 -04002898 if (ref && ref->generation != ptr_gen) {
2899 btrfs_free_leaf_ref(root, ref);
2900 ref = NULL;
2901 }
Yan Zheng31153d82008-07-28 15:32:19 -04002902 if (ref) {
Chris Masone02119d2008-09-05 16:13:11 -04002903 ret = cache_drop_leaf_ref(trans, root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04002904 BUG_ON(ret);
2905 btrfs_remove_leaf_ref(root, ref);
Yanbcc63ab2008-07-30 16:29:20 -04002906 btrfs_free_leaf_ref(root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04002907 *level = 0;
2908 break;
2909 }
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002910 if (printk_ratelimit()) {
Chris Mason37d1aee2008-07-31 10:48:37 -04002911 printk("leaf ref miss for bytenr %llu\n",
2912 (unsigned long long)bytenr);
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002913 }
Yan Zheng31153d82008-07-28 15:32:19 -04002914 }
Chris Masondb945352007-10-15 16:15:53 -04002915 next = btrfs_find_tree_block(root, bytenr, blocksize);
Chris Mason1259ab72008-05-12 13:39:03 -04002916 if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
Chris Mason5f39d392007-10-15 16:14:19 -04002917 free_extent_buffer(next);
Chris Mason333db942008-06-25 16:01:30 -04002918
Chris Masonca7a79a2008-05-12 12:59:19 -04002919 next = read_tree_block(root, bytenr, blocksize,
2920 ptr_gen);
Chris Masone7a84562008-06-25 16:01:31 -04002921 cond_resched();
Chris Masonf87f0572008-08-01 11:27:23 -04002922#if 0
2923 /*
2924 * this is a debugging check and can go away
2925 * the ref should never go all the way down to 1
2926 * at this point
2927 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04002928 ret = lookup_extent_ref(NULL, root, bytenr, blocksize,
2929 &refs);
Chris Masone9d0b132007-08-10 14:06:19 -04002930 BUG_ON(ret);
Chris Masonf87f0572008-08-01 11:27:23 -04002931 WARN_ON(refs != 1);
2932#endif
Chris Masone9d0b132007-08-10 14:06:19 -04002933 }
Chris Mason5caf2a02007-04-02 11:20:42 -04002934 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -04002935 if (path->nodes[*level-1])
Chris Mason5f39d392007-10-15 16:14:19 -04002936 free_extent_buffer(path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -05002937 path->nodes[*level-1] = next;
Chris Mason5f39d392007-10-15 16:14:19 -04002938 *level = btrfs_header_level(next);
Chris Mason20524f02007-03-10 06:35:47 -05002939 path->slots[*level] = 0;
Chris Mason2dd3e672008-08-04 08:20:15 -04002940 cond_resched();
Chris Mason20524f02007-03-10 06:35:47 -05002941 }
2942out:
Chris Mason5caf2a02007-04-02 11:20:42 -04002943 WARN_ON(*level < 0);
2944 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason7bb86312007-12-11 09:25:06 -05002945
2946 if (path->nodes[*level] == root->node) {
Chris Mason7bb86312007-12-11 09:25:06 -05002947 parent = path->nodes[*level];
Yan Zheng31153d82008-07-28 15:32:19 -04002948 bytenr = path->nodes[*level]->start;
Chris Mason7bb86312007-12-11 09:25:06 -05002949 } else {
2950 parent = path->nodes[*level + 1];
Yan Zheng31153d82008-07-28 15:32:19 -04002951 bytenr = btrfs_node_blockptr(parent, path->slots[*level + 1]);
Chris Mason7bb86312007-12-11 09:25:06 -05002952 }
2953
Yan Zheng31153d82008-07-28 15:32:19 -04002954 blocksize = btrfs_level_size(root, *level);
2955 root_owner = btrfs_header_owner(parent);
Chris Mason7bb86312007-12-11 09:25:06 -05002956 root_gen = btrfs_header_generation(parent);
Yan Zheng31153d82008-07-28 15:32:19 -04002957
2958 ret = __btrfs_free_extent(trans, root, bytenr, blocksize,
Zheng Yan31840ae2008-09-23 13:14:14 -04002959 parent->start, root_owner, root_gen,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002960 *level, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002961 free_extent_buffer(path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -05002962 path->nodes[*level] = NULL;
2963 *level += 1;
2964 BUG_ON(ret);
Chris Masonf87f0572008-08-01 11:27:23 -04002965
Chris Masone7a84562008-06-25 16:01:31 -04002966 cond_resched();
Chris Mason20524f02007-03-10 06:35:47 -05002967 return 0;
2968}
2969
Chris Mason9aca1d52007-03-13 11:09:37 -04002970/*
Yan Zhengf82d02d2008-10-29 14:49:05 -04002971 * helper function for drop_subtree, this function is similar to
2972 * walk_down_tree. The main difference is that it checks reference
2973 * counts while tree blocks are locked.
2974 */
2975static int noinline walk_down_subtree(struct btrfs_trans_handle *trans,
2976 struct btrfs_root *root,
2977 struct btrfs_path *path, int *level)
2978{
2979 struct extent_buffer *next;
2980 struct extent_buffer *cur;
2981 struct extent_buffer *parent;
2982 u64 bytenr;
2983 u64 ptr_gen;
2984 u32 blocksize;
2985 u32 refs;
2986 int ret;
2987
2988 cur = path->nodes[*level];
2989 ret = btrfs_lookup_extent_ref(trans, root, cur->start, cur->len,
2990 &refs);
2991 BUG_ON(ret);
2992 if (refs > 1)
2993 goto out;
2994
2995 while (*level >= 0) {
2996 cur = path->nodes[*level];
2997 if (*level == 0) {
2998 ret = btrfs_drop_leaf_ref(trans, root, cur);
2999 BUG_ON(ret);
3000 clean_tree_block(trans, root, cur);
3001 break;
3002 }
3003 if (path->slots[*level] >= btrfs_header_nritems(cur)) {
3004 clean_tree_block(trans, root, cur);
3005 break;
3006 }
3007
3008 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
3009 blocksize = btrfs_level_size(root, *level - 1);
3010 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
3011
3012 next = read_tree_block(root, bytenr, blocksize, ptr_gen);
3013 btrfs_tree_lock(next);
3014
3015 ret = btrfs_lookup_extent_ref(trans, root, bytenr, blocksize,
3016 &refs);
3017 BUG_ON(ret);
3018 if (refs > 1) {
3019 parent = path->nodes[*level];
3020 ret = btrfs_free_extent(trans, root, bytenr,
3021 blocksize, parent->start,
3022 btrfs_header_owner(parent),
3023 btrfs_header_generation(parent),
3024 *level - 1, 1);
3025 BUG_ON(ret);
3026 path->slots[*level]++;
3027 btrfs_tree_unlock(next);
3028 free_extent_buffer(next);
3029 continue;
3030 }
3031
3032 *level = btrfs_header_level(next);
3033 path->nodes[*level] = next;
3034 path->slots[*level] = 0;
3035 path->locks[*level] = 1;
3036 cond_resched();
3037 }
3038out:
3039 parent = path->nodes[*level + 1];
3040 bytenr = path->nodes[*level]->start;
3041 blocksize = path->nodes[*level]->len;
3042
3043 ret = btrfs_free_extent(trans, root, bytenr, blocksize,
3044 parent->start, btrfs_header_owner(parent),
3045 btrfs_header_generation(parent), *level, 1);
3046 BUG_ON(ret);
3047
3048 if (path->locks[*level]) {
3049 btrfs_tree_unlock(path->nodes[*level]);
3050 path->locks[*level] = 0;
3051 }
3052 free_extent_buffer(path->nodes[*level]);
3053 path->nodes[*level] = NULL;
3054 *level += 1;
3055 cond_resched();
3056 return 0;
3057}
3058
3059/*
Chris Mason9aca1d52007-03-13 11:09:37 -04003060 * helper for dropping snapshots. This walks back up the tree in the path
3061 * to find the first node higher up where we haven't yet gone through
3062 * all the slots
3063 */
Chris Mason98ed5172008-01-03 10:01:48 -05003064static int noinline walk_up_tree(struct btrfs_trans_handle *trans,
3065 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04003066 struct btrfs_path *path,
3067 int *level, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05003068{
Chris Mason7bb86312007-12-11 09:25:06 -05003069 u64 root_owner;
3070 u64 root_gen;
3071 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05003072 int i;
3073 int slot;
3074 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04003075
Yan Zhengf82d02d2008-10-29 14:49:05 -04003076 for (i = *level; i < max_level && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -05003077 slot = path->slots[i];
Chris Mason5f39d392007-10-15 16:14:19 -04003078 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
3079 struct extent_buffer *node;
3080 struct btrfs_disk_key disk_key;
3081 node = path->nodes[i];
Chris Mason20524f02007-03-10 06:35:47 -05003082 path->slots[i]++;
3083 *level = i;
Chris Mason9f3a7422007-08-07 15:52:19 -04003084 WARN_ON(*level == 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003085 btrfs_node_key(node, &disk_key, path->slots[i]);
Chris Mason9f3a7422007-08-07 15:52:19 -04003086 memcpy(&root_item->drop_progress,
Chris Mason5f39d392007-10-15 16:14:19 -04003087 &disk_key, sizeof(disk_key));
Chris Mason9f3a7422007-08-07 15:52:19 -04003088 root_item->drop_level = i;
Chris Mason20524f02007-03-10 06:35:47 -05003089 return 0;
3090 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04003091 struct extent_buffer *parent;
3092 if (path->nodes[*level] == root->node)
3093 parent = path->nodes[*level];
3094 else
3095 parent = path->nodes[*level + 1];
3096
3097 root_owner = btrfs_header_owner(parent);
3098 root_gen = btrfs_header_generation(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04003099
3100 clean_tree_block(trans, root, path->nodes[*level]);
Chris Masone089f052007-03-16 16:20:31 -04003101 ret = btrfs_free_extent(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04003102 path->nodes[*level]->start,
Chris Mason7bb86312007-12-11 09:25:06 -05003103 path->nodes[*level]->len,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003104 parent->start, root_owner,
3105 root_gen, *level, 1);
Chris Mason6407bf62007-03-27 06:33:00 -04003106 BUG_ON(ret);
Yan Zhengf82d02d2008-10-29 14:49:05 -04003107 if (path->locks[*level]) {
3108 btrfs_tree_unlock(path->nodes[*level]);
3109 path->locks[*level] = 0;
3110 }
Chris Mason5f39d392007-10-15 16:14:19 -04003111 free_extent_buffer(path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -04003112 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -05003113 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -05003114 }
3115 }
3116 return 1;
3117}
3118
Chris Mason9aca1d52007-03-13 11:09:37 -04003119/*
3120 * drop the reference count on the tree rooted at 'snap'. This traverses
3121 * the tree freeing any blocks that have a ref count of zero after being
3122 * decremented.
3123 */
Chris Masone089f052007-03-16 16:20:31 -04003124int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason9f3a7422007-08-07 15:52:19 -04003125 *root)
Chris Mason20524f02007-03-10 06:35:47 -05003126{
Chris Mason3768f362007-03-13 16:47:54 -04003127 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -04003128 int wret;
Chris Mason20524f02007-03-10 06:35:47 -05003129 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -04003130 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -05003131 int i;
3132 int orig_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04003133 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05003134
Chris Masona2135012008-06-25 16:01:30 -04003135 WARN_ON(!mutex_is_locked(&root->fs_info->drop_mutex));
Chris Mason5caf2a02007-04-02 11:20:42 -04003136 path = btrfs_alloc_path();
3137 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05003138
Chris Mason5f39d392007-10-15 16:14:19 -04003139 level = btrfs_header_level(root->node);
Chris Mason20524f02007-03-10 06:35:47 -05003140 orig_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04003141 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
3142 path->nodes[level] = root->node;
Chris Masonf510cfe2007-10-15 16:14:48 -04003143 extent_buffer_get(root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -04003144 path->slots[level] = 0;
3145 } else {
3146 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04003147 struct btrfs_disk_key found_key;
3148 struct extent_buffer *node;
Chris Mason6702ed42007-08-07 16:15:09 -04003149
Chris Mason9f3a7422007-08-07 15:52:19 -04003150 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Chris Mason6702ed42007-08-07 16:15:09 -04003151 level = root_item->drop_level;
3152 path->lowest_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04003153 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason6702ed42007-08-07 16:15:09 -04003154 if (wret < 0) {
Chris Mason9f3a7422007-08-07 15:52:19 -04003155 ret = wret;
3156 goto out;
3157 }
Chris Mason5f39d392007-10-15 16:14:19 -04003158 node = path->nodes[level];
3159 btrfs_node_key(node, &found_key, path->slots[level]);
3160 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
3161 sizeof(found_key)));
Chris Mason7d9eb122008-07-08 14:19:17 -04003162 /*
3163 * unlock our path, this is safe because only this
3164 * function is allowed to delete this snapshot
3165 */
Chris Mason925baed2008-06-25 16:01:30 -04003166 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
3167 if (path->nodes[i] && path->locks[i]) {
3168 path->locks[i] = 0;
3169 btrfs_tree_unlock(path->nodes[i]);
3170 }
3171 }
Chris Mason9f3a7422007-08-07 15:52:19 -04003172 }
Chris Mason20524f02007-03-10 06:35:47 -05003173 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -04003174 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04003175 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05003176 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04003177 if (wret < 0)
3178 ret = wret;
3179
Yan Zhengf82d02d2008-10-29 14:49:05 -04003180 wret = walk_up_tree(trans, root, path, &level,
3181 BTRFS_MAX_LEVEL);
Chris Mason9aca1d52007-03-13 11:09:37 -04003182 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05003183 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04003184 if (wret < 0)
3185 ret = wret;
Chris Masone7a84562008-06-25 16:01:31 -04003186 if (trans->transaction->in_commit) {
3187 ret = -EAGAIN;
3188 break;
3189 }
Chris Mason18e35e0a2008-08-01 13:11:41 -04003190 atomic_inc(&root->fs_info->throttle_gen);
Chris Mason017e5362008-07-28 15:32:51 -04003191 wake_up(&root->fs_info->transaction_throttle);
Chris Mason20524f02007-03-10 06:35:47 -05003192 }
Chris Mason83e15a22007-03-12 09:03:27 -04003193 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -04003194 if (path->nodes[i]) {
Chris Mason5f39d392007-10-15 16:14:19 -04003195 free_extent_buffer(path->nodes[i]);
Chris Mason0f827312007-10-15 16:18:56 -04003196 path->nodes[i] = NULL;
Chris Mason83e15a22007-03-12 09:03:27 -04003197 }
Chris Mason20524f02007-03-10 06:35:47 -05003198 }
Chris Mason9f3a7422007-08-07 15:52:19 -04003199out:
Chris Mason5caf2a02007-04-02 11:20:42 -04003200 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -04003201 return ret;
Chris Mason20524f02007-03-10 06:35:47 -05003202}
Chris Mason9078a3e2007-04-26 16:46:15 -04003203
Yan Zhengf82d02d2008-10-29 14:49:05 -04003204int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
3205 struct btrfs_root *root,
3206 struct extent_buffer *node,
3207 struct extent_buffer *parent)
3208{
3209 struct btrfs_path *path;
3210 int level;
3211 int parent_level;
3212 int ret = 0;
3213 int wret;
3214
3215 path = btrfs_alloc_path();
3216 BUG_ON(!path);
3217
3218 BUG_ON(!btrfs_tree_locked(parent));
3219 parent_level = btrfs_header_level(parent);
3220 extent_buffer_get(parent);
3221 path->nodes[parent_level] = parent;
3222 path->slots[parent_level] = btrfs_header_nritems(parent);
3223
3224 BUG_ON(!btrfs_tree_locked(node));
3225 level = btrfs_header_level(node);
3226 extent_buffer_get(node);
3227 path->nodes[level] = node;
3228 path->slots[level] = 0;
3229
3230 while (1) {
3231 wret = walk_down_subtree(trans, root, path, &level);
3232 if (wret < 0)
3233 ret = wret;
3234 if (wret != 0)
3235 break;
3236
3237 wret = walk_up_tree(trans, root, path, &level, parent_level);
3238 if (wret < 0)
3239 ret = wret;
3240 if (wret != 0)
3241 break;
3242 }
3243
3244 btrfs_free_path(path);
3245 return ret;
3246}
3247
Chris Mason8e7bf942008-04-28 09:02:36 -04003248static unsigned long calc_ra(unsigned long start, unsigned long last,
3249 unsigned long nr)
3250{
3251 return min(last, start + nr - 1);
3252}
3253
Chris Mason98ed5172008-01-03 10:01:48 -05003254static int noinline relocate_inode_pages(struct inode *inode, u64 start,
3255 u64 len)
Chris Masonedbd8d42007-12-21 16:27:24 -05003256{
3257 u64 page_start;
3258 u64 page_end;
Zheng Yan1a40e232008-09-26 10:09:34 -04003259 unsigned long first_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05003260 unsigned long last_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05003261 unsigned long i;
3262 struct page *page;
Chris Masond1310b22008-01-24 16:13:08 -05003263 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason4313b392008-01-03 09:08:48 -05003264 struct file_ra_state *ra;
Chris Mason3eaa2882008-07-24 11:57:52 -04003265 struct btrfs_ordered_extent *ordered;
Zheng Yan1a40e232008-09-26 10:09:34 -04003266 unsigned int total_read = 0;
3267 unsigned int total_dirty = 0;
3268 int ret = 0;
Chris Mason4313b392008-01-03 09:08:48 -05003269
3270 ra = kzalloc(sizeof(*ra), GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05003271
3272 mutex_lock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04003273 first_index = start >> PAGE_CACHE_SHIFT;
Chris Masonedbd8d42007-12-21 16:27:24 -05003274 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
3275
Zheng Yan1a40e232008-09-26 10:09:34 -04003276 /* make sure the dirty trick played by the caller work */
3277 ret = invalidate_inode_pages2_range(inode->i_mapping,
3278 first_index, last_index);
3279 if (ret)
3280 goto out_unlock;
Chris Mason8e7bf942008-04-28 09:02:36 -04003281
Chris Mason4313b392008-01-03 09:08:48 -05003282 file_ra_state_init(ra, inode->i_mapping);
Chris Masonedbd8d42007-12-21 16:27:24 -05003283
Zheng Yan1a40e232008-09-26 10:09:34 -04003284 for (i = first_index ; i <= last_index; i++) {
3285 if (total_read % ra->ra_pages == 0) {
Chris Mason8e7bf942008-04-28 09:02:36 -04003286 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
Zheng Yan1a40e232008-09-26 10:09:34 -04003287 calc_ra(i, last_index, ra->ra_pages));
Chris Mason8e7bf942008-04-28 09:02:36 -04003288 }
3289 total_read++;
Chris Mason3eaa2882008-07-24 11:57:52 -04003290again:
3291 if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
Zheng Yan1a40e232008-09-26 10:09:34 -04003292 BUG_ON(1);
Chris Masonedbd8d42007-12-21 16:27:24 -05003293 page = grab_cache_page(inode->i_mapping, i);
Chris Masona061fc82008-05-07 11:43:44 -04003294 if (!page) {
Zheng Yan1a40e232008-09-26 10:09:34 -04003295 ret = -ENOMEM;
Chris Masonedbd8d42007-12-21 16:27:24 -05003296 goto out_unlock;
Chris Masona061fc82008-05-07 11:43:44 -04003297 }
Chris Masonedbd8d42007-12-21 16:27:24 -05003298 if (!PageUptodate(page)) {
3299 btrfs_readpage(NULL, page);
3300 lock_page(page);
3301 if (!PageUptodate(page)) {
3302 unlock_page(page);
3303 page_cache_release(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04003304 ret = -EIO;
Chris Masonedbd8d42007-12-21 16:27:24 -05003305 goto out_unlock;
3306 }
3307 }
Chris Masonec44a352008-04-28 15:29:52 -04003308 wait_on_page_writeback(page);
Chris Mason3eaa2882008-07-24 11:57:52 -04003309
Chris Masonedbd8d42007-12-21 16:27:24 -05003310 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
3311 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05003312 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05003313
Chris Mason3eaa2882008-07-24 11:57:52 -04003314 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3315 if (ordered) {
3316 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3317 unlock_page(page);
3318 page_cache_release(page);
3319 btrfs_start_ordered_extent(inode, ordered, 1);
3320 btrfs_put_ordered_extent(ordered);
3321 goto again;
3322 }
3323 set_page_extent_mapped(page);
3324
Chris Masonea8c2812008-08-04 23:17:27 -04003325 btrfs_set_extent_delalloc(inode, page_start, page_end);
Zheng Yan1a40e232008-09-26 10:09:34 -04003326 if (i == first_index)
3327 set_extent_bits(io_tree, page_start, page_end,
3328 EXTENT_BOUNDARY, GFP_NOFS);
3329
Chris Masona061fc82008-05-07 11:43:44 -04003330 set_page_dirty(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04003331 total_dirty++;
Chris Masonedbd8d42007-12-21 16:27:24 -05003332
Chris Masond1310b22008-01-24 16:13:08 -05003333 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05003334 unlock_page(page);
3335 page_cache_release(page);
3336 }
3337
3338out_unlock:
Chris Masonec44a352008-04-28 15:29:52 -04003339 kfree(ra);
Chris Masonedbd8d42007-12-21 16:27:24 -05003340 mutex_unlock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04003341 balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty);
Chris Masonbf4ef672008-05-08 13:26:18 -04003342 return ret;
3343}
3344
Zheng Yan1a40e232008-09-26 10:09:34 -04003345static int noinline relocate_data_extent(struct inode *reloc_inode,
3346 struct btrfs_key *extent_key,
3347 u64 offset)
Chris Masonedbd8d42007-12-21 16:27:24 -05003348{
Zheng Yan1a40e232008-09-26 10:09:34 -04003349 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
3350 struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree;
3351 struct extent_map *em;
Yan Zheng66435582008-10-30 14:19:50 -04003352 u64 start = extent_key->objectid - offset;
3353 u64 end = start + extent_key->offset - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04003354
3355 em = alloc_extent_map(GFP_NOFS);
3356 BUG_ON(!em || IS_ERR(em));
3357
Yan Zheng66435582008-10-30 14:19:50 -04003358 em->start = start;
Zheng Yan1a40e232008-09-26 10:09:34 -04003359 em->len = extent_key->offset;
Chris Masonc8b97812008-10-29 14:49:59 -04003360 em->block_len = extent_key->offset;
Zheng Yan1a40e232008-09-26 10:09:34 -04003361 em->block_start = extent_key->objectid;
3362 em->bdev = root->fs_info->fs_devices->latest_bdev;
3363 set_bit(EXTENT_FLAG_PINNED, &em->flags);
3364
3365 /* setup extent map to cheat btrfs_readpage */
Yan Zheng66435582008-10-30 14:19:50 -04003366 lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04003367 while (1) {
3368 int ret;
3369 spin_lock(&em_tree->lock);
3370 ret = add_extent_mapping(em_tree, em);
3371 spin_unlock(&em_tree->lock);
3372 if (ret != -EEXIST) {
3373 free_extent_map(em);
3374 break;
3375 }
Yan Zheng66435582008-10-30 14:19:50 -04003376 btrfs_drop_extent_cache(reloc_inode, start, end, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04003377 }
Yan Zheng66435582008-10-30 14:19:50 -04003378 unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04003379
Yan Zheng66435582008-10-30 14:19:50 -04003380 return relocate_inode_pages(reloc_inode, start, extent_key->offset);
Zheng Yan1a40e232008-09-26 10:09:34 -04003381}
3382
3383struct btrfs_ref_path {
3384 u64 extent_start;
3385 u64 nodes[BTRFS_MAX_LEVEL];
3386 u64 root_objectid;
3387 u64 root_generation;
3388 u64 owner_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04003389 u32 num_refs;
3390 int lowest_level;
3391 int current_level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04003392 int shared_level;
3393
3394 struct btrfs_key node_keys[BTRFS_MAX_LEVEL];
3395 u64 new_nodes[BTRFS_MAX_LEVEL];
Zheng Yan1a40e232008-09-26 10:09:34 -04003396};
3397
3398struct disk_extent {
Chris Masonc8b97812008-10-29 14:49:59 -04003399 u64 ram_bytes;
Zheng Yan1a40e232008-09-26 10:09:34 -04003400 u64 disk_bytenr;
3401 u64 disk_num_bytes;
3402 u64 offset;
3403 u64 num_bytes;
Chris Masonc8b97812008-10-29 14:49:59 -04003404 u8 compression;
3405 u8 encryption;
3406 u16 other_encoding;
Zheng Yan1a40e232008-09-26 10:09:34 -04003407};
3408
3409static int is_cowonly_root(u64 root_objectid)
3410{
3411 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
3412 root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
3413 root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
3414 root_objectid == BTRFS_DEV_TREE_OBJECTID ||
3415 root_objectid == BTRFS_TREE_LOG_OBJECTID)
3416 return 1;
3417 return 0;
3418}
3419
3420static int noinline __next_ref_path(struct btrfs_trans_handle *trans,
3421 struct btrfs_root *extent_root,
3422 struct btrfs_ref_path *ref_path,
3423 int first_time)
3424{
3425 struct extent_buffer *leaf;
3426 struct btrfs_path *path;
Chris Mason4313b392008-01-03 09:08:48 -05003427 struct btrfs_extent_ref *ref;
Chris Masonedbd8d42007-12-21 16:27:24 -05003428 struct btrfs_key key;
3429 struct btrfs_key found_key;
Zheng Yan1a40e232008-09-26 10:09:34 -04003430 u64 bytenr;
Chris Masonedbd8d42007-12-21 16:27:24 -05003431 u32 nritems;
Zheng Yan1a40e232008-09-26 10:09:34 -04003432 int level;
3433 int ret = 1;
Chris Masonedbd8d42007-12-21 16:27:24 -05003434
Zheng Yan1a40e232008-09-26 10:09:34 -04003435 path = btrfs_alloc_path();
3436 if (!path)
3437 return -ENOMEM;
3438
Zheng Yan1a40e232008-09-26 10:09:34 -04003439 if (first_time) {
3440 ref_path->lowest_level = -1;
3441 ref_path->current_level = -1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04003442 ref_path->shared_level = -1;
Zheng Yan1a40e232008-09-26 10:09:34 -04003443 goto walk_up;
Chris Masona061fc82008-05-07 11:43:44 -04003444 }
Zheng Yan1a40e232008-09-26 10:09:34 -04003445walk_down:
3446 level = ref_path->current_level - 1;
3447 while (level >= -1) {
3448 u64 parent;
3449 if (level < ref_path->lowest_level)
3450 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05003451
Zheng Yan1a40e232008-09-26 10:09:34 -04003452 if (level >= 0) {
3453 bytenr = ref_path->nodes[level];
3454 } else {
3455 bytenr = ref_path->extent_start;
3456 }
3457 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05003458
Zheng Yan1a40e232008-09-26 10:09:34 -04003459 parent = ref_path->nodes[level + 1];
3460 ref_path->nodes[level + 1] = 0;
3461 ref_path->current_level = level;
3462 BUG_ON(parent == 0);
3463
3464 key.objectid = bytenr;
3465 key.offset = parent + 1;
3466 key.type = BTRFS_EXTENT_REF_KEY;
3467
3468 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05003469 if (ret < 0)
3470 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04003471 BUG_ON(ret == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05003472
Chris Masonedbd8d42007-12-21 16:27:24 -05003473 leaf = path->nodes[0];
3474 nritems = btrfs_header_nritems(leaf);
Zheng Yan1a40e232008-09-26 10:09:34 -04003475 if (path->slots[0] >= nritems) {
Chris Masona061fc82008-05-07 11:43:44 -04003476 ret = btrfs_next_leaf(extent_root, path);
Chris Masona061fc82008-05-07 11:43:44 -04003477 if (ret < 0)
3478 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04003479 if (ret > 0)
3480 goto next;
Chris Masonbf4ef672008-05-08 13:26:18 -04003481 leaf = path->nodes[0];
Chris Masona061fc82008-05-07 11:43:44 -04003482 }
Chris Masonedbd8d42007-12-21 16:27:24 -05003483
3484 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Zheng Yan1a40e232008-09-26 10:09:34 -04003485 if (found_key.objectid == bytenr &&
Yan Zhengf82d02d2008-10-29 14:49:05 -04003486 found_key.type == BTRFS_EXTENT_REF_KEY) {
3487 if (level < ref_path->shared_level)
3488 ref_path->shared_level = level;
Zheng Yan1a40e232008-09-26 10:09:34 -04003489 goto found;
Yan Zhengf82d02d2008-10-29 14:49:05 -04003490 }
Zheng Yan1a40e232008-09-26 10:09:34 -04003491next:
3492 level--;
3493 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04003494 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04003495 }
3496 /* reached lowest level */
3497 ret = 1;
3498 goto out;
3499walk_up:
3500 level = ref_path->current_level;
3501 while (level < BTRFS_MAX_LEVEL - 1) {
3502 u64 ref_objectid;
3503 if (level >= 0) {
3504 bytenr = ref_path->nodes[level];
3505 } else {
3506 bytenr = ref_path->extent_start;
Chris Masona061fc82008-05-07 11:43:44 -04003507 }
Zheng Yan1a40e232008-09-26 10:09:34 -04003508 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05003509
Zheng Yan1a40e232008-09-26 10:09:34 -04003510 key.objectid = bytenr;
3511 key.offset = 0;
3512 key.type = BTRFS_EXTENT_REF_KEY;
Chris Masonedbd8d42007-12-21 16:27:24 -05003513
Zheng Yan1a40e232008-09-26 10:09:34 -04003514 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
3515 if (ret < 0)
Chris Masonedbd8d42007-12-21 16:27:24 -05003516 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04003517
3518 leaf = path->nodes[0];
3519 nritems = btrfs_header_nritems(leaf);
3520 if (path->slots[0] >= nritems) {
3521 ret = btrfs_next_leaf(extent_root, path);
3522 if (ret < 0)
3523 goto out;
3524 if (ret > 0) {
3525 /* the extent was freed by someone */
3526 if (ref_path->lowest_level == level)
3527 goto out;
3528 btrfs_release_path(extent_root, path);
3529 goto walk_down;
3530 }
3531 leaf = path->nodes[0];
3532 }
3533
3534 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3535 if (found_key.objectid != bytenr ||
3536 found_key.type != BTRFS_EXTENT_REF_KEY) {
3537 /* the extent was freed by someone */
3538 if (ref_path->lowest_level == level) {
3539 ret = 1;
3540 goto out;
3541 }
3542 btrfs_release_path(extent_root, path);
3543 goto walk_down;
3544 }
3545found:
3546 ref = btrfs_item_ptr(leaf, path->slots[0],
3547 struct btrfs_extent_ref);
3548 ref_objectid = btrfs_ref_objectid(leaf, ref);
3549 if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) {
3550 if (first_time) {
3551 level = (int)ref_objectid;
3552 BUG_ON(level >= BTRFS_MAX_LEVEL);
3553 ref_path->lowest_level = level;
3554 ref_path->current_level = level;
3555 ref_path->nodes[level] = bytenr;
3556 } else {
3557 WARN_ON(ref_objectid != level);
3558 }
3559 } else {
3560 WARN_ON(level != -1);
3561 }
3562 first_time = 0;
3563
3564 if (ref_path->lowest_level == level) {
3565 ref_path->owner_objectid = ref_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04003566 ref_path->num_refs = btrfs_ref_num_refs(leaf, ref);
3567 }
3568
3569 /*
3570 * the block is tree root or the block isn't in reference
3571 * counted tree.
3572 */
3573 if (found_key.objectid == found_key.offset ||
3574 is_cowonly_root(btrfs_ref_root(leaf, ref))) {
3575 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
3576 ref_path->root_generation =
3577 btrfs_ref_generation(leaf, ref);
3578 if (level < 0) {
3579 /* special reference from the tree log */
3580 ref_path->nodes[0] = found_key.offset;
3581 ref_path->current_level = 0;
3582 }
3583 ret = 0;
3584 goto out;
3585 }
3586
3587 level++;
3588 BUG_ON(ref_path->nodes[level] != 0);
3589 ref_path->nodes[level] = found_key.offset;
3590 ref_path->current_level = level;
3591
3592 /*
3593 * the reference was created in the running transaction,
3594 * no need to continue walking up.
3595 */
3596 if (btrfs_ref_generation(leaf, ref) == trans->transid) {
3597 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
3598 ref_path->root_generation =
3599 btrfs_ref_generation(leaf, ref);
3600 ret = 0;
3601 goto out;
3602 }
3603
3604 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04003605 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04003606 }
3607 /* reached max tree level, but no tree root found. */
3608 BUG();
3609out:
Zheng Yan1a40e232008-09-26 10:09:34 -04003610 btrfs_free_path(path);
3611 return ret;
3612}
3613
3614static int btrfs_first_ref_path(struct btrfs_trans_handle *trans,
3615 struct btrfs_root *extent_root,
3616 struct btrfs_ref_path *ref_path,
3617 u64 extent_start)
3618{
3619 memset(ref_path, 0, sizeof(*ref_path));
3620 ref_path->extent_start = extent_start;
3621
3622 return __next_ref_path(trans, extent_root, ref_path, 1);
3623}
3624
3625static int btrfs_next_ref_path(struct btrfs_trans_handle *trans,
3626 struct btrfs_root *extent_root,
3627 struct btrfs_ref_path *ref_path)
3628{
3629 return __next_ref_path(trans, extent_root, ref_path, 0);
3630}
3631
3632static int noinline get_new_locations(struct inode *reloc_inode,
3633 struct btrfs_key *extent_key,
3634 u64 offset, int no_fragment,
3635 struct disk_extent **extents,
3636 int *nr_extents)
3637{
3638 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
3639 struct btrfs_path *path;
3640 struct btrfs_file_extent_item *fi;
3641 struct extent_buffer *leaf;
3642 struct disk_extent *exts = *extents;
3643 struct btrfs_key found_key;
3644 u64 cur_pos;
3645 u64 last_byte;
3646 u32 nritems;
3647 int nr = 0;
3648 int max = *nr_extents;
3649 int ret;
3650
3651 WARN_ON(!no_fragment && *extents);
3652 if (!exts) {
3653 max = 1;
3654 exts = kmalloc(sizeof(*exts) * max, GFP_NOFS);
3655 if (!exts)
3656 return -ENOMEM;
3657 }
3658
3659 path = btrfs_alloc_path();
3660 BUG_ON(!path);
3661
3662 cur_pos = extent_key->objectid - offset;
3663 last_byte = extent_key->objectid + extent_key->offset;
3664 ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino,
3665 cur_pos, 0);
3666 if (ret < 0)
3667 goto out;
3668 if (ret > 0) {
3669 ret = -ENOENT;
3670 goto out;
3671 }
3672
3673 while (1) {
3674 leaf = path->nodes[0];
3675 nritems = btrfs_header_nritems(leaf);
3676 if (path->slots[0] >= nritems) {
3677 ret = btrfs_next_leaf(root, path);
3678 if (ret < 0)
3679 goto out;
3680 if (ret > 0)
3681 break;
3682 leaf = path->nodes[0];
3683 }
3684
3685 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3686 if (found_key.offset != cur_pos ||
3687 found_key.type != BTRFS_EXTENT_DATA_KEY ||
3688 found_key.objectid != reloc_inode->i_ino)
3689 break;
3690
3691 fi = btrfs_item_ptr(leaf, path->slots[0],
3692 struct btrfs_file_extent_item);
3693 if (btrfs_file_extent_type(leaf, fi) !=
3694 BTRFS_FILE_EXTENT_REG ||
3695 btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
3696 break;
3697
3698 if (nr == max) {
3699 struct disk_extent *old = exts;
3700 max *= 2;
3701 exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
3702 memcpy(exts, old, sizeof(*exts) * nr);
3703 if (old != *extents)
3704 kfree(old);
3705 }
3706
3707 exts[nr].disk_bytenr =
3708 btrfs_file_extent_disk_bytenr(leaf, fi);
3709 exts[nr].disk_num_bytes =
3710 btrfs_file_extent_disk_num_bytes(leaf, fi);
3711 exts[nr].offset = btrfs_file_extent_offset(leaf, fi);
3712 exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
Chris Masonc8b97812008-10-29 14:49:59 -04003713 exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
3714 exts[nr].compression = btrfs_file_extent_compression(leaf, fi);
3715 exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi);
3716 exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf,
3717 fi);
Yan Zhengd899e052008-10-30 14:25:28 -04003718 BUG_ON(exts[nr].offset > 0);
3719 BUG_ON(exts[nr].compression || exts[nr].encryption);
3720 BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04003721
3722 cur_pos += exts[nr].num_bytes;
3723 nr++;
3724
3725 if (cur_pos + offset >= last_byte)
3726 break;
3727
3728 if (no_fragment) {
3729 ret = 1;
3730 goto out;
3731 }
3732 path->slots[0]++;
3733 }
3734
3735 WARN_ON(cur_pos + offset > last_byte);
3736 if (cur_pos + offset < last_byte) {
3737 ret = -ENOENT;
3738 goto out;
Chris Masonedbd8d42007-12-21 16:27:24 -05003739 }
3740 ret = 0;
3741out:
Zheng Yan1a40e232008-09-26 10:09:34 -04003742 btrfs_free_path(path);
3743 if (ret) {
3744 if (exts != *extents)
3745 kfree(exts);
3746 } else {
3747 *extents = exts;
3748 *nr_extents = nr;
3749 }
3750 return ret;
3751}
3752
3753static int noinline replace_one_extent(struct btrfs_trans_handle *trans,
3754 struct btrfs_root *root,
3755 struct btrfs_path *path,
3756 struct btrfs_key *extent_key,
3757 struct btrfs_key *leaf_key,
3758 struct btrfs_ref_path *ref_path,
3759 struct disk_extent *new_extents,
3760 int nr_extents)
3761{
3762 struct extent_buffer *leaf;
3763 struct btrfs_file_extent_item *fi;
3764 struct inode *inode = NULL;
3765 struct btrfs_key key;
3766 u64 lock_start = 0;
3767 u64 lock_end = 0;
3768 u64 num_bytes;
3769 u64 ext_offset;
3770 u64 first_pos;
3771 u32 nritems;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003772 int nr_scaned = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04003773 int extent_locked = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04003774 int extent_type;
Zheng Yan1a40e232008-09-26 10:09:34 -04003775 int ret;
3776
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003777 memcpy(&key, leaf_key, sizeof(key));
3778 first_pos = INT_LIMIT(loff_t) - extent_key->offset;
Zheng Yan1a40e232008-09-26 10:09:34 -04003779 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003780 if (key.objectid < ref_path->owner_objectid ||
3781 (key.objectid == ref_path->owner_objectid &&
3782 key.type < BTRFS_EXTENT_DATA_KEY)) {
3783 key.objectid = ref_path->owner_objectid;
3784 key.type = BTRFS_EXTENT_DATA_KEY;
3785 key.offset = 0;
3786 }
Zheng Yan1a40e232008-09-26 10:09:34 -04003787 }
3788
3789 while (1) {
3790 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
3791 if (ret < 0)
3792 goto out;
3793
3794 leaf = path->nodes[0];
3795 nritems = btrfs_header_nritems(leaf);
3796next:
3797 if (extent_locked && ret > 0) {
3798 /*
3799 * the file extent item was modified by someone
3800 * before the extent got locked.
3801 */
Zheng Yan1a40e232008-09-26 10:09:34 -04003802 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
3803 lock_end, GFP_NOFS);
3804 extent_locked = 0;
3805 }
3806
3807 if (path->slots[0] >= nritems) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003808 if (++nr_scaned > 2)
Zheng Yan1a40e232008-09-26 10:09:34 -04003809 break;
3810
3811 BUG_ON(extent_locked);
3812 ret = btrfs_next_leaf(root, path);
3813 if (ret < 0)
3814 goto out;
3815 if (ret > 0)
3816 break;
3817 leaf = path->nodes[0];
3818 nritems = btrfs_header_nritems(leaf);
3819 }
3820
3821 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3822
3823 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
3824 if ((key.objectid > ref_path->owner_objectid) ||
3825 (key.objectid == ref_path->owner_objectid &&
3826 key.type > BTRFS_EXTENT_DATA_KEY) ||
3827 (key.offset >= first_pos + extent_key->offset))
3828 break;
3829 }
3830
3831 if (inode && key.objectid != inode->i_ino) {
3832 BUG_ON(extent_locked);
3833 btrfs_release_path(root, path);
3834 mutex_unlock(&inode->i_mutex);
3835 iput(inode);
3836 inode = NULL;
3837 continue;
3838 }
3839
3840 if (key.type != BTRFS_EXTENT_DATA_KEY) {
3841 path->slots[0]++;
3842 ret = 1;
3843 goto next;
3844 }
3845 fi = btrfs_item_ptr(leaf, path->slots[0],
3846 struct btrfs_file_extent_item);
Yan Zhengd899e052008-10-30 14:25:28 -04003847 extent_type = btrfs_file_extent_type(leaf, fi);
3848 if ((extent_type != BTRFS_FILE_EXTENT_REG &&
3849 extent_type != BTRFS_FILE_EXTENT_PREALLOC) ||
Zheng Yan1a40e232008-09-26 10:09:34 -04003850 (btrfs_file_extent_disk_bytenr(leaf, fi) !=
3851 extent_key->objectid)) {
3852 path->slots[0]++;
3853 ret = 1;
3854 goto next;
3855 }
3856
3857 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
3858 ext_offset = btrfs_file_extent_offset(leaf, fi);
3859
3860 if (first_pos > key.offset - ext_offset)
3861 first_pos = key.offset - ext_offset;
3862
3863 if (!extent_locked) {
3864 lock_start = key.offset;
3865 lock_end = lock_start + num_bytes - 1;
3866 } else {
Yan Zheng66435582008-10-30 14:19:50 -04003867 if (lock_start > key.offset ||
3868 lock_end + 1 < key.offset + num_bytes) {
3869 unlock_extent(&BTRFS_I(inode)->io_tree,
3870 lock_start, lock_end, GFP_NOFS);
3871 extent_locked = 0;
3872 }
Zheng Yan1a40e232008-09-26 10:09:34 -04003873 }
3874
3875 if (!inode) {
3876 btrfs_release_path(root, path);
3877
3878 inode = btrfs_iget_locked(root->fs_info->sb,
3879 key.objectid, root);
3880 if (inode->i_state & I_NEW) {
3881 BTRFS_I(inode)->root = root;
3882 BTRFS_I(inode)->location.objectid =
3883 key.objectid;
3884 BTRFS_I(inode)->location.type =
3885 BTRFS_INODE_ITEM_KEY;
3886 BTRFS_I(inode)->location.offset = 0;
3887 btrfs_read_locked_inode(inode);
3888 unlock_new_inode(inode);
3889 }
3890 /*
3891 * some code call btrfs_commit_transaction while
3892 * holding the i_mutex, so we can't use mutex_lock
3893 * here.
3894 */
3895 if (is_bad_inode(inode) ||
3896 !mutex_trylock(&inode->i_mutex)) {
3897 iput(inode);
3898 inode = NULL;
3899 key.offset = (u64)-1;
3900 goto skip;
3901 }
3902 }
3903
3904 if (!extent_locked) {
3905 struct btrfs_ordered_extent *ordered;
3906
3907 btrfs_release_path(root, path);
3908
3909 lock_extent(&BTRFS_I(inode)->io_tree, lock_start,
3910 lock_end, GFP_NOFS);
3911 ordered = btrfs_lookup_first_ordered_extent(inode,
3912 lock_end);
3913 if (ordered &&
3914 ordered->file_offset <= lock_end &&
3915 ordered->file_offset + ordered->len > lock_start) {
3916 unlock_extent(&BTRFS_I(inode)->io_tree,
3917 lock_start, lock_end, GFP_NOFS);
3918 btrfs_start_ordered_extent(inode, ordered, 1);
3919 btrfs_put_ordered_extent(ordered);
3920 key.offset += num_bytes;
3921 goto skip;
3922 }
3923 if (ordered)
3924 btrfs_put_ordered_extent(ordered);
3925
Zheng Yan1a40e232008-09-26 10:09:34 -04003926 extent_locked = 1;
3927 continue;
3928 }
3929
3930 if (nr_extents == 1) {
3931 /* update extent pointer in place */
Zheng Yan1a40e232008-09-26 10:09:34 -04003932 btrfs_set_file_extent_disk_bytenr(leaf, fi,
3933 new_extents[0].disk_bytenr);
3934 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
3935 new_extents[0].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04003936 btrfs_mark_buffer_dirty(leaf);
3937
3938 btrfs_drop_extent_cache(inode, key.offset,
3939 key.offset + num_bytes - 1, 0);
3940
3941 ret = btrfs_inc_extent_ref(trans, root,
3942 new_extents[0].disk_bytenr,
3943 new_extents[0].disk_num_bytes,
3944 leaf->start,
3945 root->root_key.objectid,
3946 trans->transid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003947 key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04003948 BUG_ON(ret);
3949
3950 ret = btrfs_free_extent(trans, root,
3951 extent_key->objectid,
3952 extent_key->offset,
3953 leaf->start,
3954 btrfs_header_owner(leaf),
3955 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003956 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04003957 BUG_ON(ret);
3958
3959 btrfs_release_path(root, path);
3960 key.offset += num_bytes;
3961 } else {
Yan Zhengd899e052008-10-30 14:25:28 -04003962 BUG_ON(1);
3963#if 0
Zheng Yan1a40e232008-09-26 10:09:34 -04003964 u64 alloc_hint;
3965 u64 extent_len;
3966 int i;
3967 /*
3968 * drop old extent pointer at first, then insert the
3969 * new pointers one bye one
3970 */
3971 btrfs_release_path(root, path);
3972 ret = btrfs_drop_extents(trans, root, inode, key.offset,
3973 key.offset + num_bytes,
3974 key.offset, &alloc_hint);
3975 BUG_ON(ret);
3976
3977 for (i = 0; i < nr_extents; i++) {
3978 if (ext_offset >= new_extents[i].num_bytes) {
3979 ext_offset -= new_extents[i].num_bytes;
3980 continue;
3981 }
3982 extent_len = min(new_extents[i].num_bytes -
3983 ext_offset, num_bytes);
3984
3985 ret = btrfs_insert_empty_item(trans, root,
3986 path, &key,
3987 sizeof(*fi));
3988 BUG_ON(ret);
3989
3990 leaf = path->nodes[0];
3991 fi = btrfs_item_ptr(leaf, path->slots[0],
3992 struct btrfs_file_extent_item);
3993 btrfs_set_file_extent_generation(leaf, fi,
3994 trans->transid);
3995 btrfs_set_file_extent_type(leaf, fi,
3996 BTRFS_FILE_EXTENT_REG);
3997 btrfs_set_file_extent_disk_bytenr(leaf, fi,
3998 new_extents[i].disk_bytenr);
3999 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4000 new_extents[i].disk_num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -04004001 btrfs_set_file_extent_ram_bytes(leaf, fi,
4002 new_extents[i].ram_bytes);
4003
4004 btrfs_set_file_extent_compression(leaf, fi,
4005 new_extents[i].compression);
4006 btrfs_set_file_extent_encryption(leaf, fi,
4007 new_extents[i].encryption);
4008 btrfs_set_file_extent_other_encoding(leaf, fi,
4009 new_extents[i].other_encoding);
4010
Zheng Yan1a40e232008-09-26 10:09:34 -04004011 btrfs_set_file_extent_num_bytes(leaf, fi,
4012 extent_len);
4013 ext_offset += new_extents[i].offset;
4014 btrfs_set_file_extent_offset(leaf, fi,
4015 ext_offset);
4016 btrfs_mark_buffer_dirty(leaf);
4017
4018 btrfs_drop_extent_cache(inode, key.offset,
4019 key.offset + extent_len - 1, 0);
4020
4021 ret = btrfs_inc_extent_ref(trans, root,
4022 new_extents[i].disk_bytenr,
4023 new_extents[i].disk_num_bytes,
4024 leaf->start,
4025 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004026 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04004027 BUG_ON(ret);
4028 btrfs_release_path(root, path);
4029
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004030 inode_add_bytes(inode, extent_len);
Zheng Yan1a40e232008-09-26 10:09:34 -04004031
4032 ext_offset = 0;
4033 num_bytes -= extent_len;
4034 key.offset += extent_len;
4035
4036 if (num_bytes == 0)
4037 break;
4038 }
4039 BUG_ON(i >= nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04004040#endif
Zheng Yan1a40e232008-09-26 10:09:34 -04004041 }
4042
4043 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04004044 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4045 lock_end, GFP_NOFS);
4046 extent_locked = 0;
4047 }
4048skip:
4049 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS &&
4050 key.offset >= first_pos + extent_key->offset)
4051 break;
4052
4053 cond_resched();
4054 }
4055 ret = 0;
4056out:
4057 btrfs_release_path(root, path);
4058 if (inode) {
4059 mutex_unlock(&inode->i_mutex);
4060 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04004061 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4062 lock_end, GFP_NOFS);
4063 }
4064 iput(inode);
4065 }
4066 return ret;
4067}
4068
Zheng Yan1a40e232008-09-26 10:09:34 -04004069int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans,
4070 struct btrfs_root *root,
4071 struct extent_buffer *buf, u64 orig_start)
4072{
4073 int level;
4074 int ret;
4075
4076 BUG_ON(btrfs_header_generation(buf) != trans->transid);
4077 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
4078
4079 level = btrfs_header_level(buf);
4080 if (level == 0) {
4081 struct btrfs_leaf_ref *ref;
4082 struct btrfs_leaf_ref *orig_ref;
4083
4084 orig_ref = btrfs_lookup_leaf_ref(root, orig_start);
4085 if (!orig_ref)
4086 return -ENOENT;
4087
4088 ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems);
4089 if (!ref) {
4090 btrfs_free_leaf_ref(root, orig_ref);
4091 return -ENOMEM;
4092 }
4093
4094 ref->nritems = orig_ref->nritems;
4095 memcpy(ref->extents, orig_ref->extents,
4096 sizeof(ref->extents[0]) * ref->nritems);
4097
4098 btrfs_free_leaf_ref(root, orig_ref);
4099
4100 ref->root_gen = trans->transid;
4101 ref->bytenr = buf->start;
4102 ref->owner = btrfs_header_owner(buf);
4103 ref->generation = btrfs_header_generation(buf);
4104 ret = btrfs_add_leaf_ref(root, ref, 0);
4105 WARN_ON(ret);
4106 btrfs_free_leaf_ref(root, ref);
4107 }
4108 return 0;
4109}
4110
4111static int noinline invalidate_extent_cache(struct btrfs_root *root,
4112 struct extent_buffer *leaf,
4113 struct btrfs_block_group_cache *group,
4114 struct btrfs_root *target_root)
4115{
4116 struct btrfs_key key;
4117 struct inode *inode = NULL;
4118 struct btrfs_file_extent_item *fi;
4119 u64 num_bytes;
4120 u64 skip_objectid = 0;
4121 u32 nritems;
4122 u32 i;
4123
4124 nritems = btrfs_header_nritems(leaf);
4125 for (i = 0; i < nritems; i++) {
4126 btrfs_item_key_to_cpu(leaf, &key, i);
4127 if (key.objectid == skip_objectid ||
4128 key.type != BTRFS_EXTENT_DATA_KEY)
4129 continue;
4130 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
4131 if (btrfs_file_extent_type(leaf, fi) ==
4132 BTRFS_FILE_EXTENT_INLINE)
4133 continue;
4134 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
4135 continue;
4136 if (!inode || inode->i_ino != key.objectid) {
4137 iput(inode);
4138 inode = btrfs_ilookup(target_root->fs_info->sb,
4139 key.objectid, target_root, 1);
4140 }
4141 if (!inode) {
4142 skip_objectid = key.objectid;
4143 continue;
4144 }
4145 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
4146
4147 lock_extent(&BTRFS_I(inode)->io_tree, key.offset,
4148 key.offset + num_bytes - 1, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04004149 btrfs_drop_extent_cache(inode, key.offset,
4150 key.offset + num_bytes - 1, 1);
Zheng Yan1a40e232008-09-26 10:09:34 -04004151 unlock_extent(&BTRFS_I(inode)->io_tree, key.offset,
4152 key.offset + num_bytes - 1, GFP_NOFS);
4153 cond_resched();
4154 }
4155 iput(inode);
4156 return 0;
4157}
4158
4159static int noinline replace_extents_in_leaf(struct btrfs_trans_handle *trans,
4160 struct btrfs_root *root,
4161 struct extent_buffer *leaf,
4162 struct btrfs_block_group_cache *group,
4163 struct inode *reloc_inode)
4164{
4165 struct btrfs_key key;
4166 struct btrfs_key extent_key;
4167 struct btrfs_file_extent_item *fi;
4168 struct btrfs_leaf_ref *ref;
4169 struct disk_extent *new_extent;
4170 u64 bytenr;
4171 u64 num_bytes;
4172 u32 nritems;
4173 u32 i;
4174 int ext_index;
4175 int nr_extent;
4176 int ret;
4177
4178 new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
4179 BUG_ON(!new_extent);
4180
4181 ref = btrfs_lookup_leaf_ref(root, leaf->start);
4182 BUG_ON(!ref);
4183
4184 ext_index = -1;
4185 nritems = btrfs_header_nritems(leaf);
4186 for (i = 0; i < nritems; i++) {
4187 btrfs_item_key_to_cpu(leaf, &key, i);
4188 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
4189 continue;
4190 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
4191 if (btrfs_file_extent_type(leaf, fi) ==
4192 BTRFS_FILE_EXTENT_INLINE)
4193 continue;
4194 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
4195 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
4196 if (bytenr == 0)
4197 continue;
4198
4199 ext_index++;
4200 if (bytenr >= group->key.objectid + group->key.offset ||
4201 bytenr + num_bytes <= group->key.objectid)
4202 continue;
4203
4204 extent_key.objectid = bytenr;
4205 extent_key.offset = num_bytes;
4206 extent_key.type = BTRFS_EXTENT_ITEM_KEY;
4207 nr_extent = 1;
4208 ret = get_new_locations(reloc_inode, &extent_key,
4209 group->key.objectid, 1,
4210 &new_extent, &nr_extent);
4211 if (ret > 0)
4212 continue;
4213 BUG_ON(ret < 0);
4214
4215 BUG_ON(ref->extents[ext_index].bytenr != bytenr);
4216 BUG_ON(ref->extents[ext_index].num_bytes != num_bytes);
4217 ref->extents[ext_index].bytenr = new_extent->disk_bytenr;
4218 ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes;
4219
Zheng Yan1a40e232008-09-26 10:09:34 -04004220 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4221 new_extent->disk_bytenr);
4222 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4223 new_extent->disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04004224 btrfs_mark_buffer_dirty(leaf);
4225
4226 ret = btrfs_inc_extent_ref(trans, root,
4227 new_extent->disk_bytenr,
4228 new_extent->disk_num_bytes,
4229 leaf->start,
4230 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004231 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04004232 BUG_ON(ret);
4233 ret = btrfs_free_extent(trans, root,
4234 bytenr, num_bytes, leaf->start,
4235 btrfs_header_owner(leaf),
4236 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004237 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04004238 BUG_ON(ret);
4239 cond_resched();
4240 }
4241 kfree(new_extent);
4242 BUG_ON(ext_index + 1 != ref->nritems);
4243 btrfs_free_leaf_ref(root, ref);
4244 return 0;
4245}
4246
Yan Zhengf82d02d2008-10-29 14:49:05 -04004247int btrfs_free_reloc_root(struct btrfs_trans_handle *trans,
4248 struct btrfs_root *root)
Zheng Yan1a40e232008-09-26 10:09:34 -04004249{
4250 struct btrfs_root *reloc_root;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004251 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04004252
4253 if (root->reloc_root) {
4254 reloc_root = root->reloc_root;
4255 root->reloc_root = NULL;
4256 list_add(&reloc_root->dead_list,
4257 &root->fs_info->dead_reloc_roots);
Yan Zhengf82d02d2008-10-29 14:49:05 -04004258
4259 btrfs_set_root_bytenr(&reloc_root->root_item,
4260 reloc_root->node->start);
4261 btrfs_set_root_level(&root->root_item,
4262 btrfs_header_level(reloc_root->node));
4263 memset(&reloc_root->root_item.drop_progress, 0,
4264 sizeof(struct btrfs_disk_key));
4265 reloc_root->root_item.drop_level = 0;
4266
4267 ret = btrfs_update_root(trans, root->fs_info->tree_root,
4268 &reloc_root->root_key,
4269 &reloc_root->root_item);
4270 BUG_ON(ret);
Zheng Yan1a40e232008-09-26 10:09:34 -04004271 }
4272 return 0;
4273}
4274
4275int btrfs_drop_dead_reloc_roots(struct btrfs_root *root)
4276{
4277 struct btrfs_trans_handle *trans;
4278 struct btrfs_root *reloc_root;
4279 struct btrfs_root *prev_root = NULL;
4280 struct list_head dead_roots;
4281 int ret;
4282 unsigned long nr;
4283
4284 INIT_LIST_HEAD(&dead_roots);
4285 list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots);
4286
4287 while (!list_empty(&dead_roots)) {
4288 reloc_root = list_entry(dead_roots.prev,
4289 struct btrfs_root, dead_list);
4290 list_del_init(&reloc_root->dead_list);
4291
4292 BUG_ON(reloc_root->commit_root != NULL);
4293 while (1) {
4294 trans = btrfs_join_transaction(root, 1);
4295 BUG_ON(!trans);
4296
4297 mutex_lock(&root->fs_info->drop_mutex);
4298 ret = btrfs_drop_snapshot(trans, reloc_root);
4299 if (ret != -EAGAIN)
4300 break;
4301 mutex_unlock(&root->fs_info->drop_mutex);
4302
4303 nr = trans->blocks_used;
4304 ret = btrfs_end_transaction(trans, root);
4305 BUG_ON(ret);
4306 btrfs_btree_balance_dirty(root, nr);
4307 }
4308
4309 free_extent_buffer(reloc_root->node);
4310
4311 ret = btrfs_del_root(trans, root->fs_info->tree_root,
4312 &reloc_root->root_key);
4313 BUG_ON(ret);
4314 mutex_unlock(&root->fs_info->drop_mutex);
4315
4316 nr = trans->blocks_used;
4317 ret = btrfs_end_transaction(trans, root);
4318 BUG_ON(ret);
4319 btrfs_btree_balance_dirty(root, nr);
4320
4321 kfree(prev_root);
4322 prev_root = reloc_root;
4323 }
4324 if (prev_root) {
4325 btrfs_remove_leaf_refs(prev_root, (u64)-1, 0);
4326 kfree(prev_root);
4327 }
4328 return 0;
4329}
4330
4331int btrfs_add_dead_reloc_root(struct btrfs_root *root)
4332{
4333 list_add(&root->dead_list, &root->fs_info->dead_reloc_roots);
4334 return 0;
4335}
4336
4337int btrfs_cleanup_reloc_trees(struct btrfs_root *root)
4338{
4339 struct btrfs_root *reloc_root;
4340 struct btrfs_trans_handle *trans;
4341 struct btrfs_key location;
4342 int found;
4343 int ret;
4344
4345 mutex_lock(&root->fs_info->tree_reloc_mutex);
4346 ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL);
4347 BUG_ON(ret);
4348 found = !list_empty(&root->fs_info->dead_reloc_roots);
4349 mutex_unlock(&root->fs_info->tree_reloc_mutex);
4350
4351 if (found) {
4352 trans = btrfs_start_transaction(root, 1);
4353 BUG_ON(!trans);
4354 ret = btrfs_commit_transaction(trans, root);
4355 BUG_ON(ret);
4356 }
4357
4358 location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
4359 location.offset = (u64)-1;
4360 location.type = BTRFS_ROOT_ITEM_KEY;
4361
4362 reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
4363 BUG_ON(!reloc_root);
4364 btrfs_orphan_cleanup(reloc_root);
4365 return 0;
4366}
4367
4368static int noinline init_reloc_tree(struct btrfs_trans_handle *trans,
4369 struct btrfs_root *root)
4370{
4371 struct btrfs_root *reloc_root;
4372 struct extent_buffer *eb;
4373 struct btrfs_root_item *root_item;
4374 struct btrfs_key root_key;
4375 int ret;
4376
4377 BUG_ON(!root->ref_cows);
4378 if (root->reloc_root)
4379 return 0;
4380
4381 root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
4382 BUG_ON(!root_item);
4383
4384 ret = btrfs_copy_root(trans, root, root->commit_root,
4385 &eb, BTRFS_TREE_RELOC_OBJECTID);
4386 BUG_ON(ret);
4387
4388 root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
4389 root_key.offset = root->root_key.objectid;
4390 root_key.type = BTRFS_ROOT_ITEM_KEY;
4391
4392 memcpy(root_item, &root->root_item, sizeof(root_item));
4393 btrfs_set_root_refs(root_item, 0);
4394 btrfs_set_root_bytenr(root_item, eb->start);
4395 btrfs_set_root_level(root_item, btrfs_header_level(eb));
Yan Zheng84234f32008-10-29 14:49:05 -04004396 btrfs_set_root_generation(root_item, trans->transid);
Zheng Yan1a40e232008-09-26 10:09:34 -04004397
4398 btrfs_tree_unlock(eb);
4399 free_extent_buffer(eb);
4400
4401 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
4402 &root_key, root_item);
4403 BUG_ON(ret);
4404 kfree(root_item);
4405
4406 reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
4407 &root_key);
4408 BUG_ON(!reloc_root);
4409 reloc_root->last_trans = trans->transid;
4410 reloc_root->commit_root = NULL;
4411 reloc_root->ref_tree = &root->fs_info->reloc_ref_tree;
4412
4413 root->reloc_root = reloc_root;
4414 return 0;
4415}
4416
4417/*
4418 * Core function of space balance.
4419 *
4420 * The idea is using reloc trees to relocate tree blocks in reference
Yan Zhengf82d02d2008-10-29 14:49:05 -04004421 * counted roots. There is one reloc tree for each subvol, and all
4422 * reloc trees share same root key objectid. Reloc trees are snapshots
4423 * of the latest committed roots of subvols (root->commit_root).
4424 *
4425 * To relocate a tree block referenced by a subvol, there are two steps.
4426 * COW the block through subvol's reloc tree, then update block pointer
4427 * in the subvol to point to the new block. Since all reloc trees share
4428 * same root key objectid, doing special handing for tree blocks owned
4429 * by them is easy. Once a tree block has been COWed in one reloc tree,
4430 * we can use the resulting new block directly when the same block is
4431 * required to COW again through other reloc trees. By this way, relocated
4432 * tree blocks are shared between reloc trees, so they are also shared
4433 * between subvols.
Zheng Yan1a40e232008-09-26 10:09:34 -04004434 */
4435static int noinline relocate_one_path(struct btrfs_trans_handle *trans,
4436 struct btrfs_root *root,
4437 struct btrfs_path *path,
4438 struct btrfs_key *first_key,
4439 struct btrfs_ref_path *ref_path,
4440 struct btrfs_block_group_cache *group,
4441 struct inode *reloc_inode)
4442{
4443 struct btrfs_root *reloc_root;
4444 struct extent_buffer *eb = NULL;
4445 struct btrfs_key *keys;
4446 u64 *nodes;
4447 int level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004448 int shared_level;
Zheng Yan1a40e232008-09-26 10:09:34 -04004449 int lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04004450 int ret;
4451
4452 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
4453 lowest_level = ref_path->owner_objectid;
4454
Yan Zhengf82d02d2008-10-29 14:49:05 -04004455 if (!root->ref_cows) {
Zheng Yan1a40e232008-09-26 10:09:34 -04004456 path->lowest_level = lowest_level;
4457 ret = btrfs_search_slot(trans, root, first_key, path, 0, 1);
4458 BUG_ON(ret < 0);
4459 path->lowest_level = 0;
4460 btrfs_release_path(root, path);
4461 return 0;
4462 }
4463
Zheng Yan1a40e232008-09-26 10:09:34 -04004464 mutex_lock(&root->fs_info->tree_reloc_mutex);
4465 ret = init_reloc_tree(trans, root);
4466 BUG_ON(ret);
4467 reloc_root = root->reloc_root;
4468
Yan Zhengf82d02d2008-10-29 14:49:05 -04004469 shared_level = ref_path->shared_level;
4470 ref_path->shared_level = BTRFS_MAX_LEVEL - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04004471
Yan Zhengf82d02d2008-10-29 14:49:05 -04004472 keys = ref_path->node_keys;
4473 nodes = ref_path->new_nodes;
4474 memset(&keys[shared_level + 1], 0,
4475 sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1));
4476 memset(&nodes[shared_level + 1], 0,
4477 sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1));
Zheng Yan1a40e232008-09-26 10:09:34 -04004478
Yan Zhengf82d02d2008-10-29 14:49:05 -04004479 if (nodes[lowest_level] == 0) {
4480 path->lowest_level = lowest_level;
4481 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
4482 0, 1);
4483 BUG_ON(ret);
4484 for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) {
4485 eb = path->nodes[level];
4486 if (!eb || eb == reloc_root->node)
4487 break;
4488 nodes[level] = eb->start;
4489 if (level == 0)
4490 btrfs_item_key_to_cpu(eb, &keys[level], 0);
4491 else
4492 btrfs_node_key_to_cpu(eb, &keys[level], 0);
4493 }
4494 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
4495 eb = path->nodes[0];
4496 ret = replace_extents_in_leaf(trans, reloc_root, eb,
4497 group, reloc_inode);
4498 BUG_ON(ret);
4499 }
4500 btrfs_release_path(reloc_root, path);
4501 } else {
Zheng Yan1a40e232008-09-26 10:09:34 -04004502 ret = btrfs_merge_path(trans, reloc_root, keys, nodes,
Yan Zhengf82d02d2008-10-29 14:49:05 -04004503 lowest_level);
Zheng Yan1a40e232008-09-26 10:09:34 -04004504 BUG_ON(ret);
4505 }
4506
Zheng Yan1a40e232008-09-26 10:09:34 -04004507 /*
4508 * replace tree blocks in the fs tree with tree blocks in
4509 * the reloc tree.
4510 */
4511 ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level);
4512 BUG_ON(ret < 0);
4513
4514 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04004515 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
4516 0, 0);
4517 BUG_ON(ret);
4518 extent_buffer_get(path->nodes[0]);
4519 eb = path->nodes[0];
4520 btrfs_release_path(reloc_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04004521 ret = invalidate_extent_cache(reloc_root, eb, group, root);
4522 BUG_ON(ret);
4523 free_extent_buffer(eb);
4524 }
Zheng Yan1a40e232008-09-26 10:09:34 -04004525
Yan Zhengf82d02d2008-10-29 14:49:05 -04004526 mutex_unlock(&root->fs_info->tree_reloc_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04004527 path->lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04004528 return 0;
4529}
4530
4531static int noinline relocate_tree_block(struct btrfs_trans_handle *trans,
4532 struct btrfs_root *root,
4533 struct btrfs_path *path,
4534 struct btrfs_key *first_key,
4535 struct btrfs_ref_path *ref_path)
4536{
4537 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04004538
4539 ret = relocate_one_path(trans, root, path, first_key,
4540 ref_path, NULL, NULL);
4541 BUG_ON(ret);
4542
4543 if (root == root->fs_info->extent_root)
4544 btrfs_extent_post_op(trans, root);
Zheng Yan1a40e232008-09-26 10:09:34 -04004545
4546 return 0;
4547}
4548
4549static int noinline del_extent_zero(struct btrfs_trans_handle *trans,
4550 struct btrfs_root *extent_root,
4551 struct btrfs_path *path,
4552 struct btrfs_key *extent_key)
4553{
4554 int ret;
4555
Zheng Yan1a40e232008-09-26 10:09:34 -04004556 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
4557 if (ret)
4558 goto out;
4559 ret = btrfs_del_item(trans, extent_root, path);
4560out:
Chris Masonedbd8d42007-12-21 16:27:24 -05004561 btrfs_release_path(extent_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04004562 return ret;
4563}
4564
4565static struct btrfs_root noinline *read_ref_root(struct btrfs_fs_info *fs_info,
4566 struct btrfs_ref_path *ref_path)
4567{
4568 struct btrfs_key root_key;
4569
4570 root_key.objectid = ref_path->root_objectid;
4571 root_key.type = BTRFS_ROOT_ITEM_KEY;
4572 if (is_cowonly_root(ref_path->root_objectid))
4573 root_key.offset = 0;
4574 else
4575 root_key.offset = (u64)-1;
4576
4577 return btrfs_read_fs_root_no_name(fs_info, &root_key);
4578}
4579
4580static int noinline relocate_one_extent(struct btrfs_root *extent_root,
4581 struct btrfs_path *path,
4582 struct btrfs_key *extent_key,
4583 struct btrfs_block_group_cache *group,
4584 struct inode *reloc_inode, int pass)
4585{
4586 struct btrfs_trans_handle *trans;
4587 struct btrfs_root *found_root;
4588 struct btrfs_ref_path *ref_path = NULL;
4589 struct disk_extent *new_extents = NULL;
4590 int nr_extents = 0;
4591 int loops;
4592 int ret;
4593 int level;
4594 struct btrfs_key first_key;
4595 u64 prev_block = 0;
4596
Zheng Yan1a40e232008-09-26 10:09:34 -04004597
4598 trans = btrfs_start_transaction(extent_root, 1);
4599 BUG_ON(!trans);
4600
4601 if (extent_key->objectid == 0) {
4602 ret = del_extent_zero(trans, extent_root, path, extent_key);
4603 goto out;
4604 }
4605
4606 ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS);
4607 if (!ref_path) {
4608 ret = -ENOMEM;
4609 goto out;
4610 }
4611
4612 for (loops = 0; ; loops++) {
4613 if (loops == 0) {
4614 ret = btrfs_first_ref_path(trans, extent_root, ref_path,
4615 extent_key->objectid);
4616 } else {
4617 ret = btrfs_next_ref_path(trans, extent_root, ref_path);
4618 }
4619 if (ret < 0)
4620 goto out;
4621 if (ret > 0)
4622 break;
4623
4624 if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID ||
4625 ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID)
4626 continue;
4627
4628 found_root = read_ref_root(extent_root->fs_info, ref_path);
4629 BUG_ON(!found_root);
4630 /*
4631 * for reference counted tree, only process reference paths
4632 * rooted at the latest committed root.
4633 */
4634 if (found_root->ref_cows &&
4635 ref_path->root_generation != found_root->root_key.offset)
4636 continue;
4637
4638 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
4639 if (pass == 0) {
4640 /*
4641 * copy data extents to new locations
4642 */
4643 u64 group_start = group->key.objectid;
4644 ret = relocate_data_extent(reloc_inode,
4645 extent_key,
4646 group_start);
4647 if (ret < 0)
4648 goto out;
4649 break;
4650 }
4651 level = 0;
4652 } else {
4653 level = ref_path->owner_objectid;
4654 }
4655
4656 if (prev_block != ref_path->nodes[level]) {
4657 struct extent_buffer *eb;
4658 u64 block_start = ref_path->nodes[level];
4659 u64 block_size = btrfs_level_size(found_root, level);
4660
4661 eb = read_tree_block(found_root, block_start,
4662 block_size, 0);
4663 btrfs_tree_lock(eb);
4664 BUG_ON(level != btrfs_header_level(eb));
4665
4666 if (level == 0)
4667 btrfs_item_key_to_cpu(eb, &first_key, 0);
4668 else
4669 btrfs_node_key_to_cpu(eb, &first_key, 0);
4670
4671 btrfs_tree_unlock(eb);
4672 free_extent_buffer(eb);
4673 prev_block = block_start;
4674 }
4675
4676 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID &&
4677 pass >= 2) {
4678 /*
4679 * use fallback method to process the remaining
4680 * references.
4681 */
4682 if (!new_extents) {
4683 u64 group_start = group->key.objectid;
Yan Zhengd899e052008-10-30 14:25:28 -04004684 new_extents = kmalloc(sizeof(*new_extents),
4685 GFP_NOFS);
4686 nr_extents = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04004687 ret = get_new_locations(reloc_inode,
4688 extent_key,
Yan Zhengd899e052008-10-30 14:25:28 -04004689 group_start, 1,
Zheng Yan1a40e232008-09-26 10:09:34 -04004690 &new_extents,
4691 &nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04004692 if (ret)
Zheng Yan1a40e232008-09-26 10:09:34 -04004693 goto out;
4694 }
4695 btrfs_record_root_in_trans(found_root);
4696 ret = replace_one_extent(trans, found_root,
4697 path, extent_key,
4698 &first_key, ref_path,
4699 new_extents, nr_extents);
4700 if (ret < 0)
4701 goto out;
4702 continue;
4703 }
4704
4705 btrfs_record_root_in_trans(found_root);
4706 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
4707 ret = relocate_tree_block(trans, found_root, path,
4708 &first_key, ref_path);
4709 } else {
4710 /*
4711 * try to update data extent references while
4712 * keeping metadata shared between snapshots.
4713 */
4714 ret = relocate_one_path(trans, found_root, path,
4715 &first_key, ref_path,
4716 group, reloc_inode);
4717 }
4718 if (ret < 0)
4719 goto out;
4720 }
4721 ret = 0;
4722out:
4723 btrfs_end_transaction(trans, extent_root);
4724 kfree(new_extents);
4725 kfree(ref_path);
Chris Masonedbd8d42007-12-21 16:27:24 -05004726 return ret;
4727}
4728
Chris Masonec44a352008-04-28 15:29:52 -04004729static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
4730{
4731 u64 num_devices;
4732 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
4733 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
4734
Chris Masona061fc82008-05-07 11:43:44 -04004735 num_devices = root->fs_info->fs_devices->num_devices;
Chris Masonec44a352008-04-28 15:29:52 -04004736 if (num_devices == 1) {
4737 stripped |= BTRFS_BLOCK_GROUP_DUP;
4738 stripped = flags & ~stripped;
4739
4740 /* turn raid0 into single device chunks */
4741 if (flags & BTRFS_BLOCK_GROUP_RAID0)
4742 return stripped;
4743
4744 /* turn mirroring into duplication */
4745 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
4746 BTRFS_BLOCK_GROUP_RAID10))
4747 return stripped | BTRFS_BLOCK_GROUP_DUP;
4748 return flags;
4749 } else {
4750 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04004751 if (flags & stripped)
4752 return flags;
4753
4754 stripped |= BTRFS_BLOCK_GROUP_DUP;
4755 stripped = flags & ~stripped;
4756
4757 /* switch duplicated blocks with raid1 */
4758 if (flags & BTRFS_BLOCK_GROUP_DUP)
4759 return stripped | BTRFS_BLOCK_GROUP_RAID1;
4760
4761 /* turn single device chunks into raid0 */
4762 return stripped | BTRFS_BLOCK_GROUP_RAID0;
4763 }
4764 return flags;
4765}
4766
Chris Mason0ef3e662008-05-24 14:04:53 -04004767int __alloc_chunk_for_shrink(struct btrfs_root *root,
4768 struct btrfs_block_group_cache *shrink_block_group,
4769 int force)
4770{
4771 struct btrfs_trans_handle *trans;
4772 u64 new_alloc_flags;
4773 u64 calc;
4774
Chris Masonc286ac42008-07-22 23:06:41 -04004775 spin_lock(&shrink_block_group->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04004776 if (btrfs_block_group_used(&shrink_block_group->item) > 0) {
Chris Masonc286ac42008-07-22 23:06:41 -04004777 spin_unlock(&shrink_block_group->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04004778
Chris Mason0ef3e662008-05-24 14:04:53 -04004779 trans = btrfs_start_transaction(root, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04004780 spin_lock(&shrink_block_group->lock);
Chris Mason7d9eb122008-07-08 14:19:17 -04004781
Chris Mason0ef3e662008-05-24 14:04:53 -04004782 new_alloc_flags = update_block_group_flags(root,
4783 shrink_block_group->flags);
4784 if (new_alloc_flags != shrink_block_group->flags) {
4785 calc =
4786 btrfs_block_group_used(&shrink_block_group->item);
4787 } else {
4788 calc = shrink_block_group->key.offset;
4789 }
Chris Masonc286ac42008-07-22 23:06:41 -04004790 spin_unlock(&shrink_block_group->lock);
4791
Chris Mason0ef3e662008-05-24 14:04:53 -04004792 do_chunk_alloc(trans, root->fs_info->extent_root,
4793 calc + 2 * 1024 * 1024, new_alloc_flags, force);
Chris Mason7d9eb122008-07-08 14:19:17 -04004794
Chris Mason0ef3e662008-05-24 14:04:53 -04004795 btrfs_end_transaction(trans, root);
Chris Masonc286ac42008-07-22 23:06:41 -04004796 } else
4797 spin_unlock(&shrink_block_group->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04004798 return 0;
4799}
4800
Zheng Yan1a40e232008-09-26 10:09:34 -04004801static int __insert_orphan_inode(struct btrfs_trans_handle *trans,
4802 struct btrfs_root *root,
4803 u64 objectid, u64 size)
4804{
4805 struct btrfs_path *path;
4806 struct btrfs_inode_item *item;
4807 struct extent_buffer *leaf;
4808 int ret;
4809
4810 path = btrfs_alloc_path();
4811 if (!path)
4812 return -ENOMEM;
4813
4814 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
4815 if (ret)
4816 goto out;
4817
4818 leaf = path->nodes[0];
4819 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item);
4820 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
4821 btrfs_set_inode_generation(leaf, item, 1);
4822 btrfs_set_inode_size(leaf, item, size);
4823 btrfs_set_inode_mode(leaf, item, S_IFREG | 0600);
Yan Zhengd899e052008-10-30 14:25:28 -04004824 btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NODATASUM |
4825 BTRFS_INODE_NOCOMPRESS);
Zheng Yan1a40e232008-09-26 10:09:34 -04004826 btrfs_mark_buffer_dirty(leaf);
4827 btrfs_release_path(root, path);
4828out:
4829 btrfs_free_path(path);
4830 return ret;
4831}
4832
4833static struct inode noinline *create_reloc_inode(struct btrfs_fs_info *fs_info,
4834 struct btrfs_block_group_cache *group)
4835{
4836 struct inode *inode = NULL;
4837 struct btrfs_trans_handle *trans;
4838 struct btrfs_root *root;
4839 struct btrfs_key root_key;
4840 u64 objectid = BTRFS_FIRST_FREE_OBJECTID;
4841 int err = 0;
4842
4843 root_key.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
4844 root_key.type = BTRFS_ROOT_ITEM_KEY;
4845 root_key.offset = (u64)-1;
4846 root = btrfs_read_fs_root_no_name(fs_info, &root_key);
4847 if (IS_ERR(root))
4848 return ERR_CAST(root);
4849
4850 trans = btrfs_start_transaction(root, 1);
4851 BUG_ON(!trans);
4852
4853 err = btrfs_find_free_objectid(trans, root, objectid, &objectid);
4854 if (err)
4855 goto out;
4856
4857 err = __insert_orphan_inode(trans, root, objectid, group->key.offset);
4858 BUG_ON(err);
4859
4860 err = btrfs_insert_file_extent(trans, root, objectid, 0, 0, 0,
Chris Masonc8b97812008-10-29 14:49:59 -04004861 group->key.offset, 0, group->key.offset,
4862 0, 0, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04004863 BUG_ON(err);
4864
4865 inode = btrfs_iget_locked(root->fs_info->sb, objectid, root);
4866 if (inode->i_state & I_NEW) {
4867 BTRFS_I(inode)->root = root;
4868 BTRFS_I(inode)->location.objectid = objectid;
4869 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
4870 BTRFS_I(inode)->location.offset = 0;
4871 btrfs_read_locked_inode(inode);
4872 unlock_new_inode(inode);
4873 BUG_ON(is_bad_inode(inode));
4874 } else {
4875 BUG_ON(1);
4876 }
4877
4878 err = btrfs_orphan_add(trans, inode);
4879out:
4880 btrfs_end_transaction(trans, root);
4881 if (err) {
4882 if (inode)
4883 iput(inode);
4884 inode = ERR_PTR(err);
4885 }
4886 return inode;
4887}
4888
4889int btrfs_relocate_block_group(struct btrfs_root *root, u64 group_start)
Chris Masonedbd8d42007-12-21 16:27:24 -05004890{
4891 struct btrfs_trans_handle *trans;
Chris Masonedbd8d42007-12-21 16:27:24 -05004892 struct btrfs_path *path;
Zheng Yan1a40e232008-09-26 10:09:34 -04004893 struct btrfs_fs_info *info = root->fs_info;
4894 struct extent_buffer *leaf;
4895 struct inode *reloc_inode;
4896 struct btrfs_block_group_cache *block_group;
4897 struct btrfs_key key;
Yan Zhengd899e052008-10-30 14:25:28 -04004898 u64 skipped;
Chris Masonedbd8d42007-12-21 16:27:24 -05004899 u64 cur_byte;
4900 u64 total_found;
Chris Masonedbd8d42007-12-21 16:27:24 -05004901 u32 nritems;
4902 int ret;
Chris Masona061fc82008-05-07 11:43:44 -04004903 int progress;
Zheng Yan1a40e232008-09-26 10:09:34 -04004904 int pass = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05004905
Chris Masonedbd8d42007-12-21 16:27:24 -05004906 root = root->fs_info->extent_root;
Zheng Yan1a40e232008-09-26 10:09:34 -04004907
4908 block_group = btrfs_lookup_block_group(info, group_start);
4909 BUG_ON(!block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05004910
Chris Mason323da792008-05-09 11:46:48 -04004911 printk("btrfs relocating block group %llu flags %llu\n",
Zheng Yan1a40e232008-09-26 10:09:34 -04004912 (unsigned long long)block_group->key.objectid,
4913 (unsigned long long)block_group->flags);
Chris Mason323da792008-05-09 11:46:48 -04004914
Zheng Yan1a40e232008-09-26 10:09:34 -04004915 path = btrfs_alloc_path();
4916 BUG_ON(!path);
Chris Mason323da792008-05-09 11:46:48 -04004917
Zheng Yan1a40e232008-09-26 10:09:34 -04004918 reloc_inode = create_reloc_inode(info, block_group);
4919 BUG_ON(IS_ERR(reloc_inode));
4920
Zheng Yan1a40e232008-09-26 10:09:34 -04004921 __alloc_chunk_for_shrink(root, block_group, 1);
4922 block_group->ro = 1;
4923 block_group->space_info->total_bytes -= block_group->key.offset;
4924
Zheng Yan1a40e232008-09-26 10:09:34 -04004925 btrfs_start_delalloc_inodes(info->tree_root);
4926 btrfs_wait_ordered_extents(info->tree_root, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -04004927again:
Yan Zhengd899e052008-10-30 14:25:28 -04004928 skipped = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05004929 total_found = 0;
Chris Masona061fc82008-05-07 11:43:44 -04004930 progress = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04004931 key.objectid = block_group->key.objectid;
Chris Masonedbd8d42007-12-21 16:27:24 -05004932 key.offset = 0;
4933 key.type = 0;
Yan73e48b22008-01-03 14:14:39 -05004934 cur_byte = key.objectid;
Chris Mason4313b392008-01-03 09:08:48 -05004935
Zheng Yan1a40e232008-09-26 10:09:34 -04004936 trans = btrfs_start_transaction(info->tree_root, 1);
4937 btrfs_commit_transaction(trans, info->tree_root);
Chris Masonea8c2812008-08-04 23:17:27 -04004938
Zheng Yan1a40e232008-09-26 10:09:34 -04004939 mutex_lock(&root->fs_info->cleaner_mutex);
4940 btrfs_clean_old_snapshots(info->tree_root);
4941 btrfs_remove_leaf_refs(info->tree_root, (u64)-1, 1);
4942 mutex_unlock(&root->fs_info->cleaner_mutex);
Chris Masonea8c2812008-08-04 23:17:27 -04004943
Yan73e48b22008-01-03 14:14:39 -05004944 while(1) {
Chris Masonedbd8d42007-12-21 16:27:24 -05004945 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4946 if (ret < 0)
4947 goto out;
Chris Mason7d9eb122008-07-08 14:19:17 -04004948next:
Chris Masonedbd8d42007-12-21 16:27:24 -05004949 leaf = path->nodes[0];
Chris Masonedbd8d42007-12-21 16:27:24 -05004950 nritems = btrfs_header_nritems(leaf);
Yan73e48b22008-01-03 14:14:39 -05004951 if (path->slots[0] >= nritems) {
4952 ret = btrfs_next_leaf(root, path);
4953 if (ret < 0)
4954 goto out;
4955 if (ret == 1) {
4956 ret = 0;
4957 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05004958 }
Yan73e48b22008-01-03 14:14:39 -05004959 leaf = path->nodes[0];
4960 nritems = btrfs_header_nritems(leaf);
4961 }
4962
Zheng Yan1a40e232008-09-26 10:09:34 -04004963 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Mason725c8462008-01-04 16:47:16 -05004964
Zheng Yan1a40e232008-09-26 10:09:34 -04004965 if (key.objectid >= block_group->key.objectid +
4966 block_group->key.offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04004967 break;
4968
Chris Mason725c8462008-01-04 16:47:16 -05004969 if (progress && need_resched()) {
Chris Mason725c8462008-01-04 16:47:16 -05004970 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04004971 cond_resched();
Chris Mason725c8462008-01-04 16:47:16 -05004972 progress = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04004973 continue;
Chris Mason725c8462008-01-04 16:47:16 -05004974 }
4975 progress = 1;
4976
Zheng Yan1a40e232008-09-26 10:09:34 -04004977 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY ||
4978 key.objectid + key.offset <= cur_byte) {
Yan73e48b22008-01-03 14:14:39 -05004979 path->slots[0]++;
Chris Masonedbd8d42007-12-21 16:27:24 -05004980 goto next;
4981 }
Yan73e48b22008-01-03 14:14:39 -05004982
Chris Masonedbd8d42007-12-21 16:27:24 -05004983 total_found++;
Zheng Yan1a40e232008-09-26 10:09:34 -04004984 cur_byte = key.objectid + key.offset;
Chris Masonedbd8d42007-12-21 16:27:24 -05004985 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04004986
4987 __alloc_chunk_for_shrink(root, block_group, 0);
4988 ret = relocate_one_extent(root, path, &key, block_group,
4989 reloc_inode, pass);
4990 BUG_ON(ret < 0);
Yan Zhengd899e052008-10-30 14:25:28 -04004991 if (ret > 0)
4992 skipped++;
Zheng Yan1a40e232008-09-26 10:09:34 -04004993
4994 key.objectid = cur_byte;
4995 key.type = 0;
4996 key.offset = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05004997 }
4998
4999 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005000
5001 if (pass == 0) {
5002 btrfs_wait_ordered_range(reloc_inode, 0, (u64)-1);
5003 invalidate_mapping_pages(reloc_inode->i_mapping, 0, -1);
5004 WARN_ON(reloc_inode->i_mapping->nrpages);
5005 }
Chris Masonedbd8d42007-12-21 16:27:24 -05005006
5007 if (total_found > 0) {
Zheng Yan1a40e232008-09-26 10:09:34 -04005008 printk("btrfs found %llu extents in pass %d\n",
5009 (unsigned long long)total_found, pass);
5010 pass++;
Yan Zhengd899e052008-10-30 14:25:28 -04005011 if (total_found == skipped && pass > 2) {
5012 iput(reloc_inode);
5013 reloc_inode = create_reloc_inode(info, block_group);
5014 pass = 0;
5015 }
Chris Masonedbd8d42007-12-21 16:27:24 -05005016 goto again;
5017 }
5018
Zheng Yan1a40e232008-09-26 10:09:34 -04005019 /* delete reloc_inode */
5020 iput(reloc_inode);
Chris Mason7d9eb122008-07-08 14:19:17 -04005021
Zheng Yan1a40e232008-09-26 10:09:34 -04005022 /* unpin extents in this range */
5023 trans = btrfs_start_transaction(info->tree_root, 1);
5024 btrfs_commit_transaction(trans, info->tree_root);
Chris Mason0ef3e662008-05-24 14:04:53 -04005025
Zheng Yan1a40e232008-09-26 10:09:34 -04005026 spin_lock(&block_group->lock);
5027 WARN_ON(block_group->pinned > 0);
5028 WARN_ON(block_group->reserved > 0);
5029 WARN_ON(btrfs_block_group_used(&block_group->item) > 0);
5030 spin_unlock(&block_group->lock);
5031 ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05005032out:
Zheng Yan1a40e232008-09-26 10:09:34 -04005033 btrfs_free_path(path);
Chris Masonedbd8d42007-12-21 16:27:24 -05005034 return ret;
5035}
5036
Chris Mason0b86a832008-03-24 15:01:56 -04005037int find_first_block_group(struct btrfs_root *root, struct btrfs_path *path,
5038 struct btrfs_key *key)
5039{
Chris Mason925baed2008-06-25 16:01:30 -04005040 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005041 struct btrfs_key found_key;
5042 struct extent_buffer *leaf;
5043 int slot;
5044
5045 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
5046 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04005047 goto out;
5048
Chris Mason0b86a832008-03-24 15:01:56 -04005049 while(1) {
5050 slot = path->slots[0];
5051 leaf = path->nodes[0];
5052 if (slot >= btrfs_header_nritems(leaf)) {
5053 ret = btrfs_next_leaf(root, path);
5054 if (ret == 0)
5055 continue;
5056 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04005057 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04005058 break;
5059 }
5060 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5061
5062 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04005063 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
5064 ret = 0;
5065 goto out;
5066 }
Chris Mason0b86a832008-03-24 15:01:56 -04005067 path->slots[0]++;
5068 }
5069 ret = -ENOENT;
Chris Mason925baed2008-06-25 16:01:30 -04005070out:
Chris Mason0b86a832008-03-24 15:01:56 -04005071 return ret;
5072}
5073
Zheng Yan1a40e232008-09-26 10:09:34 -04005074int btrfs_free_block_groups(struct btrfs_fs_info *info)
5075{
5076 struct btrfs_block_group_cache *block_group;
5077 struct rb_node *n;
5078
Zheng Yan1a40e232008-09-26 10:09:34 -04005079 spin_lock(&info->block_group_cache_lock);
5080 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
5081 block_group = rb_entry(n, struct btrfs_block_group_cache,
5082 cache_node);
Zheng Yan1a40e232008-09-26 10:09:34 -04005083 rb_erase(&block_group->cache_node,
5084 &info->block_group_cache_tree);
Yan Zhengd899e052008-10-30 14:25:28 -04005085 spin_unlock(&info->block_group_cache_lock);
5086
5087 btrfs_remove_free_space_cache(block_group);
Josef Bacik80eb2342008-10-29 14:49:05 -04005088 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04005089 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04005090 up_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04005091 kfree(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04005092
5093 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04005094 }
5095 spin_unlock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04005096 return 0;
5097}
5098
Chris Mason9078a3e2007-04-26 16:46:15 -04005099int btrfs_read_block_groups(struct btrfs_root *root)
5100{
5101 struct btrfs_path *path;
5102 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04005103 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04005104 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04005105 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04005106 struct btrfs_key key;
5107 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04005108 struct extent_buffer *leaf;
Chris Mason96b51792007-10-15 16:15:19 -04005109
Chris Masonbe744172007-05-06 10:15:01 -04005110 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04005111 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005112 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04005113 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04005114 path = btrfs_alloc_path();
5115 if (!path)
5116 return -ENOMEM;
5117
5118 while(1) {
Chris Mason0b86a832008-03-24 15:01:56 -04005119 ret = find_first_block_group(root, path, &key);
5120 if (ret > 0) {
5121 ret = 0;
5122 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04005123 }
Chris Mason0b86a832008-03-24 15:01:56 -04005124 if (ret != 0)
5125 goto error;
5126
Chris Mason5f39d392007-10-15 16:14:19 -04005127 leaf = path->nodes[0];
5128 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04005129 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04005130 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04005131 ret = -ENOMEM;
Chris Mason9078a3e2007-04-26 16:46:15 -04005132 break;
5133 }
Chris Mason3e1ad542007-05-07 20:03:49 -04005134
Chris Masonc286ac42008-07-22 23:06:41 -04005135 spin_lock_init(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04005136 mutex_init(&cache->alloc_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005137 INIT_LIST_HEAD(&cache->list);
Chris Mason5f39d392007-10-15 16:14:19 -04005138 read_extent_buffer(leaf, &cache->item,
5139 btrfs_item_ptr_offset(leaf, path->slots[0]),
5140 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04005141 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04005142
Chris Mason9078a3e2007-04-26 16:46:15 -04005143 key.objectid = found_key.objectid + found_key.offset;
5144 btrfs_release_path(root, path);
Chris Mason0b86a832008-03-24 15:01:56 -04005145 cache->flags = btrfs_block_group_flags(&cache->item);
Chris Mason96b51792007-10-15 16:15:19 -04005146
Chris Mason6324fbf2008-03-24 15:01:59 -04005147 ret = update_space_info(info, cache->flags, found_key.offset,
5148 btrfs_block_group_used(&cache->item),
5149 &space_info);
5150 BUG_ON(ret);
5151 cache->space_info = space_info;
Josef Bacik80eb2342008-10-29 14:49:05 -04005152 down_write(&space_info->groups_sem);
5153 list_add_tail(&cache->list, &space_info->block_groups);
5154 up_write(&space_info->groups_sem);
Chris Mason6324fbf2008-03-24 15:01:59 -04005155
Josef Bacik0f9dd462008-09-23 13:14:11 -04005156 ret = btrfs_add_block_group_cache(root->fs_info, cache);
5157 BUG_ON(ret);
Chris Mason75ccf472008-09-30 19:24:06 -04005158
5159 set_avail_alloc_bits(root->fs_info, cache->flags);
Chris Mason9078a3e2007-04-26 16:46:15 -04005160 }
Chris Mason0b86a832008-03-24 15:01:56 -04005161 ret = 0;
5162error:
Chris Mason9078a3e2007-04-26 16:46:15 -04005163 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04005164 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04005165}
Chris Mason6324fbf2008-03-24 15:01:59 -04005166
5167int btrfs_make_block_group(struct btrfs_trans_handle *trans,
5168 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04005169 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04005170 u64 size)
5171{
5172 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04005173 struct btrfs_root *extent_root;
5174 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04005175
5176 extent_root = root->fs_info->extent_root;
Chris Mason6324fbf2008-03-24 15:01:59 -04005177
Chris Masone02119d2008-09-05 16:13:11 -04005178 root->fs_info->last_trans_new_blockgroup = trans->transid;
5179
Chris Mason8f18cf12008-04-25 16:53:30 -04005180 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005181 if (!cache)
5182 return -ENOMEM;
5183
Chris Masone17cade2008-04-15 15:41:47 -04005184 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04005185 cache->key.offset = size;
Chris Masonc286ac42008-07-22 23:06:41 -04005186 spin_lock_init(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04005187 mutex_init(&cache->alloc_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005188 INIT_LIST_HEAD(&cache->list);
Chris Mason6324fbf2008-03-24 15:01:59 -04005189 btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason0ef3e662008-05-24 14:04:53 -04005190
Chris Mason6324fbf2008-03-24 15:01:59 -04005191 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04005192 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
5193 cache->flags = type;
5194 btrfs_set_block_group_flags(&cache->item, type);
5195
5196 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
5197 &cache->space_info);
5198 BUG_ON(ret);
Josef Bacik80eb2342008-10-29 14:49:05 -04005199 down_write(&cache->space_info->groups_sem);
5200 list_add_tail(&cache->list, &cache->space_info->block_groups);
5201 up_write(&cache->space_info->groups_sem);
Chris Mason6324fbf2008-03-24 15:01:59 -04005202
Josef Bacik0f9dd462008-09-23 13:14:11 -04005203 ret = btrfs_add_block_group_cache(root->fs_info, cache);
5204 BUG_ON(ret);
Chris Masonc286ac42008-07-22 23:06:41 -04005205
Chris Mason6324fbf2008-03-24 15:01:59 -04005206 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
5207 sizeof(cache->item));
5208 BUG_ON(ret);
5209
Chris Mason87ef2bb2008-10-30 11:23:27 -04005210 finish_current_insert(trans, extent_root, 0);
5211 ret = del_pending_extents(trans, extent_root, 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04005212 BUG_ON(ret);
Chris Masond18a2c42008-04-04 15:40:00 -04005213 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04005214
Chris Mason6324fbf2008-03-24 15:01:59 -04005215 return 0;
5216}
Zheng Yan1a40e232008-09-26 10:09:34 -04005217
5218int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
5219 struct btrfs_root *root, u64 group_start)
5220{
5221 struct btrfs_path *path;
5222 struct btrfs_block_group_cache *block_group;
5223 struct btrfs_key key;
5224 int ret;
5225
Zheng Yan1a40e232008-09-26 10:09:34 -04005226 root = root->fs_info->extent_root;
5227
5228 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
5229 BUG_ON(!block_group);
5230
5231 memcpy(&key, &block_group->key, sizeof(key));
5232
5233 path = btrfs_alloc_path();
5234 BUG_ON(!path);
5235
5236 btrfs_remove_free_space_cache(block_group);
5237 rb_erase(&block_group->cache_node,
5238 &root->fs_info->block_group_cache_tree);
Josef Bacik80eb2342008-10-29 14:49:05 -04005239 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04005240 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04005241 up_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04005242
5243 /*
5244 memset(shrink_block_group, 0, sizeof(*shrink_block_group));
5245 kfree(shrink_block_group);
5246 */
5247
5248 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
5249 if (ret > 0)
5250 ret = -EIO;
5251 if (ret < 0)
5252 goto out;
5253
5254 ret = btrfs_del_item(trans, root, path);
5255out:
5256 btrfs_free_path(path);
5257 return ret;
5258}