blob: f5e7cae63d80f8828f711833b58600a8bce71df9 [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
Josef Bacik25179202008-10-29 14:49:05 -0400169 mutex_lock(&info->pinned_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400170 while (start < end) {
171 ret = find_first_extent_bit(&info->pinned_extents, start,
172 &extent_start, &extent_end,
173 EXTENT_DIRTY);
174 if (ret)
175 break;
176
177 if (extent_start == start) {
178 start = extent_end + 1;
179 } else if (extent_start > start && extent_start < end) {
180 size = extent_start - start;
Josef Bacikea6a4782008-11-20 12:16:16 -0500181 ret = btrfs_add_free_space(block_group, start,
182 size);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400183 BUG_ON(ret);
184 start = extent_end + 1;
185 } else {
186 break;
187 }
188 }
189
190 if (start < end) {
191 size = end - start;
Josef Bacikea6a4782008-11-20 12:16:16 -0500192 ret = btrfs_add_free_space(block_group, start, size);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400193 BUG_ON(ret);
194 }
Josef Bacik25179202008-10-29 14:49:05 -0400195 mutex_unlock(&info->pinned_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400196
197 return 0;
198}
199
Yan Zhenga512bbf2008-12-08 16:46:26 -0500200static int remove_sb_from_cache(struct btrfs_root *root,
201 struct btrfs_block_group_cache *cache)
202{
203 u64 bytenr;
204 u64 *logical;
205 int stripe_len;
206 int i, nr, ret;
207
208 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
209 bytenr = btrfs_sb_offset(i);
210 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
211 cache->key.objectid, bytenr, 0,
212 &logical, &nr, &stripe_len);
213 BUG_ON(ret);
214 while (nr--) {
215 btrfs_remove_free_space(cache, logical[nr],
216 stripe_len);
217 }
218 kfree(logical);
219 }
220 return 0;
221}
222
Chris Masone37c9e62007-05-09 20:13:14 -0400223static int cache_block_group(struct btrfs_root *root,
224 struct btrfs_block_group_cache *block_group)
225{
226 struct btrfs_path *path;
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400227 int ret = 0;
Chris Masone37c9e62007-05-09 20:13:14 -0400228 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400229 struct extent_buffer *leaf;
Chris Masone37c9e62007-05-09 20:13:14 -0400230 int slot;
Yan Zhenge4404d62008-12-12 10:03:26 -0500231 u64 last;
Chris Masone37c9e62007-05-09 20:13:14 -0400232
Chris Mason00f5c792007-11-30 10:09:33 -0500233 if (!block_group)
234 return 0;
235
Chris Masone37c9e62007-05-09 20:13:14 -0400236 root = root->fs_info->extent_root;
Chris Masone37c9e62007-05-09 20:13:14 -0400237
238 if (block_group->cached)
239 return 0;
Chris Masonf510cfe2007-10-15 16:14:48 -0400240
Chris Masone37c9e62007-05-09 20:13:14 -0400241 path = btrfs_alloc_path();
242 if (!path)
243 return -ENOMEM;
Yan7d7d6062007-09-14 16:15:28 -0400244
Chris Mason2cc58cf2007-08-27 16:49:44 -0400245 path->reada = 2;
Chris Mason5cd57b22008-06-25 16:01:30 -0400246 /*
247 * we get into deadlocks with paths held by callers of this function.
248 * since the alloc_mutex is protecting things right now, just
249 * skip the locking here
250 */
251 path->skip_locking = 1;
Yan Zhenge4404d62008-12-12 10:03:26 -0500252 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
253 key.objectid = last;
Chris Masone37c9e62007-05-09 20:13:14 -0400254 key.offset = 0;
255 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
256 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
257 if (ret < 0)
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400258 goto err;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500259
Chris Masond3977122009-01-05 21:25:51 -0500260 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400261 leaf = path->nodes[0];
Chris Masone37c9e62007-05-09 20:13:14 -0400262 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -0400263 if (slot >= btrfs_header_nritems(leaf)) {
Chris Masone37c9e62007-05-09 20:13:14 -0400264 ret = btrfs_next_leaf(root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400265 if (ret < 0)
266 goto err;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400267 if (ret == 0)
Chris Masone37c9e62007-05-09 20:13:14 -0400268 continue;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400269 else
Chris Masone37c9e62007-05-09 20:13:14 -0400270 break;
Chris Masone37c9e62007-05-09 20:13:14 -0400271 }
Chris Mason5f39d392007-10-15 16:14:19 -0400272 btrfs_item_key_to_cpu(leaf, &key, slot);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400273 if (key.objectid < block_group->key.objectid)
Yan7d7d6062007-09-14 16:15:28 -0400274 goto next;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400275
Chris Masone37c9e62007-05-09 20:13:14 -0400276 if (key.objectid >= block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -0400277 block_group->key.offset)
Yan7d7d6062007-09-14 16:15:28 -0400278 break;
Yan7d7d6062007-09-14 16:15:28 -0400279
280 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
Josef Bacik0f9dd462008-09-23 13:14:11 -0400281 add_new_free_space(block_group, root->fs_info, last,
282 key.objectid);
283
Yan7d7d6062007-09-14 16:15:28 -0400284 last = key.objectid + key.offset;
Chris Masone37c9e62007-05-09 20:13:14 -0400285 }
Yan7d7d6062007-09-14 16:15:28 -0400286next:
Chris Masone37c9e62007-05-09 20:13:14 -0400287 path->slots[0]++;
288 }
289
Josef Bacik0f9dd462008-09-23 13:14:11 -0400290 add_new_free_space(block_group, root->fs_info, last,
291 block_group->key.objectid +
292 block_group->key.offset);
293
Yan Zhenga512bbf2008-12-08 16:46:26 -0500294 remove_sb_from_cache(root, block_group);
Chris Masone37c9e62007-05-09 20:13:14 -0400295 block_group->cached = 1;
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400296 ret = 0;
Chris Mason54aa1f42007-06-22 14:16:25 -0400297err:
Chris Masone37c9e62007-05-09 20:13:14 -0400298 btrfs_free_path(path);
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400299 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -0400300}
301
Josef Bacik0f9dd462008-09-23 13:14:11 -0400302/*
303 * return the block group that starts at or after bytenr
304 */
Chris Masond3977122009-01-05 21:25:51 -0500305static struct btrfs_block_group_cache *
306btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
Chris Mason0ef3e662008-05-24 14:04:53 -0400307{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400308 struct btrfs_block_group_cache *cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400309
Josef Bacik0f9dd462008-09-23 13:14:11 -0400310 cache = block_group_cache_tree_search(info, bytenr, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -0400311
Josef Bacik0f9dd462008-09-23 13:14:11 -0400312 return cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400313}
314
Josef Bacik0f9dd462008-09-23 13:14:11 -0400315/*
316 * return the block group that contains teh given bytenr
317 */
Chris Masond3977122009-01-05 21:25:51 -0500318struct btrfs_block_group_cache *btrfs_lookup_block_group(
319 struct btrfs_fs_info *info,
320 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400321{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400322 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -0400323
Josef Bacik0f9dd462008-09-23 13:14:11 -0400324 cache = block_group_cache_tree_search(info, bytenr, 1);
Chris Mason96b51792007-10-15 16:15:19 -0400325
Josef Bacik0f9dd462008-09-23 13:14:11 -0400326 return cache;
Chris Masonbe744172007-05-06 10:15:01 -0400327}
Chris Mason0b86a832008-03-24 15:01:56 -0400328
Yan Zhengd2fb3432008-12-11 16:30:39 -0500329static inline void put_block_group(struct btrfs_block_group_cache *cache)
330{
331 if (atomic_dec_and_test(&cache->count))
332 kfree(cache);
333}
334
Josef Bacik0f9dd462008-09-23 13:14:11 -0400335static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
336 u64 flags)
Chris Mason6324fbf2008-03-24 15:01:59 -0400337{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400338 struct list_head *head = &info->space_info;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400339 struct btrfs_space_info *found;
Chris Mason4184ea72009-03-10 12:39:20 -0400340
341 rcu_read_lock();
342 list_for_each_entry_rcu(found, head, list) {
343 if (found->flags == flags) {
344 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400345 return found;
Chris Mason4184ea72009-03-10 12:39:20 -0400346 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400347 }
Chris Mason4184ea72009-03-10 12:39:20 -0400348 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400349 return NULL;
Chris Mason6324fbf2008-03-24 15:01:59 -0400350}
351
Chris Mason4184ea72009-03-10 12:39:20 -0400352/*
353 * after adding space to the filesystem, we need to clear the full flags
354 * on all the space infos.
355 */
356void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
357{
358 struct list_head *head = &info->space_info;
359 struct btrfs_space_info *found;
360
361 rcu_read_lock();
362 list_for_each_entry_rcu(found, head, list)
363 found->full = 0;
364 rcu_read_unlock();
365}
366
Josef Bacik80eb2342008-10-29 14:49:05 -0400367static u64 div_factor(u64 num, int factor)
368{
369 if (factor == 10)
370 return num;
371 num *= factor;
372 do_div(num, 10);
373 return num;
374}
375
Yan Zhengd2fb3432008-12-11 16:30:39 -0500376u64 btrfs_find_block_group(struct btrfs_root *root,
377 u64 search_start, u64 search_hint, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400378{
Chris Mason96b51792007-10-15 16:15:19 -0400379 struct btrfs_block_group_cache *cache;
Chris Masoncd1bc462007-04-27 10:08:34 -0400380 u64 used;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500381 u64 last = max(search_hint, search_start);
382 u64 group_start = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400383 int full_search = 0;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500384 int factor = 9;
Chris Mason0ef3e662008-05-24 14:04:53 -0400385 int wrapped = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400386again:
Zheng Yane8569812008-09-26 10:05:48 -0400387 while (1) {
388 cache = btrfs_lookup_first_block_group(root->fs_info, last);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400389 if (!cache)
Chris Masoncd1bc462007-04-27 10:08:34 -0400390 break;
Chris Mason96b51792007-10-15 16:15:19 -0400391
Chris Masonc286ac42008-07-22 23:06:41 -0400392 spin_lock(&cache->lock);
Chris Mason96b51792007-10-15 16:15:19 -0400393 last = cache->key.objectid + cache->key.offset;
394 used = btrfs_block_group_used(&cache->item);
395
Yan Zhengd2fb3432008-12-11 16:30:39 -0500396 if ((full_search || !cache->ro) &&
397 block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) {
Zheng Yane8569812008-09-26 10:05:48 -0400398 if (used + cache->pinned + cache->reserved <
Yan Zhengd2fb3432008-12-11 16:30:39 -0500399 div_factor(cache->key.offset, factor)) {
400 group_start = cache->key.objectid;
Chris Masonc286ac42008-07-22 23:06:41 -0400401 spin_unlock(&cache->lock);
Yan Zhengd2fb3432008-12-11 16:30:39 -0500402 put_block_group(cache);
Chris Mason8790d502008-04-03 16:29:03 -0400403 goto found;
404 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400405 }
Chris Masonc286ac42008-07-22 23:06:41 -0400406 spin_unlock(&cache->lock);
Yan Zhengd2fb3432008-12-11 16:30:39 -0500407 put_block_group(cache);
Chris Masonde428b62007-05-18 13:28:27 -0400408 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400409 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400410 if (!wrapped) {
411 last = search_start;
412 wrapped = 1;
413 goto again;
414 }
415 if (!full_search && factor < 10) {
Chris Masonbe744172007-05-06 10:15:01 -0400416 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400417 full_search = 1;
Chris Mason0ef3e662008-05-24 14:04:53 -0400418 factor = 10;
Chris Mason31f3c992007-04-30 15:25:45 -0400419 goto again;
420 }
Chris Masonbe744172007-05-06 10:15:01 -0400421found:
Yan Zhengd2fb3432008-12-11 16:30:39 -0500422 return group_start;
Chris Mason925baed2008-06-25 16:01:30 -0400423}
Josef Bacik0f9dd462008-09-23 13:14:11 -0400424
Chris Masone02119d2008-09-05 16:13:11 -0400425/* simple helper to search for an existing extent at a given offset */
Zheng Yan31840ae2008-09-23 13:14:14 -0400426int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
Chris Masone02119d2008-09-05 16:13:11 -0400427{
428 int ret;
429 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400430 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -0400431
Zheng Yan31840ae2008-09-23 13:14:14 -0400432 path = btrfs_alloc_path();
433 BUG_ON(!path);
Chris Masone02119d2008-09-05 16:13:11 -0400434 key.objectid = start;
435 key.offset = len;
436 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
437 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
438 0, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400439 btrfs_free_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500440 return ret;
441}
442
Chris Masond8d5f3e2007-12-11 12:42:00 -0500443/*
444 * Back reference rules. Back refs have three main goals:
445 *
446 * 1) differentiate between all holders of references to an extent so that
447 * when a reference is dropped we can make sure it was a valid reference
448 * before freeing the extent.
449 *
450 * 2) Provide enough information to quickly find the holders of an extent
451 * if we notice a given block is corrupted or bad.
452 *
453 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
454 * maintenance. This is actually the same as #2, but with a slightly
455 * different use case.
456 *
457 * File extents can be referenced by:
458 *
459 * - multiple snapshots, subvolumes, or different generations in one subvol
Zheng Yan31840ae2008-09-23 13:14:14 -0400460 * - different files inside a single subvolume
Chris Masond8d5f3e2007-12-11 12:42:00 -0500461 * - different offsets inside a file (bookend extents in file.c)
462 *
463 * The extent ref structure has fields for:
464 *
465 * - Objectid of the subvolume root
466 * - Generation number of the tree holding the reference
467 * - objectid of the file holding the reference
Zheng Yan31840ae2008-09-23 13:14:14 -0400468 * - number of references holding by parent node (alway 1 for tree blocks)
469 *
470 * Btree leaf may hold multiple references to a file extent. In most cases,
471 * these references are from same file and the corresponding offsets inside
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400472 * the file are close together.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500473 *
474 * When a file extent is allocated the fields are filled in:
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400475 * (root_key.objectid, trans->transid, inode objectid, 1)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500476 *
477 * When a leaf is cow'd new references are added for every file extent found
Zheng Yan31840ae2008-09-23 13:14:14 -0400478 * in the leaf. It looks similar to the create case, but trans->transid will
479 * be different when the block is cow'd.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500480 *
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400481 * (root_key.objectid, trans->transid, inode objectid,
Zheng Yan31840ae2008-09-23 13:14:14 -0400482 * number of references in the leaf)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500483 *
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400484 * When a file extent is removed either during snapshot deletion or
485 * file truncation, we find the corresponding back reference and check
486 * the following fields:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500487 *
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400488 * (btrfs_header_owner(leaf), btrfs_header_generation(leaf),
489 * inode objectid)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500490 *
491 * Btree extents can be referenced by:
492 *
493 * - Different subvolumes
494 * - Different generations of the same subvolume
495 *
Chris Masond8d5f3e2007-12-11 12:42:00 -0500496 * When a tree block is created, back references are inserted:
497 *
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400498 * (root->root_key.objectid, trans->transid, level, 1)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500499 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400500 * When a tree block is cow'd, new back references are added for all the
501 * blocks it points to. If the tree block isn't in reference counted root,
502 * the old back references are removed. These new back references are of
503 * the form (trans->transid will have increased since creation):
Chris Masond8d5f3e2007-12-11 12:42:00 -0500504 *
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400505 * (root->root_key.objectid, trans->transid, level, 1)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500506 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400507 * When a backref is in deleting, the following fields are checked:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500508 *
509 * if backref was for a tree root:
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400510 * (btrfs_header_owner(itself), btrfs_header_generation(itself), level)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500511 * else
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400512 * (btrfs_header_owner(parent), btrfs_header_generation(parent), level)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500513 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400514 * Back Reference Key composing:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500515 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400516 * The key objectid corresponds to the first byte in the extent, the key
517 * type is set to BTRFS_EXTENT_REF_KEY, and the key offset is the first
518 * byte of parent extent. If a extent is tree root, the key offset is set
519 * to the key objectid.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500520 */
Zheng Yan31840ae2008-09-23 13:14:14 -0400521
Chris Masond3977122009-01-05 21:25:51 -0500522static noinline int lookup_extent_backref(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -0400523 struct btrfs_root *root,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400524 struct btrfs_path *path,
525 u64 bytenr, u64 parent,
526 u64 ref_root, u64 ref_generation,
527 u64 owner_objectid, int del)
Chris Mason74493f72007-12-11 09:25:06 -0500528{
Chris Mason74493f72007-12-11 09:25:06 -0500529 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400530 struct btrfs_extent_ref *ref;
531 struct extent_buffer *leaf;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400532 u64 ref_objectid;
Chris Mason74493f72007-12-11 09:25:06 -0500533 int ret;
534
Chris Mason74493f72007-12-11 09:25:06 -0500535 key.objectid = bytenr;
536 key.type = BTRFS_EXTENT_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -0400537 key.offset = parent;
Chris Mason74493f72007-12-11 09:25:06 -0500538
Zheng Yan31840ae2008-09-23 13:14:14 -0400539 ret = btrfs_search_slot(trans, root, &key, path, del ? -1 : 0, 1);
540 if (ret < 0)
Chris Mason7bb86312007-12-11 09:25:06 -0500541 goto out;
Zheng Yan31840ae2008-09-23 13:14:14 -0400542 if (ret > 0) {
543 ret = -ENOENT;
544 goto out;
545 }
546
547 leaf = path->nodes[0];
548 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400549 ref_objectid = btrfs_ref_objectid(leaf, ref);
Zheng Yan31840ae2008-09-23 13:14:14 -0400550 if (btrfs_ref_root(leaf, ref) != ref_root ||
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400551 btrfs_ref_generation(leaf, ref) != ref_generation ||
552 (ref_objectid != owner_objectid &&
553 ref_objectid != BTRFS_MULTIPLE_OBJECTIDS)) {
Zheng Yan31840ae2008-09-23 13:14:14 -0400554 ret = -EIO;
555 WARN_ON(1);
556 goto out;
557 }
558 ret = 0;
559out:
560 return ret;
561}
562
Chris Masond3977122009-01-05 21:25:51 -0500563static noinline int insert_extent_backref(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -0400564 struct btrfs_root *root,
565 struct btrfs_path *path,
566 u64 bytenr, u64 parent,
567 u64 ref_root, u64 ref_generation,
Chris Mason56bec292009-03-13 10:10:06 -0400568 u64 owner_objectid,
569 int refs_to_add)
Zheng Yan31840ae2008-09-23 13:14:14 -0400570{
571 struct btrfs_key key;
572 struct extent_buffer *leaf;
573 struct btrfs_extent_ref *ref;
574 u32 num_refs;
575 int ret;
576
577 key.objectid = bytenr;
578 key.type = BTRFS_EXTENT_REF_KEY;
579 key.offset = parent;
580
581 ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(*ref));
582 if (ret == 0) {
583 leaf = path->nodes[0];
584 ref = btrfs_item_ptr(leaf, path->slots[0],
585 struct btrfs_extent_ref);
586 btrfs_set_ref_root(leaf, ref, ref_root);
587 btrfs_set_ref_generation(leaf, ref, ref_generation);
588 btrfs_set_ref_objectid(leaf, ref, owner_objectid);
Chris Mason56bec292009-03-13 10:10:06 -0400589 btrfs_set_ref_num_refs(leaf, ref, refs_to_add);
Zheng Yan31840ae2008-09-23 13:14:14 -0400590 } else if (ret == -EEXIST) {
591 u64 existing_owner;
Chris Mason56bec292009-03-13 10:10:06 -0400592
Zheng Yan31840ae2008-09-23 13:14:14 -0400593 BUG_ON(owner_objectid < BTRFS_FIRST_FREE_OBJECTID);
594 leaf = path->nodes[0];
595 ref = btrfs_item_ptr(leaf, path->slots[0],
596 struct btrfs_extent_ref);
597 if (btrfs_ref_root(leaf, ref) != ref_root ||
598 btrfs_ref_generation(leaf, ref) != ref_generation) {
599 ret = -EIO;
600 WARN_ON(1);
601 goto out;
602 }
603
604 num_refs = btrfs_ref_num_refs(leaf, ref);
605 BUG_ON(num_refs == 0);
Chris Mason56bec292009-03-13 10:10:06 -0400606 btrfs_set_ref_num_refs(leaf, ref, num_refs + refs_to_add);
Zheng Yan31840ae2008-09-23 13:14:14 -0400607
608 existing_owner = btrfs_ref_objectid(leaf, ref);
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400609 if (existing_owner != owner_objectid &&
610 existing_owner != BTRFS_MULTIPLE_OBJECTIDS) {
Zheng Yan31840ae2008-09-23 13:14:14 -0400611 btrfs_set_ref_objectid(leaf, ref,
612 BTRFS_MULTIPLE_OBJECTIDS);
Zheng Yan31840ae2008-09-23 13:14:14 -0400613 }
614 ret = 0;
615 } else {
616 goto out;
617 }
Chris Masonb9473432009-03-13 11:00:37 -0400618 btrfs_unlock_up_safe(path, 1);
Chris Mason7bb86312007-12-11 09:25:06 -0500619 btrfs_mark_buffer_dirty(path->nodes[0]);
620out:
621 btrfs_release_path(root, path);
622 return ret;
Chris Mason74493f72007-12-11 09:25:06 -0500623}
624
Chris Masond3977122009-01-05 21:25:51 -0500625static noinline int remove_extent_backref(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -0400626 struct btrfs_root *root,
Chris Mason56bec292009-03-13 10:10:06 -0400627 struct btrfs_path *path,
628 int refs_to_drop)
Zheng Yan31840ae2008-09-23 13:14:14 -0400629{
630 struct extent_buffer *leaf;
631 struct btrfs_extent_ref *ref;
632 u32 num_refs;
633 int ret = 0;
634
635 leaf = path->nodes[0];
636 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
637 num_refs = btrfs_ref_num_refs(leaf, ref);
Chris Mason56bec292009-03-13 10:10:06 -0400638 BUG_ON(num_refs < refs_to_drop);
639 num_refs -= refs_to_drop;
Zheng Yan31840ae2008-09-23 13:14:14 -0400640 if (num_refs == 0) {
641 ret = btrfs_del_item(trans, root, path);
642 } else {
643 btrfs_set_ref_num_refs(leaf, ref, num_refs);
644 btrfs_mark_buffer_dirty(leaf);
645 }
646 btrfs_release_path(root, path);
647 return ret;
648}
649
Chris Mason4b4e25f2008-11-20 10:22:27 -0500650#ifdef BIO_RW_DISCARD
Chris Mason15916de2008-11-19 21:17:22 -0500651static void btrfs_issue_discard(struct block_device *bdev,
652 u64 start, u64 len)
653{
Chris Mason15916de2008-11-19 21:17:22 -0500654 blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_KERNEL);
Chris Mason15916de2008-11-19 21:17:22 -0500655}
Chris Mason4b4e25f2008-11-20 10:22:27 -0500656#endif
Chris Mason15916de2008-11-19 21:17:22 -0500657
Liu Hui1f3c79a2009-01-05 15:57:51 -0500658static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
659 u64 num_bytes)
660{
661#ifdef BIO_RW_DISCARD
662 int ret;
663 u64 map_length = num_bytes;
664 struct btrfs_multi_bio *multi = NULL;
665
666 /* Tell the block device(s) that the sectors can be discarded */
667 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
668 bytenr, &map_length, &multi, 0);
669 if (!ret) {
670 struct btrfs_bio_stripe *stripe = multi->stripes;
671 int i;
672
673 if (map_length > num_bytes)
674 map_length = num_bytes;
675
676 for (i = 0; i < multi->num_stripes; i++, stripe++) {
677 btrfs_issue_discard(stripe->dev->bdev,
678 stripe->physical,
679 map_length);
680 }
681 kfree(multi);
682 }
683
684 return ret;
685#else
686 return 0;
687#endif
688}
689
Zheng Yan31840ae2008-09-23 13:14:14 -0400690static int __btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
691 struct btrfs_root *root, u64 bytenr,
Chris Mason56bec292009-03-13 10:10:06 -0400692 u64 num_bytes,
Zheng Yan31840ae2008-09-23 13:14:14 -0400693 u64 orig_parent, u64 parent,
694 u64 orig_root, u64 ref_root,
695 u64 orig_generation, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400696 u64 owner_objectid)
Zheng Yan31840ae2008-09-23 13:14:14 -0400697{
698 int ret;
Chris Mason56bec292009-03-13 10:10:06 -0400699 int pin = owner_objectid < BTRFS_FIRST_FREE_OBJECTID;
Zheng Yan31840ae2008-09-23 13:14:14 -0400700
Chris Mason56bec292009-03-13 10:10:06 -0400701 ret = btrfs_update_delayed_ref(trans, bytenr, num_bytes,
702 orig_parent, parent, orig_root,
703 ref_root, orig_generation,
704 ref_generation, owner_objectid, pin);
Zheng Yan31840ae2008-09-23 13:14:14 -0400705 BUG_ON(ret);
Zheng Yan31840ae2008-09-23 13:14:14 -0400706 return ret;
707}
708
709int btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
710 struct btrfs_root *root, u64 bytenr,
Chris Mason56bec292009-03-13 10:10:06 -0400711 u64 num_bytes, u64 orig_parent, u64 parent,
Zheng Yan31840ae2008-09-23 13:14:14 -0400712 u64 ref_root, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400713 u64 owner_objectid)
Zheng Yan31840ae2008-09-23 13:14:14 -0400714{
715 int ret;
716 if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
717 owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
718 return 0;
Chris Mason56bec292009-03-13 10:10:06 -0400719
720 ret = __btrfs_update_extent_ref(trans, root, bytenr, num_bytes,
721 orig_parent, parent, ref_root,
722 ref_root, ref_generation,
723 ref_generation, owner_objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -0400724 return ret;
725}
Chris Mason925baed2008-06-25 16:01:30 -0400726static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -0400727 struct btrfs_root *root, u64 bytenr,
Chris Mason56bec292009-03-13 10:10:06 -0400728 u64 num_bytes,
Zheng Yan31840ae2008-09-23 13:14:14 -0400729 u64 orig_parent, u64 parent,
730 u64 orig_root, u64 ref_root,
731 u64 orig_generation, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400732 u64 owner_objectid)
Chris Mason02217ed2007-03-02 16:08:05 -0500733{
Chris Mason56bec292009-03-13 10:10:06 -0400734 int ret;
735
736 ret = btrfs_add_delayed_ref(trans, bytenr, num_bytes, parent, ref_root,
737 ref_generation, owner_objectid,
738 BTRFS_ADD_DELAYED_REF, 0);
739 BUG_ON(ret);
740 return ret;
741}
742
743static noinline_for_stack int add_extent_ref(struct btrfs_trans_handle *trans,
744 struct btrfs_root *root, u64 bytenr,
745 u64 num_bytes, u64 parent, u64 ref_root,
746 u64 ref_generation, u64 owner_objectid,
747 int refs_to_add)
748{
Chris Mason5caf2a02007-04-02 11:20:42 -0400749 struct btrfs_path *path;
Chris Mason02217ed2007-03-02 16:08:05 -0500750 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400751 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400752 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -0400753 struct btrfs_extent_item *item;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400754 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500755
Chris Mason5caf2a02007-04-02 11:20:42 -0400756 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400757 if (!path)
758 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -0400759
Chris Mason3c12ac72008-04-21 12:01:38 -0400760 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -0400761 path->leave_spinning = 1;
Chris Masondb945352007-10-15 16:15:53 -0400762 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -0400763 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Mason56bec292009-03-13 10:10:06 -0400764 key.offset = num_bytes;
Zheng Yan31840ae2008-09-23 13:14:14 -0400765
Chris Mason56bec292009-03-13 10:10:06 -0400766 /* first find the extent item and update its reference count */
767 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key,
768 path, 0, 1);
Chris Masonb9473432009-03-13 11:00:37 -0400769 if (ret < 0) {
770 btrfs_set_path_blocking(path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400771 return ret;
Chris Masonb9473432009-03-13 11:00:37 -0400772 }
Zheng Yan31840ae2008-09-23 13:14:14 -0400773
Chris Mason56bec292009-03-13 10:10:06 -0400774 if (ret > 0) {
775 WARN_ON(1);
776 btrfs_free_path(path);
777 return -EIO;
778 }
Chris Mason5f39d392007-10-15 16:14:19 -0400779 l = path->nodes[0];
Zheng Yan31840ae2008-09-23 13:14:14 -0400780
781 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
Chris Mason771ed682008-11-06 22:02:51 -0500782 if (key.objectid != bytenr) {
783 btrfs_print_leaf(root->fs_info->extent_root, path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -0500784 printk(KERN_ERR "btrfs wanted %llu found %llu\n",
785 (unsigned long long)bytenr,
786 (unsigned long long)key.objectid);
Chris Mason771ed682008-11-06 22:02:51 -0500787 BUG();
788 }
Zheng Yan31840ae2008-09-23 13:14:14 -0400789 BUG_ON(key.type != BTRFS_EXTENT_ITEM_KEY);
790
Chris Mason5caf2a02007-04-02 11:20:42 -0400791 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason56bec292009-03-13 10:10:06 -0400792
Chris Mason5f39d392007-10-15 16:14:19 -0400793 refs = btrfs_extent_refs(l, item);
Chris Mason56bec292009-03-13 10:10:06 -0400794 btrfs_set_extent_refs(l, item, refs + refs_to_add);
Chris Masonb9473432009-03-13 11:00:37 -0400795 btrfs_unlock_up_safe(path, 1);
796
Chris Mason5caf2a02007-04-02 11:20:42 -0400797 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masona28ec192007-03-06 20:08:01 -0500798
Chris Mason5caf2a02007-04-02 11:20:42 -0400799 btrfs_release_path(root->fs_info->extent_root, path);
Chris Mason7bb86312007-12-11 09:25:06 -0500800
Chris Mason3c12ac72008-04-21 12:01:38 -0400801 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -0400802 path->leave_spinning = 1;
803
Chris Mason56bec292009-03-13 10:10:06 -0400804 /* now insert the actual backref */
Zheng Yan31840ae2008-09-23 13:14:14 -0400805 ret = insert_extent_backref(trans, root->fs_info->extent_root,
806 path, bytenr, parent,
807 ref_root, ref_generation,
Chris Mason56bec292009-03-13 10:10:06 -0400808 owner_objectid, refs_to_add);
Chris Mason7bb86312007-12-11 09:25:06 -0500809 BUG_ON(ret);
Chris Mason74493f72007-12-11 09:25:06 -0500810 btrfs_free_path(path);
Chris Mason02217ed2007-03-02 16:08:05 -0500811 return 0;
812}
813
Chris Mason925baed2008-06-25 16:01:30 -0400814int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -0400815 struct btrfs_root *root,
816 u64 bytenr, u64 num_bytes, u64 parent,
817 u64 ref_root, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400818 u64 owner_objectid)
Chris Mason925baed2008-06-25 16:01:30 -0400819{
820 int ret;
Zheng Yan31840ae2008-09-23 13:14:14 -0400821 if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
822 owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
823 return 0;
Chris Mason56bec292009-03-13 10:10:06 -0400824
825 ret = __btrfs_inc_extent_ref(trans, root, bytenr, num_bytes, 0, parent,
Zheng Yan31840ae2008-09-23 13:14:14 -0400826 0, ref_root, 0, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400827 owner_objectid);
Chris Mason925baed2008-06-25 16:01:30 -0400828 return ret;
829}
830
Chris Mason56bec292009-03-13 10:10:06 -0400831static int drop_delayed_ref(struct btrfs_trans_handle *trans,
832 struct btrfs_root *root,
833 struct btrfs_delayed_ref_node *node)
Chris Masone9d0b132007-08-10 14:06:19 -0400834{
Chris Mason56bec292009-03-13 10:10:06 -0400835 int ret = 0;
836 struct btrfs_delayed_ref *ref = btrfs_delayed_node_to_ref(node);
837
838 BUG_ON(node->ref_mod == 0);
839 ret = __btrfs_free_extent(trans, root, node->bytenr, node->num_bytes,
840 node->parent, ref->root, ref->generation,
841 ref->owner_objectid, ref->pin, node->ref_mod);
842
843 return ret;
844}
845
846/* helper function to actually process a single delayed ref entry */
847static noinline int run_one_delayed_ref(struct btrfs_trans_handle *trans,
848 struct btrfs_root *root,
849 struct btrfs_delayed_ref_node *node,
850 int insert_reserved)
851{
Josef Bacikeb099672009-02-12 09:27:38 -0500852 int ret;
Chris Mason56bec292009-03-13 10:10:06 -0400853 struct btrfs_delayed_ref *ref;
Josef Bacikeb099672009-02-12 09:27:38 -0500854
Chris Mason56bec292009-03-13 10:10:06 -0400855 if (node->parent == (u64)-1) {
856 struct btrfs_delayed_ref_head *head;
857 /*
858 * we've hit the end of the chain and we were supposed
859 * to insert this extent into the tree. But, it got
860 * deleted before we ever needed to insert it, so all
861 * we have to do is clean up the accounting
862 */
863 if (insert_reserved) {
864 update_reserved_extents(root, node->bytenr,
865 node->num_bytes, 0);
866 }
867 head = btrfs_delayed_node_to_head(node);
868 mutex_unlock(&head->mutex);
869 return 0;
870 }
Josef Bacikeb099672009-02-12 09:27:38 -0500871
Chris Mason56bec292009-03-13 10:10:06 -0400872 ref = btrfs_delayed_node_to_ref(node);
873 if (ref->action == BTRFS_ADD_DELAYED_REF) {
874 if (insert_reserved) {
875 struct btrfs_key ins;
876
877 ins.objectid = node->bytenr;
878 ins.offset = node->num_bytes;
879 ins.type = BTRFS_EXTENT_ITEM_KEY;
880
881 /* record the full extent allocation */
882 ret = __btrfs_alloc_reserved_extent(trans, root,
883 node->parent, ref->root,
884 ref->generation, ref->owner_objectid,
885 &ins, node->ref_mod);
886 update_reserved_extents(root, node->bytenr,
887 node->num_bytes, 0);
888 } else {
889 /* just add one backref */
890 ret = add_extent_ref(trans, root, node->bytenr,
891 node->num_bytes,
892 node->parent, ref->root, ref->generation,
893 ref->owner_objectid, node->ref_mod);
894 }
895 BUG_ON(ret);
896 } else if (ref->action == BTRFS_DROP_DELAYED_REF) {
897 WARN_ON(insert_reserved);
898 ret = drop_delayed_ref(trans, root, node);
Josef Bacikeb099672009-02-12 09:27:38 -0500899 }
Chris Masone9d0b132007-08-10 14:06:19 -0400900 return 0;
901}
902
Chris Mason56bec292009-03-13 10:10:06 -0400903static noinline struct btrfs_delayed_ref_node *
904select_delayed_ref(struct btrfs_delayed_ref_head *head)
Chris Masona28ec192007-03-06 20:08:01 -0500905{
Chris Mason56bec292009-03-13 10:10:06 -0400906 struct rb_node *node;
907 struct btrfs_delayed_ref_node *ref;
908 int action = BTRFS_ADD_DELAYED_REF;
909again:
910 /*
911 * select delayed ref of type BTRFS_ADD_DELAYED_REF first.
912 * this prevents ref count from going down to zero when
913 * there still are pending delayed ref.
914 */
915 node = rb_prev(&head->node.rb_node);
916 while (1) {
917 if (!node)
918 break;
919 ref = rb_entry(node, struct btrfs_delayed_ref_node,
920 rb_node);
921 if (ref->bytenr != head->node.bytenr)
922 break;
923 if (btrfs_delayed_node_to_ref(ref)->action == action)
924 return ref;
925 node = rb_prev(node);
Chris Mason5f39d392007-10-15 16:14:19 -0400926 }
Chris Mason56bec292009-03-13 10:10:06 -0400927 if (action == BTRFS_ADD_DELAYED_REF) {
928 action = BTRFS_DROP_DELAYED_REF;
929 goto again;
930 }
931 return NULL;
932}
933
Chris Masonc3e69d52009-03-13 10:17:05 -0400934static noinline int run_clustered_refs(struct btrfs_trans_handle *trans,
935 struct btrfs_root *root,
936 struct list_head *cluster)
Chris Mason56bec292009-03-13 10:10:06 -0400937{
Chris Mason56bec292009-03-13 10:10:06 -0400938 struct btrfs_delayed_ref_root *delayed_refs;
939 struct btrfs_delayed_ref_node *ref;
940 struct btrfs_delayed_ref_head *locked_ref = NULL;
941 int ret;
Chris Masonc3e69d52009-03-13 10:17:05 -0400942 int count = 0;
Chris Mason56bec292009-03-13 10:10:06 -0400943 int must_insert_reserved = 0;
Chris Mason56bec292009-03-13 10:10:06 -0400944
945 delayed_refs = &trans->transaction->delayed_refs;
Chris Mason56bec292009-03-13 10:10:06 -0400946 while (1) {
947 if (!locked_ref) {
Chris Masonc3e69d52009-03-13 10:17:05 -0400948 /* pick a new head ref from the cluster list */
949 if (list_empty(cluster))
Chris Mason56bec292009-03-13 10:10:06 -0400950 break;
Chris Mason56bec292009-03-13 10:10:06 -0400951
Chris Masonc3e69d52009-03-13 10:17:05 -0400952 locked_ref = list_entry(cluster->next,
953 struct btrfs_delayed_ref_head, cluster);
954
955 /* grab the lock that says we are going to process
956 * all the refs for this head */
957 ret = btrfs_delayed_ref_lock(trans, locked_ref);
958
959 /*
960 * we may have dropped the spin lock to get the head
961 * mutex lock, and that might have given someone else
962 * time to free the head. If that's true, it has been
963 * removed from our list and we can move on.
964 */
965 if (ret == -EAGAIN) {
966 locked_ref = NULL;
967 count++;
968 continue;
Chris Mason56bec292009-03-13 10:10:06 -0400969 }
970 }
971
972 /*
973 * record the must insert reserved flag before we
974 * drop the spin lock.
975 */
976 must_insert_reserved = locked_ref->must_insert_reserved;
977 locked_ref->must_insert_reserved = 0;
978
979 /*
980 * locked_ref is the head node, so we have to go one
981 * node back for any delayed ref updates
982 */
Chris Mason56bec292009-03-13 10:10:06 -0400983 ref = select_delayed_ref(locked_ref);
984 if (!ref) {
985 /* All delayed refs have been processed, Go ahead
986 * and send the head node to run_one_delayed_ref,
987 * so that any accounting fixes can happen
988 */
989 ref = &locked_ref->node;
Chris Masonc3e69d52009-03-13 10:17:05 -0400990 list_del_init(&locked_ref->cluster);
Chris Mason56bec292009-03-13 10:10:06 -0400991 locked_ref = NULL;
992 }
993
994 ref->in_tree = 0;
995 rb_erase(&ref->rb_node, &delayed_refs->root);
996 delayed_refs->num_entries--;
997 spin_unlock(&delayed_refs->lock);
998
999 ret = run_one_delayed_ref(trans, root, ref,
1000 must_insert_reserved);
1001 BUG_ON(ret);
1002 btrfs_put_delayed_ref(ref);
1003
Chris Masonc3e69d52009-03-13 10:17:05 -04001004 count++;
Chris Mason1887be62009-03-13 10:11:24 -04001005 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04001006 spin_lock(&delayed_refs->lock);
1007 }
Chris Masonc3e69d52009-03-13 10:17:05 -04001008 return count;
1009}
1010
1011/*
1012 * this starts processing the delayed reference count updates and
1013 * extent insertions we have queued up so far. count can be
1014 * 0, which means to process everything in the tree at the start
1015 * of the run (but not newly added entries), or it can be some target
1016 * number you'd like to process.
1017 */
1018int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
1019 struct btrfs_root *root, unsigned long count)
1020{
1021 struct rb_node *node;
1022 struct btrfs_delayed_ref_root *delayed_refs;
1023 struct btrfs_delayed_ref_node *ref;
1024 struct list_head cluster;
1025 int ret;
1026 int run_all = count == (unsigned long)-1;
1027 int run_most = 0;
1028
1029 if (root == root->fs_info->extent_root)
1030 root = root->fs_info->tree_root;
1031
1032 delayed_refs = &trans->transaction->delayed_refs;
1033 INIT_LIST_HEAD(&cluster);
1034again:
1035 spin_lock(&delayed_refs->lock);
1036 if (count == 0) {
1037 count = delayed_refs->num_entries * 2;
1038 run_most = 1;
1039 }
1040 while (1) {
1041 if (!(run_all || run_most) &&
1042 delayed_refs->num_heads_ready < 64)
1043 break;
1044
1045 /*
1046 * go find something we can process in the rbtree. We start at
1047 * the beginning of the tree, and then build a cluster
1048 * of refs to process starting at the first one we are able to
1049 * lock
1050 */
1051 ret = btrfs_find_ref_cluster(trans, &cluster,
1052 delayed_refs->run_delayed_start);
1053 if (ret)
1054 break;
1055
1056 ret = run_clustered_refs(trans, root, &cluster);
1057 BUG_ON(ret < 0);
1058
1059 count -= min_t(unsigned long, ret, count);
1060
1061 if (count == 0)
1062 break;
1063 }
1064
Chris Mason56bec292009-03-13 10:10:06 -04001065 if (run_all) {
Chris Mason56bec292009-03-13 10:10:06 -04001066 node = rb_first(&delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04001067 if (!node)
Chris Mason56bec292009-03-13 10:10:06 -04001068 goto out;
Chris Masonc3e69d52009-03-13 10:17:05 -04001069 count = (unsigned long)-1;
Chris Mason56bec292009-03-13 10:10:06 -04001070
1071 while (node) {
1072 ref = rb_entry(node, struct btrfs_delayed_ref_node,
1073 rb_node);
1074 if (btrfs_delayed_ref_is_head(ref)) {
1075 struct btrfs_delayed_ref_head *head;
1076
1077 head = btrfs_delayed_node_to_head(ref);
1078 atomic_inc(&ref->refs);
1079
1080 spin_unlock(&delayed_refs->lock);
1081 mutex_lock(&head->mutex);
1082 mutex_unlock(&head->mutex);
1083
1084 btrfs_put_delayed_ref(ref);
Chris Mason1887be62009-03-13 10:11:24 -04001085 cond_resched();
Chris Mason56bec292009-03-13 10:10:06 -04001086 goto again;
1087 }
1088 node = rb_next(node);
1089 }
1090 spin_unlock(&delayed_refs->lock);
Chris Mason56bec292009-03-13 10:10:06 -04001091 schedule_timeout(1);
1092 goto again;
1093 }
Chris Mason54aa1f42007-06-22 14:16:25 -04001094out:
Chris Masonc3e69d52009-03-13 10:17:05 -04001095 spin_unlock(&delayed_refs->lock);
Chris Masona28ec192007-03-06 20:08:01 -05001096 return 0;
1097}
1098
Yan Zheng80ff3852008-10-30 14:20:02 -04001099int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
Yan Zheng17d217f2008-12-12 10:03:38 -05001100 struct btrfs_root *root, u64 objectid, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05001101{
1102 struct btrfs_root *extent_root = root->fs_info->extent_root;
1103 struct btrfs_path *path;
Yan Zhengf321e492008-07-30 09:26:11 -04001104 struct extent_buffer *leaf;
1105 struct btrfs_extent_ref *ref_item;
Chris Masonbe20aa92007-12-17 20:14:01 -05001106 struct btrfs_key key;
1107 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001108 u64 ref_root;
1109 u64 last_snapshot;
Yan Zhengf321e492008-07-30 09:26:11 -04001110 u32 nritems;
1111 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001112
Chris Masonbe20aa92007-12-17 20:14:01 -05001113 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04001114 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04001115 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05001116
Yan Zhengf321e492008-07-30 09:26:11 -04001117 path = btrfs_alloc_path();
Chris Masonbe20aa92007-12-17 20:14:01 -05001118 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
1119 if (ret < 0)
1120 goto out;
1121 BUG_ON(ret == 0);
Yan Zheng80ff3852008-10-30 14:20:02 -04001122
1123 ret = -ENOENT;
1124 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04001125 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05001126
Zheng Yan31840ae2008-09-23 13:14:14 -04001127 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04001128 leaf = path->nodes[0];
1129 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001130
1131 if (found_key.objectid != bytenr ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001132 found_key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05001133 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05001134
Yan Zheng80ff3852008-10-30 14:20:02 -04001135 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Chris Masonbe20aa92007-12-17 20:14:01 -05001136 while (1) {
Yan Zhengf321e492008-07-30 09:26:11 -04001137 leaf = path->nodes[0];
1138 nritems = btrfs_header_nritems(leaf);
Chris Masonbe20aa92007-12-17 20:14:01 -05001139 if (path->slots[0] >= nritems) {
1140 ret = btrfs_next_leaf(extent_root, path);
Yan Zhengf321e492008-07-30 09:26:11 -04001141 if (ret < 0)
1142 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05001143 if (ret == 0)
1144 continue;
1145 break;
1146 }
Yan Zhengf321e492008-07-30 09:26:11 -04001147 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001148 if (found_key.objectid != bytenr)
1149 break;
Chris Masonbd098352008-01-03 13:23:19 -05001150
Chris Masonbe20aa92007-12-17 20:14:01 -05001151 if (found_key.type != BTRFS_EXTENT_REF_KEY) {
1152 path->slots[0]++;
1153 continue;
1154 }
1155
Yan Zhengf321e492008-07-30 09:26:11 -04001156 ref_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masonbe20aa92007-12-17 20:14:01 -05001157 struct btrfs_extent_ref);
Yan Zheng80ff3852008-10-30 14:20:02 -04001158 ref_root = btrfs_ref_root(leaf, ref_item);
Yan Zheng17d217f2008-12-12 10:03:38 -05001159 if ((ref_root != root->root_key.objectid &&
1160 ref_root != BTRFS_TREE_LOG_OBJECTID) ||
1161 objectid != btrfs_ref_objectid(leaf, ref_item)) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001162 ret = 1;
1163 goto out;
Yan Zhengf321e492008-07-30 09:26:11 -04001164 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001165 if (btrfs_ref_generation(leaf, ref_item) <= last_snapshot) {
1166 ret = 1;
1167 goto out;
1168 }
Yan Zhengf321e492008-07-30 09:26:11 -04001169
Chris Masonbe20aa92007-12-17 20:14:01 -05001170 path->slots[0]++;
1171 }
Yan Zhengf321e492008-07-30 09:26:11 -04001172 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001173out:
Yan Zhengf321e492008-07-30 09:26:11 -04001174 btrfs_free_path(path);
1175 return ret;
1176}
1177
Zheng Yan31840ae2008-09-23 13:14:14 -04001178int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1179 struct extent_buffer *buf, u32 nr_extents)
Chris Mason02217ed2007-03-02 16:08:05 -05001180{
Chris Mason5f39d392007-10-15 16:14:19 -04001181 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04001182 struct btrfs_file_extent_item *fi;
Zheng Yane4657682008-09-26 10:04:53 -04001183 u64 root_gen;
1184 u32 nritems;
Chris Mason02217ed2007-03-02 16:08:05 -05001185 int i;
Chris Masondb945352007-10-15 16:15:53 -04001186 int level;
Zheng Yan31840ae2008-09-23 13:14:14 -04001187 int ret = 0;
Zheng Yane4657682008-09-26 10:04:53 -04001188 int shared = 0;
Chris Masona28ec192007-03-06 20:08:01 -05001189
Chris Mason3768f362007-03-13 16:47:54 -04001190 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -05001191 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001192
Zheng Yane4657682008-09-26 10:04:53 -04001193 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
1194 shared = 0;
1195 root_gen = root->root_key.offset;
1196 } else {
1197 shared = 1;
1198 root_gen = trans->transid - 1;
1199 }
1200
Chris Masondb945352007-10-15 16:15:53 -04001201 level = btrfs_header_level(buf);
Chris Mason5f39d392007-10-15 16:14:19 -04001202 nritems = btrfs_header_nritems(buf);
Chris Mason4a096752008-07-21 10:29:44 -04001203
Zheng Yan31840ae2008-09-23 13:14:14 -04001204 if (level == 0) {
Yan Zheng31153d82008-07-28 15:32:19 -04001205 struct btrfs_leaf_ref *ref;
1206 struct btrfs_extent_info *info;
1207
Zheng Yan31840ae2008-09-23 13:14:14 -04001208 ref = btrfs_alloc_leaf_ref(root, nr_extents);
Yan Zheng31153d82008-07-28 15:32:19 -04001209 if (!ref) {
Zheng Yan31840ae2008-09-23 13:14:14 -04001210 ret = -ENOMEM;
Yan Zheng31153d82008-07-28 15:32:19 -04001211 goto out;
1212 }
1213
Zheng Yane4657682008-09-26 10:04:53 -04001214 ref->root_gen = root_gen;
Yan Zheng31153d82008-07-28 15:32:19 -04001215 ref->bytenr = buf->start;
1216 ref->owner = btrfs_header_owner(buf);
1217 ref->generation = btrfs_header_generation(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04001218 ref->nritems = nr_extents;
Yan Zheng31153d82008-07-28 15:32:19 -04001219 info = ref->extents;
Yanbcc63ab2008-07-30 16:29:20 -04001220
Zheng Yan31840ae2008-09-23 13:14:14 -04001221 for (i = 0; nr_extents > 0 && i < nritems; i++) {
Yan Zheng31153d82008-07-28 15:32:19 -04001222 u64 disk_bytenr;
1223 btrfs_item_key_to_cpu(buf, &key, i);
1224 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1225 continue;
1226 fi = btrfs_item_ptr(buf, i,
1227 struct btrfs_file_extent_item);
1228 if (btrfs_file_extent_type(buf, fi) ==
1229 BTRFS_FILE_EXTENT_INLINE)
1230 continue;
1231 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1232 if (disk_bytenr == 0)
1233 continue;
1234
1235 info->bytenr = disk_bytenr;
1236 info->num_bytes =
1237 btrfs_file_extent_disk_num_bytes(buf, fi);
1238 info->objectid = key.objectid;
1239 info->offset = key.offset;
1240 info++;
1241 }
1242
Zheng Yane4657682008-09-26 10:04:53 -04001243 ret = btrfs_add_leaf_ref(root, ref, shared);
Yan Zheng5b84e8d2008-10-09 11:46:19 -04001244 if (ret == -EEXIST && shared) {
1245 struct btrfs_leaf_ref *old;
1246 old = btrfs_lookup_leaf_ref(root, ref->bytenr);
1247 BUG_ON(!old);
1248 btrfs_remove_leaf_ref(root, old);
1249 btrfs_free_leaf_ref(root, old);
1250 ret = btrfs_add_leaf_ref(root, ref, shared);
1251 }
Yan Zheng31153d82008-07-28 15:32:19 -04001252 WARN_ON(ret);
Yanbcc63ab2008-07-30 16:29:20 -04001253 btrfs_free_leaf_ref(root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04001254 }
1255out:
Zheng Yan31840ae2008-09-23 13:14:14 -04001256 return ret;
1257}
1258
Chris Masonb7a9f292009-02-04 09:23:45 -05001259/* when a block goes through cow, we update the reference counts of
1260 * everything that block points to. The internal pointers of the block
1261 * can be in just about any order, and it is likely to have clusters of
1262 * things that are close together and clusters of things that are not.
1263 *
1264 * To help reduce the seeks that come with updating all of these reference
1265 * counts, sort them by byte number before actual updates are done.
1266 *
1267 * struct refsort is used to match byte number to slot in the btree block.
1268 * we sort based on the byte number and then use the slot to actually
1269 * find the item.
Chris Masonbd56b302009-02-04 09:27:02 -05001270 *
1271 * struct refsort is smaller than strcut btrfs_item and smaller than
1272 * struct btrfs_key_ptr. Since we're currently limited to the page size
1273 * for a btree block, there's no way for a kmalloc of refsorts for a
1274 * single node to be bigger than a page.
Chris Masonb7a9f292009-02-04 09:23:45 -05001275 */
1276struct refsort {
1277 u64 bytenr;
1278 u32 slot;
1279};
1280
1281/*
1282 * for passing into sort()
1283 */
1284static int refsort_cmp(const void *a_void, const void *b_void)
1285{
1286 const struct refsort *a = a_void;
1287 const struct refsort *b = b_void;
1288
1289 if (a->bytenr < b->bytenr)
1290 return -1;
1291 if (a->bytenr > b->bytenr)
1292 return 1;
1293 return 0;
1294}
1295
1296
1297noinline int btrfs_inc_ref(struct btrfs_trans_handle *trans,
1298 struct btrfs_root *root,
1299 struct extent_buffer *orig_buf,
1300 struct extent_buffer *buf, u32 *nr_extents)
Zheng Yan31840ae2008-09-23 13:14:14 -04001301{
1302 u64 bytenr;
1303 u64 ref_root;
1304 u64 orig_root;
1305 u64 ref_generation;
1306 u64 orig_generation;
Chris Masonb7a9f292009-02-04 09:23:45 -05001307 struct refsort *sorted;
Zheng Yan31840ae2008-09-23 13:14:14 -04001308 u32 nritems;
1309 u32 nr_file_extents = 0;
1310 struct btrfs_key key;
1311 struct btrfs_file_extent_item *fi;
1312 int i;
1313 int level;
1314 int ret = 0;
1315 int faili = 0;
Chris Masonb7a9f292009-02-04 09:23:45 -05001316 int refi = 0;
1317 int slot;
Zheng Yan31840ae2008-09-23 13:14:14 -04001318 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
Chris Mason56bec292009-03-13 10:10:06 -04001319 u64, u64, u64, u64, u64, u64, u64, u64, u64);
Zheng Yan31840ae2008-09-23 13:14:14 -04001320
1321 ref_root = btrfs_header_owner(buf);
1322 ref_generation = btrfs_header_generation(buf);
1323 orig_root = btrfs_header_owner(orig_buf);
1324 orig_generation = btrfs_header_generation(orig_buf);
1325
1326 nritems = btrfs_header_nritems(buf);
1327 level = btrfs_header_level(buf);
1328
Chris Masonb7a9f292009-02-04 09:23:45 -05001329 sorted = kmalloc(sizeof(struct refsort) * nritems, GFP_NOFS);
1330 BUG_ON(!sorted);
1331
Zheng Yan31840ae2008-09-23 13:14:14 -04001332 if (root->ref_cows) {
1333 process_func = __btrfs_inc_extent_ref;
1334 } else {
1335 if (level == 0 &&
1336 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1337 goto out;
1338 if (level != 0 &&
1339 root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
1340 goto out;
1341 process_func = __btrfs_update_extent_ref;
1342 }
1343
Chris Masonb7a9f292009-02-04 09:23:45 -05001344 /*
1345 * we make two passes through the items. In the first pass we
1346 * only record the byte number and slot. Then we sort based on
1347 * byte number and do the actual work based on the sorted results
1348 */
Zheng Yan31840ae2008-09-23 13:14:14 -04001349 for (i = 0; i < nritems; i++) {
1350 cond_resched();
Chris Masondb945352007-10-15 16:15:53 -04001351 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001352 btrfs_item_key_to_cpu(buf, &key, i);
1353 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04001354 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001355 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04001356 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001357 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04001358 BTRFS_FILE_EXTENT_INLINE)
1359 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04001360 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1361 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04001362 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04001363
1364 nr_file_extents++;
Chris Masonb7a9f292009-02-04 09:23:45 -05001365 sorted[refi].bytenr = bytenr;
1366 sorted[refi].slot = i;
1367 refi++;
1368 } else {
1369 bytenr = btrfs_node_blockptr(buf, i);
1370 sorted[refi].bytenr = bytenr;
1371 sorted[refi].slot = i;
1372 refi++;
1373 }
1374 }
1375 /*
1376 * if refi == 0, we didn't actually put anything into the sorted
1377 * array and we're done
1378 */
1379 if (refi == 0)
1380 goto out;
1381
1382 sort(sorted, refi, sizeof(struct refsort), refsort_cmp, NULL);
1383
1384 for (i = 0; i < refi; i++) {
1385 cond_resched();
1386 slot = sorted[i].slot;
1387 bytenr = sorted[i].bytenr;
1388
1389 if (level == 0) {
1390 btrfs_item_key_to_cpu(buf, &key, slot);
Chris Mason56bec292009-03-13 10:10:06 -04001391 fi = btrfs_item_ptr(buf, slot,
1392 struct btrfs_file_extent_item);
1393
1394 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1395 if (bytenr == 0)
1396 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04001397
Zheng Yan31840ae2008-09-23 13:14:14 -04001398 ret = process_func(trans, root, bytenr,
Chris Mason56bec292009-03-13 10:10:06 -04001399 btrfs_file_extent_disk_num_bytes(buf, fi),
1400 orig_buf->start, buf->start,
1401 orig_root, ref_root,
1402 orig_generation, ref_generation,
1403 key.objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -04001404
1405 if (ret) {
Chris Masonb7a9f292009-02-04 09:23:45 -05001406 faili = slot;
Zheng Yan31840ae2008-09-23 13:14:14 -04001407 WARN_ON(1);
1408 goto fail;
1409 }
Chris Mason54aa1f42007-06-22 14:16:25 -04001410 } else {
Chris Mason56bec292009-03-13 10:10:06 -04001411 ret = process_func(trans, root, bytenr, buf->len,
Zheng Yan31840ae2008-09-23 13:14:14 -04001412 orig_buf->start, buf->start,
1413 orig_root, ref_root,
1414 orig_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001415 level - 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001416 if (ret) {
Chris Masonb7a9f292009-02-04 09:23:45 -05001417 faili = slot;
Zheng Yan31840ae2008-09-23 13:14:14 -04001418 WARN_ON(1);
1419 goto fail;
1420 }
Chris Mason54aa1f42007-06-22 14:16:25 -04001421 }
1422 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001423out:
Chris Masonb7a9f292009-02-04 09:23:45 -05001424 kfree(sorted);
Zheng Yan31840ae2008-09-23 13:14:14 -04001425 if (nr_extents) {
1426 if (level == 0)
1427 *nr_extents = nr_file_extents;
1428 else
1429 *nr_extents = nritems;
1430 }
1431 return 0;
1432fail:
Chris Masonb7a9f292009-02-04 09:23:45 -05001433 kfree(sorted);
Zheng Yan31840ae2008-09-23 13:14:14 -04001434 WARN_ON(1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001435 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05001436}
1437
Zheng Yan31840ae2008-09-23 13:14:14 -04001438int btrfs_update_ref(struct btrfs_trans_handle *trans,
1439 struct btrfs_root *root, struct extent_buffer *orig_buf,
1440 struct extent_buffer *buf, int start_slot, int nr)
1441
1442{
1443 u64 bytenr;
1444 u64 ref_root;
1445 u64 orig_root;
1446 u64 ref_generation;
1447 u64 orig_generation;
1448 struct btrfs_key key;
1449 struct btrfs_file_extent_item *fi;
1450 int i;
1451 int ret;
1452 int slot;
1453 int level;
1454
1455 BUG_ON(start_slot < 0);
1456 BUG_ON(start_slot + nr > btrfs_header_nritems(buf));
1457
1458 ref_root = btrfs_header_owner(buf);
1459 ref_generation = btrfs_header_generation(buf);
1460 orig_root = btrfs_header_owner(orig_buf);
1461 orig_generation = btrfs_header_generation(orig_buf);
1462 level = btrfs_header_level(buf);
1463
1464 if (!root->ref_cows) {
1465 if (level == 0 &&
1466 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1467 return 0;
1468 if (level != 0 &&
1469 root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
1470 return 0;
1471 }
1472
1473 for (i = 0, slot = start_slot; i < nr; i++, slot++) {
1474 cond_resched();
1475 if (level == 0) {
1476 btrfs_item_key_to_cpu(buf, &key, slot);
1477 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1478 continue;
1479 fi = btrfs_item_ptr(buf, slot,
1480 struct btrfs_file_extent_item);
1481 if (btrfs_file_extent_type(buf, fi) ==
1482 BTRFS_FILE_EXTENT_INLINE)
1483 continue;
1484 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1485 if (bytenr == 0)
1486 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04001487 ret = __btrfs_update_extent_ref(trans, root, bytenr,
Chris Mason56bec292009-03-13 10:10:06 -04001488 btrfs_file_extent_disk_num_bytes(buf, fi),
1489 orig_buf->start, buf->start,
1490 orig_root, ref_root, orig_generation,
1491 ref_generation, key.objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -04001492 if (ret)
1493 goto fail;
1494 } else {
1495 bytenr = btrfs_node_blockptr(buf, slot);
Zheng Yan31840ae2008-09-23 13:14:14 -04001496 ret = __btrfs_update_extent_ref(trans, root, bytenr,
Chris Mason56bec292009-03-13 10:10:06 -04001497 buf->len, orig_buf->start,
1498 buf->start, orig_root, ref_root,
Zheng Yan31840ae2008-09-23 13:14:14 -04001499 orig_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001500 level - 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001501 if (ret)
1502 goto fail;
1503 }
1504 }
1505 return 0;
1506fail:
1507 WARN_ON(1);
1508 return -1;
1509}
1510
Chris Mason9078a3e2007-04-26 16:46:15 -04001511static int write_one_cache_group(struct btrfs_trans_handle *trans,
1512 struct btrfs_root *root,
1513 struct btrfs_path *path,
1514 struct btrfs_block_group_cache *cache)
1515{
1516 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04001517 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04001518 unsigned long bi;
1519 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04001520
Chris Mason9078a3e2007-04-26 16:46:15 -04001521 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001522 if (ret < 0)
1523 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -04001524 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001525
1526 leaf = path->nodes[0];
1527 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
1528 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
1529 btrfs_mark_buffer_dirty(leaf);
Chris Mason9078a3e2007-04-26 16:46:15 -04001530 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -04001531fail:
Chris Mason9078a3e2007-04-26 16:46:15 -04001532 if (ret)
1533 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04001534 return 0;
1535
1536}
1537
Chris Mason96b51792007-10-15 16:15:19 -04001538int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
1539 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04001540{
Josef Bacik0f9dd462008-09-23 13:14:11 -04001541 struct btrfs_block_group_cache *cache, *entry;
1542 struct rb_node *n;
Chris Mason9078a3e2007-04-26 16:46:15 -04001543 int err = 0;
1544 int werr = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04001545 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04001546 u64 last = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04001547
1548 path = btrfs_alloc_path();
1549 if (!path)
1550 return -ENOMEM;
1551
Chris Masond3977122009-01-05 21:25:51 -05001552 while (1) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001553 cache = NULL;
1554 spin_lock(&root->fs_info->block_group_cache_lock);
1555 for (n = rb_first(&root->fs_info->block_group_cache_tree);
1556 n; n = rb_next(n)) {
1557 entry = rb_entry(n, struct btrfs_block_group_cache,
1558 cache_node);
1559 if (entry->dirty) {
1560 cache = entry;
1561 break;
1562 }
1563 }
1564 spin_unlock(&root->fs_info->block_group_cache_lock);
1565
1566 if (!cache)
Chris Mason9078a3e2007-04-26 16:46:15 -04001567 break;
Chris Mason54aa1f42007-06-22 14:16:25 -04001568
Zheng Yane8569812008-09-26 10:05:48 -04001569 cache->dirty = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001570 last += cache->key.offset;
1571
Chris Mason96b51792007-10-15 16:15:19 -04001572 err = write_one_cache_group(trans, root,
1573 path, cache);
1574 /*
1575 * if we fail to write the cache group, we want
1576 * to keep it marked dirty in hopes that a later
1577 * write will work
1578 */
1579 if (err) {
1580 werr = err;
1581 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04001582 }
1583 }
1584 btrfs_free_path(path);
1585 return werr;
1586}
1587
Yan Zhengd2fb3432008-12-11 16:30:39 -05001588int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
1589{
1590 struct btrfs_block_group_cache *block_group;
1591 int readonly = 0;
1592
1593 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
1594 if (!block_group || block_group->ro)
1595 readonly = 1;
1596 if (block_group)
1597 put_block_group(block_group);
1598 return readonly;
1599}
1600
Chris Mason593060d2008-03-25 16:50:33 -04001601static int update_space_info(struct btrfs_fs_info *info, u64 flags,
1602 u64 total_bytes, u64 bytes_used,
1603 struct btrfs_space_info **space_info)
1604{
1605 struct btrfs_space_info *found;
1606
1607 found = __find_space_info(info, flags);
1608 if (found) {
Josef Bacik25179202008-10-29 14:49:05 -04001609 spin_lock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04001610 found->total_bytes += total_bytes;
1611 found->bytes_used += bytes_used;
Chris Mason8f18cf12008-04-25 16:53:30 -04001612 found->full = 0;
Josef Bacik25179202008-10-29 14:49:05 -04001613 spin_unlock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04001614 *space_info = found;
1615 return 0;
1616 }
Yan Zhengc146afa2008-11-12 14:34:12 -05001617 found = kzalloc(sizeof(*found), GFP_NOFS);
Chris Mason593060d2008-03-25 16:50:33 -04001618 if (!found)
1619 return -ENOMEM;
1620
Josef Bacik0f9dd462008-09-23 13:14:11 -04001621 INIT_LIST_HEAD(&found->block_groups);
Josef Bacik80eb2342008-10-29 14:49:05 -04001622 init_rwsem(&found->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001623 spin_lock_init(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04001624 found->flags = flags;
1625 found->total_bytes = total_bytes;
1626 found->bytes_used = bytes_used;
1627 found->bytes_pinned = 0;
Zheng Yane8569812008-09-26 10:05:48 -04001628 found->bytes_reserved = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05001629 found->bytes_readonly = 0;
Josef Bacik6a632092009-02-20 11:00:09 -05001630 found->bytes_delalloc = 0;
Chris Mason593060d2008-03-25 16:50:33 -04001631 found->full = 0;
Chris Mason0ef3e662008-05-24 14:04:53 -04001632 found->force_alloc = 0;
Chris Mason593060d2008-03-25 16:50:33 -04001633 *space_info = found;
Chris Mason4184ea72009-03-10 12:39:20 -04001634 list_add_rcu(&found->list, &info->space_info);
Chris Mason593060d2008-03-25 16:50:33 -04001635 return 0;
1636}
1637
Chris Mason8790d502008-04-03 16:29:03 -04001638static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
1639{
1640 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
Chris Mason611f0e02008-04-03 16:29:03 -04001641 BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04001642 BTRFS_BLOCK_GROUP_RAID10 |
Chris Mason611f0e02008-04-03 16:29:03 -04001643 BTRFS_BLOCK_GROUP_DUP);
Chris Mason8790d502008-04-03 16:29:03 -04001644 if (extra_flags) {
1645 if (flags & BTRFS_BLOCK_GROUP_DATA)
1646 fs_info->avail_data_alloc_bits |= extra_flags;
1647 if (flags & BTRFS_BLOCK_GROUP_METADATA)
1648 fs_info->avail_metadata_alloc_bits |= extra_flags;
1649 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
1650 fs_info->avail_system_alloc_bits |= extra_flags;
1651 }
1652}
Chris Mason593060d2008-03-25 16:50:33 -04001653
Yan Zhengc146afa2008-11-12 14:34:12 -05001654static void set_block_group_readonly(struct btrfs_block_group_cache *cache)
1655{
1656 spin_lock(&cache->space_info->lock);
1657 spin_lock(&cache->lock);
1658 if (!cache->ro) {
1659 cache->space_info->bytes_readonly += cache->key.offset -
1660 btrfs_block_group_used(&cache->item);
1661 cache->ro = 1;
1662 }
1663 spin_unlock(&cache->lock);
1664 spin_unlock(&cache->space_info->lock);
1665}
1666
Yan Zheng2b820322008-11-17 21:11:30 -05001667u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04001668{
Yan Zheng2b820322008-11-17 21:11:30 -05001669 u64 num_devices = root->fs_info->fs_devices->rw_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001670
1671 if (num_devices == 1)
1672 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
1673 if (num_devices < 4)
1674 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
1675
Chris Masonec44a352008-04-28 15:29:52 -04001676 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
1677 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
Chris Masona061fc82008-05-07 11:43:44 -04001678 BTRFS_BLOCK_GROUP_RAID10))) {
Chris Masonec44a352008-04-28 15:29:52 -04001679 flags &= ~BTRFS_BLOCK_GROUP_DUP;
Chris Masona061fc82008-05-07 11:43:44 -04001680 }
Chris Masonec44a352008-04-28 15:29:52 -04001681
1682 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
Chris Masona061fc82008-05-07 11:43:44 -04001683 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
Chris Masonec44a352008-04-28 15:29:52 -04001684 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
Chris Masona061fc82008-05-07 11:43:44 -04001685 }
Chris Masonec44a352008-04-28 15:29:52 -04001686
1687 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
1688 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
1689 (flags & BTRFS_BLOCK_GROUP_RAID10) |
1690 (flags & BTRFS_BLOCK_GROUP_DUP)))
1691 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
1692 return flags;
1693}
1694
Josef Bacik6a632092009-02-20 11:00:09 -05001695static u64 btrfs_get_alloc_profile(struct btrfs_root *root, u64 data)
1696{
1697 struct btrfs_fs_info *info = root->fs_info;
1698 u64 alloc_profile;
1699
1700 if (data) {
1701 alloc_profile = info->avail_data_alloc_bits &
1702 info->data_alloc_profile;
1703 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
1704 } else if (root == root->fs_info->chunk_root) {
1705 alloc_profile = info->avail_system_alloc_bits &
1706 info->system_alloc_profile;
1707 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
1708 } else {
1709 alloc_profile = info->avail_metadata_alloc_bits &
1710 info->metadata_alloc_profile;
1711 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
1712 }
1713
1714 return btrfs_reduce_alloc_profile(root, data);
1715}
1716
1717void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode)
1718{
1719 u64 alloc_target;
1720
1721 alloc_target = btrfs_get_alloc_profile(root, 1);
1722 BTRFS_I(inode)->space_info = __find_space_info(root->fs_info,
1723 alloc_target);
1724}
1725
1726/*
1727 * for now this just makes sure we have at least 5% of our metadata space free
1728 * for use.
1729 */
1730int btrfs_check_metadata_free_space(struct btrfs_root *root)
1731{
1732 struct btrfs_fs_info *info = root->fs_info;
1733 struct btrfs_space_info *meta_sinfo;
1734 u64 alloc_target, thresh;
Josef Bacik4e06bdd2009-02-20 10:59:53 -05001735 int committed = 0, ret;
Josef Bacik6a632092009-02-20 11:00:09 -05001736
1737 /* get the space info for where the metadata will live */
1738 alloc_target = btrfs_get_alloc_profile(root, 0);
1739 meta_sinfo = __find_space_info(info, alloc_target);
1740
Josef Bacik4e06bdd2009-02-20 10:59:53 -05001741again:
Josef Bacik6a632092009-02-20 11:00:09 -05001742 spin_lock(&meta_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05001743 if (!meta_sinfo->full)
1744 thresh = meta_sinfo->total_bytes * 80;
1745 else
1746 thresh = meta_sinfo->total_bytes * 95;
Josef Bacik6a632092009-02-20 11:00:09 -05001747
1748 do_div(thresh, 100);
1749
1750 if (meta_sinfo->bytes_used + meta_sinfo->bytes_reserved +
1751 meta_sinfo->bytes_pinned + meta_sinfo->bytes_readonly > thresh) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05001752 struct btrfs_trans_handle *trans;
1753 if (!meta_sinfo->full) {
1754 meta_sinfo->force_alloc = 1;
1755 spin_unlock(&meta_sinfo->lock);
1756
1757 trans = btrfs_start_transaction(root, 1);
1758 if (!trans)
1759 return -ENOMEM;
1760
1761 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
1762 2 * 1024 * 1024, alloc_target, 0);
1763 btrfs_end_transaction(trans, root);
1764 goto again;
1765 }
Josef Bacik6a632092009-02-20 11:00:09 -05001766 spin_unlock(&meta_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05001767
1768 if (!committed) {
1769 committed = 1;
1770 trans = btrfs_join_transaction(root, 1);
1771 if (!trans)
1772 return -ENOMEM;
1773 ret = btrfs_commit_transaction(trans, root);
1774 if (ret)
1775 return ret;
1776 goto again;
1777 }
Josef Bacik6a632092009-02-20 11:00:09 -05001778 return -ENOSPC;
1779 }
1780 spin_unlock(&meta_sinfo->lock);
1781
1782 return 0;
1783}
1784
1785/*
1786 * This will check the space that the inode allocates from to make sure we have
1787 * enough space for bytes.
1788 */
1789int btrfs_check_data_free_space(struct btrfs_root *root, struct inode *inode,
1790 u64 bytes)
1791{
1792 struct btrfs_space_info *data_sinfo;
Josef Bacik4e06bdd2009-02-20 10:59:53 -05001793 int ret = 0, committed = 0;
Josef Bacik6a632092009-02-20 11:00:09 -05001794
1795 /* make sure bytes are sectorsize aligned */
1796 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
1797
1798 data_sinfo = BTRFS_I(inode)->space_info;
1799again:
1800 /* make sure we have enough space to handle the data first */
1801 spin_lock(&data_sinfo->lock);
1802 if (data_sinfo->total_bytes - data_sinfo->bytes_used -
1803 data_sinfo->bytes_delalloc - data_sinfo->bytes_reserved -
1804 data_sinfo->bytes_pinned - data_sinfo->bytes_readonly -
1805 data_sinfo->bytes_may_use < bytes) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05001806 struct btrfs_trans_handle *trans;
1807
Josef Bacik6a632092009-02-20 11:00:09 -05001808 /*
1809 * if we don't have enough free bytes in this space then we need
1810 * to alloc a new chunk.
1811 */
1812 if (!data_sinfo->full) {
1813 u64 alloc_target;
Josef Bacik6a632092009-02-20 11:00:09 -05001814
1815 data_sinfo->force_alloc = 1;
1816 spin_unlock(&data_sinfo->lock);
1817
1818 alloc_target = btrfs_get_alloc_profile(root, 1);
1819 trans = btrfs_start_transaction(root, 1);
1820 if (!trans)
1821 return -ENOMEM;
1822
1823 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
1824 bytes + 2 * 1024 * 1024,
1825 alloc_target, 0);
1826 btrfs_end_transaction(trans, root);
1827 if (ret)
1828 return ret;
1829 goto again;
1830 }
1831 spin_unlock(&data_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05001832
1833 /* commit the current transaction and try again */
1834 if (!committed) {
1835 committed = 1;
1836 trans = btrfs_join_transaction(root, 1);
1837 if (!trans)
1838 return -ENOMEM;
1839 ret = btrfs_commit_transaction(trans, root);
1840 if (ret)
1841 return ret;
1842 goto again;
1843 }
1844
Josef Bacik6a632092009-02-20 11:00:09 -05001845 printk(KERN_ERR "no space left, need %llu, %llu delalloc bytes"
1846 ", %llu bytes_used, %llu bytes_reserved, "
1847 "%llu bytes_pinned, %llu bytes_readonly, %llu may use"
1848 "%llu total\n", bytes, data_sinfo->bytes_delalloc,
1849 data_sinfo->bytes_used, data_sinfo->bytes_reserved,
1850 data_sinfo->bytes_pinned, data_sinfo->bytes_readonly,
1851 data_sinfo->bytes_may_use, data_sinfo->total_bytes);
1852 return -ENOSPC;
1853 }
1854 data_sinfo->bytes_may_use += bytes;
1855 BTRFS_I(inode)->reserved_bytes += bytes;
1856 spin_unlock(&data_sinfo->lock);
1857
1858 return btrfs_check_metadata_free_space(root);
1859}
1860
1861/*
1862 * if there was an error for whatever reason after calling
1863 * btrfs_check_data_free_space, call this so we can cleanup the counters.
1864 */
1865void btrfs_free_reserved_data_space(struct btrfs_root *root,
1866 struct inode *inode, u64 bytes)
1867{
1868 struct btrfs_space_info *data_sinfo;
1869
1870 /* make sure bytes are sectorsize aligned */
1871 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
1872
1873 data_sinfo = BTRFS_I(inode)->space_info;
1874 spin_lock(&data_sinfo->lock);
1875 data_sinfo->bytes_may_use -= bytes;
1876 BTRFS_I(inode)->reserved_bytes -= bytes;
1877 spin_unlock(&data_sinfo->lock);
1878}
1879
1880/* called when we are adding a delalloc extent to the inode's io_tree */
1881void btrfs_delalloc_reserve_space(struct btrfs_root *root, struct inode *inode,
1882 u64 bytes)
1883{
1884 struct btrfs_space_info *data_sinfo;
1885
1886 /* get the space info for where this inode will be storing its data */
1887 data_sinfo = BTRFS_I(inode)->space_info;
1888
1889 /* make sure we have enough space to handle the data first */
1890 spin_lock(&data_sinfo->lock);
1891 data_sinfo->bytes_delalloc += bytes;
1892
1893 /*
1894 * we are adding a delalloc extent without calling
1895 * btrfs_check_data_free_space first. This happens on a weird
1896 * writepage condition, but shouldn't hurt our accounting
1897 */
1898 if (unlikely(bytes > BTRFS_I(inode)->reserved_bytes)) {
1899 data_sinfo->bytes_may_use -= BTRFS_I(inode)->reserved_bytes;
1900 BTRFS_I(inode)->reserved_bytes = 0;
1901 } else {
1902 data_sinfo->bytes_may_use -= bytes;
1903 BTRFS_I(inode)->reserved_bytes -= bytes;
1904 }
1905
1906 spin_unlock(&data_sinfo->lock);
1907}
1908
1909/* called when we are clearing an delalloc extent from the inode's io_tree */
1910void btrfs_delalloc_free_space(struct btrfs_root *root, struct inode *inode,
1911 u64 bytes)
1912{
1913 struct btrfs_space_info *info;
1914
1915 info = BTRFS_I(inode)->space_info;
1916
1917 spin_lock(&info->lock);
1918 info->bytes_delalloc -= bytes;
1919 spin_unlock(&info->lock);
1920}
1921
Chris Mason6324fbf2008-03-24 15:01:59 -04001922static int do_chunk_alloc(struct btrfs_trans_handle *trans,
1923 struct btrfs_root *extent_root, u64 alloc_bytes,
Chris Mason0ef3e662008-05-24 14:04:53 -04001924 u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04001925{
1926 struct btrfs_space_info *space_info;
1927 u64 thresh;
Yan Zhengc146afa2008-11-12 14:34:12 -05001928 int ret = 0;
1929
1930 mutex_lock(&extent_root->fs_info->chunk_mutex);
Chris Mason6324fbf2008-03-24 15:01:59 -04001931
Yan Zheng2b820322008-11-17 21:11:30 -05001932 flags = btrfs_reduce_alloc_profile(extent_root, flags);
Chris Masonec44a352008-04-28 15:29:52 -04001933
Chris Mason6324fbf2008-03-24 15:01:59 -04001934 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04001935 if (!space_info) {
1936 ret = update_space_info(extent_root->fs_info, flags,
1937 0, 0, &space_info);
1938 BUG_ON(ret);
1939 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001940 BUG_ON(!space_info);
1941
Josef Bacik25179202008-10-29 14:49:05 -04001942 spin_lock(&space_info->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04001943 if (space_info->force_alloc) {
1944 force = 1;
1945 space_info->force_alloc = 0;
1946 }
Josef Bacik25179202008-10-29 14:49:05 -04001947 if (space_info->full) {
1948 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04001949 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04001950 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001951
Yan Zhengc146afa2008-11-12 14:34:12 -05001952 thresh = space_info->total_bytes - space_info->bytes_readonly;
1953 thresh = div_factor(thresh, 6);
Chris Mason0ef3e662008-05-24 14:04:53 -04001954 if (!force &&
Zheng Yane8569812008-09-26 10:05:48 -04001955 (space_info->bytes_used + space_info->bytes_pinned +
Josef Bacik25179202008-10-29 14:49:05 -04001956 space_info->bytes_reserved + alloc_bytes) < thresh) {
1957 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04001958 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04001959 }
Josef Bacik25179202008-10-29 14:49:05 -04001960 spin_unlock(&space_info->lock);
1961
Yan Zheng2b820322008-11-17 21:11:30 -05001962 ret = btrfs_alloc_chunk(trans, extent_root, flags);
Chris Masond3977122009-01-05 21:25:51 -05001963 if (ret)
Chris Mason6324fbf2008-03-24 15:01:59 -04001964 space_info->full = 1;
Chris Masona74a4b92008-06-25 16:01:31 -04001965out:
Yan Zhengc146afa2008-11-12 14:34:12 -05001966 mutex_unlock(&extent_root->fs_info->chunk_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001967 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04001968}
1969
Chris Mason9078a3e2007-04-26 16:46:15 -04001970static int update_block_group(struct btrfs_trans_handle *trans,
1971 struct btrfs_root *root,
Chris Masondb945352007-10-15 16:15:53 -04001972 u64 bytenr, u64 num_bytes, int alloc,
Chris Mason0b86a832008-03-24 15:01:56 -04001973 int mark_free)
Chris Mason9078a3e2007-04-26 16:46:15 -04001974{
1975 struct btrfs_block_group_cache *cache;
1976 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04001977 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04001978 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04001979 u64 byte_in_group;
Chris Mason3e1ad542007-05-07 20:03:49 -04001980
Chris Masond3977122009-01-05 21:25:51 -05001981 while (total) {
Chris Masondb945352007-10-15 16:15:53 -04001982 cache = btrfs_lookup_block_group(info, bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05001983 if (!cache)
Chris Mason9078a3e2007-04-26 16:46:15 -04001984 return -1;
Chris Masondb945352007-10-15 16:15:53 -04001985 byte_in_group = bytenr - cache->key.objectid;
1986 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04001987
Josef Bacik25179202008-10-29 14:49:05 -04001988 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04001989 spin_lock(&cache->lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001990 cache->dirty = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04001991 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04001992 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04001993 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04001994 old_val += num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04001995 cache->space_info->bytes_used += num_bytes;
Yan Zhenga512bbf2008-12-08 16:46:26 -05001996 if (cache->ro)
Yan Zhengc146afa2008-11-12 14:34:12 -05001997 cache->space_info->bytes_readonly -= num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04001998 btrfs_set_block_group_used(&cache->item, old_val);
1999 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04002000 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04002001 } else {
Chris Masondb945352007-10-15 16:15:53 -04002002 old_val -= num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04002003 cache->space_info->bytes_used -= num_bytes;
Yan Zhengc146afa2008-11-12 14:34:12 -05002004 if (cache->ro)
2005 cache->space_info->bytes_readonly += num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04002006 btrfs_set_block_group_used(&cache->item, old_val);
2007 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04002008 spin_unlock(&cache->space_info->lock);
Chris Masonf510cfe2007-10-15 16:14:48 -04002009 if (mark_free) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04002010 int ret;
Liu Hui1f3c79a2009-01-05 15:57:51 -05002011
2012 ret = btrfs_discard_extent(root, bytenr,
2013 num_bytes);
2014 WARN_ON(ret);
2015
Josef Bacik0f9dd462008-09-23 13:14:11 -04002016 ret = btrfs_add_free_space(cache, bytenr,
2017 num_bytes);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002018 WARN_ON(ret);
Chris Masone37c9e62007-05-09 20:13:14 -04002019 }
Chris Masoncd1bc462007-04-27 10:08:34 -04002020 }
Yan Zhengd2fb3432008-12-11 16:30:39 -05002021 put_block_group(cache);
Chris Masondb945352007-10-15 16:15:53 -04002022 total -= num_bytes;
2023 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04002024 }
2025 return 0;
2026}
Chris Mason6324fbf2008-03-24 15:01:59 -04002027
Chris Masona061fc82008-05-07 11:43:44 -04002028static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
2029{
Josef Bacik0f9dd462008-09-23 13:14:11 -04002030 struct btrfs_block_group_cache *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05002031 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002032
2033 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
2034 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04002035 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002036
Yan Zhengd2fb3432008-12-11 16:30:39 -05002037 bytenr = cache->key.objectid;
2038 put_block_group(cache);
2039
2040 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04002041}
2042
Chris Masone02119d2008-09-05 16:13:11 -04002043int btrfs_update_pinned_extents(struct btrfs_root *root,
Yan324ae4d2007-11-16 14:57:08 -05002044 u64 bytenr, u64 num, int pin)
2045{
2046 u64 len;
2047 struct btrfs_block_group_cache *cache;
2048 struct btrfs_fs_info *fs_info = root->fs_info;
2049
Josef Bacik25179202008-10-29 14:49:05 -04002050 WARN_ON(!mutex_is_locked(&root->fs_info->pinned_mutex));
Yan324ae4d2007-11-16 14:57:08 -05002051 if (pin) {
2052 set_extent_dirty(&fs_info->pinned_extents,
2053 bytenr, bytenr + num - 1, GFP_NOFS);
2054 } else {
2055 clear_extent_dirty(&fs_info->pinned_extents,
2056 bytenr, bytenr + num - 1, GFP_NOFS);
2057 }
Chris Masonb9473432009-03-13 11:00:37 -04002058 mutex_unlock(&root->fs_info->pinned_mutex);
2059
Yan324ae4d2007-11-16 14:57:08 -05002060 while (num > 0) {
2061 cache = btrfs_lookup_block_group(fs_info, bytenr);
Zheng Yane8569812008-09-26 10:05:48 -04002062 BUG_ON(!cache);
2063 len = min(num, cache->key.offset -
2064 (bytenr - cache->key.objectid));
Yan324ae4d2007-11-16 14:57:08 -05002065 if (pin) {
Josef Bacik25179202008-10-29 14:49:05 -04002066 spin_lock(&cache->space_info->lock);
Zheng Yane8569812008-09-26 10:05:48 -04002067 spin_lock(&cache->lock);
2068 cache->pinned += len;
2069 cache->space_info->bytes_pinned += len;
2070 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04002071 spin_unlock(&cache->space_info->lock);
Yan324ae4d2007-11-16 14:57:08 -05002072 fs_info->total_pinned += len;
2073 } else {
Josef Bacik25179202008-10-29 14:49:05 -04002074 spin_lock(&cache->space_info->lock);
Zheng Yane8569812008-09-26 10:05:48 -04002075 spin_lock(&cache->lock);
2076 cache->pinned -= len;
2077 cache->space_info->bytes_pinned -= len;
2078 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04002079 spin_unlock(&cache->space_info->lock);
Yan324ae4d2007-11-16 14:57:08 -05002080 fs_info->total_pinned -= len;
Josef Bacik07103a32008-11-19 15:17:55 -05002081 if (cache->cached)
2082 btrfs_add_free_space(cache, bytenr, len);
Yan324ae4d2007-11-16 14:57:08 -05002083 }
Yan Zhengd2fb3432008-12-11 16:30:39 -05002084 put_block_group(cache);
Yan324ae4d2007-11-16 14:57:08 -05002085 bytenr += len;
2086 num -= len;
2087 }
2088 return 0;
2089}
Chris Mason9078a3e2007-04-26 16:46:15 -04002090
Zheng Yane8569812008-09-26 10:05:48 -04002091static int update_reserved_extents(struct btrfs_root *root,
2092 u64 bytenr, u64 num, int reserve)
2093{
2094 u64 len;
2095 struct btrfs_block_group_cache *cache;
2096 struct btrfs_fs_info *fs_info = root->fs_info;
2097
Zheng Yane8569812008-09-26 10:05:48 -04002098 while (num > 0) {
2099 cache = btrfs_lookup_block_group(fs_info, bytenr);
2100 BUG_ON(!cache);
2101 len = min(num, cache->key.offset -
2102 (bytenr - cache->key.objectid));
Josef Bacik25179202008-10-29 14:49:05 -04002103
2104 spin_lock(&cache->space_info->lock);
2105 spin_lock(&cache->lock);
Zheng Yane8569812008-09-26 10:05:48 -04002106 if (reserve) {
Zheng Yane8569812008-09-26 10:05:48 -04002107 cache->reserved += len;
2108 cache->space_info->bytes_reserved += len;
Zheng Yane8569812008-09-26 10:05:48 -04002109 } else {
Zheng Yane8569812008-09-26 10:05:48 -04002110 cache->reserved -= len;
2111 cache->space_info->bytes_reserved -= len;
Zheng Yane8569812008-09-26 10:05:48 -04002112 }
Josef Bacik25179202008-10-29 14:49:05 -04002113 spin_unlock(&cache->lock);
2114 spin_unlock(&cache->space_info->lock);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002115 put_block_group(cache);
Zheng Yane8569812008-09-26 10:05:48 -04002116 bytenr += len;
2117 num -= len;
2118 }
2119 return 0;
2120}
2121
Chris Masond1310b22008-01-24 16:13:08 -05002122int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
Chris Masonccd467d2007-06-28 15:57:36 -04002123{
Chris Masonccd467d2007-06-28 15:57:36 -04002124 u64 last = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -04002125 u64 start;
2126 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -05002127 struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
Chris Masonccd467d2007-06-28 15:57:36 -04002128 int ret;
Chris Masonccd467d2007-06-28 15:57:36 -04002129
Josef Bacik25179202008-10-29 14:49:05 -04002130 mutex_lock(&root->fs_info->pinned_mutex);
Chris Masond3977122009-01-05 21:25:51 -05002131 while (1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04002132 ret = find_first_extent_bit(pinned_extents, last,
2133 &start, &end, EXTENT_DIRTY);
2134 if (ret)
Chris Masonccd467d2007-06-28 15:57:36 -04002135 break;
Chris Mason1a5bc162007-10-15 16:15:26 -04002136 set_extent_dirty(copy, start, end, GFP_NOFS);
2137 last = end + 1;
Chris Masonccd467d2007-06-28 15:57:36 -04002138 }
Josef Bacik25179202008-10-29 14:49:05 -04002139 mutex_unlock(&root->fs_info->pinned_mutex);
Chris Masonccd467d2007-06-28 15:57:36 -04002140 return 0;
2141}
2142
2143int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
2144 struct btrfs_root *root,
Chris Masond1310b22008-01-24 16:13:08 -05002145 struct extent_io_tree *unpin)
Chris Masona28ec192007-03-06 20:08:01 -05002146{
Chris Mason1a5bc162007-10-15 16:15:26 -04002147 u64 start;
2148 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05002149 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05002150
Chris Masond3977122009-01-05 21:25:51 -05002151 while (1) {
Chris Masonb9473432009-03-13 11:00:37 -04002152 mutex_lock(&root->fs_info->pinned_mutex);
Chris Mason1a5bc162007-10-15 16:15:26 -04002153 ret = find_first_extent_bit(unpin, 0, &start, &end,
2154 EXTENT_DIRTY);
2155 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05002156 break;
Liu Hui1f3c79a2009-01-05 15:57:51 -05002157
2158 ret = btrfs_discard_extent(root, start, end + 1 - start);
2159
Chris Masonb9473432009-03-13 11:00:37 -04002160 /* unlocks the pinned mutex */
Chris Masone02119d2008-09-05 16:13:11 -04002161 btrfs_update_pinned_extents(root, start, end + 1 - start, 0);
Chris Mason1a5bc162007-10-15 16:15:26 -04002162 clear_extent_dirty(unpin, start, end, GFP_NOFS);
Liu Hui1f3c79a2009-01-05 15:57:51 -05002163
Chris Masonb9473432009-03-13 11:00:37 -04002164 cond_resched();
Chris Masona28ec192007-03-06 20:08:01 -05002165 }
Josef Bacik25179202008-10-29 14:49:05 -04002166 mutex_unlock(&root->fs_info->pinned_mutex);
Liu Hui1f3c79a2009-01-05 15:57:51 -05002167 return ret;
Chris Masona28ec192007-03-06 20:08:01 -05002168}
2169
Zheng Yan31840ae2008-09-23 13:14:14 -04002170static int pin_down_bytes(struct btrfs_trans_handle *trans,
2171 struct btrfs_root *root,
Chris Masonb9473432009-03-13 11:00:37 -04002172 struct btrfs_path *path,
2173 u64 bytenr, u64 num_bytes, int is_data,
2174 struct extent_buffer **must_clean)
Chris Masone20d96d2007-03-22 12:13:20 -04002175{
Chris Mason1a5bc162007-10-15 16:15:26 -04002176 int err = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04002177 struct extent_buffer *buf;
Chris Mason78fae272007-03-25 11:35:08 -04002178
Zheng Yan31840ae2008-09-23 13:14:14 -04002179 if (is_data)
2180 goto pinit;
Chris Mason4bef0842008-09-08 11:18:08 -04002181
Zheng Yan31840ae2008-09-23 13:14:14 -04002182 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
2183 if (!buf)
2184 goto pinit;
Chris Mason4bef0842008-09-08 11:18:08 -04002185
Zheng Yan31840ae2008-09-23 13:14:14 -04002186 /* we can reuse a block if it hasn't been written
2187 * and it is from this transaction. We can't
2188 * reuse anything from the tree log root because
2189 * it has tiny sub-transactions.
2190 */
2191 if (btrfs_buffer_uptodate(buf, 0) &&
2192 btrfs_try_tree_lock(buf)) {
2193 u64 header_owner = btrfs_header_owner(buf);
2194 u64 header_transid = btrfs_header_generation(buf);
2195 if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
Zheng Yan1a40e232008-09-26 10:09:34 -04002196 header_owner != BTRFS_TREE_RELOC_OBJECTID &&
Chris Mason56bec292009-03-13 10:10:06 -04002197 header_owner != BTRFS_DATA_RELOC_TREE_OBJECTID &&
Zheng Yan31840ae2008-09-23 13:14:14 -04002198 header_transid == trans->transid &&
2199 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
Chris Masonb9473432009-03-13 11:00:37 -04002200 *must_clean = buf;
Zheng Yan31840ae2008-09-23 13:14:14 -04002201 return 1;
Chris Mason8ef97622007-03-26 10:15:30 -04002202 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002203 btrfs_tree_unlock(buf);
Chris Masonf4b9aa82007-03-27 11:05:53 -04002204 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002205 free_extent_buffer(buf);
2206pinit:
Chris Masonb9473432009-03-13 11:00:37 -04002207 btrfs_set_path_blocking(path);
2208 mutex_lock(&root->fs_info->pinned_mutex);
2209 /* unlocks the pinned mutex */
Zheng Yan31840ae2008-09-23 13:14:14 -04002210 btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
2211
Chris Masonbe744172007-05-06 10:15:01 -04002212 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -04002213 return 0;
2214}
2215
Chris Masona28ec192007-03-06 20:08:01 -05002216/*
2217 * remove an extent from the root, returns 0 on success
2218 */
Zheng Yan31840ae2008-09-23 13:14:14 -04002219static int __free_extent(struct btrfs_trans_handle *trans,
2220 struct btrfs_root *root,
2221 u64 bytenr, u64 num_bytes, u64 parent,
Chris Mason7bb86312007-12-11 09:25:06 -05002222 u64 root_objectid, u64 ref_generation,
Chris Mason56bec292009-03-13 10:10:06 -04002223 u64 owner_objectid, int pin, int mark_free,
2224 int refs_to_drop)
Chris Masona28ec192007-03-06 20:08:01 -05002225{
Chris Mason5caf2a02007-04-02 11:20:42 -04002226 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -04002227 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -04002228 struct btrfs_fs_info *info = root->fs_info;
2229 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04002230 struct extent_buffer *leaf;
Chris Masona28ec192007-03-06 20:08:01 -05002231 int ret;
Chris Mason952fcca2008-02-18 16:33:44 -05002232 int extent_slot = 0;
2233 int found_extent = 0;
2234 int num_to_del = 1;
Chris Mason234b63a2007-03-13 10:46:10 -04002235 struct btrfs_extent_item *ei;
Chris Masoncf27e1e2007-03-13 09:49:06 -04002236 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -05002237
Chris Masondb945352007-10-15 16:15:53 -04002238 key.objectid = bytenr;
Chris Mason62e27492007-03-15 12:56:47 -04002239 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masondb945352007-10-15 16:15:53 -04002240 key.offset = num_bytes;
Chris Mason5caf2a02007-04-02 11:20:42 -04002241 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002242 if (!path)
2243 return -ENOMEM;
2244
Chris Mason3c12ac72008-04-21 12:01:38 -04002245 path->reada = 1;
Chris Masonb9473432009-03-13 11:00:37 -04002246 path->leave_spinning = 1;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002247 ret = lookup_extent_backref(trans, extent_root, path,
2248 bytenr, parent, root_objectid,
2249 ref_generation, owner_objectid, 1);
Chris Mason7bb86312007-12-11 09:25:06 -05002250 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05002251 struct btrfs_key found_key;
2252 extent_slot = path->slots[0];
Chris Masond3977122009-01-05 21:25:51 -05002253 while (extent_slot > 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05002254 extent_slot--;
2255 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2256 extent_slot);
2257 if (found_key.objectid != bytenr)
2258 break;
2259 if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
2260 found_key.offset == num_bytes) {
2261 found_extent = 1;
2262 break;
2263 }
2264 if (path->slots[0] - extent_slot > 5)
2265 break;
2266 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002267 if (!found_extent) {
Chris Mason56bec292009-03-13 10:10:06 -04002268 ret = remove_extent_backref(trans, extent_root, path,
2269 refs_to_drop);
Zheng Yan31840ae2008-09-23 13:14:14 -04002270 BUG_ON(ret);
2271 btrfs_release_path(extent_root, path);
Chris Masonb9473432009-03-13 11:00:37 -04002272 path->leave_spinning = 1;
Zheng Yan31840ae2008-09-23 13:14:14 -04002273 ret = btrfs_search_slot(trans, extent_root,
2274 &key, path, -1, 1);
Josef Bacikf3465ca2008-11-12 14:19:50 -05002275 if (ret) {
2276 printk(KERN_ERR "umm, got %d back from search"
Chris Masond3977122009-01-05 21:25:51 -05002277 ", was looking for %llu\n", ret,
2278 (unsigned long long)bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05002279 btrfs_print_leaf(extent_root, path->nodes[0]);
2280 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002281 BUG_ON(ret);
2282 extent_slot = path->slots[0];
2283 }
Chris Mason7bb86312007-12-11 09:25:06 -05002284 } else {
2285 btrfs_print_leaf(extent_root, path->nodes[0]);
2286 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -05002287 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
Chris Mason56bec292009-03-13 10:10:06 -04002288 "parent %llu root %llu gen %llu owner %llu\n",
Chris Masond3977122009-01-05 21:25:51 -05002289 (unsigned long long)bytenr,
Chris Mason56bec292009-03-13 10:10:06 -04002290 (unsigned long long)parent,
Chris Masond3977122009-01-05 21:25:51 -05002291 (unsigned long long)root_objectid,
2292 (unsigned long long)ref_generation,
2293 (unsigned long long)owner_objectid);
Chris Mason7bb86312007-12-11 09:25:06 -05002294 }
Chris Mason5f39d392007-10-15 16:14:19 -04002295
2296 leaf = path->nodes[0];
Chris Mason952fcca2008-02-18 16:33:44 -05002297 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04002298 struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002299 refs = btrfs_extent_refs(leaf, ei);
Chris Mason952fcca2008-02-18 16:33:44 -05002300
Chris Mason56bec292009-03-13 10:10:06 -04002301 /*
2302 * we're not allowed to delete the extent item if there
2303 * are other delayed ref updates pending
2304 */
2305
2306 BUG_ON(refs < refs_to_drop);
2307 refs -= refs_to_drop;
2308 btrfs_set_extent_refs(leaf, ei, refs);
Chris Mason5f39d392007-10-15 16:14:19 -04002309 btrfs_mark_buffer_dirty(leaf);
2310
Chris Mason56bec292009-03-13 10:10:06 -04002311 if (refs == 0 && found_extent &&
2312 path->slots[0] == extent_slot + 1) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002313 struct btrfs_extent_ref *ref;
2314 ref = btrfs_item_ptr(leaf, path->slots[0],
2315 struct btrfs_extent_ref);
Chris Mason56bec292009-03-13 10:10:06 -04002316 BUG_ON(btrfs_ref_num_refs(leaf, ref) != refs_to_drop);
Chris Mason952fcca2008-02-18 16:33:44 -05002317 /* if the back ref and the extent are next to each other
2318 * they get deleted below in one shot
2319 */
2320 path->slots[0] = extent_slot;
2321 num_to_del = 2;
2322 } else if (found_extent) {
2323 /* otherwise delete the extent back ref */
Chris Mason56bec292009-03-13 10:10:06 -04002324 ret = remove_extent_backref(trans, extent_root, path,
2325 refs_to_drop);
Chris Mason952fcca2008-02-18 16:33:44 -05002326 BUG_ON(ret);
2327 /* if refs are 0, we need to setup the path for deletion */
2328 if (refs == 0) {
2329 btrfs_release_path(extent_root, path);
Chris Masonb9473432009-03-13 11:00:37 -04002330 path->leave_spinning = 1;
Chris Mason952fcca2008-02-18 16:33:44 -05002331 ret = btrfs_search_slot(trans, extent_root, &key, path,
2332 -1, 1);
Chris Mason952fcca2008-02-18 16:33:44 -05002333 BUG_ON(ret);
2334 }
2335 }
2336
Chris Masoncf27e1e2007-03-13 09:49:06 -04002337 if (refs == 0) {
Chris Masondb945352007-10-15 16:15:53 -04002338 u64 super_used;
2339 u64 root_used;
Chris Masonb9473432009-03-13 11:00:37 -04002340 struct extent_buffer *must_clean = NULL;
Chris Mason78fae272007-03-25 11:35:08 -04002341
2342 if (pin) {
Chris Masonb9473432009-03-13 11:00:37 -04002343 ret = pin_down_bytes(trans, root, path,
2344 bytenr, num_bytes,
2345 owner_objectid >= BTRFS_FIRST_FREE_OBJECTID,
2346 &must_clean);
Yanc5492282007-11-06 10:25:25 -05002347 if (ret > 0)
2348 mark_free = 1;
2349 BUG_ON(ret < 0);
Chris Mason78fae272007-03-25 11:35:08 -04002350 }
Chris Masonb9473432009-03-13 11:00:37 -04002351
Josef Bacik58176a92007-08-29 15:47:34 -04002352 /* block accounting for super block */
Chris Mason75eff682008-12-15 15:54:40 -05002353 spin_lock(&info->delalloc_lock);
Chris Masondb945352007-10-15 16:15:53 -04002354 super_used = btrfs_super_bytes_used(&info->super_copy);
2355 btrfs_set_super_bytes_used(&info->super_copy,
2356 super_used - num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -04002357
2358 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04002359 root_used = btrfs_root_used(&root->root_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002360 btrfs_set_root_used(&root->root_item,
Chris Masondb945352007-10-15 16:15:53 -04002361 root_used - num_bytes);
Yan Zheng34bf63c2008-12-19 10:58:46 -05002362 spin_unlock(&info->delalloc_lock);
Chris Masonb9473432009-03-13 11:00:37 -04002363
2364 /*
2365 * it is going to be very rare for someone to be waiting
2366 * on the block we're freeing. del_items might need to
2367 * schedule, so rather than get fancy, just force it
2368 * to blocking here
2369 */
2370 if (must_clean)
2371 btrfs_set_lock_blocking(must_clean);
2372
Chris Mason952fcca2008-02-18 16:33:44 -05002373 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
2374 num_to_del);
Zheng Yan31840ae2008-09-23 13:14:14 -04002375 BUG_ON(ret);
Josef Bacik25179202008-10-29 14:49:05 -04002376 btrfs_release_path(extent_root, path);
David Woodhouse21af8042008-08-12 14:13:26 +01002377
Chris Masonb9473432009-03-13 11:00:37 -04002378 if (must_clean) {
2379 clean_tree_block(NULL, root, must_clean);
2380 btrfs_tree_unlock(must_clean);
2381 free_extent_buffer(must_clean);
2382 }
2383
Chris Mason459931e2008-12-10 09:10:46 -05002384 if (owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
2385 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
2386 BUG_ON(ret);
Chris Masond57e62b2009-03-31 13:47:50 -04002387 } else {
2388 invalidate_mapping_pages(info->btree_inode->i_mapping,
2389 bytenr >> PAGE_CACHE_SHIFT,
2390 (bytenr + num_bytes - 1) >> PAGE_CACHE_SHIFT);
Chris Mason459931e2008-12-10 09:10:46 -05002391 }
2392
Chris Masondcbdd4d2008-12-16 13:51:01 -05002393 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
2394 mark_free);
2395 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -05002396 }
Chris Mason5caf2a02007-04-02 11:20:42 -04002397 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05002398 return ret;
2399}
2400
2401/*
Chris Masonfec577f2007-02-26 10:40:21 -05002402 * remove an extent from the root, returns 0 on success
2403 */
Chris Mason925baed2008-06-25 16:01:30 -04002404static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
Chris Mason56bec292009-03-13 10:10:06 -04002405 struct btrfs_root *root,
2406 u64 bytenr, u64 num_bytes, u64 parent,
2407 u64 root_objectid, u64 ref_generation,
2408 u64 owner_objectid, int pin,
2409 int refs_to_drop)
Chris Masonfec577f2007-02-26 10:40:21 -05002410{
Chris Masondb945352007-10-15 16:15:53 -04002411 WARN_ON(num_bytes < root->sectorsize);
Josef Bacikf3465ca2008-11-12 14:19:50 -05002412
Chris Mason56bec292009-03-13 10:10:06 -04002413 /*
2414 * if metadata always pin
2415 * if data pin when any transaction has committed this
2416 */
2417 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID ||
2418 ref_generation != trans->transid)
Chris Mason4bef0842008-09-08 11:18:08 -04002419 pin = 1;
2420
Chris Mason4bef0842008-09-08 11:18:08 -04002421 if (ref_generation != trans->transid)
2422 pin = 1;
2423
Chris Mason56bec292009-03-13 10:10:06 -04002424 return __free_extent(trans, root, bytenr, num_bytes, parent,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002425 root_objectid, ref_generation,
Chris Mason56bec292009-03-13 10:10:06 -04002426 owner_objectid, pin, pin == 0, refs_to_drop);
Chris Masonfec577f2007-02-26 10:40:21 -05002427}
2428
Chris Mason1887be62009-03-13 10:11:24 -04002429/*
2430 * when we free an extent, it is possible (and likely) that we free the last
2431 * delayed ref for that extent as well. This searches the delayed ref tree for
2432 * a given extent, and if there are no other delayed refs to be processed, it
2433 * removes it from the tree.
2434 */
2435static noinline int check_ref_cleanup(struct btrfs_trans_handle *trans,
2436 struct btrfs_root *root, u64 bytenr)
2437{
2438 struct btrfs_delayed_ref_head *head;
2439 struct btrfs_delayed_ref_root *delayed_refs;
2440 struct btrfs_delayed_ref_node *ref;
2441 struct rb_node *node;
2442 int ret;
2443
2444 delayed_refs = &trans->transaction->delayed_refs;
2445 spin_lock(&delayed_refs->lock);
2446 head = btrfs_find_delayed_ref_head(trans, bytenr);
2447 if (!head)
2448 goto out;
2449
2450 node = rb_prev(&head->node.rb_node);
2451 if (!node)
2452 goto out;
2453
2454 ref = rb_entry(node, struct btrfs_delayed_ref_node, rb_node);
2455
2456 /* there are still entries for this ref, we can't drop it */
2457 if (ref->bytenr == bytenr)
2458 goto out;
2459
2460 /*
2461 * waiting for the lock here would deadlock. If someone else has it
2462 * locked they are already in the process of dropping it anyway
2463 */
2464 if (!mutex_trylock(&head->mutex))
2465 goto out;
2466
2467 /*
2468 * at this point we have a head with no other entries. Go
2469 * ahead and process it.
2470 */
2471 head->node.in_tree = 0;
2472 rb_erase(&head->node.rb_node, &delayed_refs->root);
Chris Masonc3e69d52009-03-13 10:17:05 -04002473
Chris Mason1887be62009-03-13 10:11:24 -04002474 delayed_refs->num_entries--;
2475
2476 /*
2477 * we don't take a ref on the node because we're removing it from the
2478 * tree, so we just steal the ref the tree was holding.
2479 */
Chris Masonc3e69d52009-03-13 10:17:05 -04002480 delayed_refs->num_heads--;
2481 if (list_empty(&head->cluster))
2482 delayed_refs->num_heads_ready--;
2483
2484 list_del_init(&head->cluster);
Chris Mason1887be62009-03-13 10:11:24 -04002485 spin_unlock(&delayed_refs->lock);
2486
2487 ret = run_one_delayed_ref(trans, root->fs_info->tree_root,
2488 &head->node, head->must_insert_reserved);
2489 BUG_ON(ret);
2490 btrfs_put_delayed_ref(&head->node);
2491 return 0;
2492out:
2493 spin_unlock(&delayed_refs->lock);
2494 return 0;
2495}
2496
Chris Mason925baed2008-06-25 16:01:30 -04002497int btrfs_free_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04002498 struct btrfs_root *root,
2499 u64 bytenr, u64 num_bytes, u64 parent,
2500 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002501 u64 owner_objectid, int pin)
Chris Mason925baed2008-06-25 16:01:30 -04002502{
2503 int ret;
2504
Chris Mason56bec292009-03-13 10:10:06 -04002505 /*
2506 * tree log blocks never actually go into the extent allocation
2507 * tree, just update pinning info and exit early.
2508 *
2509 * data extents referenced by the tree log do need to have
2510 * their reference counts bumped.
2511 */
2512 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID &&
2513 owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
2514 mutex_lock(&root->fs_info->pinned_mutex);
Chris Masonb9473432009-03-13 11:00:37 -04002515
2516 /* unlocks the pinned mutex */
Chris Mason56bec292009-03-13 10:10:06 -04002517 btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
Chris Mason56bec292009-03-13 10:10:06 -04002518 update_reserved_extents(root, bytenr, num_bytes, 0);
2519 ret = 0;
2520 } else {
2521 ret = btrfs_add_delayed_ref(trans, bytenr, num_bytes, parent,
2522 root_objectid, ref_generation,
2523 owner_objectid,
2524 BTRFS_DROP_DELAYED_REF, 1);
Chris Mason1887be62009-03-13 10:11:24 -04002525 BUG_ON(ret);
2526 ret = check_ref_cleanup(trans, root, bytenr);
2527 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04002528 }
Chris Mason925baed2008-06-25 16:01:30 -04002529 return ret;
2530}
2531
Chris Mason87ee04e2007-11-30 11:30:34 -05002532static u64 stripe_align(struct btrfs_root *root, u64 val)
2533{
2534 u64 mask = ((u64)root->stripesize - 1);
2535 u64 ret = (val + mask) & ~mask;
2536 return ret;
2537}
2538
Chris Masonfec577f2007-02-26 10:40:21 -05002539/*
2540 * walks the btree of allocated extents and find a hole of a given size.
2541 * The key ins is changed to record the hole:
2542 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04002543 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05002544 * ins->offset == number of blocks
2545 * Any available blocks before search_start are skipped.
2546 */
Chris Masond3977122009-01-05 21:25:51 -05002547static noinline int find_free_extent(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05002548 struct btrfs_root *orig_root,
2549 u64 num_bytes, u64 empty_size,
2550 u64 search_start, u64 search_end,
2551 u64 hint_byte, struct btrfs_key *ins,
2552 u64 exclude_start, u64 exclude_nr,
2553 int data)
Chris Masonfec577f2007-02-26 10:40:21 -05002554{
Josef Bacik80eb2342008-10-29 14:49:05 -04002555 int ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05002556 struct btrfs_root *root = orig_root->fs_info->extent_root;
Chris Masondb945352007-10-15 16:15:53 -04002557 u64 total_needed = num_bytes;
Chris Mason239b14b2008-03-24 15:02:07 -04002558 u64 *last_ptr = NULL;
Chris Mason43662112008-11-07 09:06:11 -05002559 u64 last_wanted = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04002560 struct btrfs_block_group_cache *block_group = NULL;
Chris Mason0ef3e662008-05-24 14:04:53 -04002561 int chunk_alloc_done = 0;
Chris Mason239b14b2008-03-24 15:02:07 -04002562 int empty_cluster = 2 * 1024 * 1024;
Chris Mason0ef3e662008-05-24 14:04:53 -04002563 int allowed_chunk_alloc = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04002564 struct list_head *head = NULL, *cur = NULL;
2565 int loop = 0;
Chris Masonf5a31e12008-11-10 11:47:09 -05002566 int extra_loop = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04002567 struct btrfs_space_info *space_info;
Chris Masonfec577f2007-02-26 10:40:21 -05002568
Chris Masondb945352007-10-15 16:15:53 -04002569 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04002570 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
Josef Bacik80eb2342008-10-29 14:49:05 -04002571 ins->objectid = 0;
2572 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04002573
Chris Mason0ef3e662008-05-24 14:04:53 -04002574 if (orig_root->ref_cows || empty_size)
2575 allowed_chunk_alloc = 1;
2576
Chris Mason239b14b2008-03-24 15:02:07 -04002577 if (data & BTRFS_BLOCK_GROUP_METADATA) {
2578 last_ptr = &root->fs_info->last_alloc;
Chris Mason536ac8a2009-02-12 09:41:38 -05002579 if (!btrfs_test_opt(root, SSD))
2580 empty_cluster = 64 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04002581 }
2582
Josef Bacik0f9dd462008-09-23 13:14:11 -04002583 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD))
Chris Mason239b14b2008-03-24 15:02:07 -04002584 last_ptr = &root->fs_info->last_data_alloc;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002585
Chris Mason239b14b2008-03-24 15:02:07 -04002586 if (last_ptr) {
Chris Mason43662112008-11-07 09:06:11 -05002587 if (*last_ptr) {
Chris Mason239b14b2008-03-24 15:02:07 -04002588 hint_byte = *last_ptr;
Chris Mason43662112008-11-07 09:06:11 -05002589 last_wanted = *last_ptr;
2590 } else
Chris Mason239b14b2008-03-24 15:02:07 -04002591 empty_size += empty_cluster;
Chris Mason43662112008-11-07 09:06:11 -05002592 } else {
2593 empty_cluster = 0;
Chris Mason239b14b2008-03-24 15:02:07 -04002594 }
Chris Masona061fc82008-05-07 11:43:44 -04002595 search_start = max(search_start, first_logical_byte(root, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04002596 search_start = max(search_start, hint_byte);
Chris Mason0b86a832008-03-24 15:01:56 -04002597
Chris Mason42e70e72008-11-07 18:17:11 -05002598 if (last_wanted && search_start != last_wanted) {
Chris Mason43662112008-11-07 09:06:11 -05002599 last_wanted = 0;
Chris Mason42e70e72008-11-07 18:17:11 -05002600 empty_size += empty_cluster;
2601 }
Chris Mason43662112008-11-07 09:06:11 -05002602
Chris Mason42e70e72008-11-07 18:17:11 -05002603 total_needed += empty_size;
Josef Bacik80eb2342008-10-29 14:49:05 -04002604 block_group = btrfs_lookup_block_group(root->fs_info, search_start);
Yan Zhengd899e052008-10-30 14:25:28 -04002605 if (!block_group)
2606 block_group = btrfs_lookup_first_block_group(root->fs_info,
2607 search_start);
Josef Bacik80eb2342008-10-29 14:49:05 -04002608 space_info = __find_space_info(root->fs_info, data);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002609
Josef Bacik80eb2342008-10-29 14:49:05 -04002610 down_read(&space_info->groups_sem);
2611 while (1) {
2612 struct btrfs_free_space *free_space;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002613 /*
Josef Bacik80eb2342008-10-29 14:49:05 -04002614 * the only way this happens if our hint points to a block
2615 * group thats not of the proper type, while looping this
2616 * should never happen
Josef Bacik0f9dd462008-09-23 13:14:11 -04002617 */
Chris Mason8a1413a2008-11-10 16:13:54 -05002618 if (empty_size)
2619 extra_loop = 1;
2620
Chris Mason42e70e72008-11-07 18:17:11 -05002621 if (!block_group)
2622 goto new_group_no_lock;
2623
Josef Bacikea6a4782008-11-20 12:16:16 -05002624 if (unlikely(!block_group->cached)) {
2625 mutex_lock(&block_group->cache_mutex);
2626 ret = cache_block_group(root, block_group);
2627 mutex_unlock(&block_group->cache_mutex);
2628 if (ret)
2629 break;
2630 }
2631
Josef Bacik25179202008-10-29 14:49:05 -04002632 mutex_lock(&block_group->alloc_mutex);
Josef Bacik80eb2342008-10-29 14:49:05 -04002633 if (unlikely(!block_group_bits(block_group, data)))
Josef Bacik0f9dd462008-09-23 13:14:11 -04002634 goto new_group;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002635
Josef Bacikea6a4782008-11-20 12:16:16 -05002636 if (unlikely(block_group->ro))
Josef Bacik80eb2342008-10-29 14:49:05 -04002637 goto new_group;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002638
Josef Bacik80eb2342008-10-29 14:49:05 -04002639 free_space = btrfs_find_free_space(block_group, search_start,
2640 total_needed);
2641 if (free_space) {
2642 u64 start = block_group->key.objectid;
2643 u64 end = block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -04002644 block_group->key.offset;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002645
Josef Bacik80eb2342008-10-29 14:49:05 -04002646 search_start = stripe_align(root, free_space->offset);
Chris Mason0b86a832008-03-24 15:01:56 -04002647
Josef Bacik80eb2342008-10-29 14:49:05 -04002648 /* move on to the next group */
2649 if (search_start + num_bytes >= search_end)
2650 goto new_group;
Chris Masone37c9e62007-05-09 20:13:14 -04002651
Josef Bacik80eb2342008-10-29 14:49:05 -04002652 /* move on to the next group */
2653 if (search_start + num_bytes > end)
2654 goto new_group;
2655
Chris Mason43662112008-11-07 09:06:11 -05002656 if (last_wanted && search_start != last_wanted) {
Chris Mason3b7885b2008-11-06 21:48:27 -05002657 total_needed += empty_cluster;
Chris Mason8a1413a2008-11-10 16:13:54 -05002658 empty_size += empty_cluster;
Chris Mason43662112008-11-07 09:06:11 -05002659 last_wanted = 0;
Chris Mason3b7885b2008-11-06 21:48:27 -05002660 /*
2661 * if search_start is still in this block group
2662 * then we just re-search this block group
2663 */
2664 if (search_start >= start &&
2665 search_start < end) {
2666 mutex_unlock(&block_group->alloc_mutex);
2667 continue;
2668 }
2669
2670 /* else we go to the next block group */
2671 goto new_group;
2672 }
2673
Josef Bacik80eb2342008-10-29 14:49:05 -04002674 if (exclude_nr > 0 &&
2675 (search_start + num_bytes > exclude_start &&
2676 search_start < exclude_start + exclude_nr)) {
2677 search_start = exclude_start + exclude_nr;
2678 /*
2679 * if search_start is still in this block group
2680 * then we just re-search this block group
2681 */
2682 if (search_start >= start &&
Josef Bacik25179202008-10-29 14:49:05 -04002683 search_start < end) {
2684 mutex_unlock(&block_group->alloc_mutex);
Chris Mason43662112008-11-07 09:06:11 -05002685 last_wanted = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04002686 continue;
Josef Bacik25179202008-10-29 14:49:05 -04002687 }
Josef Bacik80eb2342008-10-29 14:49:05 -04002688
2689 /* else we go to the next block group */
2690 goto new_group;
2691 }
2692
2693 ins->objectid = search_start;
2694 ins->offset = num_bytes;
Josef Bacik25179202008-10-29 14:49:05 -04002695
2696 btrfs_remove_free_space_lock(block_group, search_start,
2697 num_bytes);
Josef Bacik80eb2342008-10-29 14:49:05 -04002698 /* we are all good, lets return */
Josef Bacik25179202008-10-29 14:49:05 -04002699 mutex_unlock(&block_group->alloc_mutex);
Josef Bacik80eb2342008-10-29 14:49:05 -04002700 break;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002701 }
Josef Bacik80eb2342008-10-29 14:49:05 -04002702new_group:
Chris Mason42e70e72008-11-07 18:17:11 -05002703 mutex_unlock(&block_group->alloc_mutex);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002704 put_block_group(block_group);
2705 block_group = NULL;
Chris Mason42e70e72008-11-07 18:17:11 -05002706new_group_no_lock:
Chris Masonf5a31e12008-11-10 11:47:09 -05002707 /* don't try to compare new allocations against the
2708 * last allocation any more
2709 */
Chris Mason43662112008-11-07 09:06:11 -05002710 last_wanted = 0;
Chris Masonf5a31e12008-11-10 11:47:09 -05002711
Josef Bacik80eb2342008-10-29 14:49:05 -04002712 /*
2713 * Here's how this works.
2714 * loop == 0: we were searching a block group via a hint
2715 * and didn't find anything, so we start at
2716 * the head of the block groups and keep searching
2717 * loop == 1: we're searching through all of the block groups
2718 * if we hit the head again we have searched
2719 * all of the block groups for this space and we
2720 * need to try and allocate, if we cant error out.
2721 * loop == 2: we allocated more space and are looping through
2722 * all of the block groups again.
2723 */
2724 if (loop == 0) {
2725 head = &space_info->block_groups;
2726 cur = head->next;
Josef Bacik80eb2342008-10-29 14:49:05 -04002727 loop++;
2728 } else if (loop == 1 && cur == head) {
Chris Masonf5a31e12008-11-10 11:47:09 -05002729 int keep_going;
Chris Mason42e70e72008-11-07 18:17:11 -05002730
Chris Masonf5a31e12008-11-10 11:47:09 -05002731 /* at this point we give up on the empty_size
2732 * allocations and just try to allocate the min
2733 * space.
2734 *
2735 * The extra_loop field was set if an empty_size
2736 * allocation was attempted above, and if this
2737 * is try we need to try the loop again without
2738 * the additional empty_size.
2739 */
Chris Mason5b7c3fc2008-11-10 07:26:33 -05002740 total_needed -= empty_size;
2741 empty_size = 0;
Chris Masonf5a31e12008-11-10 11:47:09 -05002742 keep_going = extra_loop;
2743 loop++;
Chris Mason42e70e72008-11-07 18:17:11 -05002744
Josef Bacik80eb2342008-10-29 14:49:05 -04002745 if (allowed_chunk_alloc && !chunk_alloc_done) {
2746 up_read(&space_info->groups_sem);
2747 ret = do_chunk_alloc(trans, root, num_bytes +
2748 2 * 1024 * 1024, data, 1);
Josef Bacik80eb2342008-10-29 14:49:05 -04002749 down_read(&space_info->groups_sem);
Chris Mason2ed6d662008-11-13 09:59:33 -05002750 if (ret < 0)
2751 goto loop_check;
Josef Bacik80eb2342008-10-29 14:49:05 -04002752 head = &space_info->block_groups;
Chris Masonf5a31e12008-11-10 11:47:09 -05002753 /*
2754 * we've allocated a new chunk, keep
2755 * trying
2756 */
2757 keep_going = 1;
Josef Bacik80eb2342008-10-29 14:49:05 -04002758 chunk_alloc_done = 1;
2759 } else if (!allowed_chunk_alloc) {
2760 space_info->force_alloc = 1;
Chris Masonf5a31e12008-11-10 11:47:09 -05002761 }
Chris Mason2ed6d662008-11-13 09:59:33 -05002762loop_check:
Chris Masonf5a31e12008-11-10 11:47:09 -05002763 if (keep_going) {
2764 cur = head->next;
2765 extra_loop = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04002766 } else {
2767 break;
2768 }
2769 } else if (cur == head) {
2770 break;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002771 }
Josef Bacik80eb2342008-10-29 14:49:05 -04002772
2773 block_group = list_entry(cur, struct btrfs_block_group_cache,
2774 list);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002775 atomic_inc(&block_group->count);
2776
Josef Bacik80eb2342008-10-29 14:49:05 -04002777 search_start = block_group->key.objectid;
2778 cur = cur->next;
Chris Masone19caa52007-10-15 16:17:44 -04002779 }
Chris Mason0b86a832008-03-24 15:01:56 -04002780
Josef Bacik80eb2342008-10-29 14:49:05 -04002781 /* we found what we needed */
2782 if (ins->objectid) {
2783 if (!(data & BTRFS_BLOCK_GROUP_DATA))
Yan Zhengd2fb3432008-12-11 16:30:39 -05002784 trans->block_group = block_group->key.objectid;
Josef Bacik80eb2342008-10-29 14:49:05 -04002785
2786 if (last_ptr)
2787 *last_ptr = ins->objectid + ins->offset;
2788 ret = 0;
2789 } else if (!ret) {
Chris Masond3977122009-01-05 21:25:51 -05002790 printk(KERN_ERR "btrfs searching for %llu bytes, "
2791 "num_bytes %llu, loop %d, allowed_alloc %d\n",
2792 (unsigned long long)total_needed,
2793 (unsigned long long)num_bytes,
Josef Bacik4ce4cb52008-11-17 21:12:00 -05002794 loop, allowed_chunk_alloc);
Josef Bacik80eb2342008-10-29 14:49:05 -04002795 ret = -ENOSPC;
Chris Masonf2654de2007-06-26 12:20:46 -04002796 }
Yan Zhengd2fb3432008-12-11 16:30:39 -05002797 if (block_group)
2798 put_block_group(block_group);
Chris Mason0b86a832008-03-24 15:01:56 -04002799
Josef Bacik80eb2342008-10-29 14:49:05 -04002800 up_read(&space_info->groups_sem);
Chris Mason0f70abe2007-02-28 16:46:22 -05002801 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05002802}
Chris Masonec44a352008-04-28 15:29:52 -04002803
Josef Bacik0f9dd462008-09-23 13:14:11 -04002804static void dump_space_info(struct btrfs_space_info *info, u64 bytes)
2805{
2806 struct btrfs_block_group_cache *cache;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002807
Chris Masond3977122009-01-05 21:25:51 -05002808 printk(KERN_INFO "space_info has %llu free, is %sfull\n",
2809 (unsigned long long)(info->total_bytes - info->bytes_used -
2810 info->bytes_pinned - info->bytes_reserved),
2811 (info->full) ? "" : "not ");
Josef Bacik6a632092009-02-20 11:00:09 -05002812 printk(KERN_INFO "space_info total=%llu, pinned=%llu, delalloc=%llu,"
2813 " may_use=%llu, used=%llu\n", info->total_bytes,
2814 info->bytes_pinned, info->bytes_delalloc, info->bytes_may_use,
2815 info->bytes_used);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002816
Josef Bacik80eb2342008-10-29 14:49:05 -04002817 down_read(&info->groups_sem);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05002818 list_for_each_entry(cache, &info->block_groups, list) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04002819 spin_lock(&cache->lock);
Chris Masond3977122009-01-05 21:25:51 -05002820 printk(KERN_INFO "block group %llu has %llu bytes, %llu used "
2821 "%llu pinned %llu reserved\n",
2822 (unsigned long long)cache->key.objectid,
2823 (unsigned long long)cache->key.offset,
2824 (unsigned long long)btrfs_block_group_used(&cache->item),
2825 (unsigned long long)cache->pinned,
2826 (unsigned long long)cache->reserved);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002827 btrfs_dump_free_space(cache, bytes);
2828 spin_unlock(&cache->lock);
2829 }
Josef Bacik80eb2342008-10-29 14:49:05 -04002830 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002831}
Zheng Yane8569812008-09-26 10:05:48 -04002832
Chris Masone6dcd2d2008-07-17 12:53:50 -04002833static int __btrfs_reserve_extent(struct btrfs_trans_handle *trans,
2834 struct btrfs_root *root,
2835 u64 num_bytes, u64 min_alloc_size,
2836 u64 empty_size, u64 hint_byte,
2837 u64 search_end, struct btrfs_key *ins,
2838 u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05002839{
2840 int ret;
Chris Masonfbdc7622007-05-30 10:22:12 -04002841 u64 search_start = 0;
Chris Mason1261ec42007-03-20 20:35:03 -04002842 struct btrfs_fs_info *info = root->fs_info;
Chris Mason925baed2008-06-25 16:01:30 -04002843
Josef Bacik6a632092009-02-20 11:00:09 -05002844 data = btrfs_get_alloc_profile(root, data);
Chris Mason98d20f62008-04-14 09:46:10 -04002845again:
Chris Mason0ef3e662008-05-24 14:04:53 -04002846 /*
2847 * the only place that sets empty_size is btrfs_realloc_node, which
2848 * is not called recursively on allocations
2849 */
2850 if (empty_size || root->ref_cows) {
Chris Mason593060d2008-03-25 16:50:33 -04002851 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
Chris Mason6324fbf2008-03-24 15:01:59 -04002852 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04002853 2 * 1024 * 1024,
2854 BTRFS_BLOCK_GROUP_METADATA |
2855 (info->metadata_alloc_profile &
2856 info->avail_metadata_alloc_bits), 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04002857 }
2858 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04002859 num_bytes + 2 * 1024 * 1024, data, 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04002860 }
Chris Mason0b86a832008-03-24 15:01:56 -04002861
Chris Masondb945352007-10-15 16:15:53 -04002862 WARN_ON(num_bytes < root->sectorsize);
2863 ret = find_free_extent(trans, root, num_bytes, empty_size,
2864 search_start, search_end, hint_byte, ins,
Chris Mason26b80032007-08-08 20:17:12 -04002865 trans->alloc_exclude_start,
2866 trans->alloc_exclude_nr, data);
Chris Mason3b951512008-04-17 11:29:12 -04002867
Chris Mason98d20f62008-04-14 09:46:10 -04002868 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
2869 num_bytes = num_bytes >> 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002870 num_bytes = num_bytes & ~(root->sectorsize - 1);
Chris Mason98d20f62008-04-14 09:46:10 -04002871 num_bytes = max(num_bytes, min_alloc_size);
Chris Mason0ef3e662008-05-24 14:04:53 -04002872 do_chunk_alloc(trans, root->fs_info->extent_root,
2873 num_bytes, data, 1);
Chris Mason98d20f62008-04-14 09:46:10 -04002874 goto again;
2875 }
Chris Masonec44a352008-04-28 15:29:52 -04002876 if (ret) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04002877 struct btrfs_space_info *sinfo;
2878
2879 sinfo = __find_space_info(root->fs_info, data);
Chris Masond3977122009-01-05 21:25:51 -05002880 printk(KERN_ERR "btrfs allocation failed flags %llu, "
2881 "wanted %llu\n", (unsigned long long)data,
2882 (unsigned long long)num_bytes);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002883 dump_space_info(sinfo, num_bytes);
Chris Mason925baed2008-06-25 16:01:30 -04002884 BUG();
Chris Mason925baed2008-06-25 16:01:30 -04002885 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002886
2887 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002888}
2889
Chris Mason65b51a02008-08-01 15:11:20 -04002890int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
2891{
Josef Bacik0f9dd462008-09-23 13:14:11 -04002892 struct btrfs_block_group_cache *cache;
Liu Hui1f3c79a2009-01-05 15:57:51 -05002893 int ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002894
Josef Bacik0f9dd462008-09-23 13:14:11 -04002895 cache = btrfs_lookup_block_group(root->fs_info, start);
2896 if (!cache) {
Chris Masond3977122009-01-05 21:25:51 -05002897 printk(KERN_ERR "Unable to find block group for %llu\n",
2898 (unsigned long long)start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002899 return -ENOSPC;
2900 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05002901
2902 ret = btrfs_discard_extent(root, start, len);
2903
Josef Bacik0f9dd462008-09-23 13:14:11 -04002904 btrfs_add_free_space(cache, start, len);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002905 put_block_group(cache);
Zheng Yan1a40e232008-09-26 10:09:34 -04002906 update_reserved_extents(root, start, len, 0);
Liu Hui1f3c79a2009-01-05 15:57:51 -05002907
2908 return ret;
Chris Mason65b51a02008-08-01 15:11:20 -04002909}
2910
Chris Masone6dcd2d2008-07-17 12:53:50 -04002911int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
2912 struct btrfs_root *root,
2913 u64 num_bytes, u64 min_alloc_size,
2914 u64 empty_size, u64 hint_byte,
2915 u64 search_end, struct btrfs_key *ins,
2916 u64 data)
2917{
2918 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002919 ret = __btrfs_reserve_extent(trans, root, num_bytes, min_alloc_size,
2920 empty_size, hint_byte, search_end, ins,
2921 data);
Zheng Yane8569812008-09-26 10:05:48 -04002922 update_reserved_extents(root, ins->objectid, ins->offset, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002923 return ret;
2924}
2925
2926static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04002927 struct btrfs_root *root, u64 parent,
Chris Masone6dcd2d2008-07-17 12:53:50 -04002928 u64 root_objectid, u64 ref_generation,
Chris Mason56bec292009-03-13 10:10:06 -04002929 u64 owner, struct btrfs_key *ins,
2930 int ref_mod)
Chris Masone6dcd2d2008-07-17 12:53:50 -04002931{
2932 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04002933 u64 super_used;
2934 u64 root_used;
2935 u64 num_bytes = ins->offset;
2936 u32 sizes[2];
2937 struct btrfs_fs_info *info = root->fs_info;
2938 struct btrfs_root *extent_root = info->extent_root;
2939 struct btrfs_extent_item *extent_item;
2940 struct btrfs_extent_ref *ref;
2941 struct btrfs_path *path;
2942 struct btrfs_key keys[2];
Chris Masonf2654de2007-06-26 12:20:46 -04002943
Zheng Yan31840ae2008-09-23 13:14:14 -04002944 if (parent == 0)
2945 parent = ins->objectid;
2946
Josef Bacik58176a92007-08-29 15:47:34 -04002947 /* block accounting for super block */
Chris Mason75eff682008-12-15 15:54:40 -05002948 spin_lock(&info->delalloc_lock);
Chris Masondb945352007-10-15 16:15:53 -04002949 super_used = btrfs_super_bytes_used(&info->super_copy);
2950 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
Chris Mason26b80032007-08-08 20:17:12 -04002951
Josef Bacik58176a92007-08-29 15:47:34 -04002952 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04002953 root_used = btrfs_root_used(&root->root_item);
2954 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
Yan Zheng34bf63c2008-12-19 10:58:46 -05002955 spin_unlock(&info->delalloc_lock);
Josef Bacik58176a92007-08-29 15:47:34 -04002956
Chris Mason47e4bb92008-02-01 14:51:59 -05002957 memcpy(&keys[0], ins, sizeof(*ins));
Chris Mason47e4bb92008-02-01 14:51:59 -05002958 keys[1].objectid = ins->objectid;
2959 keys[1].type = BTRFS_EXTENT_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04002960 keys[1].offset = parent;
Chris Mason47e4bb92008-02-01 14:51:59 -05002961 sizes[0] = sizeof(*extent_item);
2962 sizes[1] = sizeof(*ref);
Chris Mason7bb86312007-12-11 09:25:06 -05002963
2964 path = btrfs_alloc_path();
2965 BUG_ON(!path);
Chris Mason47e4bb92008-02-01 14:51:59 -05002966
Chris Masonb9473432009-03-13 11:00:37 -04002967 path->leave_spinning = 1;
Chris Mason47e4bb92008-02-01 14:51:59 -05002968 ret = btrfs_insert_empty_items(trans, extent_root, path, keys,
2969 sizes, 2);
Chris Masonccd467d2007-06-28 15:57:36 -04002970 BUG_ON(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002971
Chris Mason47e4bb92008-02-01 14:51:59 -05002972 extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2973 struct btrfs_extent_item);
Chris Mason56bec292009-03-13 10:10:06 -04002974 btrfs_set_extent_refs(path->nodes[0], extent_item, ref_mod);
Chris Mason47e4bb92008-02-01 14:51:59 -05002975 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
2976 struct btrfs_extent_ref);
2977
2978 btrfs_set_ref_root(path->nodes[0], ref, root_objectid);
2979 btrfs_set_ref_generation(path->nodes[0], ref, ref_generation);
2980 btrfs_set_ref_objectid(path->nodes[0], ref, owner);
Chris Mason56bec292009-03-13 10:10:06 -04002981 btrfs_set_ref_num_refs(path->nodes[0], ref, ref_mod);
Chris Mason47e4bb92008-02-01 14:51:59 -05002982
2983 btrfs_mark_buffer_dirty(path->nodes[0]);
2984
2985 trans->alloc_exclude_start = 0;
2986 trans->alloc_exclude_nr = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05002987 btrfs_free_path(path);
Chris Masonf510cfe2007-10-15 16:14:48 -04002988
Chris Mason925baed2008-06-25 16:01:30 -04002989 if (ret)
2990 goto out;
Chris Mason26b80032007-08-08 20:17:12 -04002991
Chris Masond3977122009-01-05 21:25:51 -05002992 ret = update_block_group(trans, root, ins->objectid,
2993 ins->offset, 1, 0);
Chris Masonf5947062008-02-04 10:10:13 -05002994 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05002995 printk(KERN_ERR "btrfs update block group failed for %llu "
2996 "%llu\n", (unsigned long long)ins->objectid,
2997 (unsigned long long)ins->offset);
Chris Masonf5947062008-02-04 10:10:13 -05002998 BUG();
2999 }
Chris Mason925baed2008-06-25 16:01:30 -04003000out:
Chris Masone6dcd2d2008-07-17 12:53:50 -04003001 return ret;
3002}
3003
3004int btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04003005 struct btrfs_root *root, u64 parent,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003006 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003007 u64 owner, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04003008{
3009 int ret;
Chris Mason1c2308f2008-09-23 13:14:13 -04003010
3011 if (root_objectid == BTRFS_TREE_LOG_OBJECTID)
3012 return 0;
Chris Mason56bec292009-03-13 10:10:06 -04003013
3014 ret = btrfs_add_delayed_ref(trans, ins->objectid,
3015 ins->offset, parent, root_objectid,
3016 ref_generation, owner,
3017 BTRFS_ADD_DELAYED_EXTENT, 0);
3018 BUG_ON(ret);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003019 return ret;
3020}
Chris Masone02119d2008-09-05 16:13:11 -04003021
3022/*
3023 * this is used by the tree logging recovery code. It records that
3024 * an extent has been allocated and makes sure to clear the free
3025 * space cache bits as well
3026 */
3027int btrfs_alloc_logged_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04003028 struct btrfs_root *root, u64 parent,
Chris Masone02119d2008-09-05 16:13:11 -04003029 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003030 u64 owner, struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04003031{
3032 int ret;
3033 struct btrfs_block_group_cache *block_group;
3034
Chris Masone02119d2008-09-05 16:13:11 -04003035 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
Josef Bacikea6a4782008-11-20 12:16:16 -05003036 mutex_lock(&block_group->cache_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04003037 cache_block_group(root, block_group);
Josef Bacikea6a4782008-11-20 12:16:16 -05003038 mutex_unlock(&block_group->cache_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04003039
Josef Bacikea6a4782008-11-20 12:16:16 -05003040 ret = btrfs_remove_free_space(block_group, ins->objectid,
3041 ins->offset);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003042 BUG_ON(ret);
Yan Zhengd2fb3432008-12-11 16:30:39 -05003043 put_block_group(block_group);
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003044 ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid,
Chris Mason56bec292009-03-13 10:10:06 -04003045 ref_generation, owner, ins, 1);
Chris Masone02119d2008-09-05 16:13:11 -04003046 return ret;
3047}
3048
Chris Masone6dcd2d2008-07-17 12:53:50 -04003049/*
3050 * finds a free extent and does all the dirty work required for allocation
3051 * returns the key for the extent through ins, and a tree buffer for
3052 * the first block of the extent through buf.
3053 *
3054 * returns 0 if everything worked, non-zero otherwise.
3055 */
3056int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
3057 struct btrfs_root *root,
Zheng Yan31840ae2008-09-23 13:14:14 -04003058 u64 num_bytes, u64 parent, u64 min_alloc_size,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003059 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003060 u64 owner_objectid, u64 empty_size, u64 hint_byte,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003061 u64 search_end, struct btrfs_key *ins, u64 data)
3062{
3063 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003064 ret = __btrfs_reserve_extent(trans, root, num_bytes,
3065 min_alloc_size, empty_size, hint_byte,
3066 search_end, ins, data);
3067 BUG_ON(ret);
Chris Masond00aff02008-09-11 15:54:42 -04003068 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
Chris Mason56bec292009-03-13 10:10:06 -04003069 ret = btrfs_add_delayed_ref(trans, ins->objectid,
3070 ins->offset, parent, root_objectid,
3071 ref_generation, owner_objectid,
3072 BTRFS_ADD_DELAYED_EXTENT, 0);
Chris Masond00aff02008-09-11 15:54:42 -04003073 BUG_ON(ret);
Chris Masond00aff02008-09-11 15:54:42 -04003074 }
Chris Mason56bec292009-03-13 10:10:06 -04003075 update_reserved_extents(root, ins->objectid, ins->offset, 1);
Chris Mason925baed2008-06-25 16:01:30 -04003076 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05003077}
Chris Mason65b51a02008-08-01 15:11:20 -04003078
3079struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
3080 struct btrfs_root *root,
Chris Mason4008c042009-02-12 14:09:45 -05003081 u64 bytenr, u32 blocksize,
3082 int level)
Chris Mason65b51a02008-08-01 15:11:20 -04003083{
3084 struct extent_buffer *buf;
3085
3086 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
3087 if (!buf)
3088 return ERR_PTR(-ENOMEM);
3089 btrfs_set_header_generation(buf, trans->transid);
Chris Mason4008c042009-02-12 14:09:45 -05003090 btrfs_set_buffer_lockdep_class(buf, level);
Chris Mason65b51a02008-08-01 15:11:20 -04003091 btrfs_tree_lock(buf);
3092 clean_tree_block(trans, root, buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05003093
3094 btrfs_set_lock_blocking(buf);
Chris Mason65b51a02008-08-01 15:11:20 -04003095 btrfs_set_buffer_uptodate(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05003096
Chris Masond0c803c2008-09-11 16:17:57 -04003097 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
3098 set_extent_dirty(&root->dirty_log_pages, buf->start,
Chris Mason65b51a02008-08-01 15:11:20 -04003099 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04003100 } else {
3101 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
3102 buf->start + buf->len - 1, GFP_NOFS);
3103 }
Chris Mason65b51a02008-08-01 15:11:20 -04003104 trans->blocks_used++;
Chris Masonb4ce94d2009-02-04 09:25:08 -05003105 /* this returns a buffer locked for blocking */
Chris Mason65b51a02008-08-01 15:11:20 -04003106 return buf;
3107}
3108
Chris Masonfec577f2007-02-26 10:40:21 -05003109/*
3110 * helper function to allocate a block for a given tree
3111 * returns the tree buffer or NULL.
3112 */
Chris Mason5f39d392007-10-15 16:14:19 -04003113struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Masondb945352007-10-15 16:15:53 -04003114 struct btrfs_root *root,
Zheng Yan31840ae2008-09-23 13:14:14 -04003115 u32 blocksize, u64 parent,
Chris Mason7bb86312007-12-11 09:25:06 -05003116 u64 root_objectid,
3117 u64 ref_generation,
Chris Mason7bb86312007-12-11 09:25:06 -05003118 int level,
3119 u64 hint,
Chris Mason5f39d392007-10-15 16:14:19 -04003120 u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05003121{
Chris Masone2fa7222007-03-12 16:22:34 -04003122 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05003123 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04003124 struct extent_buffer *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05003125
Zheng Yan31840ae2008-09-23 13:14:14 -04003126 ret = btrfs_alloc_extent(trans, root, blocksize, parent, blocksize,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003127 root_objectid, ref_generation, level,
Zheng Yan31840ae2008-09-23 13:14:14 -04003128 empty_size, hint, (u64)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05003129 if (ret) {
Chris Mason54aa1f42007-06-22 14:16:25 -04003130 BUG_ON(ret > 0);
3131 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05003132 }
Chris Mason55c69072008-01-09 15:55:33 -05003133
Chris Mason4008c042009-02-12 14:09:45 -05003134 buf = btrfs_init_new_buffer(trans, root, ins.objectid,
3135 blocksize, level);
Chris Masonfec577f2007-02-26 10:40:21 -05003136 return buf;
3137}
Chris Masona28ec192007-03-06 20:08:01 -05003138
Chris Masone02119d2008-09-05 16:13:11 -04003139int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans,
3140 struct btrfs_root *root, struct extent_buffer *leaf)
Chris Mason6407bf62007-03-27 06:33:00 -04003141{
Chris Mason7bb86312007-12-11 09:25:06 -05003142 u64 leaf_owner;
3143 u64 leaf_generation;
Chris Masonbd56b302009-02-04 09:27:02 -05003144 struct refsort *sorted;
Chris Mason5f39d392007-10-15 16:14:19 -04003145 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04003146 struct btrfs_file_extent_item *fi;
3147 int i;
3148 int nritems;
3149 int ret;
Chris Masonbd56b302009-02-04 09:27:02 -05003150 int refi = 0;
3151 int slot;
Chris Mason6407bf62007-03-27 06:33:00 -04003152
Chris Mason5f39d392007-10-15 16:14:19 -04003153 BUG_ON(!btrfs_is_leaf(leaf));
3154 nritems = btrfs_header_nritems(leaf);
Chris Mason7bb86312007-12-11 09:25:06 -05003155 leaf_owner = btrfs_header_owner(leaf);
3156 leaf_generation = btrfs_header_generation(leaf);
3157
Chris Masonbd56b302009-02-04 09:27:02 -05003158 sorted = kmalloc(sizeof(*sorted) * nritems, GFP_NOFS);
3159 /* we do this loop twice. The first time we build a list
3160 * of the extents we have a reference on, then we sort the list
3161 * by bytenr. The second time around we actually do the
3162 * extent freeing.
3163 */
Chris Mason6407bf62007-03-27 06:33:00 -04003164 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04003165 u64 disk_bytenr;
Chris Masone34a5b42008-07-22 12:08:37 -04003166 cond_resched();
Chris Mason5f39d392007-10-15 16:14:19 -04003167
3168 btrfs_item_key_to_cpu(leaf, &key, i);
Chris Masonbd56b302009-02-04 09:27:02 -05003169
3170 /* only extents have references, skip everything else */
Chris Mason5f39d392007-10-15 16:14:19 -04003171 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -04003172 continue;
Chris Masonbd56b302009-02-04 09:27:02 -05003173
Chris Mason6407bf62007-03-27 06:33:00 -04003174 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Masonbd56b302009-02-04 09:27:02 -05003175
3176 /* inline extents live in the btree, they don't have refs */
Chris Mason5f39d392007-10-15 16:14:19 -04003177 if (btrfs_file_extent_type(leaf, fi) ==
3178 BTRFS_FILE_EXTENT_INLINE)
Chris Mason236454d2007-04-19 13:37:44 -04003179 continue;
Chris Masonbd56b302009-02-04 09:27:02 -05003180
Chris Masondb945352007-10-15 16:15:53 -04003181 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Masonbd56b302009-02-04 09:27:02 -05003182
3183 /* holes don't have refs */
Chris Masondb945352007-10-15 16:15:53 -04003184 if (disk_bytenr == 0)
Chris Mason3a686372007-05-24 13:35:57 -04003185 continue;
Chris Mason4a096752008-07-21 10:29:44 -04003186
Chris Masonbd56b302009-02-04 09:27:02 -05003187 sorted[refi].bytenr = disk_bytenr;
3188 sorted[refi].slot = i;
3189 refi++;
3190 }
3191
3192 if (refi == 0)
3193 goto out;
3194
3195 sort(sorted, refi, sizeof(struct refsort), refsort_cmp, NULL);
3196
3197 for (i = 0; i < refi; i++) {
3198 u64 disk_bytenr;
3199
3200 disk_bytenr = sorted[i].bytenr;
3201 slot = sorted[i].slot;
3202
3203 cond_resched();
3204
3205 btrfs_item_key_to_cpu(leaf, &key, slot);
3206 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
3207 continue;
3208
3209 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
3210
Chris Mason56bec292009-03-13 10:10:06 -04003211 ret = btrfs_free_extent(trans, root, disk_bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -05003212 btrfs_file_extent_disk_num_bytes(leaf, fi),
Zheng Yan31840ae2008-09-23 13:14:14 -04003213 leaf->start, leaf_owner, leaf_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003214 key.objectid, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04003215 BUG_ON(ret);
Chris Mason2dd3e672008-08-04 08:20:15 -04003216
3217 atomic_inc(&root->fs_info->throttle_gen);
3218 wake_up(&root->fs_info->transaction_throttle);
3219 cond_resched();
Chris Mason6407bf62007-03-27 06:33:00 -04003220 }
Chris Masonbd56b302009-02-04 09:27:02 -05003221out:
3222 kfree(sorted);
Chris Mason6407bf62007-03-27 06:33:00 -04003223 return 0;
3224}
3225
Chris Masond3977122009-01-05 21:25:51 -05003226static noinline int cache_drop_leaf_ref(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04003227 struct btrfs_root *root,
3228 struct btrfs_leaf_ref *ref)
Yan Zheng31153d82008-07-28 15:32:19 -04003229{
3230 int i;
3231 int ret;
Chris Masonbd56b302009-02-04 09:27:02 -05003232 struct btrfs_extent_info *info;
3233 struct refsort *sorted;
Yan Zheng31153d82008-07-28 15:32:19 -04003234
Chris Masonbd56b302009-02-04 09:27:02 -05003235 if (ref->nritems == 0)
3236 return 0;
3237
3238 sorted = kmalloc(sizeof(*sorted) * ref->nritems, GFP_NOFS);
Yan Zheng31153d82008-07-28 15:32:19 -04003239 for (i = 0; i < ref->nritems; i++) {
Chris Masonbd56b302009-02-04 09:27:02 -05003240 sorted[i].bytenr = ref->extents[i].bytenr;
3241 sorted[i].slot = i;
3242 }
3243 sort(sorted, ref->nritems, sizeof(struct refsort), refsort_cmp, NULL);
3244
3245 /*
3246 * the items in the ref were sorted when the ref was inserted
3247 * into the ref cache, so this is already in order
3248 */
3249 for (i = 0; i < ref->nritems; i++) {
3250 info = ref->extents + sorted[i].slot;
Chris Mason56bec292009-03-13 10:10:06 -04003251 ret = btrfs_free_extent(trans, root, info->bytenr,
Zheng Yan31840ae2008-09-23 13:14:14 -04003252 info->num_bytes, ref->bytenr,
3253 ref->owner, ref->generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003254 info->objectid, 0);
Chris Mason2dd3e672008-08-04 08:20:15 -04003255
3256 atomic_inc(&root->fs_info->throttle_gen);
3257 wake_up(&root->fs_info->transaction_throttle);
3258 cond_resched();
3259
Yan Zheng31153d82008-07-28 15:32:19 -04003260 BUG_ON(ret);
3261 info++;
3262 }
Yan Zheng31153d82008-07-28 15:32:19 -04003263
Chris Mason806638b2009-02-05 09:08:14 -05003264 kfree(sorted);
Yan Zheng31153d82008-07-28 15:32:19 -04003265 return 0;
3266}
3267
Chris Mason56bec292009-03-13 10:10:06 -04003268static int drop_snap_lookup_refcount(struct btrfs_trans_handle *trans,
3269 struct btrfs_root *root, u64 start,
Chris Masond3977122009-01-05 21:25:51 -05003270 u64 len, u32 *refs)
Chris Mason333db942008-06-25 16:01:30 -04003271{
Chris Mason017e5362008-07-28 15:32:51 -04003272 int ret;
Chris Masonf87f0572008-08-01 11:27:23 -04003273
Chris Mason56bec292009-03-13 10:10:06 -04003274 ret = btrfs_lookup_extent_ref(trans, root, start, len, refs);
Chris Masonf87f0572008-08-01 11:27:23 -04003275 BUG_ON(ret);
3276
Chris Masond3977122009-01-05 21:25:51 -05003277#if 0 /* some debugging code in case we see problems here */
Chris Masonf87f0572008-08-01 11:27:23 -04003278 /* if the refs count is one, it won't get increased again. But
3279 * if the ref count is > 1, someone may be decreasing it at
3280 * the same time we are.
3281 */
3282 if (*refs != 1) {
3283 struct extent_buffer *eb = NULL;
3284 eb = btrfs_find_create_tree_block(root, start, len);
3285 if (eb)
3286 btrfs_tree_lock(eb);
3287
3288 mutex_lock(&root->fs_info->alloc_mutex);
3289 ret = lookup_extent_ref(NULL, root, start, len, refs);
3290 BUG_ON(ret);
3291 mutex_unlock(&root->fs_info->alloc_mutex);
3292
3293 if (eb) {
3294 btrfs_tree_unlock(eb);
3295 free_extent_buffer(eb);
3296 }
3297 if (*refs == 1) {
Chris Masond3977122009-01-05 21:25:51 -05003298 printk(KERN_ERR "btrfs block %llu went down to one "
3299 "during drop_snap\n", (unsigned long long)start);
Chris Masonf87f0572008-08-01 11:27:23 -04003300 }
3301
3302 }
3303#endif
3304
Chris Masone7a84562008-06-25 16:01:31 -04003305 cond_resched();
Chris Mason017e5362008-07-28 15:32:51 -04003306 return ret;
Chris Mason333db942008-06-25 16:01:30 -04003307}
3308
3309/*
Chris Masonbd56b302009-02-04 09:27:02 -05003310 * this is used while deleting old snapshots, and it drops the refs
3311 * on a whole subtree starting from a level 1 node.
3312 *
3313 * The idea is to sort all the leaf pointers, and then drop the
3314 * ref on all the leaves in order. Most of the time the leaves
3315 * will have ref cache entries, so no leaf IOs will be required to
3316 * find the extents they have references on.
3317 *
3318 * For each leaf, any references it has are also dropped in order
3319 *
3320 * This ends up dropping the references in something close to optimal
3321 * order for reading and modifying the extent allocation tree.
3322 */
3323static noinline int drop_level_one_refs(struct btrfs_trans_handle *trans,
3324 struct btrfs_root *root,
3325 struct btrfs_path *path)
3326{
3327 u64 bytenr;
3328 u64 root_owner;
3329 u64 root_gen;
3330 struct extent_buffer *eb = path->nodes[1];
3331 struct extent_buffer *leaf;
3332 struct btrfs_leaf_ref *ref;
3333 struct refsort *sorted = NULL;
3334 int nritems = btrfs_header_nritems(eb);
3335 int ret;
3336 int i;
3337 int refi = 0;
3338 int slot = path->slots[1];
3339 u32 blocksize = btrfs_level_size(root, 0);
3340 u32 refs;
3341
3342 if (nritems == 0)
3343 goto out;
3344
3345 root_owner = btrfs_header_owner(eb);
3346 root_gen = btrfs_header_generation(eb);
3347 sorted = kmalloc(sizeof(*sorted) * nritems, GFP_NOFS);
3348
3349 /*
3350 * step one, sort all the leaf pointers so we don't scribble
3351 * randomly into the extent allocation tree
3352 */
3353 for (i = slot; i < nritems; i++) {
3354 sorted[refi].bytenr = btrfs_node_blockptr(eb, i);
3355 sorted[refi].slot = i;
3356 refi++;
3357 }
3358
3359 /*
3360 * nritems won't be zero, but if we're picking up drop_snapshot
3361 * after a crash, slot might be > 0, so double check things
3362 * just in case.
3363 */
3364 if (refi == 0)
3365 goto out;
3366
3367 sort(sorted, refi, sizeof(struct refsort), refsort_cmp, NULL);
3368
3369 /*
3370 * the first loop frees everything the leaves point to
3371 */
3372 for (i = 0; i < refi; i++) {
3373 u64 ptr_gen;
3374
3375 bytenr = sorted[i].bytenr;
3376
3377 /*
3378 * check the reference count on this leaf. If it is > 1
3379 * we just decrement it below and don't update any
3380 * of the refs the leaf points to.
3381 */
Chris Mason56bec292009-03-13 10:10:06 -04003382 ret = drop_snap_lookup_refcount(trans, root, bytenr,
3383 blocksize, &refs);
Chris Masonbd56b302009-02-04 09:27:02 -05003384 BUG_ON(ret);
3385 if (refs != 1)
3386 continue;
3387
3388 ptr_gen = btrfs_node_ptr_generation(eb, sorted[i].slot);
3389
3390 /*
3391 * the leaf only had one reference, which means the
3392 * only thing pointing to this leaf is the snapshot
3393 * we're deleting. It isn't possible for the reference
3394 * count to increase again later
3395 *
3396 * The reference cache is checked for the leaf,
3397 * and if found we'll be able to drop any refs held by
3398 * the leaf without needing to read it in.
3399 */
3400 ref = btrfs_lookup_leaf_ref(root, bytenr);
3401 if (ref && ref->generation != ptr_gen) {
3402 btrfs_free_leaf_ref(root, ref);
3403 ref = NULL;
3404 }
3405 if (ref) {
3406 ret = cache_drop_leaf_ref(trans, root, ref);
3407 BUG_ON(ret);
3408 btrfs_remove_leaf_ref(root, ref);
3409 btrfs_free_leaf_ref(root, ref);
3410 } else {
3411 /*
3412 * the leaf wasn't in the reference cache, so
3413 * we have to read it.
3414 */
3415 leaf = read_tree_block(root, bytenr, blocksize,
3416 ptr_gen);
3417 ret = btrfs_drop_leaf_ref(trans, root, leaf);
3418 BUG_ON(ret);
3419 free_extent_buffer(leaf);
3420 }
3421 atomic_inc(&root->fs_info->throttle_gen);
3422 wake_up(&root->fs_info->transaction_throttle);
3423 cond_resched();
3424 }
3425
3426 /*
3427 * run through the loop again to free the refs on the leaves.
3428 * This is faster than doing it in the loop above because
3429 * the leaves are likely to be clustered together. We end up
3430 * working in nice chunks on the extent allocation tree.
3431 */
3432 for (i = 0; i < refi; i++) {
3433 bytenr = sorted[i].bytenr;
Chris Mason56bec292009-03-13 10:10:06 -04003434 ret = btrfs_free_extent(trans, root, bytenr,
Chris Masonbd56b302009-02-04 09:27:02 -05003435 blocksize, eb->start,
3436 root_owner, root_gen, 0, 1);
3437 BUG_ON(ret);
3438
3439 atomic_inc(&root->fs_info->throttle_gen);
3440 wake_up(&root->fs_info->transaction_throttle);
3441 cond_resched();
3442 }
3443out:
3444 kfree(sorted);
3445
3446 /*
3447 * update the path to show we've processed the entire level 1
3448 * node. This will get saved into the root's drop_snapshot_progress
3449 * field so these drops are not repeated again if this transaction
3450 * commits.
3451 */
3452 path->slots[1] = nritems;
3453 return 0;
3454}
3455
3456/*
Chris Mason9aca1d52007-03-13 11:09:37 -04003457 * helper function for drop_snapshot, this walks down the tree dropping ref
3458 * counts as it goes.
3459 */
Chris Masond3977122009-01-05 21:25:51 -05003460static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05003461 struct btrfs_root *root,
3462 struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05003463{
Chris Mason7bb86312007-12-11 09:25:06 -05003464 u64 root_owner;
3465 u64 root_gen;
3466 u64 bytenr;
Chris Masonca7a79a2008-05-12 12:59:19 -04003467 u64 ptr_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04003468 struct extent_buffer *next;
3469 struct extent_buffer *cur;
Chris Mason7bb86312007-12-11 09:25:06 -05003470 struct extent_buffer *parent;
Chris Masondb945352007-10-15 16:15:53 -04003471 u32 blocksize;
Chris Mason20524f02007-03-10 06:35:47 -05003472 int ret;
3473 u32 refs;
3474
Chris Mason5caf2a02007-04-02 11:20:42 -04003475 WARN_ON(*level < 0);
3476 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason56bec292009-03-13 10:10:06 -04003477 ret = drop_snap_lookup_refcount(trans, root, path->nodes[*level]->start,
Chris Masondb945352007-10-15 16:15:53 -04003478 path->nodes[*level]->len, &refs);
Chris Mason20524f02007-03-10 06:35:47 -05003479 BUG_ON(ret);
3480 if (refs > 1)
3481 goto out;
Chris Masone0115992007-06-19 16:23:05 -04003482
Chris Mason9aca1d52007-03-13 11:09:37 -04003483 /*
3484 * walk down to the last node level and free all the leaves
3485 */
Chris Masond3977122009-01-05 21:25:51 -05003486 while (*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04003487 WARN_ON(*level < 0);
3488 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -05003489 cur = path->nodes[*level];
Chris Masone0115992007-06-19 16:23:05 -04003490
Chris Mason5f39d392007-10-15 16:14:19 -04003491 if (btrfs_header_level(cur) != *level)
Chris Mason2c90e5d2007-04-02 10:50:19 -04003492 WARN_ON(1);
Chris Masone0115992007-06-19 16:23:05 -04003493
Chris Mason7518a232007-03-12 12:01:18 -04003494 if (path->slots[*level] >=
Chris Mason5f39d392007-10-15 16:14:19 -04003495 btrfs_header_nritems(cur))
Chris Mason20524f02007-03-10 06:35:47 -05003496 break;
Chris Masonbd56b302009-02-04 09:27:02 -05003497
3498 /* the new code goes down to level 1 and does all the
3499 * leaves pointed to that node in bulk. So, this check
3500 * for level 0 will always be false.
3501 *
3502 * But, the disk format allows the drop_snapshot_progress
3503 * field in the root to leave things in a state where
3504 * a leaf will need cleaning up here. If someone crashes
3505 * with the old code and then boots with the new code,
3506 * we might find a leaf here.
3507 */
Chris Mason6407bf62007-03-27 06:33:00 -04003508 if (*level == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003509 ret = btrfs_drop_leaf_ref(trans, root, cur);
Chris Mason6407bf62007-03-27 06:33:00 -04003510 BUG_ON(ret);
3511 break;
3512 }
Chris Masonbd56b302009-02-04 09:27:02 -05003513
3514 /*
3515 * once we get to level one, process the whole node
3516 * at once, including everything below it.
3517 */
3518 if (*level == 1) {
3519 ret = drop_level_one_refs(trans, root, path);
3520 BUG_ON(ret);
3521 break;
3522 }
3523
Chris Masondb945352007-10-15 16:15:53 -04003524 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
Chris Masonca7a79a2008-05-12 12:59:19 -04003525 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
Chris Masondb945352007-10-15 16:15:53 -04003526 blocksize = btrfs_level_size(root, *level - 1);
Chris Mason925baed2008-06-25 16:01:30 -04003527
Chris Mason56bec292009-03-13 10:10:06 -04003528 ret = drop_snap_lookup_refcount(trans, root, bytenr,
3529 blocksize, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -04003530 BUG_ON(ret);
Chris Masonbd56b302009-02-04 09:27:02 -05003531
3532 /*
3533 * if there is more than one reference, we don't need
3534 * to read that node to drop any references it has. We
3535 * just drop the ref we hold on that node and move on to the
3536 * next slot in this level.
3537 */
Chris Mason6407bf62007-03-27 06:33:00 -04003538 if (refs != 1) {
Chris Mason7bb86312007-12-11 09:25:06 -05003539 parent = path->nodes[*level];
3540 root_owner = btrfs_header_owner(parent);
3541 root_gen = btrfs_header_generation(parent);
Chris Mason20524f02007-03-10 06:35:47 -05003542 path->slots[*level]++;
Chris Masonf87f0572008-08-01 11:27:23 -04003543
Chris Mason56bec292009-03-13 10:10:06 -04003544 ret = btrfs_free_extent(trans, root, bytenr,
Zheng Yan31840ae2008-09-23 13:14:14 -04003545 blocksize, parent->start,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003546 root_owner, root_gen,
3547 *level - 1, 1);
Chris Mason20524f02007-03-10 06:35:47 -05003548 BUG_ON(ret);
Chris Mason18e35e0a2008-08-01 13:11:41 -04003549
3550 atomic_inc(&root->fs_info->throttle_gen);
3551 wake_up(&root->fs_info->transaction_throttle);
Chris Mason2dd3e672008-08-04 08:20:15 -04003552 cond_resched();
Chris Mason18e35e0a2008-08-01 13:11:41 -04003553
Chris Mason20524f02007-03-10 06:35:47 -05003554 continue;
3555 }
Chris Mason333db942008-06-25 16:01:30 -04003556
Chris Masonbd56b302009-02-04 09:27:02 -05003557 /*
3558 * we need to keep freeing things in the next level down.
3559 * read the block and loop around to process it
3560 */
3561 next = read_tree_block(root, bytenr, blocksize, ptr_gen);
Chris Mason5caf2a02007-04-02 11:20:42 -04003562 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -04003563 if (path->nodes[*level-1])
Chris Mason5f39d392007-10-15 16:14:19 -04003564 free_extent_buffer(path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -05003565 path->nodes[*level-1] = next;
Chris Mason5f39d392007-10-15 16:14:19 -04003566 *level = btrfs_header_level(next);
Chris Mason20524f02007-03-10 06:35:47 -05003567 path->slots[*level] = 0;
Chris Mason2dd3e672008-08-04 08:20:15 -04003568 cond_resched();
Chris Mason20524f02007-03-10 06:35:47 -05003569 }
3570out:
Chris Mason5caf2a02007-04-02 11:20:42 -04003571 WARN_ON(*level < 0);
3572 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason7bb86312007-12-11 09:25:06 -05003573
3574 if (path->nodes[*level] == root->node) {
Chris Mason7bb86312007-12-11 09:25:06 -05003575 parent = path->nodes[*level];
Yan Zheng31153d82008-07-28 15:32:19 -04003576 bytenr = path->nodes[*level]->start;
Chris Mason7bb86312007-12-11 09:25:06 -05003577 } else {
3578 parent = path->nodes[*level + 1];
Yan Zheng31153d82008-07-28 15:32:19 -04003579 bytenr = btrfs_node_blockptr(parent, path->slots[*level + 1]);
Chris Mason7bb86312007-12-11 09:25:06 -05003580 }
3581
Yan Zheng31153d82008-07-28 15:32:19 -04003582 blocksize = btrfs_level_size(root, *level);
3583 root_owner = btrfs_header_owner(parent);
Chris Mason7bb86312007-12-11 09:25:06 -05003584 root_gen = btrfs_header_generation(parent);
Yan Zheng31153d82008-07-28 15:32:19 -04003585
Chris Masonbd56b302009-02-04 09:27:02 -05003586 /*
3587 * cleanup and free the reference on the last node
3588 * we processed
3589 */
Chris Mason56bec292009-03-13 10:10:06 -04003590 ret = btrfs_free_extent(trans, root, bytenr, blocksize,
Zheng Yan31840ae2008-09-23 13:14:14 -04003591 parent->start, root_owner, root_gen,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003592 *level, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003593 free_extent_buffer(path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -05003594 path->nodes[*level] = NULL;
Chris Masonbd56b302009-02-04 09:27:02 -05003595
Chris Mason20524f02007-03-10 06:35:47 -05003596 *level += 1;
3597 BUG_ON(ret);
Chris Masonf87f0572008-08-01 11:27:23 -04003598
Chris Masone7a84562008-06-25 16:01:31 -04003599 cond_resched();
Chris Mason20524f02007-03-10 06:35:47 -05003600 return 0;
3601}
3602
Chris Mason9aca1d52007-03-13 11:09:37 -04003603/*
Yan Zhengf82d02d2008-10-29 14:49:05 -04003604 * helper function for drop_subtree, this function is similar to
3605 * walk_down_tree. The main difference is that it checks reference
3606 * counts while tree blocks are locked.
3607 */
Chris Masond3977122009-01-05 21:25:51 -05003608static noinline int walk_down_subtree(struct btrfs_trans_handle *trans,
Yan Zhengf82d02d2008-10-29 14:49:05 -04003609 struct btrfs_root *root,
3610 struct btrfs_path *path, int *level)
3611{
3612 struct extent_buffer *next;
3613 struct extent_buffer *cur;
3614 struct extent_buffer *parent;
3615 u64 bytenr;
3616 u64 ptr_gen;
3617 u32 blocksize;
3618 u32 refs;
3619 int ret;
3620
3621 cur = path->nodes[*level];
3622 ret = btrfs_lookup_extent_ref(trans, root, cur->start, cur->len,
3623 &refs);
3624 BUG_ON(ret);
3625 if (refs > 1)
3626 goto out;
3627
3628 while (*level >= 0) {
3629 cur = path->nodes[*level];
3630 if (*level == 0) {
3631 ret = btrfs_drop_leaf_ref(trans, root, cur);
3632 BUG_ON(ret);
3633 clean_tree_block(trans, root, cur);
3634 break;
3635 }
3636 if (path->slots[*level] >= btrfs_header_nritems(cur)) {
3637 clean_tree_block(trans, root, cur);
3638 break;
3639 }
3640
3641 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
3642 blocksize = btrfs_level_size(root, *level - 1);
3643 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
3644
3645 next = read_tree_block(root, bytenr, blocksize, ptr_gen);
3646 btrfs_tree_lock(next);
Chris Masonb4ce94d2009-02-04 09:25:08 -05003647 btrfs_set_lock_blocking(next);
Yan Zhengf82d02d2008-10-29 14:49:05 -04003648
3649 ret = btrfs_lookup_extent_ref(trans, root, bytenr, blocksize,
3650 &refs);
3651 BUG_ON(ret);
3652 if (refs > 1) {
3653 parent = path->nodes[*level];
3654 ret = btrfs_free_extent(trans, root, bytenr,
3655 blocksize, parent->start,
3656 btrfs_header_owner(parent),
3657 btrfs_header_generation(parent),
3658 *level - 1, 1);
3659 BUG_ON(ret);
3660 path->slots[*level]++;
3661 btrfs_tree_unlock(next);
3662 free_extent_buffer(next);
3663 continue;
3664 }
3665
3666 *level = btrfs_header_level(next);
3667 path->nodes[*level] = next;
3668 path->slots[*level] = 0;
3669 path->locks[*level] = 1;
3670 cond_resched();
3671 }
3672out:
3673 parent = path->nodes[*level + 1];
3674 bytenr = path->nodes[*level]->start;
3675 blocksize = path->nodes[*level]->len;
3676
3677 ret = btrfs_free_extent(trans, root, bytenr, blocksize,
3678 parent->start, btrfs_header_owner(parent),
3679 btrfs_header_generation(parent), *level, 1);
3680 BUG_ON(ret);
3681
3682 if (path->locks[*level]) {
3683 btrfs_tree_unlock(path->nodes[*level]);
3684 path->locks[*level] = 0;
3685 }
3686 free_extent_buffer(path->nodes[*level]);
3687 path->nodes[*level] = NULL;
3688 *level += 1;
3689 cond_resched();
3690 return 0;
3691}
3692
3693/*
Chris Mason9aca1d52007-03-13 11:09:37 -04003694 * helper for dropping snapshots. This walks back up the tree in the path
3695 * to find the first node higher up where we haven't yet gone through
3696 * all the slots
3697 */
Chris Masond3977122009-01-05 21:25:51 -05003698static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05003699 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04003700 struct btrfs_path *path,
3701 int *level, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05003702{
Chris Mason7bb86312007-12-11 09:25:06 -05003703 u64 root_owner;
3704 u64 root_gen;
3705 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05003706 int i;
3707 int slot;
3708 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04003709
Yan Zhengf82d02d2008-10-29 14:49:05 -04003710 for (i = *level; i < max_level && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -05003711 slot = path->slots[i];
Chris Mason5f39d392007-10-15 16:14:19 -04003712 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
3713 struct extent_buffer *node;
3714 struct btrfs_disk_key disk_key;
Chris Masonbd56b302009-02-04 09:27:02 -05003715
3716 /*
3717 * there is more work to do in this level.
3718 * Update the drop_progress marker to reflect
3719 * the work we've done so far, and then bump
3720 * the slot number
3721 */
Chris Mason5f39d392007-10-15 16:14:19 -04003722 node = path->nodes[i];
Chris Mason20524f02007-03-10 06:35:47 -05003723 path->slots[i]++;
3724 *level = i;
Chris Mason9f3a7422007-08-07 15:52:19 -04003725 WARN_ON(*level == 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003726 btrfs_node_key(node, &disk_key, path->slots[i]);
Chris Mason9f3a7422007-08-07 15:52:19 -04003727 memcpy(&root_item->drop_progress,
Chris Mason5f39d392007-10-15 16:14:19 -04003728 &disk_key, sizeof(disk_key));
Chris Mason9f3a7422007-08-07 15:52:19 -04003729 root_item->drop_level = i;
Chris Mason20524f02007-03-10 06:35:47 -05003730 return 0;
3731 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04003732 struct extent_buffer *parent;
Chris Masonbd56b302009-02-04 09:27:02 -05003733
3734 /*
3735 * this whole node is done, free our reference
3736 * on it and go up one level
3737 */
Zheng Yan31840ae2008-09-23 13:14:14 -04003738 if (path->nodes[*level] == root->node)
3739 parent = path->nodes[*level];
3740 else
3741 parent = path->nodes[*level + 1];
3742
3743 root_owner = btrfs_header_owner(parent);
3744 root_gen = btrfs_header_generation(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04003745
3746 clean_tree_block(trans, root, path->nodes[*level]);
Chris Masone089f052007-03-16 16:20:31 -04003747 ret = btrfs_free_extent(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04003748 path->nodes[*level]->start,
Chris Mason7bb86312007-12-11 09:25:06 -05003749 path->nodes[*level]->len,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003750 parent->start, root_owner,
3751 root_gen, *level, 1);
Chris Mason6407bf62007-03-27 06:33:00 -04003752 BUG_ON(ret);
Yan Zhengf82d02d2008-10-29 14:49:05 -04003753 if (path->locks[*level]) {
3754 btrfs_tree_unlock(path->nodes[*level]);
3755 path->locks[*level] = 0;
3756 }
Chris Mason5f39d392007-10-15 16:14:19 -04003757 free_extent_buffer(path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -04003758 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -05003759 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -05003760 }
3761 }
3762 return 1;
3763}
3764
Chris Mason9aca1d52007-03-13 11:09:37 -04003765/*
3766 * drop the reference count on the tree rooted at 'snap'. This traverses
3767 * the tree freeing any blocks that have a ref count of zero after being
3768 * decremented.
3769 */
Chris Masone089f052007-03-16 16:20:31 -04003770int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason9f3a7422007-08-07 15:52:19 -04003771 *root)
Chris Mason20524f02007-03-10 06:35:47 -05003772{
Chris Mason3768f362007-03-13 16:47:54 -04003773 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -04003774 int wret;
Chris Mason20524f02007-03-10 06:35:47 -05003775 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -04003776 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -05003777 int i;
3778 int orig_level;
Chris Masonc3e69d52009-03-13 10:17:05 -04003779 int update_count;
Chris Mason9f3a7422007-08-07 15:52:19 -04003780 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05003781
Chris Masona2135012008-06-25 16:01:30 -04003782 WARN_ON(!mutex_is_locked(&root->fs_info->drop_mutex));
Chris Mason5caf2a02007-04-02 11:20:42 -04003783 path = btrfs_alloc_path();
3784 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05003785
Chris Mason5f39d392007-10-15 16:14:19 -04003786 level = btrfs_header_level(root->node);
Chris Mason20524f02007-03-10 06:35:47 -05003787 orig_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04003788 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
3789 path->nodes[level] = root->node;
Chris Masonf510cfe2007-10-15 16:14:48 -04003790 extent_buffer_get(root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -04003791 path->slots[level] = 0;
3792 } else {
3793 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04003794 struct btrfs_disk_key found_key;
3795 struct extent_buffer *node;
Chris Mason6702ed42007-08-07 16:15:09 -04003796
Chris Mason9f3a7422007-08-07 15:52:19 -04003797 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Chris Mason6702ed42007-08-07 16:15:09 -04003798 level = root_item->drop_level;
3799 path->lowest_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04003800 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason6702ed42007-08-07 16:15:09 -04003801 if (wret < 0) {
Chris Mason9f3a7422007-08-07 15:52:19 -04003802 ret = wret;
3803 goto out;
3804 }
Chris Mason5f39d392007-10-15 16:14:19 -04003805 node = path->nodes[level];
3806 btrfs_node_key(node, &found_key, path->slots[level]);
3807 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
3808 sizeof(found_key)));
Chris Mason7d9eb122008-07-08 14:19:17 -04003809 /*
3810 * unlock our path, this is safe because only this
3811 * function is allowed to delete this snapshot
3812 */
Chris Mason925baed2008-06-25 16:01:30 -04003813 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
3814 if (path->nodes[i] && path->locks[i]) {
3815 path->locks[i] = 0;
3816 btrfs_tree_unlock(path->nodes[i]);
3817 }
3818 }
Chris Mason9f3a7422007-08-07 15:52:19 -04003819 }
Chris Masond3977122009-01-05 21:25:51 -05003820 while (1) {
Chris Masonc3e69d52009-03-13 10:17:05 -04003821 unsigned long update;
Chris Mason5caf2a02007-04-02 11:20:42 -04003822 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04003823 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05003824 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04003825 if (wret < 0)
3826 ret = wret;
3827
Yan Zhengf82d02d2008-10-29 14:49:05 -04003828 wret = walk_up_tree(trans, root, path, &level,
3829 BTRFS_MAX_LEVEL);
Chris Mason9aca1d52007-03-13 11:09:37 -04003830 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05003831 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04003832 if (wret < 0)
3833 ret = wret;
Chris Masonb7ec40d2009-03-12 20:12:45 -04003834 if (trans->transaction->in_commit ||
3835 trans->transaction->delayed_refs.flushing) {
Chris Masone7a84562008-06-25 16:01:31 -04003836 ret = -EAGAIN;
3837 break;
3838 }
Chris Mason18e35e0a2008-08-01 13:11:41 -04003839 atomic_inc(&root->fs_info->throttle_gen);
Chris Mason017e5362008-07-28 15:32:51 -04003840 wake_up(&root->fs_info->transaction_throttle);
Chris Masonc3e69d52009-03-13 10:17:05 -04003841 for (update_count = 0; update_count < 16; update_count++) {
3842 update = trans->delayed_ref_updates;
3843 trans->delayed_ref_updates = 0;
3844 if (update)
3845 btrfs_run_delayed_refs(trans, root, update);
3846 else
3847 break;
3848 }
Chris Mason20524f02007-03-10 06:35:47 -05003849 }
Chris Mason83e15a22007-03-12 09:03:27 -04003850 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -04003851 if (path->nodes[i]) {
Chris Mason5f39d392007-10-15 16:14:19 -04003852 free_extent_buffer(path->nodes[i]);
Chris Mason0f827312007-10-15 16:18:56 -04003853 path->nodes[i] = NULL;
Chris Mason83e15a22007-03-12 09:03:27 -04003854 }
Chris Mason20524f02007-03-10 06:35:47 -05003855 }
Chris Mason9f3a7422007-08-07 15:52:19 -04003856out:
Chris Mason5caf2a02007-04-02 11:20:42 -04003857 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -04003858 return ret;
Chris Mason20524f02007-03-10 06:35:47 -05003859}
Chris Mason9078a3e2007-04-26 16:46:15 -04003860
Yan Zhengf82d02d2008-10-29 14:49:05 -04003861int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
3862 struct btrfs_root *root,
3863 struct extent_buffer *node,
3864 struct extent_buffer *parent)
3865{
3866 struct btrfs_path *path;
3867 int level;
3868 int parent_level;
3869 int ret = 0;
3870 int wret;
3871
3872 path = btrfs_alloc_path();
3873 BUG_ON(!path);
3874
Chris Masonb9447ef2009-03-09 11:45:38 -04003875 btrfs_assert_tree_locked(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04003876 parent_level = btrfs_header_level(parent);
3877 extent_buffer_get(parent);
3878 path->nodes[parent_level] = parent;
3879 path->slots[parent_level] = btrfs_header_nritems(parent);
3880
Chris Masonb9447ef2009-03-09 11:45:38 -04003881 btrfs_assert_tree_locked(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04003882 level = btrfs_header_level(node);
3883 extent_buffer_get(node);
3884 path->nodes[level] = node;
3885 path->slots[level] = 0;
3886
3887 while (1) {
3888 wret = walk_down_subtree(trans, root, path, &level);
3889 if (wret < 0)
3890 ret = wret;
3891 if (wret != 0)
3892 break;
3893
3894 wret = walk_up_tree(trans, root, path, &level, parent_level);
3895 if (wret < 0)
3896 ret = wret;
3897 if (wret != 0)
3898 break;
3899 }
3900
3901 btrfs_free_path(path);
3902 return ret;
3903}
3904
Chris Mason8e7bf942008-04-28 09:02:36 -04003905static unsigned long calc_ra(unsigned long start, unsigned long last,
3906 unsigned long nr)
3907{
3908 return min(last, start + nr - 1);
3909}
3910
Chris Masond3977122009-01-05 21:25:51 -05003911static noinline int relocate_inode_pages(struct inode *inode, u64 start,
Chris Mason98ed5172008-01-03 10:01:48 -05003912 u64 len)
Chris Masonedbd8d42007-12-21 16:27:24 -05003913{
3914 u64 page_start;
3915 u64 page_end;
Zheng Yan1a40e232008-09-26 10:09:34 -04003916 unsigned long first_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05003917 unsigned long last_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05003918 unsigned long i;
3919 struct page *page;
Chris Masond1310b22008-01-24 16:13:08 -05003920 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason4313b392008-01-03 09:08:48 -05003921 struct file_ra_state *ra;
Chris Mason3eaa2882008-07-24 11:57:52 -04003922 struct btrfs_ordered_extent *ordered;
Zheng Yan1a40e232008-09-26 10:09:34 -04003923 unsigned int total_read = 0;
3924 unsigned int total_dirty = 0;
3925 int ret = 0;
Chris Mason4313b392008-01-03 09:08:48 -05003926
3927 ra = kzalloc(sizeof(*ra), GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05003928
3929 mutex_lock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04003930 first_index = start >> PAGE_CACHE_SHIFT;
Chris Masonedbd8d42007-12-21 16:27:24 -05003931 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
3932
Zheng Yan1a40e232008-09-26 10:09:34 -04003933 /* make sure the dirty trick played by the caller work */
3934 ret = invalidate_inode_pages2_range(inode->i_mapping,
3935 first_index, last_index);
3936 if (ret)
3937 goto out_unlock;
Chris Mason8e7bf942008-04-28 09:02:36 -04003938
Chris Mason4313b392008-01-03 09:08:48 -05003939 file_ra_state_init(ra, inode->i_mapping);
Chris Masonedbd8d42007-12-21 16:27:24 -05003940
Zheng Yan1a40e232008-09-26 10:09:34 -04003941 for (i = first_index ; i <= last_index; i++) {
3942 if (total_read % ra->ra_pages == 0) {
Chris Mason8e7bf942008-04-28 09:02:36 -04003943 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
Zheng Yan1a40e232008-09-26 10:09:34 -04003944 calc_ra(i, last_index, ra->ra_pages));
Chris Mason8e7bf942008-04-28 09:02:36 -04003945 }
3946 total_read++;
Chris Mason3eaa2882008-07-24 11:57:52 -04003947again:
3948 if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
Zheng Yan1a40e232008-09-26 10:09:34 -04003949 BUG_ON(1);
Chris Masonedbd8d42007-12-21 16:27:24 -05003950 page = grab_cache_page(inode->i_mapping, i);
Chris Masona061fc82008-05-07 11:43:44 -04003951 if (!page) {
Zheng Yan1a40e232008-09-26 10:09:34 -04003952 ret = -ENOMEM;
Chris Masonedbd8d42007-12-21 16:27:24 -05003953 goto out_unlock;
Chris Masona061fc82008-05-07 11:43:44 -04003954 }
Chris Masonedbd8d42007-12-21 16:27:24 -05003955 if (!PageUptodate(page)) {
3956 btrfs_readpage(NULL, page);
3957 lock_page(page);
3958 if (!PageUptodate(page)) {
3959 unlock_page(page);
3960 page_cache_release(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04003961 ret = -EIO;
Chris Masonedbd8d42007-12-21 16:27:24 -05003962 goto out_unlock;
3963 }
3964 }
Chris Masonec44a352008-04-28 15:29:52 -04003965 wait_on_page_writeback(page);
Chris Mason3eaa2882008-07-24 11:57:52 -04003966
Chris Masonedbd8d42007-12-21 16:27:24 -05003967 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
3968 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05003969 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05003970
Chris Mason3eaa2882008-07-24 11:57:52 -04003971 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3972 if (ordered) {
3973 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3974 unlock_page(page);
3975 page_cache_release(page);
3976 btrfs_start_ordered_extent(inode, ordered, 1);
3977 btrfs_put_ordered_extent(ordered);
3978 goto again;
3979 }
3980 set_page_extent_mapped(page);
3981
Zheng Yan1a40e232008-09-26 10:09:34 -04003982 if (i == first_index)
3983 set_extent_bits(io_tree, page_start, page_end,
3984 EXTENT_BOUNDARY, GFP_NOFS);
Yan Zheng1f80e4d2008-12-19 10:59:04 -05003985 btrfs_set_extent_delalloc(inode, page_start, page_end);
Zheng Yan1a40e232008-09-26 10:09:34 -04003986
Chris Masona061fc82008-05-07 11:43:44 -04003987 set_page_dirty(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04003988 total_dirty++;
Chris Masonedbd8d42007-12-21 16:27:24 -05003989
Chris Masond1310b22008-01-24 16:13:08 -05003990 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05003991 unlock_page(page);
3992 page_cache_release(page);
3993 }
3994
3995out_unlock:
Chris Masonec44a352008-04-28 15:29:52 -04003996 kfree(ra);
Chris Masonedbd8d42007-12-21 16:27:24 -05003997 mutex_unlock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04003998 balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty);
Chris Masonbf4ef672008-05-08 13:26:18 -04003999 return ret;
4000}
4001
Chris Masond3977122009-01-05 21:25:51 -05004002static noinline int relocate_data_extent(struct inode *reloc_inode,
Zheng Yan1a40e232008-09-26 10:09:34 -04004003 struct btrfs_key *extent_key,
4004 u64 offset)
Chris Masonedbd8d42007-12-21 16:27:24 -05004005{
Zheng Yan1a40e232008-09-26 10:09:34 -04004006 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
4007 struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree;
4008 struct extent_map *em;
Yan Zheng66435582008-10-30 14:19:50 -04004009 u64 start = extent_key->objectid - offset;
4010 u64 end = start + extent_key->offset - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04004011
4012 em = alloc_extent_map(GFP_NOFS);
4013 BUG_ON(!em || IS_ERR(em));
4014
Yan Zheng66435582008-10-30 14:19:50 -04004015 em->start = start;
Zheng Yan1a40e232008-09-26 10:09:34 -04004016 em->len = extent_key->offset;
Chris Masonc8b97812008-10-29 14:49:59 -04004017 em->block_len = extent_key->offset;
Zheng Yan1a40e232008-09-26 10:09:34 -04004018 em->block_start = extent_key->objectid;
4019 em->bdev = root->fs_info->fs_devices->latest_bdev;
4020 set_bit(EXTENT_FLAG_PINNED, &em->flags);
4021
4022 /* setup extent map to cheat btrfs_readpage */
Yan Zheng66435582008-10-30 14:19:50 -04004023 lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04004024 while (1) {
4025 int ret;
4026 spin_lock(&em_tree->lock);
4027 ret = add_extent_mapping(em_tree, em);
4028 spin_unlock(&em_tree->lock);
4029 if (ret != -EEXIST) {
4030 free_extent_map(em);
4031 break;
4032 }
Yan Zheng66435582008-10-30 14:19:50 -04004033 btrfs_drop_extent_cache(reloc_inode, start, end, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04004034 }
Yan Zheng66435582008-10-30 14:19:50 -04004035 unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04004036
Yan Zheng66435582008-10-30 14:19:50 -04004037 return relocate_inode_pages(reloc_inode, start, extent_key->offset);
Zheng Yan1a40e232008-09-26 10:09:34 -04004038}
4039
4040struct btrfs_ref_path {
4041 u64 extent_start;
4042 u64 nodes[BTRFS_MAX_LEVEL];
4043 u64 root_objectid;
4044 u64 root_generation;
4045 u64 owner_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04004046 u32 num_refs;
4047 int lowest_level;
4048 int current_level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004049 int shared_level;
4050
4051 struct btrfs_key node_keys[BTRFS_MAX_LEVEL];
4052 u64 new_nodes[BTRFS_MAX_LEVEL];
Zheng Yan1a40e232008-09-26 10:09:34 -04004053};
4054
4055struct disk_extent {
Chris Masonc8b97812008-10-29 14:49:59 -04004056 u64 ram_bytes;
Zheng Yan1a40e232008-09-26 10:09:34 -04004057 u64 disk_bytenr;
4058 u64 disk_num_bytes;
4059 u64 offset;
4060 u64 num_bytes;
Chris Masonc8b97812008-10-29 14:49:59 -04004061 u8 compression;
4062 u8 encryption;
4063 u16 other_encoding;
Zheng Yan1a40e232008-09-26 10:09:34 -04004064};
4065
4066static int is_cowonly_root(u64 root_objectid)
4067{
4068 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
4069 root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
4070 root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
4071 root_objectid == BTRFS_DEV_TREE_OBJECTID ||
Yan Zheng0403e472008-12-10 20:32:51 -05004072 root_objectid == BTRFS_TREE_LOG_OBJECTID ||
4073 root_objectid == BTRFS_CSUM_TREE_OBJECTID)
Zheng Yan1a40e232008-09-26 10:09:34 -04004074 return 1;
4075 return 0;
4076}
4077
Chris Masond3977122009-01-05 21:25:51 -05004078static noinline int __next_ref_path(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04004079 struct btrfs_root *extent_root,
4080 struct btrfs_ref_path *ref_path,
4081 int first_time)
4082{
4083 struct extent_buffer *leaf;
4084 struct btrfs_path *path;
Chris Mason4313b392008-01-03 09:08:48 -05004085 struct btrfs_extent_ref *ref;
Chris Masonedbd8d42007-12-21 16:27:24 -05004086 struct btrfs_key key;
4087 struct btrfs_key found_key;
Zheng Yan1a40e232008-09-26 10:09:34 -04004088 u64 bytenr;
Chris Masonedbd8d42007-12-21 16:27:24 -05004089 u32 nritems;
Zheng Yan1a40e232008-09-26 10:09:34 -04004090 int level;
4091 int ret = 1;
Chris Masonedbd8d42007-12-21 16:27:24 -05004092
Zheng Yan1a40e232008-09-26 10:09:34 -04004093 path = btrfs_alloc_path();
4094 if (!path)
4095 return -ENOMEM;
4096
Zheng Yan1a40e232008-09-26 10:09:34 -04004097 if (first_time) {
4098 ref_path->lowest_level = -1;
4099 ref_path->current_level = -1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004100 ref_path->shared_level = -1;
Zheng Yan1a40e232008-09-26 10:09:34 -04004101 goto walk_up;
Chris Masona061fc82008-05-07 11:43:44 -04004102 }
Zheng Yan1a40e232008-09-26 10:09:34 -04004103walk_down:
4104 level = ref_path->current_level - 1;
4105 while (level >= -1) {
4106 u64 parent;
4107 if (level < ref_path->lowest_level)
4108 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05004109
Chris Masond3977122009-01-05 21:25:51 -05004110 if (level >= 0)
Zheng Yan1a40e232008-09-26 10:09:34 -04004111 bytenr = ref_path->nodes[level];
Chris Masond3977122009-01-05 21:25:51 -05004112 else
Zheng Yan1a40e232008-09-26 10:09:34 -04004113 bytenr = ref_path->extent_start;
Zheng Yan1a40e232008-09-26 10:09:34 -04004114 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05004115
Zheng Yan1a40e232008-09-26 10:09:34 -04004116 parent = ref_path->nodes[level + 1];
4117 ref_path->nodes[level + 1] = 0;
4118 ref_path->current_level = level;
4119 BUG_ON(parent == 0);
4120
4121 key.objectid = bytenr;
4122 key.offset = parent + 1;
4123 key.type = BTRFS_EXTENT_REF_KEY;
4124
4125 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05004126 if (ret < 0)
4127 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04004128 BUG_ON(ret == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05004129
Chris Masonedbd8d42007-12-21 16:27:24 -05004130 leaf = path->nodes[0];
4131 nritems = btrfs_header_nritems(leaf);
Zheng Yan1a40e232008-09-26 10:09:34 -04004132 if (path->slots[0] >= nritems) {
Chris Masona061fc82008-05-07 11:43:44 -04004133 ret = btrfs_next_leaf(extent_root, path);
Chris Masona061fc82008-05-07 11:43:44 -04004134 if (ret < 0)
4135 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04004136 if (ret > 0)
4137 goto next;
Chris Masonbf4ef672008-05-08 13:26:18 -04004138 leaf = path->nodes[0];
Chris Masona061fc82008-05-07 11:43:44 -04004139 }
Chris Masonedbd8d42007-12-21 16:27:24 -05004140
4141 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Zheng Yan1a40e232008-09-26 10:09:34 -04004142 if (found_key.objectid == bytenr &&
Yan Zhengf82d02d2008-10-29 14:49:05 -04004143 found_key.type == BTRFS_EXTENT_REF_KEY) {
4144 if (level < ref_path->shared_level)
4145 ref_path->shared_level = level;
Zheng Yan1a40e232008-09-26 10:09:34 -04004146 goto found;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004147 }
Zheng Yan1a40e232008-09-26 10:09:34 -04004148next:
4149 level--;
4150 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04004151 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04004152 }
4153 /* reached lowest level */
4154 ret = 1;
4155 goto out;
4156walk_up:
4157 level = ref_path->current_level;
4158 while (level < BTRFS_MAX_LEVEL - 1) {
4159 u64 ref_objectid;
Chris Masond3977122009-01-05 21:25:51 -05004160
4161 if (level >= 0)
Zheng Yan1a40e232008-09-26 10:09:34 -04004162 bytenr = ref_path->nodes[level];
Chris Masond3977122009-01-05 21:25:51 -05004163 else
Zheng Yan1a40e232008-09-26 10:09:34 -04004164 bytenr = ref_path->extent_start;
Chris Masond3977122009-01-05 21:25:51 -05004165
Zheng Yan1a40e232008-09-26 10:09:34 -04004166 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05004167
Zheng Yan1a40e232008-09-26 10:09:34 -04004168 key.objectid = bytenr;
4169 key.offset = 0;
4170 key.type = BTRFS_EXTENT_REF_KEY;
Chris Masonedbd8d42007-12-21 16:27:24 -05004171
Zheng Yan1a40e232008-09-26 10:09:34 -04004172 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
4173 if (ret < 0)
Chris Masonedbd8d42007-12-21 16:27:24 -05004174 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04004175
4176 leaf = path->nodes[0];
4177 nritems = btrfs_header_nritems(leaf);
4178 if (path->slots[0] >= nritems) {
4179 ret = btrfs_next_leaf(extent_root, path);
4180 if (ret < 0)
4181 goto out;
4182 if (ret > 0) {
4183 /* the extent was freed by someone */
4184 if (ref_path->lowest_level == level)
4185 goto out;
4186 btrfs_release_path(extent_root, path);
4187 goto walk_down;
4188 }
4189 leaf = path->nodes[0];
4190 }
4191
4192 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4193 if (found_key.objectid != bytenr ||
4194 found_key.type != BTRFS_EXTENT_REF_KEY) {
4195 /* the extent was freed by someone */
4196 if (ref_path->lowest_level == level) {
4197 ret = 1;
4198 goto out;
4199 }
4200 btrfs_release_path(extent_root, path);
4201 goto walk_down;
4202 }
4203found:
4204 ref = btrfs_item_ptr(leaf, path->slots[0],
4205 struct btrfs_extent_ref);
4206 ref_objectid = btrfs_ref_objectid(leaf, ref);
4207 if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) {
4208 if (first_time) {
4209 level = (int)ref_objectid;
4210 BUG_ON(level >= BTRFS_MAX_LEVEL);
4211 ref_path->lowest_level = level;
4212 ref_path->current_level = level;
4213 ref_path->nodes[level] = bytenr;
4214 } else {
4215 WARN_ON(ref_objectid != level);
4216 }
4217 } else {
4218 WARN_ON(level != -1);
4219 }
4220 first_time = 0;
4221
4222 if (ref_path->lowest_level == level) {
4223 ref_path->owner_objectid = ref_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04004224 ref_path->num_refs = btrfs_ref_num_refs(leaf, ref);
4225 }
4226
4227 /*
4228 * the block is tree root or the block isn't in reference
4229 * counted tree.
4230 */
4231 if (found_key.objectid == found_key.offset ||
4232 is_cowonly_root(btrfs_ref_root(leaf, ref))) {
4233 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
4234 ref_path->root_generation =
4235 btrfs_ref_generation(leaf, ref);
4236 if (level < 0) {
4237 /* special reference from the tree log */
4238 ref_path->nodes[0] = found_key.offset;
4239 ref_path->current_level = 0;
4240 }
4241 ret = 0;
4242 goto out;
4243 }
4244
4245 level++;
4246 BUG_ON(ref_path->nodes[level] != 0);
4247 ref_path->nodes[level] = found_key.offset;
4248 ref_path->current_level = level;
4249
4250 /*
4251 * the reference was created in the running transaction,
4252 * no need to continue walking up.
4253 */
4254 if (btrfs_ref_generation(leaf, ref) == trans->transid) {
4255 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
4256 ref_path->root_generation =
4257 btrfs_ref_generation(leaf, ref);
4258 ret = 0;
4259 goto out;
4260 }
4261
4262 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04004263 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04004264 }
4265 /* reached max tree level, but no tree root found. */
4266 BUG();
4267out:
Zheng Yan1a40e232008-09-26 10:09:34 -04004268 btrfs_free_path(path);
4269 return ret;
4270}
4271
4272static int btrfs_first_ref_path(struct btrfs_trans_handle *trans,
4273 struct btrfs_root *extent_root,
4274 struct btrfs_ref_path *ref_path,
4275 u64 extent_start)
4276{
4277 memset(ref_path, 0, sizeof(*ref_path));
4278 ref_path->extent_start = extent_start;
4279
4280 return __next_ref_path(trans, extent_root, ref_path, 1);
4281}
4282
4283static int btrfs_next_ref_path(struct btrfs_trans_handle *trans,
4284 struct btrfs_root *extent_root,
4285 struct btrfs_ref_path *ref_path)
4286{
4287 return __next_ref_path(trans, extent_root, ref_path, 0);
4288}
4289
Chris Masond3977122009-01-05 21:25:51 -05004290static noinline int get_new_locations(struct inode *reloc_inode,
Zheng Yan1a40e232008-09-26 10:09:34 -04004291 struct btrfs_key *extent_key,
4292 u64 offset, int no_fragment,
4293 struct disk_extent **extents,
4294 int *nr_extents)
4295{
4296 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
4297 struct btrfs_path *path;
4298 struct btrfs_file_extent_item *fi;
4299 struct extent_buffer *leaf;
4300 struct disk_extent *exts = *extents;
4301 struct btrfs_key found_key;
4302 u64 cur_pos;
4303 u64 last_byte;
4304 u32 nritems;
4305 int nr = 0;
4306 int max = *nr_extents;
4307 int ret;
4308
4309 WARN_ON(!no_fragment && *extents);
4310 if (!exts) {
4311 max = 1;
4312 exts = kmalloc(sizeof(*exts) * max, GFP_NOFS);
4313 if (!exts)
4314 return -ENOMEM;
4315 }
4316
4317 path = btrfs_alloc_path();
4318 BUG_ON(!path);
4319
4320 cur_pos = extent_key->objectid - offset;
4321 last_byte = extent_key->objectid + extent_key->offset;
4322 ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino,
4323 cur_pos, 0);
4324 if (ret < 0)
4325 goto out;
4326 if (ret > 0) {
4327 ret = -ENOENT;
4328 goto out;
4329 }
4330
4331 while (1) {
4332 leaf = path->nodes[0];
4333 nritems = btrfs_header_nritems(leaf);
4334 if (path->slots[0] >= nritems) {
4335 ret = btrfs_next_leaf(root, path);
4336 if (ret < 0)
4337 goto out;
4338 if (ret > 0)
4339 break;
4340 leaf = path->nodes[0];
4341 }
4342
4343 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4344 if (found_key.offset != cur_pos ||
4345 found_key.type != BTRFS_EXTENT_DATA_KEY ||
4346 found_key.objectid != reloc_inode->i_ino)
4347 break;
4348
4349 fi = btrfs_item_ptr(leaf, path->slots[0],
4350 struct btrfs_file_extent_item);
4351 if (btrfs_file_extent_type(leaf, fi) !=
4352 BTRFS_FILE_EXTENT_REG ||
4353 btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
4354 break;
4355
4356 if (nr == max) {
4357 struct disk_extent *old = exts;
4358 max *= 2;
4359 exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
4360 memcpy(exts, old, sizeof(*exts) * nr);
4361 if (old != *extents)
4362 kfree(old);
4363 }
4364
4365 exts[nr].disk_bytenr =
4366 btrfs_file_extent_disk_bytenr(leaf, fi);
4367 exts[nr].disk_num_bytes =
4368 btrfs_file_extent_disk_num_bytes(leaf, fi);
4369 exts[nr].offset = btrfs_file_extent_offset(leaf, fi);
4370 exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
Chris Masonc8b97812008-10-29 14:49:59 -04004371 exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
4372 exts[nr].compression = btrfs_file_extent_compression(leaf, fi);
4373 exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi);
4374 exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf,
4375 fi);
Yan Zhengd899e052008-10-30 14:25:28 -04004376 BUG_ON(exts[nr].offset > 0);
4377 BUG_ON(exts[nr].compression || exts[nr].encryption);
4378 BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04004379
4380 cur_pos += exts[nr].num_bytes;
4381 nr++;
4382
4383 if (cur_pos + offset >= last_byte)
4384 break;
4385
4386 if (no_fragment) {
4387 ret = 1;
4388 goto out;
4389 }
4390 path->slots[0]++;
4391 }
4392
Yan Zheng1f80e4d2008-12-19 10:59:04 -05004393 BUG_ON(cur_pos + offset > last_byte);
Zheng Yan1a40e232008-09-26 10:09:34 -04004394 if (cur_pos + offset < last_byte) {
4395 ret = -ENOENT;
4396 goto out;
Chris Masonedbd8d42007-12-21 16:27:24 -05004397 }
4398 ret = 0;
4399out:
Zheng Yan1a40e232008-09-26 10:09:34 -04004400 btrfs_free_path(path);
4401 if (ret) {
4402 if (exts != *extents)
4403 kfree(exts);
4404 } else {
4405 *extents = exts;
4406 *nr_extents = nr;
4407 }
4408 return ret;
4409}
4410
Chris Masond3977122009-01-05 21:25:51 -05004411static noinline int replace_one_extent(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04004412 struct btrfs_root *root,
4413 struct btrfs_path *path,
4414 struct btrfs_key *extent_key,
4415 struct btrfs_key *leaf_key,
4416 struct btrfs_ref_path *ref_path,
4417 struct disk_extent *new_extents,
4418 int nr_extents)
4419{
4420 struct extent_buffer *leaf;
4421 struct btrfs_file_extent_item *fi;
4422 struct inode *inode = NULL;
4423 struct btrfs_key key;
4424 u64 lock_start = 0;
4425 u64 lock_end = 0;
4426 u64 num_bytes;
4427 u64 ext_offset;
Yan Zheng86288a12009-01-21 10:49:16 -05004428 u64 search_end = (u64)-1;
Zheng Yan1a40e232008-09-26 10:09:34 -04004429 u32 nritems;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004430 int nr_scaned = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04004431 int extent_locked = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04004432 int extent_type;
Zheng Yan1a40e232008-09-26 10:09:34 -04004433 int ret;
4434
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004435 memcpy(&key, leaf_key, sizeof(key));
Zheng Yan1a40e232008-09-26 10:09:34 -04004436 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004437 if (key.objectid < ref_path->owner_objectid ||
4438 (key.objectid == ref_path->owner_objectid &&
4439 key.type < BTRFS_EXTENT_DATA_KEY)) {
4440 key.objectid = ref_path->owner_objectid;
4441 key.type = BTRFS_EXTENT_DATA_KEY;
4442 key.offset = 0;
4443 }
Zheng Yan1a40e232008-09-26 10:09:34 -04004444 }
4445
4446 while (1) {
4447 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
4448 if (ret < 0)
4449 goto out;
4450
4451 leaf = path->nodes[0];
4452 nritems = btrfs_header_nritems(leaf);
4453next:
4454 if (extent_locked && ret > 0) {
4455 /*
4456 * the file extent item was modified by someone
4457 * before the extent got locked.
4458 */
Zheng Yan1a40e232008-09-26 10:09:34 -04004459 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4460 lock_end, GFP_NOFS);
4461 extent_locked = 0;
4462 }
4463
4464 if (path->slots[0] >= nritems) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004465 if (++nr_scaned > 2)
Zheng Yan1a40e232008-09-26 10:09:34 -04004466 break;
4467
4468 BUG_ON(extent_locked);
4469 ret = btrfs_next_leaf(root, path);
4470 if (ret < 0)
4471 goto out;
4472 if (ret > 0)
4473 break;
4474 leaf = path->nodes[0];
4475 nritems = btrfs_header_nritems(leaf);
4476 }
4477
4478 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4479
4480 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
4481 if ((key.objectid > ref_path->owner_objectid) ||
4482 (key.objectid == ref_path->owner_objectid &&
4483 key.type > BTRFS_EXTENT_DATA_KEY) ||
Yan Zheng86288a12009-01-21 10:49:16 -05004484 key.offset >= search_end)
Zheng Yan1a40e232008-09-26 10:09:34 -04004485 break;
4486 }
4487
4488 if (inode && key.objectid != inode->i_ino) {
4489 BUG_ON(extent_locked);
4490 btrfs_release_path(root, path);
4491 mutex_unlock(&inode->i_mutex);
4492 iput(inode);
4493 inode = NULL;
4494 continue;
4495 }
4496
4497 if (key.type != BTRFS_EXTENT_DATA_KEY) {
4498 path->slots[0]++;
4499 ret = 1;
4500 goto next;
4501 }
4502 fi = btrfs_item_ptr(leaf, path->slots[0],
4503 struct btrfs_file_extent_item);
Yan Zhengd899e052008-10-30 14:25:28 -04004504 extent_type = btrfs_file_extent_type(leaf, fi);
4505 if ((extent_type != BTRFS_FILE_EXTENT_REG &&
4506 extent_type != BTRFS_FILE_EXTENT_PREALLOC) ||
Zheng Yan1a40e232008-09-26 10:09:34 -04004507 (btrfs_file_extent_disk_bytenr(leaf, fi) !=
4508 extent_key->objectid)) {
4509 path->slots[0]++;
4510 ret = 1;
4511 goto next;
4512 }
4513
4514 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
4515 ext_offset = btrfs_file_extent_offset(leaf, fi);
4516
Yan Zheng86288a12009-01-21 10:49:16 -05004517 if (search_end == (u64)-1) {
4518 search_end = key.offset - ext_offset +
4519 btrfs_file_extent_ram_bytes(leaf, fi);
4520 }
Zheng Yan1a40e232008-09-26 10:09:34 -04004521
4522 if (!extent_locked) {
4523 lock_start = key.offset;
4524 lock_end = lock_start + num_bytes - 1;
4525 } else {
Yan Zheng66435582008-10-30 14:19:50 -04004526 if (lock_start > key.offset ||
4527 lock_end + 1 < key.offset + num_bytes) {
4528 unlock_extent(&BTRFS_I(inode)->io_tree,
4529 lock_start, lock_end, GFP_NOFS);
4530 extent_locked = 0;
4531 }
Zheng Yan1a40e232008-09-26 10:09:34 -04004532 }
4533
4534 if (!inode) {
4535 btrfs_release_path(root, path);
4536
4537 inode = btrfs_iget_locked(root->fs_info->sb,
4538 key.objectid, root);
4539 if (inode->i_state & I_NEW) {
4540 BTRFS_I(inode)->root = root;
4541 BTRFS_I(inode)->location.objectid =
4542 key.objectid;
4543 BTRFS_I(inode)->location.type =
4544 BTRFS_INODE_ITEM_KEY;
4545 BTRFS_I(inode)->location.offset = 0;
4546 btrfs_read_locked_inode(inode);
4547 unlock_new_inode(inode);
4548 }
4549 /*
4550 * some code call btrfs_commit_transaction while
4551 * holding the i_mutex, so we can't use mutex_lock
4552 * here.
4553 */
4554 if (is_bad_inode(inode) ||
4555 !mutex_trylock(&inode->i_mutex)) {
4556 iput(inode);
4557 inode = NULL;
4558 key.offset = (u64)-1;
4559 goto skip;
4560 }
4561 }
4562
4563 if (!extent_locked) {
4564 struct btrfs_ordered_extent *ordered;
4565
4566 btrfs_release_path(root, path);
4567
4568 lock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4569 lock_end, GFP_NOFS);
4570 ordered = btrfs_lookup_first_ordered_extent(inode,
4571 lock_end);
4572 if (ordered &&
4573 ordered->file_offset <= lock_end &&
4574 ordered->file_offset + ordered->len > lock_start) {
4575 unlock_extent(&BTRFS_I(inode)->io_tree,
4576 lock_start, lock_end, GFP_NOFS);
4577 btrfs_start_ordered_extent(inode, ordered, 1);
4578 btrfs_put_ordered_extent(ordered);
4579 key.offset += num_bytes;
4580 goto skip;
4581 }
4582 if (ordered)
4583 btrfs_put_ordered_extent(ordered);
4584
Zheng Yan1a40e232008-09-26 10:09:34 -04004585 extent_locked = 1;
4586 continue;
4587 }
4588
4589 if (nr_extents == 1) {
4590 /* update extent pointer in place */
Zheng Yan1a40e232008-09-26 10:09:34 -04004591 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4592 new_extents[0].disk_bytenr);
4593 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4594 new_extents[0].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04004595 btrfs_mark_buffer_dirty(leaf);
4596
4597 btrfs_drop_extent_cache(inode, key.offset,
4598 key.offset + num_bytes - 1, 0);
4599
4600 ret = btrfs_inc_extent_ref(trans, root,
4601 new_extents[0].disk_bytenr,
4602 new_extents[0].disk_num_bytes,
4603 leaf->start,
4604 root->root_key.objectid,
4605 trans->transid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004606 key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04004607 BUG_ON(ret);
4608
4609 ret = btrfs_free_extent(trans, root,
4610 extent_key->objectid,
4611 extent_key->offset,
4612 leaf->start,
4613 btrfs_header_owner(leaf),
4614 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004615 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04004616 BUG_ON(ret);
4617
4618 btrfs_release_path(root, path);
4619 key.offset += num_bytes;
4620 } else {
Yan Zhengd899e052008-10-30 14:25:28 -04004621 BUG_ON(1);
4622#if 0
Zheng Yan1a40e232008-09-26 10:09:34 -04004623 u64 alloc_hint;
4624 u64 extent_len;
4625 int i;
4626 /*
4627 * drop old extent pointer at first, then insert the
4628 * new pointers one bye one
4629 */
4630 btrfs_release_path(root, path);
4631 ret = btrfs_drop_extents(trans, root, inode, key.offset,
4632 key.offset + num_bytes,
4633 key.offset, &alloc_hint);
4634 BUG_ON(ret);
4635
4636 for (i = 0; i < nr_extents; i++) {
4637 if (ext_offset >= new_extents[i].num_bytes) {
4638 ext_offset -= new_extents[i].num_bytes;
4639 continue;
4640 }
4641 extent_len = min(new_extents[i].num_bytes -
4642 ext_offset, num_bytes);
4643
4644 ret = btrfs_insert_empty_item(trans, root,
4645 path, &key,
4646 sizeof(*fi));
4647 BUG_ON(ret);
4648
4649 leaf = path->nodes[0];
4650 fi = btrfs_item_ptr(leaf, path->slots[0],
4651 struct btrfs_file_extent_item);
4652 btrfs_set_file_extent_generation(leaf, fi,
4653 trans->transid);
4654 btrfs_set_file_extent_type(leaf, fi,
4655 BTRFS_FILE_EXTENT_REG);
4656 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4657 new_extents[i].disk_bytenr);
4658 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4659 new_extents[i].disk_num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -04004660 btrfs_set_file_extent_ram_bytes(leaf, fi,
4661 new_extents[i].ram_bytes);
4662
4663 btrfs_set_file_extent_compression(leaf, fi,
4664 new_extents[i].compression);
4665 btrfs_set_file_extent_encryption(leaf, fi,
4666 new_extents[i].encryption);
4667 btrfs_set_file_extent_other_encoding(leaf, fi,
4668 new_extents[i].other_encoding);
4669
Zheng Yan1a40e232008-09-26 10:09:34 -04004670 btrfs_set_file_extent_num_bytes(leaf, fi,
4671 extent_len);
4672 ext_offset += new_extents[i].offset;
4673 btrfs_set_file_extent_offset(leaf, fi,
4674 ext_offset);
4675 btrfs_mark_buffer_dirty(leaf);
4676
4677 btrfs_drop_extent_cache(inode, key.offset,
4678 key.offset + extent_len - 1, 0);
4679
4680 ret = btrfs_inc_extent_ref(trans, root,
4681 new_extents[i].disk_bytenr,
4682 new_extents[i].disk_num_bytes,
4683 leaf->start,
4684 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004685 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04004686 BUG_ON(ret);
4687 btrfs_release_path(root, path);
4688
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004689 inode_add_bytes(inode, extent_len);
Zheng Yan1a40e232008-09-26 10:09:34 -04004690
4691 ext_offset = 0;
4692 num_bytes -= extent_len;
4693 key.offset += extent_len;
4694
4695 if (num_bytes == 0)
4696 break;
4697 }
4698 BUG_ON(i >= nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04004699#endif
Zheng Yan1a40e232008-09-26 10:09:34 -04004700 }
4701
4702 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04004703 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4704 lock_end, GFP_NOFS);
4705 extent_locked = 0;
4706 }
4707skip:
4708 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS &&
Yan Zheng86288a12009-01-21 10:49:16 -05004709 key.offset >= search_end)
Zheng Yan1a40e232008-09-26 10:09:34 -04004710 break;
4711
4712 cond_resched();
4713 }
4714 ret = 0;
4715out:
4716 btrfs_release_path(root, path);
4717 if (inode) {
4718 mutex_unlock(&inode->i_mutex);
4719 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04004720 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4721 lock_end, GFP_NOFS);
4722 }
4723 iput(inode);
4724 }
4725 return ret;
4726}
4727
Zheng Yan1a40e232008-09-26 10:09:34 -04004728int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans,
4729 struct btrfs_root *root,
4730 struct extent_buffer *buf, u64 orig_start)
4731{
4732 int level;
4733 int ret;
4734
4735 BUG_ON(btrfs_header_generation(buf) != trans->transid);
4736 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
4737
4738 level = btrfs_header_level(buf);
4739 if (level == 0) {
4740 struct btrfs_leaf_ref *ref;
4741 struct btrfs_leaf_ref *orig_ref;
4742
4743 orig_ref = btrfs_lookup_leaf_ref(root, orig_start);
4744 if (!orig_ref)
4745 return -ENOENT;
4746
4747 ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems);
4748 if (!ref) {
4749 btrfs_free_leaf_ref(root, orig_ref);
4750 return -ENOMEM;
4751 }
4752
4753 ref->nritems = orig_ref->nritems;
4754 memcpy(ref->extents, orig_ref->extents,
4755 sizeof(ref->extents[0]) * ref->nritems);
4756
4757 btrfs_free_leaf_ref(root, orig_ref);
4758
4759 ref->root_gen = trans->transid;
4760 ref->bytenr = buf->start;
4761 ref->owner = btrfs_header_owner(buf);
4762 ref->generation = btrfs_header_generation(buf);
Chris Masonbd56b302009-02-04 09:27:02 -05004763
Zheng Yan1a40e232008-09-26 10:09:34 -04004764 ret = btrfs_add_leaf_ref(root, ref, 0);
4765 WARN_ON(ret);
4766 btrfs_free_leaf_ref(root, ref);
4767 }
4768 return 0;
4769}
4770
Chris Masond3977122009-01-05 21:25:51 -05004771static noinline int invalidate_extent_cache(struct btrfs_root *root,
Zheng Yan1a40e232008-09-26 10:09:34 -04004772 struct extent_buffer *leaf,
4773 struct btrfs_block_group_cache *group,
4774 struct btrfs_root *target_root)
4775{
4776 struct btrfs_key key;
4777 struct inode *inode = NULL;
4778 struct btrfs_file_extent_item *fi;
4779 u64 num_bytes;
4780 u64 skip_objectid = 0;
4781 u32 nritems;
4782 u32 i;
4783
4784 nritems = btrfs_header_nritems(leaf);
4785 for (i = 0; i < nritems; i++) {
4786 btrfs_item_key_to_cpu(leaf, &key, i);
4787 if (key.objectid == skip_objectid ||
4788 key.type != BTRFS_EXTENT_DATA_KEY)
4789 continue;
4790 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
4791 if (btrfs_file_extent_type(leaf, fi) ==
4792 BTRFS_FILE_EXTENT_INLINE)
4793 continue;
4794 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
4795 continue;
4796 if (!inode || inode->i_ino != key.objectid) {
4797 iput(inode);
4798 inode = btrfs_ilookup(target_root->fs_info->sb,
4799 key.objectid, target_root, 1);
4800 }
4801 if (!inode) {
4802 skip_objectid = key.objectid;
4803 continue;
4804 }
4805 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
4806
4807 lock_extent(&BTRFS_I(inode)->io_tree, key.offset,
4808 key.offset + num_bytes - 1, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04004809 btrfs_drop_extent_cache(inode, key.offset,
4810 key.offset + num_bytes - 1, 1);
Zheng Yan1a40e232008-09-26 10:09:34 -04004811 unlock_extent(&BTRFS_I(inode)->io_tree, key.offset,
4812 key.offset + num_bytes - 1, GFP_NOFS);
4813 cond_resched();
4814 }
4815 iput(inode);
4816 return 0;
4817}
4818
Chris Masond3977122009-01-05 21:25:51 -05004819static noinline int replace_extents_in_leaf(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04004820 struct btrfs_root *root,
4821 struct extent_buffer *leaf,
4822 struct btrfs_block_group_cache *group,
4823 struct inode *reloc_inode)
4824{
4825 struct btrfs_key key;
4826 struct btrfs_key extent_key;
4827 struct btrfs_file_extent_item *fi;
4828 struct btrfs_leaf_ref *ref;
4829 struct disk_extent *new_extent;
4830 u64 bytenr;
4831 u64 num_bytes;
4832 u32 nritems;
4833 u32 i;
4834 int ext_index;
4835 int nr_extent;
4836 int ret;
4837
4838 new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
4839 BUG_ON(!new_extent);
4840
4841 ref = btrfs_lookup_leaf_ref(root, leaf->start);
4842 BUG_ON(!ref);
4843
4844 ext_index = -1;
4845 nritems = btrfs_header_nritems(leaf);
4846 for (i = 0; i < nritems; i++) {
4847 btrfs_item_key_to_cpu(leaf, &key, i);
4848 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
4849 continue;
4850 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
4851 if (btrfs_file_extent_type(leaf, fi) ==
4852 BTRFS_FILE_EXTENT_INLINE)
4853 continue;
4854 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
4855 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
4856 if (bytenr == 0)
4857 continue;
4858
4859 ext_index++;
4860 if (bytenr >= group->key.objectid + group->key.offset ||
4861 bytenr + num_bytes <= group->key.objectid)
4862 continue;
4863
4864 extent_key.objectid = bytenr;
4865 extent_key.offset = num_bytes;
4866 extent_key.type = BTRFS_EXTENT_ITEM_KEY;
4867 nr_extent = 1;
4868 ret = get_new_locations(reloc_inode, &extent_key,
4869 group->key.objectid, 1,
4870 &new_extent, &nr_extent);
4871 if (ret > 0)
4872 continue;
4873 BUG_ON(ret < 0);
4874
4875 BUG_ON(ref->extents[ext_index].bytenr != bytenr);
4876 BUG_ON(ref->extents[ext_index].num_bytes != num_bytes);
4877 ref->extents[ext_index].bytenr = new_extent->disk_bytenr;
4878 ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes;
4879
Zheng Yan1a40e232008-09-26 10:09:34 -04004880 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4881 new_extent->disk_bytenr);
4882 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4883 new_extent->disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04004884 btrfs_mark_buffer_dirty(leaf);
4885
4886 ret = btrfs_inc_extent_ref(trans, root,
4887 new_extent->disk_bytenr,
4888 new_extent->disk_num_bytes,
4889 leaf->start,
4890 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004891 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04004892 BUG_ON(ret);
Chris Mason56bec292009-03-13 10:10:06 -04004893
Zheng Yan1a40e232008-09-26 10:09:34 -04004894 ret = btrfs_free_extent(trans, root,
4895 bytenr, num_bytes, leaf->start,
4896 btrfs_header_owner(leaf),
4897 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004898 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04004899 BUG_ON(ret);
4900 cond_resched();
4901 }
4902 kfree(new_extent);
4903 BUG_ON(ext_index + 1 != ref->nritems);
4904 btrfs_free_leaf_ref(root, ref);
4905 return 0;
4906}
4907
Yan Zhengf82d02d2008-10-29 14:49:05 -04004908int btrfs_free_reloc_root(struct btrfs_trans_handle *trans,
4909 struct btrfs_root *root)
Zheng Yan1a40e232008-09-26 10:09:34 -04004910{
4911 struct btrfs_root *reloc_root;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004912 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04004913
4914 if (root->reloc_root) {
4915 reloc_root = root->reloc_root;
4916 root->reloc_root = NULL;
4917 list_add(&reloc_root->dead_list,
4918 &root->fs_info->dead_reloc_roots);
Yan Zhengf82d02d2008-10-29 14:49:05 -04004919
4920 btrfs_set_root_bytenr(&reloc_root->root_item,
4921 reloc_root->node->start);
4922 btrfs_set_root_level(&root->root_item,
4923 btrfs_header_level(reloc_root->node));
4924 memset(&reloc_root->root_item.drop_progress, 0,
4925 sizeof(struct btrfs_disk_key));
4926 reloc_root->root_item.drop_level = 0;
4927
4928 ret = btrfs_update_root(trans, root->fs_info->tree_root,
4929 &reloc_root->root_key,
4930 &reloc_root->root_item);
4931 BUG_ON(ret);
Zheng Yan1a40e232008-09-26 10:09:34 -04004932 }
4933 return 0;
4934}
4935
4936int btrfs_drop_dead_reloc_roots(struct btrfs_root *root)
4937{
4938 struct btrfs_trans_handle *trans;
4939 struct btrfs_root *reloc_root;
4940 struct btrfs_root *prev_root = NULL;
4941 struct list_head dead_roots;
4942 int ret;
4943 unsigned long nr;
4944
4945 INIT_LIST_HEAD(&dead_roots);
4946 list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots);
4947
4948 while (!list_empty(&dead_roots)) {
4949 reloc_root = list_entry(dead_roots.prev,
4950 struct btrfs_root, dead_list);
4951 list_del_init(&reloc_root->dead_list);
4952
4953 BUG_ON(reloc_root->commit_root != NULL);
4954 while (1) {
4955 trans = btrfs_join_transaction(root, 1);
4956 BUG_ON(!trans);
4957
4958 mutex_lock(&root->fs_info->drop_mutex);
4959 ret = btrfs_drop_snapshot(trans, reloc_root);
4960 if (ret != -EAGAIN)
4961 break;
4962 mutex_unlock(&root->fs_info->drop_mutex);
4963
4964 nr = trans->blocks_used;
4965 ret = btrfs_end_transaction(trans, root);
4966 BUG_ON(ret);
4967 btrfs_btree_balance_dirty(root, nr);
4968 }
4969
4970 free_extent_buffer(reloc_root->node);
4971
4972 ret = btrfs_del_root(trans, root->fs_info->tree_root,
4973 &reloc_root->root_key);
4974 BUG_ON(ret);
4975 mutex_unlock(&root->fs_info->drop_mutex);
4976
4977 nr = trans->blocks_used;
4978 ret = btrfs_end_transaction(trans, root);
4979 BUG_ON(ret);
4980 btrfs_btree_balance_dirty(root, nr);
4981
4982 kfree(prev_root);
4983 prev_root = reloc_root;
4984 }
4985 if (prev_root) {
4986 btrfs_remove_leaf_refs(prev_root, (u64)-1, 0);
4987 kfree(prev_root);
4988 }
4989 return 0;
4990}
4991
4992int btrfs_add_dead_reloc_root(struct btrfs_root *root)
4993{
4994 list_add(&root->dead_list, &root->fs_info->dead_reloc_roots);
4995 return 0;
4996}
4997
4998int btrfs_cleanup_reloc_trees(struct btrfs_root *root)
4999{
5000 struct btrfs_root *reloc_root;
5001 struct btrfs_trans_handle *trans;
5002 struct btrfs_key location;
5003 int found;
5004 int ret;
5005
5006 mutex_lock(&root->fs_info->tree_reloc_mutex);
5007 ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL);
5008 BUG_ON(ret);
5009 found = !list_empty(&root->fs_info->dead_reloc_roots);
5010 mutex_unlock(&root->fs_info->tree_reloc_mutex);
5011
5012 if (found) {
5013 trans = btrfs_start_transaction(root, 1);
5014 BUG_ON(!trans);
5015 ret = btrfs_commit_transaction(trans, root);
5016 BUG_ON(ret);
5017 }
5018
5019 location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
5020 location.offset = (u64)-1;
5021 location.type = BTRFS_ROOT_ITEM_KEY;
5022
5023 reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
5024 BUG_ON(!reloc_root);
5025 btrfs_orphan_cleanup(reloc_root);
5026 return 0;
5027}
5028
Chris Masond3977122009-01-05 21:25:51 -05005029static noinline int init_reloc_tree(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04005030 struct btrfs_root *root)
5031{
5032 struct btrfs_root *reloc_root;
5033 struct extent_buffer *eb;
5034 struct btrfs_root_item *root_item;
5035 struct btrfs_key root_key;
5036 int ret;
5037
5038 BUG_ON(!root->ref_cows);
5039 if (root->reloc_root)
5040 return 0;
5041
5042 root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
5043 BUG_ON(!root_item);
5044
5045 ret = btrfs_copy_root(trans, root, root->commit_root,
5046 &eb, BTRFS_TREE_RELOC_OBJECTID);
5047 BUG_ON(ret);
5048
5049 root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
5050 root_key.offset = root->root_key.objectid;
5051 root_key.type = BTRFS_ROOT_ITEM_KEY;
5052
5053 memcpy(root_item, &root->root_item, sizeof(root_item));
5054 btrfs_set_root_refs(root_item, 0);
5055 btrfs_set_root_bytenr(root_item, eb->start);
5056 btrfs_set_root_level(root_item, btrfs_header_level(eb));
Yan Zheng84234f32008-10-29 14:49:05 -04005057 btrfs_set_root_generation(root_item, trans->transid);
Zheng Yan1a40e232008-09-26 10:09:34 -04005058
5059 btrfs_tree_unlock(eb);
5060 free_extent_buffer(eb);
5061
5062 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
5063 &root_key, root_item);
5064 BUG_ON(ret);
5065 kfree(root_item);
5066
5067 reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
5068 &root_key);
5069 BUG_ON(!reloc_root);
5070 reloc_root->last_trans = trans->transid;
5071 reloc_root->commit_root = NULL;
5072 reloc_root->ref_tree = &root->fs_info->reloc_ref_tree;
5073
5074 root->reloc_root = reloc_root;
5075 return 0;
5076}
5077
5078/*
5079 * Core function of space balance.
5080 *
5081 * The idea is using reloc trees to relocate tree blocks in reference
Yan Zhengf82d02d2008-10-29 14:49:05 -04005082 * counted roots. There is one reloc tree for each subvol, and all
5083 * reloc trees share same root key objectid. Reloc trees are snapshots
5084 * of the latest committed roots of subvols (root->commit_root).
5085 *
5086 * To relocate a tree block referenced by a subvol, there are two steps.
5087 * COW the block through subvol's reloc tree, then update block pointer
5088 * in the subvol to point to the new block. Since all reloc trees share
5089 * same root key objectid, doing special handing for tree blocks owned
5090 * by them is easy. Once a tree block has been COWed in one reloc tree,
5091 * we can use the resulting new block directly when the same block is
5092 * required to COW again through other reloc trees. By this way, relocated
5093 * tree blocks are shared between reloc trees, so they are also shared
5094 * between subvols.
Zheng Yan1a40e232008-09-26 10:09:34 -04005095 */
Chris Masond3977122009-01-05 21:25:51 -05005096static noinline int relocate_one_path(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04005097 struct btrfs_root *root,
5098 struct btrfs_path *path,
5099 struct btrfs_key *first_key,
5100 struct btrfs_ref_path *ref_path,
5101 struct btrfs_block_group_cache *group,
5102 struct inode *reloc_inode)
5103{
5104 struct btrfs_root *reloc_root;
5105 struct extent_buffer *eb = NULL;
5106 struct btrfs_key *keys;
5107 u64 *nodes;
5108 int level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005109 int shared_level;
Zheng Yan1a40e232008-09-26 10:09:34 -04005110 int lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04005111 int ret;
5112
5113 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
5114 lowest_level = ref_path->owner_objectid;
5115
Yan Zhengf82d02d2008-10-29 14:49:05 -04005116 if (!root->ref_cows) {
Zheng Yan1a40e232008-09-26 10:09:34 -04005117 path->lowest_level = lowest_level;
5118 ret = btrfs_search_slot(trans, root, first_key, path, 0, 1);
5119 BUG_ON(ret < 0);
5120 path->lowest_level = 0;
5121 btrfs_release_path(root, path);
5122 return 0;
5123 }
5124
Zheng Yan1a40e232008-09-26 10:09:34 -04005125 mutex_lock(&root->fs_info->tree_reloc_mutex);
5126 ret = init_reloc_tree(trans, root);
5127 BUG_ON(ret);
5128 reloc_root = root->reloc_root;
5129
Yan Zhengf82d02d2008-10-29 14:49:05 -04005130 shared_level = ref_path->shared_level;
5131 ref_path->shared_level = BTRFS_MAX_LEVEL - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04005132
Yan Zhengf82d02d2008-10-29 14:49:05 -04005133 keys = ref_path->node_keys;
5134 nodes = ref_path->new_nodes;
5135 memset(&keys[shared_level + 1], 0,
5136 sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1));
5137 memset(&nodes[shared_level + 1], 0,
5138 sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1));
Zheng Yan1a40e232008-09-26 10:09:34 -04005139
Yan Zhengf82d02d2008-10-29 14:49:05 -04005140 if (nodes[lowest_level] == 0) {
5141 path->lowest_level = lowest_level;
5142 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
5143 0, 1);
5144 BUG_ON(ret);
5145 for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) {
5146 eb = path->nodes[level];
5147 if (!eb || eb == reloc_root->node)
5148 break;
5149 nodes[level] = eb->start;
5150 if (level == 0)
5151 btrfs_item_key_to_cpu(eb, &keys[level], 0);
5152 else
5153 btrfs_node_key_to_cpu(eb, &keys[level], 0);
5154 }
Yan Zheng2b820322008-11-17 21:11:30 -05005155 if (nodes[0] &&
5156 ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04005157 eb = path->nodes[0];
5158 ret = replace_extents_in_leaf(trans, reloc_root, eb,
5159 group, reloc_inode);
5160 BUG_ON(ret);
5161 }
5162 btrfs_release_path(reloc_root, path);
5163 } else {
Zheng Yan1a40e232008-09-26 10:09:34 -04005164 ret = btrfs_merge_path(trans, reloc_root, keys, nodes,
Yan Zhengf82d02d2008-10-29 14:49:05 -04005165 lowest_level);
Zheng Yan1a40e232008-09-26 10:09:34 -04005166 BUG_ON(ret);
5167 }
5168
Zheng Yan1a40e232008-09-26 10:09:34 -04005169 /*
5170 * replace tree blocks in the fs tree with tree blocks in
5171 * the reloc tree.
5172 */
5173 ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level);
5174 BUG_ON(ret < 0);
5175
5176 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04005177 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
5178 0, 0);
5179 BUG_ON(ret);
5180 extent_buffer_get(path->nodes[0]);
5181 eb = path->nodes[0];
5182 btrfs_release_path(reloc_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005183 ret = invalidate_extent_cache(reloc_root, eb, group, root);
5184 BUG_ON(ret);
5185 free_extent_buffer(eb);
5186 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005187
Yan Zhengf82d02d2008-10-29 14:49:05 -04005188 mutex_unlock(&root->fs_info->tree_reloc_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04005189 path->lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04005190 return 0;
5191}
5192
Chris Masond3977122009-01-05 21:25:51 -05005193static noinline int relocate_tree_block(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04005194 struct btrfs_root *root,
5195 struct btrfs_path *path,
5196 struct btrfs_key *first_key,
5197 struct btrfs_ref_path *ref_path)
5198{
5199 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04005200
5201 ret = relocate_one_path(trans, root, path, first_key,
5202 ref_path, NULL, NULL);
5203 BUG_ON(ret);
5204
Zheng Yan1a40e232008-09-26 10:09:34 -04005205 return 0;
5206}
5207
Chris Masond3977122009-01-05 21:25:51 -05005208static noinline int del_extent_zero(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04005209 struct btrfs_root *extent_root,
5210 struct btrfs_path *path,
5211 struct btrfs_key *extent_key)
5212{
5213 int ret;
5214
Zheng Yan1a40e232008-09-26 10:09:34 -04005215 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
5216 if (ret)
5217 goto out;
5218 ret = btrfs_del_item(trans, extent_root, path);
5219out:
Chris Masonedbd8d42007-12-21 16:27:24 -05005220 btrfs_release_path(extent_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005221 return ret;
5222}
5223
Chris Masond3977122009-01-05 21:25:51 -05005224static noinline struct btrfs_root *read_ref_root(struct btrfs_fs_info *fs_info,
Zheng Yan1a40e232008-09-26 10:09:34 -04005225 struct btrfs_ref_path *ref_path)
5226{
5227 struct btrfs_key root_key;
5228
5229 root_key.objectid = ref_path->root_objectid;
5230 root_key.type = BTRFS_ROOT_ITEM_KEY;
5231 if (is_cowonly_root(ref_path->root_objectid))
5232 root_key.offset = 0;
5233 else
5234 root_key.offset = (u64)-1;
5235
5236 return btrfs_read_fs_root_no_name(fs_info, &root_key);
5237}
5238
Chris Masond3977122009-01-05 21:25:51 -05005239static noinline int relocate_one_extent(struct btrfs_root *extent_root,
Zheng Yan1a40e232008-09-26 10:09:34 -04005240 struct btrfs_path *path,
5241 struct btrfs_key *extent_key,
5242 struct btrfs_block_group_cache *group,
5243 struct inode *reloc_inode, int pass)
5244{
5245 struct btrfs_trans_handle *trans;
5246 struct btrfs_root *found_root;
5247 struct btrfs_ref_path *ref_path = NULL;
5248 struct disk_extent *new_extents = NULL;
5249 int nr_extents = 0;
5250 int loops;
5251 int ret;
5252 int level;
5253 struct btrfs_key first_key;
5254 u64 prev_block = 0;
5255
Zheng Yan1a40e232008-09-26 10:09:34 -04005256
5257 trans = btrfs_start_transaction(extent_root, 1);
5258 BUG_ON(!trans);
5259
5260 if (extent_key->objectid == 0) {
5261 ret = del_extent_zero(trans, extent_root, path, extent_key);
5262 goto out;
5263 }
5264
5265 ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS);
5266 if (!ref_path) {
Chris Masond3977122009-01-05 21:25:51 -05005267 ret = -ENOMEM;
5268 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04005269 }
5270
5271 for (loops = 0; ; loops++) {
5272 if (loops == 0) {
5273 ret = btrfs_first_ref_path(trans, extent_root, ref_path,
5274 extent_key->objectid);
5275 } else {
5276 ret = btrfs_next_ref_path(trans, extent_root, ref_path);
5277 }
5278 if (ret < 0)
5279 goto out;
5280 if (ret > 0)
5281 break;
5282
5283 if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID ||
5284 ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID)
5285 continue;
5286
5287 found_root = read_ref_root(extent_root->fs_info, ref_path);
5288 BUG_ON(!found_root);
5289 /*
5290 * for reference counted tree, only process reference paths
5291 * rooted at the latest committed root.
5292 */
5293 if (found_root->ref_cows &&
5294 ref_path->root_generation != found_root->root_key.offset)
5295 continue;
5296
5297 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
5298 if (pass == 0) {
5299 /*
5300 * copy data extents to new locations
5301 */
5302 u64 group_start = group->key.objectid;
5303 ret = relocate_data_extent(reloc_inode,
5304 extent_key,
5305 group_start);
5306 if (ret < 0)
5307 goto out;
5308 break;
5309 }
5310 level = 0;
5311 } else {
5312 level = ref_path->owner_objectid;
5313 }
5314
5315 if (prev_block != ref_path->nodes[level]) {
5316 struct extent_buffer *eb;
5317 u64 block_start = ref_path->nodes[level];
5318 u64 block_size = btrfs_level_size(found_root, level);
5319
5320 eb = read_tree_block(found_root, block_start,
5321 block_size, 0);
5322 btrfs_tree_lock(eb);
5323 BUG_ON(level != btrfs_header_level(eb));
5324
5325 if (level == 0)
5326 btrfs_item_key_to_cpu(eb, &first_key, 0);
5327 else
5328 btrfs_node_key_to_cpu(eb, &first_key, 0);
5329
5330 btrfs_tree_unlock(eb);
5331 free_extent_buffer(eb);
5332 prev_block = block_start;
5333 }
5334
Yan Zheng24562422009-02-12 14:14:53 -05005335 mutex_lock(&extent_root->fs_info->trans_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05005336 btrfs_record_root_in_trans(found_root);
Yan Zheng24562422009-02-12 14:14:53 -05005337 mutex_unlock(&extent_root->fs_info->trans_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05005338 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
5339 /*
5340 * try to update data extent references while
5341 * keeping metadata shared between snapshots.
5342 */
5343 if (pass == 1) {
5344 ret = relocate_one_path(trans, found_root,
5345 path, &first_key, ref_path,
5346 group, reloc_inode);
5347 if (ret < 0)
5348 goto out;
5349 continue;
5350 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005351 /*
5352 * use fallback method to process the remaining
5353 * references.
5354 */
5355 if (!new_extents) {
5356 u64 group_start = group->key.objectid;
Yan Zhengd899e052008-10-30 14:25:28 -04005357 new_extents = kmalloc(sizeof(*new_extents),
5358 GFP_NOFS);
5359 nr_extents = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04005360 ret = get_new_locations(reloc_inode,
5361 extent_key,
Yan Zhengd899e052008-10-30 14:25:28 -04005362 group_start, 1,
Zheng Yan1a40e232008-09-26 10:09:34 -04005363 &new_extents,
5364 &nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04005365 if (ret)
Zheng Yan1a40e232008-09-26 10:09:34 -04005366 goto out;
5367 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005368 ret = replace_one_extent(trans, found_root,
5369 path, extent_key,
5370 &first_key, ref_path,
5371 new_extents, nr_extents);
Yan Zhenge4404d62008-12-12 10:03:26 -05005372 } else {
Zheng Yan1a40e232008-09-26 10:09:34 -04005373 ret = relocate_tree_block(trans, found_root, path,
5374 &first_key, ref_path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005375 }
5376 if (ret < 0)
5377 goto out;
5378 }
5379 ret = 0;
5380out:
5381 btrfs_end_transaction(trans, extent_root);
5382 kfree(new_extents);
5383 kfree(ref_path);
Chris Masonedbd8d42007-12-21 16:27:24 -05005384 return ret;
5385}
5386
Chris Masonec44a352008-04-28 15:29:52 -04005387static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
5388{
5389 u64 num_devices;
5390 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
5391 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
5392
Yan Zheng2b820322008-11-17 21:11:30 -05005393 num_devices = root->fs_info->fs_devices->rw_devices;
Chris Masonec44a352008-04-28 15:29:52 -04005394 if (num_devices == 1) {
5395 stripped |= BTRFS_BLOCK_GROUP_DUP;
5396 stripped = flags & ~stripped;
5397
5398 /* turn raid0 into single device chunks */
5399 if (flags & BTRFS_BLOCK_GROUP_RAID0)
5400 return stripped;
5401
5402 /* turn mirroring into duplication */
5403 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
5404 BTRFS_BLOCK_GROUP_RAID10))
5405 return stripped | BTRFS_BLOCK_GROUP_DUP;
5406 return flags;
5407 } else {
5408 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04005409 if (flags & stripped)
5410 return flags;
5411
5412 stripped |= BTRFS_BLOCK_GROUP_DUP;
5413 stripped = flags & ~stripped;
5414
5415 /* switch duplicated blocks with raid1 */
5416 if (flags & BTRFS_BLOCK_GROUP_DUP)
5417 return stripped | BTRFS_BLOCK_GROUP_RAID1;
5418
5419 /* turn single device chunks into raid0 */
5420 return stripped | BTRFS_BLOCK_GROUP_RAID0;
5421 }
5422 return flags;
5423}
5424
Christoph Hellwigb2950862008-12-02 09:54:17 -05005425static int __alloc_chunk_for_shrink(struct btrfs_root *root,
Chris Mason0ef3e662008-05-24 14:04:53 -04005426 struct btrfs_block_group_cache *shrink_block_group,
5427 int force)
5428{
5429 struct btrfs_trans_handle *trans;
5430 u64 new_alloc_flags;
5431 u64 calc;
5432
Chris Masonc286ac42008-07-22 23:06:41 -04005433 spin_lock(&shrink_block_group->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04005434 if (btrfs_block_group_used(&shrink_block_group->item) > 0) {
Chris Masonc286ac42008-07-22 23:06:41 -04005435 spin_unlock(&shrink_block_group->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04005436
Chris Mason0ef3e662008-05-24 14:04:53 -04005437 trans = btrfs_start_transaction(root, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04005438 spin_lock(&shrink_block_group->lock);
Chris Mason7d9eb122008-07-08 14:19:17 -04005439
Chris Mason0ef3e662008-05-24 14:04:53 -04005440 new_alloc_flags = update_block_group_flags(root,
5441 shrink_block_group->flags);
5442 if (new_alloc_flags != shrink_block_group->flags) {
5443 calc =
5444 btrfs_block_group_used(&shrink_block_group->item);
5445 } else {
5446 calc = shrink_block_group->key.offset;
5447 }
Chris Masonc286ac42008-07-22 23:06:41 -04005448 spin_unlock(&shrink_block_group->lock);
5449
Chris Mason0ef3e662008-05-24 14:04:53 -04005450 do_chunk_alloc(trans, root->fs_info->extent_root,
5451 calc + 2 * 1024 * 1024, new_alloc_flags, force);
Chris Mason7d9eb122008-07-08 14:19:17 -04005452
Chris Mason0ef3e662008-05-24 14:04:53 -04005453 btrfs_end_transaction(trans, root);
Chris Masonc286ac42008-07-22 23:06:41 -04005454 } else
5455 spin_unlock(&shrink_block_group->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04005456 return 0;
5457}
5458
Zheng Yan1a40e232008-09-26 10:09:34 -04005459static int __insert_orphan_inode(struct btrfs_trans_handle *trans,
5460 struct btrfs_root *root,
5461 u64 objectid, u64 size)
5462{
5463 struct btrfs_path *path;
5464 struct btrfs_inode_item *item;
5465 struct extent_buffer *leaf;
5466 int ret;
5467
5468 path = btrfs_alloc_path();
5469 if (!path)
5470 return -ENOMEM;
5471
Chris Masonb9473432009-03-13 11:00:37 -04005472 path->leave_spinning = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04005473 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
5474 if (ret)
5475 goto out;
5476
5477 leaf = path->nodes[0];
5478 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item);
5479 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
5480 btrfs_set_inode_generation(leaf, item, 1);
5481 btrfs_set_inode_size(leaf, item, size);
5482 btrfs_set_inode_mode(leaf, item, S_IFREG | 0600);
Yan Zheng0403e472008-12-10 20:32:51 -05005483 btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NOCOMPRESS);
Zheng Yan1a40e232008-09-26 10:09:34 -04005484 btrfs_mark_buffer_dirty(leaf);
5485 btrfs_release_path(root, path);
5486out:
5487 btrfs_free_path(path);
5488 return ret;
5489}
5490
Chris Masond3977122009-01-05 21:25:51 -05005491static noinline struct inode *create_reloc_inode(struct btrfs_fs_info *fs_info,
Zheng Yan1a40e232008-09-26 10:09:34 -04005492 struct btrfs_block_group_cache *group)
5493{
5494 struct inode *inode = NULL;
5495 struct btrfs_trans_handle *trans;
5496 struct btrfs_root *root;
5497 struct btrfs_key root_key;
5498 u64 objectid = BTRFS_FIRST_FREE_OBJECTID;
5499 int err = 0;
5500
5501 root_key.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
5502 root_key.type = BTRFS_ROOT_ITEM_KEY;
5503 root_key.offset = (u64)-1;
5504 root = btrfs_read_fs_root_no_name(fs_info, &root_key);
5505 if (IS_ERR(root))
5506 return ERR_CAST(root);
5507
5508 trans = btrfs_start_transaction(root, 1);
5509 BUG_ON(!trans);
5510
5511 err = btrfs_find_free_objectid(trans, root, objectid, &objectid);
5512 if (err)
5513 goto out;
5514
5515 err = __insert_orphan_inode(trans, root, objectid, group->key.offset);
5516 BUG_ON(err);
5517
5518 err = btrfs_insert_file_extent(trans, root, objectid, 0, 0, 0,
Chris Masonc8b97812008-10-29 14:49:59 -04005519 group->key.offset, 0, group->key.offset,
5520 0, 0, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04005521 BUG_ON(err);
5522
5523 inode = btrfs_iget_locked(root->fs_info->sb, objectid, root);
5524 if (inode->i_state & I_NEW) {
5525 BTRFS_I(inode)->root = root;
5526 BTRFS_I(inode)->location.objectid = objectid;
5527 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
5528 BTRFS_I(inode)->location.offset = 0;
5529 btrfs_read_locked_inode(inode);
5530 unlock_new_inode(inode);
5531 BUG_ON(is_bad_inode(inode));
5532 } else {
5533 BUG_ON(1);
5534 }
Yan Zheng17d217f2008-12-12 10:03:38 -05005535 BTRFS_I(inode)->index_cnt = group->key.objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04005536
5537 err = btrfs_orphan_add(trans, inode);
5538out:
5539 btrfs_end_transaction(trans, root);
5540 if (err) {
5541 if (inode)
5542 iput(inode);
5543 inode = ERR_PTR(err);
5544 }
5545 return inode;
5546}
5547
Yan Zheng17d217f2008-12-12 10:03:38 -05005548int btrfs_reloc_clone_csums(struct inode *inode, u64 file_pos, u64 len)
5549{
5550
5551 struct btrfs_ordered_sum *sums;
5552 struct btrfs_sector_sum *sector_sum;
5553 struct btrfs_ordered_extent *ordered;
5554 struct btrfs_root *root = BTRFS_I(inode)->root;
5555 struct list_head list;
5556 size_t offset;
5557 int ret;
5558 u64 disk_bytenr;
5559
5560 INIT_LIST_HEAD(&list);
5561
5562 ordered = btrfs_lookup_ordered_extent(inode, file_pos);
5563 BUG_ON(ordered->file_offset != file_pos || ordered->len != len);
5564
5565 disk_bytenr = file_pos + BTRFS_I(inode)->index_cnt;
Yan Zheng07d400a2009-01-06 11:42:00 -05005566 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, disk_bytenr,
Yan Zheng17d217f2008-12-12 10:03:38 -05005567 disk_bytenr + len - 1, &list);
5568
5569 while (!list_empty(&list)) {
5570 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
5571 list_del_init(&sums->list);
5572
5573 sector_sum = sums->sums;
5574 sums->bytenr = ordered->start;
5575
5576 offset = 0;
5577 while (offset < sums->len) {
5578 sector_sum->bytenr += ordered->start - disk_bytenr;
5579 sector_sum++;
5580 offset += root->sectorsize;
5581 }
5582
5583 btrfs_add_ordered_sum(inode, ordered, sums);
5584 }
5585 btrfs_put_ordered_extent(ordered);
5586 return 0;
5587}
5588
Zheng Yan1a40e232008-09-26 10:09:34 -04005589int btrfs_relocate_block_group(struct btrfs_root *root, u64 group_start)
Chris Masonedbd8d42007-12-21 16:27:24 -05005590{
5591 struct btrfs_trans_handle *trans;
Chris Masonedbd8d42007-12-21 16:27:24 -05005592 struct btrfs_path *path;
Zheng Yan1a40e232008-09-26 10:09:34 -04005593 struct btrfs_fs_info *info = root->fs_info;
5594 struct extent_buffer *leaf;
5595 struct inode *reloc_inode;
5596 struct btrfs_block_group_cache *block_group;
5597 struct btrfs_key key;
Yan Zhengd899e052008-10-30 14:25:28 -04005598 u64 skipped;
Chris Masonedbd8d42007-12-21 16:27:24 -05005599 u64 cur_byte;
5600 u64 total_found;
Chris Masonedbd8d42007-12-21 16:27:24 -05005601 u32 nritems;
5602 int ret;
Chris Masona061fc82008-05-07 11:43:44 -04005603 int progress;
Zheng Yan1a40e232008-09-26 10:09:34 -04005604 int pass = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05005605
Chris Masonedbd8d42007-12-21 16:27:24 -05005606 root = root->fs_info->extent_root;
Zheng Yan1a40e232008-09-26 10:09:34 -04005607
5608 block_group = btrfs_lookup_block_group(info, group_start);
5609 BUG_ON(!block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05005610
Chris Masond3977122009-01-05 21:25:51 -05005611 printk(KERN_INFO "btrfs relocating block group %llu flags %llu\n",
Zheng Yan1a40e232008-09-26 10:09:34 -04005612 (unsigned long long)block_group->key.objectid,
5613 (unsigned long long)block_group->flags);
Chris Mason323da792008-05-09 11:46:48 -04005614
Zheng Yan1a40e232008-09-26 10:09:34 -04005615 path = btrfs_alloc_path();
5616 BUG_ON(!path);
Chris Mason323da792008-05-09 11:46:48 -04005617
Zheng Yan1a40e232008-09-26 10:09:34 -04005618 reloc_inode = create_reloc_inode(info, block_group);
5619 BUG_ON(IS_ERR(reloc_inode));
5620
Zheng Yan1a40e232008-09-26 10:09:34 -04005621 __alloc_chunk_for_shrink(root, block_group, 1);
Yan Zhengc146afa2008-11-12 14:34:12 -05005622 set_block_group_readonly(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04005623
Zheng Yan1a40e232008-09-26 10:09:34 -04005624 btrfs_start_delalloc_inodes(info->tree_root);
5625 btrfs_wait_ordered_extents(info->tree_root, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -04005626again:
Yan Zhengd899e052008-10-30 14:25:28 -04005627 skipped = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05005628 total_found = 0;
Chris Masona061fc82008-05-07 11:43:44 -04005629 progress = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04005630 key.objectid = block_group->key.objectid;
Chris Masonedbd8d42007-12-21 16:27:24 -05005631 key.offset = 0;
5632 key.type = 0;
Yan73e48b22008-01-03 14:14:39 -05005633 cur_byte = key.objectid;
Chris Mason4313b392008-01-03 09:08:48 -05005634
Zheng Yan1a40e232008-09-26 10:09:34 -04005635 trans = btrfs_start_transaction(info->tree_root, 1);
5636 btrfs_commit_transaction(trans, info->tree_root);
Chris Masonea8c2812008-08-04 23:17:27 -04005637
Zheng Yan1a40e232008-09-26 10:09:34 -04005638 mutex_lock(&root->fs_info->cleaner_mutex);
5639 btrfs_clean_old_snapshots(info->tree_root);
5640 btrfs_remove_leaf_refs(info->tree_root, (u64)-1, 1);
5641 mutex_unlock(&root->fs_info->cleaner_mutex);
Chris Masonea8c2812008-08-04 23:17:27 -04005642
Chris Mason56bec292009-03-13 10:10:06 -04005643 trans = btrfs_start_transaction(info->tree_root, 1);
5644 btrfs_commit_transaction(trans, info->tree_root);
5645
Chris Masond3977122009-01-05 21:25:51 -05005646 while (1) {
Chris Masonedbd8d42007-12-21 16:27:24 -05005647 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5648 if (ret < 0)
5649 goto out;
Chris Mason7d9eb122008-07-08 14:19:17 -04005650next:
Chris Masonedbd8d42007-12-21 16:27:24 -05005651 leaf = path->nodes[0];
Chris Masonedbd8d42007-12-21 16:27:24 -05005652 nritems = btrfs_header_nritems(leaf);
Yan73e48b22008-01-03 14:14:39 -05005653 if (path->slots[0] >= nritems) {
5654 ret = btrfs_next_leaf(root, path);
5655 if (ret < 0)
5656 goto out;
5657 if (ret == 1) {
5658 ret = 0;
5659 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05005660 }
Yan73e48b22008-01-03 14:14:39 -05005661 leaf = path->nodes[0];
5662 nritems = btrfs_header_nritems(leaf);
5663 }
5664
Zheng Yan1a40e232008-09-26 10:09:34 -04005665 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Mason725c8462008-01-04 16:47:16 -05005666
Zheng Yan1a40e232008-09-26 10:09:34 -04005667 if (key.objectid >= block_group->key.objectid +
5668 block_group->key.offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04005669 break;
5670
Chris Mason725c8462008-01-04 16:47:16 -05005671 if (progress && need_resched()) {
Chris Mason725c8462008-01-04 16:47:16 -05005672 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005673 cond_resched();
Chris Mason725c8462008-01-04 16:47:16 -05005674 progress = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04005675 continue;
Chris Mason725c8462008-01-04 16:47:16 -05005676 }
5677 progress = 1;
5678
Zheng Yan1a40e232008-09-26 10:09:34 -04005679 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY ||
5680 key.objectid + key.offset <= cur_byte) {
Yan73e48b22008-01-03 14:14:39 -05005681 path->slots[0]++;
Chris Masonedbd8d42007-12-21 16:27:24 -05005682 goto next;
5683 }
Yan73e48b22008-01-03 14:14:39 -05005684
Chris Masonedbd8d42007-12-21 16:27:24 -05005685 total_found++;
Zheng Yan1a40e232008-09-26 10:09:34 -04005686 cur_byte = key.objectid + key.offset;
Chris Masonedbd8d42007-12-21 16:27:24 -05005687 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005688
5689 __alloc_chunk_for_shrink(root, block_group, 0);
5690 ret = relocate_one_extent(root, path, &key, block_group,
5691 reloc_inode, pass);
5692 BUG_ON(ret < 0);
Yan Zhengd899e052008-10-30 14:25:28 -04005693 if (ret > 0)
5694 skipped++;
Zheng Yan1a40e232008-09-26 10:09:34 -04005695
5696 key.objectid = cur_byte;
5697 key.type = 0;
5698 key.offset = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05005699 }
5700
5701 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005702
5703 if (pass == 0) {
5704 btrfs_wait_ordered_range(reloc_inode, 0, (u64)-1);
5705 invalidate_mapping_pages(reloc_inode->i_mapping, 0, -1);
Zheng Yan1a40e232008-09-26 10:09:34 -04005706 }
Chris Masonedbd8d42007-12-21 16:27:24 -05005707
5708 if (total_found > 0) {
Chris Masond3977122009-01-05 21:25:51 -05005709 printk(KERN_INFO "btrfs found %llu extents in pass %d\n",
Zheng Yan1a40e232008-09-26 10:09:34 -04005710 (unsigned long long)total_found, pass);
5711 pass++;
Yan Zhengd899e052008-10-30 14:25:28 -04005712 if (total_found == skipped && pass > 2) {
5713 iput(reloc_inode);
5714 reloc_inode = create_reloc_inode(info, block_group);
5715 pass = 0;
5716 }
Chris Masonedbd8d42007-12-21 16:27:24 -05005717 goto again;
5718 }
5719
Zheng Yan1a40e232008-09-26 10:09:34 -04005720 /* delete reloc_inode */
5721 iput(reloc_inode);
Chris Mason7d9eb122008-07-08 14:19:17 -04005722
Zheng Yan1a40e232008-09-26 10:09:34 -04005723 /* unpin extents in this range */
5724 trans = btrfs_start_transaction(info->tree_root, 1);
5725 btrfs_commit_transaction(trans, info->tree_root);
Chris Mason0ef3e662008-05-24 14:04:53 -04005726
Zheng Yan1a40e232008-09-26 10:09:34 -04005727 spin_lock(&block_group->lock);
5728 WARN_ON(block_group->pinned > 0);
5729 WARN_ON(block_group->reserved > 0);
5730 WARN_ON(btrfs_block_group_used(&block_group->item) > 0);
5731 spin_unlock(&block_group->lock);
Yan Zhengd2fb3432008-12-11 16:30:39 -05005732 put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04005733 ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05005734out:
Zheng Yan1a40e232008-09-26 10:09:34 -04005735 btrfs_free_path(path);
Chris Masonedbd8d42007-12-21 16:27:24 -05005736 return ret;
5737}
5738
Christoph Hellwigb2950862008-12-02 09:54:17 -05005739static int find_first_block_group(struct btrfs_root *root,
5740 struct btrfs_path *path, struct btrfs_key *key)
Chris Mason0b86a832008-03-24 15:01:56 -04005741{
Chris Mason925baed2008-06-25 16:01:30 -04005742 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005743 struct btrfs_key found_key;
5744 struct extent_buffer *leaf;
5745 int slot;
5746
5747 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
5748 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04005749 goto out;
5750
Chris Masond3977122009-01-05 21:25:51 -05005751 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04005752 slot = path->slots[0];
5753 leaf = path->nodes[0];
5754 if (slot >= btrfs_header_nritems(leaf)) {
5755 ret = btrfs_next_leaf(root, path);
5756 if (ret == 0)
5757 continue;
5758 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04005759 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04005760 break;
5761 }
5762 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5763
5764 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04005765 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
5766 ret = 0;
5767 goto out;
5768 }
Chris Mason0b86a832008-03-24 15:01:56 -04005769 path->slots[0]++;
5770 }
5771 ret = -ENOENT;
Chris Mason925baed2008-06-25 16:01:30 -04005772out:
Chris Mason0b86a832008-03-24 15:01:56 -04005773 return ret;
5774}
5775
Zheng Yan1a40e232008-09-26 10:09:34 -04005776int btrfs_free_block_groups(struct btrfs_fs_info *info)
5777{
5778 struct btrfs_block_group_cache *block_group;
Chris Mason4184ea72009-03-10 12:39:20 -04005779 struct btrfs_space_info *space_info;
Zheng Yan1a40e232008-09-26 10:09:34 -04005780 struct rb_node *n;
5781
Zheng Yan1a40e232008-09-26 10:09:34 -04005782 spin_lock(&info->block_group_cache_lock);
5783 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
5784 block_group = rb_entry(n, struct btrfs_block_group_cache,
5785 cache_node);
Zheng Yan1a40e232008-09-26 10:09:34 -04005786 rb_erase(&block_group->cache_node,
5787 &info->block_group_cache_tree);
Yan Zhengd899e052008-10-30 14:25:28 -04005788 spin_unlock(&info->block_group_cache_lock);
5789
5790 btrfs_remove_free_space_cache(block_group);
Josef Bacik80eb2342008-10-29 14:49:05 -04005791 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04005792 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04005793 up_write(&block_group->space_info->groups_sem);
Yan Zhengd2fb3432008-12-11 16:30:39 -05005794
5795 WARN_ON(atomic_read(&block_group->count) != 1);
Zheng Yan1a40e232008-09-26 10:09:34 -04005796 kfree(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04005797
5798 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04005799 }
5800 spin_unlock(&info->block_group_cache_lock);
Chris Mason4184ea72009-03-10 12:39:20 -04005801
5802 /* now that all the block groups are freed, go through and
5803 * free all the space_info structs. This is only called during
5804 * the final stages of unmount, and so we know nobody is
5805 * using them. We call synchronize_rcu() once before we start,
5806 * just to be on the safe side.
5807 */
5808 synchronize_rcu();
5809
5810 while(!list_empty(&info->space_info)) {
5811 space_info = list_entry(info->space_info.next,
5812 struct btrfs_space_info,
5813 list);
5814
5815 list_del(&space_info->list);
5816 kfree(space_info);
5817 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005818 return 0;
5819}
5820
Chris Mason9078a3e2007-04-26 16:46:15 -04005821int btrfs_read_block_groups(struct btrfs_root *root)
5822{
5823 struct btrfs_path *path;
5824 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04005825 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04005826 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04005827 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04005828 struct btrfs_key key;
5829 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04005830 struct extent_buffer *leaf;
Chris Mason96b51792007-10-15 16:15:19 -04005831
Chris Masonbe744172007-05-06 10:15:01 -04005832 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04005833 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005834 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04005835 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04005836 path = btrfs_alloc_path();
5837 if (!path)
5838 return -ENOMEM;
5839
Chris Masond3977122009-01-05 21:25:51 -05005840 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04005841 ret = find_first_block_group(root, path, &key);
5842 if (ret > 0) {
5843 ret = 0;
5844 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04005845 }
Chris Mason0b86a832008-03-24 15:01:56 -04005846 if (ret != 0)
5847 goto error;
5848
Chris Mason5f39d392007-10-15 16:14:19 -04005849 leaf = path->nodes[0];
5850 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04005851 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04005852 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04005853 ret = -ENOMEM;
Chris Mason9078a3e2007-04-26 16:46:15 -04005854 break;
5855 }
Chris Mason3e1ad542007-05-07 20:03:49 -04005856
Yan Zhengd2fb3432008-12-11 16:30:39 -05005857 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04005858 spin_lock_init(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04005859 mutex_init(&cache->alloc_mutex);
Josef Bacikea6a4782008-11-20 12:16:16 -05005860 mutex_init(&cache->cache_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005861 INIT_LIST_HEAD(&cache->list);
Chris Mason5f39d392007-10-15 16:14:19 -04005862 read_extent_buffer(leaf, &cache->item,
5863 btrfs_item_ptr_offset(leaf, path->slots[0]),
5864 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04005865 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04005866
Chris Mason9078a3e2007-04-26 16:46:15 -04005867 key.objectid = found_key.objectid + found_key.offset;
5868 btrfs_release_path(root, path);
Chris Mason0b86a832008-03-24 15:01:56 -04005869 cache->flags = btrfs_block_group_flags(&cache->item);
Chris Mason96b51792007-10-15 16:15:19 -04005870
Chris Mason6324fbf2008-03-24 15:01:59 -04005871 ret = update_space_info(info, cache->flags, found_key.offset,
5872 btrfs_block_group_used(&cache->item),
5873 &space_info);
5874 BUG_ON(ret);
5875 cache->space_info = space_info;
Josef Bacik80eb2342008-10-29 14:49:05 -04005876 down_write(&space_info->groups_sem);
5877 list_add_tail(&cache->list, &space_info->block_groups);
5878 up_write(&space_info->groups_sem);
Chris Mason6324fbf2008-03-24 15:01:59 -04005879
Josef Bacik0f9dd462008-09-23 13:14:11 -04005880 ret = btrfs_add_block_group_cache(root->fs_info, cache);
5881 BUG_ON(ret);
Chris Mason75ccf472008-09-30 19:24:06 -04005882
5883 set_avail_alloc_bits(root->fs_info, cache->flags);
Yan Zheng2b820322008-11-17 21:11:30 -05005884 if (btrfs_chunk_readonly(root, cache->key.objectid))
5885 set_block_group_readonly(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04005886 }
Chris Mason0b86a832008-03-24 15:01:56 -04005887 ret = 0;
5888error:
Chris Mason9078a3e2007-04-26 16:46:15 -04005889 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04005890 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04005891}
Chris Mason6324fbf2008-03-24 15:01:59 -04005892
5893int btrfs_make_block_group(struct btrfs_trans_handle *trans,
5894 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04005895 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04005896 u64 size)
5897{
5898 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04005899 struct btrfs_root *extent_root;
5900 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04005901
5902 extent_root = root->fs_info->extent_root;
Chris Mason6324fbf2008-03-24 15:01:59 -04005903
Chris Mason12fcfd22009-03-24 10:24:20 -04005904 root->fs_info->last_trans_log_full_commit = trans->transid;
Chris Masone02119d2008-09-05 16:13:11 -04005905
Chris Mason8f18cf12008-04-25 16:53:30 -04005906 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005907 if (!cache)
5908 return -ENOMEM;
5909
Chris Masone17cade2008-04-15 15:41:47 -04005910 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04005911 cache->key.offset = size;
Yan Zhengd2fb3432008-12-11 16:30:39 -05005912 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
5913 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04005914 spin_lock_init(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04005915 mutex_init(&cache->alloc_mutex);
Josef Bacikea6a4782008-11-20 12:16:16 -05005916 mutex_init(&cache->cache_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005917 INIT_LIST_HEAD(&cache->list);
Chris Mason0ef3e662008-05-24 14:04:53 -04005918
Chris Mason6324fbf2008-03-24 15:01:59 -04005919 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04005920 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
5921 cache->flags = type;
5922 btrfs_set_block_group_flags(&cache->item, type);
5923
5924 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
5925 &cache->space_info);
5926 BUG_ON(ret);
Josef Bacik80eb2342008-10-29 14:49:05 -04005927 down_write(&cache->space_info->groups_sem);
5928 list_add_tail(&cache->list, &cache->space_info->block_groups);
5929 up_write(&cache->space_info->groups_sem);
Chris Mason6324fbf2008-03-24 15:01:59 -04005930
Josef Bacik0f9dd462008-09-23 13:14:11 -04005931 ret = btrfs_add_block_group_cache(root->fs_info, cache);
5932 BUG_ON(ret);
Chris Masonc286ac42008-07-22 23:06:41 -04005933
Chris Mason6324fbf2008-03-24 15:01:59 -04005934 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
5935 sizeof(cache->item));
5936 BUG_ON(ret);
5937
Chris Masond18a2c42008-04-04 15:40:00 -04005938 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04005939
Chris Mason6324fbf2008-03-24 15:01:59 -04005940 return 0;
5941}
Zheng Yan1a40e232008-09-26 10:09:34 -04005942
5943int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
5944 struct btrfs_root *root, u64 group_start)
5945{
5946 struct btrfs_path *path;
5947 struct btrfs_block_group_cache *block_group;
5948 struct btrfs_key key;
5949 int ret;
5950
Zheng Yan1a40e232008-09-26 10:09:34 -04005951 root = root->fs_info->extent_root;
5952
5953 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
5954 BUG_ON(!block_group);
Yan Zhengc146afa2008-11-12 14:34:12 -05005955 BUG_ON(!block_group->ro);
Zheng Yan1a40e232008-09-26 10:09:34 -04005956
5957 memcpy(&key, &block_group->key, sizeof(key));
5958
5959 path = btrfs_alloc_path();
5960 BUG_ON(!path);
5961
Yan Zheng3dfdb932009-01-21 10:49:16 -05005962 spin_lock(&root->fs_info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04005963 rb_erase(&block_group->cache_node,
5964 &root->fs_info->block_group_cache_tree);
Yan Zheng3dfdb932009-01-21 10:49:16 -05005965 spin_unlock(&root->fs_info->block_group_cache_lock);
5966 btrfs_remove_free_space_cache(block_group);
Josef Bacik80eb2342008-10-29 14:49:05 -04005967 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04005968 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04005969 up_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04005970
Yan Zhengc146afa2008-11-12 14:34:12 -05005971 spin_lock(&block_group->space_info->lock);
5972 block_group->space_info->total_bytes -= block_group->key.offset;
5973 block_group->space_info->bytes_readonly -= block_group->key.offset;
5974 spin_unlock(&block_group->space_info->lock);
Yan Zheng2b820322008-11-17 21:11:30 -05005975 block_group->space_info->full = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05005976
Yan Zhengd2fb3432008-12-11 16:30:39 -05005977 put_block_group(block_group);
5978 put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04005979
5980 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
5981 if (ret > 0)
5982 ret = -EIO;
5983 if (ret < 0)
5984 goto out;
5985
5986 ret = btrfs_del_item(trans, root, path);
5987out:
5988 btrfs_free_path(path);
5989 return ret;
5990}