blob: 1cc89246ee2f4ae5a9fabb5b473eebc9cdb85cf5 [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 Mason4b4e25f2008-11-20 10:22:27 -050022#include <linux/version.h>
23#include "compat.h"
Chris Mason74493f72007-12-11 09:25:06 -050024#include "hash.h"
Miguela5eb62e2008-04-11 15:45:51 -040025#include "crc32c.h"
Chris Masonfec577f2007-02-26 10:40:21 -050026#include "ctree.h"
27#include "disk-io.h"
28#include "print-tree.h"
Chris Masone089f052007-03-16 16:20:31 -040029#include "transaction.h"
Chris Mason0b86a832008-03-24 15:01:56 -040030#include "volumes.h"
Chris Mason925baed2008-06-25 16:01:30 -040031#include "locking.h"
Yan Zheng31153d82008-07-28 15:32:19 -040032#include "ref-cache.h"
Chris Mason15916de2008-11-19 21:17:22 -050033#include "compat.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 Masone089f052007-03-16 16:20:31 -040052static int finish_current_insert(struct btrfs_trans_handle *trans, struct
Chris Mason87ef2bb2008-10-30 11:23:27 -040053 btrfs_root *extent_root, int all);
Chris Masone20d96d2007-03-22 12:13:20 -040054static int del_pending_extents(struct btrfs_trans_handle *trans, struct
Chris Mason87ef2bb2008-10-30 11:23:27 -040055 btrfs_root *extent_root, int all);
Josef Bacikf3465ca2008-11-12 14:19:50 -050056static int pin_down_bytes(struct btrfs_trans_handle *trans,
57 struct btrfs_root *root,
58 u64 bytenr, u64 num_bytes, int is_data);
59static 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);
Yand548ee52008-01-03 13:56:30 -050063
Josef Bacik0f9dd462008-09-23 13:14:11 -040064static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
65{
66 return (cache->flags & bits) == bits;
67}
68
69/*
70 * this adds the block group to the fs_info rb tree for the block group
71 * cache
72 */
Christoph Hellwigb2950862008-12-02 09:54:17 -050073static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
Josef Bacik0f9dd462008-09-23 13:14:11 -040074 struct btrfs_block_group_cache *block_group)
75{
76 struct rb_node **p;
77 struct rb_node *parent = NULL;
78 struct btrfs_block_group_cache *cache;
79
80 spin_lock(&info->block_group_cache_lock);
81 p = &info->block_group_cache_tree.rb_node;
82
83 while (*p) {
84 parent = *p;
85 cache = rb_entry(parent, struct btrfs_block_group_cache,
86 cache_node);
87 if (block_group->key.objectid < cache->key.objectid) {
88 p = &(*p)->rb_left;
89 } else if (block_group->key.objectid > cache->key.objectid) {
90 p = &(*p)->rb_right;
91 } else {
92 spin_unlock(&info->block_group_cache_lock);
93 return -EEXIST;
94 }
95 }
96
97 rb_link_node(&block_group->cache_node, parent, p);
98 rb_insert_color(&block_group->cache_node,
99 &info->block_group_cache_tree);
100 spin_unlock(&info->block_group_cache_lock);
101
102 return 0;
103}
104
105/*
106 * This will return the block group at or after bytenr if contains is 0, else
107 * it will return the block group that contains the bytenr
108 */
109static struct btrfs_block_group_cache *
110block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
111 int contains)
112{
113 struct btrfs_block_group_cache *cache, *ret = NULL;
114 struct rb_node *n;
115 u64 end, start;
116
117 spin_lock(&info->block_group_cache_lock);
118 n = info->block_group_cache_tree.rb_node;
119
120 while (n) {
121 cache = rb_entry(n, struct btrfs_block_group_cache,
122 cache_node);
123 end = cache->key.objectid + cache->key.offset - 1;
124 start = cache->key.objectid;
125
126 if (bytenr < start) {
127 if (!contains && (!ret || start < ret->key.objectid))
128 ret = cache;
129 n = n->rb_left;
130 } else if (bytenr > start) {
131 if (contains && bytenr <= end) {
132 ret = cache;
133 break;
134 }
135 n = n->rb_right;
136 } else {
137 ret = cache;
138 break;
139 }
140 }
Yan Zhengd2fb3432008-12-11 16:30:39 -0500141 if (ret)
142 atomic_inc(&ret->count);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400143 spin_unlock(&info->block_group_cache_lock);
144
145 return ret;
146}
147
148/*
149 * this is only called by cache_block_group, since we could have freed extents
150 * we need to check the pinned_extents for any extents that can't be used yet
151 * since their free space will be released as soon as the transaction commits.
152 */
153static int add_new_free_space(struct btrfs_block_group_cache *block_group,
154 struct btrfs_fs_info *info, u64 start, u64 end)
155{
156 u64 extent_start, extent_end, size;
157 int ret;
158
Josef Bacik25179202008-10-29 14:49:05 -0400159 mutex_lock(&info->pinned_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400160 while (start < end) {
161 ret = find_first_extent_bit(&info->pinned_extents, start,
162 &extent_start, &extent_end,
163 EXTENT_DIRTY);
164 if (ret)
165 break;
166
167 if (extent_start == start) {
168 start = extent_end + 1;
169 } else if (extent_start > start && extent_start < end) {
170 size = extent_start - start;
Josef Bacikea6a4782008-11-20 12:16:16 -0500171 ret = btrfs_add_free_space(block_group, start,
172 size);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400173 BUG_ON(ret);
174 start = extent_end + 1;
175 } else {
176 break;
177 }
178 }
179
180 if (start < end) {
181 size = end - start;
Josef Bacikea6a4782008-11-20 12:16:16 -0500182 ret = btrfs_add_free_space(block_group, start, size);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400183 BUG_ON(ret);
184 }
Josef Bacik25179202008-10-29 14:49:05 -0400185 mutex_unlock(&info->pinned_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400186
187 return 0;
188}
189
Yan Zhenga512bbf2008-12-08 16:46:26 -0500190static int remove_sb_from_cache(struct btrfs_root *root,
191 struct btrfs_block_group_cache *cache)
192{
193 u64 bytenr;
194 u64 *logical;
195 int stripe_len;
196 int i, nr, ret;
197
198 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
199 bytenr = btrfs_sb_offset(i);
200 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
201 cache->key.objectid, bytenr, 0,
202 &logical, &nr, &stripe_len);
203 BUG_ON(ret);
204 while (nr--) {
205 btrfs_remove_free_space(cache, logical[nr],
206 stripe_len);
207 }
208 kfree(logical);
209 }
210 return 0;
211}
212
Chris Masone37c9e62007-05-09 20:13:14 -0400213static int cache_block_group(struct btrfs_root *root,
214 struct btrfs_block_group_cache *block_group)
215{
216 struct btrfs_path *path;
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400217 int ret = 0;
Chris Masone37c9e62007-05-09 20:13:14 -0400218 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400219 struct extent_buffer *leaf;
Chris Masone37c9e62007-05-09 20:13:14 -0400220 int slot;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500221 u64 last = block_group->key.objectid;
Chris Masone37c9e62007-05-09 20:13:14 -0400222
Chris Mason00f5c792007-11-30 10:09:33 -0500223 if (!block_group)
224 return 0;
225
Chris Masone37c9e62007-05-09 20:13:14 -0400226 root = root->fs_info->extent_root;
Chris Masone37c9e62007-05-09 20:13:14 -0400227
228 if (block_group->cached)
229 return 0;
Chris Masonf510cfe2007-10-15 16:14:48 -0400230
Chris Masone37c9e62007-05-09 20:13:14 -0400231 path = btrfs_alloc_path();
232 if (!path)
233 return -ENOMEM;
Yan7d7d6062007-09-14 16:15:28 -0400234
Chris Mason2cc58cf2007-08-27 16:49:44 -0400235 path->reada = 2;
Chris Mason5cd57b22008-06-25 16:01:30 -0400236 /*
237 * we get into deadlocks with paths held by callers of this function.
238 * since the alloc_mutex is protecting things right now, just
239 * skip the locking here
240 */
241 path->skip_locking = 1;
Yan Zheng0403e472008-12-10 20:32:51 -0500242 key.objectid = max_t(u64, last, BTRFS_SUPER_INFO_OFFSET);
Chris Masone37c9e62007-05-09 20:13:14 -0400243 key.offset = 0;
244 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
245 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
246 if (ret < 0)
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400247 goto err;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500248
Chris Masone37c9e62007-05-09 20:13:14 -0400249 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400250 leaf = path->nodes[0];
Chris Masone37c9e62007-05-09 20:13:14 -0400251 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -0400252 if (slot >= btrfs_header_nritems(leaf)) {
Chris Masone37c9e62007-05-09 20:13:14 -0400253 ret = btrfs_next_leaf(root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400254 if (ret < 0)
255 goto err;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400256 if (ret == 0)
Chris Masone37c9e62007-05-09 20:13:14 -0400257 continue;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400258 else
Chris Masone37c9e62007-05-09 20:13:14 -0400259 break;
Chris Masone37c9e62007-05-09 20:13:14 -0400260 }
Chris Mason5f39d392007-10-15 16:14:19 -0400261 btrfs_item_key_to_cpu(leaf, &key, slot);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400262 if (key.objectid < block_group->key.objectid)
Yan7d7d6062007-09-14 16:15:28 -0400263 goto next;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400264
Chris Masone37c9e62007-05-09 20:13:14 -0400265 if (key.objectid >= block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -0400266 block_group->key.offset)
Yan7d7d6062007-09-14 16:15:28 -0400267 break;
Yan7d7d6062007-09-14 16:15:28 -0400268
269 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
Josef Bacik0f9dd462008-09-23 13:14:11 -0400270 add_new_free_space(block_group, root->fs_info, last,
271 key.objectid);
272
Yan7d7d6062007-09-14 16:15:28 -0400273 last = key.objectid + key.offset;
Chris Masone37c9e62007-05-09 20:13:14 -0400274 }
Yan7d7d6062007-09-14 16:15:28 -0400275next:
Chris Masone37c9e62007-05-09 20:13:14 -0400276 path->slots[0]++;
277 }
278
Josef Bacik0f9dd462008-09-23 13:14:11 -0400279 add_new_free_space(block_group, root->fs_info, last,
280 block_group->key.objectid +
281 block_group->key.offset);
282
Yan Zhenga512bbf2008-12-08 16:46:26 -0500283 remove_sb_from_cache(root, block_group);
Chris Masone37c9e62007-05-09 20:13:14 -0400284 block_group->cached = 1;
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400285 ret = 0;
Chris Mason54aa1f42007-06-22 14:16:25 -0400286err:
Chris Masone37c9e62007-05-09 20:13:14 -0400287 btrfs_free_path(path);
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400288 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -0400289}
290
Josef Bacik0f9dd462008-09-23 13:14:11 -0400291/*
292 * return the block group that starts at or after bytenr
293 */
Christoph Hellwigb2950862008-12-02 09:54:17 -0500294static struct btrfs_block_group_cache *btrfs_lookup_first_block_group(struct
Chris Mason0ef3e662008-05-24 14:04:53 -0400295 btrfs_fs_info *info,
296 u64 bytenr)
297{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400298 struct btrfs_block_group_cache *cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400299
Josef Bacik0f9dd462008-09-23 13:14:11 -0400300 cache = block_group_cache_tree_search(info, bytenr, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -0400301
Josef Bacik0f9dd462008-09-23 13:14:11 -0400302 return cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400303}
304
Josef Bacik0f9dd462008-09-23 13:14:11 -0400305/*
306 * return the block group that contains teh given bytenr
307 */
Chris Mason5276aed2007-06-11 21:33:38 -0400308struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
309 btrfs_fs_info *info,
Chris Masondb945352007-10-15 16:15:53 -0400310 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400311{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400312 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -0400313
Josef Bacik0f9dd462008-09-23 13:14:11 -0400314 cache = block_group_cache_tree_search(info, bytenr, 1);
Chris Mason96b51792007-10-15 16:15:19 -0400315
Josef Bacik0f9dd462008-09-23 13:14:11 -0400316 return cache;
Chris Masonbe744172007-05-06 10:15:01 -0400317}
Chris Mason0b86a832008-03-24 15:01:56 -0400318
Yan Zhengd2fb3432008-12-11 16:30:39 -0500319static inline void put_block_group(struct btrfs_block_group_cache *cache)
320{
321 if (atomic_dec_and_test(&cache->count))
322 kfree(cache);
323}
324
Josef Bacik0f9dd462008-09-23 13:14:11 -0400325static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
326 u64 flags)
Chris Mason6324fbf2008-03-24 15:01:59 -0400327{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400328 struct list_head *head = &info->space_info;
329 struct list_head *cur;
330 struct btrfs_space_info *found;
331 list_for_each(cur, head) {
332 found = list_entry(cur, struct btrfs_space_info, list);
333 if (found->flags == flags)
334 return found;
335 }
336 return NULL;
Chris Mason6324fbf2008-03-24 15:01:59 -0400337}
338
Josef Bacik80eb2342008-10-29 14:49:05 -0400339static u64 div_factor(u64 num, int factor)
340{
341 if (factor == 10)
342 return num;
343 num *= factor;
344 do_div(num, 10);
345 return num;
346}
347
Yan Zhengd2fb3432008-12-11 16:30:39 -0500348u64 btrfs_find_block_group(struct btrfs_root *root,
349 u64 search_start, u64 search_hint, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400350{
Chris Mason96b51792007-10-15 16:15:19 -0400351 struct btrfs_block_group_cache *cache;
Chris Masoncd1bc462007-04-27 10:08:34 -0400352 u64 used;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500353 u64 last = max(search_hint, search_start);
354 u64 group_start = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400355 int full_search = 0;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500356 int factor = 9;
Chris Mason0ef3e662008-05-24 14:04:53 -0400357 int wrapped = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400358again:
Zheng Yane8569812008-09-26 10:05:48 -0400359 while (1) {
360 cache = btrfs_lookup_first_block_group(root->fs_info, last);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400361 if (!cache)
Chris Masoncd1bc462007-04-27 10:08:34 -0400362 break;
Chris Mason96b51792007-10-15 16:15:19 -0400363
Chris Masonc286ac42008-07-22 23:06:41 -0400364 spin_lock(&cache->lock);
Chris Mason96b51792007-10-15 16:15:19 -0400365 last = cache->key.objectid + cache->key.offset;
366 used = btrfs_block_group_used(&cache->item);
367
Yan Zhengd2fb3432008-12-11 16:30:39 -0500368 if ((full_search || !cache->ro) &&
369 block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) {
Zheng Yane8569812008-09-26 10:05:48 -0400370 if (used + cache->pinned + cache->reserved <
Yan Zhengd2fb3432008-12-11 16:30:39 -0500371 div_factor(cache->key.offset, factor)) {
372 group_start = cache->key.objectid;
Chris Masonc286ac42008-07-22 23:06:41 -0400373 spin_unlock(&cache->lock);
Yan Zhengd2fb3432008-12-11 16:30:39 -0500374 put_block_group(cache);
Chris Mason8790d502008-04-03 16:29:03 -0400375 goto found;
376 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400377 }
Chris Masonc286ac42008-07-22 23:06:41 -0400378 spin_unlock(&cache->lock);
Yan Zhengd2fb3432008-12-11 16:30:39 -0500379 put_block_group(cache);
Chris Masonde428b62007-05-18 13:28:27 -0400380 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400381 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400382 if (!wrapped) {
383 last = search_start;
384 wrapped = 1;
385 goto again;
386 }
387 if (!full_search && factor < 10) {
Chris Masonbe744172007-05-06 10:15:01 -0400388 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400389 full_search = 1;
Chris Mason0ef3e662008-05-24 14:04:53 -0400390 factor = 10;
Chris Mason31f3c992007-04-30 15:25:45 -0400391 goto again;
392 }
Chris Masonbe744172007-05-06 10:15:01 -0400393found:
Yan Zhengd2fb3432008-12-11 16:30:39 -0500394 return group_start;
Chris Mason925baed2008-06-25 16:01:30 -0400395}
Josef Bacik0f9dd462008-09-23 13:14:11 -0400396
Chris Masone02119d2008-09-05 16:13:11 -0400397/* simple helper to search for an existing extent at a given offset */
Zheng Yan31840ae2008-09-23 13:14:14 -0400398int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
Chris Masone02119d2008-09-05 16:13:11 -0400399{
400 int ret;
401 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400402 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -0400403
Zheng Yan31840ae2008-09-23 13:14:14 -0400404 path = btrfs_alloc_path();
405 BUG_ON(!path);
Chris Masone02119d2008-09-05 16:13:11 -0400406 key.objectid = start;
407 key.offset = len;
408 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
409 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
410 0, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400411 btrfs_free_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500412 return ret;
413}
414
Chris Masond8d5f3e2007-12-11 12:42:00 -0500415/*
416 * Back reference rules. Back refs have three main goals:
417 *
418 * 1) differentiate between all holders of references to an extent so that
419 * when a reference is dropped we can make sure it was a valid reference
420 * before freeing the extent.
421 *
422 * 2) Provide enough information to quickly find the holders of an extent
423 * if we notice a given block is corrupted or bad.
424 *
425 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
426 * maintenance. This is actually the same as #2, but with a slightly
427 * different use case.
428 *
429 * File extents can be referenced by:
430 *
431 * - multiple snapshots, subvolumes, or different generations in one subvol
Zheng Yan31840ae2008-09-23 13:14:14 -0400432 * - different files inside a single subvolume
Chris Masond8d5f3e2007-12-11 12:42:00 -0500433 * - different offsets inside a file (bookend extents in file.c)
434 *
435 * The extent ref structure has fields for:
436 *
437 * - Objectid of the subvolume root
438 * - Generation number of the tree holding the reference
439 * - objectid of the file holding the reference
Zheng Yan31840ae2008-09-23 13:14:14 -0400440 * - number of references holding by parent node (alway 1 for tree blocks)
441 *
442 * Btree leaf may hold multiple references to a file extent. In most cases,
443 * these references are from same file and the corresponding offsets inside
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400444 * the file are close together.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500445 *
446 * When a file extent is allocated the fields are filled in:
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400447 * (root_key.objectid, trans->transid, inode objectid, 1)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500448 *
449 * When a leaf is cow'd new references are added for every file extent found
Zheng Yan31840ae2008-09-23 13:14:14 -0400450 * in the leaf. It looks similar to the create case, but trans->transid will
451 * be different when the block is cow'd.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500452 *
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400453 * (root_key.objectid, trans->transid, inode objectid,
Zheng Yan31840ae2008-09-23 13:14:14 -0400454 * number of references in the leaf)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500455 *
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400456 * When a file extent is removed either during snapshot deletion or
457 * file truncation, we find the corresponding back reference and check
458 * the following fields:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500459 *
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400460 * (btrfs_header_owner(leaf), btrfs_header_generation(leaf),
461 * inode objectid)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500462 *
463 * Btree extents can be referenced by:
464 *
465 * - Different subvolumes
466 * - Different generations of the same subvolume
467 *
Chris Masond8d5f3e2007-12-11 12:42:00 -0500468 * When a tree block is created, back references are inserted:
469 *
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400470 * (root->root_key.objectid, trans->transid, level, 1)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500471 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400472 * When a tree block is cow'd, new back references are added for all the
473 * blocks it points to. If the tree block isn't in reference counted root,
474 * the old back references are removed. These new back references are of
475 * the form (trans->transid will have increased since creation):
Chris Masond8d5f3e2007-12-11 12:42:00 -0500476 *
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400477 * (root->root_key.objectid, trans->transid, level, 1)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500478 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400479 * When a backref is in deleting, the following fields are checked:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500480 *
481 * if backref was for a tree root:
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400482 * (btrfs_header_owner(itself), btrfs_header_generation(itself), level)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500483 * else
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400484 * (btrfs_header_owner(parent), btrfs_header_generation(parent), level)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500485 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400486 * Back Reference Key composing:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500487 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400488 * The key objectid corresponds to the first byte in the extent, the key
489 * type is set to BTRFS_EXTENT_REF_KEY, and the key offset is the first
490 * byte of parent extent. If a extent is tree root, the key offset is set
491 * to the key objectid.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500492 */
Zheng Yan31840ae2008-09-23 13:14:14 -0400493
494static int noinline lookup_extent_backref(struct btrfs_trans_handle *trans,
495 struct btrfs_root *root,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400496 struct btrfs_path *path,
497 u64 bytenr, u64 parent,
498 u64 ref_root, u64 ref_generation,
499 u64 owner_objectid, int del)
Chris Mason74493f72007-12-11 09:25:06 -0500500{
Chris Mason74493f72007-12-11 09:25:06 -0500501 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400502 struct btrfs_extent_ref *ref;
503 struct extent_buffer *leaf;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400504 u64 ref_objectid;
Chris Mason74493f72007-12-11 09:25:06 -0500505 int ret;
506
Chris Mason74493f72007-12-11 09:25:06 -0500507 key.objectid = bytenr;
508 key.type = BTRFS_EXTENT_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -0400509 key.offset = parent;
Chris Mason74493f72007-12-11 09:25:06 -0500510
Zheng Yan31840ae2008-09-23 13:14:14 -0400511 ret = btrfs_search_slot(trans, root, &key, path, del ? -1 : 0, 1);
512 if (ret < 0)
Chris Mason7bb86312007-12-11 09:25:06 -0500513 goto out;
Zheng Yan31840ae2008-09-23 13:14:14 -0400514 if (ret > 0) {
515 ret = -ENOENT;
516 goto out;
517 }
518
519 leaf = path->nodes[0];
520 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400521 ref_objectid = btrfs_ref_objectid(leaf, ref);
Zheng Yan31840ae2008-09-23 13:14:14 -0400522 if (btrfs_ref_root(leaf, ref) != ref_root ||
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400523 btrfs_ref_generation(leaf, ref) != ref_generation ||
524 (ref_objectid != owner_objectid &&
525 ref_objectid != BTRFS_MULTIPLE_OBJECTIDS)) {
Zheng Yan31840ae2008-09-23 13:14:14 -0400526 ret = -EIO;
527 WARN_ON(1);
528 goto out;
529 }
530 ret = 0;
531out:
532 return ret;
533}
534
Josef Bacikf3465ca2008-11-12 14:19:50 -0500535/*
536 * updates all the backrefs that are pending on update_list for the
537 * extent_root
538 */
539static int noinline update_backrefs(struct btrfs_trans_handle *trans,
540 struct btrfs_root *extent_root,
541 struct btrfs_path *path,
542 struct list_head *update_list)
543{
544 struct btrfs_key key;
545 struct btrfs_extent_ref *ref;
546 struct btrfs_fs_info *info = extent_root->fs_info;
547 struct pending_extent_op *op;
548 struct extent_buffer *leaf;
549 int ret = 0;
550 struct list_head *cur = update_list->next;
551 u64 ref_objectid;
552 u64 ref_root = extent_root->root_key.objectid;
553
554 op = list_entry(cur, struct pending_extent_op, list);
555
556search:
557 key.objectid = op->bytenr;
558 key.type = BTRFS_EXTENT_REF_KEY;
559 key.offset = op->orig_parent;
560
561 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 1);
562 BUG_ON(ret);
563
564 leaf = path->nodes[0];
565
566loop:
567 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
568
569 ref_objectid = btrfs_ref_objectid(leaf, ref);
570
571 if (btrfs_ref_root(leaf, ref) != ref_root ||
572 btrfs_ref_generation(leaf, ref) != op->orig_generation ||
573 (ref_objectid != op->level &&
574 ref_objectid != BTRFS_MULTIPLE_OBJECTIDS)) {
575 printk(KERN_ERR "couldn't find %Lu, parent %Lu, root %Lu, "
576 "owner %u\n", op->bytenr, op->orig_parent,
577 ref_root, op->level);
578 btrfs_print_leaf(extent_root, leaf);
579 BUG();
580 }
581
582 key.objectid = op->bytenr;
583 key.offset = op->parent;
584 key.type = BTRFS_EXTENT_REF_KEY;
585 ret = btrfs_set_item_key_safe(trans, extent_root, path, &key);
586 BUG_ON(ret);
587 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
588 btrfs_set_ref_generation(leaf, ref, op->generation);
589
590 cur = cur->next;
591
592 list_del_init(&op->list);
593 unlock_extent(&info->extent_ins, op->bytenr,
594 op->bytenr + op->num_bytes - 1, GFP_NOFS);
595 kfree(op);
596
597 if (cur == update_list) {
598 btrfs_mark_buffer_dirty(path->nodes[0]);
599 btrfs_release_path(extent_root, path);
600 goto out;
601 }
602
603 op = list_entry(cur, struct pending_extent_op, list);
604
605 path->slots[0]++;
606 while (path->slots[0] < btrfs_header_nritems(leaf)) {
607 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
608 if (key.objectid == op->bytenr &&
609 key.type == BTRFS_EXTENT_REF_KEY)
610 goto loop;
611 path->slots[0]++;
612 }
613
614 btrfs_mark_buffer_dirty(path->nodes[0]);
615 btrfs_release_path(extent_root, path);
616 goto search;
617
618out:
619 return 0;
620}
621
622static int noinline insert_extents(struct btrfs_trans_handle *trans,
623 struct btrfs_root *extent_root,
624 struct btrfs_path *path,
625 struct list_head *insert_list, int nr)
626{
627 struct btrfs_key *keys;
628 u32 *data_size;
629 struct pending_extent_op *op;
630 struct extent_buffer *leaf;
631 struct list_head *cur = insert_list->next;
632 struct btrfs_fs_info *info = extent_root->fs_info;
633 u64 ref_root = extent_root->root_key.objectid;
634 int i = 0, last = 0, ret;
635 int total = nr * 2;
636
637 if (!nr)
638 return 0;
639
640 keys = kzalloc(total * sizeof(struct btrfs_key), GFP_NOFS);
641 if (!keys)
642 return -ENOMEM;
643
644 data_size = kzalloc(total * sizeof(u32), GFP_NOFS);
645 if (!data_size) {
646 kfree(keys);
647 return -ENOMEM;
648 }
649
650 list_for_each_entry(op, insert_list, list) {
651 keys[i].objectid = op->bytenr;
652 keys[i].offset = op->num_bytes;
653 keys[i].type = BTRFS_EXTENT_ITEM_KEY;
654 data_size[i] = sizeof(struct btrfs_extent_item);
655 i++;
656
657 keys[i].objectid = op->bytenr;
658 keys[i].offset = op->parent;
659 keys[i].type = BTRFS_EXTENT_REF_KEY;
660 data_size[i] = sizeof(struct btrfs_extent_ref);
661 i++;
662 }
663
664 op = list_entry(cur, struct pending_extent_op, list);
665 i = 0;
666 while (i < total) {
667 int c;
668 ret = btrfs_insert_some_items(trans, extent_root, path,
669 keys+i, data_size+i, total-i);
670 BUG_ON(ret < 0);
671
672 if (last && ret > 1)
673 BUG();
674
675 leaf = path->nodes[0];
676 for (c = 0; c < ret; c++) {
677 int ref_first = keys[i].type == BTRFS_EXTENT_REF_KEY;
678
679 /*
680 * if the first item we inserted was a backref, then
681 * the EXTENT_ITEM will be the odd c's, else it will
682 * be the even c's
683 */
684 if ((ref_first && (c % 2)) ||
685 (!ref_first && !(c % 2))) {
686 struct btrfs_extent_item *itm;
687
688 itm = btrfs_item_ptr(leaf, path->slots[0] + c,
689 struct btrfs_extent_item);
690 btrfs_set_extent_refs(path->nodes[0], itm, 1);
691 op->del++;
692 } else {
693 struct btrfs_extent_ref *ref;
694
695 ref = btrfs_item_ptr(leaf, path->slots[0] + c,
696 struct btrfs_extent_ref);
697 btrfs_set_ref_root(leaf, ref, ref_root);
698 btrfs_set_ref_generation(leaf, ref,
699 op->generation);
700 btrfs_set_ref_objectid(leaf, ref, op->level);
701 btrfs_set_ref_num_refs(leaf, ref, 1);
702 op->del++;
703 }
704
705 /*
706 * using del to see when its ok to free up the
707 * pending_extent_op. In the case where we insert the
708 * last item on the list in order to help do batching
709 * we need to not free the extent op until we actually
710 * insert the extent_item
711 */
712 if (op->del == 2) {
713 unlock_extent(&info->extent_ins, op->bytenr,
714 op->bytenr + op->num_bytes - 1,
715 GFP_NOFS);
716 cur = cur->next;
717 list_del_init(&op->list);
718 kfree(op);
719 if (cur != insert_list)
720 op = list_entry(cur,
721 struct pending_extent_op,
722 list);
723 }
724 }
725 btrfs_mark_buffer_dirty(leaf);
726 btrfs_release_path(extent_root, path);
727
728 /*
729 * Ok backref's and items usually go right next to eachother,
730 * but if we could only insert 1 item that means that we
731 * inserted on the end of a leaf, and we have no idea what may
732 * be on the next leaf so we just play it safe. In order to
733 * try and help this case we insert the last thing on our
734 * insert list so hopefully it will end up being the last
735 * thing on the leaf and everything else will be before it,
736 * which will let us insert a whole bunch of items at the same
737 * time.
738 */
739 if (ret == 1 && !last && (i + ret < total)) {
740 /*
741 * last: where we will pick up the next time around
742 * i: our current key to insert, will be total - 1
743 * cur: the current op we are screwing with
744 * op: duh
745 */
746 last = i + ret;
747 i = total - 1;
748 cur = insert_list->prev;
749 op = list_entry(cur, struct pending_extent_op, list);
750 } else if (last) {
751 /*
752 * ok we successfully inserted the last item on the
753 * list, lets reset everything
754 *
755 * i: our current key to insert, so where we left off
756 * last time
757 * last: done with this
758 * cur: the op we are messing with
759 * op: duh
760 * total: since we inserted the last key, we need to
761 * decrement total so we dont overflow
762 */
763 i = last;
764 last = 0;
Josef Bacikf3465ca2008-11-12 14:19:50 -0500765 total--;
Liu Huib4eec2c2008-11-18 11:30:10 -0500766 if (i < total) {
767 cur = insert_list->next;
768 op = list_entry(cur, struct pending_extent_op,
769 list);
770 }
Josef Bacikf3465ca2008-11-12 14:19:50 -0500771 } else {
772 i += ret;
773 }
774
775 cond_resched();
776 }
777 ret = 0;
778 kfree(keys);
779 kfree(data_size);
780 return ret;
781}
782
Zheng Yan31840ae2008-09-23 13:14:14 -0400783static int noinline insert_extent_backref(struct btrfs_trans_handle *trans,
784 struct btrfs_root *root,
785 struct btrfs_path *path,
786 u64 bytenr, u64 parent,
787 u64 ref_root, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400788 u64 owner_objectid)
Zheng Yan31840ae2008-09-23 13:14:14 -0400789{
790 struct btrfs_key key;
791 struct extent_buffer *leaf;
792 struct btrfs_extent_ref *ref;
793 u32 num_refs;
794 int ret;
795
796 key.objectid = bytenr;
797 key.type = BTRFS_EXTENT_REF_KEY;
798 key.offset = parent;
799
800 ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(*ref));
801 if (ret == 0) {
802 leaf = path->nodes[0];
803 ref = btrfs_item_ptr(leaf, path->slots[0],
804 struct btrfs_extent_ref);
805 btrfs_set_ref_root(leaf, ref, ref_root);
806 btrfs_set_ref_generation(leaf, ref, ref_generation);
807 btrfs_set_ref_objectid(leaf, ref, owner_objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -0400808 btrfs_set_ref_num_refs(leaf, ref, 1);
809 } else if (ret == -EEXIST) {
810 u64 existing_owner;
811 BUG_ON(owner_objectid < BTRFS_FIRST_FREE_OBJECTID);
812 leaf = path->nodes[0];
813 ref = btrfs_item_ptr(leaf, path->slots[0],
814 struct btrfs_extent_ref);
815 if (btrfs_ref_root(leaf, ref) != ref_root ||
816 btrfs_ref_generation(leaf, ref) != ref_generation) {
817 ret = -EIO;
818 WARN_ON(1);
819 goto out;
820 }
821
822 num_refs = btrfs_ref_num_refs(leaf, ref);
823 BUG_ON(num_refs == 0);
824 btrfs_set_ref_num_refs(leaf, ref, num_refs + 1);
825
826 existing_owner = btrfs_ref_objectid(leaf, ref);
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400827 if (existing_owner != owner_objectid &&
828 existing_owner != BTRFS_MULTIPLE_OBJECTIDS) {
Zheng Yan31840ae2008-09-23 13:14:14 -0400829 btrfs_set_ref_objectid(leaf, ref,
830 BTRFS_MULTIPLE_OBJECTIDS);
Zheng Yan31840ae2008-09-23 13:14:14 -0400831 }
832 ret = 0;
833 } else {
834 goto out;
835 }
Chris Mason7bb86312007-12-11 09:25:06 -0500836 btrfs_mark_buffer_dirty(path->nodes[0]);
837out:
838 btrfs_release_path(root, path);
839 return ret;
Chris Mason74493f72007-12-11 09:25:06 -0500840}
841
Zheng Yan31840ae2008-09-23 13:14:14 -0400842static int noinline remove_extent_backref(struct btrfs_trans_handle *trans,
843 struct btrfs_root *root,
844 struct btrfs_path *path)
845{
846 struct extent_buffer *leaf;
847 struct btrfs_extent_ref *ref;
848 u32 num_refs;
849 int ret = 0;
850
851 leaf = path->nodes[0];
852 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
853 num_refs = btrfs_ref_num_refs(leaf, ref);
854 BUG_ON(num_refs == 0);
855 num_refs -= 1;
856 if (num_refs == 0) {
857 ret = btrfs_del_item(trans, root, path);
858 } else {
859 btrfs_set_ref_num_refs(leaf, ref, num_refs);
860 btrfs_mark_buffer_dirty(leaf);
861 }
862 btrfs_release_path(root, path);
863 return ret;
864}
865
Chris Mason4b4e25f2008-11-20 10:22:27 -0500866#ifdef BIO_RW_DISCARD
Chris Mason15916de2008-11-19 21:17:22 -0500867static void btrfs_issue_discard(struct block_device *bdev,
868 u64 start, u64 len)
869{
870#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,28)
871 blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_KERNEL);
872#else
873 blkdev_issue_discard(bdev, start >> 9, len >> 9);
874#endif
875}
Chris Mason4b4e25f2008-11-20 10:22:27 -0500876#endif
Chris Mason15916de2008-11-19 21:17:22 -0500877
Josef Bacikf3465ca2008-11-12 14:19:50 -0500878static int noinline free_extents(struct btrfs_trans_handle *trans,
879 struct btrfs_root *extent_root,
880 struct list_head *del_list)
881{
882 struct btrfs_fs_info *info = extent_root->fs_info;
883 struct btrfs_path *path;
884 struct btrfs_key key, found_key;
885 struct extent_buffer *leaf;
886 struct list_head *cur;
887 struct pending_extent_op *op;
888 struct btrfs_extent_item *ei;
889 int ret, num_to_del, extent_slot = 0, found_extent = 0;
890 u32 refs;
891 u64 bytes_freed = 0;
892
893 path = btrfs_alloc_path();
894 if (!path)
895 return -ENOMEM;
896 path->reada = 1;
897
898search:
899 /* search for the backref for the current ref we want to delete */
900 cur = del_list->next;
901 op = list_entry(cur, struct pending_extent_op, list);
902 ret = lookup_extent_backref(trans, extent_root, path, op->bytenr,
903 op->orig_parent,
904 extent_root->root_key.objectid,
905 op->orig_generation, op->level, 1);
906 if (ret) {
907 printk("Unable to find backref byte nr %Lu root %Lu gen %Lu "
908 "owner %u\n", op->bytenr,
909 extent_root->root_key.objectid, op->orig_generation,
910 op->level);
911 btrfs_print_leaf(extent_root, path->nodes[0]);
912 WARN_ON(1);
913 goto out;
914 }
915
916 extent_slot = path->slots[0];
917 num_to_del = 1;
918 found_extent = 0;
919
920 /*
921 * if we aren't the first item on the leaf we can move back one and see
922 * if our ref is right next to our extent item
923 */
924 if (likely(extent_slot)) {
925 extent_slot--;
926 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
927 extent_slot);
928 if (found_key.objectid == op->bytenr &&
929 found_key.type == BTRFS_EXTENT_ITEM_KEY &&
930 found_key.offset == op->num_bytes) {
931 num_to_del++;
932 found_extent = 1;
933 }
934 }
935
936 /*
937 * if we didn't find the extent we need to delete the backref and then
938 * search for the extent item key so we can update its ref count
939 */
940 if (!found_extent) {
941 key.objectid = op->bytenr;
942 key.type = BTRFS_EXTENT_ITEM_KEY;
943 key.offset = op->num_bytes;
944
945 ret = remove_extent_backref(trans, extent_root, path);
946 BUG_ON(ret);
947 btrfs_release_path(extent_root, path);
948 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
949 BUG_ON(ret);
950 extent_slot = path->slots[0];
951 }
952
953 /* this is where we update the ref count for the extent */
954 leaf = path->nodes[0];
955 ei = btrfs_item_ptr(leaf, extent_slot, struct btrfs_extent_item);
956 refs = btrfs_extent_refs(leaf, ei);
957 BUG_ON(refs == 0);
958 refs--;
959 btrfs_set_extent_refs(leaf, ei, refs);
960
961 btrfs_mark_buffer_dirty(leaf);
962
963 /*
964 * This extent needs deleting. The reason cur_slot is extent_slot +
965 * num_to_del is because extent_slot points to the slot where the extent
966 * is, and if the backref was not right next to the extent we will be
967 * deleting at least 1 item, and will want to start searching at the
968 * slot directly next to extent_slot. However if we did find the
969 * backref next to the extent item them we will be deleting at least 2
970 * items and will want to start searching directly after the ref slot
971 */
972 if (!refs) {
973 struct list_head *pos, *n, *end;
974 int cur_slot = extent_slot+num_to_del;
975 u64 super_used;
976 u64 root_used;
977
978 path->slots[0] = extent_slot;
979 bytes_freed = op->num_bytes;
980
Josef Bacike3e469f2008-11-17 21:11:49 -0500981 mutex_lock(&info->pinned_mutex);
982 ret = pin_down_bytes(trans, extent_root, op->bytenr,
983 op->num_bytes, op->level >=
984 BTRFS_FIRST_FREE_OBJECTID);
985 mutex_unlock(&info->pinned_mutex);
986 BUG_ON(ret < 0);
987 op->del = ret;
988
Josef Bacikf3465ca2008-11-12 14:19:50 -0500989 /*
990 * we need to see if we can delete multiple things at once, so
991 * start looping through the list of extents we are wanting to
992 * delete and see if their extent/backref's are right next to
993 * eachother and the extents only have 1 ref
994 */
995 for (pos = cur->next; pos != del_list; pos = pos->next) {
996 struct pending_extent_op *tmp;
997
998 tmp = list_entry(pos, struct pending_extent_op, list);
999
1000 /* we only want to delete extent+ref at this stage */
1001 if (cur_slot >= btrfs_header_nritems(leaf) - 1)
1002 break;
1003
1004 btrfs_item_key_to_cpu(leaf, &found_key, cur_slot);
1005 if (found_key.objectid != tmp->bytenr ||
1006 found_key.type != BTRFS_EXTENT_ITEM_KEY ||
1007 found_key.offset != tmp->num_bytes)
1008 break;
1009
1010 /* check to make sure this extent only has one ref */
1011 ei = btrfs_item_ptr(leaf, cur_slot,
1012 struct btrfs_extent_item);
1013 if (btrfs_extent_refs(leaf, ei) != 1)
1014 break;
1015
1016 btrfs_item_key_to_cpu(leaf, &found_key, cur_slot+1);
1017 if (found_key.objectid != tmp->bytenr ||
1018 found_key.type != BTRFS_EXTENT_REF_KEY ||
1019 found_key.offset != tmp->orig_parent)
1020 break;
1021
1022 /*
1023 * the ref is right next to the extent, we can set the
1024 * ref count to 0 since we will delete them both now
1025 */
1026 btrfs_set_extent_refs(leaf, ei, 0);
1027
1028 /* pin down the bytes for this extent */
1029 mutex_lock(&info->pinned_mutex);
1030 ret = pin_down_bytes(trans, extent_root, tmp->bytenr,
1031 tmp->num_bytes, tmp->level >=
1032 BTRFS_FIRST_FREE_OBJECTID);
1033 mutex_unlock(&info->pinned_mutex);
1034 BUG_ON(ret < 0);
1035
1036 /*
1037 * use the del field to tell if we need to go ahead and
1038 * free up the extent when we delete the item or not.
1039 */
1040 tmp->del = ret;
1041 bytes_freed += tmp->num_bytes;
1042
1043 num_to_del += 2;
1044 cur_slot += 2;
1045 }
1046 end = pos;
1047
1048 /* update the free space counters */
1049 spin_lock_irq(&info->delalloc_lock);
1050 super_used = btrfs_super_bytes_used(&info->super_copy);
1051 btrfs_set_super_bytes_used(&info->super_copy,
1052 super_used - bytes_freed);
1053 spin_unlock_irq(&info->delalloc_lock);
1054
1055 root_used = btrfs_root_used(&extent_root->root_item);
1056 btrfs_set_root_used(&extent_root->root_item,
1057 root_used - bytes_freed);
1058
1059 /* delete the items */
1060 ret = btrfs_del_items(trans, extent_root, path,
1061 path->slots[0], num_to_del);
1062 BUG_ON(ret);
1063
1064 /*
1065 * loop through the extents we deleted and do the cleanup work
1066 * on them
1067 */
1068 for (pos = cur, n = pos->next; pos != end;
1069 pos = n, n = pos->next) {
1070 struct pending_extent_op *tmp;
1071#ifdef BIO_RW_DISCARD
1072 u64 map_length;
1073 struct btrfs_multi_bio *multi = NULL;
1074#endif
1075 tmp = list_entry(pos, struct pending_extent_op, list);
1076
1077 /*
1078 * remember tmp->del tells us wether or not we pinned
1079 * down the extent
1080 */
1081 ret = update_block_group(trans, extent_root,
1082 tmp->bytenr, tmp->num_bytes, 0,
1083 tmp->del);
1084 BUG_ON(ret);
1085
1086#ifdef BIO_RW_DISCARD
Chris Mason15916de2008-11-19 21:17:22 -05001087 map_length = tmp->num_bytes;
Josef Bacikf3465ca2008-11-12 14:19:50 -05001088 ret = btrfs_map_block(&info->mapping_tree, READ,
1089 tmp->bytenr, &map_length, &multi,
1090 0);
1091 if (!ret) {
1092 struct btrfs_bio_stripe *stripe;
1093 int i;
1094
Chris Mason15916de2008-11-19 21:17:22 -05001095 stripe = multi->stripes;
Josef Bacikf3465ca2008-11-12 14:19:50 -05001096
1097 if (map_length > tmp->num_bytes)
1098 map_length = tmp->num_bytes;
1099
1100 for (i = 0; i < multi->num_stripes;
1101 i++, stripe++)
Chris Mason15916de2008-11-19 21:17:22 -05001102 btrfs_issue_discard(stripe->dev->bdev,
1103 stripe->physical,
1104 map_length);
Josef Bacikf3465ca2008-11-12 14:19:50 -05001105 kfree(multi);
1106 }
1107#endif
1108 list_del_init(&tmp->list);
1109 unlock_extent(&info->extent_ins, tmp->bytenr,
1110 tmp->bytenr + tmp->num_bytes - 1,
1111 GFP_NOFS);
1112 kfree(tmp);
1113 }
1114 } else if (refs && found_extent) {
1115 /*
1116 * the ref and extent were right next to eachother, but the
1117 * extent still has a ref, so just free the backref and keep
1118 * going
1119 */
1120 ret = remove_extent_backref(trans, extent_root, path);
1121 BUG_ON(ret);
1122
1123 list_del_init(&op->list);
1124 unlock_extent(&info->extent_ins, op->bytenr,
1125 op->bytenr + op->num_bytes - 1, GFP_NOFS);
1126 kfree(op);
1127 } else {
1128 /*
1129 * the extent has multiple refs and the backref we were looking
1130 * for was not right next to it, so just unlock and go next,
1131 * we're good to go
1132 */
1133 list_del_init(&op->list);
1134 unlock_extent(&info->extent_ins, op->bytenr,
1135 op->bytenr + op->num_bytes - 1, GFP_NOFS);
1136 kfree(op);
1137 }
1138
1139 btrfs_release_path(extent_root, path);
1140 if (!list_empty(del_list))
1141 goto search;
1142
1143out:
1144 btrfs_free_path(path);
1145 return ret;
1146}
1147
Zheng Yan31840ae2008-09-23 13:14:14 -04001148static int __btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
1149 struct btrfs_root *root, u64 bytenr,
1150 u64 orig_parent, u64 parent,
1151 u64 orig_root, u64 ref_root,
1152 u64 orig_generation, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001153 u64 owner_objectid)
Zheng Yan31840ae2008-09-23 13:14:14 -04001154{
1155 int ret;
1156 struct btrfs_root *extent_root = root->fs_info->extent_root;
1157 struct btrfs_path *path;
1158
1159 if (root == root->fs_info->extent_root) {
1160 struct pending_extent_op *extent_op;
1161 u64 num_bytes;
1162
1163 BUG_ON(owner_objectid >= BTRFS_MAX_LEVEL);
1164 num_bytes = btrfs_level_size(root, (int)owner_objectid);
Josef Bacik25179202008-10-29 14:49:05 -04001165 mutex_lock(&root->fs_info->extent_ins_mutex);
Zheng Yan31840ae2008-09-23 13:14:14 -04001166 if (test_range_bit(&root->fs_info->extent_ins, bytenr,
Josef Bacik25179202008-10-29 14:49:05 -04001167 bytenr + num_bytes - 1, EXTENT_WRITEBACK, 0)) {
Zheng Yan31840ae2008-09-23 13:14:14 -04001168 u64 priv;
1169 ret = get_state_private(&root->fs_info->extent_ins,
1170 bytenr, &priv);
1171 BUG_ON(ret);
1172 extent_op = (struct pending_extent_op *)
1173 (unsigned long)priv;
1174 BUG_ON(extent_op->parent != orig_parent);
1175 BUG_ON(extent_op->generation != orig_generation);
Josef Bacik25179202008-10-29 14:49:05 -04001176
Zheng Yan31840ae2008-09-23 13:14:14 -04001177 extent_op->parent = parent;
1178 extent_op->generation = ref_generation;
1179 } else {
1180 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
1181 BUG_ON(!extent_op);
1182
1183 extent_op->type = PENDING_BACKREF_UPDATE;
1184 extent_op->bytenr = bytenr;
1185 extent_op->num_bytes = num_bytes;
1186 extent_op->parent = parent;
1187 extent_op->orig_parent = orig_parent;
1188 extent_op->generation = ref_generation;
1189 extent_op->orig_generation = orig_generation;
1190 extent_op->level = (int)owner_objectid;
Josef Bacikf3465ca2008-11-12 14:19:50 -05001191 INIT_LIST_HEAD(&extent_op->list);
1192 extent_op->del = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001193
1194 set_extent_bits(&root->fs_info->extent_ins,
1195 bytenr, bytenr + num_bytes - 1,
Josef Bacik25179202008-10-29 14:49:05 -04001196 EXTENT_WRITEBACK, GFP_NOFS);
Zheng Yan31840ae2008-09-23 13:14:14 -04001197 set_state_private(&root->fs_info->extent_ins,
1198 bytenr, (unsigned long)extent_op);
1199 }
Josef Bacik25179202008-10-29 14:49:05 -04001200 mutex_unlock(&root->fs_info->extent_ins_mutex);
Zheng Yan31840ae2008-09-23 13:14:14 -04001201 return 0;
1202 }
1203
1204 path = btrfs_alloc_path();
1205 if (!path)
1206 return -ENOMEM;
1207 ret = lookup_extent_backref(trans, extent_root, path,
1208 bytenr, orig_parent, orig_root,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001209 orig_generation, owner_objectid, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001210 if (ret)
1211 goto out;
1212 ret = remove_extent_backref(trans, extent_root, path);
1213 if (ret)
1214 goto out;
1215 ret = insert_extent_backref(trans, extent_root, path, bytenr,
1216 parent, ref_root, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001217 owner_objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -04001218 BUG_ON(ret);
Chris Mason87ef2bb2008-10-30 11:23:27 -04001219 finish_current_insert(trans, extent_root, 0);
1220 del_pending_extents(trans, extent_root, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04001221out:
1222 btrfs_free_path(path);
1223 return ret;
1224}
1225
1226int btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
1227 struct btrfs_root *root, u64 bytenr,
1228 u64 orig_parent, u64 parent,
1229 u64 ref_root, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001230 u64 owner_objectid)
Zheng Yan31840ae2008-09-23 13:14:14 -04001231{
1232 int ret;
1233 if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
1234 owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
1235 return 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001236 ret = __btrfs_update_extent_ref(trans, root, bytenr, orig_parent,
1237 parent, ref_root, ref_root,
1238 ref_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001239 owner_objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -04001240 return ret;
1241}
1242
Chris Mason925baed2008-06-25 16:01:30 -04001243static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04001244 struct btrfs_root *root, u64 bytenr,
1245 u64 orig_parent, u64 parent,
1246 u64 orig_root, u64 ref_root,
1247 u64 orig_generation, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001248 u64 owner_objectid)
Chris Mason02217ed2007-03-02 16:08:05 -05001249{
Chris Mason5caf2a02007-04-02 11:20:42 -04001250 struct btrfs_path *path;
Chris Mason02217ed2007-03-02 16:08:05 -05001251 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -04001252 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001253 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -04001254 struct btrfs_extent_item *item;
Chris Masoncf27e1e2007-03-13 09:49:06 -04001255 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -05001256
Chris Mason5caf2a02007-04-02 11:20:42 -04001257 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001258 if (!path)
1259 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -04001260
Chris Mason3c12ac72008-04-21 12:01:38 -04001261 path->reada = 1;
Chris Masondb945352007-10-15 16:15:53 -04001262 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04001263 key.type = BTRFS_EXTENT_ITEM_KEY;
1264 key.offset = (u64)-1;
1265
Chris Mason5caf2a02007-04-02 11:20:42 -04001266 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -04001267 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001268 if (ret < 0)
1269 return ret;
Zheng Yan31840ae2008-09-23 13:14:14 -04001270 BUG_ON(ret == 0 || path->slots[0] == 0);
1271
1272 path->slots[0]--;
Chris Mason5f39d392007-10-15 16:14:19 -04001273 l = path->nodes[0];
Zheng Yan31840ae2008-09-23 13:14:14 -04001274
1275 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
Chris Mason771ed682008-11-06 22:02:51 -05001276 if (key.objectid != bytenr) {
1277 btrfs_print_leaf(root->fs_info->extent_root, path->nodes[0]);
1278 printk("wanted %Lu found %Lu\n", bytenr, key.objectid);
1279 BUG();
1280 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001281 BUG_ON(key.type != BTRFS_EXTENT_ITEM_KEY);
1282
Chris Mason5caf2a02007-04-02 11:20:42 -04001283 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001284 refs = btrfs_extent_refs(l, item);
1285 btrfs_set_extent_refs(l, item, refs + 1);
Chris Mason5caf2a02007-04-02 11:20:42 -04001286 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masona28ec192007-03-06 20:08:01 -05001287
Chris Mason5caf2a02007-04-02 11:20:42 -04001288 btrfs_release_path(root->fs_info->extent_root, path);
Chris Mason7bb86312007-12-11 09:25:06 -05001289
Chris Mason3c12ac72008-04-21 12:01:38 -04001290 path->reada = 1;
Zheng Yan31840ae2008-09-23 13:14:14 -04001291 ret = insert_extent_backref(trans, root->fs_info->extent_root,
1292 path, bytenr, parent,
1293 ref_root, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001294 owner_objectid);
Chris Mason7bb86312007-12-11 09:25:06 -05001295 BUG_ON(ret);
Chris Mason87ef2bb2008-10-30 11:23:27 -04001296 finish_current_insert(trans, root->fs_info->extent_root, 0);
1297 del_pending_extents(trans, root->fs_info->extent_root, 0);
Chris Mason74493f72007-12-11 09:25:06 -05001298
1299 btrfs_free_path(path);
Chris Mason02217ed2007-03-02 16:08:05 -05001300 return 0;
1301}
1302
Chris Mason925baed2008-06-25 16:01:30 -04001303int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04001304 struct btrfs_root *root,
1305 u64 bytenr, u64 num_bytes, u64 parent,
1306 u64 ref_root, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001307 u64 owner_objectid)
Chris Mason925baed2008-06-25 16:01:30 -04001308{
1309 int ret;
Zheng Yan31840ae2008-09-23 13:14:14 -04001310 if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
1311 owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
1312 return 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001313 ret = __btrfs_inc_extent_ref(trans, root, bytenr, 0, parent,
1314 0, ref_root, 0, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001315 owner_objectid);
Chris Mason925baed2008-06-25 16:01:30 -04001316 return ret;
1317}
1318
Chris Masone9d0b132007-08-10 14:06:19 -04001319int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
1320 struct btrfs_root *root)
1321{
Chris Mason87ef2bb2008-10-30 11:23:27 -04001322 finish_current_insert(trans, root->fs_info->extent_root, 1);
1323 del_pending_extents(trans, root->fs_info->extent_root, 1);
Chris Masone9d0b132007-08-10 14:06:19 -04001324 return 0;
1325}
1326
Zheng Yan31840ae2008-09-23 13:14:14 -04001327int btrfs_lookup_extent_ref(struct btrfs_trans_handle *trans,
1328 struct btrfs_root *root, u64 bytenr,
1329 u64 num_bytes, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -05001330{
Chris Mason5caf2a02007-04-02 11:20:42 -04001331 struct btrfs_path *path;
Chris Masona28ec192007-03-06 20:08:01 -05001332 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -04001333 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001334 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -04001335 struct btrfs_extent_item *item;
Chris Mason5caf2a02007-04-02 11:20:42 -04001336
Chris Masondb945352007-10-15 16:15:53 -04001337 WARN_ON(num_bytes < root->sectorsize);
Chris Mason5caf2a02007-04-02 11:20:42 -04001338 path = btrfs_alloc_path();
Chris Mason3c12ac72008-04-21 12:01:38 -04001339 path->reada = 1;
Chris Masondb945352007-10-15 16:15:53 -04001340 key.objectid = bytenr;
1341 key.offset = num_bytes;
Chris Mason62e27492007-03-15 12:56:47 -04001342 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5caf2a02007-04-02 11:20:42 -04001343 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -04001344 0, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001345 if (ret < 0)
1346 goto out;
Chris Mason5f39d392007-10-15 16:14:19 -04001347 if (ret != 0) {
1348 btrfs_print_leaf(root, path->nodes[0]);
Chris Masondb945352007-10-15 16:15:53 -04001349 printk("failed to find block number %Lu\n", bytenr);
Chris Masona28ec192007-03-06 20:08:01 -05001350 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04001351 }
1352 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -04001353 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001354 *refs = btrfs_extent_refs(l, item);
Chris Mason54aa1f42007-06-22 14:16:25 -04001355out:
Chris Mason5caf2a02007-04-02 11:20:42 -04001356 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05001357 return 0;
1358}
1359
Yan Zheng80ff3852008-10-30 14:20:02 -04001360int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
1361 struct btrfs_root *root, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05001362{
1363 struct btrfs_root *extent_root = root->fs_info->extent_root;
1364 struct btrfs_path *path;
Yan Zhengf321e492008-07-30 09:26:11 -04001365 struct extent_buffer *leaf;
1366 struct btrfs_extent_ref *ref_item;
Chris Masonbe20aa92007-12-17 20:14:01 -05001367 struct btrfs_key key;
1368 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001369 u64 ref_root;
1370 u64 last_snapshot;
Yan Zhengf321e492008-07-30 09:26:11 -04001371 u32 nritems;
1372 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001373
Chris Masonbe20aa92007-12-17 20:14:01 -05001374 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04001375 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04001376 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05001377
Yan Zhengf321e492008-07-30 09:26:11 -04001378 path = btrfs_alloc_path();
Chris Masonbe20aa92007-12-17 20:14:01 -05001379 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
1380 if (ret < 0)
1381 goto out;
1382 BUG_ON(ret == 0);
Yan Zheng80ff3852008-10-30 14:20:02 -04001383
1384 ret = -ENOENT;
1385 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04001386 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05001387
Zheng Yan31840ae2008-09-23 13:14:14 -04001388 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04001389 leaf = path->nodes[0];
1390 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001391
1392 if (found_key.objectid != bytenr ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001393 found_key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05001394 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05001395
Yan Zheng80ff3852008-10-30 14:20:02 -04001396 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Chris Masonbe20aa92007-12-17 20:14:01 -05001397 while (1) {
Yan Zhengf321e492008-07-30 09:26:11 -04001398 leaf = path->nodes[0];
1399 nritems = btrfs_header_nritems(leaf);
Chris Masonbe20aa92007-12-17 20:14:01 -05001400 if (path->slots[0] >= nritems) {
1401 ret = btrfs_next_leaf(extent_root, path);
Yan Zhengf321e492008-07-30 09:26:11 -04001402 if (ret < 0)
1403 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05001404 if (ret == 0)
1405 continue;
1406 break;
1407 }
Yan Zhengf321e492008-07-30 09:26:11 -04001408 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001409 if (found_key.objectid != bytenr)
1410 break;
Chris Masonbd098352008-01-03 13:23:19 -05001411
Chris Masonbe20aa92007-12-17 20:14:01 -05001412 if (found_key.type != BTRFS_EXTENT_REF_KEY) {
1413 path->slots[0]++;
1414 continue;
1415 }
1416
Yan Zhengf321e492008-07-30 09:26:11 -04001417 ref_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masonbe20aa92007-12-17 20:14:01 -05001418 struct btrfs_extent_ref);
Yan Zheng80ff3852008-10-30 14:20:02 -04001419 ref_root = btrfs_ref_root(leaf, ref_item);
1420 if (ref_root != root->root_key.objectid &&
1421 ref_root != BTRFS_TREE_LOG_OBJECTID) {
1422 ret = 1;
1423 goto out;
Yan Zhengf321e492008-07-30 09:26:11 -04001424 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001425 if (btrfs_ref_generation(leaf, ref_item) <= last_snapshot) {
1426 ret = 1;
1427 goto out;
1428 }
Yan Zhengf321e492008-07-30 09:26:11 -04001429
Chris Masonbe20aa92007-12-17 20:14:01 -05001430 path->slots[0]++;
1431 }
Yan Zhengf321e492008-07-30 09:26:11 -04001432 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001433out:
Yan Zhengf321e492008-07-30 09:26:11 -04001434 btrfs_free_path(path);
1435 return ret;
1436}
1437
Zheng Yan31840ae2008-09-23 13:14:14 -04001438int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1439 struct extent_buffer *buf, u32 nr_extents)
Chris Mason02217ed2007-03-02 16:08:05 -05001440{
Chris Mason5f39d392007-10-15 16:14:19 -04001441 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04001442 struct btrfs_file_extent_item *fi;
Zheng Yane4657682008-09-26 10:04:53 -04001443 u64 root_gen;
1444 u32 nritems;
Chris Mason02217ed2007-03-02 16:08:05 -05001445 int i;
Chris Masondb945352007-10-15 16:15:53 -04001446 int level;
Zheng Yan31840ae2008-09-23 13:14:14 -04001447 int ret = 0;
Zheng Yane4657682008-09-26 10:04:53 -04001448 int shared = 0;
Chris Masona28ec192007-03-06 20:08:01 -05001449
Chris Mason3768f362007-03-13 16:47:54 -04001450 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -05001451 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001452
Zheng Yane4657682008-09-26 10:04:53 -04001453 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
1454 shared = 0;
1455 root_gen = root->root_key.offset;
1456 } else {
1457 shared = 1;
1458 root_gen = trans->transid - 1;
1459 }
1460
Chris Masondb945352007-10-15 16:15:53 -04001461 level = btrfs_header_level(buf);
Chris Mason5f39d392007-10-15 16:14:19 -04001462 nritems = btrfs_header_nritems(buf);
Chris Mason4a096752008-07-21 10:29:44 -04001463
Zheng Yan31840ae2008-09-23 13:14:14 -04001464 if (level == 0) {
Yan Zheng31153d82008-07-28 15:32:19 -04001465 struct btrfs_leaf_ref *ref;
1466 struct btrfs_extent_info *info;
1467
Zheng Yan31840ae2008-09-23 13:14:14 -04001468 ref = btrfs_alloc_leaf_ref(root, nr_extents);
Yan Zheng31153d82008-07-28 15:32:19 -04001469 if (!ref) {
Zheng Yan31840ae2008-09-23 13:14:14 -04001470 ret = -ENOMEM;
Yan Zheng31153d82008-07-28 15:32:19 -04001471 goto out;
1472 }
1473
Zheng Yane4657682008-09-26 10:04:53 -04001474 ref->root_gen = root_gen;
Yan Zheng31153d82008-07-28 15:32:19 -04001475 ref->bytenr = buf->start;
1476 ref->owner = btrfs_header_owner(buf);
1477 ref->generation = btrfs_header_generation(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04001478 ref->nritems = nr_extents;
Yan Zheng31153d82008-07-28 15:32:19 -04001479 info = ref->extents;
Yanbcc63ab2008-07-30 16:29:20 -04001480
Zheng Yan31840ae2008-09-23 13:14:14 -04001481 for (i = 0; nr_extents > 0 && i < nritems; i++) {
Yan Zheng31153d82008-07-28 15:32:19 -04001482 u64 disk_bytenr;
1483 btrfs_item_key_to_cpu(buf, &key, i);
1484 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1485 continue;
1486 fi = btrfs_item_ptr(buf, i,
1487 struct btrfs_file_extent_item);
1488 if (btrfs_file_extent_type(buf, fi) ==
1489 BTRFS_FILE_EXTENT_INLINE)
1490 continue;
1491 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1492 if (disk_bytenr == 0)
1493 continue;
1494
1495 info->bytenr = disk_bytenr;
1496 info->num_bytes =
1497 btrfs_file_extent_disk_num_bytes(buf, fi);
1498 info->objectid = key.objectid;
1499 info->offset = key.offset;
1500 info++;
1501 }
1502
Zheng Yane4657682008-09-26 10:04:53 -04001503 ret = btrfs_add_leaf_ref(root, ref, shared);
Yan Zheng5b84e8d2008-10-09 11:46:19 -04001504 if (ret == -EEXIST && shared) {
1505 struct btrfs_leaf_ref *old;
1506 old = btrfs_lookup_leaf_ref(root, ref->bytenr);
1507 BUG_ON(!old);
1508 btrfs_remove_leaf_ref(root, old);
1509 btrfs_free_leaf_ref(root, old);
1510 ret = btrfs_add_leaf_ref(root, ref, shared);
1511 }
Yan Zheng31153d82008-07-28 15:32:19 -04001512 WARN_ON(ret);
Yanbcc63ab2008-07-30 16:29:20 -04001513 btrfs_free_leaf_ref(root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04001514 }
1515out:
Zheng Yan31840ae2008-09-23 13:14:14 -04001516 return ret;
1517}
1518
1519int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1520 struct extent_buffer *orig_buf, struct extent_buffer *buf,
1521 u32 *nr_extents)
1522{
1523 u64 bytenr;
1524 u64 ref_root;
1525 u64 orig_root;
1526 u64 ref_generation;
1527 u64 orig_generation;
1528 u32 nritems;
1529 u32 nr_file_extents = 0;
1530 struct btrfs_key key;
1531 struct btrfs_file_extent_item *fi;
1532 int i;
1533 int level;
1534 int ret = 0;
1535 int faili = 0;
1536 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001537 u64, u64, u64, u64, u64, u64, u64, u64);
Zheng Yan31840ae2008-09-23 13:14:14 -04001538
1539 ref_root = btrfs_header_owner(buf);
1540 ref_generation = btrfs_header_generation(buf);
1541 orig_root = btrfs_header_owner(orig_buf);
1542 orig_generation = btrfs_header_generation(orig_buf);
1543
1544 nritems = btrfs_header_nritems(buf);
1545 level = btrfs_header_level(buf);
1546
1547 if (root->ref_cows) {
1548 process_func = __btrfs_inc_extent_ref;
1549 } else {
1550 if (level == 0 &&
1551 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1552 goto out;
1553 if (level != 0 &&
1554 root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
1555 goto out;
1556 process_func = __btrfs_update_extent_ref;
1557 }
1558
1559 for (i = 0; i < nritems; i++) {
1560 cond_resched();
Chris Masondb945352007-10-15 16:15:53 -04001561 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001562 btrfs_item_key_to_cpu(buf, &key, i);
1563 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04001564 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001565 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04001566 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001567 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04001568 BTRFS_FILE_EXTENT_INLINE)
1569 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04001570 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1571 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04001572 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04001573
1574 nr_file_extents++;
1575
Zheng Yan31840ae2008-09-23 13:14:14 -04001576 ret = process_func(trans, root, bytenr,
1577 orig_buf->start, buf->start,
1578 orig_root, ref_root,
1579 orig_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001580 key.objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -04001581
1582 if (ret) {
1583 faili = i;
1584 WARN_ON(1);
1585 goto fail;
1586 }
Chris Mason54aa1f42007-06-22 14:16:25 -04001587 } else {
Chris Masondb945352007-10-15 16:15:53 -04001588 bytenr = btrfs_node_blockptr(buf, i);
Zheng Yan31840ae2008-09-23 13:14:14 -04001589 ret = process_func(trans, root, bytenr,
1590 orig_buf->start, buf->start,
1591 orig_root, ref_root,
1592 orig_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001593 level - 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001594 if (ret) {
1595 faili = i;
1596 WARN_ON(1);
1597 goto fail;
1598 }
Chris Mason54aa1f42007-06-22 14:16:25 -04001599 }
1600 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001601out:
1602 if (nr_extents) {
1603 if (level == 0)
1604 *nr_extents = nr_file_extents;
1605 else
1606 *nr_extents = nritems;
1607 }
1608 return 0;
1609fail:
1610 WARN_ON(1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001611 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05001612}
1613
Zheng Yan31840ae2008-09-23 13:14:14 -04001614int btrfs_update_ref(struct btrfs_trans_handle *trans,
1615 struct btrfs_root *root, struct extent_buffer *orig_buf,
1616 struct extent_buffer *buf, int start_slot, int nr)
1617
1618{
1619 u64 bytenr;
1620 u64 ref_root;
1621 u64 orig_root;
1622 u64 ref_generation;
1623 u64 orig_generation;
1624 struct btrfs_key key;
1625 struct btrfs_file_extent_item *fi;
1626 int i;
1627 int ret;
1628 int slot;
1629 int level;
1630
1631 BUG_ON(start_slot < 0);
1632 BUG_ON(start_slot + nr > btrfs_header_nritems(buf));
1633
1634 ref_root = btrfs_header_owner(buf);
1635 ref_generation = btrfs_header_generation(buf);
1636 orig_root = btrfs_header_owner(orig_buf);
1637 orig_generation = btrfs_header_generation(orig_buf);
1638 level = btrfs_header_level(buf);
1639
1640 if (!root->ref_cows) {
1641 if (level == 0 &&
1642 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1643 return 0;
1644 if (level != 0 &&
1645 root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
1646 return 0;
1647 }
1648
1649 for (i = 0, slot = start_slot; i < nr; i++, slot++) {
1650 cond_resched();
1651 if (level == 0) {
1652 btrfs_item_key_to_cpu(buf, &key, slot);
1653 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1654 continue;
1655 fi = btrfs_item_ptr(buf, slot,
1656 struct btrfs_file_extent_item);
1657 if (btrfs_file_extent_type(buf, fi) ==
1658 BTRFS_FILE_EXTENT_INLINE)
1659 continue;
1660 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1661 if (bytenr == 0)
1662 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04001663 ret = __btrfs_update_extent_ref(trans, root, bytenr,
1664 orig_buf->start, buf->start,
1665 orig_root, ref_root,
1666 orig_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001667 key.objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -04001668 if (ret)
1669 goto fail;
1670 } else {
1671 bytenr = btrfs_node_blockptr(buf, slot);
Zheng Yan31840ae2008-09-23 13:14:14 -04001672 ret = __btrfs_update_extent_ref(trans, root, bytenr,
1673 orig_buf->start, buf->start,
1674 orig_root, ref_root,
1675 orig_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001676 level - 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001677 if (ret)
1678 goto fail;
1679 }
1680 }
1681 return 0;
1682fail:
1683 WARN_ON(1);
1684 return -1;
1685}
1686
Chris Mason9078a3e2007-04-26 16:46:15 -04001687static int write_one_cache_group(struct btrfs_trans_handle *trans,
1688 struct btrfs_root *root,
1689 struct btrfs_path *path,
1690 struct btrfs_block_group_cache *cache)
1691{
1692 int ret;
1693 int pending_ret;
1694 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04001695 unsigned long bi;
1696 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04001697
Chris Mason9078a3e2007-04-26 16:46:15 -04001698 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001699 if (ret < 0)
1700 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -04001701 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001702
1703 leaf = path->nodes[0];
1704 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
1705 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
1706 btrfs_mark_buffer_dirty(leaf);
Chris Mason9078a3e2007-04-26 16:46:15 -04001707 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -04001708fail:
Chris Mason87ef2bb2008-10-30 11:23:27 -04001709 finish_current_insert(trans, extent_root, 0);
1710 pending_ret = del_pending_extents(trans, extent_root, 0);
Chris Mason9078a3e2007-04-26 16:46:15 -04001711 if (ret)
1712 return ret;
1713 if (pending_ret)
1714 return pending_ret;
1715 return 0;
1716
1717}
1718
Chris Mason96b51792007-10-15 16:15:19 -04001719int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
1720 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04001721{
Josef Bacik0f9dd462008-09-23 13:14:11 -04001722 struct btrfs_block_group_cache *cache, *entry;
1723 struct rb_node *n;
Chris Mason9078a3e2007-04-26 16:46:15 -04001724 int err = 0;
1725 int werr = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04001726 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04001727 u64 last = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04001728
1729 path = btrfs_alloc_path();
1730 if (!path)
1731 return -ENOMEM;
1732
1733 while(1) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001734 cache = NULL;
1735 spin_lock(&root->fs_info->block_group_cache_lock);
1736 for (n = rb_first(&root->fs_info->block_group_cache_tree);
1737 n; n = rb_next(n)) {
1738 entry = rb_entry(n, struct btrfs_block_group_cache,
1739 cache_node);
1740 if (entry->dirty) {
1741 cache = entry;
1742 break;
1743 }
1744 }
1745 spin_unlock(&root->fs_info->block_group_cache_lock);
1746
1747 if (!cache)
Chris Mason9078a3e2007-04-26 16:46:15 -04001748 break;
Chris Mason54aa1f42007-06-22 14:16:25 -04001749
Zheng Yane8569812008-09-26 10:05:48 -04001750 cache->dirty = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001751 last += cache->key.offset;
1752
Chris Mason96b51792007-10-15 16:15:19 -04001753 err = write_one_cache_group(trans, root,
1754 path, cache);
1755 /*
1756 * if we fail to write the cache group, we want
1757 * to keep it marked dirty in hopes that a later
1758 * write will work
1759 */
1760 if (err) {
1761 werr = err;
1762 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04001763 }
1764 }
1765 btrfs_free_path(path);
1766 return werr;
1767}
1768
Yan Zhengd2fb3432008-12-11 16:30:39 -05001769int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
1770{
1771 struct btrfs_block_group_cache *block_group;
1772 int readonly = 0;
1773
1774 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
1775 if (!block_group || block_group->ro)
1776 readonly = 1;
1777 if (block_group)
1778 put_block_group(block_group);
1779 return readonly;
1780}
1781
Chris Mason593060d2008-03-25 16:50:33 -04001782static int update_space_info(struct btrfs_fs_info *info, u64 flags,
1783 u64 total_bytes, u64 bytes_used,
1784 struct btrfs_space_info **space_info)
1785{
1786 struct btrfs_space_info *found;
1787
1788 found = __find_space_info(info, flags);
1789 if (found) {
Josef Bacik25179202008-10-29 14:49:05 -04001790 spin_lock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04001791 found->total_bytes += total_bytes;
1792 found->bytes_used += bytes_used;
Chris Mason8f18cf12008-04-25 16:53:30 -04001793 found->full = 0;
Josef Bacik25179202008-10-29 14:49:05 -04001794 spin_unlock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04001795 *space_info = found;
1796 return 0;
1797 }
Yan Zhengc146afa2008-11-12 14:34:12 -05001798 found = kzalloc(sizeof(*found), GFP_NOFS);
Chris Mason593060d2008-03-25 16:50:33 -04001799 if (!found)
1800 return -ENOMEM;
1801
1802 list_add(&found->list, &info->space_info);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001803 INIT_LIST_HEAD(&found->block_groups);
Josef Bacik80eb2342008-10-29 14:49:05 -04001804 init_rwsem(&found->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001805 spin_lock_init(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04001806 found->flags = flags;
1807 found->total_bytes = total_bytes;
1808 found->bytes_used = bytes_used;
1809 found->bytes_pinned = 0;
Zheng Yane8569812008-09-26 10:05:48 -04001810 found->bytes_reserved = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05001811 found->bytes_readonly = 0;
Chris Mason593060d2008-03-25 16:50:33 -04001812 found->full = 0;
Chris Mason0ef3e662008-05-24 14:04:53 -04001813 found->force_alloc = 0;
Chris Mason593060d2008-03-25 16:50:33 -04001814 *space_info = found;
1815 return 0;
1816}
1817
Chris Mason8790d502008-04-03 16:29:03 -04001818static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
1819{
1820 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
Chris Mason611f0e02008-04-03 16:29:03 -04001821 BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04001822 BTRFS_BLOCK_GROUP_RAID10 |
Chris Mason611f0e02008-04-03 16:29:03 -04001823 BTRFS_BLOCK_GROUP_DUP);
Chris Mason8790d502008-04-03 16:29:03 -04001824 if (extra_flags) {
1825 if (flags & BTRFS_BLOCK_GROUP_DATA)
1826 fs_info->avail_data_alloc_bits |= extra_flags;
1827 if (flags & BTRFS_BLOCK_GROUP_METADATA)
1828 fs_info->avail_metadata_alloc_bits |= extra_flags;
1829 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
1830 fs_info->avail_system_alloc_bits |= extra_flags;
1831 }
1832}
Chris Mason593060d2008-03-25 16:50:33 -04001833
Yan Zhengc146afa2008-11-12 14:34:12 -05001834static void set_block_group_readonly(struct btrfs_block_group_cache *cache)
1835{
1836 spin_lock(&cache->space_info->lock);
1837 spin_lock(&cache->lock);
1838 if (!cache->ro) {
1839 cache->space_info->bytes_readonly += cache->key.offset -
1840 btrfs_block_group_used(&cache->item);
1841 cache->ro = 1;
1842 }
1843 spin_unlock(&cache->lock);
1844 spin_unlock(&cache->space_info->lock);
1845}
1846
Yan Zheng2b820322008-11-17 21:11:30 -05001847u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04001848{
Yan Zheng2b820322008-11-17 21:11:30 -05001849 u64 num_devices = root->fs_info->fs_devices->rw_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001850
1851 if (num_devices == 1)
1852 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
1853 if (num_devices < 4)
1854 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
1855
Chris Masonec44a352008-04-28 15:29:52 -04001856 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
1857 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
Chris Masona061fc82008-05-07 11:43:44 -04001858 BTRFS_BLOCK_GROUP_RAID10))) {
Chris Masonec44a352008-04-28 15:29:52 -04001859 flags &= ~BTRFS_BLOCK_GROUP_DUP;
Chris Masona061fc82008-05-07 11:43:44 -04001860 }
Chris Masonec44a352008-04-28 15:29:52 -04001861
1862 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
Chris Masona061fc82008-05-07 11:43:44 -04001863 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
Chris Masonec44a352008-04-28 15:29:52 -04001864 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
Chris Masona061fc82008-05-07 11:43:44 -04001865 }
Chris Masonec44a352008-04-28 15:29:52 -04001866
1867 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
1868 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
1869 (flags & BTRFS_BLOCK_GROUP_RAID10) |
1870 (flags & BTRFS_BLOCK_GROUP_DUP)))
1871 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
1872 return flags;
1873}
1874
Chris Mason6324fbf2008-03-24 15:01:59 -04001875static int do_chunk_alloc(struct btrfs_trans_handle *trans,
1876 struct btrfs_root *extent_root, u64 alloc_bytes,
Chris Mason0ef3e662008-05-24 14:04:53 -04001877 u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04001878{
1879 struct btrfs_space_info *space_info;
1880 u64 thresh;
Yan Zhengc146afa2008-11-12 14:34:12 -05001881 int ret = 0;
1882
1883 mutex_lock(&extent_root->fs_info->chunk_mutex);
Chris Mason6324fbf2008-03-24 15:01:59 -04001884
Yan Zheng2b820322008-11-17 21:11:30 -05001885 flags = btrfs_reduce_alloc_profile(extent_root, flags);
Chris Masonec44a352008-04-28 15:29:52 -04001886
Chris Mason6324fbf2008-03-24 15:01:59 -04001887 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04001888 if (!space_info) {
1889 ret = update_space_info(extent_root->fs_info, flags,
1890 0, 0, &space_info);
1891 BUG_ON(ret);
1892 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001893 BUG_ON(!space_info);
1894
Josef Bacik25179202008-10-29 14:49:05 -04001895 spin_lock(&space_info->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04001896 if (space_info->force_alloc) {
1897 force = 1;
1898 space_info->force_alloc = 0;
1899 }
Josef Bacik25179202008-10-29 14:49:05 -04001900 if (space_info->full) {
1901 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04001902 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04001903 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001904
Yan Zhengc146afa2008-11-12 14:34:12 -05001905 thresh = space_info->total_bytes - space_info->bytes_readonly;
1906 thresh = div_factor(thresh, 6);
Chris Mason0ef3e662008-05-24 14:04:53 -04001907 if (!force &&
Zheng Yane8569812008-09-26 10:05:48 -04001908 (space_info->bytes_used + space_info->bytes_pinned +
Josef Bacik25179202008-10-29 14:49:05 -04001909 space_info->bytes_reserved + alloc_bytes) < thresh) {
1910 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04001911 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04001912 }
Josef Bacik25179202008-10-29 14:49:05 -04001913 spin_unlock(&space_info->lock);
1914
Yan Zheng2b820322008-11-17 21:11:30 -05001915 ret = btrfs_alloc_chunk(trans, extent_root, flags);
Josef Bacik25179202008-10-29 14:49:05 -04001916 if (ret) {
Chris Mason6324fbf2008-03-24 15:01:59 -04001917printk("space info full %Lu\n", flags);
1918 space_info->full = 1;
Chris Mason6324fbf2008-03-24 15:01:59 -04001919 }
Chris Masona74a4b92008-06-25 16:01:31 -04001920out:
Yan Zhengc146afa2008-11-12 14:34:12 -05001921 mutex_unlock(&extent_root->fs_info->chunk_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001922 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04001923}
1924
Chris Mason9078a3e2007-04-26 16:46:15 -04001925static int update_block_group(struct btrfs_trans_handle *trans,
1926 struct btrfs_root *root,
Chris Masondb945352007-10-15 16:15:53 -04001927 u64 bytenr, u64 num_bytes, int alloc,
Chris Mason0b86a832008-03-24 15:01:56 -04001928 int mark_free)
Chris Mason9078a3e2007-04-26 16:46:15 -04001929{
1930 struct btrfs_block_group_cache *cache;
1931 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04001932 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04001933 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04001934 u64 byte_in_group;
Chris Mason3e1ad542007-05-07 20:03:49 -04001935
Chris Mason9078a3e2007-04-26 16:46:15 -04001936 while(total) {
Chris Masondb945352007-10-15 16:15:53 -04001937 cache = btrfs_lookup_block_group(info, bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05001938 if (!cache)
Chris Mason9078a3e2007-04-26 16:46:15 -04001939 return -1;
Chris Masondb945352007-10-15 16:15:53 -04001940 byte_in_group = bytenr - cache->key.objectid;
1941 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04001942
Josef Bacik25179202008-10-29 14:49:05 -04001943 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04001944 spin_lock(&cache->lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001945 cache->dirty = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04001946 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04001947 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04001948 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04001949 old_val += num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04001950 cache->space_info->bytes_used += num_bytes;
Yan Zhenga512bbf2008-12-08 16:46:26 -05001951 if (cache->ro)
Yan Zhengc146afa2008-11-12 14:34:12 -05001952 cache->space_info->bytes_readonly -= num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04001953 btrfs_set_block_group_used(&cache->item, old_val);
1954 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04001955 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04001956 } else {
Chris Masondb945352007-10-15 16:15:53 -04001957 old_val -= num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04001958 cache->space_info->bytes_used -= num_bytes;
Yan Zhengc146afa2008-11-12 14:34:12 -05001959 if (cache->ro)
1960 cache->space_info->bytes_readonly += num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04001961 btrfs_set_block_group_used(&cache->item, old_val);
1962 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04001963 spin_unlock(&cache->space_info->lock);
Chris Masonf510cfe2007-10-15 16:14:48 -04001964 if (mark_free) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001965 int ret;
1966 ret = btrfs_add_free_space(cache, bytenr,
1967 num_bytes);
Yan Zhengd2fb3432008-12-11 16:30:39 -05001968 WARN_ON(ret);
Chris Masone37c9e62007-05-09 20:13:14 -04001969 }
Chris Masoncd1bc462007-04-27 10:08:34 -04001970 }
Yan Zhengd2fb3432008-12-11 16:30:39 -05001971 put_block_group(cache);
Chris Masondb945352007-10-15 16:15:53 -04001972 total -= num_bytes;
1973 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04001974 }
1975 return 0;
1976}
Chris Mason6324fbf2008-03-24 15:01:59 -04001977
Chris Masona061fc82008-05-07 11:43:44 -04001978static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
1979{
Josef Bacik0f9dd462008-09-23 13:14:11 -04001980 struct btrfs_block_group_cache *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001981 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001982
1983 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
1984 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04001985 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001986
Yan Zhengd2fb3432008-12-11 16:30:39 -05001987 bytenr = cache->key.objectid;
1988 put_block_group(cache);
1989
1990 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04001991}
1992
Chris Masone02119d2008-09-05 16:13:11 -04001993int btrfs_update_pinned_extents(struct btrfs_root *root,
Yan324ae4d2007-11-16 14:57:08 -05001994 u64 bytenr, u64 num, int pin)
1995{
1996 u64 len;
1997 struct btrfs_block_group_cache *cache;
1998 struct btrfs_fs_info *fs_info = root->fs_info;
1999
Josef Bacik25179202008-10-29 14:49:05 -04002000 WARN_ON(!mutex_is_locked(&root->fs_info->pinned_mutex));
Yan324ae4d2007-11-16 14:57:08 -05002001 if (pin) {
2002 set_extent_dirty(&fs_info->pinned_extents,
2003 bytenr, bytenr + num - 1, GFP_NOFS);
2004 } else {
2005 clear_extent_dirty(&fs_info->pinned_extents,
2006 bytenr, bytenr + num - 1, GFP_NOFS);
2007 }
2008 while (num > 0) {
2009 cache = btrfs_lookup_block_group(fs_info, bytenr);
Zheng Yane8569812008-09-26 10:05:48 -04002010 BUG_ON(!cache);
2011 len = min(num, cache->key.offset -
2012 (bytenr - cache->key.objectid));
Yan324ae4d2007-11-16 14:57:08 -05002013 if (pin) {
Josef Bacik25179202008-10-29 14:49:05 -04002014 spin_lock(&cache->space_info->lock);
Zheng Yane8569812008-09-26 10:05:48 -04002015 spin_lock(&cache->lock);
2016 cache->pinned += len;
2017 cache->space_info->bytes_pinned += len;
2018 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04002019 spin_unlock(&cache->space_info->lock);
Yan324ae4d2007-11-16 14:57:08 -05002020 fs_info->total_pinned += len;
2021 } else {
Josef Bacik25179202008-10-29 14:49:05 -04002022 spin_lock(&cache->space_info->lock);
Zheng Yane8569812008-09-26 10:05:48 -04002023 spin_lock(&cache->lock);
2024 cache->pinned -= len;
2025 cache->space_info->bytes_pinned -= len;
2026 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04002027 spin_unlock(&cache->space_info->lock);
Yan324ae4d2007-11-16 14:57:08 -05002028 fs_info->total_pinned -= len;
Josef Bacik07103a32008-11-19 15:17:55 -05002029 if (cache->cached)
2030 btrfs_add_free_space(cache, bytenr, len);
Yan324ae4d2007-11-16 14:57:08 -05002031 }
Yan Zhengd2fb3432008-12-11 16:30:39 -05002032 put_block_group(cache);
Yan324ae4d2007-11-16 14:57:08 -05002033 bytenr += len;
2034 num -= len;
2035 }
2036 return 0;
2037}
Chris Mason9078a3e2007-04-26 16:46:15 -04002038
Zheng Yane8569812008-09-26 10:05:48 -04002039static int update_reserved_extents(struct btrfs_root *root,
2040 u64 bytenr, u64 num, int reserve)
2041{
2042 u64 len;
2043 struct btrfs_block_group_cache *cache;
2044 struct btrfs_fs_info *fs_info = root->fs_info;
2045
Zheng Yane8569812008-09-26 10:05:48 -04002046 while (num > 0) {
2047 cache = btrfs_lookup_block_group(fs_info, bytenr);
2048 BUG_ON(!cache);
2049 len = min(num, cache->key.offset -
2050 (bytenr - cache->key.objectid));
Josef Bacik25179202008-10-29 14:49:05 -04002051
2052 spin_lock(&cache->space_info->lock);
2053 spin_lock(&cache->lock);
Zheng Yane8569812008-09-26 10:05:48 -04002054 if (reserve) {
Zheng Yane8569812008-09-26 10:05:48 -04002055 cache->reserved += len;
2056 cache->space_info->bytes_reserved += len;
Zheng Yane8569812008-09-26 10:05:48 -04002057 } else {
Zheng Yane8569812008-09-26 10:05:48 -04002058 cache->reserved -= len;
2059 cache->space_info->bytes_reserved -= len;
Zheng Yane8569812008-09-26 10:05:48 -04002060 }
Josef Bacik25179202008-10-29 14:49:05 -04002061 spin_unlock(&cache->lock);
2062 spin_unlock(&cache->space_info->lock);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002063 put_block_group(cache);
Zheng Yane8569812008-09-26 10:05:48 -04002064 bytenr += len;
2065 num -= len;
2066 }
2067 return 0;
2068}
2069
Chris Masond1310b22008-01-24 16:13:08 -05002070int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
Chris Masonccd467d2007-06-28 15:57:36 -04002071{
Chris Masonccd467d2007-06-28 15:57:36 -04002072 u64 last = 0;
Chris Mason1a5bc1672007-10-15 16:15:26 -04002073 u64 start;
2074 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -05002075 struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
Chris Masonccd467d2007-06-28 15:57:36 -04002076 int ret;
Chris Masonccd467d2007-06-28 15:57:36 -04002077
Josef Bacik25179202008-10-29 14:49:05 -04002078 mutex_lock(&root->fs_info->pinned_mutex);
Chris Masonccd467d2007-06-28 15:57:36 -04002079 while(1) {
Chris Mason1a5bc1672007-10-15 16:15:26 -04002080 ret = find_first_extent_bit(pinned_extents, last,
2081 &start, &end, EXTENT_DIRTY);
2082 if (ret)
Chris Masonccd467d2007-06-28 15:57:36 -04002083 break;
Chris Mason1a5bc1672007-10-15 16:15:26 -04002084 set_extent_dirty(copy, start, end, GFP_NOFS);
2085 last = end + 1;
Chris Masonccd467d2007-06-28 15:57:36 -04002086 }
Josef Bacik25179202008-10-29 14:49:05 -04002087 mutex_unlock(&root->fs_info->pinned_mutex);
Chris Masonccd467d2007-06-28 15:57:36 -04002088 return 0;
2089}
2090
2091int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
2092 struct btrfs_root *root,
Chris Masond1310b22008-01-24 16:13:08 -05002093 struct extent_io_tree *unpin)
Chris Masona28ec192007-03-06 20:08:01 -05002094{
Chris Mason1a5bc1672007-10-15 16:15:26 -04002095 u64 start;
2096 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05002097 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05002098
Josef Bacik25179202008-10-29 14:49:05 -04002099 mutex_lock(&root->fs_info->pinned_mutex);
Chris Masona28ec192007-03-06 20:08:01 -05002100 while(1) {
Chris Mason1a5bc1672007-10-15 16:15:26 -04002101 ret = find_first_extent_bit(unpin, 0, &start, &end,
2102 EXTENT_DIRTY);
2103 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05002104 break;
Chris Masone02119d2008-09-05 16:13:11 -04002105 btrfs_update_pinned_extents(root, start, end + 1 - start, 0);
Chris Mason1a5bc1672007-10-15 16:15:26 -04002106 clear_extent_dirty(unpin, start, end, GFP_NOFS);
Chris Masonc286ac42008-07-22 23:06:41 -04002107 if (need_resched()) {
Josef Bacik25179202008-10-29 14:49:05 -04002108 mutex_unlock(&root->fs_info->pinned_mutex);
Chris Masonc286ac42008-07-22 23:06:41 -04002109 cond_resched();
Josef Bacik25179202008-10-29 14:49:05 -04002110 mutex_lock(&root->fs_info->pinned_mutex);
Chris Masonc286ac42008-07-22 23:06:41 -04002111 }
Chris Masona28ec192007-03-06 20:08:01 -05002112 }
Josef Bacik25179202008-10-29 14:49:05 -04002113 mutex_unlock(&root->fs_info->pinned_mutex);
Chris Masona28ec192007-03-06 20:08:01 -05002114 return 0;
2115}
2116
Chris Mason98ed5172008-01-03 10:01:48 -05002117static int finish_current_insert(struct btrfs_trans_handle *trans,
Chris Mason87ef2bb2008-10-30 11:23:27 -04002118 struct btrfs_root *extent_root, int all)
Chris Mason037e6392007-03-07 11:50:24 -05002119{
Chris Mason7bb86312007-12-11 09:25:06 -05002120 u64 start;
2121 u64 end;
Zheng Yan31840ae2008-09-23 13:14:14 -04002122 u64 priv;
Josef Bacik25179202008-10-29 14:49:05 -04002123 u64 search = 0;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002124 u64 skipped = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05002125 struct btrfs_fs_info *info = extent_root->fs_info;
2126 struct btrfs_path *path;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002127 struct pending_extent_op *extent_op, *tmp;
2128 struct list_head insert_list, update_list;
Chris Mason037e6392007-03-07 11:50:24 -05002129 int ret;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002130 int num_inserts = 0, max_inserts;
Chris Mason037e6392007-03-07 11:50:24 -05002131
Chris Mason7bb86312007-12-11 09:25:06 -05002132 path = btrfs_alloc_path();
Josef Bacikf3465ca2008-11-12 14:19:50 -05002133 INIT_LIST_HEAD(&insert_list);
2134 INIT_LIST_HEAD(&update_list);
Chris Mason037e6392007-03-07 11:50:24 -05002135
Josef Bacikf3465ca2008-11-12 14:19:50 -05002136 max_inserts = extent_root->leafsize /
2137 (2 * sizeof(struct btrfs_key) + 2 * sizeof(struct btrfs_item) +
2138 sizeof(struct btrfs_extent_ref) +
2139 sizeof(struct btrfs_extent_item));
2140again:
2141 mutex_lock(&info->extent_ins_mutex);
2142 while (1) {
Josef Bacik25179202008-10-29 14:49:05 -04002143 ret = find_first_extent_bit(&info->extent_ins, search, &start,
2144 &end, EXTENT_WRITEBACK);
2145 if (ret) {
Josef Bacikf3465ca2008-11-12 14:19:50 -05002146 if (skipped && all && !num_inserts) {
2147 skipped = 0;
Liu Huib4eec2c2008-11-18 11:30:10 -05002148 search = 0;
Josef Bacik25179202008-10-29 14:49:05 -04002149 continue;
2150 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05002151 mutex_unlock(&info->extent_ins_mutex);
Chris Mason26b80032007-08-08 20:17:12 -04002152 break;
Josef Bacik25179202008-10-29 14:49:05 -04002153 }
2154
2155 ret = try_lock_extent(&info->extent_ins, start, end, GFP_NOFS);
2156 if (!ret) {
Josef Bacikf3465ca2008-11-12 14:19:50 -05002157 skipped = 1;
Chris Mason87ef2bb2008-10-30 11:23:27 -04002158 search = end + 1;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002159 if (need_resched()) {
2160 mutex_unlock(&info->extent_ins_mutex);
2161 cond_resched();
2162 mutex_lock(&info->extent_ins_mutex);
2163 }
Josef Bacik25179202008-10-29 14:49:05 -04002164 continue;
2165 }
Chris Mason26b80032007-08-08 20:17:12 -04002166
Zheng Yan31840ae2008-09-23 13:14:14 -04002167 ret = get_state_private(&info->extent_ins, start, &priv);
2168 BUG_ON(ret);
Josef Bacikf3465ca2008-11-12 14:19:50 -05002169 extent_op = (struct pending_extent_op *)(unsigned long) priv;
Josef Bacik25179202008-10-29 14:49:05 -04002170
Zheng Yan31840ae2008-09-23 13:14:14 -04002171 if (extent_op->type == PENDING_EXTENT_INSERT) {
Josef Bacikf3465ca2008-11-12 14:19:50 -05002172 num_inserts++;
2173 list_add_tail(&extent_op->list, &insert_list);
Chris Mason87ef2bb2008-10-30 11:23:27 -04002174 search = end + 1;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002175 if (num_inserts == max_inserts) {
2176 mutex_unlock(&info->extent_ins_mutex);
2177 break;
2178 }
2179 } else if (extent_op->type == PENDING_BACKREF_UPDATE) {
2180 list_add_tail(&extent_op->list, &update_list);
2181 search = end + 1;
2182 } else {
2183 BUG();
2184 }
Chris Mason037e6392007-03-07 11:50:24 -05002185 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05002186
2187 /*
Liu Huib4eec2c2008-11-18 11:30:10 -05002188 * process the update list, clear the writeback bit for it, and if
Josef Bacikf3465ca2008-11-12 14:19:50 -05002189 * somebody marked this thing for deletion then just unlock it and be
2190 * done, the free_extents will handle it
2191 */
2192 mutex_lock(&info->extent_ins_mutex);
2193 list_for_each_entry_safe(extent_op, tmp, &update_list, list) {
2194 clear_extent_bits(&info->extent_ins, extent_op->bytenr,
2195 extent_op->bytenr + extent_op->num_bytes - 1,
2196 EXTENT_WRITEBACK, GFP_NOFS);
2197 if (extent_op->del) {
2198 list_del_init(&extent_op->list);
2199 unlock_extent(&info->extent_ins, extent_op->bytenr,
2200 extent_op->bytenr + extent_op->num_bytes
2201 - 1, GFP_NOFS);
2202 kfree(extent_op);
2203 }
2204 }
2205 mutex_unlock(&info->extent_ins_mutex);
2206
2207 /*
2208 * still have things left on the update list, go ahead an update
2209 * everything
2210 */
2211 if (!list_empty(&update_list)) {
2212 ret = update_backrefs(trans, extent_root, path, &update_list);
2213 BUG_ON(ret);
2214 }
2215
2216 /*
2217 * if no inserts need to be done, but we skipped some extents and we
2218 * need to make sure everything is cleaned then reset everything and
2219 * go back to the beginning
2220 */
2221 if (!num_inserts && all && skipped) {
2222 search = 0;
2223 skipped = 0;
2224 INIT_LIST_HEAD(&update_list);
2225 INIT_LIST_HEAD(&insert_list);
2226 goto again;
2227 } else if (!num_inserts) {
2228 goto out;
2229 }
2230
2231 /*
2232 * process the insert extents list. Again if we are deleting this
2233 * extent, then just unlock it, pin down the bytes if need be, and be
2234 * done with it. Saves us from having to actually insert the extent
2235 * into the tree and then subsequently come along and delete it
2236 */
2237 mutex_lock(&info->extent_ins_mutex);
2238 list_for_each_entry_safe(extent_op, tmp, &insert_list, list) {
2239 clear_extent_bits(&info->extent_ins, extent_op->bytenr,
2240 extent_op->bytenr + extent_op->num_bytes - 1,
2241 EXTENT_WRITEBACK, GFP_NOFS);
2242 if (extent_op->del) {
2243 list_del_init(&extent_op->list);
2244 unlock_extent(&info->extent_ins, extent_op->bytenr,
2245 extent_op->bytenr + extent_op->num_bytes
2246 - 1, GFP_NOFS);
2247
2248 mutex_lock(&extent_root->fs_info->pinned_mutex);
2249 ret = pin_down_bytes(trans, extent_root,
2250 extent_op->bytenr,
2251 extent_op->num_bytes, 0);
2252 mutex_unlock(&extent_root->fs_info->pinned_mutex);
2253
2254 ret = update_block_group(trans, extent_root,
2255 extent_op->bytenr,
2256 extent_op->num_bytes,
2257 0, ret > 0);
2258 BUG_ON(ret);
2259 kfree(extent_op);
2260 num_inserts--;
2261 }
2262 }
2263 mutex_unlock(&info->extent_ins_mutex);
2264
2265 ret = insert_extents(trans, extent_root, path, &insert_list,
2266 num_inserts);
2267 BUG_ON(ret);
2268
2269 /*
2270 * if we broke out of the loop in order to insert stuff because we hit
2271 * the maximum number of inserts at a time we can handle, then loop
2272 * back and pick up where we left off
2273 */
2274 if (num_inserts == max_inserts) {
2275 INIT_LIST_HEAD(&insert_list);
2276 INIT_LIST_HEAD(&update_list);
2277 num_inserts = 0;
2278 goto again;
2279 }
2280
2281 /*
2282 * again, if we need to make absolutely sure there are no more pending
2283 * extent operations left and we know that we skipped some, go back to
2284 * the beginning and do it all again
2285 */
2286 if (all && skipped) {
2287 INIT_LIST_HEAD(&insert_list);
2288 INIT_LIST_HEAD(&update_list);
2289 search = 0;
2290 skipped = 0;
2291 num_inserts = 0;
2292 goto again;
2293 }
2294out:
Chris Mason7bb86312007-12-11 09:25:06 -05002295 btrfs_free_path(path);
Chris Mason037e6392007-03-07 11:50:24 -05002296 return 0;
2297}
2298
Zheng Yan31840ae2008-09-23 13:14:14 -04002299static int pin_down_bytes(struct btrfs_trans_handle *trans,
2300 struct btrfs_root *root,
2301 u64 bytenr, u64 num_bytes, int is_data)
Chris Masone20d96d2007-03-22 12:13:20 -04002302{
Chris Mason1a5bc1672007-10-15 16:15:26 -04002303 int err = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04002304 struct extent_buffer *buf;
Chris Mason78fae272007-03-25 11:35:08 -04002305
Zheng Yan31840ae2008-09-23 13:14:14 -04002306 if (is_data)
2307 goto pinit;
Chris Mason4bef0842008-09-08 11:18:08 -04002308
Zheng Yan31840ae2008-09-23 13:14:14 -04002309 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
2310 if (!buf)
2311 goto pinit;
Chris Mason4bef0842008-09-08 11:18:08 -04002312
Zheng Yan31840ae2008-09-23 13:14:14 -04002313 /* we can reuse a block if it hasn't been written
2314 * and it is from this transaction. We can't
2315 * reuse anything from the tree log root because
2316 * it has tiny sub-transactions.
2317 */
2318 if (btrfs_buffer_uptodate(buf, 0) &&
2319 btrfs_try_tree_lock(buf)) {
2320 u64 header_owner = btrfs_header_owner(buf);
2321 u64 header_transid = btrfs_header_generation(buf);
2322 if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
Zheng Yan1a40e232008-09-26 10:09:34 -04002323 header_owner != BTRFS_TREE_RELOC_OBJECTID &&
Zheng Yan31840ae2008-09-23 13:14:14 -04002324 header_transid == trans->transid &&
2325 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
2326 clean_tree_block(NULL, root, buf);
2327 btrfs_tree_unlock(buf);
Chris Mason5f39d392007-10-15 16:14:19 -04002328 free_extent_buffer(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002329 return 1;
Chris Mason8ef97622007-03-26 10:15:30 -04002330 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002331 btrfs_tree_unlock(buf);
Chris Masonf4b9aa82007-03-27 11:05:53 -04002332 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002333 free_extent_buffer(buf);
2334pinit:
2335 btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
2336
Chris Masonbe744172007-05-06 10:15:01 -04002337 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -04002338 return 0;
2339}
2340
Chris Masona28ec192007-03-06 20:08:01 -05002341/*
2342 * remove an extent from the root, returns 0 on success
2343 */
Zheng Yan31840ae2008-09-23 13:14:14 -04002344static int __free_extent(struct btrfs_trans_handle *trans,
2345 struct btrfs_root *root,
2346 u64 bytenr, u64 num_bytes, u64 parent,
Chris Mason7bb86312007-12-11 09:25:06 -05002347 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002348 u64 owner_objectid, int pin, int mark_free)
Chris Masona28ec192007-03-06 20:08:01 -05002349{
Chris Mason5caf2a02007-04-02 11:20:42 -04002350 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -04002351 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -04002352 struct btrfs_fs_info *info = root->fs_info;
2353 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04002354 struct extent_buffer *leaf;
Chris Masona28ec192007-03-06 20:08:01 -05002355 int ret;
Chris Mason952fcca2008-02-18 16:33:44 -05002356 int extent_slot = 0;
2357 int found_extent = 0;
2358 int num_to_del = 1;
Chris Mason234b63a2007-03-13 10:46:10 -04002359 struct btrfs_extent_item *ei;
Chris Masoncf27e1e2007-03-13 09:49:06 -04002360 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -05002361
Chris Masondb945352007-10-15 16:15:53 -04002362 key.objectid = bytenr;
Chris Mason62e27492007-03-15 12:56:47 -04002363 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masondb945352007-10-15 16:15:53 -04002364 key.offset = num_bytes;
Chris Mason5caf2a02007-04-02 11:20:42 -04002365 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002366 if (!path)
2367 return -ENOMEM;
2368
Chris Mason3c12ac72008-04-21 12:01:38 -04002369 path->reada = 1;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002370 ret = lookup_extent_backref(trans, extent_root, path,
2371 bytenr, parent, root_objectid,
2372 ref_generation, owner_objectid, 1);
Chris Mason7bb86312007-12-11 09:25:06 -05002373 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05002374 struct btrfs_key found_key;
2375 extent_slot = path->slots[0];
2376 while(extent_slot > 0) {
2377 extent_slot--;
2378 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2379 extent_slot);
2380 if (found_key.objectid != bytenr)
2381 break;
2382 if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
2383 found_key.offset == num_bytes) {
2384 found_extent = 1;
2385 break;
2386 }
2387 if (path->slots[0] - extent_slot > 5)
2388 break;
2389 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002390 if (!found_extent) {
2391 ret = remove_extent_backref(trans, extent_root, path);
2392 BUG_ON(ret);
2393 btrfs_release_path(extent_root, path);
2394 ret = btrfs_search_slot(trans, extent_root,
2395 &key, path, -1, 1);
Josef Bacikf3465ca2008-11-12 14:19:50 -05002396 if (ret) {
2397 printk(KERN_ERR "umm, got %d back from search"
2398 ", was looking for %Lu\n", ret,
2399 bytenr);
2400 btrfs_print_leaf(extent_root, path->nodes[0]);
2401 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002402 BUG_ON(ret);
2403 extent_slot = path->slots[0];
2404 }
Chris Mason7bb86312007-12-11 09:25:06 -05002405 } else {
2406 btrfs_print_leaf(extent_root, path->nodes[0]);
2407 WARN_ON(1);
2408 printk("Unable to find ref byte nr %Lu root %Lu "
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002409 "gen %Lu owner %Lu\n", bytenr,
2410 root_objectid, ref_generation, owner_objectid);
Chris Mason7bb86312007-12-11 09:25:06 -05002411 }
Chris Mason5f39d392007-10-15 16:14:19 -04002412
2413 leaf = path->nodes[0];
Chris Mason952fcca2008-02-18 16:33:44 -05002414 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04002415 struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002416 refs = btrfs_extent_refs(leaf, ei);
2417 BUG_ON(refs == 0);
2418 refs -= 1;
2419 btrfs_set_extent_refs(leaf, ei, refs);
Chris Mason952fcca2008-02-18 16:33:44 -05002420
Chris Mason5f39d392007-10-15 16:14:19 -04002421 btrfs_mark_buffer_dirty(leaf);
2422
Chris Mason952fcca2008-02-18 16:33:44 -05002423 if (refs == 0 && found_extent && path->slots[0] == extent_slot + 1) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002424 struct btrfs_extent_ref *ref;
2425 ref = btrfs_item_ptr(leaf, path->slots[0],
2426 struct btrfs_extent_ref);
2427 BUG_ON(btrfs_ref_num_refs(leaf, ref) != 1);
Chris Mason952fcca2008-02-18 16:33:44 -05002428 /* if the back ref and the extent are next to each other
2429 * they get deleted below in one shot
2430 */
2431 path->slots[0] = extent_slot;
2432 num_to_del = 2;
2433 } else if (found_extent) {
2434 /* otherwise delete the extent back ref */
Zheng Yan31840ae2008-09-23 13:14:14 -04002435 ret = remove_extent_backref(trans, extent_root, path);
Chris Mason952fcca2008-02-18 16:33:44 -05002436 BUG_ON(ret);
2437 /* if refs are 0, we need to setup the path for deletion */
2438 if (refs == 0) {
2439 btrfs_release_path(extent_root, path);
2440 ret = btrfs_search_slot(trans, extent_root, &key, path,
2441 -1, 1);
Chris Mason952fcca2008-02-18 16:33:44 -05002442 BUG_ON(ret);
2443 }
2444 }
2445
Chris Masoncf27e1e2007-03-13 09:49:06 -04002446 if (refs == 0) {
Chris Masondb945352007-10-15 16:15:53 -04002447 u64 super_used;
2448 u64 root_used;
David Woodhouse21af8042008-08-12 14:13:26 +01002449#ifdef BIO_RW_DISCARD
2450 u64 map_length = num_bytes;
2451 struct btrfs_multi_bio *multi = NULL;
2452#endif
Chris Mason78fae272007-03-25 11:35:08 -04002453
2454 if (pin) {
Josef Bacik25179202008-10-29 14:49:05 -04002455 mutex_lock(&root->fs_info->pinned_mutex);
Zheng Yan31840ae2008-09-23 13:14:14 -04002456 ret = pin_down_bytes(trans, root, bytenr, num_bytes,
2457 owner_objectid >= BTRFS_FIRST_FREE_OBJECTID);
Josef Bacik25179202008-10-29 14:49:05 -04002458 mutex_unlock(&root->fs_info->pinned_mutex);
Yanc5492282007-11-06 10:25:25 -05002459 if (ret > 0)
2460 mark_free = 1;
2461 BUG_ON(ret < 0);
Chris Mason78fae272007-03-25 11:35:08 -04002462 }
Josef Bacik58176a92007-08-29 15:47:34 -04002463 /* block accounting for super block */
Chris Masona2135012008-06-25 16:01:30 -04002464 spin_lock_irq(&info->delalloc_lock);
Chris Masondb945352007-10-15 16:15:53 -04002465 super_used = btrfs_super_bytes_used(&info->super_copy);
2466 btrfs_set_super_bytes_used(&info->super_copy,
2467 super_used - num_bytes);
Chris Masona2135012008-06-25 16:01:30 -04002468 spin_unlock_irq(&info->delalloc_lock);
Josef Bacik58176a92007-08-29 15:47:34 -04002469
2470 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04002471 root_used = btrfs_root_used(&root->root_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002472 btrfs_set_root_used(&root->root_item,
Chris Masondb945352007-10-15 16:15:53 -04002473 root_used - num_bytes);
Chris Mason952fcca2008-02-18 16:33:44 -05002474 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
2475 num_to_del);
Zheng Yan31840ae2008-09-23 13:14:14 -04002476 BUG_ON(ret);
Josef Bacik25179202008-10-29 14:49:05 -04002477 btrfs_release_path(extent_root, path);
Chris Masondb945352007-10-15 16:15:53 -04002478 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
Chris Mason0b86a832008-03-24 15:01:56 -04002479 mark_free);
Chris Mason9078a3e2007-04-26 16:46:15 -04002480 BUG_ON(ret);
David Woodhouse21af8042008-08-12 14:13:26 +01002481
Chris Mason459931e2008-12-10 09:10:46 -05002482 if (owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
2483 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
2484 BUG_ON(ret);
2485 }
2486
David Woodhouse21af8042008-08-12 14:13:26 +01002487#ifdef BIO_RW_DISCARD
2488 /* Tell the block device(s) that the sectors can be discarded */
2489 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
2490 bytenr, &map_length, &multi, 0);
2491 if (!ret) {
2492 struct btrfs_bio_stripe *stripe = multi->stripes;
2493 int i;
2494
2495 if (map_length > num_bytes)
2496 map_length = num_bytes;
2497
2498 for (i = 0; i < multi->num_stripes; i++, stripe++) {
Chris Mason15916de2008-11-19 21:17:22 -05002499 btrfs_issue_discard(stripe->dev->bdev,
2500 stripe->physical,
2501 map_length);
David Woodhouse21af8042008-08-12 14:13:26 +01002502 }
2503 kfree(multi);
2504 }
2505#endif
Chris Masona28ec192007-03-06 20:08:01 -05002506 }
Chris Mason5caf2a02007-04-02 11:20:42 -04002507 btrfs_free_path(path);
Chris Mason87ef2bb2008-10-30 11:23:27 -04002508 finish_current_insert(trans, extent_root, 0);
Chris Masona28ec192007-03-06 20:08:01 -05002509 return ret;
2510}
2511
2512/*
Chris Masonfec577f2007-02-26 10:40:21 -05002513 * find all the blocks marked as pending in the radix tree and remove
2514 * them from the extent map
2515 */
Chris Masone089f052007-03-16 16:20:31 -04002516static int del_pending_extents(struct btrfs_trans_handle *trans, struct
Chris Mason87ef2bb2008-10-30 11:23:27 -04002517 btrfs_root *extent_root, int all)
Chris Masonfec577f2007-02-26 10:40:21 -05002518{
2519 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -04002520 int err = 0;
Chris Mason1a5bc1672007-10-15 16:15:26 -04002521 u64 start;
2522 u64 end;
Zheng Yan31840ae2008-09-23 13:14:14 -04002523 u64 priv;
Josef Bacik25179202008-10-29 14:49:05 -04002524 u64 search = 0;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002525 int nr = 0, skipped = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002526 struct extent_io_tree *pending_del;
Zheng Yan31840ae2008-09-23 13:14:14 -04002527 struct extent_io_tree *extent_ins;
2528 struct pending_extent_op *extent_op;
Josef Bacik25179202008-10-29 14:49:05 -04002529 struct btrfs_fs_info *info = extent_root->fs_info;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002530 struct list_head delete_list;
Chris Mason8ef97622007-03-26 10:15:30 -04002531
Josef Bacikf3465ca2008-11-12 14:19:50 -05002532 INIT_LIST_HEAD(&delete_list);
Zheng Yan31840ae2008-09-23 13:14:14 -04002533 extent_ins = &extent_root->fs_info->extent_ins;
Chris Mason1a5bc1672007-10-15 16:15:26 -04002534 pending_del = &extent_root->fs_info->pending_del;
Chris Masonfec577f2007-02-26 10:40:21 -05002535
Josef Bacikf3465ca2008-11-12 14:19:50 -05002536again:
2537 mutex_lock(&info->extent_ins_mutex);
Chris Masonfec577f2007-02-26 10:40:21 -05002538 while(1) {
Josef Bacik25179202008-10-29 14:49:05 -04002539 ret = find_first_extent_bit(pending_del, search, &start, &end,
2540 EXTENT_WRITEBACK);
2541 if (ret) {
Josef Bacikf3465ca2008-11-12 14:19:50 -05002542 if (all && skipped && !nr) {
Josef Bacik25179202008-10-29 14:49:05 -04002543 search = 0;
2544 continue;
2545 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05002546 mutex_unlock(&info->extent_ins_mutex);
Chris Masonfec577f2007-02-26 10:40:21 -05002547 break;
Josef Bacik25179202008-10-29 14:49:05 -04002548 }
2549
2550 ret = try_lock_extent(extent_ins, start, end, GFP_NOFS);
2551 if (!ret) {
2552 search = end+1;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002553 skipped = 1;
2554
2555 if (need_resched()) {
2556 mutex_unlock(&info->extent_ins_mutex);
2557 cond_resched();
2558 mutex_lock(&info->extent_ins_mutex);
2559 }
2560
Josef Bacik25179202008-10-29 14:49:05 -04002561 continue;
2562 }
2563 BUG_ON(ret < 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002564
2565 ret = get_state_private(pending_del, start, &priv);
2566 BUG_ON(ret);
2567 extent_op = (struct pending_extent_op *)(unsigned long)priv;
2568
Josef Bacik25179202008-10-29 14:49:05 -04002569 clear_extent_bits(pending_del, start, end, EXTENT_WRITEBACK,
Chris Mason1a5bc1672007-10-15 16:15:26 -04002570 GFP_NOFS);
Zheng Yan31840ae2008-09-23 13:14:14 -04002571 if (!test_range_bit(extent_ins, start, end,
Josef Bacik25179202008-10-29 14:49:05 -04002572 EXTENT_WRITEBACK, 0)) {
Josef Bacikf3465ca2008-11-12 14:19:50 -05002573 list_add_tail(&extent_op->list, &delete_list);
2574 nr++;
Chris Masonc286ac42008-07-22 23:06:41 -04002575 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04002576 kfree(extent_op);
Josef Bacik25179202008-10-29 14:49:05 -04002577
2578 ret = get_state_private(&info->extent_ins, start,
2579 &priv);
Zheng Yan31840ae2008-09-23 13:14:14 -04002580 BUG_ON(ret);
2581 extent_op = (struct pending_extent_op *)
Josef Bacik25179202008-10-29 14:49:05 -04002582 (unsigned long)priv;
Zheng Yan31840ae2008-09-23 13:14:14 -04002583
Josef Bacik25179202008-10-29 14:49:05 -04002584 clear_extent_bits(&info->extent_ins, start, end,
2585 EXTENT_WRITEBACK, GFP_NOFS);
2586
Josef Bacikf3465ca2008-11-12 14:19:50 -05002587 if (extent_op->type == PENDING_BACKREF_UPDATE) {
2588 list_add_tail(&extent_op->list, &delete_list);
2589 search = end + 1;
2590 nr++;
2591 continue;
2592 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002593
Josef Bacik25179202008-10-29 14:49:05 -04002594 mutex_lock(&extent_root->fs_info->pinned_mutex);
2595 ret = pin_down_bytes(trans, extent_root, start,
2596 end + 1 - start, 0);
2597 mutex_unlock(&extent_root->fs_info->pinned_mutex);
2598
Zheng Yan31840ae2008-09-23 13:14:14 -04002599 ret = update_block_group(trans, extent_root, start,
Josef Bacik25179202008-10-29 14:49:05 -04002600 end + 1 - start, 0, ret > 0);
2601
Josef Bacikf3465ca2008-11-12 14:19:50 -05002602 unlock_extent(extent_ins, start, end, GFP_NOFS);
Zheng Yan31840ae2008-09-23 13:14:14 -04002603 BUG_ON(ret);
2604 kfree(extent_op);
Chris Masonc286ac42008-07-22 23:06:41 -04002605 }
Chris Mason1a5bc1672007-10-15 16:15:26 -04002606 if (ret)
2607 err = ret;
Chris Masonc286ac42008-07-22 23:06:41 -04002608
Josef Bacikf3465ca2008-11-12 14:19:50 -05002609 search = end + 1;
2610
2611 if (need_resched()) {
2612 mutex_unlock(&info->extent_ins_mutex);
2613 cond_resched();
2614 mutex_lock(&info->extent_ins_mutex);
2615 }
Chris Masonfec577f2007-02-26 10:40:21 -05002616 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05002617
2618 if (nr) {
2619 ret = free_extents(trans, extent_root, &delete_list);
2620 BUG_ON(ret);
2621 }
2622
2623 if (all && skipped) {
2624 INIT_LIST_HEAD(&delete_list);
2625 search = 0;
2626 nr = 0;
2627 goto again;
2628 }
2629
Chris Masone20d96d2007-03-22 12:13:20 -04002630 return err;
Chris Masonfec577f2007-02-26 10:40:21 -05002631}
2632
2633/*
2634 * remove an extent from the root, returns 0 on success
2635 */
Chris Mason925baed2008-06-25 16:01:30 -04002636static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04002637 struct btrfs_root *root,
2638 u64 bytenr, u64 num_bytes, u64 parent,
2639 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002640 u64 owner_objectid, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -05002641{
Chris Mason9f5fae22007-03-20 14:38:32 -04002642 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -05002643 int pending_ret;
2644 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05002645
Chris Masondb945352007-10-15 16:15:53 -04002646 WARN_ON(num_bytes < root->sectorsize);
Chris Masona28ec192007-03-06 20:08:01 -05002647 if (root == extent_root) {
Josef Bacikf3465ca2008-11-12 14:19:50 -05002648 struct pending_extent_op *extent_op = NULL;
2649
2650 mutex_lock(&root->fs_info->extent_ins_mutex);
2651 if (test_range_bit(&root->fs_info->extent_ins, bytenr,
2652 bytenr + num_bytes - 1, EXTENT_WRITEBACK, 0)) {
2653 u64 priv;
2654 ret = get_state_private(&root->fs_info->extent_ins,
2655 bytenr, &priv);
2656 BUG_ON(ret);
2657 extent_op = (struct pending_extent_op *)
2658 (unsigned long)priv;
2659
2660 extent_op->del = 1;
2661 if (extent_op->type == PENDING_EXTENT_INSERT) {
2662 mutex_unlock(&root->fs_info->extent_ins_mutex);
2663 return 0;
2664 }
2665 }
2666
2667 if (extent_op) {
2668 ref_generation = extent_op->orig_generation;
2669 parent = extent_op->orig_parent;
2670 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002671
2672 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2673 BUG_ON(!extent_op);
2674
2675 extent_op->type = PENDING_EXTENT_DELETE;
2676 extent_op->bytenr = bytenr;
2677 extent_op->num_bytes = num_bytes;
2678 extent_op->parent = parent;
2679 extent_op->orig_parent = parent;
2680 extent_op->generation = ref_generation;
2681 extent_op->orig_generation = ref_generation;
2682 extent_op->level = (int)owner_objectid;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002683 INIT_LIST_HEAD(&extent_op->list);
2684 extent_op->del = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04002685
2686 set_extent_bits(&root->fs_info->pending_del,
2687 bytenr, bytenr + num_bytes - 1,
Josef Bacik25179202008-10-29 14:49:05 -04002688 EXTENT_WRITEBACK, GFP_NOFS);
Zheng Yan31840ae2008-09-23 13:14:14 -04002689 set_state_private(&root->fs_info->pending_del,
2690 bytenr, (unsigned long)extent_op);
Josef Bacik25179202008-10-29 14:49:05 -04002691 mutex_unlock(&root->fs_info->extent_ins_mutex);
Chris Masona28ec192007-03-06 20:08:01 -05002692 return 0;
2693 }
Chris Mason4bef0842008-09-08 11:18:08 -04002694 /* if metadata always pin */
Chris Masond00aff02008-09-11 15:54:42 -04002695 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
2696 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04002697 struct btrfs_block_group_cache *cache;
2698
Chris Masond00aff02008-09-11 15:54:42 -04002699 /* btrfs_free_reserved_extent */
Josef Bacik0f9dd462008-09-23 13:14:11 -04002700 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
2701 BUG_ON(!cache);
2702 btrfs_add_free_space(cache, bytenr, num_bytes);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002703 put_block_group(cache);
Zheng Yane8569812008-09-26 10:05:48 -04002704 update_reserved_extents(root, bytenr, num_bytes, 0);
Chris Masond00aff02008-09-11 15:54:42 -04002705 return 0;
2706 }
Chris Mason4bef0842008-09-08 11:18:08 -04002707 pin = 1;
Chris Masond00aff02008-09-11 15:54:42 -04002708 }
Chris Mason4bef0842008-09-08 11:18:08 -04002709
2710 /* if data pin when any transaction has committed this */
2711 if (ref_generation != trans->transid)
2712 pin = 1;
2713
Zheng Yan31840ae2008-09-23 13:14:14 -04002714 ret = __free_extent(trans, root, bytenr, num_bytes, parent,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002715 root_objectid, ref_generation,
2716 owner_objectid, pin, pin == 0);
Chris Masonee6e6502008-07-17 12:54:40 -04002717
Chris Mason87ef2bb2008-10-30 11:23:27 -04002718 finish_current_insert(trans, root->fs_info->extent_root, 0);
2719 pending_ret = del_pending_extents(trans, root->fs_info->extent_root, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05002720 return ret ? ret : pending_ret;
2721}
2722
Chris Mason925baed2008-06-25 16:01:30 -04002723int btrfs_free_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04002724 struct btrfs_root *root,
2725 u64 bytenr, u64 num_bytes, u64 parent,
2726 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002727 u64 owner_objectid, int pin)
Chris Mason925baed2008-06-25 16:01:30 -04002728{
2729 int ret;
2730
Zheng Yan31840ae2008-09-23 13:14:14 -04002731 ret = __btrfs_free_extent(trans, root, bytenr, num_bytes, parent,
Chris Mason925baed2008-06-25 16:01:30 -04002732 root_objectid, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002733 owner_objectid, pin);
Chris Mason925baed2008-06-25 16:01:30 -04002734 return ret;
2735}
2736
Chris Mason87ee04e2007-11-30 11:30:34 -05002737static u64 stripe_align(struct btrfs_root *root, u64 val)
2738{
2739 u64 mask = ((u64)root->stripesize - 1);
2740 u64 ret = (val + mask) & ~mask;
2741 return ret;
2742}
2743
Chris Masonfec577f2007-02-26 10:40:21 -05002744/*
2745 * walks the btree of allocated extents and find a hole of a given size.
2746 * The key ins is changed to record the hole:
2747 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04002748 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05002749 * ins->offset == number of blocks
2750 * Any available blocks before search_start are skipped.
2751 */
Chris Mason98ed5172008-01-03 10:01:48 -05002752static int noinline find_free_extent(struct btrfs_trans_handle *trans,
2753 struct btrfs_root *orig_root,
2754 u64 num_bytes, u64 empty_size,
2755 u64 search_start, u64 search_end,
2756 u64 hint_byte, struct btrfs_key *ins,
2757 u64 exclude_start, u64 exclude_nr,
2758 int data)
Chris Masonfec577f2007-02-26 10:40:21 -05002759{
Josef Bacik80eb2342008-10-29 14:49:05 -04002760 int ret = 0;
Chris Mason9f5fae22007-03-20 14:38:32 -04002761 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Masondb945352007-10-15 16:15:53 -04002762 u64 total_needed = num_bytes;
Chris Mason239b14b2008-03-24 15:02:07 -04002763 u64 *last_ptr = NULL;
Chris Mason43662112008-11-07 09:06:11 -05002764 u64 last_wanted = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04002765 struct btrfs_block_group_cache *block_group = NULL;
Chris Mason0ef3e662008-05-24 14:04:53 -04002766 int chunk_alloc_done = 0;
Chris Mason239b14b2008-03-24 15:02:07 -04002767 int empty_cluster = 2 * 1024 * 1024;
Chris Mason0ef3e662008-05-24 14:04:53 -04002768 int allowed_chunk_alloc = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04002769 struct list_head *head = NULL, *cur = NULL;
2770 int loop = 0;
Chris Masonf5a31e12008-11-10 11:47:09 -05002771 int extra_loop = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04002772 struct btrfs_space_info *space_info;
Chris Masonfec577f2007-02-26 10:40:21 -05002773
Chris Masondb945352007-10-15 16:15:53 -04002774 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04002775 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
Josef Bacik80eb2342008-10-29 14:49:05 -04002776 ins->objectid = 0;
2777 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04002778
Chris Mason0ef3e662008-05-24 14:04:53 -04002779 if (orig_root->ref_cows || empty_size)
2780 allowed_chunk_alloc = 1;
2781
Chris Mason239b14b2008-03-24 15:02:07 -04002782 if (data & BTRFS_BLOCK_GROUP_METADATA) {
2783 last_ptr = &root->fs_info->last_alloc;
Chris Mason43662112008-11-07 09:06:11 -05002784 empty_cluster = 64 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04002785 }
2786
Josef Bacik0f9dd462008-09-23 13:14:11 -04002787 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD))
Chris Mason239b14b2008-03-24 15:02:07 -04002788 last_ptr = &root->fs_info->last_data_alloc;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002789
Chris Mason239b14b2008-03-24 15:02:07 -04002790 if (last_ptr) {
Chris Mason43662112008-11-07 09:06:11 -05002791 if (*last_ptr) {
Chris Mason239b14b2008-03-24 15:02:07 -04002792 hint_byte = *last_ptr;
Chris Mason43662112008-11-07 09:06:11 -05002793 last_wanted = *last_ptr;
2794 } else
Chris Mason239b14b2008-03-24 15:02:07 -04002795 empty_size += empty_cluster;
Chris Mason43662112008-11-07 09:06:11 -05002796 } else {
2797 empty_cluster = 0;
Chris Mason239b14b2008-03-24 15:02:07 -04002798 }
Chris Masona061fc82008-05-07 11:43:44 -04002799 search_start = max(search_start, first_logical_byte(root, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04002800 search_start = max(search_start, hint_byte);
Chris Mason0b86a832008-03-24 15:01:56 -04002801
Chris Mason42e70e72008-11-07 18:17:11 -05002802 if (last_wanted && search_start != last_wanted) {
Chris Mason43662112008-11-07 09:06:11 -05002803 last_wanted = 0;
Chris Mason42e70e72008-11-07 18:17:11 -05002804 empty_size += empty_cluster;
2805 }
Chris Mason43662112008-11-07 09:06:11 -05002806
Chris Mason42e70e72008-11-07 18:17:11 -05002807 total_needed += empty_size;
Josef Bacik80eb2342008-10-29 14:49:05 -04002808 block_group = btrfs_lookup_block_group(root->fs_info, search_start);
Yan Zhengd899e052008-10-30 14:25:28 -04002809 if (!block_group)
2810 block_group = btrfs_lookup_first_block_group(root->fs_info,
2811 search_start);
Josef Bacik80eb2342008-10-29 14:49:05 -04002812 space_info = __find_space_info(root->fs_info, data);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002813
Josef Bacik80eb2342008-10-29 14:49:05 -04002814 down_read(&space_info->groups_sem);
2815 while (1) {
2816 struct btrfs_free_space *free_space;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002817 /*
Josef Bacik80eb2342008-10-29 14:49:05 -04002818 * the only way this happens if our hint points to a block
2819 * group thats not of the proper type, while looping this
2820 * should never happen
Josef Bacik0f9dd462008-09-23 13:14:11 -04002821 */
Chris Mason8a1413a2008-11-10 16:13:54 -05002822 if (empty_size)
2823 extra_loop = 1;
2824
Chris Mason42e70e72008-11-07 18:17:11 -05002825 if (!block_group)
2826 goto new_group_no_lock;
2827
Josef Bacikea6a4782008-11-20 12:16:16 -05002828 if (unlikely(!block_group->cached)) {
2829 mutex_lock(&block_group->cache_mutex);
2830 ret = cache_block_group(root, block_group);
2831 mutex_unlock(&block_group->cache_mutex);
2832 if (ret)
2833 break;
2834 }
2835
Josef Bacik25179202008-10-29 14:49:05 -04002836 mutex_lock(&block_group->alloc_mutex);
Josef Bacik80eb2342008-10-29 14:49:05 -04002837 if (unlikely(!block_group_bits(block_group, data)))
Josef Bacik0f9dd462008-09-23 13:14:11 -04002838 goto new_group;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002839
Josef Bacikea6a4782008-11-20 12:16:16 -05002840 if (unlikely(block_group->ro))
Josef Bacik80eb2342008-10-29 14:49:05 -04002841 goto new_group;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002842
Josef Bacik80eb2342008-10-29 14:49:05 -04002843 free_space = btrfs_find_free_space(block_group, search_start,
2844 total_needed);
2845 if (free_space) {
2846 u64 start = block_group->key.objectid;
2847 u64 end = block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -04002848 block_group->key.offset;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002849
Josef Bacik80eb2342008-10-29 14:49:05 -04002850 search_start = stripe_align(root, free_space->offset);
Chris Mason0b86a832008-03-24 15:01:56 -04002851
Josef Bacik80eb2342008-10-29 14:49:05 -04002852 /* move on to the next group */
2853 if (search_start + num_bytes >= search_end)
2854 goto new_group;
Chris Masone37c9e62007-05-09 20:13:14 -04002855
Josef Bacik80eb2342008-10-29 14:49:05 -04002856 /* move on to the next group */
2857 if (search_start + num_bytes > end)
2858 goto new_group;
2859
Chris Mason43662112008-11-07 09:06:11 -05002860 if (last_wanted && search_start != last_wanted) {
Chris Mason3b7885b2008-11-06 21:48:27 -05002861 total_needed += empty_cluster;
Chris Mason8a1413a2008-11-10 16:13:54 -05002862 empty_size += empty_cluster;
Chris Mason43662112008-11-07 09:06:11 -05002863 last_wanted = 0;
Chris Mason3b7885b2008-11-06 21:48:27 -05002864 /*
2865 * if search_start is still in this block group
2866 * then we just re-search this block group
2867 */
2868 if (search_start >= start &&
2869 search_start < end) {
2870 mutex_unlock(&block_group->alloc_mutex);
2871 continue;
2872 }
2873
2874 /* else we go to the next block group */
2875 goto new_group;
2876 }
2877
Josef Bacik80eb2342008-10-29 14:49:05 -04002878 if (exclude_nr > 0 &&
2879 (search_start + num_bytes > exclude_start &&
2880 search_start < exclude_start + exclude_nr)) {
2881 search_start = exclude_start + exclude_nr;
2882 /*
2883 * if search_start is still in this block group
2884 * then we just re-search this block group
2885 */
2886 if (search_start >= start &&
Josef Bacik25179202008-10-29 14:49:05 -04002887 search_start < end) {
2888 mutex_unlock(&block_group->alloc_mutex);
Chris Mason43662112008-11-07 09:06:11 -05002889 last_wanted = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04002890 continue;
Josef Bacik25179202008-10-29 14:49:05 -04002891 }
Josef Bacik80eb2342008-10-29 14:49:05 -04002892
2893 /* else we go to the next block group */
2894 goto new_group;
2895 }
2896
2897 ins->objectid = search_start;
2898 ins->offset = num_bytes;
Josef Bacik25179202008-10-29 14:49:05 -04002899
2900 btrfs_remove_free_space_lock(block_group, search_start,
2901 num_bytes);
Josef Bacik80eb2342008-10-29 14:49:05 -04002902 /* we are all good, lets return */
Josef Bacik25179202008-10-29 14:49:05 -04002903 mutex_unlock(&block_group->alloc_mutex);
Josef Bacik80eb2342008-10-29 14:49:05 -04002904 break;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002905 }
Josef Bacik80eb2342008-10-29 14:49:05 -04002906new_group:
Chris Mason42e70e72008-11-07 18:17:11 -05002907 mutex_unlock(&block_group->alloc_mutex);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002908 put_block_group(block_group);
2909 block_group = NULL;
Chris Mason42e70e72008-11-07 18:17:11 -05002910new_group_no_lock:
Chris Masonf5a31e12008-11-10 11:47:09 -05002911 /* don't try to compare new allocations against the
2912 * last allocation any more
2913 */
Chris Mason43662112008-11-07 09:06:11 -05002914 last_wanted = 0;
Chris Masonf5a31e12008-11-10 11:47:09 -05002915
Josef Bacik80eb2342008-10-29 14:49:05 -04002916 /*
2917 * Here's how this works.
2918 * loop == 0: we were searching a block group via a hint
2919 * and didn't find anything, so we start at
2920 * the head of the block groups and keep searching
2921 * loop == 1: we're searching through all of the block groups
2922 * if we hit the head again we have searched
2923 * all of the block groups for this space and we
2924 * need to try and allocate, if we cant error out.
2925 * loop == 2: we allocated more space and are looping through
2926 * all of the block groups again.
2927 */
2928 if (loop == 0) {
2929 head = &space_info->block_groups;
2930 cur = head->next;
Josef Bacik80eb2342008-10-29 14:49:05 -04002931 loop++;
2932 } else if (loop == 1 && cur == head) {
Chris Masonf5a31e12008-11-10 11:47:09 -05002933 int keep_going;
Chris Mason42e70e72008-11-07 18:17:11 -05002934
Chris Masonf5a31e12008-11-10 11:47:09 -05002935 /* at this point we give up on the empty_size
2936 * allocations and just try to allocate the min
2937 * space.
2938 *
2939 * The extra_loop field was set if an empty_size
2940 * allocation was attempted above, and if this
2941 * is try we need to try the loop again without
2942 * the additional empty_size.
2943 */
Chris Mason5b7c3fc2008-11-10 07:26:33 -05002944 total_needed -= empty_size;
2945 empty_size = 0;
Chris Masonf5a31e12008-11-10 11:47:09 -05002946 keep_going = extra_loop;
2947 loop++;
Chris Mason42e70e72008-11-07 18:17:11 -05002948
Josef Bacik80eb2342008-10-29 14:49:05 -04002949 if (allowed_chunk_alloc && !chunk_alloc_done) {
2950 up_read(&space_info->groups_sem);
2951 ret = do_chunk_alloc(trans, root, num_bytes +
2952 2 * 1024 * 1024, data, 1);
Josef Bacik80eb2342008-10-29 14:49:05 -04002953 down_read(&space_info->groups_sem);
Chris Mason2ed6d662008-11-13 09:59:33 -05002954 if (ret < 0)
2955 goto loop_check;
Josef Bacik80eb2342008-10-29 14:49:05 -04002956 head = &space_info->block_groups;
Chris Masonf5a31e12008-11-10 11:47:09 -05002957 /*
2958 * we've allocated a new chunk, keep
2959 * trying
2960 */
2961 keep_going = 1;
Josef Bacik80eb2342008-10-29 14:49:05 -04002962 chunk_alloc_done = 1;
2963 } else if (!allowed_chunk_alloc) {
2964 space_info->force_alloc = 1;
Chris Masonf5a31e12008-11-10 11:47:09 -05002965 }
Chris Mason2ed6d662008-11-13 09:59:33 -05002966loop_check:
Chris Masonf5a31e12008-11-10 11:47:09 -05002967 if (keep_going) {
2968 cur = head->next;
2969 extra_loop = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04002970 } else {
2971 break;
2972 }
2973 } else if (cur == head) {
2974 break;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002975 }
Josef Bacik80eb2342008-10-29 14:49:05 -04002976
2977 block_group = list_entry(cur, struct btrfs_block_group_cache,
2978 list);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002979 atomic_inc(&block_group->count);
2980
Josef Bacik80eb2342008-10-29 14:49:05 -04002981 search_start = block_group->key.objectid;
2982 cur = cur->next;
Chris Masone19caa52007-10-15 16:17:44 -04002983 }
Chris Mason0b86a832008-03-24 15:01:56 -04002984
Josef Bacik80eb2342008-10-29 14:49:05 -04002985 /* we found what we needed */
2986 if (ins->objectid) {
2987 if (!(data & BTRFS_BLOCK_GROUP_DATA))
Yan Zhengd2fb3432008-12-11 16:30:39 -05002988 trans->block_group = block_group->key.objectid;
Josef Bacik80eb2342008-10-29 14:49:05 -04002989
2990 if (last_ptr)
2991 *last_ptr = ins->objectid + ins->offset;
2992 ret = 0;
2993 } else if (!ret) {
Josef Bacik4ce4cb52008-11-17 21:12:00 -05002994 printk(KERN_ERR "we were searching for %Lu bytes, num_bytes %Lu,"
2995 " loop %d, allowed_alloc %d\n", total_needed, num_bytes,
2996 loop, allowed_chunk_alloc);
Josef Bacik80eb2342008-10-29 14:49:05 -04002997 ret = -ENOSPC;
Chris Masonf2654de2007-06-26 12:20:46 -04002998 }
Yan Zhengd2fb3432008-12-11 16:30:39 -05002999 if (block_group)
3000 put_block_group(block_group);
Chris Mason0b86a832008-03-24 15:01:56 -04003001
Josef Bacik80eb2342008-10-29 14:49:05 -04003002 up_read(&space_info->groups_sem);
Chris Mason0f70abe2007-02-28 16:46:22 -05003003 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05003004}
Chris Masonec44a352008-04-28 15:29:52 -04003005
Josef Bacik0f9dd462008-09-23 13:14:11 -04003006static void dump_space_info(struct btrfs_space_info *info, u64 bytes)
3007{
3008 struct btrfs_block_group_cache *cache;
3009 struct list_head *l;
3010
3011 printk(KERN_INFO "space_info has %Lu free, is %sfull\n",
Zheng Yane8569812008-09-26 10:05:48 -04003012 info->total_bytes - info->bytes_used - info->bytes_pinned -
3013 info->bytes_reserved, (info->full) ? "" : "not ");
Josef Bacik0f9dd462008-09-23 13:14:11 -04003014
Josef Bacik80eb2342008-10-29 14:49:05 -04003015 down_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003016 list_for_each(l, &info->block_groups) {
3017 cache = list_entry(l, struct btrfs_block_group_cache, list);
3018 spin_lock(&cache->lock);
3019 printk(KERN_INFO "block group %Lu has %Lu bytes, %Lu used "
Zheng Yane8569812008-09-26 10:05:48 -04003020 "%Lu pinned %Lu reserved\n",
Josef Bacik0f9dd462008-09-23 13:14:11 -04003021 cache->key.objectid, cache->key.offset,
Zheng Yane8569812008-09-26 10:05:48 -04003022 btrfs_block_group_used(&cache->item),
3023 cache->pinned, cache->reserved);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003024 btrfs_dump_free_space(cache, bytes);
3025 spin_unlock(&cache->lock);
3026 }
Josef Bacik80eb2342008-10-29 14:49:05 -04003027 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003028}
Zheng Yane8569812008-09-26 10:05:48 -04003029
Chris Masone6dcd2d2008-07-17 12:53:50 -04003030static int __btrfs_reserve_extent(struct btrfs_trans_handle *trans,
3031 struct btrfs_root *root,
3032 u64 num_bytes, u64 min_alloc_size,
3033 u64 empty_size, u64 hint_byte,
3034 u64 search_end, struct btrfs_key *ins,
3035 u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05003036{
3037 int ret;
Chris Masonfbdc7622007-05-30 10:22:12 -04003038 u64 search_start = 0;
Chris Mason8790d502008-04-03 16:29:03 -04003039 u64 alloc_profile;
Chris Mason1261ec42007-03-20 20:35:03 -04003040 struct btrfs_fs_info *info = root->fs_info;
Chris Mason925baed2008-06-25 16:01:30 -04003041
Chris Mason6324fbf2008-03-24 15:01:59 -04003042 if (data) {
Chris Mason8790d502008-04-03 16:29:03 -04003043 alloc_profile = info->avail_data_alloc_bits &
3044 info->data_alloc_profile;
3045 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04003046 } else if (root == root->fs_info->chunk_root) {
Chris Mason8790d502008-04-03 16:29:03 -04003047 alloc_profile = info->avail_system_alloc_bits &
3048 info->system_alloc_profile;
3049 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04003050 } else {
Chris Mason8790d502008-04-03 16:29:03 -04003051 alloc_profile = info->avail_metadata_alloc_bits &
3052 info->metadata_alloc_profile;
3053 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04003054 }
Chris Mason98d20f62008-04-14 09:46:10 -04003055again:
Yan Zheng2b820322008-11-17 21:11:30 -05003056 data = btrfs_reduce_alloc_profile(root, data);
Chris Mason0ef3e662008-05-24 14:04:53 -04003057 /*
3058 * the only place that sets empty_size is btrfs_realloc_node, which
3059 * is not called recursively on allocations
3060 */
3061 if (empty_size || root->ref_cows) {
Chris Mason593060d2008-03-25 16:50:33 -04003062 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
Chris Mason6324fbf2008-03-24 15:01:59 -04003063 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04003064 2 * 1024 * 1024,
3065 BTRFS_BLOCK_GROUP_METADATA |
3066 (info->metadata_alloc_profile &
3067 info->avail_metadata_alloc_bits), 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04003068 }
3069 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04003070 num_bytes + 2 * 1024 * 1024, data, 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04003071 }
Chris Mason0b86a832008-03-24 15:01:56 -04003072
Chris Masondb945352007-10-15 16:15:53 -04003073 WARN_ON(num_bytes < root->sectorsize);
3074 ret = find_free_extent(trans, root, num_bytes, empty_size,
3075 search_start, search_end, hint_byte, ins,
Chris Mason26b80032007-08-08 20:17:12 -04003076 trans->alloc_exclude_start,
3077 trans->alloc_exclude_nr, data);
Chris Mason3b951512008-04-17 11:29:12 -04003078
Chris Mason98d20f62008-04-14 09:46:10 -04003079 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
3080 num_bytes = num_bytes >> 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003081 num_bytes = num_bytes & ~(root->sectorsize - 1);
Chris Mason98d20f62008-04-14 09:46:10 -04003082 num_bytes = max(num_bytes, min_alloc_size);
Chris Mason0ef3e662008-05-24 14:04:53 -04003083 do_chunk_alloc(trans, root->fs_info->extent_root,
3084 num_bytes, data, 1);
Chris Mason98d20f62008-04-14 09:46:10 -04003085 goto again;
3086 }
Chris Masonec44a352008-04-28 15:29:52 -04003087 if (ret) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04003088 struct btrfs_space_info *sinfo;
3089
3090 sinfo = __find_space_info(root->fs_info, data);
3091 printk("allocation failed flags %Lu, wanted %Lu\n",
3092 data, num_bytes);
3093 dump_space_info(sinfo, num_bytes);
Chris Mason925baed2008-06-25 16:01:30 -04003094 BUG();
Chris Mason925baed2008-06-25 16:01:30 -04003095 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04003096
3097 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003098}
3099
Chris Mason65b51a02008-08-01 15:11:20 -04003100int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
3101{
Josef Bacik0f9dd462008-09-23 13:14:11 -04003102 struct btrfs_block_group_cache *cache;
3103
Josef Bacik0f9dd462008-09-23 13:14:11 -04003104 cache = btrfs_lookup_block_group(root->fs_info, start);
3105 if (!cache) {
3106 printk(KERN_ERR "Unable to find block group for %Lu\n", start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003107 return -ENOSPC;
3108 }
3109 btrfs_add_free_space(cache, start, len);
Yan Zhengd2fb3432008-12-11 16:30:39 -05003110 put_block_group(cache);
Zheng Yan1a40e232008-09-26 10:09:34 -04003111 update_reserved_extents(root, start, len, 0);
Chris Mason65b51a02008-08-01 15:11:20 -04003112 return 0;
3113}
3114
Chris Masone6dcd2d2008-07-17 12:53:50 -04003115int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
3116 struct btrfs_root *root,
3117 u64 num_bytes, u64 min_alloc_size,
3118 u64 empty_size, u64 hint_byte,
3119 u64 search_end, struct btrfs_key *ins,
3120 u64 data)
3121{
3122 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003123 ret = __btrfs_reserve_extent(trans, root, num_bytes, min_alloc_size,
3124 empty_size, hint_byte, search_end, ins,
3125 data);
Zheng Yane8569812008-09-26 10:05:48 -04003126 update_reserved_extents(root, ins->objectid, ins->offset, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003127 return ret;
3128}
3129
3130static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04003131 struct btrfs_root *root, u64 parent,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003132 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003133 u64 owner, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04003134{
3135 int ret;
3136 int pending_ret;
3137 u64 super_used;
3138 u64 root_used;
3139 u64 num_bytes = ins->offset;
3140 u32 sizes[2];
3141 struct btrfs_fs_info *info = root->fs_info;
3142 struct btrfs_root *extent_root = info->extent_root;
3143 struct btrfs_extent_item *extent_item;
3144 struct btrfs_extent_ref *ref;
3145 struct btrfs_path *path;
3146 struct btrfs_key keys[2];
Chris Masonf2654de2007-06-26 12:20:46 -04003147
Zheng Yan31840ae2008-09-23 13:14:14 -04003148 if (parent == 0)
3149 parent = ins->objectid;
3150
Josef Bacik58176a92007-08-29 15:47:34 -04003151 /* block accounting for super block */
Chris Masona2135012008-06-25 16:01:30 -04003152 spin_lock_irq(&info->delalloc_lock);
Chris Masondb945352007-10-15 16:15:53 -04003153 super_used = btrfs_super_bytes_used(&info->super_copy);
3154 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
Chris Masona2135012008-06-25 16:01:30 -04003155 spin_unlock_irq(&info->delalloc_lock);
Chris Mason26b80032007-08-08 20:17:12 -04003156
Josef Bacik58176a92007-08-29 15:47:34 -04003157 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04003158 root_used = btrfs_root_used(&root->root_item);
3159 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -04003160
Chris Mason26b80032007-08-08 20:17:12 -04003161 if (root == extent_root) {
Zheng Yan31840ae2008-09-23 13:14:14 -04003162 struct pending_extent_op *extent_op;
3163
3164 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
3165 BUG_ON(!extent_op);
3166
3167 extent_op->type = PENDING_EXTENT_INSERT;
3168 extent_op->bytenr = ins->objectid;
3169 extent_op->num_bytes = ins->offset;
3170 extent_op->parent = parent;
3171 extent_op->orig_parent = 0;
3172 extent_op->generation = ref_generation;
3173 extent_op->orig_generation = 0;
3174 extent_op->level = (int)owner;
Josef Bacikf3465ca2008-11-12 14:19:50 -05003175 INIT_LIST_HEAD(&extent_op->list);
3176 extent_op->del = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04003177
Josef Bacik25179202008-10-29 14:49:05 -04003178 mutex_lock(&root->fs_info->extent_ins_mutex);
Chris Mason1a5bc1672007-10-15 16:15:26 -04003179 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
3180 ins->objectid + ins->offset - 1,
Josef Bacik25179202008-10-29 14:49:05 -04003181 EXTENT_WRITEBACK, GFP_NOFS);
Zheng Yan31840ae2008-09-23 13:14:14 -04003182 set_state_private(&root->fs_info->extent_ins,
3183 ins->objectid, (unsigned long)extent_op);
Josef Bacik25179202008-10-29 14:49:05 -04003184 mutex_unlock(&root->fs_info->extent_ins_mutex);
Chris Mason26b80032007-08-08 20:17:12 -04003185 goto update_block;
3186 }
3187
Chris Mason47e4bb92008-02-01 14:51:59 -05003188 memcpy(&keys[0], ins, sizeof(*ins));
Chris Mason47e4bb92008-02-01 14:51:59 -05003189 keys[1].objectid = ins->objectid;
3190 keys[1].type = BTRFS_EXTENT_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04003191 keys[1].offset = parent;
Chris Mason47e4bb92008-02-01 14:51:59 -05003192 sizes[0] = sizeof(*extent_item);
3193 sizes[1] = sizeof(*ref);
Chris Mason7bb86312007-12-11 09:25:06 -05003194
3195 path = btrfs_alloc_path();
3196 BUG_ON(!path);
Chris Mason47e4bb92008-02-01 14:51:59 -05003197
3198 ret = btrfs_insert_empty_items(trans, extent_root, path, keys,
3199 sizes, 2);
Chris Masonccd467d2007-06-28 15:57:36 -04003200 BUG_ON(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003201
Chris Mason47e4bb92008-02-01 14:51:59 -05003202 extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3203 struct btrfs_extent_item);
3204 btrfs_set_extent_refs(path->nodes[0], extent_item, 1);
3205 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
3206 struct btrfs_extent_ref);
3207
3208 btrfs_set_ref_root(path->nodes[0], ref, root_objectid);
3209 btrfs_set_ref_generation(path->nodes[0], ref, ref_generation);
3210 btrfs_set_ref_objectid(path->nodes[0], ref, owner);
Zheng Yan31840ae2008-09-23 13:14:14 -04003211 btrfs_set_ref_num_refs(path->nodes[0], ref, 1);
Chris Mason47e4bb92008-02-01 14:51:59 -05003212
3213 btrfs_mark_buffer_dirty(path->nodes[0]);
3214
3215 trans->alloc_exclude_start = 0;
3216 trans->alloc_exclude_nr = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05003217 btrfs_free_path(path);
Chris Mason87ef2bb2008-10-30 11:23:27 -04003218 finish_current_insert(trans, extent_root, 0);
3219 pending_ret = del_pending_extents(trans, extent_root, 0);
Chris Masonf510cfe2007-10-15 16:14:48 -04003220
Chris Mason925baed2008-06-25 16:01:30 -04003221 if (ret)
3222 goto out;
Chris Masone37c9e62007-05-09 20:13:14 -04003223 if (pending_ret) {
Chris Mason925baed2008-06-25 16:01:30 -04003224 ret = pending_ret;
3225 goto out;
Chris Masone37c9e62007-05-09 20:13:14 -04003226 }
Chris Mason26b80032007-08-08 20:17:12 -04003227
3228update_block:
Chris Mason0b86a832008-03-24 15:01:56 -04003229 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0);
Chris Masonf5947062008-02-04 10:10:13 -05003230 if (ret) {
3231 printk("update block group failed for %Lu %Lu\n",
3232 ins->objectid, ins->offset);
3233 BUG();
3234 }
Chris Mason925baed2008-06-25 16:01:30 -04003235out:
Chris Masone6dcd2d2008-07-17 12:53:50 -04003236 return ret;
3237}
3238
3239int btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04003240 struct btrfs_root *root, u64 parent,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003241 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003242 u64 owner, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04003243{
3244 int ret;
Chris Mason1c2308f2008-09-23 13:14:13 -04003245
3246 if (root_objectid == BTRFS_TREE_LOG_OBJECTID)
3247 return 0;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003248 ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid,
3249 ref_generation, owner, ins);
Zheng Yane8569812008-09-26 10:05:48 -04003250 update_reserved_extents(root, ins->objectid, ins->offset, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003251 return ret;
3252}
Chris Masone02119d2008-09-05 16:13:11 -04003253
3254/*
3255 * this is used by the tree logging recovery code. It records that
3256 * an extent has been allocated and makes sure to clear the free
3257 * space cache bits as well
3258 */
3259int btrfs_alloc_logged_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04003260 struct btrfs_root *root, u64 parent,
Chris Masone02119d2008-09-05 16:13:11 -04003261 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003262 u64 owner, struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04003263{
3264 int ret;
3265 struct btrfs_block_group_cache *block_group;
3266
Chris Masone02119d2008-09-05 16:13:11 -04003267 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
Josef Bacikea6a4782008-11-20 12:16:16 -05003268 mutex_lock(&block_group->cache_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04003269 cache_block_group(root, block_group);
Josef Bacikea6a4782008-11-20 12:16:16 -05003270 mutex_unlock(&block_group->cache_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04003271
Josef Bacikea6a4782008-11-20 12:16:16 -05003272 ret = btrfs_remove_free_space(block_group, ins->objectid,
3273 ins->offset);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003274 BUG_ON(ret);
Yan Zhengd2fb3432008-12-11 16:30:39 -05003275 put_block_group(block_group);
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003276 ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid,
3277 ref_generation, owner, ins);
Chris Masone02119d2008-09-05 16:13:11 -04003278 return ret;
3279}
3280
Chris Masone6dcd2d2008-07-17 12:53:50 -04003281/*
3282 * finds a free extent and does all the dirty work required for allocation
3283 * returns the key for the extent through ins, and a tree buffer for
3284 * the first block of the extent through buf.
3285 *
3286 * returns 0 if everything worked, non-zero otherwise.
3287 */
3288int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
3289 struct btrfs_root *root,
Zheng Yan31840ae2008-09-23 13:14:14 -04003290 u64 num_bytes, u64 parent, u64 min_alloc_size,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003291 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003292 u64 owner_objectid, u64 empty_size, u64 hint_byte,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003293 u64 search_end, struct btrfs_key *ins, u64 data)
3294{
3295 int ret;
3296
Chris Masone6dcd2d2008-07-17 12:53:50 -04003297 ret = __btrfs_reserve_extent(trans, root, num_bytes,
3298 min_alloc_size, empty_size, hint_byte,
3299 search_end, ins, data);
3300 BUG_ON(ret);
Chris Masond00aff02008-09-11 15:54:42 -04003301 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
Zheng Yan31840ae2008-09-23 13:14:14 -04003302 ret = __btrfs_alloc_reserved_extent(trans, root, parent,
3303 root_objectid, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003304 owner_objectid, ins);
Chris Masond00aff02008-09-11 15:54:42 -04003305 BUG_ON(ret);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003306
Zheng Yane8569812008-09-26 10:05:48 -04003307 } else {
3308 update_reserved_extents(root, ins->objectid, ins->offset, 1);
Chris Masond00aff02008-09-11 15:54:42 -04003309 }
Chris Mason925baed2008-06-25 16:01:30 -04003310 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05003311}
Chris Mason65b51a02008-08-01 15:11:20 -04003312
3313struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
3314 struct btrfs_root *root,
3315 u64 bytenr, u32 blocksize)
3316{
3317 struct extent_buffer *buf;
3318
3319 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
3320 if (!buf)
3321 return ERR_PTR(-ENOMEM);
3322 btrfs_set_header_generation(buf, trans->transid);
3323 btrfs_tree_lock(buf);
3324 clean_tree_block(trans, root, buf);
3325 btrfs_set_buffer_uptodate(buf);
Chris Masond0c803c2008-09-11 16:17:57 -04003326 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
3327 set_extent_dirty(&root->dirty_log_pages, buf->start,
Chris Mason65b51a02008-08-01 15:11:20 -04003328 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04003329 } else {
3330 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
3331 buf->start + buf->len - 1, GFP_NOFS);
3332 }
Chris Mason65b51a02008-08-01 15:11:20 -04003333 trans->blocks_used++;
3334 return buf;
3335}
3336
Chris Masonfec577f2007-02-26 10:40:21 -05003337/*
3338 * helper function to allocate a block for a given tree
3339 * returns the tree buffer or NULL.
3340 */
Chris Mason5f39d392007-10-15 16:14:19 -04003341struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Masondb945352007-10-15 16:15:53 -04003342 struct btrfs_root *root,
Zheng Yan31840ae2008-09-23 13:14:14 -04003343 u32 blocksize, u64 parent,
Chris Mason7bb86312007-12-11 09:25:06 -05003344 u64 root_objectid,
3345 u64 ref_generation,
Chris Mason7bb86312007-12-11 09:25:06 -05003346 int level,
3347 u64 hint,
Chris Mason5f39d392007-10-15 16:14:19 -04003348 u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05003349{
Chris Masone2fa7222007-03-12 16:22:34 -04003350 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05003351 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04003352 struct extent_buffer *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05003353
Zheng Yan31840ae2008-09-23 13:14:14 -04003354 ret = btrfs_alloc_extent(trans, root, blocksize, parent, blocksize,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003355 root_objectid, ref_generation, level,
Zheng Yan31840ae2008-09-23 13:14:14 -04003356 empty_size, hint, (u64)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05003357 if (ret) {
Chris Mason54aa1f42007-06-22 14:16:25 -04003358 BUG_ON(ret > 0);
3359 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05003360 }
Chris Mason55c69072008-01-09 15:55:33 -05003361
Chris Mason65b51a02008-08-01 15:11:20 -04003362 buf = btrfs_init_new_buffer(trans, root, ins.objectid, blocksize);
Chris Masonfec577f2007-02-26 10:40:21 -05003363 return buf;
3364}
Chris Masona28ec192007-03-06 20:08:01 -05003365
Chris Masone02119d2008-09-05 16:13:11 -04003366int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans,
3367 struct btrfs_root *root, struct extent_buffer *leaf)
Chris Mason6407bf62007-03-27 06:33:00 -04003368{
Chris Mason7bb86312007-12-11 09:25:06 -05003369 u64 leaf_owner;
3370 u64 leaf_generation;
Chris Mason5f39d392007-10-15 16:14:19 -04003371 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04003372 struct btrfs_file_extent_item *fi;
3373 int i;
3374 int nritems;
3375 int ret;
3376
Chris Mason5f39d392007-10-15 16:14:19 -04003377 BUG_ON(!btrfs_is_leaf(leaf));
3378 nritems = btrfs_header_nritems(leaf);
Chris Mason7bb86312007-12-11 09:25:06 -05003379 leaf_owner = btrfs_header_owner(leaf);
3380 leaf_generation = btrfs_header_generation(leaf);
3381
Chris Mason6407bf62007-03-27 06:33:00 -04003382 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04003383 u64 disk_bytenr;
Chris Masone34a5b42008-07-22 12:08:37 -04003384 cond_resched();
Chris Mason5f39d392007-10-15 16:14:19 -04003385
3386 btrfs_item_key_to_cpu(leaf, &key, i);
3387 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -04003388 continue;
3389 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003390 if (btrfs_file_extent_type(leaf, fi) ==
3391 BTRFS_FILE_EXTENT_INLINE)
Chris Mason236454df2007-04-19 13:37:44 -04003392 continue;
Chris Mason6407bf62007-03-27 06:33:00 -04003393 /*
3394 * FIXME make sure to insert a trans record that
3395 * repeats the snapshot del on crash
3396 */
Chris Masondb945352007-10-15 16:15:53 -04003397 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
3398 if (disk_bytenr == 0)
Chris Mason3a686372007-05-24 13:35:57 -04003399 continue;
Chris Mason4a096752008-07-21 10:29:44 -04003400
Chris Mason925baed2008-06-25 16:01:30 -04003401 ret = __btrfs_free_extent(trans, root, disk_bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -05003402 btrfs_file_extent_disk_num_bytes(leaf, fi),
Zheng Yan31840ae2008-09-23 13:14:14 -04003403 leaf->start, leaf_owner, leaf_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003404 key.objectid, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04003405 BUG_ON(ret);
Chris Mason2dd3e672008-08-04 08:20:15 -04003406
3407 atomic_inc(&root->fs_info->throttle_gen);
3408 wake_up(&root->fs_info->transaction_throttle);
3409 cond_resched();
Chris Mason6407bf62007-03-27 06:33:00 -04003410 }
3411 return 0;
3412}
3413
Chris Masone02119d2008-09-05 16:13:11 -04003414static int noinline cache_drop_leaf_ref(struct btrfs_trans_handle *trans,
3415 struct btrfs_root *root,
3416 struct btrfs_leaf_ref *ref)
Yan Zheng31153d82008-07-28 15:32:19 -04003417{
3418 int i;
3419 int ret;
3420 struct btrfs_extent_info *info = ref->extents;
3421
Yan Zheng31153d82008-07-28 15:32:19 -04003422 for (i = 0; i < ref->nritems; i++) {
Zheng Yan31840ae2008-09-23 13:14:14 -04003423 ret = __btrfs_free_extent(trans, root, info->bytenr,
3424 info->num_bytes, ref->bytenr,
3425 ref->owner, ref->generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003426 info->objectid, 0);
Chris Mason2dd3e672008-08-04 08:20:15 -04003427
3428 atomic_inc(&root->fs_info->throttle_gen);
3429 wake_up(&root->fs_info->transaction_throttle);
3430 cond_resched();
3431
Yan Zheng31153d82008-07-28 15:32:19 -04003432 BUG_ON(ret);
3433 info++;
3434 }
Yan Zheng31153d82008-07-28 15:32:19 -04003435
3436 return 0;
3437}
3438
Christoph Hellwigb2950862008-12-02 09:54:17 -05003439static int drop_snap_lookup_refcount(struct btrfs_root *root, u64 start, u64 len,
Chris Mason333db942008-06-25 16:01:30 -04003440 u32 *refs)
3441{
Chris Mason017e5362008-07-28 15:32:51 -04003442 int ret;
Chris Masonf87f0572008-08-01 11:27:23 -04003443
Zheng Yan31840ae2008-09-23 13:14:14 -04003444 ret = btrfs_lookup_extent_ref(NULL, root, start, len, refs);
Chris Masonf87f0572008-08-01 11:27:23 -04003445 BUG_ON(ret);
3446
3447#if 0 // some debugging code in case we see problems here
3448 /* if the refs count is one, it won't get increased again. But
3449 * if the ref count is > 1, someone may be decreasing it at
3450 * the same time we are.
3451 */
3452 if (*refs != 1) {
3453 struct extent_buffer *eb = NULL;
3454 eb = btrfs_find_create_tree_block(root, start, len);
3455 if (eb)
3456 btrfs_tree_lock(eb);
3457
3458 mutex_lock(&root->fs_info->alloc_mutex);
3459 ret = lookup_extent_ref(NULL, root, start, len, refs);
3460 BUG_ON(ret);
3461 mutex_unlock(&root->fs_info->alloc_mutex);
3462
3463 if (eb) {
3464 btrfs_tree_unlock(eb);
3465 free_extent_buffer(eb);
3466 }
3467 if (*refs == 1) {
3468 printk("block %llu went down to one during drop_snap\n",
3469 (unsigned long long)start);
3470 }
3471
3472 }
3473#endif
3474
Chris Masone7a84562008-06-25 16:01:31 -04003475 cond_resched();
Chris Mason017e5362008-07-28 15:32:51 -04003476 return ret;
Chris Mason333db942008-06-25 16:01:30 -04003477}
3478
3479/*
Chris Mason9aca1d52007-03-13 11:09:37 -04003480 * helper function for drop_snapshot, this walks down the tree dropping ref
3481 * counts as it goes.
3482 */
Chris Mason98ed5172008-01-03 10:01:48 -05003483static int noinline walk_down_tree(struct btrfs_trans_handle *trans,
3484 struct btrfs_root *root,
3485 struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05003486{
Chris Mason7bb86312007-12-11 09:25:06 -05003487 u64 root_owner;
3488 u64 root_gen;
3489 u64 bytenr;
Chris Masonca7a79a2008-05-12 12:59:19 -04003490 u64 ptr_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04003491 struct extent_buffer *next;
3492 struct extent_buffer *cur;
Chris Mason7bb86312007-12-11 09:25:06 -05003493 struct extent_buffer *parent;
Yan Zheng31153d82008-07-28 15:32:19 -04003494 struct btrfs_leaf_ref *ref;
Chris Masondb945352007-10-15 16:15:53 -04003495 u32 blocksize;
Chris Mason20524f02007-03-10 06:35:47 -05003496 int ret;
3497 u32 refs;
3498
Chris Mason5caf2a02007-04-02 11:20:42 -04003499 WARN_ON(*level < 0);
3500 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason333db942008-06-25 16:01:30 -04003501 ret = drop_snap_lookup_refcount(root, path->nodes[*level]->start,
Chris Masondb945352007-10-15 16:15:53 -04003502 path->nodes[*level]->len, &refs);
Chris Mason20524f02007-03-10 06:35:47 -05003503 BUG_ON(ret);
3504 if (refs > 1)
3505 goto out;
Chris Masone0115992007-06-19 16:23:05 -04003506
Chris Mason9aca1d52007-03-13 11:09:37 -04003507 /*
3508 * walk down to the last node level and free all the leaves
3509 */
Chris Mason6407bf62007-03-27 06:33:00 -04003510 while(*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04003511 WARN_ON(*level < 0);
3512 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -05003513 cur = path->nodes[*level];
Chris Masone0115992007-06-19 16:23:05 -04003514
Chris Mason5f39d392007-10-15 16:14:19 -04003515 if (btrfs_header_level(cur) != *level)
Chris Mason2c90e5d2007-04-02 10:50:19 -04003516 WARN_ON(1);
Chris Masone0115992007-06-19 16:23:05 -04003517
Chris Mason7518a232007-03-12 12:01:18 -04003518 if (path->slots[*level] >=
Chris Mason5f39d392007-10-15 16:14:19 -04003519 btrfs_header_nritems(cur))
Chris Mason20524f02007-03-10 06:35:47 -05003520 break;
Chris Mason6407bf62007-03-27 06:33:00 -04003521 if (*level == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003522 ret = btrfs_drop_leaf_ref(trans, root, cur);
Chris Mason6407bf62007-03-27 06:33:00 -04003523 BUG_ON(ret);
3524 break;
3525 }
Chris Masondb945352007-10-15 16:15:53 -04003526 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
Chris Masonca7a79a2008-05-12 12:59:19 -04003527 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
Chris Masondb945352007-10-15 16:15:53 -04003528 blocksize = btrfs_level_size(root, *level - 1);
Chris Mason925baed2008-06-25 16:01:30 -04003529
Chris Mason333db942008-06-25 16:01:30 -04003530 ret = drop_snap_lookup_refcount(root, bytenr, blocksize, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -04003531 BUG_ON(ret);
3532 if (refs != 1) {
Chris Mason7bb86312007-12-11 09:25:06 -05003533 parent = path->nodes[*level];
3534 root_owner = btrfs_header_owner(parent);
3535 root_gen = btrfs_header_generation(parent);
Chris Mason20524f02007-03-10 06:35:47 -05003536 path->slots[*level]++;
Chris Masonf87f0572008-08-01 11:27:23 -04003537
Chris Mason925baed2008-06-25 16:01:30 -04003538 ret = __btrfs_free_extent(trans, root, bytenr,
Zheng Yan31840ae2008-09-23 13:14:14 -04003539 blocksize, parent->start,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003540 root_owner, root_gen,
3541 *level - 1, 1);
Chris Mason20524f02007-03-10 06:35:47 -05003542 BUG_ON(ret);
Chris Mason18e35e0a2008-08-01 13:11:41 -04003543
3544 atomic_inc(&root->fs_info->throttle_gen);
3545 wake_up(&root->fs_info->transaction_throttle);
Chris Mason2dd3e672008-08-04 08:20:15 -04003546 cond_resched();
Chris Mason18e35e0a2008-08-01 13:11:41 -04003547
Chris Mason20524f02007-03-10 06:35:47 -05003548 continue;
3549 }
Chris Masonf87f0572008-08-01 11:27:23 -04003550 /*
3551 * at this point, we have a single ref, and since the
3552 * only place referencing this extent is a dead root
3553 * the reference count should never go higher.
3554 * So, we don't need to check it again
3555 */
Yan Zheng31153d82008-07-28 15:32:19 -04003556 if (*level == 1) {
Chris Mason017e5362008-07-28 15:32:51 -04003557 ref = btrfs_lookup_leaf_ref(root, bytenr);
Zheng Yan1a40e232008-09-26 10:09:34 -04003558 if (ref && ref->generation != ptr_gen) {
3559 btrfs_free_leaf_ref(root, ref);
3560 ref = NULL;
3561 }
Yan Zheng31153d82008-07-28 15:32:19 -04003562 if (ref) {
Chris Masone02119d2008-09-05 16:13:11 -04003563 ret = cache_drop_leaf_ref(trans, root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04003564 BUG_ON(ret);
3565 btrfs_remove_leaf_ref(root, ref);
Yanbcc63ab2008-07-30 16:29:20 -04003566 btrfs_free_leaf_ref(root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04003567 *level = 0;
3568 break;
3569 }
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003570 if (printk_ratelimit()) {
Chris Mason37d1aee2008-07-31 10:48:37 -04003571 printk("leaf ref miss for bytenr %llu\n",
3572 (unsigned long long)bytenr);
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003573 }
Yan Zheng31153d82008-07-28 15:32:19 -04003574 }
Chris Masondb945352007-10-15 16:15:53 -04003575 next = btrfs_find_tree_block(root, bytenr, blocksize);
Chris Mason1259ab72008-05-12 13:39:03 -04003576 if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
Chris Mason5f39d392007-10-15 16:14:19 -04003577 free_extent_buffer(next);
Chris Mason333db942008-06-25 16:01:30 -04003578
Chris Masonca7a79a2008-05-12 12:59:19 -04003579 next = read_tree_block(root, bytenr, blocksize,
3580 ptr_gen);
Chris Masone7a84562008-06-25 16:01:31 -04003581 cond_resched();
Chris Masonf87f0572008-08-01 11:27:23 -04003582#if 0
3583 /*
3584 * this is a debugging check and can go away
3585 * the ref should never go all the way down to 1
3586 * at this point
3587 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04003588 ret = lookup_extent_ref(NULL, root, bytenr, blocksize,
3589 &refs);
Chris Masone9d0b132007-08-10 14:06:19 -04003590 BUG_ON(ret);
Chris Masonf87f0572008-08-01 11:27:23 -04003591 WARN_ON(refs != 1);
3592#endif
Chris Masone9d0b132007-08-10 14:06:19 -04003593 }
Chris Mason5caf2a02007-04-02 11:20:42 -04003594 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -04003595 if (path->nodes[*level-1])
Chris Mason5f39d392007-10-15 16:14:19 -04003596 free_extent_buffer(path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -05003597 path->nodes[*level-1] = next;
Chris Mason5f39d392007-10-15 16:14:19 -04003598 *level = btrfs_header_level(next);
Chris Mason20524f02007-03-10 06:35:47 -05003599 path->slots[*level] = 0;
Chris Mason2dd3e672008-08-04 08:20:15 -04003600 cond_resched();
Chris Mason20524f02007-03-10 06:35:47 -05003601 }
3602out:
Chris Mason5caf2a02007-04-02 11:20:42 -04003603 WARN_ON(*level < 0);
3604 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason7bb86312007-12-11 09:25:06 -05003605
3606 if (path->nodes[*level] == root->node) {
Chris Mason7bb86312007-12-11 09:25:06 -05003607 parent = path->nodes[*level];
Yan Zheng31153d82008-07-28 15:32:19 -04003608 bytenr = path->nodes[*level]->start;
Chris Mason7bb86312007-12-11 09:25:06 -05003609 } else {
3610 parent = path->nodes[*level + 1];
Yan Zheng31153d82008-07-28 15:32:19 -04003611 bytenr = btrfs_node_blockptr(parent, path->slots[*level + 1]);
Chris Mason7bb86312007-12-11 09:25:06 -05003612 }
3613
Yan Zheng31153d82008-07-28 15:32:19 -04003614 blocksize = btrfs_level_size(root, *level);
3615 root_owner = btrfs_header_owner(parent);
Chris Mason7bb86312007-12-11 09:25:06 -05003616 root_gen = btrfs_header_generation(parent);
Yan Zheng31153d82008-07-28 15:32:19 -04003617
3618 ret = __btrfs_free_extent(trans, root, bytenr, blocksize,
Zheng Yan31840ae2008-09-23 13:14:14 -04003619 parent->start, root_owner, root_gen,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003620 *level, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003621 free_extent_buffer(path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -05003622 path->nodes[*level] = NULL;
3623 *level += 1;
3624 BUG_ON(ret);
Chris Masonf87f0572008-08-01 11:27:23 -04003625
Chris Masone7a84562008-06-25 16:01:31 -04003626 cond_resched();
Chris Mason20524f02007-03-10 06:35:47 -05003627 return 0;
3628}
3629
Chris Mason9aca1d52007-03-13 11:09:37 -04003630/*
Yan Zhengf82d02d2008-10-29 14:49:05 -04003631 * helper function for drop_subtree, this function is similar to
3632 * walk_down_tree. The main difference is that it checks reference
3633 * counts while tree blocks are locked.
3634 */
3635static int noinline walk_down_subtree(struct btrfs_trans_handle *trans,
3636 struct btrfs_root *root,
3637 struct btrfs_path *path, int *level)
3638{
3639 struct extent_buffer *next;
3640 struct extent_buffer *cur;
3641 struct extent_buffer *parent;
3642 u64 bytenr;
3643 u64 ptr_gen;
3644 u32 blocksize;
3645 u32 refs;
3646 int ret;
3647
3648 cur = path->nodes[*level];
3649 ret = btrfs_lookup_extent_ref(trans, root, cur->start, cur->len,
3650 &refs);
3651 BUG_ON(ret);
3652 if (refs > 1)
3653 goto out;
3654
3655 while (*level >= 0) {
3656 cur = path->nodes[*level];
3657 if (*level == 0) {
3658 ret = btrfs_drop_leaf_ref(trans, root, cur);
3659 BUG_ON(ret);
3660 clean_tree_block(trans, root, cur);
3661 break;
3662 }
3663 if (path->slots[*level] >= btrfs_header_nritems(cur)) {
3664 clean_tree_block(trans, root, cur);
3665 break;
3666 }
3667
3668 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
3669 blocksize = btrfs_level_size(root, *level - 1);
3670 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
3671
3672 next = read_tree_block(root, bytenr, blocksize, ptr_gen);
3673 btrfs_tree_lock(next);
3674
3675 ret = btrfs_lookup_extent_ref(trans, root, bytenr, blocksize,
3676 &refs);
3677 BUG_ON(ret);
3678 if (refs > 1) {
3679 parent = path->nodes[*level];
3680 ret = btrfs_free_extent(trans, root, bytenr,
3681 blocksize, parent->start,
3682 btrfs_header_owner(parent),
3683 btrfs_header_generation(parent),
3684 *level - 1, 1);
3685 BUG_ON(ret);
3686 path->slots[*level]++;
3687 btrfs_tree_unlock(next);
3688 free_extent_buffer(next);
3689 continue;
3690 }
3691
3692 *level = btrfs_header_level(next);
3693 path->nodes[*level] = next;
3694 path->slots[*level] = 0;
3695 path->locks[*level] = 1;
3696 cond_resched();
3697 }
3698out:
3699 parent = path->nodes[*level + 1];
3700 bytenr = path->nodes[*level]->start;
3701 blocksize = path->nodes[*level]->len;
3702
3703 ret = btrfs_free_extent(trans, root, bytenr, blocksize,
3704 parent->start, btrfs_header_owner(parent),
3705 btrfs_header_generation(parent), *level, 1);
3706 BUG_ON(ret);
3707
3708 if (path->locks[*level]) {
3709 btrfs_tree_unlock(path->nodes[*level]);
3710 path->locks[*level] = 0;
3711 }
3712 free_extent_buffer(path->nodes[*level]);
3713 path->nodes[*level] = NULL;
3714 *level += 1;
3715 cond_resched();
3716 return 0;
3717}
3718
3719/*
Chris Mason9aca1d52007-03-13 11:09:37 -04003720 * helper for dropping snapshots. This walks back up the tree in the path
3721 * to find the first node higher up where we haven't yet gone through
3722 * all the slots
3723 */
Chris Mason98ed5172008-01-03 10:01:48 -05003724static int noinline walk_up_tree(struct btrfs_trans_handle *trans,
3725 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04003726 struct btrfs_path *path,
3727 int *level, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05003728{
Chris Mason7bb86312007-12-11 09:25:06 -05003729 u64 root_owner;
3730 u64 root_gen;
3731 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05003732 int i;
3733 int slot;
3734 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04003735
Yan Zhengf82d02d2008-10-29 14:49:05 -04003736 for (i = *level; i < max_level && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -05003737 slot = path->slots[i];
Chris Mason5f39d392007-10-15 16:14:19 -04003738 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
3739 struct extent_buffer *node;
3740 struct btrfs_disk_key disk_key;
3741 node = path->nodes[i];
Chris Mason20524f02007-03-10 06:35:47 -05003742 path->slots[i]++;
3743 *level = i;
Chris Mason9f3a7422007-08-07 15:52:19 -04003744 WARN_ON(*level == 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003745 btrfs_node_key(node, &disk_key, path->slots[i]);
Chris Mason9f3a7422007-08-07 15:52:19 -04003746 memcpy(&root_item->drop_progress,
Chris Mason5f39d392007-10-15 16:14:19 -04003747 &disk_key, sizeof(disk_key));
Chris Mason9f3a7422007-08-07 15:52:19 -04003748 root_item->drop_level = i;
Chris Mason20524f02007-03-10 06:35:47 -05003749 return 0;
3750 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04003751 struct extent_buffer *parent;
3752 if (path->nodes[*level] == root->node)
3753 parent = path->nodes[*level];
3754 else
3755 parent = path->nodes[*level + 1];
3756
3757 root_owner = btrfs_header_owner(parent);
3758 root_gen = btrfs_header_generation(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04003759
3760 clean_tree_block(trans, root, path->nodes[*level]);
Chris Masone089f052007-03-16 16:20:31 -04003761 ret = btrfs_free_extent(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04003762 path->nodes[*level]->start,
Chris Mason7bb86312007-12-11 09:25:06 -05003763 path->nodes[*level]->len,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003764 parent->start, root_owner,
3765 root_gen, *level, 1);
Chris Mason6407bf62007-03-27 06:33:00 -04003766 BUG_ON(ret);
Yan Zhengf82d02d2008-10-29 14:49:05 -04003767 if (path->locks[*level]) {
3768 btrfs_tree_unlock(path->nodes[*level]);
3769 path->locks[*level] = 0;
3770 }
Chris Mason5f39d392007-10-15 16:14:19 -04003771 free_extent_buffer(path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -04003772 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -05003773 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -05003774 }
3775 }
3776 return 1;
3777}
3778
Chris Mason9aca1d52007-03-13 11:09:37 -04003779/*
3780 * drop the reference count on the tree rooted at 'snap'. This traverses
3781 * the tree freeing any blocks that have a ref count of zero after being
3782 * decremented.
3783 */
Chris Masone089f052007-03-16 16:20:31 -04003784int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason9f3a7422007-08-07 15:52:19 -04003785 *root)
Chris Mason20524f02007-03-10 06:35:47 -05003786{
Chris Mason3768f362007-03-13 16:47:54 -04003787 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -04003788 int wret;
Chris Mason20524f02007-03-10 06:35:47 -05003789 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -04003790 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -05003791 int i;
3792 int orig_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04003793 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05003794
Chris Masona2135012008-06-25 16:01:30 -04003795 WARN_ON(!mutex_is_locked(&root->fs_info->drop_mutex));
Chris Mason5caf2a02007-04-02 11:20:42 -04003796 path = btrfs_alloc_path();
3797 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05003798
Chris Mason5f39d392007-10-15 16:14:19 -04003799 level = btrfs_header_level(root->node);
Chris Mason20524f02007-03-10 06:35:47 -05003800 orig_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04003801 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
3802 path->nodes[level] = root->node;
Chris Masonf510cfe2007-10-15 16:14:48 -04003803 extent_buffer_get(root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -04003804 path->slots[level] = 0;
3805 } else {
3806 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04003807 struct btrfs_disk_key found_key;
3808 struct extent_buffer *node;
Chris Mason6702ed42007-08-07 16:15:09 -04003809
Chris Mason9f3a7422007-08-07 15:52:19 -04003810 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Chris Mason6702ed42007-08-07 16:15:09 -04003811 level = root_item->drop_level;
3812 path->lowest_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04003813 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason6702ed42007-08-07 16:15:09 -04003814 if (wret < 0) {
Chris Mason9f3a7422007-08-07 15:52:19 -04003815 ret = wret;
3816 goto out;
3817 }
Chris Mason5f39d392007-10-15 16:14:19 -04003818 node = path->nodes[level];
3819 btrfs_node_key(node, &found_key, path->slots[level]);
3820 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
3821 sizeof(found_key)));
Chris Mason7d9eb122008-07-08 14:19:17 -04003822 /*
3823 * unlock our path, this is safe because only this
3824 * function is allowed to delete this snapshot
3825 */
Chris Mason925baed2008-06-25 16:01:30 -04003826 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
3827 if (path->nodes[i] && path->locks[i]) {
3828 path->locks[i] = 0;
3829 btrfs_tree_unlock(path->nodes[i]);
3830 }
3831 }
Chris Mason9f3a7422007-08-07 15:52:19 -04003832 }
Chris Mason20524f02007-03-10 06:35:47 -05003833 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -04003834 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04003835 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05003836 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04003837 if (wret < 0)
3838 ret = wret;
3839
Yan Zhengf82d02d2008-10-29 14:49:05 -04003840 wret = walk_up_tree(trans, root, path, &level,
3841 BTRFS_MAX_LEVEL);
Chris Mason9aca1d52007-03-13 11:09:37 -04003842 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05003843 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04003844 if (wret < 0)
3845 ret = wret;
Chris Masone7a84562008-06-25 16:01:31 -04003846 if (trans->transaction->in_commit) {
3847 ret = -EAGAIN;
3848 break;
3849 }
Chris Mason18e35e0a2008-08-01 13:11:41 -04003850 atomic_inc(&root->fs_info->throttle_gen);
Chris Mason017e5362008-07-28 15:32:51 -04003851 wake_up(&root->fs_info->transaction_throttle);
Chris Mason20524f02007-03-10 06:35:47 -05003852 }
Chris Mason83e15a22007-03-12 09:03:27 -04003853 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -04003854 if (path->nodes[i]) {
Chris Mason5f39d392007-10-15 16:14:19 -04003855 free_extent_buffer(path->nodes[i]);
Chris Mason0f827312007-10-15 16:18:56 -04003856 path->nodes[i] = NULL;
Chris Mason83e15a22007-03-12 09:03:27 -04003857 }
Chris Mason20524f02007-03-10 06:35:47 -05003858 }
Chris Mason9f3a7422007-08-07 15:52:19 -04003859out:
Chris Mason5caf2a02007-04-02 11:20:42 -04003860 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -04003861 return ret;
Chris Mason20524f02007-03-10 06:35:47 -05003862}
Chris Mason9078a3e2007-04-26 16:46:15 -04003863
Yan Zhengf82d02d2008-10-29 14:49:05 -04003864int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
3865 struct btrfs_root *root,
3866 struct extent_buffer *node,
3867 struct extent_buffer *parent)
3868{
3869 struct btrfs_path *path;
3870 int level;
3871 int parent_level;
3872 int ret = 0;
3873 int wret;
3874
3875 path = btrfs_alloc_path();
3876 BUG_ON(!path);
3877
3878 BUG_ON(!btrfs_tree_locked(parent));
3879 parent_level = btrfs_header_level(parent);
3880 extent_buffer_get(parent);
3881 path->nodes[parent_level] = parent;
3882 path->slots[parent_level] = btrfs_header_nritems(parent);
3883
3884 BUG_ON(!btrfs_tree_locked(node));
3885 level = btrfs_header_level(node);
3886 extent_buffer_get(node);
3887 path->nodes[level] = node;
3888 path->slots[level] = 0;
3889
3890 while (1) {
3891 wret = walk_down_subtree(trans, root, path, &level);
3892 if (wret < 0)
3893 ret = wret;
3894 if (wret != 0)
3895 break;
3896
3897 wret = walk_up_tree(trans, root, path, &level, parent_level);
3898 if (wret < 0)
3899 ret = wret;
3900 if (wret != 0)
3901 break;
3902 }
3903
3904 btrfs_free_path(path);
3905 return ret;
3906}
3907
Chris Mason8e7bf942008-04-28 09:02:36 -04003908static unsigned long calc_ra(unsigned long start, unsigned long last,
3909 unsigned long nr)
3910{
3911 return min(last, start + nr - 1);
3912}
3913
Chris Mason98ed5172008-01-03 10:01:48 -05003914static int noinline relocate_inode_pages(struct inode *inode, u64 start,
3915 u64 len)
Chris Masonedbd8d42007-12-21 16:27:24 -05003916{
3917 u64 page_start;
3918 u64 page_end;
Zheng Yan1a40e232008-09-26 10:09:34 -04003919 unsigned long first_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05003920 unsigned long last_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05003921 unsigned long i;
3922 struct page *page;
Chris Masond1310b22008-01-24 16:13:08 -05003923 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason4313b392008-01-03 09:08:48 -05003924 struct file_ra_state *ra;
Chris Mason3eaa2882008-07-24 11:57:52 -04003925 struct btrfs_ordered_extent *ordered;
Zheng Yan1a40e232008-09-26 10:09:34 -04003926 unsigned int total_read = 0;
3927 unsigned int total_dirty = 0;
3928 int ret = 0;
Chris Mason4313b392008-01-03 09:08:48 -05003929
3930 ra = kzalloc(sizeof(*ra), GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05003931
3932 mutex_lock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04003933 first_index = start >> PAGE_CACHE_SHIFT;
Chris Masonedbd8d42007-12-21 16:27:24 -05003934 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
3935
Zheng Yan1a40e232008-09-26 10:09:34 -04003936 /* make sure the dirty trick played by the caller work */
3937 ret = invalidate_inode_pages2_range(inode->i_mapping,
3938 first_index, last_index);
3939 if (ret)
3940 goto out_unlock;
Chris Mason8e7bf942008-04-28 09:02:36 -04003941
Chris Mason4313b392008-01-03 09:08:48 -05003942 file_ra_state_init(ra, inode->i_mapping);
Chris Masonedbd8d42007-12-21 16:27:24 -05003943
Zheng Yan1a40e232008-09-26 10:09:34 -04003944 for (i = first_index ; i <= last_index; i++) {
3945 if (total_read % ra->ra_pages == 0) {
Chris Mason8e7bf942008-04-28 09:02:36 -04003946 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
Zheng Yan1a40e232008-09-26 10:09:34 -04003947 calc_ra(i, last_index, ra->ra_pages));
Chris Mason8e7bf942008-04-28 09:02:36 -04003948 }
3949 total_read++;
Chris Mason3eaa2882008-07-24 11:57:52 -04003950again:
3951 if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
Zheng Yan1a40e232008-09-26 10:09:34 -04003952 BUG_ON(1);
Chris Masonedbd8d42007-12-21 16:27:24 -05003953 page = grab_cache_page(inode->i_mapping, i);
Chris Masona061fc82008-05-07 11:43:44 -04003954 if (!page) {
Zheng Yan1a40e232008-09-26 10:09:34 -04003955 ret = -ENOMEM;
Chris Masonedbd8d42007-12-21 16:27:24 -05003956 goto out_unlock;
Chris Masona061fc82008-05-07 11:43:44 -04003957 }
Chris Masonedbd8d42007-12-21 16:27:24 -05003958 if (!PageUptodate(page)) {
3959 btrfs_readpage(NULL, page);
3960 lock_page(page);
3961 if (!PageUptodate(page)) {
3962 unlock_page(page);
3963 page_cache_release(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04003964 ret = -EIO;
Chris Masonedbd8d42007-12-21 16:27:24 -05003965 goto out_unlock;
3966 }
3967 }
Chris Masonec44a352008-04-28 15:29:52 -04003968 wait_on_page_writeback(page);
Chris Mason3eaa2882008-07-24 11:57:52 -04003969
Chris Masonedbd8d42007-12-21 16:27:24 -05003970 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
3971 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05003972 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05003973
Chris Mason3eaa2882008-07-24 11:57:52 -04003974 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3975 if (ordered) {
3976 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3977 unlock_page(page);
3978 page_cache_release(page);
3979 btrfs_start_ordered_extent(inode, ordered, 1);
3980 btrfs_put_ordered_extent(ordered);
3981 goto again;
3982 }
3983 set_page_extent_mapped(page);
3984
Chris Masonea8c2812008-08-04 23:17:27 -04003985 btrfs_set_extent_delalloc(inode, page_start, page_end);
Zheng Yan1a40e232008-09-26 10:09:34 -04003986 if (i == first_index)
3987 set_extent_bits(io_tree, page_start, page_end,
3988 EXTENT_BOUNDARY, GFP_NOFS);
3989
Chris Masona061fc82008-05-07 11:43:44 -04003990 set_page_dirty(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04003991 total_dirty++;
Chris Masonedbd8d42007-12-21 16:27:24 -05003992
Chris Masond1310b22008-01-24 16:13:08 -05003993 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05003994 unlock_page(page);
3995 page_cache_release(page);
3996 }
3997
3998out_unlock:
Chris Masonec44a352008-04-28 15:29:52 -04003999 kfree(ra);
Chris Masonedbd8d42007-12-21 16:27:24 -05004000 mutex_unlock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04004001 balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty);
Chris Masonbf4ef672008-05-08 13:26:18 -04004002 return ret;
4003}
4004
Zheng Yan1a40e232008-09-26 10:09:34 -04004005static int noinline relocate_data_extent(struct inode *reloc_inode,
4006 struct btrfs_key *extent_key,
4007 u64 offset)
Chris Masonedbd8d42007-12-21 16:27:24 -05004008{
Zheng Yan1a40e232008-09-26 10:09:34 -04004009 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
4010 struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree;
4011 struct extent_map *em;
Yan Zheng66435582008-10-30 14:19:50 -04004012 u64 start = extent_key->objectid - offset;
4013 u64 end = start + extent_key->offset - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04004014
4015 em = alloc_extent_map(GFP_NOFS);
4016 BUG_ON(!em || IS_ERR(em));
4017
Yan Zheng66435582008-10-30 14:19:50 -04004018 em->start = start;
Zheng Yan1a40e232008-09-26 10:09:34 -04004019 em->len = extent_key->offset;
Chris Masonc8b97812008-10-29 14:49:59 -04004020 em->block_len = extent_key->offset;
Zheng Yan1a40e232008-09-26 10:09:34 -04004021 em->block_start = extent_key->objectid;
4022 em->bdev = root->fs_info->fs_devices->latest_bdev;
4023 set_bit(EXTENT_FLAG_PINNED, &em->flags);
4024
4025 /* setup extent map to cheat btrfs_readpage */
Yan Zheng66435582008-10-30 14:19:50 -04004026 lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04004027 while (1) {
4028 int ret;
4029 spin_lock(&em_tree->lock);
4030 ret = add_extent_mapping(em_tree, em);
4031 spin_unlock(&em_tree->lock);
4032 if (ret != -EEXIST) {
4033 free_extent_map(em);
4034 break;
4035 }
Yan Zheng66435582008-10-30 14:19:50 -04004036 btrfs_drop_extent_cache(reloc_inode, start, end, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04004037 }
Yan Zheng66435582008-10-30 14:19:50 -04004038 unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04004039
Yan Zheng66435582008-10-30 14:19:50 -04004040 return relocate_inode_pages(reloc_inode, start, extent_key->offset);
Zheng Yan1a40e232008-09-26 10:09:34 -04004041}
4042
4043struct btrfs_ref_path {
4044 u64 extent_start;
4045 u64 nodes[BTRFS_MAX_LEVEL];
4046 u64 root_objectid;
4047 u64 root_generation;
4048 u64 owner_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04004049 u32 num_refs;
4050 int lowest_level;
4051 int current_level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004052 int shared_level;
4053
4054 struct btrfs_key node_keys[BTRFS_MAX_LEVEL];
4055 u64 new_nodes[BTRFS_MAX_LEVEL];
Zheng Yan1a40e232008-09-26 10:09:34 -04004056};
4057
4058struct disk_extent {
Chris Masonc8b97812008-10-29 14:49:59 -04004059 u64 ram_bytes;
Zheng Yan1a40e232008-09-26 10:09:34 -04004060 u64 disk_bytenr;
4061 u64 disk_num_bytes;
4062 u64 offset;
4063 u64 num_bytes;
Chris Masonc8b97812008-10-29 14:49:59 -04004064 u8 compression;
4065 u8 encryption;
4066 u16 other_encoding;
Zheng Yan1a40e232008-09-26 10:09:34 -04004067};
4068
4069static int is_cowonly_root(u64 root_objectid)
4070{
4071 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
4072 root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
4073 root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
4074 root_objectid == BTRFS_DEV_TREE_OBJECTID ||
Yan Zheng0403e472008-12-10 20:32:51 -05004075 root_objectid == BTRFS_TREE_LOG_OBJECTID ||
4076 root_objectid == BTRFS_CSUM_TREE_OBJECTID)
Zheng Yan1a40e232008-09-26 10:09:34 -04004077 return 1;
4078 return 0;
4079}
4080
4081static int noinline __next_ref_path(struct btrfs_trans_handle *trans,
4082 struct btrfs_root *extent_root,
4083 struct btrfs_ref_path *ref_path,
4084 int first_time)
4085{
4086 struct extent_buffer *leaf;
4087 struct btrfs_path *path;
Chris Mason4313b392008-01-03 09:08:48 -05004088 struct btrfs_extent_ref *ref;
Chris Masonedbd8d42007-12-21 16:27:24 -05004089 struct btrfs_key key;
4090 struct btrfs_key found_key;
Zheng Yan1a40e232008-09-26 10:09:34 -04004091 u64 bytenr;
Chris Masonedbd8d42007-12-21 16:27:24 -05004092 u32 nritems;
Zheng Yan1a40e232008-09-26 10:09:34 -04004093 int level;
4094 int ret = 1;
Chris Masonedbd8d42007-12-21 16:27:24 -05004095
Zheng Yan1a40e232008-09-26 10:09:34 -04004096 path = btrfs_alloc_path();
4097 if (!path)
4098 return -ENOMEM;
4099
Zheng Yan1a40e232008-09-26 10:09:34 -04004100 if (first_time) {
4101 ref_path->lowest_level = -1;
4102 ref_path->current_level = -1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004103 ref_path->shared_level = -1;
Zheng Yan1a40e232008-09-26 10:09:34 -04004104 goto walk_up;
Chris Masona061fc82008-05-07 11:43:44 -04004105 }
Zheng Yan1a40e232008-09-26 10:09:34 -04004106walk_down:
4107 level = ref_path->current_level - 1;
4108 while (level >= -1) {
4109 u64 parent;
4110 if (level < ref_path->lowest_level)
4111 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05004112
Zheng Yan1a40e232008-09-26 10:09:34 -04004113 if (level >= 0) {
4114 bytenr = ref_path->nodes[level];
4115 } else {
4116 bytenr = ref_path->extent_start;
4117 }
4118 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05004119
Zheng Yan1a40e232008-09-26 10:09:34 -04004120 parent = ref_path->nodes[level + 1];
4121 ref_path->nodes[level + 1] = 0;
4122 ref_path->current_level = level;
4123 BUG_ON(parent == 0);
4124
4125 key.objectid = bytenr;
4126 key.offset = parent + 1;
4127 key.type = BTRFS_EXTENT_REF_KEY;
4128
4129 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05004130 if (ret < 0)
4131 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04004132 BUG_ON(ret == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05004133
Chris Masonedbd8d42007-12-21 16:27:24 -05004134 leaf = path->nodes[0];
4135 nritems = btrfs_header_nritems(leaf);
Zheng Yan1a40e232008-09-26 10:09:34 -04004136 if (path->slots[0] >= nritems) {
Chris Masona061fc82008-05-07 11:43:44 -04004137 ret = btrfs_next_leaf(extent_root, path);
Chris Masona061fc82008-05-07 11:43:44 -04004138 if (ret < 0)
4139 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04004140 if (ret > 0)
4141 goto next;
Chris Masonbf4ef672008-05-08 13:26:18 -04004142 leaf = path->nodes[0];
Chris Masona061fc82008-05-07 11:43:44 -04004143 }
Chris Masonedbd8d42007-12-21 16:27:24 -05004144
4145 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Zheng Yan1a40e232008-09-26 10:09:34 -04004146 if (found_key.objectid == bytenr &&
Yan Zhengf82d02d2008-10-29 14:49:05 -04004147 found_key.type == BTRFS_EXTENT_REF_KEY) {
4148 if (level < ref_path->shared_level)
4149 ref_path->shared_level = level;
Zheng Yan1a40e232008-09-26 10:09:34 -04004150 goto found;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004151 }
Zheng Yan1a40e232008-09-26 10:09:34 -04004152next:
4153 level--;
4154 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04004155 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04004156 }
4157 /* reached lowest level */
4158 ret = 1;
4159 goto out;
4160walk_up:
4161 level = ref_path->current_level;
4162 while (level < BTRFS_MAX_LEVEL - 1) {
4163 u64 ref_objectid;
4164 if (level >= 0) {
4165 bytenr = ref_path->nodes[level];
4166 } else {
4167 bytenr = ref_path->extent_start;
Chris Masona061fc82008-05-07 11:43:44 -04004168 }
Zheng Yan1a40e232008-09-26 10:09:34 -04004169 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05004170
Zheng Yan1a40e232008-09-26 10:09:34 -04004171 key.objectid = bytenr;
4172 key.offset = 0;
4173 key.type = BTRFS_EXTENT_REF_KEY;
Chris Masonedbd8d42007-12-21 16:27:24 -05004174
Zheng Yan1a40e232008-09-26 10:09:34 -04004175 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
4176 if (ret < 0)
Chris Masonedbd8d42007-12-21 16:27:24 -05004177 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04004178
4179 leaf = path->nodes[0];
4180 nritems = btrfs_header_nritems(leaf);
4181 if (path->slots[0] >= nritems) {
4182 ret = btrfs_next_leaf(extent_root, path);
4183 if (ret < 0)
4184 goto out;
4185 if (ret > 0) {
4186 /* the extent was freed by someone */
4187 if (ref_path->lowest_level == level)
4188 goto out;
4189 btrfs_release_path(extent_root, path);
4190 goto walk_down;
4191 }
4192 leaf = path->nodes[0];
4193 }
4194
4195 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4196 if (found_key.objectid != bytenr ||
4197 found_key.type != BTRFS_EXTENT_REF_KEY) {
4198 /* the extent was freed by someone */
4199 if (ref_path->lowest_level == level) {
4200 ret = 1;
4201 goto out;
4202 }
4203 btrfs_release_path(extent_root, path);
4204 goto walk_down;
4205 }
4206found:
4207 ref = btrfs_item_ptr(leaf, path->slots[0],
4208 struct btrfs_extent_ref);
4209 ref_objectid = btrfs_ref_objectid(leaf, ref);
4210 if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) {
4211 if (first_time) {
4212 level = (int)ref_objectid;
4213 BUG_ON(level >= BTRFS_MAX_LEVEL);
4214 ref_path->lowest_level = level;
4215 ref_path->current_level = level;
4216 ref_path->nodes[level] = bytenr;
4217 } else {
4218 WARN_ON(ref_objectid != level);
4219 }
4220 } else {
4221 WARN_ON(level != -1);
4222 }
4223 first_time = 0;
4224
4225 if (ref_path->lowest_level == level) {
4226 ref_path->owner_objectid = ref_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04004227 ref_path->num_refs = btrfs_ref_num_refs(leaf, ref);
4228 }
4229
4230 /*
4231 * the block is tree root or the block isn't in reference
4232 * counted tree.
4233 */
4234 if (found_key.objectid == found_key.offset ||
4235 is_cowonly_root(btrfs_ref_root(leaf, ref))) {
4236 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
4237 ref_path->root_generation =
4238 btrfs_ref_generation(leaf, ref);
4239 if (level < 0) {
4240 /* special reference from the tree log */
4241 ref_path->nodes[0] = found_key.offset;
4242 ref_path->current_level = 0;
4243 }
4244 ret = 0;
4245 goto out;
4246 }
4247
4248 level++;
4249 BUG_ON(ref_path->nodes[level] != 0);
4250 ref_path->nodes[level] = found_key.offset;
4251 ref_path->current_level = level;
4252
4253 /*
4254 * the reference was created in the running transaction,
4255 * no need to continue walking up.
4256 */
4257 if (btrfs_ref_generation(leaf, ref) == trans->transid) {
4258 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
4259 ref_path->root_generation =
4260 btrfs_ref_generation(leaf, ref);
4261 ret = 0;
4262 goto out;
4263 }
4264
4265 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04004266 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04004267 }
4268 /* reached max tree level, but no tree root found. */
4269 BUG();
4270out:
Zheng Yan1a40e232008-09-26 10:09:34 -04004271 btrfs_free_path(path);
4272 return ret;
4273}
4274
4275static int btrfs_first_ref_path(struct btrfs_trans_handle *trans,
4276 struct btrfs_root *extent_root,
4277 struct btrfs_ref_path *ref_path,
4278 u64 extent_start)
4279{
4280 memset(ref_path, 0, sizeof(*ref_path));
4281 ref_path->extent_start = extent_start;
4282
4283 return __next_ref_path(trans, extent_root, ref_path, 1);
4284}
4285
4286static int btrfs_next_ref_path(struct btrfs_trans_handle *trans,
4287 struct btrfs_root *extent_root,
4288 struct btrfs_ref_path *ref_path)
4289{
4290 return __next_ref_path(trans, extent_root, ref_path, 0);
4291}
4292
4293static int noinline get_new_locations(struct inode *reloc_inode,
4294 struct btrfs_key *extent_key,
4295 u64 offset, int no_fragment,
4296 struct disk_extent **extents,
4297 int *nr_extents)
4298{
4299 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
4300 struct btrfs_path *path;
4301 struct btrfs_file_extent_item *fi;
4302 struct extent_buffer *leaf;
4303 struct disk_extent *exts = *extents;
4304 struct btrfs_key found_key;
4305 u64 cur_pos;
4306 u64 last_byte;
4307 u32 nritems;
4308 int nr = 0;
4309 int max = *nr_extents;
4310 int ret;
4311
4312 WARN_ON(!no_fragment && *extents);
4313 if (!exts) {
4314 max = 1;
4315 exts = kmalloc(sizeof(*exts) * max, GFP_NOFS);
4316 if (!exts)
4317 return -ENOMEM;
4318 }
4319
4320 path = btrfs_alloc_path();
4321 BUG_ON(!path);
4322
4323 cur_pos = extent_key->objectid - offset;
4324 last_byte = extent_key->objectid + extent_key->offset;
4325 ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino,
4326 cur_pos, 0);
4327 if (ret < 0)
4328 goto out;
4329 if (ret > 0) {
4330 ret = -ENOENT;
4331 goto out;
4332 }
4333
4334 while (1) {
4335 leaf = path->nodes[0];
4336 nritems = btrfs_header_nritems(leaf);
4337 if (path->slots[0] >= nritems) {
4338 ret = btrfs_next_leaf(root, path);
4339 if (ret < 0)
4340 goto out;
4341 if (ret > 0)
4342 break;
4343 leaf = path->nodes[0];
4344 }
4345
4346 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4347 if (found_key.offset != cur_pos ||
4348 found_key.type != BTRFS_EXTENT_DATA_KEY ||
4349 found_key.objectid != reloc_inode->i_ino)
4350 break;
4351
4352 fi = btrfs_item_ptr(leaf, path->slots[0],
4353 struct btrfs_file_extent_item);
4354 if (btrfs_file_extent_type(leaf, fi) !=
4355 BTRFS_FILE_EXTENT_REG ||
4356 btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
4357 break;
4358
4359 if (nr == max) {
4360 struct disk_extent *old = exts;
4361 max *= 2;
4362 exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
4363 memcpy(exts, old, sizeof(*exts) * nr);
4364 if (old != *extents)
4365 kfree(old);
4366 }
4367
4368 exts[nr].disk_bytenr =
4369 btrfs_file_extent_disk_bytenr(leaf, fi);
4370 exts[nr].disk_num_bytes =
4371 btrfs_file_extent_disk_num_bytes(leaf, fi);
4372 exts[nr].offset = btrfs_file_extent_offset(leaf, fi);
4373 exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
Chris Masonc8b97812008-10-29 14:49:59 -04004374 exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
4375 exts[nr].compression = btrfs_file_extent_compression(leaf, fi);
4376 exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi);
4377 exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf,
4378 fi);
Yan Zhengd899e052008-10-30 14:25:28 -04004379 BUG_ON(exts[nr].offset > 0);
4380 BUG_ON(exts[nr].compression || exts[nr].encryption);
4381 BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04004382
4383 cur_pos += exts[nr].num_bytes;
4384 nr++;
4385
4386 if (cur_pos + offset >= last_byte)
4387 break;
4388
4389 if (no_fragment) {
4390 ret = 1;
4391 goto out;
4392 }
4393 path->slots[0]++;
4394 }
4395
4396 WARN_ON(cur_pos + offset > last_byte);
4397 if (cur_pos + offset < last_byte) {
4398 ret = -ENOENT;
4399 goto out;
Chris Masonedbd8d42007-12-21 16:27:24 -05004400 }
4401 ret = 0;
4402out:
Zheng Yan1a40e232008-09-26 10:09:34 -04004403 btrfs_free_path(path);
4404 if (ret) {
4405 if (exts != *extents)
4406 kfree(exts);
4407 } else {
4408 *extents = exts;
4409 *nr_extents = nr;
4410 }
4411 return ret;
4412}
4413
4414static int noinline replace_one_extent(struct btrfs_trans_handle *trans,
4415 struct btrfs_root *root,
4416 struct btrfs_path *path,
4417 struct btrfs_key *extent_key,
4418 struct btrfs_key *leaf_key,
4419 struct btrfs_ref_path *ref_path,
4420 struct disk_extent *new_extents,
4421 int nr_extents)
4422{
4423 struct extent_buffer *leaf;
4424 struct btrfs_file_extent_item *fi;
4425 struct inode *inode = NULL;
4426 struct btrfs_key key;
4427 u64 lock_start = 0;
4428 u64 lock_end = 0;
4429 u64 num_bytes;
4430 u64 ext_offset;
4431 u64 first_pos;
4432 u32 nritems;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004433 int nr_scaned = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04004434 int extent_locked = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04004435 int extent_type;
Zheng Yan1a40e232008-09-26 10:09:34 -04004436 int ret;
4437
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004438 memcpy(&key, leaf_key, sizeof(key));
4439 first_pos = INT_LIMIT(loff_t) - extent_key->offset;
Zheng Yan1a40e232008-09-26 10:09:34 -04004440 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004441 if (key.objectid < ref_path->owner_objectid ||
4442 (key.objectid == ref_path->owner_objectid &&
4443 key.type < BTRFS_EXTENT_DATA_KEY)) {
4444 key.objectid = ref_path->owner_objectid;
4445 key.type = BTRFS_EXTENT_DATA_KEY;
4446 key.offset = 0;
4447 }
Zheng Yan1a40e232008-09-26 10:09:34 -04004448 }
4449
4450 while (1) {
4451 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
4452 if (ret < 0)
4453 goto out;
4454
4455 leaf = path->nodes[0];
4456 nritems = btrfs_header_nritems(leaf);
4457next:
4458 if (extent_locked && ret > 0) {
4459 /*
4460 * the file extent item was modified by someone
4461 * before the extent got locked.
4462 */
Zheng Yan1a40e232008-09-26 10:09:34 -04004463 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4464 lock_end, GFP_NOFS);
4465 extent_locked = 0;
4466 }
4467
4468 if (path->slots[0] >= nritems) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004469 if (++nr_scaned > 2)
Zheng Yan1a40e232008-09-26 10:09:34 -04004470 break;
4471
4472 BUG_ON(extent_locked);
4473 ret = btrfs_next_leaf(root, path);
4474 if (ret < 0)
4475 goto out;
4476 if (ret > 0)
4477 break;
4478 leaf = path->nodes[0];
4479 nritems = btrfs_header_nritems(leaf);
4480 }
4481
4482 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4483
4484 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
4485 if ((key.objectid > ref_path->owner_objectid) ||
4486 (key.objectid == ref_path->owner_objectid &&
4487 key.type > BTRFS_EXTENT_DATA_KEY) ||
4488 (key.offset >= first_pos + extent_key->offset))
4489 break;
4490 }
4491
4492 if (inode && key.objectid != inode->i_ino) {
4493 BUG_ON(extent_locked);
4494 btrfs_release_path(root, path);
4495 mutex_unlock(&inode->i_mutex);
4496 iput(inode);
4497 inode = NULL;
4498 continue;
4499 }
4500
4501 if (key.type != BTRFS_EXTENT_DATA_KEY) {
4502 path->slots[0]++;
4503 ret = 1;
4504 goto next;
4505 }
4506 fi = btrfs_item_ptr(leaf, path->slots[0],
4507 struct btrfs_file_extent_item);
Yan Zhengd899e052008-10-30 14:25:28 -04004508 extent_type = btrfs_file_extent_type(leaf, fi);
4509 if ((extent_type != BTRFS_FILE_EXTENT_REG &&
4510 extent_type != BTRFS_FILE_EXTENT_PREALLOC) ||
Zheng Yan1a40e232008-09-26 10:09:34 -04004511 (btrfs_file_extent_disk_bytenr(leaf, fi) !=
4512 extent_key->objectid)) {
4513 path->slots[0]++;
4514 ret = 1;
4515 goto next;
4516 }
4517
4518 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
4519 ext_offset = btrfs_file_extent_offset(leaf, fi);
4520
4521 if (first_pos > key.offset - ext_offset)
4522 first_pos = key.offset - ext_offset;
4523
4524 if (!extent_locked) {
4525 lock_start = key.offset;
4526 lock_end = lock_start + num_bytes - 1;
4527 } else {
Yan Zheng66435582008-10-30 14:19:50 -04004528 if (lock_start > key.offset ||
4529 lock_end + 1 < key.offset + num_bytes) {
4530 unlock_extent(&BTRFS_I(inode)->io_tree,
4531 lock_start, lock_end, GFP_NOFS);
4532 extent_locked = 0;
4533 }
Zheng Yan1a40e232008-09-26 10:09:34 -04004534 }
4535
4536 if (!inode) {
4537 btrfs_release_path(root, path);
4538
4539 inode = btrfs_iget_locked(root->fs_info->sb,
4540 key.objectid, root);
4541 if (inode->i_state & I_NEW) {
4542 BTRFS_I(inode)->root = root;
4543 BTRFS_I(inode)->location.objectid =
4544 key.objectid;
4545 BTRFS_I(inode)->location.type =
4546 BTRFS_INODE_ITEM_KEY;
4547 BTRFS_I(inode)->location.offset = 0;
4548 btrfs_read_locked_inode(inode);
4549 unlock_new_inode(inode);
4550 }
4551 /*
4552 * some code call btrfs_commit_transaction while
4553 * holding the i_mutex, so we can't use mutex_lock
4554 * here.
4555 */
4556 if (is_bad_inode(inode) ||
4557 !mutex_trylock(&inode->i_mutex)) {
4558 iput(inode);
4559 inode = NULL;
4560 key.offset = (u64)-1;
4561 goto skip;
4562 }
4563 }
4564
4565 if (!extent_locked) {
4566 struct btrfs_ordered_extent *ordered;
4567
4568 btrfs_release_path(root, path);
4569
4570 lock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4571 lock_end, GFP_NOFS);
4572 ordered = btrfs_lookup_first_ordered_extent(inode,
4573 lock_end);
4574 if (ordered &&
4575 ordered->file_offset <= lock_end &&
4576 ordered->file_offset + ordered->len > lock_start) {
4577 unlock_extent(&BTRFS_I(inode)->io_tree,
4578 lock_start, lock_end, GFP_NOFS);
4579 btrfs_start_ordered_extent(inode, ordered, 1);
4580 btrfs_put_ordered_extent(ordered);
4581 key.offset += num_bytes;
4582 goto skip;
4583 }
4584 if (ordered)
4585 btrfs_put_ordered_extent(ordered);
4586
Zheng Yan1a40e232008-09-26 10:09:34 -04004587 extent_locked = 1;
4588 continue;
4589 }
4590
4591 if (nr_extents == 1) {
4592 /* update extent pointer in place */
Zheng Yan1a40e232008-09-26 10:09:34 -04004593 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4594 new_extents[0].disk_bytenr);
4595 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4596 new_extents[0].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04004597 btrfs_mark_buffer_dirty(leaf);
4598
4599 btrfs_drop_extent_cache(inode, key.offset,
4600 key.offset + num_bytes - 1, 0);
4601
4602 ret = btrfs_inc_extent_ref(trans, root,
4603 new_extents[0].disk_bytenr,
4604 new_extents[0].disk_num_bytes,
4605 leaf->start,
4606 root->root_key.objectid,
4607 trans->transid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004608 key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04004609 BUG_ON(ret);
4610
4611 ret = btrfs_free_extent(trans, root,
4612 extent_key->objectid,
4613 extent_key->offset,
4614 leaf->start,
4615 btrfs_header_owner(leaf),
4616 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004617 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04004618 BUG_ON(ret);
4619
4620 btrfs_release_path(root, path);
4621 key.offset += num_bytes;
4622 } else {
Yan Zhengd899e052008-10-30 14:25:28 -04004623 BUG_ON(1);
4624#if 0
Zheng Yan1a40e232008-09-26 10:09:34 -04004625 u64 alloc_hint;
4626 u64 extent_len;
4627 int i;
4628 /*
4629 * drop old extent pointer at first, then insert the
4630 * new pointers one bye one
4631 */
4632 btrfs_release_path(root, path);
4633 ret = btrfs_drop_extents(trans, root, inode, key.offset,
4634 key.offset + num_bytes,
4635 key.offset, &alloc_hint);
4636 BUG_ON(ret);
4637
4638 for (i = 0; i < nr_extents; i++) {
4639 if (ext_offset >= new_extents[i].num_bytes) {
4640 ext_offset -= new_extents[i].num_bytes;
4641 continue;
4642 }
4643 extent_len = min(new_extents[i].num_bytes -
4644 ext_offset, num_bytes);
4645
4646 ret = btrfs_insert_empty_item(trans, root,
4647 path, &key,
4648 sizeof(*fi));
4649 BUG_ON(ret);
4650
4651 leaf = path->nodes[0];
4652 fi = btrfs_item_ptr(leaf, path->slots[0],
4653 struct btrfs_file_extent_item);
4654 btrfs_set_file_extent_generation(leaf, fi,
4655 trans->transid);
4656 btrfs_set_file_extent_type(leaf, fi,
4657 BTRFS_FILE_EXTENT_REG);
4658 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4659 new_extents[i].disk_bytenr);
4660 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4661 new_extents[i].disk_num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -04004662 btrfs_set_file_extent_ram_bytes(leaf, fi,
4663 new_extents[i].ram_bytes);
4664
4665 btrfs_set_file_extent_compression(leaf, fi,
4666 new_extents[i].compression);
4667 btrfs_set_file_extent_encryption(leaf, fi,
4668 new_extents[i].encryption);
4669 btrfs_set_file_extent_other_encoding(leaf, fi,
4670 new_extents[i].other_encoding);
4671
Zheng Yan1a40e232008-09-26 10:09:34 -04004672 btrfs_set_file_extent_num_bytes(leaf, fi,
4673 extent_len);
4674 ext_offset += new_extents[i].offset;
4675 btrfs_set_file_extent_offset(leaf, fi,
4676 ext_offset);
4677 btrfs_mark_buffer_dirty(leaf);
4678
4679 btrfs_drop_extent_cache(inode, key.offset,
4680 key.offset + extent_len - 1, 0);
4681
4682 ret = btrfs_inc_extent_ref(trans, root,
4683 new_extents[i].disk_bytenr,
4684 new_extents[i].disk_num_bytes,
4685 leaf->start,
4686 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004687 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04004688 BUG_ON(ret);
4689 btrfs_release_path(root, path);
4690
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004691 inode_add_bytes(inode, extent_len);
Zheng Yan1a40e232008-09-26 10:09:34 -04004692
4693 ext_offset = 0;
4694 num_bytes -= extent_len;
4695 key.offset += extent_len;
4696
4697 if (num_bytes == 0)
4698 break;
4699 }
4700 BUG_ON(i >= nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04004701#endif
Zheng Yan1a40e232008-09-26 10:09:34 -04004702 }
4703
4704 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04004705 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4706 lock_end, GFP_NOFS);
4707 extent_locked = 0;
4708 }
4709skip:
4710 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS &&
4711 key.offset >= first_pos + extent_key->offset)
4712 break;
4713
4714 cond_resched();
4715 }
4716 ret = 0;
4717out:
4718 btrfs_release_path(root, path);
4719 if (inode) {
4720 mutex_unlock(&inode->i_mutex);
4721 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04004722 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4723 lock_end, GFP_NOFS);
4724 }
4725 iput(inode);
4726 }
4727 return ret;
4728}
4729
Zheng Yan1a40e232008-09-26 10:09:34 -04004730int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans,
4731 struct btrfs_root *root,
4732 struct extent_buffer *buf, u64 orig_start)
4733{
4734 int level;
4735 int ret;
4736
4737 BUG_ON(btrfs_header_generation(buf) != trans->transid);
4738 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
4739
4740 level = btrfs_header_level(buf);
4741 if (level == 0) {
4742 struct btrfs_leaf_ref *ref;
4743 struct btrfs_leaf_ref *orig_ref;
4744
4745 orig_ref = btrfs_lookup_leaf_ref(root, orig_start);
4746 if (!orig_ref)
4747 return -ENOENT;
4748
4749 ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems);
4750 if (!ref) {
4751 btrfs_free_leaf_ref(root, orig_ref);
4752 return -ENOMEM;
4753 }
4754
4755 ref->nritems = orig_ref->nritems;
4756 memcpy(ref->extents, orig_ref->extents,
4757 sizeof(ref->extents[0]) * ref->nritems);
4758
4759 btrfs_free_leaf_ref(root, orig_ref);
4760
4761 ref->root_gen = trans->transid;
4762 ref->bytenr = buf->start;
4763 ref->owner = btrfs_header_owner(buf);
4764 ref->generation = btrfs_header_generation(buf);
4765 ret = btrfs_add_leaf_ref(root, ref, 0);
4766 WARN_ON(ret);
4767 btrfs_free_leaf_ref(root, ref);
4768 }
4769 return 0;
4770}
4771
4772static int noinline invalidate_extent_cache(struct btrfs_root *root,
4773 struct extent_buffer *leaf,
4774 struct btrfs_block_group_cache *group,
4775 struct btrfs_root *target_root)
4776{
4777 struct btrfs_key key;
4778 struct inode *inode = NULL;
4779 struct btrfs_file_extent_item *fi;
4780 u64 num_bytes;
4781 u64 skip_objectid = 0;
4782 u32 nritems;
4783 u32 i;
4784
4785 nritems = btrfs_header_nritems(leaf);
4786 for (i = 0; i < nritems; i++) {
4787 btrfs_item_key_to_cpu(leaf, &key, i);
4788 if (key.objectid == skip_objectid ||
4789 key.type != BTRFS_EXTENT_DATA_KEY)
4790 continue;
4791 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
4792 if (btrfs_file_extent_type(leaf, fi) ==
4793 BTRFS_FILE_EXTENT_INLINE)
4794 continue;
4795 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
4796 continue;
4797 if (!inode || inode->i_ino != key.objectid) {
4798 iput(inode);
4799 inode = btrfs_ilookup(target_root->fs_info->sb,
4800 key.objectid, target_root, 1);
4801 }
4802 if (!inode) {
4803 skip_objectid = key.objectid;
4804 continue;
4805 }
4806 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
4807
4808 lock_extent(&BTRFS_I(inode)->io_tree, key.offset,
4809 key.offset + num_bytes - 1, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04004810 btrfs_drop_extent_cache(inode, key.offset,
4811 key.offset + num_bytes - 1, 1);
Zheng Yan1a40e232008-09-26 10:09:34 -04004812 unlock_extent(&BTRFS_I(inode)->io_tree, key.offset,
4813 key.offset + num_bytes - 1, GFP_NOFS);
4814 cond_resched();
4815 }
4816 iput(inode);
4817 return 0;
4818}
4819
4820static int noinline replace_extents_in_leaf(struct btrfs_trans_handle *trans,
4821 struct btrfs_root *root,
4822 struct extent_buffer *leaf,
4823 struct btrfs_block_group_cache *group,
4824 struct inode *reloc_inode)
4825{
4826 struct btrfs_key key;
4827 struct btrfs_key extent_key;
4828 struct btrfs_file_extent_item *fi;
4829 struct btrfs_leaf_ref *ref;
4830 struct disk_extent *new_extent;
4831 u64 bytenr;
4832 u64 num_bytes;
4833 u32 nritems;
4834 u32 i;
4835 int ext_index;
4836 int nr_extent;
4837 int ret;
4838
4839 new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
4840 BUG_ON(!new_extent);
4841
4842 ref = btrfs_lookup_leaf_ref(root, leaf->start);
4843 BUG_ON(!ref);
4844
4845 ext_index = -1;
4846 nritems = btrfs_header_nritems(leaf);
4847 for (i = 0; i < nritems; i++) {
4848 btrfs_item_key_to_cpu(leaf, &key, i);
4849 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
4850 continue;
4851 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
4852 if (btrfs_file_extent_type(leaf, fi) ==
4853 BTRFS_FILE_EXTENT_INLINE)
4854 continue;
4855 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
4856 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
4857 if (bytenr == 0)
4858 continue;
4859
4860 ext_index++;
4861 if (bytenr >= group->key.objectid + group->key.offset ||
4862 bytenr + num_bytes <= group->key.objectid)
4863 continue;
4864
4865 extent_key.objectid = bytenr;
4866 extent_key.offset = num_bytes;
4867 extent_key.type = BTRFS_EXTENT_ITEM_KEY;
4868 nr_extent = 1;
4869 ret = get_new_locations(reloc_inode, &extent_key,
4870 group->key.objectid, 1,
4871 &new_extent, &nr_extent);
4872 if (ret > 0)
4873 continue;
4874 BUG_ON(ret < 0);
4875
4876 BUG_ON(ref->extents[ext_index].bytenr != bytenr);
4877 BUG_ON(ref->extents[ext_index].num_bytes != num_bytes);
4878 ref->extents[ext_index].bytenr = new_extent->disk_bytenr;
4879 ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes;
4880
Zheng Yan1a40e232008-09-26 10:09:34 -04004881 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4882 new_extent->disk_bytenr);
4883 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4884 new_extent->disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04004885 btrfs_mark_buffer_dirty(leaf);
4886
4887 ret = btrfs_inc_extent_ref(trans, root,
4888 new_extent->disk_bytenr,
4889 new_extent->disk_num_bytes,
4890 leaf->start,
4891 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004892 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04004893 BUG_ON(ret);
4894 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
5029static int noinline init_reloc_tree(struct btrfs_trans_handle *trans,
5030 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 */
5096static int noinline relocate_one_path(struct btrfs_trans_handle *trans,
5097 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
5193static int noinline relocate_tree_block(struct btrfs_trans_handle *trans,
5194 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
5205 if (root == root->fs_info->extent_root)
5206 btrfs_extent_post_op(trans, root);
Zheng Yan1a40e232008-09-26 10:09:34 -04005207
5208 return 0;
5209}
5210
5211static int noinline del_extent_zero(struct btrfs_trans_handle *trans,
5212 struct btrfs_root *extent_root,
5213 struct btrfs_path *path,
5214 struct btrfs_key *extent_key)
5215{
5216 int ret;
5217
Zheng Yan1a40e232008-09-26 10:09:34 -04005218 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
5219 if (ret)
5220 goto out;
5221 ret = btrfs_del_item(trans, extent_root, path);
5222out:
Chris Masonedbd8d42007-12-21 16:27:24 -05005223 btrfs_release_path(extent_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005224 return ret;
5225}
5226
5227static struct btrfs_root noinline *read_ref_root(struct btrfs_fs_info *fs_info,
5228 struct btrfs_ref_path *ref_path)
5229{
5230 struct btrfs_key root_key;
5231
5232 root_key.objectid = ref_path->root_objectid;
5233 root_key.type = BTRFS_ROOT_ITEM_KEY;
5234 if (is_cowonly_root(ref_path->root_objectid))
5235 root_key.offset = 0;
5236 else
5237 root_key.offset = (u64)-1;
5238
5239 return btrfs_read_fs_root_no_name(fs_info, &root_key);
5240}
5241
5242static int noinline relocate_one_extent(struct btrfs_root *extent_root,
5243 struct btrfs_path *path,
5244 struct btrfs_key *extent_key,
5245 struct btrfs_block_group_cache *group,
5246 struct inode *reloc_inode, int pass)
5247{
5248 struct btrfs_trans_handle *trans;
5249 struct btrfs_root *found_root;
5250 struct btrfs_ref_path *ref_path = NULL;
5251 struct disk_extent *new_extents = NULL;
5252 int nr_extents = 0;
5253 int loops;
5254 int ret;
5255 int level;
5256 struct btrfs_key first_key;
5257 u64 prev_block = 0;
5258
Zheng Yan1a40e232008-09-26 10:09:34 -04005259
5260 trans = btrfs_start_transaction(extent_root, 1);
5261 BUG_ON(!trans);
5262
5263 if (extent_key->objectid == 0) {
5264 ret = del_extent_zero(trans, extent_root, path, extent_key);
5265 goto out;
5266 }
5267
5268 ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS);
5269 if (!ref_path) {
5270 ret = -ENOMEM;
5271 goto out;
5272 }
5273
5274 for (loops = 0; ; loops++) {
5275 if (loops == 0) {
5276 ret = btrfs_first_ref_path(trans, extent_root, ref_path,
5277 extent_key->objectid);
5278 } else {
5279 ret = btrfs_next_ref_path(trans, extent_root, ref_path);
5280 }
5281 if (ret < 0)
5282 goto out;
5283 if (ret > 0)
5284 break;
5285
5286 if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID ||
5287 ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID)
5288 continue;
5289
5290 found_root = read_ref_root(extent_root->fs_info, ref_path);
5291 BUG_ON(!found_root);
5292 /*
5293 * for reference counted tree, only process reference paths
5294 * rooted at the latest committed root.
5295 */
5296 if (found_root->ref_cows &&
5297 ref_path->root_generation != found_root->root_key.offset)
5298 continue;
5299
5300 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
5301 if (pass == 0) {
5302 /*
5303 * copy data extents to new locations
5304 */
5305 u64 group_start = group->key.objectid;
5306 ret = relocate_data_extent(reloc_inode,
5307 extent_key,
5308 group_start);
5309 if (ret < 0)
5310 goto out;
5311 break;
5312 }
5313 level = 0;
5314 } else {
5315 level = ref_path->owner_objectid;
5316 }
5317
5318 if (prev_block != ref_path->nodes[level]) {
5319 struct extent_buffer *eb;
5320 u64 block_start = ref_path->nodes[level];
5321 u64 block_size = btrfs_level_size(found_root, level);
5322
5323 eb = read_tree_block(found_root, block_start,
5324 block_size, 0);
5325 btrfs_tree_lock(eb);
5326 BUG_ON(level != btrfs_header_level(eb));
5327
5328 if (level == 0)
5329 btrfs_item_key_to_cpu(eb, &first_key, 0);
5330 else
5331 btrfs_node_key_to_cpu(eb, &first_key, 0);
5332
5333 btrfs_tree_unlock(eb);
5334 free_extent_buffer(eb);
5335 prev_block = block_start;
5336 }
5337
5338 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID &&
5339 pass >= 2) {
5340 /*
5341 * use fallback method to process the remaining
5342 * references.
5343 */
5344 if (!new_extents) {
5345 u64 group_start = group->key.objectid;
Yan Zhengd899e052008-10-30 14:25:28 -04005346 new_extents = kmalloc(sizeof(*new_extents),
5347 GFP_NOFS);
5348 nr_extents = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04005349 ret = get_new_locations(reloc_inode,
5350 extent_key,
Yan Zhengd899e052008-10-30 14:25:28 -04005351 group_start, 1,
Zheng Yan1a40e232008-09-26 10:09:34 -04005352 &new_extents,
5353 &nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04005354 if (ret)
Zheng Yan1a40e232008-09-26 10:09:34 -04005355 goto out;
5356 }
5357 btrfs_record_root_in_trans(found_root);
5358 ret = replace_one_extent(trans, found_root,
5359 path, extent_key,
5360 &first_key, ref_path,
5361 new_extents, nr_extents);
5362 if (ret < 0)
5363 goto out;
5364 continue;
5365 }
5366
5367 btrfs_record_root_in_trans(found_root);
5368 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
5369 ret = relocate_tree_block(trans, found_root, path,
5370 &first_key, ref_path);
5371 } else {
5372 /*
5373 * try to update data extent references while
5374 * keeping metadata shared between snapshots.
5375 */
5376 ret = relocate_one_path(trans, found_root, path,
5377 &first_key, ref_path,
5378 group, reloc_inode);
5379 }
5380 if (ret < 0)
5381 goto out;
5382 }
5383 ret = 0;
5384out:
5385 btrfs_end_transaction(trans, extent_root);
5386 kfree(new_extents);
5387 kfree(ref_path);
Chris Masonedbd8d42007-12-21 16:27:24 -05005388 return ret;
5389}
5390
Chris Masonec44a352008-04-28 15:29:52 -04005391static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
5392{
5393 u64 num_devices;
5394 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
5395 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
5396
Yan Zheng2b820322008-11-17 21:11:30 -05005397 num_devices = root->fs_info->fs_devices->rw_devices;
Chris Masonec44a352008-04-28 15:29:52 -04005398 if (num_devices == 1) {
5399 stripped |= BTRFS_BLOCK_GROUP_DUP;
5400 stripped = flags & ~stripped;
5401
5402 /* turn raid0 into single device chunks */
5403 if (flags & BTRFS_BLOCK_GROUP_RAID0)
5404 return stripped;
5405
5406 /* turn mirroring into duplication */
5407 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
5408 BTRFS_BLOCK_GROUP_RAID10))
5409 return stripped | BTRFS_BLOCK_GROUP_DUP;
5410 return flags;
5411 } else {
5412 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04005413 if (flags & stripped)
5414 return flags;
5415
5416 stripped |= BTRFS_BLOCK_GROUP_DUP;
5417 stripped = flags & ~stripped;
5418
5419 /* switch duplicated blocks with raid1 */
5420 if (flags & BTRFS_BLOCK_GROUP_DUP)
5421 return stripped | BTRFS_BLOCK_GROUP_RAID1;
5422
5423 /* turn single device chunks into raid0 */
5424 return stripped | BTRFS_BLOCK_GROUP_RAID0;
5425 }
5426 return flags;
5427}
5428
Christoph Hellwigb2950862008-12-02 09:54:17 -05005429static int __alloc_chunk_for_shrink(struct btrfs_root *root,
Chris Mason0ef3e662008-05-24 14:04:53 -04005430 struct btrfs_block_group_cache *shrink_block_group,
5431 int force)
5432{
5433 struct btrfs_trans_handle *trans;
5434 u64 new_alloc_flags;
5435 u64 calc;
5436
Chris Masonc286ac42008-07-22 23:06:41 -04005437 spin_lock(&shrink_block_group->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04005438 if (btrfs_block_group_used(&shrink_block_group->item) > 0) {
Chris Masonc286ac42008-07-22 23:06:41 -04005439 spin_unlock(&shrink_block_group->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04005440
Chris Mason0ef3e662008-05-24 14:04:53 -04005441 trans = btrfs_start_transaction(root, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04005442 spin_lock(&shrink_block_group->lock);
Chris Mason7d9eb122008-07-08 14:19:17 -04005443
Chris Mason0ef3e662008-05-24 14:04:53 -04005444 new_alloc_flags = update_block_group_flags(root,
5445 shrink_block_group->flags);
5446 if (new_alloc_flags != shrink_block_group->flags) {
5447 calc =
5448 btrfs_block_group_used(&shrink_block_group->item);
5449 } else {
5450 calc = shrink_block_group->key.offset;
5451 }
Chris Masonc286ac42008-07-22 23:06:41 -04005452 spin_unlock(&shrink_block_group->lock);
5453
Chris Mason0ef3e662008-05-24 14:04:53 -04005454 do_chunk_alloc(trans, root->fs_info->extent_root,
5455 calc + 2 * 1024 * 1024, new_alloc_flags, force);
Chris Mason7d9eb122008-07-08 14:19:17 -04005456
Chris Mason0ef3e662008-05-24 14:04:53 -04005457 btrfs_end_transaction(trans, root);
Chris Masonc286ac42008-07-22 23:06:41 -04005458 } else
5459 spin_unlock(&shrink_block_group->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04005460 return 0;
5461}
5462
Zheng Yan1a40e232008-09-26 10:09:34 -04005463static int __insert_orphan_inode(struct btrfs_trans_handle *trans,
5464 struct btrfs_root *root,
5465 u64 objectid, u64 size)
5466{
5467 struct btrfs_path *path;
5468 struct btrfs_inode_item *item;
5469 struct extent_buffer *leaf;
5470 int ret;
5471
5472 path = btrfs_alloc_path();
5473 if (!path)
5474 return -ENOMEM;
5475
5476 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
5477 if (ret)
5478 goto out;
5479
5480 leaf = path->nodes[0];
5481 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item);
5482 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
5483 btrfs_set_inode_generation(leaf, item, 1);
5484 btrfs_set_inode_size(leaf, item, size);
5485 btrfs_set_inode_mode(leaf, item, S_IFREG | 0600);
Yan Zheng0403e472008-12-10 20:32:51 -05005486 btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NOCOMPRESS);
Zheng Yan1a40e232008-09-26 10:09:34 -04005487 btrfs_mark_buffer_dirty(leaf);
5488 btrfs_release_path(root, path);
5489out:
5490 btrfs_free_path(path);
5491 return ret;
5492}
5493
5494static struct inode noinline *create_reloc_inode(struct btrfs_fs_info *fs_info,
5495 struct btrfs_block_group_cache *group)
5496{
5497 struct inode *inode = NULL;
5498 struct btrfs_trans_handle *trans;
5499 struct btrfs_root *root;
5500 struct btrfs_key root_key;
5501 u64 objectid = BTRFS_FIRST_FREE_OBJECTID;
5502 int err = 0;
5503
5504 root_key.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
5505 root_key.type = BTRFS_ROOT_ITEM_KEY;
5506 root_key.offset = (u64)-1;
5507 root = btrfs_read_fs_root_no_name(fs_info, &root_key);
5508 if (IS_ERR(root))
5509 return ERR_CAST(root);
5510
5511 trans = btrfs_start_transaction(root, 1);
5512 BUG_ON(!trans);
5513
5514 err = btrfs_find_free_objectid(trans, root, objectid, &objectid);
5515 if (err)
5516 goto out;
5517
5518 err = __insert_orphan_inode(trans, root, objectid, group->key.offset);
5519 BUG_ON(err);
5520
5521 err = btrfs_insert_file_extent(trans, root, objectid, 0, 0, 0,
Chris Masonc8b97812008-10-29 14:49:59 -04005522 group->key.offset, 0, group->key.offset,
5523 0, 0, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04005524 BUG_ON(err);
5525
5526 inode = btrfs_iget_locked(root->fs_info->sb, objectid, root);
5527 if (inode->i_state & I_NEW) {
5528 BTRFS_I(inode)->root = root;
5529 BTRFS_I(inode)->location.objectid = objectid;
5530 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
5531 BTRFS_I(inode)->location.offset = 0;
5532 btrfs_read_locked_inode(inode);
5533 unlock_new_inode(inode);
5534 BUG_ON(is_bad_inode(inode));
5535 } else {
5536 BUG_ON(1);
5537 }
5538
5539 err = btrfs_orphan_add(trans, inode);
5540out:
5541 btrfs_end_transaction(trans, root);
5542 if (err) {
5543 if (inode)
5544 iput(inode);
5545 inode = ERR_PTR(err);
5546 }
5547 return inode;
5548}
5549
5550int btrfs_relocate_block_group(struct btrfs_root *root, u64 group_start)
Chris Masonedbd8d42007-12-21 16:27:24 -05005551{
5552 struct btrfs_trans_handle *trans;
Chris Masonedbd8d42007-12-21 16:27:24 -05005553 struct btrfs_path *path;
Zheng Yan1a40e232008-09-26 10:09:34 -04005554 struct btrfs_fs_info *info = root->fs_info;
5555 struct extent_buffer *leaf;
5556 struct inode *reloc_inode;
5557 struct btrfs_block_group_cache *block_group;
5558 struct btrfs_key key;
Yan Zhengd899e052008-10-30 14:25:28 -04005559 u64 skipped;
Chris Masonedbd8d42007-12-21 16:27:24 -05005560 u64 cur_byte;
5561 u64 total_found;
Chris Masonedbd8d42007-12-21 16:27:24 -05005562 u32 nritems;
5563 int ret;
Chris Masona061fc82008-05-07 11:43:44 -04005564 int progress;
Zheng Yan1a40e232008-09-26 10:09:34 -04005565 int pass = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05005566
Chris Masonedbd8d42007-12-21 16:27:24 -05005567 root = root->fs_info->extent_root;
Zheng Yan1a40e232008-09-26 10:09:34 -04005568
5569 block_group = btrfs_lookup_block_group(info, group_start);
5570 BUG_ON(!block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05005571
Chris Mason323da792008-05-09 11:46:48 -04005572 printk("btrfs relocating block group %llu flags %llu\n",
Zheng Yan1a40e232008-09-26 10:09:34 -04005573 (unsigned long long)block_group->key.objectid,
5574 (unsigned long long)block_group->flags);
Chris Mason323da792008-05-09 11:46:48 -04005575
Zheng Yan1a40e232008-09-26 10:09:34 -04005576 path = btrfs_alloc_path();
5577 BUG_ON(!path);
Chris Mason323da792008-05-09 11:46:48 -04005578
Zheng Yan1a40e232008-09-26 10:09:34 -04005579 reloc_inode = create_reloc_inode(info, block_group);
5580 BUG_ON(IS_ERR(reloc_inode));
5581
Zheng Yan1a40e232008-09-26 10:09:34 -04005582 __alloc_chunk_for_shrink(root, block_group, 1);
Yan Zhengc146afa2008-11-12 14:34:12 -05005583 set_block_group_readonly(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04005584
Zheng Yan1a40e232008-09-26 10:09:34 -04005585 btrfs_start_delalloc_inodes(info->tree_root);
5586 btrfs_wait_ordered_extents(info->tree_root, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -04005587again:
Yan Zhengd899e052008-10-30 14:25:28 -04005588 skipped = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05005589 total_found = 0;
Chris Masona061fc82008-05-07 11:43:44 -04005590 progress = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04005591 key.objectid = block_group->key.objectid;
Chris Masonedbd8d42007-12-21 16:27:24 -05005592 key.offset = 0;
5593 key.type = 0;
Yan73e48b22008-01-03 14:14:39 -05005594 cur_byte = key.objectid;
Chris Mason4313b392008-01-03 09:08:48 -05005595
Zheng Yan1a40e232008-09-26 10:09:34 -04005596 trans = btrfs_start_transaction(info->tree_root, 1);
5597 btrfs_commit_transaction(trans, info->tree_root);
Chris Masonea8c2812008-08-04 23:17:27 -04005598
Zheng Yan1a40e232008-09-26 10:09:34 -04005599 mutex_lock(&root->fs_info->cleaner_mutex);
5600 btrfs_clean_old_snapshots(info->tree_root);
5601 btrfs_remove_leaf_refs(info->tree_root, (u64)-1, 1);
5602 mutex_unlock(&root->fs_info->cleaner_mutex);
Chris Masonea8c2812008-08-04 23:17:27 -04005603
Yan73e48b22008-01-03 14:14:39 -05005604 while(1) {
Chris Masonedbd8d42007-12-21 16:27:24 -05005605 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5606 if (ret < 0)
5607 goto out;
Chris Mason7d9eb122008-07-08 14:19:17 -04005608next:
Chris Masonedbd8d42007-12-21 16:27:24 -05005609 leaf = path->nodes[0];
Chris Masonedbd8d42007-12-21 16:27:24 -05005610 nritems = btrfs_header_nritems(leaf);
Yan73e48b22008-01-03 14:14:39 -05005611 if (path->slots[0] >= nritems) {
5612 ret = btrfs_next_leaf(root, path);
5613 if (ret < 0)
5614 goto out;
5615 if (ret == 1) {
5616 ret = 0;
5617 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05005618 }
Yan73e48b22008-01-03 14:14:39 -05005619 leaf = path->nodes[0];
5620 nritems = btrfs_header_nritems(leaf);
5621 }
5622
Zheng Yan1a40e232008-09-26 10:09:34 -04005623 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Mason725c8462008-01-04 16:47:16 -05005624
Zheng Yan1a40e232008-09-26 10:09:34 -04005625 if (key.objectid >= block_group->key.objectid +
5626 block_group->key.offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04005627 break;
5628
Chris Mason725c8462008-01-04 16:47:16 -05005629 if (progress && need_resched()) {
Chris Mason725c8462008-01-04 16:47:16 -05005630 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005631 cond_resched();
Chris Mason725c8462008-01-04 16:47:16 -05005632 progress = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04005633 continue;
Chris Mason725c8462008-01-04 16:47:16 -05005634 }
5635 progress = 1;
5636
Zheng Yan1a40e232008-09-26 10:09:34 -04005637 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY ||
5638 key.objectid + key.offset <= cur_byte) {
Yan73e48b22008-01-03 14:14:39 -05005639 path->slots[0]++;
Chris Masonedbd8d42007-12-21 16:27:24 -05005640 goto next;
5641 }
Yan73e48b22008-01-03 14:14:39 -05005642
Chris Masonedbd8d42007-12-21 16:27:24 -05005643 total_found++;
Zheng Yan1a40e232008-09-26 10:09:34 -04005644 cur_byte = key.objectid + key.offset;
Chris Masonedbd8d42007-12-21 16:27:24 -05005645 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005646
5647 __alloc_chunk_for_shrink(root, block_group, 0);
5648 ret = relocate_one_extent(root, path, &key, block_group,
5649 reloc_inode, pass);
5650 BUG_ON(ret < 0);
Yan Zhengd899e052008-10-30 14:25:28 -04005651 if (ret > 0)
5652 skipped++;
Zheng Yan1a40e232008-09-26 10:09:34 -04005653
5654 key.objectid = cur_byte;
5655 key.type = 0;
5656 key.offset = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05005657 }
5658
5659 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005660
5661 if (pass == 0) {
5662 btrfs_wait_ordered_range(reloc_inode, 0, (u64)-1);
5663 invalidate_mapping_pages(reloc_inode->i_mapping, 0, -1);
5664 WARN_ON(reloc_inode->i_mapping->nrpages);
5665 }
Chris Masonedbd8d42007-12-21 16:27:24 -05005666
5667 if (total_found > 0) {
Zheng Yan1a40e232008-09-26 10:09:34 -04005668 printk("btrfs found %llu extents in pass %d\n",
5669 (unsigned long long)total_found, pass);
5670 pass++;
Yan Zhengd899e052008-10-30 14:25:28 -04005671 if (total_found == skipped && pass > 2) {
5672 iput(reloc_inode);
5673 reloc_inode = create_reloc_inode(info, block_group);
5674 pass = 0;
5675 }
Chris Masonedbd8d42007-12-21 16:27:24 -05005676 goto again;
5677 }
5678
Zheng Yan1a40e232008-09-26 10:09:34 -04005679 /* delete reloc_inode */
5680 iput(reloc_inode);
Chris Mason7d9eb122008-07-08 14:19:17 -04005681
Zheng Yan1a40e232008-09-26 10:09:34 -04005682 /* unpin extents in this range */
5683 trans = btrfs_start_transaction(info->tree_root, 1);
5684 btrfs_commit_transaction(trans, info->tree_root);
Chris Mason0ef3e662008-05-24 14:04:53 -04005685
Zheng Yan1a40e232008-09-26 10:09:34 -04005686 spin_lock(&block_group->lock);
5687 WARN_ON(block_group->pinned > 0);
5688 WARN_ON(block_group->reserved > 0);
5689 WARN_ON(btrfs_block_group_used(&block_group->item) > 0);
5690 spin_unlock(&block_group->lock);
Yan Zhengd2fb3432008-12-11 16:30:39 -05005691 put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04005692 ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05005693out:
Zheng Yan1a40e232008-09-26 10:09:34 -04005694 btrfs_free_path(path);
Chris Masonedbd8d42007-12-21 16:27:24 -05005695 return ret;
5696}
5697
Christoph Hellwigb2950862008-12-02 09:54:17 -05005698static int find_first_block_group(struct btrfs_root *root,
5699 struct btrfs_path *path, struct btrfs_key *key)
Chris Mason0b86a832008-03-24 15:01:56 -04005700{
Chris Mason925baed2008-06-25 16:01:30 -04005701 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005702 struct btrfs_key found_key;
5703 struct extent_buffer *leaf;
5704 int slot;
5705
5706 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
5707 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04005708 goto out;
5709
Chris Mason0b86a832008-03-24 15:01:56 -04005710 while(1) {
5711 slot = path->slots[0];
5712 leaf = path->nodes[0];
5713 if (slot >= btrfs_header_nritems(leaf)) {
5714 ret = btrfs_next_leaf(root, path);
5715 if (ret == 0)
5716 continue;
5717 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04005718 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04005719 break;
5720 }
5721 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5722
5723 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04005724 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
5725 ret = 0;
5726 goto out;
5727 }
Chris Mason0b86a832008-03-24 15:01:56 -04005728 path->slots[0]++;
5729 }
5730 ret = -ENOENT;
Chris Mason925baed2008-06-25 16:01:30 -04005731out:
Chris Mason0b86a832008-03-24 15:01:56 -04005732 return ret;
5733}
5734
Zheng Yan1a40e232008-09-26 10:09:34 -04005735int btrfs_free_block_groups(struct btrfs_fs_info *info)
5736{
5737 struct btrfs_block_group_cache *block_group;
5738 struct rb_node *n;
5739
Zheng Yan1a40e232008-09-26 10:09:34 -04005740 spin_lock(&info->block_group_cache_lock);
5741 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
5742 block_group = rb_entry(n, struct btrfs_block_group_cache,
5743 cache_node);
Zheng Yan1a40e232008-09-26 10:09:34 -04005744 rb_erase(&block_group->cache_node,
5745 &info->block_group_cache_tree);
Yan Zhengd899e052008-10-30 14:25:28 -04005746 spin_unlock(&info->block_group_cache_lock);
5747
5748 btrfs_remove_free_space_cache(block_group);
Josef Bacik80eb2342008-10-29 14:49:05 -04005749 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04005750 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04005751 up_write(&block_group->space_info->groups_sem);
Yan Zhengd2fb3432008-12-11 16:30:39 -05005752
5753 WARN_ON(atomic_read(&block_group->count) != 1);
Zheng Yan1a40e232008-09-26 10:09:34 -04005754 kfree(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04005755
5756 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04005757 }
5758 spin_unlock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04005759 return 0;
5760}
5761
Chris Mason9078a3e2007-04-26 16:46:15 -04005762int btrfs_read_block_groups(struct btrfs_root *root)
5763{
5764 struct btrfs_path *path;
5765 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04005766 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04005767 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04005768 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04005769 struct btrfs_key key;
5770 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04005771 struct extent_buffer *leaf;
Chris Mason96b51792007-10-15 16:15:19 -04005772
Chris Masonbe744172007-05-06 10:15:01 -04005773 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04005774 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005775 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04005776 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04005777 path = btrfs_alloc_path();
5778 if (!path)
5779 return -ENOMEM;
5780
5781 while(1) {
Chris Mason0b86a832008-03-24 15:01:56 -04005782 ret = find_first_block_group(root, path, &key);
5783 if (ret > 0) {
5784 ret = 0;
5785 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04005786 }
Chris Mason0b86a832008-03-24 15:01:56 -04005787 if (ret != 0)
5788 goto error;
5789
Chris Mason5f39d392007-10-15 16:14:19 -04005790 leaf = path->nodes[0];
5791 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04005792 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04005793 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04005794 ret = -ENOMEM;
Chris Mason9078a3e2007-04-26 16:46:15 -04005795 break;
5796 }
Chris Mason3e1ad542007-05-07 20:03:49 -04005797
Yan Zhengd2fb3432008-12-11 16:30:39 -05005798 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04005799 spin_lock_init(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04005800 mutex_init(&cache->alloc_mutex);
Josef Bacikea6a4782008-11-20 12:16:16 -05005801 mutex_init(&cache->cache_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005802 INIT_LIST_HEAD(&cache->list);
Chris Mason5f39d392007-10-15 16:14:19 -04005803 read_extent_buffer(leaf, &cache->item,
5804 btrfs_item_ptr_offset(leaf, path->slots[0]),
5805 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04005806 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04005807
Chris Mason9078a3e2007-04-26 16:46:15 -04005808 key.objectid = found_key.objectid + found_key.offset;
5809 btrfs_release_path(root, path);
Chris Mason0b86a832008-03-24 15:01:56 -04005810 cache->flags = btrfs_block_group_flags(&cache->item);
Chris Mason96b51792007-10-15 16:15:19 -04005811
Chris Mason6324fbf2008-03-24 15:01:59 -04005812 ret = update_space_info(info, cache->flags, found_key.offset,
5813 btrfs_block_group_used(&cache->item),
5814 &space_info);
5815 BUG_ON(ret);
5816 cache->space_info = space_info;
Josef Bacik80eb2342008-10-29 14:49:05 -04005817 down_write(&space_info->groups_sem);
5818 list_add_tail(&cache->list, &space_info->block_groups);
5819 up_write(&space_info->groups_sem);
Chris Mason6324fbf2008-03-24 15:01:59 -04005820
Josef Bacik0f9dd462008-09-23 13:14:11 -04005821 ret = btrfs_add_block_group_cache(root->fs_info, cache);
5822 BUG_ON(ret);
Chris Mason75ccf472008-09-30 19:24:06 -04005823
5824 set_avail_alloc_bits(root->fs_info, cache->flags);
Yan Zheng2b820322008-11-17 21:11:30 -05005825 if (btrfs_chunk_readonly(root, cache->key.objectid))
5826 set_block_group_readonly(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04005827 }
Chris Mason0b86a832008-03-24 15:01:56 -04005828 ret = 0;
5829error:
Chris Mason9078a3e2007-04-26 16:46:15 -04005830 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04005831 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04005832}
Chris Mason6324fbf2008-03-24 15:01:59 -04005833
5834int btrfs_make_block_group(struct btrfs_trans_handle *trans,
5835 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04005836 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04005837 u64 size)
5838{
5839 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04005840 struct btrfs_root *extent_root;
5841 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04005842
5843 extent_root = root->fs_info->extent_root;
Chris Mason6324fbf2008-03-24 15:01:59 -04005844
Chris Masone02119d2008-09-05 16:13:11 -04005845 root->fs_info->last_trans_new_blockgroup = trans->transid;
5846
Chris Mason8f18cf12008-04-25 16:53:30 -04005847 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005848 if (!cache)
5849 return -ENOMEM;
5850
Chris Masone17cade2008-04-15 15:41:47 -04005851 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04005852 cache->key.offset = size;
Yan Zhengd2fb3432008-12-11 16:30:39 -05005853 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
5854 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04005855 spin_lock_init(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04005856 mutex_init(&cache->alloc_mutex);
Josef Bacikea6a4782008-11-20 12:16:16 -05005857 mutex_init(&cache->cache_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005858 INIT_LIST_HEAD(&cache->list);
Chris Mason0ef3e662008-05-24 14:04:53 -04005859
Chris Mason6324fbf2008-03-24 15:01:59 -04005860 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04005861 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
5862 cache->flags = type;
5863 btrfs_set_block_group_flags(&cache->item, type);
5864
5865 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
5866 &cache->space_info);
5867 BUG_ON(ret);
Josef Bacik80eb2342008-10-29 14:49:05 -04005868 down_write(&cache->space_info->groups_sem);
5869 list_add_tail(&cache->list, &cache->space_info->block_groups);
5870 up_write(&cache->space_info->groups_sem);
Chris Mason6324fbf2008-03-24 15:01:59 -04005871
Josef Bacik0f9dd462008-09-23 13:14:11 -04005872 ret = btrfs_add_block_group_cache(root->fs_info, cache);
5873 BUG_ON(ret);
Chris Masonc286ac42008-07-22 23:06:41 -04005874
Chris Mason6324fbf2008-03-24 15:01:59 -04005875 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
5876 sizeof(cache->item));
5877 BUG_ON(ret);
5878
Chris Mason87ef2bb2008-10-30 11:23:27 -04005879 finish_current_insert(trans, extent_root, 0);
5880 ret = del_pending_extents(trans, extent_root, 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04005881 BUG_ON(ret);
Chris Masond18a2c42008-04-04 15:40:00 -04005882 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04005883
Chris Mason6324fbf2008-03-24 15:01:59 -04005884 return 0;
5885}
Zheng Yan1a40e232008-09-26 10:09:34 -04005886
5887int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
5888 struct btrfs_root *root, u64 group_start)
5889{
5890 struct btrfs_path *path;
5891 struct btrfs_block_group_cache *block_group;
5892 struct btrfs_key key;
5893 int ret;
5894
Zheng Yan1a40e232008-09-26 10:09:34 -04005895 root = root->fs_info->extent_root;
5896
5897 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
5898 BUG_ON(!block_group);
Yan Zhengc146afa2008-11-12 14:34:12 -05005899 BUG_ON(!block_group->ro);
Zheng Yan1a40e232008-09-26 10:09:34 -04005900
5901 memcpy(&key, &block_group->key, sizeof(key));
5902
5903 path = btrfs_alloc_path();
5904 BUG_ON(!path);
5905
5906 btrfs_remove_free_space_cache(block_group);
5907 rb_erase(&block_group->cache_node,
5908 &root->fs_info->block_group_cache_tree);
Josef Bacik80eb2342008-10-29 14:49:05 -04005909 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04005910 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04005911 up_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04005912
Yan Zhengc146afa2008-11-12 14:34:12 -05005913 spin_lock(&block_group->space_info->lock);
5914 block_group->space_info->total_bytes -= block_group->key.offset;
5915 block_group->space_info->bytes_readonly -= block_group->key.offset;
5916 spin_unlock(&block_group->space_info->lock);
Yan Zheng2b820322008-11-17 21:11:30 -05005917 block_group->space_info->full = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05005918
Yan Zhengd2fb3432008-12-11 16:30:39 -05005919 put_block_group(block_group);
5920 put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04005921
5922 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
5923 if (ret > 0)
5924 ret = -EIO;
5925 if (ret < 0)
5926 goto out;
5927
5928 ret = btrfs_del_item(trans, root, path);
5929out:
5930 btrfs_free_path(path);
5931 return ret;
5932}