blob: fefe83ad20595eba078f6480781eefb0fe81c659 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
Zach Brownec6b9102007-07-11 10:00:37 -040018#include <linux/sched.h>
Chris Masonedbd8d42007-12-21 16:27:24 -050019#include <linux/pagemap.h>
Chris Masonec44a352008-04-28 15:29:52 -040020#include <linux/writeback.h>
David Woodhouse21af8042008-08-12 14:13:26 +010021#include <linux/blkdev.h>
Chris Masonb7a9f292009-02-04 09:23:45 -050022#include <linux/sort.h>
Chris Mason4184ea72009-03-10 12:39:20 -040023#include <linux/rcupdate.h>
Chris Mason4b4e25f2008-11-20 10:22:27 -050024#include "compat.h"
Chris Mason74493f72007-12-11 09:25:06 -050025#include "hash.h"
Miguela5eb62e2008-04-11 15:45:51 -040026#include "crc32c.h"
Chris Masonfec577f2007-02-26 10:40:21 -050027#include "ctree.h"
28#include "disk-io.h"
29#include "print-tree.h"
Chris Masone089f052007-03-16 16:20:31 -040030#include "transaction.h"
Chris Mason0b86a832008-03-24 15:01:56 -040031#include "volumes.h"
Chris Mason925baed2008-06-25 16:01:30 -040032#include "locking.h"
Yan Zheng31153d82008-07-28 15:32:19 -040033#include "ref-cache.h"
Chris Masonfec577f2007-02-26 10:40:21 -050034
Zheng Yan31840ae2008-09-23 13:14:14 -040035#define PENDING_EXTENT_INSERT 0
36#define PENDING_EXTENT_DELETE 1
37#define PENDING_BACKREF_UPDATE 2
38
39struct pending_extent_op {
40 int type;
41 u64 bytenr;
42 u64 num_bytes;
43 u64 parent;
44 u64 orig_parent;
45 u64 generation;
46 u64 orig_generation;
47 int level;
Josef Bacikf3465ca2008-11-12 14:19:50 -050048 struct list_head list;
49 int del;
Zheng Yan31840ae2008-09-23 13:14:14 -040050};
51
Chris 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 Bacik6a632092009-02-20 11:00:09 -050064static int do_chunk_alloc(struct btrfs_trans_handle *trans,
65 struct btrfs_root *extent_root, u64 alloc_bytes,
66 u64 flags, int force);
67
Josef Bacik0f9dd462008-09-23 13:14:11 -040068static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
69{
70 return (cache->flags & bits) == bits;
71}
72
73/*
74 * this adds the block group to the fs_info rb tree for the block group
75 * cache
76 */
Christoph Hellwigb2950862008-12-02 09:54:17 -050077static int btrfs_add_block_group_cache(struct btrfs_fs_info *info,
Josef Bacik0f9dd462008-09-23 13:14:11 -040078 struct btrfs_block_group_cache *block_group)
79{
80 struct rb_node **p;
81 struct rb_node *parent = NULL;
82 struct btrfs_block_group_cache *cache;
83
84 spin_lock(&info->block_group_cache_lock);
85 p = &info->block_group_cache_tree.rb_node;
86
87 while (*p) {
88 parent = *p;
89 cache = rb_entry(parent, struct btrfs_block_group_cache,
90 cache_node);
91 if (block_group->key.objectid < cache->key.objectid) {
92 p = &(*p)->rb_left;
93 } else if (block_group->key.objectid > cache->key.objectid) {
94 p = &(*p)->rb_right;
95 } else {
96 spin_unlock(&info->block_group_cache_lock);
97 return -EEXIST;
98 }
99 }
100
101 rb_link_node(&block_group->cache_node, parent, p);
102 rb_insert_color(&block_group->cache_node,
103 &info->block_group_cache_tree);
104 spin_unlock(&info->block_group_cache_lock);
105
106 return 0;
107}
108
109/*
110 * This will return the block group at or after bytenr if contains is 0, else
111 * it will return the block group that contains the bytenr
112 */
113static struct btrfs_block_group_cache *
114block_group_cache_tree_search(struct btrfs_fs_info *info, u64 bytenr,
115 int contains)
116{
117 struct btrfs_block_group_cache *cache, *ret = NULL;
118 struct rb_node *n;
119 u64 end, start;
120
121 spin_lock(&info->block_group_cache_lock);
122 n = info->block_group_cache_tree.rb_node;
123
124 while (n) {
125 cache = rb_entry(n, struct btrfs_block_group_cache,
126 cache_node);
127 end = cache->key.objectid + cache->key.offset - 1;
128 start = cache->key.objectid;
129
130 if (bytenr < start) {
131 if (!contains && (!ret || start < ret->key.objectid))
132 ret = cache;
133 n = n->rb_left;
134 } else if (bytenr > start) {
135 if (contains && bytenr <= end) {
136 ret = cache;
137 break;
138 }
139 n = n->rb_right;
140 } else {
141 ret = cache;
142 break;
143 }
144 }
Yan Zhengd2fb3432008-12-11 16:30:39 -0500145 if (ret)
146 atomic_inc(&ret->count);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400147 spin_unlock(&info->block_group_cache_lock);
148
149 return ret;
150}
151
152/*
153 * this is only called by cache_block_group, since we could have freed extents
154 * we need to check the pinned_extents for any extents that can't be used yet
155 * since their free space will be released as soon as the transaction commits.
156 */
157static int add_new_free_space(struct btrfs_block_group_cache *block_group,
158 struct btrfs_fs_info *info, u64 start, u64 end)
159{
160 u64 extent_start, extent_end, size;
161 int ret;
162
Josef Bacik25179202008-10-29 14:49:05 -0400163 mutex_lock(&info->pinned_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400164 while (start < end) {
165 ret = find_first_extent_bit(&info->pinned_extents, start,
166 &extent_start, &extent_end,
167 EXTENT_DIRTY);
168 if (ret)
169 break;
170
171 if (extent_start == start) {
172 start = extent_end + 1;
173 } else if (extent_start > start && extent_start < end) {
174 size = extent_start - start;
Josef Bacikea6a4782008-11-20 12:16:16 -0500175 ret = btrfs_add_free_space(block_group, start,
176 size);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400177 BUG_ON(ret);
178 start = extent_end + 1;
179 } else {
180 break;
181 }
182 }
183
184 if (start < end) {
185 size = end - start;
Josef Bacikea6a4782008-11-20 12:16:16 -0500186 ret = btrfs_add_free_space(block_group, start, size);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400187 BUG_ON(ret);
188 }
Josef Bacik25179202008-10-29 14:49:05 -0400189 mutex_unlock(&info->pinned_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400190
191 return 0;
192}
193
Yan Zhenga512bbf2008-12-08 16:46:26 -0500194static int remove_sb_from_cache(struct btrfs_root *root,
195 struct btrfs_block_group_cache *cache)
196{
197 u64 bytenr;
198 u64 *logical;
199 int stripe_len;
200 int i, nr, ret;
201
202 for (i = 0; i < BTRFS_SUPER_MIRROR_MAX; i++) {
203 bytenr = btrfs_sb_offset(i);
204 ret = btrfs_rmap_block(&root->fs_info->mapping_tree,
205 cache->key.objectid, bytenr, 0,
206 &logical, &nr, &stripe_len);
207 BUG_ON(ret);
208 while (nr--) {
209 btrfs_remove_free_space(cache, logical[nr],
210 stripe_len);
211 }
212 kfree(logical);
213 }
214 return 0;
215}
216
Chris Masone37c9e62007-05-09 20:13:14 -0400217static int cache_block_group(struct btrfs_root *root,
218 struct btrfs_block_group_cache *block_group)
219{
220 struct btrfs_path *path;
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400221 int ret = 0;
Chris Masone37c9e62007-05-09 20:13:14 -0400222 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400223 struct extent_buffer *leaf;
Chris Masone37c9e62007-05-09 20:13:14 -0400224 int slot;
Yan Zhenge4404d62008-12-12 10:03:26 -0500225 u64 last;
Chris Masone37c9e62007-05-09 20:13:14 -0400226
Chris Mason00f5c792007-11-30 10:09:33 -0500227 if (!block_group)
228 return 0;
229
Chris Masone37c9e62007-05-09 20:13:14 -0400230 root = root->fs_info->extent_root;
Chris Masone37c9e62007-05-09 20:13:14 -0400231
232 if (block_group->cached)
233 return 0;
Chris Masonf510cfe2007-10-15 16:14:48 -0400234
Chris Masone37c9e62007-05-09 20:13:14 -0400235 path = btrfs_alloc_path();
236 if (!path)
237 return -ENOMEM;
Yan7d7d6062007-09-14 16:15:28 -0400238
Chris Mason2cc58cf2007-08-27 16:49:44 -0400239 path->reada = 2;
Chris Mason5cd57b22008-06-25 16:01:30 -0400240 /*
241 * we get into deadlocks with paths held by callers of this function.
242 * since the alloc_mutex is protecting things right now, just
243 * skip the locking here
244 */
245 path->skip_locking = 1;
Yan Zhenge4404d62008-12-12 10:03:26 -0500246 last = max_t(u64, block_group->key.objectid, BTRFS_SUPER_INFO_OFFSET);
247 key.objectid = last;
Chris Masone37c9e62007-05-09 20:13:14 -0400248 key.offset = 0;
249 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
250 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
251 if (ret < 0)
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400252 goto err;
Yan Zhenga512bbf2008-12-08 16:46:26 -0500253
Chris Masond3977122009-01-05 21:25:51 -0500254 while (1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400255 leaf = path->nodes[0];
Chris Masone37c9e62007-05-09 20:13:14 -0400256 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -0400257 if (slot >= btrfs_header_nritems(leaf)) {
Chris Masone37c9e62007-05-09 20:13:14 -0400258 ret = btrfs_next_leaf(root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400259 if (ret < 0)
260 goto err;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400261 if (ret == 0)
Chris Masone37c9e62007-05-09 20:13:14 -0400262 continue;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400263 else
Chris Masone37c9e62007-05-09 20:13:14 -0400264 break;
Chris Masone37c9e62007-05-09 20:13:14 -0400265 }
Chris Mason5f39d392007-10-15 16:14:19 -0400266 btrfs_item_key_to_cpu(leaf, &key, slot);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400267 if (key.objectid < block_group->key.objectid)
Yan7d7d6062007-09-14 16:15:28 -0400268 goto next;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400269
Chris Masone37c9e62007-05-09 20:13:14 -0400270 if (key.objectid >= block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -0400271 block_group->key.offset)
Yan7d7d6062007-09-14 16:15:28 -0400272 break;
Yan7d7d6062007-09-14 16:15:28 -0400273
274 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
Josef Bacik0f9dd462008-09-23 13:14:11 -0400275 add_new_free_space(block_group, root->fs_info, last,
276 key.objectid);
277
Yan7d7d6062007-09-14 16:15:28 -0400278 last = key.objectid + key.offset;
Chris Masone37c9e62007-05-09 20:13:14 -0400279 }
Yan7d7d6062007-09-14 16:15:28 -0400280next:
Chris Masone37c9e62007-05-09 20:13:14 -0400281 path->slots[0]++;
282 }
283
Josef Bacik0f9dd462008-09-23 13:14:11 -0400284 add_new_free_space(block_group, root->fs_info, last,
285 block_group->key.objectid +
286 block_group->key.offset);
287
Yan Zhenga512bbf2008-12-08 16:46:26 -0500288 remove_sb_from_cache(root, block_group);
Chris Masone37c9e62007-05-09 20:13:14 -0400289 block_group->cached = 1;
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400290 ret = 0;
Chris Mason54aa1f42007-06-22 14:16:25 -0400291err:
Chris Masone37c9e62007-05-09 20:13:14 -0400292 btrfs_free_path(path);
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400293 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -0400294}
295
Josef Bacik0f9dd462008-09-23 13:14:11 -0400296/*
297 * return the block group that starts at or after bytenr
298 */
Chris Masond3977122009-01-05 21:25:51 -0500299static struct btrfs_block_group_cache *
300btrfs_lookup_first_block_group(struct btrfs_fs_info *info, u64 bytenr)
Chris Mason0ef3e662008-05-24 14:04:53 -0400301{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400302 struct btrfs_block_group_cache *cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400303
Josef Bacik0f9dd462008-09-23 13:14:11 -0400304 cache = block_group_cache_tree_search(info, bytenr, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -0400305
Josef Bacik0f9dd462008-09-23 13:14:11 -0400306 return cache;
Chris Mason0ef3e662008-05-24 14:04:53 -0400307}
308
Josef Bacik0f9dd462008-09-23 13:14:11 -0400309/*
310 * return the block group that contains teh given bytenr
311 */
Chris Masond3977122009-01-05 21:25:51 -0500312struct btrfs_block_group_cache *btrfs_lookup_block_group(
313 struct btrfs_fs_info *info,
314 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400315{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400316 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -0400317
Josef Bacik0f9dd462008-09-23 13:14:11 -0400318 cache = block_group_cache_tree_search(info, bytenr, 1);
Chris Mason96b51792007-10-15 16:15:19 -0400319
Josef Bacik0f9dd462008-09-23 13:14:11 -0400320 return cache;
Chris Masonbe744172007-05-06 10:15:01 -0400321}
Chris Mason0b86a832008-03-24 15:01:56 -0400322
Yan Zhengd2fb3432008-12-11 16:30:39 -0500323static inline void put_block_group(struct btrfs_block_group_cache *cache)
324{
325 if (atomic_dec_and_test(&cache->count))
326 kfree(cache);
327}
328
Josef Bacik0f9dd462008-09-23 13:14:11 -0400329static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
330 u64 flags)
Chris Mason6324fbf2008-03-24 15:01:59 -0400331{
Josef Bacik0f9dd462008-09-23 13:14:11 -0400332 struct list_head *head = &info->space_info;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400333 struct btrfs_space_info *found;
Chris Mason4184ea72009-03-10 12:39:20 -0400334
335 rcu_read_lock();
336 list_for_each_entry_rcu(found, head, list) {
337 if (found->flags == flags) {
338 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400339 return found;
Chris Mason4184ea72009-03-10 12:39:20 -0400340 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400341 }
Chris Mason4184ea72009-03-10 12:39:20 -0400342 rcu_read_unlock();
Josef Bacik0f9dd462008-09-23 13:14:11 -0400343 return NULL;
Chris Mason6324fbf2008-03-24 15:01:59 -0400344}
345
Chris Mason4184ea72009-03-10 12:39:20 -0400346/*
347 * after adding space to the filesystem, we need to clear the full flags
348 * on all the space infos.
349 */
350void btrfs_clear_space_info_full(struct btrfs_fs_info *info)
351{
352 struct list_head *head = &info->space_info;
353 struct btrfs_space_info *found;
354
355 rcu_read_lock();
356 list_for_each_entry_rcu(found, head, list)
357 found->full = 0;
358 rcu_read_unlock();
359}
360
Josef Bacik80eb2342008-10-29 14:49:05 -0400361static u64 div_factor(u64 num, int factor)
362{
363 if (factor == 10)
364 return num;
365 num *= factor;
366 do_div(num, 10);
367 return num;
368}
369
Yan Zhengd2fb3432008-12-11 16:30:39 -0500370u64 btrfs_find_block_group(struct btrfs_root *root,
371 u64 search_start, u64 search_hint, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400372{
Chris Mason96b51792007-10-15 16:15:19 -0400373 struct btrfs_block_group_cache *cache;
Chris Masoncd1bc462007-04-27 10:08:34 -0400374 u64 used;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500375 u64 last = max(search_hint, search_start);
376 u64 group_start = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400377 int full_search = 0;
Yan Zhengd2fb3432008-12-11 16:30:39 -0500378 int factor = 9;
Chris Mason0ef3e662008-05-24 14:04:53 -0400379 int wrapped = 0;
Chris Mason31f3c992007-04-30 15:25:45 -0400380again:
Zheng Yane8569812008-09-26 10:05:48 -0400381 while (1) {
382 cache = btrfs_lookup_first_block_group(root->fs_info, last);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400383 if (!cache)
Chris Masoncd1bc462007-04-27 10:08:34 -0400384 break;
Chris Mason96b51792007-10-15 16:15:19 -0400385
Chris Masonc286ac42008-07-22 23:06:41 -0400386 spin_lock(&cache->lock);
Chris Mason96b51792007-10-15 16:15:19 -0400387 last = cache->key.objectid + cache->key.offset;
388 used = btrfs_block_group_used(&cache->item);
389
Yan Zhengd2fb3432008-12-11 16:30:39 -0500390 if ((full_search || !cache->ro) &&
391 block_group_bits(cache, BTRFS_BLOCK_GROUP_METADATA)) {
Zheng Yane8569812008-09-26 10:05:48 -0400392 if (used + cache->pinned + cache->reserved <
Yan Zhengd2fb3432008-12-11 16:30:39 -0500393 div_factor(cache->key.offset, factor)) {
394 group_start = cache->key.objectid;
Chris Masonc286ac42008-07-22 23:06:41 -0400395 spin_unlock(&cache->lock);
Yan Zhengd2fb3432008-12-11 16:30:39 -0500396 put_block_group(cache);
Chris Mason8790d502008-04-03 16:29:03 -0400397 goto found;
398 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400399 }
Chris Masonc286ac42008-07-22 23:06:41 -0400400 spin_unlock(&cache->lock);
Yan Zhengd2fb3432008-12-11 16:30:39 -0500401 put_block_group(cache);
Chris Masonde428b62007-05-18 13:28:27 -0400402 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400403 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400404 if (!wrapped) {
405 last = search_start;
406 wrapped = 1;
407 goto again;
408 }
409 if (!full_search && factor < 10) {
Chris Masonbe744172007-05-06 10:15:01 -0400410 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400411 full_search = 1;
Chris Mason0ef3e662008-05-24 14:04:53 -0400412 factor = 10;
Chris Mason31f3c992007-04-30 15:25:45 -0400413 goto again;
414 }
Chris Masonbe744172007-05-06 10:15:01 -0400415found:
Yan Zhengd2fb3432008-12-11 16:30:39 -0500416 return group_start;
Chris Mason925baed2008-06-25 16:01:30 -0400417}
Josef Bacik0f9dd462008-09-23 13:14:11 -0400418
Chris Masone02119d2008-09-05 16:13:11 -0400419/* simple helper to search for an existing extent at a given offset */
Zheng Yan31840ae2008-09-23 13:14:14 -0400420int btrfs_lookup_extent(struct btrfs_root *root, u64 start, u64 len)
Chris Masone02119d2008-09-05 16:13:11 -0400421{
422 int ret;
423 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400424 struct btrfs_path *path;
Chris Masone02119d2008-09-05 16:13:11 -0400425
Zheng Yan31840ae2008-09-23 13:14:14 -0400426 path = btrfs_alloc_path();
427 BUG_ON(!path);
Chris Masone02119d2008-09-05 16:13:11 -0400428 key.objectid = start;
429 key.offset = len;
430 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
431 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
432 0, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -0400433 btrfs_free_path(path);
Chris Mason7bb86312007-12-11 09:25:06 -0500434 return ret;
435}
436
Chris Masond8d5f3e2007-12-11 12:42:00 -0500437/*
438 * Back reference rules. Back refs have three main goals:
439 *
440 * 1) differentiate between all holders of references to an extent so that
441 * when a reference is dropped we can make sure it was a valid reference
442 * before freeing the extent.
443 *
444 * 2) Provide enough information to quickly find the holders of an extent
445 * if we notice a given block is corrupted or bad.
446 *
447 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
448 * maintenance. This is actually the same as #2, but with a slightly
449 * different use case.
450 *
451 * File extents can be referenced by:
452 *
453 * - multiple snapshots, subvolumes, or different generations in one subvol
Zheng Yan31840ae2008-09-23 13:14:14 -0400454 * - different files inside a single subvolume
Chris Masond8d5f3e2007-12-11 12:42:00 -0500455 * - different offsets inside a file (bookend extents in file.c)
456 *
457 * The extent ref structure has fields for:
458 *
459 * - Objectid of the subvolume root
460 * - Generation number of the tree holding the reference
461 * - objectid of the file holding the reference
Zheng Yan31840ae2008-09-23 13:14:14 -0400462 * - number of references holding by parent node (alway 1 for tree blocks)
463 *
464 * Btree leaf may hold multiple references to a file extent. In most cases,
465 * these references are from same file and the corresponding offsets inside
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400466 * the file are close together.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500467 *
468 * When a file extent is allocated the fields are filled in:
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400469 * (root_key.objectid, trans->transid, inode objectid, 1)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500470 *
471 * When a leaf is cow'd new references are added for every file extent found
Zheng Yan31840ae2008-09-23 13:14:14 -0400472 * in the leaf. It looks similar to the create case, but trans->transid will
473 * be different when the block is cow'd.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500474 *
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400475 * (root_key.objectid, trans->transid, inode objectid,
Zheng Yan31840ae2008-09-23 13:14:14 -0400476 * number of references in the leaf)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500477 *
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400478 * When a file extent is removed either during snapshot deletion or
479 * file truncation, we find the corresponding back reference and check
480 * the following fields:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500481 *
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400482 * (btrfs_header_owner(leaf), btrfs_header_generation(leaf),
483 * inode objectid)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500484 *
485 * Btree extents can be referenced by:
486 *
487 * - Different subvolumes
488 * - Different generations of the same subvolume
489 *
Chris Masond8d5f3e2007-12-11 12:42:00 -0500490 * When a tree block is created, back references are inserted:
491 *
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400492 * (root->root_key.objectid, trans->transid, level, 1)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500493 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400494 * When a tree block is cow'd, new back references are added for all the
495 * blocks it points to. If the tree block isn't in reference counted root,
496 * the old back references are removed. These new back references are of
497 * the form (trans->transid will have increased since creation):
Chris Masond8d5f3e2007-12-11 12:42:00 -0500498 *
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400499 * (root->root_key.objectid, trans->transid, level, 1)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500500 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400501 * When a backref is in deleting, the following fields are checked:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500502 *
503 * if backref was for a tree root:
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400504 * (btrfs_header_owner(itself), btrfs_header_generation(itself), level)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500505 * else
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400506 * (btrfs_header_owner(parent), btrfs_header_generation(parent), level)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500507 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400508 * Back Reference Key composing:
Chris Masond8d5f3e2007-12-11 12:42:00 -0500509 *
Zheng Yan31840ae2008-09-23 13:14:14 -0400510 * The key objectid corresponds to the first byte in the extent, the key
511 * type is set to BTRFS_EXTENT_REF_KEY, and the key offset is the first
512 * byte of parent extent. If a extent is tree root, the key offset is set
513 * to the key objectid.
Chris Masond8d5f3e2007-12-11 12:42:00 -0500514 */
Zheng Yan31840ae2008-09-23 13:14:14 -0400515
Chris Masond3977122009-01-05 21:25:51 -0500516static noinline int lookup_extent_backref(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -0400517 struct btrfs_root *root,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400518 struct btrfs_path *path,
519 u64 bytenr, u64 parent,
520 u64 ref_root, u64 ref_generation,
521 u64 owner_objectid, int del)
Chris Mason74493f72007-12-11 09:25:06 -0500522{
Chris Mason74493f72007-12-11 09:25:06 -0500523 struct btrfs_key key;
Zheng Yan31840ae2008-09-23 13:14:14 -0400524 struct btrfs_extent_ref *ref;
525 struct extent_buffer *leaf;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400526 u64 ref_objectid;
Chris Mason74493f72007-12-11 09:25:06 -0500527 int ret;
528
Chris Mason74493f72007-12-11 09:25:06 -0500529 key.objectid = bytenr;
530 key.type = BTRFS_EXTENT_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -0400531 key.offset = parent;
Chris Mason74493f72007-12-11 09:25:06 -0500532
Zheng Yan31840ae2008-09-23 13:14:14 -0400533 ret = btrfs_search_slot(trans, root, &key, path, del ? -1 : 0, 1);
534 if (ret < 0)
Chris Mason7bb86312007-12-11 09:25:06 -0500535 goto out;
Zheng Yan31840ae2008-09-23 13:14:14 -0400536 if (ret > 0) {
537 ret = -ENOENT;
538 goto out;
539 }
540
541 leaf = path->nodes[0];
542 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400543 ref_objectid = btrfs_ref_objectid(leaf, ref);
Zheng Yan31840ae2008-09-23 13:14:14 -0400544 if (btrfs_ref_root(leaf, ref) != ref_root ||
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400545 btrfs_ref_generation(leaf, ref) != ref_generation ||
546 (ref_objectid != owner_objectid &&
547 ref_objectid != BTRFS_MULTIPLE_OBJECTIDS)) {
Zheng Yan31840ae2008-09-23 13:14:14 -0400548 ret = -EIO;
549 WARN_ON(1);
550 goto out;
551 }
552 ret = 0;
553out:
554 return ret;
555}
556
Josef Bacikf3465ca2008-11-12 14:19:50 -0500557/*
558 * updates all the backrefs that are pending on update_list for the
559 * extent_root
560 */
Chris Masond3977122009-01-05 21:25:51 -0500561static noinline int update_backrefs(struct btrfs_trans_handle *trans,
Josef Bacikf3465ca2008-11-12 14:19:50 -0500562 struct btrfs_root *extent_root,
563 struct btrfs_path *path,
564 struct list_head *update_list)
565{
566 struct btrfs_key key;
567 struct btrfs_extent_ref *ref;
568 struct btrfs_fs_info *info = extent_root->fs_info;
569 struct pending_extent_op *op;
570 struct extent_buffer *leaf;
571 int ret = 0;
572 struct list_head *cur = update_list->next;
573 u64 ref_objectid;
574 u64 ref_root = extent_root->root_key.objectid;
575
576 op = list_entry(cur, struct pending_extent_op, list);
577
578search:
579 key.objectid = op->bytenr;
580 key.type = BTRFS_EXTENT_REF_KEY;
581 key.offset = op->orig_parent;
582
583 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 1);
584 BUG_ON(ret);
585
586 leaf = path->nodes[0];
587
588loop:
589 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
590
591 ref_objectid = btrfs_ref_objectid(leaf, ref);
592
593 if (btrfs_ref_root(leaf, ref) != ref_root ||
594 btrfs_ref_generation(leaf, ref) != op->orig_generation ||
595 (ref_objectid != op->level &&
596 ref_objectid != BTRFS_MULTIPLE_OBJECTIDS)) {
Chris Masond3977122009-01-05 21:25:51 -0500597 printk(KERN_ERR "btrfs couldn't find %llu, parent %llu, "
598 "root %llu, owner %u\n",
599 (unsigned long long)op->bytenr,
600 (unsigned long long)op->orig_parent,
601 (unsigned long long)ref_root, op->level);
Josef Bacikf3465ca2008-11-12 14:19:50 -0500602 btrfs_print_leaf(extent_root, leaf);
603 BUG();
604 }
605
606 key.objectid = op->bytenr;
607 key.offset = op->parent;
608 key.type = BTRFS_EXTENT_REF_KEY;
609 ret = btrfs_set_item_key_safe(trans, extent_root, path, &key);
610 BUG_ON(ret);
611 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
612 btrfs_set_ref_generation(leaf, ref, op->generation);
613
614 cur = cur->next;
615
616 list_del_init(&op->list);
617 unlock_extent(&info->extent_ins, op->bytenr,
618 op->bytenr + op->num_bytes - 1, GFP_NOFS);
619 kfree(op);
620
621 if (cur == update_list) {
622 btrfs_mark_buffer_dirty(path->nodes[0]);
623 btrfs_release_path(extent_root, path);
624 goto out;
625 }
626
627 op = list_entry(cur, struct pending_extent_op, list);
628
629 path->slots[0]++;
630 while (path->slots[0] < btrfs_header_nritems(leaf)) {
631 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
632 if (key.objectid == op->bytenr &&
633 key.type == BTRFS_EXTENT_REF_KEY)
634 goto loop;
635 path->slots[0]++;
636 }
637
638 btrfs_mark_buffer_dirty(path->nodes[0]);
639 btrfs_release_path(extent_root, path);
640 goto search;
641
642out:
643 return 0;
644}
645
Chris Masond3977122009-01-05 21:25:51 -0500646static noinline int insert_extents(struct btrfs_trans_handle *trans,
Josef Bacikf3465ca2008-11-12 14:19:50 -0500647 struct btrfs_root *extent_root,
648 struct btrfs_path *path,
649 struct list_head *insert_list, int nr)
650{
651 struct btrfs_key *keys;
652 u32 *data_size;
653 struct pending_extent_op *op;
654 struct extent_buffer *leaf;
655 struct list_head *cur = insert_list->next;
656 struct btrfs_fs_info *info = extent_root->fs_info;
657 u64 ref_root = extent_root->root_key.objectid;
658 int i = 0, last = 0, ret;
659 int total = nr * 2;
660
661 if (!nr)
662 return 0;
663
664 keys = kzalloc(total * sizeof(struct btrfs_key), GFP_NOFS);
665 if (!keys)
666 return -ENOMEM;
667
668 data_size = kzalloc(total * sizeof(u32), GFP_NOFS);
669 if (!data_size) {
670 kfree(keys);
671 return -ENOMEM;
672 }
673
674 list_for_each_entry(op, insert_list, list) {
675 keys[i].objectid = op->bytenr;
676 keys[i].offset = op->num_bytes;
677 keys[i].type = BTRFS_EXTENT_ITEM_KEY;
678 data_size[i] = sizeof(struct btrfs_extent_item);
679 i++;
680
681 keys[i].objectid = op->bytenr;
682 keys[i].offset = op->parent;
683 keys[i].type = BTRFS_EXTENT_REF_KEY;
684 data_size[i] = sizeof(struct btrfs_extent_ref);
685 i++;
686 }
687
688 op = list_entry(cur, struct pending_extent_op, list);
689 i = 0;
690 while (i < total) {
691 int c;
692 ret = btrfs_insert_some_items(trans, extent_root, path,
693 keys+i, data_size+i, total-i);
694 BUG_ON(ret < 0);
695
696 if (last && ret > 1)
697 BUG();
698
699 leaf = path->nodes[0];
700 for (c = 0; c < ret; c++) {
701 int ref_first = keys[i].type == BTRFS_EXTENT_REF_KEY;
702
703 /*
704 * if the first item we inserted was a backref, then
705 * the EXTENT_ITEM will be the odd c's, else it will
706 * be the even c's
707 */
708 if ((ref_first && (c % 2)) ||
709 (!ref_first && !(c % 2))) {
710 struct btrfs_extent_item *itm;
711
712 itm = btrfs_item_ptr(leaf, path->slots[0] + c,
713 struct btrfs_extent_item);
714 btrfs_set_extent_refs(path->nodes[0], itm, 1);
715 op->del++;
716 } else {
717 struct btrfs_extent_ref *ref;
718
719 ref = btrfs_item_ptr(leaf, path->slots[0] + c,
720 struct btrfs_extent_ref);
721 btrfs_set_ref_root(leaf, ref, ref_root);
722 btrfs_set_ref_generation(leaf, ref,
723 op->generation);
724 btrfs_set_ref_objectid(leaf, ref, op->level);
725 btrfs_set_ref_num_refs(leaf, ref, 1);
726 op->del++;
727 }
728
729 /*
730 * using del to see when its ok to free up the
731 * pending_extent_op. In the case where we insert the
732 * last item on the list in order to help do batching
733 * we need to not free the extent op until we actually
734 * insert the extent_item
735 */
736 if (op->del == 2) {
737 unlock_extent(&info->extent_ins, op->bytenr,
738 op->bytenr + op->num_bytes - 1,
739 GFP_NOFS);
740 cur = cur->next;
741 list_del_init(&op->list);
742 kfree(op);
743 if (cur != insert_list)
744 op = list_entry(cur,
745 struct pending_extent_op,
746 list);
747 }
748 }
749 btrfs_mark_buffer_dirty(leaf);
750 btrfs_release_path(extent_root, path);
751
752 /*
753 * Ok backref's and items usually go right next to eachother,
754 * but if we could only insert 1 item that means that we
755 * inserted on the end of a leaf, and we have no idea what may
756 * be on the next leaf so we just play it safe. In order to
757 * try and help this case we insert the last thing on our
758 * insert list so hopefully it will end up being the last
759 * thing on the leaf and everything else will be before it,
760 * which will let us insert a whole bunch of items at the same
761 * time.
762 */
763 if (ret == 1 && !last && (i + ret < total)) {
764 /*
765 * last: where we will pick up the next time around
766 * i: our current key to insert, will be total - 1
767 * cur: the current op we are screwing with
768 * op: duh
769 */
770 last = i + ret;
771 i = total - 1;
772 cur = insert_list->prev;
773 op = list_entry(cur, struct pending_extent_op, list);
774 } else if (last) {
775 /*
776 * ok we successfully inserted the last item on the
777 * list, lets reset everything
778 *
779 * i: our current key to insert, so where we left off
780 * last time
781 * last: done with this
782 * cur: the op we are messing with
783 * op: duh
784 * total: since we inserted the last key, we need to
785 * decrement total so we dont overflow
786 */
787 i = last;
788 last = 0;
Josef Bacikf3465ca2008-11-12 14:19:50 -0500789 total--;
Liu Huib4eec2c2008-11-18 11:30:10 -0500790 if (i < total) {
791 cur = insert_list->next;
792 op = list_entry(cur, struct pending_extent_op,
793 list);
794 }
Josef Bacikf3465ca2008-11-12 14:19:50 -0500795 } else {
796 i += ret;
797 }
798
799 cond_resched();
800 }
801 ret = 0;
802 kfree(keys);
803 kfree(data_size);
804 return ret;
805}
806
Chris Masond3977122009-01-05 21:25:51 -0500807static noinline int insert_extent_backref(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -0400808 struct btrfs_root *root,
809 struct btrfs_path *path,
810 u64 bytenr, u64 parent,
811 u64 ref_root, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400812 u64 owner_objectid)
Zheng Yan31840ae2008-09-23 13:14:14 -0400813{
814 struct btrfs_key key;
815 struct extent_buffer *leaf;
816 struct btrfs_extent_ref *ref;
817 u32 num_refs;
818 int ret;
819
820 key.objectid = bytenr;
821 key.type = BTRFS_EXTENT_REF_KEY;
822 key.offset = parent;
823
824 ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(*ref));
825 if (ret == 0) {
826 leaf = path->nodes[0];
827 ref = btrfs_item_ptr(leaf, path->slots[0],
828 struct btrfs_extent_ref);
829 btrfs_set_ref_root(leaf, ref, ref_root);
830 btrfs_set_ref_generation(leaf, ref, ref_generation);
831 btrfs_set_ref_objectid(leaf, ref, owner_objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -0400832 btrfs_set_ref_num_refs(leaf, ref, 1);
833 } else if (ret == -EEXIST) {
834 u64 existing_owner;
835 BUG_ON(owner_objectid < BTRFS_FIRST_FREE_OBJECTID);
836 leaf = path->nodes[0];
837 ref = btrfs_item_ptr(leaf, path->slots[0],
838 struct btrfs_extent_ref);
839 if (btrfs_ref_root(leaf, ref) != ref_root ||
840 btrfs_ref_generation(leaf, ref) != ref_generation) {
841 ret = -EIO;
842 WARN_ON(1);
843 goto out;
844 }
845
846 num_refs = btrfs_ref_num_refs(leaf, ref);
847 BUG_ON(num_refs == 0);
848 btrfs_set_ref_num_refs(leaf, ref, num_refs + 1);
849
850 existing_owner = btrfs_ref_objectid(leaf, ref);
Yan Zheng3bb1a1b2008-10-09 11:46:24 -0400851 if (existing_owner != owner_objectid &&
852 existing_owner != BTRFS_MULTIPLE_OBJECTIDS) {
Zheng Yan31840ae2008-09-23 13:14:14 -0400853 btrfs_set_ref_objectid(leaf, ref,
854 BTRFS_MULTIPLE_OBJECTIDS);
Zheng Yan31840ae2008-09-23 13:14:14 -0400855 }
856 ret = 0;
857 } else {
858 goto out;
859 }
Chris Mason7bb86312007-12-11 09:25:06 -0500860 btrfs_mark_buffer_dirty(path->nodes[0]);
861out:
862 btrfs_release_path(root, path);
863 return ret;
Chris Mason74493f72007-12-11 09:25:06 -0500864}
865
Chris Masond3977122009-01-05 21:25:51 -0500866static noinline int remove_extent_backref(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -0400867 struct btrfs_root *root,
868 struct btrfs_path *path)
869{
870 struct extent_buffer *leaf;
871 struct btrfs_extent_ref *ref;
872 u32 num_refs;
873 int ret = 0;
874
875 leaf = path->nodes[0];
876 ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_extent_ref);
877 num_refs = btrfs_ref_num_refs(leaf, ref);
878 BUG_ON(num_refs == 0);
879 num_refs -= 1;
880 if (num_refs == 0) {
881 ret = btrfs_del_item(trans, root, path);
882 } else {
883 btrfs_set_ref_num_refs(leaf, ref, num_refs);
884 btrfs_mark_buffer_dirty(leaf);
885 }
886 btrfs_release_path(root, path);
887 return ret;
888}
889
Chris Mason4b4e25f2008-11-20 10:22:27 -0500890#ifdef BIO_RW_DISCARD
Chris Mason15916de2008-11-19 21:17:22 -0500891static void btrfs_issue_discard(struct block_device *bdev,
892 u64 start, u64 len)
893{
Chris Mason15916de2008-11-19 21:17:22 -0500894 blkdev_issue_discard(bdev, start >> 9, len >> 9, GFP_KERNEL);
Chris Mason15916de2008-11-19 21:17:22 -0500895}
Chris Mason4b4e25f2008-11-20 10:22:27 -0500896#endif
Chris Mason15916de2008-11-19 21:17:22 -0500897
Liu Hui1f3c79a2009-01-05 15:57:51 -0500898static int btrfs_discard_extent(struct btrfs_root *root, u64 bytenr,
899 u64 num_bytes)
900{
901#ifdef BIO_RW_DISCARD
902 int ret;
903 u64 map_length = num_bytes;
904 struct btrfs_multi_bio *multi = NULL;
905
906 /* Tell the block device(s) that the sectors can be discarded */
907 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
908 bytenr, &map_length, &multi, 0);
909 if (!ret) {
910 struct btrfs_bio_stripe *stripe = multi->stripes;
911 int i;
912
913 if (map_length > num_bytes)
914 map_length = num_bytes;
915
916 for (i = 0; i < multi->num_stripes; i++, stripe++) {
917 btrfs_issue_discard(stripe->dev->bdev,
918 stripe->physical,
919 map_length);
920 }
921 kfree(multi);
922 }
923
924 return ret;
925#else
926 return 0;
927#endif
928}
929
Chris Masond3977122009-01-05 21:25:51 -0500930static noinline int free_extents(struct btrfs_trans_handle *trans,
Josef Bacikf3465ca2008-11-12 14:19:50 -0500931 struct btrfs_root *extent_root,
932 struct list_head *del_list)
933{
934 struct btrfs_fs_info *info = extent_root->fs_info;
935 struct btrfs_path *path;
936 struct btrfs_key key, found_key;
937 struct extent_buffer *leaf;
938 struct list_head *cur;
939 struct pending_extent_op *op;
940 struct btrfs_extent_item *ei;
941 int ret, num_to_del, extent_slot = 0, found_extent = 0;
942 u32 refs;
943 u64 bytes_freed = 0;
944
945 path = btrfs_alloc_path();
946 if (!path)
947 return -ENOMEM;
948 path->reada = 1;
949
950search:
951 /* search for the backref for the current ref we want to delete */
952 cur = del_list->next;
953 op = list_entry(cur, struct pending_extent_op, list);
954 ret = lookup_extent_backref(trans, extent_root, path, op->bytenr,
955 op->orig_parent,
956 extent_root->root_key.objectid,
957 op->orig_generation, op->level, 1);
958 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -0500959 printk(KERN_ERR "btrfs unable to find backref byte nr %llu "
960 "root %llu gen %llu owner %u\n",
961 (unsigned long long)op->bytenr,
962 (unsigned long long)extent_root->root_key.objectid,
963 (unsigned long long)op->orig_generation, op->level);
Josef Bacikf3465ca2008-11-12 14:19:50 -0500964 btrfs_print_leaf(extent_root, path->nodes[0]);
965 WARN_ON(1);
966 goto out;
967 }
968
969 extent_slot = path->slots[0];
970 num_to_del = 1;
971 found_extent = 0;
972
973 /*
974 * if we aren't the first item on the leaf we can move back one and see
975 * if our ref is right next to our extent item
976 */
977 if (likely(extent_slot)) {
978 extent_slot--;
979 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
980 extent_slot);
981 if (found_key.objectid == op->bytenr &&
982 found_key.type == BTRFS_EXTENT_ITEM_KEY &&
983 found_key.offset == op->num_bytes) {
984 num_to_del++;
985 found_extent = 1;
986 }
987 }
988
989 /*
990 * if we didn't find the extent we need to delete the backref and then
991 * search for the extent item key so we can update its ref count
992 */
993 if (!found_extent) {
994 key.objectid = op->bytenr;
995 key.type = BTRFS_EXTENT_ITEM_KEY;
996 key.offset = op->num_bytes;
997
998 ret = remove_extent_backref(trans, extent_root, path);
999 BUG_ON(ret);
1000 btrfs_release_path(extent_root, path);
1001 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
1002 BUG_ON(ret);
1003 extent_slot = path->slots[0];
1004 }
1005
1006 /* this is where we update the ref count for the extent */
1007 leaf = path->nodes[0];
1008 ei = btrfs_item_ptr(leaf, extent_slot, struct btrfs_extent_item);
1009 refs = btrfs_extent_refs(leaf, ei);
1010 BUG_ON(refs == 0);
1011 refs--;
1012 btrfs_set_extent_refs(leaf, ei, refs);
1013
1014 btrfs_mark_buffer_dirty(leaf);
1015
1016 /*
1017 * This extent needs deleting. The reason cur_slot is extent_slot +
1018 * num_to_del is because extent_slot points to the slot where the extent
1019 * is, and if the backref was not right next to the extent we will be
1020 * deleting at least 1 item, and will want to start searching at the
1021 * slot directly next to extent_slot. However if we did find the
1022 * backref next to the extent item them we will be deleting at least 2
1023 * items and will want to start searching directly after the ref slot
1024 */
1025 if (!refs) {
1026 struct list_head *pos, *n, *end;
1027 int cur_slot = extent_slot+num_to_del;
1028 u64 super_used;
1029 u64 root_used;
1030
1031 path->slots[0] = extent_slot;
1032 bytes_freed = op->num_bytes;
1033
Josef Bacike3e469f2008-11-17 21:11:49 -05001034 mutex_lock(&info->pinned_mutex);
1035 ret = pin_down_bytes(trans, extent_root, op->bytenr,
1036 op->num_bytes, op->level >=
1037 BTRFS_FIRST_FREE_OBJECTID);
1038 mutex_unlock(&info->pinned_mutex);
1039 BUG_ON(ret < 0);
1040 op->del = ret;
1041
Josef Bacikf3465ca2008-11-12 14:19:50 -05001042 /*
1043 * we need to see if we can delete multiple things at once, so
1044 * start looping through the list of extents we are wanting to
1045 * delete and see if their extent/backref's are right next to
1046 * eachother and the extents only have 1 ref
1047 */
1048 for (pos = cur->next; pos != del_list; pos = pos->next) {
1049 struct pending_extent_op *tmp;
1050
1051 tmp = list_entry(pos, struct pending_extent_op, list);
1052
1053 /* we only want to delete extent+ref at this stage */
1054 if (cur_slot >= btrfs_header_nritems(leaf) - 1)
1055 break;
1056
1057 btrfs_item_key_to_cpu(leaf, &found_key, cur_slot);
1058 if (found_key.objectid != tmp->bytenr ||
1059 found_key.type != BTRFS_EXTENT_ITEM_KEY ||
1060 found_key.offset != tmp->num_bytes)
1061 break;
1062
1063 /* check to make sure this extent only has one ref */
1064 ei = btrfs_item_ptr(leaf, cur_slot,
1065 struct btrfs_extent_item);
1066 if (btrfs_extent_refs(leaf, ei) != 1)
1067 break;
1068
1069 btrfs_item_key_to_cpu(leaf, &found_key, cur_slot+1);
1070 if (found_key.objectid != tmp->bytenr ||
1071 found_key.type != BTRFS_EXTENT_REF_KEY ||
1072 found_key.offset != tmp->orig_parent)
1073 break;
1074
1075 /*
1076 * the ref is right next to the extent, we can set the
1077 * ref count to 0 since we will delete them both now
1078 */
1079 btrfs_set_extent_refs(leaf, ei, 0);
1080
1081 /* pin down the bytes for this extent */
1082 mutex_lock(&info->pinned_mutex);
1083 ret = pin_down_bytes(trans, extent_root, tmp->bytenr,
1084 tmp->num_bytes, tmp->level >=
1085 BTRFS_FIRST_FREE_OBJECTID);
1086 mutex_unlock(&info->pinned_mutex);
1087 BUG_ON(ret < 0);
1088
1089 /*
1090 * use the del field to tell if we need to go ahead and
1091 * free up the extent when we delete the item or not.
1092 */
1093 tmp->del = ret;
1094 bytes_freed += tmp->num_bytes;
1095
1096 num_to_del += 2;
1097 cur_slot += 2;
1098 }
1099 end = pos;
1100
1101 /* update the free space counters */
Chris Mason75eff682008-12-15 15:54:40 -05001102 spin_lock(&info->delalloc_lock);
Josef Bacikf3465ca2008-11-12 14:19:50 -05001103 super_used = btrfs_super_bytes_used(&info->super_copy);
1104 btrfs_set_super_bytes_used(&info->super_copy,
1105 super_used - bytes_freed);
Josef Bacikf3465ca2008-11-12 14:19:50 -05001106
1107 root_used = btrfs_root_used(&extent_root->root_item);
1108 btrfs_set_root_used(&extent_root->root_item,
1109 root_used - bytes_freed);
Yan Zheng34bf63c2008-12-19 10:58:46 -05001110 spin_unlock(&info->delalloc_lock);
Josef Bacikf3465ca2008-11-12 14:19:50 -05001111
1112 /* delete the items */
1113 ret = btrfs_del_items(trans, extent_root, path,
1114 path->slots[0], num_to_del);
1115 BUG_ON(ret);
1116
1117 /*
1118 * loop through the extents we deleted and do the cleanup work
1119 * on them
1120 */
1121 for (pos = cur, n = pos->next; pos != end;
1122 pos = n, n = pos->next) {
1123 struct pending_extent_op *tmp;
Josef Bacikf3465ca2008-11-12 14:19:50 -05001124 tmp = list_entry(pos, struct pending_extent_op, list);
1125
1126 /*
1127 * remember tmp->del tells us wether or not we pinned
1128 * down the extent
1129 */
1130 ret = update_block_group(trans, extent_root,
1131 tmp->bytenr, tmp->num_bytes, 0,
1132 tmp->del);
1133 BUG_ON(ret);
1134
Josef Bacikf3465ca2008-11-12 14:19:50 -05001135 list_del_init(&tmp->list);
1136 unlock_extent(&info->extent_ins, tmp->bytenr,
1137 tmp->bytenr + tmp->num_bytes - 1,
1138 GFP_NOFS);
1139 kfree(tmp);
1140 }
1141 } else if (refs && found_extent) {
1142 /*
1143 * the ref and extent were right next to eachother, but the
1144 * extent still has a ref, so just free the backref and keep
1145 * going
1146 */
1147 ret = remove_extent_backref(trans, extent_root, path);
1148 BUG_ON(ret);
1149
1150 list_del_init(&op->list);
1151 unlock_extent(&info->extent_ins, op->bytenr,
1152 op->bytenr + op->num_bytes - 1, GFP_NOFS);
1153 kfree(op);
1154 } else {
1155 /*
1156 * the extent has multiple refs and the backref we were looking
1157 * for was not right next to it, so just unlock and go next,
1158 * we're good to go
1159 */
1160 list_del_init(&op->list);
1161 unlock_extent(&info->extent_ins, op->bytenr,
1162 op->bytenr + op->num_bytes - 1, GFP_NOFS);
1163 kfree(op);
1164 }
1165
1166 btrfs_release_path(extent_root, path);
1167 if (!list_empty(del_list))
1168 goto search;
1169
1170out:
1171 btrfs_free_path(path);
1172 return ret;
1173}
1174
Zheng Yan31840ae2008-09-23 13:14:14 -04001175static int __btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
1176 struct btrfs_root *root, u64 bytenr,
1177 u64 orig_parent, u64 parent,
1178 u64 orig_root, u64 ref_root,
1179 u64 orig_generation, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001180 u64 owner_objectid)
Zheng Yan31840ae2008-09-23 13:14:14 -04001181{
1182 int ret;
1183 struct btrfs_root *extent_root = root->fs_info->extent_root;
1184 struct btrfs_path *path;
1185
1186 if (root == root->fs_info->extent_root) {
1187 struct pending_extent_op *extent_op;
1188 u64 num_bytes;
1189
1190 BUG_ON(owner_objectid >= BTRFS_MAX_LEVEL);
1191 num_bytes = btrfs_level_size(root, (int)owner_objectid);
Josef Bacik25179202008-10-29 14:49:05 -04001192 mutex_lock(&root->fs_info->extent_ins_mutex);
Zheng Yan31840ae2008-09-23 13:14:14 -04001193 if (test_range_bit(&root->fs_info->extent_ins, bytenr,
Josef Bacik25179202008-10-29 14:49:05 -04001194 bytenr + num_bytes - 1, EXTENT_WRITEBACK, 0)) {
Zheng Yan31840ae2008-09-23 13:14:14 -04001195 u64 priv;
1196 ret = get_state_private(&root->fs_info->extent_ins,
1197 bytenr, &priv);
1198 BUG_ON(ret);
1199 extent_op = (struct pending_extent_op *)
1200 (unsigned long)priv;
1201 BUG_ON(extent_op->parent != orig_parent);
1202 BUG_ON(extent_op->generation != orig_generation);
Josef Bacik25179202008-10-29 14:49:05 -04001203
Zheng Yan31840ae2008-09-23 13:14:14 -04001204 extent_op->parent = parent;
1205 extent_op->generation = ref_generation;
1206 } else {
1207 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
1208 BUG_ON(!extent_op);
1209
1210 extent_op->type = PENDING_BACKREF_UPDATE;
1211 extent_op->bytenr = bytenr;
1212 extent_op->num_bytes = num_bytes;
1213 extent_op->parent = parent;
1214 extent_op->orig_parent = orig_parent;
1215 extent_op->generation = ref_generation;
1216 extent_op->orig_generation = orig_generation;
1217 extent_op->level = (int)owner_objectid;
Josef Bacikf3465ca2008-11-12 14:19:50 -05001218 INIT_LIST_HEAD(&extent_op->list);
1219 extent_op->del = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001220
1221 set_extent_bits(&root->fs_info->extent_ins,
1222 bytenr, bytenr + num_bytes - 1,
Josef Bacik25179202008-10-29 14:49:05 -04001223 EXTENT_WRITEBACK, GFP_NOFS);
Zheng Yan31840ae2008-09-23 13:14:14 -04001224 set_state_private(&root->fs_info->extent_ins,
1225 bytenr, (unsigned long)extent_op);
1226 }
Josef Bacik25179202008-10-29 14:49:05 -04001227 mutex_unlock(&root->fs_info->extent_ins_mutex);
Zheng Yan31840ae2008-09-23 13:14:14 -04001228 return 0;
1229 }
1230
1231 path = btrfs_alloc_path();
1232 if (!path)
1233 return -ENOMEM;
1234 ret = lookup_extent_backref(trans, extent_root, path,
1235 bytenr, orig_parent, orig_root,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001236 orig_generation, owner_objectid, 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001237 if (ret)
1238 goto out;
1239 ret = remove_extent_backref(trans, extent_root, path);
1240 if (ret)
1241 goto out;
1242 ret = insert_extent_backref(trans, extent_root, path, bytenr,
1243 parent, ref_root, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001244 owner_objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -04001245 BUG_ON(ret);
Chris Mason87ef2bb2008-10-30 11:23:27 -04001246 finish_current_insert(trans, extent_root, 0);
1247 del_pending_extents(trans, extent_root, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04001248out:
1249 btrfs_free_path(path);
1250 return ret;
1251}
1252
1253int btrfs_update_extent_ref(struct btrfs_trans_handle *trans,
1254 struct btrfs_root *root, u64 bytenr,
1255 u64 orig_parent, u64 parent,
1256 u64 ref_root, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001257 u64 owner_objectid)
Zheng Yan31840ae2008-09-23 13:14:14 -04001258{
1259 int ret;
1260 if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
1261 owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
1262 return 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001263 ret = __btrfs_update_extent_ref(trans, root, bytenr, orig_parent,
1264 parent, ref_root, ref_root,
1265 ref_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001266 owner_objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -04001267 return ret;
1268}
1269
Chris Mason925baed2008-06-25 16:01:30 -04001270static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04001271 struct btrfs_root *root, u64 bytenr,
1272 u64 orig_parent, u64 parent,
1273 u64 orig_root, u64 ref_root,
1274 u64 orig_generation, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001275 u64 owner_objectid)
Chris Mason02217ed2007-03-02 16:08:05 -05001276{
Chris Mason5caf2a02007-04-02 11:20:42 -04001277 struct btrfs_path *path;
Chris Mason02217ed2007-03-02 16:08:05 -05001278 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -04001279 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001280 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -04001281 struct btrfs_extent_item *item;
Chris Masoncf27e1e2007-03-13 09:49:06 -04001282 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -05001283
Chris Mason5caf2a02007-04-02 11:20:42 -04001284 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001285 if (!path)
1286 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -04001287
Chris Mason3c12ac72008-04-21 12:01:38 -04001288 path->reada = 1;
Chris Masondb945352007-10-15 16:15:53 -04001289 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04001290 key.type = BTRFS_EXTENT_ITEM_KEY;
1291 key.offset = (u64)-1;
1292
Chris Mason5caf2a02007-04-02 11:20:42 -04001293 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -04001294 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001295 if (ret < 0)
1296 return ret;
Zheng Yan31840ae2008-09-23 13:14:14 -04001297 BUG_ON(ret == 0 || path->slots[0] == 0);
1298
1299 path->slots[0]--;
Chris Mason5f39d392007-10-15 16:14:19 -04001300 l = path->nodes[0];
Zheng Yan31840ae2008-09-23 13:14:14 -04001301
1302 btrfs_item_key_to_cpu(l, &key, path->slots[0]);
Chris Mason771ed682008-11-06 22:02:51 -05001303 if (key.objectid != bytenr) {
1304 btrfs_print_leaf(root->fs_info->extent_root, path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05001305 printk(KERN_ERR "btrfs wanted %llu found %llu\n",
1306 (unsigned long long)bytenr,
1307 (unsigned long long)key.objectid);
Chris Mason771ed682008-11-06 22:02:51 -05001308 BUG();
1309 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001310 BUG_ON(key.type != BTRFS_EXTENT_ITEM_KEY);
1311
Chris Mason5caf2a02007-04-02 11:20:42 -04001312 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001313 refs = btrfs_extent_refs(l, item);
1314 btrfs_set_extent_refs(l, item, refs + 1);
Chris Mason5caf2a02007-04-02 11:20:42 -04001315 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masona28ec192007-03-06 20:08:01 -05001316
Chris Mason5caf2a02007-04-02 11:20:42 -04001317 btrfs_release_path(root->fs_info->extent_root, path);
Chris Mason7bb86312007-12-11 09:25:06 -05001318
Chris Mason3c12ac72008-04-21 12:01:38 -04001319 path->reada = 1;
Zheng Yan31840ae2008-09-23 13:14:14 -04001320 ret = insert_extent_backref(trans, root->fs_info->extent_root,
1321 path, bytenr, parent,
1322 ref_root, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001323 owner_objectid);
Chris Mason7bb86312007-12-11 09:25:06 -05001324 BUG_ON(ret);
Chris Mason87ef2bb2008-10-30 11:23:27 -04001325 finish_current_insert(trans, root->fs_info->extent_root, 0);
1326 del_pending_extents(trans, root->fs_info->extent_root, 0);
Chris Mason74493f72007-12-11 09:25:06 -05001327
1328 btrfs_free_path(path);
Chris Mason02217ed2007-03-02 16:08:05 -05001329 return 0;
1330}
1331
Chris Mason925baed2008-06-25 16:01:30 -04001332int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04001333 struct btrfs_root *root,
1334 u64 bytenr, u64 num_bytes, u64 parent,
1335 u64 ref_root, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001336 u64 owner_objectid)
Chris Mason925baed2008-06-25 16:01:30 -04001337{
1338 int ret;
Zheng Yan31840ae2008-09-23 13:14:14 -04001339 if (ref_root == BTRFS_TREE_LOG_OBJECTID &&
1340 owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
1341 return 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04001342 ret = __btrfs_inc_extent_ref(trans, root, bytenr, 0, parent,
1343 0, ref_root, 0, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001344 owner_objectid);
Chris Mason925baed2008-06-25 16:01:30 -04001345 return ret;
1346}
1347
Chris Masone9d0b132007-08-10 14:06:19 -04001348int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
1349 struct btrfs_root *root)
1350{
Josef Bacikeb099672009-02-12 09:27:38 -05001351 u64 start;
1352 u64 end;
1353 int ret;
1354
1355 while(1) {
1356 finish_current_insert(trans, root->fs_info->extent_root, 1);
1357 del_pending_extents(trans, root->fs_info->extent_root, 1);
1358
1359 /* is there more work to do? */
1360 ret = find_first_extent_bit(&root->fs_info->pending_del,
1361 0, &start, &end, EXTENT_WRITEBACK);
1362 if (!ret)
1363 continue;
1364 ret = find_first_extent_bit(&root->fs_info->extent_ins,
1365 0, &start, &end, EXTENT_WRITEBACK);
1366 if (!ret)
1367 continue;
1368 break;
1369 }
Chris Masone9d0b132007-08-10 14:06:19 -04001370 return 0;
1371}
1372
Zheng Yan31840ae2008-09-23 13:14:14 -04001373int btrfs_lookup_extent_ref(struct btrfs_trans_handle *trans,
1374 struct btrfs_root *root, u64 bytenr,
1375 u64 num_bytes, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -05001376{
Chris Mason5caf2a02007-04-02 11:20:42 -04001377 struct btrfs_path *path;
Chris Masona28ec192007-03-06 20:08:01 -05001378 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -04001379 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04001380 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -04001381 struct btrfs_extent_item *item;
Chris Mason5caf2a02007-04-02 11:20:42 -04001382
Chris Masondb945352007-10-15 16:15:53 -04001383 WARN_ON(num_bytes < root->sectorsize);
Chris Mason5caf2a02007-04-02 11:20:42 -04001384 path = btrfs_alloc_path();
Chris Mason3c12ac72008-04-21 12:01:38 -04001385 path->reada = 1;
Chris Masondb945352007-10-15 16:15:53 -04001386 key.objectid = bytenr;
1387 key.offset = num_bytes;
Chris Mason62e27492007-03-15 12:56:47 -04001388 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5caf2a02007-04-02 11:20:42 -04001389 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -04001390 0, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001391 if (ret < 0)
1392 goto out;
Chris Mason5f39d392007-10-15 16:14:19 -04001393 if (ret != 0) {
1394 btrfs_print_leaf(root, path->nodes[0]);
Chris Masond3977122009-01-05 21:25:51 -05001395 printk(KERN_INFO "btrfs failed to find block number %llu\n",
1396 (unsigned long long)bytenr);
Chris Masona28ec192007-03-06 20:08:01 -05001397 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -04001398 }
1399 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -04001400 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001401 *refs = btrfs_extent_refs(l, item);
Chris Mason54aa1f42007-06-22 14:16:25 -04001402out:
Chris Mason5caf2a02007-04-02 11:20:42 -04001403 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -05001404 return 0;
1405}
1406
Yan Zheng80ff3852008-10-30 14:20:02 -04001407int btrfs_cross_ref_exist(struct btrfs_trans_handle *trans,
Yan Zheng17d217f2008-12-12 10:03:38 -05001408 struct btrfs_root *root, u64 objectid, u64 bytenr)
Chris Masonbe20aa92007-12-17 20:14:01 -05001409{
1410 struct btrfs_root *extent_root = root->fs_info->extent_root;
1411 struct btrfs_path *path;
Yan Zhengf321e492008-07-30 09:26:11 -04001412 struct extent_buffer *leaf;
1413 struct btrfs_extent_ref *ref_item;
Chris Masonbe20aa92007-12-17 20:14:01 -05001414 struct btrfs_key key;
1415 struct btrfs_key found_key;
Yan Zheng80ff3852008-10-30 14:20:02 -04001416 u64 ref_root;
1417 u64 last_snapshot;
Yan Zhengf321e492008-07-30 09:26:11 -04001418 u32 nritems;
1419 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -05001420
Chris Masonbe20aa92007-12-17 20:14:01 -05001421 key.objectid = bytenr;
Zheng Yan31840ae2008-09-23 13:14:14 -04001422 key.offset = (u64)-1;
Yan Zhengf321e492008-07-30 09:26:11 -04001423 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -05001424
Yan Zhengf321e492008-07-30 09:26:11 -04001425 path = btrfs_alloc_path();
Chris Masonbe20aa92007-12-17 20:14:01 -05001426 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
1427 if (ret < 0)
1428 goto out;
1429 BUG_ON(ret == 0);
Yan Zheng80ff3852008-10-30 14:20:02 -04001430
1431 ret = -ENOENT;
1432 if (path->slots[0] == 0)
Zheng Yan31840ae2008-09-23 13:14:14 -04001433 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05001434
Zheng Yan31840ae2008-09-23 13:14:14 -04001435 path->slots[0]--;
Yan Zhengf321e492008-07-30 09:26:11 -04001436 leaf = path->nodes[0];
1437 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001438
1439 if (found_key.objectid != bytenr ||
Yan Zheng80ff3852008-10-30 14:20:02 -04001440 found_key.type != BTRFS_EXTENT_ITEM_KEY)
Chris Masonbe20aa92007-12-17 20:14:01 -05001441 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05001442
Yan Zheng80ff3852008-10-30 14:20:02 -04001443 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
Chris Masonbe20aa92007-12-17 20:14:01 -05001444 while (1) {
Yan Zhengf321e492008-07-30 09:26:11 -04001445 leaf = path->nodes[0];
1446 nritems = btrfs_header_nritems(leaf);
Chris Masonbe20aa92007-12-17 20:14:01 -05001447 if (path->slots[0] >= nritems) {
1448 ret = btrfs_next_leaf(extent_root, path);
Yan Zhengf321e492008-07-30 09:26:11 -04001449 if (ret < 0)
1450 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -05001451 if (ret == 0)
1452 continue;
1453 break;
1454 }
Yan Zhengf321e492008-07-30 09:26:11 -04001455 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -05001456 if (found_key.objectid != bytenr)
1457 break;
Chris Masonbd098352008-01-03 13:23:19 -05001458
Chris Masonbe20aa92007-12-17 20:14:01 -05001459 if (found_key.type != BTRFS_EXTENT_REF_KEY) {
1460 path->slots[0]++;
1461 continue;
1462 }
1463
Yan Zhengf321e492008-07-30 09:26:11 -04001464 ref_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masonbe20aa92007-12-17 20:14:01 -05001465 struct btrfs_extent_ref);
Yan Zheng80ff3852008-10-30 14:20:02 -04001466 ref_root = btrfs_ref_root(leaf, ref_item);
Yan Zheng17d217f2008-12-12 10:03:38 -05001467 if ((ref_root != root->root_key.objectid &&
1468 ref_root != BTRFS_TREE_LOG_OBJECTID) ||
1469 objectid != btrfs_ref_objectid(leaf, ref_item)) {
Yan Zheng80ff3852008-10-30 14:20:02 -04001470 ret = 1;
1471 goto out;
Yan Zhengf321e492008-07-30 09:26:11 -04001472 }
Yan Zheng80ff3852008-10-30 14:20:02 -04001473 if (btrfs_ref_generation(leaf, ref_item) <= last_snapshot) {
1474 ret = 1;
1475 goto out;
1476 }
Yan Zhengf321e492008-07-30 09:26:11 -04001477
Chris Masonbe20aa92007-12-17 20:14:01 -05001478 path->slots[0]++;
1479 }
Yan Zhengf321e492008-07-30 09:26:11 -04001480 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -05001481out:
Yan Zhengf321e492008-07-30 09:26:11 -04001482 btrfs_free_path(path);
1483 return ret;
1484}
1485
Zheng Yan31840ae2008-09-23 13:14:14 -04001486int btrfs_cache_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
1487 struct extent_buffer *buf, u32 nr_extents)
Chris Mason02217ed2007-03-02 16:08:05 -05001488{
Chris Mason5f39d392007-10-15 16:14:19 -04001489 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04001490 struct btrfs_file_extent_item *fi;
Zheng Yane4657682008-09-26 10:04:53 -04001491 u64 root_gen;
1492 u32 nritems;
Chris Mason02217ed2007-03-02 16:08:05 -05001493 int i;
Chris Masondb945352007-10-15 16:15:53 -04001494 int level;
Zheng Yan31840ae2008-09-23 13:14:14 -04001495 int ret = 0;
Zheng Yane4657682008-09-26 10:04:53 -04001496 int shared = 0;
Chris Masona28ec192007-03-06 20:08:01 -05001497
Chris Mason3768f362007-03-13 16:47:54 -04001498 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -05001499 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001500
Zheng Yane4657682008-09-26 10:04:53 -04001501 if (root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID) {
1502 shared = 0;
1503 root_gen = root->root_key.offset;
1504 } else {
1505 shared = 1;
1506 root_gen = trans->transid - 1;
1507 }
1508
Chris Masondb945352007-10-15 16:15:53 -04001509 level = btrfs_header_level(buf);
Chris Mason5f39d392007-10-15 16:14:19 -04001510 nritems = btrfs_header_nritems(buf);
Chris Mason4a096752008-07-21 10:29:44 -04001511
Zheng Yan31840ae2008-09-23 13:14:14 -04001512 if (level == 0) {
Yan Zheng31153d82008-07-28 15:32:19 -04001513 struct btrfs_leaf_ref *ref;
1514 struct btrfs_extent_info *info;
1515
Zheng Yan31840ae2008-09-23 13:14:14 -04001516 ref = btrfs_alloc_leaf_ref(root, nr_extents);
Yan Zheng31153d82008-07-28 15:32:19 -04001517 if (!ref) {
Zheng Yan31840ae2008-09-23 13:14:14 -04001518 ret = -ENOMEM;
Yan Zheng31153d82008-07-28 15:32:19 -04001519 goto out;
1520 }
1521
Zheng Yane4657682008-09-26 10:04:53 -04001522 ref->root_gen = root_gen;
Yan Zheng31153d82008-07-28 15:32:19 -04001523 ref->bytenr = buf->start;
1524 ref->owner = btrfs_header_owner(buf);
1525 ref->generation = btrfs_header_generation(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04001526 ref->nritems = nr_extents;
Yan Zheng31153d82008-07-28 15:32:19 -04001527 info = ref->extents;
Yanbcc63ab2008-07-30 16:29:20 -04001528
Zheng Yan31840ae2008-09-23 13:14:14 -04001529 for (i = 0; nr_extents > 0 && i < nritems; i++) {
Yan Zheng31153d82008-07-28 15:32:19 -04001530 u64 disk_bytenr;
1531 btrfs_item_key_to_cpu(buf, &key, i);
1532 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1533 continue;
1534 fi = btrfs_item_ptr(buf, i,
1535 struct btrfs_file_extent_item);
1536 if (btrfs_file_extent_type(buf, fi) ==
1537 BTRFS_FILE_EXTENT_INLINE)
1538 continue;
1539 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1540 if (disk_bytenr == 0)
1541 continue;
1542
1543 info->bytenr = disk_bytenr;
1544 info->num_bytes =
1545 btrfs_file_extent_disk_num_bytes(buf, fi);
1546 info->objectid = key.objectid;
1547 info->offset = key.offset;
1548 info++;
1549 }
1550
Zheng Yane4657682008-09-26 10:04:53 -04001551 ret = btrfs_add_leaf_ref(root, ref, shared);
Yan Zheng5b84e8d2008-10-09 11:46:19 -04001552 if (ret == -EEXIST && shared) {
1553 struct btrfs_leaf_ref *old;
1554 old = btrfs_lookup_leaf_ref(root, ref->bytenr);
1555 BUG_ON(!old);
1556 btrfs_remove_leaf_ref(root, old);
1557 btrfs_free_leaf_ref(root, old);
1558 ret = btrfs_add_leaf_ref(root, ref, shared);
1559 }
Yan Zheng31153d82008-07-28 15:32:19 -04001560 WARN_ON(ret);
Yanbcc63ab2008-07-30 16:29:20 -04001561 btrfs_free_leaf_ref(root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04001562 }
1563out:
Zheng Yan31840ae2008-09-23 13:14:14 -04001564 return ret;
1565}
1566
Chris Masonb7a9f292009-02-04 09:23:45 -05001567/* when a block goes through cow, we update the reference counts of
1568 * everything that block points to. The internal pointers of the block
1569 * can be in just about any order, and it is likely to have clusters of
1570 * things that are close together and clusters of things that are not.
1571 *
1572 * To help reduce the seeks that come with updating all of these reference
1573 * counts, sort them by byte number before actual updates are done.
1574 *
1575 * struct refsort is used to match byte number to slot in the btree block.
1576 * we sort based on the byte number and then use the slot to actually
1577 * find the item.
Chris Masonbd56b302009-02-04 09:27:02 -05001578 *
1579 * struct refsort is smaller than strcut btrfs_item and smaller than
1580 * struct btrfs_key_ptr. Since we're currently limited to the page size
1581 * for a btree block, there's no way for a kmalloc of refsorts for a
1582 * single node to be bigger than a page.
Chris Masonb7a9f292009-02-04 09:23:45 -05001583 */
1584struct refsort {
1585 u64 bytenr;
1586 u32 slot;
1587};
1588
1589/*
1590 * for passing into sort()
1591 */
1592static int refsort_cmp(const void *a_void, const void *b_void)
1593{
1594 const struct refsort *a = a_void;
1595 const struct refsort *b = b_void;
1596
1597 if (a->bytenr < b->bytenr)
1598 return -1;
1599 if (a->bytenr > b->bytenr)
1600 return 1;
1601 return 0;
1602}
1603
1604
1605noinline int btrfs_inc_ref(struct btrfs_trans_handle *trans,
1606 struct btrfs_root *root,
1607 struct extent_buffer *orig_buf,
1608 struct extent_buffer *buf, u32 *nr_extents)
Zheng Yan31840ae2008-09-23 13:14:14 -04001609{
1610 u64 bytenr;
1611 u64 ref_root;
1612 u64 orig_root;
1613 u64 ref_generation;
1614 u64 orig_generation;
Chris Masonb7a9f292009-02-04 09:23:45 -05001615 struct refsort *sorted;
Zheng Yan31840ae2008-09-23 13:14:14 -04001616 u32 nritems;
1617 u32 nr_file_extents = 0;
1618 struct btrfs_key key;
1619 struct btrfs_file_extent_item *fi;
1620 int i;
1621 int level;
1622 int ret = 0;
1623 int faili = 0;
Chris Masonb7a9f292009-02-04 09:23:45 -05001624 int refi = 0;
1625 int slot;
Zheng Yan31840ae2008-09-23 13:14:14 -04001626 int (*process_func)(struct btrfs_trans_handle *, struct btrfs_root *,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001627 u64, u64, u64, u64, u64, u64, u64, u64);
Zheng Yan31840ae2008-09-23 13:14:14 -04001628
1629 ref_root = btrfs_header_owner(buf);
1630 ref_generation = btrfs_header_generation(buf);
1631 orig_root = btrfs_header_owner(orig_buf);
1632 orig_generation = btrfs_header_generation(orig_buf);
1633
1634 nritems = btrfs_header_nritems(buf);
1635 level = btrfs_header_level(buf);
1636
Chris Masonb7a9f292009-02-04 09:23:45 -05001637 sorted = kmalloc(sizeof(struct refsort) * nritems, GFP_NOFS);
1638 BUG_ON(!sorted);
1639
Zheng Yan31840ae2008-09-23 13:14:14 -04001640 if (root->ref_cows) {
1641 process_func = __btrfs_inc_extent_ref;
1642 } else {
1643 if (level == 0 &&
1644 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1645 goto out;
1646 if (level != 0 &&
1647 root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
1648 goto out;
1649 process_func = __btrfs_update_extent_ref;
1650 }
1651
Chris Masonb7a9f292009-02-04 09:23:45 -05001652 /*
1653 * we make two passes through the items. In the first pass we
1654 * only record the byte number and slot. Then we sort based on
1655 * byte number and do the actual work based on the sorted results
1656 */
Zheng Yan31840ae2008-09-23 13:14:14 -04001657 for (i = 0; i < nritems; i++) {
1658 cond_resched();
Chris Masondb945352007-10-15 16:15:53 -04001659 if (level == 0) {
Chris Mason5f39d392007-10-15 16:14:19 -04001660 btrfs_item_key_to_cpu(buf, &key, i);
1661 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04001662 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001663 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04001664 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001665 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04001666 BTRFS_FILE_EXTENT_INLINE)
1667 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04001668 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1669 if (bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04001670 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04001671
1672 nr_file_extents++;
Chris Masonb7a9f292009-02-04 09:23:45 -05001673 sorted[refi].bytenr = bytenr;
1674 sorted[refi].slot = i;
1675 refi++;
1676 } else {
1677 bytenr = btrfs_node_blockptr(buf, i);
1678 sorted[refi].bytenr = bytenr;
1679 sorted[refi].slot = i;
1680 refi++;
1681 }
1682 }
1683 /*
1684 * if refi == 0, we didn't actually put anything into the sorted
1685 * array and we're done
1686 */
1687 if (refi == 0)
1688 goto out;
1689
1690 sort(sorted, refi, sizeof(struct refsort), refsort_cmp, NULL);
1691
1692 for (i = 0; i < refi; i++) {
1693 cond_resched();
1694 slot = sorted[i].slot;
1695 bytenr = sorted[i].bytenr;
1696
1697 if (level == 0) {
1698 btrfs_item_key_to_cpu(buf, &key, slot);
Zheng Yan31840ae2008-09-23 13:14:14 -04001699
Zheng Yan31840ae2008-09-23 13:14:14 -04001700 ret = process_func(trans, root, bytenr,
1701 orig_buf->start, buf->start,
1702 orig_root, ref_root,
1703 orig_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001704 key.objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -04001705
1706 if (ret) {
Chris Masonb7a9f292009-02-04 09:23:45 -05001707 faili = slot;
Zheng Yan31840ae2008-09-23 13:14:14 -04001708 WARN_ON(1);
1709 goto fail;
1710 }
Chris Mason54aa1f42007-06-22 14:16:25 -04001711 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04001712 ret = process_func(trans, root, bytenr,
1713 orig_buf->start, buf->start,
1714 orig_root, ref_root,
1715 orig_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001716 level - 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001717 if (ret) {
Chris Masonb7a9f292009-02-04 09:23:45 -05001718 faili = slot;
Zheng Yan31840ae2008-09-23 13:14:14 -04001719 WARN_ON(1);
1720 goto fail;
1721 }
Chris Mason54aa1f42007-06-22 14:16:25 -04001722 }
1723 }
Zheng Yan31840ae2008-09-23 13:14:14 -04001724out:
Chris Masonb7a9f292009-02-04 09:23:45 -05001725 kfree(sorted);
Zheng Yan31840ae2008-09-23 13:14:14 -04001726 if (nr_extents) {
1727 if (level == 0)
1728 *nr_extents = nr_file_extents;
1729 else
1730 *nr_extents = nritems;
1731 }
1732 return 0;
1733fail:
Chris Masonb7a9f292009-02-04 09:23:45 -05001734 kfree(sorted);
Zheng Yan31840ae2008-09-23 13:14:14 -04001735 WARN_ON(1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001736 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05001737}
1738
Zheng Yan31840ae2008-09-23 13:14:14 -04001739int btrfs_update_ref(struct btrfs_trans_handle *trans,
1740 struct btrfs_root *root, struct extent_buffer *orig_buf,
1741 struct extent_buffer *buf, int start_slot, int nr)
1742
1743{
1744 u64 bytenr;
1745 u64 ref_root;
1746 u64 orig_root;
1747 u64 ref_generation;
1748 u64 orig_generation;
1749 struct btrfs_key key;
1750 struct btrfs_file_extent_item *fi;
1751 int i;
1752 int ret;
1753 int slot;
1754 int level;
1755
1756 BUG_ON(start_slot < 0);
1757 BUG_ON(start_slot + nr > btrfs_header_nritems(buf));
1758
1759 ref_root = btrfs_header_owner(buf);
1760 ref_generation = btrfs_header_generation(buf);
1761 orig_root = btrfs_header_owner(orig_buf);
1762 orig_generation = btrfs_header_generation(orig_buf);
1763 level = btrfs_header_level(buf);
1764
1765 if (!root->ref_cows) {
1766 if (level == 0 &&
1767 root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
1768 return 0;
1769 if (level != 0 &&
1770 root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID)
1771 return 0;
1772 }
1773
1774 for (i = 0, slot = start_slot; i < nr; i++, slot++) {
1775 cond_resched();
1776 if (level == 0) {
1777 btrfs_item_key_to_cpu(buf, &key, slot);
1778 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1779 continue;
1780 fi = btrfs_item_ptr(buf, slot,
1781 struct btrfs_file_extent_item);
1782 if (btrfs_file_extent_type(buf, fi) ==
1783 BTRFS_FILE_EXTENT_INLINE)
1784 continue;
1785 bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1786 if (bytenr == 0)
1787 continue;
Zheng Yan31840ae2008-09-23 13:14:14 -04001788 ret = __btrfs_update_extent_ref(trans, root, bytenr,
1789 orig_buf->start, buf->start,
1790 orig_root, ref_root,
1791 orig_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001792 key.objectid);
Zheng Yan31840ae2008-09-23 13:14:14 -04001793 if (ret)
1794 goto fail;
1795 } else {
1796 bytenr = btrfs_node_blockptr(buf, slot);
Zheng Yan31840ae2008-09-23 13:14:14 -04001797 ret = __btrfs_update_extent_ref(trans, root, bytenr,
1798 orig_buf->start, buf->start,
1799 orig_root, ref_root,
1800 orig_generation, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04001801 level - 1);
Zheng Yan31840ae2008-09-23 13:14:14 -04001802 if (ret)
1803 goto fail;
1804 }
1805 }
1806 return 0;
1807fail:
1808 WARN_ON(1);
1809 return -1;
1810}
1811
Chris Mason9078a3e2007-04-26 16:46:15 -04001812static int write_one_cache_group(struct btrfs_trans_handle *trans,
1813 struct btrfs_root *root,
1814 struct btrfs_path *path,
1815 struct btrfs_block_group_cache *cache)
1816{
1817 int ret;
1818 int pending_ret;
1819 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04001820 unsigned long bi;
1821 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04001822
Chris Mason9078a3e2007-04-26 16:46:15 -04001823 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001824 if (ret < 0)
1825 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -04001826 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001827
1828 leaf = path->nodes[0];
1829 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
1830 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
1831 btrfs_mark_buffer_dirty(leaf);
Chris Mason9078a3e2007-04-26 16:46:15 -04001832 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -04001833fail:
Chris Mason87ef2bb2008-10-30 11:23:27 -04001834 finish_current_insert(trans, extent_root, 0);
1835 pending_ret = del_pending_extents(trans, extent_root, 0);
Chris Mason9078a3e2007-04-26 16:46:15 -04001836 if (ret)
1837 return ret;
1838 if (pending_ret)
1839 return pending_ret;
1840 return 0;
1841
1842}
1843
Chris Mason96b51792007-10-15 16:15:19 -04001844int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
1845 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04001846{
Josef Bacik0f9dd462008-09-23 13:14:11 -04001847 struct btrfs_block_group_cache *cache, *entry;
1848 struct rb_node *n;
Chris Mason9078a3e2007-04-26 16:46:15 -04001849 int err = 0;
1850 int werr = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04001851 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04001852 u64 last = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04001853
1854 path = btrfs_alloc_path();
1855 if (!path)
1856 return -ENOMEM;
1857
Chris Masond3977122009-01-05 21:25:51 -05001858 while (1) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001859 cache = NULL;
1860 spin_lock(&root->fs_info->block_group_cache_lock);
1861 for (n = rb_first(&root->fs_info->block_group_cache_tree);
1862 n; n = rb_next(n)) {
1863 entry = rb_entry(n, struct btrfs_block_group_cache,
1864 cache_node);
1865 if (entry->dirty) {
1866 cache = entry;
1867 break;
1868 }
1869 }
1870 spin_unlock(&root->fs_info->block_group_cache_lock);
1871
1872 if (!cache)
Chris Mason9078a3e2007-04-26 16:46:15 -04001873 break;
Chris Mason54aa1f42007-06-22 14:16:25 -04001874
Zheng Yane8569812008-09-26 10:05:48 -04001875 cache->dirty = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001876 last += cache->key.offset;
1877
Chris Mason96b51792007-10-15 16:15:19 -04001878 err = write_one_cache_group(trans, root,
1879 path, cache);
1880 /*
1881 * if we fail to write the cache group, we want
1882 * to keep it marked dirty in hopes that a later
1883 * write will work
1884 */
1885 if (err) {
1886 werr = err;
1887 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04001888 }
1889 }
1890 btrfs_free_path(path);
1891 return werr;
1892}
1893
Yan Zhengd2fb3432008-12-11 16:30:39 -05001894int btrfs_extent_readonly(struct btrfs_root *root, u64 bytenr)
1895{
1896 struct btrfs_block_group_cache *block_group;
1897 int readonly = 0;
1898
1899 block_group = btrfs_lookup_block_group(root->fs_info, bytenr);
1900 if (!block_group || block_group->ro)
1901 readonly = 1;
1902 if (block_group)
1903 put_block_group(block_group);
1904 return readonly;
1905}
1906
Chris Mason593060d2008-03-25 16:50:33 -04001907static int update_space_info(struct btrfs_fs_info *info, u64 flags,
1908 u64 total_bytes, u64 bytes_used,
1909 struct btrfs_space_info **space_info)
1910{
1911 struct btrfs_space_info *found;
1912
1913 found = __find_space_info(info, flags);
1914 if (found) {
Josef Bacik25179202008-10-29 14:49:05 -04001915 spin_lock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04001916 found->total_bytes += total_bytes;
1917 found->bytes_used += bytes_used;
Chris Mason8f18cf12008-04-25 16:53:30 -04001918 found->full = 0;
Josef Bacik25179202008-10-29 14:49:05 -04001919 spin_unlock(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04001920 *space_info = found;
1921 return 0;
1922 }
Yan Zhengc146afa2008-11-12 14:34:12 -05001923 found = kzalloc(sizeof(*found), GFP_NOFS);
Chris Mason593060d2008-03-25 16:50:33 -04001924 if (!found)
1925 return -ENOMEM;
1926
Josef Bacik0f9dd462008-09-23 13:14:11 -04001927 INIT_LIST_HEAD(&found->block_groups);
Josef Bacik80eb2342008-10-29 14:49:05 -04001928 init_rwsem(&found->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001929 spin_lock_init(&found->lock);
Chris Mason593060d2008-03-25 16:50:33 -04001930 found->flags = flags;
1931 found->total_bytes = total_bytes;
1932 found->bytes_used = bytes_used;
1933 found->bytes_pinned = 0;
Zheng Yane8569812008-09-26 10:05:48 -04001934 found->bytes_reserved = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05001935 found->bytes_readonly = 0;
Josef Bacik6a632092009-02-20 11:00:09 -05001936 found->bytes_delalloc = 0;
Chris Mason593060d2008-03-25 16:50:33 -04001937 found->full = 0;
Chris Mason0ef3e662008-05-24 14:04:53 -04001938 found->force_alloc = 0;
Chris Mason593060d2008-03-25 16:50:33 -04001939 *space_info = found;
Chris Mason4184ea72009-03-10 12:39:20 -04001940 list_add_rcu(&found->list, &info->space_info);
Chris Mason593060d2008-03-25 16:50:33 -04001941 return 0;
1942}
1943
Chris Mason8790d502008-04-03 16:29:03 -04001944static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
1945{
1946 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
Chris Mason611f0e02008-04-03 16:29:03 -04001947 BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04001948 BTRFS_BLOCK_GROUP_RAID10 |
Chris Mason611f0e02008-04-03 16:29:03 -04001949 BTRFS_BLOCK_GROUP_DUP);
Chris Mason8790d502008-04-03 16:29:03 -04001950 if (extra_flags) {
1951 if (flags & BTRFS_BLOCK_GROUP_DATA)
1952 fs_info->avail_data_alloc_bits |= extra_flags;
1953 if (flags & BTRFS_BLOCK_GROUP_METADATA)
1954 fs_info->avail_metadata_alloc_bits |= extra_flags;
1955 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
1956 fs_info->avail_system_alloc_bits |= extra_flags;
1957 }
1958}
Chris Mason593060d2008-03-25 16:50:33 -04001959
Yan Zhengc146afa2008-11-12 14:34:12 -05001960static void set_block_group_readonly(struct btrfs_block_group_cache *cache)
1961{
1962 spin_lock(&cache->space_info->lock);
1963 spin_lock(&cache->lock);
1964 if (!cache->ro) {
1965 cache->space_info->bytes_readonly += cache->key.offset -
1966 btrfs_block_group_used(&cache->item);
1967 cache->ro = 1;
1968 }
1969 spin_unlock(&cache->lock);
1970 spin_unlock(&cache->space_info->lock);
1971}
1972
Yan Zheng2b820322008-11-17 21:11:30 -05001973u64 btrfs_reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04001974{
Yan Zheng2b820322008-11-17 21:11:30 -05001975 u64 num_devices = root->fs_info->fs_devices->rw_devices;
Chris Masona061fc82008-05-07 11:43:44 -04001976
1977 if (num_devices == 1)
1978 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
1979 if (num_devices < 4)
1980 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
1981
Chris Masonec44a352008-04-28 15:29:52 -04001982 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
1983 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
Chris Masona061fc82008-05-07 11:43:44 -04001984 BTRFS_BLOCK_GROUP_RAID10))) {
Chris Masonec44a352008-04-28 15:29:52 -04001985 flags &= ~BTRFS_BLOCK_GROUP_DUP;
Chris Masona061fc82008-05-07 11:43:44 -04001986 }
Chris Masonec44a352008-04-28 15:29:52 -04001987
1988 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
Chris Masona061fc82008-05-07 11:43:44 -04001989 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
Chris Masonec44a352008-04-28 15:29:52 -04001990 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
Chris Masona061fc82008-05-07 11:43:44 -04001991 }
Chris Masonec44a352008-04-28 15:29:52 -04001992
1993 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
1994 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
1995 (flags & BTRFS_BLOCK_GROUP_RAID10) |
1996 (flags & BTRFS_BLOCK_GROUP_DUP)))
1997 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
1998 return flags;
1999}
2000
Josef Bacik6a632092009-02-20 11:00:09 -05002001static u64 btrfs_get_alloc_profile(struct btrfs_root *root, u64 data)
2002{
2003 struct btrfs_fs_info *info = root->fs_info;
2004 u64 alloc_profile;
2005
2006 if (data) {
2007 alloc_profile = info->avail_data_alloc_bits &
2008 info->data_alloc_profile;
2009 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
2010 } else if (root == root->fs_info->chunk_root) {
2011 alloc_profile = info->avail_system_alloc_bits &
2012 info->system_alloc_profile;
2013 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
2014 } else {
2015 alloc_profile = info->avail_metadata_alloc_bits &
2016 info->metadata_alloc_profile;
2017 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
2018 }
2019
2020 return btrfs_reduce_alloc_profile(root, data);
2021}
2022
2023void btrfs_set_inode_space_info(struct btrfs_root *root, struct inode *inode)
2024{
2025 u64 alloc_target;
2026
2027 alloc_target = btrfs_get_alloc_profile(root, 1);
2028 BTRFS_I(inode)->space_info = __find_space_info(root->fs_info,
2029 alloc_target);
2030}
2031
2032/*
2033 * for now this just makes sure we have at least 5% of our metadata space free
2034 * for use.
2035 */
2036int btrfs_check_metadata_free_space(struct btrfs_root *root)
2037{
2038 struct btrfs_fs_info *info = root->fs_info;
2039 struct btrfs_space_info *meta_sinfo;
2040 u64 alloc_target, thresh;
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002041 int committed = 0, ret;
Josef Bacik6a632092009-02-20 11:00:09 -05002042
2043 /* get the space info for where the metadata will live */
2044 alloc_target = btrfs_get_alloc_profile(root, 0);
2045 meta_sinfo = __find_space_info(info, alloc_target);
2046
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002047again:
Josef Bacik6a632092009-02-20 11:00:09 -05002048 spin_lock(&meta_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002049 if (!meta_sinfo->full)
2050 thresh = meta_sinfo->total_bytes * 80;
2051 else
2052 thresh = meta_sinfo->total_bytes * 95;
Josef Bacik6a632092009-02-20 11:00:09 -05002053
2054 do_div(thresh, 100);
2055
2056 if (meta_sinfo->bytes_used + meta_sinfo->bytes_reserved +
2057 meta_sinfo->bytes_pinned + meta_sinfo->bytes_readonly > thresh) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002058 struct btrfs_trans_handle *trans;
2059 if (!meta_sinfo->full) {
2060 meta_sinfo->force_alloc = 1;
2061 spin_unlock(&meta_sinfo->lock);
2062
2063 trans = btrfs_start_transaction(root, 1);
2064 if (!trans)
2065 return -ENOMEM;
2066
2067 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
2068 2 * 1024 * 1024, alloc_target, 0);
2069 btrfs_end_transaction(trans, root);
2070 goto again;
2071 }
Josef Bacik6a632092009-02-20 11:00:09 -05002072 spin_unlock(&meta_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002073
2074 if (!committed) {
2075 committed = 1;
2076 trans = btrfs_join_transaction(root, 1);
2077 if (!trans)
2078 return -ENOMEM;
2079 ret = btrfs_commit_transaction(trans, root);
2080 if (ret)
2081 return ret;
2082 goto again;
2083 }
Josef Bacik6a632092009-02-20 11:00:09 -05002084 return -ENOSPC;
2085 }
2086 spin_unlock(&meta_sinfo->lock);
2087
2088 return 0;
2089}
2090
2091/*
2092 * This will check the space that the inode allocates from to make sure we have
2093 * enough space for bytes.
2094 */
2095int btrfs_check_data_free_space(struct btrfs_root *root, struct inode *inode,
2096 u64 bytes)
2097{
2098 struct btrfs_space_info *data_sinfo;
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002099 int ret = 0, committed = 0;
Josef Bacik6a632092009-02-20 11:00:09 -05002100
2101 /* make sure bytes are sectorsize aligned */
2102 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
2103
2104 data_sinfo = BTRFS_I(inode)->space_info;
2105again:
2106 /* make sure we have enough space to handle the data first */
2107 spin_lock(&data_sinfo->lock);
2108 if (data_sinfo->total_bytes - data_sinfo->bytes_used -
2109 data_sinfo->bytes_delalloc - data_sinfo->bytes_reserved -
2110 data_sinfo->bytes_pinned - data_sinfo->bytes_readonly -
2111 data_sinfo->bytes_may_use < bytes) {
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002112 struct btrfs_trans_handle *trans;
2113
Josef Bacik6a632092009-02-20 11:00:09 -05002114 /*
2115 * if we don't have enough free bytes in this space then we need
2116 * to alloc a new chunk.
2117 */
2118 if (!data_sinfo->full) {
2119 u64 alloc_target;
Josef Bacik6a632092009-02-20 11:00:09 -05002120
2121 data_sinfo->force_alloc = 1;
2122 spin_unlock(&data_sinfo->lock);
2123
2124 alloc_target = btrfs_get_alloc_profile(root, 1);
2125 trans = btrfs_start_transaction(root, 1);
2126 if (!trans)
2127 return -ENOMEM;
2128
2129 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
2130 bytes + 2 * 1024 * 1024,
2131 alloc_target, 0);
2132 btrfs_end_transaction(trans, root);
2133 if (ret)
2134 return ret;
2135 goto again;
2136 }
2137 spin_unlock(&data_sinfo->lock);
Josef Bacik4e06bdd2009-02-20 10:59:53 -05002138
2139 /* commit the current transaction and try again */
2140 if (!committed) {
2141 committed = 1;
2142 trans = btrfs_join_transaction(root, 1);
2143 if (!trans)
2144 return -ENOMEM;
2145 ret = btrfs_commit_transaction(trans, root);
2146 if (ret)
2147 return ret;
2148 goto again;
2149 }
2150
Josef Bacik6a632092009-02-20 11:00:09 -05002151 printk(KERN_ERR "no space left, need %llu, %llu delalloc bytes"
2152 ", %llu bytes_used, %llu bytes_reserved, "
2153 "%llu bytes_pinned, %llu bytes_readonly, %llu may use"
2154 "%llu total\n", bytes, data_sinfo->bytes_delalloc,
2155 data_sinfo->bytes_used, data_sinfo->bytes_reserved,
2156 data_sinfo->bytes_pinned, data_sinfo->bytes_readonly,
2157 data_sinfo->bytes_may_use, data_sinfo->total_bytes);
2158 return -ENOSPC;
2159 }
2160 data_sinfo->bytes_may_use += bytes;
2161 BTRFS_I(inode)->reserved_bytes += bytes;
2162 spin_unlock(&data_sinfo->lock);
2163
2164 return btrfs_check_metadata_free_space(root);
2165}
2166
2167/*
2168 * if there was an error for whatever reason after calling
2169 * btrfs_check_data_free_space, call this so we can cleanup the counters.
2170 */
2171void btrfs_free_reserved_data_space(struct btrfs_root *root,
2172 struct inode *inode, u64 bytes)
2173{
2174 struct btrfs_space_info *data_sinfo;
2175
2176 /* make sure bytes are sectorsize aligned */
2177 bytes = (bytes + root->sectorsize - 1) & ~((u64)root->sectorsize - 1);
2178
2179 data_sinfo = BTRFS_I(inode)->space_info;
2180 spin_lock(&data_sinfo->lock);
2181 data_sinfo->bytes_may_use -= bytes;
2182 BTRFS_I(inode)->reserved_bytes -= bytes;
2183 spin_unlock(&data_sinfo->lock);
2184}
2185
2186/* called when we are adding a delalloc extent to the inode's io_tree */
2187void btrfs_delalloc_reserve_space(struct btrfs_root *root, struct inode *inode,
2188 u64 bytes)
2189{
2190 struct btrfs_space_info *data_sinfo;
2191
2192 /* get the space info for where this inode will be storing its data */
2193 data_sinfo = BTRFS_I(inode)->space_info;
2194
2195 /* make sure we have enough space to handle the data first */
2196 spin_lock(&data_sinfo->lock);
2197 data_sinfo->bytes_delalloc += bytes;
2198
2199 /*
2200 * we are adding a delalloc extent without calling
2201 * btrfs_check_data_free_space first. This happens on a weird
2202 * writepage condition, but shouldn't hurt our accounting
2203 */
2204 if (unlikely(bytes > BTRFS_I(inode)->reserved_bytes)) {
2205 data_sinfo->bytes_may_use -= BTRFS_I(inode)->reserved_bytes;
2206 BTRFS_I(inode)->reserved_bytes = 0;
2207 } else {
2208 data_sinfo->bytes_may_use -= bytes;
2209 BTRFS_I(inode)->reserved_bytes -= bytes;
2210 }
2211
2212 spin_unlock(&data_sinfo->lock);
2213}
2214
2215/* called when we are clearing an delalloc extent from the inode's io_tree */
2216void btrfs_delalloc_free_space(struct btrfs_root *root, struct inode *inode,
2217 u64 bytes)
2218{
2219 struct btrfs_space_info *info;
2220
2221 info = BTRFS_I(inode)->space_info;
2222
2223 spin_lock(&info->lock);
2224 info->bytes_delalloc -= bytes;
2225 spin_unlock(&info->lock);
2226}
2227
Chris Mason6324fbf2008-03-24 15:01:59 -04002228static int do_chunk_alloc(struct btrfs_trans_handle *trans,
2229 struct btrfs_root *extent_root, u64 alloc_bytes,
Chris Mason0ef3e662008-05-24 14:04:53 -04002230 u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04002231{
2232 struct btrfs_space_info *space_info;
2233 u64 thresh;
Yan Zhengc146afa2008-11-12 14:34:12 -05002234 int ret = 0;
2235
2236 mutex_lock(&extent_root->fs_info->chunk_mutex);
Chris Mason6324fbf2008-03-24 15:01:59 -04002237
Yan Zheng2b820322008-11-17 21:11:30 -05002238 flags = btrfs_reduce_alloc_profile(extent_root, flags);
Chris Masonec44a352008-04-28 15:29:52 -04002239
Chris Mason6324fbf2008-03-24 15:01:59 -04002240 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04002241 if (!space_info) {
2242 ret = update_space_info(extent_root->fs_info, flags,
2243 0, 0, &space_info);
2244 BUG_ON(ret);
2245 }
Chris Mason6324fbf2008-03-24 15:01:59 -04002246 BUG_ON(!space_info);
2247
Josef Bacik25179202008-10-29 14:49:05 -04002248 spin_lock(&space_info->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04002249 if (space_info->force_alloc) {
2250 force = 1;
2251 space_info->force_alloc = 0;
2252 }
Josef Bacik25179202008-10-29 14:49:05 -04002253 if (space_info->full) {
2254 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04002255 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04002256 }
Chris Mason6324fbf2008-03-24 15:01:59 -04002257
Yan Zhengc146afa2008-11-12 14:34:12 -05002258 thresh = space_info->total_bytes - space_info->bytes_readonly;
2259 thresh = div_factor(thresh, 6);
Chris Mason0ef3e662008-05-24 14:04:53 -04002260 if (!force &&
Zheng Yane8569812008-09-26 10:05:48 -04002261 (space_info->bytes_used + space_info->bytes_pinned +
Josef Bacik25179202008-10-29 14:49:05 -04002262 space_info->bytes_reserved + alloc_bytes) < thresh) {
2263 spin_unlock(&space_info->lock);
Chris Mason925baed2008-06-25 16:01:30 -04002264 goto out;
Josef Bacik25179202008-10-29 14:49:05 -04002265 }
Josef Bacik25179202008-10-29 14:49:05 -04002266 spin_unlock(&space_info->lock);
2267
Yan Zheng2b820322008-11-17 21:11:30 -05002268 ret = btrfs_alloc_chunk(trans, extent_root, flags);
Chris Masond3977122009-01-05 21:25:51 -05002269 if (ret)
Chris Mason6324fbf2008-03-24 15:01:59 -04002270 space_info->full = 1;
Chris Masona74a4b92008-06-25 16:01:31 -04002271out:
Yan Zhengc146afa2008-11-12 14:34:12 -05002272 mutex_unlock(&extent_root->fs_info->chunk_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002273 return ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04002274}
2275
Chris Mason9078a3e2007-04-26 16:46:15 -04002276static int update_block_group(struct btrfs_trans_handle *trans,
2277 struct btrfs_root *root,
Chris Masondb945352007-10-15 16:15:53 -04002278 u64 bytenr, u64 num_bytes, int alloc,
Chris Mason0b86a832008-03-24 15:01:56 -04002279 int mark_free)
Chris Mason9078a3e2007-04-26 16:46:15 -04002280{
2281 struct btrfs_block_group_cache *cache;
2282 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04002283 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04002284 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04002285 u64 byte_in_group;
Chris Mason3e1ad542007-05-07 20:03:49 -04002286
Chris Masond3977122009-01-05 21:25:51 -05002287 while (total) {
Chris Masondb945352007-10-15 16:15:53 -04002288 cache = btrfs_lookup_block_group(info, bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05002289 if (!cache)
Chris Mason9078a3e2007-04-26 16:46:15 -04002290 return -1;
Chris Masondb945352007-10-15 16:15:53 -04002291 byte_in_group = bytenr - cache->key.objectid;
2292 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason9078a3e2007-04-26 16:46:15 -04002293
Josef Bacik25179202008-10-29 14:49:05 -04002294 spin_lock(&cache->space_info->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04002295 spin_lock(&cache->lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04002296 cache->dirty = 1;
Chris Mason9078a3e2007-04-26 16:46:15 -04002297 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04002298 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04002299 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04002300 old_val += num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04002301 cache->space_info->bytes_used += num_bytes;
Yan Zhenga512bbf2008-12-08 16:46:26 -05002302 if (cache->ro)
Yan Zhengc146afa2008-11-12 14:34:12 -05002303 cache->space_info->bytes_readonly -= num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04002304 btrfs_set_block_group_used(&cache->item, old_val);
2305 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04002306 spin_unlock(&cache->space_info->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04002307 } else {
Chris Masondb945352007-10-15 16:15:53 -04002308 old_val -= num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04002309 cache->space_info->bytes_used -= num_bytes;
Yan Zhengc146afa2008-11-12 14:34:12 -05002310 if (cache->ro)
2311 cache->space_info->bytes_readonly += num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04002312 btrfs_set_block_group_used(&cache->item, old_val);
2313 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04002314 spin_unlock(&cache->space_info->lock);
Chris Masonf510cfe2007-10-15 16:14:48 -04002315 if (mark_free) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04002316 int ret;
Liu Hui1f3c79a2009-01-05 15:57:51 -05002317
2318 ret = btrfs_discard_extent(root, bytenr,
2319 num_bytes);
2320 WARN_ON(ret);
2321
Josef Bacik0f9dd462008-09-23 13:14:11 -04002322 ret = btrfs_add_free_space(cache, bytenr,
2323 num_bytes);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002324 WARN_ON(ret);
Chris Masone37c9e62007-05-09 20:13:14 -04002325 }
Chris Masoncd1bc462007-04-27 10:08:34 -04002326 }
Yan Zhengd2fb3432008-12-11 16:30:39 -05002327 put_block_group(cache);
Chris Masondb945352007-10-15 16:15:53 -04002328 total -= num_bytes;
2329 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04002330 }
2331 return 0;
2332}
Chris Mason6324fbf2008-03-24 15:01:59 -04002333
Chris Masona061fc82008-05-07 11:43:44 -04002334static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
2335{
Josef Bacik0f9dd462008-09-23 13:14:11 -04002336 struct btrfs_block_group_cache *cache;
Yan Zhengd2fb3432008-12-11 16:30:39 -05002337 u64 bytenr;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002338
2339 cache = btrfs_lookup_first_block_group(root->fs_info, search_start);
2340 if (!cache)
Chris Masona061fc82008-05-07 11:43:44 -04002341 return 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002342
Yan Zhengd2fb3432008-12-11 16:30:39 -05002343 bytenr = cache->key.objectid;
2344 put_block_group(cache);
2345
2346 return bytenr;
Chris Masona061fc82008-05-07 11:43:44 -04002347}
2348
Chris Masone02119d2008-09-05 16:13:11 -04002349int btrfs_update_pinned_extents(struct btrfs_root *root,
Yan324ae4d2007-11-16 14:57:08 -05002350 u64 bytenr, u64 num, int pin)
2351{
2352 u64 len;
2353 struct btrfs_block_group_cache *cache;
2354 struct btrfs_fs_info *fs_info = root->fs_info;
2355
Josef Bacik25179202008-10-29 14:49:05 -04002356 WARN_ON(!mutex_is_locked(&root->fs_info->pinned_mutex));
Yan324ae4d2007-11-16 14:57:08 -05002357 if (pin) {
2358 set_extent_dirty(&fs_info->pinned_extents,
2359 bytenr, bytenr + num - 1, GFP_NOFS);
2360 } else {
2361 clear_extent_dirty(&fs_info->pinned_extents,
2362 bytenr, bytenr + num - 1, GFP_NOFS);
2363 }
2364 while (num > 0) {
2365 cache = btrfs_lookup_block_group(fs_info, bytenr);
Zheng Yane8569812008-09-26 10:05:48 -04002366 BUG_ON(!cache);
2367 len = min(num, cache->key.offset -
2368 (bytenr - cache->key.objectid));
Yan324ae4d2007-11-16 14:57:08 -05002369 if (pin) {
Josef Bacik25179202008-10-29 14:49:05 -04002370 spin_lock(&cache->space_info->lock);
Zheng Yane8569812008-09-26 10:05:48 -04002371 spin_lock(&cache->lock);
2372 cache->pinned += len;
2373 cache->space_info->bytes_pinned += len;
2374 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04002375 spin_unlock(&cache->space_info->lock);
Yan324ae4d2007-11-16 14:57:08 -05002376 fs_info->total_pinned += len;
2377 } else {
Josef Bacik25179202008-10-29 14:49:05 -04002378 spin_lock(&cache->space_info->lock);
Zheng Yane8569812008-09-26 10:05:48 -04002379 spin_lock(&cache->lock);
2380 cache->pinned -= len;
2381 cache->space_info->bytes_pinned -= len;
2382 spin_unlock(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04002383 spin_unlock(&cache->space_info->lock);
Yan324ae4d2007-11-16 14:57:08 -05002384 fs_info->total_pinned -= len;
Josef Bacik07103a32008-11-19 15:17:55 -05002385 if (cache->cached)
2386 btrfs_add_free_space(cache, bytenr, len);
Yan324ae4d2007-11-16 14:57:08 -05002387 }
Yan Zhengd2fb3432008-12-11 16:30:39 -05002388 put_block_group(cache);
Yan324ae4d2007-11-16 14:57:08 -05002389 bytenr += len;
2390 num -= len;
2391 }
2392 return 0;
2393}
Chris Mason9078a3e2007-04-26 16:46:15 -04002394
Zheng Yane8569812008-09-26 10:05:48 -04002395static int update_reserved_extents(struct btrfs_root *root,
2396 u64 bytenr, u64 num, int reserve)
2397{
2398 u64 len;
2399 struct btrfs_block_group_cache *cache;
2400 struct btrfs_fs_info *fs_info = root->fs_info;
2401
Zheng Yane8569812008-09-26 10:05:48 -04002402 while (num > 0) {
2403 cache = btrfs_lookup_block_group(fs_info, bytenr);
2404 BUG_ON(!cache);
2405 len = min(num, cache->key.offset -
2406 (bytenr - cache->key.objectid));
Josef Bacik25179202008-10-29 14:49:05 -04002407
2408 spin_lock(&cache->space_info->lock);
2409 spin_lock(&cache->lock);
Zheng Yane8569812008-09-26 10:05:48 -04002410 if (reserve) {
Zheng Yane8569812008-09-26 10:05:48 -04002411 cache->reserved += len;
2412 cache->space_info->bytes_reserved += len;
Zheng Yane8569812008-09-26 10:05:48 -04002413 } else {
Zheng Yane8569812008-09-26 10:05:48 -04002414 cache->reserved -= len;
2415 cache->space_info->bytes_reserved -= len;
Zheng Yane8569812008-09-26 10:05:48 -04002416 }
Josef Bacik25179202008-10-29 14:49:05 -04002417 spin_unlock(&cache->lock);
2418 spin_unlock(&cache->space_info->lock);
Yan Zhengd2fb3432008-12-11 16:30:39 -05002419 put_block_group(cache);
Zheng Yane8569812008-09-26 10:05:48 -04002420 bytenr += len;
2421 num -= len;
2422 }
2423 return 0;
2424}
2425
Chris Masond1310b22008-01-24 16:13:08 -05002426int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
Chris Masonccd467d2007-06-28 15:57:36 -04002427{
Chris Masonccd467d2007-06-28 15:57:36 -04002428 u64 last = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -04002429 u64 start;
2430 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -05002431 struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
Chris Masonccd467d2007-06-28 15:57:36 -04002432 int ret;
Chris Masonccd467d2007-06-28 15:57:36 -04002433
Josef Bacik25179202008-10-29 14:49:05 -04002434 mutex_lock(&root->fs_info->pinned_mutex);
Chris Masond3977122009-01-05 21:25:51 -05002435 while (1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04002436 ret = find_first_extent_bit(pinned_extents, last,
2437 &start, &end, EXTENT_DIRTY);
2438 if (ret)
Chris Masonccd467d2007-06-28 15:57:36 -04002439 break;
Chris Mason1a5bc162007-10-15 16:15:26 -04002440 set_extent_dirty(copy, start, end, GFP_NOFS);
2441 last = end + 1;
Chris Masonccd467d2007-06-28 15:57:36 -04002442 }
Josef Bacik25179202008-10-29 14:49:05 -04002443 mutex_unlock(&root->fs_info->pinned_mutex);
Chris Masonccd467d2007-06-28 15:57:36 -04002444 return 0;
2445}
2446
2447int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
2448 struct btrfs_root *root,
Chris Masond1310b22008-01-24 16:13:08 -05002449 struct extent_io_tree *unpin)
Chris Masona28ec192007-03-06 20:08:01 -05002450{
Chris Mason1a5bc162007-10-15 16:15:26 -04002451 u64 start;
2452 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05002453 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05002454
Josef Bacik25179202008-10-29 14:49:05 -04002455 mutex_lock(&root->fs_info->pinned_mutex);
Chris Masond3977122009-01-05 21:25:51 -05002456 while (1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04002457 ret = find_first_extent_bit(unpin, 0, &start, &end,
2458 EXTENT_DIRTY);
2459 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05002460 break;
Liu Hui1f3c79a2009-01-05 15:57:51 -05002461
2462 ret = btrfs_discard_extent(root, start, end + 1 - start);
2463
Chris Masone02119d2008-09-05 16:13:11 -04002464 btrfs_update_pinned_extents(root, start, end + 1 - start, 0);
Chris Mason1a5bc162007-10-15 16:15:26 -04002465 clear_extent_dirty(unpin, start, end, GFP_NOFS);
Liu Hui1f3c79a2009-01-05 15:57:51 -05002466
Chris Masonc286ac42008-07-22 23:06:41 -04002467 if (need_resched()) {
Josef Bacik25179202008-10-29 14:49:05 -04002468 mutex_unlock(&root->fs_info->pinned_mutex);
Chris Masonc286ac42008-07-22 23:06:41 -04002469 cond_resched();
Josef Bacik25179202008-10-29 14:49:05 -04002470 mutex_lock(&root->fs_info->pinned_mutex);
Chris Masonc286ac42008-07-22 23:06:41 -04002471 }
Chris Masona28ec192007-03-06 20:08:01 -05002472 }
Josef Bacik25179202008-10-29 14:49:05 -04002473 mutex_unlock(&root->fs_info->pinned_mutex);
Liu Hui1f3c79a2009-01-05 15:57:51 -05002474 return ret;
Chris Masona28ec192007-03-06 20:08:01 -05002475}
2476
Chris Mason98ed5172008-01-03 10:01:48 -05002477static int finish_current_insert(struct btrfs_trans_handle *trans,
Chris Mason87ef2bb2008-10-30 11:23:27 -04002478 struct btrfs_root *extent_root, int all)
Chris Mason037e6392007-03-07 11:50:24 -05002479{
Chris Mason7bb86312007-12-11 09:25:06 -05002480 u64 start;
2481 u64 end;
Zheng Yan31840ae2008-09-23 13:14:14 -04002482 u64 priv;
Josef Bacik25179202008-10-29 14:49:05 -04002483 u64 search = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05002484 struct btrfs_fs_info *info = extent_root->fs_info;
2485 struct btrfs_path *path;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002486 struct pending_extent_op *extent_op, *tmp;
2487 struct list_head insert_list, update_list;
Chris Mason037e6392007-03-07 11:50:24 -05002488 int ret;
Josef Bacikeb099672009-02-12 09:27:38 -05002489 int num_inserts = 0, max_inserts, restart = 0;
Chris Mason037e6392007-03-07 11:50:24 -05002490
Chris Mason7bb86312007-12-11 09:25:06 -05002491 path = btrfs_alloc_path();
Josef Bacikf3465ca2008-11-12 14:19:50 -05002492 INIT_LIST_HEAD(&insert_list);
2493 INIT_LIST_HEAD(&update_list);
Chris Mason037e6392007-03-07 11:50:24 -05002494
Josef Bacikf3465ca2008-11-12 14:19:50 -05002495 max_inserts = extent_root->leafsize /
2496 (2 * sizeof(struct btrfs_key) + 2 * sizeof(struct btrfs_item) +
2497 sizeof(struct btrfs_extent_ref) +
2498 sizeof(struct btrfs_extent_item));
2499again:
2500 mutex_lock(&info->extent_ins_mutex);
2501 while (1) {
Josef Bacik25179202008-10-29 14:49:05 -04002502 ret = find_first_extent_bit(&info->extent_ins, search, &start,
2503 &end, EXTENT_WRITEBACK);
2504 if (ret) {
Josef Bacikeb099672009-02-12 09:27:38 -05002505 if (restart && !num_inserts &&
Yan Zheng5a7be512009-01-21 10:49:16 -05002506 list_empty(&update_list)) {
Josef Bacikeb099672009-02-12 09:27:38 -05002507 restart = 0;
Liu Huib4eec2c2008-11-18 11:30:10 -05002508 search = 0;
Josef Bacik25179202008-10-29 14:49:05 -04002509 continue;
2510 }
Chris Mason26b80032007-08-08 20:17:12 -04002511 break;
Josef Bacik25179202008-10-29 14:49:05 -04002512 }
2513
2514 ret = try_lock_extent(&info->extent_ins, start, end, GFP_NOFS);
2515 if (!ret) {
Josef Bacikeb099672009-02-12 09:27:38 -05002516 if (all)
2517 restart = 1;
Chris Mason87ef2bb2008-10-30 11:23:27 -04002518 search = end + 1;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002519 if (need_resched()) {
2520 mutex_unlock(&info->extent_ins_mutex);
2521 cond_resched();
2522 mutex_lock(&info->extent_ins_mutex);
2523 }
Josef Bacik25179202008-10-29 14:49:05 -04002524 continue;
2525 }
Chris Mason26b80032007-08-08 20:17:12 -04002526
Zheng Yan31840ae2008-09-23 13:14:14 -04002527 ret = get_state_private(&info->extent_ins, start, &priv);
2528 BUG_ON(ret);
Josef Bacikf3465ca2008-11-12 14:19:50 -05002529 extent_op = (struct pending_extent_op *)(unsigned long) priv;
Josef Bacik25179202008-10-29 14:49:05 -04002530
Zheng Yan31840ae2008-09-23 13:14:14 -04002531 if (extent_op->type == PENDING_EXTENT_INSERT) {
Josef Bacikf3465ca2008-11-12 14:19:50 -05002532 num_inserts++;
2533 list_add_tail(&extent_op->list, &insert_list);
Chris Mason87ef2bb2008-10-30 11:23:27 -04002534 search = end + 1;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002535 if (num_inserts == max_inserts) {
Josef Bacikeb099672009-02-12 09:27:38 -05002536 restart = 1;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002537 break;
2538 }
2539 } else if (extent_op->type == PENDING_BACKREF_UPDATE) {
2540 list_add_tail(&extent_op->list, &update_list);
2541 search = end + 1;
2542 } else {
2543 BUG();
2544 }
Chris Mason037e6392007-03-07 11:50:24 -05002545 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05002546
2547 /*
Liu Huib4eec2c2008-11-18 11:30:10 -05002548 * process the update list, clear the writeback bit for it, and if
Josef Bacikf3465ca2008-11-12 14:19:50 -05002549 * somebody marked this thing for deletion then just unlock it and be
2550 * done, the free_extents will handle it
2551 */
Josef Bacikf3465ca2008-11-12 14:19:50 -05002552 list_for_each_entry_safe(extent_op, tmp, &update_list, list) {
2553 clear_extent_bits(&info->extent_ins, extent_op->bytenr,
2554 extent_op->bytenr + extent_op->num_bytes - 1,
2555 EXTENT_WRITEBACK, GFP_NOFS);
2556 if (extent_op->del) {
2557 list_del_init(&extent_op->list);
2558 unlock_extent(&info->extent_ins, extent_op->bytenr,
2559 extent_op->bytenr + extent_op->num_bytes
2560 - 1, GFP_NOFS);
2561 kfree(extent_op);
2562 }
2563 }
2564 mutex_unlock(&info->extent_ins_mutex);
2565
2566 /*
2567 * still have things left on the update list, go ahead an update
2568 * everything
2569 */
2570 if (!list_empty(&update_list)) {
2571 ret = update_backrefs(trans, extent_root, path, &update_list);
2572 BUG_ON(ret);
Josef Bacikeb099672009-02-12 09:27:38 -05002573
2574 /* we may have COW'ed new blocks, so lets start over */
2575 if (all)
2576 restart = 1;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002577 }
2578
2579 /*
2580 * if no inserts need to be done, but we skipped some extents and we
2581 * need to make sure everything is cleaned then reset everything and
2582 * go back to the beginning
2583 */
Josef Bacikeb099672009-02-12 09:27:38 -05002584 if (!num_inserts && restart) {
Josef Bacikf3465ca2008-11-12 14:19:50 -05002585 search = 0;
Josef Bacikeb099672009-02-12 09:27:38 -05002586 restart = 0;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002587 INIT_LIST_HEAD(&update_list);
2588 INIT_LIST_HEAD(&insert_list);
2589 goto again;
2590 } else if (!num_inserts) {
2591 goto out;
2592 }
2593
2594 /*
2595 * process the insert extents list. Again if we are deleting this
2596 * extent, then just unlock it, pin down the bytes if need be, and be
2597 * done with it. Saves us from having to actually insert the extent
2598 * into the tree and then subsequently come along and delete it
2599 */
2600 mutex_lock(&info->extent_ins_mutex);
2601 list_for_each_entry_safe(extent_op, tmp, &insert_list, list) {
2602 clear_extent_bits(&info->extent_ins, extent_op->bytenr,
2603 extent_op->bytenr + extent_op->num_bytes - 1,
2604 EXTENT_WRITEBACK, GFP_NOFS);
2605 if (extent_op->del) {
Yan Zheng34bf63c2008-12-19 10:58:46 -05002606 u64 used;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002607 list_del_init(&extent_op->list);
2608 unlock_extent(&info->extent_ins, extent_op->bytenr,
2609 extent_op->bytenr + extent_op->num_bytes
2610 - 1, GFP_NOFS);
2611
2612 mutex_lock(&extent_root->fs_info->pinned_mutex);
2613 ret = pin_down_bytes(trans, extent_root,
2614 extent_op->bytenr,
2615 extent_op->num_bytes, 0);
2616 mutex_unlock(&extent_root->fs_info->pinned_mutex);
2617
Yan Zheng34bf63c2008-12-19 10:58:46 -05002618 spin_lock(&info->delalloc_lock);
2619 used = btrfs_super_bytes_used(&info->super_copy);
2620 btrfs_set_super_bytes_used(&info->super_copy,
2621 used - extent_op->num_bytes);
2622 used = btrfs_root_used(&extent_root->root_item);
2623 btrfs_set_root_used(&extent_root->root_item,
2624 used - extent_op->num_bytes);
2625 spin_unlock(&info->delalloc_lock);
2626
Josef Bacikf3465ca2008-11-12 14:19:50 -05002627 ret = update_block_group(trans, extent_root,
2628 extent_op->bytenr,
2629 extent_op->num_bytes,
2630 0, ret > 0);
2631 BUG_ON(ret);
2632 kfree(extent_op);
2633 num_inserts--;
2634 }
2635 }
2636 mutex_unlock(&info->extent_ins_mutex);
2637
2638 ret = insert_extents(trans, extent_root, path, &insert_list,
2639 num_inserts);
2640 BUG_ON(ret);
2641
2642 /*
Josef Bacikeb099672009-02-12 09:27:38 -05002643 * if restart is set for whatever reason we need to go back and start
2644 * searching through the pending list again.
2645 *
2646 * We just inserted some extents, which could have resulted in new
2647 * blocks being allocated, which would result in new blocks needing
2648 * updates, so if all is set we _must_ restart to get the updated
2649 * blocks.
Josef Bacikf3465ca2008-11-12 14:19:50 -05002650 */
Josef Bacikeb099672009-02-12 09:27:38 -05002651 if (restart || all) {
Josef Bacikf3465ca2008-11-12 14:19:50 -05002652 INIT_LIST_HEAD(&insert_list);
2653 INIT_LIST_HEAD(&update_list);
2654 search = 0;
Josef Bacikeb099672009-02-12 09:27:38 -05002655 restart = 0;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002656 num_inserts = 0;
2657 goto again;
2658 }
2659out:
Chris Mason7bb86312007-12-11 09:25:06 -05002660 btrfs_free_path(path);
Chris Mason037e6392007-03-07 11:50:24 -05002661 return 0;
2662}
2663
Zheng Yan31840ae2008-09-23 13:14:14 -04002664static int pin_down_bytes(struct btrfs_trans_handle *trans,
2665 struct btrfs_root *root,
2666 u64 bytenr, u64 num_bytes, int is_data)
Chris Masone20d96d2007-03-22 12:13:20 -04002667{
Chris Mason1a5bc162007-10-15 16:15:26 -04002668 int err = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04002669 struct extent_buffer *buf;
Chris Mason78fae272007-03-25 11:35:08 -04002670
Zheng Yan31840ae2008-09-23 13:14:14 -04002671 if (is_data)
2672 goto pinit;
Chris Mason4bef0842008-09-08 11:18:08 -04002673
Zheng Yan31840ae2008-09-23 13:14:14 -04002674 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
2675 if (!buf)
2676 goto pinit;
Chris Mason4bef0842008-09-08 11:18:08 -04002677
Zheng Yan31840ae2008-09-23 13:14:14 -04002678 /* we can reuse a block if it hasn't been written
2679 * and it is from this transaction. We can't
2680 * reuse anything from the tree log root because
2681 * it has tiny sub-transactions.
2682 */
2683 if (btrfs_buffer_uptodate(buf, 0) &&
2684 btrfs_try_tree_lock(buf)) {
2685 u64 header_owner = btrfs_header_owner(buf);
2686 u64 header_transid = btrfs_header_generation(buf);
2687 if (header_owner != BTRFS_TREE_LOG_OBJECTID &&
Zheng Yan1a40e232008-09-26 10:09:34 -04002688 header_owner != BTRFS_TREE_RELOC_OBJECTID &&
Zheng Yan31840ae2008-09-23 13:14:14 -04002689 header_transid == trans->transid &&
2690 !btrfs_header_flag(buf, BTRFS_HEADER_FLAG_WRITTEN)) {
2691 clean_tree_block(NULL, root, buf);
2692 btrfs_tree_unlock(buf);
Chris Mason5f39d392007-10-15 16:14:19 -04002693 free_extent_buffer(buf);
Zheng Yan31840ae2008-09-23 13:14:14 -04002694 return 1;
Chris Mason8ef97622007-03-26 10:15:30 -04002695 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002696 btrfs_tree_unlock(buf);
Chris Masonf4b9aa82007-03-27 11:05:53 -04002697 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002698 free_extent_buffer(buf);
2699pinit:
2700 btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
2701
Chris Masonbe744172007-05-06 10:15:01 -04002702 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -04002703 return 0;
2704}
2705
Chris Masona28ec192007-03-06 20:08:01 -05002706/*
2707 * remove an extent from the root, returns 0 on success
2708 */
Zheng Yan31840ae2008-09-23 13:14:14 -04002709static int __free_extent(struct btrfs_trans_handle *trans,
2710 struct btrfs_root *root,
2711 u64 bytenr, u64 num_bytes, u64 parent,
Chris Mason7bb86312007-12-11 09:25:06 -05002712 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002713 u64 owner_objectid, int pin, int mark_free)
Chris Masona28ec192007-03-06 20:08:01 -05002714{
Chris Mason5caf2a02007-04-02 11:20:42 -04002715 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -04002716 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -04002717 struct btrfs_fs_info *info = root->fs_info;
2718 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04002719 struct extent_buffer *leaf;
Chris Masona28ec192007-03-06 20:08:01 -05002720 int ret;
Chris Mason952fcca2008-02-18 16:33:44 -05002721 int extent_slot = 0;
2722 int found_extent = 0;
2723 int num_to_del = 1;
Chris Mason234b63a2007-03-13 10:46:10 -04002724 struct btrfs_extent_item *ei;
Chris Masoncf27e1e2007-03-13 09:49:06 -04002725 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -05002726
Chris Masondb945352007-10-15 16:15:53 -04002727 key.objectid = bytenr;
Chris Mason62e27492007-03-15 12:56:47 -04002728 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masondb945352007-10-15 16:15:53 -04002729 key.offset = num_bytes;
Chris Mason5caf2a02007-04-02 11:20:42 -04002730 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04002731 if (!path)
2732 return -ENOMEM;
2733
Chris Mason3c12ac72008-04-21 12:01:38 -04002734 path->reada = 1;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002735 ret = lookup_extent_backref(trans, extent_root, path,
2736 bytenr, parent, root_objectid,
2737 ref_generation, owner_objectid, 1);
Chris Mason7bb86312007-12-11 09:25:06 -05002738 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05002739 struct btrfs_key found_key;
2740 extent_slot = path->slots[0];
Chris Masond3977122009-01-05 21:25:51 -05002741 while (extent_slot > 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05002742 extent_slot--;
2743 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
2744 extent_slot);
2745 if (found_key.objectid != bytenr)
2746 break;
2747 if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
2748 found_key.offset == num_bytes) {
2749 found_extent = 1;
2750 break;
2751 }
2752 if (path->slots[0] - extent_slot > 5)
2753 break;
2754 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002755 if (!found_extent) {
2756 ret = remove_extent_backref(trans, extent_root, path);
2757 BUG_ON(ret);
2758 btrfs_release_path(extent_root, path);
2759 ret = btrfs_search_slot(trans, extent_root,
2760 &key, path, -1, 1);
Josef Bacikf3465ca2008-11-12 14:19:50 -05002761 if (ret) {
2762 printk(KERN_ERR "umm, got %d back from search"
Chris Masond3977122009-01-05 21:25:51 -05002763 ", was looking for %llu\n", ret,
2764 (unsigned long long)bytenr);
Josef Bacikf3465ca2008-11-12 14:19:50 -05002765 btrfs_print_leaf(extent_root, path->nodes[0]);
2766 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002767 BUG_ON(ret);
2768 extent_slot = path->slots[0];
2769 }
Chris Mason7bb86312007-12-11 09:25:06 -05002770 } else {
2771 btrfs_print_leaf(extent_root, path->nodes[0]);
2772 WARN_ON(1);
Chris Masond3977122009-01-05 21:25:51 -05002773 printk(KERN_ERR "btrfs unable to find ref byte nr %llu "
2774 "root %llu gen %llu owner %llu\n",
2775 (unsigned long long)bytenr,
2776 (unsigned long long)root_objectid,
2777 (unsigned long long)ref_generation,
2778 (unsigned long long)owner_objectid);
Chris Mason7bb86312007-12-11 09:25:06 -05002779 }
Chris Mason5f39d392007-10-15 16:14:19 -04002780
2781 leaf = path->nodes[0];
Chris Mason952fcca2008-02-18 16:33:44 -05002782 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04002783 struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002784 refs = btrfs_extent_refs(leaf, ei);
2785 BUG_ON(refs == 0);
2786 refs -= 1;
2787 btrfs_set_extent_refs(leaf, ei, refs);
Chris Mason952fcca2008-02-18 16:33:44 -05002788
Chris Mason5f39d392007-10-15 16:14:19 -04002789 btrfs_mark_buffer_dirty(leaf);
2790
Chris Mason952fcca2008-02-18 16:33:44 -05002791 if (refs == 0 && found_extent && path->slots[0] == extent_slot + 1) {
Zheng Yan31840ae2008-09-23 13:14:14 -04002792 struct btrfs_extent_ref *ref;
2793 ref = btrfs_item_ptr(leaf, path->slots[0],
2794 struct btrfs_extent_ref);
2795 BUG_ON(btrfs_ref_num_refs(leaf, ref) != 1);
Chris Mason952fcca2008-02-18 16:33:44 -05002796 /* if the back ref and the extent are next to each other
2797 * they get deleted below in one shot
2798 */
2799 path->slots[0] = extent_slot;
2800 num_to_del = 2;
2801 } else if (found_extent) {
2802 /* otherwise delete the extent back ref */
Zheng Yan31840ae2008-09-23 13:14:14 -04002803 ret = remove_extent_backref(trans, extent_root, path);
Chris Mason952fcca2008-02-18 16:33:44 -05002804 BUG_ON(ret);
2805 /* if refs are 0, we need to setup the path for deletion */
2806 if (refs == 0) {
2807 btrfs_release_path(extent_root, path);
2808 ret = btrfs_search_slot(trans, extent_root, &key, path,
2809 -1, 1);
Chris Mason952fcca2008-02-18 16:33:44 -05002810 BUG_ON(ret);
2811 }
2812 }
2813
Chris Masoncf27e1e2007-03-13 09:49:06 -04002814 if (refs == 0) {
Chris Masondb945352007-10-15 16:15:53 -04002815 u64 super_used;
2816 u64 root_used;
Chris Mason78fae272007-03-25 11:35:08 -04002817
2818 if (pin) {
Josef Bacik25179202008-10-29 14:49:05 -04002819 mutex_lock(&root->fs_info->pinned_mutex);
Zheng Yan31840ae2008-09-23 13:14:14 -04002820 ret = pin_down_bytes(trans, root, bytenr, num_bytes,
2821 owner_objectid >= BTRFS_FIRST_FREE_OBJECTID);
Josef Bacik25179202008-10-29 14:49:05 -04002822 mutex_unlock(&root->fs_info->pinned_mutex);
Yanc5492282007-11-06 10:25:25 -05002823 if (ret > 0)
2824 mark_free = 1;
2825 BUG_ON(ret < 0);
Chris Mason78fae272007-03-25 11:35:08 -04002826 }
Josef Bacik58176a92007-08-29 15:47:34 -04002827 /* block accounting for super block */
Chris Mason75eff682008-12-15 15:54:40 -05002828 spin_lock(&info->delalloc_lock);
Chris Masondb945352007-10-15 16:15:53 -04002829 super_used = btrfs_super_bytes_used(&info->super_copy);
2830 btrfs_set_super_bytes_used(&info->super_copy,
2831 super_used - num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -04002832
2833 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04002834 root_used = btrfs_root_used(&root->root_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002835 btrfs_set_root_used(&root->root_item,
Chris Masondb945352007-10-15 16:15:53 -04002836 root_used - num_bytes);
Yan Zheng34bf63c2008-12-19 10:58:46 -05002837 spin_unlock(&info->delalloc_lock);
Chris Mason952fcca2008-02-18 16:33:44 -05002838 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
2839 num_to_del);
Zheng Yan31840ae2008-09-23 13:14:14 -04002840 BUG_ON(ret);
Josef Bacik25179202008-10-29 14:49:05 -04002841 btrfs_release_path(extent_root, path);
David Woodhouse21af8042008-08-12 14:13:26 +01002842
Chris Mason459931e2008-12-10 09:10:46 -05002843 if (owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
2844 ret = btrfs_del_csums(trans, root, bytenr, num_bytes);
2845 BUG_ON(ret);
2846 }
2847
Chris Masondcbdd4d2008-12-16 13:51:01 -05002848 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
2849 mark_free);
2850 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -05002851 }
Chris Mason5caf2a02007-04-02 11:20:42 -04002852 btrfs_free_path(path);
Chris Mason87ef2bb2008-10-30 11:23:27 -04002853 finish_current_insert(trans, extent_root, 0);
Chris Masona28ec192007-03-06 20:08:01 -05002854 return ret;
2855}
2856
2857/*
Chris Masonfec577f2007-02-26 10:40:21 -05002858 * find all the blocks marked as pending in the radix tree and remove
2859 * them from the extent map
2860 */
Chris Masond3977122009-01-05 21:25:51 -05002861static int del_pending_extents(struct btrfs_trans_handle *trans,
2862 struct btrfs_root *extent_root, int all)
Chris Masonfec577f2007-02-26 10:40:21 -05002863{
2864 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -04002865 int err = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -04002866 u64 start;
2867 u64 end;
Zheng Yan31840ae2008-09-23 13:14:14 -04002868 u64 priv;
Josef Bacik25179202008-10-29 14:49:05 -04002869 u64 search = 0;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002870 int nr = 0, skipped = 0;
Chris Masond1310b22008-01-24 16:13:08 -05002871 struct extent_io_tree *pending_del;
Zheng Yan31840ae2008-09-23 13:14:14 -04002872 struct extent_io_tree *extent_ins;
2873 struct pending_extent_op *extent_op;
Josef Bacik25179202008-10-29 14:49:05 -04002874 struct btrfs_fs_info *info = extent_root->fs_info;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002875 struct list_head delete_list;
Chris Mason8ef97622007-03-26 10:15:30 -04002876
Josef Bacikf3465ca2008-11-12 14:19:50 -05002877 INIT_LIST_HEAD(&delete_list);
Zheng Yan31840ae2008-09-23 13:14:14 -04002878 extent_ins = &extent_root->fs_info->extent_ins;
Chris Mason1a5bc162007-10-15 16:15:26 -04002879 pending_del = &extent_root->fs_info->pending_del;
Chris Masonfec577f2007-02-26 10:40:21 -05002880
Josef Bacikf3465ca2008-11-12 14:19:50 -05002881again:
2882 mutex_lock(&info->extent_ins_mutex);
Chris Masond3977122009-01-05 21:25:51 -05002883 while (1) {
Josef Bacik25179202008-10-29 14:49:05 -04002884 ret = find_first_extent_bit(pending_del, search, &start, &end,
2885 EXTENT_WRITEBACK);
2886 if (ret) {
Josef Bacikf3465ca2008-11-12 14:19:50 -05002887 if (all && skipped && !nr) {
Josef Bacik25179202008-10-29 14:49:05 -04002888 search = 0;
Yan Zheng5a7be512009-01-21 10:49:16 -05002889 skipped = 0;
Josef Bacik25179202008-10-29 14:49:05 -04002890 continue;
2891 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05002892 mutex_unlock(&info->extent_ins_mutex);
Chris Masonfec577f2007-02-26 10:40:21 -05002893 break;
Josef Bacik25179202008-10-29 14:49:05 -04002894 }
2895
2896 ret = try_lock_extent(extent_ins, start, end, GFP_NOFS);
2897 if (!ret) {
2898 search = end+1;
Josef Bacikf3465ca2008-11-12 14:19:50 -05002899 skipped = 1;
2900
2901 if (need_resched()) {
2902 mutex_unlock(&info->extent_ins_mutex);
2903 cond_resched();
2904 mutex_lock(&info->extent_ins_mutex);
2905 }
2906
Josef Bacik25179202008-10-29 14:49:05 -04002907 continue;
2908 }
2909 BUG_ON(ret < 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04002910
2911 ret = get_state_private(pending_del, start, &priv);
2912 BUG_ON(ret);
2913 extent_op = (struct pending_extent_op *)(unsigned long)priv;
2914
Josef Bacik25179202008-10-29 14:49:05 -04002915 clear_extent_bits(pending_del, start, end, EXTENT_WRITEBACK,
Chris Mason1a5bc162007-10-15 16:15:26 -04002916 GFP_NOFS);
Zheng Yan31840ae2008-09-23 13:14:14 -04002917 if (!test_range_bit(extent_ins, start, end,
Josef Bacik25179202008-10-29 14:49:05 -04002918 EXTENT_WRITEBACK, 0)) {
Josef Bacikf3465ca2008-11-12 14:19:50 -05002919 list_add_tail(&extent_op->list, &delete_list);
2920 nr++;
Chris Masonc286ac42008-07-22 23:06:41 -04002921 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04002922 kfree(extent_op);
Josef Bacik25179202008-10-29 14:49:05 -04002923
2924 ret = get_state_private(&info->extent_ins, start,
2925 &priv);
Zheng Yan31840ae2008-09-23 13:14:14 -04002926 BUG_ON(ret);
2927 extent_op = (struct pending_extent_op *)
Josef Bacik25179202008-10-29 14:49:05 -04002928 (unsigned long)priv;
Zheng Yan31840ae2008-09-23 13:14:14 -04002929
Josef Bacik25179202008-10-29 14:49:05 -04002930 clear_extent_bits(&info->extent_ins, start, end,
2931 EXTENT_WRITEBACK, GFP_NOFS);
2932
Josef Bacikf3465ca2008-11-12 14:19:50 -05002933 if (extent_op->type == PENDING_BACKREF_UPDATE) {
2934 list_add_tail(&extent_op->list, &delete_list);
2935 search = end + 1;
2936 nr++;
2937 continue;
2938 }
Zheng Yan31840ae2008-09-23 13:14:14 -04002939
Josef Bacik25179202008-10-29 14:49:05 -04002940 mutex_lock(&extent_root->fs_info->pinned_mutex);
2941 ret = pin_down_bytes(trans, extent_root, start,
2942 end + 1 - start, 0);
2943 mutex_unlock(&extent_root->fs_info->pinned_mutex);
2944
Zheng Yan31840ae2008-09-23 13:14:14 -04002945 ret = update_block_group(trans, extent_root, start,
Josef Bacik25179202008-10-29 14:49:05 -04002946 end + 1 - start, 0, ret > 0);
2947
Josef Bacikf3465ca2008-11-12 14:19:50 -05002948 unlock_extent(extent_ins, start, end, GFP_NOFS);
Zheng Yan31840ae2008-09-23 13:14:14 -04002949 BUG_ON(ret);
2950 kfree(extent_op);
Chris Masonc286ac42008-07-22 23:06:41 -04002951 }
Chris Mason1a5bc162007-10-15 16:15:26 -04002952 if (ret)
2953 err = ret;
Chris Masonc286ac42008-07-22 23:06:41 -04002954
Josef Bacikf3465ca2008-11-12 14:19:50 -05002955 search = end + 1;
2956
2957 if (need_resched()) {
2958 mutex_unlock(&info->extent_ins_mutex);
2959 cond_resched();
2960 mutex_lock(&info->extent_ins_mutex);
2961 }
Chris Masonfec577f2007-02-26 10:40:21 -05002962 }
Josef Bacikf3465ca2008-11-12 14:19:50 -05002963
2964 if (nr) {
2965 ret = free_extents(trans, extent_root, &delete_list);
2966 BUG_ON(ret);
2967 }
2968
2969 if (all && skipped) {
2970 INIT_LIST_HEAD(&delete_list);
2971 search = 0;
2972 nr = 0;
2973 goto again;
2974 }
2975
Josef Bacikeb099672009-02-12 09:27:38 -05002976 if (!err)
2977 finish_current_insert(trans, extent_root, 0);
Chris Masone20d96d2007-03-22 12:13:20 -04002978 return err;
Chris Masonfec577f2007-02-26 10:40:21 -05002979}
2980
2981/*
2982 * remove an extent from the root, returns 0 on success
2983 */
Chris Mason925baed2008-06-25 16:01:30 -04002984static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04002985 struct btrfs_root *root,
2986 u64 bytenr, u64 num_bytes, u64 parent,
2987 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04002988 u64 owner_objectid, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -05002989{
Chris Mason9f5fae22007-03-20 14:38:32 -04002990 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -05002991 int pending_ret;
2992 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05002993
Chris Masondb945352007-10-15 16:15:53 -04002994 WARN_ON(num_bytes < root->sectorsize);
Chris Masona28ec192007-03-06 20:08:01 -05002995 if (root == extent_root) {
Josef Bacikf3465ca2008-11-12 14:19:50 -05002996 struct pending_extent_op *extent_op = NULL;
2997
2998 mutex_lock(&root->fs_info->extent_ins_mutex);
2999 if (test_range_bit(&root->fs_info->extent_ins, bytenr,
3000 bytenr + num_bytes - 1, EXTENT_WRITEBACK, 0)) {
3001 u64 priv;
3002 ret = get_state_private(&root->fs_info->extent_ins,
3003 bytenr, &priv);
3004 BUG_ON(ret);
3005 extent_op = (struct pending_extent_op *)
3006 (unsigned long)priv;
3007
3008 extent_op->del = 1;
3009 if (extent_op->type == PENDING_EXTENT_INSERT) {
3010 mutex_unlock(&root->fs_info->extent_ins_mutex);
3011 return 0;
3012 }
3013 }
3014
3015 if (extent_op) {
3016 ref_generation = extent_op->orig_generation;
3017 parent = extent_op->orig_parent;
3018 }
Zheng Yan31840ae2008-09-23 13:14:14 -04003019
3020 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
3021 BUG_ON(!extent_op);
3022
3023 extent_op->type = PENDING_EXTENT_DELETE;
3024 extent_op->bytenr = bytenr;
3025 extent_op->num_bytes = num_bytes;
3026 extent_op->parent = parent;
3027 extent_op->orig_parent = parent;
3028 extent_op->generation = ref_generation;
3029 extent_op->orig_generation = ref_generation;
3030 extent_op->level = (int)owner_objectid;
Josef Bacikf3465ca2008-11-12 14:19:50 -05003031 INIT_LIST_HEAD(&extent_op->list);
3032 extent_op->del = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04003033
3034 set_extent_bits(&root->fs_info->pending_del,
3035 bytenr, bytenr + num_bytes - 1,
Josef Bacik25179202008-10-29 14:49:05 -04003036 EXTENT_WRITEBACK, GFP_NOFS);
Zheng Yan31840ae2008-09-23 13:14:14 -04003037 set_state_private(&root->fs_info->pending_del,
3038 bytenr, (unsigned long)extent_op);
Josef Bacik25179202008-10-29 14:49:05 -04003039 mutex_unlock(&root->fs_info->extent_ins_mutex);
Chris Masona28ec192007-03-06 20:08:01 -05003040 return 0;
3041 }
Chris Mason4bef0842008-09-08 11:18:08 -04003042 /* if metadata always pin */
Chris Masond00aff02008-09-11 15:54:42 -04003043 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
3044 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
Yan Zheng7237f182009-01-21 12:54:03 -05003045 mutex_lock(&root->fs_info->pinned_mutex);
3046 btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
3047 mutex_unlock(&root->fs_info->pinned_mutex);
Zheng Yane8569812008-09-26 10:05:48 -04003048 update_reserved_extents(root, bytenr, num_bytes, 0);
Chris Masond00aff02008-09-11 15:54:42 -04003049 return 0;
3050 }
Chris Mason4bef0842008-09-08 11:18:08 -04003051 pin = 1;
Chris Masond00aff02008-09-11 15:54:42 -04003052 }
Chris Mason4bef0842008-09-08 11:18:08 -04003053
3054 /* if data pin when any transaction has committed this */
3055 if (ref_generation != trans->transid)
3056 pin = 1;
3057
Zheng Yan31840ae2008-09-23 13:14:14 -04003058 ret = __free_extent(trans, root, bytenr, num_bytes, parent,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003059 root_objectid, ref_generation,
3060 owner_objectid, pin, pin == 0);
Chris Masonee6e6502008-07-17 12:54:40 -04003061
Chris Mason87ef2bb2008-10-30 11:23:27 -04003062 finish_current_insert(trans, root->fs_info->extent_root, 0);
3063 pending_ret = del_pending_extents(trans, root->fs_info->extent_root, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05003064 return ret ? ret : pending_ret;
3065}
3066
Chris Mason925baed2008-06-25 16:01:30 -04003067int btrfs_free_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04003068 struct btrfs_root *root,
3069 u64 bytenr, u64 num_bytes, u64 parent,
3070 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003071 u64 owner_objectid, int pin)
Chris Mason925baed2008-06-25 16:01:30 -04003072{
3073 int ret;
3074
Zheng Yan31840ae2008-09-23 13:14:14 -04003075 ret = __btrfs_free_extent(trans, root, bytenr, num_bytes, parent,
Chris Mason925baed2008-06-25 16:01:30 -04003076 root_objectid, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003077 owner_objectid, pin);
Chris Mason925baed2008-06-25 16:01:30 -04003078 return ret;
3079}
3080
Chris Mason87ee04e2007-11-30 11:30:34 -05003081static u64 stripe_align(struct btrfs_root *root, u64 val)
3082{
3083 u64 mask = ((u64)root->stripesize - 1);
3084 u64 ret = (val + mask) & ~mask;
3085 return ret;
3086}
3087
Chris Masonfec577f2007-02-26 10:40:21 -05003088/*
3089 * walks the btree of allocated extents and find a hole of a given size.
3090 * The key ins is changed to record the hole:
3091 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04003092 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05003093 * ins->offset == number of blocks
3094 * Any available blocks before search_start are skipped.
3095 */
Chris Masond3977122009-01-05 21:25:51 -05003096static noinline int find_free_extent(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05003097 struct btrfs_root *orig_root,
3098 u64 num_bytes, u64 empty_size,
3099 u64 search_start, u64 search_end,
3100 u64 hint_byte, struct btrfs_key *ins,
3101 u64 exclude_start, u64 exclude_nr,
3102 int data)
Chris Masonfec577f2007-02-26 10:40:21 -05003103{
Josef Bacik80eb2342008-10-29 14:49:05 -04003104 int ret = 0;
Chris Masond3977122009-01-05 21:25:51 -05003105 struct btrfs_root *root = orig_root->fs_info->extent_root;
Chris Masondb945352007-10-15 16:15:53 -04003106 u64 total_needed = num_bytes;
Chris Mason239b14b2008-03-24 15:02:07 -04003107 u64 *last_ptr = NULL;
Chris Mason43662112008-11-07 09:06:11 -05003108 u64 last_wanted = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04003109 struct btrfs_block_group_cache *block_group = NULL;
Chris Mason0ef3e662008-05-24 14:04:53 -04003110 int chunk_alloc_done = 0;
Chris Mason239b14b2008-03-24 15:02:07 -04003111 int empty_cluster = 2 * 1024 * 1024;
Chris Mason0ef3e662008-05-24 14:04:53 -04003112 int allowed_chunk_alloc = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04003113 struct list_head *head = NULL, *cur = NULL;
3114 int loop = 0;
Chris Masonf5a31e12008-11-10 11:47:09 -05003115 int extra_loop = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04003116 struct btrfs_space_info *space_info;
Chris Masonfec577f2007-02-26 10:40:21 -05003117
Chris Masondb945352007-10-15 16:15:53 -04003118 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04003119 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
Josef Bacik80eb2342008-10-29 14:49:05 -04003120 ins->objectid = 0;
3121 ins->offset = 0;
Chris Masonb1a4d962007-04-04 15:27:52 -04003122
Chris Mason0ef3e662008-05-24 14:04:53 -04003123 if (orig_root->ref_cows || empty_size)
3124 allowed_chunk_alloc = 1;
3125
Chris Mason239b14b2008-03-24 15:02:07 -04003126 if (data & BTRFS_BLOCK_GROUP_METADATA) {
3127 last_ptr = &root->fs_info->last_alloc;
Chris Mason536ac8a2009-02-12 09:41:38 -05003128 if (!btrfs_test_opt(root, SSD))
3129 empty_cluster = 64 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04003130 }
3131
Josef Bacik0f9dd462008-09-23 13:14:11 -04003132 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD))
Chris Mason239b14b2008-03-24 15:02:07 -04003133 last_ptr = &root->fs_info->last_data_alloc;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003134
Chris Mason239b14b2008-03-24 15:02:07 -04003135 if (last_ptr) {
Chris Mason43662112008-11-07 09:06:11 -05003136 if (*last_ptr) {
Chris Mason239b14b2008-03-24 15:02:07 -04003137 hint_byte = *last_ptr;
Chris Mason43662112008-11-07 09:06:11 -05003138 last_wanted = *last_ptr;
3139 } else
Chris Mason239b14b2008-03-24 15:02:07 -04003140 empty_size += empty_cluster;
Chris Mason43662112008-11-07 09:06:11 -05003141 } else {
3142 empty_cluster = 0;
Chris Mason239b14b2008-03-24 15:02:07 -04003143 }
Chris Masona061fc82008-05-07 11:43:44 -04003144 search_start = max(search_start, first_logical_byte(root, 0));
Chris Mason239b14b2008-03-24 15:02:07 -04003145 search_start = max(search_start, hint_byte);
Chris Mason0b86a832008-03-24 15:01:56 -04003146
Chris Mason42e70e72008-11-07 18:17:11 -05003147 if (last_wanted && search_start != last_wanted) {
Chris Mason43662112008-11-07 09:06:11 -05003148 last_wanted = 0;
Chris Mason42e70e72008-11-07 18:17:11 -05003149 empty_size += empty_cluster;
3150 }
Chris Mason43662112008-11-07 09:06:11 -05003151
Chris Mason42e70e72008-11-07 18:17:11 -05003152 total_needed += empty_size;
Josef Bacik80eb2342008-10-29 14:49:05 -04003153 block_group = btrfs_lookup_block_group(root->fs_info, search_start);
Yan Zhengd899e052008-10-30 14:25:28 -04003154 if (!block_group)
3155 block_group = btrfs_lookup_first_block_group(root->fs_info,
3156 search_start);
Josef Bacik80eb2342008-10-29 14:49:05 -04003157 space_info = __find_space_info(root->fs_info, data);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003158
Josef Bacik80eb2342008-10-29 14:49:05 -04003159 down_read(&space_info->groups_sem);
3160 while (1) {
3161 struct btrfs_free_space *free_space;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003162 /*
Josef Bacik80eb2342008-10-29 14:49:05 -04003163 * the only way this happens if our hint points to a block
3164 * group thats not of the proper type, while looping this
3165 * should never happen
Josef Bacik0f9dd462008-09-23 13:14:11 -04003166 */
Chris Mason8a1413a2008-11-10 16:13:54 -05003167 if (empty_size)
3168 extra_loop = 1;
3169
Chris Mason42e70e72008-11-07 18:17:11 -05003170 if (!block_group)
3171 goto new_group_no_lock;
3172
Josef Bacikea6a4782008-11-20 12:16:16 -05003173 if (unlikely(!block_group->cached)) {
3174 mutex_lock(&block_group->cache_mutex);
3175 ret = cache_block_group(root, block_group);
3176 mutex_unlock(&block_group->cache_mutex);
3177 if (ret)
3178 break;
3179 }
3180
Josef Bacik25179202008-10-29 14:49:05 -04003181 mutex_lock(&block_group->alloc_mutex);
Josef Bacik80eb2342008-10-29 14:49:05 -04003182 if (unlikely(!block_group_bits(block_group, data)))
Josef Bacik0f9dd462008-09-23 13:14:11 -04003183 goto new_group;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003184
Josef Bacikea6a4782008-11-20 12:16:16 -05003185 if (unlikely(block_group->ro))
Josef Bacik80eb2342008-10-29 14:49:05 -04003186 goto new_group;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003187
Josef Bacik80eb2342008-10-29 14:49:05 -04003188 free_space = btrfs_find_free_space(block_group, search_start,
3189 total_needed);
3190 if (free_space) {
3191 u64 start = block_group->key.objectid;
3192 u64 end = block_group->key.objectid +
Josef Bacik0f9dd462008-09-23 13:14:11 -04003193 block_group->key.offset;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003194
Josef Bacik80eb2342008-10-29 14:49:05 -04003195 search_start = stripe_align(root, free_space->offset);
Chris Mason0b86a832008-03-24 15:01:56 -04003196
Josef Bacik80eb2342008-10-29 14:49:05 -04003197 /* move on to the next group */
3198 if (search_start + num_bytes >= search_end)
3199 goto new_group;
Chris Masone37c9e62007-05-09 20:13:14 -04003200
Josef Bacik80eb2342008-10-29 14:49:05 -04003201 /* move on to the next group */
3202 if (search_start + num_bytes > end)
3203 goto new_group;
3204
Chris Mason43662112008-11-07 09:06:11 -05003205 if (last_wanted && search_start != last_wanted) {
Chris Mason3b7885b2008-11-06 21:48:27 -05003206 total_needed += empty_cluster;
Chris Mason8a1413a2008-11-10 16:13:54 -05003207 empty_size += empty_cluster;
Chris Mason43662112008-11-07 09:06:11 -05003208 last_wanted = 0;
Chris Mason3b7885b2008-11-06 21:48:27 -05003209 /*
3210 * if search_start is still in this block group
3211 * then we just re-search this block group
3212 */
3213 if (search_start >= start &&
3214 search_start < end) {
3215 mutex_unlock(&block_group->alloc_mutex);
3216 continue;
3217 }
3218
3219 /* else we go to the next block group */
3220 goto new_group;
3221 }
3222
Josef Bacik80eb2342008-10-29 14:49:05 -04003223 if (exclude_nr > 0 &&
3224 (search_start + num_bytes > exclude_start &&
3225 search_start < exclude_start + exclude_nr)) {
3226 search_start = exclude_start + exclude_nr;
3227 /*
3228 * if search_start is still in this block group
3229 * then we just re-search this block group
3230 */
3231 if (search_start >= start &&
Josef Bacik25179202008-10-29 14:49:05 -04003232 search_start < end) {
3233 mutex_unlock(&block_group->alloc_mutex);
Chris Mason43662112008-11-07 09:06:11 -05003234 last_wanted = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04003235 continue;
Josef Bacik25179202008-10-29 14:49:05 -04003236 }
Josef Bacik80eb2342008-10-29 14:49:05 -04003237
3238 /* else we go to the next block group */
3239 goto new_group;
3240 }
3241
3242 ins->objectid = search_start;
3243 ins->offset = num_bytes;
Josef Bacik25179202008-10-29 14:49:05 -04003244
3245 btrfs_remove_free_space_lock(block_group, search_start,
3246 num_bytes);
Josef Bacik80eb2342008-10-29 14:49:05 -04003247 /* we are all good, lets return */
Josef Bacik25179202008-10-29 14:49:05 -04003248 mutex_unlock(&block_group->alloc_mutex);
Josef Bacik80eb2342008-10-29 14:49:05 -04003249 break;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003250 }
Josef Bacik80eb2342008-10-29 14:49:05 -04003251new_group:
Chris Mason42e70e72008-11-07 18:17:11 -05003252 mutex_unlock(&block_group->alloc_mutex);
Yan Zhengd2fb3432008-12-11 16:30:39 -05003253 put_block_group(block_group);
3254 block_group = NULL;
Chris Mason42e70e72008-11-07 18:17:11 -05003255new_group_no_lock:
Chris Masonf5a31e12008-11-10 11:47:09 -05003256 /* don't try to compare new allocations against the
3257 * last allocation any more
3258 */
Chris Mason43662112008-11-07 09:06:11 -05003259 last_wanted = 0;
Chris Masonf5a31e12008-11-10 11:47:09 -05003260
Josef Bacik80eb2342008-10-29 14:49:05 -04003261 /*
3262 * Here's how this works.
3263 * loop == 0: we were searching a block group via a hint
3264 * and didn't find anything, so we start at
3265 * the head of the block groups and keep searching
3266 * loop == 1: we're searching through all of the block groups
3267 * if we hit the head again we have searched
3268 * all of the block groups for this space and we
3269 * need to try and allocate, if we cant error out.
3270 * loop == 2: we allocated more space and are looping through
3271 * all of the block groups again.
3272 */
3273 if (loop == 0) {
3274 head = &space_info->block_groups;
3275 cur = head->next;
Josef Bacik80eb2342008-10-29 14:49:05 -04003276 loop++;
3277 } else if (loop == 1 && cur == head) {
Chris Masonf5a31e12008-11-10 11:47:09 -05003278 int keep_going;
Chris Mason42e70e72008-11-07 18:17:11 -05003279
Chris Masonf5a31e12008-11-10 11:47:09 -05003280 /* at this point we give up on the empty_size
3281 * allocations and just try to allocate the min
3282 * space.
3283 *
3284 * The extra_loop field was set if an empty_size
3285 * allocation was attempted above, and if this
3286 * is try we need to try the loop again without
3287 * the additional empty_size.
3288 */
Chris Mason5b7c3fc2008-11-10 07:26:33 -05003289 total_needed -= empty_size;
3290 empty_size = 0;
Chris Masonf5a31e12008-11-10 11:47:09 -05003291 keep_going = extra_loop;
3292 loop++;
Chris Mason42e70e72008-11-07 18:17:11 -05003293
Josef Bacik80eb2342008-10-29 14:49:05 -04003294 if (allowed_chunk_alloc && !chunk_alloc_done) {
3295 up_read(&space_info->groups_sem);
3296 ret = do_chunk_alloc(trans, root, num_bytes +
3297 2 * 1024 * 1024, data, 1);
Josef Bacik80eb2342008-10-29 14:49:05 -04003298 down_read(&space_info->groups_sem);
Chris Mason2ed6d662008-11-13 09:59:33 -05003299 if (ret < 0)
3300 goto loop_check;
Josef Bacik80eb2342008-10-29 14:49:05 -04003301 head = &space_info->block_groups;
Chris Masonf5a31e12008-11-10 11:47:09 -05003302 /*
3303 * we've allocated a new chunk, keep
3304 * trying
3305 */
3306 keep_going = 1;
Josef Bacik80eb2342008-10-29 14:49:05 -04003307 chunk_alloc_done = 1;
3308 } else if (!allowed_chunk_alloc) {
3309 space_info->force_alloc = 1;
Chris Masonf5a31e12008-11-10 11:47:09 -05003310 }
Chris Mason2ed6d662008-11-13 09:59:33 -05003311loop_check:
Chris Masonf5a31e12008-11-10 11:47:09 -05003312 if (keep_going) {
3313 cur = head->next;
3314 extra_loop = 0;
Josef Bacik80eb2342008-10-29 14:49:05 -04003315 } else {
3316 break;
3317 }
3318 } else if (cur == head) {
3319 break;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003320 }
Josef Bacik80eb2342008-10-29 14:49:05 -04003321
3322 block_group = list_entry(cur, struct btrfs_block_group_cache,
3323 list);
Yan Zhengd2fb3432008-12-11 16:30:39 -05003324 atomic_inc(&block_group->count);
3325
Josef Bacik80eb2342008-10-29 14:49:05 -04003326 search_start = block_group->key.objectid;
3327 cur = cur->next;
Chris Masone19caa52007-10-15 16:17:44 -04003328 }
Chris Mason0b86a832008-03-24 15:01:56 -04003329
Josef Bacik80eb2342008-10-29 14:49:05 -04003330 /* we found what we needed */
3331 if (ins->objectid) {
3332 if (!(data & BTRFS_BLOCK_GROUP_DATA))
Yan Zhengd2fb3432008-12-11 16:30:39 -05003333 trans->block_group = block_group->key.objectid;
Josef Bacik80eb2342008-10-29 14:49:05 -04003334
3335 if (last_ptr)
3336 *last_ptr = ins->objectid + ins->offset;
3337 ret = 0;
3338 } else if (!ret) {
Chris Masond3977122009-01-05 21:25:51 -05003339 printk(KERN_ERR "btrfs searching for %llu bytes, "
3340 "num_bytes %llu, loop %d, allowed_alloc %d\n",
3341 (unsigned long long)total_needed,
3342 (unsigned long long)num_bytes,
Josef Bacik4ce4cb52008-11-17 21:12:00 -05003343 loop, allowed_chunk_alloc);
Josef Bacik80eb2342008-10-29 14:49:05 -04003344 ret = -ENOSPC;
Chris Masonf2654de2007-06-26 12:20:46 -04003345 }
Yan Zhengd2fb3432008-12-11 16:30:39 -05003346 if (block_group)
3347 put_block_group(block_group);
Chris Mason0b86a832008-03-24 15:01:56 -04003348
Josef Bacik80eb2342008-10-29 14:49:05 -04003349 up_read(&space_info->groups_sem);
Chris Mason0f70abe2007-02-28 16:46:22 -05003350 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05003351}
Chris Masonec44a352008-04-28 15:29:52 -04003352
Josef Bacik0f9dd462008-09-23 13:14:11 -04003353static void dump_space_info(struct btrfs_space_info *info, u64 bytes)
3354{
3355 struct btrfs_block_group_cache *cache;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003356
Chris Masond3977122009-01-05 21:25:51 -05003357 printk(KERN_INFO "space_info has %llu free, is %sfull\n",
3358 (unsigned long long)(info->total_bytes - info->bytes_used -
3359 info->bytes_pinned - info->bytes_reserved),
3360 (info->full) ? "" : "not ");
Josef Bacik6a632092009-02-20 11:00:09 -05003361 printk(KERN_INFO "space_info total=%llu, pinned=%llu, delalloc=%llu,"
3362 " may_use=%llu, used=%llu\n", info->total_bytes,
3363 info->bytes_pinned, info->bytes_delalloc, info->bytes_may_use,
3364 info->bytes_used);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003365
Josef Bacik80eb2342008-10-29 14:49:05 -04003366 down_read(&info->groups_sem);
Qinghuang Fengc6e30872009-01-21 10:59:08 -05003367 list_for_each_entry(cache, &info->block_groups, list) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04003368 spin_lock(&cache->lock);
Chris Masond3977122009-01-05 21:25:51 -05003369 printk(KERN_INFO "block group %llu has %llu bytes, %llu used "
3370 "%llu pinned %llu reserved\n",
3371 (unsigned long long)cache->key.objectid,
3372 (unsigned long long)cache->key.offset,
3373 (unsigned long long)btrfs_block_group_used(&cache->item),
3374 (unsigned long long)cache->pinned,
3375 (unsigned long long)cache->reserved);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003376 btrfs_dump_free_space(cache, bytes);
3377 spin_unlock(&cache->lock);
3378 }
Josef Bacik80eb2342008-10-29 14:49:05 -04003379 up_read(&info->groups_sem);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003380}
Zheng Yane8569812008-09-26 10:05:48 -04003381
Chris Masone6dcd2d2008-07-17 12:53:50 -04003382static int __btrfs_reserve_extent(struct btrfs_trans_handle *trans,
3383 struct btrfs_root *root,
3384 u64 num_bytes, u64 min_alloc_size,
3385 u64 empty_size, u64 hint_byte,
3386 u64 search_end, struct btrfs_key *ins,
3387 u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05003388{
3389 int ret;
Chris Masonfbdc7622007-05-30 10:22:12 -04003390 u64 search_start = 0;
Chris Mason1261ec42007-03-20 20:35:03 -04003391 struct btrfs_fs_info *info = root->fs_info;
Chris Mason925baed2008-06-25 16:01:30 -04003392
Josef Bacik6a632092009-02-20 11:00:09 -05003393 data = btrfs_get_alloc_profile(root, data);
Chris Mason98d20f62008-04-14 09:46:10 -04003394again:
Chris Mason0ef3e662008-05-24 14:04:53 -04003395 /*
3396 * the only place that sets empty_size is btrfs_realloc_node, which
3397 * is not called recursively on allocations
3398 */
3399 if (empty_size || root->ref_cows) {
Chris Mason593060d2008-03-25 16:50:33 -04003400 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
Chris Mason6324fbf2008-03-24 15:01:59 -04003401 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04003402 2 * 1024 * 1024,
3403 BTRFS_BLOCK_GROUP_METADATA |
3404 (info->metadata_alloc_profile &
3405 info->avail_metadata_alloc_bits), 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04003406 }
3407 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04003408 num_bytes + 2 * 1024 * 1024, data, 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04003409 }
Chris Mason0b86a832008-03-24 15:01:56 -04003410
Chris Masondb945352007-10-15 16:15:53 -04003411 WARN_ON(num_bytes < root->sectorsize);
3412 ret = find_free_extent(trans, root, num_bytes, empty_size,
3413 search_start, search_end, hint_byte, ins,
Chris Mason26b80032007-08-08 20:17:12 -04003414 trans->alloc_exclude_start,
3415 trans->alloc_exclude_nr, data);
Chris Mason3b951512008-04-17 11:29:12 -04003416
Chris Mason98d20f62008-04-14 09:46:10 -04003417 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
3418 num_bytes = num_bytes >> 1;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003419 num_bytes = num_bytes & ~(root->sectorsize - 1);
Chris Mason98d20f62008-04-14 09:46:10 -04003420 num_bytes = max(num_bytes, min_alloc_size);
Chris Mason0ef3e662008-05-24 14:04:53 -04003421 do_chunk_alloc(trans, root->fs_info->extent_root,
3422 num_bytes, data, 1);
Chris Mason98d20f62008-04-14 09:46:10 -04003423 goto again;
3424 }
Chris Masonec44a352008-04-28 15:29:52 -04003425 if (ret) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04003426 struct btrfs_space_info *sinfo;
3427
3428 sinfo = __find_space_info(root->fs_info, data);
Chris Masond3977122009-01-05 21:25:51 -05003429 printk(KERN_ERR "btrfs allocation failed flags %llu, "
3430 "wanted %llu\n", (unsigned long long)data,
3431 (unsigned long long)num_bytes);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003432 dump_space_info(sinfo, num_bytes);
Chris Mason925baed2008-06-25 16:01:30 -04003433 BUG();
Chris Mason925baed2008-06-25 16:01:30 -04003434 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04003435
3436 return ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003437}
3438
Chris Mason65b51a02008-08-01 15:11:20 -04003439int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
3440{
Josef Bacik0f9dd462008-09-23 13:14:11 -04003441 struct btrfs_block_group_cache *cache;
Liu Hui1f3c79a2009-01-05 15:57:51 -05003442 int ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04003443
Josef Bacik0f9dd462008-09-23 13:14:11 -04003444 cache = btrfs_lookup_block_group(root->fs_info, start);
3445 if (!cache) {
Chris Masond3977122009-01-05 21:25:51 -05003446 printk(KERN_ERR "Unable to find block group for %llu\n",
3447 (unsigned long long)start);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003448 return -ENOSPC;
3449 }
Liu Hui1f3c79a2009-01-05 15:57:51 -05003450
3451 ret = btrfs_discard_extent(root, start, len);
3452
Josef Bacik0f9dd462008-09-23 13:14:11 -04003453 btrfs_add_free_space(cache, start, len);
Yan Zhengd2fb3432008-12-11 16:30:39 -05003454 put_block_group(cache);
Zheng Yan1a40e232008-09-26 10:09:34 -04003455 update_reserved_extents(root, start, len, 0);
Liu Hui1f3c79a2009-01-05 15:57:51 -05003456
3457 return ret;
Chris Mason65b51a02008-08-01 15:11:20 -04003458}
3459
Chris Masone6dcd2d2008-07-17 12:53:50 -04003460int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
3461 struct btrfs_root *root,
3462 u64 num_bytes, u64 min_alloc_size,
3463 u64 empty_size, u64 hint_byte,
3464 u64 search_end, struct btrfs_key *ins,
3465 u64 data)
3466{
3467 int ret;
Chris Masone6dcd2d2008-07-17 12:53:50 -04003468 ret = __btrfs_reserve_extent(trans, root, num_bytes, min_alloc_size,
3469 empty_size, hint_byte, search_end, ins,
3470 data);
Zheng Yane8569812008-09-26 10:05:48 -04003471 update_reserved_extents(root, ins->objectid, ins->offset, 1);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003472 return ret;
3473}
3474
3475static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04003476 struct btrfs_root *root, u64 parent,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003477 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003478 u64 owner, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04003479{
3480 int ret;
3481 int pending_ret;
3482 u64 super_used;
3483 u64 root_used;
3484 u64 num_bytes = ins->offset;
3485 u32 sizes[2];
3486 struct btrfs_fs_info *info = root->fs_info;
3487 struct btrfs_root *extent_root = info->extent_root;
3488 struct btrfs_extent_item *extent_item;
3489 struct btrfs_extent_ref *ref;
3490 struct btrfs_path *path;
3491 struct btrfs_key keys[2];
Chris Masonf2654de2007-06-26 12:20:46 -04003492
Zheng Yan31840ae2008-09-23 13:14:14 -04003493 if (parent == 0)
3494 parent = ins->objectid;
3495
Josef Bacik58176a92007-08-29 15:47:34 -04003496 /* block accounting for super block */
Chris Mason75eff682008-12-15 15:54:40 -05003497 spin_lock(&info->delalloc_lock);
Chris Masondb945352007-10-15 16:15:53 -04003498 super_used = btrfs_super_bytes_used(&info->super_copy);
3499 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
Chris Mason26b80032007-08-08 20:17:12 -04003500
Josef Bacik58176a92007-08-29 15:47:34 -04003501 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04003502 root_used = btrfs_root_used(&root->root_item);
3503 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
Yan Zheng34bf63c2008-12-19 10:58:46 -05003504 spin_unlock(&info->delalloc_lock);
Josef Bacik58176a92007-08-29 15:47:34 -04003505
Chris Mason26b80032007-08-08 20:17:12 -04003506 if (root == extent_root) {
Zheng Yan31840ae2008-09-23 13:14:14 -04003507 struct pending_extent_op *extent_op;
3508
3509 extent_op = kmalloc(sizeof(*extent_op), GFP_NOFS);
3510 BUG_ON(!extent_op);
3511
3512 extent_op->type = PENDING_EXTENT_INSERT;
3513 extent_op->bytenr = ins->objectid;
3514 extent_op->num_bytes = ins->offset;
3515 extent_op->parent = parent;
3516 extent_op->orig_parent = 0;
3517 extent_op->generation = ref_generation;
3518 extent_op->orig_generation = 0;
3519 extent_op->level = (int)owner;
Josef Bacikf3465ca2008-11-12 14:19:50 -05003520 INIT_LIST_HEAD(&extent_op->list);
3521 extent_op->del = 0;
Zheng Yan31840ae2008-09-23 13:14:14 -04003522
Josef Bacik25179202008-10-29 14:49:05 -04003523 mutex_lock(&root->fs_info->extent_ins_mutex);
Chris Mason1a5bc162007-10-15 16:15:26 -04003524 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
3525 ins->objectid + ins->offset - 1,
Josef Bacik25179202008-10-29 14:49:05 -04003526 EXTENT_WRITEBACK, GFP_NOFS);
Zheng Yan31840ae2008-09-23 13:14:14 -04003527 set_state_private(&root->fs_info->extent_ins,
3528 ins->objectid, (unsigned long)extent_op);
Josef Bacik25179202008-10-29 14:49:05 -04003529 mutex_unlock(&root->fs_info->extent_ins_mutex);
Chris Mason26b80032007-08-08 20:17:12 -04003530 goto update_block;
3531 }
3532
Chris Mason47e4bb92008-02-01 14:51:59 -05003533 memcpy(&keys[0], ins, sizeof(*ins));
Chris Mason47e4bb92008-02-01 14:51:59 -05003534 keys[1].objectid = ins->objectid;
3535 keys[1].type = BTRFS_EXTENT_REF_KEY;
Zheng Yan31840ae2008-09-23 13:14:14 -04003536 keys[1].offset = parent;
Chris Mason47e4bb92008-02-01 14:51:59 -05003537 sizes[0] = sizeof(*extent_item);
3538 sizes[1] = sizeof(*ref);
Chris Mason7bb86312007-12-11 09:25:06 -05003539
3540 path = btrfs_alloc_path();
3541 BUG_ON(!path);
Chris Mason47e4bb92008-02-01 14:51:59 -05003542
3543 ret = btrfs_insert_empty_items(trans, extent_root, path, keys,
3544 sizes, 2);
Chris Masonccd467d2007-06-28 15:57:36 -04003545 BUG_ON(ret);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003546
Chris Mason47e4bb92008-02-01 14:51:59 -05003547 extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
3548 struct btrfs_extent_item);
3549 btrfs_set_extent_refs(path->nodes[0], extent_item, 1);
3550 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
3551 struct btrfs_extent_ref);
3552
3553 btrfs_set_ref_root(path->nodes[0], ref, root_objectid);
3554 btrfs_set_ref_generation(path->nodes[0], ref, ref_generation);
3555 btrfs_set_ref_objectid(path->nodes[0], ref, owner);
Zheng Yan31840ae2008-09-23 13:14:14 -04003556 btrfs_set_ref_num_refs(path->nodes[0], ref, 1);
Chris Mason47e4bb92008-02-01 14:51:59 -05003557
3558 btrfs_mark_buffer_dirty(path->nodes[0]);
3559
3560 trans->alloc_exclude_start = 0;
3561 trans->alloc_exclude_nr = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05003562 btrfs_free_path(path);
Chris Mason87ef2bb2008-10-30 11:23:27 -04003563 finish_current_insert(trans, extent_root, 0);
3564 pending_ret = del_pending_extents(trans, extent_root, 0);
Chris Masonf510cfe2007-10-15 16:14:48 -04003565
Chris Mason925baed2008-06-25 16:01:30 -04003566 if (ret)
3567 goto out;
Chris Masone37c9e62007-05-09 20:13:14 -04003568 if (pending_ret) {
Chris Mason925baed2008-06-25 16:01:30 -04003569 ret = pending_ret;
3570 goto out;
Chris Masone37c9e62007-05-09 20:13:14 -04003571 }
Chris Mason26b80032007-08-08 20:17:12 -04003572
3573update_block:
Chris Masond3977122009-01-05 21:25:51 -05003574 ret = update_block_group(trans, root, ins->objectid,
3575 ins->offset, 1, 0);
Chris Masonf5947062008-02-04 10:10:13 -05003576 if (ret) {
Chris Masond3977122009-01-05 21:25:51 -05003577 printk(KERN_ERR "btrfs update block group failed for %llu "
3578 "%llu\n", (unsigned long long)ins->objectid,
3579 (unsigned long long)ins->offset);
Chris Masonf5947062008-02-04 10:10:13 -05003580 BUG();
3581 }
Chris Mason925baed2008-06-25 16:01:30 -04003582out:
Chris Masone6dcd2d2008-07-17 12:53:50 -04003583 return ret;
3584}
3585
3586int btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04003587 struct btrfs_root *root, u64 parent,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003588 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003589 u64 owner, struct btrfs_key *ins)
Chris Masone6dcd2d2008-07-17 12:53:50 -04003590{
3591 int ret;
Chris Mason1c2308f2008-09-23 13:14:13 -04003592
3593 if (root_objectid == BTRFS_TREE_LOG_OBJECTID)
3594 return 0;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003595 ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid,
3596 ref_generation, owner, ins);
Zheng Yane8569812008-09-26 10:05:48 -04003597 update_reserved_extents(root, ins->objectid, ins->offset, 0);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003598 return ret;
3599}
Chris Masone02119d2008-09-05 16:13:11 -04003600
3601/*
3602 * this is used by the tree logging recovery code. It records that
3603 * an extent has been allocated and makes sure to clear the free
3604 * space cache bits as well
3605 */
3606int btrfs_alloc_logged_extent(struct btrfs_trans_handle *trans,
Zheng Yan31840ae2008-09-23 13:14:14 -04003607 struct btrfs_root *root, u64 parent,
Chris Masone02119d2008-09-05 16:13:11 -04003608 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003609 u64 owner, struct btrfs_key *ins)
Chris Masone02119d2008-09-05 16:13:11 -04003610{
3611 int ret;
3612 struct btrfs_block_group_cache *block_group;
3613
Chris Masone02119d2008-09-05 16:13:11 -04003614 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
Josef Bacikea6a4782008-11-20 12:16:16 -05003615 mutex_lock(&block_group->cache_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04003616 cache_block_group(root, block_group);
Josef Bacikea6a4782008-11-20 12:16:16 -05003617 mutex_unlock(&block_group->cache_mutex);
Chris Masone02119d2008-09-05 16:13:11 -04003618
Josef Bacikea6a4782008-11-20 12:16:16 -05003619 ret = btrfs_remove_free_space(block_group, ins->objectid,
3620 ins->offset);
Josef Bacik0f9dd462008-09-23 13:14:11 -04003621 BUG_ON(ret);
Yan Zhengd2fb3432008-12-11 16:30:39 -05003622 put_block_group(block_group);
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003623 ret = __btrfs_alloc_reserved_extent(trans, root, parent, root_objectid,
3624 ref_generation, owner, ins);
Chris Masone02119d2008-09-05 16:13:11 -04003625 return ret;
3626}
3627
Chris Masone6dcd2d2008-07-17 12:53:50 -04003628/*
3629 * finds a free extent and does all the dirty work required for allocation
3630 * returns the key for the extent through ins, and a tree buffer for
3631 * the first block of the extent through buf.
3632 *
3633 * returns 0 if everything worked, non-zero otherwise.
3634 */
3635int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
3636 struct btrfs_root *root,
Zheng Yan31840ae2008-09-23 13:14:14 -04003637 u64 num_bytes, u64 parent, u64 min_alloc_size,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003638 u64 root_objectid, u64 ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003639 u64 owner_objectid, u64 empty_size, u64 hint_byte,
Chris Masone6dcd2d2008-07-17 12:53:50 -04003640 u64 search_end, struct btrfs_key *ins, u64 data)
3641{
3642 int ret;
3643
Chris Masone6dcd2d2008-07-17 12:53:50 -04003644 ret = __btrfs_reserve_extent(trans, root, num_bytes,
3645 min_alloc_size, empty_size, hint_byte,
3646 search_end, ins, data);
3647 BUG_ON(ret);
Chris Masond00aff02008-09-11 15:54:42 -04003648 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
Zheng Yan31840ae2008-09-23 13:14:14 -04003649 ret = __btrfs_alloc_reserved_extent(trans, root, parent,
3650 root_objectid, ref_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003651 owner_objectid, ins);
Chris Masond00aff02008-09-11 15:54:42 -04003652 BUG_ON(ret);
Chris Masone6dcd2d2008-07-17 12:53:50 -04003653
Zheng Yane8569812008-09-26 10:05:48 -04003654 } else {
3655 update_reserved_extents(root, ins->objectid, ins->offset, 1);
Chris Masond00aff02008-09-11 15:54:42 -04003656 }
Chris Mason925baed2008-06-25 16:01:30 -04003657 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05003658}
Chris Mason65b51a02008-08-01 15:11:20 -04003659
3660struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
3661 struct btrfs_root *root,
Chris Mason4008c042009-02-12 14:09:45 -05003662 u64 bytenr, u32 blocksize,
3663 int level)
Chris Mason65b51a02008-08-01 15:11:20 -04003664{
3665 struct extent_buffer *buf;
3666
3667 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
3668 if (!buf)
3669 return ERR_PTR(-ENOMEM);
3670 btrfs_set_header_generation(buf, trans->transid);
Chris Mason4008c042009-02-12 14:09:45 -05003671 btrfs_set_buffer_lockdep_class(buf, level);
Chris Mason65b51a02008-08-01 15:11:20 -04003672 btrfs_tree_lock(buf);
3673 clean_tree_block(trans, root, buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05003674
3675 btrfs_set_lock_blocking(buf);
Chris Mason65b51a02008-08-01 15:11:20 -04003676 btrfs_set_buffer_uptodate(buf);
Chris Masonb4ce94d2009-02-04 09:25:08 -05003677
Chris Masond0c803c2008-09-11 16:17:57 -04003678 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
3679 set_extent_dirty(&root->dirty_log_pages, buf->start,
Chris Mason65b51a02008-08-01 15:11:20 -04003680 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04003681 } else {
3682 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
3683 buf->start + buf->len - 1, GFP_NOFS);
3684 }
Chris Mason65b51a02008-08-01 15:11:20 -04003685 trans->blocks_used++;
Chris Masonb4ce94d2009-02-04 09:25:08 -05003686 /* this returns a buffer locked for blocking */
Chris Mason65b51a02008-08-01 15:11:20 -04003687 return buf;
3688}
3689
Chris Masonfec577f2007-02-26 10:40:21 -05003690/*
3691 * helper function to allocate a block for a given tree
3692 * returns the tree buffer or NULL.
3693 */
Chris Mason5f39d392007-10-15 16:14:19 -04003694struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Masondb945352007-10-15 16:15:53 -04003695 struct btrfs_root *root,
Zheng Yan31840ae2008-09-23 13:14:14 -04003696 u32 blocksize, u64 parent,
Chris Mason7bb86312007-12-11 09:25:06 -05003697 u64 root_objectid,
3698 u64 ref_generation,
Chris Mason7bb86312007-12-11 09:25:06 -05003699 int level,
3700 u64 hint,
Chris Mason5f39d392007-10-15 16:14:19 -04003701 u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05003702{
Chris Masone2fa7222007-03-12 16:22:34 -04003703 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05003704 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04003705 struct extent_buffer *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05003706
Zheng Yan31840ae2008-09-23 13:14:14 -04003707 ret = btrfs_alloc_extent(trans, root, blocksize, parent, blocksize,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003708 root_objectid, ref_generation, level,
Zheng Yan31840ae2008-09-23 13:14:14 -04003709 empty_size, hint, (u64)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05003710 if (ret) {
Chris Mason54aa1f42007-06-22 14:16:25 -04003711 BUG_ON(ret > 0);
3712 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05003713 }
Chris Mason55c69072008-01-09 15:55:33 -05003714
Chris Mason4008c042009-02-12 14:09:45 -05003715 buf = btrfs_init_new_buffer(trans, root, ins.objectid,
3716 blocksize, level);
Chris Masonfec577f2007-02-26 10:40:21 -05003717 return buf;
3718}
Chris Masona28ec192007-03-06 20:08:01 -05003719
Chris Masone02119d2008-09-05 16:13:11 -04003720int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans,
3721 struct btrfs_root *root, struct extent_buffer *leaf)
Chris Mason6407bf62007-03-27 06:33:00 -04003722{
Chris Mason7bb86312007-12-11 09:25:06 -05003723 u64 leaf_owner;
3724 u64 leaf_generation;
Chris Masonbd56b302009-02-04 09:27:02 -05003725 struct refsort *sorted;
Chris Mason5f39d392007-10-15 16:14:19 -04003726 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04003727 struct btrfs_file_extent_item *fi;
3728 int i;
3729 int nritems;
3730 int ret;
Chris Masonbd56b302009-02-04 09:27:02 -05003731 int refi = 0;
3732 int slot;
Chris Mason6407bf62007-03-27 06:33:00 -04003733
Chris Mason5f39d392007-10-15 16:14:19 -04003734 BUG_ON(!btrfs_is_leaf(leaf));
3735 nritems = btrfs_header_nritems(leaf);
Chris Mason7bb86312007-12-11 09:25:06 -05003736 leaf_owner = btrfs_header_owner(leaf);
3737 leaf_generation = btrfs_header_generation(leaf);
3738
Chris Masonbd56b302009-02-04 09:27:02 -05003739 sorted = kmalloc(sizeof(*sorted) * nritems, GFP_NOFS);
3740 /* we do this loop twice. The first time we build a list
3741 * of the extents we have a reference on, then we sort the list
3742 * by bytenr. The second time around we actually do the
3743 * extent freeing.
3744 */
Chris Mason6407bf62007-03-27 06:33:00 -04003745 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04003746 u64 disk_bytenr;
Chris Masone34a5b42008-07-22 12:08:37 -04003747 cond_resched();
Chris Mason5f39d392007-10-15 16:14:19 -04003748
3749 btrfs_item_key_to_cpu(leaf, &key, i);
Chris Masonbd56b302009-02-04 09:27:02 -05003750
3751 /* only extents have references, skip everything else */
Chris Mason5f39d392007-10-15 16:14:19 -04003752 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -04003753 continue;
Chris Masonbd56b302009-02-04 09:27:02 -05003754
Chris Mason6407bf62007-03-27 06:33:00 -04003755 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Masonbd56b302009-02-04 09:27:02 -05003756
3757 /* inline extents live in the btree, they don't have refs */
Chris Mason5f39d392007-10-15 16:14:19 -04003758 if (btrfs_file_extent_type(leaf, fi) ==
3759 BTRFS_FILE_EXTENT_INLINE)
Chris Mason236454d2007-04-19 13:37:44 -04003760 continue;
Chris Masonbd56b302009-02-04 09:27:02 -05003761
Chris Masondb945352007-10-15 16:15:53 -04003762 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
Chris Masonbd56b302009-02-04 09:27:02 -05003763
3764 /* holes don't have refs */
Chris Masondb945352007-10-15 16:15:53 -04003765 if (disk_bytenr == 0)
Chris Mason3a686372007-05-24 13:35:57 -04003766 continue;
Chris Mason4a096752008-07-21 10:29:44 -04003767
Chris Masonbd56b302009-02-04 09:27:02 -05003768 sorted[refi].bytenr = disk_bytenr;
3769 sorted[refi].slot = i;
3770 refi++;
3771 }
3772
3773 if (refi == 0)
3774 goto out;
3775
3776 sort(sorted, refi, sizeof(struct refsort), refsort_cmp, NULL);
3777
3778 for (i = 0; i < refi; i++) {
3779 u64 disk_bytenr;
3780
3781 disk_bytenr = sorted[i].bytenr;
3782 slot = sorted[i].slot;
3783
3784 cond_resched();
3785
3786 btrfs_item_key_to_cpu(leaf, &key, slot);
3787 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
3788 continue;
3789
3790 fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
3791
Chris Mason925baed2008-06-25 16:01:30 -04003792 ret = __btrfs_free_extent(trans, root, disk_bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -05003793 btrfs_file_extent_disk_num_bytes(leaf, fi),
Zheng Yan31840ae2008-09-23 13:14:14 -04003794 leaf->start, leaf_owner, leaf_generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003795 key.objectid, 0);
Zheng Yan31840ae2008-09-23 13:14:14 -04003796 BUG_ON(ret);
Chris Mason2dd3e672008-08-04 08:20:15 -04003797
3798 atomic_inc(&root->fs_info->throttle_gen);
3799 wake_up(&root->fs_info->transaction_throttle);
3800 cond_resched();
Chris Mason6407bf62007-03-27 06:33:00 -04003801 }
Chris Masonbd56b302009-02-04 09:27:02 -05003802out:
3803 kfree(sorted);
Chris Mason6407bf62007-03-27 06:33:00 -04003804 return 0;
3805}
3806
Chris Masond3977122009-01-05 21:25:51 -05003807static noinline int cache_drop_leaf_ref(struct btrfs_trans_handle *trans,
Chris Masone02119d2008-09-05 16:13:11 -04003808 struct btrfs_root *root,
3809 struct btrfs_leaf_ref *ref)
Yan Zheng31153d82008-07-28 15:32:19 -04003810{
3811 int i;
3812 int ret;
Chris Masonbd56b302009-02-04 09:27:02 -05003813 struct btrfs_extent_info *info;
3814 struct refsort *sorted;
Yan Zheng31153d82008-07-28 15:32:19 -04003815
Chris Masonbd56b302009-02-04 09:27:02 -05003816 if (ref->nritems == 0)
3817 return 0;
3818
3819 sorted = kmalloc(sizeof(*sorted) * ref->nritems, GFP_NOFS);
Yan Zheng31153d82008-07-28 15:32:19 -04003820 for (i = 0; i < ref->nritems; i++) {
Chris Masonbd56b302009-02-04 09:27:02 -05003821 sorted[i].bytenr = ref->extents[i].bytenr;
3822 sorted[i].slot = i;
3823 }
3824 sort(sorted, ref->nritems, sizeof(struct refsort), refsort_cmp, NULL);
3825
3826 /*
3827 * the items in the ref were sorted when the ref was inserted
3828 * into the ref cache, so this is already in order
3829 */
3830 for (i = 0; i < ref->nritems; i++) {
3831 info = ref->extents + sorted[i].slot;
Zheng Yan31840ae2008-09-23 13:14:14 -04003832 ret = __btrfs_free_extent(trans, root, info->bytenr,
3833 info->num_bytes, ref->bytenr,
3834 ref->owner, ref->generation,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04003835 info->objectid, 0);
Chris Mason2dd3e672008-08-04 08:20:15 -04003836
3837 atomic_inc(&root->fs_info->throttle_gen);
3838 wake_up(&root->fs_info->transaction_throttle);
3839 cond_resched();
3840
Yan Zheng31153d82008-07-28 15:32:19 -04003841 BUG_ON(ret);
3842 info++;
3843 }
Yan Zheng31153d82008-07-28 15:32:19 -04003844
Chris Mason806638b2009-02-05 09:08:14 -05003845 kfree(sorted);
Yan Zheng31153d82008-07-28 15:32:19 -04003846 return 0;
3847}
3848
Chris Masond3977122009-01-05 21:25:51 -05003849static int drop_snap_lookup_refcount(struct btrfs_root *root, u64 start,
3850 u64 len, u32 *refs)
Chris Mason333db942008-06-25 16:01:30 -04003851{
Chris Mason017e5362008-07-28 15:32:51 -04003852 int ret;
Chris Masonf87f0572008-08-01 11:27:23 -04003853
Zheng Yan31840ae2008-09-23 13:14:14 -04003854 ret = btrfs_lookup_extent_ref(NULL, root, start, len, refs);
Chris Masonf87f0572008-08-01 11:27:23 -04003855 BUG_ON(ret);
3856
Chris Masond3977122009-01-05 21:25:51 -05003857#if 0 /* some debugging code in case we see problems here */
Chris Masonf87f0572008-08-01 11:27:23 -04003858 /* if the refs count is one, it won't get increased again. But
3859 * if the ref count is > 1, someone may be decreasing it at
3860 * the same time we are.
3861 */
3862 if (*refs != 1) {
3863 struct extent_buffer *eb = NULL;
3864 eb = btrfs_find_create_tree_block(root, start, len);
3865 if (eb)
3866 btrfs_tree_lock(eb);
3867
3868 mutex_lock(&root->fs_info->alloc_mutex);
3869 ret = lookup_extent_ref(NULL, root, start, len, refs);
3870 BUG_ON(ret);
3871 mutex_unlock(&root->fs_info->alloc_mutex);
3872
3873 if (eb) {
3874 btrfs_tree_unlock(eb);
3875 free_extent_buffer(eb);
3876 }
3877 if (*refs == 1) {
Chris Masond3977122009-01-05 21:25:51 -05003878 printk(KERN_ERR "btrfs block %llu went down to one "
3879 "during drop_snap\n", (unsigned long long)start);
Chris Masonf87f0572008-08-01 11:27:23 -04003880 }
3881
3882 }
3883#endif
3884
Chris Masone7a84562008-06-25 16:01:31 -04003885 cond_resched();
Chris Mason017e5362008-07-28 15:32:51 -04003886 return ret;
Chris Mason333db942008-06-25 16:01:30 -04003887}
3888
3889/*
Chris Masonbd56b302009-02-04 09:27:02 -05003890 * this is used while deleting old snapshots, and it drops the refs
3891 * on a whole subtree starting from a level 1 node.
3892 *
3893 * The idea is to sort all the leaf pointers, and then drop the
3894 * ref on all the leaves in order. Most of the time the leaves
3895 * will have ref cache entries, so no leaf IOs will be required to
3896 * find the extents they have references on.
3897 *
3898 * For each leaf, any references it has are also dropped in order
3899 *
3900 * This ends up dropping the references in something close to optimal
3901 * order for reading and modifying the extent allocation tree.
3902 */
3903static noinline int drop_level_one_refs(struct btrfs_trans_handle *trans,
3904 struct btrfs_root *root,
3905 struct btrfs_path *path)
3906{
3907 u64 bytenr;
3908 u64 root_owner;
3909 u64 root_gen;
3910 struct extent_buffer *eb = path->nodes[1];
3911 struct extent_buffer *leaf;
3912 struct btrfs_leaf_ref *ref;
3913 struct refsort *sorted = NULL;
3914 int nritems = btrfs_header_nritems(eb);
3915 int ret;
3916 int i;
3917 int refi = 0;
3918 int slot = path->slots[1];
3919 u32 blocksize = btrfs_level_size(root, 0);
3920 u32 refs;
3921
3922 if (nritems == 0)
3923 goto out;
3924
3925 root_owner = btrfs_header_owner(eb);
3926 root_gen = btrfs_header_generation(eb);
3927 sorted = kmalloc(sizeof(*sorted) * nritems, GFP_NOFS);
3928
3929 /*
3930 * step one, sort all the leaf pointers so we don't scribble
3931 * randomly into the extent allocation tree
3932 */
3933 for (i = slot; i < nritems; i++) {
3934 sorted[refi].bytenr = btrfs_node_blockptr(eb, i);
3935 sorted[refi].slot = i;
3936 refi++;
3937 }
3938
3939 /*
3940 * nritems won't be zero, but if we're picking up drop_snapshot
3941 * after a crash, slot might be > 0, so double check things
3942 * just in case.
3943 */
3944 if (refi == 0)
3945 goto out;
3946
3947 sort(sorted, refi, sizeof(struct refsort), refsort_cmp, NULL);
3948
3949 /*
3950 * the first loop frees everything the leaves point to
3951 */
3952 for (i = 0; i < refi; i++) {
3953 u64 ptr_gen;
3954
3955 bytenr = sorted[i].bytenr;
3956
3957 /*
3958 * check the reference count on this leaf. If it is > 1
3959 * we just decrement it below and don't update any
3960 * of the refs the leaf points to.
3961 */
3962 ret = drop_snap_lookup_refcount(root, bytenr, blocksize, &refs);
3963 BUG_ON(ret);
3964 if (refs != 1)
3965 continue;
3966
3967 ptr_gen = btrfs_node_ptr_generation(eb, sorted[i].slot);
3968
3969 /*
3970 * the leaf only had one reference, which means the
3971 * only thing pointing to this leaf is the snapshot
3972 * we're deleting. It isn't possible for the reference
3973 * count to increase again later
3974 *
3975 * The reference cache is checked for the leaf,
3976 * and if found we'll be able to drop any refs held by
3977 * the leaf without needing to read it in.
3978 */
3979 ref = btrfs_lookup_leaf_ref(root, bytenr);
3980 if (ref && ref->generation != ptr_gen) {
3981 btrfs_free_leaf_ref(root, ref);
3982 ref = NULL;
3983 }
3984 if (ref) {
3985 ret = cache_drop_leaf_ref(trans, root, ref);
3986 BUG_ON(ret);
3987 btrfs_remove_leaf_ref(root, ref);
3988 btrfs_free_leaf_ref(root, ref);
3989 } else {
3990 /*
3991 * the leaf wasn't in the reference cache, so
3992 * we have to read it.
3993 */
3994 leaf = read_tree_block(root, bytenr, blocksize,
3995 ptr_gen);
3996 ret = btrfs_drop_leaf_ref(trans, root, leaf);
3997 BUG_ON(ret);
3998 free_extent_buffer(leaf);
3999 }
4000 atomic_inc(&root->fs_info->throttle_gen);
4001 wake_up(&root->fs_info->transaction_throttle);
4002 cond_resched();
4003 }
4004
4005 /*
4006 * run through the loop again to free the refs on the leaves.
4007 * This is faster than doing it in the loop above because
4008 * the leaves are likely to be clustered together. We end up
4009 * working in nice chunks on the extent allocation tree.
4010 */
4011 for (i = 0; i < refi; i++) {
4012 bytenr = sorted[i].bytenr;
4013 ret = __btrfs_free_extent(trans, root, bytenr,
4014 blocksize, eb->start,
4015 root_owner, root_gen, 0, 1);
4016 BUG_ON(ret);
4017
4018 atomic_inc(&root->fs_info->throttle_gen);
4019 wake_up(&root->fs_info->transaction_throttle);
4020 cond_resched();
4021 }
4022out:
4023 kfree(sorted);
4024
4025 /*
4026 * update the path to show we've processed the entire level 1
4027 * node. This will get saved into the root's drop_snapshot_progress
4028 * field so these drops are not repeated again if this transaction
4029 * commits.
4030 */
4031 path->slots[1] = nritems;
4032 return 0;
4033}
4034
4035/*
Chris Mason9aca1d52007-03-13 11:09:37 -04004036 * helper function for drop_snapshot, this walks down the tree dropping ref
4037 * counts as it goes.
4038 */
Chris Masond3977122009-01-05 21:25:51 -05004039static noinline int walk_down_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05004040 struct btrfs_root *root,
4041 struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05004042{
Chris Mason7bb86312007-12-11 09:25:06 -05004043 u64 root_owner;
4044 u64 root_gen;
4045 u64 bytenr;
Chris Masonca7a79a2008-05-12 12:59:19 -04004046 u64 ptr_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04004047 struct extent_buffer *next;
4048 struct extent_buffer *cur;
Chris Mason7bb86312007-12-11 09:25:06 -05004049 struct extent_buffer *parent;
Chris Masondb945352007-10-15 16:15:53 -04004050 u32 blocksize;
Chris Mason20524f02007-03-10 06:35:47 -05004051 int ret;
4052 u32 refs;
4053
Chris Mason5caf2a02007-04-02 11:20:42 -04004054 WARN_ON(*level < 0);
4055 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason333db942008-06-25 16:01:30 -04004056 ret = drop_snap_lookup_refcount(root, path->nodes[*level]->start,
Chris Masondb945352007-10-15 16:15:53 -04004057 path->nodes[*level]->len, &refs);
Chris Mason20524f02007-03-10 06:35:47 -05004058 BUG_ON(ret);
4059 if (refs > 1)
4060 goto out;
Chris Masone0115992007-06-19 16:23:05 -04004061
Chris Mason9aca1d52007-03-13 11:09:37 -04004062 /*
4063 * walk down to the last node level and free all the leaves
4064 */
Chris Masond3977122009-01-05 21:25:51 -05004065 while (*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04004066 WARN_ON(*level < 0);
4067 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -05004068 cur = path->nodes[*level];
Chris Masone0115992007-06-19 16:23:05 -04004069
Chris Mason5f39d392007-10-15 16:14:19 -04004070 if (btrfs_header_level(cur) != *level)
Chris Mason2c90e5d2007-04-02 10:50:19 -04004071 WARN_ON(1);
Chris Masone0115992007-06-19 16:23:05 -04004072
Chris Mason7518a232007-03-12 12:01:18 -04004073 if (path->slots[*level] >=
Chris Mason5f39d392007-10-15 16:14:19 -04004074 btrfs_header_nritems(cur))
Chris Mason20524f02007-03-10 06:35:47 -05004075 break;
Chris Masonbd56b302009-02-04 09:27:02 -05004076
4077 /* the new code goes down to level 1 and does all the
4078 * leaves pointed to that node in bulk. So, this check
4079 * for level 0 will always be false.
4080 *
4081 * But, the disk format allows the drop_snapshot_progress
4082 * field in the root to leave things in a state where
4083 * a leaf will need cleaning up here. If someone crashes
4084 * with the old code and then boots with the new code,
4085 * we might find a leaf here.
4086 */
Chris Mason6407bf62007-03-27 06:33:00 -04004087 if (*level == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04004088 ret = btrfs_drop_leaf_ref(trans, root, cur);
Chris Mason6407bf62007-03-27 06:33:00 -04004089 BUG_ON(ret);
4090 break;
4091 }
Chris Masonbd56b302009-02-04 09:27:02 -05004092
4093 /*
4094 * once we get to level one, process the whole node
4095 * at once, including everything below it.
4096 */
4097 if (*level == 1) {
4098 ret = drop_level_one_refs(trans, root, path);
4099 BUG_ON(ret);
4100 break;
4101 }
4102
Chris Masondb945352007-10-15 16:15:53 -04004103 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
Chris Masonca7a79a2008-05-12 12:59:19 -04004104 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
Chris Masondb945352007-10-15 16:15:53 -04004105 blocksize = btrfs_level_size(root, *level - 1);
Chris Mason925baed2008-06-25 16:01:30 -04004106
Chris Mason333db942008-06-25 16:01:30 -04004107 ret = drop_snap_lookup_refcount(root, bytenr, blocksize, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -04004108 BUG_ON(ret);
Chris Masonbd56b302009-02-04 09:27:02 -05004109
4110 /*
4111 * if there is more than one reference, we don't need
4112 * to read that node to drop any references it has. We
4113 * just drop the ref we hold on that node and move on to the
4114 * next slot in this level.
4115 */
Chris Mason6407bf62007-03-27 06:33:00 -04004116 if (refs != 1) {
Chris Mason7bb86312007-12-11 09:25:06 -05004117 parent = path->nodes[*level];
4118 root_owner = btrfs_header_owner(parent);
4119 root_gen = btrfs_header_generation(parent);
Chris Mason20524f02007-03-10 06:35:47 -05004120 path->slots[*level]++;
Chris Masonf87f0572008-08-01 11:27:23 -04004121
Chris Mason925baed2008-06-25 16:01:30 -04004122 ret = __btrfs_free_extent(trans, root, bytenr,
Zheng Yan31840ae2008-09-23 13:14:14 -04004123 blocksize, parent->start,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004124 root_owner, root_gen,
4125 *level - 1, 1);
Chris Mason20524f02007-03-10 06:35:47 -05004126 BUG_ON(ret);
Chris Mason18e35e02008-08-01 13:11:41 -04004127
4128 atomic_inc(&root->fs_info->throttle_gen);
4129 wake_up(&root->fs_info->transaction_throttle);
Chris Mason2dd3e672008-08-04 08:20:15 -04004130 cond_resched();
Chris Mason18e35e02008-08-01 13:11:41 -04004131
Chris Mason20524f02007-03-10 06:35:47 -05004132 continue;
4133 }
Chris Mason333db942008-06-25 16:01:30 -04004134
Chris Masonbd56b302009-02-04 09:27:02 -05004135 /*
4136 * we need to keep freeing things in the next level down.
4137 * read the block and loop around to process it
4138 */
4139 next = read_tree_block(root, bytenr, blocksize, ptr_gen);
Chris Mason5caf2a02007-04-02 11:20:42 -04004140 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -04004141 if (path->nodes[*level-1])
Chris Mason5f39d392007-10-15 16:14:19 -04004142 free_extent_buffer(path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -05004143 path->nodes[*level-1] = next;
Chris Mason5f39d392007-10-15 16:14:19 -04004144 *level = btrfs_header_level(next);
Chris Mason20524f02007-03-10 06:35:47 -05004145 path->slots[*level] = 0;
Chris Mason2dd3e672008-08-04 08:20:15 -04004146 cond_resched();
Chris Mason20524f02007-03-10 06:35:47 -05004147 }
4148out:
Chris Mason5caf2a02007-04-02 11:20:42 -04004149 WARN_ON(*level < 0);
4150 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason7bb86312007-12-11 09:25:06 -05004151
4152 if (path->nodes[*level] == root->node) {
Chris Mason7bb86312007-12-11 09:25:06 -05004153 parent = path->nodes[*level];
Yan Zheng31153d82008-07-28 15:32:19 -04004154 bytenr = path->nodes[*level]->start;
Chris Mason7bb86312007-12-11 09:25:06 -05004155 } else {
4156 parent = path->nodes[*level + 1];
Yan Zheng31153d82008-07-28 15:32:19 -04004157 bytenr = btrfs_node_blockptr(parent, path->slots[*level + 1]);
Chris Mason7bb86312007-12-11 09:25:06 -05004158 }
4159
Yan Zheng31153d82008-07-28 15:32:19 -04004160 blocksize = btrfs_level_size(root, *level);
4161 root_owner = btrfs_header_owner(parent);
Chris Mason7bb86312007-12-11 09:25:06 -05004162 root_gen = btrfs_header_generation(parent);
Yan Zheng31153d82008-07-28 15:32:19 -04004163
Chris Masonbd56b302009-02-04 09:27:02 -05004164 /*
4165 * cleanup and free the reference on the last node
4166 * we processed
4167 */
Yan Zheng31153d82008-07-28 15:32:19 -04004168 ret = __btrfs_free_extent(trans, root, bytenr, blocksize,
Zheng Yan31840ae2008-09-23 13:14:14 -04004169 parent->start, root_owner, root_gen,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004170 *level, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04004171 free_extent_buffer(path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -05004172 path->nodes[*level] = NULL;
Chris Masonbd56b302009-02-04 09:27:02 -05004173
Chris Mason20524f02007-03-10 06:35:47 -05004174 *level += 1;
4175 BUG_ON(ret);
Chris Masonf87f0572008-08-01 11:27:23 -04004176
Chris Masone7a84562008-06-25 16:01:31 -04004177 cond_resched();
Chris Mason20524f02007-03-10 06:35:47 -05004178 return 0;
4179}
4180
Chris Mason9aca1d52007-03-13 11:09:37 -04004181/*
Yan Zhengf82d02d2008-10-29 14:49:05 -04004182 * helper function for drop_subtree, this function is similar to
4183 * walk_down_tree. The main difference is that it checks reference
4184 * counts while tree blocks are locked.
4185 */
Chris Masond3977122009-01-05 21:25:51 -05004186static noinline int walk_down_subtree(struct btrfs_trans_handle *trans,
Yan Zhengf82d02d2008-10-29 14:49:05 -04004187 struct btrfs_root *root,
4188 struct btrfs_path *path, int *level)
4189{
4190 struct extent_buffer *next;
4191 struct extent_buffer *cur;
4192 struct extent_buffer *parent;
4193 u64 bytenr;
4194 u64 ptr_gen;
4195 u32 blocksize;
4196 u32 refs;
4197 int ret;
4198
4199 cur = path->nodes[*level];
4200 ret = btrfs_lookup_extent_ref(trans, root, cur->start, cur->len,
4201 &refs);
4202 BUG_ON(ret);
4203 if (refs > 1)
4204 goto out;
4205
4206 while (*level >= 0) {
4207 cur = path->nodes[*level];
4208 if (*level == 0) {
4209 ret = btrfs_drop_leaf_ref(trans, root, cur);
4210 BUG_ON(ret);
4211 clean_tree_block(trans, root, cur);
4212 break;
4213 }
4214 if (path->slots[*level] >= btrfs_header_nritems(cur)) {
4215 clean_tree_block(trans, root, cur);
4216 break;
4217 }
4218
4219 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
4220 blocksize = btrfs_level_size(root, *level - 1);
4221 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
4222
4223 next = read_tree_block(root, bytenr, blocksize, ptr_gen);
4224 btrfs_tree_lock(next);
Chris Masonb4ce94d2009-02-04 09:25:08 -05004225 btrfs_set_lock_blocking(next);
Yan Zhengf82d02d2008-10-29 14:49:05 -04004226
4227 ret = btrfs_lookup_extent_ref(trans, root, bytenr, blocksize,
4228 &refs);
4229 BUG_ON(ret);
4230 if (refs > 1) {
4231 parent = path->nodes[*level];
4232 ret = btrfs_free_extent(trans, root, bytenr,
4233 blocksize, parent->start,
4234 btrfs_header_owner(parent),
4235 btrfs_header_generation(parent),
4236 *level - 1, 1);
4237 BUG_ON(ret);
4238 path->slots[*level]++;
4239 btrfs_tree_unlock(next);
4240 free_extent_buffer(next);
4241 continue;
4242 }
4243
4244 *level = btrfs_header_level(next);
4245 path->nodes[*level] = next;
4246 path->slots[*level] = 0;
4247 path->locks[*level] = 1;
4248 cond_resched();
4249 }
4250out:
4251 parent = path->nodes[*level + 1];
4252 bytenr = path->nodes[*level]->start;
4253 blocksize = path->nodes[*level]->len;
4254
4255 ret = btrfs_free_extent(trans, root, bytenr, blocksize,
4256 parent->start, btrfs_header_owner(parent),
4257 btrfs_header_generation(parent), *level, 1);
4258 BUG_ON(ret);
4259
4260 if (path->locks[*level]) {
4261 btrfs_tree_unlock(path->nodes[*level]);
4262 path->locks[*level] = 0;
4263 }
4264 free_extent_buffer(path->nodes[*level]);
4265 path->nodes[*level] = NULL;
4266 *level += 1;
4267 cond_resched();
4268 return 0;
4269}
4270
4271/*
Chris Mason9aca1d52007-03-13 11:09:37 -04004272 * helper for dropping snapshots. This walks back up the tree in the path
4273 * to find the first node higher up where we haven't yet gone through
4274 * all the slots
4275 */
Chris Masond3977122009-01-05 21:25:51 -05004276static noinline int walk_up_tree(struct btrfs_trans_handle *trans,
Chris Mason98ed5172008-01-03 10:01:48 -05004277 struct btrfs_root *root,
Yan Zhengf82d02d2008-10-29 14:49:05 -04004278 struct btrfs_path *path,
4279 int *level, int max_level)
Chris Mason20524f02007-03-10 06:35:47 -05004280{
Chris Mason7bb86312007-12-11 09:25:06 -05004281 u64 root_owner;
4282 u64 root_gen;
4283 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05004284 int i;
4285 int slot;
4286 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04004287
Yan Zhengf82d02d2008-10-29 14:49:05 -04004288 for (i = *level; i < max_level && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -05004289 slot = path->slots[i];
Chris Mason5f39d392007-10-15 16:14:19 -04004290 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
4291 struct extent_buffer *node;
4292 struct btrfs_disk_key disk_key;
Chris Masonbd56b302009-02-04 09:27:02 -05004293
4294 /*
4295 * there is more work to do in this level.
4296 * Update the drop_progress marker to reflect
4297 * the work we've done so far, and then bump
4298 * the slot number
4299 */
Chris Mason5f39d392007-10-15 16:14:19 -04004300 node = path->nodes[i];
Chris Mason20524f02007-03-10 06:35:47 -05004301 path->slots[i]++;
4302 *level = i;
Chris Mason9f3a7422007-08-07 15:52:19 -04004303 WARN_ON(*level == 0);
Chris Mason5f39d392007-10-15 16:14:19 -04004304 btrfs_node_key(node, &disk_key, path->slots[i]);
Chris Mason9f3a7422007-08-07 15:52:19 -04004305 memcpy(&root_item->drop_progress,
Chris Mason5f39d392007-10-15 16:14:19 -04004306 &disk_key, sizeof(disk_key));
Chris Mason9f3a7422007-08-07 15:52:19 -04004307 root_item->drop_level = i;
Chris Mason20524f02007-03-10 06:35:47 -05004308 return 0;
4309 } else {
Zheng Yan31840ae2008-09-23 13:14:14 -04004310 struct extent_buffer *parent;
Chris Masonbd56b302009-02-04 09:27:02 -05004311
4312 /*
4313 * this whole node is done, free our reference
4314 * on it and go up one level
4315 */
Zheng Yan31840ae2008-09-23 13:14:14 -04004316 if (path->nodes[*level] == root->node)
4317 parent = path->nodes[*level];
4318 else
4319 parent = path->nodes[*level + 1];
4320
4321 root_owner = btrfs_header_owner(parent);
4322 root_gen = btrfs_header_generation(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04004323
4324 clean_tree_block(trans, root, path->nodes[*level]);
Chris Masone089f052007-03-16 16:20:31 -04004325 ret = btrfs_free_extent(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04004326 path->nodes[*level]->start,
Chris Mason7bb86312007-12-11 09:25:06 -05004327 path->nodes[*level]->len,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004328 parent->start, root_owner,
4329 root_gen, *level, 1);
Chris Mason6407bf62007-03-27 06:33:00 -04004330 BUG_ON(ret);
Yan Zhengf82d02d2008-10-29 14:49:05 -04004331 if (path->locks[*level]) {
4332 btrfs_tree_unlock(path->nodes[*level]);
4333 path->locks[*level] = 0;
4334 }
Chris Mason5f39d392007-10-15 16:14:19 -04004335 free_extent_buffer(path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -04004336 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -05004337 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -05004338 }
4339 }
4340 return 1;
4341}
4342
Chris Mason9aca1d52007-03-13 11:09:37 -04004343/*
4344 * drop the reference count on the tree rooted at 'snap'. This traverses
4345 * the tree freeing any blocks that have a ref count of zero after being
4346 * decremented.
4347 */
Chris Masone089f052007-03-16 16:20:31 -04004348int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason9f3a7422007-08-07 15:52:19 -04004349 *root)
Chris Mason20524f02007-03-10 06:35:47 -05004350{
Chris Mason3768f362007-03-13 16:47:54 -04004351 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -04004352 int wret;
Chris Mason20524f02007-03-10 06:35:47 -05004353 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -04004354 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -05004355 int i;
4356 int orig_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04004357 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05004358
Chris Masona2135012008-06-25 16:01:30 -04004359 WARN_ON(!mutex_is_locked(&root->fs_info->drop_mutex));
Chris Mason5caf2a02007-04-02 11:20:42 -04004360 path = btrfs_alloc_path();
4361 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05004362
Chris Mason5f39d392007-10-15 16:14:19 -04004363 level = btrfs_header_level(root->node);
Chris Mason20524f02007-03-10 06:35:47 -05004364 orig_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04004365 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
4366 path->nodes[level] = root->node;
Chris Masonf510cfe2007-10-15 16:14:48 -04004367 extent_buffer_get(root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -04004368 path->slots[level] = 0;
4369 } else {
4370 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04004371 struct btrfs_disk_key found_key;
4372 struct extent_buffer *node;
Chris Mason6702ed42007-08-07 16:15:09 -04004373
Chris Mason9f3a7422007-08-07 15:52:19 -04004374 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Chris Mason6702ed42007-08-07 16:15:09 -04004375 level = root_item->drop_level;
4376 path->lowest_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04004377 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason6702ed42007-08-07 16:15:09 -04004378 if (wret < 0) {
Chris Mason9f3a7422007-08-07 15:52:19 -04004379 ret = wret;
4380 goto out;
4381 }
Chris Mason5f39d392007-10-15 16:14:19 -04004382 node = path->nodes[level];
4383 btrfs_node_key(node, &found_key, path->slots[level]);
4384 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
4385 sizeof(found_key)));
Chris Mason7d9eb122008-07-08 14:19:17 -04004386 /*
4387 * unlock our path, this is safe because only this
4388 * function is allowed to delete this snapshot
4389 */
Chris Mason925baed2008-06-25 16:01:30 -04004390 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
4391 if (path->nodes[i] && path->locks[i]) {
4392 path->locks[i] = 0;
4393 btrfs_tree_unlock(path->nodes[i]);
4394 }
4395 }
Chris Mason9f3a7422007-08-07 15:52:19 -04004396 }
Chris Masond3977122009-01-05 21:25:51 -05004397 while (1) {
Chris Mason5caf2a02007-04-02 11:20:42 -04004398 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04004399 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05004400 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04004401 if (wret < 0)
4402 ret = wret;
4403
Yan Zhengf82d02d2008-10-29 14:49:05 -04004404 wret = walk_up_tree(trans, root, path, &level,
4405 BTRFS_MAX_LEVEL);
Chris Mason9aca1d52007-03-13 11:09:37 -04004406 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05004407 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04004408 if (wret < 0)
4409 ret = wret;
Chris Masone7a84562008-06-25 16:01:31 -04004410 if (trans->transaction->in_commit) {
4411 ret = -EAGAIN;
4412 break;
4413 }
Chris Mason18e35e02008-08-01 13:11:41 -04004414 atomic_inc(&root->fs_info->throttle_gen);
Chris Mason017e5362008-07-28 15:32:51 -04004415 wake_up(&root->fs_info->transaction_throttle);
Chris Mason20524f02007-03-10 06:35:47 -05004416 }
Chris Mason83e15a22007-03-12 09:03:27 -04004417 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -04004418 if (path->nodes[i]) {
Chris Mason5f39d392007-10-15 16:14:19 -04004419 free_extent_buffer(path->nodes[i]);
Chris Mason0f827312007-10-15 16:18:56 -04004420 path->nodes[i] = NULL;
Chris Mason83e15a22007-03-12 09:03:27 -04004421 }
Chris Mason20524f02007-03-10 06:35:47 -05004422 }
Chris Mason9f3a7422007-08-07 15:52:19 -04004423out:
Chris Mason5caf2a02007-04-02 11:20:42 -04004424 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -04004425 return ret;
Chris Mason20524f02007-03-10 06:35:47 -05004426}
Chris Mason9078a3e2007-04-26 16:46:15 -04004427
Yan Zhengf82d02d2008-10-29 14:49:05 -04004428int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
4429 struct btrfs_root *root,
4430 struct extent_buffer *node,
4431 struct extent_buffer *parent)
4432{
4433 struct btrfs_path *path;
4434 int level;
4435 int parent_level;
4436 int ret = 0;
4437 int wret;
4438
4439 path = btrfs_alloc_path();
4440 BUG_ON(!path);
4441
Chris Masonb9447ef2009-03-09 11:45:38 -04004442 btrfs_assert_tree_locked(parent);
Yan Zhengf82d02d2008-10-29 14:49:05 -04004443 parent_level = btrfs_header_level(parent);
4444 extent_buffer_get(parent);
4445 path->nodes[parent_level] = parent;
4446 path->slots[parent_level] = btrfs_header_nritems(parent);
4447
Chris Masonb9447ef2009-03-09 11:45:38 -04004448 btrfs_assert_tree_locked(node);
Yan Zhengf82d02d2008-10-29 14:49:05 -04004449 level = btrfs_header_level(node);
4450 extent_buffer_get(node);
4451 path->nodes[level] = node;
4452 path->slots[level] = 0;
4453
4454 while (1) {
4455 wret = walk_down_subtree(trans, root, path, &level);
4456 if (wret < 0)
4457 ret = wret;
4458 if (wret != 0)
4459 break;
4460
4461 wret = walk_up_tree(trans, root, path, &level, parent_level);
4462 if (wret < 0)
4463 ret = wret;
4464 if (wret != 0)
4465 break;
4466 }
4467
4468 btrfs_free_path(path);
4469 return ret;
4470}
4471
Chris Mason8e7bf942008-04-28 09:02:36 -04004472static unsigned long calc_ra(unsigned long start, unsigned long last,
4473 unsigned long nr)
4474{
4475 return min(last, start + nr - 1);
4476}
4477
Chris Masond3977122009-01-05 21:25:51 -05004478static noinline int relocate_inode_pages(struct inode *inode, u64 start,
Chris Mason98ed5172008-01-03 10:01:48 -05004479 u64 len)
Chris Masonedbd8d42007-12-21 16:27:24 -05004480{
4481 u64 page_start;
4482 u64 page_end;
Zheng Yan1a40e232008-09-26 10:09:34 -04004483 unsigned long first_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05004484 unsigned long last_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05004485 unsigned long i;
4486 struct page *page;
Chris Masond1310b22008-01-24 16:13:08 -05004487 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason4313b392008-01-03 09:08:48 -05004488 struct file_ra_state *ra;
Chris Mason3eaa2882008-07-24 11:57:52 -04004489 struct btrfs_ordered_extent *ordered;
Zheng Yan1a40e232008-09-26 10:09:34 -04004490 unsigned int total_read = 0;
4491 unsigned int total_dirty = 0;
4492 int ret = 0;
Chris Mason4313b392008-01-03 09:08:48 -05004493
4494 ra = kzalloc(sizeof(*ra), GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05004495
4496 mutex_lock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04004497 first_index = start >> PAGE_CACHE_SHIFT;
Chris Masonedbd8d42007-12-21 16:27:24 -05004498 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
4499
Zheng Yan1a40e232008-09-26 10:09:34 -04004500 /* make sure the dirty trick played by the caller work */
4501 ret = invalidate_inode_pages2_range(inode->i_mapping,
4502 first_index, last_index);
4503 if (ret)
4504 goto out_unlock;
Chris Mason8e7bf942008-04-28 09:02:36 -04004505
Chris Mason4313b392008-01-03 09:08:48 -05004506 file_ra_state_init(ra, inode->i_mapping);
Chris Masonedbd8d42007-12-21 16:27:24 -05004507
Zheng Yan1a40e232008-09-26 10:09:34 -04004508 for (i = first_index ; i <= last_index; i++) {
4509 if (total_read % ra->ra_pages == 0) {
Chris Mason8e7bf942008-04-28 09:02:36 -04004510 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
Zheng Yan1a40e232008-09-26 10:09:34 -04004511 calc_ra(i, last_index, ra->ra_pages));
Chris Mason8e7bf942008-04-28 09:02:36 -04004512 }
4513 total_read++;
Chris Mason3eaa2882008-07-24 11:57:52 -04004514again:
4515 if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
Zheng Yan1a40e232008-09-26 10:09:34 -04004516 BUG_ON(1);
Chris Masonedbd8d42007-12-21 16:27:24 -05004517 page = grab_cache_page(inode->i_mapping, i);
Chris Masona061fc82008-05-07 11:43:44 -04004518 if (!page) {
Zheng Yan1a40e232008-09-26 10:09:34 -04004519 ret = -ENOMEM;
Chris Masonedbd8d42007-12-21 16:27:24 -05004520 goto out_unlock;
Chris Masona061fc82008-05-07 11:43:44 -04004521 }
Chris Masonedbd8d42007-12-21 16:27:24 -05004522 if (!PageUptodate(page)) {
4523 btrfs_readpage(NULL, page);
4524 lock_page(page);
4525 if (!PageUptodate(page)) {
4526 unlock_page(page);
4527 page_cache_release(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04004528 ret = -EIO;
Chris Masonedbd8d42007-12-21 16:27:24 -05004529 goto out_unlock;
4530 }
4531 }
Chris Masonec44a352008-04-28 15:29:52 -04004532 wait_on_page_writeback(page);
Chris Mason3eaa2882008-07-24 11:57:52 -04004533
Chris Masonedbd8d42007-12-21 16:27:24 -05004534 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
4535 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05004536 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05004537
Chris Mason3eaa2882008-07-24 11:57:52 -04004538 ordered = btrfs_lookup_ordered_extent(inode, page_start);
4539 if (ordered) {
4540 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
4541 unlock_page(page);
4542 page_cache_release(page);
4543 btrfs_start_ordered_extent(inode, ordered, 1);
4544 btrfs_put_ordered_extent(ordered);
4545 goto again;
4546 }
4547 set_page_extent_mapped(page);
4548
Zheng Yan1a40e232008-09-26 10:09:34 -04004549 if (i == first_index)
4550 set_extent_bits(io_tree, page_start, page_end,
4551 EXTENT_BOUNDARY, GFP_NOFS);
Yan Zheng1f80e4d2008-12-19 10:59:04 -05004552 btrfs_set_extent_delalloc(inode, page_start, page_end);
Zheng Yan1a40e232008-09-26 10:09:34 -04004553
Chris Masona061fc82008-05-07 11:43:44 -04004554 set_page_dirty(page);
Zheng Yan1a40e232008-09-26 10:09:34 -04004555 total_dirty++;
Chris Masonedbd8d42007-12-21 16:27:24 -05004556
Chris Masond1310b22008-01-24 16:13:08 -05004557 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05004558 unlock_page(page);
4559 page_cache_release(page);
4560 }
4561
4562out_unlock:
Chris Masonec44a352008-04-28 15:29:52 -04004563 kfree(ra);
Chris Masonedbd8d42007-12-21 16:27:24 -05004564 mutex_unlock(&inode->i_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04004565 balance_dirty_pages_ratelimited_nr(inode->i_mapping, total_dirty);
Chris Masonbf4ef672008-05-08 13:26:18 -04004566 return ret;
4567}
4568
Chris Masond3977122009-01-05 21:25:51 -05004569static noinline int relocate_data_extent(struct inode *reloc_inode,
Zheng Yan1a40e232008-09-26 10:09:34 -04004570 struct btrfs_key *extent_key,
4571 u64 offset)
Chris Masonedbd8d42007-12-21 16:27:24 -05004572{
Zheng Yan1a40e232008-09-26 10:09:34 -04004573 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
4574 struct extent_map_tree *em_tree = &BTRFS_I(reloc_inode)->extent_tree;
4575 struct extent_map *em;
Yan Zheng66435582008-10-30 14:19:50 -04004576 u64 start = extent_key->objectid - offset;
4577 u64 end = start + extent_key->offset - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04004578
4579 em = alloc_extent_map(GFP_NOFS);
4580 BUG_ON(!em || IS_ERR(em));
4581
Yan Zheng66435582008-10-30 14:19:50 -04004582 em->start = start;
Zheng Yan1a40e232008-09-26 10:09:34 -04004583 em->len = extent_key->offset;
Chris Masonc8b97812008-10-29 14:49:59 -04004584 em->block_len = extent_key->offset;
Zheng Yan1a40e232008-09-26 10:09:34 -04004585 em->block_start = extent_key->objectid;
4586 em->bdev = root->fs_info->fs_devices->latest_bdev;
4587 set_bit(EXTENT_FLAG_PINNED, &em->flags);
4588
4589 /* setup extent map to cheat btrfs_readpage */
Yan Zheng66435582008-10-30 14:19:50 -04004590 lock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04004591 while (1) {
4592 int ret;
4593 spin_lock(&em_tree->lock);
4594 ret = add_extent_mapping(em_tree, em);
4595 spin_unlock(&em_tree->lock);
4596 if (ret != -EEXIST) {
4597 free_extent_map(em);
4598 break;
4599 }
Yan Zheng66435582008-10-30 14:19:50 -04004600 btrfs_drop_extent_cache(reloc_inode, start, end, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04004601 }
Yan Zheng66435582008-10-30 14:19:50 -04004602 unlock_extent(&BTRFS_I(reloc_inode)->io_tree, start, end, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04004603
Yan Zheng66435582008-10-30 14:19:50 -04004604 return relocate_inode_pages(reloc_inode, start, extent_key->offset);
Zheng Yan1a40e232008-09-26 10:09:34 -04004605}
4606
4607struct btrfs_ref_path {
4608 u64 extent_start;
4609 u64 nodes[BTRFS_MAX_LEVEL];
4610 u64 root_objectid;
4611 u64 root_generation;
4612 u64 owner_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04004613 u32 num_refs;
4614 int lowest_level;
4615 int current_level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004616 int shared_level;
4617
4618 struct btrfs_key node_keys[BTRFS_MAX_LEVEL];
4619 u64 new_nodes[BTRFS_MAX_LEVEL];
Zheng Yan1a40e232008-09-26 10:09:34 -04004620};
4621
4622struct disk_extent {
Chris Masonc8b97812008-10-29 14:49:59 -04004623 u64 ram_bytes;
Zheng Yan1a40e232008-09-26 10:09:34 -04004624 u64 disk_bytenr;
4625 u64 disk_num_bytes;
4626 u64 offset;
4627 u64 num_bytes;
Chris Masonc8b97812008-10-29 14:49:59 -04004628 u8 compression;
4629 u8 encryption;
4630 u16 other_encoding;
Zheng Yan1a40e232008-09-26 10:09:34 -04004631};
4632
4633static int is_cowonly_root(u64 root_objectid)
4634{
4635 if (root_objectid == BTRFS_ROOT_TREE_OBJECTID ||
4636 root_objectid == BTRFS_EXTENT_TREE_OBJECTID ||
4637 root_objectid == BTRFS_CHUNK_TREE_OBJECTID ||
4638 root_objectid == BTRFS_DEV_TREE_OBJECTID ||
Yan Zheng0403e472008-12-10 20:32:51 -05004639 root_objectid == BTRFS_TREE_LOG_OBJECTID ||
4640 root_objectid == BTRFS_CSUM_TREE_OBJECTID)
Zheng Yan1a40e232008-09-26 10:09:34 -04004641 return 1;
4642 return 0;
4643}
4644
Chris Masond3977122009-01-05 21:25:51 -05004645static noinline int __next_ref_path(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04004646 struct btrfs_root *extent_root,
4647 struct btrfs_ref_path *ref_path,
4648 int first_time)
4649{
4650 struct extent_buffer *leaf;
4651 struct btrfs_path *path;
Chris Mason4313b392008-01-03 09:08:48 -05004652 struct btrfs_extent_ref *ref;
Chris Masonedbd8d42007-12-21 16:27:24 -05004653 struct btrfs_key key;
4654 struct btrfs_key found_key;
Zheng Yan1a40e232008-09-26 10:09:34 -04004655 u64 bytenr;
Chris Masonedbd8d42007-12-21 16:27:24 -05004656 u32 nritems;
Zheng Yan1a40e232008-09-26 10:09:34 -04004657 int level;
4658 int ret = 1;
Chris Masonedbd8d42007-12-21 16:27:24 -05004659
Zheng Yan1a40e232008-09-26 10:09:34 -04004660 path = btrfs_alloc_path();
4661 if (!path)
4662 return -ENOMEM;
4663
Zheng Yan1a40e232008-09-26 10:09:34 -04004664 if (first_time) {
4665 ref_path->lowest_level = -1;
4666 ref_path->current_level = -1;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004667 ref_path->shared_level = -1;
Zheng Yan1a40e232008-09-26 10:09:34 -04004668 goto walk_up;
Chris Masona061fc82008-05-07 11:43:44 -04004669 }
Zheng Yan1a40e232008-09-26 10:09:34 -04004670walk_down:
4671 level = ref_path->current_level - 1;
4672 while (level >= -1) {
4673 u64 parent;
4674 if (level < ref_path->lowest_level)
4675 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05004676
Chris Masond3977122009-01-05 21:25:51 -05004677 if (level >= 0)
Zheng Yan1a40e232008-09-26 10:09:34 -04004678 bytenr = ref_path->nodes[level];
Chris Masond3977122009-01-05 21:25:51 -05004679 else
Zheng Yan1a40e232008-09-26 10:09:34 -04004680 bytenr = ref_path->extent_start;
Zheng Yan1a40e232008-09-26 10:09:34 -04004681 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05004682
Zheng Yan1a40e232008-09-26 10:09:34 -04004683 parent = ref_path->nodes[level + 1];
4684 ref_path->nodes[level + 1] = 0;
4685 ref_path->current_level = level;
4686 BUG_ON(parent == 0);
4687
4688 key.objectid = bytenr;
4689 key.offset = parent + 1;
4690 key.type = BTRFS_EXTENT_REF_KEY;
4691
4692 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05004693 if (ret < 0)
4694 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04004695 BUG_ON(ret == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05004696
Chris Masonedbd8d42007-12-21 16:27:24 -05004697 leaf = path->nodes[0];
4698 nritems = btrfs_header_nritems(leaf);
Zheng Yan1a40e232008-09-26 10:09:34 -04004699 if (path->slots[0] >= nritems) {
Chris Masona061fc82008-05-07 11:43:44 -04004700 ret = btrfs_next_leaf(extent_root, path);
Chris Masona061fc82008-05-07 11:43:44 -04004701 if (ret < 0)
4702 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04004703 if (ret > 0)
4704 goto next;
Chris Masonbf4ef672008-05-08 13:26:18 -04004705 leaf = path->nodes[0];
Chris Masona061fc82008-05-07 11:43:44 -04004706 }
Chris Masonedbd8d42007-12-21 16:27:24 -05004707
4708 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Zheng Yan1a40e232008-09-26 10:09:34 -04004709 if (found_key.objectid == bytenr &&
Yan Zhengf82d02d2008-10-29 14:49:05 -04004710 found_key.type == BTRFS_EXTENT_REF_KEY) {
4711 if (level < ref_path->shared_level)
4712 ref_path->shared_level = level;
Zheng Yan1a40e232008-09-26 10:09:34 -04004713 goto found;
Yan Zhengf82d02d2008-10-29 14:49:05 -04004714 }
Zheng Yan1a40e232008-09-26 10:09:34 -04004715next:
4716 level--;
4717 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04004718 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04004719 }
4720 /* reached lowest level */
4721 ret = 1;
4722 goto out;
4723walk_up:
4724 level = ref_path->current_level;
4725 while (level < BTRFS_MAX_LEVEL - 1) {
4726 u64 ref_objectid;
Chris Masond3977122009-01-05 21:25:51 -05004727
4728 if (level >= 0)
Zheng Yan1a40e232008-09-26 10:09:34 -04004729 bytenr = ref_path->nodes[level];
Chris Masond3977122009-01-05 21:25:51 -05004730 else
Zheng Yan1a40e232008-09-26 10:09:34 -04004731 bytenr = ref_path->extent_start;
Chris Masond3977122009-01-05 21:25:51 -05004732
Zheng Yan1a40e232008-09-26 10:09:34 -04004733 BUG_ON(bytenr == 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05004734
Zheng Yan1a40e232008-09-26 10:09:34 -04004735 key.objectid = bytenr;
4736 key.offset = 0;
4737 key.type = BTRFS_EXTENT_REF_KEY;
Chris Masonedbd8d42007-12-21 16:27:24 -05004738
Zheng Yan1a40e232008-09-26 10:09:34 -04004739 ret = btrfs_search_slot(trans, extent_root, &key, path, 0, 0);
4740 if (ret < 0)
Chris Masonedbd8d42007-12-21 16:27:24 -05004741 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04004742
4743 leaf = path->nodes[0];
4744 nritems = btrfs_header_nritems(leaf);
4745 if (path->slots[0] >= nritems) {
4746 ret = btrfs_next_leaf(extent_root, path);
4747 if (ret < 0)
4748 goto out;
4749 if (ret > 0) {
4750 /* the extent was freed by someone */
4751 if (ref_path->lowest_level == level)
4752 goto out;
4753 btrfs_release_path(extent_root, path);
4754 goto walk_down;
4755 }
4756 leaf = path->nodes[0];
4757 }
4758
4759 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4760 if (found_key.objectid != bytenr ||
4761 found_key.type != BTRFS_EXTENT_REF_KEY) {
4762 /* the extent was freed by someone */
4763 if (ref_path->lowest_level == level) {
4764 ret = 1;
4765 goto out;
4766 }
4767 btrfs_release_path(extent_root, path);
4768 goto walk_down;
4769 }
4770found:
4771 ref = btrfs_item_ptr(leaf, path->slots[0],
4772 struct btrfs_extent_ref);
4773 ref_objectid = btrfs_ref_objectid(leaf, ref);
4774 if (ref_objectid < BTRFS_FIRST_FREE_OBJECTID) {
4775 if (first_time) {
4776 level = (int)ref_objectid;
4777 BUG_ON(level >= BTRFS_MAX_LEVEL);
4778 ref_path->lowest_level = level;
4779 ref_path->current_level = level;
4780 ref_path->nodes[level] = bytenr;
4781 } else {
4782 WARN_ON(ref_objectid != level);
4783 }
4784 } else {
4785 WARN_ON(level != -1);
4786 }
4787 first_time = 0;
4788
4789 if (ref_path->lowest_level == level) {
4790 ref_path->owner_objectid = ref_objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04004791 ref_path->num_refs = btrfs_ref_num_refs(leaf, ref);
4792 }
4793
4794 /*
4795 * the block is tree root or the block isn't in reference
4796 * counted tree.
4797 */
4798 if (found_key.objectid == found_key.offset ||
4799 is_cowonly_root(btrfs_ref_root(leaf, ref))) {
4800 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
4801 ref_path->root_generation =
4802 btrfs_ref_generation(leaf, ref);
4803 if (level < 0) {
4804 /* special reference from the tree log */
4805 ref_path->nodes[0] = found_key.offset;
4806 ref_path->current_level = 0;
4807 }
4808 ret = 0;
4809 goto out;
4810 }
4811
4812 level++;
4813 BUG_ON(ref_path->nodes[level] != 0);
4814 ref_path->nodes[level] = found_key.offset;
4815 ref_path->current_level = level;
4816
4817 /*
4818 * the reference was created in the running transaction,
4819 * no need to continue walking up.
4820 */
4821 if (btrfs_ref_generation(leaf, ref) == trans->transid) {
4822 ref_path->root_objectid = btrfs_ref_root(leaf, ref);
4823 ref_path->root_generation =
4824 btrfs_ref_generation(leaf, ref);
4825 ret = 0;
4826 goto out;
4827 }
4828
4829 btrfs_release_path(extent_root, path);
Yan Zhengd899e052008-10-30 14:25:28 -04004830 cond_resched();
Zheng Yan1a40e232008-09-26 10:09:34 -04004831 }
4832 /* reached max tree level, but no tree root found. */
4833 BUG();
4834out:
Zheng Yan1a40e232008-09-26 10:09:34 -04004835 btrfs_free_path(path);
4836 return ret;
4837}
4838
4839static int btrfs_first_ref_path(struct btrfs_trans_handle *trans,
4840 struct btrfs_root *extent_root,
4841 struct btrfs_ref_path *ref_path,
4842 u64 extent_start)
4843{
4844 memset(ref_path, 0, sizeof(*ref_path));
4845 ref_path->extent_start = extent_start;
4846
4847 return __next_ref_path(trans, extent_root, ref_path, 1);
4848}
4849
4850static int btrfs_next_ref_path(struct btrfs_trans_handle *trans,
4851 struct btrfs_root *extent_root,
4852 struct btrfs_ref_path *ref_path)
4853{
4854 return __next_ref_path(trans, extent_root, ref_path, 0);
4855}
4856
Chris Masond3977122009-01-05 21:25:51 -05004857static noinline int get_new_locations(struct inode *reloc_inode,
Zheng Yan1a40e232008-09-26 10:09:34 -04004858 struct btrfs_key *extent_key,
4859 u64 offset, int no_fragment,
4860 struct disk_extent **extents,
4861 int *nr_extents)
4862{
4863 struct btrfs_root *root = BTRFS_I(reloc_inode)->root;
4864 struct btrfs_path *path;
4865 struct btrfs_file_extent_item *fi;
4866 struct extent_buffer *leaf;
4867 struct disk_extent *exts = *extents;
4868 struct btrfs_key found_key;
4869 u64 cur_pos;
4870 u64 last_byte;
4871 u32 nritems;
4872 int nr = 0;
4873 int max = *nr_extents;
4874 int ret;
4875
4876 WARN_ON(!no_fragment && *extents);
4877 if (!exts) {
4878 max = 1;
4879 exts = kmalloc(sizeof(*exts) * max, GFP_NOFS);
4880 if (!exts)
4881 return -ENOMEM;
4882 }
4883
4884 path = btrfs_alloc_path();
4885 BUG_ON(!path);
4886
4887 cur_pos = extent_key->objectid - offset;
4888 last_byte = extent_key->objectid + extent_key->offset;
4889 ret = btrfs_lookup_file_extent(NULL, root, path, reloc_inode->i_ino,
4890 cur_pos, 0);
4891 if (ret < 0)
4892 goto out;
4893 if (ret > 0) {
4894 ret = -ENOENT;
4895 goto out;
4896 }
4897
4898 while (1) {
4899 leaf = path->nodes[0];
4900 nritems = btrfs_header_nritems(leaf);
4901 if (path->slots[0] >= nritems) {
4902 ret = btrfs_next_leaf(root, path);
4903 if (ret < 0)
4904 goto out;
4905 if (ret > 0)
4906 break;
4907 leaf = path->nodes[0];
4908 }
4909
4910 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4911 if (found_key.offset != cur_pos ||
4912 found_key.type != BTRFS_EXTENT_DATA_KEY ||
4913 found_key.objectid != reloc_inode->i_ino)
4914 break;
4915
4916 fi = btrfs_item_ptr(leaf, path->slots[0],
4917 struct btrfs_file_extent_item);
4918 if (btrfs_file_extent_type(leaf, fi) !=
4919 BTRFS_FILE_EXTENT_REG ||
4920 btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
4921 break;
4922
4923 if (nr == max) {
4924 struct disk_extent *old = exts;
4925 max *= 2;
4926 exts = kzalloc(sizeof(*exts) * max, GFP_NOFS);
4927 memcpy(exts, old, sizeof(*exts) * nr);
4928 if (old != *extents)
4929 kfree(old);
4930 }
4931
4932 exts[nr].disk_bytenr =
4933 btrfs_file_extent_disk_bytenr(leaf, fi);
4934 exts[nr].disk_num_bytes =
4935 btrfs_file_extent_disk_num_bytes(leaf, fi);
4936 exts[nr].offset = btrfs_file_extent_offset(leaf, fi);
4937 exts[nr].num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
Chris Masonc8b97812008-10-29 14:49:59 -04004938 exts[nr].ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
4939 exts[nr].compression = btrfs_file_extent_compression(leaf, fi);
4940 exts[nr].encryption = btrfs_file_extent_encryption(leaf, fi);
4941 exts[nr].other_encoding = btrfs_file_extent_other_encoding(leaf,
4942 fi);
Yan Zhengd899e052008-10-30 14:25:28 -04004943 BUG_ON(exts[nr].offset > 0);
4944 BUG_ON(exts[nr].compression || exts[nr].encryption);
4945 BUG_ON(exts[nr].num_bytes != exts[nr].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04004946
4947 cur_pos += exts[nr].num_bytes;
4948 nr++;
4949
4950 if (cur_pos + offset >= last_byte)
4951 break;
4952
4953 if (no_fragment) {
4954 ret = 1;
4955 goto out;
4956 }
4957 path->slots[0]++;
4958 }
4959
Yan Zheng1f80e4d2008-12-19 10:59:04 -05004960 BUG_ON(cur_pos + offset > last_byte);
Zheng Yan1a40e232008-09-26 10:09:34 -04004961 if (cur_pos + offset < last_byte) {
4962 ret = -ENOENT;
4963 goto out;
Chris Masonedbd8d42007-12-21 16:27:24 -05004964 }
4965 ret = 0;
4966out:
Zheng Yan1a40e232008-09-26 10:09:34 -04004967 btrfs_free_path(path);
4968 if (ret) {
4969 if (exts != *extents)
4970 kfree(exts);
4971 } else {
4972 *extents = exts;
4973 *nr_extents = nr;
4974 }
4975 return ret;
4976}
4977
Chris Masond3977122009-01-05 21:25:51 -05004978static noinline int replace_one_extent(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04004979 struct btrfs_root *root,
4980 struct btrfs_path *path,
4981 struct btrfs_key *extent_key,
4982 struct btrfs_key *leaf_key,
4983 struct btrfs_ref_path *ref_path,
4984 struct disk_extent *new_extents,
4985 int nr_extents)
4986{
4987 struct extent_buffer *leaf;
4988 struct btrfs_file_extent_item *fi;
4989 struct inode *inode = NULL;
4990 struct btrfs_key key;
4991 u64 lock_start = 0;
4992 u64 lock_end = 0;
4993 u64 num_bytes;
4994 u64 ext_offset;
Yan Zheng86288a12009-01-21 10:49:16 -05004995 u64 search_end = (u64)-1;
Zheng Yan1a40e232008-09-26 10:09:34 -04004996 u32 nritems;
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04004997 int nr_scaned = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04004998 int extent_locked = 0;
Yan Zhengd899e052008-10-30 14:25:28 -04004999 int extent_type;
Zheng Yan1a40e232008-09-26 10:09:34 -04005000 int ret;
5001
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04005002 memcpy(&key, leaf_key, sizeof(key));
Zheng Yan1a40e232008-09-26 10:09:34 -04005003 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04005004 if (key.objectid < ref_path->owner_objectid ||
5005 (key.objectid == ref_path->owner_objectid &&
5006 key.type < BTRFS_EXTENT_DATA_KEY)) {
5007 key.objectid = ref_path->owner_objectid;
5008 key.type = BTRFS_EXTENT_DATA_KEY;
5009 key.offset = 0;
5010 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005011 }
5012
5013 while (1) {
5014 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
5015 if (ret < 0)
5016 goto out;
5017
5018 leaf = path->nodes[0];
5019 nritems = btrfs_header_nritems(leaf);
5020next:
5021 if (extent_locked && ret > 0) {
5022 /*
5023 * the file extent item was modified by someone
5024 * before the extent got locked.
5025 */
Zheng Yan1a40e232008-09-26 10:09:34 -04005026 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
5027 lock_end, GFP_NOFS);
5028 extent_locked = 0;
5029 }
5030
5031 if (path->slots[0] >= nritems) {
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04005032 if (++nr_scaned > 2)
Zheng Yan1a40e232008-09-26 10:09:34 -04005033 break;
5034
5035 BUG_ON(extent_locked);
5036 ret = btrfs_next_leaf(root, path);
5037 if (ret < 0)
5038 goto out;
5039 if (ret > 0)
5040 break;
5041 leaf = path->nodes[0];
5042 nritems = btrfs_header_nritems(leaf);
5043 }
5044
5045 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
5046
5047 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS) {
5048 if ((key.objectid > ref_path->owner_objectid) ||
5049 (key.objectid == ref_path->owner_objectid &&
5050 key.type > BTRFS_EXTENT_DATA_KEY) ||
Yan Zheng86288a12009-01-21 10:49:16 -05005051 key.offset >= search_end)
Zheng Yan1a40e232008-09-26 10:09:34 -04005052 break;
5053 }
5054
5055 if (inode && key.objectid != inode->i_ino) {
5056 BUG_ON(extent_locked);
5057 btrfs_release_path(root, path);
5058 mutex_unlock(&inode->i_mutex);
5059 iput(inode);
5060 inode = NULL;
5061 continue;
5062 }
5063
5064 if (key.type != BTRFS_EXTENT_DATA_KEY) {
5065 path->slots[0]++;
5066 ret = 1;
5067 goto next;
5068 }
5069 fi = btrfs_item_ptr(leaf, path->slots[0],
5070 struct btrfs_file_extent_item);
Yan Zhengd899e052008-10-30 14:25:28 -04005071 extent_type = btrfs_file_extent_type(leaf, fi);
5072 if ((extent_type != BTRFS_FILE_EXTENT_REG &&
5073 extent_type != BTRFS_FILE_EXTENT_PREALLOC) ||
Zheng Yan1a40e232008-09-26 10:09:34 -04005074 (btrfs_file_extent_disk_bytenr(leaf, fi) !=
5075 extent_key->objectid)) {
5076 path->slots[0]++;
5077 ret = 1;
5078 goto next;
5079 }
5080
5081 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
5082 ext_offset = btrfs_file_extent_offset(leaf, fi);
5083
Yan Zheng86288a12009-01-21 10:49:16 -05005084 if (search_end == (u64)-1) {
5085 search_end = key.offset - ext_offset +
5086 btrfs_file_extent_ram_bytes(leaf, fi);
5087 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005088
5089 if (!extent_locked) {
5090 lock_start = key.offset;
5091 lock_end = lock_start + num_bytes - 1;
5092 } else {
Yan Zheng66435582008-10-30 14:19:50 -04005093 if (lock_start > key.offset ||
5094 lock_end + 1 < key.offset + num_bytes) {
5095 unlock_extent(&BTRFS_I(inode)->io_tree,
5096 lock_start, lock_end, GFP_NOFS);
5097 extent_locked = 0;
5098 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005099 }
5100
5101 if (!inode) {
5102 btrfs_release_path(root, path);
5103
5104 inode = btrfs_iget_locked(root->fs_info->sb,
5105 key.objectid, root);
5106 if (inode->i_state & I_NEW) {
5107 BTRFS_I(inode)->root = root;
5108 BTRFS_I(inode)->location.objectid =
5109 key.objectid;
5110 BTRFS_I(inode)->location.type =
5111 BTRFS_INODE_ITEM_KEY;
5112 BTRFS_I(inode)->location.offset = 0;
5113 btrfs_read_locked_inode(inode);
5114 unlock_new_inode(inode);
5115 }
5116 /*
5117 * some code call btrfs_commit_transaction while
5118 * holding the i_mutex, so we can't use mutex_lock
5119 * here.
5120 */
5121 if (is_bad_inode(inode) ||
5122 !mutex_trylock(&inode->i_mutex)) {
5123 iput(inode);
5124 inode = NULL;
5125 key.offset = (u64)-1;
5126 goto skip;
5127 }
5128 }
5129
5130 if (!extent_locked) {
5131 struct btrfs_ordered_extent *ordered;
5132
5133 btrfs_release_path(root, path);
5134
5135 lock_extent(&BTRFS_I(inode)->io_tree, lock_start,
5136 lock_end, GFP_NOFS);
5137 ordered = btrfs_lookup_first_ordered_extent(inode,
5138 lock_end);
5139 if (ordered &&
5140 ordered->file_offset <= lock_end &&
5141 ordered->file_offset + ordered->len > lock_start) {
5142 unlock_extent(&BTRFS_I(inode)->io_tree,
5143 lock_start, lock_end, GFP_NOFS);
5144 btrfs_start_ordered_extent(inode, ordered, 1);
5145 btrfs_put_ordered_extent(ordered);
5146 key.offset += num_bytes;
5147 goto skip;
5148 }
5149 if (ordered)
5150 btrfs_put_ordered_extent(ordered);
5151
Zheng Yan1a40e232008-09-26 10:09:34 -04005152 extent_locked = 1;
5153 continue;
5154 }
5155
5156 if (nr_extents == 1) {
5157 /* update extent pointer in place */
Zheng Yan1a40e232008-09-26 10:09:34 -04005158 btrfs_set_file_extent_disk_bytenr(leaf, fi,
5159 new_extents[0].disk_bytenr);
5160 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
5161 new_extents[0].disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04005162 btrfs_mark_buffer_dirty(leaf);
5163
5164 btrfs_drop_extent_cache(inode, key.offset,
5165 key.offset + num_bytes - 1, 0);
5166
5167 ret = btrfs_inc_extent_ref(trans, root,
5168 new_extents[0].disk_bytenr,
5169 new_extents[0].disk_num_bytes,
5170 leaf->start,
5171 root->root_key.objectid,
5172 trans->transid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04005173 key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04005174 BUG_ON(ret);
5175
5176 ret = btrfs_free_extent(trans, root,
5177 extent_key->objectid,
5178 extent_key->offset,
5179 leaf->start,
5180 btrfs_header_owner(leaf),
5181 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04005182 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04005183 BUG_ON(ret);
5184
5185 btrfs_release_path(root, path);
5186 key.offset += num_bytes;
5187 } else {
Yan Zhengd899e052008-10-30 14:25:28 -04005188 BUG_ON(1);
5189#if 0
Zheng Yan1a40e232008-09-26 10:09:34 -04005190 u64 alloc_hint;
5191 u64 extent_len;
5192 int i;
5193 /*
5194 * drop old extent pointer at first, then insert the
5195 * new pointers one bye one
5196 */
5197 btrfs_release_path(root, path);
5198 ret = btrfs_drop_extents(trans, root, inode, key.offset,
5199 key.offset + num_bytes,
5200 key.offset, &alloc_hint);
5201 BUG_ON(ret);
5202
5203 for (i = 0; i < nr_extents; i++) {
5204 if (ext_offset >= new_extents[i].num_bytes) {
5205 ext_offset -= new_extents[i].num_bytes;
5206 continue;
5207 }
5208 extent_len = min(new_extents[i].num_bytes -
5209 ext_offset, num_bytes);
5210
5211 ret = btrfs_insert_empty_item(trans, root,
5212 path, &key,
5213 sizeof(*fi));
5214 BUG_ON(ret);
5215
5216 leaf = path->nodes[0];
5217 fi = btrfs_item_ptr(leaf, path->slots[0],
5218 struct btrfs_file_extent_item);
5219 btrfs_set_file_extent_generation(leaf, fi,
5220 trans->transid);
5221 btrfs_set_file_extent_type(leaf, fi,
5222 BTRFS_FILE_EXTENT_REG);
5223 btrfs_set_file_extent_disk_bytenr(leaf, fi,
5224 new_extents[i].disk_bytenr);
5225 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
5226 new_extents[i].disk_num_bytes);
Chris Masonc8b97812008-10-29 14:49:59 -04005227 btrfs_set_file_extent_ram_bytes(leaf, fi,
5228 new_extents[i].ram_bytes);
5229
5230 btrfs_set_file_extent_compression(leaf, fi,
5231 new_extents[i].compression);
5232 btrfs_set_file_extent_encryption(leaf, fi,
5233 new_extents[i].encryption);
5234 btrfs_set_file_extent_other_encoding(leaf, fi,
5235 new_extents[i].other_encoding);
5236
Zheng Yan1a40e232008-09-26 10:09:34 -04005237 btrfs_set_file_extent_num_bytes(leaf, fi,
5238 extent_len);
5239 ext_offset += new_extents[i].offset;
5240 btrfs_set_file_extent_offset(leaf, fi,
5241 ext_offset);
5242 btrfs_mark_buffer_dirty(leaf);
5243
5244 btrfs_drop_extent_cache(inode, key.offset,
5245 key.offset + extent_len - 1, 0);
5246
5247 ret = btrfs_inc_extent_ref(trans, root,
5248 new_extents[i].disk_bytenr,
5249 new_extents[i].disk_num_bytes,
5250 leaf->start,
5251 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04005252 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04005253 BUG_ON(ret);
5254 btrfs_release_path(root, path);
5255
Yan Zhenga76a3cd2008-10-09 11:46:29 -04005256 inode_add_bytes(inode, extent_len);
Zheng Yan1a40e232008-09-26 10:09:34 -04005257
5258 ext_offset = 0;
5259 num_bytes -= extent_len;
5260 key.offset += extent_len;
5261
5262 if (num_bytes == 0)
5263 break;
5264 }
5265 BUG_ON(i >= nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04005266#endif
Zheng Yan1a40e232008-09-26 10:09:34 -04005267 }
5268
5269 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04005270 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
5271 lock_end, GFP_NOFS);
5272 extent_locked = 0;
5273 }
5274skip:
5275 if (ref_path->owner_objectid != BTRFS_MULTIPLE_OBJECTIDS &&
Yan Zheng86288a12009-01-21 10:49:16 -05005276 key.offset >= search_end)
Zheng Yan1a40e232008-09-26 10:09:34 -04005277 break;
5278
5279 cond_resched();
5280 }
5281 ret = 0;
5282out:
5283 btrfs_release_path(root, path);
5284 if (inode) {
5285 mutex_unlock(&inode->i_mutex);
5286 if (extent_locked) {
Zheng Yan1a40e232008-09-26 10:09:34 -04005287 unlock_extent(&BTRFS_I(inode)->io_tree, lock_start,
5288 lock_end, GFP_NOFS);
5289 }
5290 iput(inode);
5291 }
5292 return ret;
5293}
5294
Zheng Yan1a40e232008-09-26 10:09:34 -04005295int btrfs_reloc_tree_cache_ref(struct btrfs_trans_handle *trans,
5296 struct btrfs_root *root,
5297 struct extent_buffer *buf, u64 orig_start)
5298{
5299 int level;
5300 int ret;
5301
5302 BUG_ON(btrfs_header_generation(buf) != trans->transid);
5303 BUG_ON(root->root_key.objectid != BTRFS_TREE_RELOC_OBJECTID);
5304
5305 level = btrfs_header_level(buf);
5306 if (level == 0) {
5307 struct btrfs_leaf_ref *ref;
5308 struct btrfs_leaf_ref *orig_ref;
5309
5310 orig_ref = btrfs_lookup_leaf_ref(root, orig_start);
5311 if (!orig_ref)
5312 return -ENOENT;
5313
5314 ref = btrfs_alloc_leaf_ref(root, orig_ref->nritems);
5315 if (!ref) {
5316 btrfs_free_leaf_ref(root, orig_ref);
5317 return -ENOMEM;
5318 }
5319
5320 ref->nritems = orig_ref->nritems;
5321 memcpy(ref->extents, orig_ref->extents,
5322 sizeof(ref->extents[0]) * ref->nritems);
5323
5324 btrfs_free_leaf_ref(root, orig_ref);
5325
5326 ref->root_gen = trans->transid;
5327 ref->bytenr = buf->start;
5328 ref->owner = btrfs_header_owner(buf);
5329 ref->generation = btrfs_header_generation(buf);
Chris Masonbd56b302009-02-04 09:27:02 -05005330
Zheng Yan1a40e232008-09-26 10:09:34 -04005331 ret = btrfs_add_leaf_ref(root, ref, 0);
5332 WARN_ON(ret);
5333 btrfs_free_leaf_ref(root, ref);
5334 }
5335 return 0;
5336}
5337
Chris Masond3977122009-01-05 21:25:51 -05005338static noinline int invalidate_extent_cache(struct btrfs_root *root,
Zheng Yan1a40e232008-09-26 10:09:34 -04005339 struct extent_buffer *leaf,
5340 struct btrfs_block_group_cache *group,
5341 struct btrfs_root *target_root)
5342{
5343 struct btrfs_key key;
5344 struct inode *inode = NULL;
5345 struct btrfs_file_extent_item *fi;
5346 u64 num_bytes;
5347 u64 skip_objectid = 0;
5348 u32 nritems;
5349 u32 i;
5350
5351 nritems = btrfs_header_nritems(leaf);
5352 for (i = 0; i < nritems; i++) {
5353 btrfs_item_key_to_cpu(leaf, &key, i);
5354 if (key.objectid == skip_objectid ||
5355 key.type != BTRFS_EXTENT_DATA_KEY)
5356 continue;
5357 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
5358 if (btrfs_file_extent_type(leaf, fi) ==
5359 BTRFS_FILE_EXTENT_INLINE)
5360 continue;
5361 if (btrfs_file_extent_disk_bytenr(leaf, fi) == 0)
5362 continue;
5363 if (!inode || inode->i_ino != key.objectid) {
5364 iput(inode);
5365 inode = btrfs_ilookup(target_root->fs_info->sb,
5366 key.objectid, target_root, 1);
5367 }
5368 if (!inode) {
5369 skip_objectid = key.objectid;
5370 continue;
5371 }
5372 num_bytes = btrfs_file_extent_num_bytes(leaf, fi);
5373
5374 lock_extent(&BTRFS_I(inode)->io_tree, key.offset,
5375 key.offset + num_bytes - 1, GFP_NOFS);
Zheng Yan1a40e232008-09-26 10:09:34 -04005376 btrfs_drop_extent_cache(inode, key.offset,
5377 key.offset + num_bytes - 1, 1);
Zheng Yan1a40e232008-09-26 10:09:34 -04005378 unlock_extent(&BTRFS_I(inode)->io_tree, key.offset,
5379 key.offset + num_bytes - 1, GFP_NOFS);
5380 cond_resched();
5381 }
5382 iput(inode);
5383 return 0;
5384}
5385
Chris Masond3977122009-01-05 21:25:51 -05005386static noinline int replace_extents_in_leaf(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04005387 struct btrfs_root *root,
5388 struct extent_buffer *leaf,
5389 struct btrfs_block_group_cache *group,
5390 struct inode *reloc_inode)
5391{
5392 struct btrfs_key key;
5393 struct btrfs_key extent_key;
5394 struct btrfs_file_extent_item *fi;
5395 struct btrfs_leaf_ref *ref;
5396 struct disk_extent *new_extent;
5397 u64 bytenr;
5398 u64 num_bytes;
5399 u32 nritems;
5400 u32 i;
5401 int ext_index;
5402 int nr_extent;
5403 int ret;
5404
5405 new_extent = kmalloc(sizeof(*new_extent), GFP_NOFS);
5406 BUG_ON(!new_extent);
5407
5408 ref = btrfs_lookup_leaf_ref(root, leaf->start);
5409 BUG_ON(!ref);
5410
5411 ext_index = -1;
5412 nritems = btrfs_header_nritems(leaf);
5413 for (i = 0; i < nritems; i++) {
5414 btrfs_item_key_to_cpu(leaf, &key, i);
5415 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
5416 continue;
5417 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
5418 if (btrfs_file_extent_type(leaf, fi) ==
5419 BTRFS_FILE_EXTENT_INLINE)
5420 continue;
5421 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
5422 num_bytes = btrfs_file_extent_disk_num_bytes(leaf, fi);
5423 if (bytenr == 0)
5424 continue;
5425
5426 ext_index++;
5427 if (bytenr >= group->key.objectid + group->key.offset ||
5428 bytenr + num_bytes <= group->key.objectid)
5429 continue;
5430
5431 extent_key.objectid = bytenr;
5432 extent_key.offset = num_bytes;
5433 extent_key.type = BTRFS_EXTENT_ITEM_KEY;
5434 nr_extent = 1;
5435 ret = get_new_locations(reloc_inode, &extent_key,
5436 group->key.objectid, 1,
5437 &new_extent, &nr_extent);
5438 if (ret > 0)
5439 continue;
5440 BUG_ON(ret < 0);
5441
5442 BUG_ON(ref->extents[ext_index].bytenr != bytenr);
5443 BUG_ON(ref->extents[ext_index].num_bytes != num_bytes);
5444 ref->extents[ext_index].bytenr = new_extent->disk_bytenr;
5445 ref->extents[ext_index].num_bytes = new_extent->disk_num_bytes;
5446
Zheng Yan1a40e232008-09-26 10:09:34 -04005447 btrfs_set_file_extent_disk_bytenr(leaf, fi,
5448 new_extent->disk_bytenr);
5449 btrfs_set_file_extent_disk_num_bytes(leaf, fi,
5450 new_extent->disk_num_bytes);
Zheng Yan1a40e232008-09-26 10:09:34 -04005451 btrfs_mark_buffer_dirty(leaf);
5452
5453 ret = btrfs_inc_extent_ref(trans, root,
5454 new_extent->disk_bytenr,
5455 new_extent->disk_num_bytes,
5456 leaf->start,
5457 root->root_key.objectid,
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04005458 trans->transid, key.objectid);
Zheng Yan1a40e232008-09-26 10:09:34 -04005459 BUG_ON(ret);
5460 ret = btrfs_free_extent(trans, root,
5461 bytenr, num_bytes, leaf->start,
5462 btrfs_header_owner(leaf),
5463 btrfs_header_generation(leaf),
Yan Zheng3bb1a1b2008-10-09 11:46:24 -04005464 key.objectid, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04005465 BUG_ON(ret);
5466 cond_resched();
5467 }
5468 kfree(new_extent);
5469 BUG_ON(ext_index + 1 != ref->nritems);
5470 btrfs_free_leaf_ref(root, ref);
5471 return 0;
5472}
5473
Yan Zhengf82d02d2008-10-29 14:49:05 -04005474int btrfs_free_reloc_root(struct btrfs_trans_handle *trans,
5475 struct btrfs_root *root)
Zheng Yan1a40e232008-09-26 10:09:34 -04005476{
5477 struct btrfs_root *reloc_root;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005478 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04005479
5480 if (root->reloc_root) {
5481 reloc_root = root->reloc_root;
5482 root->reloc_root = NULL;
5483 list_add(&reloc_root->dead_list,
5484 &root->fs_info->dead_reloc_roots);
Yan Zhengf82d02d2008-10-29 14:49:05 -04005485
5486 btrfs_set_root_bytenr(&reloc_root->root_item,
5487 reloc_root->node->start);
5488 btrfs_set_root_level(&root->root_item,
5489 btrfs_header_level(reloc_root->node));
5490 memset(&reloc_root->root_item.drop_progress, 0,
5491 sizeof(struct btrfs_disk_key));
5492 reloc_root->root_item.drop_level = 0;
5493
5494 ret = btrfs_update_root(trans, root->fs_info->tree_root,
5495 &reloc_root->root_key,
5496 &reloc_root->root_item);
5497 BUG_ON(ret);
Zheng Yan1a40e232008-09-26 10:09:34 -04005498 }
5499 return 0;
5500}
5501
5502int btrfs_drop_dead_reloc_roots(struct btrfs_root *root)
5503{
5504 struct btrfs_trans_handle *trans;
5505 struct btrfs_root *reloc_root;
5506 struct btrfs_root *prev_root = NULL;
5507 struct list_head dead_roots;
5508 int ret;
5509 unsigned long nr;
5510
5511 INIT_LIST_HEAD(&dead_roots);
5512 list_splice_init(&root->fs_info->dead_reloc_roots, &dead_roots);
5513
5514 while (!list_empty(&dead_roots)) {
5515 reloc_root = list_entry(dead_roots.prev,
5516 struct btrfs_root, dead_list);
5517 list_del_init(&reloc_root->dead_list);
5518
5519 BUG_ON(reloc_root->commit_root != NULL);
5520 while (1) {
5521 trans = btrfs_join_transaction(root, 1);
5522 BUG_ON(!trans);
5523
5524 mutex_lock(&root->fs_info->drop_mutex);
5525 ret = btrfs_drop_snapshot(trans, reloc_root);
5526 if (ret != -EAGAIN)
5527 break;
5528 mutex_unlock(&root->fs_info->drop_mutex);
5529
5530 nr = trans->blocks_used;
5531 ret = btrfs_end_transaction(trans, root);
5532 BUG_ON(ret);
5533 btrfs_btree_balance_dirty(root, nr);
5534 }
5535
5536 free_extent_buffer(reloc_root->node);
5537
5538 ret = btrfs_del_root(trans, root->fs_info->tree_root,
5539 &reloc_root->root_key);
5540 BUG_ON(ret);
5541 mutex_unlock(&root->fs_info->drop_mutex);
5542
5543 nr = trans->blocks_used;
5544 ret = btrfs_end_transaction(trans, root);
5545 BUG_ON(ret);
5546 btrfs_btree_balance_dirty(root, nr);
5547
5548 kfree(prev_root);
5549 prev_root = reloc_root;
5550 }
5551 if (prev_root) {
5552 btrfs_remove_leaf_refs(prev_root, (u64)-1, 0);
5553 kfree(prev_root);
5554 }
5555 return 0;
5556}
5557
5558int btrfs_add_dead_reloc_root(struct btrfs_root *root)
5559{
5560 list_add(&root->dead_list, &root->fs_info->dead_reloc_roots);
5561 return 0;
5562}
5563
5564int btrfs_cleanup_reloc_trees(struct btrfs_root *root)
5565{
5566 struct btrfs_root *reloc_root;
5567 struct btrfs_trans_handle *trans;
5568 struct btrfs_key location;
5569 int found;
5570 int ret;
5571
5572 mutex_lock(&root->fs_info->tree_reloc_mutex);
5573 ret = btrfs_find_dead_roots(root, BTRFS_TREE_RELOC_OBJECTID, NULL);
5574 BUG_ON(ret);
5575 found = !list_empty(&root->fs_info->dead_reloc_roots);
5576 mutex_unlock(&root->fs_info->tree_reloc_mutex);
5577
5578 if (found) {
5579 trans = btrfs_start_transaction(root, 1);
5580 BUG_ON(!trans);
5581 ret = btrfs_commit_transaction(trans, root);
5582 BUG_ON(ret);
5583 }
5584
5585 location.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
5586 location.offset = (u64)-1;
5587 location.type = BTRFS_ROOT_ITEM_KEY;
5588
5589 reloc_root = btrfs_read_fs_root_no_name(root->fs_info, &location);
5590 BUG_ON(!reloc_root);
5591 btrfs_orphan_cleanup(reloc_root);
5592 return 0;
5593}
5594
Chris Masond3977122009-01-05 21:25:51 -05005595static noinline int init_reloc_tree(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04005596 struct btrfs_root *root)
5597{
5598 struct btrfs_root *reloc_root;
5599 struct extent_buffer *eb;
5600 struct btrfs_root_item *root_item;
5601 struct btrfs_key root_key;
5602 int ret;
5603
5604 BUG_ON(!root->ref_cows);
5605 if (root->reloc_root)
5606 return 0;
5607
5608 root_item = kmalloc(sizeof(*root_item), GFP_NOFS);
5609 BUG_ON(!root_item);
5610
5611 ret = btrfs_copy_root(trans, root, root->commit_root,
5612 &eb, BTRFS_TREE_RELOC_OBJECTID);
5613 BUG_ON(ret);
5614
5615 root_key.objectid = BTRFS_TREE_RELOC_OBJECTID;
5616 root_key.offset = root->root_key.objectid;
5617 root_key.type = BTRFS_ROOT_ITEM_KEY;
5618
5619 memcpy(root_item, &root->root_item, sizeof(root_item));
5620 btrfs_set_root_refs(root_item, 0);
5621 btrfs_set_root_bytenr(root_item, eb->start);
5622 btrfs_set_root_level(root_item, btrfs_header_level(eb));
Yan Zheng84234f32008-10-29 14:49:05 -04005623 btrfs_set_root_generation(root_item, trans->transid);
Zheng Yan1a40e232008-09-26 10:09:34 -04005624
5625 btrfs_tree_unlock(eb);
5626 free_extent_buffer(eb);
5627
5628 ret = btrfs_insert_root(trans, root->fs_info->tree_root,
5629 &root_key, root_item);
5630 BUG_ON(ret);
5631 kfree(root_item);
5632
5633 reloc_root = btrfs_read_fs_root_no_radix(root->fs_info->tree_root,
5634 &root_key);
5635 BUG_ON(!reloc_root);
5636 reloc_root->last_trans = trans->transid;
5637 reloc_root->commit_root = NULL;
5638 reloc_root->ref_tree = &root->fs_info->reloc_ref_tree;
5639
5640 root->reloc_root = reloc_root;
5641 return 0;
5642}
5643
5644/*
5645 * Core function of space balance.
5646 *
5647 * The idea is using reloc trees to relocate tree blocks in reference
Yan Zhengf82d02d2008-10-29 14:49:05 -04005648 * counted roots. There is one reloc tree for each subvol, and all
5649 * reloc trees share same root key objectid. Reloc trees are snapshots
5650 * of the latest committed roots of subvols (root->commit_root).
5651 *
5652 * To relocate a tree block referenced by a subvol, there are two steps.
5653 * COW the block through subvol's reloc tree, then update block pointer
5654 * in the subvol to point to the new block. Since all reloc trees share
5655 * same root key objectid, doing special handing for tree blocks owned
5656 * by them is easy. Once a tree block has been COWed in one reloc tree,
5657 * we can use the resulting new block directly when the same block is
5658 * required to COW again through other reloc trees. By this way, relocated
5659 * tree blocks are shared between reloc trees, so they are also shared
5660 * between subvols.
Zheng Yan1a40e232008-09-26 10:09:34 -04005661 */
Chris Masond3977122009-01-05 21:25:51 -05005662static noinline int relocate_one_path(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04005663 struct btrfs_root *root,
5664 struct btrfs_path *path,
5665 struct btrfs_key *first_key,
5666 struct btrfs_ref_path *ref_path,
5667 struct btrfs_block_group_cache *group,
5668 struct inode *reloc_inode)
5669{
5670 struct btrfs_root *reloc_root;
5671 struct extent_buffer *eb = NULL;
5672 struct btrfs_key *keys;
5673 u64 *nodes;
5674 int level;
Yan Zhengf82d02d2008-10-29 14:49:05 -04005675 int shared_level;
Zheng Yan1a40e232008-09-26 10:09:34 -04005676 int lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04005677 int ret;
5678
5679 if (ref_path->owner_objectid < BTRFS_FIRST_FREE_OBJECTID)
5680 lowest_level = ref_path->owner_objectid;
5681
Yan Zhengf82d02d2008-10-29 14:49:05 -04005682 if (!root->ref_cows) {
Zheng Yan1a40e232008-09-26 10:09:34 -04005683 path->lowest_level = lowest_level;
5684 ret = btrfs_search_slot(trans, root, first_key, path, 0, 1);
5685 BUG_ON(ret < 0);
5686 path->lowest_level = 0;
5687 btrfs_release_path(root, path);
5688 return 0;
5689 }
5690
Zheng Yan1a40e232008-09-26 10:09:34 -04005691 mutex_lock(&root->fs_info->tree_reloc_mutex);
5692 ret = init_reloc_tree(trans, root);
5693 BUG_ON(ret);
5694 reloc_root = root->reloc_root;
5695
Yan Zhengf82d02d2008-10-29 14:49:05 -04005696 shared_level = ref_path->shared_level;
5697 ref_path->shared_level = BTRFS_MAX_LEVEL - 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04005698
Yan Zhengf82d02d2008-10-29 14:49:05 -04005699 keys = ref_path->node_keys;
5700 nodes = ref_path->new_nodes;
5701 memset(&keys[shared_level + 1], 0,
5702 sizeof(*keys) * (BTRFS_MAX_LEVEL - shared_level - 1));
5703 memset(&nodes[shared_level + 1], 0,
5704 sizeof(*nodes) * (BTRFS_MAX_LEVEL - shared_level - 1));
Zheng Yan1a40e232008-09-26 10:09:34 -04005705
Yan Zhengf82d02d2008-10-29 14:49:05 -04005706 if (nodes[lowest_level] == 0) {
5707 path->lowest_level = lowest_level;
5708 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
5709 0, 1);
5710 BUG_ON(ret);
5711 for (level = lowest_level; level < BTRFS_MAX_LEVEL; level++) {
5712 eb = path->nodes[level];
5713 if (!eb || eb == reloc_root->node)
5714 break;
5715 nodes[level] = eb->start;
5716 if (level == 0)
5717 btrfs_item_key_to_cpu(eb, &keys[level], 0);
5718 else
5719 btrfs_node_key_to_cpu(eb, &keys[level], 0);
5720 }
Yan Zheng2b820322008-11-17 21:11:30 -05005721 if (nodes[0] &&
5722 ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04005723 eb = path->nodes[0];
5724 ret = replace_extents_in_leaf(trans, reloc_root, eb,
5725 group, reloc_inode);
5726 BUG_ON(ret);
5727 }
5728 btrfs_release_path(reloc_root, path);
5729 } else {
Zheng Yan1a40e232008-09-26 10:09:34 -04005730 ret = btrfs_merge_path(trans, reloc_root, keys, nodes,
Yan Zhengf82d02d2008-10-29 14:49:05 -04005731 lowest_level);
Zheng Yan1a40e232008-09-26 10:09:34 -04005732 BUG_ON(ret);
5733 }
5734
Zheng Yan1a40e232008-09-26 10:09:34 -04005735 /*
5736 * replace tree blocks in the fs tree with tree blocks in
5737 * the reloc tree.
5738 */
5739 ret = btrfs_merge_path(trans, root, keys, nodes, lowest_level);
5740 BUG_ON(ret < 0);
5741
5742 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Yan Zhengf82d02d2008-10-29 14:49:05 -04005743 ret = btrfs_search_slot(trans, reloc_root, first_key, path,
5744 0, 0);
5745 BUG_ON(ret);
5746 extent_buffer_get(path->nodes[0]);
5747 eb = path->nodes[0];
5748 btrfs_release_path(reloc_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005749 ret = invalidate_extent_cache(reloc_root, eb, group, root);
5750 BUG_ON(ret);
5751 free_extent_buffer(eb);
5752 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005753
Yan Zhengf82d02d2008-10-29 14:49:05 -04005754 mutex_unlock(&root->fs_info->tree_reloc_mutex);
Zheng Yan1a40e232008-09-26 10:09:34 -04005755 path->lowest_level = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04005756 return 0;
5757}
5758
Chris Masond3977122009-01-05 21:25:51 -05005759static noinline int relocate_tree_block(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04005760 struct btrfs_root *root,
5761 struct btrfs_path *path,
5762 struct btrfs_key *first_key,
5763 struct btrfs_ref_path *ref_path)
5764{
5765 int ret;
Zheng Yan1a40e232008-09-26 10:09:34 -04005766
5767 ret = relocate_one_path(trans, root, path, first_key,
5768 ref_path, NULL, NULL);
5769 BUG_ON(ret);
5770
5771 if (root == root->fs_info->extent_root)
5772 btrfs_extent_post_op(trans, root);
Zheng Yan1a40e232008-09-26 10:09:34 -04005773
5774 return 0;
5775}
5776
Chris Masond3977122009-01-05 21:25:51 -05005777static noinline int del_extent_zero(struct btrfs_trans_handle *trans,
Zheng Yan1a40e232008-09-26 10:09:34 -04005778 struct btrfs_root *extent_root,
5779 struct btrfs_path *path,
5780 struct btrfs_key *extent_key)
5781{
5782 int ret;
5783
Zheng Yan1a40e232008-09-26 10:09:34 -04005784 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
5785 if (ret)
5786 goto out;
5787 ret = btrfs_del_item(trans, extent_root, path);
5788out:
Chris Masonedbd8d42007-12-21 16:27:24 -05005789 btrfs_release_path(extent_root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005790 return ret;
5791}
5792
Chris Masond3977122009-01-05 21:25:51 -05005793static noinline struct btrfs_root *read_ref_root(struct btrfs_fs_info *fs_info,
Zheng Yan1a40e232008-09-26 10:09:34 -04005794 struct btrfs_ref_path *ref_path)
5795{
5796 struct btrfs_key root_key;
5797
5798 root_key.objectid = ref_path->root_objectid;
5799 root_key.type = BTRFS_ROOT_ITEM_KEY;
5800 if (is_cowonly_root(ref_path->root_objectid))
5801 root_key.offset = 0;
5802 else
5803 root_key.offset = (u64)-1;
5804
5805 return btrfs_read_fs_root_no_name(fs_info, &root_key);
5806}
5807
Chris Masond3977122009-01-05 21:25:51 -05005808static noinline int relocate_one_extent(struct btrfs_root *extent_root,
Zheng Yan1a40e232008-09-26 10:09:34 -04005809 struct btrfs_path *path,
5810 struct btrfs_key *extent_key,
5811 struct btrfs_block_group_cache *group,
5812 struct inode *reloc_inode, int pass)
5813{
5814 struct btrfs_trans_handle *trans;
5815 struct btrfs_root *found_root;
5816 struct btrfs_ref_path *ref_path = NULL;
5817 struct disk_extent *new_extents = NULL;
5818 int nr_extents = 0;
5819 int loops;
5820 int ret;
5821 int level;
5822 struct btrfs_key first_key;
5823 u64 prev_block = 0;
5824
Zheng Yan1a40e232008-09-26 10:09:34 -04005825
5826 trans = btrfs_start_transaction(extent_root, 1);
5827 BUG_ON(!trans);
5828
5829 if (extent_key->objectid == 0) {
5830 ret = del_extent_zero(trans, extent_root, path, extent_key);
5831 goto out;
5832 }
5833
5834 ref_path = kmalloc(sizeof(*ref_path), GFP_NOFS);
5835 if (!ref_path) {
Chris Masond3977122009-01-05 21:25:51 -05005836 ret = -ENOMEM;
5837 goto out;
Zheng Yan1a40e232008-09-26 10:09:34 -04005838 }
5839
5840 for (loops = 0; ; loops++) {
5841 if (loops == 0) {
5842 ret = btrfs_first_ref_path(trans, extent_root, ref_path,
5843 extent_key->objectid);
5844 } else {
5845 ret = btrfs_next_ref_path(trans, extent_root, ref_path);
5846 }
5847 if (ret < 0)
5848 goto out;
5849 if (ret > 0)
5850 break;
5851
5852 if (ref_path->root_objectid == BTRFS_TREE_LOG_OBJECTID ||
5853 ref_path->root_objectid == BTRFS_TREE_RELOC_OBJECTID)
5854 continue;
5855
5856 found_root = read_ref_root(extent_root->fs_info, ref_path);
5857 BUG_ON(!found_root);
5858 /*
5859 * for reference counted tree, only process reference paths
5860 * rooted at the latest committed root.
5861 */
5862 if (found_root->ref_cows &&
5863 ref_path->root_generation != found_root->root_key.offset)
5864 continue;
5865
5866 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
5867 if (pass == 0) {
5868 /*
5869 * copy data extents to new locations
5870 */
5871 u64 group_start = group->key.objectid;
5872 ret = relocate_data_extent(reloc_inode,
5873 extent_key,
5874 group_start);
5875 if (ret < 0)
5876 goto out;
5877 break;
5878 }
5879 level = 0;
5880 } else {
5881 level = ref_path->owner_objectid;
5882 }
5883
5884 if (prev_block != ref_path->nodes[level]) {
5885 struct extent_buffer *eb;
5886 u64 block_start = ref_path->nodes[level];
5887 u64 block_size = btrfs_level_size(found_root, level);
5888
5889 eb = read_tree_block(found_root, block_start,
5890 block_size, 0);
5891 btrfs_tree_lock(eb);
5892 BUG_ON(level != btrfs_header_level(eb));
5893
5894 if (level == 0)
5895 btrfs_item_key_to_cpu(eb, &first_key, 0);
5896 else
5897 btrfs_node_key_to_cpu(eb, &first_key, 0);
5898
5899 btrfs_tree_unlock(eb);
5900 free_extent_buffer(eb);
5901 prev_block = block_start;
5902 }
5903
Yan Zheng24562422009-02-12 14:14:53 -05005904 mutex_lock(&extent_root->fs_info->trans_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05005905 btrfs_record_root_in_trans(found_root);
Yan Zheng24562422009-02-12 14:14:53 -05005906 mutex_unlock(&extent_root->fs_info->trans_mutex);
Yan Zhenge4404d62008-12-12 10:03:26 -05005907 if (ref_path->owner_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
5908 /*
5909 * try to update data extent references while
5910 * keeping metadata shared between snapshots.
5911 */
5912 if (pass == 1) {
5913 ret = relocate_one_path(trans, found_root,
5914 path, &first_key, ref_path,
5915 group, reloc_inode);
5916 if (ret < 0)
5917 goto out;
5918 continue;
5919 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005920 /*
5921 * use fallback method to process the remaining
5922 * references.
5923 */
5924 if (!new_extents) {
5925 u64 group_start = group->key.objectid;
Yan Zhengd899e052008-10-30 14:25:28 -04005926 new_extents = kmalloc(sizeof(*new_extents),
5927 GFP_NOFS);
5928 nr_extents = 1;
Zheng Yan1a40e232008-09-26 10:09:34 -04005929 ret = get_new_locations(reloc_inode,
5930 extent_key,
Yan Zhengd899e052008-10-30 14:25:28 -04005931 group_start, 1,
Zheng Yan1a40e232008-09-26 10:09:34 -04005932 &new_extents,
5933 &nr_extents);
Yan Zhengd899e052008-10-30 14:25:28 -04005934 if (ret)
Zheng Yan1a40e232008-09-26 10:09:34 -04005935 goto out;
5936 }
Zheng Yan1a40e232008-09-26 10:09:34 -04005937 ret = replace_one_extent(trans, found_root,
5938 path, extent_key,
5939 &first_key, ref_path,
5940 new_extents, nr_extents);
Yan Zhenge4404d62008-12-12 10:03:26 -05005941 } else {
Zheng Yan1a40e232008-09-26 10:09:34 -04005942 ret = relocate_tree_block(trans, found_root, path,
5943 &first_key, ref_path);
Zheng Yan1a40e232008-09-26 10:09:34 -04005944 }
5945 if (ret < 0)
5946 goto out;
5947 }
5948 ret = 0;
5949out:
5950 btrfs_end_transaction(trans, extent_root);
5951 kfree(new_extents);
5952 kfree(ref_path);
Chris Masonedbd8d42007-12-21 16:27:24 -05005953 return ret;
5954}
5955
Chris Masonec44a352008-04-28 15:29:52 -04005956static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
5957{
5958 u64 num_devices;
5959 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
5960 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
5961
Yan Zheng2b820322008-11-17 21:11:30 -05005962 num_devices = root->fs_info->fs_devices->rw_devices;
Chris Masonec44a352008-04-28 15:29:52 -04005963 if (num_devices == 1) {
5964 stripped |= BTRFS_BLOCK_GROUP_DUP;
5965 stripped = flags & ~stripped;
5966
5967 /* turn raid0 into single device chunks */
5968 if (flags & BTRFS_BLOCK_GROUP_RAID0)
5969 return stripped;
5970
5971 /* turn mirroring into duplication */
5972 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
5973 BTRFS_BLOCK_GROUP_RAID10))
5974 return stripped | BTRFS_BLOCK_GROUP_DUP;
5975 return flags;
5976 } else {
5977 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04005978 if (flags & stripped)
5979 return flags;
5980
5981 stripped |= BTRFS_BLOCK_GROUP_DUP;
5982 stripped = flags & ~stripped;
5983
5984 /* switch duplicated blocks with raid1 */
5985 if (flags & BTRFS_BLOCK_GROUP_DUP)
5986 return stripped | BTRFS_BLOCK_GROUP_RAID1;
5987
5988 /* turn single device chunks into raid0 */
5989 return stripped | BTRFS_BLOCK_GROUP_RAID0;
5990 }
5991 return flags;
5992}
5993
Christoph Hellwigb2950862008-12-02 09:54:17 -05005994static int __alloc_chunk_for_shrink(struct btrfs_root *root,
Chris Mason0ef3e662008-05-24 14:04:53 -04005995 struct btrfs_block_group_cache *shrink_block_group,
5996 int force)
5997{
5998 struct btrfs_trans_handle *trans;
5999 u64 new_alloc_flags;
6000 u64 calc;
6001
Chris Masonc286ac42008-07-22 23:06:41 -04006002 spin_lock(&shrink_block_group->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04006003 if (btrfs_block_group_used(&shrink_block_group->item) > 0) {
Chris Masonc286ac42008-07-22 23:06:41 -04006004 spin_unlock(&shrink_block_group->lock);
Chris Masonc286ac42008-07-22 23:06:41 -04006005
Chris Mason0ef3e662008-05-24 14:04:53 -04006006 trans = btrfs_start_transaction(root, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04006007 spin_lock(&shrink_block_group->lock);
Chris Mason7d9eb122008-07-08 14:19:17 -04006008
Chris Mason0ef3e662008-05-24 14:04:53 -04006009 new_alloc_flags = update_block_group_flags(root,
6010 shrink_block_group->flags);
6011 if (new_alloc_flags != shrink_block_group->flags) {
6012 calc =
6013 btrfs_block_group_used(&shrink_block_group->item);
6014 } else {
6015 calc = shrink_block_group->key.offset;
6016 }
Chris Masonc286ac42008-07-22 23:06:41 -04006017 spin_unlock(&shrink_block_group->lock);
6018
Chris Mason0ef3e662008-05-24 14:04:53 -04006019 do_chunk_alloc(trans, root->fs_info->extent_root,
6020 calc + 2 * 1024 * 1024, new_alloc_flags, force);
Chris Mason7d9eb122008-07-08 14:19:17 -04006021
Chris Mason0ef3e662008-05-24 14:04:53 -04006022 btrfs_end_transaction(trans, root);
Chris Masonc286ac42008-07-22 23:06:41 -04006023 } else
6024 spin_unlock(&shrink_block_group->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04006025 return 0;
6026}
6027
Zheng Yan1a40e232008-09-26 10:09:34 -04006028static int __insert_orphan_inode(struct btrfs_trans_handle *trans,
6029 struct btrfs_root *root,
6030 u64 objectid, u64 size)
6031{
6032 struct btrfs_path *path;
6033 struct btrfs_inode_item *item;
6034 struct extent_buffer *leaf;
6035 int ret;
6036
6037 path = btrfs_alloc_path();
6038 if (!path)
6039 return -ENOMEM;
6040
6041 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
6042 if (ret)
6043 goto out;
6044
6045 leaf = path->nodes[0];
6046 item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_inode_item);
6047 memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
6048 btrfs_set_inode_generation(leaf, item, 1);
6049 btrfs_set_inode_size(leaf, item, size);
6050 btrfs_set_inode_mode(leaf, item, S_IFREG | 0600);
Yan Zheng0403e472008-12-10 20:32:51 -05006051 btrfs_set_inode_flags(leaf, item, BTRFS_INODE_NOCOMPRESS);
Zheng Yan1a40e232008-09-26 10:09:34 -04006052 btrfs_mark_buffer_dirty(leaf);
6053 btrfs_release_path(root, path);
6054out:
6055 btrfs_free_path(path);
6056 return ret;
6057}
6058
Chris Masond3977122009-01-05 21:25:51 -05006059static noinline struct inode *create_reloc_inode(struct btrfs_fs_info *fs_info,
Zheng Yan1a40e232008-09-26 10:09:34 -04006060 struct btrfs_block_group_cache *group)
6061{
6062 struct inode *inode = NULL;
6063 struct btrfs_trans_handle *trans;
6064 struct btrfs_root *root;
6065 struct btrfs_key root_key;
6066 u64 objectid = BTRFS_FIRST_FREE_OBJECTID;
6067 int err = 0;
6068
6069 root_key.objectid = BTRFS_DATA_RELOC_TREE_OBJECTID;
6070 root_key.type = BTRFS_ROOT_ITEM_KEY;
6071 root_key.offset = (u64)-1;
6072 root = btrfs_read_fs_root_no_name(fs_info, &root_key);
6073 if (IS_ERR(root))
6074 return ERR_CAST(root);
6075
6076 trans = btrfs_start_transaction(root, 1);
6077 BUG_ON(!trans);
6078
6079 err = btrfs_find_free_objectid(trans, root, objectid, &objectid);
6080 if (err)
6081 goto out;
6082
6083 err = __insert_orphan_inode(trans, root, objectid, group->key.offset);
6084 BUG_ON(err);
6085
6086 err = btrfs_insert_file_extent(trans, root, objectid, 0, 0, 0,
Chris Masonc8b97812008-10-29 14:49:59 -04006087 group->key.offset, 0, group->key.offset,
6088 0, 0, 0);
Zheng Yan1a40e232008-09-26 10:09:34 -04006089 BUG_ON(err);
6090
6091 inode = btrfs_iget_locked(root->fs_info->sb, objectid, root);
6092 if (inode->i_state & I_NEW) {
6093 BTRFS_I(inode)->root = root;
6094 BTRFS_I(inode)->location.objectid = objectid;
6095 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
6096 BTRFS_I(inode)->location.offset = 0;
6097 btrfs_read_locked_inode(inode);
6098 unlock_new_inode(inode);
6099 BUG_ON(is_bad_inode(inode));
6100 } else {
6101 BUG_ON(1);
6102 }
Yan Zheng17d217f2008-12-12 10:03:38 -05006103 BTRFS_I(inode)->index_cnt = group->key.objectid;
Zheng Yan1a40e232008-09-26 10:09:34 -04006104
6105 err = btrfs_orphan_add(trans, inode);
6106out:
6107 btrfs_end_transaction(trans, root);
6108 if (err) {
6109 if (inode)
6110 iput(inode);
6111 inode = ERR_PTR(err);
6112 }
6113 return inode;
6114}
6115
Yan Zheng17d217f2008-12-12 10:03:38 -05006116int btrfs_reloc_clone_csums(struct inode *inode, u64 file_pos, u64 len)
6117{
6118
6119 struct btrfs_ordered_sum *sums;
6120 struct btrfs_sector_sum *sector_sum;
6121 struct btrfs_ordered_extent *ordered;
6122 struct btrfs_root *root = BTRFS_I(inode)->root;
6123 struct list_head list;
6124 size_t offset;
6125 int ret;
6126 u64 disk_bytenr;
6127
6128 INIT_LIST_HEAD(&list);
6129
6130 ordered = btrfs_lookup_ordered_extent(inode, file_pos);
6131 BUG_ON(ordered->file_offset != file_pos || ordered->len != len);
6132
6133 disk_bytenr = file_pos + BTRFS_I(inode)->index_cnt;
Yan Zheng07d400a2009-01-06 11:42:00 -05006134 ret = btrfs_lookup_csums_range(root->fs_info->csum_root, disk_bytenr,
Yan Zheng17d217f2008-12-12 10:03:38 -05006135 disk_bytenr + len - 1, &list);
6136
6137 while (!list_empty(&list)) {
6138 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
6139 list_del_init(&sums->list);
6140
6141 sector_sum = sums->sums;
6142 sums->bytenr = ordered->start;
6143
6144 offset = 0;
6145 while (offset < sums->len) {
6146 sector_sum->bytenr += ordered->start - disk_bytenr;
6147 sector_sum++;
6148 offset += root->sectorsize;
6149 }
6150
6151 btrfs_add_ordered_sum(inode, ordered, sums);
6152 }
6153 btrfs_put_ordered_extent(ordered);
6154 return 0;
6155}
6156
Zheng Yan1a40e232008-09-26 10:09:34 -04006157int btrfs_relocate_block_group(struct btrfs_root *root, u64 group_start)
Chris Masonedbd8d42007-12-21 16:27:24 -05006158{
6159 struct btrfs_trans_handle *trans;
Chris Masonedbd8d42007-12-21 16:27:24 -05006160 struct btrfs_path *path;
Zheng Yan1a40e232008-09-26 10:09:34 -04006161 struct btrfs_fs_info *info = root->fs_info;
6162 struct extent_buffer *leaf;
6163 struct inode *reloc_inode;
6164 struct btrfs_block_group_cache *block_group;
6165 struct btrfs_key key;
Yan Zhengd899e052008-10-30 14:25:28 -04006166 u64 skipped;
Chris Masonedbd8d42007-12-21 16:27:24 -05006167 u64 cur_byte;
6168 u64 total_found;
Chris Masonedbd8d42007-12-21 16:27:24 -05006169 u32 nritems;
6170 int ret;
Chris Masona061fc82008-05-07 11:43:44 -04006171 int progress;
Zheng Yan1a40e232008-09-26 10:09:34 -04006172 int pass = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05006173
Chris Masonedbd8d42007-12-21 16:27:24 -05006174 root = root->fs_info->extent_root;
Zheng Yan1a40e232008-09-26 10:09:34 -04006175
6176 block_group = btrfs_lookup_block_group(info, group_start);
6177 BUG_ON(!block_group);
Chris Masonedbd8d42007-12-21 16:27:24 -05006178
Chris Masond3977122009-01-05 21:25:51 -05006179 printk(KERN_INFO "btrfs relocating block group %llu flags %llu\n",
Zheng Yan1a40e232008-09-26 10:09:34 -04006180 (unsigned long long)block_group->key.objectid,
6181 (unsigned long long)block_group->flags);
Chris Mason323da792008-05-09 11:46:48 -04006182
Zheng Yan1a40e232008-09-26 10:09:34 -04006183 path = btrfs_alloc_path();
6184 BUG_ON(!path);
Chris Mason323da792008-05-09 11:46:48 -04006185
Zheng Yan1a40e232008-09-26 10:09:34 -04006186 reloc_inode = create_reloc_inode(info, block_group);
6187 BUG_ON(IS_ERR(reloc_inode));
6188
Zheng Yan1a40e232008-09-26 10:09:34 -04006189 __alloc_chunk_for_shrink(root, block_group, 1);
Yan Zhengc146afa2008-11-12 14:34:12 -05006190 set_block_group_readonly(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04006191
Zheng Yan1a40e232008-09-26 10:09:34 -04006192 btrfs_start_delalloc_inodes(info->tree_root);
6193 btrfs_wait_ordered_extents(info->tree_root, 0);
Chris Mason0ef3e662008-05-24 14:04:53 -04006194again:
Yan Zhengd899e052008-10-30 14:25:28 -04006195 skipped = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05006196 total_found = 0;
Chris Masona061fc82008-05-07 11:43:44 -04006197 progress = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04006198 key.objectid = block_group->key.objectid;
Chris Masonedbd8d42007-12-21 16:27:24 -05006199 key.offset = 0;
6200 key.type = 0;
Yan73e48b22008-01-03 14:14:39 -05006201 cur_byte = key.objectid;
Chris Mason4313b392008-01-03 09:08:48 -05006202
Zheng Yan1a40e232008-09-26 10:09:34 -04006203 trans = btrfs_start_transaction(info->tree_root, 1);
6204 btrfs_commit_transaction(trans, info->tree_root);
Chris Masonea8c2812008-08-04 23:17:27 -04006205
Zheng Yan1a40e232008-09-26 10:09:34 -04006206 mutex_lock(&root->fs_info->cleaner_mutex);
6207 btrfs_clean_old_snapshots(info->tree_root);
6208 btrfs_remove_leaf_refs(info->tree_root, (u64)-1, 1);
6209 mutex_unlock(&root->fs_info->cleaner_mutex);
Chris Masonea8c2812008-08-04 23:17:27 -04006210
Chris Masond3977122009-01-05 21:25:51 -05006211 while (1) {
Chris Masonedbd8d42007-12-21 16:27:24 -05006212 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
6213 if (ret < 0)
6214 goto out;
Chris Mason7d9eb122008-07-08 14:19:17 -04006215next:
Chris Masonedbd8d42007-12-21 16:27:24 -05006216 leaf = path->nodes[0];
Chris Masonedbd8d42007-12-21 16:27:24 -05006217 nritems = btrfs_header_nritems(leaf);
Yan73e48b22008-01-03 14:14:39 -05006218 if (path->slots[0] >= nritems) {
6219 ret = btrfs_next_leaf(root, path);
6220 if (ret < 0)
6221 goto out;
6222 if (ret == 1) {
6223 ret = 0;
6224 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05006225 }
Yan73e48b22008-01-03 14:14:39 -05006226 leaf = path->nodes[0];
6227 nritems = btrfs_header_nritems(leaf);
6228 }
6229
Zheng Yan1a40e232008-09-26 10:09:34 -04006230 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
Chris Mason725c8462008-01-04 16:47:16 -05006231
Zheng Yan1a40e232008-09-26 10:09:34 -04006232 if (key.objectid >= block_group->key.objectid +
6233 block_group->key.offset)
Chris Mason8f18cf12008-04-25 16:53:30 -04006234 break;
6235
Chris Mason725c8462008-01-04 16:47:16 -05006236 if (progress && need_resched()) {
Chris Mason725c8462008-01-04 16:47:16 -05006237 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04006238 cond_resched();
Chris Mason725c8462008-01-04 16:47:16 -05006239 progress = 0;
Zheng Yan1a40e232008-09-26 10:09:34 -04006240 continue;
Chris Mason725c8462008-01-04 16:47:16 -05006241 }
6242 progress = 1;
6243
Zheng Yan1a40e232008-09-26 10:09:34 -04006244 if (btrfs_key_type(&key) != BTRFS_EXTENT_ITEM_KEY ||
6245 key.objectid + key.offset <= cur_byte) {
Yan73e48b22008-01-03 14:14:39 -05006246 path->slots[0]++;
Chris Masonedbd8d42007-12-21 16:27:24 -05006247 goto next;
6248 }
Yan73e48b22008-01-03 14:14:39 -05006249
Chris Masonedbd8d42007-12-21 16:27:24 -05006250 total_found++;
Zheng Yan1a40e232008-09-26 10:09:34 -04006251 cur_byte = key.objectid + key.offset;
Chris Masonedbd8d42007-12-21 16:27:24 -05006252 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04006253
6254 __alloc_chunk_for_shrink(root, block_group, 0);
6255 ret = relocate_one_extent(root, path, &key, block_group,
6256 reloc_inode, pass);
6257 BUG_ON(ret < 0);
Yan Zhengd899e052008-10-30 14:25:28 -04006258 if (ret > 0)
6259 skipped++;
Zheng Yan1a40e232008-09-26 10:09:34 -04006260
6261 key.objectid = cur_byte;
6262 key.type = 0;
6263 key.offset = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05006264 }
6265
6266 btrfs_release_path(root, path);
Zheng Yan1a40e232008-09-26 10:09:34 -04006267
6268 if (pass == 0) {
6269 btrfs_wait_ordered_range(reloc_inode, 0, (u64)-1);
6270 invalidate_mapping_pages(reloc_inode->i_mapping, 0, -1);
Zheng Yan1a40e232008-09-26 10:09:34 -04006271 }
Chris Masonedbd8d42007-12-21 16:27:24 -05006272
6273 if (total_found > 0) {
Chris Masond3977122009-01-05 21:25:51 -05006274 printk(KERN_INFO "btrfs found %llu extents in pass %d\n",
Zheng Yan1a40e232008-09-26 10:09:34 -04006275 (unsigned long long)total_found, pass);
6276 pass++;
Yan Zhengd899e052008-10-30 14:25:28 -04006277 if (total_found == skipped && pass > 2) {
6278 iput(reloc_inode);
6279 reloc_inode = create_reloc_inode(info, block_group);
6280 pass = 0;
6281 }
Chris Masonedbd8d42007-12-21 16:27:24 -05006282 goto again;
6283 }
6284
Zheng Yan1a40e232008-09-26 10:09:34 -04006285 /* delete reloc_inode */
6286 iput(reloc_inode);
Chris Mason7d9eb122008-07-08 14:19:17 -04006287
Zheng Yan1a40e232008-09-26 10:09:34 -04006288 /* unpin extents in this range */
6289 trans = btrfs_start_transaction(info->tree_root, 1);
6290 btrfs_commit_transaction(trans, info->tree_root);
Chris Mason0ef3e662008-05-24 14:04:53 -04006291
Zheng Yan1a40e232008-09-26 10:09:34 -04006292 spin_lock(&block_group->lock);
6293 WARN_ON(block_group->pinned > 0);
6294 WARN_ON(block_group->reserved > 0);
6295 WARN_ON(btrfs_block_group_used(&block_group->item) > 0);
6296 spin_unlock(&block_group->lock);
Yan Zhengd2fb3432008-12-11 16:30:39 -05006297 put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04006298 ret = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05006299out:
Zheng Yan1a40e232008-09-26 10:09:34 -04006300 btrfs_free_path(path);
Chris Masonedbd8d42007-12-21 16:27:24 -05006301 return ret;
6302}
6303
Christoph Hellwigb2950862008-12-02 09:54:17 -05006304static int find_first_block_group(struct btrfs_root *root,
6305 struct btrfs_path *path, struct btrfs_key *key)
Chris Mason0b86a832008-03-24 15:01:56 -04006306{
Chris Mason925baed2008-06-25 16:01:30 -04006307 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006308 struct btrfs_key found_key;
6309 struct extent_buffer *leaf;
6310 int slot;
6311
6312 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
6313 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04006314 goto out;
6315
Chris Masond3977122009-01-05 21:25:51 -05006316 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04006317 slot = path->slots[0];
6318 leaf = path->nodes[0];
6319 if (slot >= btrfs_header_nritems(leaf)) {
6320 ret = btrfs_next_leaf(root, path);
6321 if (ret == 0)
6322 continue;
6323 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04006324 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04006325 break;
6326 }
6327 btrfs_item_key_to_cpu(leaf, &found_key, slot);
6328
6329 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04006330 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
6331 ret = 0;
6332 goto out;
6333 }
Chris Mason0b86a832008-03-24 15:01:56 -04006334 path->slots[0]++;
6335 }
6336 ret = -ENOENT;
Chris Mason925baed2008-06-25 16:01:30 -04006337out:
Chris Mason0b86a832008-03-24 15:01:56 -04006338 return ret;
6339}
6340
Zheng Yan1a40e232008-09-26 10:09:34 -04006341int btrfs_free_block_groups(struct btrfs_fs_info *info)
6342{
6343 struct btrfs_block_group_cache *block_group;
Chris Mason4184ea72009-03-10 12:39:20 -04006344 struct btrfs_space_info *space_info;
Zheng Yan1a40e232008-09-26 10:09:34 -04006345 struct rb_node *n;
6346
Zheng Yan1a40e232008-09-26 10:09:34 -04006347 spin_lock(&info->block_group_cache_lock);
6348 while ((n = rb_last(&info->block_group_cache_tree)) != NULL) {
6349 block_group = rb_entry(n, struct btrfs_block_group_cache,
6350 cache_node);
Zheng Yan1a40e232008-09-26 10:09:34 -04006351 rb_erase(&block_group->cache_node,
6352 &info->block_group_cache_tree);
Yan Zhengd899e052008-10-30 14:25:28 -04006353 spin_unlock(&info->block_group_cache_lock);
6354
6355 btrfs_remove_free_space_cache(block_group);
Josef Bacik80eb2342008-10-29 14:49:05 -04006356 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04006357 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04006358 up_write(&block_group->space_info->groups_sem);
Yan Zhengd2fb3432008-12-11 16:30:39 -05006359
6360 WARN_ON(atomic_read(&block_group->count) != 1);
Zheng Yan1a40e232008-09-26 10:09:34 -04006361 kfree(block_group);
Yan Zhengd899e052008-10-30 14:25:28 -04006362
6363 spin_lock(&info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04006364 }
6365 spin_unlock(&info->block_group_cache_lock);
Chris Mason4184ea72009-03-10 12:39:20 -04006366
6367 /* now that all the block groups are freed, go through and
6368 * free all the space_info structs. This is only called during
6369 * the final stages of unmount, and so we know nobody is
6370 * using them. We call synchronize_rcu() once before we start,
6371 * just to be on the safe side.
6372 */
6373 synchronize_rcu();
6374
6375 while(!list_empty(&info->space_info)) {
6376 space_info = list_entry(info->space_info.next,
6377 struct btrfs_space_info,
6378 list);
6379
6380 list_del(&space_info->list);
6381 kfree(space_info);
6382 }
Zheng Yan1a40e232008-09-26 10:09:34 -04006383 return 0;
6384}
6385
Chris Mason9078a3e2007-04-26 16:46:15 -04006386int btrfs_read_block_groups(struct btrfs_root *root)
6387{
6388 struct btrfs_path *path;
6389 int ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04006390 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04006391 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04006392 struct btrfs_space_info *space_info;
Chris Mason9078a3e2007-04-26 16:46:15 -04006393 struct btrfs_key key;
6394 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04006395 struct extent_buffer *leaf;
Chris Mason96b51792007-10-15 16:15:19 -04006396
Chris Masonbe744172007-05-06 10:15:01 -04006397 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04006398 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04006399 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04006400 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04006401 path = btrfs_alloc_path();
6402 if (!path)
6403 return -ENOMEM;
6404
Chris Masond3977122009-01-05 21:25:51 -05006405 while (1) {
Chris Mason0b86a832008-03-24 15:01:56 -04006406 ret = find_first_block_group(root, path, &key);
6407 if (ret > 0) {
6408 ret = 0;
6409 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04006410 }
Chris Mason0b86a832008-03-24 15:01:56 -04006411 if (ret != 0)
6412 goto error;
6413
Chris Mason5f39d392007-10-15 16:14:19 -04006414 leaf = path->nodes[0];
6415 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04006416 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04006417 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04006418 ret = -ENOMEM;
Chris Mason9078a3e2007-04-26 16:46:15 -04006419 break;
6420 }
Chris Mason3e1ad542007-05-07 20:03:49 -04006421
Yan Zhengd2fb3432008-12-11 16:30:39 -05006422 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04006423 spin_lock_init(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04006424 mutex_init(&cache->alloc_mutex);
Josef Bacikea6a4782008-11-20 12:16:16 -05006425 mutex_init(&cache->cache_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04006426 INIT_LIST_HEAD(&cache->list);
Chris Mason5f39d392007-10-15 16:14:19 -04006427 read_extent_buffer(leaf, &cache->item,
6428 btrfs_item_ptr_offset(leaf, path->slots[0]),
6429 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04006430 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04006431
Chris Mason9078a3e2007-04-26 16:46:15 -04006432 key.objectid = found_key.objectid + found_key.offset;
6433 btrfs_release_path(root, path);
Chris Mason0b86a832008-03-24 15:01:56 -04006434 cache->flags = btrfs_block_group_flags(&cache->item);
Chris Mason96b51792007-10-15 16:15:19 -04006435
Chris Mason6324fbf2008-03-24 15:01:59 -04006436 ret = update_space_info(info, cache->flags, found_key.offset,
6437 btrfs_block_group_used(&cache->item),
6438 &space_info);
6439 BUG_ON(ret);
6440 cache->space_info = space_info;
Josef Bacik80eb2342008-10-29 14:49:05 -04006441 down_write(&space_info->groups_sem);
6442 list_add_tail(&cache->list, &space_info->block_groups);
6443 up_write(&space_info->groups_sem);
Chris Mason6324fbf2008-03-24 15:01:59 -04006444
Josef Bacik0f9dd462008-09-23 13:14:11 -04006445 ret = btrfs_add_block_group_cache(root->fs_info, cache);
6446 BUG_ON(ret);
Chris Mason75ccf472008-09-30 19:24:06 -04006447
6448 set_avail_alloc_bits(root->fs_info, cache->flags);
Yan Zheng2b820322008-11-17 21:11:30 -05006449 if (btrfs_chunk_readonly(root, cache->key.objectid))
6450 set_block_group_readonly(cache);
Chris Mason9078a3e2007-04-26 16:46:15 -04006451 }
Chris Mason0b86a832008-03-24 15:01:56 -04006452 ret = 0;
6453error:
Chris Mason9078a3e2007-04-26 16:46:15 -04006454 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04006455 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04006456}
Chris Mason6324fbf2008-03-24 15:01:59 -04006457
6458int btrfs_make_block_group(struct btrfs_trans_handle *trans,
6459 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04006460 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04006461 u64 size)
6462{
6463 int ret;
Chris Mason6324fbf2008-03-24 15:01:59 -04006464 struct btrfs_root *extent_root;
6465 struct btrfs_block_group_cache *cache;
Chris Mason6324fbf2008-03-24 15:01:59 -04006466
6467 extent_root = root->fs_info->extent_root;
Chris Mason6324fbf2008-03-24 15:01:59 -04006468
Chris Masone02119d2008-09-05 16:13:11 -04006469 root->fs_info->last_trans_new_blockgroup = trans->transid;
6470
Chris Mason8f18cf12008-04-25 16:53:30 -04006471 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Josef Bacik0f9dd462008-09-23 13:14:11 -04006472 if (!cache)
6473 return -ENOMEM;
6474
Chris Masone17cade2008-04-15 15:41:47 -04006475 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04006476 cache->key.offset = size;
Yan Zhengd2fb3432008-12-11 16:30:39 -05006477 cache->key.type = BTRFS_BLOCK_GROUP_ITEM_KEY;
6478 atomic_set(&cache->count, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04006479 spin_lock_init(&cache->lock);
Josef Bacik25179202008-10-29 14:49:05 -04006480 mutex_init(&cache->alloc_mutex);
Josef Bacikea6a4782008-11-20 12:16:16 -05006481 mutex_init(&cache->cache_mutex);
Josef Bacik0f9dd462008-09-23 13:14:11 -04006482 INIT_LIST_HEAD(&cache->list);
Chris Mason0ef3e662008-05-24 14:04:53 -04006483
Chris Mason6324fbf2008-03-24 15:01:59 -04006484 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04006485 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
6486 cache->flags = type;
6487 btrfs_set_block_group_flags(&cache->item, type);
6488
6489 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
6490 &cache->space_info);
6491 BUG_ON(ret);
Josef Bacik80eb2342008-10-29 14:49:05 -04006492 down_write(&cache->space_info->groups_sem);
6493 list_add_tail(&cache->list, &cache->space_info->block_groups);
6494 up_write(&cache->space_info->groups_sem);
Chris Mason6324fbf2008-03-24 15:01:59 -04006495
Josef Bacik0f9dd462008-09-23 13:14:11 -04006496 ret = btrfs_add_block_group_cache(root->fs_info, cache);
6497 BUG_ON(ret);
Chris Masonc286ac42008-07-22 23:06:41 -04006498
Chris Mason6324fbf2008-03-24 15:01:59 -04006499 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
6500 sizeof(cache->item));
6501 BUG_ON(ret);
6502
Chris Mason87ef2bb2008-10-30 11:23:27 -04006503 finish_current_insert(trans, extent_root, 0);
6504 ret = del_pending_extents(trans, extent_root, 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04006505 BUG_ON(ret);
Chris Masond18a2c42008-04-04 15:40:00 -04006506 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04006507
Chris Mason6324fbf2008-03-24 15:01:59 -04006508 return 0;
6509}
Zheng Yan1a40e232008-09-26 10:09:34 -04006510
6511int btrfs_remove_block_group(struct btrfs_trans_handle *trans,
6512 struct btrfs_root *root, u64 group_start)
6513{
6514 struct btrfs_path *path;
6515 struct btrfs_block_group_cache *block_group;
6516 struct btrfs_key key;
6517 int ret;
6518
Zheng Yan1a40e232008-09-26 10:09:34 -04006519 root = root->fs_info->extent_root;
6520
6521 block_group = btrfs_lookup_block_group(root->fs_info, group_start);
6522 BUG_ON(!block_group);
Yan Zhengc146afa2008-11-12 14:34:12 -05006523 BUG_ON(!block_group->ro);
Zheng Yan1a40e232008-09-26 10:09:34 -04006524
6525 memcpy(&key, &block_group->key, sizeof(key));
6526
6527 path = btrfs_alloc_path();
6528 BUG_ON(!path);
6529
Yan Zheng3dfdb932009-01-21 10:49:16 -05006530 spin_lock(&root->fs_info->block_group_cache_lock);
Zheng Yan1a40e232008-09-26 10:09:34 -04006531 rb_erase(&block_group->cache_node,
6532 &root->fs_info->block_group_cache_tree);
Yan Zheng3dfdb932009-01-21 10:49:16 -05006533 spin_unlock(&root->fs_info->block_group_cache_lock);
6534 btrfs_remove_free_space_cache(block_group);
Josef Bacik80eb2342008-10-29 14:49:05 -04006535 down_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04006536 list_del(&block_group->list);
Josef Bacik80eb2342008-10-29 14:49:05 -04006537 up_write(&block_group->space_info->groups_sem);
Zheng Yan1a40e232008-09-26 10:09:34 -04006538
Yan Zhengc146afa2008-11-12 14:34:12 -05006539 spin_lock(&block_group->space_info->lock);
6540 block_group->space_info->total_bytes -= block_group->key.offset;
6541 block_group->space_info->bytes_readonly -= block_group->key.offset;
6542 spin_unlock(&block_group->space_info->lock);
Yan Zheng2b820322008-11-17 21:11:30 -05006543 block_group->space_info->full = 0;
Yan Zhengc146afa2008-11-12 14:34:12 -05006544
Yan Zhengd2fb3432008-12-11 16:30:39 -05006545 put_block_group(block_group);
6546 put_block_group(block_group);
Zheng Yan1a40e232008-09-26 10:09:34 -04006547
6548 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
6549 if (ret > 0)
6550 ret = -EIO;
6551 if (ret < 0)
6552 goto out;
6553
6554 ret = btrfs_del_item(trans, root, path);
6555out:
6556 btrfs_free_path(path);
6557 return ret;
6558}