blob: 15d62a9214b7ac7d180e4847c41e6dd7a2bc979b [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 Masonb7a9f292009-02-04 09:23:45 -050022#include <linux/sort.h>
Chris Mason4184ea72009-03-10 12:39:20 -040023#include <linux/rcupdate.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050024#include "compat.h"
Chris Mason74493f72007-12-11 09:25:06 -050025#include "hash.h"
Miguela5eb62e2008-04-11 15:45:51 -040026#include "crc32c.h"
Chris Masonfec577f2007-02-26 10:40:21 -050027#include "ctree.h"
28#include "disk-io.h"
29#include "print-tree.h"
Chris Masone089f052007-03-16 16:20:31 -040030#include "transaction.h"
Chris Mason0b86a832008-03-24 15:01:56 -040031#include "volumes.h"
Chris Mason925baed2008-06-25 16:01:30 -040032#include "locking.h"
Yan Zheng31153d82008-07-28 15:32:19 -040033#include "ref-cache.h"
Chris Masonfec577f2007-02-26 10:40:21 -050034
Zheng Yan31840ae2008-09-23 13:14:14 -040035#define PENDING_EXTENT_INSERT 0
36#define PENDING_EXTENT_DELETE 1
37#define PENDING_BACKREF_UPDATE 2
38
39struct pending_extent_op {
40 int type;
41 u64 bytenr;
42 u64 num_bytes;
43 u64 parent;
44 u64 orig_parent;
45 u64 generation;
46 u64 orig_generation;
47 int level;
Josef Bacikf3465ca2008-11-12 14:19:50 -050048 struct list_head list;
49 int del;
Zheng Yan31840ae2008-09-23 13:14:14 -040050};
51
Chris Mason56bec292009-03-13 10:10:06 -040052static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
53 struct btrfs_root *root, u64 parent,
54 u64 root_objectid, u64 ref_generation,
55 u64 owner, struct btrfs_key *ins,
56 int ref_mod);
57static int update_reserved_extents(struct btrfs_root *root,
58 u64 bytenr, u64 num, int reserve);
Josef Bacikf3465ca2008-11-12 14:19:50 -050059static int update_block_group(struct btrfs_trans_handle *trans,
60 struct btrfs_root *root,
61 u64 bytenr, u64 num_bytes, int alloc,
62 int mark_free);
Chris Mason56bec292009-03-13 10:10:06 -040063static noinline int __btrfs_free_extent(struct btrfs_trans_handle *trans,
64 struct btrfs_root *root,
65 u64 bytenr, u64 num_bytes, u64 parent,
66 u64 root_objectid, u64 ref_generation,
67 u64 owner_objectid, int pin,
68 int ref_to_drop);
Yand548ee52008-01-03 13:56:30 -050069
Josef Bacik6a632092009-02-20 11:00:09 -050070static int do_chunk_alloc(struct btrfs_trans_handle *trans,
71 struct btrfs_root *extent_root, u64 alloc_bytes,
72 u64 flags, int force);
73
Josef Bacik0f9dd462008-09-23 13:14:11 -040074static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
75{
76 return (cache->flags & bits) == bits;
77}
78
79/*
80 * this adds the block group to the fs_info rb tree for the block group
81 * cache
82 */
Christoph Hellwigb2950862008-12-02 09:54:17 -050083static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
Josef Bacik0f9dd462008-09-23 13:14:11 -040084 struct btrfs_block_group_cache *block_group)
85{
86 struct rb_node **p;
87 struct rb_node *parent = NULL;
88 struct btrfs_block_group_cache *cache;
89
90 spin_lock(&info->block_group_cache_lock);
91 p = &info->block_group_cache_tree.rb_node;
92
93 while (*p) {
94 parent = *p;
95 cache = rb_entry(parent, struct btrfs_block_group_cache,
96 cache_node);
97 if (block_group->key.objectid < cache->key.objectid) {
98 p = &(*p)->rb_left;
99 } else if (block_group->key.objectid > cache->key.objectid) {
100 p = &(*p)->rb_right;
101 } else {
102 spin_unlock(&info->block_group_cache_lock);
103 return -EEXIST;
104 }
105 }
106
107 rb_link_node(&block_group->cache_node, parent, p);
108 rb_insert_color(&block_group->cache_node,
109 &info->block_group_cache_tree);
110 spin_unlock(&info->block_group_cache_lock);
111
112 return 0;
113}
114
115/*
116 * This will return the block group at or after bytenr if contains is 0, else
117 * it will return the block group that contains the bytenr
118 */
119static struct btrfs_block_group_cache *
120block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
121 int contains)
122{
123 struct btrfs_block_group_cache *cache, *ret = NULL;
124 struct rb_node *n;
125 u64 end, start;
126
127 spin_lock(&info->block_group_cache_lock);
128 n = info->block_group_cache_tree.rb_node;
129
130 while (n) {
131 cache = rb_entry(n, struct btrfs_block_group_cache,
132 cache_node);
133 end = cache->key.objectid + cache->key.offset - 1;
134 start = cache->key.objectid;
135
136 if (bytenr < start) {
137 if (!contains && (!ret || start < ret->key.objectid))
138 ret = cache;
139 n = n->rb_left;
140 } else if (bytenr > start) {
141 if (contains && bytenr <= end) {
142 ret = cache;
143 break;
144 }
145 n = n->rb_right;
146 } else {
147 ret = cache;
148 break;
149 }
150 }
Yan Zhengd2fb3432008-12-11 16:30:39 -0500151 if (ret)
152 atomic_inc(&ret->count);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400153 spin_unlock(&info->block_group_cache_lock);
154
155 return ret;
156}
157
158/*
159 * this is only called by cache_block_group, since we could have freed extents
160 * we need to check the pinned_extents for any extents that can't be used yet
161 * since their free space will be released as soon as the transaction commits.
162 */
163static int add_new_free_space(struct btrfs_block_group_cache *block_group,
164 struct btrfs_fs_info *info, u64 start, u64 end)
165{
166 u64 extent_start, extent_end, size;
167 int ret;
168
169 while (start < end) {
170 ret = find_first_extent_bit(&info->pinned_extents, start,
171 &extent_start, &extent_end,
172 EXTENT_DIRTY);
173 if (ret)
174 break;
175
176 if (extent_start == start) {
177 start = extent_end + 1;
178 } else if (extent_start > start && extent_start < end) {
179 size = extent_start - start;
Josef Bacikea6a4782008-11-20 12:16:16 -0500180 ret = btrfs_add_free_space(block_group, start,
181 size);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400182 BUG_ON(ret);
183 start = extent_end + 1;
184 } else {
185 break;
186 }
187 }
188
189 if (start < end) {
190 size = end - start;
Josef Bacikea6a4782008-11-20 12:16:16 -0500191 ret = btrfs_add_free_space(block_group, start, size);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400192 BUG_ON(ret);
193 }
194
195 return 0;
196}
197
Yan Zhenga512bbf2008-12-08 16:46:26 -0500198static int remove_sb_from_cache(struct btrfs_root *root,
199 struct btrfs_block_group_cache *cache)
200{
201 u64 bytenr;
202 u64 *logical;
203 int stripe_len;
204 int i, nr, ret;
205
206 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
207 bytenr = btrfs_sb_offset(i);
208 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
209 cache->key.objectid, bytenr, 0,
210 &logical, &nr, &stripe_len);
211 BUG_ON(ret);
212 while (nr--) {
213 btrfs_remove_free_space(cache, logical[nr],
214 stripe_len);
215 }
216 kfree(logical);
217 }
218 return 0;
219}
220
Chris Masone37c9e62007-05-09 20:13:14 -0400221static int cache_block_group(struct btrfs_root *root,
222 struct btrfs_block_group_cache *block_group)
223{
224 struct btrfs_path *path;
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400225 int ret = 0;
Chris Masone37c9e62007-05-09 20:13:14 -0400226 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400227 struct extent_buffer *leaf;
Chris Masone37c9e62007-05-09 20:13:14 -0400228 int slot;
Yan Zhenge4404d62008-12-12 10:03:26 -0500229 u64 last;
Chris Masone37c9e62007-05-09 20:13:14 -0400230
Chris Mason00f5c792007-11-30 10:09:33 -0500231 if (!block_group)
232 return 0;
233
Chris Masone37c9e62007-05-09 20:13:14 -0400234 root = root->fs_info->extent_root;
Chris Masone37c9e62007-05-09 20:13:14 -0400235
236 if (block_group->cached)
237 return 0;
Chris Masonf510cfe2007-10-15 16:14:48 -0400238
Chris Masone37c9e62007-05-09 20:13:14 -0400239 path = btrfs_alloc_path();
240 if (!path)
241 return -ENOMEM;
Yan7d7d6062007-09-14 16:15:28 -0400242
Chris Mason2cc58cf2007-08-27 16:49:44 -0400243 path->reada = 2;
Chris Mason5cd57b22008-06-25 16:01:30 -0400244 /*
245 * we get into deadlocks with paths held by callers of this function.
246 * since the alloc_mutex is protecting things right now, just
247 * skip the locking here
248 */
249 path->skip_locking = 1;
Yan Zhenge4404d62008-12-12 10:03:26 -0500250 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
251 key.objectid = last;
Chris Masone37c9e62007-05-09 20:13:14 -0400252 key.offset = 0;
253 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
254 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
255 if (ret < 0)
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400256 goto err;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500257
Chris Masond3977122009-01-05 21:25:51 -0500258 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400259 leaf = path->nodes[0];
Chris Masone37c9e62007-05-09 20:13:14 -0400260 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -0400261 if (slot >= btrfs_header_nritems(leaf)) {
Chris Masone37c9e62007-05-09 20:13:14 -0400262 ret = btrfs_next_leaf(root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400263 if (ret < 0)
264 goto err;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400265 if (ret == 0)
Chris Masone37c9e62007-05-09 20:13:14 -0400266 continue;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400267 else
Chris Masone37c9e62007-05-09 20:13:14 -0400268 break;
Chris Masone37c9e62007-05-09 20:13:14 -0400269 }
Chris Mason5f39d392007-10-15 16:14:19 -0400270 btrfs_item_key_to_cpu(leaf, &key, slot);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400271 if (key.objectid < block_group->key.objectid)
Yan7d7d6062007-09-14 16:15:28 -0400272 goto next;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400273
Chris Masone37c9e62007-05-09 20:13:14 -0400274 if (key.objectid >= block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -0400275 block_group->key.offset)
Yan7d7d6062007-09-14 16:15:28 -0400276 break;
Yan7d7d6062007-09-14 16:15:28 -0400277
278 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
Josef Bacik0f9dd462008-09-23 13:14:11 -0400279 add_new_free_space(block_group, root->fs_info, last,
280 key.objectid);
281
Yan7d7d6062007-09-14 16:15:28 -0400282 last = key.objectid + key.offset;
Chris Masone37c9e62007-05-09 20:13:14 -0400283 }
Yan7d7d6062007-09-14 16:15:28 -0400284next:
Chris Masone37c9e62007-05-09 20:13:14 -0400285 path->slots[0]++;
286 }
287
Josef Bacik0f9dd462008-09-23 13:14:11 -0400288 add_new_free_space(block_group, root->fs_info, last,
289 block_group->key.objectid +
290 block_group->key.offset);
291
Chris Masone37c9e62007-05-09 20:13:14 -0400292 block_group->cached = 1;
Josef Bacik70cb0742009-04-03 10:14:19 -0400293 remove_sb_from_cache(root, block_group);
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400294 ret = 0;
Chris Mason54aa1f42007-06-22 14:16:25 -0400295err:
Chris Masone37c9e62007-05-09 20:13:14 -0400296 btrfs_free_path(path);
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400297 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -0400298}
299
Josef Bacik0f9dd462008-09-23 13:14:11 -0400300/*
301 * return the block group that starts at or after bytenr
302 */
Chris Masond3977122009-01-05 21:25:51 -0500303static struct btrfs_block_group_cache *
304btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
Chris Mason0ef3e662008-05-24 14:04:53 -0400305{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400306 struct btrfs_block_group_cache *cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400307
Josef Bacik0f9dd462008-09-23 13:14:11 -0400308 cache = block_group_cache_tree_search(info, bytenr, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -0400309
Josef Bacik0f9dd462008-09-23 13:14:11 -0400310 return cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400311}
312
Josef Bacik0f9dd462008-09-23 13:14:11 -0400313/*
314 * return the block group that contains teh given bytenr
315 */
Chris Masond3977122009-01-05 21:25:51 -0500316struct btrfs_block_group_cache *btrfs_lookup_block_group(
317 struct btrfs_fs_info *info,
318 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400319{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400320 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -0400321
Josef Bacik0f9dd462008-09-23 13:14:11 -0400322 cache = block_group_cache_tree_search(info, bytenr, 1);
Chris Mason96b51792007-10-15 16:15:19 -0400323
Josef Bacik0f9dd462008-09-23 13:14:11 -0400324 return cache;
Chris Masonbe744172007-05-06 10:15:01 -0400325}
Chris Mason0b86a832008-03-24 15:01:56 -0400326
Yan Zhengd2fb3432008-12-11 16:30:39 -0500327static inline void put_block_group(struct btrfs_block_group_cache *cache)
328{
329 if (atomic_dec_and_test(&cache->count))
330 kfree(cache);
331}
332
Josef Bacik0f9dd462008-09-23 13:14:11 -0400333static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
334 u64 flags)
Chris Mason6324fbf2008-03-24 15:01:59 -0400335{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400336 struct list_head *head = &info->space_info;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400337 struct btrfs_space_info *found;
Chris Mason4184ea72009-03-10 12:39:20 -0400338
339 rcu_read_lock();
340 list_for_each_entry_rcu(found, head, list) {
341 if (found->flags == flags) {
342 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400343 return found;
Chris Mason4184ea72009-03-10 12:39:20 -0400344 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400345 }
Chris Mason4184ea72009-03-10 12:39:20 -0400346 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400347 return NULL;
Chris Mason6324fbf2008-03-24 15:01:59 -0400348}
349
Chris Mason4184ea72009-03-10 12:39:20 -0400350/*
351 * after adding space to the filesystem, we need to clear the full flags
352 * on all the space infos.
353 */
354void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
355{
356 struct list_head *head = &info->space_info;
357 struct btrfs_space_info *found;
358
359 rcu_read_lock();
360 list_for_each_entry_rcu(found, head, list)
361 found->full = 0;
362 rcu_read_unlock();
363}
364
Josef Bacik80eb2342008-10-29 14:49:05 -0400365static u64 div_factor(u64 num, int factor)
366{
367 if (factor == 10)
368 return num;
369 num *= factor;
370 do_div(num, 10);
371 return num;
372}
373
Yan Zhengd2fb3432008-12-11 16:30:39 -0500374u64 btrfs_find_block_group(struct btrfs_root *root,
375 u64 search_start, u64 search_hint, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400376{
Chris Mason96b51792007-10-15 16:15:19 -0400377 struct btrfs_block_group_cache *cache;
Chris Masoncd1bc462007-04-27 10:08:34 -0400378 u64 used;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500379 u64 last = max(search_hint, search_start);
380 u64 group_start = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400381 int full_search = 0;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500382 int factor = 9;
Chris Mason0ef3e662008-05-24 14:04:53 -0400383 int wrapped = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400384again:
Zheng Yane8569812008-09-26 10:05:48 -0400385 while (1) {
386 cache = btrfs_lookup_first_block_group(root->fs_info, last);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400387 if (!cache)
Chris Masoncd1bc462007-04-27 10:08:34 -0400388 break;
Chris Mason96b51792007-10-15 16:15:19 -0400389
Chris Masonc286ac42008-07-22 23:06:41 -0400390 spin_lock(&cache->lock);
Chris Mason96b51792007-10-15 16:15:19 -0400391 last = cache->key.objectid + cache->key.offset;
392 used = btrfs_block_group_used(&cache->item);
393
Yan Zhengd2fb3432008-12-11 16:30:39 -0500394 if ((full_search || !cache->ro) &&
395 block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) {
Zheng Yane8569812008-09-26 10:05:48 -0400396 if (used + cache->pinned + cache->reserved <
Yan Zhengd2fb3432008-12-11 16:30:39 -0500397 div_factor(cache->key.offset, factor)) {
398 group_start = cache->key.objectid;
Chris Masonc286ac42008-07-22 23:06:41 -0400399 spin_unlock(&cache->lock);
Yan Zhengd2fb3432008-12-11 16:30:39 -0500400 put_block_group(cache);
Chris Mason8790d502008-04-03 16:29:03 -0400401 goto found;
402 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400403 }
Chris Masonc286ac42008-07-22 23:06:41 -0400404 spin_unlock(&cache->lock);
Yan Zhengd2fb3432008-12-11 16:30:39 -0500405 put_block_group(cache);
Chris Masonde428b62007-05-18 13:28:27 -0400406 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400407 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400408 if (!wrapped) {
409 last = search_start;
410 wrapped = 1;
411 goto again;
412 }
413 if (!full_search && factor < 10) {
Chris Masonbe744172007-05-06 10:15:01 -0400414 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400415 full_search = 1;
Chris Mason0ef3e662008-05-24 14:04:53 -0400416 factor = 10;
Chris Mason31f3c992007-04-30 15:25:45 -0400417 goto again;
418 }
Chris Masonbe744172007-05-06 10:15:01 -0400419found:
Yan Zhengd2fb3432008-12-11 16:30:39 -0500420 return group_start;
Chris Mason925baed2008-06-25 16:01:30 -0400421}
Josef Bacik0f9dd462008-09-23 13:14:11 -0400422
Chris Masone02119d2008-09-05 16:13:11 -0400423/* simple helper to search for an existing extent at a given offset */
Zheng Yan31840ae2008-09-23 13:14:14 -0400424int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
Chris Masone02119d2008-09-05 16:13:11 -0400425{
426 int ret;
427 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400428 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -0400429
Zheng Yan31840ae2008-09-23 13:14:14 -0400430 path = btrfs_alloc_path();
431 BUG_ON(!path);
Chris Masone02119d2008-09-05 16:13:11 -0400432 key.objectid = start;
433 key.offset = len;
434 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
435 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
436 0, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400437 btrfs_free_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500438 return ret;
439}
440
Chris Masond8d5f3e2007-12-11 12:42:00 -0500441/*
442 * Back reference rules. Back refs have three main goals:
443 *
444 * 1) differentiate between all holders of references to an extent so that
445 * when a reference is dropped we can make sure it was a valid reference
446 * before freeing the extent.
447 *
448 * 2) Provide enough information to quickly find the holders of an extent
449 * if we notice a given block is corrupted or bad.
450 *
451 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
452 * maintenance. This is actually the same as #2, but with a slightly
453 * different use case.
454 *
455 * File extents can be referenced by:
456 *
457 * - multiple snapshots, subvolumes, or different generations in one subvol
Zheng Yan31840ae2008-09-23 13:14:14 -0400458 * - different files inside a single subvolume
Chris Masond8d5f3e2007-12-11 12:42:00 -0500459 * - different offsets inside a file (bookend extents in file.c)
460 *
461 * The extent ref structure has fields for:
462 *
463 * - Objectid of the subvolume root
464 * - Generation number of the tree holding the reference
465 * - objectid of the file holding the reference
Zheng Yan31840ae2008-09-23 13:14:14 -0400466 * - number of references holding by parent node (alway 1 for tree blocks)
467 *
468 * Btree leaf may hold multiple references to a file extent. In most cases,
469 * these references are from same file and the corresponding offsets inside
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400470 * the file are close together.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500471 *
472 * When a file extent is allocated the fields are filled in:
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400473 * (root_key.objectid, trans->transid, inode objectid, 1)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500474 *
475 * When a leaf is cow'd new references are added for every file extent found
Zheng Yan31840ae2008-09-23 13:14:14 -0400476 * in the leaf. It looks similar to the create case, but trans->transid will
477 * be different when the block is cow'd.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500478 *
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400479 * (root_key.objectid, trans->transid, inode objectid,
Zheng Yan31840ae2008-09-23 13:14:14 -0400480 * number of references in the leaf)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500481 *
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400482 * When a file extent is removed either during snapshot deletion or
483 * file truncation, we find the corresponding back reference and check
484 * the following fields:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500485 *
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400486 * (btrfs_header_owner(leaf), btrfs_header_generation(leaf),
487 * inode objectid)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500488 *
489 * Btree extents can be referenced by:
490 *
491 * - Different subvolumes
492 * - Different generations of the same subvolume
493 *
Chris Masond8d5f3e2007-12-11 12:42:00 -0500494 * When a tree block is created, back references are inserted:
495 *
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400496 * (root->root_key.objectid, trans->transid, level, 1)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500497 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400498 * When a tree block is cow'd, new back references are added for all the
499 * blocks it points to. If the tree block isn't in reference counted root,
500 * the old back references are removed. These new back references are of
501 * the form (trans->transid will have increased since creation):
Chris Masond8d5f3e2007-12-11 12:42:00 -0500502 *
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400503 * (root->root_key.objectid, trans->transid, level, 1)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500504 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400505 * When a backref is in deleting, the following fields are checked:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500506 *
507 * if backref was for a tree root:
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400508 * (btrfs_header_owner(itself), btrfs_header_generation(itself), level)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500509 * else
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400510 * (btrfs_header_owner(parent), btrfs_header_generation(parent), level)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500511 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400512 * Back Reference Key composing:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500513 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400514 * The key objectid corresponds to the first byte in the extent, the key
515 * type is set to BTRFS_EXTENT_REF_KEY, and the key offset is the first
516 * byte of parent extent. If a extent is tree root, the key offset is set
517 * to the key objectid.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500518 */
Zheng Yan31840ae2008-09-23 13:14:14 -0400519
Chris Masond3977122009-01-05 21:25:51 -0500520static noinline int lookup_extent_backref(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -0400521 struct btrfs_root *root,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400522 struct btrfs_path *path,
523 u64 bytenr, u64 parent,
524 u64 ref_root, u64 ref_generation,
525 u64 owner_objectid, int del)
Chris Mason74493f72007-12-11 09:25:06 -0500526{
Chris Mason74493f72007-12-11 09:25:06 -0500527 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400528 struct btrfs_extent_ref *ref;
529 struct extent_buffer *leaf;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400530 u64 ref_objectid;
Chris Mason74493f72007-12-11 09:25:06 -0500531 int ret;
532
Chris Mason74493f72007-12-11 09:25:06 -0500533 key.objectid = bytenr;
534 key.type = BTRFS_EXTENT_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -0400535 key.offset = parent;
Chris Mason74493f72007-12-11 09:25:06 -0500536
Zheng Yan31840ae2008-09-23 13:14:14 -0400537 ret = btrfs_search_slot(trans, root, &key, path, del ? -1 : 0, 1);
538 if (ret < 0)
Chris Mason7bb86312007-12-11 09:25:06 -0500539 goto out;
Zheng Yan31840ae2008-09-23 13:14:14 -0400540 if (ret > 0) {
541 ret = -ENOENT;
542 goto out;
543 }
544
545 leaf = path->nodes[0];
546 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400547 ref_objectid = btrfs_ref_objectid(leaf, ref);
Zheng Yan31840ae2008-09-23 13:14:14 -0400548 if (btrfs_ref_root(leaf, ref) != ref_root ||
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400549 btrfs_ref_generation(leaf, ref) != ref_generation ||
550 (ref_objectid != owner_objectid &&
551 ref_objectid != BTRFS_MULTIPLE_OBJECTIDS)) {
Zheng Yan31840ae2008-09-23 13:14:14 -0400552 ret = -EIO;
553 WARN_ON(1);
554 goto out;
555 }
556 ret = 0;
557out:
558 return ret;
559}
560
Chris Masond3977122009-01-05 21:25:51 -0500561static noinline int insert_extent_backref(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -0400562 struct btrfs_root *root,
563 struct btrfs_path *path,
564 u64 bytenr, u64 parent,
565 u64 ref_root, u64 ref_generation,
Chris Mason56bec292009-03-13 10:10:06 -0400566 u64 owner_objectid,
567 int refs_to_add)
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);
Chris Mason56bec292009-03-13 10:10:06 -0400587 btrfs_set_ref_num_refs(leaf, ref, refs_to_add);
Zheng Yan31840ae2008-09-23 13:14:14 -0400588 } else if (ret == -EEXIST) {
589 u64 existing_owner;
Chris Mason56bec292009-03-13 10:10:06 -0400590
Zheng Yan31840ae2008-09-23 13:14:14 -0400591 BUG_ON(owner_objectid < BTRFS_FIRST_FREE_OBJECTID);
592 leaf = path->nodes[0];
593 ref = btrfs_item_ptr(leaf, path->slots[0],
594 struct btrfs_extent_ref);
595 if (btrfs_ref_root(leaf, ref) != ref_root ||
596 btrfs_ref_generation(leaf, ref) != ref_generation) {
597 ret = -EIO;
598 WARN_ON(1);
599 goto out;
600 }
601
602 num_refs = btrfs_ref_num_refs(leaf, ref);
603 BUG_ON(num_refs == 0);
Chris Mason56bec292009-03-13 10:10:06 -0400604 btrfs_set_ref_num_refs(leaf, ref, num_refs + refs_to_add);
Zheng Yan31840ae2008-09-23 13:14:14 -0400605
606 existing_owner = btrfs_ref_objectid(leaf, ref);
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400607 if (existing_owner != owner_objectid &&
608 existing_owner != BTRFS_MULTIPLE_OBJECTIDS) {
Zheng Yan31840ae2008-09-23 13:14:14 -0400609 btrfs_set_ref_objectid(leaf, ref,
610 BTRFS_MULTIPLE_OBJECTIDS);
Zheng Yan31840ae2008-09-23 13:14:14 -0400611 }
612 ret = 0;
613 } else {
614 goto out;
615 }
Chris Masonb9473432009-03-13 11:00:37 -0400616 btrfs_unlock_up_safe(path, 1);
Chris Mason7bb86312007-12-11 09:25:06 -0500617 btrfs_mark_buffer_dirty(path->nodes[0]);
618out:
619 btrfs_release_path(root, path);
620 return ret;
Chris Mason74493f72007-12-11 09:25:06 -0500621}
622
Chris Masond3977122009-01-05 21:25:51 -0500623static noinline int remove_extent_backref(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -0400624 struct btrfs_root *root,
Chris Mason56bec292009-03-13 10:10:06 -0400625 struct btrfs_path *path,
626 int refs_to_drop)
Zheng Yan31840ae2008-09-23 13:14:14 -0400627{
628 struct extent_buffer *leaf;
629 struct btrfs_extent_ref *ref;
630 u32 num_refs;
631 int ret = 0;
632
633 leaf = path->nodes[0];
634 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
635 num_refs = btrfs_ref_num_refs(leaf, ref);
Chris Mason56bec292009-03-13 10:10:06 -0400636 BUG_ON(num_refs < refs_to_drop);
637 num_refs -= refs_to_drop;
Zheng Yan31840ae2008-09-23 13:14:14 -0400638 if (num_refs == 0) {
639 ret = btrfs_del_item(trans, root, path);
640 } else {
641 btrfs_set_ref_num_refs(leaf, ref, num_refs);
642 btrfs_mark_buffer_dirty(leaf);
643 }
644 btrfs_release_path(root, path);
645 return ret;
646}
647
Chris Mason4b4e25f2008-11-20 10:22:27 -0500648#ifdef BIO_RW_DISCARD
Chris Mason15916de2008-11-19 21:17:22 -0500649static void btrfs_issue_discard(struct block_device *bdev,
650 u64 start, u64 len)
651{
Chris Mason15916de2008-11-19 21:17:22 -0500652 blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_KERNEL);
Chris Mason15916de2008-11-19 21:17:22 -0500653}
Chris Mason4b4e25f2008-11-20 10:22:27 -0500654#endif
Chris Mason15916de2008-11-19 21:17:22 -0500655
Liu Hui1f3c79a2009-01-05 15:57:51 -0500656static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
657 u64 num_bytes)
658{
659#ifdef BIO_RW_DISCARD
660 int ret;
661 u64 map_length = num_bytes;
662 struct btrfs_multi_bio *multi = NULL;
663
664 /* Tell the block device(s) that the sectors can be discarded */
665 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
666 bytenr, &map_length, &multi, 0);
667 if (!ret) {
668 struct btrfs_bio_stripe *stripe = multi->stripes;
669 int i;
670
671 if (map_length > num_bytes)
672 map_length = num_bytes;
673
674 for (i = 0; i < multi->num_stripes; i++, stripe++) {
675 btrfs_issue_discard(stripe->dev->bdev,
676 stripe->physical,
677 map_length);
678 }
679 kfree(multi);
680 }
681
682 return ret;
683#else
684 return 0;
685#endif
686}
687
Zheng Yan31840ae2008-09-23 13:14:14 -0400688static int __btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
689 struct btrfs_root *root, u64 bytenr,
Chris Mason56bec292009-03-13 10:10:06 -0400690 u64 num_bytes,
Zheng Yan31840ae2008-09-23 13:14:14 -0400691 u64 orig_parent, u64 parent,
692 u64 orig_root, u64 ref_root,
693 u64 orig_generation, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400694 u64 owner_objectid)
Zheng Yan31840ae2008-09-23 13:14:14 -0400695{
696 int ret;
Chris Mason56bec292009-03-13 10:10:06 -0400697 int pin = owner_objectid < BTRFS_FIRST_FREE_OBJECTID;
Zheng Yan31840ae2008-09-23 13:14:14 -0400698
Chris Mason56bec292009-03-13 10:10:06 -0400699 ret = btrfs_update_delayed_ref(trans, bytenr, num_bytes,
700 orig_parent, parent, orig_root,
701 ref_root, orig_generation,
702 ref_generation, owner_objectid, pin);
Zheng Yan31840ae2008-09-23 13:14:14 -0400703 BUG_ON(ret);
Zheng Yan31840ae2008-09-23 13:14:14 -0400704 return ret;
705}
706
707int btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
708 struct btrfs_root *root, u64 bytenr,
Chris Mason56bec292009-03-13 10:10:06 -0400709 u64 num_bytes, u64 orig_parent, u64 parent,
Zheng Yan31840ae2008-09-23 13:14:14 -0400710 u64 ref_root, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400711 u64 owner_objectid)
Zheng Yan31840ae2008-09-23 13:14:14 -0400712{
713 int ret;
714 if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
715 owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
716 return 0;
Chris Mason56bec292009-03-13 10:10:06 -0400717
718 ret = __btrfs_update_extent_ref(trans, root, bytenr, num_bytes,
719 orig_parent, parent, ref_root,
720 ref_root, ref_generation,
721 ref_generation, owner_objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -0400722 return ret;
723}
Chris Mason925baed2008-06-25 16:01:30 -0400724static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -0400725 struct btrfs_root *root, u64 bytenr,
Chris Mason56bec292009-03-13 10:10:06 -0400726 u64 num_bytes,
Zheng Yan31840ae2008-09-23 13:14:14 -0400727 u64 orig_parent, u64 parent,
728 u64 orig_root, u64 ref_root,
729 u64 orig_generation, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400730 u64 owner_objectid)
Chris Mason02217ed2007-03-02 16:08:05 -0500731{
Chris Mason56bec292009-03-13 10:10:06 -0400732 int ret;
733
734 ret = btrfs_add_delayed_ref(trans, bytenr, num_bytes, parent, ref_root,
735 ref_generation, owner_objectid,
736 BTRFS_ADD_DELAYED_REF, 0);
737 BUG_ON(ret);
738 return ret;
739}
740
741static noinline_for_stack int add_extent_ref(struct btrfs_trans_handle *trans,
742 struct btrfs_root *root, u64 bytenr,
743 u64 num_bytes, u64 parent, u64 ref_root,
744 u64 ref_generation, u64 owner_objectid,
745 int refs_to_add)
746{
Chris Mason5caf2a02007-04-02 11:20:42 -0400747 struct btrfs_path *path;
Chris Mason02217ed2007-03-02 16:08:05 -0500748 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400749 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400750 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -0400751 struct btrfs_extent_item *item;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400752 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500753
Chris Mason5caf2a02007-04-02 11:20:42 -0400754 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400755 if (!path)
756 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -0400757
Chris Mason3c12ac72008-04-21 12:01:38 -0400758 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -0400759 path->leave_spinning = 1;
Chris Masondb945352007-10-15 16:15:53 -0400760 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -0400761 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason56bec292009-03-13 10:10:06 -0400762 key.offset = num_bytes;
Zheng Yan31840ae2008-09-23 13:14:14 -0400763
Chris Mason56bec292009-03-13 10:10:06 -0400764 /* first find the extent item and update its reference count */
765 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
766 path, 0, 1);
Chris Masonb9473432009-03-13 11:00:37 -0400767 if (ret < 0) {
768 btrfs_set_path_blocking(path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400769 return ret;
Chris Masonb9473432009-03-13 11:00:37 -0400770 }
Zheng Yan31840ae2008-09-23 13:14:14 -0400771
Chris Mason56bec292009-03-13 10:10:06 -0400772 if (ret > 0) {
773 WARN_ON(1);
774 btrfs_free_path(path);
775 return -EIO;
776 }
Chris Mason5f39d392007-10-15 16:14:19 -0400777 l = path->nodes[0];
Zheng Yan31840ae2008-09-23 13:14:14 -0400778
779 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
Chris Mason771ed682008-11-06 22:02:51 -0500780 if (key.objectid != bytenr) {
781 btrfs_print_leaf(root->fs_info->extent_root, path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -0500782 printk(KERN_ERR "btrfs wanted %llu found %llu\n",
783 (unsigned long long)bytenr,
784 (unsigned long long)key.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500785 BUG();
786 }
Zheng Yan31840ae2008-09-23 13:14:14 -0400787 BUG_ON(key.type != BTRFS_EXTENT_ITEM_KEY);
788
Chris Mason5caf2a02007-04-02 11:20:42 -0400789 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason56bec292009-03-13 10:10:06 -0400790
Chris Mason5f39d392007-10-15 16:14:19 -0400791 refs = btrfs_extent_refs(l, item);
Chris Mason56bec292009-03-13 10:10:06 -0400792 btrfs_set_extent_refs(l, item, refs + refs_to_add);
Chris Masonb9473432009-03-13 11:00:37 -0400793 btrfs_unlock_up_safe(path, 1);
794
Chris Mason5caf2a02007-04-02 11:20:42 -0400795 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masona28ec192007-03-06 20:08:01 -0500796
Chris Mason5caf2a02007-04-02 11:20:42 -0400797 btrfs_release_path(root->fs_info->extent_root, path);
Chris Mason7bb86312007-12-11 09:25:06 -0500798
Chris Mason3c12ac72008-04-21 12:01:38 -0400799 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -0400800 path->leave_spinning = 1;
801
Chris Mason56bec292009-03-13 10:10:06 -0400802 /* now insert the actual backref */
Zheng Yan31840ae2008-09-23 13:14:14 -0400803 ret = insert_extent_backref(trans, root->fs_info->extent_root,
804 path, bytenr, parent,
805 ref_root, ref_generation,
Chris Mason56bec292009-03-13 10:10:06 -0400806 owner_objectid, refs_to_add);
Chris Mason7bb86312007-12-11 09:25:06 -0500807 BUG_ON(ret);
Chris Mason74493f72007-12-11 09:25:06 -0500808 btrfs_free_path(path);
Chris Mason02217ed2007-03-02 16:08:05 -0500809 return 0;
810}
811
Chris Mason925baed2008-06-25 16:01:30 -0400812int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -0400813 struct btrfs_root *root,
814 u64 bytenr, u64 num_bytes, u64 parent,
815 u64 ref_root, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400816 u64 owner_objectid)
Chris Mason925baed2008-06-25 16:01:30 -0400817{
818 int ret;
Zheng Yan31840ae2008-09-23 13:14:14 -0400819 if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
820 owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
821 return 0;
Chris Mason56bec292009-03-13 10:10:06 -0400822
823 ret = __btrfs_inc_extent_ref(trans, root, bytenr, num_bytes, 0, parent,
Zheng Yan31840ae2008-09-23 13:14:14 -0400824 0, ref_root, 0, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400825 owner_objectid);
Chris Mason925baed2008-06-25 16:01:30 -0400826 return ret;
827}
828
Chris Mason56bec292009-03-13 10:10:06 -0400829static int drop_delayed_ref(struct btrfs_trans_handle *trans,
830 struct btrfs_root *root,
831 struct btrfs_delayed_ref_node *node)
Chris Masone9d0b132007-08-10 14:06:19 -0400832{
Chris Mason56bec292009-03-13 10:10:06 -0400833 int ret = 0;
834 struct btrfs_delayed_ref *ref = btrfs_delayed_node_to_ref(node);
835
836 BUG_ON(node->ref_mod == 0);
837 ret = __btrfs_free_extent(trans, root, node->bytenr, node->num_bytes,
838 node->parent, ref->root, ref->generation,
839 ref->owner_objectid, ref->pin, node->ref_mod);
840
841 return ret;
842}
843
844/* helper function to actually process a single delayed ref entry */
845static noinline int run_one_delayed_ref(struct btrfs_trans_handle *trans,
846 struct btrfs_root *root,
847 struct btrfs_delayed_ref_node *node,
848 int insert_reserved)
849{
Josef Bacikeb099672009-02-12 09:27:38 -0500850 int ret;
Chris Mason56bec292009-03-13 10:10:06 -0400851 struct btrfs_delayed_ref *ref;
Josef Bacikeb099672009-02-12 09:27:38 -0500852
Chris Mason56bec292009-03-13 10:10:06 -0400853 if (node->parent == (u64)-1) {
854 struct btrfs_delayed_ref_head *head;
855 /*
856 * we've hit the end of the chain and we were supposed
857 * to insert this extent into the tree. But, it got
858 * deleted before we ever needed to insert it, so all
859 * we have to do is clean up the accounting
860 */
861 if (insert_reserved) {
862 update_reserved_extents(root, node->bytenr,
863 node->num_bytes, 0);
864 }
865 head = btrfs_delayed_node_to_head(node);
866 mutex_unlock(&head->mutex);
867 return 0;
868 }
Josef Bacikeb099672009-02-12 09:27:38 -0500869
Chris Mason56bec292009-03-13 10:10:06 -0400870 ref = btrfs_delayed_node_to_ref(node);
871 if (ref->action == BTRFS_ADD_DELAYED_REF) {
872 if (insert_reserved) {
873 struct btrfs_key ins;
874
875 ins.objectid = node->bytenr;
876 ins.offset = node->num_bytes;
877 ins.type = BTRFS_EXTENT_ITEM_KEY;
878
879 /* record the full extent allocation */
880 ret = __btrfs_alloc_reserved_extent(trans, root,
881 node->parent, ref->root,
882 ref->generation, ref->owner_objectid,
883 &ins, node->ref_mod);
884 update_reserved_extents(root, node->bytenr,
885 node->num_bytes, 0);
886 } else {
887 /* just add one backref */
888 ret = add_extent_ref(trans, root, node->bytenr,
889 node->num_bytes,
890 node->parent, ref->root, ref->generation,
891 ref->owner_objectid, node->ref_mod);
892 }
893 BUG_ON(ret);
894 } else if (ref->action == BTRFS_DROP_DELAYED_REF) {
895 WARN_ON(insert_reserved);
896 ret = drop_delayed_ref(trans, root, node);
Josef Bacikeb099672009-02-12 09:27:38 -0500897 }
Chris Masone9d0b132007-08-10 14:06:19 -0400898 return 0;
899}
900
Chris Mason56bec292009-03-13 10:10:06 -0400901static noinline struct btrfs_delayed_ref_node *
902select_delayed_ref(struct btrfs_delayed_ref_head *head)
Chris Masona28ec192007-03-06 20:08:01 -0500903{
Chris Mason56bec292009-03-13 10:10:06 -0400904 struct rb_node *node;
905 struct btrfs_delayed_ref_node *ref;
906 int action = BTRFS_ADD_DELAYED_REF;
907again:
908 /*
909 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
910 * this prevents ref count from going down to zero when
911 * there still are pending delayed ref.
912 */
913 node = rb_prev(&head->node.rb_node);
914 while (1) {
915 if (!node)
916 break;
917 ref = rb_entry(node, struct btrfs_delayed_ref_node,
918 rb_node);
919 if (ref->bytenr != head->node.bytenr)
920 break;
921 if (btrfs_delayed_node_to_ref(ref)->action == action)
922 return ref;
923 node = rb_prev(node);
Chris Mason5f39d392007-10-15 16:14:19 -0400924 }
Chris Mason56bec292009-03-13 10:10:06 -0400925 if (action == BTRFS_ADD_DELAYED_REF) {
926 action = BTRFS_DROP_DELAYED_REF;
927 goto again;
928 }
929 return NULL;
930}
931
Chris Masonc3e69d52009-03-13 10:17:05 -0400932static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
933 struct btrfs_root *root,
934 struct list_head *cluster)
Chris Mason56bec292009-03-13 10:10:06 -0400935{
Chris Mason56bec292009-03-13 10:10:06 -0400936 struct btrfs_delayed_ref_root *delayed_refs;
937 struct btrfs_delayed_ref_node *ref;
938 struct btrfs_delayed_ref_head *locked_ref = NULL;
939 int ret;
Chris Masonc3e69d52009-03-13 10:17:05 -0400940 int count = 0;
Chris Mason56bec292009-03-13 10:10:06 -0400941 int must_insert_reserved = 0;
Chris Mason56bec292009-03-13 10:10:06 -0400942
943 delayed_refs = &trans->transaction->delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -0400944 while (1) {
945 if (!locked_ref) {
Chris Masonc3e69d52009-03-13 10:17:05 -0400946 /* pick a new head ref from the cluster list */
947 if (list_empty(cluster))
Chris Mason56bec292009-03-13 10:10:06 -0400948 break;
Chris Mason56bec292009-03-13 10:10:06 -0400949
Chris Masonc3e69d52009-03-13 10:17:05 -0400950 locked_ref = list_entry(cluster->next,
951 struct btrfs_delayed_ref_head, cluster);
952
953 /* grab the lock that says we are going to process
954 * all the refs for this head */
955 ret = btrfs_delayed_ref_lock(trans, locked_ref);
956
957 /*
958 * we may have dropped the spin lock to get the head
959 * mutex lock, and that might have given someone else
960 * time to free the head. If that's true, it has been
961 * removed from our list and we can move on.
962 */
963 if (ret == -EAGAIN) {
964 locked_ref = NULL;
965 count++;
966 continue;
Chris Mason56bec292009-03-13 10:10:06 -0400967 }
968 }
969
970 /*
971 * record the must insert reserved flag before we
972 * drop the spin lock.
973 */
974 must_insert_reserved = locked_ref->must_insert_reserved;
975 locked_ref->must_insert_reserved = 0;
976
977 /*
978 * locked_ref is the head node, so we have to go one
979 * node back for any delayed ref updates
980 */
Chris Mason56bec292009-03-13 10:10:06 -0400981 ref = select_delayed_ref(locked_ref);
982 if (!ref) {
983 /* All delayed refs have been processed, Go ahead
984 * and send the head node to run_one_delayed_ref,
985 * so that any accounting fixes can happen
986 */
987 ref = &locked_ref->node;
Chris Masonc3e69d52009-03-13 10:17:05 -0400988 list_del_init(&locked_ref->cluster);
Chris Mason56bec292009-03-13 10:10:06 -0400989 locked_ref = NULL;
990 }
991
992 ref->in_tree = 0;
993 rb_erase(&ref->rb_node, &delayed_refs->root);
994 delayed_refs->num_entries--;
995 spin_unlock(&delayed_refs->lock);
996
997 ret = run_one_delayed_ref(trans, root, ref,
998 must_insert_reserved);
999 BUG_ON(ret);
1000 btrfs_put_delayed_ref(ref);
1001
Chris Masonc3e69d52009-03-13 10:17:05 -04001002 count++;
Chris Mason1887be62009-03-13 10:11:24 -04001003 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04001004 spin_lock(&delayed_refs->lock);
1005 }
Chris Masonc3e69d52009-03-13 10:17:05 -04001006 return count;
1007}
1008
1009/*
1010 * this starts processing the delayed reference count updates and
1011 * extent insertions we have queued up so far. count can be
1012 * 0, which means to process everything in the tree at the start
1013 * of the run (but not newly added entries), or it can be some target
1014 * number you'd like to process.
1015 */
1016int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
1017 struct btrfs_root *root, unsigned long count)
1018{
1019 struct rb_node *node;
1020 struct btrfs_delayed_ref_root *delayed_refs;
1021 struct btrfs_delayed_ref_node *ref;
1022 struct list_head cluster;
1023 int ret;
1024 int run_all = count == (unsigned long)-1;
1025 int run_most = 0;
1026
1027 if (root == root->fs_info->extent_root)
1028 root = root->fs_info->tree_root;
1029
1030 delayed_refs = &trans->transaction->delayed_refs;
1031 INIT_LIST_HEAD(&cluster);
1032again:
1033 spin_lock(&delayed_refs->lock);
1034 if (count == 0) {
1035 count = delayed_refs->num_entries * 2;
1036 run_most = 1;
1037 }
1038 while (1) {
1039 if (!(run_all || run_most) &&
1040 delayed_refs->num_heads_ready < 64)
1041 break;
1042
1043 /*
1044 * go find something we can process in the rbtree. We start at
1045 * the beginning of the tree, and then build a cluster
1046 * of refs to process starting at the first one we are able to
1047 * lock
1048 */
1049 ret = btrfs_find_ref_cluster(trans, &cluster,
1050 delayed_refs->run_delayed_start);
1051 if (ret)
1052 break;
1053
1054 ret = run_clustered_refs(trans, root, &cluster);
1055 BUG_ON(ret < 0);
1056
1057 count -= min_t(unsigned long, ret, count);
1058
1059 if (count == 0)
1060 break;
1061 }
1062
Chris Mason56bec292009-03-13 10:10:06 -04001063 if (run_all) {
Chris Mason56bec292009-03-13 10:10:06 -04001064 node = rb_first(&delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04001065 if (!node)
Chris Mason56bec292009-03-13 10:10:06 -04001066 goto out;
Chris Masonc3e69d52009-03-13 10:17:05 -04001067 count = (unsigned long)-1;
Chris Mason56bec292009-03-13 10:10:06 -04001068
1069 while (node) {
1070 ref = rb_entry(node, struct btrfs_delayed_ref_node,
1071 rb_node);
1072 if (btrfs_delayed_ref_is_head(ref)) {
1073 struct btrfs_delayed_ref_head *head;
1074
1075 head = btrfs_delayed_node_to_head(ref);
1076 atomic_inc(&ref->refs);
1077
1078 spin_unlock(&delayed_refs->lock);
1079 mutex_lock(&head->mutex);
1080 mutex_unlock(&head->mutex);
1081
1082 btrfs_put_delayed_ref(ref);
Chris Mason1887be62009-03-13 10:11:24 -04001083 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04001084 goto again;
1085 }
1086 node = rb_next(node);
1087 }
1088 spin_unlock(&delayed_refs->lock);
Chris Mason56bec292009-03-13 10:10:06 -04001089 schedule_timeout(1);
1090 goto again;
1091 }
Chris Mason54aa1f42007-06-22 14:16:25 -04001092out:
Chris Masonc3e69d52009-03-13 10:17:05 -04001093 spin_unlock(&delayed_refs->lock);
Chris Masona28ec192007-03-06 20:08:01 -05001094 return 0;
1095}
1096
Yan Zheng80ff3852008-10-30 14:20:02 -04001097int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
Yan Zheng17d217f2008-12-12 10:03:38 -05001098 struct btrfs_root *root, u64 objectid, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05001099{
1100 struct btrfs_root *extent_root = root->fs_info->extent_root;
1101 struct btrfs_path *path;
Yan Zhengf321e492008-07-30 09:26:11 -04001102 struct extent_buffer *leaf;
1103 struct btrfs_extent_ref *ref_item;
Chris Masonbe20aa92007-12-17 20:14:01 -05001104 struct btrfs_key key;
1105 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001106 u64 ref_root;
1107 u64 last_snapshot;
Yan Zhengf321e492008-07-30 09:26:11 -04001108 u32 nritems;
1109 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001110
Chris Masonbe20aa92007-12-17 20:14:01 -05001111 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04001112 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04001113 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05001114
Yan Zhengf321e492008-07-30 09:26:11 -04001115 path = btrfs_alloc_path();
Chris Masonbe20aa92007-12-17 20:14:01 -05001116 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
1117 if (ret < 0)
1118 goto out;
1119 BUG_ON(ret == 0);
Yan Zheng80ff3852008-10-30 14:20:02 -04001120
1121 ret = -ENOENT;
1122 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04001123 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05001124
Zheng Yan31840ae2008-09-23 13:14:14 -04001125 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04001126 leaf = path->nodes[0];
1127 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001128
1129 if (found_key.objectid != bytenr ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001130 found_key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05001131 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05001132
Yan Zheng80ff3852008-10-30 14:20:02 -04001133 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Chris Masonbe20aa92007-12-17 20:14:01 -05001134 while (1) {
Yan Zhengf321e492008-07-30 09:26:11 -04001135 leaf = path->nodes[0];
1136 nritems = btrfs_header_nritems(leaf);
Chris Masonbe20aa92007-12-17 20:14:01 -05001137 if (path->slots[0] >= nritems) {
1138 ret = btrfs_next_leaf(extent_root, path);
Yan Zhengf321e492008-07-30 09:26:11 -04001139 if (ret < 0)
1140 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05001141 if (ret == 0)
1142 continue;
1143 break;
1144 }
Yan Zhengf321e492008-07-30 09:26:11 -04001145 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001146 if (found_key.objectid != bytenr)
1147 break;
Chris Masonbd098352008-01-03 13:23:19 -05001148
Chris Masonbe20aa92007-12-17 20:14:01 -05001149 if (found_key.type != BTRFS_EXTENT_REF_KEY) {
1150 path->slots[0]++;
1151 continue;
1152 }
1153
Yan Zhengf321e492008-07-30 09:26:11 -04001154 ref_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masonbe20aa92007-12-17 20:14:01 -05001155 struct btrfs_extent_ref);
Yan Zheng80ff3852008-10-30 14:20:02 -04001156 ref_root = btrfs_ref_root(leaf, ref_item);
Yan Zheng17d217f2008-12-12 10:03:38 -05001157 if ((ref_root != root->root_key.objectid &&
1158 ref_root != BTRFS_TREE_LOG_OBJECTID) ||
1159 objectid != btrfs_ref_objectid(leaf, ref_item)) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001160 ret = 1;
1161 goto out;
Yan Zhengf321e492008-07-30 09:26:11 -04001162 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001163 if (btrfs_ref_generation(leaf, ref_item) <= last_snapshot) {
1164 ret = 1;
1165 goto out;
1166 }
Yan Zhengf321e492008-07-30 09:26:11 -04001167
Chris Masonbe20aa92007-12-17 20:14:01 -05001168 path->slots[0]++;
1169 }
Yan Zhengf321e492008-07-30 09:26:11 -04001170 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001171out:
Yan Zhengf321e492008-07-30 09:26:11 -04001172 btrfs_free_path(path);
1173 return ret;
1174}
1175
Zheng Yan31840ae2008-09-23 13:14:14 -04001176int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1177 struct extent_buffer *buf, u32 nr_extents)
Chris Mason02217ed2007-03-02 16:08:05 -05001178{
Chris Mason5f39d392007-10-15 16:14:19 -04001179 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04001180 struct btrfs_file_extent_item *fi;
Zheng Yane4657682008-09-26 10:04:53 -04001181 u64 root_gen;
1182 u32 nritems;
Chris Mason02217ed2007-03-02 16:08:05 -05001183 int i;
Chris Masondb945352007-10-15 16:15:53 -04001184 int level;
Zheng Yan31840ae2008-09-23 13:14:14 -04001185 int ret = 0;
Zheng Yane4657682008-09-26 10:04:53 -04001186 int shared = 0;
Chris Masona28ec192007-03-06 20:08:01 -05001187
Chris Mason3768f362007-03-13 16:47:54 -04001188 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -05001189 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001190
Zheng Yane4657682008-09-26 10:04:53 -04001191 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
1192 shared = 0;
1193 root_gen = root->root_key.offset;
1194 } else {
1195 shared = 1;
1196 root_gen = trans->transid - 1;
1197 }
1198
Chris Masondb945352007-10-15 16:15:53 -04001199 level = btrfs_header_level(buf);
Chris Mason5f39d392007-10-15 16:14:19 -04001200 nritems = btrfs_header_nritems(buf);
Chris Mason4a096752008-07-21 10:29:44 -04001201
Zheng Yan31840ae2008-09-23 13:14:14 -04001202 if (level == 0) {
Yan Zheng31153d82008-07-28 15:32:19 -04001203 struct btrfs_leaf_ref *ref;
1204 struct btrfs_extent_info *info;
1205
Zheng Yan31840ae2008-09-23 13:14:14 -04001206 ref = btrfs_alloc_leaf_ref(root, nr_extents);
Yan Zheng31153d82008-07-28 15:32:19 -04001207 if (!ref) {
Zheng Yan31840ae2008-09-23 13:14:14 -04001208 ret = -ENOMEM;
Yan Zheng31153d82008-07-28 15:32:19 -04001209 goto out;
1210 }
1211
Zheng Yane4657682008-09-26 10:04:53 -04001212 ref->root_gen = root_gen;
Yan Zheng31153d82008-07-28 15:32:19 -04001213 ref->bytenr = buf->start;
1214 ref->owner = btrfs_header_owner(buf);
1215 ref->generation = btrfs_header_generation(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04001216 ref->nritems = nr_extents;
Yan Zheng31153d82008-07-28 15:32:19 -04001217 info = ref->extents;
Yanbcc63ab2008-07-30 16:29:20 -04001218
Zheng Yan31840ae2008-09-23 13:14:14 -04001219 for (i = 0; nr_extents > 0 && i < nritems; i++) {
Yan Zheng31153d82008-07-28 15:32:19 -04001220 u64 disk_bytenr;
1221 btrfs_item_key_to_cpu(buf, &key, i);
1222 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1223 continue;
1224 fi = btrfs_item_ptr(buf, i,
1225 struct btrfs_file_extent_item);
1226 if (btrfs_file_extent_type(buf, fi) ==
1227 BTRFS_FILE_EXTENT_INLINE)
1228 continue;
1229 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1230 if (disk_bytenr == 0)
1231 continue;
1232
1233 info->bytenr = disk_bytenr;
1234 info->num_bytes =
1235 btrfs_file_extent_disk_num_bytes(buf, fi);
1236 info->objectid = key.objectid;
1237 info->offset = key.offset;
1238 info++;
1239 }
1240
Zheng Yane4657682008-09-26 10:04:53 -04001241 ret = btrfs_add_leaf_ref(root, ref, shared);
Yan Zheng5b84e8d2008-10-09 11:46:19 -04001242 if (ret == -EEXIST && shared) {
1243 struct btrfs_leaf_ref *old;
1244 old = btrfs_lookup_leaf_ref(root, ref->bytenr);
1245 BUG_ON(!old);
1246 btrfs_remove_leaf_ref(root, old);
1247 btrfs_free_leaf_ref(root, old);
1248 ret = btrfs_add_leaf_ref(root, ref, shared);
1249 }
Yan Zheng31153d82008-07-28 15:32:19 -04001250 WARN_ON(ret);
Yanbcc63ab2008-07-30 16:29:20 -04001251 btrfs_free_leaf_ref(root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04001252 }
1253out:
Zheng Yan31840ae2008-09-23 13:14:14 -04001254 return ret;
1255}
1256
Chris Masonb7a9f292009-02-04 09:23:45 -05001257/* when a block goes through cow, we update the reference counts of
1258 * everything that block points to. The internal pointers of the block
1259 * can be in just about any order, and it is likely to have clusters of
1260 * things that are close together and clusters of things that are not.
1261 *
1262 * To help reduce the seeks that come with updating all of these reference
1263 * counts, sort them by byte number before actual updates are done.
1264 *
1265 * struct refsort is used to match byte number to slot in the btree block.
1266 * we sort based on the byte number and then use the slot to actually
1267 * find the item.
Chris Masonbd56b302009-02-04 09:27:02 -05001268 *
1269 * struct refsort is smaller than strcut btrfs_item and smaller than
1270 * struct btrfs_key_ptr. Since we're currently limited to the page size
1271 * for a btree block, there's no way for a kmalloc of refsorts for a
1272 * single node to be bigger than a page.
Chris Masonb7a9f292009-02-04 09:23:45 -05001273 */
1274struct refsort {
1275 u64 bytenr;
1276 u32 slot;
1277};
1278
1279/*
1280 * for passing into sort()
1281 */
1282static int refsort_cmp(const void *a_void, const void *b_void)
1283{
1284 const struct refsort *a = a_void;
1285 const struct refsort *b = b_void;
1286
1287 if (a->bytenr < b->bytenr)
1288 return -1;
1289 if (a->bytenr > b->bytenr)
1290 return 1;
1291 return 0;
1292}
1293
1294
1295noinline int btrfs_inc_ref(struct btrfs_trans_handle *trans,
1296 struct btrfs_root *root,
1297 struct extent_buffer *orig_buf,
1298 struct extent_buffer *buf, u32 *nr_extents)
Zheng Yan31840ae2008-09-23 13:14:14 -04001299{
1300 u64 bytenr;
1301 u64 ref_root;
1302 u64 orig_root;
1303 u64 ref_generation;
1304 u64 orig_generation;
Chris Masonb7a9f292009-02-04 09:23:45 -05001305 struct refsort *sorted;
Zheng Yan31840ae2008-09-23 13:14:14 -04001306 u32 nritems;
1307 u32 nr_file_extents = 0;
1308 struct btrfs_key key;
1309 struct btrfs_file_extent_item *fi;
1310 int i;
1311 int level;
1312 int ret = 0;
1313 int faili = 0;
Chris Masonb7a9f292009-02-04 09:23:45 -05001314 int refi = 0;
1315 int slot;
Zheng Yan31840ae2008-09-23 13:14:14 -04001316 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
Chris Mason56bec292009-03-13 10:10:06 -04001317 u64, u64, u64, u64, u64, u64, u64, u64, u64);
Zheng Yan31840ae2008-09-23 13:14:14 -04001318
1319 ref_root = btrfs_header_owner(buf);
1320 ref_generation = btrfs_header_generation(buf);
1321 orig_root = btrfs_header_owner(orig_buf);
1322 orig_generation = btrfs_header_generation(orig_buf);
1323
1324 nritems = btrfs_header_nritems(buf);
1325 level = btrfs_header_level(buf);
1326
Chris Masonb7a9f292009-02-04 09:23:45 -05001327 sorted = kmalloc(sizeof(struct refsort) * nritems, GFP_NOFS);
1328 BUG_ON(!sorted);
1329
Zheng Yan31840ae2008-09-23 13:14:14 -04001330 if (root->ref_cows) {
1331 process_func = __btrfs_inc_extent_ref;
1332 } else {
1333 if (level == 0 &&
1334 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1335 goto out;
1336 if (level != 0 &&
1337 root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
1338 goto out;
1339 process_func = __btrfs_update_extent_ref;
1340 }
1341
Chris Masonb7a9f292009-02-04 09:23:45 -05001342 /*
1343 * we make two passes through the items. In the first pass we
1344 * only record the byte number and slot. Then we sort based on
1345 * byte number and do the actual work based on the sorted results
1346 */
Zheng Yan31840ae2008-09-23 13:14:14 -04001347 for (i = 0; i < nritems; i++) {
1348 cond_resched();
Chris Masondb945352007-10-15 16:15:53 -04001349 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001350 btrfs_item_key_to_cpu(buf, &key, i);
1351 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04001352 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001353 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04001354 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001355 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04001356 BTRFS_FILE_EXTENT_INLINE)
1357 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04001358 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1359 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04001360 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04001361
1362 nr_file_extents++;
Chris Masonb7a9f292009-02-04 09:23:45 -05001363 sorted[refi].bytenr = bytenr;
1364 sorted[refi].slot = i;
1365 refi++;
1366 } else {
1367 bytenr = btrfs_node_blockptr(buf, i);
1368 sorted[refi].bytenr = bytenr;
1369 sorted[refi].slot = i;
1370 refi++;
1371 }
1372 }
1373 /*
1374 * if refi == 0, we didn't actually put anything into the sorted
1375 * array and we're done
1376 */
1377 if (refi == 0)
1378 goto out;
1379
1380 sort(sorted, refi, sizeof(struct refsort), refsort_cmp, NULL);
1381
1382 for (i = 0; i < refi; i++) {
1383 cond_resched();
1384 slot = sorted[i].slot;
1385 bytenr = sorted[i].bytenr;
1386
1387 if (level == 0) {
1388 btrfs_item_key_to_cpu(buf, &key, slot);
Chris Mason56bec292009-03-13 10:10:06 -04001389 fi = btrfs_item_ptr(buf, slot,
1390 struct btrfs_file_extent_item);
1391
1392 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1393 if (bytenr == 0)
1394 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04001395
Zheng Yan31840ae2008-09-23 13:14:14 -04001396 ret = process_func(trans, root, bytenr,
Chris Mason56bec292009-03-13 10:10:06 -04001397 btrfs_file_extent_disk_num_bytes(buf, fi),
1398 orig_buf->start, buf->start,
1399 orig_root, ref_root,
1400 orig_generation, ref_generation,
1401 key.objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -04001402
1403 if (ret) {
Chris Masonb7a9f292009-02-04 09:23:45 -05001404 faili = slot;
Zheng Yan31840ae2008-09-23 13:14:14 -04001405 WARN_ON(1);
1406 goto fail;
1407 }
Chris Mason54aa1f42007-06-22 14:16:25 -04001408 } else {
Chris Mason56bec292009-03-13 10:10:06 -04001409 ret = process_func(trans, root, bytenr, buf->len,
Zheng Yan31840ae2008-09-23 13:14:14 -04001410 orig_buf->start, buf->start,
1411 orig_root, ref_root,
1412 orig_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001413 level - 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001414 if (ret) {
Chris Masonb7a9f292009-02-04 09:23:45 -05001415 faili = slot;
Zheng Yan31840ae2008-09-23 13:14:14 -04001416 WARN_ON(1);
1417 goto fail;
1418 }
Chris Mason54aa1f42007-06-22 14:16:25 -04001419 }
1420 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001421out:
Chris Masonb7a9f292009-02-04 09:23:45 -05001422 kfree(sorted);
Zheng Yan31840ae2008-09-23 13:14:14 -04001423 if (nr_extents) {
1424 if (level == 0)
1425 *nr_extents = nr_file_extents;
1426 else
1427 *nr_extents = nritems;
1428 }
1429 return 0;
1430fail:
Chris Masonb7a9f292009-02-04 09:23:45 -05001431 kfree(sorted);
Zheng Yan31840ae2008-09-23 13:14:14 -04001432 WARN_ON(1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001433 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05001434}
1435
Zheng Yan31840ae2008-09-23 13:14:14 -04001436int btrfs_update_ref(struct btrfs_trans_handle *trans,
1437 struct btrfs_root *root, struct extent_buffer *orig_buf,
1438 struct extent_buffer *buf, int start_slot, int nr)
1439
1440{
1441 u64 bytenr;
1442 u64 ref_root;
1443 u64 orig_root;
1444 u64 ref_generation;
1445 u64 orig_generation;
1446 struct btrfs_key key;
1447 struct btrfs_file_extent_item *fi;
1448 int i;
1449 int ret;
1450 int slot;
1451 int level;
1452
1453 BUG_ON(start_slot < 0);
1454 BUG_ON(start_slot + nr > btrfs_header_nritems(buf));
1455
1456 ref_root = btrfs_header_owner(buf);
1457 ref_generation = btrfs_header_generation(buf);
1458 orig_root = btrfs_header_owner(orig_buf);
1459 orig_generation = btrfs_header_generation(orig_buf);
1460 level = btrfs_header_level(buf);
1461
1462 if (!root->ref_cows) {
1463 if (level == 0 &&
1464 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1465 return 0;
1466 if (level != 0 &&
1467 root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
1468 return 0;
1469 }
1470
1471 for (i = 0, slot = start_slot; i < nr; i++, slot++) {
1472 cond_resched();
1473 if (level == 0) {
1474 btrfs_item_key_to_cpu(buf, &key, slot);
1475 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1476 continue;
1477 fi = btrfs_item_ptr(buf, slot,
1478 struct btrfs_file_extent_item);
1479 if (btrfs_file_extent_type(buf, fi) ==
1480 BTRFS_FILE_EXTENT_INLINE)
1481 continue;
1482 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1483 if (bytenr == 0)
1484 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04001485 ret = __btrfs_update_extent_ref(trans, root, bytenr,
Chris Mason56bec292009-03-13 10:10:06 -04001486 btrfs_file_extent_disk_num_bytes(buf, fi),
1487 orig_buf->start, buf->start,
1488 orig_root, ref_root, orig_generation,
1489 ref_generation, key.objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -04001490 if (ret)
1491 goto fail;
1492 } else {
1493 bytenr = btrfs_node_blockptr(buf, slot);
Zheng Yan31840ae2008-09-23 13:14:14 -04001494 ret = __btrfs_update_extent_ref(trans, root, bytenr,
Chris Mason56bec292009-03-13 10:10:06 -04001495 buf->len, orig_buf->start,
1496 buf->start, orig_root, ref_root,
Zheng Yan31840ae2008-09-23 13:14:14 -04001497 orig_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001498 level - 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001499 if (ret)
1500 goto fail;
1501 }
1502 }
1503 return 0;
1504fail:
1505 WARN_ON(1);
1506 return -1;
1507}
1508
Chris Mason9078a3e2007-04-26 16:46:15 -04001509static int write_one_cache_group(struct btrfs_trans_handle *trans,
1510 struct btrfs_root *root,
1511 struct btrfs_path *path,
1512 struct btrfs_block_group_cache *cache)
1513{
1514 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04001515 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04001516 unsigned long bi;
1517 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04001518
Chris Mason9078a3e2007-04-26 16:46:15 -04001519 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001520 if (ret < 0)
1521 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -04001522 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001523
1524 leaf = path->nodes[0];
1525 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
1526 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
1527 btrfs_mark_buffer_dirty(leaf);
Chris Mason9078a3e2007-04-26 16:46:15 -04001528 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -04001529fail:
Chris Mason9078a3e2007-04-26 16:46:15 -04001530 if (ret)
1531 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04001532 return 0;
1533
1534}
1535
Chris Mason96b51792007-10-15 16:15:19 -04001536int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
1537 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04001538{
Josef Bacik0f9dd462008-09-23 13:14:11 -04001539 struct btrfs_block_group_cache *cache, *entry;
1540 struct rb_node *n;
Chris Mason9078a3e2007-04-26 16:46:15 -04001541 int err = 0;
1542 int werr = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04001543 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04001544 u64 last = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04001545
1546 path = btrfs_alloc_path();
1547 if (!path)
1548 return -ENOMEM;
1549
Chris Masond3977122009-01-05 21:25:51 -05001550 while (1) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001551 cache = NULL;
1552 spin_lock(&root->fs_info->block_group_cache_lock);
1553 for (n = rb_first(&root->fs_info->block_group_cache_tree);
1554 n; n = rb_next(n)) {
1555 entry = rb_entry(n, struct btrfs_block_group_cache,
1556 cache_node);
1557 if (entry->dirty) {
1558 cache = entry;
1559 break;
1560 }
1561 }
1562 spin_unlock(&root->fs_info->block_group_cache_lock);
1563
1564 if (!cache)
Chris Mason9078a3e2007-04-26 16:46:15 -04001565 break;
Chris Mason54aa1f42007-06-22 14:16:25 -04001566
Zheng Yane8569812008-09-26 10:05:48 -04001567 cache->dirty = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001568 last += cache->key.offset;
1569
Chris Mason96b51792007-10-15 16:15:19 -04001570 err = write_one_cache_group(trans, root,
1571 path, cache);
1572 /*
1573 * if we fail to write the cache group, we want
1574 * to keep it marked dirty in hopes that a later
1575 * write will work
1576 */
1577 if (err) {
1578 werr = err;
1579 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04001580 }
1581 }
1582 btrfs_free_path(path);
1583 return werr;
1584}
1585
Yan Zhengd2fb3432008-12-11 16:30:39 -05001586int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
1587{
1588 struct btrfs_block_group_cache *block_group;
1589 int readonly = 0;
1590
1591 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
1592 if (!block_group || block_group->ro)
1593 readonly = 1;
1594 if (block_group)
1595 put_block_group(block_group);
1596 return readonly;
1597}
1598
Chris Mason593060d2008-03-25 16:50:33 -04001599static int update_space_info(struct btrfs_fs_info *info, u64 flags,
1600 u64 total_bytes, u64 bytes_used,
1601 struct btrfs_space_info **space_info)
1602{
1603 struct btrfs_space_info *found;
1604
1605 found = __find_space_info(info, flags);
1606 if (found) {
Josef Bacik25179202008-10-29 14:49:05 -04001607 spin_lock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04001608 found->total_bytes += total_bytes;
1609 found->bytes_used += bytes_used;
Chris Mason8f18cf12008-04-25 16:53:30 -04001610 found->full = 0;
Josef Bacik25179202008-10-29 14:49:05 -04001611 spin_unlock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04001612 *space_info = found;
1613 return 0;
1614 }
Yan Zhengc146afa2008-11-12 14:34:12 -05001615 found = kzalloc(sizeof(*found), GFP_NOFS);
Chris Mason593060d2008-03-25 16:50:33 -04001616 if (!found)
1617 return -ENOMEM;
1618
Josef Bacik0f9dd462008-09-23 13:14:11 -04001619 INIT_LIST_HEAD(&found->block_groups);
Josef Bacik80eb2342008-10-29 14:49:05 -04001620 init_rwsem(&found->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001621 spin_lock_init(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04001622 found->flags = flags;
1623 found->total_bytes = total_bytes;
1624 found->bytes_used = bytes_used;
1625 found->bytes_pinned = 0;
Zheng Yane8569812008-09-26 10:05:48 -04001626 found->bytes_reserved = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05001627 found->bytes_readonly = 0;
Josef Bacik6a632092009-02-20 11:00:09 -05001628 found->bytes_delalloc = 0;
Chris Mason593060d2008-03-25 16:50:33 -04001629 found->full = 0;
Chris Mason0ef3e662008-05-24 14:04:53 -04001630 found->force_alloc = 0;
Chris Mason593060d2008-03-25 16:50:33 -04001631 *space_info = found;
Chris Mason4184ea72009-03-10 12:39:20 -04001632 list_add_rcu(&found->list, &info->space_info);
Chris Mason593060d2008-03-25 16:50:33 -04001633 return 0;
1634}
1635
Chris Mason8790d502008-04-03 16:29:03 -04001636static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
1637{
1638 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
Chris Mason611f0e02008-04-03 16:29:03 -04001639 BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04001640 BTRFS_BLOCK_GROUP_RAID10 |
Chris Mason611f0e02008-04-03 16:29:03 -04001641 BTRFS_BLOCK_GROUP_DUP);
Chris Mason8790d502008-04-03 16:29:03 -04001642 if (extra_flags) {
1643 if (flags & BTRFS_BLOCK_GROUP_DATA)
1644 fs_info->avail_data_alloc_bits |= extra_flags;
1645 if (flags & BTRFS_BLOCK_GROUP_METADATA)
1646 fs_info->avail_metadata_alloc_bits |= extra_flags;
1647 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
1648 fs_info->avail_system_alloc_bits |= extra_flags;
1649 }
1650}
Chris Mason593060d2008-03-25 16:50:33 -04001651
Yan Zhengc146afa2008-11-12 14:34:12 -05001652static void set_block_group_readonly(struct btrfs_block_group_cache *cache)
1653{
1654 spin_lock(&cache->space_info->lock);
1655 spin_lock(&cache->lock);
1656 if (!cache->ro) {
1657 cache->space_info->bytes_readonly += cache->key.offset -
1658 btrfs_block_group_used(&cache->item);
1659 cache->ro = 1;
1660 }
1661 spin_unlock(&cache->lock);
1662 spin_unlock(&cache->space_info->lock);
1663}
1664
Yan Zheng2b820322008-11-17 21:11:30 -05001665u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04001666{
Yan Zheng2b820322008-11-17 21:11:30 -05001667 u64 num_devices = root->fs_info->fs_devices->rw_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001668
1669 if (num_devices == 1)
1670 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
1671 if (num_devices < 4)
1672 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
1673
Chris Masonec44a352008-04-28 15:29:52 -04001674 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
1675 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
Chris Masona061fc82008-05-07 11:43:44 -04001676 BTRFS_BLOCK_GROUP_RAID10))) {
Chris Masonec44a352008-04-28 15:29:52 -04001677 flags &= ~BTRFS_BLOCK_GROUP_DUP;
Chris Masona061fc82008-05-07 11:43:44 -04001678 }
Chris Masonec44a352008-04-28 15:29:52 -04001679
1680 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
Chris Masona061fc82008-05-07 11:43:44 -04001681 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
Chris Masonec44a352008-04-28 15:29:52 -04001682 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
Chris Masona061fc82008-05-07 11:43:44 -04001683 }
Chris Masonec44a352008-04-28 15:29:52 -04001684
1685 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
1686 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
1687 (flags & BTRFS_BLOCK_GROUP_RAID10) |
1688 (flags & BTRFS_BLOCK_GROUP_DUP)))
1689 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
1690 return flags;
1691}
1692
Josef Bacik6a632092009-02-20 11:00:09 -05001693static u64 btrfs_get_alloc_profile(struct btrfs_root *root, u64 data)
1694{
1695 struct btrfs_fs_info *info = root->fs_info;
1696 u64 alloc_profile;
1697
1698 if (data) {
1699 alloc_profile = info->avail_data_alloc_bits &
1700 info->data_alloc_profile;
1701 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
1702 } else if (root == root->fs_info->chunk_root) {
1703 alloc_profile = info->avail_system_alloc_bits &
1704 info->system_alloc_profile;
1705 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
1706 } else {
1707 alloc_profile = info->avail_metadata_alloc_bits &
1708 info->metadata_alloc_profile;
1709 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
1710 }
1711
1712 return btrfs_reduce_alloc_profile(root, data);
1713}
1714
1715void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode)
1716{
1717 u64 alloc_target;
1718
1719 alloc_target = btrfs_get_alloc_profile(root, 1);
1720 BTRFS_I(inode)->space_info = __find_space_info(root->fs_info,
1721 alloc_target);
1722}
1723
1724/*
1725 * for now this just makes sure we have at least 5% of our metadata space free
1726 * for use.
1727 */
1728int btrfs_check_metadata_free_space(struct btrfs_root *root)
1729{
1730 struct btrfs_fs_info *info = root->fs_info;
1731 struct btrfs_space_info *meta_sinfo;
1732 u64 alloc_target, thresh;
Josef Bacik4e06bdd2009-02-20 10:59:53 -05001733 int committed = 0, ret;
Josef Bacik6a632092009-02-20 11:00:09 -05001734
1735 /* get the space info for where the metadata will live */
1736 alloc_target = btrfs_get_alloc_profile(root, 0);
1737 meta_sinfo = __find_space_info(info, alloc_target);
1738
Josef Bacik4e06bdd2009-02-20 10:59:53 -05001739again:
Josef Bacik6a632092009-02-20 11:00:09 -05001740 spin_lock(&meta_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05001741 if (!meta_sinfo->full)
1742 thresh = meta_sinfo->total_bytes * 80;
1743 else
1744 thresh = meta_sinfo->total_bytes * 95;
Josef Bacik6a632092009-02-20 11:00:09 -05001745
1746 do_div(thresh, 100);
1747
1748 if (meta_sinfo->bytes_used + meta_sinfo->bytes_reserved +
1749 meta_sinfo->bytes_pinned + meta_sinfo->bytes_readonly > thresh) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05001750 struct btrfs_trans_handle *trans;
1751 if (!meta_sinfo->full) {
1752 meta_sinfo->force_alloc = 1;
1753 spin_unlock(&meta_sinfo->lock);
1754
1755 trans = btrfs_start_transaction(root, 1);
1756 if (!trans)
1757 return -ENOMEM;
1758
1759 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
1760 2 * 1024 * 1024, alloc_target, 0);
1761 btrfs_end_transaction(trans, root);
1762 goto again;
1763 }
Josef Bacik6a632092009-02-20 11:00:09 -05001764 spin_unlock(&meta_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05001765
1766 if (!committed) {
1767 committed = 1;
1768 trans = btrfs_join_transaction(root, 1);
1769 if (!trans)
1770 return -ENOMEM;
1771 ret = btrfs_commit_transaction(trans, root);
1772 if (ret)
1773 return ret;
1774 goto again;
1775 }
Josef Bacik6a632092009-02-20 11:00:09 -05001776 return -ENOSPC;
1777 }
1778 spin_unlock(&meta_sinfo->lock);
1779
1780 return 0;
1781}
1782
1783/*
1784 * This will check the space that the inode allocates from to make sure we have
1785 * enough space for bytes.
1786 */
1787int btrfs_check_data_free_space(struct btrfs_root *root, struct inode *inode,
1788 u64 bytes)
1789{
1790 struct btrfs_space_info *data_sinfo;
Josef Bacik4e06bdd2009-02-20 10:59:53 -05001791 int ret = 0, committed = 0;
Josef Bacik6a632092009-02-20 11:00:09 -05001792
1793 /* make sure bytes are sectorsize aligned */
1794 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
1795
1796 data_sinfo = BTRFS_I(inode)->space_info;
1797again:
1798 /* make sure we have enough space to handle the data first */
1799 spin_lock(&data_sinfo->lock);
1800 if (data_sinfo->total_bytes - data_sinfo->bytes_used -
1801 data_sinfo->bytes_delalloc - data_sinfo->bytes_reserved -
1802 data_sinfo->bytes_pinned - data_sinfo->bytes_readonly -
1803 data_sinfo->bytes_may_use < bytes) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05001804 struct btrfs_trans_handle *trans;
1805
Josef Bacik6a632092009-02-20 11:00:09 -05001806 /*
1807 * if we don't have enough free bytes in this space then we need
1808 * to alloc a new chunk.
1809 */
1810 if (!data_sinfo->full) {
1811 u64 alloc_target;
Josef Bacik6a632092009-02-20 11:00:09 -05001812
1813 data_sinfo->force_alloc = 1;
1814 spin_unlock(&data_sinfo->lock);
1815
1816 alloc_target = btrfs_get_alloc_profile(root, 1);
1817 trans = btrfs_start_transaction(root, 1);
1818 if (!trans)
1819 return -ENOMEM;
1820
1821 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
1822 bytes + 2 * 1024 * 1024,
1823 alloc_target, 0);
1824 btrfs_end_transaction(trans, root);
1825 if (ret)
1826 return ret;
1827 goto again;
1828 }
1829 spin_unlock(&data_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05001830
1831 /* commit the current transaction and try again */
1832 if (!committed) {
1833 committed = 1;
1834 trans = btrfs_join_transaction(root, 1);
1835 if (!trans)
1836 return -ENOMEM;
1837 ret = btrfs_commit_transaction(trans, root);
1838 if (ret)
1839 return ret;
1840 goto again;
1841 }
1842
Josef Bacik6a632092009-02-20 11:00:09 -05001843 printk(KERN_ERR "no space left, need %llu, %llu delalloc bytes"
1844 ", %llu bytes_used, %llu bytes_reserved, "
1845 "%llu bytes_pinned, %llu bytes_readonly, %llu may use"
1846 "%llu total\n", bytes, data_sinfo->bytes_delalloc,
1847 data_sinfo->bytes_used, data_sinfo->bytes_reserved,
1848 data_sinfo->bytes_pinned, data_sinfo->bytes_readonly,
1849 data_sinfo->bytes_may_use, data_sinfo->total_bytes);
1850 return -ENOSPC;
1851 }
1852 data_sinfo->bytes_may_use += bytes;
1853 BTRFS_I(inode)->reserved_bytes += bytes;
1854 spin_unlock(&data_sinfo->lock);
1855
1856 return btrfs_check_metadata_free_space(root);
1857}
1858
1859/*
1860 * if there was an error for whatever reason after calling
1861 * btrfs_check_data_free_space, call this so we can cleanup the counters.
1862 */
1863void btrfs_free_reserved_data_space(struct btrfs_root *root,
1864 struct inode *inode, u64 bytes)
1865{
1866 struct btrfs_space_info *data_sinfo;
1867
1868 /* make sure bytes are sectorsize aligned */
1869 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
1870
1871 data_sinfo = BTRFS_I(inode)->space_info;
1872 spin_lock(&data_sinfo->lock);
1873 data_sinfo->bytes_may_use -= bytes;
1874 BTRFS_I(inode)->reserved_bytes -= bytes;
1875 spin_unlock(&data_sinfo->lock);
1876}
1877
1878/* called when we are adding a delalloc extent to the inode's io_tree */
1879void btrfs_delalloc_reserve_space(struct btrfs_root *root, struct inode *inode,
1880 u64 bytes)
1881{
1882 struct btrfs_space_info *data_sinfo;
1883
1884 /* get the space info for where this inode will be storing its data */
1885 data_sinfo = BTRFS_I(inode)->space_info;
1886
1887 /* make sure we have enough space to handle the data first */
1888 spin_lock(&data_sinfo->lock);
1889 data_sinfo->bytes_delalloc += bytes;
1890
1891 /*
1892 * we are adding a delalloc extent without calling
1893 * btrfs_check_data_free_space first. This happens on a weird
1894 * writepage condition, but shouldn't hurt our accounting
1895 */
1896 if (unlikely(bytes > BTRFS_I(inode)->reserved_bytes)) {
1897 data_sinfo->bytes_may_use -= BTRFS_I(inode)->reserved_bytes;
1898 BTRFS_I(inode)->reserved_bytes = 0;
1899 } else {
1900 data_sinfo->bytes_may_use -= bytes;
1901 BTRFS_I(inode)->reserved_bytes -= bytes;
1902 }
1903
1904 spin_unlock(&data_sinfo->lock);
1905}
1906
1907/* called when we are clearing an delalloc extent from the inode's io_tree */
1908void btrfs_delalloc_free_space(struct btrfs_root *root, struct inode *inode,
1909 u64 bytes)
1910{
1911 struct btrfs_space_info *info;
1912
1913 info = BTRFS_I(inode)->space_info;
1914
1915 spin_lock(&info->lock);
1916 info->bytes_delalloc -= bytes;
1917 spin_unlock(&info->lock);
1918}
1919
Chris Mason6324fbf2008-03-24 15:01:59 -04001920static int do_chunk_alloc(struct btrfs_trans_handle *trans,
1921 struct btrfs_root *extent_root, u64 alloc_bytes,
Chris Mason0ef3e662008-05-24 14:04:53 -04001922 u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04001923{
1924 struct btrfs_space_info *space_info;
1925 u64 thresh;
Yan Zhengc146afa2008-11-12 14:34:12 -05001926 int ret = 0;
1927
1928 mutex_lock(&extent_root->fs_info->chunk_mutex);
Chris Mason6324fbf2008-03-24 15:01:59 -04001929
Yan Zheng2b820322008-11-17 21:11:30 -05001930 flags = btrfs_reduce_alloc_profile(extent_root, flags);
Chris Masonec44a352008-04-28 15:29:52 -04001931
Chris Mason6324fbf2008-03-24 15:01:59 -04001932 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04001933 if (!space_info) {
1934 ret = update_space_info(extent_root->fs_info, flags,
1935 0, 0, &space_info);
1936 BUG_ON(ret);
1937 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001938 BUG_ON(!space_info);
1939
Josef Bacik25179202008-10-29 14:49:05 -04001940 spin_lock(&space_info->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04001941 if (space_info->force_alloc) {
1942 force = 1;
1943 space_info->force_alloc = 0;
1944 }
Josef Bacik25179202008-10-29 14:49:05 -04001945 if (space_info->full) {
1946 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04001947 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04001948 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001949
Yan Zhengc146afa2008-11-12 14:34:12 -05001950 thresh = space_info->total_bytes - space_info->bytes_readonly;
1951 thresh = div_factor(thresh, 6);
Chris Mason0ef3e662008-05-24 14:04:53 -04001952 if (!force &&
Zheng Yane8569812008-09-26 10:05:48 -04001953 (space_info->bytes_used + space_info->bytes_pinned +
Josef Bacik25179202008-10-29 14:49:05 -04001954 space_info->bytes_reserved + alloc_bytes) < thresh) {
1955 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04001956 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04001957 }
Josef Bacik25179202008-10-29 14:49:05 -04001958 spin_unlock(&space_info->lock);
1959
Yan Zheng2b820322008-11-17 21:11:30 -05001960 ret = btrfs_alloc_chunk(trans, extent_root, flags);
Chris Masond3977122009-01-05 21:25:51 -05001961 if (ret)
Chris Mason6324fbf2008-03-24 15:01:59 -04001962 space_info->full = 1;
Chris Masona74a4b92008-06-25 16:01:31 -04001963out:
Yan Zhengc146afa2008-11-12 14:34:12 -05001964 mutex_unlock(&extent_root->fs_info->chunk_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001965 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04001966}
1967
Chris Mason9078a3e2007-04-26 16:46:15 -04001968static int update_block_group(struct btrfs_trans_handle *trans,
1969 struct btrfs_root *root,
Chris Masondb945352007-10-15 16:15:53 -04001970 u64 bytenr, u64 num_bytes, int alloc,
Chris Mason0b86a832008-03-24 15:01:56 -04001971 int mark_free)
Chris Mason9078a3e2007-04-26 16:46:15 -04001972{
1973 struct btrfs_block_group_cache *cache;
1974 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04001975 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04001976 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04001977 u64 byte_in_group;
Chris Mason3e1ad542007-05-07 20:03:49 -04001978
Chris Masond3977122009-01-05 21:25:51 -05001979 while (total) {
Chris Masondb945352007-10-15 16:15:53 -04001980 cache = btrfs_lookup_block_group(info, bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05001981 if (!cache)
Chris Mason9078a3e2007-04-26 16:46:15 -04001982 return -1;
Chris Masondb945352007-10-15 16:15:53 -04001983 byte_in_group = bytenr - cache->key.objectid;
1984 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04001985
Josef Bacik25179202008-10-29 14:49:05 -04001986 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04001987 spin_lock(&cache->lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001988 cache->dirty = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04001989 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04001990 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04001991 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04001992 old_val += num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04001993 cache->space_info->bytes_used += num_bytes;
Yan Zhenga512bbf2008-12-08 16:46:26 -05001994 if (cache->ro)
Yan Zhengc146afa2008-11-12 14:34:12 -05001995 cache->space_info->bytes_readonly -= num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04001996 btrfs_set_block_group_used(&cache->item, old_val);
1997 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04001998 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04001999 } else {
Chris Masondb945352007-10-15 16:15:53 -04002000 old_val -= num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04002001 cache->space_info->bytes_used -= num_bytes;
Yan Zhengc146afa2008-11-12 14:34:12 -05002002 if (cache->ro)
2003 cache->space_info->bytes_readonly += num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04002004 btrfs_set_block_group_used(&cache->item, old_val);
2005 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04002006 spin_unlock(&cache->space_info->lock);
Chris Masonf510cfe2007-10-15 16:14:48 -04002007 if (mark_free) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04002008 int ret;
Liu Hui1f3c79a2009-01-05 15:57:51 -05002009
2010 ret = btrfs_discard_extent(root, bytenr,
2011 num_bytes);
2012 WARN_ON(ret);
2013
Josef Bacik0f9dd462008-09-23 13:14:11 -04002014 ret = btrfs_add_free_space(cache, bytenr,
2015 num_bytes);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002016 WARN_ON(ret);
Chris Masone37c9e62007-05-09 20:13:14 -04002017 }
Chris Masoncd1bc462007-04-27 10:08:34 -04002018 }
Yan Zhengd2fb3432008-12-11 16:30:39 -05002019 put_block_group(cache);
Chris Masondb945352007-10-15 16:15:53 -04002020 total -= num_bytes;
2021 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04002022 }
2023 return 0;
2024}
Chris Mason6324fbf2008-03-24 15:01:59 -04002025
Chris Masona061fc82008-05-07 11:43:44 -04002026static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
2027{
Josef Bacik0f9dd462008-09-23 13:14:11 -04002028 struct btrfs_block_group_cache *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05002029 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002030
2031 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
2032 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04002033 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002034
Yan Zhengd2fb3432008-12-11 16:30:39 -05002035 bytenr = cache->key.objectid;
2036 put_block_group(cache);
2037
2038 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04002039}
2040
Chris Masone02119d2008-09-05 16:13:11 -04002041int btrfs_update_pinned_extents(struct btrfs_root *root,
Yan324ae4d2007-11-16 14:57:08 -05002042 u64 bytenr, u64 num, int pin)
2043{
2044 u64 len;
2045 struct btrfs_block_group_cache *cache;
2046 struct btrfs_fs_info *fs_info = root->fs_info;
2047
2048 if (pin) {
2049 set_extent_dirty(&fs_info->pinned_extents,
2050 bytenr, bytenr + num - 1, GFP_NOFS);
2051 } else {
2052 clear_extent_dirty(&fs_info->pinned_extents,
2053 bytenr, bytenr + num - 1, GFP_NOFS);
2054 }
Chris Masonb9473432009-03-13 11:00:37 -04002055
Yan324ae4d2007-11-16 14:57:08 -05002056 while (num > 0) {
2057 cache = btrfs_lookup_block_group(fs_info, bytenr);
Zheng Yane8569812008-09-26 10:05:48 -04002058 BUG_ON(!cache);
2059 len = min(num, cache->key.offset -
2060 (bytenr - cache->key.objectid));
Yan324ae4d2007-11-16 14:57:08 -05002061 if (pin) {
Josef Bacik25179202008-10-29 14:49:05 -04002062 spin_lock(&cache->space_info->lock);
Zheng Yane8569812008-09-26 10:05:48 -04002063 spin_lock(&cache->lock);
2064 cache->pinned += len;
2065 cache->space_info->bytes_pinned += len;
2066 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04002067 spin_unlock(&cache->space_info->lock);
Yan324ae4d2007-11-16 14:57:08 -05002068 fs_info->total_pinned += len;
2069 } else {
Josef Bacik25179202008-10-29 14:49:05 -04002070 spin_lock(&cache->space_info->lock);
Zheng Yane8569812008-09-26 10:05:48 -04002071 spin_lock(&cache->lock);
2072 cache->pinned -= len;
2073 cache->space_info->bytes_pinned -= len;
2074 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04002075 spin_unlock(&cache->space_info->lock);
Yan324ae4d2007-11-16 14:57:08 -05002076 fs_info->total_pinned -= len;
Josef Bacik07103a32008-11-19 15:17:55 -05002077 if (cache->cached)
2078 btrfs_add_free_space(cache, bytenr, len);
Yan324ae4d2007-11-16 14:57:08 -05002079 }
Yan Zhengd2fb3432008-12-11 16:30:39 -05002080 put_block_group(cache);
Yan324ae4d2007-11-16 14:57:08 -05002081 bytenr += len;
2082 num -= len;
2083 }
2084 return 0;
2085}
Chris Mason9078a3e2007-04-26 16:46:15 -04002086
Zheng Yane8569812008-09-26 10:05:48 -04002087static int update_reserved_extents(struct btrfs_root *root,
2088 u64 bytenr, u64 num, int reserve)
2089{
2090 u64 len;
2091 struct btrfs_block_group_cache *cache;
2092 struct btrfs_fs_info *fs_info = root->fs_info;
2093
Zheng Yane8569812008-09-26 10:05:48 -04002094 while (num > 0) {
2095 cache = btrfs_lookup_block_group(fs_info, bytenr);
2096 BUG_ON(!cache);
2097 len = min(num, cache->key.offset -
2098 (bytenr - cache->key.objectid));
Josef Bacik25179202008-10-29 14:49:05 -04002099
2100 spin_lock(&cache->space_info->lock);
2101 spin_lock(&cache->lock);
Zheng Yane8569812008-09-26 10:05:48 -04002102 if (reserve) {
Zheng Yane8569812008-09-26 10:05:48 -04002103 cache->reserved += len;
2104 cache->space_info->bytes_reserved += len;
Zheng Yane8569812008-09-26 10:05:48 -04002105 } else {
Zheng Yane8569812008-09-26 10:05:48 -04002106 cache->reserved -= len;
2107 cache->space_info->bytes_reserved -= len;
Zheng Yane8569812008-09-26 10:05:48 -04002108 }
Josef Bacik25179202008-10-29 14:49:05 -04002109 spin_unlock(&cache->lock);
2110 spin_unlock(&cache->space_info->lock);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002111 put_block_group(cache);
Zheng Yane8569812008-09-26 10:05:48 -04002112 bytenr += len;
2113 num -= len;
2114 }
2115 return 0;
2116}
2117
Chris Masond1310b22008-01-24 16:13:08 -05002118int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
Chris Masonccd467d2007-06-28 15:57:36 -04002119{
Chris Masonccd467d2007-06-28 15:57:36 -04002120 u64 last = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -04002121 u64 start;
2122 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -05002123 struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
Chris Masonccd467d2007-06-28 15:57:36 -04002124 int ret;
Chris Masonccd467d2007-06-28 15:57:36 -04002125
Chris Masond3977122009-01-05 21:25:51 -05002126 while (1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04002127 ret = find_first_extent_bit(pinned_extents, last,
2128 &start, &end, EXTENT_DIRTY);
2129 if (ret)
Chris Masonccd467d2007-06-28 15:57:36 -04002130 break;
Chris Mason1a5bc162007-10-15 16:15:26 -04002131 set_extent_dirty(copy, start, end, GFP_NOFS);
2132 last = end + 1;
Chris Masonccd467d2007-06-28 15:57:36 -04002133 }
2134 return 0;
2135}
2136
2137int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
2138 struct btrfs_root *root,
Chris Masond1310b22008-01-24 16:13:08 -05002139 struct extent_io_tree *unpin)
Chris Masona28ec192007-03-06 20:08:01 -05002140{
Chris Mason1a5bc162007-10-15 16:15:26 -04002141 u64 start;
2142 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05002143 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05002144
Chris Masond3977122009-01-05 21:25:51 -05002145 while (1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04002146 ret = find_first_extent_bit(unpin, 0, &start, &end,
2147 EXTENT_DIRTY);
2148 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05002149 break;
Liu Hui1f3c79a2009-01-05 15:57:51 -05002150
2151 ret = btrfs_discard_extent(root, start, end + 1 - start);
2152
Chris Masonb9473432009-03-13 11:00:37 -04002153 /* unlocks the pinned mutex */
Chris Masone02119d2008-09-05 16:13:11 -04002154 btrfs_update_pinned_extents(root, start, end + 1 - start, 0);
Chris Mason1a5bc162007-10-15 16:15:26 -04002155 clear_extent_dirty(unpin, start, end, GFP_NOFS);
Liu Hui1f3c79a2009-01-05 15:57:51 -05002156
Chris Masonb9473432009-03-13 11:00:37 -04002157 cond_resched();
Chris Masona28ec192007-03-06 20:08:01 -05002158 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05002159 return ret;
Chris Masona28ec192007-03-06 20:08:01 -05002160}
2161
Zheng Yan31840ae2008-09-23 13:14:14 -04002162static int pin_down_bytes(struct btrfs_trans_handle *trans,
2163 struct btrfs_root *root,
Chris Masonb9473432009-03-13 11:00:37 -04002164 struct btrfs_path *path,
2165 u64 bytenr, u64 num_bytes, int is_data,
2166 struct extent_buffer **must_clean)
Chris Masone20d96d2007-03-22 12:13:20 -04002167{
Chris Mason1a5bc162007-10-15 16:15:26 -04002168 int err = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04002169 struct extent_buffer *buf;
Chris Mason78fae272007-03-25 11:35:08 -04002170
Zheng Yan31840ae2008-09-23 13:14:14 -04002171 if (is_data)
2172 goto pinit;
Chris Mason4bef0842008-09-08 11:18:08 -04002173
Zheng Yan31840ae2008-09-23 13:14:14 -04002174 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
2175 if (!buf)
2176 goto pinit;
Chris Mason4bef0842008-09-08 11:18:08 -04002177
Zheng Yan31840ae2008-09-23 13:14:14 -04002178 /* we can reuse a block if it hasn't been written
2179 * and it is from this transaction. We can't
2180 * reuse anything from the tree log root because
2181 * it has tiny sub-transactions.
2182 */
2183 if (btrfs_buffer_uptodate(buf, 0) &&
2184 btrfs_try_tree_lock(buf)) {
2185 u64 header_owner = btrfs_header_owner(buf);
2186 u64 header_transid = btrfs_header_generation(buf);
2187 if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
Zheng Yan1a40e232008-09-26 10:09:34 -04002188 header_owner != BTRFS_TREE_RELOC_OBJECTID &&
Chris Mason56bec292009-03-13 10:10:06 -04002189 header_owner != BTRFS_DATA_RELOC_TREE_OBJECTID &&
Zheng Yan31840ae2008-09-23 13:14:14 -04002190 header_transid == trans->transid &&
2191 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
Chris Masonb9473432009-03-13 11:00:37 -04002192 *must_clean = buf;
Zheng Yan31840ae2008-09-23 13:14:14 -04002193 return 1;
Chris Mason8ef97622007-03-26 10:15:30 -04002194 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002195 btrfs_tree_unlock(buf);
Chris Masonf4b9aa82007-03-27 11:05:53 -04002196 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002197 free_extent_buffer(buf);
2198pinit:
Chris Masonb9473432009-03-13 11:00:37 -04002199 btrfs_set_path_blocking(path);
Chris Masonb9473432009-03-13 11:00:37 -04002200 /* unlocks the pinned mutex */
Zheng Yan31840ae2008-09-23 13:14:14 -04002201 btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
2202
Chris Masonbe744172007-05-06 10:15:01 -04002203 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -04002204 return 0;
2205}
2206
Chris Masona28ec192007-03-06 20:08:01 -05002207/*
2208 * remove an extent from the root, returns 0 on success
2209 */
Zheng Yan31840ae2008-09-23 13:14:14 -04002210static int __free_extent(struct btrfs_trans_handle *trans,
2211 struct btrfs_root *root,
2212 u64 bytenr, u64 num_bytes, u64 parent,
Chris Mason7bb86312007-12-11 09:25:06 -05002213 u64 root_objectid, u64 ref_generation,
Chris Mason56bec292009-03-13 10:10:06 -04002214 u64 owner_objectid, int pin, int mark_free,
2215 int refs_to_drop)
Chris Masona28ec192007-03-06 20:08:01 -05002216{
Chris Mason5caf2a02007-04-02 11:20:42 -04002217 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -04002218 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -04002219 struct btrfs_fs_info *info = root->fs_info;
2220 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04002221 struct extent_buffer *leaf;
Chris Masona28ec192007-03-06 20:08:01 -05002222 int ret;
Chris Mason952fcca2008-02-18 16:33:44 -05002223 int extent_slot = 0;
2224 int found_extent = 0;
2225 int num_to_del = 1;
Chris Mason234b63a2007-03-13 10:46:10 -04002226 struct btrfs_extent_item *ei;
Chris Masoncf27e1e2007-03-13 09:49:06 -04002227 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -05002228
Chris Masondb945352007-10-15 16:15:53 -04002229 key.objectid = bytenr;
Chris Mason62e27492007-03-15 12:56:47 -04002230 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masondb945352007-10-15 16:15:53 -04002231 key.offset = num_bytes;
Chris Mason5caf2a02007-04-02 11:20:42 -04002232 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002233 if (!path)
2234 return -ENOMEM;
2235
Chris Mason3c12ac72008-04-21 12:01:38 -04002236 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04002237 path->leave_spinning = 1;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002238 ret = lookup_extent_backref(trans, extent_root, path,
2239 bytenr, parent, root_objectid,
2240 ref_generation, owner_objectid, 1);
Chris Mason7bb86312007-12-11 09:25:06 -05002241 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05002242 struct btrfs_key found_key;
2243 extent_slot = path->slots[0];
Chris Masond3977122009-01-05 21:25:51 -05002244 while (extent_slot > 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05002245 extent_slot--;
2246 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2247 extent_slot);
2248 if (found_key.objectid != bytenr)
2249 break;
2250 if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
2251 found_key.offset == num_bytes) {
2252 found_extent = 1;
2253 break;
2254 }
2255 if (path->slots[0] - extent_slot > 5)
2256 break;
2257 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002258 if (!found_extent) {
Chris Mason56bec292009-03-13 10:10:06 -04002259 ret = remove_extent_backref(trans, extent_root, path,
2260 refs_to_drop);
Zheng Yan31840ae2008-09-23 13:14:14 -04002261 BUG_ON(ret);
2262 btrfs_release_path(extent_root, path);
Chris Masonb9473432009-03-13 11:00:37 -04002263 path->leave_spinning = 1;
Zheng Yan31840ae2008-09-23 13:14:14 -04002264 ret = btrfs_search_slot(trans, extent_root,
2265 &key, path, -1, 1);
Josef Bacikf3465ca2008-11-12 14:19:50 -05002266 if (ret) {
2267 printk(KERN_ERR "umm, got %d back from search"
Chris Masond3977122009-01-05 21:25:51 -05002268 ", was looking for %llu\n", ret,
2269 (unsigned long long)bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05002270 btrfs_print_leaf(extent_root, path->nodes[0]);
2271 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002272 BUG_ON(ret);
2273 extent_slot = path->slots[0];
2274 }
Chris Mason7bb86312007-12-11 09:25:06 -05002275 } else {
2276 btrfs_print_leaf(extent_root, path->nodes[0]);
2277 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -05002278 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
Chris Mason56bec292009-03-13 10:10:06 -04002279 "parent %llu root %llu gen %llu owner %llu\n",
Chris Masond3977122009-01-05 21:25:51 -05002280 (unsigned long long)bytenr,
Chris Mason56bec292009-03-13 10:10:06 -04002281 (unsigned long long)parent,
Chris Masond3977122009-01-05 21:25:51 -05002282 (unsigned long long)root_objectid,
2283 (unsigned long long)ref_generation,
2284 (unsigned long long)owner_objectid);
Chris Mason7bb86312007-12-11 09:25:06 -05002285 }
Chris Mason5f39d392007-10-15 16:14:19 -04002286
2287 leaf = path->nodes[0];
Chris Mason952fcca2008-02-18 16:33:44 -05002288 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04002289 struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002290 refs = btrfs_extent_refs(leaf, ei);
Chris Mason952fcca2008-02-18 16:33:44 -05002291
Chris Mason56bec292009-03-13 10:10:06 -04002292 /*
2293 * we're not allowed to delete the extent item if there
2294 * are other delayed ref updates pending
2295 */
2296
2297 BUG_ON(refs < refs_to_drop);
2298 refs -= refs_to_drop;
2299 btrfs_set_extent_refs(leaf, ei, refs);
Chris Mason5f39d392007-10-15 16:14:19 -04002300 btrfs_mark_buffer_dirty(leaf);
2301
Chris Mason56bec292009-03-13 10:10:06 -04002302 if (refs == 0 && found_extent &&
2303 path->slots[0] == extent_slot + 1) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002304 struct btrfs_extent_ref *ref;
2305 ref = btrfs_item_ptr(leaf, path->slots[0],
2306 struct btrfs_extent_ref);
Chris Mason56bec292009-03-13 10:10:06 -04002307 BUG_ON(btrfs_ref_num_refs(leaf, ref) != refs_to_drop);
Chris Mason952fcca2008-02-18 16:33:44 -05002308 /* if the back ref and the extent are next to each other
2309 * they get deleted below in one shot
2310 */
2311 path->slots[0] = extent_slot;
2312 num_to_del = 2;
2313 } else if (found_extent) {
2314 /* otherwise delete the extent back ref */
Chris Mason56bec292009-03-13 10:10:06 -04002315 ret = remove_extent_backref(trans, extent_root, path,
2316 refs_to_drop);
Chris Mason952fcca2008-02-18 16:33:44 -05002317 BUG_ON(ret);
2318 /* if refs are 0, we need to setup the path for deletion */
2319 if (refs == 0) {
2320 btrfs_release_path(extent_root, path);
Chris Masonb9473432009-03-13 11:00:37 -04002321 path->leave_spinning = 1;
Chris Mason952fcca2008-02-18 16:33:44 -05002322 ret = btrfs_search_slot(trans, extent_root, &key, path,
2323 -1, 1);
Chris Mason952fcca2008-02-18 16:33:44 -05002324 BUG_ON(ret);
2325 }
2326 }
2327
Chris Masoncf27e1e2007-03-13 09:49:06 -04002328 if (refs == 0) {
Chris Masondb945352007-10-15 16:15:53 -04002329 u64 super_used;
2330 u64 root_used;
Chris Masonb9473432009-03-13 11:00:37 -04002331 struct extent_buffer *must_clean = NULL;
Chris Mason78fae272007-03-25 11:35:08 -04002332
2333 if (pin) {
Chris Masonb9473432009-03-13 11:00:37 -04002334 ret = pin_down_bytes(trans, root, path,
2335 bytenr, num_bytes,
2336 owner_objectid >= BTRFS_FIRST_FREE_OBJECTID,
2337 &must_clean);
Yanc5492282007-11-06 10:25:25 -05002338 if (ret > 0)
2339 mark_free = 1;
2340 BUG_ON(ret < 0);
Chris Mason78fae272007-03-25 11:35:08 -04002341 }
Chris Masonb9473432009-03-13 11:00:37 -04002342
Josef Bacik58176a92007-08-29 15:47:34 -04002343 /* block accounting for super block */
Chris Mason75eff682008-12-15 15:54:40 -05002344 spin_lock(&info->delalloc_lock);
Chris Masondb945352007-10-15 16:15:53 -04002345 super_used = btrfs_super_bytes_used(&info->super_copy);
2346 btrfs_set_super_bytes_used(&info->super_copy,
2347 super_used - num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -04002348
2349 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04002350 root_used = btrfs_root_used(&root->root_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002351 btrfs_set_root_used(&root->root_item,
Chris Masondb945352007-10-15 16:15:53 -04002352 root_used - num_bytes);
Yan Zheng34bf63c2008-12-19 10:58:46 -05002353 spin_unlock(&info->delalloc_lock);
Chris Masonb9473432009-03-13 11:00:37 -04002354
2355 /*
2356 * it is going to be very rare for someone to be waiting
2357 * on the block we're freeing. del_items might need to
2358 * schedule, so rather than get fancy, just force it
2359 * to blocking here
2360 */
2361 if (must_clean)
2362 btrfs_set_lock_blocking(must_clean);
2363
Chris Mason952fcca2008-02-18 16:33:44 -05002364 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
2365 num_to_del);
Zheng Yan31840ae2008-09-23 13:14:14 -04002366 BUG_ON(ret);
Josef Bacik25179202008-10-29 14:49:05 -04002367 btrfs_release_path(extent_root, path);
David Woodhouse21af8042008-08-12 14:13:26 +01002368
Chris Masonb9473432009-03-13 11:00:37 -04002369 if (must_clean) {
2370 clean_tree_block(NULL, root, must_clean);
2371 btrfs_tree_unlock(must_clean);
2372 free_extent_buffer(must_clean);
2373 }
2374
Chris Mason459931e2008-12-10 09:10:46 -05002375 if (owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
2376 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
2377 BUG_ON(ret);
Chris Masond57e62b2009-03-31 13:47:50 -04002378 } else {
2379 invalidate_mapping_pages(info->btree_inode->i_mapping,
2380 bytenr >> PAGE_CACHE_SHIFT,
2381 (bytenr + num_bytes - 1) >> PAGE_CACHE_SHIFT);
Chris Mason459931e2008-12-10 09:10:46 -05002382 }
2383
Chris Masondcbdd4d2008-12-16 13:51:01 -05002384 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
2385 mark_free);
2386 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -05002387 }
Chris Mason5caf2a02007-04-02 11:20:42 -04002388 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05002389 return ret;
2390}
2391
2392/*
Chris Masonfec577f2007-02-26 10:40:21 -05002393 * remove an extent from the root, returns 0 on success
2394 */
Chris Mason925baed2008-06-25 16:01:30 -04002395static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
Chris Mason56bec292009-03-13 10:10:06 -04002396 struct btrfs_root *root,
2397 u64 bytenr, u64 num_bytes, u64 parent,
2398 u64 root_objectid, u64 ref_generation,
2399 u64 owner_objectid, int pin,
2400 int refs_to_drop)
Chris Masonfec577f2007-02-26 10:40:21 -05002401{
Chris Masondb945352007-10-15 16:15:53 -04002402 WARN_ON(num_bytes < root->sectorsize);
Josef Bacikf3465ca2008-11-12 14:19:50 -05002403
Chris Mason56bec292009-03-13 10:10:06 -04002404 /*
2405 * if metadata always pin
2406 * if data pin when any transaction has committed this
2407 */
2408 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID ||
2409 ref_generation != trans->transid)
Chris Mason4bef0842008-09-08 11:18:08 -04002410 pin = 1;
2411
Chris Mason4bef0842008-09-08 11:18:08 -04002412 if (ref_generation != trans->transid)
2413 pin = 1;
2414
Chris Mason56bec292009-03-13 10:10:06 -04002415 return __free_extent(trans, root, bytenr, num_bytes, parent,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002416 root_objectid, ref_generation,
Chris Mason56bec292009-03-13 10:10:06 -04002417 owner_objectid, pin, pin == 0, refs_to_drop);
Chris Masonfec577f2007-02-26 10:40:21 -05002418}
2419
Chris Mason1887be62009-03-13 10:11:24 -04002420/*
2421 * when we free an extent, it is possible (and likely) that we free the last
2422 * delayed ref for that extent as well. This searches the delayed ref tree for
2423 * a given extent, and if there are no other delayed refs to be processed, it
2424 * removes it from the tree.
2425 */
2426static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
2427 struct btrfs_root *root, u64 bytenr)
2428{
2429 struct btrfs_delayed_ref_head *head;
2430 struct btrfs_delayed_ref_root *delayed_refs;
2431 struct btrfs_delayed_ref_node *ref;
2432 struct rb_node *node;
2433 int ret;
2434
2435 delayed_refs = &trans->transaction->delayed_refs;
2436 spin_lock(&delayed_refs->lock);
2437 head = btrfs_find_delayed_ref_head(trans, bytenr);
2438 if (!head)
2439 goto out;
2440
2441 node = rb_prev(&head->node.rb_node);
2442 if (!node)
2443 goto out;
2444
2445 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2446
2447 /* there are still entries for this ref, we can't drop it */
2448 if (ref->bytenr == bytenr)
2449 goto out;
2450
2451 /*
2452 * waiting for the lock here would deadlock. If someone else has it
2453 * locked they are already in the process of dropping it anyway
2454 */
2455 if (!mutex_trylock(&head->mutex))
2456 goto out;
2457
2458 /*
2459 * at this point we have a head with no other entries. Go
2460 * ahead and process it.
2461 */
2462 head->node.in_tree = 0;
2463 rb_erase(&head->node.rb_node, &delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04002464
Chris Mason1887be62009-03-13 10:11:24 -04002465 delayed_refs->num_entries--;
2466
2467 /*
2468 * we don't take a ref on the node because we're removing it from the
2469 * tree, so we just steal the ref the tree was holding.
2470 */
Chris Masonc3e69d52009-03-13 10:17:05 -04002471 delayed_refs->num_heads--;
2472 if (list_empty(&head->cluster))
2473 delayed_refs->num_heads_ready--;
2474
2475 list_del_init(&head->cluster);
Chris Mason1887be62009-03-13 10:11:24 -04002476 spin_unlock(&delayed_refs->lock);
2477
2478 ret = run_one_delayed_ref(trans, root->fs_info->tree_root,
2479 &head->node, head->must_insert_reserved);
2480 BUG_ON(ret);
2481 btrfs_put_delayed_ref(&head->node);
2482 return 0;
2483out:
2484 spin_unlock(&delayed_refs->lock);
2485 return 0;
2486}
2487
Chris Mason925baed2008-06-25 16:01:30 -04002488int btrfs_free_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04002489 struct btrfs_root *root,
2490 u64 bytenr, u64 num_bytes, u64 parent,
2491 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002492 u64 owner_objectid, int pin)
Chris Mason925baed2008-06-25 16:01:30 -04002493{
2494 int ret;
2495
Chris Mason56bec292009-03-13 10:10:06 -04002496 /*
2497 * tree log blocks never actually go into the extent allocation
2498 * tree, just update pinning info and exit early.
2499 *
2500 * data extents referenced by the tree log do need to have
2501 * their reference counts bumped.
2502 */
2503 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID &&
2504 owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
Chris Masonb9473432009-03-13 11:00:37 -04002505 /* unlocks the pinned mutex */
Chris Mason56bec292009-03-13 10:10:06 -04002506 btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
Chris Mason56bec292009-03-13 10:10:06 -04002507 update_reserved_extents(root, bytenr, num_bytes, 0);
2508 ret = 0;
2509 } else {
2510 ret = btrfs_add_delayed_ref(trans, bytenr, num_bytes, parent,
2511 root_objectid, ref_generation,
2512 owner_objectid,
2513 BTRFS_DROP_DELAYED_REF, 1);
Chris Mason1887be62009-03-13 10:11:24 -04002514 BUG_ON(ret);
2515 ret = check_ref_cleanup(trans, root, bytenr);
2516 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04002517 }
Chris Mason925baed2008-06-25 16:01:30 -04002518 return ret;
2519}
2520
Chris Mason87ee04e2007-11-30 11:30:34 -05002521static u64 stripe_align(struct btrfs_root *root, u64 val)
2522{
2523 u64 mask = ((u64)root->stripesize - 1);
2524 u64 ret = (val + mask) & ~mask;
2525 return ret;
2526}
2527
Chris Masonfec577f2007-02-26 10:40:21 -05002528/*
2529 * walks the btree of allocated extents and find a hole of a given size.
2530 * The key ins is changed to record the hole:
2531 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04002532 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05002533 * ins->offset == number of blocks
2534 * Any available blocks before search_start are skipped.
2535 */
Chris Masond3977122009-01-05 21:25:51 -05002536static noinline int find_free_extent(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05002537 struct btrfs_root *orig_root,
2538 u64 num_bytes, u64 empty_size,
2539 u64 search_start, u64 search_end,
2540 u64 hint_byte, struct btrfs_key *ins,
2541 u64 exclude_start, u64 exclude_nr,
2542 int data)
Chris Masonfec577f2007-02-26 10:40:21 -05002543{
Josef Bacik80eb2342008-10-29 14:49:05 -04002544 int ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05002545 struct btrfs_root *root = orig_root->fs_info->extent_root;
Chris Mason239b14b2008-03-24 15:02:07 -04002546 u64 *last_ptr = NULL;
Josef Bacik80eb2342008-10-29 14:49:05 -04002547 struct btrfs_block_group_cache *block_group = NULL;
Chris Mason239b14b2008-03-24 15:02:07 -04002548 int empty_cluster = 2 * 1024 * 1024;
Chris Mason0ef3e662008-05-24 14:04:53 -04002549 int allowed_chunk_alloc = 0;
Josef Bacik2552d172009-04-03 10:14:19 -04002550 int using_hint = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04002551 struct btrfs_space_info *space_info;
Chris Masonfec577f2007-02-26 10:40:21 -05002552
Chris Masondb945352007-10-15 16:15:53 -04002553 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04002554 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
Josef Bacik80eb2342008-10-29 14:49:05 -04002555 ins->objectid = 0;
2556 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04002557
Josef Bacik2552d172009-04-03 10:14:19 -04002558 space_info = __find_space_info(root->fs_info, data);
2559
Chris Mason0ef3e662008-05-24 14:04:53 -04002560 if (orig_root->ref_cows || empty_size)
2561 allowed_chunk_alloc = 1;
2562
Chris Mason239b14b2008-03-24 15:02:07 -04002563 if (data & BTRFS_BLOCK_GROUP_METADATA) {
2564 last_ptr = &root->fs_info->last_alloc;
Chris Mason536ac8a2009-02-12 09:41:38 -05002565 if (!btrfs_test_opt(root, SSD))
2566 empty_cluster = 64 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04002567 }
2568
Josef Bacik0f9dd462008-09-23 13:14:11 -04002569 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD))
Chris Mason239b14b2008-03-24 15:02:07 -04002570 last_ptr = &root->fs_info->last_data_alloc;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002571
Chris Mason239b14b2008-03-24 15:02:07 -04002572 if (last_ptr) {
Josef Bacik2552d172009-04-03 10:14:19 -04002573 if (*last_ptr)
Chris Mason239b14b2008-03-24 15:02:07 -04002574 hint_byte = *last_ptr;
Josef Bacik2552d172009-04-03 10:14:19 -04002575 else
Chris Mason239b14b2008-03-24 15:02:07 -04002576 empty_size += empty_cluster;
Chris Mason43662112008-11-07 09:06:11 -05002577 } else {
2578 empty_cluster = 0;
Chris Mason239b14b2008-03-24 15:02:07 -04002579 }
Chris Masona061fc82008-05-07 11:43:44 -04002580 search_start = max(search_start, first_logical_byte(root, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04002581 search_start = max(search_start, hint_byte);
Chris Mason0b86a832008-03-24 15:01:56 -04002582
Josef Bacik2552d172009-04-03 10:14:19 -04002583 if (search_start == hint_byte) {
2584 using_hint = 1;
2585 block_group = btrfs_lookup_block_group(root->fs_info,
2586 search_start);
2587 if (block_group && block_group_bits(block_group, data)) {
Josef Bacik2552d172009-04-03 10:14:19 -04002588 down_read(&space_info->groups_sem);
2589 goto have_block_group;
2590 } else if (block_group) {
2591 put_block_group(block_group);
2592 }
2593
Chris Mason42e70e72008-11-07 18:17:11 -05002594 empty_size += empty_cluster;
Josef Bacik2552d172009-04-03 10:14:19 -04002595 using_hint = 0;
Chris Mason42e70e72008-11-07 18:17:11 -05002596 }
Chris Mason43662112008-11-07 09:06:11 -05002597
Josef Bacik2552d172009-04-03 10:14:19 -04002598search:
Josef Bacik80eb2342008-10-29 14:49:05 -04002599 down_read(&space_info->groups_sem);
Josef Bacik2552d172009-04-03 10:14:19 -04002600 list_for_each_entry(block_group, &space_info->block_groups, list) {
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002601 u64 offset;
Chris Mason8a1413a2008-11-10 16:13:54 -05002602
Josef Bacik2552d172009-04-03 10:14:19 -04002603 atomic_inc(&block_group->count);
2604 search_start = block_group->key.objectid;
Chris Mason42e70e72008-11-07 18:17:11 -05002605
Josef Bacik2552d172009-04-03 10:14:19 -04002606have_block_group:
Josef Bacikea6a4782008-11-20 12:16:16 -05002607 if (unlikely(!block_group->cached)) {
2608 mutex_lock(&block_group->cache_mutex);
2609 ret = cache_block_group(root, block_group);
2610 mutex_unlock(&block_group->cache_mutex);
Josef Bacik2552d172009-04-03 10:14:19 -04002611 if (ret) {
2612 put_block_group(block_group);
Josef Bacikea6a4782008-11-20 12:16:16 -05002613 break;
Josef Bacik2552d172009-04-03 10:14:19 -04002614 }
Josef Bacikea6a4782008-11-20 12:16:16 -05002615 }
2616
Josef Bacikea6a4782008-11-20 12:16:16 -05002617 if (unlikely(block_group->ro))
Josef Bacik2552d172009-04-03 10:14:19 -04002618 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002619
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002620 offset = btrfs_find_space_for_alloc(block_group, search_start,
2621 num_bytes, empty_size);
2622 if (!offset)
Josef Bacik2552d172009-04-03 10:14:19 -04002623 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002624
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002625 search_start = stripe_align(root, offset);
Chris Mason0b86a832008-03-24 15:01:56 -04002626
Josef Bacik2552d172009-04-03 10:14:19 -04002627 /* move on to the next group */
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002628 if (search_start + num_bytes >= search_end) {
2629 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04002630 goto loop;
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002631 }
Chris Masone37c9e62007-05-09 20:13:14 -04002632
Josef Bacik2552d172009-04-03 10:14:19 -04002633 /* move on to the next group */
2634 if (search_start + num_bytes >
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002635 block_group->key.objectid + block_group->key.offset) {
2636 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04002637 goto loop;
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002638 }
Josef Bacik80eb2342008-10-29 14:49:05 -04002639
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002640 if (using_hint && search_start > hint_byte) {
2641 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04002642 goto loop;
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002643 }
Chris Mason3b7885b2008-11-06 21:48:27 -05002644
Josef Bacik2552d172009-04-03 10:14:19 -04002645 if (exclude_nr > 0 &&
2646 (search_start + num_bytes > exclude_start &&
2647 search_start < exclude_start + exclude_nr)) {
2648 search_start = exclude_start + exclude_nr;
2649
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002650 btrfs_add_free_space(block_group, offset, num_bytes);
Josef Bacik2552d172009-04-03 10:14:19 -04002651 /*
2652 * if search_start is still in this block group
2653 * then we just re-search this block group
2654 */
2655 if (search_start >= block_group->key.objectid &&
2656 search_start < (block_group->key.objectid +
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002657 block_group->key.offset))
Josef Bacik2552d172009-04-03 10:14:19 -04002658 goto have_block_group;
Josef Bacik2552d172009-04-03 10:14:19 -04002659 goto loop;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002660 }
Josef Bacik2552d172009-04-03 10:14:19 -04002661
2662 ins->objectid = search_start;
2663 ins->offset = num_bytes;
2664
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002665 if (offset < search_start)
2666 btrfs_add_free_space(block_group, offset,
2667 search_start - offset);
2668 BUG_ON(offset > search_start);
2669
Josef Bacik2552d172009-04-03 10:14:19 -04002670 /* we are all good, lets return */
Josef Bacik2552d172009-04-03 10:14:19 -04002671 break;
2672loop:
Yan Zhengd2fb3432008-12-11 16:30:39 -05002673 put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04002674 if (using_hint) {
2675 empty_size += empty_cluster;
Josef Bacik2552d172009-04-03 10:14:19 -04002676 using_hint = 0;
2677 up_read(&space_info->groups_sem);
2678 goto search;
2679 }
2680 }
2681 up_read(&space_info->groups_sem);
Chris Masonf5a31e12008-11-10 11:47:09 -05002682
Josef Bacik2552d172009-04-03 10:14:19 -04002683 if (!ins->objectid && (empty_size || allowed_chunk_alloc)) {
2684 int try_again = empty_size;
Chris Mason42e70e72008-11-07 18:17:11 -05002685
Josef Bacik2552d172009-04-03 10:14:19 -04002686 empty_size = 0;
Chris Mason42e70e72008-11-07 18:17:11 -05002687
Josef Bacik2552d172009-04-03 10:14:19 -04002688 if (allowed_chunk_alloc) {
2689 ret = do_chunk_alloc(trans, root, num_bytes +
2690 2 * 1024 * 1024, data, 1);
2691 if (!ret)
2692 try_again = 1;
2693 allowed_chunk_alloc = 0;
2694 } else {
2695 space_info->force_alloc = 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002696 }
Josef Bacik80eb2342008-10-29 14:49:05 -04002697
Josef Bacik2552d172009-04-03 10:14:19 -04002698 if (try_again)
2699 goto search;
2700 ret = -ENOSPC;
2701 } else if (!ins->objectid) {
2702 ret = -ENOSPC;
Chris Masone19caa52007-10-15 16:17:44 -04002703 }
Chris Mason0b86a832008-03-24 15:01:56 -04002704
Josef Bacik80eb2342008-10-29 14:49:05 -04002705 /* we found what we needed */
2706 if (ins->objectid) {
2707 if (!(data & BTRFS_BLOCK_GROUP_DATA))
Yan Zhengd2fb3432008-12-11 16:30:39 -05002708 trans->block_group = block_group->key.objectid;
Josef Bacik80eb2342008-10-29 14:49:05 -04002709
2710 if (last_ptr)
2711 *last_ptr = ins->objectid + ins->offset;
Yan Zhengd2fb3432008-12-11 16:30:39 -05002712 put_block_group(block_group);
Josef Bacik2552d172009-04-03 10:14:19 -04002713 ret = 0;
2714 }
Chris Mason0b86a832008-03-24 15:01:56 -04002715
Chris Mason0f70abe2007-02-28 16:46:22 -05002716 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05002717}
Chris Masonec44a352008-04-28 15:29:52 -04002718
Josef Bacik0f9dd462008-09-23 13:14:11 -04002719static void dump_space_info(struct btrfs_space_info *info, u64 bytes)
2720{
2721 struct btrfs_block_group_cache *cache;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002722
Chris Masond3977122009-01-05 21:25:51 -05002723 printk(KERN_INFO "space_info has %llu free, is %sfull\n",
2724 (unsigned long long)(info->total_bytes - info->bytes_used -
2725 info->bytes_pinned - info->bytes_reserved),
2726 (info->full) ? "" : "not ");
Josef Bacik6a632092009-02-20 11:00:09 -05002727 printk(KERN_INFO "space_info total=%llu, pinned=%llu, delalloc=%llu,"
2728 " may_use=%llu, used=%llu\n", info->total_bytes,
2729 info->bytes_pinned, info->bytes_delalloc, info->bytes_may_use,
2730 info->bytes_used);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002731
Josef Bacik80eb2342008-10-29 14:49:05 -04002732 down_read(&info->groups_sem);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002733 list_for_each_entry(cache, &info->block_groups, list) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04002734 spin_lock(&cache->lock);
Chris Masond3977122009-01-05 21:25:51 -05002735 printk(KERN_INFO "block group %llu has %llu bytes, %llu used "
2736 "%llu pinned %llu reserved\n",
2737 (unsigned long long)cache->key.objectid,
2738 (unsigned long long)cache->key.offset,
2739 (unsigned long long)btrfs_block_group_used(&cache->item),
2740 (unsigned long long)cache->pinned,
2741 (unsigned long long)cache->reserved);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002742 btrfs_dump_free_space(cache, bytes);
2743 spin_unlock(&cache->lock);
2744 }
Josef Bacik80eb2342008-10-29 14:49:05 -04002745 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002746}
Zheng Yane8569812008-09-26 10:05:48 -04002747
Chris Masone6dcd2d2008-07-17 12:53:50 -04002748static int __btrfs_reserve_extent(struct btrfs_trans_handle *trans,
2749 struct btrfs_root *root,
2750 u64 num_bytes, u64 min_alloc_size,
2751 u64 empty_size, u64 hint_byte,
2752 u64 search_end, struct btrfs_key *ins,
2753 u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05002754{
2755 int ret;
Chris Masonfbdc7622007-05-30 10:22:12 -04002756 u64 search_start = 0;
Chris Mason1261ec42007-03-20 20:35:03 -04002757 struct btrfs_fs_info *info = root->fs_info;
Chris Mason925baed2008-06-25 16:01:30 -04002758
Josef Bacik6a632092009-02-20 11:00:09 -05002759 data = btrfs_get_alloc_profile(root, data);
Chris Mason98d20f62008-04-14 09:46:10 -04002760again:
Chris Mason0ef3e662008-05-24 14:04:53 -04002761 /*
2762 * the only place that sets empty_size is btrfs_realloc_node, which
2763 * is not called recursively on allocations
2764 */
2765 if (empty_size || root->ref_cows) {
Chris Mason593060d2008-03-25 16:50:33 -04002766 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
Chris Mason6324fbf2008-03-24 15:01:59 -04002767 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04002768 2 * 1024 * 1024,
2769 BTRFS_BLOCK_GROUP_METADATA |
2770 (info->metadata_alloc_profile &
2771 info->avail_metadata_alloc_bits), 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04002772 }
2773 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04002774 num_bytes + 2 * 1024 * 1024, data, 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04002775 }
Chris Mason0b86a832008-03-24 15:01:56 -04002776
Chris Masondb945352007-10-15 16:15:53 -04002777 WARN_ON(num_bytes < root->sectorsize);
2778 ret = find_free_extent(trans, root, num_bytes, empty_size,
2779 search_start, search_end, hint_byte, ins,
Chris Mason26b80032007-08-08 20:17:12 -04002780 trans->alloc_exclude_start,
2781 trans->alloc_exclude_nr, data);
Chris Mason3b951512008-04-17 11:29:12 -04002782
Chris Mason98d20f62008-04-14 09:46:10 -04002783 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
2784 num_bytes = num_bytes >> 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002785 num_bytes = num_bytes & ~(root->sectorsize - 1);
Chris Mason98d20f62008-04-14 09:46:10 -04002786 num_bytes = max(num_bytes, min_alloc_size);
Chris Mason0ef3e662008-05-24 14:04:53 -04002787 do_chunk_alloc(trans, root->fs_info->extent_root,
2788 num_bytes, data, 1);
Chris Mason98d20f62008-04-14 09:46:10 -04002789 goto again;
2790 }
Chris Masonec44a352008-04-28 15:29:52 -04002791 if (ret) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04002792 struct btrfs_space_info *sinfo;
2793
2794 sinfo = __find_space_info(root->fs_info, data);
Chris Masond3977122009-01-05 21:25:51 -05002795 printk(KERN_ERR "btrfs allocation failed flags %llu, "
2796 "wanted %llu\n", (unsigned long long)data,
2797 (unsigned long long)num_bytes);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002798 dump_space_info(sinfo, num_bytes);
Chris Mason925baed2008-06-25 16:01:30 -04002799 BUG();
Chris Mason925baed2008-06-25 16:01:30 -04002800 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002801
2802 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002803}
2804
Chris Mason65b51a02008-08-01 15:11:20 -04002805int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
2806{
Josef Bacik0f9dd462008-09-23 13:14:11 -04002807 struct btrfs_block_group_cache *cache;
Liu Hui1f3c79a2009-01-05 15:57:51 -05002808 int ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002809
Josef Bacik0f9dd462008-09-23 13:14:11 -04002810 cache = btrfs_lookup_block_group(root->fs_info, start);
2811 if (!cache) {
Chris Masond3977122009-01-05 21:25:51 -05002812 printk(KERN_ERR "Unable to find block group for %llu\n",
2813 (unsigned long long)start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002814 return -ENOSPC;
2815 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05002816
2817 ret = btrfs_discard_extent(root, start, len);
2818
Josef Bacik0f9dd462008-09-23 13:14:11 -04002819 btrfs_add_free_space(cache, start, len);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002820 put_block_group(cache);
Zheng Yan1a40e232008-09-26 10:09:34 -04002821 update_reserved_extents(root, start, len, 0);
Liu Hui1f3c79a2009-01-05 15:57:51 -05002822
2823 return ret;
Chris Mason65b51a02008-08-01 15:11:20 -04002824}
2825
Chris Masone6dcd2d2008-07-17 12:53:50 -04002826int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
2827 struct btrfs_root *root,
2828 u64 num_bytes, u64 min_alloc_size,
2829 u64 empty_size, u64 hint_byte,
2830 u64 search_end, struct btrfs_key *ins,
2831 u64 data)
2832{
2833 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002834 ret = __btrfs_reserve_extent(trans, root, num_bytes, min_alloc_size,
2835 empty_size, hint_byte, search_end, ins,
2836 data);
Zheng Yane8569812008-09-26 10:05:48 -04002837 update_reserved_extents(root, ins->objectid, ins->offset, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002838 return ret;
2839}
2840
2841static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04002842 struct btrfs_root *root, u64 parent,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002843 u64 root_objectid, u64 ref_generation,
Chris Mason56bec292009-03-13 10:10:06 -04002844 u64 owner, struct btrfs_key *ins,
2845 int ref_mod)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002846{
2847 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002848 u64 super_used;
2849 u64 root_used;
2850 u64 num_bytes = ins->offset;
2851 u32 sizes[2];
2852 struct btrfs_fs_info *info = root->fs_info;
2853 struct btrfs_root *extent_root = info->extent_root;
2854 struct btrfs_extent_item *extent_item;
2855 struct btrfs_extent_ref *ref;
2856 struct btrfs_path *path;
2857 struct btrfs_key keys[2];
Chris Masonf2654de2007-06-26 12:20:46 -04002858
Zheng Yan31840ae2008-09-23 13:14:14 -04002859 if (parent == 0)
2860 parent = ins->objectid;
2861
Josef Bacik58176a92007-08-29 15:47:34 -04002862 /* block accounting for super block */
Chris Mason75eff682008-12-15 15:54:40 -05002863 spin_lock(&info->delalloc_lock);
Chris Masondb945352007-10-15 16:15:53 -04002864 super_used = btrfs_super_bytes_used(&info->super_copy);
2865 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
Chris Mason26b80032007-08-08 20:17:12 -04002866
Josef Bacik58176a92007-08-29 15:47:34 -04002867 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04002868 root_used = btrfs_root_used(&root->root_item);
2869 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
Yan Zheng34bf63c2008-12-19 10:58:46 -05002870 spin_unlock(&info->delalloc_lock);
Josef Bacik58176a92007-08-29 15:47:34 -04002871
Chris Mason47e4bb92008-02-01 14:51:59 -05002872 memcpy(&keys[0], ins, sizeof(*ins));
Chris Mason47e4bb92008-02-01 14:51:59 -05002873 keys[1].objectid = ins->objectid;
2874 keys[1].type = BTRFS_EXTENT_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04002875 keys[1].offset = parent;
Chris Mason47e4bb92008-02-01 14:51:59 -05002876 sizes[0] = sizeof(*extent_item);
2877 sizes[1] = sizeof(*ref);
Chris Mason7bb86312007-12-11 09:25:06 -05002878
2879 path = btrfs_alloc_path();
2880 BUG_ON(!path);
Chris Mason47e4bb92008-02-01 14:51:59 -05002881
Chris Masonb9473432009-03-13 11:00:37 -04002882 path->leave_spinning = 1;
Chris Mason47e4bb92008-02-01 14:51:59 -05002883 ret = btrfs_insert_empty_items(trans, extent_root, path, keys,
2884 sizes, 2);
Chris Masonccd467d2007-06-28 15:57:36 -04002885 BUG_ON(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002886
Chris Mason47e4bb92008-02-01 14:51:59 -05002887 extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2888 struct btrfs_extent_item);
Chris Mason56bec292009-03-13 10:10:06 -04002889 btrfs_set_extent_refs(path->nodes[0], extent_item, ref_mod);
Chris Mason47e4bb92008-02-01 14:51:59 -05002890 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
2891 struct btrfs_extent_ref);
2892
2893 btrfs_set_ref_root(path->nodes[0], ref, root_objectid);
2894 btrfs_set_ref_generation(path->nodes[0], ref, ref_generation);
2895 btrfs_set_ref_objectid(path->nodes[0], ref, owner);
Chris Mason56bec292009-03-13 10:10:06 -04002896 btrfs_set_ref_num_refs(path->nodes[0], ref, ref_mod);
Chris Mason47e4bb92008-02-01 14:51:59 -05002897
2898 btrfs_mark_buffer_dirty(path->nodes[0]);
2899
2900 trans->alloc_exclude_start = 0;
2901 trans->alloc_exclude_nr = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05002902 btrfs_free_path(path);
Chris Masonf510cfe2007-10-15 16:14:48 -04002903
Chris Mason925baed2008-06-25 16:01:30 -04002904 if (ret)
2905 goto out;
Chris Mason26b80032007-08-08 20:17:12 -04002906
Chris Masond3977122009-01-05 21:25:51 -05002907 ret = update_block_group(trans, root, ins->objectid,
2908 ins->offset, 1, 0);
Chris Masonf5947062008-02-04 10:10:13 -05002909 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05002910 printk(KERN_ERR "btrfs update block group failed for %llu "
2911 "%llu\n", (unsigned long long)ins->objectid,
2912 (unsigned long long)ins->offset);
Chris Masonf5947062008-02-04 10:10:13 -05002913 BUG();
2914 }
Chris Mason925baed2008-06-25 16:01:30 -04002915out:
Chris Masone6dcd2d2008-07-17 12:53:50 -04002916 return ret;
2917}
2918
2919int btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04002920 struct btrfs_root *root, u64 parent,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002921 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002922 u64 owner, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002923{
2924 int ret;
Chris Mason1c2308f2008-09-23 13:14:13 -04002925
2926 if (root_objectid == BTRFS_TREE_LOG_OBJECTID)
2927 return 0;
Chris Mason56bec292009-03-13 10:10:06 -04002928
2929 ret = btrfs_add_delayed_ref(trans, ins->objectid,
2930 ins->offset, parent, root_objectid,
2931 ref_generation, owner,
2932 BTRFS_ADD_DELAYED_EXTENT, 0);
2933 BUG_ON(ret);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002934 return ret;
2935}
Chris Masone02119d2008-09-05 16:13:11 -04002936
2937/*
2938 * this is used by the tree logging recovery code. It records that
2939 * an extent has been allocated and makes sure to clear the free
2940 * space cache bits as well
2941 */
2942int btrfs_alloc_logged_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04002943 struct btrfs_root *root, u64 parent,
Chris Masone02119d2008-09-05 16:13:11 -04002944 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002945 u64 owner, struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04002946{
2947 int ret;
2948 struct btrfs_block_group_cache *block_group;
2949
Chris Masone02119d2008-09-05 16:13:11 -04002950 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
Josef Bacikea6a4782008-11-20 12:16:16 -05002951 mutex_lock(&block_group->cache_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04002952 cache_block_group(root, block_group);
Josef Bacikea6a4782008-11-20 12:16:16 -05002953 mutex_unlock(&block_group->cache_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04002954
Josef Bacikea6a4782008-11-20 12:16:16 -05002955 ret = btrfs_remove_free_space(block_group, ins->objectid,
2956 ins->offset);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002957 BUG_ON(ret);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002958 put_block_group(block_group);
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002959 ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid,
Chris Mason56bec292009-03-13 10:10:06 -04002960 ref_generation, owner, ins, 1);
Chris Masone02119d2008-09-05 16:13:11 -04002961 return ret;
2962}
2963
Chris Masone6dcd2d2008-07-17 12:53:50 -04002964/*
2965 * finds a free extent and does all the dirty work required for allocation
2966 * returns the key for the extent through ins, and a tree buffer for
2967 * the first block of the extent through buf.
2968 *
2969 * returns 0 if everything worked, non-zero otherwise.
2970 */
2971int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
2972 struct btrfs_root *root,
Zheng Yan31840ae2008-09-23 13:14:14 -04002973 u64 num_bytes, u64 parent, u64 min_alloc_size,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002974 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002975 u64 owner_objectid, u64 empty_size, u64 hint_byte,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002976 u64 search_end, struct btrfs_key *ins, u64 data)
2977{
2978 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002979 ret = __btrfs_reserve_extent(trans, root, num_bytes,
2980 min_alloc_size, empty_size, hint_byte,
2981 search_end, ins, data);
2982 BUG_ON(ret);
Chris Masond00aff02008-09-11 15:54:42 -04002983 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
Chris Mason56bec292009-03-13 10:10:06 -04002984 ret = btrfs_add_delayed_ref(trans, ins->objectid,
2985 ins->offset, parent, root_objectid,
2986 ref_generation, owner_objectid,
2987 BTRFS_ADD_DELAYED_EXTENT, 0);
Chris Masond00aff02008-09-11 15:54:42 -04002988 BUG_ON(ret);
Chris Masond00aff02008-09-11 15:54:42 -04002989 }
Chris Mason56bec292009-03-13 10:10:06 -04002990 update_reserved_extents(root, ins->objectid, ins->offset, 1);
Chris Mason925baed2008-06-25 16:01:30 -04002991 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05002992}
Chris Mason65b51a02008-08-01 15:11:20 -04002993
2994struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
2995 struct btrfs_root *root,
Chris Mason4008c042009-02-12 14:09:45 -05002996 u64 bytenr, u32 blocksize,
2997 int level)
Chris Mason65b51a02008-08-01 15:11:20 -04002998{
2999 struct extent_buffer *buf;
3000
3001 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
3002 if (!buf)
3003 return ERR_PTR(-ENOMEM);
3004 btrfs_set_header_generation(buf, trans->transid);
Chris Mason4008c042009-02-12 14:09:45 -05003005 btrfs_set_buffer_lockdep_class(buf, level);
Chris Mason65b51a02008-08-01 15:11:20 -04003006 btrfs_tree_lock(buf);
3007 clean_tree_block(trans, root, buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05003008
3009 btrfs_set_lock_blocking(buf);
Chris Mason65b51a02008-08-01 15:11:20 -04003010 btrfs_set_buffer_uptodate(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05003011
Chris Masond0c803c2008-09-11 16:17:57 -04003012 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
3013 set_extent_dirty(&root->dirty_log_pages, buf->start,
Chris Mason65b51a02008-08-01 15:11:20 -04003014 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04003015 } else {
3016 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
3017 buf->start + buf->len - 1, GFP_NOFS);
3018 }
Chris Mason65b51a02008-08-01 15:11:20 -04003019 trans->blocks_used++;
Chris Masonb4ce94d2009-02-04 09:25:08 -05003020 /* this returns a buffer locked for blocking */
Chris Mason65b51a02008-08-01 15:11:20 -04003021 return buf;
3022}
3023
Chris Masonfec577f2007-02-26 10:40:21 -05003024/*
3025 * helper function to allocate a block for a given tree
3026 * returns the tree buffer or NULL.
3027 */
Chris Mason5f39d392007-10-15 16:14:19 -04003028struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Masondb945352007-10-15 16:15:53 -04003029 struct btrfs_root *root,
Zheng Yan31840ae2008-09-23 13:14:14 -04003030 u32 blocksize, u64 parent,
Chris Mason7bb86312007-12-11 09:25:06 -05003031 u64 root_objectid,
3032 u64 ref_generation,
Chris Mason7bb86312007-12-11 09:25:06 -05003033 int level,
3034 u64 hint,
Chris Mason5f39d392007-10-15 16:14:19 -04003035 u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05003036{
Chris Masone2fa7222007-03-12 16:22:34 -04003037 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05003038 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04003039 struct extent_buffer *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05003040
Zheng Yan31840ae2008-09-23 13:14:14 -04003041 ret = btrfs_alloc_extent(trans, root, blocksize, parent, blocksize,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003042 root_objectid, ref_generation, level,
Zheng Yan31840ae2008-09-23 13:14:14 -04003043 empty_size, hint, (u64)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05003044 if (ret) {
Chris Mason54aa1f42007-06-22 14:16:25 -04003045 BUG_ON(ret > 0);
3046 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05003047 }
Chris Mason55c69072008-01-09 15:55:33 -05003048
Chris Mason4008c042009-02-12 14:09:45 -05003049 buf = btrfs_init_new_buffer(trans, root, ins.objectid,
3050 blocksize, level);
Chris Masonfec577f2007-02-26 10:40:21 -05003051 return buf;
3052}
Chris Masona28ec192007-03-06 20:08:01 -05003053
Chris Masone02119d2008-09-05 16:13:11 -04003054int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans,
3055 struct btrfs_root *root, struct extent_buffer *leaf)
Chris Mason6407bf62007-03-27 06:33:00 -04003056{
Chris Mason7bb86312007-12-11 09:25:06 -05003057 u64 leaf_owner;
3058 u64 leaf_generation;
Chris Masonbd56b302009-02-04 09:27:02 -05003059 struct refsort *sorted;
Chris Mason5f39d392007-10-15 16:14:19 -04003060 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04003061 struct btrfs_file_extent_item *fi;
3062 int i;
3063 int nritems;
3064 int ret;
Chris Masonbd56b302009-02-04 09:27:02 -05003065 int refi = 0;
3066 int slot;
Chris Mason6407bf62007-03-27 06:33:00 -04003067
Chris Mason5f39d392007-10-15 16:14:19 -04003068 BUG_ON(!btrfs_is_leaf(leaf));
3069 nritems = btrfs_header_nritems(leaf);
Chris Mason7bb86312007-12-11 09:25:06 -05003070 leaf_owner = btrfs_header_owner(leaf);
3071 leaf_generation = btrfs_header_generation(leaf);
3072
Chris Masonbd56b302009-02-04 09:27:02 -05003073 sorted = kmalloc(sizeof(*sorted) * nritems, GFP_NOFS);
3074 /* we do this loop twice. The first time we build a list
3075 * of the extents we have a reference on, then we sort the list
3076 * by bytenr. The second time around we actually do the
3077 * extent freeing.
3078 */
Chris Mason6407bf62007-03-27 06:33:00 -04003079 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04003080 u64 disk_bytenr;
Chris Masone34a5b42008-07-22 12:08:37 -04003081 cond_resched();
Chris Mason5f39d392007-10-15 16:14:19 -04003082
3083 btrfs_item_key_to_cpu(leaf, &key, i);
Chris Masonbd56b302009-02-04 09:27:02 -05003084
3085 /* only extents have references, skip everything else */
Chris Mason5f39d392007-10-15 16:14:19 -04003086 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -04003087 continue;
Chris Masonbd56b302009-02-04 09:27:02 -05003088
Chris Mason6407bf62007-03-27 06:33:00 -04003089 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Masonbd56b302009-02-04 09:27:02 -05003090
3091 /* inline extents live in the btree, they don't have refs */
Chris Mason5f39d392007-10-15 16:14:19 -04003092 if (btrfs_file_extent_type(leaf, fi) ==
3093 BTRFS_FILE_EXTENT_INLINE)
Chris Mason236454df2007-04-19 13:37:44 -04003094 continue;
Chris Masonbd56b302009-02-04 09:27:02 -05003095
Chris Masondb945352007-10-15 16:15:53 -04003096 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Masonbd56b302009-02-04 09:27:02 -05003097
3098 /* holes don't have refs */
Chris Masondb945352007-10-15 16:15:53 -04003099 if (disk_bytenr == 0)
Chris Mason3a686372007-05-24 13:35:57 -04003100 continue;
Chris Mason4a096752008-07-21 10:29:44 -04003101
Chris Masonbd56b302009-02-04 09:27:02 -05003102 sorted[refi].bytenr = disk_bytenr;
3103 sorted[refi].slot = i;
3104 refi++;
3105 }
3106
3107 if (refi == 0)
3108 goto out;
3109
3110 sort(sorted, refi, sizeof(struct refsort), refsort_cmp, NULL);
3111
3112 for (i = 0; i < refi; i++) {
3113 u64 disk_bytenr;
3114
3115 disk_bytenr = sorted[i].bytenr;
3116 slot = sorted[i].slot;
3117
3118 cond_resched();
3119
3120 btrfs_item_key_to_cpu(leaf, &key, slot);
3121 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
3122 continue;
3123
3124 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
3125
Chris Mason56bec292009-03-13 10:10:06 -04003126 ret = btrfs_free_extent(trans, root, disk_bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -05003127 btrfs_file_extent_disk_num_bytes(leaf, fi),
Zheng Yan31840ae2008-09-23 13:14:14 -04003128 leaf->start, leaf_owner, leaf_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003129 key.objectid, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04003130 BUG_ON(ret);
Chris Mason2dd3e672008-08-04 08:20:15 -04003131
3132 atomic_inc(&root->fs_info->throttle_gen);
3133 wake_up(&root->fs_info->transaction_throttle);
3134 cond_resched();
Chris Mason6407bf62007-03-27 06:33:00 -04003135 }
Chris Masonbd56b302009-02-04 09:27:02 -05003136out:
3137 kfree(sorted);
Chris Mason6407bf62007-03-27 06:33:00 -04003138 return 0;
3139}
3140
Chris Masond3977122009-01-05 21:25:51 -05003141static noinline int cache_drop_leaf_ref(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04003142 struct btrfs_root *root,
3143 struct btrfs_leaf_ref *ref)
Yan Zheng31153d82008-07-28 15:32:19 -04003144{
3145 int i;
3146 int ret;
Chris Masonbd56b302009-02-04 09:27:02 -05003147 struct btrfs_extent_info *info;
3148 struct refsort *sorted;
Yan Zheng31153d82008-07-28 15:32:19 -04003149
Chris Masonbd56b302009-02-04 09:27:02 -05003150 if (ref->nritems == 0)
3151 return 0;
3152
3153 sorted = kmalloc(sizeof(*sorted) * ref->nritems, GFP_NOFS);
Yan Zheng31153d82008-07-28 15:32:19 -04003154 for (i = 0; i < ref->nritems; i++) {
Chris Masonbd56b302009-02-04 09:27:02 -05003155 sorted[i].bytenr = ref->extents[i].bytenr;
3156 sorted[i].slot = i;
3157 }
3158 sort(sorted, ref->nritems, sizeof(struct refsort), refsort_cmp, NULL);
3159
3160 /*
3161 * the items in the ref were sorted when the ref was inserted
3162 * into the ref cache, so this is already in order
3163 */
3164 for (i = 0; i < ref->nritems; i++) {
3165 info = ref->extents + sorted[i].slot;
Chris Mason56bec292009-03-13 10:10:06 -04003166 ret = btrfs_free_extent(trans, root, info->bytenr,
Zheng Yan31840ae2008-09-23 13:14:14 -04003167 info->num_bytes, ref->bytenr,
3168 ref->owner, ref->generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003169 info->objectid, 0);
Chris Mason2dd3e672008-08-04 08:20:15 -04003170
3171 atomic_inc(&root->fs_info->throttle_gen);
3172 wake_up(&root->fs_info->transaction_throttle);
3173 cond_resched();
3174
Yan Zheng31153d82008-07-28 15:32:19 -04003175 BUG_ON(ret);
3176 info++;
3177 }
Yan Zheng31153d82008-07-28 15:32:19 -04003178
Chris Mason806638b2009-02-05 09:08:14 -05003179 kfree(sorted);
Yan Zheng31153d82008-07-28 15:32:19 -04003180 return 0;
3181}
3182
Chris Mason56bec292009-03-13 10:10:06 -04003183static int drop_snap_lookup_refcount(struct btrfs_trans_handle *trans,
3184 struct btrfs_root *root, u64 start,
Chris Masond3977122009-01-05 21:25:51 -05003185 u64 len, u32 *refs)
Chris Mason333db942008-06-25 16:01:30 -04003186{
Chris Mason017e5362008-07-28 15:32:51 -04003187 int ret;
Chris Masonf87f0572008-08-01 11:27:23 -04003188
Chris Mason56bec292009-03-13 10:10:06 -04003189 ret = btrfs_lookup_extent_ref(trans, root, start, len, refs);
Chris Masonf87f0572008-08-01 11:27:23 -04003190 BUG_ON(ret);
3191
Chris Masond3977122009-01-05 21:25:51 -05003192#if 0 /* some debugging code in case we see problems here */
Chris Masonf87f0572008-08-01 11:27:23 -04003193 /* if the refs count is one, it won't get increased again. But
3194 * if the ref count is > 1, someone may be decreasing it at
3195 * the same time we are.
3196 */
3197 if (*refs != 1) {
3198 struct extent_buffer *eb = NULL;
3199 eb = btrfs_find_create_tree_block(root, start, len);
3200 if (eb)
3201 btrfs_tree_lock(eb);
3202
3203 mutex_lock(&root->fs_info->alloc_mutex);
3204 ret = lookup_extent_ref(NULL, root, start, len, refs);
3205 BUG_ON(ret);
3206 mutex_unlock(&root->fs_info->alloc_mutex);
3207
3208 if (eb) {
3209 btrfs_tree_unlock(eb);
3210 free_extent_buffer(eb);
3211 }
3212 if (*refs == 1) {
Chris Masond3977122009-01-05 21:25:51 -05003213 printk(KERN_ERR "btrfs block %llu went down to one "
3214 "during drop_snap\n", (unsigned long long)start);
Chris Masonf87f0572008-08-01 11:27:23 -04003215 }
3216
3217 }
3218#endif
3219
Chris Masone7a84562008-06-25 16:01:31 -04003220 cond_resched();
Chris Mason017e5362008-07-28 15:32:51 -04003221 return ret;
Chris Mason333db942008-06-25 16:01:30 -04003222}
3223
3224/*
Chris Masonbd56b302009-02-04 09:27:02 -05003225 * this is used while deleting old snapshots, and it drops the refs
3226 * on a whole subtree starting from a level 1 node.
3227 *
3228 * The idea is to sort all the leaf pointers, and then drop the
3229 * ref on all the leaves in order. Most of the time the leaves
3230 * will have ref cache entries, so no leaf IOs will be required to
3231 * find the extents they have references on.
3232 *
3233 * For each leaf, any references it has are also dropped in order
3234 *
3235 * This ends up dropping the references in something close to optimal
3236 * order for reading and modifying the extent allocation tree.
3237 */
3238static noinline int drop_level_one_refs(struct btrfs_trans_handle *trans,
3239 struct btrfs_root *root,
3240 struct btrfs_path *path)
3241{
3242 u64 bytenr;
3243 u64 root_owner;
3244 u64 root_gen;
3245 struct extent_buffer *eb = path->nodes[1];
3246 struct extent_buffer *leaf;
3247 struct btrfs_leaf_ref *ref;
3248 struct refsort *sorted = NULL;
3249 int nritems = btrfs_header_nritems(eb);
3250 int ret;
3251 int i;
3252 int refi = 0;
3253 int slot = path->slots[1];
3254 u32 blocksize = btrfs_level_size(root, 0);
3255 u32 refs;
3256
3257 if (nritems == 0)
3258 goto out;
3259
3260 root_owner = btrfs_header_owner(eb);
3261 root_gen = btrfs_header_generation(eb);
3262 sorted = kmalloc(sizeof(*sorted) * nritems, GFP_NOFS);
3263
3264 /*
3265 * step one, sort all the leaf pointers so we don't scribble
3266 * randomly into the extent allocation tree
3267 */
3268 for (i = slot; i < nritems; i++) {
3269 sorted[refi].bytenr = btrfs_node_blockptr(eb, i);
3270 sorted[refi].slot = i;
3271 refi++;
3272 }
3273
3274 /*
3275 * nritems won't be zero, but if we're picking up drop_snapshot
3276 * after a crash, slot might be > 0, so double check things
3277 * just in case.
3278 */
3279 if (refi == 0)
3280 goto out;
3281
3282 sort(sorted, refi, sizeof(struct refsort), refsort_cmp, NULL);
3283
3284 /*
3285 * the first loop frees everything the leaves point to
3286 */
3287 for (i = 0; i < refi; i++) {
3288 u64 ptr_gen;
3289
3290 bytenr = sorted[i].bytenr;
3291
3292 /*
3293 * check the reference count on this leaf. If it is > 1
3294 * we just decrement it below and don't update any
3295 * of the refs the leaf points to.
3296 */
Chris Mason56bec292009-03-13 10:10:06 -04003297 ret = drop_snap_lookup_refcount(trans, root, bytenr,
3298 blocksize, &refs);
Chris Masonbd56b302009-02-04 09:27:02 -05003299 BUG_ON(ret);
3300 if (refs != 1)
3301 continue;
3302
3303 ptr_gen = btrfs_node_ptr_generation(eb, sorted[i].slot);
3304
3305 /*
3306 * the leaf only had one reference, which means the
3307 * only thing pointing to this leaf is the snapshot
3308 * we're deleting. It isn't possible for the reference
3309 * count to increase again later
3310 *
3311 * The reference cache is checked for the leaf,
3312 * and if found we'll be able to drop any refs held by
3313 * the leaf without needing to read it in.
3314 */
3315 ref = btrfs_lookup_leaf_ref(root, bytenr);
3316 if (ref && ref->generation != ptr_gen) {
3317 btrfs_free_leaf_ref(root, ref);
3318 ref = NULL;
3319 }
3320 if (ref) {
3321 ret = cache_drop_leaf_ref(trans, root, ref);
3322 BUG_ON(ret);
3323 btrfs_remove_leaf_ref(root, ref);
3324 btrfs_free_leaf_ref(root, ref);
3325 } else {
3326 /*
3327 * the leaf wasn't in the reference cache, so
3328 * we have to read it.
3329 */
3330 leaf = read_tree_block(root, bytenr, blocksize,
3331 ptr_gen);
3332 ret = btrfs_drop_leaf_ref(trans, root, leaf);
3333 BUG_ON(ret);
3334 free_extent_buffer(leaf);
3335 }
3336 atomic_inc(&root->fs_info->throttle_gen);
3337 wake_up(&root->fs_info->transaction_throttle);
3338 cond_resched();
3339 }
3340
3341 /*
3342 * run through the loop again to free the refs on the leaves.
3343 * This is faster than doing it in the loop above because
3344 * the leaves are likely to be clustered together. We end up
3345 * working in nice chunks on the extent allocation tree.
3346 */
3347 for (i = 0; i < refi; i++) {
3348 bytenr = sorted[i].bytenr;
Chris Mason56bec292009-03-13 10:10:06 -04003349 ret = btrfs_free_extent(trans, root, bytenr,
Chris Masonbd56b302009-02-04 09:27:02 -05003350 blocksize, eb->start,
3351 root_owner, root_gen, 0, 1);
3352 BUG_ON(ret);
3353
3354 atomic_inc(&root->fs_info->throttle_gen);
3355 wake_up(&root->fs_info->transaction_throttle);
3356 cond_resched();
3357 }
3358out:
3359 kfree(sorted);
3360
3361 /*
3362 * update the path to show we've processed the entire level 1
3363 * node. This will get saved into the root's drop_snapshot_progress
3364 * field so these drops are not repeated again if this transaction
3365 * commits.
3366 */
3367 path->slots[1] = nritems;
3368 return 0;
3369}
3370
3371/*
Chris Mason9aca1d52007-03-13 11:09:37 -04003372 * helper function for drop_snapshot, this walks down the tree dropping ref
3373 * counts as it goes.
3374 */
Chris Masond3977122009-01-05 21:25:51 -05003375static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05003376 struct btrfs_root *root,
3377 struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05003378{
Chris Mason7bb86312007-12-11 09:25:06 -05003379 u64 root_owner;
3380 u64 root_gen;
3381 u64 bytenr;
Chris Masonca7a79a2008-05-12 12:59:19 -04003382 u64 ptr_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04003383 struct extent_buffer *next;
3384 struct extent_buffer *cur;
Chris Mason7bb86312007-12-11 09:25:06 -05003385 struct extent_buffer *parent;
Chris Masondb945352007-10-15 16:15:53 -04003386 u32 blocksize;
Chris Mason20524f02007-03-10 06:35:47 -05003387 int ret;
3388 u32 refs;
3389
Chris Mason5caf2a02007-04-02 11:20:42 -04003390 WARN_ON(*level < 0);
3391 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason56bec292009-03-13 10:10:06 -04003392 ret = drop_snap_lookup_refcount(trans, root, path->nodes[*level]->start,
Chris Masondb945352007-10-15 16:15:53 -04003393 path->nodes[*level]->len, &refs);
Chris Mason20524f02007-03-10 06:35:47 -05003394 BUG_ON(ret);
3395 if (refs > 1)
3396 goto out;
Chris Masone0115992007-06-19 16:23:05 -04003397
Chris Mason9aca1d52007-03-13 11:09:37 -04003398 /*
3399 * walk down to the last node level and free all the leaves
3400 */
Chris Masond3977122009-01-05 21:25:51 -05003401 while (*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04003402 WARN_ON(*level < 0);
3403 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -05003404 cur = path->nodes[*level];
Chris Masone0115992007-06-19 16:23:05 -04003405
Chris Mason5f39d392007-10-15 16:14:19 -04003406 if (btrfs_header_level(cur) != *level)
Chris Mason2c90e5d2007-04-02 10:50:19 -04003407 WARN_ON(1);
Chris Masone0115992007-06-19 16:23:05 -04003408
Chris Mason7518a232007-03-12 12:01:18 -04003409 if (path->slots[*level] >=
Chris Mason5f39d392007-10-15 16:14:19 -04003410 btrfs_header_nritems(cur))
Chris Mason20524f02007-03-10 06:35:47 -05003411 break;
Chris Masonbd56b302009-02-04 09:27:02 -05003412
3413 /* the new code goes down to level 1 and does all the
3414 * leaves pointed to that node in bulk. So, this check
3415 * for level 0 will always be false.
3416 *
3417 * But, the disk format allows the drop_snapshot_progress
3418 * field in the root to leave things in a state where
3419 * a leaf will need cleaning up here. If someone crashes
3420 * with the old code and then boots with the new code,
3421 * we might find a leaf here.
3422 */
Chris Mason6407bf62007-03-27 06:33:00 -04003423 if (*level == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003424 ret = btrfs_drop_leaf_ref(trans, root, cur);
Chris Mason6407bf62007-03-27 06:33:00 -04003425 BUG_ON(ret);
3426 break;
3427 }
Chris Masonbd56b302009-02-04 09:27:02 -05003428
3429 /*
3430 * once we get to level one, process the whole node
3431 * at once, including everything below it.
3432 */
3433 if (*level == 1) {
3434 ret = drop_level_one_refs(trans, root, path);
3435 BUG_ON(ret);
3436 break;
3437 }
3438
Chris Masondb945352007-10-15 16:15:53 -04003439 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
Chris Masonca7a79a2008-05-12 12:59:19 -04003440 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
Chris Masondb945352007-10-15 16:15:53 -04003441 blocksize = btrfs_level_size(root, *level - 1);
Chris Mason925baed2008-06-25 16:01:30 -04003442
Chris Mason56bec292009-03-13 10:10:06 -04003443 ret = drop_snap_lookup_refcount(trans, root, bytenr,
3444 blocksize, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -04003445 BUG_ON(ret);
Chris Masonbd56b302009-02-04 09:27:02 -05003446
3447 /*
3448 * if there is more than one reference, we don't need
3449 * to read that node to drop any references it has. We
3450 * just drop the ref we hold on that node and move on to the
3451 * next slot in this level.
3452 */
Chris Mason6407bf62007-03-27 06:33:00 -04003453 if (refs != 1) {
Chris Mason7bb86312007-12-11 09:25:06 -05003454 parent = path->nodes[*level];
3455 root_owner = btrfs_header_owner(parent);
3456 root_gen = btrfs_header_generation(parent);
Chris Mason20524f02007-03-10 06:35:47 -05003457 path->slots[*level]++;
Chris Masonf87f0572008-08-01 11:27:23 -04003458
Chris Mason56bec292009-03-13 10:10:06 -04003459 ret = btrfs_free_extent(trans, root, bytenr,
Zheng Yan31840ae2008-09-23 13:14:14 -04003460 blocksize, parent->start,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003461 root_owner, root_gen,
3462 *level - 1, 1);
Chris Mason20524f02007-03-10 06:35:47 -05003463 BUG_ON(ret);
Chris Mason18e35e0a2008-08-01 13:11:41 -04003464
3465 atomic_inc(&root->fs_info->throttle_gen);
3466 wake_up(&root->fs_info->transaction_throttle);
Chris Mason2dd3e672008-08-04 08:20:15 -04003467 cond_resched();
Chris Mason18e35e0a2008-08-01 13:11:41 -04003468
Chris Mason20524f02007-03-10 06:35:47 -05003469 continue;
3470 }
Chris Mason333db942008-06-25 16:01:30 -04003471
Chris Masonbd56b302009-02-04 09:27:02 -05003472 /*
3473 * we need to keep freeing things in the next level down.
3474 * read the block and loop around to process it
3475 */
3476 next = read_tree_block(root, bytenr, blocksize, ptr_gen);
Chris Mason5caf2a02007-04-02 11:20:42 -04003477 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -04003478 if (path->nodes[*level-1])
Chris Mason5f39d392007-10-15 16:14:19 -04003479 free_extent_buffer(path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -05003480 path->nodes[*level-1] = next;
Chris Mason5f39d392007-10-15 16:14:19 -04003481 *level = btrfs_header_level(next);
Chris Mason20524f02007-03-10 06:35:47 -05003482 path->slots[*level] = 0;
Chris Mason2dd3e672008-08-04 08:20:15 -04003483 cond_resched();
Chris Mason20524f02007-03-10 06:35:47 -05003484 }
3485out:
Chris Mason5caf2a02007-04-02 11:20:42 -04003486 WARN_ON(*level < 0);
3487 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason7bb86312007-12-11 09:25:06 -05003488
3489 if (path->nodes[*level] == root->node) {
Chris Mason7bb86312007-12-11 09:25:06 -05003490 parent = path->nodes[*level];
Yan Zheng31153d82008-07-28 15:32:19 -04003491 bytenr = path->nodes[*level]->start;
Chris Mason7bb86312007-12-11 09:25:06 -05003492 } else {
3493 parent = path->nodes[*level + 1];
Yan Zheng31153d82008-07-28 15:32:19 -04003494 bytenr = btrfs_node_blockptr(parent, path->slots[*level + 1]);
Chris Mason7bb86312007-12-11 09:25:06 -05003495 }
3496
Yan Zheng31153d82008-07-28 15:32:19 -04003497 blocksize = btrfs_level_size(root, *level);
3498 root_owner = btrfs_header_owner(parent);
Chris Mason7bb86312007-12-11 09:25:06 -05003499 root_gen = btrfs_header_generation(parent);
Yan Zheng31153d82008-07-28 15:32:19 -04003500
Chris Masonbd56b302009-02-04 09:27:02 -05003501 /*
3502 * cleanup and free the reference on the last node
3503 * we processed
3504 */
Chris Mason56bec292009-03-13 10:10:06 -04003505 ret = btrfs_free_extent(trans, root, bytenr, blocksize,
Zheng Yan31840ae2008-09-23 13:14:14 -04003506 parent->start, root_owner, root_gen,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003507 *level, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003508 free_extent_buffer(path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -05003509 path->nodes[*level] = NULL;
Chris Masonbd56b302009-02-04 09:27:02 -05003510
Chris Mason20524f02007-03-10 06:35:47 -05003511 *level += 1;
3512 BUG_ON(ret);
Chris Masonf87f0572008-08-01 11:27:23 -04003513
Chris Masone7a84562008-06-25 16:01:31 -04003514 cond_resched();
Chris Mason20524f02007-03-10 06:35:47 -05003515 return 0;
3516}
3517
Chris Mason9aca1d52007-03-13 11:09:37 -04003518/*
Yan Zhengf82d02d2008-10-29 14:49:05 -04003519 * helper function for drop_subtree, this function is similar to
3520 * walk_down_tree. The main difference is that it checks reference
3521 * counts while tree blocks are locked.
3522 */
Chris Masond3977122009-01-05 21:25:51 -05003523static noinline int walk_down_subtree(struct btrfs_trans_handle *trans,
Yan Zhengf82d02d2008-10-29 14:49:05 -04003524 struct btrfs_root *root,
3525 struct btrfs_path *path, int *level)
3526{
3527 struct extent_buffer *next;
3528 struct extent_buffer *cur;
3529 struct extent_buffer *parent;
3530 u64 bytenr;
3531 u64 ptr_gen;
3532 u32 blocksize;
3533 u32 refs;
3534 int ret;
3535
3536 cur = path->nodes[*level];
3537 ret = btrfs_lookup_extent_ref(trans, root, cur->start, cur->len,
3538 &refs);
3539 BUG_ON(ret);
3540 if (refs > 1)
3541 goto out;
3542
3543 while (*level >= 0) {
3544 cur = path->nodes[*level];
3545 if (*level == 0) {
3546 ret = btrfs_drop_leaf_ref(trans, root, cur);
3547 BUG_ON(ret);
3548 clean_tree_block(trans, root, cur);
3549 break;
3550 }
3551 if (path->slots[*level] >= btrfs_header_nritems(cur)) {
3552 clean_tree_block(trans, root, cur);
3553 break;
3554 }
3555
3556 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
3557 blocksize = btrfs_level_size(root, *level - 1);
3558 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
3559
3560 next = read_tree_block(root, bytenr, blocksize, ptr_gen);
3561 btrfs_tree_lock(next);
Chris Masonb4ce94d2009-02-04 09:25:08 -05003562 btrfs_set_lock_blocking(next);
Yan Zhengf82d02d2008-10-29 14:49:05 -04003563
3564 ret = btrfs_lookup_extent_ref(trans, root, bytenr, blocksize,
3565 &refs);
3566 BUG_ON(ret);
3567 if (refs > 1) {
3568 parent = path->nodes[*level];
3569 ret = btrfs_free_extent(trans, root, bytenr,
3570 blocksize, parent->start,
3571 btrfs_header_owner(parent),
3572 btrfs_header_generation(parent),
3573 *level - 1, 1);
3574 BUG_ON(ret);
3575 path->slots[*level]++;
3576 btrfs_tree_unlock(next);
3577 free_extent_buffer(next);
3578 continue;
3579 }
3580
3581 *level = btrfs_header_level(next);
3582 path->nodes[*level] = next;
3583 path->slots[*level] = 0;
3584 path->locks[*level] = 1;
3585 cond_resched();
3586 }
3587out:
3588 parent = path->nodes[*level + 1];
3589 bytenr = path->nodes[*level]->start;
3590 blocksize = path->nodes[*level]->len;
3591
3592 ret = btrfs_free_extent(trans, root, bytenr, blocksize,
3593 parent->start, btrfs_header_owner(parent),
3594 btrfs_header_generation(parent), *level, 1);
3595 BUG_ON(ret);
3596
3597 if (path->locks[*level]) {
3598 btrfs_tree_unlock(path->nodes[*level]);
3599 path->locks[*level] = 0;
3600 }
3601 free_extent_buffer(path->nodes[*level]);
3602 path->nodes[*level] = NULL;
3603 *level += 1;
3604 cond_resched();
3605 return 0;
3606}
3607
3608/*
Chris Mason9aca1d52007-03-13 11:09:37 -04003609 * helper for dropping snapshots. This walks back up the tree in the path
3610 * to find the first node higher up where we haven't yet gone through
3611 * all the slots
3612 */
Chris Masond3977122009-01-05 21:25:51 -05003613static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05003614 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04003615 struct btrfs_path *path,
3616 int *level, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05003617{
Chris Mason7bb86312007-12-11 09:25:06 -05003618 u64 root_owner;
3619 u64 root_gen;
3620 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05003621 int i;
3622 int slot;
3623 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04003624
Yan Zhengf82d02d2008-10-29 14:49:05 -04003625 for (i = *level; i < max_level && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -05003626 slot = path->slots[i];
Chris Mason5f39d392007-10-15 16:14:19 -04003627 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
3628 struct extent_buffer *node;
3629 struct btrfs_disk_key disk_key;
Chris Masonbd56b302009-02-04 09:27:02 -05003630
3631 /*
3632 * there is more work to do in this level.
3633 * Update the drop_progress marker to reflect
3634 * the work we've done so far, and then bump
3635 * the slot number
3636 */
Chris Mason5f39d392007-10-15 16:14:19 -04003637 node = path->nodes[i];
Chris Mason20524f02007-03-10 06:35:47 -05003638 path->slots[i]++;
3639 *level = i;
Chris Mason9f3a7422007-08-07 15:52:19 -04003640 WARN_ON(*level == 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003641 btrfs_node_key(node, &disk_key, path->slots[i]);
Chris Mason9f3a7422007-08-07 15:52:19 -04003642 memcpy(&root_item->drop_progress,
Chris Mason5f39d392007-10-15 16:14:19 -04003643 &disk_key, sizeof(disk_key));
Chris Mason9f3a7422007-08-07 15:52:19 -04003644 root_item->drop_level = i;
Chris Mason20524f02007-03-10 06:35:47 -05003645 return 0;
3646 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04003647 struct extent_buffer *parent;
Chris Masonbd56b302009-02-04 09:27:02 -05003648
3649 /*
3650 * this whole node is done, free our reference
3651 * on it and go up one level
3652 */
Zheng Yan31840ae2008-09-23 13:14:14 -04003653 if (path->nodes[*level] == root->node)
3654 parent = path->nodes[*level];
3655 else
3656 parent = path->nodes[*level + 1];
3657
3658 root_owner = btrfs_header_owner(parent);
3659 root_gen = btrfs_header_generation(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04003660
3661 clean_tree_block(trans, root, path->nodes[*level]);
Chris Masone089f052007-03-16 16:20:31 -04003662 ret = btrfs_free_extent(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04003663 path->nodes[*level]->start,
Chris Mason7bb86312007-12-11 09:25:06 -05003664 path->nodes[*level]->len,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003665 parent->start, root_owner,
3666 root_gen, *level, 1);
Chris Mason6407bf62007-03-27 06:33:00 -04003667 BUG_ON(ret);
Yan Zhengf82d02d2008-10-29 14:49:05 -04003668 if (path->locks[*level]) {
3669 btrfs_tree_unlock(path->nodes[*level]);
3670 path->locks[*level] = 0;
3671 }
Chris Mason5f39d392007-10-15 16:14:19 -04003672 free_extent_buffer(path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -04003673 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -05003674 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -05003675 }
3676 }
3677 return 1;
3678}
3679
Chris Mason9aca1d52007-03-13 11:09:37 -04003680/*
3681 * drop the reference count on the tree rooted at 'snap'. This traverses
3682 * the tree freeing any blocks that have a ref count of zero after being
3683 * decremented.
3684 */
Chris Masone089f052007-03-16 16:20:31 -04003685int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason9f3a7422007-08-07 15:52:19 -04003686 *root)
Chris Mason20524f02007-03-10 06:35:47 -05003687{
Chris Mason3768f362007-03-13 16:47:54 -04003688 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -04003689 int wret;
Chris Mason20524f02007-03-10 06:35:47 -05003690 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -04003691 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -05003692 int i;
3693 int orig_level;
Chris Masonc3e69d52009-03-13 10:17:05 -04003694 int update_count;
Chris Mason9f3a7422007-08-07 15:52:19 -04003695 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05003696
Chris Masona2135012008-06-25 16:01:30 -04003697 WARN_ON(!mutex_is_locked(&root->fs_info->drop_mutex));
Chris Mason5caf2a02007-04-02 11:20:42 -04003698 path = btrfs_alloc_path();
3699 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05003700
Chris Mason5f39d392007-10-15 16:14:19 -04003701 level = btrfs_header_level(root->node);
Chris Mason20524f02007-03-10 06:35:47 -05003702 orig_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04003703 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
3704 path->nodes[level] = root->node;
Chris Masonf510cfe2007-10-15 16:14:48 -04003705 extent_buffer_get(root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -04003706 path->slots[level] = 0;
3707 } else {
3708 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04003709 struct btrfs_disk_key found_key;
3710 struct extent_buffer *node;
Chris Mason6702ed42007-08-07 16:15:09 -04003711
Chris Mason9f3a7422007-08-07 15:52:19 -04003712 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Chris Mason6702ed42007-08-07 16:15:09 -04003713 level = root_item->drop_level;
3714 path->lowest_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04003715 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason6702ed42007-08-07 16:15:09 -04003716 if (wret < 0) {
Chris Mason9f3a7422007-08-07 15:52:19 -04003717 ret = wret;
3718 goto out;
3719 }
Chris Mason5f39d392007-10-15 16:14:19 -04003720 node = path->nodes[level];
3721 btrfs_node_key(node, &found_key, path->slots[level]);
3722 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
3723 sizeof(found_key)));
Chris Mason7d9eb122008-07-08 14:19:17 -04003724 /*
3725 * unlock our path, this is safe because only this
3726 * function is allowed to delete this snapshot
3727 */
Chris Mason925baed2008-06-25 16:01:30 -04003728 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
3729 if (path->nodes[i] && path->locks[i]) {
3730 path->locks[i] = 0;
3731 btrfs_tree_unlock(path->nodes[i]);
3732 }
3733 }
Chris Mason9f3a7422007-08-07 15:52:19 -04003734 }
Chris Masond3977122009-01-05 21:25:51 -05003735 while (1) {
Chris Masonc3e69d52009-03-13 10:17:05 -04003736 unsigned long update;
Chris Mason5caf2a02007-04-02 11:20:42 -04003737 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04003738 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05003739 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04003740 if (wret < 0)
3741 ret = wret;
3742
Yan Zhengf82d02d2008-10-29 14:49:05 -04003743 wret = walk_up_tree(trans, root, path, &level,
3744 BTRFS_MAX_LEVEL);
Chris Mason9aca1d52007-03-13 11:09:37 -04003745 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05003746 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04003747 if (wret < 0)
3748 ret = wret;
Chris Masonb7ec40d2009-03-12 20:12:45 -04003749 if (trans->transaction->in_commit ||
3750 trans->transaction->delayed_refs.flushing) {
Chris Masone7a84562008-06-25 16:01:31 -04003751 ret = -EAGAIN;
3752 break;
3753 }
Chris Mason18e35e0a2008-08-01 13:11:41 -04003754 atomic_inc(&root->fs_info->throttle_gen);
Chris Mason017e5362008-07-28 15:32:51 -04003755 wake_up(&root->fs_info->transaction_throttle);
Chris Masonc3e69d52009-03-13 10:17:05 -04003756 for (update_count = 0; update_count < 16; update_count++) {
3757 update = trans->delayed_ref_updates;
3758 trans->delayed_ref_updates = 0;
3759 if (update)
3760 btrfs_run_delayed_refs(trans, root, update);
3761 else
3762 break;
3763 }
Chris Mason20524f02007-03-10 06:35:47 -05003764 }
Chris Mason83e15a22007-03-12 09:03:27 -04003765 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -04003766 if (path->nodes[i]) {
Chris Mason5f39d392007-10-15 16:14:19 -04003767 free_extent_buffer(path->nodes[i]);
Chris Mason0f827312007-10-15 16:18:56 -04003768 path->nodes[i] = NULL;
Chris Mason83e15a22007-03-12 09:03:27 -04003769 }
Chris Mason20524f02007-03-10 06:35:47 -05003770 }
Chris Mason9f3a7422007-08-07 15:52:19 -04003771out:
Chris Mason5caf2a02007-04-02 11:20:42 -04003772 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -04003773 return ret;
Chris Mason20524f02007-03-10 06:35:47 -05003774}
Chris Mason9078a3e2007-04-26 16:46:15 -04003775
Yan Zhengf82d02d2008-10-29 14:49:05 -04003776int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
3777 struct btrfs_root *root,
3778 struct extent_buffer *node,
3779 struct extent_buffer *parent)
3780{
3781 struct btrfs_path *path;
3782 int level;
3783 int parent_level;
3784 int ret = 0;
3785 int wret;
3786
3787 path = btrfs_alloc_path();
3788 BUG_ON(!path);
3789
Chris Masonb9447ef2009-03-09 11:45:38 -04003790 btrfs_assert_tree_locked(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04003791 parent_level = btrfs_header_level(parent);
3792 extent_buffer_get(parent);
3793 path->nodes[parent_level] = parent;
3794 path->slots[parent_level] = btrfs_header_nritems(parent);
3795
Chris Masonb9447ef2009-03-09 11:45:38 -04003796 btrfs_assert_tree_locked(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04003797 level = btrfs_header_level(node);
3798 extent_buffer_get(node);
3799 path->nodes[level] = node;
3800 path->slots[level] = 0;
3801
3802 while (1) {
3803 wret = walk_down_subtree(trans, root, path, &level);
3804 if (wret < 0)
3805 ret = wret;
3806 if (wret != 0)
3807 break;
3808
3809 wret = walk_up_tree(trans, root, path, &level, parent_level);
3810 if (wret < 0)
3811 ret = wret;
3812 if (wret != 0)
3813 break;
3814 }
3815
3816 btrfs_free_path(path);
3817 return ret;
3818}
3819
Chris Mason8e7bf942008-04-28 09:02:36 -04003820static unsigned long calc_ra(unsigned long start, unsigned long last,
3821 unsigned long nr)
3822{
3823 return min(last, start + nr - 1);
3824}
3825
Chris Masond3977122009-01-05 21:25:51 -05003826static noinline int relocate_inode_pages(struct inode *inode, u64 start,
Chris Mason98ed5172008-01-03 10:01:48 -05003827 u64 len)
Chris Masonedbd8d42007-12-21 16:27:24 -05003828{
3829 u64 page_start;
3830 u64 page_end;
Zheng Yan1a40e232008-09-26 10:09:34 -04003831 unsigned long first_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05003832 unsigned long last_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05003833 unsigned long i;
3834 struct page *page;
Chris Masond1310b22008-01-24 16:13:08 -05003835 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason4313b392008-01-03 09:08:48 -05003836 struct file_ra_state *ra;
Chris Mason3eaa2882008-07-24 11:57:52 -04003837 struct btrfs_ordered_extent *ordered;
Zheng Yan1a40e232008-09-26 10:09:34 -04003838 unsigned int total_read = 0;
3839 unsigned int total_dirty = 0;
3840 int ret = 0;
Chris Mason4313b392008-01-03 09:08:48 -05003841
3842 ra = kzalloc(sizeof(*ra), GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05003843
3844 mutex_lock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04003845 first_index = start >> PAGE_CACHE_SHIFT;
Chris Masonedbd8d42007-12-21 16:27:24 -05003846 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
3847
Zheng Yan1a40e232008-09-26 10:09:34 -04003848 /* make sure the dirty trick played by the caller work */
3849 ret = invalidate_inode_pages2_range(inode->i_mapping,
3850 first_index, last_index);
3851 if (ret)
3852 goto out_unlock;
Chris Mason8e7bf942008-04-28 09:02:36 -04003853
Chris Mason4313b392008-01-03 09:08:48 -05003854 file_ra_state_init(ra, inode->i_mapping);
Chris Masonedbd8d42007-12-21 16:27:24 -05003855
Zheng Yan1a40e232008-09-26 10:09:34 -04003856 for (i = first_index ; i <= last_index; i++) {
3857 if (total_read % ra->ra_pages == 0) {
Chris Mason8e7bf942008-04-28 09:02:36 -04003858 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
Zheng Yan1a40e232008-09-26 10:09:34 -04003859 calc_ra(i, last_index, ra->ra_pages));
Chris Mason8e7bf942008-04-28 09:02:36 -04003860 }
3861 total_read++;
Chris Mason3eaa2882008-07-24 11:57:52 -04003862again:
3863 if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
Zheng Yan1a40e232008-09-26 10:09:34 -04003864 BUG_ON(1);
Chris Masonedbd8d42007-12-21 16:27:24 -05003865 page = grab_cache_page(inode->i_mapping, i);
Chris Masona061fc82008-05-07 11:43:44 -04003866 if (!page) {
Zheng Yan1a40e232008-09-26 10:09:34 -04003867 ret = -ENOMEM;
Chris Masonedbd8d42007-12-21 16:27:24 -05003868 goto out_unlock;
Chris Masona061fc82008-05-07 11:43:44 -04003869 }
Chris Masonedbd8d42007-12-21 16:27:24 -05003870 if (!PageUptodate(page)) {
3871 btrfs_readpage(NULL, page);
3872 lock_page(page);
3873 if (!PageUptodate(page)) {
3874 unlock_page(page);
3875 page_cache_release(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04003876 ret = -EIO;
Chris Masonedbd8d42007-12-21 16:27:24 -05003877 goto out_unlock;
3878 }
3879 }
Chris Masonec44a352008-04-28 15:29:52 -04003880 wait_on_page_writeback(page);
Chris Mason3eaa2882008-07-24 11:57:52 -04003881
Chris Masonedbd8d42007-12-21 16:27:24 -05003882 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
3883 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05003884 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05003885
Chris Mason3eaa2882008-07-24 11:57:52 -04003886 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3887 if (ordered) {
3888 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3889 unlock_page(page);
3890 page_cache_release(page);
3891 btrfs_start_ordered_extent(inode, ordered, 1);
3892 btrfs_put_ordered_extent(ordered);
3893 goto again;
3894 }
3895 set_page_extent_mapped(page);
3896
Zheng Yan1a40e232008-09-26 10:09:34 -04003897 if (i == first_index)
3898 set_extent_bits(io_tree, page_start, page_end,
3899 EXTENT_BOUNDARY, GFP_NOFS);
Yan Zheng1f80e4d2008-12-19 10:59:04 -05003900 btrfs_set_extent_delalloc(inode, page_start, page_end);
Zheng Yan1a40e232008-09-26 10:09:34 -04003901
Chris Masona061fc82008-05-07 11:43:44 -04003902 set_page_dirty(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04003903 total_dirty++;
Chris Masonedbd8d42007-12-21 16:27:24 -05003904
Chris Masond1310b22008-01-24 16:13:08 -05003905 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05003906 unlock_page(page);
3907 page_cache_release(page);
3908 }
3909
3910out_unlock:
Chris Masonec44a352008-04-28 15:29:52 -04003911 kfree(ra);
Chris Masonedbd8d42007-12-21 16:27:24 -05003912 mutex_unlock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04003913 balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty);
Chris Masonbf4ef672008-05-08 13:26:18 -04003914 return ret;
3915}
3916
Chris Masond3977122009-01-05 21:25:51 -05003917static noinline int relocate_data_extent(struct inode *reloc_inode,
Zheng Yan1a40e232008-09-26 10:09:34 -04003918 struct btrfs_key *extent_key,
3919 u64 offset)
Chris Masonedbd8d42007-12-21 16:27:24 -05003920{
Zheng Yan1a40e232008-09-26 10:09:34 -04003921 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
3922 struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree;
3923 struct extent_map *em;
Yan Zheng66435582008-10-30 14:19:50 -04003924 u64 start = extent_key->objectid - offset;
3925 u64 end = start + extent_key->offset - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04003926
3927 em = alloc_extent_map(GFP_NOFS);
3928 BUG_ON(!em || IS_ERR(em));
3929
Yan Zheng66435582008-10-30 14:19:50 -04003930 em->start = start;
Zheng Yan1a40e232008-09-26 10:09:34 -04003931 em->len = extent_key->offset;
Chris Masonc8b97812008-10-29 14:49:59 -04003932 em->block_len = extent_key->offset;
Zheng Yan1a40e232008-09-26 10:09:34 -04003933 em->block_start = extent_key->objectid;
3934 em->bdev = root->fs_info->fs_devices->latest_bdev;
3935 set_bit(EXTENT_FLAG_PINNED, &em->flags);
3936
3937 /* setup extent map to cheat btrfs_readpage */
Yan Zheng66435582008-10-30 14:19:50 -04003938 lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04003939 while (1) {
3940 int ret;
3941 spin_lock(&em_tree->lock);
3942 ret = add_extent_mapping(em_tree, em);
3943 spin_unlock(&em_tree->lock);
3944 if (ret != -EEXIST) {
3945 free_extent_map(em);
3946 break;
3947 }
Yan Zheng66435582008-10-30 14:19:50 -04003948 btrfs_drop_extent_cache(reloc_inode, start, end, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04003949 }
Yan Zheng66435582008-10-30 14:19:50 -04003950 unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04003951
Yan Zheng66435582008-10-30 14:19:50 -04003952 return relocate_inode_pages(reloc_inode, start, extent_key->offset);
Zheng Yan1a40e232008-09-26 10:09:34 -04003953}
3954
3955struct btrfs_ref_path {
3956 u64 extent_start;
3957 u64 nodes[BTRFS_MAX_LEVEL];
3958 u64 root_objectid;
3959 u64 root_generation;
3960 u64 owner_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04003961 u32 num_refs;
3962 int lowest_level;
3963 int current_level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04003964 int shared_level;
3965
3966 struct btrfs_key node_keys[BTRFS_MAX_LEVEL];
3967 u64 new_nodes[BTRFS_MAX_LEVEL];
Zheng Yan1a40e232008-09-26 10:09:34 -04003968};
3969
3970struct disk_extent {
Chris Masonc8b97812008-10-29 14:49:59 -04003971 u64 ram_bytes;
Zheng Yan1a40e232008-09-26 10:09:34 -04003972 u64 disk_bytenr;
3973 u64 disk_num_bytes;
3974 u64 offset;
3975 u64 num_bytes;
Chris Masonc8b97812008-10-29 14:49:59 -04003976 u8 compression;
3977 u8 encryption;
3978 u16 other_encoding;
Zheng Yan1a40e232008-09-26 10:09:34 -04003979};
3980
3981static int is_cowonly_root(u64 root_objectid)
3982{
3983 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
3984 root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
3985 root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
3986 root_objectid == BTRFS_DEV_TREE_OBJECTID ||
Yan Zheng0403e472008-12-10 20:32:51 -05003987 root_objectid == BTRFS_TREE_LOG_OBJECTID ||
3988 root_objectid == BTRFS_CSUM_TREE_OBJECTID)
Zheng Yan1a40e232008-09-26 10:09:34 -04003989 return 1;
3990 return 0;
3991}
3992
Chris Masond3977122009-01-05 21:25:51 -05003993static noinline int __next_ref_path(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04003994 struct btrfs_root *extent_root,
3995 struct btrfs_ref_path *ref_path,
3996 int first_time)
3997{
3998 struct extent_buffer *leaf;
3999 struct btrfs_path *path;
Chris Mason4313b392008-01-03 09:08:48 -05004000 struct btrfs_extent_ref *ref;
Chris Masonedbd8d42007-12-21 16:27:24 -05004001 struct btrfs_key key;
4002 struct btrfs_key found_key;
Zheng Yan1a40e232008-09-26 10:09:34 -04004003 u64 bytenr;
Chris Masonedbd8d42007-12-21 16:27:24 -05004004 u32 nritems;
Zheng Yan1a40e232008-09-26 10:09:34 -04004005 int level;
4006 int ret = 1;
Chris Masonedbd8d42007-12-21 16:27:24 -05004007
Zheng Yan1a40e232008-09-26 10:09:34 -04004008 path = btrfs_alloc_path();
4009 if (!path)
4010 return -ENOMEM;
4011
Zheng Yan1a40e232008-09-26 10:09:34 -04004012 if (first_time) {
4013 ref_path->lowest_level = -1;
4014 ref_path->current_level = -1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004015 ref_path->shared_level = -1;
Zheng Yan1a40e232008-09-26 10:09:34 -04004016 goto walk_up;
Chris Masona061fc82008-05-07 11:43:44 -04004017 }
Zheng Yan1a40e232008-09-26 10:09:34 -04004018walk_down:
4019 level = ref_path->current_level - 1;
4020 while (level >= -1) {
4021 u64 parent;
4022 if (level < ref_path->lowest_level)
4023 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05004024
Chris Masond3977122009-01-05 21:25:51 -05004025 if (level >= 0)
Zheng Yan1a40e232008-09-26 10:09:34 -04004026 bytenr = ref_path->nodes[level];
Chris Masond3977122009-01-05 21:25:51 -05004027 else
Zheng Yan1a40e232008-09-26 10:09:34 -04004028 bytenr = ref_path->extent_start;
Zheng Yan1a40e232008-09-26 10:09:34 -04004029 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05004030
Zheng Yan1a40e232008-09-26 10:09:34 -04004031 parent = ref_path->nodes[level + 1];
4032 ref_path->nodes[level + 1] = 0;
4033 ref_path->current_level = level;
4034 BUG_ON(parent == 0);
4035
4036 key.objectid = bytenr;
4037 key.offset = parent + 1;
4038 key.type = BTRFS_EXTENT_REF_KEY;
4039
4040 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05004041 if (ret < 0)
4042 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04004043 BUG_ON(ret == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05004044
Chris Masonedbd8d42007-12-21 16:27:24 -05004045 leaf = path->nodes[0];
4046 nritems = btrfs_header_nritems(leaf);
Zheng Yan1a40e232008-09-26 10:09:34 -04004047 if (path->slots[0] >= nritems) {
Chris Masona061fc82008-05-07 11:43:44 -04004048 ret = btrfs_next_leaf(extent_root, path);
Chris Masona061fc82008-05-07 11:43:44 -04004049 if (ret < 0)
4050 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04004051 if (ret > 0)
4052 goto next;
Chris Masonbf4ef672008-05-08 13:26:18 -04004053 leaf = path->nodes[0];
Chris Masona061fc82008-05-07 11:43:44 -04004054 }
Chris Masonedbd8d42007-12-21 16:27:24 -05004055
4056 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Zheng Yan1a40e232008-09-26 10:09:34 -04004057 if (found_key.objectid == bytenr &&
Yan Zhengf82d02d2008-10-29 14:49:05 -04004058 found_key.type == BTRFS_EXTENT_REF_KEY) {
4059 if (level < ref_path->shared_level)
4060 ref_path->shared_level = level;
Zheng Yan1a40e232008-09-26 10:09:34 -04004061 goto found;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004062 }
Zheng Yan1a40e232008-09-26 10:09:34 -04004063next:
4064 level--;
4065 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04004066 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04004067 }
4068 /* reached lowest level */
4069 ret = 1;
4070 goto out;
4071walk_up:
4072 level = ref_path->current_level;
4073 while (level < BTRFS_MAX_LEVEL - 1) {
4074 u64 ref_objectid;
Chris Masond3977122009-01-05 21:25:51 -05004075
4076 if (level >= 0)
Zheng Yan1a40e232008-09-26 10:09:34 -04004077 bytenr = ref_path->nodes[level];
Chris Masond3977122009-01-05 21:25:51 -05004078 else
Zheng Yan1a40e232008-09-26 10:09:34 -04004079 bytenr = ref_path->extent_start;
Chris Masond3977122009-01-05 21:25:51 -05004080
Zheng Yan1a40e232008-09-26 10:09:34 -04004081 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05004082
Zheng Yan1a40e232008-09-26 10:09:34 -04004083 key.objectid = bytenr;
4084 key.offset = 0;
4085 key.type = BTRFS_EXTENT_REF_KEY;
Chris Masonedbd8d42007-12-21 16:27:24 -05004086
Zheng Yan1a40e232008-09-26 10:09:34 -04004087 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
4088 if (ret < 0)
Chris Masonedbd8d42007-12-21 16:27:24 -05004089 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04004090
4091 leaf = path->nodes[0];
4092 nritems = btrfs_header_nritems(leaf);
4093 if (path->slots[0] >= nritems) {
4094 ret = btrfs_next_leaf(extent_root, path);
4095 if (ret < 0)
4096 goto out;
4097 if (ret > 0) {
4098 /* the extent was freed by someone */
4099 if (ref_path->lowest_level == level)
4100 goto out;
4101 btrfs_release_path(extent_root, path);
4102 goto walk_down;
4103 }
4104 leaf = path->nodes[0];
4105 }
4106
4107 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4108 if (found_key.objectid != bytenr ||
4109 found_key.type != BTRFS_EXTENT_REF_KEY) {
4110 /* the extent was freed by someone */
4111 if (ref_path->lowest_level == level) {
4112 ret = 1;
4113 goto out;
4114 }
4115 btrfs_release_path(extent_root, path);
4116 goto walk_down;
4117 }
4118found:
4119 ref = btrfs_item_ptr(leaf, path->slots[0],
4120 struct btrfs_extent_ref);
4121 ref_objectid = btrfs_ref_objectid(leaf, ref);
4122 if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) {
4123 if (first_time) {
4124 level = (int)ref_objectid;
4125 BUG_ON(level >= BTRFS_MAX_LEVEL);
4126 ref_path->lowest_level = level;
4127 ref_path->current_level = level;
4128 ref_path->nodes[level] = bytenr;
4129 } else {
4130 WARN_ON(ref_objectid != level);
4131 }
4132 } else {
4133 WARN_ON(level != -1);
4134 }
4135 first_time = 0;
4136
4137 if (ref_path->lowest_level == level) {
4138 ref_path->owner_objectid = ref_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04004139 ref_path->num_refs = btrfs_ref_num_refs(leaf, ref);
4140 }
4141
4142 /*
4143 * the block is tree root or the block isn't in reference
4144 * counted tree.
4145 */
4146 if (found_key.objectid == found_key.offset ||
4147 is_cowonly_root(btrfs_ref_root(leaf, ref))) {
4148 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
4149 ref_path->root_generation =
4150 btrfs_ref_generation(leaf, ref);
4151 if (level < 0) {
4152 /* special reference from the tree log */
4153 ref_path->nodes[0] = found_key.offset;
4154 ref_path->current_level = 0;
4155 }
4156 ret = 0;
4157 goto out;
4158 }
4159
4160 level++;
4161 BUG_ON(ref_path->nodes[level] != 0);
4162 ref_path->nodes[level] = found_key.offset;
4163 ref_path->current_level = level;
4164
4165 /*
4166 * the reference was created in the running transaction,
4167 * no need to continue walking up.
4168 */
4169 if (btrfs_ref_generation(leaf, ref) == trans->transid) {
4170 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
4171 ref_path->root_generation =
4172 btrfs_ref_generation(leaf, ref);
4173 ret = 0;
4174 goto out;
4175 }
4176
4177 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04004178 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04004179 }
4180 /* reached max tree level, but no tree root found. */
4181 BUG();
4182out:
Zheng Yan1a40e232008-09-26 10:09:34 -04004183 btrfs_free_path(path);
4184 return ret;
4185}
4186
4187static int btrfs_first_ref_path(struct btrfs_trans_handle *trans,
4188 struct btrfs_root *extent_root,
4189 struct btrfs_ref_path *ref_path,
4190 u64 extent_start)
4191{
4192 memset(ref_path, 0, sizeof(*ref_path));
4193 ref_path->extent_start = extent_start;
4194
4195 return __next_ref_path(trans, extent_root, ref_path, 1);
4196}
4197
4198static int btrfs_next_ref_path(struct btrfs_trans_handle *trans,
4199 struct btrfs_root *extent_root,
4200 struct btrfs_ref_path *ref_path)
4201{
4202 return __next_ref_path(trans, extent_root, ref_path, 0);
4203}
4204
Chris Masond3977122009-01-05 21:25:51 -05004205static noinline int get_new_locations(struct inode *reloc_inode,
Zheng Yan1a40e232008-09-26 10:09:34 -04004206 struct btrfs_key *extent_key,
4207 u64 offset, int no_fragment,
4208 struct disk_extent **extents,
4209 int *nr_extents)
4210{
4211 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
4212 struct btrfs_path *path;
4213 struct btrfs_file_extent_item *fi;
4214 struct extent_buffer *leaf;
4215 struct disk_extent *exts = *extents;
4216 struct btrfs_key found_key;
4217 u64 cur_pos;
4218 u64 last_byte;
4219 u32 nritems;
4220 int nr = 0;
4221 int max = *nr_extents;
4222 int ret;
4223
4224 WARN_ON(!no_fragment && *extents);
4225 if (!exts) {
4226 max = 1;
4227 exts = kmalloc(sizeof(*exts) * max, GFP_NOFS);
4228 if (!exts)
4229 return -ENOMEM;
4230 }
4231
4232 path = btrfs_alloc_path();
4233 BUG_ON(!path);
4234
4235 cur_pos = extent_key->objectid - offset;
4236 last_byte = extent_key->objectid + extent_key->offset;
4237 ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino,
4238 cur_pos, 0);
4239 if (ret < 0)
4240 goto out;
4241 if (ret > 0) {
4242 ret = -ENOENT;
4243 goto out;
4244 }
4245
4246 while (1) {
4247 leaf = path->nodes[0];
4248 nritems = btrfs_header_nritems(leaf);
4249 if (path->slots[0] >= nritems) {
4250 ret = btrfs_next_leaf(root, path);
4251 if (ret < 0)
4252 goto out;
4253 if (ret > 0)
4254 break;
4255 leaf = path->nodes[0];
4256 }
4257
4258 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4259 if (found_key.offset != cur_pos ||
4260 found_key.type != BTRFS_EXTENT_DATA_KEY ||
4261 found_key.objectid != reloc_inode->i_ino)
4262 break;
4263
4264 fi = btrfs_item_ptr(leaf, path->slots[0],
4265 struct btrfs_file_extent_item);
4266 if (btrfs_file_extent_type(leaf, fi) !=
4267 BTRFS_FILE_EXTENT_REG ||
4268 btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
4269 break;
4270
4271 if (nr == max) {
4272 struct disk_extent *old = exts;
4273 max *= 2;
4274 exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
4275 memcpy(exts, old, sizeof(*exts) * nr);
4276 if (old != *extents)
4277 kfree(old);
4278 }
4279
4280 exts[nr].disk_bytenr =
4281 btrfs_file_extent_disk_bytenr(leaf, fi);
4282 exts[nr].disk_num_bytes =
4283 btrfs_file_extent_disk_num_bytes(leaf, fi);
4284 exts[nr].offset = btrfs_file_extent_offset(leaf, fi);
4285 exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
Chris Masonc8b97812008-10-29 14:49:59 -04004286 exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
4287 exts[nr].compression = btrfs_file_extent_compression(leaf, fi);
4288 exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi);
4289 exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf,
4290 fi);
Yan Zhengd899e052008-10-30 14:25:28 -04004291 BUG_ON(exts[nr].offset > 0);
4292 BUG_ON(exts[nr].compression || exts[nr].encryption);
4293 BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04004294
4295 cur_pos += exts[nr].num_bytes;
4296 nr++;
4297
4298 if (cur_pos + offset >= last_byte)
4299 break;
4300
4301 if (no_fragment) {
4302 ret = 1;
4303 goto out;
4304 }
4305 path->slots[0]++;
4306 }
4307
Yan Zheng1f80e4d2008-12-19 10:59:04 -05004308 BUG_ON(cur_pos + offset > last_byte);
Zheng Yan1a40e232008-09-26 10:09:34 -04004309 if (cur_pos + offset < last_byte) {
4310 ret = -ENOENT;
4311 goto out;
Chris Masonedbd8d42007-12-21 16:27:24 -05004312 }
4313 ret = 0;
4314out:
Zheng Yan1a40e232008-09-26 10:09:34 -04004315 btrfs_free_path(path);
4316 if (ret) {
4317 if (exts != *extents)
4318 kfree(exts);
4319 } else {
4320 *extents = exts;
4321 *nr_extents = nr;
4322 }
4323 return ret;
4324}
4325
Chris Masond3977122009-01-05 21:25:51 -05004326static noinline int replace_one_extent(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04004327 struct btrfs_root *root,
4328 struct btrfs_path *path,
4329 struct btrfs_key *extent_key,
4330 struct btrfs_key *leaf_key,
4331 struct btrfs_ref_path *ref_path,
4332 struct disk_extent *new_extents,
4333 int nr_extents)
4334{
4335 struct extent_buffer *leaf;
4336 struct btrfs_file_extent_item *fi;
4337 struct inode *inode = NULL;
4338 struct btrfs_key key;
4339 u64 lock_start = 0;
4340 u64 lock_end = 0;
4341 u64 num_bytes;
4342 u64 ext_offset;
Yan Zheng86288a12009-01-21 10:49:16 -05004343 u64 search_end = (u64)-1;
Zheng Yan1a40e232008-09-26 10:09:34 -04004344 u32 nritems;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004345 int nr_scaned = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04004346 int extent_locked = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04004347 int extent_type;
Zheng Yan1a40e232008-09-26 10:09:34 -04004348 int ret;
4349
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004350 memcpy(&key, leaf_key, sizeof(key));
Zheng Yan1a40e232008-09-26 10:09:34 -04004351 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004352 if (key.objectid < ref_path->owner_objectid ||
4353 (key.objectid == ref_path->owner_objectid &&
4354 key.type < BTRFS_EXTENT_DATA_KEY)) {
4355 key.objectid = ref_path->owner_objectid;
4356 key.type = BTRFS_EXTENT_DATA_KEY;
4357 key.offset = 0;
4358 }
Zheng Yan1a40e232008-09-26 10:09:34 -04004359 }
4360
4361 while (1) {
4362 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
4363 if (ret < 0)
4364 goto out;
4365
4366 leaf = path->nodes[0];
4367 nritems = btrfs_header_nritems(leaf);
4368next:
4369 if (extent_locked && ret > 0) {
4370 /*
4371 * the file extent item was modified by someone
4372 * before the extent got locked.
4373 */
Zheng Yan1a40e232008-09-26 10:09:34 -04004374 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4375 lock_end, GFP_NOFS);
4376 extent_locked = 0;
4377 }
4378
4379 if (path->slots[0] >= nritems) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004380 if (++nr_scaned > 2)
Zheng Yan1a40e232008-09-26 10:09:34 -04004381 break;
4382
4383 BUG_ON(extent_locked);
4384 ret = btrfs_next_leaf(root, path);
4385 if (ret < 0)
4386 goto out;
4387 if (ret > 0)
4388 break;
4389 leaf = path->nodes[0];
4390 nritems = btrfs_header_nritems(leaf);
4391 }
4392
4393 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4394
4395 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
4396 if ((key.objectid > ref_path->owner_objectid) ||
4397 (key.objectid == ref_path->owner_objectid &&
4398 key.type > BTRFS_EXTENT_DATA_KEY) ||
Yan Zheng86288a12009-01-21 10:49:16 -05004399 key.offset >= search_end)
Zheng Yan1a40e232008-09-26 10:09:34 -04004400 break;
4401 }
4402
4403 if (inode && key.objectid != inode->i_ino) {
4404 BUG_ON(extent_locked);
4405 btrfs_release_path(root, path);
4406 mutex_unlock(&inode->i_mutex);
4407 iput(inode);
4408 inode = NULL;
4409 continue;
4410 }
4411
4412 if (key.type != BTRFS_EXTENT_DATA_KEY) {
4413 path->slots[0]++;
4414 ret = 1;
4415 goto next;
4416 }
4417 fi = btrfs_item_ptr(leaf, path->slots[0],
4418 struct btrfs_file_extent_item);
Yan Zhengd899e052008-10-30 14:25:28 -04004419 extent_type = btrfs_file_extent_type(leaf, fi);
4420 if ((extent_type != BTRFS_FILE_EXTENT_REG &&
4421 extent_type != BTRFS_FILE_EXTENT_PREALLOC) ||
Zheng Yan1a40e232008-09-26 10:09:34 -04004422 (btrfs_file_extent_disk_bytenr(leaf, fi) !=
4423 extent_key->objectid)) {
4424 path->slots[0]++;
4425 ret = 1;
4426 goto next;
4427 }
4428
4429 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
4430 ext_offset = btrfs_file_extent_offset(leaf, fi);
4431
Yan Zheng86288a12009-01-21 10:49:16 -05004432 if (search_end == (u64)-1) {
4433 search_end = key.offset - ext_offset +
4434 btrfs_file_extent_ram_bytes(leaf, fi);
4435 }
Zheng Yan1a40e232008-09-26 10:09:34 -04004436
4437 if (!extent_locked) {
4438 lock_start = key.offset;
4439 lock_end = lock_start + num_bytes - 1;
4440 } else {
Yan Zheng66435582008-10-30 14:19:50 -04004441 if (lock_start > key.offset ||
4442 lock_end + 1 < key.offset + num_bytes) {
4443 unlock_extent(&BTRFS_I(inode)->io_tree,
4444 lock_start, lock_end, GFP_NOFS);
4445 extent_locked = 0;
4446 }
Zheng Yan1a40e232008-09-26 10:09:34 -04004447 }
4448
4449 if (!inode) {
4450 btrfs_release_path(root, path);
4451
4452 inode = btrfs_iget_locked(root->fs_info->sb,
4453 key.objectid, root);
4454 if (inode->i_state & I_NEW) {
4455 BTRFS_I(inode)->root = root;
4456 BTRFS_I(inode)->location.objectid =
4457 key.objectid;
4458 BTRFS_I(inode)->location.type =
4459 BTRFS_INODE_ITEM_KEY;
4460 BTRFS_I(inode)->location.offset = 0;
4461 btrfs_read_locked_inode(inode);
4462 unlock_new_inode(inode);
4463 }
4464 /*
4465 * some code call btrfs_commit_transaction while
4466 * holding the i_mutex, so we can't use mutex_lock
4467 * here.
4468 */
4469 if (is_bad_inode(inode) ||
4470 !mutex_trylock(&inode->i_mutex)) {
4471 iput(inode);
4472 inode = NULL;
4473 key.offset = (u64)-1;
4474 goto skip;
4475 }
4476 }
4477
4478 if (!extent_locked) {
4479 struct btrfs_ordered_extent *ordered;
4480
4481 btrfs_release_path(root, path);
4482
4483 lock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4484 lock_end, GFP_NOFS);
4485 ordered = btrfs_lookup_first_ordered_extent(inode,
4486 lock_end);
4487 if (ordered &&
4488 ordered->file_offset <= lock_end &&
4489 ordered->file_offset + ordered->len > lock_start) {
4490 unlock_extent(&BTRFS_I(inode)->io_tree,
4491 lock_start, lock_end, GFP_NOFS);
4492 btrfs_start_ordered_extent(inode, ordered, 1);
4493 btrfs_put_ordered_extent(ordered);
4494 key.offset += num_bytes;
4495 goto skip;
4496 }
4497 if (ordered)
4498 btrfs_put_ordered_extent(ordered);
4499
Zheng Yan1a40e232008-09-26 10:09:34 -04004500 extent_locked = 1;
4501 continue;
4502 }
4503
4504 if (nr_extents == 1) {
4505 /* update extent pointer in place */
Zheng Yan1a40e232008-09-26 10:09:34 -04004506 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4507 new_extents[0].disk_bytenr);
4508 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4509 new_extents[0].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04004510 btrfs_mark_buffer_dirty(leaf);
4511
4512 btrfs_drop_extent_cache(inode, key.offset,
4513 key.offset + num_bytes - 1, 0);
4514
4515 ret = btrfs_inc_extent_ref(trans, root,
4516 new_extents[0].disk_bytenr,
4517 new_extents[0].disk_num_bytes,
4518 leaf->start,
4519 root->root_key.objectid,
4520 trans->transid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004521 key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04004522 BUG_ON(ret);
4523
4524 ret = btrfs_free_extent(trans, root,
4525 extent_key->objectid,
4526 extent_key->offset,
4527 leaf->start,
4528 btrfs_header_owner(leaf),
4529 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004530 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04004531 BUG_ON(ret);
4532
4533 btrfs_release_path(root, path);
4534 key.offset += num_bytes;
4535 } else {
Yan Zhengd899e052008-10-30 14:25:28 -04004536 BUG_ON(1);
4537#if 0
Zheng Yan1a40e232008-09-26 10:09:34 -04004538 u64 alloc_hint;
4539 u64 extent_len;
4540 int i;
4541 /*
4542 * drop old extent pointer at first, then insert the
4543 * new pointers one bye one
4544 */
4545 btrfs_release_path(root, path);
4546 ret = btrfs_drop_extents(trans, root, inode, key.offset,
4547 key.offset + num_bytes,
4548 key.offset, &alloc_hint);
4549 BUG_ON(ret);
4550
4551 for (i = 0; i < nr_extents; i++) {
4552 if (ext_offset >= new_extents[i].num_bytes) {
4553 ext_offset -= new_extents[i].num_bytes;
4554 continue;
4555 }
4556 extent_len = min(new_extents[i].num_bytes -
4557 ext_offset, num_bytes);
4558
4559 ret = btrfs_insert_empty_item(trans, root,
4560 path, &key,
4561 sizeof(*fi));
4562 BUG_ON(ret);
4563
4564 leaf = path->nodes[0];
4565 fi = btrfs_item_ptr(leaf, path->slots[0],
4566 struct btrfs_file_extent_item);
4567 btrfs_set_file_extent_generation(leaf, fi,
4568 trans->transid);
4569 btrfs_set_file_extent_type(leaf, fi,
4570 BTRFS_FILE_EXTENT_REG);
4571 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4572 new_extents[i].disk_bytenr);
4573 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4574 new_extents[i].disk_num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -04004575 btrfs_set_file_extent_ram_bytes(leaf, fi,
4576 new_extents[i].ram_bytes);
4577
4578 btrfs_set_file_extent_compression(leaf, fi,
4579 new_extents[i].compression);
4580 btrfs_set_file_extent_encryption(leaf, fi,
4581 new_extents[i].encryption);
4582 btrfs_set_file_extent_other_encoding(leaf, fi,
4583 new_extents[i].other_encoding);
4584
Zheng Yan1a40e232008-09-26 10:09:34 -04004585 btrfs_set_file_extent_num_bytes(leaf, fi,
4586 extent_len);
4587 ext_offset += new_extents[i].offset;
4588 btrfs_set_file_extent_offset(leaf, fi,
4589 ext_offset);
4590 btrfs_mark_buffer_dirty(leaf);
4591
4592 btrfs_drop_extent_cache(inode, key.offset,
4593 key.offset + extent_len - 1, 0);
4594
4595 ret = btrfs_inc_extent_ref(trans, root,
4596 new_extents[i].disk_bytenr,
4597 new_extents[i].disk_num_bytes,
4598 leaf->start,
4599 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004600 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04004601 BUG_ON(ret);
4602 btrfs_release_path(root, path);
4603
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004604 inode_add_bytes(inode, extent_len);
Zheng Yan1a40e232008-09-26 10:09:34 -04004605
4606 ext_offset = 0;
4607 num_bytes -= extent_len;
4608 key.offset += extent_len;
4609
4610 if (num_bytes == 0)
4611 break;
4612 }
4613 BUG_ON(i >= nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04004614#endif
Zheng Yan1a40e232008-09-26 10:09:34 -04004615 }
4616
4617 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04004618 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4619 lock_end, GFP_NOFS);
4620 extent_locked = 0;
4621 }
4622skip:
4623 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS &&
Yan Zheng86288a12009-01-21 10:49:16 -05004624 key.offset >= search_end)
Zheng Yan1a40e232008-09-26 10:09:34 -04004625 break;
4626
4627 cond_resched();
4628 }
4629 ret = 0;
4630out:
4631 btrfs_release_path(root, path);
4632 if (inode) {
4633 mutex_unlock(&inode->i_mutex);
4634 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04004635 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4636 lock_end, GFP_NOFS);
4637 }
4638 iput(inode);
4639 }
4640 return ret;
4641}
4642
Zheng Yan1a40e232008-09-26 10:09:34 -04004643int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans,
4644 struct btrfs_root *root,
4645 struct extent_buffer *buf, u64 orig_start)
4646{
4647 int level;
4648 int ret;
4649
4650 BUG_ON(btrfs_header_generation(buf) != trans->transid);
4651 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
4652
4653 level = btrfs_header_level(buf);
4654 if (level == 0) {
4655 struct btrfs_leaf_ref *ref;
4656 struct btrfs_leaf_ref *orig_ref;
4657
4658 orig_ref = btrfs_lookup_leaf_ref(root, orig_start);
4659 if (!orig_ref)
4660 return -ENOENT;
4661
4662 ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems);
4663 if (!ref) {
4664 btrfs_free_leaf_ref(root, orig_ref);
4665 return -ENOMEM;
4666 }
4667
4668 ref->nritems = orig_ref->nritems;
4669 memcpy(ref->extents, orig_ref->extents,
4670 sizeof(ref->extents[0]) * ref->nritems);
4671
4672 btrfs_free_leaf_ref(root, orig_ref);
4673
4674 ref->root_gen = trans->transid;
4675 ref->bytenr = buf->start;
4676 ref->owner = btrfs_header_owner(buf);
4677 ref->generation = btrfs_header_generation(buf);
Chris Masonbd56b302009-02-04 09:27:02 -05004678
Zheng Yan1a40e232008-09-26 10:09:34 -04004679 ret = btrfs_add_leaf_ref(root, ref, 0);
4680 WARN_ON(ret);
4681 btrfs_free_leaf_ref(root, ref);
4682 }
4683 return 0;
4684}
4685
Chris Masond3977122009-01-05 21:25:51 -05004686static noinline int invalidate_extent_cache(struct btrfs_root *root,
Zheng Yan1a40e232008-09-26 10:09:34 -04004687 struct extent_buffer *leaf,
4688 struct btrfs_block_group_cache *group,
4689 struct btrfs_root *target_root)
4690{
4691 struct btrfs_key key;
4692 struct inode *inode = NULL;
4693 struct btrfs_file_extent_item *fi;
4694 u64 num_bytes;
4695 u64 skip_objectid = 0;
4696 u32 nritems;
4697 u32 i;
4698
4699 nritems = btrfs_header_nritems(leaf);
4700 for (i = 0; i < nritems; i++) {
4701 btrfs_item_key_to_cpu(leaf, &key, i);
4702 if (key.objectid == skip_objectid ||
4703 key.type != BTRFS_EXTENT_DATA_KEY)
4704 continue;
4705 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
4706 if (btrfs_file_extent_type(leaf, fi) ==
4707 BTRFS_FILE_EXTENT_INLINE)
4708 continue;
4709 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
4710 continue;
4711 if (!inode || inode->i_ino != key.objectid) {
4712 iput(inode);
4713 inode = btrfs_ilookup(target_root->fs_info->sb,
4714 key.objectid, target_root, 1);
4715 }
4716 if (!inode) {
4717 skip_objectid = key.objectid;
4718 continue;
4719 }
4720 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
4721
4722 lock_extent(&BTRFS_I(inode)->io_tree, key.offset,
4723 key.offset + num_bytes - 1, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04004724 btrfs_drop_extent_cache(inode, key.offset,
4725 key.offset + num_bytes - 1, 1);
Zheng Yan1a40e232008-09-26 10:09:34 -04004726 unlock_extent(&BTRFS_I(inode)->io_tree, key.offset,
4727 key.offset + num_bytes - 1, GFP_NOFS);
4728 cond_resched();
4729 }
4730 iput(inode);
4731 return 0;
4732}
4733
Chris Masond3977122009-01-05 21:25:51 -05004734static noinline int replace_extents_in_leaf(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04004735 struct btrfs_root *root,
4736 struct extent_buffer *leaf,
4737 struct btrfs_block_group_cache *group,
4738 struct inode *reloc_inode)
4739{
4740 struct btrfs_key key;
4741 struct btrfs_key extent_key;
4742 struct btrfs_file_extent_item *fi;
4743 struct btrfs_leaf_ref *ref;
4744 struct disk_extent *new_extent;
4745 u64 bytenr;
4746 u64 num_bytes;
4747 u32 nritems;
4748 u32 i;
4749 int ext_index;
4750 int nr_extent;
4751 int ret;
4752
4753 new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
4754 BUG_ON(!new_extent);
4755
4756 ref = btrfs_lookup_leaf_ref(root, leaf->start);
4757 BUG_ON(!ref);
4758
4759 ext_index = -1;
4760 nritems = btrfs_header_nritems(leaf);
4761 for (i = 0; i < nritems; i++) {
4762 btrfs_item_key_to_cpu(leaf, &key, i);
4763 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
4764 continue;
4765 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
4766 if (btrfs_file_extent_type(leaf, fi) ==
4767 BTRFS_FILE_EXTENT_INLINE)
4768 continue;
4769 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
4770 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
4771 if (bytenr == 0)
4772 continue;
4773
4774 ext_index++;
4775 if (bytenr >= group->key.objectid + group->key.offset ||
4776 bytenr + num_bytes <= group->key.objectid)
4777 continue;
4778
4779 extent_key.objectid = bytenr;
4780 extent_key.offset = num_bytes;
4781 extent_key.type = BTRFS_EXTENT_ITEM_KEY;
4782 nr_extent = 1;
4783 ret = get_new_locations(reloc_inode, &extent_key,
4784 group->key.objectid, 1,
4785 &new_extent, &nr_extent);
4786 if (ret > 0)
4787 continue;
4788 BUG_ON(ret < 0);
4789
4790 BUG_ON(ref->extents[ext_index].bytenr != bytenr);
4791 BUG_ON(ref->extents[ext_index].num_bytes != num_bytes);
4792 ref->extents[ext_index].bytenr = new_extent->disk_bytenr;
4793 ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes;
4794
Zheng Yan1a40e232008-09-26 10:09:34 -04004795 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4796 new_extent->disk_bytenr);
4797 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4798 new_extent->disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04004799 btrfs_mark_buffer_dirty(leaf);
4800
4801 ret = btrfs_inc_extent_ref(trans, root,
4802 new_extent->disk_bytenr,
4803 new_extent->disk_num_bytes,
4804 leaf->start,
4805 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004806 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04004807 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04004808
Zheng Yan1a40e232008-09-26 10:09:34 -04004809 ret = btrfs_free_extent(trans, root,
4810 bytenr, num_bytes, leaf->start,
4811 btrfs_header_owner(leaf),
4812 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004813 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04004814 BUG_ON(ret);
4815 cond_resched();
4816 }
4817 kfree(new_extent);
4818 BUG_ON(ext_index + 1 != ref->nritems);
4819 btrfs_free_leaf_ref(root, ref);
4820 return 0;
4821}
4822
Yan Zhengf82d02d2008-10-29 14:49:05 -04004823int btrfs_free_reloc_root(struct btrfs_trans_handle *trans,
4824 struct btrfs_root *root)
Zheng Yan1a40e232008-09-26 10:09:34 -04004825{
4826 struct btrfs_root *reloc_root;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004827 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04004828
4829 if (root->reloc_root) {
4830 reloc_root = root->reloc_root;
4831 root->reloc_root = NULL;
4832 list_add(&reloc_root->dead_list,
4833 &root->fs_info->dead_reloc_roots);
Yan Zhengf82d02d2008-10-29 14:49:05 -04004834
4835 btrfs_set_root_bytenr(&reloc_root->root_item,
4836 reloc_root->node->start);
4837 btrfs_set_root_level(&root->root_item,
4838 btrfs_header_level(reloc_root->node));
4839 memset(&reloc_root->root_item.drop_progress, 0,
4840 sizeof(struct btrfs_disk_key));
4841 reloc_root->root_item.drop_level = 0;
4842
4843 ret = btrfs_update_root(trans, root->fs_info->tree_root,
4844 &reloc_root->root_key,
4845 &reloc_root->root_item);
4846 BUG_ON(ret);
Zheng Yan1a40e232008-09-26 10:09:34 -04004847 }
4848 return 0;
4849}
4850
4851int btrfs_drop_dead_reloc_roots(struct btrfs_root *root)
4852{
4853 struct btrfs_trans_handle *trans;
4854 struct btrfs_root *reloc_root;
4855 struct btrfs_root *prev_root = NULL;
4856 struct list_head dead_roots;
4857 int ret;
4858 unsigned long nr;
4859
4860 INIT_LIST_HEAD(&dead_roots);
4861 list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots);
4862
4863 while (!list_empty(&dead_roots)) {
4864 reloc_root = list_entry(dead_roots.prev,
4865 struct btrfs_root, dead_list);
4866 list_del_init(&reloc_root->dead_list);
4867
4868 BUG_ON(reloc_root->commit_root != NULL);
4869 while (1) {
4870 trans = btrfs_join_transaction(root, 1);
4871 BUG_ON(!trans);
4872
4873 mutex_lock(&root->fs_info->drop_mutex);
4874 ret = btrfs_drop_snapshot(trans, reloc_root);
4875 if (ret != -EAGAIN)
4876 break;
4877 mutex_unlock(&root->fs_info->drop_mutex);
4878
4879 nr = trans->blocks_used;
4880 ret = btrfs_end_transaction(trans, root);
4881 BUG_ON(ret);
4882 btrfs_btree_balance_dirty(root, nr);
4883 }
4884
4885 free_extent_buffer(reloc_root->node);
4886
4887 ret = btrfs_del_root(trans, root->fs_info->tree_root,
4888 &reloc_root->root_key);
4889 BUG_ON(ret);
4890 mutex_unlock(&root->fs_info->drop_mutex);
4891
4892 nr = trans->blocks_used;
4893 ret = btrfs_end_transaction(trans, root);
4894 BUG_ON(ret);
4895 btrfs_btree_balance_dirty(root, nr);
4896
4897 kfree(prev_root);
4898 prev_root = reloc_root;
4899 }
4900 if (prev_root) {
4901 btrfs_remove_leaf_refs(prev_root, (u64)-1, 0);
4902 kfree(prev_root);
4903 }
4904 return 0;
4905}
4906
4907int btrfs_add_dead_reloc_root(struct btrfs_root *root)
4908{
4909 list_add(&root->dead_list, &root->fs_info->dead_reloc_roots);
4910 return 0;
4911}
4912
4913int btrfs_cleanup_reloc_trees(struct btrfs_root *root)
4914{
4915 struct btrfs_root *reloc_root;
4916 struct btrfs_trans_handle *trans;
4917 struct btrfs_key location;
4918 int found;
4919 int ret;
4920
4921 mutex_lock(&root->fs_info->tree_reloc_mutex);
4922 ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL);
4923 BUG_ON(ret);
4924 found = !list_empty(&root->fs_info->dead_reloc_roots);
4925 mutex_unlock(&root->fs_info->tree_reloc_mutex);
4926
4927 if (found) {
4928 trans = btrfs_start_transaction(root, 1);
4929 BUG_ON(!trans);
4930 ret = btrfs_commit_transaction(trans, root);
4931 BUG_ON(ret);
4932 }
4933
4934 location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
4935 location.offset = (u64)-1;
4936 location.type = BTRFS_ROOT_ITEM_KEY;
4937
4938 reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
4939 BUG_ON(!reloc_root);
4940 btrfs_orphan_cleanup(reloc_root);
4941 return 0;
4942}
4943
Chris Masond3977122009-01-05 21:25:51 -05004944static noinline int init_reloc_tree(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04004945 struct btrfs_root *root)
4946{
4947 struct btrfs_root *reloc_root;
4948 struct extent_buffer *eb;
4949 struct btrfs_root_item *root_item;
4950 struct btrfs_key root_key;
4951 int ret;
4952
4953 BUG_ON(!root->ref_cows);
4954 if (root->reloc_root)
4955 return 0;
4956
4957 root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
4958 BUG_ON(!root_item);
4959
4960 ret = btrfs_copy_root(trans, root, root->commit_root,
4961 &eb, BTRFS_TREE_RELOC_OBJECTID);
4962 BUG_ON(ret);
4963
4964 root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
4965 root_key.offset = root->root_key.objectid;
4966 root_key.type = BTRFS_ROOT_ITEM_KEY;
4967
4968 memcpy(root_item, &root->root_item, sizeof(root_item));
4969 btrfs_set_root_refs(root_item, 0);
4970 btrfs_set_root_bytenr(root_item, eb->start);
4971 btrfs_set_root_level(root_item, btrfs_header_level(eb));
Yan Zheng84234f32008-10-29 14:49:05 -04004972 btrfs_set_root_generation(root_item, trans->transid);
Zheng Yan1a40e232008-09-26 10:09:34 -04004973
4974 btrfs_tree_unlock(eb);
4975 free_extent_buffer(eb);
4976
4977 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
4978 &root_key, root_item);
4979 BUG_ON(ret);
4980 kfree(root_item);
4981
4982 reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
4983 &root_key);
4984 BUG_ON(!reloc_root);
4985 reloc_root->last_trans = trans->transid;
4986 reloc_root->commit_root = NULL;
4987 reloc_root->ref_tree = &root->fs_info->reloc_ref_tree;
4988
4989 root->reloc_root = reloc_root;
4990 return 0;
4991}
4992
4993/*
4994 * Core function of space balance.
4995 *
4996 * The idea is using reloc trees to relocate tree blocks in reference
Yan Zhengf82d02d2008-10-29 14:49:05 -04004997 * counted roots. There is one reloc tree for each subvol, and all
4998 * reloc trees share same root key objectid. Reloc trees are snapshots
4999 * of the latest committed roots of subvols (root->commit_root).
5000 *
5001 * To relocate a tree block referenced by a subvol, there are two steps.
5002 * COW the block through subvol's reloc tree, then update block pointer
5003 * in the subvol to point to the new block. Since all reloc trees share
5004 * same root key objectid, doing special handing for tree blocks owned
5005 * by them is easy. Once a tree block has been COWed in one reloc tree,
5006 * we can use the resulting new block directly when the same block is
5007 * required to COW again through other reloc trees. By this way, relocated
5008 * tree blocks are shared between reloc trees, so they are also shared
5009 * between subvols.
Zheng Yan1a40e232008-09-26 10:09:34 -04005010 */
Chris Masond3977122009-01-05 21:25:51 -05005011static noinline int relocate_one_path(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04005012 struct btrfs_root *root,
5013 struct btrfs_path *path,
5014 struct btrfs_key *first_key,
5015 struct btrfs_ref_path *ref_path,
5016 struct btrfs_block_group_cache *group,
5017 struct inode *reloc_inode)
5018{
5019 struct btrfs_root *reloc_root;
5020 struct extent_buffer *eb = NULL;
5021 struct btrfs_key *keys;
5022 u64 *nodes;
5023 int level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005024 int shared_level;
Zheng Yan1a40e232008-09-26 10:09:34 -04005025 int lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04005026 int ret;
5027
5028 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
5029 lowest_level = ref_path->owner_objectid;
5030
Yan Zhengf82d02d2008-10-29 14:49:05 -04005031 if (!root->ref_cows) {
Zheng Yan1a40e232008-09-26 10:09:34 -04005032 path->lowest_level = lowest_level;
5033 ret = btrfs_search_slot(trans, root, first_key, path, 0, 1);
5034 BUG_ON(ret < 0);
5035 path->lowest_level = 0;
5036 btrfs_release_path(root, path);
5037 return 0;
5038 }
5039
Zheng Yan1a40e232008-09-26 10:09:34 -04005040 mutex_lock(&root->fs_info->tree_reloc_mutex);
5041 ret = init_reloc_tree(trans, root);
5042 BUG_ON(ret);
5043 reloc_root = root->reloc_root;
5044
Yan Zhengf82d02d2008-10-29 14:49:05 -04005045 shared_level = ref_path->shared_level;
5046 ref_path->shared_level = BTRFS_MAX_LEVEL - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04005047
Yan Zhengf82d02d2008-10-29 14:49:05 -04005048 keys = ref_path->node_keys;
5049 nodes = ref_path->new_nodes;
5050 memset(&keys[shared_level + 1], 0,
5051 sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1));
5052 memset(&nodes[shared_level + 1], 0,
5053 sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1));
Zheng Yan1a40e232008-09-26 10:09:34 -04005054
Yan Zhengf82d02d2008-10-29 14:49:05 -04005055 if (nodes[lowest_level] == 0) {
5056 path->lowest_level = lowest_level;
5057 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
5058 0, 1);
5059 BUG_ON(ret);
5060 for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) {
5061 eb = path->nodes[level];
5062 if (!eb || eb == reloc_root->node)
5063 break;
5064 nodes[level] = eb->start;
5065 if (level == 0)
5066 btrfs_item_key_to_cpu(eb, &keys[level], 0);
5067 else
5068 btrfs_node_key_to_cpu(eb, &keys[level], 0);
5069 }
Yan Zheng2b820322008-11-17 21:11:30 -05005070 if (nodes[0] &&
5071 ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04005072 eb = path->nodes[0];
5073 ret = replace_extents_in_leaf(trans, reloc_root, eb,
5074 group, reloc_inode);
5075 BUG_ON(ret);
5076 }
5077 btrfs_release_path(reloc_root, path);
5078 } else {
Zheng Yan1a40e232008-09-26 10:09:34 -04005079 ret = btrfs_merge_path(trans, reloc_root, keys, nodes,
Yan Zhengf82d02d2008-10-29 14:49:05 -04005080 lowest_level);
Zheng Yan1a40e232008-09-26 10:09:34 -04005081 BUG_ON(ret);
5082 }
5083
Zheng Yan1a40e232008-09-26 10:09:34 -04005084 /*
5085 * replace tree blocks in the fs tree with tree blocks in
5086 * the reloc tree.
5087 */
5088 ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level);
5089 BUG_ON(ret < 0);
5090
5091 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04005092 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
5093 0, 0);
5094 BUG_ON(ret);
5095 extent_buffer_get(path->nodes[0]);
5096 eb = path->nodes[0];
5097 btrfs_release_path(reloc_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005098 ret = invalidate_extent_cache(reloc_root, eb, group, root);
5099 BUG_ON(ret);
5100 free_extent_buffer(eb);
5101 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005102
Yan Zhengf82d02d2008-10-29 14:49:05 -04005103 mutex_unlock(&root->fs_info->tree_reloc_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04005104 path->lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04005105 return 0;
5106}
5107
Chris Masond3977122009-01-05 21:25:51 -05005108static noinline int relocate_tree_block(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04005109 struct btrfs_root *root,
5110 struct btrfs_path *path,
5111 struct btrfs_key *first_key,
5112 struct btrfs_ref_path *ref_path)
5113{
5114 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04005115
5116 ret = relocate_one_path(trans, root, path, first_key,
5117 ref_path, NULL, NULL);
5118 BUG_ON(ret);
5119
Zheng Yan1a40e232008-09-26 10:09:34 -04005120 return 0;
5121}
5122
Chris Masond3977122009-01-05 21:25:51 -05005123static noinline int del_extent_zero(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04005124 struct btrfs_root *extent_root,
5125 struct btrfs_path *path,
5126 struct btrfs_key *extent_key)
5127{
5128 int ret;
5129
Zheng Yan1a40e232008-09-26 10:09:34 -04005130 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
5131 if (ret)
5132 goto out;
5133 ret = btrfs_del_item(trans, extent_root, path);
5134out:
Chris Masonedbd8d42007-12-21 16:27:24 -05005135 btrfs_release_path(extent_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005136 return ret;
5137}
5138
Chris Masond3977122009-01-05 21:25:51 -05005139static noinline struct btrfs_root *read_ref_root(struct btrfs_fs_info *fs_info,
Zheng Yan1a40e232008-09-26 10:09:34 -04005140 struct btrfs_ref_path *ref_path)
5141{
5142 struct btrfs_key root_key;
5143
5144 root_key.objectid = ref_path->root_objectid;
5145 root_key.type = BTRFS_ROOT_ITEM_KEY;
5146 if (is_cowonly_root(ref_path->root_objectid))
5147 root_key.offset = 0;
5148 else
5149 root_key.offset = (u64)-1;
5150
5151 return btrfs_read_fs_root_no_name(fs_info, &root_key);
5152}
5153
Chris Masond3977122009-01-05 21:25:51 -05005154static noinline int relocate_one_extent(struct btrfs_root *extent_root,
Zheng Yan1a40e232008-09-26 10:09:34 -04005155 struct btrfs_path *path,
5156 struct btrfs_key *extent_key,
5157 struct btrfs_block_group_cache *group,
5158 struct inode *reloc_inode, int pass)
5159{
5160 struct btrfs_trans_handle *trans;
5161 struct btrfs_root *found_root;
5162 struct btrfs_ref_path *ref_path = NULL;
5163 struct disk_extent *new_extents = NULL;
5164 int nr_extents = 0;
5165 int loops;
5166 int ret;
5167 int level;
5168 struct btrfs_key first_key;
5169 u64 prev_block = 0;
5170
Zheng Yan1a40e232008-09-26 10:09:34 -04005171
5172 trans = btrfs_start_transaction(extent_root, 1);
5173 BUG_ON(!trans);
5174
5175 if (extent_key->objectid == 0) {
5176 ret = del_extent_zero(trans, extent_root, path, extent_key);
5177 goto out;
5178 }
5179
5180 ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS);
5181 if (!ref_path) {
Chris Masond3977122009-01-05 21:25:51 -05005182 ret = -ENOMEM;
5183 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04005184 }
5185
5186 for (loops = 0; ; loops++) {
5187 if (loops == 0) {
5188 ret = btrfs_first_ref_path(trans, extent_root, ref_path,
5189 extent_key->objectid);
5190 } else {
5191 ret = btrfs_next_ref_path(trans, extent_root, ref_path);
5192 }
5193 if (ret < 0)
5194 goto out;
5195 if (ret > 0)
5196 break;
5197
5198 if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID ||
5199 ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID)
5200 continue;
5201
5202 found_root = read_ref_root(extent_root->fs_info, ref_path);
5203 BUG_ON(!found_root);
5204 /*
5205 * for reference counted tree, only process reference paths
5206 * rooted at the latest committed root.
5207 */
5208 if (found_root->ref_cows &&
5209 ref_path->root_generation != found_root->root_key.offset)
5210 continue;
5211
5212 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
5213 if (pass == 0) {
5214 /*
5215 * copy data extents to new locations
5216 */
5217 u64 group_start = group->key.objectid;
5218 ret = relocate_data_extent(reloc_inode,
5219 extent_key,
5220 group_start);
5221 if (ret < 0)
5222 goto out;
5223 break;
5224 }
5225 level = 0;
5226 } else {
5227 level = ref_path->owner_objectid;
5228 }
5229
5230 if (prev_block != ref_path->nodes[level]) {
5231 struct extent_buffer *eb;
5232 u64 block_start = ref_path->nodes[level];
5233 u64 block_size = btrfs_level_size(found_root, level);
5234
5235 eb = read_tree_block(found_root, block_start,
5236 block_size, 0);
5237 btrfs_tree_lock(eb);
5238 BUG_ON(level != btrfs_header_level(eb));
5239
5240 if (level == 0)
5241 btrfs_item_key_to_cpu(eb, &first_key, 0);
5242 else
5243 btrfs_node_key_to_cpu(eb, &first_key, 0);
5244
5245 btrfs_tree_unlock(eb);
5246 free_extent_buffer(eb);
5247 prev_block = block_start;
5248 }
5249
Yan Zheng24562422009-02-12 14:14:53 -05005250 mutex_lock(&extent_root->fs_info->trans_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05005251 btrfs_record_root_in_trans(found_root);
Yan Zheng24562422009-02-12 14:14:53 -05005252 mutex_unlock(&extent_root->fs_info->trans_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05005253 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
5254 /*
5255 * try to update data extent references while
5256 * keeping metadata shared between snapshots.
5257 */
5258 if (pass == 1) {
5259 ret = relocate_one_path(trans, found_root,
5260 path, &first_key, ref_path,
5261 group, reloc_inode);
5262 if (ret < 0)
5263 goto out;
5264 continue;
5265 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005266 /*
5267 * use fallback method to process the remaining
5268 * references.
5269 */
5270 if (!new_extents) {
5271 u64 group_start = group->key.objectid;
Yan Zhengd899e052008-10-30 14:25:28 -04005272 new_extents = kmalloc(sizeof(*new_extents),
5273 GFP_NOFS);
5274 nr_extents = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04005275 ret = get_new_locations(reloc_inode,
5276 extent_key,
Yan Zhengd899e052008-10-30 14:25:28 -04005277 group_start, 1,
Zheng Yan1a40e232008-09-26 10:09:34 -04005278 &new_extents,
5279 &nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04005280 if (ret)
Zheng Yan1a40e232008-09-26 10:09:34 -04005281 goto out;
5282 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005283 ret = replace_one_extent(trans, found_root,
5284 path, extent_key,
5285 &first_key, ref_path,
5286 new_extents, nr_extents);
Yan Zhenge4404d62008-12-12 10:03:26 -05005287 } else {
Zheng Yan1a40e232008-09-26 10:09:34 -04005288 ret = relocate_tree_block(trans, found_root, path,
5289 &first_key, ref_path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005290 }
5291 if (ret < 0)
5292 goto out;
5293 }
5294 ret = 0;
5295out:
5296 btrfs_end_transaction(trans, extent_root);
5297 kfree(new_extents);
5298 kfree(ref_path);
Chris Masonedbd8d42007-12-21 16:27:24 -05005299 return ret;
5300}
5301
Chris Masonec44a352008-04-28 15:29:52 -04005302static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
5303{
5304 u64 num_devices;
5305 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
5306 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
5307
Yan Zheng2b820322008-11-17 21:11:30 -05005308 num_devices = root->fs_info->fs_devices->rw_devices;
Chris Masonec44a352008-04-28 15:29:52 -04005309 if (num_devices == 1) {
5310 stripped |= BTRFS_BLOCK_GROUP_DUP;
5311 stripped = flags & ~stripped;
5312
5313 /* turn raid0 into single device chunks */
5314 if (flags & BTRFS_BLOCK_GROUP_RAID0)
5315 return stripped;
5316
5317 /* turn mirroring into duplication */
5318 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
5319 BTRFS_BLOCK_GROUP_RAID10))
5320 return stripped | BTRFS_BLOCK_GROUP_DUP;
5321 return flags;
5322 } else {
5323 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04005324 if (flags & stripped)
5325 return flags;
5326
5327 stripped |= BTRFS_BLOCK_GROUP_DUP;
5328 stripped = flags & ~stripped;
5329
5330 /* switch duplicated blocks with raid1 */
5331 if (flags & BTRFS_BLOCK_GROUP_DUP)
5332 return stripped | BTRFS_BLOCK_GROUP_RAID1;
5333
5334 /* turn single device chunks into raid0 */
5335 return stripped | BTRFS_BLOCK_GROUP_RAID0;
5336 }
5337 return flags;
5338}
5339
Christoph Hellwigb2950862008-12-02 09:54:17 -05005340static int __alloc_chunk_for_shrink(struct btrfs_root *root,
Chris Mason0ef3e662008-05-24 14:04:53 -04005341 struct btrfs_block_group_cache *shrink_block_group,
5342 int force)
5343{
5344 struct btrfs_trans_handle *trans;
5345 u64 new_alloc_flags;
5346 u64 calc;
5347
Chris Masonc286ac42008-07-22 23:06:41 -04005348 spin_lock(&shrink_block_group->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04005349 if (btrfs_block_group_used(&shrink_block_group->item) > 0) {
Chris Masonc286ac42008-07-22 23:06:41 -04005350 spin_unlock(&shrink_block_group->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04005351
Chris Mason0ef3e662008-05-24 14:04:53 -04005352 trans = btrfs_start_transaction(root, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04005353 spin_lock(&shrink_block_group->lock);
Chris Mason7d9eb122008-07-08 14:19:17 -04005354
Chris Mason0ef3e662008-05-24 14:04:53 -04005355 new_alloc_flags = update_block_group_flags(root,
5356 shrink_block_group->flags);
5357 if (new_alloc_flags != shrink_block_group->flags) {
5358 calc =
5359 btrfs_block_group_used(&shrink_block_group->item);
5360 } else {
5361 calc = shrink_block_group->key.offset;
5362 }
Chris Masonc286ac42008-07-22 23:06:41 -04005363 spin_unlock(&shrink_block_group->lock);
5364
Chris Mason0ef3e662008-05-24 14:04:53 -04005365 do_chunk_alloc(trans, root->fs_info->extent_root,
5366 calc + 2 * 1024 * 1024, new_alloc_flags, force);
Chris Mason7d9eb122008-07-08 14:19:17 -04005367
Chris Mason0ef3e662008-05-24 14:04:53 -04005368 btrfs_end_transaction(trans, root);
Chris Masonc286ac42008-07-22 23:06:41 -04005369 } else
5370 spin_unlock(&shrink_block_group->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04005371 return 0;
5372}
5373
Zheng Yan1a40e232008-09-26 10:09:34 -04005374static int __insert_orphan_inode(struct btrfs_trans_handle *trans,
5375 struct btrfs_root *root,
5376 u64 objectid, u64 size)
5377{
5378 struct btrfs_path *path;
5379 struct btrfs_inode_item *item;
5380 struct extent_buffer *leaf;
5381 int ret;
5382
5383 path = btrfs_alloc_path();
5384 if (!path)
5385 return -ENOMEM;
5386
Chris Masonb9473432009-03-13 11:00:37 -04005387 path->leave_spinning = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04005388 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
5389 if (ret)
5390 goto out;
5391
5392 leaf = path->nodes[0];
5393 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item);
5394 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
5395 btrfs_set_inode_generation(leaf, item, 1);
5396 btrfs_set_inode_size(leaf, item, size);
5397 btrfs_set_inode_mode(leaf, item, S_IFREG | 0600);
Yan Zheng0403e472008-12-10 20:32:51 -05005398 btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NOCOMPRESS);
Zheng Yan1a40e232008-09-26 10:09:34 -04005399 btrfs_mark_buffer_dirty(leaf);
5400 btrfs_release_path(root, path);
5401out:
5402 btrfs_free_path(path);
5403 return ret;
5404}
5405
Chris Masond3977122009-01-05 21:25:51 -05005406static noinline struct inode *create_reloc_inode(struct btrfs_fs_info *fs_info,
Zheng Yan1a40e232008-09-26 10:09:34 -04005407 struct btrfs_block_group_cache *group)
5408{
5409 struct inode *inode = NULL;
5410 struct btrfs_trans_handle *trans;
5411 struct btrfs_root *root;
5412 struct btrfs_key root_key;
5413 u64 objectid = BTRFS_FIRST_FREE_OBJECTID;
5414 int err = 0;
5415
5416 root_key.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
5417 root_key.type = BTRFS_ROOT_ITEM_KEY;
5418 root_key.offset = (u64)-1;
5419 root = btrfs_read_fs_root_no_name(fs_info, &root_key);
5420 if (IS_ERR(root))
5421 return ERR_CAST(root);
5422
5423 trans = btrfs_start_transaction(root, 1);
5424 BUG_ON(!trans);
5425
5426 err = btrfs_find_free_objectid(trans, root, objectid, &objectid);
5427 if (err)
5428 goto out;
5429
5430 err = __insert_orphan_inode(trans, root, objectid, group->key.offset);
5431 BUG_ON(err);
5432
5433 err = btrfs_insert_file_extent(trans, root, objectid, 0, 0, 0,
Chris Masonc8b97812008-10-29 14:49:59 -04005434 group->key.offset, 0, group->key.offset,
5435 0, 0, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04005436 BUG_ON(err);
5437
5438 inode = btrfs_iget_locked(root->fs_info->sb, objectid, root);
5439 if (inode->i_state & I_NEW) {
5440 BTRFS_I(inode)->root = root;
5441 BTRFS_I(inode)->location.objectid = objectid;
5442 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
5443 BTRFS_I(inode)->location.offset = 0;
5444 btrfs_read_locked_inode(inode);
5445 unlock_new_inode(inode);
5446 BUG_ON(is_bad_inode(inode));
5447 } else {
5448 BUG_ON(1);
5449 }
Yan Zheng17d217f2008-12-12 10:03:38 -05005450 BTRFS_I(inode)->index_cnt = group->key.objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04005451
5452 err = btrfs_orphan_add(trans, inode);
5453out:
5454 btrfs_end_transaction(trans, root);
5455 if (err) {
5456 if (inode)
5457 iput(inode);
5458 inode = ERR_PTR(err);
5459 }
5460 return inode;
5461}
5462
Yan Zheng17d217f2008-12-12 10:03:38 -05005463int btrfs_reloc_clone_csums(struct inode *inode, u64 file_pos, u64 len)
5464{
5465
5466 struct btrfs_ordered_sum *sums;
5467 struct btrfs_sector_sum *sector_sum;
5468 struct btrfs_ordered_extent *ordered;
5469 struct btrfs_root *root = BTRFS_I(inode)->root;
5470 struct list_head list;
5471 size_t offset;
5472 int ret;
5473 u64 disk_bytenr;
5474
5475 INIT_LIST_HEAD(&list);
5476
5477 ordered = btrfs_lookup_ordered_extent(inode, file_pos);
5478 BUG_ON(ordered->file_offset != file_pos || ordered->len != len);
5479
5480 disk_bytenr = file_pos + BTRFS_I(inode)->index_cnt;
Yan Zheng07d400a2009-01-06 11:42:00 -05005481 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, disk_bytenr,
Yan Zheng17d217f2008-12-12 10:03:38 -05005482 disk_bytenr + len - 1, &list);
5483
5484 while (!list_empty(&list)) {
5485 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
5486 list_del_init(&sums->list);
5487
5488 sector_sum = sums->sums;
5489 sums->bytenr = ordered->start;
5490
5491 offset = 0;
5492 while (offset < sums->len) {
5493 sector_sum->bytenr += ordered->start - disk_bytenr;
5494 sector_sum++;
5495 offset += root->sectorsize;
5496 }
5497
5498 btrfs_add_ordered_sum(inode, ordered, sums);
5499 }
5500 btrfs_put_ordered_extent(ordered);
5501 return 0;
5502}
5503
Zheng Yan1a40e232008-09-26 10:09:34 -04005504int btrfs_relocate_block_group(struct btrfs_root *root, u64 group_start)
Chris Masonedbd8d42007-12-21 16:27:24 -05005505{
5506 struct btrfs_trans_handle *trans;
Chris Masonedbd8d42007-12-21 16:27:24 -05005507 struct btrfs_path *path;
Zheng Yan1a40e232008-09-26 10:09:34 -04005508 struct btrfs_fs_info *info = root->fs_info;
5509 struct extent_buffer *leaf;
5510 struct inode *reloc_inode;
5511 struct btrfs_block_group_cache *block_group;
5512 struct btrfs_key key;
Yan Zhengd899e052008-10-30 14:25:28 -04005513 u64 skipped;
Chris Masonedbd8d42007-12-21 16:27:24 -05005514 u64 cur_byte;
5515 u64 total_found;
Chris Masonedbd8d42007-12-21 16:27:24 -05005516 u32 nritems;
5517 int ret;
Chris Masona061fc82008-05-07 11:43:44 -04005518 int progress;
Zheng Yan1a40e232008-09-26 10:09:34 -04005519 int pass = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05005520
Chris Masonedbd8d42007-12-21 16:27:24 -05005521 root = root->fs_info->extent_root;
Zheng Yan1a40e232008-09-26 10:09:34 -04005522
5523 block_group = btrfs_lookup_block_group(info, group_start);
5524 BUG_ON(!block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05005525
Chris Masond3977122009-01-05 21:25:51 -05005526 printk(KERN_INFO "btrfs relocating block group %llu flags %llu\n",
Zheng Yan1a40e232008-09-26 10:09:34 -04005527 (unsigned long long)block_group->key.objectid,
5528 (unsigned long long)block_group->flags);
Chris Mason323da792008-05-09 11:46:48 -04005529
Zheng Yan1a40e232008-09-26 10:09:34 -04005530 path = btrfs_alloc_path();
5531 BUG_ON(!path);
Chris Mason323da792008-05-09 11:46:48 -04005532
Zheng Yan1a40e232008-09-26 10:09:34 -04005533 reloc_inode = create_reloc_inode(info, block_group);
5534 BUG_ON(IS_ERR(reloc_inode));
5535
Zheng Yan1a40e232008-09-26 10:09:34 -04005536 __alloc_chunk_for_shrink(root, block_group, 1);
Yan Zhengc146afa2008-11-12 14:34:12 -05005537 set_block_group_readonly(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04005538
Zheng Yan1a40e232008-09-26 10:09:34 -04005539 btrfs_start_delalloc_inodes(info->tree_root);
5540 btrfs_wait_ordered_extents(info->tree_root, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -04005541again:
Yan Zhengd899e052008-10-30 14:25:28 -04005542 skipped = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05005543 total_found = 0;
Chris Masona061fc82008-05-07 11:43:44 -04005544 progress = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04005545 key.objectid = block_group->key.objectid;
Chris Masonedbd8d42007-12-21 16:27:24 -05005546 key.offset = 0;
5547 key.type = 0;
Yan73e48b22008-01-03 14:14:39 -05005548 cur_byte = key.objectid;
Chris Mason4313b392008-01-03 09:08:48 -05005549
Zheng Yan1a40e232008-09-26 10:09:34 -04005550 trans = btrfs_start_transaction(info->tree_root, 1);
5551 btrfs_commit_transaction(trans, info->tree_root);
Chris Masonea8c2812008-08-04 23:17:27 -04005552
Zheng Yan1a40e232008-09-26 10:09:34 -04005553 mutex_lock(&root->fs_info->cleaner_mutex);
5554 btrfs_clean_old_snapshots(info->tree_root);
5555 btrfs_remove_leaf_refs(info->tree_root, (u64)-1, 1);
5556 mutex_unlock(&root->fs_info->cleaner_mutex);
Chris Masonea8c2812008-08-04 23:17:27 -04005557
Chris Mason56bec292009-03-13 10:10:06 -04005558 trans = btrfs_start_transaction(info->tree_root, 1);
5559 btrfs_commit_transaction(trans, info->tree_root);
5560
Chris Masond3977122009-01-05 21:25:51 -05005561 while (1) {
Chris Masonedbd8d42007-12-21 16:27:24 -05005562 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5563 if (ret < 0)
5564 goto out;
Chris Mason7d9eb122008-07-08 14:19:17 -04005565next:
Chris Masonedbd8d42007-12-21 16:27:24 -05005566 leaf = path->nodes[0];
Chris Masonedbd8d42007-12-21 16:27:24 -05005567 nritems = btrfs_header_nritems(leaf);
Yan73e48b22008-01-03 14:14:39 -05005568 if (path->slots[0] >= nritems) {
5569 ret = btrfs_next_leaf(root, path);
5570 if (ret < 0)
5571 goto out;
5572 if (ret == 1) {
5573 ret = 0;
5574 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05005575 }
Yan73e48b22008-01-03 14:14:39 -05005576 leaf = path->nodes[0];
5577 nritems = btrfs_header_nritems(leaf);
5578 }
5579
Zheng Yan1a40e232008-09-26 10:09:34 -04005580 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Mason725c8462008-01-04 16:47:16 -05005581
Zheng Yan1a40e232008-09-26 10:09:34 -04005582 if (key.objectid >= block_group->key.objectid +
5583 block_group->key.offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04005584 break;
5585
Chris Mason725c8462008-01-04 16:47:16 -05005586 if (progress && need_resched()) {
Chris Mason725c8462008-01-04 16:47:16 -05005587 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005588 cond_resched();
Chris Mason725c8462008-01-04 16:47:16 -05005589 progress = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04005590 continue;
Chris Mason725c8462008-01-04 16:47:16 -05005591 }
5592 progress = 1;
5593
Zheng Yan1a40e232008-09-26 10:09:34 -04005594 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY ||
5595 key.objectid + key.offset <= cur_byte) {
Yan73e48b22008-01-03 14:14:39 -05005596 path->slots[0]++;
Chris Masonedbd8d42007-12-21 16:27:24 -05005597 goto next;
5598 }
Yan73e48b22008-01-03 14:14:39 -05005599
Chris Masonedbd8d42007-12-21 16:27:24 -05005600 total_found++;
Zheng Yan1a40e232008-09-26 10:09:34 -04005601 cur_byte = key.objectid + key.offset;
Chris Masonedbd8d42007-12-21 16:27:24 -05005602 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005603
5604 __alloc_chunk_for_shrink(root, block_group, 0);
5605 ret = relocate_one_extent(root, path, &key, block_group,
5606 reloc_inode, pass);
5607 BUG_ON(ret < 0);
Yan Zhengd899e052008-10-30 14:25:28 -04005608 if (ret > 0)
5609 skipped++;
Zheng Yan1a40e232008-09-26 10:09:34 -04005610
5611 key.objectid = cur_byte;
5612 key.type = 0;
5613 key.offset = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05005614 }
5615
5616 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005617
5618 if (pass == 0) {
5619 btrfs_wait_ordered_range(reloc_inode, 0, (u64)-1);
5620 invalidate_mapping_pages(reloc_inode->i_mapping, 0, -1);
Zheng Yan1a40e232008-09-26 10:09:34 -04005621 }
Chris Masonedbd8d42007-12-21 16:27:24 -05005622
5623 if (total_found > 0) {
Chris Masond3977122009-01-05 21:25:51 -05005624 printk(KERN_INFO "btrfs found %llu extents in pass %d\n",
Zheng Yan1a40e232008-09-26 10:09:34 -04005625 (unsigned long long)total_found, pass);
5626 pass++;
Yan Zhengd899e052008-10-30 14:25:28 -04005627 if (total_found == skipped && pass > 2) {
5628 iput(reloc_inode);
5629 reloc_inode = create_reloc_inode(info, block_group);
5630 pass = 0;
5631 }
Chris Masonedbd8d42007-12-21 16:27:24 -05005632 goto again;
5633 }
5634
Zheng Yan1a40e232008-09-26 10:09:34 -04005635 /* delete reloc_inode */
5636 iput(reloc_inode);
Chris Mason7d9eb122008-07-08 14:19:17 -04005637
Zheng Yan1a40e232008-09-26 10:09:34 -04005638 /* unpin extents in this range */
5639 trans = btrfs_start_transaction(info->tree_root, 1);
5640 btrfs_commit_transaction(trans, info->tree_root);
Chris Mason0ef3e662008-05-24 14:04:53 -04005641
Zheng Yan1a40e232008-09-26 10:09:34 -04005642 spin_lock(&block_group->lock);
5643 WARN_ON(block_group->pinned > 0);
5644 WARN_ON(block_group->reserved > 0);
5645 WARN_ON(btrfs_block_group_used(&block_group->item) > 0);
5646 spin_unlock(&block_group->lock);
Yan Zhengd2fb3432008-12-11 16:30:39 -05005647 put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04005648 ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05005649out:
Zheng Yan1a40e232008-09-26 10:09:34 -04005650 btrfs_free_path(path);
Chris Masonedbd8d42007-12-21 16:27:24 -05005651 return ret;
5652}
5653
Christoph Hellwigb2950862008-12-02 09:54:17 -05005654static int find_first_block_group(struct btrfs_root *root,
5655 struct btrfs_path *path, struct btrfs_key *key)
Chris Mason0b86a832008-03-24 15:01:56 -04005656{
Chris Mason925baed2008-06-25 16:01:30 -04005657 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005658 struct btrfs_key found_key;
5659 struct extent_buffer *leaf;
5660 int slot;
5661
5662 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
5663 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04005664 goto out;
5665
Chris Masond3977122009-01-05 21:25:51 -05005666 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04005667 slot = path->slots[0];
5668 leaf = path->nodes[0];
5669 if (slot >= btrfs_header_nritems(leaf)) {
5670 ret = btrfs_next_leaf(root, path);
5671 if (ret == 0)
5672 continue;
5673 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04005674 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04005675 break;
5676 }
5677 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5678
5679 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04005680 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
5681 ret = 0;
5682 goto out;
5683 }
Chris Mason0b86a832008-03-24 15:01:56 -04005684 path->slots[0]++;
5685 }
5686 ret = -ENOENT;
Chris Mason925baed2008-06-25 16:01:30 -04005687out:
Chris Mason0b86a832008-03-24 15:01:56 -04005688 return ret;
5689}
5690
Zheng Yan1a40e232008-09-26 10:09:34 -04005691int btrfs_free_block_groups(struct btrfs_fs_info *info)
5692{
5693 struct btrfs_block_group_cache *block_group;
Chris Mason4184ea72009-03-10 12:39:20 -04005694 struct btrfs_space_info *space_info;
Zheng Yan1a40e232008-09-26 10:09:34 -04005695 struct rb_node *n;
5696
Zheng Yan1a40e232008-09-26 10:09:34 -04005697 spin_lock(&info->block_group_cache_lock);
5698 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
5699 block_group = rb_entry(n, struct btrfs_block_group_cache,
5700 cache_node);
Zheng Yan1a40e232008-09-26 10:09:34 -04005701 rb_erase(&block_group->cache_node,
5702 &info->block_group_cache_tree);
Yan Zhengd899e052008-10-30 14:25:28 -04005703 spin_unlock(&info->block_group_cache_lock);
5704
5705 btrfs_remove_free_space_cache(block_group);
Josef Bacik80eb2342008-10-29 14:49:05 -04005706 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04005707 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04005708 up_write(&block_group->space_info->groups_sem);
Yan Zhengd2fb3432008-12-11 16:30:39 -05005709
5710 WARN_ON(atomic_read(&block_group->count) != 1);
Zheng Yan1a40e232008-09-26 10:09:34 -04005711 kfree(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04005712
5713 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04005714 }
5715 spin_unlock(&info->block_group_cache_lock);
Chris Mason4184ea72009-03-10 12:39:20 -04005716
5717 /* now that all the block groups are freed, go through and
5718 * free all the space_info structs. This is only called during
5719 * the final stages of unmount, and so we know nobody is
5720 * using them. We call synchronize_rcu() once before we start,
5721 * just to be on the safe side.
5722 */
5723 synchronize_rcu();
5724
5725 while(!list_empty(&info->space_info)) {
5726 space_info = list_entry(info->space_info.next,
5727 struct btrfs_space_info,
5728 list);
5729
5730 list_del(&space_info->list);
5731 kfree(space_info);
5732 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005733 return 0;
5734}
5735
Chris Mason9078a3e2007-04-26 16:46:15 -04005736int btrfs_read_block_groups(struct btrfs_root *root)
5737{
5738 struct btrfs_path *path;
5739 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04005740 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04005741 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04005742 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04005743 struct btrfs_key key;
5744 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04005745 struct extent_buffer *leaf;
Chris Mason96b51792007-10-15 16:15:19 -04005746
Chris Masonbe744172007-05-06 10:15:01 -04005747 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04005748 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005749 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04005750 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04005751 path = btrfs_alloc_path();
5752 if (!path)
5753 return -ENOMEM;
5754
Chris Masond3977122009-01-05 21:25:51 -05005755 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04005756 ret = find_first_block_group(root, path, &key);
5757 if (ret > 0) {
5758 ret = 0;
5759 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04005760 }
Chris Mason0b86a832008-03-24 15:01:56 -04005761 if (ret != 0)
5762 goto error;
5763
Chris Mason5f39d392007-10-15 16:14:19 -04005764 leaf = path->nodes[0];
5765 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04005766 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04005767 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04005768 ret = -ENOMEM;
Chris Mason9078a3e2007-04-26 16:46:15 -04005769 break;
5770 }
Chris Mason3e1ad542007-05-07 20:03:49 -04005771
Yan Zhengd2fb3432008-12-11 16:30:39 -05005772 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04005773 spin_lock_init(&cache->lock);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04005774 spin_lock_init(&cache->tree_lock);
Josef Bacikea6a4782008-11-20 12:16:16 -05005775 mutex_init(&cache->cache_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005776 INIT_LIST_HEAD(&cache->list);
Chris Mason5f39d392007-10-15 16:14:19 -04005777 read_extent_buffer(leaf, &cache->item,
5778 btrfs_item_ptr_offset(leaf, path->slots[0]),
5779 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04005780 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04005781
Chris Mason9078a3e2007-04-26 16:46:15 -04005782 key.objectid = found_key.objectid + found_key.offset;
5783 btrfs_release_path(root, path);
Chris Mason0b86a832008-03-24 15:01:56 -04005784 cache->flags = btrfs_block_group_flags(&cache->item);
Chris Mason96b51792007-10-15 16:15:19 -04005785
Chris Mason6324fbf2008-03-24 15:01:59 -04005786 ret = update_space_info(info, cache->flags, found_key.offset,
5787 btrfs_block_group_used(&cache->item),
5788 &space_info);
5789 BUG_ON(ret);
5790 cache->space_info = space_info;
Josef Bacik80eb2342008-10-29 14:49:05 -04005791 down_write(&space_info->groups_sem);
5792 list_add_tail(&cache->list, &space_info->block_groups);
5793 up_write(&space_info->groups_sem);
Chris Mason6324fbf2008-03-24 15:01:59 -04005794
Josef Bacik0f9dd462008-09-23 13:14:11 -04005795 ret = btrfs_add_block_group_cache(root->fs_info, cache);
5796 BUG_ON(ret);
Chris Mason75ccf472008-09-30 19:24:06 -04005797
5798 set_avail_alloc_bits(root->fs_info, cache->flags);
Yan Zheng2b820322008-11-17 21:11:30 -05005799 if (btrfs_chunk_readonly(root, cache->key.objectid))
5800 set_block_group_readonly(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04005801 }
Chris Mason0b86a832008-03-24 15:01:56 -04005802 ret = 0;
5803error:
Chris Mason9078a3e2007-04-26 16:46:15 -04005804 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04005805 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04005806}
Chris Mason6324fbf2008-03-24 15:01:59 -04005807
5808int btrfs_make_block_group(struct btrfs_trans_handle *trans,
5809 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04005810 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04005811 u64 size)
5812{
5813 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04005814 struct btrfs_root *extent_root;
5815 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04005816
5817 extent_root = root->fs_info->extent_root;
Chris Mason6324fbf2008-03-24 15:01:59 -04005818
Chris Mason12fcfd22009-03-24 10:24:20 -04005819 root->fs_info->last_trans_log_full_commit = trans->transid;
Chris Masone02119d2008-09-05 16:13:11 -04005820
Chris Mason8f18cf12008-04-25 16:53:30 -04005821 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005822 if (!cache)
5823 return -ENOMEM;
5824
Chris Masone17cade2008-04-15 15:41:47 -04005825 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04005826 cache->key.offset = size;
Yan Zhengd2fb3432008-12-11 16:30:39 -05005827 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
5828 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04005829 spin_lock_init(&cache->lock);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04005830 spin_lock_init(&cache->tree_lock);
Josef Bacikea6a4782008-11-20 12:16:16 -05005831 mutex_init(&cache->cache_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005832 INIT_LIST_HEAD(&cache->list);
Chris Mason0ef3e662008-05-24 14:04:53 -04005833
Chris Mason6324fbf2008-03-24 15:01:59 -04005834 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04005835 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
5836 cache->flags = type;
5837 btrfs_set_block_group_flags(&cache->item, type);
5838
5839 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
5840 &cache->space_info);
5841 BUG_ON(ret);
Josef Bacik80eb2342008-10-29 14:49:05 -04005842 down_write(&cache->space_info->groups_sem);
5843 list_add_tail(&cache->list, &cache->space_info->block_groups);
5844 up_write(&cache->space_info->groups_sem);
Chris Mason6324fbf2008-03-24 15:01:59 -04005845
Josef Bacik0f9dd462008-09-23 13:14:11 -04005846 ret = btrfs_add_block_group_cache(root->fs_info, cache);
5847 BUG_ON(ret);
Chris Masonc286ac42008-07-22 23:06:41 -04005848
Chris Mason6324fbf2008-03-24 15:01:59 -04005849 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
5850 sizeof(cache->item));
5851 BUG_ON(ret);
5852
Chris Masond18a2c42008-04-04 15:40:00 -04005853 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04005854
Chris Mason6324fbf2008-03-24 15:01:59 -04005855 return 0;
5856}
Zheng Yan1a40e232008-09-26 10:09:34 -04005857
5858int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
5859 struct btrfs_root *root, u64 group_start)
5860{
5861 struct btrfs_path *path;
5862 struct btrfs_block_group_cache *block_group;
5863 struct btrfs_key key;
5864 int ret;
5865
Zheng Yan1a40e232008-09-26 10:09:34 -04005866 root = root->fs_info->extent_root;
5867
5868 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
5869 BUG_ON(!block_group);
Yan Zhengc146afa2008-11-12 14:34:12 -05005870 BUG_ON(!block_group->ro);
Zheng Yan1a40e232008-09-26 10:09:34 -04005871
5872 memcpy(&key, &block_group->key, sizeof(key));
5873
5874 path = btrfs_alloc_path();
5875 BUG_ON(!path);
5876
Yan Zheng3dfdb932009-01-21 10:49:16 -05005877 spin_lock(&root->fs_info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04005878 rb_erase(&block_group->cache_node,
5879 &root->fs_info->block_group_cache_tree);
Yan Zheng3dfdb932009-01-21 10:49:16 -05005880 spin_unlock(&root->fs_info->block_group_cache_lock);
5881 btrfs_remove_free_space_cache(block_group);
Josef Bacik80eb2342008-10-29 14:49:05 -04005882 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04005883 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04005884 up_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04005885
Yan Zhengc146afa2008-11-12 14:34:12 -05005886 spin_lock(&block_group->space_info->lock);
5887 block_group->space_info->total_bytes -= block_group->key.offset;
5888 block_group->space_info->bytes_readonly -= block_group->key.offset;
5889 spin_unlock(&block_group->space_info->lock);
Yan Zheng2b820322008-11-17 21:11:30 -05005890 block_group->space_info->full = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05005891
Yan Zhengd2fb3432008-12-11 16:30:39 -05005892 put_block_group(block_group);
5893 put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04005894
5895 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
5896 if (ret > 0)
5897 ret = -EIO;
5898 if (ret < 0)
5899 goto out;
5900
5901 ret = btrfs_del_item(trans, root, path);
5902out:
5903 btrfs_free_path(path);
5904 return ret;
5905}