blob: 293da650873f5193c726c4883bc6ecc5114b0a1a [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 Masond3977122009-01-05 21:25:51 -050052static int finish_current_insert(struct btrfs_trans_handle *trans,
53 struct btrfs_root *extent_root, int all);
54static int del_pending_extents(struct btrfs_trans_handle *trans,
55 struct 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 Zhenge4404d62008-12-12 10:03:26 -0500221 u64 last;
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 Zhenge4404d62008-12-12 10:03:26 -0500242 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
243 key.objectid = last;
Chris Masone37c9e62007-05-09 20:13:14 -0400244 key.offset = 0;
245 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
246 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
247 if (ret < 0)
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400248 goto err;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500249
Chris Masond3977122009-01-05 21:25:51 -0500250 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400251 leaf = path->nodes[0];
Chris Masone37c9e62007-05-09 20:13:14 -0400252 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -0400253 if (slot >= btrfs_header_nritems(leaf)) {
Chris Masone37c9e62007-05-09 20:13:14 -0400254 ret = btrfs_next_leaf(root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400255 if (ret < 0)
256 goto err;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400257 if (ret == 0)
Chris Masone37c9e62007-05-09 20:13:14 -0400258 continue;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400259 else
Chris Masone37c9e62007-05-09 20:13:14 -0400260 break;
Chris Masone37c9e62007-05-09 20:13:14 -0400261 }
Chris Mason5f39d392007-10-15 16:14:19 -0400262 btrfs_item_key_to_cpu(leaf, &key, slot);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400263 if (key.objectid < block_group->key.objectid)
Yan7d7d6062007-09-14 16:15:28 -0400264 goto next;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400265
Chris Masone37c9e62007-05-09 20:13:14 -0400266 if (key.objectid >= block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -0400267 block_group->key.offset)
Yan7d7d6062007-09-14 16:15:28 -0400268 break;
Yan7d7d6062007-09-14 16:15:28 -0400269
270 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
Josef Bacik0f9dd462008-09-23 13:14:11 -0400271 add_new_free_space(block_group, root->fs_info, last,
272 key.objectid);
273
Yan7d7d6062007-09-14 16:15:28 -0400274 last = key.objectid + key.offset;
Chris Masone37c9e62007-05-09 20:13:14 -0400275 }
Yan7d7d6062007-09-14 16:15:28 -0400276next:
Chris Masone37c9e62007-05-09 20:13:14 -0400277 path->slots[0]++;
278 }
279
Josef Bacik0f9dd462008-09-23 13:14:11 -0400280 add_new_free_space(block_group, root->fs_info, last,
281 block_group->key.objectid +
282 block_group->key.offset);
283
Yan Zhenga512bbf2008-12-08 16:46:26 -0500284 remove_sb_from_cache(root, block_group);
Chris Masone37c9e62007-05-09 20:13:14 -0400285 block_group->cached = 1;
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400286 ret = 0;
Chris Mason54aa1f42007-06-22 14:16:25 -0400287err:
Chris Masone37c9e62007-05-09 20:13:14 -0400288 btrfs_free_path(path);
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400289 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -0400290}
291
Josef Bacik0f9dd462008-09-23 13:14:11 -0400292/*
293 * return the block group that starts at or after bytenr
294 */
Chris Masond3977122009-01-05 21:25:51 -0500295static struct btrfs_block_group_cache *
296btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
Chris Mason0ef3e662008-05-24 14:04:53 -0400297{
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 Masond3977122009-01-05 21:25:51 -0500308struct btrfs_block_group_cache *btrfs_lookup_block_group(
309 struct btrfs_fs_info *info,
310 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
Chris Masond3977122009-01-05 21:25:51 -0500494static noinline int lookup_extent_backref(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -0400495 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 */
Chris Masond3977122009-01-05 21:25:51 -0500539static noinline int update_backrefs(struct btrfs_trans_handle *trans,
Josef Bacikf3465ca2008-11-12 14:19:50 -0500540 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)) {
Chris Masond3977122009-01-05 21:25:51 -0500575 printk(KERN_ERR "btrfs couldn't find %llu, parent %llu, "
576 "root %llu, owner %u\n",
577 (unsigned long long)op->bytenr,
578 (unsigned long long)op->orig_parent,
579 (unsigned long long)ref_root, op->level);
Josef Bacikf3465ca2008-11-12 14:19:50 -0500580 btrfs_print_leaf(extent_root, leaf);
581 BUG();
582 }
583
584 key.objectid = op->bytenr;
585 key.offset = op->parent;
586 key.type = BTRFS_EXTENT_REF_KEY;
587 ret = btrfs_set_item_key_safe(trans, extent_root, path, &key);
588 BUG_ON(ret);
589 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
590 btrfs_set_ref_generation(leaf, ref, op->generation);
591
592 cur = cur->next;
593
594 list_del_init(&op->list);
595 unlock_extent(&info->extent_ins, op->bytenr,
596 op->bytenr + op->num_bytes - 1, GFP_NOFS);
597 kfree(op);
598
599 if (cur == update_list) {
600 btrfs_mark_buffer_dirty(path->nodes[0]);
601 btrfs_release_path(extent_root, path);
602 goto out;
603 }
604
605 op = list_entry(cur, struct pending_extent_op, list);
606
607 path->slots[0]++;
608 while (path->slots[0] < btrfs_header_nritems(leaf)) {
609 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
610 if (key.objectid == op->bytenr &&
611 key.type == BTRFS_EXTENT_REF_KEY)
612 goto loop;
613 path->slots[0]++;
614 }
615
616 btrfs_mark_buffer_dirty(path->nodes[0]);
617 btrfs_release_path(extent_root, path);
618 goto search;
619
620out:
621 return 0;
622}
623
Chris Masond3977122009-01-05 21:25:51 -0500624static noinline int insert_extents(struct btrfs_trans_handle *trans,
Josef Bacikf3465ca2008-11-12 14:19:50 -0500625 struct btrfs_root *extent_root,
626 struct btrfs_path *path,
627 struct list_head *insert_list, int nr)
628{
629 struct btrfs_key *keys;
630 u32 *data_size;
631 struct pending_extent_op *op;
632 struct extent_buffer *leaf;
633 struct list_head *cur = insert_list->next;
634 struct btrfs_fs_info *info = extent_root->fs_info;
635 u64 ref_root = extent_root->root_key.objectid;
636 int i = 0, last = 0, ret;
637 int total = nr * 2;
638
639 if (!nr)
640 return 0;
641
642 keys = kzalloc(total * sizeof(struct btrfs_key), GFP_NOFS);
643 if (!keys)
644 return -ENOMEM;
645
646 data_size = kzalloc(total * sizeof(u32), GFP_NOFS);
647 if (!data_size) {
648 kfree(keys);
649 return -ENOMEM;
650 }
651
652 list_for_each_entry(op, insert_list, list) {
653 keys[i].objectid = op->bytenr;
654 keys[i].offset = op->num_bytes;
655 keys[i].type = BTRFS_EXTENT_ITEM_KEY;
656 data_size[i] = sizeof(struct btrfs_extent_item);
657 i++;
658
659 keys[i].objectid = op->bytenr;
660 keys[i].offset = op->parent;
661 keys[i].type = BTRFS_EXTENT_REF_KEY;
662 data_size[i] = sizeof(struct btrfs_extent_ref);
663 i++;
664 }
665
666 op = list_entry(cur, struct pending_extent_op, list);
667 i = 0;
668 while (i < total) {
669 int c;
670 ret = btrfs_insert_some_items(trans, extent_root, path,
671 keys+i, data_size+i, total-i);
672 BUG_ON(ret < 0);
673
674 if (last && ret > 1)
675 BUG();
676
677 leaf = path->nodes[0];
678 for (c = 0; c < ret; c++) {
679 int ref_first = keys[i].type == BTRFS_EXTENT_REF_KEY;
680
681 /*
682 * if the first item we inserted was a backref, then
683 * the EXTENT_ITEM will be the odd c's, else it will
684 * be the even c's
685 */
686 if ((ref_first && (c % 2)) ||
687 (!ref_first && !(c % 2))) {
688 struct btrfs_extent_item *itm;
689
690 itm = btrfs_item_ptr(leaf, path->slots[0] + c,
691 struct btrfs_extent_item);
692 btrfs_set_extent_refs(path->nodes[0], itm, 1);
693 op->del++;
694 } else {
695 struct btrfs_extent_ref *ref;
696
697 ref = btrfs_item_ptr(leaf, path->slots[0] + c,
698 struct btrfs_extent_ref);
699 btrfs_set_ref_root(leaf, ref, ref_root);
700 btrfs_set_ref_generation(leaf, ref,
701 op->generation);
702 btrfs_set_ref_objectid(leaf, ref, op->level);
703 btrfs_set_ref_num_refs(leaf, ref, 1);
704 op->del++;
705 }
706
707 /*
708 * using del to see when its ok to free up the
709 * pending_extent_op. In the case where we insert the
710 * last item on the list in order to help do batching
711 * we need to not free the extent op until we actually
712 * insert the extent_item
713 */
714 if (op->del == 2) {
715 unlock_extent(&info->extent_ins, op->bytenr,
716 op->bytenr + op->num_bytes - 1,
717 GFP_NOFS);
718 cur = cur->next;
719 list_del_init(&op->list);
720 kfree(op);
721 if (cur != insert_list)
722 op = list_entry(cur,
723 struct pending_extent_op,
724 list);
725 }
726 }
727 btrfs_mark_buffer_dirty(leaf);
728 btrfs_release_path(extent_root, path);
729
730 /*
731 * Ok backref's and items usually go right next to eachother,
732 * but if we could only insert 1 item that means that we
733 * inserted on the end of a leaf, and we have no idea what may
734 * be on the next leaf so we just play it safe. In order to
735 * try and help this case we insert the last thing on our
736 * insert list so hopefully it will end up being the last
737 * thing on the leaf and everything else will be before it,
738 * which will let us insert a whole bunch of items at the same
739 * time.
740 */
741 if (ret == 1 && !last && (i + ret < total)) {
742 /*
743 * last: where we will pick up the next time around
744 * i: our current key to insert, will be total - 1
745 * cur: the current op we are screwing with
746 * op: duh
747 */
748 last = i + ret;
749 i = total - 1;
750 cur = insert_list->prev;
751 op = list_entry(cur, struct pending_extent_op, list);
752 } else if (last) {
753 /*
754 * ok we successfully inserted the last item on the
755 * list, lets reset everything
756 *
757 * i: our current key to insert, so where we left off
758 * last time
759 * last: done with this
760 * cur: the op we are messing with
761 * op: duh
762 * total: since we inserted the last key, we need to
763 * decrement total so we dont overflow
764 */
765 i = last;
766 last = 0;
Josef Bacikf3465ca2008-11-12 14:19:50 -0500767 total--;
Liu Huib4eec2c2008-11-18 11:30:10 -0500768 if (i < total) {
769 cur = insert_list->next;
770 op = list_entry(cur, struct pending_extent_op,
771 list);
772 }
Josef Bacikf3465ca2008-11-12 14:19:50 -0500773 } else {
774 i += ret;
775 }
776
777 cond_resched();
778 }
779 ret = 0;
780 kfree(keys);
781 kfree(data_size);
782 return ret;
783}
784
Chris Masond3977122009-01-05 21:25:51 -0500785static noinline int insert_extent_backref(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -0400786 struct btrfs_root *root,
787 struct btrfs_path *path,
788 u64 bytenr, u64 parent,
789 u64 ref_root, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400790 u64 owner_objectid)
Zheng Yan31840ae2008-09-23 13:14:14 -0400791{
792 struct btrfs_key key;
793 struct extent_buffer *leaf;
794 struct btrfs_extent_ref *ref;
795 u32 num_refs;
796 int ret;
797
798 key.objectid = bytenr;
799 key.type = BTRFS_EXTENT_REF_KEY;
800 key.offset = parent;
801
802 ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(*ref));
803 if (ret == 0) {
804 leaf = path->nodes[0];
805 ref = btrfs_item_ptr(leaf, path->slots[0],
806 struct btrfs_extent_ref);
807 btrfs_set_ref_root(leaf, ref, ref_root);
808 btrfs_set_ref_generation(leaf, ref, ref_generation);
809 btrfs_set_ref_objectid(leaf, ref, owner_objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -0400810 btrfs_set_ref_num_refs(leaf, ref, 1);
811 } else if (ret == -EEXIST) {
812 u64 existing_owner;
813 BUG_ON(owner_objectid < BTRFS_FIRST_FREE_OBJECTID);
814 leaf = path->nodes[0];
815 ref = btrfs_item_ptr(leaf, path->slots[0],
816 struct btrfs_extent_ref);
817 if (btrfs_ref_root(leaf, ref) != ref_root ||
818 btrfs_ref_generation(leaf, ref) != ref_generation) {
819 ret = -EIO;
820 WARN_ON(1);
821 goto out;
822 }
823
824 num_refs = btrfs_ref_num_refs(leaf, ref);
825 BUG_ON(num_refs == 0);
826 btrfs_set_ref_num_refs(leaf, ref, num_refs + 1);
827
828 existing_owner = btrfs_ref_objectid(leaf, ref);
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400829 if (existing_owner != owner_objectid &&
830 existing_owner != BTRFS_MULTIPLE_OBJECTIDS) {
Zheng Yan31840ae2008-09-23 13:14:14 -0400831 btrfs_set_ref_objectid(leaf, ref,
832 BTRFS_MULTIPLE_OBJECTIDS);
Zheng Yan31840ae2008-09-23 13:14:14 -0400833 }
834 ret = 0;
835 } else {
836 goto out;
837 }
Chris Mason7bb86312007-12-11 09:25:06 -0500838 btrfs_mark_buffer_dirty(path->nodes[0]);
839out:
840 btrfs_release_path(root, path);
841 return ret;
Chris Mason74493f72007-12-11 09:25:06 -0500842}
843
Chris Masond3977122009-01-05 21:25:51 -0500844static noinline int remove_extent_backref(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -0400845 struct btrfs_root *root,
846 struct btrfs_path *path)
847{
848 struct extent_buffer *leaf;
849 struct btrfs_extent_ref *ref;
850 u32 num_refs;
851 int ret = 0;
852
853 leaf = path->nodes[0];
854 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
855 num_refs = btrfs_ref_num_refs(leaf, ref);
856 BUG_ON(num_refs == 0);
857 num_refs -= 1;
858 if (num_refs == 0) {
859 ret = btrfs_del_item(trans, root, path);
860 } else {
861 btrfs_set_ref_num_refs(leaf, ref, num_refs);
862 btrfs_mark_buffer_dirty(leaf);
863 }
864 btrfs_release_path(root, path);
865 return ret;
866}
867
Chris Mason4b4e25f2008-11-20 10:22:27 -0500868#ifdef BIO_RW_DISCARD
Chris Mason15916de2008-11-19 21:17:22 -0500869static void btrfs_issue_discard(struct block_device *bdev,
870 u64 start, u64 len)
871{
Chris Mason15916de2008-11-19 21:17:22 -0500872 blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_KERNEL);
Chris Mason15916de2008-11-19 21:17:22 -0500873}
Chris Mason4b4e25f2008-11-20 10:22:27 -0500874#endif
Chris Mason15916de2008-11-19 21:17:22 -0500875
Liu Hui1f3c79a2009-01-05 15:57:51 -0500876static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
877 u64 num_bytes)
878{
879#ifdef BIO_RW_DISCARD
880 int ret;
881 u64 map_length = num_bytes;
882 struct btrfs_multi_bio *multi = NULL;
883
884 /* Tell the block device(s) that the sectors can be discarded */
885 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
886 bytenr, &map_length, &multi, 0);
887 if (!ret) {
888 struct btrfs_bio_stripe *stripe = multi->stripes;
889 int i;
890
891 if (map_length > num_bytes)
892 map_length = num_bytes;
893
894 for (i = 0; i < multi->num_stripes; i++, stripe++) {
895 btrfs_issue_discard(stripe->dev->bdev,
896 stripe->physical,
897 map_length);
898 }
899 kfree(multi);
900 }
901
902 return ret;
903#else
904 return 0;
905#endif
906}
907
Chris Masond3977122009-01-05 21:25:51 -0500908static noinline int free_extents(struct btrfs_trans_handle *trans,
Josef Bacikf3465ca2008-11-12 14:19:50 -0500909 struct btrfs_root *extent_root,
910 struct list_head *del_list)
911{
912 struct btrfs_fs_info *info = extent_root->fs_info;
913 struct btrfs_path *path;
914 struct btrfs_key key, found_key;
915 struct extent_buffer *leaf;
916 struct list_head *cur;
917 struct pending_extent_op *op;
918 struct btrfs_extent_item *ei;
919 int ret, num_to_del, extent_slot = 0, found_extent = 0;
920 u32 refs;
921 u64 bytes_freed = 0;
922
923 path = btrfs_alloc_path();
924 if (!path)
925 return -ENOMEM;
926 path->reada = 1;
927
928search:
929 /* search for the backref for the current ref we want to delete */
930 cur = del_list->next;
931 op = list_entry(cur, struct pending_extent_op, list);
932 ret = lookup_extent_backref(trans, extent_root, path, op->bytenr,
933 op->orig_parent,
934 extent_root->root_key.objectid,
935 op->orig_generation, op->level, 1);
936 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -0500937 printk(KERN_ERR "btrfs unable to find backref byte nr %llu "
938 "root %llu gen %llu owner %u\n",
939 (unsigned long long)op->bytenr,
940 (unsigned long long)extent_root->root_key.objectid,
941 (unsigned long long)op->orig_generation, op->level);
Josef Bacikf3465ca2008-11-12 14:19:50 -0500942 btrfs_print_leaf(extent_root, path->nodes[0]);
943 WARN_ON(1);
944 goto out;
945 }
946
947 extent_slot = path->slots[0];
948 num_to_del = 1;
949 found_extent = 0;
950
951 /*
952 * if we aren't the first item on the leaf we can move back one and see
953 * if our ref is right next to our extent item
954 */
955 if (likely(extent_slot)) {
956 extent_slot--;
957 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
958 extent_slot);
959 if (found_key.objectid == op->bytenr &&
960 found_key.type == BTRFS_EXTENT_ITEM_KEY &&
961 found_key.offset == op->num_bytes) {
962 num_to_del++;
963 found_extent = 1;
964 }
965 }
966
967 /*
968 * if we didn't find the extent we need to delete the backref and then
969 * search for the extent item key so we can update its ref count
970 */
971 if (!found_extent) {
972 key.objectid = op->bytenr;
973 key.type = BTRFS_EXTENT_ITEM_KEY;
974 key.offset = op->num_bytes;
975
976 ret = remove_extent_backref(trans, extent_root, path);
977 BUG_ON(ret);
978 btrfs_release_path(extent_root, path);
979 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
980 BUG_ON(ret);
981 extent_slot = path->slots[0];
982 }
983
984 /* this is where we update the ref count for the extent */
985 leaf = path->nodes[0];
986 ei = btrfs_item_ptr(leaf, extent_slot, struct btrfs_extent_item);
987 refs = btrfs_extent_refs(leaf, ei);
988 BUG_ON(refs == 0);
989 refs--;
990 btrfs_set_extent_refs(leaf, ei, refs);
991
992 btrfs_mark_buffer_dirty(leaf);
993
994 /*
995 * This extent needs deleting. The reason cur_slot is extent_slot +
996 * num_to_del is because extent_slot points to the slot where the extent
997 * is, and if the backref was not right next to the extent we will be
998 * deleting at least 1 item, and will want to start searching at the
999 * slot directly next to extent_slot. However if we did find the
1000 * backref next to the extent item them we will be deleting at least 2
1001 * items and will want to start searching directly after the ref slot
1002 */
1003 if (!refs) {
1004 struct list_head *pos, *n, *end;
1005 int cur_slot = extent_slot+num_to_del;
1006 u64 super_used;
1007 u64 root_used;
1008
1009 path->slots[0] = extent_slot;
1010 bytes_freed = op->num_bytes;
1011
Josef Bacike3e469f2008-11-17 21:11:49 -05001012 mutex_lock(&info->pinned_mutex);
1013 ret = pin_down_bytes(trans, extent_root, op->bytenr,
1014 op->num_bytes, op->level >=
1015 BTRFS_FIRST_FREE_OBJECTID);
1016 mutex_unlock(&info->pinned_mutex);
1017 BUG_ON(ret < 0);
1018 op->del = ret;
1019
Josef Bacikf3465ca2008-11-12 14:19:50 -05001020 /*
1021 * we need to see if we can delete multiple things at once, so
1022 * start looping through the list of extents we are wanting to
1023 * delete and see if their extent/backref's are right next to
1024 * eachother and the extents only have 1 ref
1025 */
1026 for (pos = cur->next; pos != del_list; pos = pos->next) {
1027 struct pending_extent_op *tmp;
1028
1029 tmp = list_entry(pos, struct pending_extent_op, list);
1030
1031 /* we only want to delete extent+ref at this stage */
1032 if (cur_slot >= btrfs_header_nritems(leaf) - 1)
1033 break;
1034
1035 btrfs_item_key_to_cpu(leaf, &found_key, cur_slot);
1036 if (found_key.objectid != tmp->bytenr ||
1037 found_key.type != BTRFS_EXTENT_ITEM_KEY ||
1038 found_key.offset != tmp->num_bytes)
1039 break;
1040
1041 /* check to make sure this extent only has one ref */
1042 ei = btrfs_item_ptr(leaf, cur_slot,
1043 struct btrfs_extent_item);
1044 if (btrfs_extent_refs(leaf, ei) != 1)
1045 break;
1046
1047 btrfs_item_key_to_cpu(leaf, &found_key, cur_slot+1);
1048 if (found_key.objectid != tmp->bytenr ||
1049 found_key.type != BTRFS_EXTENT_REF_KEY ||
1050 found_key.offset != tmp->orig_parent)
1051 break;
1052
1053 /*
1054 * the ref is right next to the extent, we can set the
1055 * ref count to 0 since we will delete them both now
1056 */
1057 btrfs_set_extent_refs(leaf, ei, 0);
1058
1059 /* pin down the bytes for this extent */
1060 mutex_lock(&info->pinned_mutex);
1061 ret = pin_down_bytes(trans, extent_root, tmp->bytenr,
1062 tmp->num_bytes, tmp->level >=
1063 BTRFS_FIRST_FREE_OBJECTID);
1064 mutex_unlock(&info->pinned_mutex);
1065 BUG_ON(ret < 0);
1066
1067 /*
1068 * use the del field to tell if we need to go ahead and
1069 * free up the extent when we delete the item or not.
1070 */
1071 tmp->del = ret;
1072 bytes_freed += tmp->num_bytes;
1073
1074 num_to_del += 2;
1075 cur_slot += 2;
1076 }
1077 end = pos;
1078
1079 /* update the free space counters */
Chris Mason75eff682008-12-15 15:54:40 -05001080 spin_lock(&info->delalloc_lock);
Josef Bacikf3465ca2008-11-12 14:19:50 -05001081 super_used = btrfs_super_bytes_used(&info->super_copy);
1082 btrfs_set_super_bytes_used(&info->super_copy,
1083 super_used - bytes_freed);
Josef Bacikf3465ca2008-11-12 14:19:50 -05001084
1085 root_used = btrfs_root_used(&extent_root->root_item);
1086 btrfs_set_root_used(&extent_root->root_item,
1087 root_used - bytes_freed);
Yan Zheng34bf63c2008-12-19 10:58:46 -05001088 spin_unlock(&info->delalloc_lock);
Josef Bacikf3465ca2008-11-12 14:19:50 -05001089
1090 /* delete the items */
1091 ret = btrfs_del_items(trans, extent_root, path,
1092 path->slots[0], num_to_del);
1093 BUG_ON(ret);
1094
1095 /*
1096 * loop through the extents we deleted and do the cleanup work
1097 * on them
1098 */
1099 for (pos = cur, n = pos->next; pos != end;
1100 pos = n, n = pos->next) {
1101 struct pending_extent_op *tmp;
Josef Bacikf3465ca2008-11-12 14:19:50 -05001102 tmp = list_entry(pos, struct pending_extent_op, list);
1103
1104 /*
1105 * remember tmp->del tells us wether or not we pinned
1106 * down the extent
1107 */
1108 ret = update_block_group(trans, extent_root,
1109 tmp->bytenr, tmp->num_bytes, 0,
1110 tmp->del);
1111 BUG_ON(ret);
1112
Josef Bacikf3465ca2008-11-12 14:19:50 -05001113 list_del_init(&tmp->list);
1114 unlock_extent(&info->extent_ins, tmp->bytenr,
1115 tmp->bytenr + tmp->num_bytes - 1,
1116 GFP_NOFS);
1117 kfree(tmp);
1118 }
1119 } else if (refs && found_extent) {
1120 /*
1121 * the ref and extent were right next to eachother, but the
1122 * extent still has a ref, so just free the backref and keep
1123 * going
1124 */
1125 ret = remove_extent_backref(trans, extent_root, path);
1126 BUG_ON(ret);
1127
1128 list_del_init(&op->list);
1129 unlock_extent(&info->extent_ins, op->bytenr,
1130 op->bytenr + op->num_bytes - 1, GFP_NOFS);
1131 kfree(op);
1132 } else {
1133 /*
1134 * the extent has multiple refs and the backref we were looking
1135 * for was not right next to it, so just unlock and go next,
1136 * we're good to go
1137 */
1138 list_del_init(&op->list);
1139 unlock_extent(&info->extent_ins, op->bytenr,
1140 op->bytenr + op->num_bytes - 1, GFP_NOFS);
1141 kfree(op);
1142 }
1143
1144 btrfs_release_path(extent_root, path);
1145 if (!list_empty(del_list))
1146 goto search;
1147
1148out:
1149 btrfs_free_path(path);
1150 return ret;
1151}
1152
Zheng Yan31840ae2008-09-23 13:14:14 -04001153static int __btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
1154 struct btrfs_root *root, u64 bytenr,
1155 u64 orig_parent, u64 parent,
1156 u64 orig_root, u64 ref_root,
1157 u64 orig_generation, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001158 u64 owner_objectid)
Zheng Yan31840ae2008-09-23 13:14:14 -04001159{
1160 int ret;
1161 struct btrfs_root *extent_root = root->fs_info->extent_root;
1162 struct btrfs_path *path;
1163
1164 if (root == root->fs_info->extent_root) {
1165 struct pending_extent_op *extent_op;
1166 u64 num_bytes;
1167
1168 BUG_ON(owner_objectid >= BTRFS_MAX_LEVEL);
1169 num_bytes = btrfs_level_size(root, (int)owner_objectid);
Josef Bacik25179202008-10-29 14:49:05 -04001170 mutex_lock(&root->fs_info->extent_ins_mutex);
Zheng Yan31840ae2008-09-23 13:14:14 -04001171 if (test_range_bit(&root->fs_info->extent_ins, bytenr,
Josef Bacik25179202008-10-29 14:49:05 -04001172 bytenr + num_bytes - 1, EXTENT_WRITEBACK, 0)) {
Zheng Yan31840ae2008-09-23 13:14:14 -04001173 u64 priv;
1174 ret = get_state_private(&root->fs_info->extent_ins,
1175 bytenr, &priv);
1176 BUG_ON(ret);
1177 extent_op = (struct pending_extent_op *)
1178 (unsigned long)priv;
1179 BUG_ON(extent_op->parent != orig_parent);
1180 BUG_ON(extent_op->generation != orig_generation);
Josef Bacik25179202008-10-29 14:49:05 -04001181
Zheng Yan31840ae2008-09-23 13:14:14 -04001182 extent_op->parent = parent;
1183 extent_op->generation = ref_generation;
1184 } else {
1185 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
1186 BUG_ON(!extent_op);
1187
1188 extent_op->type = PENDING_BACKREF_UPDATE;
1189 extent_op->bytenr = bytenr;
1190 extent_op->num_bytes = num_bytes;
1191 extent_op->parent = parent;
1192 extent_op->orig_parent = orig_parent;
1193 extent_op->generation = ref_generation;
1194 extent_op->orig_generation = orig_generation;
1195 extent_op->level = (int)owner_objectid;
Josef Bacikf3465ca2008-11-12 14:19:50 -05001196 INIT_LIST_HEAD(&extent_op->list);
1197 extent_op->del = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001198
1199 set_extent_bits(&root->fs_info->extent_ins,
1200 bytenr, bytenr + num_bytes - 1,
Josef Bacik25179202008-10-29 14:49:05 -04001201 EXTENT_WRITEBACK, GFP_NOFS);
Zheng Yan31840ae2008-09-23 13:14:14 -04001202 set_state_private(&root->fs_info->extent_ins,
1203 bytenr, (unsigned long)extent_op);
1204 }
Josef Bacik25179202008-10-29 14:49:05 -04001205 mutex_unlock(&root->fs_info->extent_ins_mutex);
Zheng Yan31840ae2008-09-23 13:14:14 -04001206 return 0;
1207 }
1208
1209 path = btrfs_alloc_path();
1210 if (!path)
1211 return -ENOMEM;
1212 ret = lookup_extent_backref(trans, extent_root, path,
1213 bytenr, orig_parent, orig_root,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001214 orig_generation, owner_objectid, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001215 if (ret)
1216 goto out;
1217 ret = remove_extent_backref(trans, extent_root, path);
1218 if (ret)
1219 goto out;
1220 ret = insert_extent_backref(trans, extent_root, path, bytenr,
1221 parent, ref_root, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001222 owner_objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -04001223 BUG_ON(ret);
Chris Mason87ef2bb2008-10-30 11:23:27 -04001224 finish_current_insert(trans, extent_root, 0);
1225 del_pending_extents(trans, extent_root, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04001226out:
1227 btrfs_free_path(path);
1228 return ret;
1229}
1230
1231int btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
1232 struct btrfs_root *root, u64 bytenr,
1233 u64 orig_parent, u64 parent,
1234 u64 ref_root, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001235 u64 owner_objectid)
Zheng Yan31840ae2008-09-23 13:14:14 -04001236{
1237 int ret;
1238 if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
1239 owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
1240 return 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001241 ret = __btrfs_update_extent_ref(trans, root, bytenr, orig_parent,
1242 parent, ref_root, ref_root,
1243 ref_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001244 owner_objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -04001245 return ret;
1246}
1247
Chris Mason925baed2008-06-25 16:01:30 -04001248static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04001249 struct btrfs_root *root, u64 bytenr,
1250 u64 orig_parent, u64 parent,
1251 u64 orig_root, u64 ref_root,
1252 u64 orig_generation, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001253 u64 owner_objectid)
Chris Mason02217ed2007-03-02 16:08:05 -05001254{
Chris Mason5caf2a02007-04-02 11:20:42 -04001255 struct btrfs_path *path;
Chris Mason02217ed2007-03-02 16:08:05 -05001256 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -04001257 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001258 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -04001259 struct btrfs_extent_item *item;
Chris Masoncf27e1e2007-03-13 09:49:06 -04001260 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -05001261
Chris Mason5caf2a02007-04-02 11:20:42 -04001262 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001263 if (!path)
1264 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -04001265
Chris Mason3c12ac72008-04-21 12:01:38 -04001266 path->reada = 1;
Chris Masondb945352007-10-15 16:15:53 -04001267 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04001268 key.type = BTRFS_EXTENT_ITEM_KEY;
1269 key.offset = (u64)-1;
1270
Chris Mason5caf2a02007-04-02 11:20:42 -04001271 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -04001272 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001273 if (ret < 0)
1274 return ret;
Zheng Yan31840ae2008-09-23 13:14:14 -04001275 BUG_ON(ret == 0 || path->slots[0] == 0);
1276
1277 path->slots[0]--;
Chris Mason5f39d392007-10-15 16:14:19 -04001278 l = path->nodes[0];
Zheng Yan31840ae2008-09-23 13:14:14 -04001279
1280 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
Chris Mason771ed682008-11-06 22:02:51 -05001281 if (key.objectid != bytenr) {
1282 btrfs_print_leaf(root->fs_info->extent_root, path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05001283 printk(KERN_ERR "btrfs wanted %llu found %llu\n",
1284 (unsigned long long)bytenr,
1285 (unsigned long long)key.objectid);
Chris Mason771ed682008-11-06 22:02:51 -05001286 BUG();
1287 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001288 BUG_ON(key.type != BTRFS_EXTENT_ITEM_KEY);
1289
Chris Mason5caf2a02007-04-02 11:20:42 -04001290 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001291 refs = btrfs_extent_refs(l, item);
1292 btrfs_set_extent_refs(l, item, refs + 1);
Chris Mason5caf2a02007-04-02 11:20:42 -04001293 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masona28ec192007-03-06 20:08:01 -05001294
Chris Mason5caf2a02007-04-02 11:20:42 -04001295 btrfs_release_path(root->fs_info->extent_root, path);
Chris Mason7bb86312007-12-11 09:25:06 -05001296
Chris Mason3c12ac72008-04-21 12:01:38 -04001297 path->reada = 1;
Zheng Yan31840ae2008-09-23 13:14:14 -04001298 ret = insert_extent_backref(trans, root->fs_info->extent_root,
1299 path, bytenr, parent,
1300 ref_root, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001301 owner_objectid);
Chris Mason7bb86312007-12-11 09:25:06 -05001302 BUG_ON(ret);
Chris Mason87ef2bb2008-10-30 11:23:27 -04001303 finish_current_insert(trans, root->fs_info->extent_root, 0);
1304 del_pending_extents(trans, root->fs_info->extent_root, 0);
Chris Mason74493f72007-12-11 09:25:06 -05001305
1306 btrfs_free_path(path);
Chris Mason02217ed2007-03-02 16:08:05 -05001307 return 0;
1308}
1309
Chris Mason925baed2008-06-25 16:01:30 -04001310int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04001311 struct btrfs_root *root,
1312 u64 bytenr, u64 num_bytes, u64 parent,
1313 u64 ref_root, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001314 u64 owner_objectid)
Chris Mason925baed2008-06-25 16:01:30 -04001315{
1316 int ret;
Zheng Yan31840ae2008-09-23 13:14:14 -04001317 if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
1318 owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
1319 return 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001320 ret = __btrfs_inc_extent_ref(trans, root, bytenr, 0, parent,
1321 0, ref_root, 0, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001322 owner_objectid);
Chris Mason925baed2008-06-25 16:01:30 -04001323 return ret;
1324}
1325
Chris Masone9d0b132007-08-10 14:06:19 -04001326int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
1327 struct btrfs_root *root)
1328{
Chris Mason87ef2bb2008-10-30 11:23:27 -04001329 finish_current_insert(trans, root->fs_info->extent_root, 1);
1330 del_pending_extents(trans, root->fs_info->extent_root, 1);
Chris Masone9d0b132007-08-10 14:06:19 -04001331 return 0;
1332}
1333
Zheng Yan31840ae2008-09-23 13:14:14 -04001334int btrfs_lookup_extent_ref(struct btrfs_trans_handle *trans,
1335 struct btrfs_root *root, u64 bytenr,
1336 u64 num_bytes, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -05001337{
Chris Mason5caf2a02007-04-02 11:20:42 -04001338 struct btrfs_path *path;
Chris Masona28ec192007-03-06 20:08:01 -05001339 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -04001340 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001341 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -04001342 struct btrfs_extent_item *item;
Chris Mason5caf2a02007-04-02 11:20:42 -04001343
Chris Masondb945352007-10-15 16:15:53 -04001344 WARN_ON(num_bytes < root->sectorsize);
Chris Mason5caf2a02007-04-02 11:20:42 -04001345 path = btrfs_alloc_path();
Chris Mason3c12ac72008-04-21 12:01:38 -04001346 path->reada = 1;
Chris Masondb945352007-10-15 16:15:53 -04001347 key.objectid = bytenr;
1348 key.offset = num_bytes;
Chris Mason62e27492007-03-15 12:56:47 -04001349 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5caf2a02007-04-02 11:20:42 -04001350 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -04001351 0, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001352 if (ret < 0)
1353 goto out;
Chris Mason5f39d392007-10-15 16:14:19 -04001354 if (ret != 0) {
1355 btrfs_print_leaf(root, path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05001356 printk(KERN_INFO "btrfs failed to find block number %llu\n",
1357 (unsigned long long)bytenr);
Chris Masona28ec192007-03-06 20:08:01 -05001358 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04001359 }
1360 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -04001361 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001362 *refs = btrfs_extent_refs(l, item);
Chris Mason54aa1f42007-06-22 14:16:25 -04001363out:
Chris Mason5caf2a02007-04-02 11:20:42 -04001364 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05001365 return 0;
1366}
1367
Yan Zheng80ff3852008-10-30 14:20:02 -04001368int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
Yan Zheng17d217f2008-12-12 10:03:38 -05001369 struct btrfs_root *root, u64 objectid, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05001370{
1371 struct btrfs_root *extent_root = root->fs_info->extent_root;
1372 struct btrfs_path *path;
Yan Zhengf321e492008-07-30 09:26:11 -04001373 struct extent_buffer *leaf;
1374 struct btrfs_extent_ref *ref_item;
Chris Masonbe20aa92007-12-17 20:14:01 -05001375 struct btrfs_key key;
1376 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001377 u64 ref_root;
1378 u64 last_snapshot;
Yan Zhengf321e492008-07-30 09:26:11 -04001379 u32 nritems;
1380 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001381
Chris Masonbe20aa92007-12-17 20:14:01 -05001382 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04001383 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04001384 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05001385
Yan Zhengf321e492008-07-30 09:26:11 -04001386 path = btrfs_alloc_path();
Chris Masonbe20aa92007-12-17 20:14:01 -05001387 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
1388 if (ret < 0)
1389 goto out;
1390 BUG_ON(ret == 0);
Yan Zheng80ff3852008-10-30 14:20:02 -04001391
1392 ret = -ENOENT;
1393 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04001394 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05001395
Zheng Yan31840ae2008-09-23 13:14:14 -04001396 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04001397 leaf = path->nodes[0];
1398 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001399
1400 if (found_key.objectid != bytenr ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001401 found_key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05001402 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05001403
Yan Zheng80ff3852008-10-30 14:20:02 -04001404 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Chris Masonbe20aa92007-12-17 20:14:01 -05001405 while (1) {
Yan Zhengf321e492008-07-30 09:26:11 -04001406 leaf = path->nodes[0];
1407 nritems = btrfs_header_nritems(leaf);
Chris Masonbe20aa92007-12-17 20:14:01 -05001408 if (path->slots[0] >= nritems) {
1409 ret = btrfs_next_leaf(extent_root, path);
Yan Zhengf321e492008-07-30 09:26:11 -04001410 if (ret < 0)
1411 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05001412 if (ret == 0)
1413 continue;
1414 break;
1415 }
Yan Zhengf321e492008-07-30 09:26:11 -04001416 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001417 if (found_key.objectid != bytenr)
1418 break;
Chris Masonbd098352008-01-03 13:23:19 -05001419
Chris Masonbe20aa92007-12-17 20:14:01 -05001420 if (found_key.type != BTRFS_EXTENT_REF_KEY) {
1421 path->slots[0]++;
1422 continue;
1423 }
1424
Yan Zhengf321e492008-07-30 09:26:11 -04001425 ref_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masonbe20aa92007-12-17 20:14:01 -05001426 struct btrfs_extent_ref);
Yan Zheng80ff3852008-10-30 14:20:02 -04001427 ref_root = btrfs_ref_root(leaf, ref_item);
Yan Zheng17d217f2008-12-12 10:03:38 -05001428 if ((ref_root != root->root_key.objectid &&
1429 ref_root != BTRFS_TREE_LOG_OBJECTID) ||
1430 objectid != btrfs_ref_objectid(leaf, ref_item)) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001431 ret = 1;
1432 goto out;
Yan Zhengf321e492008-07-30 09:26:11 -04001433 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001434 if (btrfs_ref_generation(leaf, ref_item) <= last_snapshot) {
1435 ret = 1;
1436 goto out;
1437 }
Yan Zhengf321e492008-07-30 09:26:11 -04001438
Chris Masonbe20aa92007-12-17 20:14:01 -05001439 path->slots[0]++;
1440 }
Yan Zhengf321e492008-07-30 09:26:11 -04001441 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001442out:
Yan Zhengf321e492008-07-30 09:26:11 -04001443 btrfs_free_path(path);
1444 return ret;
1445}
1446
Zheng Yan31840ae2008-09-23 13:14:14 -04001447int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1448 struct extent_buffer *buf, u32 nr_extents)
Chris Mason02217ed2007-03-02 16:08:05 -05001449{
Chris Mason5f39d392007-10-15 16:14:19 -04001450 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04001451 struct btrfs_file_extent_item *fi;
Zheng Yane4657682008-09-26 10:04:53 -04001452 u64 root_gen;
1453 u32 nritems;
Chris Mason02217ed2007-03-02 16:08:05 -05001454 int i;
Chris Masondb945352007-10-15 16:15:53 -04001455 int level;
Zheng Yan31840ae2008-09-23 13:14:14 -04001456 int ret = 0;
Zheng Yane4657682008-09-26 10:04:53 -04001457 int shared = 0;
Chris Masona28ec192007-03-06 20:08:01 -05001458
Chris Mason3768f362007-03-13 16:47:54 -04001459 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -05001460 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001461
Zheng Yane4657682008-09-26 10:04:53 -04001462 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
1463 shared = 0;
1464 root_gen = root->root_key.offset;
1465 } else {
1466 shared = 1;
1467 root_gen = trans->transid - 1;
1468 }
1469
Chris Masondb945352007-10-15 16:15:53 -04001470 level = btrfs_header_level(buf);
Chris Mason5f39d392007-10-15 16:14:19 -04001471 nritems = btrfs_header_nritems(buf);
Chris Mason4a096752008-07-21 10:29:44 -04001472
Zheng Yan31840ae2008-09-23 13:14:14 -04001473 if (level == 0) {
Yan Zheng31153d82008-07-28 15:32:19 -04001474 struct btrfs_leaf_ref *ref;
1475 struct btrfs_extent_info *info;
1476
Zheng Yan31840ae2008-09-23 13:14:14 -04001477 ref = btrfs_alloc_leaf_ref(root, nr_extents);
Yan Zheng31153d82008-07-28 15:32:19 -04001478 if (!ref) {
Zheng Yan31840ae2008-09-23 13:14:14 -04001479 ret = -ENOMEM;
Yan Zheng31153d82008-07-28 15:32:19 -04001480 goto out;
1481 }
1482
Zheng Yane4657682008-09-26 10:04:53 -04001483 ref->root_gen = root_gen;
Yan Zheng31153d82008-07-28 15:32:19 -04001484 ref->bytenr = buf->start;
1485 ref->owner = btrfs_header_owner(buf);
1486 ref->generation = btrfs_header_generation(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04001487 ref->nritems = nr_extents;
Yan Zheng31153d82008-07-28 15:32:19 -04001488 info = ref->extents;
Yanbcc63ab2008-07-30 16:29:20 -04001489
Zheng Yan31840ae2008-09-23 13:14:14 -04001490 for (i = 0; nr_extents > 0 && i < nritems; i++) {
Yan Zheng31153d82008-07-28 15:32:19 -04001491 u64 disk_bytenr;
1492 btrfs_item_key_to_cpu(buf, &key, i);
1493 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1494 continue;
1495 fi = btrfs_item_ptr(buf, i,
1496 struct btrfs_file_extent_item);
1497 if (btrfs_file_extent_type(buf, fi) ==
1498 BTRFS_FILE_EXTENT_INLINE)
1499 continue;
1500 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1501 if (disk_bytenr == 0)
1502 continue;
1503
1504 info->bytenr = disk_bytenr;
1505 info->num_bytes =
1506 btrfs_file_extent_disk_num_bytes(buf, fi);
1507 info->objectid = key.objectid;
1508 info->offset = key.offset;
1509 info++;
1510 }
1511
Zheng Yane4657682008-09-26 10:04:53 -04001512 ret = btrfs_add_leaf_ref(root, ref, shared);
Yan Zheng5b84e8d2008-10-09 11:46:19 -04001513 if (ret == -EEXIST && shared) {
1514 struct btrfs_leaf_ref *old;
1515 old = btrfs_lookup_leaf_ref(root, ref->bytenr);
1516 BUG_ON(!old);
1517 btrfs_remove_leaf_ref(root, old);
1518 btrfs_free_leaf_ref(root, old);
1519 ret = btrfs_add_leaf_ref(root, ref, shared);
1520 }
Yan Zheng31153d82008-07-28 15:32:19 -04001521 WARN_ON(ret);
Yanbcc63ab2008-07-30 16:29:20 -04001522 btrfs_free_leaf_ref(root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04001523 }
1524out:
Zheng Yan31840ae2008-09-23 13:14:14 -04001525 return ret;
1526}
1527
1528int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1529 struct extent_buffer *orig_buf, struct extent_buffer *buf,
1530 u32 *nr_extents)
1531{
1532 u64 bytenr;
1533 u64 ref_root;
1534 u64 orig_root;
1535 u64 ref_generation;
1536 u64 orig_generation;
1537 u32 nritems;
1538 u32 nr_file_extents = 0;
1539 struct btrfs_key key;
1540 struct btrfs_file_extent_item *fi;
1541 int i;
1542 int level;
1543 int ret = 0;
1544 int faili = 0;
1545 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001546 u64, u64, u64, u64, u64, u64, u64, u64);
Zheng Yan31840ae2008-09-23 13:14:14 -04001547
1548 ref_root = btrfs_header_owner(buf);
1549 ref_generation = btrfs_header_generation(buf);
1550 orig_root = btrfs_header_owner(orig_buf);
1551 orig_generation = btrfs_header_generation(orig_buf);
1552
1553 nritems = btrfs_header_nritems(buf);
1554 level = btrfs_header_level(buf);
1555
1556 if (root->ref_cows) {
1557 process_func = __btrfs_inc_extent_ref;
1558 } else {
1559 if (level == 0 &&
1560 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1561 goto out;
1562 if (level != 0 &&
1563 root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
1564 goto out;
1565 process_func = __btrfs_update_extent_ref;
1566 }
1567
1568 for (i = 0; i < nritems; i++) {
1569 cond_resched();
Chris Masondb945352007-10-15 16:15:53 -04001570 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001571 btrfs_item_key_to_cpu(buf, &key, i);
1572 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04001573 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001574 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04001575 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001576 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04001577 BTRFS_FILE_EXTENT_INLINE)
1578 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04001579 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1580 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04001581 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04001582
1583 nr_file_extents++;
1584
Zheng Yan31840ae2008-09-23 13:14:14 -04001585 ret = process_func(trans, root, bytenr,
1586 orig_buf->start, buf->start,
1587 orig_root, ref_root,
1588 orig_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001589 key.objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -04001590
1591 if (ret) {
1592 faili = i;
1593 WARN_ON(1);
1594 goto fail;
1595 }
Chris Mason54aa1f42007-06-22 14:16:25 -04001596 } else {
Chris Masondb945352007-10-15 16:15:53 -04001597 bytenr = btrfs_node_blockptr(buf, i);
Zheng Yan31840ae2008-09-23 13:14:14 -04001598 ret = process_func(trans, root, bytenr,
1599 orig_buf->start, buf->start,
1600 orig_root, ref_root,
1601 orig_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001602 level - 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001603 if (ret) {
1604 faili = i;
1605 WARN_ON(1);
1606 goto fail;
1607 }
Chris Mason54aa1f42007-06-22 14:16:25 -04001608 }
1609 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001610out:
1611 if (nr_extents) {
1612 if (level == 0)
1613 *nr_extents = nr_file_extents;
1614 else
1615 *nr_extents = nritems;
1616 }
1617 return 0;
1618fail:
1619 WARN_ON(1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001620 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05001621}
1622
Zheng Yan31840ae2008-09-23 13:14:14 -04001623int btrfs_update_ref(struct btrfs_trans_handle *trans,
1624 struct btrfs_root *root, struct extent_buffer *orig_buf,
1625 struct extent_buffer *buf, int start_slot, int nr)
1626
1627{
1628 u64 bytenr;
1629 u64 ref_root;
1630 u64 orig_root;
1631 u64 ref_generation;
1632 u64 orig_generation;
1633 struct btrfs_key key;
1634 struct btrfs_file_extent_item *fi;
1635 int i;
1636 int ret;
1637 int slot;
1638 int level;
1639
1640 BUG_ON(start_slot < 0);
1641 BUG_ON(start_slot + nr > btrfs_header_nritems(buf));
1642
1643 ref_root = btrfs_header_owner(buf);
1644 ref_generation = btrfs_header_generation(buf);
1645 orig_root = btrfs_header_owner(orig_buf);
1646 orig_generation = btrfs_header_generation(orig_buf);
1647 level = btrfs_header_level(buf);
1648
1649 if (!root->ref_cows) {
1650 if (level == 0 &&
1651 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1652 return 0;
1653 if (level != 0 &&
1654 root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
1655 return 0;
1656 }
1657
1658 for (i = 0, slot = start_slot; i < nr; i++, slot++) {
1659 cond_resched();
1660 if (level == 0) {
1661 btrfs_item_key_to_cpu(buf, &key, slot);
1662 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1663 continue;
1664 fi = btrfs_item_ptr(buf, slot,
1665 struct btrfs_file_extent_item);
1666 if (btrfs_file_extent_type(buf, fi) ==
1667 BTRFS_FILE_EXTENT_INLINE)
1668 continue;
1669 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1670 if (bytenr == 0)
1671 continue;
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 key.objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -04001677 if (ret)
1678 goto fail;
1679 } else {
1680 bytenr = btrfs_node_blockptr(buf, slot);
Zheng Yan31840ae2008-09-23 13:14:14 -04001681 ret = __btrfs_update_extent_ref(trans, root, bytenr,
1682 orig_buf->start, buf->start,
1683 orig_root, ref_root,
1684 orig_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001685 level - 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001686 if (ret)
1687 goto fail;
1688 }
1689 }
1690 return 0;
1691fail:
1692 WARN_ON(1);
1693 return -1;
1694}
1695
Chris Mason9078a3e2007-04-26 16:46:15 -04001696static int write_one_cache_group(struct btrfs_trans_handle *trans,
1697 struct btrfs_root *root,
1698 struct btrfs_path *path,
1699 struct btrfs_block_group_cache *cache)
1700{
1701 int ret;
1702 int pending_ret;
1703 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04001704 unsigned long bi;
1705 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04001706
Chris Mason9078a3e2007-04-26 16:46:15 -04001707 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001708 if (ret < 0)
1709 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -04001710 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001711
1712 leaf = path->nodes[0];
1713 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
1714 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
1715 btrfs_mark_buffer_dirty(leaf);
Chris Mason9078a3e2007-04-26 16:46:15 -04001716 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -04001717fail:
Chris Mason87ef2bb2008-10-30 11:23:27 -04001718 finish_current_insert(trans, extent_root, 0);
1719 pending_ret = del_pending_extents(trans, extent_root, 0);
Chris Mason9078a3e2007-04-26 16:46:15 -04001720 if (ret)
1721 return ret;
1722 if (pending_ret)
1723 return pending_ret;
1724 return 0;
1725
1726}
1727
Chris Mason96b51792007-10-15 16:15:19 -04001728int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
1729 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04001730{
Josef Bacik0f9dd462008-09-23 13:14:11 -04001731 struct btrfs_block_group_cache *cache, *entry;
1732 struct rb_node *n;
Chris Mason9078a3e2007-04-26 16:46:15 -04001733 int err = 0;
1734 int werr = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04001735 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04001736 u64 last = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04001737
1738 path = btrfs_alloc_path();
1739 if (!path)
1740 return -ENOMEM;
1741
Chris Masond3977122009-01-05 21:25:51 -05001742 while (1) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001743 cache = NULL;
1744 spin_lock(&root->fs_info->block_group_cache_lock);
1745 for (n = rb_first(&root->fs_info->block_group_cache_tree);
1746 n; n = rb_next(n)) {
1747 entry = rb_entry(n, struct btrfs_block_group_cache,
1748 cache_node);
1749 if (entry->dirty) {
1750 cache = entry;
1751 break;
1752 }
1753 }
1754 spin_unlock(&root->fs_info->block_group_cache_lock);
1755
1756 if (!cache)
Chris Mason9078a3e2007-04-26 16:46:15 -04001757 break;
Chris Mason54aa1f42007-06-22 14:16:25 -04001758
Zheng Yane8569812008-09-26 10:05:48 -04001759 cache->dirty = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001760 last += cache->key.offset;
1761
Chris Mason96b51792007-10-15 16:15:19 -04001762 err = write_one_cache_group(trans, root,
1763 path, cache);
1764 /*
1765 * if we fail to write the cache group, we want
1766 * to keep it marked dirty in hopes that a later
1767 * write will work
1768 */
1769 if (err) {
1770 werr = err;
1771 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04001772 }
1773 }
1774 btrfs_free_path(path);
1775 return werr;
1776}
1777
Yan Zhengd2fb3432008-12-11 16:30:39 -05001778int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
1779{
1780 struct btrfs_block_group_cache *block_group;
1781 int readonly = 0;
1782
1783 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
1784 if (!block_group || block_group->ro)
1785 readonly = 1;
1786 if (block_group)
1787 put_block_group(block_group);
1788 return readonly;
1789}
1790
Chris Mason593060d2008-03-25 16:50:33 -04001791static int update_space_info(struct btrfs_fs_info *info, u64 flags,
1792 u64 total_bytes, u64 bytes_used,
1793 struct btrfs_space_info **space_info)
1794{
1795 struct btrfs_space_info *found;
1796
1797 found = __find_space_info(info, flags);
1798 if (found) {
Josef Bacik25179202008-10-29 14:49:05 -04001799 spin_lock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04001800 found->total_bytes += total_bytes;
1801 found->bytes_used += bytes_used;
Chris Mason8f18cf12008-04-25 16:53:30 -04001802 found->full = 0;
Josef Bacik25179202008-10-29 14:49:05 -04001803 spin_unlock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04001804 *space_info = found;
1805 return 0;
1806 }
Yan Zhengc146afa2008-11-12 14:34:12 -05001807 found = kzalloc(sizeof(*found), GFP_NOFS);
Chris Mason593060d2008-03-25 16:50:33 -04001808 if (!found)
1809 return -ENOMEM;
1810
1811 list_add(&found->list, &info->space_info);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001812 INIT_LIST_HEAD(&found->block_groups);
Josef Bacik80eb2342008-10-29 14:49:05 -04001813 init_rwsem(&found->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001814 spin_lock_init(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04001815 found->flags = flags;
1816 found->total_bytes = total_bytes;
1817 found->bytes_used = bytes_used;
1818 found->bytes_pinned = 0;
Zheng Yane8569812008-09-26 10:05:48 -04001819 found->bytes_reserved = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05001820 found->bytes_readonly = 0;
Chris Mason593060d2008-03-25 16:50:33 -04001821 found->full = 0;
Chris Mason0ef3e662008-05-24 14:04:53 -04001822 found->force_alloc = 0;
Chris Mason593060d2008-03-25 16:50:33 -04001823 *space_info = found;
1824 return 0;
1825}
1826
Chris Mason8790d502008-04-03 16:29:03 -04001827static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
1828{
1829 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
Chris Mason611f0e02008-04-03 16:29:03 -04001830 BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04001831 BTRFS_BLOCK_GROUP_RAID10 |
Chris Mason611f0e02008-04-03 16:29:03 -04001832 BTRFS_BLOCK_GROUP_DUP);
Chris Mason8790d502008-04-03 16:29:03 -04001833 if (extra_flags) {
1834 if (flags & BTRFS_BLOCK_GROUP_DATA)
1835 fs_info->avail_data_alloc_bits |= extra_flags;
1836 if (flags & BTRFS_BLOCK_GROUP_METADATA)
1837 fs_info->avail_metadata_alloc_bits |= extra_flags;
1838 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
1839 fs_info->avail_system_alloc_bits |= extra_flags;
1840 }
1841}
Chris Mason593060d2008-03-25 16:50:33 -04001842
Yan Zhengc146afa2008-11-12 14:34:12 -05001843static void set_block_group_readonly(struct btrfs_block_group_cache *cache)
1844{
1845 spin_lock(&cache->space_info->lock);
1846 spin_lock(&cache->lock);
1847 if (!cache->ro) {
1848 cache->space_info->bytes_readonly += cache->key.offset -
1849 btrfs_block_group_used(&cache->item);
1850 cache->ro = 1;
1851 }
1852 spin_unlock(&cache->lock);
1853 spin_unlock(&cache->space_info->lock);
1854}
1855
Yan Zheng2b820322008-11-17 21:11:30 -05001856u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04001857{
Yan Zheng2b820322008-11-17 21:11:30 -05001858 u64 num_devices = root->fs_info->fs_devices->rw_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001859
1860 if (num_devices == 1)
1861 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
1862 if (num_devices < 4)
1863 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
1864
Chris Masonec44a352008-04-28 15:29:52 -04001865 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
1866 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
Chris Masona061fc82008-05-07 11:43:44 -04001867 BTRFS_BLOCK_GROUP_RAID10))) {
Chris Masonec44a352008-04-28 15:29:52 -04001868 flags &= ~BTRFS_BLOCK_GROUP_DUP;
Chris Masona061fc82008-05-07 11:43:44 -04001869 }
Chris Masonec44a352008-04-28 15:29:52 -04001870
1871 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
Chris Masona061fc82008-05-07 11:43:44 -04001872 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
Chris Masonec44a352008-04-28 15:29:52 -04001873 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
Chris Masona061fc82008-05-07 11:43:44 -04001874 }
Chris Masonec44a352008-04-28 15:29:52 -04001875
1876 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
1877 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
1878 (flags & BTRFS_BLOCK_GROUP_RAID10) |
1879 (flags & BTRFS_BLOCK_GROUP_DUP)))
1880 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
1881 return flags;
1882}
1883
Chris Mason6324fbf2008-03-24 15:01:59 -04001884static int do_chunk_alloc(struct btrfs_trans_handle *trans,
1885 struct btrfs_root *extent_root, u64 alloc_bytes,
Chris Mason0ef3e662008-05-24 14:04:53 -04001886 u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04001887{
1888 struct btrfs_space_info *space_info;
1889 u64 thresh;
Yan Zhengc146afa2008-11-12 14:34:12 -05001890 int ret = 0;
1891
1892 mutex_lock(&extent_root->fs_info->chunk_mutex);
Chris Mason6324fbf2008-03-24 15:01:59 -04001893
Yan Zheng2b820322008-11-17 21:11:30 -05001894 flags = btrfs_reduce_alloc_profile(extent_root, flags);
Chris Masonec44a352008-04-28 15:29:52 -04001895
Chris Mason6324fbf2008-03-24 15:01:59 -04001896 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04001897 if (!space_info) {
1898 ret = update_space_info(extent_root->fs_info, flags,
1899 0, 0, &space_info);
1900 BUG_ON(ret);
1901 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001902 BUG_ON(!space_info);
1903
Josef Bacik25179202008-10-29 14:49:05 -04001904 spin_lock(&space_info->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04001905 if (space_info->force_alloc) {
1906 force = 1;
1907 space_info->force_alloc = 0;
1908 }
Josef Bacik25179202008-10-29 14:49:05 -04001909 if (space_info->full) {
1910 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04001911 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04001912 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001913
Yan Zhengc146afa2008-11-12 14:34:12 -05001914 thresh = space_info->total_bytes - space_info->bytes_readonly;
1915 thresh = div_factor(thresh, 6);
Chris Mason0ef3e662008-05-24 14:04:53 -04001916 if (!force &&
Zheng Yane8569812008-09-26 10:05:48 -04001917 (space_info->bytes_used + space_info->bytes_pinned +
Josef Bacik25179202008-10-29 14:49:05 -04001918 space_info->bytes_reserved + alloc_bytes) < thresh) {
1919 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04001920 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04001921 }
Josef Bacik25179202008-10-29 14:49:05 -04001922 spin_unlock(&space_info->lock);
1923
Yan Zheng2b820322008-11-17 21:11:30 -05001924 ret = btrfs_alloc_chunk(trans, extent_root, flags);
Chris Masond3977122009-01-05 21:25:51 -05001925 if (ret)
Chris Mason6324fbf2008-03-24 15:01:59 -04001926 space_info->full = 1;
Chris Masona74a4b92008-06-25 16:01:31 -04001927out:
Yan Zhengc146afa2008-11-12 14:34:12 -05001928 mutex_unlock(&extent_root->fs_info->chunk_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001929 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04001930}
1931
Chris Mason9078a3e2007-04-26 16:46:15 -04001932static int update_block_group(struct btrfs_trans_handle *trans,
1933 struct btrfs_root *root,
Chris Masondb945352007-10-15 16:15:53 -04001934 u64 bytenr, u64 num_bytes, int alloc,
Chris Mason0b86a832008-03-24 15:01:56 -04001935 int mark_free)
Chris Mason9078a3e2007-04-26 16:46:15 -04001936{
1937 struct btrfs_block_group_cache *cache;
1938 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04001939 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04001940 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04001941 u64 byte_in_group;
Chris Mason3e1ad542007-05-07 20:03:49 -04001942
Chris Masond3977122009-01-05 21:25:51 -05001943 while (total) {
Chris Masondb945352007-10-15 16:15:53 -04001944 cache = btrfs_lookup_block_group(info, bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05001945 if (!cache)
Chris Mason9078a3e2007-04-26 16:46:15 -04001946 return -1;
Chris Masondb945352007-10-15 16:15:53 -04001947 byte_in_group = bytenr - cache->key.objectid;
1948 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04001949
Josef Bacik25179202008-10-29 14:49:05 -04001950 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04001951 spin_lock(&cache->lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001952 cache->dirty = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04001953 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04001954 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04001955 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04001956 old_val += num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04001957 cache->space_info->bytes_used += num_bytes;
Yan Zhenga512bbf2008-12-08 16:46:26 -05001958 if (cache->ro)
Yan Zhengc146afa2008-11-12 14:34:12 -05001959 cache->space_info->bytes_readonly -= num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04001960 btrfs_set_block_group_used(&cache->item, old_val);
1961 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04001962 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04001963 } else {
Chris Masondb945352007-10-15 16:15:53 -04001964 old_val -= num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04001965 cache->space_info->bytes_used -= num_bytes;
Yan Zhengc146afa2008-11-12 14:34:12 -05001966 if (cache->ro)
1967 cache->space_info->bytes_readonly += num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04001968 btrfs_set_block_group_used(&cache->item, old_val);
1969 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04001970 spin_unlock(&cache->space_info->lock);
Chris Masonf510cfe2007-10-15 16:14:48 -04001971 if (mark_free) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001972 int ret;
Liu Hui1f3c79a2009-01-05 15:57:51 -05001973
1974 ret = btrfs_discard_extent(root, bytenr,
1975 num_bytes);
1976 WARN_ON(ret);
1977
Josef Bacik0f9dd462008-09-23 13:14:11 -04001978 ret = btrfs_add_free_space(cache, bytenr,
1979 num_bytes);
Yan Zhengd2fb3432008-12-11 16:30:39 -05001980 WARN_ON(ret);
Chris Masone37c9e62007-05-09 20:13:14 -04001981 }
Chris Masoncd1bc462007-04-27 10:08:34 -04001982 }
Yan Zhengd2fb3432008-12-11 16:30:39 -05001983 put_block_group(cache);
Chris Masondb945352007-10-15 16:15:53 -04001984 total -= num_bytes;
1985 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04001986 }
1987 return 0;
1988}
Chris Mason6324fbf2008-03-24 15:01:59 -04001989
Chris Masona061fc82008-05-07 11:43:44 -04001990static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
1991{
Josef Bacik0f9dd462008-09-23 13:14:11 -04001992 struct btrfs_block_group_cache *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05001993 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001994
1995 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
1996 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04001997 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001998
Yan Zhengd2fb3432008-12-11 16:30:39 -05001999 bytenr = cache->key.objectid;
2000 put_block_group(cache);
2001
2002 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04002003}
2004
Chris Masone02119d2008-09-05 16:13:11 -04002005int btrfs_update_pinned_extents(struct btrfs_root *root,
Yan324ae4d2007-11-16 14:57:08 -05002006 u64 bytenr, u64 num, int pin)
2007{
2008 u64 len;
2009 struct btrfs_block_group_cache *cache;
2010 struct btrfs_fs_info *fs_info = root->fs_info;
2011
Josef Bacik25179202008-10-29 14:49:05 -04002012 WARN_ON(!mutex_is_locked(&root->fs_info->pinned_mutex));
Yan324ae4d2007-11-16 14:57:08 -05002013 if (pin) {
2014 set_extent_dirty(&fs_info->pinned_extents,
2015 bytenr, bytenr + num - 1, GFP_NOFS);
2016 } else {
2017 clear_extent_dirty(&fs_info->pinned_extents,
2018 bytenr, bytenr + num - 1, GFP_NOFS);
2019 }
2020 while (num > 0) {
2021 cache = btrfs_lookup_block_group(fs_info, bytenr);
Zheng Yane8569812008-09-26 10:05:48 -04002022 BUG_ON(!cache);
2023 len = min(num, cache->key.offset -
2024 (bytenr - cache->key.objectid));
Yan324ae4d2007-11-16 14:57:08 -05002025 if (pin) {
Josef Bacik25179202008-10-29 14:49:05 -04002026 spin_lock(&cache->space_info->lock);
Zheng Yane8569812008-09-26 10:05:48 -04002027 spin_lock(&cache->lock);
2028 cache->pinned += len;
2029 cache->space_info->bytes_pinned += len;
2030 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04002031 spin_unlock(&cache->space_info->lock);
Yan324ae4d2007-11-16 14:57:08 -05002032 fs_info->total_pinned += len;
2033 } else {
Josef Bacik25179202008-10-29 14:49:05 -04002034 spin_lock(&cache->space_info->lock);
Zheng Yane8569812008-09-26 10:05:48 -04002035 spin_lock(&cache->lock);
2036 cache->pinned -= len;
2037 cache->space_info->bytes_pinned -= len;
2038 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04002039 spin_unlock(&cache->space_info->lock);
Yan324ae4d2007-11-16 14:57:08 -05002040 fs_info->total_pinned -= len;
Josef Bacik07103a32008-11-19 15:17:55 -05002041 if (cache->cached)
2042 btrfs_add_free_space(cache, bytenr, len);
Yan324ae4d2007-11-16 14:57:08 -05002043 }
Yan Zhengd2fb3432008-12-11 16:30:39 -05002044 put_block_group(cache);
Yan324ae4d2007-11-16 14:57:08 -05002045 bytenr += len;
2046 num -= len;
2047 }
2048 return 0;
2049}
Chris Mason9078a3e2007-04-26 16:46:15 -04002050
Zheng Yane8569812008-09-26 10:05:48 -04002051static int update_reserved_extents(struct btrfs_root *root,
2052 u64 bytenr, u64 num, int reserve)
2053{
2054 u64 len;
2055 struct btrfs_block_group_cache *cache;
2056 struct btrfs_fs_info *fs_info = root->fs_info;
2057
Zheng Yane8569812008-09-26 10:05:48 -04002058 while (num > 0) {
2059 cache = btrfs_lookup_block_group(fs_info, bytenr);
2060 BUG_ON(!cache);
2061 len = min(num, cache->key.offset -
2062 (bytenr - cache->key.objectid));
Josef Bacik25179202008-10-29 14:49:05 -04002063
2064 spin_lock(&cache->space_info->lock);
2065 spin_lock(&cache->lock);
Zheng Yane8569812008-09-26 10:05:48 -04002066 if (reserve) {
Zheng Yane8569812008-09-26 10:05:48 -04002067 cache->reserved += len;
2068 cache->space_info->bytes_reserved += len;
Zheng Yane8569812008-09-26 10:05:48 -04002069 } else {
Zheng Yane8569812008-09-26 10:05:48 -04002070 cache->reserved -= len;
2071 cache->space_info->bytes_reserved -= len;
Zheng Yane8569812008-09-26 10:05:48 -04002072 }
Josef Bacik25179202008-10-29 14:49:05 -04002073 spin_unlock(&cache->lock);
2074 spin_unlock(&cache->space_info->lock);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002075 put_block_group(cache);
Zheng Yane8569812008-09-26 10:05:48 -04002076 bytenr += len;
2077 num -= len;
2078 }
2079 return 0;
2080}
2081
Chris Masond1310b22008-01-24 16:13:08 -05002082int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
Chris Masonccd467d2007-06-28 15:57:36 -04002083{
Chris Masonccd467d2007-06-28 15:57:36 -04002084 u64 last = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -04002085 u64 start;
2086 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -05002087 struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
Chris Masonccd467d2007-06-28 15:57:36 -04002088 int ret;
Chris Masonccd467d2007-06-28 15:57:36 -04002089
Josef Bacik25179202008-10-29 14:49:05 -04002090 mutex_lock(&root->fs_info->pinned_mutex);
Chris Masond3977122009-01-05 21:25:51 -05002091 while (1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04002092 ret = find_first_extent_bit(pinned_extents, last,
2093 &start, &end, EXTENT_DIRTY);
2094 if (ret)
Chris Masonccd467d2007-06-28 15:57:36 -04002095 break;
Chris Mason1a5bc162007-10-15 16:15:26 -04002096 set_extent_dirty(copy, start, end, GFP_NOFS);
2097 last = end + 1;
Chris Masonccd467d2007-06-28 15:57:36 -04002098 }
Josef Bacik25179202008-10-29 14:49:05 -04002099 mutex_unlock(&root->fs_info->pinned_mutex);
Chris Masonccd467d2007-06-28 15:57:36 -04002100 return 0;
2101}
2102
2103int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
2104 struct btrfs_root *root,
Chris Masond1310b22008-01-24 16:13:08 -05002105 struct extent_io_tree *unpin)
Chris Masona28ec192007-03-06 20:08:01 -05002106{
Chris Mason1a5bc162007-10-15 16:15:26 -04002107 u64 start;
2108 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05002109 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05002110
Josef Bacik25179202008-10-29 14:49:05 -04002111 mutex_lock(&root->fs_info->pinned_mutex);
Chris Masond3977122009-01-05 21:25:51 -05002112 while (1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04002113 ret = find_first_extent_bit(unpin, 0, &start, &end,
2114 EXTENT_DIRTY);
2115 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05002116 break;
Liu Hui1f3c79a2009-01-05 15:57:51 -05002117
2118 ret = btrfs_discard_extent(root, start, end + 1 - start);
2119
Chris Masone02119d2008-09-05 16:13:11 -04002120 btrfs_update_pinned_extents(root, start, end + 1 - start, 0);
Chris Mason1a5bc162007-10-15 16:15:26 -04002121 clear_extent_dirty(unpin, start, end, GFP_NOFS);
Liu Hui1f3c79a2009-01-05 15:57:51 -05002122
Chris Masonc286ac42008-07-22 23:06:41 -04002123 if (need_resched()) {
Josef Bacik25179202008-10-29 14:49:05 -04002124 mutex_unlock(&root->fs_info->pinned_mutex);
Chris Masonc286ac42008-07-22 23:06:41 -04002125 cond_resched();
Josef Bacik25179202008-10-29 14:49:05 -04002126 mutex_lock(&root->fs_info->pinned_mutex);
Chris Masonc286ac42008-07-22 23:06:41 -04002127 }
Chris Masona28ec192007-03-06 20:08:01 -05002128 }
Josef Bacik25179202008-10-29 14:49:05 -04002129 mutex_unlock(&root->fs_info->pinned_mutex);
Liu Hui1f3c79a2009-01-05 15:57:51 -05002130 return ret;
Chris Masona28ec192007-03-06 20:08:01 -05002131}
2132
Chris Mason98ed5172008-01-03 10:01:48 -05002133static int finish_current_insert(struct btrfs_trans_handle *trans,
Chris Mason87ef2bb2008-10-30 11:23:27 -04002134 struct btrfs_root *extent_root, int all)
Chris Mason037e6392007-03-07 11:50:24 -05002135{
Chris Mason7bb86312007-12-11 09:25:06 -05002136 u64 start;
2137 u64 end;
Zheng Yan31840ae2008-09-23 13:14:14 -04002138 u64 priv;
Josef Bacik25179202008-10-29 14:49:05 -04002139 u64 search = 0;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002140 u64 skipped = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05002141 struct btrfs_fs_info *info = extent_root->fs_info;
2142 struct btrfs_path *path;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002143 struct pending_extent_op *extent_op, *tmp;
2144 struct list_head insert_list, update_list;
Chris Mason037e6392007-03-07 11:50:24 -05002145 int ret;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002146 int num_inserts = 0, max_inserts;
Chris Mason037e6392007-03-07 11:50:24 -05002147
Chris Mason7bb86312007-12-11 09:25:06 -05002148 path = btrfs_alloc_path();
Josef Bacikf3465ca2008-11-12 14:19:50 -05002149 INIT_LIST_HEAD(&insert_list);
2150 INIT_LIST_HEAD(&update_list);
Chris Mason037e6392007-03-07 11:50:24 -05002151
Josef Bacikf3465ca2008-11-12 14:19:50 -05002152 max_inserts = extent_root->leafsize /
2153 (2 * sizeof(struct btrfs_key) + 2 * sizeof(struct btrfs_item) +
2154 sizeof(struct btrfs_extent_ref) +
2155 sizeof(struct btrfs_extent_item));
2156again:
2157 mutex_lock(&info->extent_ins_mutex);
2158 while (1) {
Josef Bacik25179202008-10-29 14:49:05 -04002159 ret = find_first_extent_bit(&info->extent_ins, search, &start,
2160 &end, EXTENT_WRITEBACK);
2161 if (ret) {
Josef Bacikf3465ca2008-11-12 14:19:50 -05002162 if (skipped && all && !num_inserts) {
2163 skipped = 0;
Liu Huib4eec2c2008-11-18 11:30:10 -05002164 search = 0;
Josef Bacik25179202008-10-29 14:49:05 -04002165 continue;
2166 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05002167 mutex_unlock(&info->extent_ins_mutex);
Chris Mason26b80032007-08-08 20:17:12 -04002168 break;
Josef Bacik25179202008-10-29 14:49:05 -04002169 }
2170
2171 ret = try_lock_extent(&info->extent_ins, start, end, GFP_NOFS);
2172 if (!ret) {
Josef Bacikf3465ca2008-11-12 14:19:50 -05002173 skipped = 1;
Chris Mason87ef2bb2008-10-30 11:23:27 -04002174 search = end + 1;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002175 if (need_resched()) {
2176 mutex_unlock(&info->extent_ins_mutex);
2177 cond_resched();
2178 mutex_lock(&info->extent_ins_mutex);
2179 }
Josef Bacik25179202008-10-29 14:49:05 -04002180 continue;
2181 }
Chris Mason26b80032007-08-08 20:17:12 -04002182
Zheng Yan31840ae2008-09-23 13:14:14 -04002183 ret = get_state_private(&info->extent_ins, start, &priv);
2184 BUG_ON(ret);
Josef Bacikf3465ca2008-11-12 14:19:50 -05002185 extent_op = (struct pending_extent_op *)(unsigned long) priv;
Josef Bacik25179202008-10-29 14:49:05 -04002186
Zheng Yan31840ae2008-09-23 13:14:14 -04002187 if (extent_op->type == PENDING_EXTENT_INSERT) {
Josef Bacikf3465ca2008-11-12 14:19:50 -05002188 num_inserts++;
2189 list_add_tail(&extent_op->list, &insert_list);
Chris Mason87ef2bb2008-10-30 11:23:27 -04002190 search = end + 1;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002191 if (num_inserts == max_inserts) {
2192 mutex_unlock(&info->extent_ins_mutex);
2193 break;
2194 }
2195 } else if (extent_op->type == PENDING_BACKREF_UPDATE) {
2196 list_add_tail(&extent_op->list, &update_list);
2197 search = end + 1;
2198 } else {
2199 BUG();
2200 }
Chris Mason037e6392007-03-07 11:50:24 -05002201 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05002202
2203 /*
Liu Huib4eec2c2008-11-18 11:30:10 -05002204 * process the update list, clear the writeback bit for it, and if
Josef Bacikf3465ca2008-11-12 14:19:50 -05002205 * somebody marked this thing for deletion then just unlock it and be
2206 * done, the free_extents will handle it
2207 */
2208 mutex_lock(&info->extent_ins_mutex);
2209 list_for_each_entry_safe(extent_op, tmp, &update_list, list) {
2210 clear_extent_bits(&info->extent_ins, extent_op->bytenr,
2211 extent_op->bytenr + extent_op->num_bytes - 1,
2212 EXTENT_WRITEBACK, GFP_NOFS);
2213 if (extent_op->del) {
2214 list_del_init(&extent_op->list);
2215 unlock_extent(&info->extent_ins, extent_op->bytenr,
2216 extent_op->bytenr + extent_op->num_bytes
2217 - 1, GFP_NOFS);
2218 kfree(extent_op);
2219 }
2220 }
2221 mutex_unlock(&info->extent_ins_mutex);
2222
2223 /*
2224 * still have things left on the update list, go ahead an update
2225 * everything
2226 */
2227 if (!list_empty(&update_list)) {
2228 ret = update_backrefs(trans, extent_root, path, &update_list);
2229 BUG_ON(ret);
2230 }
2231
2232 /*
2233 * if no inserts need to be done, but we skipped some extents and we
2234 * need to make sure everything is cleaned then reset everything and
2235 * go back to the beginning
2236 */
2237 if (!num_inserts && all && skipped) {
2238 search = 0;
2239 skipped = 0;
2240 INIT_LIST_HEAD(&update_list);
2241 INIT_LIST_HEAD(&insert_list);
2242 goto again;
2243 } else if (!num_inserts) {
2244 goto out;
2245 }
2246
2247 /*
2248 * process the insert extents list. Again if we are deleting this
2249 * extent, then just unlock it, pin down the bytes if need be, and be
2250 * done with it. Saves us from having to actually insert the extent
2251 * into the tree and then subsequently come along and delete it
2252 */
2253 mutex_lock(&info->extent_ins_mutex);
2254 list_for_each_entry_safe(extent_op, tmp, &insert_list, list) {
2255 clear_extent_bits(&info->extent_ins, extent_op->bytenr,
2256 extent_op->bytenr + extent_op->num_bytes - 1,
2257 EXTENT_WRITEBACK, GFP_NOFS);
2258 if (extent_op->del) {
Yan Zheng34bf63c2008-12-19 10:58:46 -05002259 u64 used;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002260 list_del_init(&extent_op->list);
2261 unlock_extent(&info->extent_ins, extent_op->bytenr,
2262 extent_op->bytenr + extent_op->num_bytes
2263 - 1, GFP_NOFS);
2264
2265 mutex_lock(&extent_root->fs_info->pinned_mutex);
2266 ret = pin_down_bytes(trans, extent_root,
2267 extent_op->bytenr,
2268 extent_op->num_bytes, 0);
2269 mutex_unlock(&extent_root->fs_info->pinned_mutex);
2270
Yan Zheng34bf63c2008-12-19 10:58:46 -05002271 spin_lock(&info->delalloc_lock);
2272 used = btrfs_super_bytes_used(&info->super_copy);
2273 btrfs_set_super_bytes_used(&info->super_copy,
2274 used - extent_op->num_bytes);
2275 used = btrfs_root_used(&extent_root->root_item);
2276 btrfs_set_root_used(&extent_root->root_item,
2277 used - extent_op->num_bytes);
2278 spin_unlock(&info->delalloc_lock);
2279
Josef Bacikf3465ca2008-11-12 14:19:50 -05002280 ret = update_block_group(trans, extent_root,
2281 extent_op->bytenr,
2282 extent_op->num_bytes,
2283 0, ret > 0);
2284 BUG_ON(ret);
2285 kfree(extent_op);
2286 num_inserts--;
2287 }
2288 }
2289 mutex_unlock(&info->extent_ins_mutex);
2290
2291 ret = insert_extents(trans, extent_root, path, &insert_list,
2292 num_inserts);
2293 BUG_ON(ret);
2294
2295 /*
2296 * if we broke out of the loop in order to insert stuff because we hit
2297 * the maximum number of inserts at a time we can handle, then loop
2298 * back and pick up where we left off
2299 */
2300 if (num_inserts == max_inserts) {
2301 INIT_LIST_HEAD(&insert_list);
2302 INIT_LIST_HEAD(&update_list);
2303 num_inserts = 0;
2304 goto again;
2305 }
2306
2307 /*
2308 * again, if we need to make absolutely sure there are no more pending
2309 * extent operations left and we know that we skipped some, go back to
2310 * the beginning and do it all again
2311 */
2312 if (all && skipped) {
2313 INIT_LIST_HEAD(&insert_list);
2314 INIT_LIST_HEAD(&update_list);
2315 search = 0;
2316 skipped = 0;
2317 num_inserts = 0;
2318 goto again;
2319 }
2320out:
Chris Mason7bb86312007-12-11 09:25:06 -05002321 btrfs_free_path(path);
Chris Mason037e6392007-03-07 11:50:24 -05002322 return 0;
2323}
2324
Zheng Yan31840ae2008-09-23 13:14:14 -04002325static int pin_down_bytes(struct btrfs_trans_handle *trans,
2326 struct btrfs_root *root,
2327 u64 bytenr, u64 num_bytes, int is_data)
Chris Masone20d96d2007-03-22 12:13:20 -04002328{
Chris Mason1a5bc162007-10-15 16:15:26 -04002329 int err = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04002330 struct extent_buffer *buf;
Chris Mason78fae272007-03-25 11:35:08 -04002331
Zheng Yan31840ae2008-09-23 13:14:14 -04002332 if (is_data)
2333 goto pinit;
Chris Mason4bef0842008-09-08 11:18:08 -04002334
Zheng Yan31840ae2008-09-23 13:14:14 -04002335 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
2336 if (!buf)
2337 goto pinit;
Chris Mason4bef0842008-09-08 11:18:08 -04002338
Zheng Yan31840ae2008-09-23 13:14:14 -04002339 /* we can reuse a block if it hasn't been written
2340 * and it is from this transaction. We can't
2341 * reuse anything from the tree log root because
2342 * it has tiny sub-transactions.
2343 */
2344 if (btrfs_buffer_uptodate(buf, 0) &&
2345 btrfs_try_tree_lock(buf)) {
2346 u64 header_owner = btrfs_header_owner(buf);
2347 u64 header_transid = btrfs_header_generation(buf);
2348 if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
Zheng Yan1a40e232008-09-26 10:09:34 -04002349 header_owner != BTRFS_TREE_RELOC_OBJECTID &&
Zheng Yan31840ae2008-09-23 13:14:14 -04002350 header_transid == trans->transid &&
2351 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
2352 clean_tree_block(NULL, root, buf);
2353 btrfs_tree_unlock(buf);
Chris Mason5f39d392007-10-15 16:14:19 -04002354 free_extent_buffer(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002355 return 1;
Chris Mason8ef97622007-03-26 10:15:30 -04002356 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002357 btrfs_tree_unlock(buf);
Chris Masonf4b9aa82007-03-27 11:05:53 -04002358 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002359 free_extent_buffer(buf);
2360pinit:
2361 btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
2362
Chris Masonbe744172007-05-06 10:15:01 -04002363 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -04002364 return 0;
2365}
2366
Chris Masona28ec192007-03-06 20:08:01 -05002367/*
2368 * remove an extent from the root, returns 0 on success
2369 */
Zheng Yan31840ae2008-09-23 13:14:14 -04002370static int __free_extent(struct btrfs_trans_handle *trans,
2371 struct btrfs_root *root,
2372 u64 bytenr, u64 num_bytes, u64 parent,
Chris Mason7bb86312007-12-11 09:25:06 -05002373 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002374 u64 owner_objectid, int pin, int mark_free)
Chris Masona28ec192007-03-06 20:08:01 -05002375{
Chris Mason5caf2a02007-04-02 11:20:42 -04002376 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -04002377 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -04002378 struct btrfs_fs_info *info = root->fs_info;
2379 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04002380 struct extent_buffer *leaf;
Chris Masona28ec192007-03-06 20:08:01 -05002381 int ret;
Chris Mason952fcca2008-02-18 16:33:44 -05002382 int extent_slot = 0;
2383 int found_extent = 0;
2384 int num_to_del = 1;
Chris Mason234b63a2007-03-13 10:46:10 -04002385 struct btrfs_extent_item *ei;
Chris Masoncf27e1e2007-03-13 09:49:06 -04002386 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -05002387
Chris Masondb945352007-10-15 16:15:53 -04002388 key.objectid = bytenr;
Chris Mason62e27492007-03-15 12:56:47 -04002389 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masondb945352007-10-15 16:15:53 -04002390 key.offset = num_bytes;
Chris Mason5caf2a02007-04-02 11:20:42 -04002391 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002392 if (!path)
2393 return -ENOMEM;
2394
Chris Mason3c12ac72008-04-21 12:01:38 -04002395 path->reada = 1;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002396 ret = lookup_extent_backref(trans, extent_root, path,
2397 bytenr, parent, root_objectid,
2398 ref_generation, owner_objectid, 1);
Chris Mason7bb86312007-12-11 09:25:06 -05002399 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05002400 struct btrfs_key found_key;
2401 extent_slot = path->slots[0];
Chris Masond3977122009-01-05 21:25:51 -05002402 while (extent_slot > 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05002403 extent_slot--;
2404 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2405 extent_slot);
2406 if (found_key.objectid != bytenr)
2407 break;
2408 if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
2409 found_key.offset == num_bytes) {
2410 found_extent = 1;
2411 break;
2412 }
2413 if (path->slots[0] - extent_slot > 5)
2414 break;
2415 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002416 if (!found_extent) {
2417 ret = remove_extent_backref(trans, extent_root, path);
2418 BUG_ON(ret);
2419 btrfs_release_path(extent_root, path);
2420 ret = btrfs_search_slot(trans, extent_root,
2421 &key, path, -1, 1);
Josef Bacikf3465ca2008-11-12 14:19:50 -05002422 if (ret) {
2423 printk(KERN_ERR "umm, got %d back from search"
Chris Masond3977122009-01-05 21:25:51 -05002424 ", was looking for %llu\n", ret,
2425 (unsigned long long)bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05002426 btrfs_print_leaf(extent_root, path->nodes[0]);
2427 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002428 BUG_ON(ret);
2429 extent_slot = path->slots[0];
2430 }
Chris Mason7bb86312007-12-11 09:25:06 -05002431 } else {
2432 btrfs_print_leaf(extent_root, path->nodes[0]);
2433 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -05002434 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
2435 "root %llu gen %llu owner %llu\n",
2436 (unsigned long long)bytenr,
2437 (unsigned long long)root_objectid,
2438 (unsigned long long)ref_generation,
2439 (unsigned long long)owner_objectid);
Chris Mason7bb86312007-12-11 09:25:06 -05002440 }
Chris Mason5f39d392007-10-15 16:14:19 -04002441
2442 leaf = path->nodes[0];
Chris Mason952fcca2008-02-18 16:33:44 -05002443 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04002444 struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002445 refs = btrfs_extent_refs(leaf, ei);
2446 BUG_ON(refs == 0);
2447 refs -= 1;
2448 btrfs_set_extent_refs(leaf, ei, refs);
Chris Mason952fcca2008-02-18 16:33:44 -05002449
Chris Mason5f39d392007-10-15 16:14:19 -04002450 btrfs_mark_buffer_dirty(leaf);
2451
Chris Mason952fcca2008-02-18 16:33:44 -05002452 if (refs == 0 && found_extent && path->slots[0] == extent_slot + 1) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002453 struct btrfs_extent_ref *ref;
2454 ref = btrfs_item_ptr(leaf, path->slots[0],
2455 struct btrfs_extent_ref);
2456 BUG_ON(btrfs_ref_num_refs(leaf, ref) != 1);
Chris Mason952fcca2008-02-18 16:33:44 -05002457 /* if the back ref and the extent are next to each other
2458 * they get deleted below in one shot
2459 */
2460 path->slots[0] = extent_slot;
2461 num_to_del = 2;
2462 } else if (found_extent) {
2463 /* otherwise delete the extent back ref */
Zheng Yan31840ae2008-09-23 13:14:14 -04002464 ret = remove_extent_backref(trans, extent_root, path);
Chris Mason952fcca2008-02-18 16:33:44 -05002465 BUG_ON(ret);
2466 /* if refs are 0, we need to setup the path for deletion */
2467 if (refs == 0) {
2468 btrfs_release_path(extent_root, path);
2469 ret = btrfs_search_slot(trans, extent_root, &key, path,
2470 -1, 1);
Chris Mason952fcca2008-02-18 16:33:44 -05002471 BUG_ON(ret);
2472 }
2473 }
2474
Chris Masoncf27e1e2007-03-13 09:49:06 -04002475 if (refs == 0) {
Chris Masondb945352007-10-15 16:15:53 -04002476 u64 super_used;
2477 u64 root_used;
Chris Mason78fae272007-03-25 11:35:08 -04002478
2479 if (pin) {
Josef Bacik25179202008-10-29 14:49:05 -04002480 mutex_lock(&root->fs_info->pinned_mutex);
Zheng Yan31840ae2008-09-23 13:14:14 -04002481 ret = pin_down_bytes(trans, root, bytenr, num_bytes,
2482 owner_objectid >= BTRFS_FIRST_FREE_OBJECTID);
Josef Bacik25179202008-10-29 14:49:05 -04002483 mutex_unlock(&root->fs_info->pinned_mutex);
Yanc5492282007-11-06 10:25:25 -05002484 if (ret > 0)
2485 mark_free = 1;
2486 BUG_ON(ret < 0);
Chris Mason78fae272007-03-25 11:35:08 -04002487 }
Josef Bacik58176a92007-08-29 15:47:34 -04002488 /* block accounting for super block */
Chris Mason75eff682008-12-15 15:54:40 -05002489 spin_lock(&info->delalloc_lock);
Chris Masondb945352007-10-15 16:15:53 -04002490 super_used = btrfs_super_bytes_used(&info->super_copy);
2491 btrfs_set_super_bytes_used(&info->super_copy,
2492 super_used - num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -04002493
2494 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04002495 root_used = btrfs_root_used(&root->root_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002496 btrfs_set_root_used(&root->root_item,
Chris Masondb945352007-10-15 16:15:53 -04002497 root_used - num_bytes);
Yan Zheng34bf63c2008-12-19 10:58:46 -05002498 spin_unlock(&info->delalloc_lock);
Chris Mason952fcca2008-02-18 16:33:44 -05002499 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
2500 num_to_del);
Zheng Yan31840ae2008-09-23 13:14:14 -04002501 BUG_ON(ret);
Josef Bacik25179202008-10-29 14:49:05 -04002502 btrfs_release_path(extent_root, path);
David Woodhouse21af8042008-08-12 14:13:26 +01002503
Chris Mason459931e2008-12-10 09:10:46 -05002504 if (owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
2505 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
2506 BUG_ON(ret);
2507 }
2508
Chris Masondcbdd4d2008-12-16 13:51:01 -05002509 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
2510 mark_free);
2511 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -05002512 }
Chris Mason5caf2a02007-04-02 11:20:42 -04002513 btrfs_free_path(path);
Chris Mason87ef2bb2008-10-30 11:23:27 -04002514 finish_current_insert(trans, extent_root, 0);
Chris Masona28ec192007-03-06 20:08:01 -05002515 return ret;
2516}
2517
2518/*
Chris Masonfec577f2007-02-26 10:40:21 -05002519 * find all the blocks marked as pending in the radix tree and remove
2520 * them from the extent map
2521 */
Chris Masond3977122009-01-05 21:25:51 -05002522static int del_pending_extents(struct btrfs_trans_handle *trans,
2523 struct btrfs_root *extent_root, int all)
Chris Masonfec577f2007-02-26 10:40:21 -05002524{
2525 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -04002526 int err = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -04002527 u64 start;
2528 u64 end;
Zheng Yan31840ae2008-09-23 13:14:14 -04002529 u64 priv;
Josef Bacik25179202008-10-29 14:49:05 -04002530 u64 search = 0;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002531 int nr = 0, skipped = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002532 struct extent_io_tree *pending_del;
Zheng Yan31840ae2008-09-23 13:14:14 -04002533 struct extent_io_tree *extent_ins;
2534 struct pending_extent_op *extent_op;
Josef Bacik25179202008-10-29 14:49:05 -04002535 struct btrfs_fs_info *info = extent_root->fs_info;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002536 struct list_head delete_list;
Chris Mason8ef97622007-03-26 10:15:30 -04002537
Josef Bacikf3465ca2008-11-12 14:19:50 -05002538 INIT_LIST_HEAD(&delete_list);
Zheng Yan31840ae2008-09-23 13:14:14 -04002539 extent_ins = &extent_root->fs_info->extent_ins;
Chris Mason1a5bc162007-10-15 16:15:26 -04002540 pending_del = &extent_root->fs_info->pending_del;
Chris Masonfec577f2007-02-26 10:40:21 -05002541
Josef Bacikf3465ca2008-11-12 14:19:50 -05002542again:
2543 mutex_lock(&info->extent_ins_mutex);
Chris Masond3977122009-01-05 21:25:51 -05002544 while (1) {
Josef Bacik25179202008-10-29 14:49:05 -04002545 ret = find_first_extent_bit(pending_del, search, &start, &end,
2546 EXTENT_WRITEBACK);
2547 if (ret) {
Josef Bacikf3465ca2008-11-12 14:19:50 -05002548 if (all && skipped && !nr) {
Josef Bacik25179202008-10-29 14:49:05 -04002549 search = 0;
2550 continue;
2551 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05002552 mutex_unlock(&info->extent_ins_mutex);
Chris Masonfec577f2007-02-26 10:40:21 -05002553 break;
Josef Bacik25179202008-10-29 14:49:05 -04002554 }
2555
2556 ret = try_lock_extent(extent_ins, start, end, GFP_NOFS);
2557 if (!ret) {
2558 search = end+1;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002559 skipped = 1;
2560
2561 if (need_resched()) {
2562 mutex_unlock(&info->extent_ins_mutex);
2563 cond_resched();
2564 mutex_lock(&info->extent_ins_mutex);
2565 }
2566
Josef Bacik25179202008-10-29 14:49:05 -04002567 continue;
2568 }
2569 BUG_ON(ret < 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002570
2571 ret = get_state_private(pending_del, start, &priv);
2572 BUG_ON(ret);
2573 extent_op = (struct pending_extent_op *)(unsigned long)priv;
2574
Josef Bacik25179202008-10-29 14:49:05 -04002575 clear_extent_bits(pending_del, start, end, EXTENT_WRITEBACK,
Chris Mason1a5bc162007-10-15 16:15:26 -04002576 GFP_NOFS);
Zheng Yan31840ae2008-09-23 13:14:14 -04002577 if (!test_range_bit(extent_ins, start, end,
Josef Bacik25179202008-10-29 14:49:05 -04002578 EXTENT_WRITEBACK, 0)) {
Josef Bacikf3465ca2008-11-12 14:19:50 -05002579 list_add_tail(&extent_op->list, &delete_list);
2580 nr++;
Chris Masonc286ac42008-07-22 23:06:41 -04002581 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04002582 kfree(extent_op);
Josef Bacik25179202008-10-29 14:49:05 -04002583
2584 ret = get_state_private(&info->extent_ins, start,
2585 &priv);
Zheng Yan31840ae2008-09-23 13:14:14 -04002586 BUG_ON(ret);
2587 extent_op = (struct pending_extent_op *)
Josef Bacik25179202008-10-29 14:49:05 -04002588 (unsigned long)priv;
Zheng Yan31840ae2008-09-23 13:14:14 -04002589
Josef Bacik25179202008-10-29 14:49:05 -04002590 clear_extent_bits(&info->extent_ins, start, end,
2591 EXTENT_WRITEBACK, GFP_NOFS);
2592
Josef Bacikf3465ca2008-11-12 14:19:50 -05002593 if (extent_op->type == PENDING_BACKREF_UPDATE) {
2594 list_add_tail(&extent_op->list, &delete_list);
2595 search = end + 1;
2596 nr++;
2597 continue;
2598 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002599
Josef Bacik25179202008-10-29 14:49:05 -04002600 mutex_lock(&extent_root->fs_info->pinned_mutex);
2601 ret = pin_down_bytes(trans, extent_root, start,
2602 end + 1 - start, 0);
2603 mutex_unlock(&extent_root->fs_info->pinned_mutex);
2604
Zheng Yan31840ae2008-09-23 13:14:14 -04002605 ret = update_block_group(trans, extent_root, start,
Josef Bacik25179202008-10-29 14:49:05 -04002606 end + 1 - start, 0, ret > 0);
2607
Josef Bacikf3465ca2008-11-12 14:19:50 -05002608 unlock_extent(extent_ins, start, end, GFP_NOFS);
Zheng Yan31840ae2008-09-23 13:14:14 -04002609 BUG_ON(ret);
2610 kfree(extent_op);
Chris Masonc286ac42008-07-22 23:06:41 -04002611 }
Chris Mason1a5bc162007-10-15 16:15:26 -04002612 if (ret)
2613 err = ret;
Chris Masonc286ac42008-07-22 23:06:41 -04002614
Josef Bacikf3465ca2008-11-12 14:19:50 -05002615 search = end + 1;
2616
2617 if (need_resched()) {
2618 mutex_unlock(&info->extent_ins_mutex);
2619 cond_resched();
2620 mutex_lock(&info->extent_ins_mutex);
2621 }
Chris Masonfec577f2007-02-26 10:40:21 -05002622 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05002623
2624 if (nr) {
2625 ret = free_extents(trans, extent_root, &delete_list);
2626 BUG_ON(ret);
2627 }
2628
2629 if (all && skipped) {
2630 INIT_LIST_HEAD(&delete_list);
2631 search = 0;
2632 nr = 0;
2633 goto again;
2634 }
2635
Chris Masone20d96d2007-03-22 12:13:20 -04002636 return err;
Chris Masonfec577f2007-02-26 10:40:21 -05002637}
2638
2639/*
2640 * remove an extent from the root, returns 0 on success
2641 */
Chris Mason925baed2008-06-25 16:01:30 -04002642static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04002643 struct btrfs_root *root,
2644 u64 bytenr, u64 num_bytes, u64 parent,
2645 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002646 u64 owner_objectid, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -05002647{
Chris Mason9f5fae22007-03-20 14:38:32 -04002648 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -05002649 int pending_ret;
2650 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05002651
Chris Masondb945352007-10-15 16:15:53 -04002652 WARN_ON(num_bytes < root->sectorsize);
Chris Masona28ec192007-03-06 20:08:01 -05002653 if (root == extent_root) {
Josef Bacikf3465ca2008-11-12 14:19:50 -05002654 struct pending_extent_op *extent_op = NULL;
2655
2656 mutex_lock(&root->fs_info->extent_ins_mutex);
2657 if (test_range_bit(&root->fs_info->extent_ins, bytenr,
2658 bytenr + num_bytes - 1, EXTENT_WRITEBACK, 0)) {
2659 u64 priv;
2660 ret = get_state_private(&root->fs_info->extent_ins,
2661 bytenr, &priv);
2662 BUG_ON(ret);
2663 extent_op = (struct pending_extent_op *)
2664 (unsigned long)priv;
2665
2666 extent_op->del = 1;
2667 if (extent_op->type == PENDING_EXTENT_INSERT) {
2668 mutex_unlock(&root->fs_info->extent_ins_mutex);
2669 return 0;
2670 }
2671 }
2672
2673 if (extent_op) {
2674 ref_generation = extent_op->orig_generation;
2675 parent = extent_op->orig_parent;
2676 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002677
2678 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
2679 BUG_ON(!extent_op);
2680
2681 extent_op->type = PENDING_EXTENT_DELETE;
2682 extent_op->bytenr = bytenr;
2683 extent_op->num_bytes = num_bytes;
2684 extent_op->parent = parent;
2685 extent_op->orig_parent = parent;
2686 extent_op->generation = ref_generation;
2687 extent_op->orig_generation = ref_generation;
2688 extent_op->level = (int)owner_objectid;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002689 INIT_LIST_HEAD(&extent_op->list);
2690 extent_op->del = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04002691
2692 set_extent_bits(&root->fs_info->pending_del,
2693 bytenr, bytenr + num_bytes - 1,
Josef Bacik25179202008-10-29 14:49:05 -04002694 EXTENT_WRITEBACK, GFP_NOFS);
Zheng Yan31840ae2008-09-23 13:14:14 -04002695 set_state_private(&root->fs_info->pending_del,
2696 bytenr, (unsigned long)extent_op);
Josef Bacik25179202008-10-29 14:49:05 -04002697 mutex_unlock(&root->fs_info->extent_ins_mutex);
Chris Masona28ec192007-03-06 20:08:01 -05002698 return 0;
2699 }
Chris Mason4bef0842008-09-08 11:18:08 -04002700 /* if metadata always pin */
Chris Masond00aff02008-09-11 15:54:42 -04002701 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
2702 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04002703 struct btrfs_block_group_cache *cache;
2704
Chris Masond00aff02008-09-11 15:54:42 -04002705 /* btrfs_free_reserved_extent */
Josef Bacik0f9dd462008-09-23 13:14:11 -04002706 cache = btrfs_lookup_block_group(root->fs_info, bytenr);
2707 BUG_ON(!cache);
2708 btrfs_add_free_space(cache, bytenr, num_bytes);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002709 put_block_group(cache);
Zheng Yane8569812008-09-26 10:05:48 -04002710 update_reserved_extents(root, bytenr, num_bytes, 0);
Chris Masond00aff02008-09-11 15:54:42 -04002711 return 0;
2712 }
Chris Mason4bef0842008-09-08 11:18:08 -04002713 pin = 1;
Chris Masond00aff02008-09-11 15:54:42 -04002714 }
Chris Mason4bef0842008-09-08 11:18:08 -04002715
2716 /* if data pin when any transaction has committed this */
2717 if (ref_generation != trans->transid)
2718 pin = 1;
2719
Zheng Yan31840ae2008-09-23 13:14:14 -04002720 ret = __free_extent(trans, root, bytenr, num_bytes, parent,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002721 root_objectid, ref_generation,
2722 owner_objectid, pin, pin == 0);
Chris Masonee6e6502008-07-17 12:54:40 -04002723
Chris Mason87ef2bb2008-10-30 11:23:27 -04002724 finish_current_insert(trans, root->fs_info->extent_root, 0);
2725 pending_ret = del_pending_extents(trans, root->fs_info->extent_root, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05002726 return ret ? ret : pending_ret;
2727}
2728
Chris Mason925baed2008-06-25 16:01:30 -04002729int btrfs_free_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04002730 struct btrfs_root *root,
2731 u64 bytenr, u64 num_bytes, u64 parent,
2732 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002733 u64 owner_objectid, int pin)
Chris Mason925baed2008-06-25 16:01:30 -04002734{
2735 int ret;
2736
Zheng Yan31840ae2008-09-23 13:14:14 -04002737 ret = __btrfs_free_extent(trans, root, bytenr, num_bytes, parent,
Chris Mason925baed2008-06-25 16:01:30 -04002738 root_objectid, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002739 owner_objectid, pin);
Chris Mason925baed2008-06-25 16:01:30 -04002740 return ret;
2741}
2742
Chris Mason87ee04e2007-11-30 11:30:34 -05002743static u64 stripe_align(struct btrfs_root *root, u64 val)
2744{
2745 u64 mask = ((u64)root->stripesize - 1);
2746 u64 ret = (val + mask) & ~mask;
2747 return ret;
2748}
2749
Chris Masonfec577f2007-02-26 10:40:21 -05002750/*
2751 * walks the btree of allocated extents and find a hole of a given size.
2752 * The key ins is changed to record the hole:
2753 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04002754 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05002755 * ins->offset == number of blocks
2756 * Any available blocks before search_start are skipped.
2757 */
Chris Masond3977122009-01-05 21:25:51 -05002758static noinline int find_free_extent(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05002759 struct btrfs_root *orig_root,
2760 u64 num_bytes, u64 empty_size,
2761 u64 search_start, u64 search_end,
2762 u64 hint_byte, struct btrfs_key *ins,
2763 u64 exclude_start, u64 exclude_nr,
2764 int data)
Chris Masonfec577f2007-02-26 10:40:21 -05002765{
Josef Bacik80eb2342008-10-29 14:49:05 -04002766 int ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05002767 struct btrfs_root *root = orig_root->fs_info->extent_root;
Chris Masondb945352007-10-15 16:15:53 -04002768 u64 total_needed = num_bytes;
Chris Mason239b14b2008-03-24 15:02:07 -04002769 u64 *last_ptr = NULL;
Chris Mason43662112008-11-07 09:06:11 -05002770 u64 last_wanted = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04002771 struct btrfs_block_group_cache *block_group = NULL;
Chris Mason0ef3e662008-05-24 14:04:53 -04002772 int chunk_alloc_done = 0;
Chris Mason239b14b2008-03-24 15:02:07 -04002773 int empty_cluster = 2 * 1024 * 1024;
Chris Mason0ef3e662008-05-24 14:04:53 -04002774 int allowed_chunk_alloc = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04002775 struct list_head *head = NULL, *cur = NULL;
2776 int loop = 0;
Chris Masonf5a31e12008-11-10 11:47:09 -05002777 int extra_loop = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04002778 struct btrfs_space_info *space_info;
Chris Masonfec577f2007-02-26 10:40:21 -05002779
Chris Masondb945352007-10-15 16:15:53 -04002780 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04002781 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
Josef Bacik80eb2342008-10-29 14:49:05 -04002782 ins->objectid = 0;
2783 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04002784
Chris Mason0ef3e662008-05-24 14:04:53 -04002785 if (orig_root->ref_cows || empty_size)
2786 allowed_chunk_alloc = 1;
2787
Chris Mason239b14b2008-03-24 15:02:07 -04002788 if (data & BTRFS_BLOCK_GROUP_METADATA) {
2789 last_ptr = &root->fs_info->last_alloc;
Chris Mason43662112008-11-07 09:06:11 -05002790 empty_cluster = 64 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04002791 }
2792
Josef Bacik0f9dd462008-09-23 13:14:11 -04002793 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD))
Chris Mason239b14b2008-03-24 15:02:07 -04002794 last_ptr = &root->fs_info->last_data_alloc;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002795
Chris Mason239b14b2008-03-24 15:02:07 -04002796 if (last_ptr) {
Chris Mason43662112008-11-07 09:06:11 -05002797 if (*last_ptr) {
Chris Mason239b14b2008-03-24 15:02:07 -04002798 hint_byte = *last_ptr;
Chris Mason43662112008-11-07 09:06:11 -05002799 last_wanted = *last_ptr;
2800 } else
Chris Mason239b14b2008-03-24 15:02:07 -04002801 empty_size += empty_cluster;
Chris Mason43662112008-11-07 09:06:11 -05002802 } else {
2803 empty_cluster = 0;
Chris Mason239b14b2008-03-24 15:02:07 -04002804 }
Chris Masona061fc82008-05-07 11:43:44 -04002805 search_start = max(search_start, first_logical_byte(root, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04002806 search_start = max(search_start, hint_byte);
Chris Mason0b86a832008-03-24 15:01:56 -04002807
Chris Mason42e70e72008-11-07 18:17:11 -05002808 if (last_wanted && search_start != last_wanted) {
Chris Mason43662112008-11-07 09:06:11 -05002809 last_wanted = 0;
Chris Mason42e70e72008-11-07 18:17:11 -05002810 empty_size += empty_cluster;
2811 }
Chris Mason43662112008-11-07 09:06:11 -05002812
Chris Mason42e70e72008-11-07 18:17:11 -05002813 total_needed += empty_size;
Josef Bacik80eb2342008-10-29 14:49:05 -04002814 block_group = btrfs_lookup_block_group(root->fs_info, search_start);
Yan Zhengd899e052008-10-30 14:25:28 -04002815 if (!block_group)
2816 block_group = btrfs_lookup_first_block_group(root->fs_info,
2817 search_start);
Josef Bacik80eb2342008-10-29 14:49:05 -04002818 space_info = __find_space_info(root->fs_info, data);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002819
Josef Bacik80eb2342008-10-29 14:49:05 -04002820 down_read(&space_info->groups_sem);
2821 while (1) {
2822 struct btrfs_free_space *free_space;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002823 /*
Josef Bacik80eb2342008-10-29 14:49:05 -04002824 * the only way this happens if our hint points to a block
2825 * group thats not of the proper type, while looping this
2826 * should never happen
Josef Bacik0f9dd462008-09-23 13:14:11 -04002827 */
Chris Mason8a1413a2008-11-10 16:13:54 -05002828 if (empty_size)
2829 extra_loop = 1;
2830
Chris Mason42e70e72008-11-07 18:17:11 -05002831 if (!block_group)
2832 goto new_group_no_lock;
2833
Josef Bacikea6a4782008-11-20 12:16:16 -05002834 if (unlikely(!block_group->cached)) {
2835 mutex_lock(&block_group->cache_mutex);
2836 ret = cache_block_group(root, block_group);
2837 mutex_unlock(&block_group->cache_mutex);
2838 if (ret)
2839 break;
2840 }
2841
Josef Bacik25179202008-10-29 14:49:05 -04002842 mutex_lock(&block_group->alloc_mutex);
Josef Bacik80eb2342008-10-29 14:49:05 -04002843 if (unlikely(!block_group_bits(block_group, data)))
Josef Bacik0f9dd462008-09-23 13:14:11 -04002844 goto new_group;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002845
Josef Bacikea6a4782008-11-20 12:16:16 -05002846 if (unlikely(block_group->ro))
Josef Bacik80eb2342008-10-29 14:49:05 -04002847 goto new_group;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002848
Josef Bacik80eb2342008-10-29 14:49:05 -04002849 free_space = btrfs_find_free_space(block_group, search_start,
2850 total_needed);
2851 if (free_space) {
2852 u64 start = block_group->key.objectid;
2853 u64 end = block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -04002854 block_group->key.offset;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002855
Josef Bacik80eb2342008-10-29 14:49:05 -04002856 search_start = stripe_align(root, free_space->offset);
Chris Mason0b86a832008-03-24 15:01:56 -04002857
Josef Bacik80eb2342008-10-29 14:49:05 -04002858 /* move on to the next group */
2859 if (search_start + num_bytes >= search_end)
2860 goto new_group;
Chris Masone37c9e62007-05-09 20:13:14 -04002861
Josef Bacik80eb2342008-10-29 14:49:05 -04002862 /* move on to the next group */
2863 if (search_start + num_bytes > end)
2864 goto new_group;
2865
Chris Mason43662112008-11-07 09:06:11 -05002866 if (last_wanted && search_start != last_wanted) {
Chris Mason3b7885b2008-11-06 21:48:27 -05002867 total_needed += empty_cluster;
Chris Mason8a1413a2008-11-10 16:13:54 -05002868 empty_size += empty_cluster;
Chris Mason43662112008-11-07 09:06:11 -05002869 last_wanted = 0;
Chris Mason3b7885b2008-11-06 21:48:27 -05002870 /*
2871 * if search_start is still in this block group
2872 * then we just re-search this block group
2873 */
2874 if (search_start >= start &&
2875 search_start < end) {
2876 mutex_unlock(&block_group->alloc_mutex);
2877 continue;
2878 }
2879
2880 /* else we go to the next block group */
2881 goto new_group;
2882 }
2883
Josef Bacik80eb2342008-10-29 14:49:05 -04002884 if (exclude_nr > 0 &&
2885 (search_start + num_bytes > exclude_start &&
2886 search_start < exclude_start + exclude_nr)) {
2887 search_start = exclude_start + exclude_nr;
2888 /*
2889 * if search_start is still in this block group
2890 * then we just re-search this block group
2891 */
2892 if (search_start >= start &&
Josef Bacik25179202008-10-29 14:49:05 -04002893 search_start < end) {
2894 mutex_unlock(&block_group->alloc_mutex);
Chris Mason43662112008-11-07 09:06:11 -05002895 last_wanted = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04002896 continue;
Josef Bacik25179202008-10-29 14:49:05 -04002897 }
Josef Bacik80eb2342008-10-29 14:49:05 -04002898
2899 /* else we go to the next block group */
2900 goto new_group;
2901 }
2902
2903 ins->objectid = search_start;
2904 ins->offset = num_bytes;
Josef Bacik25179202008-10-29 14:49:05 -04002905
2906 btrfs_remove_free_space_lock(block_group, search_start,
2907 num_bytes);
Josef Bacik80eb2342008-10-29 14:49:05 -04002908 /* we are all good, lets return */
Josef Bacik25179202008-10-29 14:49:05 -04002909 mutex_unlock(&block_group->alloc_mutex);
Josef Bacik80eb2342008-10-29 14:49:05 -04002910 break;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002911 }
Josef Bacik80eb2342008-10-29 14:49:05 -04002912new_group:
Chris Mason42e70e72008-11-07 18:17:11 -05002913 mutex_unlock(&block_group->alloc_mutex);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002914 put_block_group(block_group);
2915 block_group = NULL;
Chris Mason42e70e72008-11-07 18:17:11 -05002916new_group_no_lock:
Chris Masonf5a31e12008-11-10 11:47:09 -05002917 /* don't try to compare new allocations against the
2918 * last allocation any more
2919 */
Chris Mason43662112008-11-07 09:06:11 -05002920 last_wanted = 0;
Chris Masonf5a31e12008-11-10 11:47:09 -05002921
Josef Bacik80eb2342008-10-29 14:49:05 -04002922 /*
2923 * Here's how this works.
2924 * loop == 0: we were searching a block group via a hint
2925 * and didn't find anything, so we start at
2926 * the head of the block groups and keep searching
2927 * loop == 1: we're searching through all of the block groups
2928 * if we hit the head again we have searched
2929 * all of the block groups for this space and we
2930 * need to try and allocate, if we cant error out.
2931 * loop == 2: we allocated more space and are looping through
2932 * all of the block groups again.
2933 */
2934 if (loop == 0) {
2935 head = &space_info->block_groups;
2936 cur = head->next;
Josef Bacik80eb2342008-10-29 14:49:05 -04002937 loop++;
2938 } else if (loop == 1 && cur == head) {
Chris Masonf5a31e12008-11-10 11:47:09 -05002939 int keep_going;
Chris Mason42e70e72008-11-07 18:17:11 -05002940
Chris Masonf5a31e12008-11-10 11:47:09 -05002941 /* at this point we give up on the empty_size
2942 * allocations and just try to allocate the min
2943 * space.
2944 *
2945 * The extra_loop field was set if an empty_size
2946 * allocation was attempted above, and if this
2947 * is try we need to try the loop again without
2948 * the additional empty_size.
2949 */
Chris Mason5b7c3fc2008-11-10 07:26:33 -05002950 total_needed -= empty_size;
2951 empty_size = 0;
Chris Masonf5a31e12008-11-10 11:47:09 -05002952 keep_going = extra_loop;
2953 loop++;
Chris Mason42e70e72008-11-07 18:17:11 -05002954
Josef Bacik80eb2342008-10-29 14:49:05 -04002955 if (allowed_chunk_alloc && !chunk_alloc_done) {
2956 up_read(&space_info->groups_sem);
2957 ret = do_chunk_alloc(trans, root, num_bytes +
2958 2 * 1024 * 1024, data, 1);
Josef Bacik80eb2342008-10-29 14:49:05 -04002959 down_read(&space_info->groups_sem);
Chris Mason2ed6d662008-11-13 09:59:33 -05002960 if (ret < 0)
2961 goto loop_check;
Josef Bacik80eb2342008-10-29 14:49:05 -04002962 head = &space_info->block_groups;
Chris Masonf5a31e12008-11-10 11:47:09 -05002963 /*
2964 * we've allocated a new chunk, keep
2965 * trying
2966 */
2967 keep_going = 1;
Josef Bacik80eb2342008-10-29 14:49:05 -04002968 chunk_alloc_done = 1;
2969 } else if (!allowed_chunk_alloc) {
2970 space_info->force_alloc = 1;
Chris Masonf5a31e12008-11-10 11:47:09 -05002971 }
Chris Mason2ed6d662008-11-13 09:59:33 -05002972loop_check:
Chris Masonf5a31e12008-11-10 11:47:09 -05002973 if (keep_going) {
2974 cur = head->next;
2975 extra_loop = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04002976 } else {
2977 break;
2978 }
2979 } else if (cur == head) {
2980 break;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002981 }
Josef Bacik80eb2342008-10-29 14:49:05 -04002982
2983 block_group = list_entry(cur, struct btrfs_block_group_cache,
2984 list);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002985 atomic_inc(&block_group->count);
2986
Josef Bacik80eb2342008-10-29 14:49:05 -04002987 search_start = block_group->key.objectid;
2988 cur = cur->next;
Chris Masone19caa52007-10-15 16:17:44 -04002989 }
Chris Mason0b86a832008-03-24 15:01:56 -04002990
Josef Bacik80eb2342008-10-29 14:49:05 -04002991 /* we found what we needed */
2992 if (ins->objectid) {
2993 if (!(data & BTRFS_BLOCK_GROUP_DATA))
Yan Zhengd2fb3432008-12-11 16:30:39 -05002994 trans->block_group = block_group->key.objectid;
Josef Bacik80eb2342008-10-29 14:49:05 -04002995
2996 if (last_ptr)
2997 *last_ptr = ins->objectid + ins->offset;
2998 ret = 0;
2999 } else if (!ret) {
Chris Masond3977122009-01-05 21:25:51 -05003000 printk(KERN_ERR "btrfs searching for %llu bytes, "
3001 "num_bytes %llu, loop %d, allowed_alloc %d\n",
3002 (unsigned long long)total_needed,
3003 (unsigned long long)num_bytes,
Josef Bacik4ce4cb52008-11-17 21:12:00 -05003004 loop, allowed_chunk_alloc);
Josef Bacik80eb2342008-10-29 14:49:05 -04003005 ret = -ENOSPC;
Chris Masonf2654de2007-06-26 12:20:46 -04003006 }
Yan Zhengd2fb3432008-12-11 16:30:39 -05003007 if (block_group)
3008 put_block_group(block_group);
Chris Mason0b86a832008-03-24 15:01:56 -04003009
Josef Bacik80eb2342008-10-29 14:49:05 -04003010 up_read(&space_info->groups_sem);
Chris Mason0f70abe2007-02-28 16:46:22 -05003011 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05003012}
Chris Masonec44a352008-04-28 15:29:52 -04003013
Josef Bacik0f9dd462008-09-23 13:14:11 -04003014static void dump_space_info(struct btrfs_space_info *info, u64 bytes)
3015{
3016 struct btrfs_block_group_cache *cache;
3017 struct list_head *l;
3018
Chris Masond3977122009-01-05 21:25:51 -05003019 printk(KERN_INFO "space_info has %llu free, is %sfull\n",
3020 (unsigned long long)(info->total_bytes - info->bytes_used -
3021 info->bytes_pinned - info->bytes_reserved),
3022 (info->full) ? "" : "not ");
Josef Bacik0f9dd462008-09-23 13:14:11 -04003023
Josef Bacik80eb2342008-10-29 14:49:05 -04003024 down_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003025 list_for_each(l, &info->block_groups) {
3026 cache = list_entry(l, struct btrfs_block_group_cache, list);
3027 spin_lock(&cache->lock);
Chris Masond3977122009-01-05 21:25:51 -05003028 printk(KERN_INFO "block group %llu has %llu bytes, %llu used "
3029 "%llu pinned %llu reserved\n",
3030 (unsigned long long)cache->key.objectid,
3031 (unsigned long long)cache->key.offset,
3032 (unsigned long long)btrfs_block_group_used(&cache->item),
3033 (unsigned long long)cache->pinned,
3034 (unsigned long long)cache->reserved);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003035 btrfs_dump_free_space(cache, bytes);
3036 spin_unlock(&cache->lock);
3037 }
Josef Bacik80eb2342008-10-29 14:49:05 -04003038 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003039}
Zheng Yane8569812008-09-26 10:05:48 -04003040
Chris Masone6dcd2d2008-07-17 12:53:50 -04003041static int __btrfs_reserve_extent(struct btrfs_trans_handle *trans,
3042 struct btrfs_root *root,
3043 u64 num_bytes, u64 min_alloc_size,
3044 u64 empty_size, u64 hint_byte,
3045 u64 search_end, struct btrfs_key *ins,
3046 u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05003047{
3048 int ret;
Chris Masonfbdc7622007-05-30 10:22:12 -04003049 u64 search_start = 0;
Chris Mason8790d502008-04-03 16:29:03 -04003050 u64 alloc_profile;
Chris Mason1261ec42007-03-20 20:35:03 -04003051 struct btrfs_fs_info *info = root->fs_info;
Chris Mason925baed2008-06-25 16:01:30 -04003052
Chris Mason6324fbf2008-03-24 15:01:59 -04003053 if (data) {
Chris Mason8790d502008-04-03 16:29:03 -04003054 alloc_profile = info->avail_data_alloc_bits &
Chris Masond3977122009-01-05 21:25:51 -05003055 info->data_alloc_profile;
Chris Mason8790d502008-04-03 16:29:03 -04003056 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04003057 } else if (root == root->fs_info->chunk_root) {
Chris Mason8790d502008-04-03 16:29:03 -04003058 alloc_profile = info->avail_system_alloc_bits &
Chris Masond3977122009-01-05 21:25:51 -05003059 info->system_alloc_profile;
Chris Mason8790d502008-04-03 16:29:03 -04003060 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04003061 } else {
Chris Mason8790d502008-04-03 16:29:03 -04003062 alloc_profile = info->avail_metadata_alloc_bits &
Chris Masond3977122009-01-05 21:25:51 -05003063 info->metadata_alloc_profile;
Chris Mason8790d502008-04-03 16:29:03 -04003064 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04003065 }
Chris Mason98d20f62008-04-14 09:46:10 -04003066again:
Yan Zheng2b820322008-11-17 21:11:30 -05003067 data = btrfs_reduce_alloc_profile(root, data);
Chris Mason0ef3e662008-05-24 14:04:53 -04003068 /*
3069 * the only place that sets empty_size is btrfs_realloc_node, which
3070 * is not called recursively on allocations
3071 */
3072 if (empty_size || root->ref_cows) {
Chris Mason593060d2008-03-25 16:50:33 -04003073 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
Chris Mason6324fbf2008-03-24 15:01:59 -04003074 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04003075 2 * 1024 * 1024,
3076 BTRFS_BLOCK_GROUP_METADATA |
3077 (info->metadata_alloc_profile &
3078 info->avail_metadata_alloc_bits), 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04003079 }
3080 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04003081 num_bytes + 2 * 1024 * 1024, data, 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04003082 }
Chris Mason0b86a832008-03-24 15:01:56 -04003083
Chris Masondb945352007-10-15 16:15:53 -04003084 WARN_ON(num_bytes < root->sectorsize);
3085 ret = find_free_extent(trans, root, num_bytes, empty_size,
3086 search_start, search_end, hint_byte, ins,
Chris Mason26b80032007-08-08 20:17:12 -04003087 trans->alloc_exclude_start,
3088 trans->alloc_exclude_nr, data);
Chris Mason3b951512008-04-17 11:29:12 -04003089
Chris Mason98d20f62008-04-14 09:46:10 -04003090 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
3091 num_bytes = num_bytes >> 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003092 num_bytes = num_bytes & ~(root->sectorsize - 1);
Chris Mason98d20f62008-04-14 09:46:10 -04003093 num_bytes = max(num_bytes, min_alloc_size);
Chris Mason0ef3e662008-05-24 14:04:53 -04003094 do_chunk_alloc(trans, root->fs_info->extent_root,
3095 num_bytes, data, 1);
Chris Mason98d20f62008-04-14 09:46:10 -04003096 goto again;
3097 }
Chris Masonec44a352008-04-28 15:29:52 -04003098 if (ret) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04003099 struct btrfs_space_info *sinfo;
3100
3101 sinfo = __find_space_info(root->fs_info, data);
Chris Masond3977122009-01-05 21:25:51 -05003102 printk(KERN_ERR "btrfs allocation failed flags %llu, "
3103 "wanted %llu\n", (unsigned long long)data,
3104 (unsigned long long)num_bytes);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003105 dump_space_info(sinfo, num_bytes);
Chris Mason925baed2008-06-25 16:01:30 -04003106 BUG();
Chris Mason925baed2008-06-25 16:01:30 -04003107 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04003108
3109 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003110}
3111
Chris Mason65b51a02008-08-01 15:11:20 -04003112int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
3113{
Josef Bacik0f9dd462008-09-23 13:14:11 -04003114 struct btrfs_block_group_cache *cache;
Liu Hui1f3c79a2009-01-05 15:57:51 -05003115 int ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003116
Josef Bacik0f9dd462008-09-23 13:14:11 -04003117 cache = btrfs_lookup_block_group(root->fs_info, start);
3118 if (!cache) {
Chris Masond3977122009-01-05 21:25:51 -05003119 printk(KERN_ERR "Unable to find block group for %llu\n",
3120 (unsigned long long)start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003121 return -ENOSPC;
3122 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05003123
3124 ret = btrfs_discard_extent(root, start, len);
3125
Josef Bacik0f9dd462008-09-23 13:14:11 -04003126 btrfs_add_free_space(cache, start, len);
Yan Zhengd2fb3432008-12-11 16:30:39 -05003127 put_block_group(cache);
Zheng Yan1a40e232008-09-26 10:09:34 -04003128 update_reserved_extents(root, start, len, 0);
Liu Hui1f3c79a2009-01-05 15:57:51 -05003129
3130 return ret;
Chris Mason65b51a02008-08-01 15:11:20 -04003131}
3132
Chris Masone6dcd2d2008-07-17 12:53:50 -04003133int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
3134 struct btrfs_root *root,
3135 u64 num_bytes, u64 min_alloc_size,
3136 u64 empty_size, u64 hint_byte,
3137 u64 search_end, struct btrfs_key *ins,
3138 u64 data)
3139{
3140 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003141 ret = __btrfs_reserve_extent(trans, root, num_bytes, min_alloc_size,
3142 empty_size, hint_byte, search_end, ins,
3143 data);
Zheng Yane8569812008-09-26 10:05:48 -04003144 update_reserved_extents(root, ins->objectid, ins->offset, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003145 return ret;
3146}
3147
3148static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04003149 struct btrfs_root *root, u64 parent,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003150 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003151 u64 owner, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04003152{
3153 int ret;
3154 int pending_ret;
3155 u64 super_used;
3156 u64 root_used;
3157 u64 num_bytes = ins->offset;
3158 u32 sizes[2];
3159 struct btrfs_fs_info *info = root->fs_info;
3160 struct btrfs_root *extent_root = info->extent_root;
3161 struct btrfs_extent_item *extent_item;
3162 struct btrfs_extent_ref *ref;
3163 struct btrfs_path *path;
3164 struct btrfs_key keys[2];
Chris Masonf2654de2007-06-26 12:20:46 -04003165
Zheng Yan31840ae2008-09-23 13:14:14 -04003166 if (parent == 0)
3167 parent = ins->objectid;
3168
Josef Bacik58176a92007-08-29 15:47:34 -04003169 /* block accounting for super block */
Chris Mason75eff682008-12-15 15:54:40 -05003170 spin_lock(&info->delalloc_lock);
Chris Masondb945352007-10-15 16:15:53 -04003171 super_used = btrfs_super_bytes_used(&info->super_copy);
3172 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
Chris Mason26b80032007-08-08 20:17:12 -04003173
Josef Bacik58176a92007-08-29 15:47:34 -04003174 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04003175 root_used = btrfs_root_used(&root->root_item);
3176 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
Yan Zheng34bf63c2008-12-19 10:58:46 -05003177 spin_unlock(&info->delalloc_lock);
Josef Bacik58176a92007-08-29 15:47:34 -04003178
Chris Mason26b80032007-08-08 20:17:12 -04003179 if (root == extent_root) {
Zheng Yan31840ae2008-09-23 13:14:14 -04003180 struct pending_extent_op *extent_op;
3181
3182 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
3183 BUG_ON(!extent_op);
3184
3185 extent_op->type = PENDING_EXTENT_INSERT;
3186 extent_op->bytenr = ins->objectid;
3187 extent_op->num_bytes = ins->offset;
3188 extent_op->parent = parent;
3189 extent_op->orig_parent = 0;
3190 extent_op->generation = ref_generation;
3191 extent_op->orig_generation = 0;
3192 extent_op->level = (int)owner;
Josef Bacikf3465ca2008-11-12 14:19:50 -05003193 INIT_LIST_HEAD(&extent_op->list);
3194 extent_op->del = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04003195
Josef Bacik25179202008-10-29 14:49:05 -04003196 mutex_lock(&root->fs_info->extent_ins_mutex);
Chris Mason1a5bc162007-10-15 16:15:26 -04003197 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
3198 ins->objectid + ins->offset - 1,
Josef Bacik25179202008-10-29 14:49:05 -04003199 EXTENT_WRITEBACK, GFP_NOFS);
Zheng Yan31840ae2008-09-23 13:14:14 -04003200 set_state_private(&root->fs_info->extent_ins,
3201 ins->objectid, (unsigned long)extent_op);
Josef Bacik25179202008-10-29 14:49:05 -04003202 mutex_unlock(&root->fs_info->extent_ins_mutex);
Chris Mason26b80032007-08-08 20:17:12 -04003203 goto update_block;
3204 }
3205
Chris Mason47e4bb92008-02-01 14:51:59 -05003206 memcpy(&keys[0], ins, sizeof(*ins));
Chris Mason47e4bb92008-02-01 14:51:59 -05003207 keys[1].objectid = ins->objectid;
3208 keys[1].type = BTRFS_EXTENT_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04003209 keys[1].offset = parent;
Chris Mason47e4bb92008-02-01 14:51:59 -05003210 sizes[0] = sizeof(*extent_item);
3211 sizes[1] = sizeof(*ref);
Chris Mason7bb86312007-12-11 09:25:06 -05003212
3213 path = btrfs_alloc_path();
3214 BUG_ON(!path);
Chris Mason47e4bb92008-02-01 14:51:59 -05003215
3216 ret = btrfs_insert_empty_items(trans, extent_root, path, keys,
3217 sizes, 2);
Chris Masonccd467d2007-06-28 15:57:36 -04003218 BUG_ON(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003219
Chris Mason47e4bb92008-02-01 14:51:59 -05003220 extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3221 struct btrfs_extent_item);
3222 btrfs_set_extent_refs(path->nodes[0], extent_item, 1);
3223 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
3224 struct btrfs_extent_ref);
3225
3226 btrfs_set_ref_root(path->nodes[0], ref, root_objectid);
3227 btrfs_set_ref_generation(path->nodes[0], ref, ref_generation);
3228 btrfs_set_ref_objectid(path->nodes[0], ref, owner);
Zheng Yan31840ae2008-09-23 13:14:14 -04003229 btrfs_set_ref_num_refs(path->nodes[0], ref, 1);
Chris Mason47e4bb92008-02-01 14:51:59 -05003230
3231 btrfs_mark_buffer_dirty(path->nodes[0]);
3232
3233 trans->alloc_exclude_start = 0;
3234 trans->alloc_exclude_nr = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05003235 btrfs_free_path(path);
Chris Mason87ef2bb2008-10-30 11:23:27 -04003236 finish_current_insert(trans, extent_root, 0);
3237 pending_ret = del_pending_extents(trans, extent_root, 0);
Chris Masonf510cfe2007-10-15 16:14:48 -04003238
Chris Mason925baed2008-06-25 16:01:30 -04003239 if (ret)
3240 goto out;
Chris Masone37c9e62007-05-09 20:13:14 -04003241 if (pending_ret) {
Chris Mason925baed2008-06-25 16:01:30 -04003242 ret = pending_ret;
3243 goto out;
Chris Masone37c9e62007-05-09 20:13:14 -04003244 }
Chris Mason26b80032007-08-08 20:17:12 -04003245
3246update_block:
Chris Masond3977122009-01-05 21:25:51 -05003247 ret = update_block_group(trans, root, ins->objectid,
3248 ins->offset, 1, 0);
Chris Masonf5947062008-02-04 10:10:13 -05003249 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05003250 printk(KERN_ERR "btrfs update block group failed for %llu "
3251 "%llu\n", (unsigned long long)ins->objectid,
3252 (unsigned long long)ins->offset);
Chris Masonf5947062008-02-04 10:10:13 -05003253 BUG();
3254 }
Chris Mason925baed2008-06-25 16:01:30 -04003255out:
Chris Masone6dcd2d2008-07-17 12:53:50 -04003256 return ret;
3257}
3258
3259int btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04003260 struct btrfs_root *root, u64 parent,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003261 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003262 u64 owner, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04003263{
3264 int ret;
Chris Mason1c2308f2008-09-23 13:14:13 -04003265
3266 if (root_objectid == BTRFS_TREE_LOG_OBJECTID)
3267 return 0;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003268 ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid,
3269 ref_generation, owner, ins);
Zheng Yane8569812008-09-26 10:05:48 -04003270 update_reserved_extents(root, ins->objectid, ins->offset, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003271 return ret;
3272}
Chris Masone02119d2008-09-05 16:13:11 -04003273
3274/*
3275 * this is used by the tree logging recovery code. It records that
3276 * an extent has been allocated and makes sure to clear the free
3277 * space cache bits as well
3278 */
3279int btrfs_alloc_logged_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04003280 struct btrfs_root *root, u64 parent,
Chris Masone02119d2008-09-05 16:13:11 -04003281 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003282 u64 owner, struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04003283{
3284 int ret;
3285 struct btrfs_block_group_cache *block_group;
3286
Chris Masone02119d2008-09-05 16:13:11 -04003287 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
Josef Bacikea6a4782008-11-20 12:16:16 -05003288 mutex_lock(&block_group->cache_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04003289 cache_block_group(root, block_group);
Josef Bacikea6a4782008-11-20 12:16:16 -05003290 mutex_unlock(&block_group->cache_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04003291
Josef Bacikea6a4782008-11-20 12:16:16 -05003292 ret = btrfs_remove_free_space(block_group, ins->objectid,
3293 ins->offset);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003294 BUG_ON(ret);
Yan Zhengd2fb3432008-12-11 16:30:39 -05003295 put_block_group(block_group);
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003296 ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid,
3297 ref_generation, owner, ins);
Chris Masone02119d2008-09-05 16:13:11 -04003298 return ret;
3299}
3300
Chris Masone6dcd2d2008-07-17 12:53:50 -04003301/*
3302 * finds a free extent and does all the dirty work required for allocation
3303 * returns the key for the extent through ins, and a tree buffer for
3304 * the first block of the extent through buf.
3305 *
3306 * returns 0 if everything worked, non-zero otherwise.
3307 */
3308int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
3309 struct btrfs_root *root,
Zheng Yan31840ae2008-09-23 13:14:14 -04003310 u64 num_bytes, u64 parent, u64 min_alloc_size,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003311 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003312 u64 owner_objectid, u64 empty_size, u64 hint_byte,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003313 u64 search_end, struct btrfs_key *ins, u64 data)
3314{
3315 int ret;
3316
Chris Masone6dcd2d2008-07-17 12:53:50 -04003317 ret = __btrfs_reserve_extent(trans, root, num_bytes,
3318 min_alloc_size, empty_size, hint_byte,
3319 search_end, ins, data);
3320 BUG_ON(ret);
Chris Masond00aff02008-09-11 15:54:42 -04003321 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
Zheng Yan31840ae2008-09-23 13:14:14 -04003322 ret = __btrfs_alloc_reserved_extent(trans, root, parent,
3323 root_objectid, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003324 owner_objectid, ins);
Chris Masond00aff02008-09-11 15:54:42 -04003325 BUG_ON(ret);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003326
Zheng Yane8569812008-09-26 10:05:48 -04003327 } else {
3328 update_reserved_extents(root, ins->objectid, ins->offset, 1);
Chris Masond00aff02008-09-11 15:54:42 -04003329 }
Chris Mason925baed2008-06-25 16:01:30 -04003330 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05003331}
Chris Mason65b51a02008-08-01 15:11:20 -04003332
3333struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
3334 struct btrfs_root *root,
3335 u64 bytenr, u32 blocksize)
3336{
3337 struct extent_buffer *buf;
3338
3339 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
3340 if (!buf)
3341 return ERR_PTR(-ENOMEM);
3342 btrfs_set_header_generation(buf, trans->transid);
3343 btrfs_tree_lock(buf);
3344 clean_tree_block(trans, root, buf);
3345 btrfs_set_buffer_uptodate(buf);
Chris Masond0c803c2008-09-11 16:17:57 -04003346 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
3347 set_extent_dirty(&root->dirty_log_pages, buf->start,
Chris Mason65b51a02008-08-01 15:11:20 -04003348 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04003349 } else {
3350 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
3351 buf->start + buf->len - 1, GFP_NOFS);
3352 }
Chris Mason65b51a02008-08-01 15:11:20 -04003353 trans->blocks_used++;
3354 return buf;
3355}
3356
Chris Masonfec577f2007-02-26 10:40:21 -05003357/*
3358 * helper function to allocate a block for a given tree
3359 * returns the tree buffer or NULL.
3360 */
Chris Mason5f39d392007-10-15 16:14:19 -04003361struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Masondb945352007-10-15 16:15:53 -04003362 struct btrfs_root *root,
Zheng Yan31840ae2008-09-23 13:14:14 -04003363 u32 blocksize, u64 parent,
Chris Mason7bb86312007-12-11 09:25:06 -05003364 u64 root_objectid,
3365 u64 ref_generation,
Chris Mason7bb86312007-12-11 09:25:06 -05003366 int level,
3367 u64 hint,
Chris Mason5f39d392007-10-15 16:14:19 -04003368 u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05003369{
Chris Masone2fa7222007-03-12 16:22:34 -04003370 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05003371 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04003372 struct extent_buffer *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05003373
Zheng Yan31840ae2008-09-23 13:14:14 -04003374 ret = btrfs_alloc_extent(trans, root, blocksize, parent, blocksize,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003375 root_objectid, ref_generation, level,
Zheng Yan31840ae2008-09-23 13:14:14 -04003376 empty_size, hint, (u64)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05003377 if (ret) {
Chris Mason54aa1f42007-06-22 14:16:25 -04003378 BUG_ON(ret > 0);
3379 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05003380 }
Chris Mason55c69072008-01-09 15:55:33 -05003381
Chris Mason65b51a02008-08-01 15:11:20 -04003382 buf = btrfs_init_new_buffer(trans, root, ins.objectid, blocksize);
Chris Masonfec577f2007-02-26 10:40:21 -05003383 return buf;
3384}
Chris Masona28ec192007-03-06 20:08:01 -05003385
Chris Masone02119d2008-09-05 16:13:11 -04003386int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans,
3387 struct btrfs_root *root, struct extent_buffer *leaf)
Chris Mason6407bf62007-03-27 06:33:00 -04003388{
Chris Mason7bb86312007-12-11 09:25:06 -05003389 u64 leaf_owner;
3390 u64 leaf_generation;
Chris Mason5f39d392007-10-15 16:14:19 -04003391 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04003392 struct btrfs_file_extent_item *fi;
3393 int i;
3394 int nritems;
3395 int ret;
3396
Chris Mason5f39d392007-10-15 16:14:19 -04003397 BUG_ON(!btrfs_is_leaf(leaf));
3398 nritems = btrfs_header_nritems(leaf);
Chris Mason7bb86312007-12-11 09:25:06 -05003399 leaf_owner = btrfs_header_owner(leaf);
3400 leaf_generation = btrfs_header_generation(leaf);
3401
Chris Mason6407bf62007-03-27 06:33:00 -04003402 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04003403 u64 disk_bytenr;
Chris Masone34a5b42008-07-22 12:08:37 -04003404 cond_resched();
Chris Mason5f39d392007-10-15 16:14:19 -04003405
3406 btrfs_item_key_to_cpu(leaf, &key, i);
3407 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -04003408 continue;
3409 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04003410 if (btrfs_file_extent_type(leaf, fi) ==
3411 BTRFS_FILE_EXTENT_INLINE)
Chris Mason236454df2007-04-19 13:37:44 -04003412 continue;
Chris Mason6407bf62007-03-27 06:33:00 -04003413 /*
3414 * FIXME make sure to insert a trans record that
3415 * repeats the snapshot del on crash
3416 */
Chris Masondb945352007-10-15 16:15:53 -04003417 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
3418 if (disk_bytenr == 0)
Chris Mason3a686372007-05-24 13:35:57 -04003419 continue;
Chris Mason4a096752008-07-21 10:29:44 -04003420
Chris Mason925baed2008-06-25 16:01:30 -04003421 ret = __btrfs_free_extent(trans, root, disk_bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -05003422 btrfs_file_extent_disk_num_bytes(leaf, fi),
Zheng Yan31840ae2008-09-23 13:14:14 -04003423 leaf->start, leaf_owner, leaf_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003424 key.objectid, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04003425 BUG_ON(ret);
Chris Mason2dd3e672008-08-04 08:20:15 -04003426
3427 atomic_inc(&root->fs_info->throttle_gen);
3428 wake_up(&root->fs_info->transaction_throttle);
3429 cond_resched();
Chris Mason6407bf62007-03-27 06:33:00 -04003430 }
3431 return 0;
3432}
3433
Chris Masond3977122009-01-05 21:25:51 -05003434static noinline int cache_drop_leaf_ref(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04003435 struct btrfs_root *root,
3436 struct btrfs_leaf_ref *ref)
Yan Zheng31153d82008-07-28 15:32:19 -04003437{
3438 int i;
3439 int ret;
3440 struct btrfs_extent_info *info = ref->extents;
3441
Yan Zheng31153d82008-07-28 15:32:19 -04003442 for (i = 0; i < ref->nritems; i++) {
Zheng Yan31840ae2008-09-23 13:14:14 -04003443 ret = __btrfs_free_extent(trans, root, info->bytenr,
3444 info->num_bytes, ref->bytenr,
3445 ref->owner, ref->generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003446 info->objectid, 0);
Chris Mason2dd3e672008-08-04 08:20:15 -04003447
3448 atomic_inc(&root->fs_info->throttle_gen);
3449 wake_up(&root->fs_info->transaction_throttle);
3450 cond_resched();
3451
Yan Zheng31153d82008-07-28 15:32:19 -04003452 BUG_ON(ret);
3453 info++;
3454 }
Yan Zheng31153d82008-07-28 15:32:19 -04003455
3456 return 0;
3457}
3458
Chris Masond3977122009-01-05 21:25:51 -05003459static int drop_snap_lookup_refcount(struct btrfs_root *root, u64 start,
3460 u64 len, u32 *refs)
Chris Mason333db942008-06-25 16:01:30 -04003461{
Chris Mason017e5362008-07-28 15:32:51 -04003462 int ret;
Chris Masonf87f0572008-08-01 11:27:23 -04003463
Zheng Yan31840ae2008-09-23 13:14:14 -04003464 ret = btrfs_lookup_extent_ref(NULL, root, start, len, refs);
Chris Masonf87f0572008-08-01 11:27:23 -04003465 BUG_ON(ret);
3466
Chris Masond3977122009-01-05 21:25:51 -05003467#if 0 /* some debugging code in case we see problems here */
Chris Masonf87f0572008-08-01 11:27:23 -04003468 /* if the refs count is one, it won't get increased again. But
3469 * if the ref count is > 1, someone may be decreasing it at
3470 * the same time we are.
3471 */
3472 if (*refs != 1) {
3473 struct extent_buffer *eb = NULL;
3474 eb = btrfs_find_create_tree_block(root, start, len);
3475 if (eb)
3476 btrfs_tree_lock(eb);
3477
3478 mutex_lock(&root->fs_info->alloc_mutex);
3479 ret = lookup_extent_ref(NULL, root, start, len, refs);
3480 BUG_ON(ret);
3481 mutex_unlock(&root->fs_info->alloc_mutex);
3482
3483 if (eb) {
3484 btrfs_tree_unlock(eb);
3485 free_extent_buffer(eb);
3486 }
3487 if (*refs == 1) {
Chris Masond3977122009-01-05 21:25:51 -05003488 printk(KERN_ERR "btrfs block %llu went down to one "
3489 "during drop_snap\n", (unsigned long long)start);
Chris Masonf87f0572008-08-01 11:27:23 -04003490 }
3491
3492 }
3493#endif
3494
Chris Masone7a84562008-06-25 16:01:31 -04003495 cond_resched();
Chris Mason017e5362008-07-28 15:32:51 -04003496 return ret;
Chris Mason333db942008-06-25 16:01:30 -04003497}
3498
3499/*
Chris Mason9aca1d52007-03-13 11:09:37 -04003500 * helper function for drop_snapshot, this walks down the tree dropping ref
3501 * counts as it goes.
3502 */
Chris Masond3977122009-01-05 21:25:51 -05003503static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05003504 struct btrfs_root *root,
3505 struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05003506{
Chris Mason7bb86312007-12-11 09:25:06 -05003507 u64 root_owner;
3508 u64 root_gen;
3509 u64 bytenr;
Chris Masonca7a79a2008-05-12 12:59:19 -04003510 u64 ptr_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04003511 struct extent_buffer *next;
3512 struct extent_buffer *cur;
Chris Mason7bb86312007-12-11 09:25:06 -05003513 struct extent_buffer *parent;
Yan Zheng31153d82008-07-28 15:32:19 -04003514 struct btrfs_leaf_ref *ref;
Chris Masondb945352007-10-15 16:15:53 -04003515 u32 blocksize;
Chris Mason20524f02007-03-10 06:35:47 -05003516 int ret;
3517 u32 refs;
3518
Chris Mason5caf2a02007-04-02 11:20:42 -04003519 WARN_ON(*level < 0);
3520 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason333db942008-06-25 16:01:30 -04003521 ret = drop_snap_lookup_refcount(root, path->nodes[*level]->start,
Chris Masondb945352007-10-15 16:15:53 -04003522 path->nodes[*level]->len, &refs);
Chris Mason20524f02007-03-10 06:35:47 -05003523 BUG_ON(ret);
3524 if (refs > 1)
3525 goto out;
Chris Masone0115992007-06-19 16:23:05 -04003526
Chris Mason9aca1d52007-03-13 11:09:37 -04003527 /*
3528 * walk down to the last node level and free all the leaves
3529 */
Chris Masond3977122009-01-05 21:25:51 -05003530 while (*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04003531 WARN_ON(*level < 0);
3532 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -05003533 cur = path->nodes[*level];
Chris Masone0115992007-06-19 16:23:05 -04003534
Chris Mason5f39d392007-10-15 16:14:19 -04003535 if (btrfs_header_level(cur) != *level)
Chris Mason2c90e5d2007-04-02 10:50:19 -04003536 WARN_ON(1);
Chris Masone0115992007-06-19 16:23:05 -04003537
Chris Mason7518a232007-03-12 12:01:18 -04003538 if (path->slots[*level] >=
Chris Mason5f39d392007-10-15 16:14:19 -04003539 btrfs_header_nritems(cur))
Chris Mason20524f02007-03-10 06:35:47 -05003540 break;
Chris Mason6407bf62007-03-27 06:33:00 -04003541 if (*level == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04003542 ret = btrfs_drop_leaf_ref(trans, root, cur);
Chris Mason6407bf62007-03-27 06:33:00 -04003543 BUG_ON(ret);
3544 break;
3545 }
Chris Masondb945352007-10-15 16:15:53 -04003546 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
Chris Masonca7a79a2008-05-12 12:59:19 -04003547 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
Chris Masondb945352007-10-15 16:15:53 -04003548 blocksize = btrfs_level_size(root, *level - 1);
Chris Mason925baed2008-06-25 16:01:30 -04003549
Chris Mason333db942008-06-25 16:01:30 -04003550 ret = drop_snap_lookup_refcount(root, bytenr, blocksize, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -04003551 BUG_ON(ret);
3552 if (refs != 1) {
Chris Mason7bb86312007-12-11 09:25:06 -05003553 parent = path->nodes[*level];
3554 root_owner = btrfs_header_owner(parent);
3555 root_gen = btrfs_header_generation(parent);
Chris Mason20524f02007-03-10 06:35:47 -05003556 path->slots[*level]++;
Chris Masonf87f0572008-08-01 11:27:23 -04003557
Chris Mason925baed2008-06-25 16:01:30 -04003558 ret = __btrfs_free_extent(trans, root, bytenr,
Zheng Yan31840ae2008-09-23 13:14:14 -04003559 blocksize, parent->start,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003560 root_owner, root_gen,
3561 *level - 1, 1);
Chris Mason20524f02007-03-10 06:35:47 -05003562 BUG_ON(ret);
Chris Mason18e35e0a2008-08-01 13:11:41 -04003563
3564 atomic_inc(&root->fs_info->throttle_gen);
3565 wake_up(&root->fs_info->transaction_throttle);
Chris Mason2dd3e672008-08-04 08:20:15 -04003566 cond_resched();
Chris Mason18e35e0a2008-08-01 13:11:41 -04003567
Chris Mason20524f02007-03-10 06:35:47 -05003568 continue;
3569 }
Chris Masonf87f0572008-08-01 11:27:23 -04003570 /*
3571 * at this point, we have a single ref, and since the
3572 * only place referencing this extent is a dead root
3573 * the reference count should never go higher.
3574 * So, we don't need to check it again
3575 */
Yan Zheng31153d82008-07-28 15:32:19 -04003576 if (*level == 1) {
Chris Mason017e5362008-07-28 15:32:51 -04003577 ref = btrfs_lookup_leaf_ref(root, bytenr);
Zheng Yan1a40e232008-09-26 10:09:34 -04003578 if (ref && ref->generation != ptr_gen) {
3579 btrfs_free_leaf_ref(root, ref);
3580 ref = NULL;
3581 }
Yan Zheng31153d82008-07-28 15:32:19 -04003582 if (ref) {
Chris Masone02119d2008-09-05 16:13:11 -04003583 ret = cache_drop_leaf_ref(trans, root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04003584 BUG_ON(ret);
3585 btrfs_remove_leaf_ref(root, ref);
Yanbcc63ab2008-07-30 16:29:20 -04003586 btrfs_free_leaf_ref(root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04003587 *level = 0;
3588 break;
3589 }
3590 }
Chris Masondb945352007-10-15 16:15:53 -04003591 next = btrfs_find_tree_block(root, bytenr, blocksize);
Chris Mason1259ab72008-05-12 13:39:03 -04003592 if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
Chris Mason5f39d392007-10-15 16:14:19 -04003593 free_extent_buffer(next);
Chris Mason333db942008-06-25 16:01:30 -04003594
Chris Masonca7a79a2008-05-12 12:59:19 -04003595 next = read_tree_block(root, bytenr, blocksize,
3596 ptr_gen);
Chris Masone7a84562008-06-25 16:01:31 -04003597 cond_resched();
Chris Masonf87f0572008-08-01 11:27:23 -04003598#if 0
3599 /*
3600 * this is a debugging check and can go away
3601 * the ref should never go all the way down to 1
3602 * at this point
3603 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04003604 ret = lookup_extent_ref(NULL, root, bytenr, blocksize,
3605 &refs);
Chris Masone9d0b132007-08-10 14:06:19 -04003606 BUG_ON(ret);
Chris Masonf87f0572008-08-01 11:27:23 -04003607 WARN_ON(refs != 1);
3608#endif
Chris Masone9d0b132007-08-10 14:06:19 -04003609 }
Chris Mason5caf2a02007-04-02 11:20:42 -04003610 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -04003611 if (path->nodes[*level-1])
Chris Mason5f39d392007-10-15 16:14:19 -04003612 free_extent_buffer(path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -05003613 path->nodes[*level-1] = next;
Chris Mason5f39d392007-10-15 16:14:19 -04003614 *level = btrfs_header_level(next);
Chris Mason20524f02007-03-10 06:35:47 -05003615 path->slots[*level] = 0;
Chris Mason2dd3e672008-08-04 08:20:15 -04003616 cond_resched();
Chris Mason20524f02007-03-10 06:35:47 -05003617 }
3618out:
Chris Mason5caf2a02007-04-02 11:20:42 -04003619 WARN_ON(*level < 0);
3620 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason7bb86312007-12-11 09:25:06 -05003621
3622 if (path->nodes[*level] == root->node) {
Chris Mason7bb86312007-12-11 09:25:06 -05003623 parent = path->nodes[*level];
Yan Zheng31153d82008-07-28 15:32:19 -04003624 bytenr = path->nodes[*level]->start;
Chris Mason7bb86312007-12-11 09:25:06 -05003625 } else {
3626 parent = path->nodes[*level + 1];
Yan Zheng31153d82008-07-28 15:32:19 -04003627 bytenr = btrfs_node_blockptr(parent, path->slots[*level + 1]);
Chris Mason7bb86312007-12-11 09:25:06 -05003628 }
3629
Yan Zheng31153d82008-07-28 15:32:19 -04003630 blocksize = btrfs_level_size(root, *level);
3631 root_owner = btrfs_header_owner(parent);
Chris Mason7bb86312007-12-11 09:25:06 -05003632 root_gen = btrfs_header_generation(parent);
Yan Zheng31153d82008-07-28 15:32:19 -04003633
3634 ret = __btrfs_free_extent(trans, root, bytenr, blocksize,
Zheng Yan31840ae2008-09-23 13:14:14 -04003635 parent->start, root_owner, root_gen,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003636 *level, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04003637 free_extent_buffer(path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -05003638 path->nodes[*level] = NULL;
3639 *level += 1;
3640 BUG_ON(ret);
Chris Masonf87f0572008-08-01 11:27:23 -04003641
Chris Masone7a84562008-06-25 16:01:31 -04003642 cond_resched();
Chris Mason20524f02007-03-10 06:35:47 -05003643 return 0;
3644}
3645
Chris Mason9aca1d52007-03-13 11:09:37 -04003646/*
Yan Zhengf82d02d2008-10-29 14:49:05 -04003647 * helper function for drop_subtree, this function is similar to
3648 * walk_down_tree. The main difference is that it checks reference
3649 * counts while tree blocks are locked.
3650 */
Chris Masond3977122009-01-05 21:25:51 -05003651static noinline int walk_down_subtree(struct btrfs_trans_handle *trans,
Yan Zhengf82d02d2008-10-29 14:49:05 -04003652 struct btrfs_root *root,
3653 struct btrfs_path *path, int *level)
3654{
3655 struct extent_buffer *next;
3656 struct extent_buffer *cur;
3657 struct extent_buffer *parent;
3658 u64 bytenr;
3659 u64 ptr_gen;
3660 u32 blocksize;
3661 u32 refs;
3662 int ret;
3663
3664 cur = path->nodes[*level];
3665 ret = btrfs_lookup_extent_ref(trans, root, cur->start, cur->len,
3666 &refs);
3667 BUG_ON(ret);
3668 if (refs > 1)
3669 goto out;
3670
3671 while (*level >= 0) {
3672 cur = path->nodes[*level];
3673 if (*level == 0) {
3674 ret = btrfs_drop_leaf_ref(trans, root, cur);
3675 BUG_ON(ret);
3676 clean_tree_block(trans, root, cur);
3677 break;
3678 }
3679 if (path->slots[*level] >= btrfs_header_nritems(cur)) {
3680 clean_tree_block(trans, root, cur);
3681 break;
3682 }
3683
3684 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
3685 blocksize = btrfs_level_size(root, *level - 1);
3686 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
3687
3688 next = read_tree_block(root, bytenr, blocksize, ptr_gen);
3689 btrfs_tree_lock(next);
3690
3691 ret = btrfs_lookup_extent_ref(trans, root, bytenr, blocksize,
3692 &refs);
3693 BUG_ON(ret);
3694 if (refs > 1) {
3695 parent = path->nodes[*level];
3696 ret = btrfs_free_extent(trans, root, bytenr,
3697 blocksize, parent->start,
3698 btrfs_header_owner(parent),
3699 btrfs_header_generation(parent),
3700 *level - 1, 1);
3701 BUG_ON(ret);
3702 path->slots[*level]++;
3703 btrfs_tree_unlock(next);
3704 free_extent_buffer(next);
3705 continue;
3706 }
3707
3708 *level = btrfs_header_level(next);
3709 path->nodes[*level] = next;
3710 path->slots[*level] = 0;
3711 path->locks[*level] = 1;
3712 cond_resched();
3713 }
3714out:
3715 parent = path->nodes[*level + 1];
3716 bytenr = path->nodes[*level]->start;
3717 blocksize = path->nodes[*level]->len;
3718
3719 ret = btrfs_free_extent(trans, root, bytenr, blocksize,
3720 parent->start, btrfs_header_owner(parent),
3721 btrfs_header_generation(parent), *level, 1);
3722 BUG_ON(ret);
3723
3724 if (path->locks[*level]) {
3725 btrfs_tree_unlock(path->nodes[*level]);
3726 path->locks[*level] = 0;
3727 }
3728 free_extent_buffer(path->nodes[*level]);
3729 path->nodes[*level] = NULL;
3730 *level += 1;
3731 cond_resched();
3732 return 0;
3733}
3734
3735/*
Chris Mason9aca1d52007-03-13 11:09:37 -04003736 * helper for dropping snapshots. This walks back up the tree in the path
3737 * to find the first node higher up where we haven't yet gone through
3738 * all the slots
3739 */
Chris Masond3977122009-01-05 21:25:51 -05003740static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05003741 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04003742 struct btrfs_path *path,
3743 int *level, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05003744{
Chris Mason7bb86312007-12-11 09:25:06 -05003745 u64 root_owner;
3746 u64 root_gen;
3747 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05003748 int i;
3749 int slot;
3750 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04003751
Yan Zhengf82d02d2008-10-29 14:49:05 -04003752 for (i = *level; i < max_level && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -05003753 slot = path->slots[i];
Chris Mason5f39d392007-10-15 16:14:19 -04003754 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
3755 struct extent_buffer *node;
3756 struct btrfs_disk_key disk_key;
3757 node = path->nodes[i];
Chris Mason20524f02007-03-10 06:35:47 -05003758 path->slots[i]++;
3759 *level = i;
Chris Mason9f3a7422007-08-07 15:52:19 -04003760 WARN_ON(*level == 0);
Chris Mason5f39d392007-10-15 16:14:19 -04003761 btrfs_node_key(node, &disk_key, path->slots[i]);
Chris Mason9f3a7422007-08-07 15:52:19 -04003762 memcpy(&root_item->drop_progress,
Chris Mason5f39d392007-10-15 16:14:19 -04003763 &disk_key, sizeof(disk_key));
Chris Mason9f3a7422007-08-07 15:52:19 -04003764 root_item->drop_level = i;
Chris Mason20524f02007-03-10 06:35:47 -05003765 return 0;
3766 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04003767 struct extent_buffer *parent;
3768 if (path->nodes[*level] == root->node)
3769 parent = path->nodes[*level];
3770 else
3771 parent = path->nodes[*level + 1];
3772
3773 root_owner = btrfs_header_owner(parent);
3774 root_gen = btrfs_header_generation(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04003775
3776 clean_tree_block(trans, root, path->nodes[*level]);
Chris Masone089f052007-03-16 16:20:31 -04003777 ret = btrfs_free_extent(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04003778 path->nodes[*level]->start,
Chris Mason7bb86312007-12-11 09:25:06 -05003779 path->nodes[*level]->len,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003780 parent->start, root_owner,
3781 root_gen, *level, 1);
Chris Mason6407bf62007-03-27 06:33:00 -04003782 BUG_ON(ret);
Yan Zhengf82d02d2008-10-29 14:49:05 -04003783 if (path->locks[*level]) {
3784 btrfs_tree_unlock(path->nodes[*level]);
3785 path->locks[*level] = 0;
3786 }
Chris Mason5f39d392007-10-15 16:14:19 -04003787 free_extent_buffer(path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -04003788 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -05003789 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -05003790 }
3791 }
3792 return 1;
3793}
3794
Chris Mason9aca1d52007-03-13 11:09:37 -04003795/*
3796 * drop the reference count on the tree rooted at 'snap'. This traverses
3797 * the tree freeing any blocks that have a ref count of zero after being
3798 * decremented.
3799 */
Chris Masone089f052007-03-16 16:20:31 -04003800int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason9f3a7422007-08-07 15:52:19 -04003801 *root)
Chris Mason20524f02007-03-10 06:35:47 -05003802{
Chris Mason3768f362007-03-13 16:47:54 -04003803 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -04003804 int wret;
Chris Mason20524f02007-03-10 06:35:47 -05003805 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -04003806 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -05003807 int i;
3808 int orig_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04003809 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05003810
Chris Masona2135012008-06-25 16:01:30 -04003811 WARN_ON(!mutex_is_locked(&root->fs_info->drop_mutex));
Chris Mason5caf2a02007-04-02 11:20:42 -04003812 path = btrfs_alloc_path();
3813 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05003814
Chris Mason5f39d392007-10-15 16:14:19 -04003815 level = btrfs_header_level(root->node);
Chris Mason20524f02007-03-10 06:35:47 -05003816 orig_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04003817 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
3818 path->nodes[level] = root->node;
Chris Masonf510cfe2007-10-15 16:14:48 -04003819 extent_buffer_get(root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -04003820 path->slots[level] = 0;
3821 } else {
3822 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04003823 struct btrfs_disk_key found_key;
3824 struct extent_buffer *node;
Chris Mason6702ed42007-08-07 16:15:09 -04003825
Chris Mason9f3a7422007-08-07 15:52:19 -04003826 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Chris Mason6702ed42007-08-07 16:15:09 -04003827 level = root_item->drop_level;
3828 path->lowest_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04003829 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason6702ed42007-08-07 16:15:09 -04003830 if (wret < 0) {
Chris Mason9f3a7422007-08-07 15:52:19 -04003831 ret = wret;
3832 goto out;
3833 }
Chris Mason5f39d392007-10-15 16:14:19 -04003834 node = path->nodes[level];
3835 btrfs_node_key(node, &found_key, path->slots[level]);
3836 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
3837 sizeof(found_key)));
Chris Mason7d9eb122008-07-08 14:19:17 -04003838 /*
3839 * unlock our path, this is safe because only this
3840 * function is allowed to delete this snapshot
3841 */
Chris Mason925baed2008-06-25 16:01:30 -04003842 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
3843 if (path->nodes[i] && path->locks[i]) {
3844 path->locks[i] = 0;
3845 btrfs_tree_unlock(path->nodes[i]);
3846 }
3847 }
Chris Mason9f3a7422007-08-07 15:52:19 -04003848 }
Chris Masond3977122009-01-05 21:25:51 -05003849 while (1) {
Chris Mason5caf2a02007-04-02 11:20:42 -04003850 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04003851 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05003852 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04003853 if (wret < 0)
3854 ret = wret;
3855
Yan Zhengf82d02d2008-10-29 14:49:05 -04003856 wret = walk_up_tree(trans, root, path, &level,
3857 BTRFS_MAX_LEVEL);
Chris Mason9aca1d52007-03-13 11:09:37 -04003858 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05003859 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04003860 if (wret < 0)
3861 ret = wret;
Chris Masone7a84562008-06-25 16:01:31 -04003862 if (trans->transaction->in_commit) {
3863 ret = -EAGAIN;
3864 break;
3865 }
Chris Mason18e35e0a2008-08-01 13:11:41 -04003866 atomic_inc(&root->fs_info->throttle_gen);
Chris Mason017e5362008-07-28 15:32:51 -04003867 wake_up(&root->fs_info->transaction_throttle);
Chris Mason20524f02007-03-10 06:35:47 -05003868 }
Chris Mason83e15a22007-03-12 09:03:27 -04003869 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -04003870 if (path->nodes[i]) {
Chris Mason5f39d392007-10-15 16:14:19 -04003871 free_extent_buffer(path->nodes[i]);
Chris Mason0f827312007-10-15 16:18:56 -04003872 path->nodes[i] = NULL;
Chris Mason83e15a22007-03-12 09:03:27 -04003873 }
Chris Mason20524f02007-03-10 06:35:47 -05003874 }
Chris Mason9f3a7422007-08-07 15:52:19 -04003875out:
Chris Mason5caf2a02007-04-02 11:20:42 -04003876 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -04003877 return ret;
Chris Mason20524f02007-03-10 06:35:47 -05003878}
Chris Mason9078a3e2007-04-26 16:46:15 -04003879
Yan Zhengf82d02d2008-10-29 14:49:05 -04003880int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
3881 struct btrfs_root *root,
3882 struct extent_buffer *node,
3883 struct extent_buffer *parent)
3884{
3885 struct btrfs_path *path;
3886 int level;
3887 int parent_level;
3888 int ret = 0;
3889 int wret;
3890
3891 path = btrfs_alloc_path();
3892 BUG_ON(!path);
3893
3894 BUG_ON(!btrfs_tree_locked(parent));
3895 parent_level = btrfs_header_level(parent);
3896 extent_buffer_get(parent);
3897 path->nodes[parent_level] = parent;
3898 path->slots[parent_level] = btrfs_header_nritems(parent);
3899
3900 BUG_ON(!btrfs_tree_locked(node));
3901 level = btrfs_header_level(node);
3902 extent_buffer_get(node);
3903 path->nodes[level] = node;
3904 path->slots[level] = 0;
3905
3906 while (1) {
3907 wret = walk_down_subtree(trans, root, path, &level);
3908 if (wret < 0)
3909 ret = wret;
3910 if (wret != 0)
3911 break;
3912
3913 wret = walk_up_tree(trans, root, path, &level, parent_level);
3914 if (wret < 0)
3915 ret = wret;
3916 if (wret != 0)
3917 break;
3918 }
3919
3920 btrfs_free_path(path);
3921 return ret;
3922}
3923
Chris Mason8e7bf942008-04-28 09:02:36 -04003924static unsigned long calc_ra(unsigned long start, unsigned long last,
3925 unsigned long nr)
3926{
3927 return min(last, start + nr - 1);
3928}
3929
Chris Masond3977122009-01-05 21:25:51 -05003930static noinline int relocate_inode_pages(struct inode *inode, u64 start,
Chris Mason98ed5172008-01-03 10:01:48 -05003931 u64 len)
Chris Masonedbd8d42007-12-21 16:27:24 -05003932{
3933 u64 page_start;
3934 u64 page_end;
Zheng Yan1a40e232008-09-26 10:09:34 -04003935 unsigned long first_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05003936 unsigned long last_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05003937 unsigned long i;
3938 struct page *page;
Chris Masond1310b22008-01-24 16:13:08 -05003939 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason4313b392008-01-03 09:08:48 -05003940 struct file_ra_state *ra;
Chris Mason3eaa2882008-07-24 11:57:52 -04003941 struct btrfs_ordered_extent *ordered;
Zheng Yan1a40e232008-09-26 10:09:34 -04003942 unsigned int total_read = 0;
3943 unsigned int total_dirty = 0;
3944 int ret = 0;
Chris Mason4313b392008-01-03 09:08:48 -05003945
3946 ra = kzalloc(sizeof(*ra), GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05003947
3948 mutex_lock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04003949 first_index = start >> PAGE_CACHE_SHIFT;
Chris Masonedbd8d42007-12-21 16:27:24 -05003950 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
3951
Zheng Yan1a40e232008-09-26 10:09:34 -04003952 /* make sure the dirty trick played by the caller work */
3953 ret = invalidate_inode_pages2_range(inode->i_mapping,
3954 first_index, last_index);
3955 if (ret)
3956 goto out_unlock;
Chris Mason8e7bf942008-04-28 09:02:36 -04003957
Chris Mason4313b392008-01-03 09:08:48 -05003958 file_ra_state_init(ra, inode->i_mapping);
Chris Masonedbd8d42007-12-21 16:27:24 -05003959
Zheng Yan1a40e232008-09-26 10:09:34 -04003960 for (i = first_index ; i <= last_index; i++) {
3961 if (total_read % ra->ra_pages == 0) {
Chris Mason8e7bf942008-04-28 09:02:36 -04003962 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
Zheng Yan1a40e232008-09-26 10:09:34 -04003963 calc_ra(i, last_index, ra->ra_pages));
Chris Mason8e7bf942008-04-28 09:02:36 -04003964 }
3965 total_read++;
Chris Mason3eaa2882008-07-24 11:57:52 -04003966again:
3967 if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
Zheng Yan1a40e232008-09-26 10:09:34 -04003968 BUG_ON(1);
Chris Masonedbd8d42007-12-21 16:27:24 -05003969 page = grab_cache_page(inode->i_mapping, i);
Chris Masona061fc82008-05-07 11:43:44 -04003970 if (!page) {
Zheng Yan1a40e232008-09-26 10:09:34 -04003971 ret = -ENOMEM;
Chris Masonedbd8d42007-12-21 16:27:24 -05003972 goto out_unlock;
Chris Masona061fc82008-05-07 11:43:44 -04003973 }
Chris Masonedbd8d42007-12-21 16:27:24 -05003974 if (!PageUptodate(page)) {
3975 btrfs_readpage(NULL, page);
3976 lock_page(page);
3977 if (!PageUptodate(page)) {
3978 unlock_page(page);
3979 page_cache_release(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04003980 ret = -EIO;
Chris Masonedbd8d42007-12-21 16:27:24 -05003981 goto out_unlock;
3982 }
3983 }
Chris Masonec44a352008-04-28 15:29:52 -04003984 wait_on_page_writeback(page);
Chris Mason3eaa2882008-07-24 11:57:52 -04003985
Chris Masonedbd8d42007-12-21 16:27:24 -05003986 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
3987 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05003988 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05003989
Chris Mason3eaa2882008-07-24 11:57:52 -04003990 ordered = btrfs_lookup_ordered_extent(inode, page_start);
3991 if (ordered) {
3992 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3993 unlock_page(page);
3994 page_cache_release(page);
3995 btrfs_start_ordered_extent(inode, ordered, 1);
3996 btrfs_put_ordered_extent(ordered);
3997 goto again;
3998 }
3999 set_page_extent_mapped(page);
4000
Zheng Yan1a40e232008-09-26 10:09:34 -04004001 if (i == first_index)
4002 set_extent_bits(io_tree, page_start, page_end,
4003 EXTENT_BOUNDARY, GFP_NOFS);
Yan Zheng1f80e4d2008-12-19 10:59:04 -05004004 btrfs_set_extent_delalloc(inode, page_start, page_end);
Zheng Yan1a40e232008-09-26 10:09:34 -04004005
Chris Masona061fc82008-05-07 11:43:44 -04004006 set_page_dirty(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04004007 total_dirty++;
Chris Masonedbd8d42007-12-21 16:27:24 -05004008
Chris Masond1310b22008-01-24 16:13:08 -05004009 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05004010 unlock_page(page);
4011 page_cache_release(page);
4012 }
4013
4014out_unlock:
Chris Masonec44a352008-04-28 15:29:52 -04004015 kfree(ra);
Chris Masonedbd8d42007-12-21 16:27:24 -05004016 mutex_unlock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04004017 balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty);
Chris Masonbf4ef672008-05-08 13:26:18 -04004018 return ret;
4019}
4020
Chris Masond3977122009-01-05 21:25:51 -05004021static noinline int relocate_data_extent(struct inode *reloc_inode,
Zheng Yan1a40e232008-09-26 10:09:34 -04004022 struct btrfs_key *extent_key,
4023 u64 offset)
Chris Masonedbd8d42007-12-21 16:27:24 -05004024{
Zheng Yan1a40e232008-09-26 10:09:34 -04004025 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
4026 struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree;
4027 struct extent_map *em;
Yan Zheng66435582008-10-30 14:19:50 -04004028 u64 start = extent_key->objectid - offset;
4029 u64 end = start + extent_key->offset - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04004030
4031 em = alloc_extent_map(GFP_NOFS);
4032 BUG_ON(!em || IS_ERR(em));
4033
Yan Zheng66435582008-10-30 14:19:50 -04004034 em->start = start;
Zheng Yan1a40e232008-09-26 10:09:34 -04004035 em->len = extent_key->offset;
Chris Masonc8b97812008-10-29 14:49:59 -04004036 em->block_len = extent_key->offset;
Zheng Yan1a40e232008-09-26 10:09:34 -04004037 em->block_start = extent_key->objectid;
4038 em->bdev = root->fs_info->fs_devices->latest_bdev;
4039 set_bit(EXTENT_FLAG_PINNED, &em->flags);
4040
4041 /* setup extent map to cheat btrfs_readpage */
Yan Zheng66435582008-10-30 14:19:50 -04004042 lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04004043 while (1) {
4044 int ret;
4045 spin_lock(&em_tree->lock);
4046 ret = add_extent_mapping(em_tree, em);
4047 spin_unlock(&em_tree->lock);
4048 if (ret != -EEXIST) {
4049 free_extent_map(em);
4050 break;
4051 }
Yan Zheng66435582008-10-30 14:19:50 -04004052 btrfs_drop_extent_cache(reloc_inode, start, end, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04004053 }
Yan Zheng66435582008-10-30 14:19:50 -04004054 unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04004055
Yan Zheng66435582008-10-30 14:19:50 -04004056 return relocate_inode_pages(reloc_inode, start, extent_key->offset);
Zheng Yan1a40e232008-09-26 10:09:34 -04004057}
4058
4059struct btrfs_ref_path {
4060 u64 extent_start;
4061 u64 nodes[BTRFS_MAX_LEVEL];
4062 u64 root_objectid;
4063 u64 root_generation;
4064 u64 owner_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04004065 u32 num_refs;
4066 int lowest_level;
4067 int current_level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004068 int shared_level;
4069
4070 struct btrfs_key node_keys[BTRFS_MAX_LEVEL];
4071 u64 new_nodes[BTRFS_MAX_LEVEL];
Zheng Yan1a40e232008-09-26 10:09:34 -04004072};
4073
4074struct disk_extent {
Chris Masonc8b97812008-10-29 14:49:59 -04004075 u64 ram_bytes;
Zheng Yan1a40e232008-09-26 10:09:34 -04004076 u64 disk_bytenr;
4077 u64 disk_num_bytes;
4078 u64 offset;
4079 u64 num_bytes;
Chris Masonc8b97812008-10-29 14:49:59 -04004080 u8 compression;
4081 u8 encryption;
4082 u16 other_encoding;
Zheng Yan1a40e232008-09-26 10:09:34 -04004083};
4084
4085static int is_cowonly_root(u64 root_objectid)
4086{
4087 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
4088 root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
4089 root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
4090 root_objectid == BTRFS_DEV_TREE_OBJECTID ||
Yan Zheng0403e472008-12-10 20:32:51 -05004091 root_objectid == BTRFS_TREE_LOG_OBJECTID ||
4092 root_objectid == BTRFS_CSUM_TREE_OBJECTID)
Zheng Yan1a40e232008-09-26 10:09:34 -04004093 return 1;
4094 return 0;
4095}
4096
Chris Masond3977122009-01-05 21:25:51 -05004097static noinline int __next_ref_path(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04004098 struct btrfs_root *extent_root,
4099 struct btrfs_ref_path *ref_path,
4100 int first_time)
4101{
4102 struct extent_buffer *leaf;
4103 struct btrfs_path *path;
Chris Mason4313b392008-01-03 09:08:48 -05004104 struct btrfs_extent_ref *ref;
Chris Masonedbd8d42007-12-21 16:27:24 -05004105 struct btrfs_key key;
4106 struct btrfs_key found_key;
Zheng Yan1a40e232008-09-26 10:09:34 -04004107 u64 bytenr;
Chris Masonedbd8d42007-12-21 16:27:24 -05004108 u32 nritems;
Zheng Yan1a40e232008-09-26 10:09:34 -04004109 int level;
4110 int ret = 1;
Chris Masonedbd8d42007-12-21 16:27:24 -05004111
Zheng Yan1a40e232008-09-26 10:09:34 -04004112 path = btrfs_alloc_path();
4113 if (!path)
4114 return -ENOMEM;
4115
Zheng Yan1a40e232008-09-26 10:09:34 -04004116 if (first_time) {
4117 ref_path->lowest_level = -1;
4118 ref_path->current_level = -1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004119 ref_path->shared_level = -1;
Zheng Yan1a40e232008-09-26 10:09:34 -04004120 goto walk_up;
Chris Masona061fc82008-05-07 11:43:44 -04004121 }
Zheng Yan1a40e232008-09-26 10:09:34 -04004122walk_down:
4123 level = ref_path->current_level - 1;
4124 while (level >= -1) {
4125 u64 parent;
4126 if (level < ref_path->lowest_level)
4127 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05004128
Chris Masond3977122009-01-05 21:25:51 -05004129 if (level >= 0)
Zheng Yan1a40e232008-09-26 10:09:34 -04004130 bytenr = ref_path->nodes[level];
Chris Masond3977122009-01-05 21:25:51 -05004131 else
Zheng Yan1a40e232008-09-26 10:09:34 -04004132 bytenr = ref_path->extent_start;
Zheng Yan1a40e232008-09-26 10:09:34 -04004133 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05004134
Zheng Yan1a40e232008-09-26 10:09:34 -04004135 parent = ref_path->nodes[level + 1];
4136 ref_path->nodes[level + 1] = 0;
4137 ref_path->current_level = level;
4138 BUG_ON(parent == 0);
4139
4140 key.objectid = bytenr;
4141 key.offset = parent + 1;
4142 key.type = BTRFS_EXTENT_REF_KEY;
4143
4144 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05004145 if (ret < 0)
4146 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04004147 BUG_ON(ret == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05004148
Chris Masonedbd8d42007-12-21 16:27:24 -05004149 leaf = path->nodes[0];
4150 nritems = btrfs_header_nritems(leaf);
Zheng Yan1a40e232008-09-26 10:09:34 -04004151 if (path->slots[0] >= nritems) {
Chris Masona061fc82008-05-07 11:43:44 -04004152 ret = btrfs_next_leaf(extent_root, path);
Chris Masona061fc82008-05-07 11:43:44 -04004153 if (ret < 0)
4154 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04004155 if (ret > 0)
4156 goto next;
Chris Masonbf4ef672008-05-08 13:26:18 -04004157 leaf = path->nodes[0];
Chris Masona061fc82008-05-07 11:43:44 -04004158 }
Chris Masonedbd8d42007-12-21 16:27:24 -05004159
4160 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Zheng Yan1a40e232008-09-26 10:09:34 -04004161 if (found_key.objectid == bytenr &&
Yan Zhengf82d02d2008-10-29 14:49:05 -04004162 found_key.type == BTRFS_EXTENT_REF_KEY) {
4163 if (level < ref_path->shared_level)
4164 ref_path->shared_level = level;
Zheng Yan1a40e232008-09-26 10:09:34 -04004165 goto found;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004166 }
Zheng Yan1a40e232008-09-26 10:09:34 -04004167next:
4168 level--;
4169 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04004170 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04004171 }
4172 /* reached lowest level */
4173 ret = 1;
4174 goto out;
4175walk_up:
4176 level = ref_path->current_level;
4177 while (level < BTRFS_MAX_LEVEL - 1) {
4178 u64 ref_objectid;
Chris Masond3977122009-01-05 21:25:51 -05004179
4180 if (level >= 0)
Zheng Yan1a40e232008-09-26 10:09:34 -04004181 bytenr = ref_path->nodes[level];
Chris Masond3977122009-01-05 21:25:51 -05004182 else
Zheng Yan1a40e232008-09-26 10:09:34 -04004183 bytenr = ref_path->extent_start;
Chris Masond3977122009-01-05 21:25:51 -05004184
Zheng Yan1a40e232008-09-26 10:09:34 -04004185 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05004186
Zheng Yan1a40e232008-09-26 10:09:34 -04004187 key.objectid = bytenr;
4188 key.offset = 0;
4189 key.type = BTRFS_EXTENT_REF_KEY;
Chris Masonedbd8d42007-12-21 16:27:24 -05004190
Zheng Yan1a40e232008-09-26 10:09:34 -04004191 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
4192 if (ret < 0)
Chris Masonedbd8d42007-12-21 16:27:24 -05004193 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04004194
4195 leaf = path->nodes[0];
4196 nritems = btrfs_header_nritems(leaf);
4197 if (path->slots[0] >= nritems) {
4198 ret = btrfs_next_leaf(extent_root, path);
4199 if (ret < 0)
4200 goto out;
4201 if (ret > 0) {
4202 /* the extent was freed by someone */
4203 if (ref_path->lowest_level == level)
4204 goto out;
4205 btrfs_release_path(extent_root, path);
4206 goto walk_down;
4207 }
4208 leaf = path->nodes[0];
4209 }
4210
4211 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4212 if (found_key.objectid != bytenr ||
4213 found_key.type != BTRFS_EXTENT_REF_KEY) {
4214 /* the extent was freed by someone */
4215 if (ref_path->lowest_level == level) {
4216 ret = 1;
4217 goto out;
4218 }
4219 btrfs_release_path(extent_root, path);
4220 goto walk_down;
4221 }
4222found:
4223 ref = btrfs_item_ptr(leaf, path->slots[0],
4224 struct btrfs_extent_ref);
4225 ref_objectid = btrfs_ref_objectid(leaf, ref);
4226 if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) {
4227 if (first_time) {
4228 level = (int)ref_objectid;
4229 BUG_ON(level >= BTRFS_MAX_LEVEL);
4230 ref_path->lowest_level = level;
4231 ref_path->current_level = level;
4232 ref_path->nodes[level] = bytenr;
4233 } else {
4234 WARN_ON(ref_objectid != level);
4235 }
4236 } else {
4237 WARN_ON(level != -1);
4238 }
4239 first_time = 0;
4240
4241 if (ref_path->lowest_level == level) {
4242 ref_path->owner_objectid = ref_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04004243 ref_path->num_refs = btrfs_ref_num_refs(leaf, ref);
4244 }
4245
4246 /*
4247 * the block is tree root or the block isn't in reference
4248 * counted tree.
4249 */
4250 if (found_key.objectid == found_key.offset ||
4251 is_cowonly_root(btrfs_ref_root(leaf, ref))) {
4252 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
4253 ref_path->root_generation =
4254 btrfs_ref_generation(leaf, ref);
4255 if (level < 0) {
4256 /* special reference from the tree log */
4257 ref_path->nodes[0] = found_key.offset;
4258 ref_path->current_level = 0;
4259 }
4260 ret = 0;
4261 goto out;
4262 }
4263
4264 level++;
4265 BUG_ON(ref_path->nodes[level] != 0);
4266 ref_path->nodes[level] = found_key.offset;
4267 ref_path->current_level = level;
4268
4269 /*
4270 * the reference was created in the running transaction,
4271 * no need to continue walking up.
4272 */
4273 if (btrfs_ref_generation(leaf, ref) == trans->transid) {
4274 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
4275 ref_path->root_generation =
4276 btrfs_ref_generation(leaf, ref);
4277 ret = 0;
4278 goto out;
4279 }
4280
4281 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04004282 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04004283 }
4284 /* reached max tree level, but no tree root found. */
4285 BUG();
4286out:
Zheng Yan1a40e232008-09-26 10:09:34 -04004287 btrfs_free_path(path);
4288 return ret;
4289}
4290
4291static int btrfs_first_ref_path(struct btrfs_trans_handle *trans,
4292 struct btrfs_root *extent_root,
4293 struct btrfs_ref_path *ref_path,
4294 u64 extent_start)
4295{
4296 memset(ref_path, 0, sizeof(*ref_path));
4297 ref_path->extent_start = extent_start;
4298
4299 return __next_ref_path(trans, extent_root, ref_path, 1);
4300}
4301
4302static int btrfs_next_ref_path(struct btrfs_trans_handle *trans,
4303 struct btrfs_root *extent_root,
4304 struct btrfs_ref_path *ref_path)
4305{
4306 return __next_ref_path(trans, extent_root, ref_path, 0);
4307}
4308
Chris Masond3977122009-01-05 21:25:51 -05004309static noinline int get_new_locations(struct inode *reloc_inode,
Zheng Yan1a40e232008-09-26 10:09:34 -04004310 struct btrfs_key *extent_key,
4311 u64 offset, int no_fragment,
4312 struct disk_extent **extents,
4313 int *nr_extents)
4314{
4315 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
4316 struct btrfs_path *path;
4317 struct btrfs_file_extent_item *fi;
4318 struct extent_buffer *leaf;
4319 struct disk_extent *exts = *extents;
4320 struct btrfs_key found_key;
4321 u64 cur_pos;
4322 u64 last_byte;
4323 u32 nritems;
4324 int nr = 0;
4325 int max = *nr_extents;
4326 int ret;
4327
4328 WARN_ON(!no_fragment && *extents);
4329 if (!exts) {
4330 max = 1;
4331 exts = kmalloc(sizeof(*exts) * max, GFP_NOFS);
4332 if (!exts)
4333 return -ENOMEM;
4334 }
4335
4336 path = btrfs_alloc_path();
4337 BUG_ON(!path);
4338
4339 cur_pos = extent_key->objectid - offset;
4340 last_byte = extent_key->objectid + extent_key->offset;
4341 ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino,
4342 cur_pos, 0);
4343 if (ret < 0)
4344 goto out;
4345 if (ret > 0) {
4346 ret = -ENOENT;
4347 goto out;
4348 }
4349
4350 while (1) {
4351 leaf = path->nodes[0];
4352 nritems = btrfs_header_nritems(leaf);
4353 if (path->slots[0] >= nritems) {
4354 ret = btrfs_next_leaf(root, path);
4355 if (ret < 0)
4356 goto out;
4357 if (ret > 0)
4358 break;
4359 leaf = path->nodes[0];
4360 }
4361
4362 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4363 if (found_key.offset != cur_pos ||
4364 found_key.type != BTRFS_EXTENT_DATA_KEY ||
4365 found_key.objectid != reloc_inode->i_ino)
4366 break;
4367
4368 fi = btrfs_item_ptr(leaf, path->slots[0],
4369 struct btrfs_file_extent_item);
4370 if (btrfs_file_extent_type(leaf, fi) !=
4371 BTRFS_FILE_EXTENT_REG ||
4372 btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
4373 break;
4374
4375 if (nr == max) {
4376 struct disk_extent *old = exts;
4377 max *= 2;
4378 exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
4379 memcpy(exts, old, sizeof(*exts) * nr);
4380 if (old != *extents)
4381 kfree(old);
4382 }
4383
4384 exts[nr].disk_bytenr =
4385 btrfs_file_extent_disk_bytenr(leaf, fi);
4386 exts[nr].disk_num_bytes =
4387 btrfs_file_extent_disk_num_bytes(leaf, fi);
4388 exts[nr].offset = btrfs_file_extent_offset(leaf, fi);
4389 exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
Chris Masonc8b97812008-10-29 14:49:59 -04004390 exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
4391 exts[nr].compression = btrfs_file_extent_compression(leaf, fi);
4392 exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi);
4393 exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf,
4394 fi);
Yan Zhengd899e052008-10-30 14:25:28 -04004395 BUG_ON(exts[nr].offset > 0);
4396 BUG_ON(exts[nr].compression || exts[nr].encryption);
4397 BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04004398
4399 cur_pos += exts[nr].num_bytes;
4400 nr++;
4401
4402 if (cur_pos + offset >= last_byte)
4403 break;
4404
4405 if (no_fragment) {
4406 ret = 1;
4407 goto out;
4408 }
4409 path->slots[0]++;
4410 }
4411
Yan Zheng1f80e4d2008-12-19 10:59:04 -05004412 BUG_ON(cur_pos + offset > last_byte);
Zheng Yan1a40e232008-09-26 10:09:34 -04004413 if (cur_pos + offset < last_byte) {
4414 ret = -ENOENT;
4415 goto out;
Chris Masonedbd8d42007-12-21 16:27:24 -05004416 }
4417 ret = 0;
4418out:
Zheng Yan1a40e232008-09-26 10:09:34 -04004419 btrfs_free_path(path);
4420 if (ret) {
4421 if (exts != *extents)
4422 kfree(exts);
4423 } else {
4424 *extents = exts;
4425 *nr_extents = nr;
4426 }
4427 return ret;
4428}
4429
Chris Masond3977122009-01-05 21:25:51 -05004430static noinline int replace_one_extent(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04004431 struct btrfs_root *root,
4432 struct btrfs_path *path,
4433 struct btrfs_key *extent_key,
4434 struct btrfs_key *leaf_key,
4435 struct btrfs_ref_path *ref_path,
4436 struct disk_extent *new_extents,
4437 int nr_extents)
4438{
4439 struct extent_buffer *leaf;
4440 struct btrfs_file_extent_item *fi;
4441 struct inode *inode = NULL;
4442 struct btrfs_key key;
4443 u64 lock_start = 0;
4444 u64 lock_end = 0;
4445 u64 num_bytes;
4446 u64 ext_offset;
4447 u64 first_pos;
4448 u32 nritems;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004449 int nr_scaned = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04004450 int extent_locked = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04004451 int extent_type;
Zheng Yan1a40e232008-09-26 10:09:34 -04004452 int ret;
4453
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004454 memcpy(&key, leaf_key, sizeof(key));
4455 first_pos = INT_LIMIT(loff_t) - extent_key->offset;
Zheng Yan1a40e232008-09-26 10:09:34 -04004456 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004457 if (key.objectid < ref_path->owner_objectid ||
4458 (key.objectid == ref_path->owner_objectid &&
4459 key.type < BTRFS_EXTENT_DATA_KEY)) {
4460 key.objectid = ref_path->owner_objectid;
4461 key.type = BTRFS_EXTENT_DATA_KEY;
4462 key.offset = 0;
4463 }
Zheng Yan1a40e232008-09-26 10:09:34 -04004464 }
4465
4466 while (1) {
4467 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
4468 if (ret < 0)
4469 goto out;
4470
4471 leaf = path->nodes[0];
4472 nritems = btrfs_header_nritems(leaf);
4473next:
4474 if (extent_locked && ret > 0) {
4475 /*
4476 * the file extent item was modified by someone
4477 * before the extent got locked.
4478 */
Zheng Yan1a40e232008-09-26 10:09:34 -04004479 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4480 lock_end, GFP_NOFS);
4481 extent_locked = 0;
4482 }
4483
4484 if (path->slots[0] >= nritems) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004485 if (++nr_scaned > 2)
Zheng Yan1a40e232008-09-26 10:09:34 -04004486 break;
4487
4488 BUG_ON(extent_locked);
4489 ret = btrfs_next_leaf(root, path);
4490 if (ret < 0)
4491 goto out;
4492 if (ret > 0)
4493 break;
4494 leaf = path->nodes[0];
4495 nritems = btrfs_header_nritems(leaf);
4496 }
4497
4498 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4499
4500 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
4501 if ((key.objectid > ref_path->owner_objectid) ||
4502 (key.objectid == ref_path->owner_objectid &&
4503 key.type > BTRFS_EXTENT_DATA_KEY) ||
4504 (key.offset >= first_pos + extent_key->offset))
4505 break;
4506 }
4507
4508 if (inode && key.objectid != inode->i_ino) {
4509 BUG_ON(extent_locked);
4510 btrfs_release_path(root, path);
4511 mutex_unlock(&inode->i_mutex);
4512 iput(inode);
4513 inode = NULL;
4514 continue;
4515 }
4516
4517 if (key.type != BTRFS_EXTENT_DATA_KEY) {
4518 path->slots[0]++;
4519 ret = 1;
4520 goto next;
4521 }
4522 fi = btrfs_item_ptr(leaf, path->slots[0],
4523 struct btrfs_file_extent_item);
Yan Zhengd899e052008-10-30 14:25:28 -04004524 extent_type = btrfs_file_extent_type(leaf, fi);
4525 if ((extent_type != BTRFS_FILE_EXTENT_REG &&
4526 extent_type != BTRFS_FILE_EXTENT_PREALLOC) ||
Zheng Yan1a40e232008-09-26 10:09:34 -04004527 (btrfs_file_extent_disk_bytenr(leaf, fi) !=
4528 extent_key->objectid)) {
4529 path->slots[0]++;
4530 ret = 1;
4531 goto next;
4532 }
4533
4534 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
4535 ext_offset = btrfs_file_extent_offset(leaf, fi);
4536
4537 if (first_pos > key.offset - ext_offset)
4538 first_pos = key.offset - ext_offset;
4539
4540 if (!extent_locked) {
4541 lock_start = key.offset;
4542 lock_end = lock_start + num_bytes - 1;
4543 } else {
Yan Zheng66435582008-10-30 14:19:50 -04004544 if (lock_start > key.offset ||
4545 lock_end + 1 < key.offset + num_bytes) {
4546 unlock_extent(&BTRFS_I(inode)->io_tree,
4547 lock_start, lock_end, GFP_NOFS);
4548 extent_locked = 0;
4549 }
Zheng Yan1a40e232008-09-26 10:09:34 -04004550 }
4551
4552 if (!inode) {
4553 btrfs_release_path(root, path);
4554
4555 inode = btrfs_iget_locked(root->fs_info->sb,
4556 key.objectid, root);
4557 if (inode->i_state & I_NEW) {
4558 BTRFS_I(inode)->root = root;
4559 BTRFS_I(inode)->location.objectid =
4560 key.objectid;
4561 BTRFS_I(inode)->location.type =
4562 BTRFS_INODE_ITEM_KEY;
4563 BTRFS_I(inode)->location.offset = 0;
4564 btrfs_read_locked_inode(inode);
4565 unlock_new_inode(inode);
4566 }
4567 /*
4568 * some code call btrfs_commit_transaction while
4569 * holding the i_mutex, so we can't use mutex_lock
4570 * here.
4571 */
4572 if (is_bad_inode(inode) ||
4573 !mutex_trylock(&inode->i_mutex)) {
4574 iput(inode);
4575 inode = NULL;
4576 key.offset = (u64)-1;
4577 goto skip;
4578 }
4579 }
4580
4581 if (!extent_locked) {
4582 struct btrfs_ordered_extent *ordered;
4583
4584 btrfs_release_path(root, path);
4585
4586 lock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4587 lock_end, GFP_NOFS);
4588 ordered = btrfs_lookup_first_ordered_extent(inode,
4589 lock_end);
4590 if (ordered &&
4591 ordered->file_offset <= lock_end &&
4592 ordered->file_offset + ordered->len > lock_start) {
4593 unlock_extent(&BTRFS_I(inode)->io_tree,
4594 lock_start, lock_end, GFP_NOFS);
4595 btrfs_start_ordered_extent(inode, ordered, 1);
4596 btrfs_put_ordered_extent(ordered);
4597 key.offset += num_bytes;
4598 goto skip;
4599 }
4600 if (ordered)
4601 btrfs_put_ordered_extent(ordered);
4602
Zheng Yan1a40e232008-09-26 10:09:34 -04004603 extent_locked = 1;
4604 continue;
4605 }
4606
4607 if (nr_extents == 1) {
4608 /* update extent pointer in place */
Zheng Yan1a40e232008-09-26 10:09:34 -04004609 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4610 new_extents[0].disk_bytenr);
4611 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4612 new_extents[0].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04004613 btrfs_mark_buffer_dirty(leaf);
4614
4615 btrfs_drop_extent_cache(inode, key.offset,
4616 key.offset + num_bytes - 1, 0);
4617
4618 ret = btrfs_inc_extent_ref(trans, root,
4619 new_extents[0].disk_bytenr,
4620 new_extents[0].disk_num_bytes,
4621 leaf->start,
4622 root->root_key.objectid,
4623 trans->transid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004624 key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04004625 BUG_ON(ret);
4626
4627 ret = btrfs_free_extent(trans, root,
4628 extent_key->objectid,
4629 extent_key->offset,
4630 leaf->start,
4631 btrfs_header_owner(leaf),
4632 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004633 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04004634 BUG_ON(ret);
4635
4636 btrfs_release_path(root, path);
4637 key.offset += num_bytes;
4638 } else {
Yan Zhengd899e052008-10-30 14:25:28 -04004639 BUG_ON(1);
4640#if 0
Zheng Yan1a40e232008-09-26 10:09:34 -04004641 u64 alloc_hint;
4642 u64 extent_len;
4643 int i;
4644 /*
4645 * drop old extent pointer at first, then insert the
4646 * new pointers one bye one
4647 */
4648 btrfs_release_path(root, path);
4649 ret = btrfs_drop_extents(trans, root, inode, key.offset,
4650 key.offset + num_bytes,
4651 key.offset, &alloc_hint);
4652 BUG_ON(ret);
4653
4654 for (i = 0; i < nr_extents; i++) {
4655 if (ext_offset >= new_extents[i].num_bytes) {
4656 ext_offset -= new_extents[i].num_bytes;
4657 continue;
4658 }
4659 extent_len = min(new_extents[i].num_bytes -
4660 ext_offset, num_bytes);
4661
4662 ret = btrfs_insert_empty_item(trans, root,
4663 path, &key,
4664 sizeof(*fi));
4665 BUG_ON(ret);
4666
4667 leaf = path->nodes[0];
4668 fi = btrfs_item_ptr(leaf, path->slots[0],
4669 struct btrfs_file_extent_item);
4670 btrfs_set_file_extent_generation(leaf, fi,
4671 trans->transid);
4672 btrfs_set_file_extent_type(leaf, fi,
4673 BTRFS_FILE_EXTENT_REG);
4674 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4675 new_extents[i].disk_bytenr);
4676 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4677 new_extents[i].disk_num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -04004678 btrfs_set_file_extent_ram_bytes(leaf, fi,
4679 new_extents[i].ram_bytes);
4680
4681 btrfs_set_file_extent_compression(leaf, fi,
4682 new_extents[i].compression);
4683 btrfs_set_file_extent_encryption(leaf, fi,
4684 new_extents[i].encryption);
4685 btrfs_set_file_extent_other_encoding(leaf, fi,
4686 new_extents[i].other_encoding);
4687
Zheng Yan1a40e232008-09-26 10:09:34 -04004688 btrfs_set_file_extent_num_bytes(leaf, fi,
4689 extent_len);
4690 ext_offset += new_extents[i].offset;
4691 btrfs_set_file_extent_offset(leaf, fi,
4692 ext_offset);
4693 btrfs_mark_buffer_dirty(leaf);
4694
4695 btrfs_drop_extent_cache(inode, key.offset,
4696 key.offset + extent_len - 1, 0);
4697
4698 ret = btrfs_inc_extent_ref(trans, root,
4699 new_extents[i].disk_bytenr,
4700 new_extents[i].disk_num_bytes,
4701 leaf->start,
4702 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004703 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04004704 BUG_ON(ret);
4705 btrfs_release_path(root, path);
4706
Yan Zhenga76a3cd2008-10-09 11:46:29 -04004707 inode_add_bytes(inode, extent_len);
Zheng Yan1a40e232008-09-26 10:09:34 -04004708
4709 ext_offset = 0;
4710 num_bytes -= extent_len;
4711 key.offset += extent_len;
4712
4713 if (num_bytes == 0)
4714 break;
4715 }
4716 BUG_ON(i >= nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04004717#endif
Zheng Yan1a40e232008-09-26 10:09:34 -04004718 }
4719
4720 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04004721 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4722 lock_end, GFP_NOFS);
4723 extent_locked = 0;
4724 }
4725skip:
4726 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS &&
4727 key.offset >= first_pos + extent_key->offset)
4728 break;
4729
4730 cond_resched();
4731 }
4732 ret = 0;
4733out:
4734 btrfs_release_path(root, path);
4735 if (inode) {
4736 mutex_unlock(&inode->i_mutex);
4737 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04004738 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
4739 lock_end, GFP_NOFS);
4740 }
4741 iput(inode);
4742 }
4743 return ret;
4744}
4745
Zheng Yan1a40e232008-09-26 10:09:34 -04004746int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans,
4747 struct btrfs_root *root,
4748 struct extent_buffer *buf, u64 orig_start)
4749{
4750 int level;
4751 int ret;
4752
4753 BUG_ON(btrfs_header_generation(buf) != trans->transid);
4754 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
4755
4756 level = btrfs_header_level(buf);
4757 if (level == 0) {
4758 struct btrfs_leaf_ref *ref;
4759 struct btrfs_leaf_ref *orig_ref;
4760
4761 orig_ref = btrfs_lookup_leaf_ref(root, orig_start);
4762 if (!orig_ref)
4763 return -ENOENT;
4764
4765 ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems);
4766 if (!ref) {
4767 btrfs_free_leaf_ref(root, orig_ref);
4768 return -ENOMEM;
4769 }
4770
4771 ref->nritems = orig_ref->nritems;
4772 memcpy(ref->extents, orig_ref->extents,
4773 sizeof(ref->extents[0]) * ref->nritems);
4774
4775 btrfs_free_leaf_ref(root, orig_ref);
4776
4777 ref->root_gen = trans->transid;
4778 ref->bytenr = buf->start;
4779 ref->owner = btrfs_header_owner(buf);
4780 ref->generation = btrfs_header_generation(buf);
4781 ret = btrfs_add_leaf_ref(root, ref, 0);
4782 WARN_ON(ret);
4783 btrfs_free_leaf_ref(root, ref);
4784 }
4785 return 0;
4786}
4787
Chris Masond3977122009-01-05 21:25:51 -05004788static noinline int invalidate_extent_cache(struct btrfs_root *root,
Zheng Yan1a40e232008-09-26 10:09:34 -04004789 struct extent_buffer *leaf,
4790 struct btrfs_block_group_cache *group,
4791 struct btrfs_root *target_root)
4792{
4793 struct btrfs_key key;
4794 struct inode *inode = NULL;
4795 struct btrfs_file_extent_item *fi;
4796 u64 num_bytes;
4797 u64 skip_objectid = 0;
4798 u32 nritems;
4799 u32 i;
4800
4801 nritems = btrfs_header_nritems(leaf);
4802 for (i = 0; i < nritems; i++) {
4803 btrfs_item_key_to_cpu(leaf, &key, i);
4804 if (key.objectid == skip_objectid ||
4805 key.type != BTRFS_EXTENT_DATA_KEY)
4806 continue;
4807 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
4808 if (btrfs_file_extent_type(leaf, fi) ==
4809 BTRFS_FILE_EXTENT_INLINE)
4810 continue;
4811 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
4812 continue;
4813 if (!inode || inode->i_ino != key.objectid) {
4814 iput(inode);
4815 inode = btrfs_ilookup(target_root->fs_info->sb,
4816 key.objectid, target_root, 1);
4817 }
4818 if (!inode) {
4819 skip_objectid = key.objectid;
4820 continue;
4821 }
4822 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
4823
4824 lock_extent(&BTRFS_I(inode)->io_tree, key.offset,
4825 key.offset + num_bytes - 1, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04004826 btrfs_drop_extent_cache(inode, key.offset,
4827 key.offset + num_bytes - 1, 1);
Zheng Yan1a40e232008-09-26 10:09:34 -04004828 unlock_extent(&BTRFS_I(inode)->io_tree, key.offset,
4829 key.offset + num_bytes - 1, GFP_NOFS);
4830 cond_resched();
4831 }
4832 iput(inode);
4833 return 0;
4834}
4835
Chris Masond3977122009-01-05 21:25:51 -05004836static noinline int replace_extents_in_leaf(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04004837 struct btrfs_root *root,
4838 struct extent_buffer *leaf,
4839 struct btrfs_block_group_cache *group,
4840 struct inode *reloc_inode)
4841{
4842 struct btrfs_key key;
4843 struct btrfs_key extent_key;
4844 struct btrfs_file_extent_item *fi;
4845 struct btrfs_leaf_ref *ref;
4846 struct disk_extent *new_extent;
4847 u64 bytenr;
4848 u64 num_bytes;
4849 u32 nritems;
4850 u32 i;
4851 int ext_index;
4852 int nr_extent;
4853 int ret;
4854
4855 new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
4856 BUG_ON(!new_extent);
4857
4858 ref = btrfs_lookup_leaf_ref(root, leaf->start);
4859 BUG_ON(!ref);
4860
4861 ext_index = -1;
4862 nritems = btrfs_header_nritems(leaf);
4863 for (i = 0; i < nritems; i++) {
4864 btrfs_item_key_to_cpu(leaf, &key, i);
4865 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
4866 continue;
4867 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
4868 if (btrfs_file_extent_type(leaf, fi) ==
4869 BTRFS_FILE_EXTENT_INLINE)
4870 continue;
4871 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
4872 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
4873 if (bytenr == 0)
4874 continue;
4875
4876 ext_index++;
4877 if (bytenr >= group->key.objectid + group->key.offset ||
4878 bytenr + num_bytes <= group->key.objectid)
4879 continue;
4880
4881 extent_key.objectid = bytenr;
4882 extent_key.offset = num_bytes;
4883 extent_key.type = BTRFS_EXTENT_ITEM_KEY;
4884 nr_extent = 1;
4885 ret = get_new_locations(reloc_inode, &extent_key,
4886 group->key.objectid, 1,
4887 &new_extent, &nr_extent);
4888 if (ret > 0)
4889 continue;
4890 BUG_ON(ret < 0);
4891
4892 BUG_ON(ref->extents[ext_index].bytenr != bytenr);
4893 BUG_ON(ref->extents[ext_index].num_bytes != num_bytes);
4894 ref->extents[ext_index].bytenr = new_extent->disk_bytenr;
4895 ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes;
4896
Zheng Yan1a40e232008-09-26 10:09:34 -04004897 btrfs_set_file_extent_disk_bytenr(leaf, fi,
4898 new_extent->disk_bytenr);
4899 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
4900 new_extent->disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04004901 btrfs_mark_buffer_dirty(leaf);
4902
4903 ret = btrfs_inc_extent_ref(trans, root,
4904 new_extent->disk_bytenr,
4905 new_extent->disk_num_bytes,
4906 leaf->start,
4907 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004908 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04004909 BUG_ON(ret);
4910 ret = btrfs_free_extent(trans, root,
4911 bytenr, num_bytes, leaf->start,
4912 btrfs_header_owner(leaf),
4913 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004914 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04004915 BUG_ON(ret);
4916 cond_resched();
4917 }
4918 kfree(new_extent);
4919 BUG_ON(ext_index + 1 != ref->nritems);
4920 btrfs_free_leaf_ref(root, ref);
4921 return 0;
4922}
4923
Yan Zhengf82d02d2008-10-29 14:49:05 -04004924int btrfs_free_reloc_root(struct btrfs_trans_handle *trans,
4925 struct btrfs_root *root)
Zheng Yan1a40e232008-09-26 10:09:34 -04004926{
4927 struct btrfs_root *reloc_root;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004928 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04004929
4930 if (root->reloc_root) {
4931 reloc_root = root->reloc_root;
4932 root->reloc_root = NULL;
4933 list_add(&reloc_root->dead_list,
4934 &root->fs_info->dead_reloc_roots);
Yan Zhengf82d02d2008-10-29 14:49:05 -04004935
4936 btrfs_set_root_bytenr(&reloc_root->root_item,
4937 reloc_root->node->start);
4938 btrfs_set_root_level(&root->root_item,
4939 btrfs_header_level(reloc_root->node));
4940 memset(&reloc_root->root_item.drop_progress, 0,
4941 sizeof(struct btrfs_disk_key));
4942 reloc_root->root_item.drop_level = 0;
4943
4944 ret = btrfs_update_root(trans, root->fs_info->tree_root,
4945 &reloc_root->root_key,
4946 &reloc_root->root_item);
4947 BUG_ON(ret);
Zheng Yan1a40e232008-09-26 10:09:34 -04004948 }
4949 return 0;
4950}
4951
4952int btrfs_drop_dead_reloc_roots(struct btrfs_root *root)
4953{
4954 struct btrfs_trans_handle *trans;
4955 struct btrfs_root *reloc_root;
4956 struct btrfs_root *prev_root = NULL;
4957 struct list_head dead_roots;
4958 int ret;
4959 unsigned long nr;
4960
4961 INIT_LIST_HEAD(&dead_roots);
4962 list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots);
4963
4964 while (!list_empty(&dead_roots)) {
4965 reloc_root = list_entry(dead_roots.prev,
4966 struct btrfs_root, dead_list);
4967 list_del_init(&reloc_root->dead_list);
4968
4969 BUG_ON(reloc_root->commit_root != NULL);
4970 while (1) {
4971 trans = btrfs_join_transaction(root, 1);
4972 BUG_ON(!trans);
4973
4974 mutex_lock(&root->fs_info->drop_mutex);
4975 ret = btrfs_drop_snapshot(trans, reloc_root);
4976 if (ret != -EAGAIN)
4977 break;
4978 mutex_unlock(&root->fs_info->drop_mutex);
4979
4980 nr = trans->blocks_used;
4981 ret = btrfs_end_transaction(trans, root);
4982 BUG_ON(ret);
4983 btrfs_btree_balance_dirty(root, nr);
4984 }
4985
4986 free_extent_buffer(reloc_root->node);
4987
4988 ret = btrfs_del_root(trans, root->fs_info->tree_root,
4989 &reloc_root->root_key);
4990 BUG_ON(ret);
4991 mutex_unlock(&root->fs_info->drop_mutex);
4992
4993 nr = trans->blocks_used;
4994 ret = btrfs_end_transaction(trans, root);
4995 BUG_ON(ret);
4996 btrfs_btree_balance_dirty(root, nr);
4997
4998 kfree(prev_root);
4999 prev_root = reloc_root;
5000 }
5001 if (prev_root) {
5002 btrfs_remove_leaf_refs(prev_root, (u64)-1, 0);
5003 kfree(prev_root);
5004 }
5005 return 0;
5006}
5007
5008int btrfs_add_dead_reloc_root(struct btrfs_root *root)
5009{
5010 list_add(&root->dead_list, &root->fs_info->dead_reloc_roots);
5011 return 0;
5012}
5013
5014int btrfs_cleanup_reloc_trees(struct btrfs_root *root)
5015{
5016 struct btrfs_root *reloc_root;
5017 struct btrfs_trans_handle *trans;
5018 struct btrfs_key location;
5019 int found;
5020 int ret;
5021
5022 mutex_lock(&root->fs_info->tree_reloc_mutex);
5023 ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL);
5024 BUG_ON(ret);
5025 found = !list_empty(&root->fs_info->dead_reloc_roots);
5026 mutex_unlock(&root->fs_info->tree_reloc_mutex);
5027
5028 if (found) {
5029 trans = btrfs_start_transaction(root, 1);
5030 BUG_ON(!trans);
5031 ret = btrfs_commit_transaction(trans, root);
5032 BUG_ON(ret);
5033 }
5034
5035 location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
5036 location.offset = (u64)-1;
5037 location.type = BTRFS_ROOT_ITEM_KEY;
5038
5039 reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
5040 BUG_ON(!reloc_root);
5041 btrfs_orphan_cleanup(reloc_root);
5042 return 0;
5043}
5044
Chris Masond3977122009-01-05 21:25:51 -05005045static noinline int init_reloc_tree(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04005046 struct btrfs_root *root)
5047{
5048 struct btrfs_root *reloc_root;
5049 struct extent_buffer *eb;
5050 struct btrfs_root_item *root_item;
5051 struct btrfs_key root_key;
5052 int ret;
5053
5054 BUG_ON(!root->ref_cows);
5055 if (root->reloc_root)
5056 return 0;
5057
5058 root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
5059 BUG_ON(!root_item);
5060
5061 ret = btrfs_copy_root(trans, root, root->commit_root,
5062 &eb, BTRFS_TREE_RELOC_OBJECTID);
5063 BUG_ON(ret);
5064
5065 root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
5066 root_key.offset = root->root_key.objectid;
5067 root_key.type = BTRFS_ROOT_ITEM_KEY;
5068
5069 memcpy(root_item, &root->root_item, sizeof(root_item));
5070 btrfs_set_root_refs(root_item, 0);
5071 btrfs_set_root_bytenr(root_item, eb->start);
5072 btrfs_set_root_level(root_item, btrfs_header_level(eb));
Yan Zheng84234f32008-10-29 14:49:05 -04005073 btrfs_set_root_generation(root_item, trans->transid);
Zheng Yan1a40e232008-09-26 10:09:34 -04005074
5075 btrfs_tree_unlock(eb);
5076 free_extent_buffer(eb);
5077
5078 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
5079 &root_key, root_item);
5080 BUG_ON(ret);
5081 kfree(root_item);
5082
5083 reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
5084 &root_key);
5085 BUG_ON(!reloc_root);
5086 reloc_root->last_trans = trans->transid;
5087 reloc_root->commit_root = NULL;
5088 reloc_root->ref_tree = &root->fs_info->reloc_ref_tree;
5089
5090 root->reloc_root = reloc_root;
5091 return 0;
5092}
5093
5094/*
5095 * Core function of space balance.
5096 *
5097 * The idea is using reloc trees to relocate tree blocks in reference
Yan Zhengf82d02d2008-10-29 14:49:05 -04005098 * counted roots. There is one reloc tree for each subvol, and all
5099 * reloc trees share same root key objectid. Reloc trees are snapshots
5100 * of the latest committed roots of subvols (root->commit_root).
5101 *
5102 * To relocate a tree block referenced by a subvol, there are two steps.
5103 * COW the block through subvol's reloc tree, then update block pointer
5104 * in the subvol to point to the new block. Since all reloc trees share
5105 * same root key objectid, doing special handing for tree blocks owned
5106 * by them is easy. Once a tree block has been COWed in one reloc tree,
5107 * we can use the resulting new block directly when the same block is
5108 * required to COW again through other reloc trees. By this way, relocated
5109 * tree blocks are shared between reloc trees, so they are also shared
5110 * between subvols.
Zheng Yan1a40e232008-09-26 10:09:34 -04005111 */
Chris Masond3977122009-01-05 21:25:51 -05005112static noinline int relocate_one_path(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04005113 struct btrfs_root *root,
5114 struct btrfs_path *path,
5115 struct btrfs_key *first_key,
5116 struct btrfs_ref_path *ref_path,
5117 struct btrfs_block_group_cache *group,
5118 struct inode *reloc_inode)
5119{
5120 struct btrfs_root *reloc_root;
5121 struct extent_buffer *eb = NULL;
5122 struct btrfs_key *keys;
5123 u64 *nodes;
5124 int level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005125 int shared_level;
Zheng Yan1a40e232008-09-26 10:09:34 -04005126 int lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04005127 int ret;
5128
5129 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
5130 lowest_level = ref_path->owner_objectid;
5131
Yan Zhengf82d02d2008-10-29 14:49:05 -04005132 if (!root->ref_cows) {
Zheng Yan1a40e232008-09-26 10:09:34 -04005133 path->lowest_level = lowest_level;
5134 ret = btrfs_search_slot(trans, root, first_key, path, 0, 1);
5135 BUG_ON(ret < 0);
5136 path->lowest_level = 0;
5137 btrfs_release_path(root, path);
5138 return 0;
5139 }
5140
Zheng Yan1a40e232008-09-26 10:09:34 -04005141 mutex_lock(&root->fs_info->tree_reloc_mutex);
5142 ret = init_reloc_tree(trans, root);
5143 BUG_ON(ret);
5144 reloc_root = root->reloc_root;
5145
Yan Zhengf82d02d2008-10-29 14:49:05 -04005146 shared_level = ref_path->shared_level;
5147 ref_path->shared_level = BTRFS_MAX_LEVEL - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04005148
Yan Zhengf82d02d2008-10-29 14:49:05 -04005149 keys = ref_path->node_keys;
5150 nodes = ref_path->new_nodes;
5151 memset(&keys[shared_level + 1], 0,
5152 sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1));
5153 memset(&nodes[shared_level + 1], 0,
5154 sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1));
Zheng Yan1a40e232008-09-26 10:09:34 -04005155
Yan Zhengf82d02d2008-10-29 14:49:05 -04005156 if (nodes[lowest_level] == 0) {
5157 path->lowest_level = lowest_level;
5158 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
5159 0, 1);
5160 BUG_ON(ret);
5161 for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) {
5162 eb = path->nodes[level];
5163 if (!eb || eb == reloc_root->node)
5164 break;
5165 nodes[level] = eb->start;
5166 if (level == 0)
5167 btrfs_item_key_to_cpu(eb, &keys[level], 0);
5168 else
5169 btrfs_node_key_to_cpu(eb, &keys[level], 0);
5170 }
Yan Zheng2b820322008-11-17 21:11:30 -05005171 if (nodes[0] &&
5172 ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04005173 eb = path->nodes[0];
5174 ret = replace_extents_in_leaf(trans, reloc_root, eb,
5175 group, reloc_inode);
5176 BUG_ON(ret);
5177 }
5178 btrfs_release_path(reloc_root, path);
5179 } else {
Zheng Yan1a40e232008-09-26 10:09:34 -04005180 ret = btrfs_merge_path(trans, reloc_root, keys, nodes,
Yan Zhengf82d02d2008-10-29 14:49:05 -04005181 lowest_level);
Zheng Yan1a40e232008-09-26 10:09:34 -04005182 BUG_ON(ret);
5183 }
5184
Zheng Yan1a40e232008-09-26 10:09:34 -04005185 /*
5186 * replace tree blocks in the fs tree with tree blocks in
5187 * the reloc tree.
5188 */
5189 ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level);
5190 BUG_ON(ret < 0);
5191
5192 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04005193 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
5194 0, 0);
5195 BUG_ON(ret);
5196 extent_buffer_get(path->nodes[0]);
5197 eb = path->nodes[0];
5198 btrfs_release_path(reloc_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005199 ret = invalidate_extent_cache(reloc_root, eb, group, root);
5200 BUG_ON(ret);
5201 free_extent_buffer(eb);
5202 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005203
Yan Zhengf82d02d2008-10-29 14:49:05 -04005204 mutex_unlock(&root->fs_info->tree_reloc_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04005205 path->lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04005206 return 0;
5207}
5208
Chris Masond3977122009-01-05 21:25:51 -05005209static noinline int relocate_tree_block(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04005210 struct btrfs_root *root,
5211 struct btrfs_path *path,
5212 struct btrfs_key *first_key,
5213 struct btrfs_ref_path *ref_path)
5214{
5215 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04005216
5217 ret = relocate_one_path(trans, root, path, first_key,
5218 ref_path, NULL, NULL);
5219 BUG_ON(ret);
5220
5221 if (root == root->fs_info->extent_root)
5222 btrfs_extent_post_op(trans, root);
Zheng Yan1a40e232008-09-26 10:09:34 -04005223
5224 return 0;
5225}
5226
Chris Masond3977122009-01-05 21:25:51 -05005227static noinline int del_extent_zero(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04005228 struct btrfs_root *extent_root,
5229 struct btrfs_path *path,
5230 struct btrfs_key *extent_key)
5231{
5232 int ret;
5233
Zheng Yan1a40e232008-09-26 10:09:34 -04005234 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
5235 if (ret)
5236 goto out;
5237 ret = btrfs_del_item(trans, extent_root, path);
5238out:
Chris Masonedbd8d42007-12-21 16:27:24 -05005239 btrfs_release_path(extent_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005240 return ret;
5241}
5242
Chris Masond3977122009-01-05 21:25:51 -05005243static noinline struct btrfs_root *read_ref_root(struct btrfs_fs_info *fs_info,
Zheng Yan1a40e232008-09-26 10:09:34 -04005244 struct btrfs_ref_path *ref_path)
5245{
5246 struct btrfs_key root_key;
5247
5248 root_key.objectid = ref_path->root_objectid;
5249 root_key.type = BTRFS_ROOT_ITEM_KEY;
5250 if (is_cowonly_root(ref_path->root_objectid))
5251 root_key.offset = 0;
5252 else
5253 root_key.offset = (u64)-1;
5254
5255 return btrfs_read_fs_root_no_name(fs_info, &root_key);
5256}
5257
Chris Masond3977122009-01-05 21:25:51 -05005258static noinline int relocate_one_extent(struct btrfs_root *extent_root,
Zheng Yan1a40e232008-09-26 10:09:34 -04005259 struct btrfs_path *path,
5260 struct btrfs_key *extent_key,
5261 struct btrfs_block_group_cache *group,
5262 struct inode *reloc_inode, int pass)
5263{
5264 struct btrfs_trans_handle *trans;
5265 struct btrfs_root *found_root;
5266 struct btrfs_ref_path *ref_path = NULL;
5267 struct disk_extent *new_extents = NULL;
5268 int nr_extents = 0;
5269 int loops;
5270 int ret;
5271 int level;
5272 struct btrfs_key first_key;
5273 u64 prev_block = 0;
5274
Zheng Yan1a40e232008-09-26 10:09:34 -04005275
5276 trans = btrfs_start_transaction(extent_root, 1);
5277 BUG_ON(!trans);
5278
5279 if (extent_key->objectid == 0) {
5280 ret = del_extent_zero(trans, extent_root, path, extent_key);
5281 goto out;
5282 }
5283
5284 ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS);
5285 if (!ref_path) {
Chris Masond3977122009-01-05 21:25:51 -05005286 ret = -ENOMEM;
5287 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04005288 }
5289
5290 for (loops = 0; ; loops++) {
5291 if (loops == 0) {
5292 ret = btrfs_first_ref_path(trans, extent_root, ref_path,
5293 extent_key->objectid);
5294 } else {
5295 ret = btrfs_next_ref_path(trans, extent_root, ref_path);
5296 }
5297 if (ret < 0)
5298 goto out;
5299 if (ret > 0)
5300 break;
5301
5302 if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID ||
5303 ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID)
5304 continue;
5305
5306 found_root = read_ref_root(extent_root->fs_info, ref_path);
5307 BUG_ON(!found_root);
5308 /*
5309 * for reference counted tree, only process reference paths
5310 * rooted at the latest committed root.
5311 */
5312 if (found_root->ref_cows &&
5313 ref_path->root_generation != found_root->root_key.offset)
5314 continue;
5315
5316 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
5317 if (pass == 0) {
5318 /*
5319 * copy data extents to new locations
5320 */
5321 u64 group_start = group->key.objectid;
5322 ret = relocate_data_extent(reloc_inode,
5323 extent_key,
5324 group_start);
5325 if (ret < 0)
5326 goto out;
5327 break;
5328 }
5329 level = 0;
5330 } else {
5331 level = ref_path->owner_objectid;
5332 }
5333
5334 if (prev_block != ref_path->nodes[level]) {
5335 struct extent_buffer *eb;
5336 u64 block_start = ref_path->nodes[level];
5337 u64 block_size = btrfs_level_size(found_root, level);
5338
5339 eb = read_tree_block(found_root, block_start,
5340 block_size, 0);
5341 btrfs_tree_lock(eb);
5342 BUG_ON(level != btrfs_header_level(eb));
5343
5344 if (level == 0)
5345 btrfs_item_key_to_cpu(eb, &first_key, 0);
5346 else
5347 btrfs_node_key_to_cpu(eb, &first_key, 0);
5348
5349 btrfs_tree_unlock(eb);
5350 free_extent_buffer(eb);
5351 prev_block = block_start;
5352 }
5353
Yan Zhenge4404d62008-12-12 10:03:26 -05005354 btrfs_record_root_in_trans(found_root);
5355 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
5356 /*
5357 * try to update data extent references while
5358 * keeping metadata shared between snapshots.
5359 */
5360 if (pass == 1) {
5361 ret = relocate_one_path(trans, found_root,
5362 path, &first_key, ref_path,
5363 group, reloc_inode);
5364 if (ret < 0)
5365 goto out;
5366 continue;
5367 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005368 /*
5369 * use fallback method to process the remaining
5370 * references.
5371 */
5372 if (!new_extents) {
5373 u64 group_start = group->key.objectid;
Yan Zhengd899e052008-10-30 14:25:28 -04005374 new_extents = kmalloc(sizeof(*new_extents),
5375 GFP_NOFS);
5376 nr_extents = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04005377 ret = get_new_locations(reloc_inode,
5378 extent_key,
Yan Zhengd899e052008-10-30 14:25:28 -04005379 group_start, 1,
Zheng Yan1a40e232008-09-26 10:09:34 -04005380 &new_extents,
5381 &nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04005382 if (ret)
Zheng Yan1a40e232008-09-26 10:09:34 -04005383 goto out;
5384 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005385 ret = replace_one_extent(trans, found_root,
5386 path, extent_key,
5387 &first_key, ref_path,
5388 new_extents, nr_extents);
Yan Zhenge4404d62008-12-12 10:03:26 -05005389 } else {
Zheng Yan1a40e232008-09-26 10:09:34 -04005390 ret = relocate_tree_block(trans, found_root, path,
5391 &first_key, ref_path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005392 }
5393 if (ret < 0)
5394 goto out;
5395 }
5396 ret = 0;
5397out:
5398 btrfs_end_transaction(trans, extent_root);
5399 kfree(new_extents);
5400 kfree(ref_path);
Chris Masonedbd8d42007-12-21 16:27:24 -05005401 return ret;
5402}
5403
Chris Masonec44a352008-04-28 15:29:52 -04005404static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
5405{
5406 u64 num_devices;
5407 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
5408 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
5409
Yan Zheng2b820322008-11-17 21:11:30 -05005410 num_devices = root->fs_info->fs_devices->rw_devices;
Chris Masonec44a352008-04-28 15:29:52 -04005411 if (num_devices == 1) {
5412 stripped |= BTRFS_BLOCK_GROUP_DUP;
5413 stripped = flags & ~stripped;
5414
5415 /* turn raid0 into single device chunks */
5416 if (flags & BTRFS_BLOCK_GROUP_RAID0)
5417 return stripped;
5418
5419 /* turn mirroring into duplication */
5420 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
5421 BTRFS_BLOCK_GROUP_RAID10))
5422 return stripped | BTRFS_BLOCK_GROUP_DUP;
5423 return flags;
5424 } else {
5425 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04005426 if (flags & stripped)
5427 return flags;
5428
5429 stripped |= BTRFS_BLOCK_GROUP_DUP;
5430 stripped = flags & ~stripped;
5431
5432 /* switch duplicated blocks with raid1 */
5433 if (flags & BTRFS_BLOCK_GROUP_DUP)
5434 return stripped | BTRFS_BLOCK_GROUP_RAID1;
5435
5436 /* turn single device chunks into raid0 */
5437 return stripped | BTRFS_BLOCK_GROUP_RAID0;
5438 }
5439 return flags;
5440}
5441
Christoph Hellwigb2950862008-12-02 09:54:17 -05005442static int __alloc_chunk_for_shrink(struct btrfs_root *root,
Chris Mason0ef3e662008-05-24 14:04:53 -04005443 struct btrfs_block_group_cache *shrink_block_group,
5444 int force)
5445{
5446 struct btrfs_trans_handle *trans;
5447 u64 new_alloc_flags;
5448 u64 calc;
5449
Chris Masonc286ac42008-07-22 23:06:41 -04005450 spin_lock(&shrink_block_group->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04005451 if (btrfs_block_group_used(&shrink_block_group->item) > 0) {
Chris Masonc286ac42008-07-22 23:06:41 -04005452 spin_unlock(&shrink_block_group->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04005453
Chris Mason0ef3e662008-05-24 14:04:53 -04005454 trans = btrfs_start_transaction(root, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04005455 spin_lock(&shrink_block_group->lock);
Chris Mason7d9eb122008-07-08 14:19:17 -04005456
Chris Mason0ef3e662008-05-24 14:04:53 -04005457 new_alloc_flags = update_block_group_flags(root,
5458 shrink_block_group->flags);
5459 if (new_alloc_flags != shrink_block_group->flags) {
5460 calc =
5461 btrfs_block_group_used(&shrink_block_group->item);
5462 } else {
5463 calc = shrink_block_group->key.offset;
5464 }
Chris Masonc286ac42008-07-22 23:06:41 -04005465 spin_unlock(&shrink_block_group->lock);
5466
Chris Mason0ef3e662008-05-24 14:04:53 -04005467 do_chunk_alloc(trans, root->fs_info->extent_root,
5468 calc + 2 * 1024 * 1024, new_alloc_flags, force);
Chris Mason7d9eb122008-07-08 14:19:17 -04005469
Chris Mason0ef3e662008-05-24 14:04:53 -04005470 btrfs_end_transaction(trans, root);
Chris Masonc286ac42008-07-22 23:06:41 -04005471 } else
5472 spin_unlock(&shrink_block_group->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04005473 return 0;
5474}
5475
Zheng Yan1a40e232008-09-26 10:09:34 -04005476static int __insert_orphan_inode(struct btrfs_trans_handle *trans,
5477 struct btrfs_root *root,
5478 u64 objectid, u64 size)
5479{
5480 struct btrfs_path *path;
5481 struct btrfs_inode_item *item;
5482 struct extent_buffer *leaf;
5483 int ret;
5484
5485 path = btrfs_alloc_path();
5486 if (!path)
5487 return -ENOMEM;
5488
5489 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
5490 if (ret)
5491 goto out;
5492
5493 leaf = path->nodes[0];
5494 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item);
5495 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
5496 btrfs_set_inode_generation(leaf, item, 1);
5497 btrfs_set_inode_size(leaf, item, size);
5498 btrfs_set_inode_mode(leaf, item, S_IFREG | 0600);
Yan Zheng0403e472008-12-10 20:32:51 -05005499 btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NOCOMPRESS);
Zheng Yan1a40e232008-09-26 10:09:34 -04005500 btrfs_mark_buffer_dirty(leaf);
5501 btrfs_release_path(root, path);
5502out:
5503 btrfs_free_path(path);
5504 return ret;
5505}
5506
Chris Masond3977122009-01-05 21:25:51 -05005507static noinline struct inode *create_reloc_inode(struct btrfs_fs_info *fs_info,
Zheng Yan1a40e232008-09-26 10:09:34 -04005508 struct btrfs_block_group_cache *group)
5509{
5510 struct inode *inode = NULL;
5511 struct btrfs_trans_handle *trans;
5512 struct btrfs_root *root;
5513 struct btrfs_key root_key;
5514 u64 objectid = BTRFS_FIRST_FREE_OBJECTID;
5515 int err = 0;
5516
5517 root_key.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
5518 root_key.type = BTRFS_ROOT_ITEM_KEY;
5519 root_key.offset = (u64)-1;
5520 root = btrfs_read_fs_root_no_name(fs_info, &root_key);
5521 if (IS_ERR(root))
5522 return ERR_CAST(root);
5523
5524 trans = btrfs_start_transaction(root, 1);
5525 BUG_ON(!trans);
5526
5527 err = btrfs_find_free_objectid(trans, root, objectid, &objectid);
5528 if (err)
5529 goto out;
5530
5531 err = __insert_orphan_inode(trans, root, objectid, group->key.offset);
5532 BUG_ON(err);
5533
5534 err = btrfs_insert_file_extent(trans, root, objectid, 0, 0, 0,
Chris Masonc8b97812008-10-29 14:49:59 -04005535 group->key.offset, 0, group->key.offset,
5536 0, 0, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04005537 BUG_ON(err);
5538
5539 inode = btrfs_iget_locked(root->fs_info->sb, objectid, root);
5540 if (inode->i_state & I_NEW) {
5541 BTRFS_I(inode)->root = root;
5542 BTRFS_I(inode)->location.objectid = objectid;
5543 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
5544 BTRFS_I(inode)->location.offset = 0;
5545 btrfs_read_locked_inode(inode);
5546 unlock_new_inode(inode);
5547 BUG_ON(is_bad_inode(inode));
5548 } else {
5549 BUG_ON(1);
5550 }
Yan Zheng17d217f2008-12-12 10:03:38 -05005551 BTRFS_I(inode)->index_cnt = group->key.objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04005552
5553 err = btrfs_orphan_add(trans, inode);
5554out:
5555 btrfs_end_transaction(trans, root);
5556 if (err) {
5557 if (inode)
5558 iput(inode);
5559 inode = ERR_PTR(err);
5560 }
5561 return inode;
5562}
5563
Yan Zheng17d217f2008-12-12 10:03:38 -05005564int btrfs_reloc_clone_csums(struct inode *inode, u64 file_pos, u64 len)
5565{
5566
5567 struct btrfs_ordered_sum *sums;
5568 struct btrfs_sector_sum *sector_sum;
5569 struct btrfs_ordered_extent *ordered;
5570 struct btrfs_root *root = BTRFS_I(inode)->root;
5571 struct list_head list;
5572 size_t offset;
5573 int ret;
5574 u64 disk_bytenr;
5575
5576 INIT_LIST_HEAD(&list);
5577
5578 ordered = btrfs_lookup_ordered_extent(inode, file_pos);
5579 BUG_ON(ordered->file_offset != file_pos || ordered->len != len);
5580
5581 disk_bytenr = file_pos + BTRFS_I(inode)->index_cnt;
Yan Zheng07d400a2009-01-06 11:42:00 -05005582 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, disk_bytenr,
Yan Zheng17d217f2008-12-12 10:03:38 -05005583 disk_bytenr + len - 1, &list);
5584
5585 while (!list_empty(&list)) {
5586 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
5587 list_del_init(&sums->list);
5588
5589 sector_sum = sums->sums;
5590 sums->bytenr = ordered->start;
5591
5592 offset = 0;
5593 while (offset < sums->len) {
5594 sector_sum->bytenr += ordered->start - disk_bytenr;
5595 sector_sum++;
5596 offset += root->sectorsize;
5597 }
5598
5599 btrfs_add_ordered_sum(inode, ordered, sums);
5600 }
5601 btrfs_put_ordered_extent(ordered);
5602 return 0;
5603}
5604
Zheng Yan1a40e232008-09-26 10:09:34 -04005605int btrfs_relocate_block_group(struct btrfs_root *root, u64 group_start)
Chris Masonedbd8d42007-12-21 16:27:24 -05005606{
5607 struct btrfs_trans_handle *trans;
Chris Masonedbd8d42007-12-21 16:27:24 -05005608 struct btrfs_path *path;
Zheng Yan1a40e232008-09-26 10:09:34 -04005609 struct btrfs_fs_info *info = root->fs_info;
5610 struct extent_buffer *leaf;
5611 struct inode *reloc_inode;
5612 struct btrfs_block_group_cache *block_group;
5613 struct btrfs_key key;
Yan Zhengd899e052008-10-30 14:25:28 -04005614 u64 skipped;
Chris Masonedbd8d42007-12-21 16:27:24 -05005615 u64 cur_byte;
5616 u64 total_found;
Chris Masonedbd8d42007-12-21 16:27:24 -05005617 u32 nritems;
5618 int ret;
Chris Masona061fc82008-05-07 11:43:44 -04005619 int progress;
Zheng Yan1a40e232008-09-26 10:09:34 -04005620 int pass = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05005621
Chris Masonedbd8d42007-12-21 16:27:24 -05005622 root = root->fs_info->extent_root;
Zheng Yan1a40e232008-09-26 10:09:34 -04005623
5624 block_group = btrfs_lookup_block_group(info, group_start);
5625 BUG_ON(!block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05005626
Chris Masond3977122009-01-05 21:25:51 -05005627 printk(KERN_INFO "btrfs relocating block group %llu flags %llu\n",
Zheng Yan1a40e232008-09-26 10:09:34 -04005628 (unsigned long long)block_group->key.objectid,
5629 (unsigned long long)block_group->flags);
Chris Mason323da792008-05-09 11:46:48 -04005630
Zheng Yan1a40e232008-09-26 10:09:34 -04005631 path = btrfs_alloc_path();
5632 BUG_ON(!path);
Chris Mason323da792008-05-09 11:46:48 -04005633
Zheng Yan1a40e232008-09-26 10:09:34 -04005634 reloc_inode = create_reloc_inode(info, block_group);
5635 BUG_ON(IS_ERR(reloc_inode));
5636
Zheng Yan1a40e232008-09-26 10:09:34 -04005637 __alloc_chunk_for_shrink(root, block_group, 1);
Yan Zhengc146afa2008-11-12 14:34:12 -05005638 set_block_group_readonly(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04005639
Zheng Yan1a40e232008-09-26 10:09:34 -04005640 btrfs_start_delalloc_inodes(info->tree_root);
5641 btrfs_wait_ordered_extents(info->tree_root, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -04005642again:
Yan Zhengd899e052008-10-30 14:25:28 -04005643 skipped = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05005644 total_found = 0;
Chris Masona061fc82008-05-07 11:43:44 -04005645 progress = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04005646 key.objectid = block_group->key.objectid;
Chris Masonedbd8d42007-12-21 16:27:24 -05005647 key.offset = 0;
5648 key.type = 0;
Yan73e48b22008-01-03 14:14:39 -05005649 cur_byte = key.objectid;
Chris Mason4313b392008-01-03 09:08:48 -05005650
Zheng Yan1a40e232008-09-26 10:09:34 -04005651 trans = btrfs_start_transaction(info->tree_root, 1);
5652 btrfs_commit_transaction(trans, info->tree_root);
Chris Masonea8c2812008-08-04 23:17:27 -04005653
Zheng Yan1a40e232008-09-26 10:09:34 -04005654 mutex_lock(&root->fs_info->cleaner_mutex);
5655 btrfs_clean_old_snapshots(info->tree_root);
5656 btrfs_remove_leaf_refs(info->tree_root, (u64)-1, 1);
5657 mutex_unlock(&root->fs_info->cleaner_mutex);
Chris Masonea8c2812008-08-04 23:17:27 -04005658
Chris Masond3977122009-01-05 21:25:51 -05005659 while (1) {
Chris Masonedbd8d42007-12-21 16:27:24 -05005660 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5661 if (ret < 0)
5662 goto out;
Chris Mason7d9eb122008-07-08 14:19:17 -04005663next:
Chris Masonedbd8d42007-12-21 16:27:24 -05005664 leaf = path->nodes[0];
Chris Masonedbd8d42007-12-21 16:27:24 -05005665 nritems = btrfs_header_nritems(leaf);
Yan73e48b22008-01-03 14:14:39 -05005666 if (path->slots[0] >= nritems) {
5667 ret = btrfs_next_leaf(root, path);
5668 if (ret < 0)
5669 goto out;
5670 if (ret == 1) {
5671 ret = 0;
5672 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05005673 }
Yan73e48b22008-01-03 14:14:39 -05005674 leaf = path->nodes[0];
5675 nritems = btrfs_header_nritems(leaf);
5676 }
5677
Zheng Yan1a40e232008-09-26 10:09:34 -04005678 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Mason725c8462008-01-04 16:47:16 -05005679
Zheng Yan1a40e232008-09-26 10:09:34 -04005680 if (key.objectid >= block_group->key.objectid +
5681 block_group->key.offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04005682 break;
5683
Chris Mason725c8462008-01-04 16:47:16 -05005684 if (progress && need_resched()) {
Chris Mason725c8462008-01-04 16:47:16 -05005685 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005686 cond_resched();
Chris Mason725c8462008-01-04 16:47:16 -05005687 progress = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04005688 continue;
Chris Mason725c8462008-01-04 16:47:16 -05005689 }
5690 progress = 1;
5691
Zheng Yan1a40e232008-09-26 10:09:34 -04005692 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY ||
5693 key.objectid + key.offset <= cur_byte) {
Yan73e48b22008-01-03 14:14:39 -05005694 path->slots[0]++;
Chris Masonedbd8d42007-12-21 16:27:24 -05005695 goto next;
5696 }
Yan73e48b22008-01-03 14:14:39 -05005697
Chris Masonedbd8d42007-12-21 16:27:24 -05005698 total_found++;
Zheng Yan1a40e232008-09-26 10:09:34 -04005699 cur_byte = key.objectid + key.offset;
Chris Masonedbd8d42007-12-21 16:27:24 -05005700 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005701
5702 __alloc_chunk_for_shrink(root, block_group, 0);
5703 ret = relocate_one_extent(root, path, &key, block_group,
5704 reloc_inode, pass);
5705 BUG_ON(ret < 0);
Yan Zhengd899e052008-10-30 14:25:28 -04005706 if (ret > 0)
5707 skipped++;
Zheng Yan1a40e232008-09-26 10:09:34 -04005708
5709 key.objectid = cur_byte;
5710 key.type = 0;
5711 key.offset = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05005712 }
5713
5714 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005715
5716 if (pass == 0) {
5717 btrfs_wait_ordered_range(reloc_inode, 0, (u64)-1);
5718 invalidate_mapping_pages(reloc_inode->i_mapping, 0, -1);
Zheng Yan1a40e232008-09-26 10:09:34 -04005719 }
Chris Masonedbd8d42007-12-21 16:27:24 -05005720
5721 if (total_found > 0) {
Chris Masond3977122009-01-05 21:25:51 -05005722 printk(KERN_INFO "btrfs found %llu extents in pass %d\n",
Zheng Yan1a40e232008-09-26 10:09:34 -04005723 (unsigned long long)total_found, pass);
5724 pass++;
Yan Zhengd899e052008-10-30 14:25:28 -04005725 if (total_found == skipped && pass > 2) {
5726 iput(reloc_inode);
5727 reloc_inode = create_reloc_inode(info, block_group);
5728 pass = 0;
5729 }
Chris Masonedbd8d42007-12-21 16:27:24 -05005730 goto again;
5731 }
5732
Zheng Yan1a40e232008-09-26 10:09:34 -04005733 /* delete reloc_inode */
5734 iput(reloc_inode);
Chris Mason7d9eb122008-07-08 14:19:17 -04005735
Zheng Yan1a40e232008-09-26 10:09:34 -04005736 /* unpin extents in this range */
5737 trans = btrfs_start_transaction(info->tree_root, 1);
5738 btrfs_commit_transaction(trans, info->tree_root);
Chris Mason0ef3e662008-05-24 14:04:53 -04005739
Zheng Yan1a40e232008-09-26 10:09:34 -04005740 spin_lock(&block_group->lock);
5741 WARN_ON(block_group->pinned > 0);
5742 WARN_ON(block_group->reserved > 0);
5743 WARN_ON(btrfs_block_group_used(&block_group->item) > 0);
5744 spin_unlock(&block_group->lock);
Yan Zhengd2fb3432008-12-11 16:30:39 -05005745 put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04005746 ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05005747out:
Zheng Yan1a40e232008-09-26 10:09:34 -04005748 btrfs_free_path(path);
Chris Masonedbd8d42007-12-21 16:27:24 -05005749 return ret;
5750}
5751
Christoph Hellwigb2950862008-12-02 09:54:17 -05005752static int find_first_block_group(struct btrfs_root *root,
5753 struct btrfs_path *path, struct btrfs_key *key)
Chris Mason0b86a832008-03-24 15:01:56 -04005754{
Chris Mason925baed2008-06-25 16:01:30 -04005755 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005756 struct btrfs_key found_key;
5757 struct extent_buffer *leaf;
5758 int slot;
5759
5760 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
5761 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04005762 goto out;
5763
Chris Masond3977122009-01-05 21:25:51 -05005764 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04005765 slot = path->slots[0];
5766 leaf = path->nodes[0];
5767 if (slot >= btrfs_header_nritems(leaf)) {
5768 ret = btrfs_next_leaf(root, path);
5769 if (ret == 0)
5770 continue;
5771 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04005772 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04005773 break;
5774 }
5775 btrfs_item_key_to_cpu(leaf, &found_key, slot);
5776
5777 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04005778 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
5779 ret = 0;
5780 goto out;
5781 }
Chris Mason0b86a832008-03-24 15:01:56 -04005782 path->slots[0]++;
5783 }
5784 ret = -ENOENT;
Chris Mason925baed2008-06-25 16:01:30 -04005785out:
Chris Mason0b86a832008-03-24 15:01:56 -04005786 return ret;
5787}
5788
Zheng Yan1a40e232008-09-26 10:09:34 -04005789int btrfs_free_block_groups(struct btrfs_fs_info *info)
5790{
5791 struct btrfs_block_group_cache *block_group;
5792 struct rb_node *n;
5793
Zheng Yan1a40e232008-09-26 10:09:34 -04005794 spin_lock(&info->block_group_cache_lock);
5795 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
5796 block_group = rb_entry(n, struct btrfs_block_group_cache,
5797 cache_node);
Zheng Yan1a40e232008-09-26 10:09:34 -04005798 rb_erase(&block_group->cache_node,
5799 &info->block_group_cache_tree);
Yan Zhengd899e052008-10-30 14:25:28 -04005800 spin_unlock(&info->block_group_cache_lock);
5801
5802 btrfs_remove_free_space_cache(block_group);
Josef Bacik80eb2342008-10-29 14:49:05 -04005803 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04005804 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04005805 up_write(&block_group->space_info->groups_sem);
Yan Zhengd2fb3432008-12-11 16:30:39 -05005806
5807 WARN_ON(atomic_read(&block_group->count) != 1);
Zheng Yan1a40e232008-09-26 10:09:34 -04005808 kfree(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04005809
5810 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04005811 }
5812 spin_unlock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04005813 return 0;
5814}
5815
Chris Mason9078a3e2007-04-26 16:46:15 -04005816int btrfs_read_block_groups(struct btrfs_root *root)
5817{
5818 struct btrfs_path *path;
5819 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04005820 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04005821 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04005822 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04005823 struct btrfs_key key;
5824 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04005825 struct extent_buffer *leaf;
Chris Mason96b51792007-10-15 16:15:19 -04005826
Chris Masonbe744172007-05-06 10:15:01 -04005827 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04005828 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04005829 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04005830 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04005831 path = btrfs_alloc_path();
5832 if (!path)
5833 return -ENOMEM;
5834
Chris Masond3977122009-01-05 21:25:51 -05005835 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04005836 ret = find_first_block_group(root, path, &key);
5837 if (ret > 0) {
5838 ret = 0;
5839 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04005840 }
Chris Mason0b86a832008-03-24 15:01:56 -04005841 if (ret != 0)
5842 goto error;
5843
Chris Mason5f39d392007-10-15 16:14:19 -04005844 leaf = path->nodes[0];
5845 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04005846 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04005847 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04005848 ret = -ENOMEM;
Chris Mason9078a3e2007-04-26 16:46:15 -04005849 break;
5850 }
Chris Mason3e1ad542007-05-07 20:03:49 -04005851
Yan Zhengd2fb3432008-12-11 16:30:39 -05005852 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04005853 spin_lock_init(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04005854 mutex_init(&cache->alloc_mutex);
Josef Bacikea6a4782008-11-20 12:16:16 -05005855 mutex_init(&cache->cache_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005856 INIT_LIST_HEAD(&cache->list);
Chris Mason5f39d392007-10-15 16:14:19 -04005857 read_extent_buffer(leaf, &cache->item,
5858 btrfs_item_ptr_offset(leaf, path->slots[0]),
5859 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04005860 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04005861
Chris Mason9078a3e2007-04-26 16:46:15 -04005862 key.objectid = found_key.objectid + found_key.offset;
5863 btrfs_release_path(root, path);
Chris Mason0b86a832008-03-24 15:01:56 -04005864 cache->flags = btrfs_block_group_flags(&cache->item);
Chris Mason96b51792007-10-15 16:15:19 -04005865
Chris Mason6324fbf2008-03-24 15:01:59 -04005866 ret = update_space_info(info, cache->flags, found_key.offset,
5867 btrfs_block_group_used(&cache->item),
5868 &space_info);
5869 BUG_ON(ret);
5870 cache->space_info = space_info;
Josef Bacik80eb2342008-10-29 14:49:05 -04005871 down_write(&space_info->groups_sem);
5872 list_add_tail(&cache->list, &space_info->block_groups);
5873 up_write(&space_info->groups_sem);
Chris Mason6324fbf2008-03-24 15:01:59 -04005874
Josef Bacik0f9dd462008-09-23 13:14:11 -04005875 ret = btrfs_add_block_group_cache(root->fs_info, cache);
5876 BUG_ON(ret);
Chris Mason75ccf472008-09-30 19:24:06 -04005877
5878 set_avail_alloc_bits(root->fs_info, cache->flags);
Yan Zheng2b820322008-11-17 21:11:30 -05005879 if (btrfs_chunk_readonly(root, cache->key.objectid))
5880 set_block_group_readonly(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04005881 }
Chris Mason0b86a832008-03-24 15:01:56 -04005882 ret = 0;
5883error:
Chris Mason9078a3e2007-04-26 16:46:15 -04005884 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04005885 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04005886}
Chris Mason6324fbf2008-03-24 15:01:59 -04005887
5888int btrfs_make_block_group(struct btrfs_trans_handle *trans,
5889 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04005890 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04005891 u64 size)
5892{
5893 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04005894 struct btrfs_root *extent_root;
5895 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04005896
5897 extent_root = root->fs_info->extent_root;
Chris Mason6324fbf2008-03-24 15:01:59 -04005898
Chris Masone02119d2008-09-05 16:13:11 -04005899 root->fs_info->last_trans_new_blockgroup = trans->transid;
5900
Chris Mason8f18cf12008-04-25 16:53:30 -04005901 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005902 if (!cache)
5903 return -ENOMEM;
5904
Chris Masone17cade2008-04-15 15:41:47 -04005905 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04005906 cache->key.offset = size;
Yan Zhengd2fb3432008-12-11 16:30:39 -05005907 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
5908 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04005909 spin_lock_init(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04005910 mutex_init(&cache->alloc_mutex);
Josef Bacikea6a4782008-11-20 12:16:16 -05005911 mutex_init(&cache->cache_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04005912 INIT_LIST_HEAD(&cache->list);
Chris Mason0ef3e662008-05-24 14:04:53 -04005913
Chris Mason6324fbf2008-03-24 15:01:59 -04005914 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04005915 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
5916 cache->flags = type;
5917 btrfs_set_block_group_flags(&cache->item, type);
5918
5919 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
5920 &cache->space_info);
5921 BUG_ON(ret);
Josef Bacik80eb2342008-10-29 14:49:05 -04005922 down_write(&cache->space_info->groups_sem);
5923 list_add_tail(&cache->list, &cache->space_info->block_groups);
5924 up_write(&cache->space_info->groups_sem);
Chris Mason6324fbf2008-03-24 15:01:59 -04005925
Josef Bacik0f9dd462008-09-23 13:14:11 -04005926 ret = btrfs_add_block_group_cache(root->fs_info, cache);
5927 BUG_ON(ret);
Chris Masonc286ac42008-07-22 23:06:41 -04005928
Chris Mason6324fbf2008-03-24 15:01:59 -04005929 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
5930 sizeof(cache->item));
5931 BUG_ON(ret);
5932
Chris Mason87ef2bb2008-10-30 11:23:27 -04005933 finish_current_insert(trans, extent_root, 0);
5934 ret = del_pending_extents(trans, extent_root, 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04005935 BUG_ON(ret);
Chris Masond18a2c42008-04-04 15:40:00 -04005936 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04005937
Chris Mason6324fbf2008-03-24 15:01:59 -04005938 return 0;
5939}
Zheng Yan1a40e232008-09-26 10:09:34 -04005940
5941int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
5942 struct btrfs_root *root, u64 group_start)
5943{
5944 struct btrfs_path *path;
5945 struct btrfs_block_group_cache *block_group;
5946 struct btrfs_key key;
5947 int ret;
5948
Zheng Yan1a40e232008-09-26 10:09:34 -04005949 root = root->fs_info->extent_root;
5950
5951 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
5952 BUG_ON(!block_group);
Yan Zhengc146afa2008-11-12 14:34:12 -05005953 BUG_ON(!block_group->ro);
Zheng Yan1a40e232008-09-26 10:09:34 -04005954
5955 memcpy(&key, &block_group->key, sizeof(key));
5956
5957 path = btrfs_alloc_path();
5958 BUG_ON(!path);
5959
5960 btrfs_remove_free_space_cache(block_group);
5961 rb_erase(&block_group->cache_node,
5962 &root->fs_info->block_group_cache_tree);
Josef Bacik80eb2342008-10-29 14:49:05 -04005963 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04005964 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04005965 up_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04005966
Yan Zhengc146afa2008-11-12 14:34:12 -05005967 spin_lock(&block_group->space_info->lock);
5968 block_group->space_info->total_bytes -= block_group->key.offset;
5969 block_group->space_info->bytes_readonly -= block_group->key.offset;
5970 spin_unlock(&block_group->space_info->lock);
Yan Zheng2b820322008-11-17 21:11:30 -05005971 block_group->space_info->full = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05005972
Yan Zhengd2fb3432008-12-11 16:30:39 -05005973 put_block_group(block_group);
5974 put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04005975
5976 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
5977 if (ret > 0)
5978 ret = -EIO;
5979 if (ret < 0)
5980 goto out;
5981
5982 ret = btrfs_del_item(trans, root, path);
5983out:
5984 btrfs_free_path(path);
5985 return ret;
5986}