blob: 95aee5a29375f24a193ac2cf05916eec18dc4324 [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>
Chris Mason74493f72007-12-11 09:25:06 -050021#include "hash.h"
Miguela5eb62e2008-04-11 15:45:51 -040022#include "crc32c.h"
Chris Masonfec577f2007-02-26 10:40:21 -050023#include "ctree.h"
24#include "disk-io.h"
25#include "print-tree.h"
Chris Masone089f052007-03-16 16:20:31 -040026#include "transaction.h"
Chris Mason0b86a832008-03-24 15:01:56 -040027#include "volumes.h"
Chris Masonfec577f2007-02-26 10:40:21 -050028
Chris Mason0b86a832008-03-24 15:01:56 -040029#define BLOCK_GROUP_DATA EXTENT_WRITEBACK
Chris Mason96b51792007-10-15 16:15:19 -040030#define BLOCK_GROUP_METADATA EXTENT_UPTODATE
Chris Mason0b86a832008-03-24 15:01:56 -040031#define BLOCK_GROUP_SYSTEM EXTENT_NEW
32
Chris Mason96b51792007-10-15 16:15:19 -040033#define BLOCK_GROUP_DIRTY EXTENT_DIRTY
34
Chris Masone089f052007-03-16 16:20:31 -040035static int finish_current_insert(struct btrfs_trans_handle *trans, struct
36 btrfs_root *extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -040037static int del_pending_extents(struct btrfs_trans_handle *trans, struct
38 btrfs_root *extent_root);
Yand548ee52008-01-03 13:56:30 -050039
Chris Masonfec577f2007-02-26 10:40:21 -050040
Chris Masone37c9e62007-05-09 20:13:14 -040041static int cache_block_group(struct btrfs_root *root,
42 struct btrfs_block_group_cache *block_group)
43{
44 struct btrfs_path *path;
45 int ret;
46 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -040047 struct extent_buffer *leaf;
Chris Masond1310b22008-01-24 16:13:08 -050048 struct extent_io_tree *free_space_cache;
Chris Masone37c9e62007-05-09 20:13:14 -040049 int slot;
Chris Masone37c9e62007-05-09 20:13:14 -040050 u64 last = 0;
51 u64 hole_size;
Yan7d7d6062007-09-14 16:15:28 -040052 u64 first_free;
Chris Masone37c9e62007-05-09 20:13:14 -040053 int found = 0;
54
Chris Mason00f5c792007-11-30 10:09:33 -050055 if (!block_group)
56 return 0;
57
Chris Masone37c9e62007-05-09 20:13:14 -040058 root = root->fs_info->extent_root;
Chris Masonf510cfe2007-10-15 16:14:48 -040059 free_space_cache = &root->fs_info->free_space_cache;
Chris Masone37c9e62007-05-09 20:13:14 -040060
61 if (block_group->cached)
62 return 0;
Chris Masonf510cfe2007-10-15 16:14:48 -040063
Chris Masone37c9e62007-05-09 20:13:14 -040064 path = btrfs_alloc_path();
65 if (!path)
66 return -ENOMEM;
Yan7d7d6062007-09-14 16:15:28 -040067
Chris Mason2cc58cf2007-08-27 16:49:44 -040068 path->reada = 2;
Yan7d7d6062007-09-14 16:15:28 -040069 first_free = block_group->key.objectid;
Chris Masone37c9e62007-05-09 20:13:14 -040070 key.objectid = block_group->key.objectid;
Chris Masone37c9e62007-05-09 20:13:14 -040071 key.offset = 0;
72 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
73 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
74 if (ret < 0)
75 return ret;
Chris Mason0b86a832008-03-24 15:01:56 -040076 ret = btrfs_previous_item(root, path, 0, BTRFS_EXTENT_ITEM_KEY);
Yand548ee52008-01-03 13:56:30 -050077 if (ret < 0)
78 return ret;
79 if (ret == 0) {
80 leaf = path->nodes[0];
81 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
82 if (key.objectid + key.offset > first_free)
83 first_free = key.objectid + key.offset;
84 }
Chris Masone37c9e62007-05-09 20:13:14 -040085 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -040086 leaf = path->nodes[0];
Chris Masone37c9e62007-05-09 20:13:14 -040087 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -040088 if (slot >= btrfs_header_nritems(leaf)) {
Chris Masone37c9e62007-05-09 20:13:14 -040089 ret = btrfs_next_leaf(root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -040090 if (ret < 0)
91 goto err;
Chris Masonde428b62007-05-18 13:28:27 -040092 if (ret == 0) {
Chris Masone37c9e62007-05-09 20:13:14 -040093 continue;
Chris Masonde428b62007-05-18 13:28:27 -040094 } else {
Chris Masone37c9e62007-05-09 20:13:14 -040095 break;
96 }
97 }
Chris Mason5f39d392007-10-15 16:14:19 -040098 btrfs_item_key_to_cpu(leaf, &key, slot);
Yan7d7d6062007-09-14 16:15:28 -040099 if (key.objectid < block_group->key.objectid) {
Yan7d7d6062007-09-14 16:15:28 -0400100 goto next;
101 }
Chris Masone37c9e62007-05-09 20:13:14 -0400102 if (key.objectid >= block_group->key.objectid +
103 block_group->key.offset) {
Yan7d7d6062007-09-14 16:15:28 -0400104 break;
105 }
106
107 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
108 if (!found) {
109 last = first_free;
110 found = 1;
Chris Masone37c9e62007-05-09 20:13:14 -0400111 }
Chris Masonf510cfe2007-10-15 16:14:48 -0400112 if (key.objectid > last) {
113 hole_size = key.objectid - last;
114 set_extent_dirty(free_space_cache, last,
115 last + hole_size - 1,
116 GFP_NOFS);
Chris Masone37c9e62007-05-09 20:13:14 -0400117 }
Yan7d7d6062007-09-14 16:15:28 -0400118 last = key.objectid + key.offset;
Chris Masone37c9e62007-05-09 20:13:14 -0400119 }
Yan7d7d6062007-09-14 16:15:28 -0400120next:
Chris Masone37c9e62007-05-09 20:13:14 -0400121 path->slots[0]++;
122 }
123
Yan7d7d6062007-09-14 16:15:28 -0400124 if (!found)
125 last = first_free;
126 if (block_group->key.objectid +
127 block_group->key.offset > last) {
128 hole_size = block_group->key.objectid +
129 block_group->key.offset - last;
Chris Masonf510cfe2007-10-15 16:14:48 -0400130 set_extent_dirty(free_space_cache, last,
131 last + hole_size - 1, GFP_NOFS);
Yan7d7d6062007-09-14 16:15:28 -0400132 }
Chris Masone37c9e62007-05-09 20:13:14 -0400133 block_group->cached = 1;
Chris Mason54aa1f42007-06-22 14:16:25 -0400134err:
Chris Masone37c9e62007-05-09 20:13:14 -0400135 btrfs_free_path(path);
136 return 0;
137}
138
Chris Mason5276aed2007-06-11 21:33:38 -0400139struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
140 btrfs_fs_info *info,
Chris Masondb945352007-10-15 16:15:53 -0400141 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400142{
Chris Masond1310b22008-01-24 16:13:08 -0500143 struct extent_io_tree *block_group_cache;
Chris Mason96b51792007-10-15 16:15:19 -0400144 struct btrfs_block_group_cache *block_group = NULL;
145 u64 ptr;
146 u64 start;
147 u64 end;
Chris Masonbe744172007-05-06 10:15:01 -0400148 int ret;
149
Chris Mason96b51792007-10-15 16:15:19 -0400150 block_group_cache = &info->block_group_cache;
151 ret = find_first_extent_bit(block_group_cache,
Chris Masondb945352007-10-15 16:15:53 -0400152 bytenr, &start, &end,
Chris Mason0b86a832008-03-24 15:01:56 -0400153 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA |
154 BLOCK_GROUP_SYSTEM);
Chris Masonbe744172007-05-06 10:15:01 -0400155 if (ret) {
Chris Mason96b51792007-10-15 16:15:19 -0400156 return NULL;
Chris Masonbe744172007-05-06 10:15:01 -0400157 }
Chris Mason96b51792007-10-15 16:15:19 -0400158 ret = get_state_private(block_group_cache, start, &ptr);
159 if (ret)
160 return NULL;
161
Jens Axboeae2f5412007-10-19 09:22:59 -0400162 block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
Yan5cf66422007-11-16 14:57:09 -0500163 if (block_group->key.objectid <= bytenr && bytenr <
Chris Mason96b51792007-10-15 16:15:19 -0400164 block_group->key.objectid + block_group->key.offset)
165 return block_group;
Chris Masonbe744172007-05-06 10:15:01 -0400166 return NULL;
167}
Chris Mason0b86a832008-03-24 15:01:56 -0400168
169static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
170{
Chris Mason593060d2008-03-25 16:50:33 -0400171 return (cache->flags & bits) == bits;
Chris Mason0b86a832008-03-24 15:01:56 -0400172}
173
174static int noinline find_search_start(struct btrfs_root *root,
Chris Mason98ed5172008-01-03 10:01:48 -0500175 struct btrfs_block_group_cache **cache_ret,
Chris Mason0b86a832008-03-24 15:01:56 -0400176 u64 *start_ret, int num, int data)
Chris Masone37c9e62007-05-09 20:13:14 -0400177{
Chris Masone37c9e62007-05-09 20:13:14 -0400178 int ret;
179 struct btrfs_block_group_cache *cache = *cache_ret;
Chris Masond7fc6402008-02-18 12:12:38 -0500180 struct extent_io_tree *free_space_cache;
Chris Mason7d1660d2008-03-24 15:02:03 -0400181 struct extent_state *state;
Chris Masone19caa52007-10-15 16:17:44 -0400182 u64 last;
Chris Masonf510cfe2007-10-15 16:14:48 -0400183 u64 start = 0;
Chris Mason257d0ce2007-11-07 21:08:16 -0500184 u64 cache_miss = 0;
Chris Masonc31f8832008-01-08 15:46:31 -0500185 u64 total_fs_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -0400186 u64 search_start = *start_ret;
Chris Masonf84a8b32007-11-06 10:26:29 -0500187 int wrapped = 0;
Chris Masone37c9e62007-05-09 20:13:14 -0400188
Chris Mason0b86a832008-03-24 15:01:56 -0400189 if (!cache)
Chris Mason1a2b2ac2007-12-04 13:18:24 -0500190 goto out;
Chris Mason8f18cf12008-04-25 16:53:30 -0400191
Chris Masonc31f8832008-01-08 15:46:31 -0500192 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
Chris Masond7fc6402008-02-18 12:12:38 -0500193 free_space_cache = &root->fs_info->free_space_cache;
194
Chris Masone37c9e62007-05-09 20:13:14 -0400195again:
Chris Mason54aa1f42007-06-22 14:16:25 -0400196 ret = cache_block_group(root, cache);
197 if (ret)
198 goto out;
Chris Masonf84a8b32007-11-06 10:26:29 -0500199
Chris Masone19caa52007-10-15 16:17:44 -0400200 last = max(search_start, cache->key.objectid);
Chris Mason8f18cf12008-04-25 16:53:30 -0400201 if (!block_group_bits(cache, data) || cache->ro) {
Chris Mason0b86a832008-03-24 15:01:56 -0400202 goto new_group;
203 }
Chris Masone19caa52007-10-15 16:17:44 -0400204
Chris Mason7d1660d2008-03-24 15:02:03 -0400205 spin_lock_irq(&free_space_cache->lock);
206 state = find_first_extent_bit_state(free_space_cache, last, EXTENT_DIRTY);
Chris Masone37c9e62007-05-09 20:13:14 -0400207 while(1) {
Chris Mason7d1660d2008-03-24 15:02:03 -0400208 if (!state) {
Chris Mason257d0ce2007-11-07 21:08:16 -0500209 if (!cache_miss)
210 cache_miss = last;
Chris Mason7d1660d2008-03-24 15:02:03 -0400211 spin_unlock_irq(&free_space_cache->lock);
Chris Masone19caa52007-10-15 16:17:44 -0400212 goto new_group;
213 }
Chris Masonf510cfe2007-10-15 16:14:48 -0400214
Chris Mason7d1660d2008-03-24 15:02:03 -0400215 start = max(last, state->start);
216 last = state->end + 1;
Chris Mason257d0ce2007-11-07 21:08:16 -0500217 if (last - start < num) {
218 if (last == cache->key.objectid + cache->key.offset)
219 cache_miss = start;
Chris Mason7d1660d2008-03-24 15:02:03 -0400220 do {
221 state = extent_state_next(state);
222 } while(state && !(state->state & EXTENT_DIRTY));
Chris Masonf510cfe2007-10-15 16:14:48 -0400223 continue;
Chris Mason257d0ce2007-11-07 21:08:16 -0500224 }
Chris Mason7d1660d2008-03-24 15:02:03 -0400225 spin_unlock_irq(&free_space_cache->lock);
Chris Mason8f18cf12008-04-25 16:53:30 -0400226 if (cache->ro)
227 goto new_group;
Chris Mason0b86a832008-03-24 15:01:56 -0400228 if (start + num > cache->key.objectid + cache->key.offset)
Chris Masone37c9e62007-05-09 20:13:14 -0400229 goto new_group;
Chris Masonc31f8832008-01-08 15:46:31 -0500230 if (start + num > total_fs_bytes)
231 goto new_group;
Chris Mason8790d502008-04-03 16:29:03 -0400232 if (!block_group_bits(cache, data)) {
Chris Mason611f0e02008-04-03 16:29:03 -0400233 printk("block group bits don't match %Lu %d\n", cache->flags, data);
Chris Mason8790d502008-04-03 16:29:03 -0400234 }
Chris Mason0b86a832008-03-24 15:01:56 -0400235 *start_ret = start;
236 return 0;
Chris Mason8790d502008-04-03 16:29:03 -0400237 }
238out:
Chris Mason1a2b2ac2007-12-04 13:18:24 -0500239 cache = btrfs_lookup_block_group(root->fs_info, search_start);
240 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -0400241 printk("Unable to find block group for %Lu\n", search_start);
Chris Mason1a2b2ac2007-12-04 13:18:24 -0500242 WARN_ON(1);
Chris Mason1a2b2ac2007-12-04 13:18:24 -0500243 }
Chris Mason0b86a832008-03-24 15:01:56 -0400244 return -ENOSPC;
Chris Masone37c9e62007-05-09 20:13:14 -0400245
246new_group:
Chris Masone19caa52007-10-15 16:17:44 -0400247 last = cache->key.objectid + cache->key.offset;
Chris Masonf84a8b32007-11-06 10:26:29 -0500248wrapped:
Chris Masone19caa52007-10-15 16:17:44 -0400249 cache = btrfs_lookup_block_group(root->fs_info, last);
Chris Masonc31f8832008-01-08 15:46:31 -0500250 if (!cache || cache->key.objectid >= total_fs_bytes) {
Chris Mason0e4de582007-11-26 10:55:49 -0500251no_cache:
Chris Masonf84a8b32007-11-06 10:26:29 -0500252 if (!wrapped) {
253 wrapped = 1;
254 last = search_start;
Chris Masonf84a8b32007-11-06 10:26:29 -0500255 goto wrapped;
256 }
Chris Mason1a2b2ac2007-12-04 13:18:24 -0500257 goto out;
Chris Masone37c9e62007-05-09 20:13:14 -0400258 }
Chris Mason257d0ce2007-11-07 21:08:16 -0500259 if (cache_miss && !cache->cached) {
260 cache_block_group(root, cache);
261 last = cache_miss;
Chris Mason257d0ce2007-11-07 21:08:16 -0500262 cache = btrfs_lookup_block_group(root->fs_info, last);
263 }
Chris Mason1a2b2ac2007-12-04 13:18:24 -0500264 cache = btrfs_find_block_group(root, cache, last, data, 0);
Chris Mason0e4de582007-11-26 10:55:49 -0500265 if (!cache)
266 goto no_cache;
Chris Masone37c9e62007-05-09 20:13:14 -0400267 *cache_ret = cache;
Chris Mason257d0ce2007-11-07 21:08:16 -0500268 cache_miss = 0;
Chris Masone37c9e62007-05-09 20:13:14 -0400269 goto again;
270}
271
Chris Mason84f54cf2007-06-12 07:43:08 -0400272static u64 div_factor(u64 num, int factor)
273{
Chris Mason257d0ce2007-11-07 21:08:16 -0500274 if (factor == 10)
275 return num;
Chris Mason84f54cf2007-06-12 07:43:08 -0400276 num *= factor;
277 do_div(num, 10);
278 return num;
279}
280
Chris Mason6324fbf2008-03-24 15:01:59 -0400281static int block_group_state_bits(u64 flags)
282{
283 int bits = 0;
284 if (flags & BTRFS_BLOCK_GROUP_DATA)
285 bits |= BLOCK_GROUP_DATA;
286 if (flags & BTRFS_BLOCK_GROUP_METADATA)
287 bits |= BLOCK_GROUP_METADATA;
288 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
289 bits |= BLOCK_GROUP_SYSTEM;
290 return bits;
291}
292
Chris Mason31f3c992007-04-30 15:25:45 -0400293struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
294 struct btrfs_block_group_cache
Chris Masonbe744172007-05-06 10:15:01 -0400295 *hint, u64 search_start,
Chris Masonde428b62007-05-18 13:28:27 -0400296 int data, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400297{
Chris Mason96b51792007-10-15 16:15:19 -0400298 struct btrfs_block_group_cache *cache;
Chris Masond1310b22008-01-24 16:13:08 -0500299 struct extent_io_tree *block_group_cache;
Chris Mason31f3c992007-04-30 15:25:45 -0400300 struct btrfs_block_group_cache *found_group = NULL;
Chris Masoncd1bc462007-04-27 10:08:34 -0400301 struct btrfs_fs_info *info = root->fs_info;
302 u64 used;
Chris Mason31f3c992007-04-30 15:25:45 -0400303 u64 last = 0;
304 u64 hint_last;
Chris Mason96b51792007-10-15 16:15:19 -0400305 u64 start;
306 u64 end;
307 u64 free_check;
308 u64 ptr;
Chris Masonc31f8832008-01-08 15:46:31 -0500309 u64 total_fs_bytes;
Chris Mason96b51792007-10-15 16:15:19 -0400310 int bit;
Chris Masoncd1bc462007-04-27 10:08:34 -0400311 int ret;
Chris Mason31f3c992007-04-30 15:25:45 -0400312 int full_search = 0;
Chris Masonbce4eae2008-04-24 14:42:46 -0400313 int factor = 10;
Chris Masonde428b62007-05-18 13:28:27 -0400314
Chris Mason96b51792007-10-15 16:15:19 -0400315 block_group_cache = &info->block_group_cache;
Chris Masonc31f8832008-01-08 15:46:31 -0500316 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
Chris Mason96b51792007-10-15 16:15:19 -0400317
Chris Masonde428b62007-05-18 13:28:27 -0400318 if (!owner)
Chris Masonbce4eae2008-04-24 14:42:46 -0400319 factor = 10;
Chris Masonbe744172007-05-06 10:15:01 -0400320
Chris Mason6324fbf2008-03-24 15:01:59 -0400321 bit = block_group_state_bits(data);
Chris Masonbe744172007-05-06 10:15:01 -0400322
Chris Masonc31f8832008-01-08 15:46:31 -0500323 if (search_start && search_start < total_fs_bytes) {
Chris Masonbe744172007-05-06 10:15:01 -0400324 struct btrfs_block_group_cache *shint;
Chris Mason5276aed2007-06-11 21:33:38 -0400325 shint = btrfs_lookup_block_group(info, search_start);
Chris Mason8f18cf12008-04-25 16:53:30 -0400326 if (shint && block_group_bits(shint, data) && !shint->ro) {
Chris Masonbe744172007-05-06 10:15:01 -0400327 used = btrfs_block_group_used(&shint->item);
Yan324ae4d2007-11-16 14:57:08 -0500328 if (used + shint->pinned <
329 div_factor(shint->key.offset, factor)) {
Chris Masonbe744172007-05-06 10:15:01 -0400330 return shint;
331 }
332 }
333 }
Chris Mason8f18cf12008-04-25 16:53:30 -0400334 if (hint && !hint->ro && block_group_bits(hint, data) &&
Chris Mason0b86a832008-03-24 15:01:56 -0400335 hint->key.objectid < total_fs_bytes) {
Chris Mason31f3c992007-04-30 15:25:45 -0400336 used = btrfs_block_group_used(&hint->item);
Yan324ae4d2007-11-16 14:57:08 -0500337 if (used + hint->pinned <
338 div_factor(hint->key.offset, factor)) {
Chris Mason31f3c992007-04-30 15:25:45 -0400339 return hint;
340 }
Chris Masone19caa52007-10-15 16:17:44 -0400341 last = hint->key.objectid + hint->key.offset;
Chris Mason31f3c992007-04-30 15:25:45 -0400342 hint_last = last;
343 } else {
Chris Masone37c9e62007-05-09 20:13:14 -0400344 if (hint)
345 hint_last = max(hint->key.objectid, search_start);
346 else
347 hint_last = search_start;
348
Chris Masonc31f8832008-01-08 15:46:31 -0500349 if (hint_last >= total_fs_bytes)
350 hint_last = search_start;
Chris Masone37c9e62007-05-09 20:13:14 -0400351 last = hint_last;
Chris Mason31f3c992007-04-30 15:25:45 -0400352 }
Chris Mason31f3c992007-04-30 15:25:45 -0400353again:
Chris Masoncd1bc462007-04-27 10:08:34 -0400354 while(1) {
Chris Mason96b51792007-10-15 16:15:19 -0400355 ret = find_first_extent_bit(block_group_cache, last,
356 &start, &end, bit);
357 if (ret)
Chris Masoncd1bc462007-04-27 10:08:34 -0400358 break;
Chris Mason96b51792007-10-15 16:15:19 -0400359
360 ret = get_state_private(block_group_cache, start, &ptr);
361 if (ret)
362 break;
363
Jens Axboeae2f5412007-10-19 09:22:59 -0400364 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
Chris Mason96b51792007-10-15 16:15:19 -0400365 last = cache->key.objectid + cache->key.offset;
366 used = btrfs_block_group_used(&cache->item);
367
Chris Masonc31f8832008-01-08 15:46:31 -0500368 if (cache->key.objectid > total_fs_bytes)
369 break;
370
Chris Mason8f18cf12008-04-25 16:53:30 -0400371 if (!cache->ro && block_group_bits(cache, data)) {
Chris Mason8790d502008-04-03 16:29:03 -0400372 if (full_search)
373 free_check = cache->key.offset;
374 else
375 free_check = div_factor(cache->key.offset,
376 factor);
Chris Mason6324fbf2008-03-24 15:01:59 -0400377
Chris Mason8790d502008-04-03 16:29:03 -0400378 if (used + cache->pinned < free_check) {
379 found_group = cache;
380 goto found;
381 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400382 }
Chris Masonde428b62007-05-18 13:28:27 -0400383 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400384 }
Chris Mason31f3c992007-04-30 15:25:45 -0400385 if (!full_search) {
Chris Masonbe744172007-05-06 10:15:01 -0400386 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400387 full_search = 1;
388 goto again;
389 }
Chris Masonbe744172007-05-06 10:15:01 -0400390found:
Chris Mason31f3c992007-04-30 15:25:45 -0400391 return found_group;
Chris Masoncd1bc462007-04-27 10:08:34 -0400392}
393
Chris Mason7bb86312007-12-11 09:25:06 -0500394static u64 hash_extent_ref(u64 root_objectid, u64 ref_generation,
Chris Mason74493f72007-12-11 09:25:06 -0500395 u64 owner, u64 owner_offset)
396{
397 u32 high_crc = ~(u32)0;
398 u32 low_crc = ~(u32)0;
399 __le64 lenum;
Chris Mason74493f72007-12-11 09:25:06 -0500400 lenum = cpu_to_le64(root_objectid);
Miguela5eb62e2008-04-11 15:45:51 -0400401 high_crc = btrfs_crc32c(high_crc, &lenum, sizeof(lenum));
Chris Mason7bb86312007-12-11 09:25:06 -0500402 lenum = cpu_to_le64(ref_generation);
Miguela5eb62e2008-04-11 15:45:51 -0400403 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
Chris Mason21a49892008-02-01 14:51:59 -0500404 if (owner >= BTRFS_FIRST_FREE_OBJECTID) {
405 lenum = cpu_to_le64(owner);
Miguela5eb62e2008-04-11 15:45:51 -0400406 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
Chris Mason21a49892008-02-01 14:51:59 -0500407 lenum = cpu_to_le64(owner_offset);
Miguela5eb62e2008-04-11 15:45:51 -0400408 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
Chris Mason21a49892008-02-01 14:51:59 -0500409 }
Chris Mason74493f72007-12-11 09:25:06 -0500410 return ((u64)high_crc << 32) | (u64)low_crc;
411}
412
Chris Mason7bb86312007-12-11 09:25:06 -0500413static int match_extent_ref(struct extent_buffer *leaf,
414 struct btrfs_extent_ref *disk_ref,
415 struct btrfs_extent_ref *cpu_ref)
416{
417 int ret;
418 int len;
419
420 if (cpu_ref->objectid)
421 len = sizeof(*cpu_ref);
422 else
423 len = 2 * sizeof(u64);
424 ret = memcmp_extent_buffer(leaf, cpu_ref, (unsigned long)disk_ref,
425 len);
426 return ret == 0;
427}
428
Chris Mason98ed5172008-01-03 10:01:48 -0500429static int noinline lookup_extent_backref(struct btrfs_trans_handle *trans,
430 struct btrfs_root *root,
431 struct btrfs_path *path, u64 bytenr,
432 u64 root_objectid,
433 u64 ref_generation, u64 owner,
434 u64 owner_offset, int del)
Chris Mason7bb86312007-12-11 09:25:06 -0500435{
436 u64 hash;
437 struct btrfs_key key;
438 struct btrfs_key found_key;
439 struct btrfs_extent_ref ref;
440 struct extent_buffer *leaf;
441 struct btrfs_extent_ref *disk_ref;
442 int ret;
443 int ret2;
444
445 btrfs_set_stack_ref_root(&ref, root_objectid);
446 btrfs_set_stack_ref_generation(&ref, ref_generation);
447 btrfs_set_stack_ref_objectid(&ref, owner);
448 btrfs_set_stack_ref_offset(&ref, owner_offset);
449
450 hash = hash_extent_ref(root_objectid, ref_generation, owner,
451 owner_offset);
452 key.offset = hash;
453 key.objectid = bytenr;
454 key.type = BTRFS_EXTENT_REF_KEY;
455
456 while (1) {
457 ret = btrfs_search_slot(trans, root, &key, path,
458 del ? -1 : 0, del);
459 if (ret < 0)
460 goto out;
461 leaf = path->nodes[0];
462 if (ret != 0) {
463 u32 nritems = btrfs_header_nritems(leaf);
464 if (path->slots[0] >= nritems) {
465 ret2 = btrfs_next_leaf(root, path);
466 if (ret2)
467 goto out;
468 leaf = path->nodes[0];
469 }
470 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
471 if (found_key.objectid != bytenr ||
472 found_key.type != BTRFS_EXTENT_REF_KEY)
473 goto out;
474 key.offset = found_key.offset;
475 if (del) {
476 btrfs_release_path(root, path);
477 continue;
478 }
479 }
480 disk_ref = btrfs_item_ptr(path->nodes[0],
481 path->slots[0],
482 struct btrfs_extent_ref);
483 if (match_extent_ref(path->nodes[0], disk_ref, &ref)) {
484 ret = 0;
485 goto out;
486 }
487 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
488 key.offset = found_key.offset + 1;
489 btrfs_release_path(root, path);
490 }
491out:
492 return ret;
493}
494
Chris Masond8d5f3e2007-12-11 12:42:00 -0500495/*
496 * Back reference rules. Back refs have three main goals:
497 *
498 * 1) differentiate between all holders of references to an extent so that
499 * when a reference is dropped we can make sure it was a valid reference
500 * before freeing the extent.
501 *
502 * 2) Provide enough information to quickly find the holders of an extent
503 * if we notice a given block is corrupted or bad.
504 *
505 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
506 * maintenance. This is actually the same as #2, but with a slightly
507 * different use case.
508 *
509 * File extents can be referenced by:
510 *
511 * - multiple snapshots, subvolumes, or different generations in one subvol
512 * - different files inside a single subvolume (in theory, not implemented yet)
513 * - different offsets inside a file (bookend extents in file.c)
514 *
515 * The extent ref structure has fields for:
516 *
517 * - Objectid of the subvolume root
518 * - Generation number of the tree holding the reference
519 * - objectid of the file holding the reference
520 * - offset in the file corresponding to the key holding the reference
521 *
522 * When a file extent is allocated the fields are filled in:
523 * (root_key.objectid, trans->transid, inode objectid, offset in file)
524 *
525 * When a leaf is cow'd new references are added for every file extent found
526 * in the leaf. It looks the same as the create case, but trans->transid
527 * will be different when the block is cow'd.
528 *
529 * (root_key.objectid, trans->transid, inode objectid, offset in file)
530 *
531 * When a file extent is removed either during snapshot deletion or file
532 * truncation, the corresponding back reference is found
533 * by searching for:
534 *
535 * (btrfs_header_owner(leaf), btrfs_header_generation(leaf),
536 * inode objectid, offset in file)
537 *
538 * Btree extents can be referenced by:
539 *
540 * - Different subvolumes
541 * - Different generations of the same subvolume
542 *
543 * Storing sufficient information for a full reverse mapping of a btree
544 * block would require storing the lowest key of the block in the backref,
545 * and it would require updating that lowest key either before write out or
546 * every time it changed. Instead, the objectid of the lowest key is stored
547 * along with the level of the tree block. This provides a hint
548 * about where in the btree the block can be found. Searches through the
549 * btree only need to look for a pointer to that block, so they stop one
550 * level higher than the level recorded in the backref.
551 *
552 * Some btrees do not do reference counting on their extents. These
553 * include the extent tree and the tree of tree roots. Backrefs for these
554 * trees always have a generation of zero.
555 *
556 * When a tree block is created, back references are inserted:
557 *
Chris Masonf6dbff52007-12-13 11:13:32 -0500558 * (root->root_key.objectid, trans->transid or zero, level, lowest_key_objectid)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500559 *
560 * When a tree block is cow'd in a reference counted root,
561 * new back references are added for all the blocks it points to.
562 * These are of the form (trans->transid will have increased since creation):
563 *
Chris Masonf6dbff52007-12-13 11:13:32 -0500564 * (root->root_key.objectid, trans->transid, level, lowest_key_objectid)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500565 *
566 * Because the lowest_key_objectid and the level are just hints
567 * they are not used when backrefs are deleted. When a backref is deleted:
568 *
569 * if backref was for a tree root:
570 * root_objectid = root->root_key.objectid
571 * else
572 * root_objectid = btrfs_header_owner(parent)
573 *
574 * (root_objectid, btrfs_header_generation(parent) or zero, 0, 0)
575 *
576 * Back Reference Key hashing:
577 *
578 * Back references have four fields, each 64 bits long. Unfortunately,
579 * This is hashed into a single 64 bit number and placed into the key offset.
580 * The key objectid corresponds to the first byte in the extent, and the
581 * key type is set to BTRFS_EXTENT_REF_KEY
582 */
Chris Mason7bb86312007-12-11 09:25:06 -0500583int btrfs_insert_extent_backref(struct btrfs_trans_handle *trans,
584 struct btrfs_root *root,
585 struct btrfs_path *path, u64 bytenr,
586 u64 root_objectid, u64 ref_generation,
587 u64 owner, u64 owner_offset)
Chris Mason74493f72007-12-11 09:25:06 -0500588{
589 u64 hash;
590 struct btrfs_key key;
591 struct btrfs_extent_ref ref;
Chris Mason7bb86312007-12-11 09:25:06 -0500592 struct btrfs_extent_ref *disk_ref;
Chris Mason74493f72007-12-11 09:25:06 -0500593 int ret;
594
595 btrfs_set_stack_ref_root(&ref, root_objectid);
Chris Mason7bb86312007-12-11 09:25:06 -0500596 btrfs_set_stack_ref_generation(&ref, ref_generation);
Chris Mason74493f72007-12-11 09:25:06 -0500597 btrfs_set_stack_ref_objectid(&ref, owner);
598 btrfs_set_stack_ref_offset(&ref, owner_offset);
599
Chris Mason7bb86312007-12-11 09:25:06 -0500600 hash = hash_extent_ref(root_objectid, ref_generation, owner,
601 owner_offset);
Chris Mason74493f72007-12-11 09:25:06 -0500602 key.offset = hash;
603 key.objectid = bytenr;
604 key.type = BTRFS_EXTENT_REF_KEY;
605
606 ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(ref));
607 while (ret == -EEXIST) {
Chris Mason7bb86312007-12-11 09:25:06 -0500608 disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
609 struct btrfs_extent_ref);
610 if (match_extent_ref(path->nodes[0], disk_ref, &ref))
611 goto out;
612 key.offset++;
613 btrfs_release_path(root, path);
614 ret = btrfs_insert_empty_item(trans, root, path, &key,
615 sizeof(ref));
Chris Mason74493f72007-12-11 09:25:06 -0500616 }
Chris Mason7bb86312007-12-11 09:25:06 -0500617 if (ret)
618 goto out;
619 disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
620 struct btrfs_extent_ref);
621 write_extent_buffer(path->nodes[0], &ref, (unsigned long)disk_ref,
622 sizeof(ref));
623 btrfs_mark_buffer_dirty(path->nodes[0]);
624out:
625 btrfs_release_path(root, path);
626 return ret;
Chris Mason74493f72007-12-11 09:25:06 -0500627}
628
Chris Masonb18c6682007-04-17 13:26:50 -0400629int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
630 struct btrfs_root *root,
Chris Mason74493f72007-12-11 09:25:06 -0500631 u64 bytenr, u64 num_bytes,
Chris Mason7bb86312007-12-11 09:25:06 -0500632 u64 root_objectid, u64 ref_generation,
Chris Mason74493f72007-12-11 09:25:06 -0500633 u64 owner, u64 owner_offset)
Chris Mason02217ed2007-03-02 16:08:05 -0500634{
Chris Mason5caf2a02007-04-02 11:20:42 -0400635 struct btrfs_path *path;
Chris Mason02217ed2007-03-02 16:08:05 -0500636 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400637 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400638 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -0400639 struct btrfs_extent_item *item;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400640 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500641
Chris Masondb945352007-10-15 16:15:53 -0400642 WARN_ON(num_bytes < root->sectorsize);
Chris Mason5caf2a02007-04-02 11:20:42 -0400643 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400644 if (!path)
645 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -0400646
Chris Mason3c12ac72008-04-21 12:01:38 -0400647 path->reada = 1;
Chris Masondb945352007-10-15 16:15:53 -0400648 key.objectid = bytenr;
Chris Mason62e27492007-03-15 12:56:47 -0400649 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masondb945352007-10-15 16:15:53 -0400650 key.offset = num_bytes;
Chris Mason5caf2a02007-04-02 11:20:42 -0400651 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400652 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400653 if (ret < 0)
654 return ret;
Chris Masona429e512007-04-18 16:15:28 -0400655 if (ret != 0) {
Chris Masona28ec192007-03-06 20:08:01 -0500656 BUG();
Chris Masona429e512007-04-18 16:15:28 -0400657 }
Chris Mason02217ed2007-03-02 16:08:05 -0500658 BUG_ON(ret != 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400659 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -0400660 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400661 refs = btrfs_extent_refs(l, item);
662 btrfs_set_extent_refs(l, item, refs + 1);
Chris Mason5caf2a02007-04-02 11:20:42 -0400663 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masona28ec192007-03-06 20:08:01 -0500664
Chris Mason5caf2a02007-04-02 11:20:42 -0400665 btrfs_release_path(root->fs_info->extent_root, path);
Chris Mason7bb86312007-12-11 09:25:06 -0500666
Chris Mason3c12ac72008-04-21 12:01:38 -0400667 path->reada = 1;
Chris Mason7bb86312007-12-11 09:25:06 -0500668 ret = btrfs_insert_extent_backref(trans, root->fs_info->extent_root,
669 path, bytenr, root_objectid,
670 ref_generation, owner, owner_offset);
671 BUG_ON(ret);
Chris Mason9f5fae22007-03-20 14:38:32 -0400672 finish_current_insert(trans, root->fs_info->extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400673 del_pending_extents(trans, root->fs_info->extent_root);
Chris Mason74493f72007-12-11 09:25:06 -0500674
675 btrfs_free_path(path);
Chris Mason02217ed2007-03-02 16:08:05 -0500676 return 0;
677}
678
Chris Masone9d0b132007-08-10 14:06:19 -0400679int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
680 struct btrfs_root *root)
681{
682 finish_current_insert(trans, root->fs_info->extent_root);
683 del_pending_extents(trans, root->fs_info->extent_root);
684 return 0;
685}
686
Chris Masonb18c6682007-04-17 13:26:50 -0400687static int lookup_extent_ref(struct btrfs_trans_handle *trans,
Chris Masondb945352007-10-15 16:15:53 -0400688 struct btrfs_root *root, u64 bytenr,
689 u64 num_bytes, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -0500690{
Chris Mason5caf2a02007-04-02 11:20:42 -0400691 struct btrfs_path *path;
Chris Masona28ec192007-03-06 20:08:01 -0500692 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400693 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400694 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -0400695 struct btrfs_extent_item *item;
Chris Mason5caf2a02007-04-02 11:20:42 -0400696
Chris Masondb945352007-10-15 16:15:53 -0400697 WARN_ON(num_bytes < root->sectorsize);
Chris Mason5caf2a02007-04-02 11:20:42 -0400698 path = btrfs_alloc_path();
Chris Mason3c12ac72008-04-21 12:01:38 -0400699 path->reada = 1;
Chris Masondb945352007-10-15 16:15:53 -0400700 key.objectid = bytenr;
701 key.offset = num_bytes;
Chris Mason62e27492007-03-15 12:56:47 -0400702 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5caf2a02007-04-02 11:20:42 -0400703 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400704 0, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400705 if (ret < 0)
706 goto out;
Chris Mason5f39d392007-10-15 16:14:19 -0400707 if (ret != 0) {
708 btrfs_print_leaf(root, path->nodes[0]);
Chris Masondb945352007-10-15 16:15:53 -0400709 printk("failed to find block number %Lu\n", bytenr);
Chris Masona28ec192007-03-06 20:08:01 -0500710 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -0400711 }
712 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -0400713 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400714 *refs = btrfs_extent_refs(l, item);
Chris Mason54aa1f42007-06-22 14:16:25 -0400715out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400716 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -0500717 return 0;
718}
719
Chris Masonbe20aa92007-12-17 20:14:01 -0500720u32 btrfs_count_snapshots_in_path(struct btrfs_root *root,
721 struct btrfs_path *count_path,
722 u64 first_extent)
723{
724 struct btrfs_root *extent_root = root->fs_info->extent_root;
725 struct btrfs_path *path;
726 u64 bytenr;
727 u64 found_objectid;
Chris Mason56b453c2008-01-03 09:08:27 -0500728 u64 root_objectid = root->root_key.objectid;
Chris Masonbe20aa92007-12-17 20:14:01 -0500729 u32 total_count = 0;
730 u32 cur_count;
Chris Masonbe20aa92007-12-17 20:14:01 -0500731 u32 nritems;
732 int ret;
733 struct btrfs_key key;
734 struct btrfs_key found_key;
735 struct extent_buffer *l;
736 struct btrfs_extent_item *item;
737 struct btrfs_extent_ref *ref_item;
738 int level = -1;
739
740 path = btrfs_alloc_path();
741again:
742 if (level == -1)
743 bytenr = first_extent;
744 else
745 bytenr = count_path->nodes[level]->start;
746
747 cur_count = 0;
748 key.objectid = bytenr;
749 key.offset = 0;
750
751 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
752 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
753 if (ret < 0)
754 goto out;
755 BUG_ON(ret == 0);
756
757 l = path->nodes[0];
758 btrfs_item_key_to_cpu(l, &found_key, path->slots[0]);
759
760 if (found_key.objectid != bytenr ||
761 found_key.type != BTRFS_EXTENT_ITEM_KEY) {
762 goto out;
763 }
764
765 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Masonbe20aa92007-12-17 20:14:01 -0500766 while (1) {
Chris Masonbd098352008-01-03 13:23:19 -0500767 l = path->nodes[0];
Chris Masonbe20aa92007-12-17 20:14:01 -0500768 nritems = btrfs_header_nritems(l);
769 if (path->slots[0] >= nritems) {
770 ret = btrfs_next_leaf(extent_root, path);
771 if (ret == 0)
772 continue;
773 break;
774 }
775 btrfs_item_key_to_cpu(l, &found_key, path->slots[0]);
776 if (found_key.objectid != bytenr)
777 break;
Chris Masonbd098352008-01-03 13:23:19 -0500778
Chris Masonbe20aa92007-12-17 20:14:01 -0500779 if (found_key.type != BTRFS_EXTENT_REF_KEY) {
780 path->slots[0]++;
781 continue;
782 }
783
784 cur_count++;
785 ref_item = btrfs_item_ptr(l, path->slots[0],
786 struct btrfs_extent_ref);
787 found_objectid = btrfs_ref_root(l, ref_item);
788
Chris Mason56b453c2008-01-03 09:08:27 -0500789 if (found_objectid != root_objectid) {
790 total_count = 2;
Chris Mason4313b392008-01-03 09:08:48 -0500791 goto out;
Chris Mason56b453c2008-01-03 09:08:27 -0500792 }
793 total_count = 1;
Chris Masonbe20aa92007-12-17 20:14:01 -0500794 path->slots[0]++;
795 }
796 if (cur_count == 0) {
797 total_count = 0;
798 goto out;
799 }
Chris Masonbe20aa92007-12-17 20:14:01 -0500800 if (level >= 0 && root->node == count_path->nodes[level])
801 goto out;
802 level++;
803 btrfs_release_path(root, path);
804 goto again;
805
806out:
807 btrfs_free_path(path);
808 return total_count;
Chris Masonbe20aa92007-12-17 20:14:01 -0500809}
Chris Masonc5739bb2007-04-10 09:27:04 -0400810int btrfs_inc_root_ref(struct btrfs_trans_handle *trans,
Chris Mason7bb86312007-12-11 09:25:06 -0500811 struct btrfs_root *root, u64 owner_objectid)
Chris Masonc5739bb2007-04-10 09:27:04 -0400812{
Chris Mason7bb86312007-12-11 09:25:06 -0500813 u64 generation;
814 u64 key_objectid;
815 u64 level;
816 u32 nritems;
817 struct btrfs_disk_key disk_key;
818
819 level = btrfs_header_level(root->node);
820 generation = trans->transid;
821 nritems = btrfs_header_nritems(root->node);
822 if (nritems > 0) {
823 if (level == 0)
824 btrfs_item_key(root->node, &disk_key, 0);
825 else
826 btrfs_node_key(root->node, &disk_key, 0);
827 key_objectid = btrfs_disk_key_objectid(&disk_key);
828 } else {
829 key_objectid = 0;
830 }
Chris Masondb945352007-10-15 16:15:53 -0400831 return btrfs_inc_extent_ref(trans, root, root->node->start,
Chris Mason7bb86312007-12-11 09:25:06 -0500832 root->node->len, owner_objectid,
Chris Masonf6dbff52007-12-13 11:13:32 -0500833 generation, level, key_objectid);
Chris Masonc5739bb2007-04-10 09:27:04 -0400834}
835
Chris Masone089f052007-03-16 16:20:31 -0400836int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Chris Mason5f39d392007-10-15 16:14:19 -0400837 struct extent_buffer *buf)
Chris Mason02217ed2007-03-02 16:08:05 -0500838{
Chris Masondb945352007-10-15 16:15:53 -0400839 u64 bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -0400840 u32 nritems;
841 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -0400842 struct btrfs_file_extent_item *fi;
Chris Mason02217ed2007-03-02 16:08:05 -0500843 int i;
Chris Masondb945352007-10-15 16:15:53 -0400844 int level;
Chris Mason6407bf62007-03-27 06:33:00 -0400845 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -0400846 int faili;
Chris Masona28ec192007-03-06 20:08:01 -0500847
Chris Mason3768f362007-03-13 16:47:54 -0400848 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -0500849 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -0400850
Chris Masondb945352007-10-15 16:15:53 -0400851 level = btrfs_header_level(buf);
Chris Mason5f39d392007-10-15 16:14:19 -0400852 nritems = btrfs_header_nritems(buf);
853 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -0400854 if (level == 0) {
855 u64 disk_bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -0400856 btrfs_item_key_to_cpu(buf, &key, i);
857 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -0400858 continue;
Chris Mason5f39d392007-10-15 16:14:19 -0400859 fi = btrfs_item_ptr(buf, i,
Chris Mason6407bf62007-03-27 06:33:00 -0400860 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400861 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason236454d2007-04-19 13:37:44 -0400862 BTRFS_FILE_EXTENT_INLINE)
863 continue;
Chris Masondb945352007-10-15 16:15:53 -0400864 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
865 if (disk_bytenr == 0)
Chris Mason3a686372007-05-24 13:35:57 -0400866 continue;
Chris Masondb945352007-10-15 16:15:53 -0400867 ret = btrfs_inc_extent_ref(trans, root, disk_bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -0500868 btrfs_file_extent_disk_num_bytes(buf, fi),
869 root->root_key.objectid, trans->transid,
870 key.objectid, key.offset);
Chris Mason54aa1f42007-06-22 14:16:25 -0400871 if (ret) {
872 faili = i;
873 goto fail;
874 }
Chris Mason6407bf62007-03-27 06:33:00 -0400875 } else {
Chris Masondb945352007-10-15 16:15:53 -0400876 bytenr = btrfs_node_blockptr(buf, i);
Chris Mason6caab482007-12-13 09:48:07 -0500877 btrfs_node_key_to_cpu(buf, &key, i);
Chris Masondb945352007-10-15 16:15:53 -0400878 ret = btrfs_inc_extent_ref(trans, root, bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -0500879 btrfs_level_size(root, level - 1),
880 root->root_key.objectid,
Chris Masonf6dbff52007-12-13 11:13:32 -0500881 trans->transid,
882 level - 1, key.objectid);
Chris Mason54aa1f42007-06-22 14:16:25 -0400883 if (ret) {
884 faili = i;
885 goto fail;
886 }
Chris Mason6407bf62007-03-27 06:33:00 -0400887 }
Chris Mason02217ed2007-03-02 16:08:05 -0500888 }
889 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -0400890fail:
Chris Masonccd467d2007-06-28 15:57:36 -0400891 WARN_ON(1);
Chris Mason7bb86312007-12-11 09:25:06 -0500892#if 0
Chris Mason54aa1f42007-06-22 14:16:25 -0400893 for (i =0; i < faili; i++) {
Chris Masondb945352007-10-15 16:15:53 -0400894 if (level == 0) {
895 u64 disk_bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -0400896 btrfs_item_key_to_cpu(buf, &key, i);
897 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -0400898 continue;
Chris Mason5f39d392007-10-15 16:14:19 -0400899 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -0400900 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400901 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -0400902 BTRFS_FILE_EXTENT_INLINE)
903 continue;
Chris Masondb945352007-10-15 16:15:53 -0400904 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
905 if (disk_bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -0400906 continue;
Chris Masondb945352007-10-15 16:15:53 -0400907 err = btrfs_free_extent(trans, root, disk_bytenr,
908 btrfs_file_extent_disk_num_bytes(buf,
Chris Mason5f39d392007-10-15 16:14:19 -0400909 fi), 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400910 BUG_ON(err);
911 } else {
Chris Masondb945352007-10-15 16:15:53 -0400912 bytenr = btrfs_node_blockptr(buf, i);
913 err = btrfs_free_extent(trans, root, bytenr,
914 btrfs_level_size(root, level - 1), 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400915 BUG_ON(err);
916 }
917 }
Chris Mason7bb86312007-12-11 09:25:06 -0500918#endif
Chris Mason54aa1f42007-06-22 14:16:25 -0400919 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -0500920}
921
Chris Mason9078a3e2007-04-26 16:46:15 -0400922static int write_one_cache_group(struct btrfs_trans_handle *trans,
923 struct btrfs_root *root,
924 struct btrfs_path *path,
925 struct btrfs_block_group_cache *cache)
926{
927 int ret;
928 int pending_ret;
929 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -0400930 unsigned long bi;
931 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -0400932
Chris Mason9078a3e2007-04-26 16:46:15 -0400933 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400934 if (ret < 0)
935 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -0400936 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -0400937
938 leaf = path->nodes[0];
939 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
940 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
941 btrfs_mark_buffer_dirty(leaf);
Chris Mason9078a3e2007-04-26 16:46:15 -0400942 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400943fail:
Chris Mason9078a3e2007-04-26 16:46:15 -0400944 finish_current_insert(trans, extent_root);
945 pending_ret = del_pending_extents(trans, extent_root);
946 if (ret)
947 return ret;
948 if (pending_ret)
949 return pending_ret;
950 return 0;
951
952}
953
Chris Mason96b51792007-10-15 16:15:19 -0400954int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
955 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -0400956{
Chris Masond1310b22008-01-24 16:13:08 -0500957 struct extent_io_tree *block_group_cache;
Chris Mason96b51792007-10-15 16:15:19 -0400958 struct btrfs_block_group_cache *cache;
Chris Mason9078a3e2007-04-26 16:46:15 -0400959 int ret;
960 int err = 0;
961 int werr = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -0400962 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -0400963 u64 last = 0;
964 u64 start;
965 u64 end;
966 u64 ptr;
Chris Mason9078a3e2007-04-26 16:46:15 -0400967
Chris Mason96b51792007-10-15 16:15:19 -0400968 block_group_cache = &root->fs_info->block_group_cache;
Chris Mason9078a3e2007-04-26 16:46:15 -0400969 path = btrfs_alloc_path();
970 if (!path)
971 return -ENOMEM;
972
973 while(1) {
Chris Mason96b51792007-10-15 16:15:19 -0400974 ret = find_first_extent_bit(block_group_cache, last,
975 &start, &end, BLOCK_GROUP_DIRTY);
976 if (ret)
Chris Mason9078a3e2007-04-26 16:46:15 -0400977 break;
Chris Mason54aa1f42007-06-22 14:16:25 -0400978
Chris Mason96b51792007-10-15 16:15:19 -0400979 last = end + 1;
980 ret = get_state_private(block_group_cache, start, &ptr);
981 if (ret)
982 break;
Jens Axboeae2f5412007-10-19 09:22:59 -0400983 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
Chris Mason96b51792007-10-15 16:15:19 -0400984 err = write_one_cache_group(trans, root,
985 path, cache);
986 /*
987 * if we fail to write the cache group, we want
988 * to keep it marked dirty in hopes that a later
989 * write will work
990 */
991 if (err) {
992 werr = err;
993 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -0400994 }
Chris Mason96b51792007-10-15 16:15:19 -0400995 clear_extent_bits(block_group_cache, start, end,
996 BLOCK_GROUP_DIRTY, GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -0400997 }
998 btrfs_free_path(path);
999 return werr;
1000}
1001
Chris Mason6324fbf2008-03-24 15:01:59 -04001002static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
1003 u64 flags)
1004{
1005 struct list_head *head = &info->space_info;
1006 struct list_head *cur;
1007 struct btrfs_space_info *found;
1008 list_for_each(cur, head) {
1009 found = list_entry(cur, struct btrfs_space_info, list);
1010 if (found->flags == flags)
1011 return found;
1012 }
1013 return NULL;
1014
1015}
1016
Chris Mason593060d2008-03-25 16:50:33 -04001017static int update_space_info(struct btrfs_fs_info *info, u64 flags,
1018 u64 total_bytes, u64 bytes_used,
1019 struct btrfs_space_info **space_info)
1020{
1021 struct btrfs_space_info *found;
1022
1023 found = __find_space_info(info, flags);
1024 if (found) {
1025 found->total_bytes += total_bytes;
1026 found->bytes_used += bytes_used;
Chris Mason8f18cf12008-04-25 16:53:30 -04001027 found->full = 0;
Chris Mason593060d2008-03-25 16:50:33 -04001028 WARN_ON(found->total_bytes < found->bytes_used);
1029 *space_info = found;
1030 return 0;
1031 }
1032 found = kmalloc(sizeof(*found), GFP_NOFS);
1033 if (!found)
1034 return -ENOMEM;
1035
1036 list_add(&found->list, &info->space_info);
1037 found->flags = flags;
1038 found->total_bytes = total_bytes;
1039 found->bytes_used = bytes_used;
1040 found->bytes_pinned = 0;
1041 found->full = 0;
1042 *space_info = found;
1043 return 0;
1044}
1045
Chris Mason8790d502008-04-03 16:29:03 -04001046static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
1047{
1048 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
Chris Mason611f0e02008-04-03 16:29:03 -04001049 BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04001050 BTRFS_BLOCK_GROUP_RAID10 |
Chris Mason611f0e02008-04-03 16:29:03 -04001051 BTRFS_BLOCK_GROUP_DUP);
Chris Mason8790d502008-04-03 16:29:03 -04001052 if (extra_flags) {
1053 if (flags & BTRFS_BLOCK_GROUP_DATA)
1054 fs_info->avail_data_alloc_bits |= extra_flags;
1055 if (flags & BTRFS_BLOCK_GROUP_METADATA)
1056 fs_info->avail_metadata_alloc_bits |= extra_flags;
1057 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
1058 fs_info->avail_system_alloc_bits |= extra_flags;
1059 }
1060}
Chris Mason593060d2008-03-25 16:50:33 -04001061
Chris Masonec44a352008-04-28 15:29:52 -04001062static u64 reduce_alloc_profile(u64 flags)
1063{
1064 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
1065 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
1066 BTRFS_BLOCK_GROUP_RAID10)))
1067 flags &= ~BTRFS_BLOCK_GROUP_DUP;
1068
1069 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
1070 (flags & BTRFS_BLOCK_GROUP_RAID10))
1071 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
1072
1073 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
1074 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
1075 (flags & BTRFS_BLOCK_GROUP_RAID10) |
1076 (flags & BTRFS_BLOCK_GROUP_DUP)))
1077 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
1078 return flags;
1079}
1080
1081
Chris Mason6324fbf2008-03-24 15:01:59 -04001082static int do_chunk_alloc(struct btrfs_trans_handle *trans,
1083 struct btrfs_root *extent_root, u64 alloc_bytes,
1084 u64 flags)
1085{
1086 struct btrfs_space_info *space_info;
1087 u64 thresh;
1088 u64 start;
1089 u64 num_bytes;
1090 int ret;
1091
Chris Masonec44a352008-04-28 15:29:52 -04001092 flags = reduce_alloc_profile(flags);
1093
Chris Mason6324fbf2008-03-24 15:01:59 -04001094 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04001095 if (!space_info) {
1096 ret = update_space_info(extent_root->fs_info, flags,
1097 0, 0, &space_info);
1098 BUG_ON(ret);
1099 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001100 BUG_ON(!space_info);
1101
1102 if (space_info->full)
1103 return 0;
1104
Chris Mason8790d502008-04-03 16:29:03 -04001105 thresh = div_factor(space_info->total_bytes, 6);
Chris Mason6324fbf2008-03-24 15:01:59 -04001106 if ((space_info->bytes_used + space_info->bytes_pinned + alloc_bytes) <
1107 thresh)
1108 return 0;
1109
1110 ret = btrfs_alloc_chunk(trans, extent_root, &start, &num_bytes, flags);
1111 if (ret == -ENOSPC) {
1112printk("space info full %Lu\n", flags);
1113 space_info->full = 1;
1114 return 0;
1115 }
1116
1117 BUG_ON(ret);
1118
1119 ret = btrfs_make_block_group(trans, extent_root, 0, flags,
Chris Masone17cade2008-04-15 15:41:47 -04001120 BTRFS_FIRST_CHUNK_TREE_OBJECTID, start, num_bytes);
Chris Mason6324fbf2008-03-24 15:01:59 -04001121 BUG_ON(ret);
Chris Mason593060d2008-03-25 16:50:33 -04001122
Chris Mason6324fbf2008-03-24 15:01:59 -04001123 return 0;
1124}
1125
Chris Mason9078a3e2007-04-26 16:46:15 -04001126static int update_block_group(struct btrfs_trans_handle *trans,
1127 struct btrfs_root *root,
Chris Masondb945352007-10-15 16:15:53 -04001128 u64 bytenr, u64 num_bytes, int alloc,
Chris Mason0b86a832008-03-24 15:01:56 -04001129 int mark_free)
Chris Mason9078a3e2007-04-26 16:46:15 -04001130{
1131 struct btrfs_block_group_cache *cache;
1132 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04001133 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04001134 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04001135 u64 byte_in_group;
Chris Mason96b51792007-10-15 16:15:19 -04001136 u64 start;
1137 u64 end;
Chris Mason3e1ad542007-05-07 20:03:49 -04001138
Chris Mason9078a3e2007-04-26 16:46:15 -04001139 while(total) {
Chris Masondb945352007-10-15 16:15:53 -04001140 cache = btrfs_lookup_block_group(info, bytenr);
Chris Mason3e1ad542007-05-07 20:03:49 -04001141 if (!cache) {
Chris Mason9078a3e2007-04-26 16:46:15 -04001142 return -1;
Chris Masoncd1bc462007-04-27 10:08:34 -04001143 }
Chris Masondb945352007-10-15 16:15:53 -04001144 byte_in_group = bytenr - cache->key.objectid;
1145 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason96b51792007-10-15 16:15:19 -04001146 start = cache->key.objectid;
1147 end = start + cache->key.offset - 1;
1148 set_extent_bits(&info->block_group_cache, start, end,
1149 BLOCK_GROUP_DIRTY, GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04001150
1151 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04001152 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04001153 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04001154 old_val += num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04001155 cache->space_info->bytes_used += num_bytes;
Chris Masoncd1bc462007-04-27 10:08:34 -04001156 } else {
Chris Masondb945352007-10-15 16:15:53 -04001157 old_val -= num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04001158 cache->space_info->bytes_used -= num_bytes;
Chris Masonf510cfe2007-10-15 16:14:48 -04001159 if (mark_free) {
1160 set_extent_dirty(&info->free_space_cache,
Chris Masondb945352007-10-15 16:15:53 -04001161 bytenr, bytenr + num_bytes - 1,
Chris Masonf510cfe2007-10-15 16:14:48 -04001162 GFP_NOFS);
Chris Masone37c9e62007-05-09 20:13:14 -04001163 }
Chris Masoncd1bc462007-04-27 10:08:34 -04001164 }
Chris Mason9078a3e2007-04-26 16:46:15 -04001165 btrfs_set_block_group_used(&cache->item, old_val);
Chris Masondb945352007-10-15 16:15:53 -04001166 total -= num_bytes;
1167 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04001168 }
1169 return 0;
1170}
Chris Mason6324fbf2008-03-24 15:01:59 -04001171
Yan324ae4d2007-11-16 14:57:08 -05001172static int update_pinned_extents(struct btrfs_root *root,
1173 u64 bytenr, u64 num, int pin)
1174{
1175 u64 len;
1176 struct btrfs_block_group_cache *cache;
1177 struct btrfs_fs_info *fs_info = root->fs_info;
1178
1179 if (pin) {
1180 set_extent_dirty(&fs_info->pinned_extents,
1181 bytenr, bytenr + num - 1, GFP_NOFS);
1182 } else {
1183 clear_extent_dirty(&fs_info->pinned_extents,
1184 bytenr, bytenr + num - 1, GFP_NOFS);
1185 }
1186 while (num > 0) {
1187 cache = btrfs_lookup_block_group(fs_info, bytenr);
1188 WARN_ON(!cache);
1189 len = min(num, cache->key.offset -
1190 (bytenr - cache->key.objectid));
1191 if (pin) {
1192 cache->pinned += len;
Chris Mason6324fbf2008-03-24 15:01:59 -04001193 cache->space_info->bytes_pinned += len;
Yan324ae4d2007-11-16 14:57:08 -05001194 fs_info->total_pinned += len;
1195 } else {
1196 cache->pinned -= len;
Chris Mason6324fbf2008-03-24 15:01:59 -04001197 cache->space_info->bytes_pinned -= len;
Yan324ae4d2007-11-16 14:57:08 -05001198 fs_info->total_pinned -= len;
1199 }
1200 bytenr += len;
1201 num -= len;
1202 }
1203 return 0;
1204}
Chris Mason9078a3e2007-04-26 16:46:15 -04001205
Chris Masond1310b22008-01-24 16:13:08 -05001206int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
Chris Masonccd467d2007-06-28 15:57:36 -04001207{
Chris Masonccd467d2007-06-28 15:57:36 -04001208 u64 last = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -04001209 u64 start;
1210 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -05001211 struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
Chris Masonccd467d2007-06-28 15:57:36 -04001212 int ret;
Chris Masonccd467d2007-06-28 15:57:36 -04001213
1214 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001215 ret = find_first_extent_bit(pinned_extents, last,
1216 &start, &end, EXTENT_DIRTY);
1217 if (ret)
Chris Masonccd467d2007-06-28 15:57:36 -04001218 break;
Chris Mason1a5bc162007-10-15 16:15:26 -04001219 set_extent_dirty(copy, start, end, GFP_NOFS);
1220 last = end + 1;
Chris Masonccd467d2007-06-28 15:57:36 -04001221 }
1222 return 0;
1223}
1224
1225int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
1226 struct btrfs_root *root,
Chris Masond1310b22008-01-24 16:13:08 -05001227 struct extent_io_tree *unpin)
Chris Masona28ec192007-03-06 20:08:01 -05001228{
Chris Mason1a5bc162007-10-15 16:15:26 -04001229 u64 start;
1230 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05001231 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05001232 struct extent_io_tree *free_space_cache;
Chris Masonf510cfe2007-10-15 16:14:48 -04001233 free_space_cache = &root->fs_info->free_space_cache;
Chris Masona28ec192007-03-06 20:08:01 -05001234
1235 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001236 ret = find_first_extent_bit(unpin, 0, &start, &end,
1237 EXTENT_DIRTY);
1238 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05001239 break;
Yan324ae4d2007-11-16 14:57:08 -05001240 update_pinned_extents(root, start, end + 1 - start, 0);
Chris Mason1a5bc162007-10-15 16:15:26 -04001241 clear_extent_dirty(unpin, start, end, GFP_NOFS);
1242 set_extent_dirty(free_space_cache, start, end, GFP_NOFS);
Chris Masona28ec192007-03-06 20:08:01 -05001243 }
1244 return 0;
1245}
1246
Chris Mason98ed5172008-01-03 10:01:48 -05001247static int finish_current_insert(struct btrfs_trans_handle *trans,
1248 struct btrfs_root *extent_root)
Chris Mason037e6392007-03-07 11:50:24 -05001249{
Chris Mason7bb86312007-12-11 09:25:06 -05001250 u64 start;
1251 u64 end;
1252 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Masond8d5f3e2007-12-11 12:42:00 -05001253 struct extent_buffer *eb;
Chris Mason7bb86312007-12-11 09:25:06 -05001254 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -04001255 struct btrfs_key ins;
Chris Masond8d5f3e2007-12-11 12:42:00 -05001256 struct btrfs_disk_key first;
Chris Mason234b63a2007-03-13 10:46:10 -04001257 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -05001258 int ret;
Chris Masond8d5f3e2007-12-11 12:42:00 -05001259 int level;
Chris Mason1a5bc162007-10-15 16:15:26 -04001260 int err = 0;
Chris Mason037e6392007-03-07 11:50:24 -05001261
Chris Mason5f39d392007-10-15 16:14:19 -04001262 btrfs_set_stack_extent_refs(&extent_item, 1);
Chris Mason62e27492007-03-15 12:56:47 -04001263 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
Chris Mason7bb86312007-12-11 09:25:06 -05001264 path = btrfs_alloc_path();
Chris Mason037e6392007-03-07 11:50:24 -05001265
Chris Mason26b80032007-08-08 20:17:12 -04001266 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001267 ret = find_first_extent_bit(&info->extent_ins, 0, &start,
1268 &end, EXTENT_LOCKED);
1269 if (ret)
Chris Mason26b80032007-08-08 20:17:12 -04001270 break;
1271
Chris Mason1a5bc162007-10-15 16:15:26 -04001272 ins.objectid = start;
1273 ins.offset = end + 1 - start;
1274 err = btrfs_insert_item(trans, extent_root, &ins,
1275 &extent_item, sizeof(extent_item));
1276 clear_extent_bits(&info->extent_ins, start, end, EXTENT_LOCKED,
1277 GFP_NOFS);
Chris Masond8d5f3e2007-12-11 12:42:00 -05001278 eb = read_tree_block(extent_root, ins.objectid, ins.offset);
1279 level = btrfs_header_level(eb);
1280 if (level == 0) {
1281 btrfs_item_key(eb, &first, 0);
1282 } else {
1283 btrfs_node_key(eb, &first, 0);
1284 }
Chris Mason7bb86312007-12-11 09:25:06 -05001285 err = btrfs_insert_extent_backref(trans, extent_root, path,
1286 start, extent_root->root_key.objectid,
Chris Masonf6dbff52007-12-13 11:13:32 -05001287 0, level,
1288 btrfs_disk_key_objectid(&first));
Chris Mason7bb86312007-12-11 09:25:06 -05001289 BUG_ON(err);
Chris Masond8d5f3e2007-12-11 12:42:00 -05001290 free_extent_buffer(eb);
Chris Mason037e6392007-03-07 11:50:24 -05001291 }
Chris Mason7bb86312007-12-11 09:25:06 -05001292 btrfs_free_path(path);
Chris Mason037e6392007-03-07 11:50:24 -05001293 return 0;
1294}
1295
Chris Masondb945352007-10-15 16:15:53 -04001296static int pin_down_bytes(struct btrfs_root *root, u64 bytenr, u32 num_bytes,
1297 int pending)
Chris Masone20d96d2007-03-22 12:13:20 -04001298{
Chris Mason1a5bc162007-10-15 16:15:26 -04001299 int err = 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001300 struct extent_buffer *buf;
Chris Mason78fae272007-03-25 11:35:08 -04001301
Chris Masonf4b9aa82007-03-27 11:05:53 -04001302 if (!pending) {
Chris Masondb945352007-10-15 16:15:53 -04001303 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
Chris Mason5f39d392007-10-15 16:14:19 -04001304 if (buf) {
1305 if (btrfs_buffer_uptodate(buf)) {
Chris Mason2c90e5d2007-04-02 10:50:19 -04001306 u64 transid =
1307 root->fs_info->running_transaction->transid;
Chris Masondc17ff82008-01-08 15:46:30 -05001308 u64 header_transid =
1309 btrfs_header_generation(buf);
Chris Mason6bc34672008-04-04 15:40:00 -04001310 if (header_transid == transid &&
1311 !btrfs_header_flag(buf,
1312 BTRFS_HEADER_FLAG_WRITTEN)) {
Chris Mason55c69072008-01-09 15:55:33 -05001313 clean_tree_block(NULL, root, buf);
Chris Mason5f39d392007-10-15 16:14:19 -04001314 free_extent_buffer(buf);
Yanc5492282007-11-06 10:25:25 -05001315 return 1;
Chris Mason2c90e5d2007-04-02 10:50:19 -04001316 }
Chris Masonf4b9aa82007-03-27 11:05:53 -04001317 }
Chris Mason5f39d392007-10-15 16:14:19 -04001318 free_extent_buffer(buf);
Chris Mason8ef97622007-03-26 10:15:30 -04001319 }
Yan324ae4d2007-11-16 14:57:08 -05001320 update_pinned_extents(root, bytenr, num_bytes, 1);
Chris Masonf4b9aa82007-03-27 11:05:53 -04001321 } else {
Chris Mason1a5bc162007-10-15 16:15:26 -04001322 set_extent_bits(&root->fs_info->pending_del,
Chris Masondb945352007-10-15 16:15:53 -04001323 bytenr, bytenr + num_bytes - 1,
1324 EXTENT_LOCKED, GFP_NOFS);
Chris Masonf4b9aa82007-03-27 11:05:53 -04001325 }
Chris Masonbe744172007-05-06 10:15:01 -04001326 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -04001327 return 0;
1328}
1329
Chris Masona28ec192007-03-06 20:08:01 -05001330/*
1331 * remove an extent from the root, returns 0 on success
1332 */
Chris Masone089f052007-03-16 16:20:31 -04001333static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason7bb86312007-12-11 09:25:06 -05001334 *root, u64 bytenr, u64 num_bytes,
1335 u64 root_objectid, u64 ref_generation,
1336 u64 owner_objectid, u64 owner_offset, int pin,
Chris Masone37c9e62007-05-09 20:13:14 -04001337 int mark_free)
Chris Masona28ec192007-03-06 20:08:01 -05001338{
Chris Mason5caf2a02007-04-02 11:20:42 -04001339 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -04001340 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -04001341 struct btrfs_fs_info *info = root->fs_info;
1342 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04001343 struct extent_buffer *leaf;
Chris Masona28ec192007-03-06 20:08:01 -05001344 int ret;
Chris Mason952fcca2008-02-18 16:33:44 -05001345 int extent_slot = 0;
1346 int found_extent = 0;
1347 int num_to_del = 1;
Chris Mason234b63a2007-03-13 10:46:10 -04001348 struct btrfs_extent_item *ei;
Chris Masoncf27e1e2007-03-13 09:49:06 -04001349 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -05001350
Chris Masondb945352007-10-15 16:15:53 -04001351 key.objectid = bytenr;
Chris Mason62e27492007-03-15 12:56:47 -04001352 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masondb945352007-10-15 16:15:53 -04001353 key.offset = num_bytes;
Chris Mason5caf2a02007-04-02 11:20:42 -04001354 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001355 if (!path)
1356 return -ENOMEM;
1357
Chris Mason3c12ac72008-04-21 12:01:38 -04001358 path->reada = 1;
Chris Mason7bb86312007-12-11 09:25:06 -05001359 ret = lookup_extent_backref(trans, extent_root, path,
1360 bytenr, root_objectid,
1361 ref_generation,
1362 owner_objectid, owner_offset, 1);
1363 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05001364 struct btrfs_key found_key;
1365 extent_slot = path->slots[0];
1366 while(extent_slot > 0) {
1367 extent_slot--;
1368 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1369 extent_slot);
1370 if (found_key.objectid != bytenr)
1371 break;
1372 if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
1373 found_key.offset == num_bytes) {
1374 found_extent = 1;
1375 break;
1376 }
1377 if (path->slots[0] - extent_slot > 5)
1378 break;
1379 }
1380 if (!found_extent)
1381 ret = btrfs_del_item(trans, extent_root, path);
Chris Mason7bb86312007-12-11 09:25:06 -05001382 } else {
1383 btrfs_print_leaf(extent_root, path->nodes[0]);
1384 WARN_ON(1);
1385 printk("Unable to find ref byte nr %Lu root %Lu "
1386 " gen %Lu owner %Lu offset %Lu\n", bytenr,
1387 root_objectid, ref_generation, owner_objectid,
1388 owner_offset);
1389 }
Chris Mason952fcca2008-02-18 16:33:44 -05001390 if (!found_extent) {
1391 btrfs_release_path(extent_root, path);
1392 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
1393 if (ret < 0)
1394 return ret;
1395 BUG_ON(ret);
1396 extent_slot = path->slots[0];
1397 }
Chris Mason5f39d392007-10-15 16:14:19 -04001398
1399 leaf = path->nodes[0];
Chris Mason952fcca2008-02-18 16:33:44 -05001400 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04001401 struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001402 refs = btrfs_extent_refs(leaf, ei);
1403 BUG_ON(refs == 0);
1404 refs -= 1;
1405 btrfs_set_extent_refs(leaf, ei, refs);
Chris Mason952fcca2008-02-18 16:33:44 -05001406
Chris Mason5f39d392007-10-15 16:14:19 -04001407 btrfs_mark_buffer_dirty(leaf);
1408
Chris Mason952fcca2008-02-18 16:33:44 -05001409 if (refs == 0 && found_extent && path->slots[0] == extent_slot + 1) {
1410 /* if the back ref and the extent are next to each other
1411 * they get deleted below in one shot
1412 */
1413 path->slots[0] = extent_slot;
1414 num_to_del = 2;
1415 } else if (found_extent) {
1416 /* otherwise delete the extent back ref */
1417 ret = btrfs_del_item(trans, extent_root, path);
1418 BUG_ON(ret);
1419 /* if refs are 0, we need to setup the path for deletion */
1420 if (refs == 0) {
1421 btrfs_release_path(extent_root, path);
1422 ret = btrfs_search_slot(trans, extent_root, &key, path,
1423 -1, 1);
1424 if (ret < 0)
1425 return ret;
1426 BUG_ON(ret);
1427 }
1428 }
1429
Chris Masoncf27e1e2007-03-13 09:49:06 -04001430 if (refs == 0) {
Chris Masondb945352007-10-15 16:15:53 -04001431 u64 super_used;
1432 u64 root_used;
Chris Mason78fae272007-03-25 11:35:08 -04001433
1434 if (pin) {
Chris Masondb945352007-10-15 16:15:53 -04001435 ret = pin_down_bytes(root, bytenr, num_bytes, 0);
Yanc5492282007-11-06 10:25:25 -05001436 if (ret > 0)
1437 mark_free = 1;
1438 BUG_ON(ret < 0);
Chris Mason78fae272007-03-25 11:35:08 -04001439 }
1440
Josef Bacik58176a92007-08-29 15:47:34 -04001441 /* block accounting for super block */
Chris Masondb945352007-10-15 16:15:53 -04001442 super_used = btrfs_super_bytes_used(&info->super_copy);
1443 btrfs_set_super_bytes_used(&info->super_copy,
1444 super_used - num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -04001445
1446 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04001447 root_used = btrfs_root_used(&root->root_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001448 btrfs_set_root_used(&root->root_item,
Chris Masondb945352007-10-15 16:15:53 -04001449 root_used - num_bytes);
Chris Mason952fcca2008-02-18 16:33:44 -05001450 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
1451 num_to_del);
Chris Mason54aa1f42007-06-22 14:16:25 -04001452 if (ret) {
1453 return ret;
1454 }
Chris Masondb945352007-10-15 16:15:53 -04001455 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
Chris Mason0b86a832008-03-24 15:01:56 -04001456 mark_free);
Chris Mason9078a3e2007-04-26 16:46:15 -04001457 BUG_ON(ret);
Chris Masona28ec192007-03-06 20:08:01 -05001458 }
Chris Mason5caf2a02007-04-02 11:20:42 -04001459 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -04001460 finish_current_insert(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -05001461 return ret;
1462}
1463
1464/*
Chris Masonfec577f2007-02-26 10:40:21 -05001465 * find all the blocks marked as pending in the radix tree and remove
1466 * them from the extent map
1467 */
Chris Masone089f052007-03-16 16:20:31 -04001468static int del_pending_extents(struct btrfs_trans_handle *trans, struct
1469 btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -05001470{
1471 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -04001472 int err = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -04001473 u64 start;
1474 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -05001475 struct extent_io_tree *pending_del;
1476 struct extent_io_tree *pinned_extents;
Chris Mason8ef97622007-03-26 10:15:30 -04001477
Chris Mason1a5bc162007-10-15 16:15:26 -04001478 pending_del = &extent_root->fs_info->pending_del;
1479 pinned_extents = &extent_root->fs_info->pinned_extents;
Chris Masonfec577f2007-02-26 10:40:21 -05001480
1481 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001482 ret = find_first_extent_bit(pending_del, 0, &start, &end,
1483 EXTENT_LOCKED);
1484 if (ret)
Chris Masonfec577f2007-02-26 10:40:21 -05001485 break;
Yan324ae4d2007-11-16 14:57:08 -05001486 update_pinned_extents(extent_root, start, end + 1 - start, 1);
Chris Mason1a5bc162007-10-15 16:15:26 -04001487 clear_extent_bits(pending_del, start, end, EXTENT_LOCKED,
1488 GFP_NOFS);
1489 ret = __free_extent(trans, extent_root,
Chris Mason7bb86312007-12-11 09:25:06 -05001490 start, end + 1 - start,
1491 extent_root->root_key.objectid,
1492 0, 0, 0, 0, 0);
Chris Mason1a5bc162007-10-15 16:15:26 -04001493 if (ret)
1494 err = ret;
Chris Masonfec577f2007-02-26 10:40:21 -05001495 }
Chris Masone20d96d2007-03-22 12:13:20 -04001496 return err;
Chris Masonfec577f2007-02-26 10:40:21 -05001497}
1498
1499/*
1500 * remove an extent from the root, returns 0 on success
1501 */
Chris Masone089f052007-03-16 16:20:31 -04001502int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason7bb86312007-12-11 09:25:06 -05001503 *root, u64 bytenr, u64 num_bytes,
1504 u64 root_objectid, u64 ref_generation,
1505 u64 owner_objectid, u64 owner_offset, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -05001506{
Chris Mason9f5fae22007-03-20 14:38:32 -04001507 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -05001508 int pending_ret;
1509 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05001510
Chris Masondb945352007-10-15 16:15:53 -04001511 WARN_ON(num_bytes < root->sectorsize);
Chris Mason7bb86312007-12-11 09:25:06 -05001512 if (!root->ref_cows)
1513 ref_generation = 0;
1514
Chris Masona28ec192007-03-06 20:08:01 -05001515 if (root == extent_root) {
Chris Masondb945352007-10-15 16:15:53 -04001516 pin_down_bytes(root, bytenr, num_bytes, 1);
Chris Masona28ec192007-03-06 20:08:01 -05001517 return 0;
1518 }
Chris Mason7bb86312007-12-11 09:25:06 -05001519 ret = __free_extent(trans, root, bytenr, num_bytes, root_objectid,
1520 ref_generation, owner_objectid, owner_offset,
1521 pin, pin == 0);
Chris Masone20d96d2007-03-22 12:13:20 -04001522 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -05001523 return ret ? ret : pending_ret;
1524}
1525
Chris Mason87ee04e2007-11-30 11:30:34 -05001526static u64 stripe_align(struct btrfs_root *root, u64 val)
1527{
1528 u64 mask = ((u64)root->stripesize - 1);
1529 u64 ret = (val + mask) & ~mask;
1530 return ret;
1531}
1532
Chris Masonfec577f2007-02-26 10:40:21 -05001533/*
1534 * walks the btree of allocated extents and find a hole of a given size.
1535 * The key ins is changed to record the hole:
1536 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04001537 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05001538 * ins->offset == number of blocks
1539 * Any available blocks before search_start are skipped.
1540 */
Chris Mason98ed5172008-01-03 10:01:48 -05001541static int noinline find_free_extent(struct btrfs_trans_handle *trans,
1542 struct btrfs_root *orig_root,
1543 u64 num_bytes, u64 empty_size,
1544 u64 search_start, u64 search_end,
1545 u64 hint_byte, struct btrfs_key *ins,
1546 u64 exclude_start, u64 exclude_nr,
1547 int data)
Chris Masonfec577f2007-02-26 10:40:21 -05001548{
Chris Mason87ee04e2007-11-30 11:30:34 -05001549 int ret;
Chris Masonbe744172007-05-06 10:15:01 -04001550 u64 orig_search_start = search_start;
Chris Mason9f5fae22007-03-20 14:38:32 -04001551 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Masonf2458e12007-04-25 15:52:25 -04001552 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04001553 u64 total_needed = num_bytes;
Chris Mason239b14b2008-03-24 15:02:07 -04001554 u64 *last_ptr = NULL;
Chris Masonbe08c1b2007-05-03 09:06:49 -04001555 struct btrfs_block_group_cache *block_group;
Chris Masonbe744172007-05-06 10:15:01 -04001556 int full_scan = 0;
Chris Masonfbdc7622007-05-30 10:22:12 -04001557 int wrapped = 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001558 int empty_cluster = 2 * 1024 * 1024;
Chris Masonfec577f2007-02-26 10:40:21 -05001559
Chris Masondb945352007-10-15 16:15:53 -04001560 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04001561 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
1562
Chris Mason239b14b2008-03-24 15:02:07 -04001563 if (data & BTRFS_BLOCK_GROUP_METADATA) {
1564 last_ptr = &root->fs_info->last_alloc;
Chris Mason8790d502008-04-03 16:29:03 -04001565 empty_cluster = 256 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04001566 }
1567
1568 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD)) {
1569 last_ptr = &root->fs_info->last_data_alloc;
1570 }
1571
1572 if (last_ptr) {
1573 if (*last_ptr)
1574 hint_byte = *last_ptr;
1575 else {
1576 empty_size += empty_cluster;
1577 }
1578 }
1579
Chris Mason7f93bf82008-03-24 15:01:28 -04001580 if (search_end == (u64)-1)
1581 search_end = btrfs_super_total_bytes(&info->super_copy);
Chris Mason0b86a832008-03-24 15:01:56 -04001582
Chris Masondb945352007-10-15 16:15:53 -04001583 if (hint_byte) {
1584 block_group = btrfs_lookup_block_group(info, hint_byte);
Chris Mason1a2b2ac2007-12-04 13:18:24 -05001585 if (!block_group)
1586 hint_byte = search_start;
Chris Masonbe744172007-05-06 10:15:01 -04001587 block_group = btrfs_find_block_group(root, block_group,
Chris Masondb945352007-10-15 16:15:53 -04001588 hint_byte, data, 1);
Chris Mason239b14b2008-03-24 15:02:07 -04001589 if (last_ptr && *last_ptr == 0 && block_group)
1590 hint_byte = block_group->key.objectid;
Chris Masonbe744172007-05-06 10:15:01 -04001591 } else {
1592 block_group = btrfs_find_block_group(root,
Chris Mason1a2b2ac2007-12-04 13:18:24 -05001593 trans->block_group,
1594 search_start, data, 1);
Chris Masonbe744172007-05-06 10:15:01 -04001595 }
Chris Mason239b14b2008-03-24 15:02:07 -04001596 search_start = max(search_start, hint_byte);
Chris Masonbe744172007-05-06 10:15:01 -04001597
Chris Mason6702ed42007-08-07 16:15:09 -04001598 total_needed += empty_size;
Chris Mason0b86a832008-03-24 15:01:56 -04001599
Chris Masonbe744172007-05-06 10:15:01 -04001600check_failed:
Chris Mason70b043f2007-12-13 09:02:46 -05001601 if (!block_group) {
1602 block_group = btrfs_lookup_block_group(info, search_start);
1603 if (!block_group)
1604 block_group = btrfs_lookup_block_group(info,
1605 orig_search_start);
1606 }
Chris Mason0b86a832008-03-24 15:01:56 -04001607 ret = find_search_start(root, &block_group, &search_start,
1608 total_needed, data);
Chris Mason239b14b2008-03-24 15:02:07 -04001609 if (ret == -ENOSPC && last_ptr && *last_ptr) {
1610 *last_ptr = 0;
1611 block_group = btrfs_lookup_block_group(info,
1612 orig_search_start);
1613 search_start = orig_search_start;
1614 ret = find_search_start(root, &block_group, &search_start,
1615 total_needed, data);
1616 }
1617 if (ret == -ENOSPC)
1618 goto enospc;
Chris Mason0b86a832008-03-24 15:01:56 -04001619 if (ret)
1620 goto error;
1621
Chris Mason239b14b2008-03-24 15:02:07 -04001622 if (last_ptr && *last_ptr && search_start != *last_ptr) {
1623 *last_ptr = 0;
1624 if (!empty_size) {
1625 empty_size += empty_cluster;
1626 total_needed += empty_size;
1627 }
1628 block_group = btrfs_lookup_block_group(info,
1629 orig_search_start);
1630 search_start = orig_search_start;
1631 ret = find_search_start(root, &block_group,
1632 &search_start, total_needed, data);
1633 if (ret == -ENOSPC)
1634 goto enospc;
1635 if (ret)
1636 goto error;
1637 }
1638
Chris Mason87ee04e2007-11-30 11:30:34 -05001639 search_start = stripe_align(root, search_start);
Chris Masonfec577f2007-02-26 10:40:21 -05001640 ins->objectid = search_start;
Chris Mason0b86a832008-03-24 15:01:56 -04001641 ins->offset = num_bytes;
Chris Masone37c9e62007-05-09 20:13:14 -04001642
Chris Masondb945352007-10-15 16:15:53 -04001643 if (ins->objectid + num_bytes >= search_end)
Chris Masoncf675822007-09-17 11:00:51 -04001644 goto enospc;
Chris Mason0b86a832008-03-24 15:01:56 -04001645
1646 if (ins->objectid + num_bytes >
1647 block_group->key.objectid + block_group->key.offset) {
Chris Masone19caa52007-10-15 16:17:44 -04001648 search_start = block_group->key.objectid +
1649 block_group->key.offset;
1650 goto new_group;
1651 }
Chris Mason0b86a832008-03-24 15:01:56 -04001652
Chris Mason1a5bc162007-10-15 16:15:26 -04001653 if (test_range_bit(&info->extent_ins, ins->objectid,
Chris Masondb945352007-10-15 16:15:53 -04001654 ins->objectid + num_bytes -1, EXTENT_LOCKED, 0)) {
1655 search_start = ins->objectid + num_bytes;
Chris Mason1a5bc162007-10-15 16:15:26 -04001656 goto new_group;
1657 }
Chris Mason0b86a832008-03-24 15:01:56 -04001658
Chris Mason1a5bc162007-10-15 16:15:26 -04001659 if (test_range_bit(&info->pinned_extents, ins->objectid,
Chris Masondb945352007-10-15 16:15:53 -04001660 ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) {
1661 search_start = ins->objectid + num_bytes;
Chris Mason1a5bc162007-10-15 16:15:26 -04001662 goto new_group;
Chris Masonfec577f2007-02-26 10:40:21 -05001663 }
Chris Mason0b86a832008-03-24 15:01:56 -04001664
Chris Masondb945352007-10-15 16:15:53 -04001665 if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
Chris Masonf2654de2007-06-26 12:20:46 -04001666 ins->objectid < exclude_start + exclude_nr)) {
1667 search_start = exclude_start + exclude_nr;
1668 goto new_group;
1669 }
Chris Mason0b86a832008-03-24 15:01:56 -04001670
Chris Mason6324fbf2008-03-24 15:01:59 -04001671 if (!(data & BTRFS_BLOCK_GROUP_DATA)) {
Chris Mason5276aed2007-06-11 21:33:38 -04001672 block_group = btrfs_lookup_block_group(info, ins->objectid);
Chris Mason26b80032007-08-08 20:17:12 -04001673 if (block_group)
1674 trans->block_group = block_group;
Chris Masoncd1bc462007-04-27 10:08:34 -04001675 }
Chris Masondb945352007-10-15 16:15:53 -04001676 ins->offset = num_bytes;
Chris Mason239b14b2008-03-24 15:02:07 -04001677 if (last_ptr) {
1678 *last_ptr = ins->objectid + ins->offset;
1679 if (*last_ptr ==
1680 btrfs_super_total_bytes(&root->fs_info->super_copy)) {
1681 *last_ptr = 0;
1682 }
1683 }
Chris Masonfec577f2007-02-26 10:40:21 -05001684 return 0;
Chris Masonbe744172007-05-06 10:15:01 -04001685
1686new_group:
Chris Masondb945352007-10-15 16:15:53 -04001687 if (search_start + num_bytes >= search_end) {
Chris Masoncf675822007-09-17 11:00:51 -04001688enospc:
Chris Masonbe744172007-05-06 10:15:01 -04001689 search_start = orig_search_start;
Chris Masonfbdc7622007-05-30 10:22:12 -04001690 if (full_scan) {
1691 ret = -ENOSPC;
1692 goto error;
1693 }
Chris Mason6702ed42007-08-07 16:15:09 -04001694 if (wrapped) {
1695 if (!full_scan)
1696 total_needed -= empty_size;
Chris Masonfbdc7622007-05-30 10:22:12 -04001697 full_scan = 1;
Chris Mason6702ed42007-08-07 16:15:09 -04001698 } else
Chris Masonfbdc7622007-05-30 10:22:12 -04001699 wrapped = 1;
Chris Masonbe744172007-05-06 10:15:01 -04001700 }
Chris Mason5276aed2007-06-11 21:33:38 -04001701 block_group = btrfs_lookup_block_group(info, search_start);
Chris Masonfbdc7622007-05-30 10:22:12 -04001702 cond_resched();
Chris Mason1a2b2ac2007-12-04 13:18:24 -05001703 block_group = btrfs_find_block_group(root, block_group,
1704 search_start, data, 0);
Chris Masonbe744172007-05-06 10:15:01 -04001705 goto check_failed;
1706
Chris Mason0f70abe2007-02-28 16:46:22 -05001707error:
Chris Mason0f70abe2007-02-28 16:46:22 -05001708 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05001709}
Chris Masonec44a352008-04-28 15:29:52 -04001710
Chris Masonfec577f2007-02-26 10:40:21 -05001711/*
Chris Masonfec577f2007-02-26 10:40:21 -05001712 * finds a free extent and does all the dirty work required for allocation
1713 * returns the key for the extent through ins, and a tree buffer for
1714 * the first block of the extent through buf.
1715 *
1716 * returns 0 if everything worked, non-zero otherwise.
1717 */
Chris Mason4d775672007-04-20 20:23:12 -04001718int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
Chris Mason7bb86312007-12-11 09:25:06 -05001719 struct btrfs_root *root,
Chris Mason98d20f62008-04-14 09:46:10 -04001720 u64 num_bytes, u64 min_alloc_size,
1721 u64 root_objectid, u64 ref_generation,
Chris Mason7bb86312007-12-11 09:25:06 -05001722 u64 owner, u64 owner_offset,
1723 u64 empty_size, u64 hint_byte,
Chris Masonec44a352008-04-28 15:29:52 -04001724 u64 search_end, struct btrfs_key *ins, u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05001725{
1726 int ret;
1727 int pending_ret;
Chris Masonc31f8832008-01-08 15:46:31 -05001728 u64 super_used;
1729 u64 root_used;
Chris Masonfbdc7622007-05-30 10:22:12 -04001730 u64 search_start = 0;
Chris Mason8790d502008-04-03 16:29:03 -04001731 u64 alloc_profile;
Chris Mason47e4bb92008-02-01 14:51:59 -05001732 u32 sizes[2];
Chris Mason1261ec42007-03-20 20:35:03 -04001733 struct btrfs_fs_info *info = root->fs_info;
1734 struct btrfs_root *extent_root = info->extent_root;
Chris Mason47e4bb92008-02-01 14:51:59 -05001735 struct btrfs_extent_item *extent_item;
1736 struct btrfs_extent_ref *ref;
Chris Mason7bb86312007-12-11 09:25:06 -05001737 struct btrfs_path *path;
Chris Mason47e4bb92008-02-01 14:51:59 -05001738 struct btrfs_key keys[2];
Chris Mason8f662a72008-01-02 10:01:11 -05001739
Chris Mason6324fbf2008-03-24 15:01:59 -04001740 if (data) {
Chris Mason8790d502008-04-03 16:29:03 -04001741 alloc_profile = info->avail_data_alloc_bits &
1742 info->data_alloc_profile;
1743 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04001744 } else if (root == root->fs_info->chunk_root) {
Chris Mason8790d502008-04-03 16:29:03 -04001745 alloc_profile = info->avail_system_alloc_bits &
1746 info->system_alloc_profile;
1747 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04001748 } else {
Chris Mason8790d502008-04-03 16:29:03 -04001749 alloc_profile = info->avail_metadata_alloc_bits &
1750 info->metadata_alloc_profile;
1751 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04001752 }
Chris Mason98d20f62008-04-14 09:46:10 -04001753again:
Chris Masonec44a352008-04-28 15:29:52 -04001754 data = reduce_alloc_profile(data);
Chris Mason3bf3d9e2008-04-26 11:03:32 -04001755 if (root->ref_cows) {
Chris Mason593060d2008-03-25 16:50:33 -04001756 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
Chris Mason6324fbf2008-03-24 15:01:59 -04001757 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason239b14b2008-03-24 15:02:07 -04001758 2 * 1024 * 1024,
Chris Mason593060d2008-03-25 16:50:33 -04001759 BTRFS_BLOCK_GROUP_METADATA |
Chris Mason8790d502008-04-03 16:29:03 -04001760 (info->metadata_alloc_profile &
1761 info->avail_metadata_alloc_bits));
Chris Mason6324fbf2008-03-24 15:01:59 -04001762 BUG_ON(ret);
1763 }
1764 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason8790d502008-04-03 16:29:03 -04001765 num_bytes + 2 * 1024 * 1024, data);
Chris Mason6324fbf2008-03-24 15:01:59 -04001766 BUG_ON(ret);
1767 }
Chris Mason0b86a832008-03-24 15:01:56 -04001768
Chris Masondb945352007-10-15 16:15:53 -04001769 WARN_ON(num_bytes < root->sectorsize);
1770 ret = find_free_extent(trans, root, num_bytes, empty_size,
1771 search_start, search_end, hint_byte, ins,
Chris Mason26b80032007-08-08 20:17:12 -04001772 trans->alloc_exclude_start,
1773 trans->alloc_exclude_nr, data);
Chris Mason3b951512008-04-17 11:29:12 -04001774
Chris Mason98d20f62008-04-14 09:46:10 -04001775 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
1776 num_bytes = num_bytes >> 1;
1777 num_bytes = max(num_bytes, min_alloc_size);
1778 goto again;
1779 }
Chris Masonec44a352008-04-28 15:29:52 -04001780 if (ret) {
1781 printk("allocation failed flags %Lu\n", data);
1782 }
Chris Masonccd467d2007-06-28 15:57:36 -04001783 BUG_ON(ret);
Chris Masonf2654de2007-06-26 12:20:46 -04001784 if (ret)
1785 return ret;
1786
Josef Bacik58176a92007-08-29 15:47:34 -04001787 /* block accounting for super block */
Chris Masondb945352007-10-15 16:15:53 -04001788 super_used = btrfs_super_bytes_used(&info->super_copy);
1789 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
Chris Mason26b80032007-08-08 20:17:12 -04001790
Josef Bacik58176a92007-08-29 15:47:34 -04001791 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04001792 root_used = btrfs_root_used(&root->root_item);
1793 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -04001794
Chris Masonf510cfe2007-10-15 16:14:48 -04001795 clear_extent_dirty(&root->fs_info->free_space_cache,
1796 ins->objectid, ins->objectid + ins->offset - 1,
1797 GFP_NOFS);
1798
Chris Mason26b80032007-08-08 20:17:12 -04001799 if (root == extent_root) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001800 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
1801 ins->objectid + ins->offset - 1,
1802 EXTENT_LOCKED, GFP_NOFS);
Chris Mason26b80032007-08-08 20:17:12 -04001803 goto update_block;
1804 }
1805
1806 WARN_ON(trans->alloc_exclude_nr);
1807 trans->alloc_exclude_start = ins->objectid;
1808 trans->alloc_exclude_nr = ins->offset;
Chris Mason037e6392007-03-07 11:50:24 -05001809
Chris Mason47e4bb92008-02-01 14:51:59 -05001810 memcpy(&keys[0], ins, sizeof(*ins));
1811 keys[1].offset = hash_extent_ref(root_objectid, ref_generation,
1812 owner, owner_offset);
1813 keys[1].objectid = ins->objectid;
1814 keys[1].type = BTRFS_EXTENT_REF_KEY;
1815 sizes[0] = sizeof(*extent_item);
1816 sizes[1] = sizeof(*ref);
Chris Mason7bb86312007-12-11 09:25:06 -05001817
1818 path = btrfs_alloc_path();
1819 BUG_ON(!path);
Chris Mason47e4bb92008-02-01 14:51:59 -05001820
1821 ret = btrfs_insert_empty_items(trans, extent_root, path, keys,
1822 sizes, 2);
Chris Mason26b80032007-08-08 20:17:12 -04001823
Chris Masonccd467d2007-06-28 15:57:36 -04001824 BUG_ON(ret);
Chris Mason47e4bb92008-02-01 14:51:59 -05001825 extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1826 struct btrfs_extent_item);
1827 btrfs_set_extent_refs(path->nodes[0], extent_item, 1);
1828 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
1829 struct btrfs_extent_ref);
1830
1831 btrfs_set_ref_root(path->nodes[0], ref, root_objectid);
1832 btrfs_set_ref_generation(path->nodes[0], ref, ref_generation);
1833 btrfs_set_ref_objectid(path->nodes[0], ref, owner);
1834 btrfs_set_ref_offset(path->nodes[0], ref, owner_offset);
1835
1836 btrfs_mark_buffer_dirty(path->nodes[0]);
1837
1838 trans->alloc_exclude_start = 0;
1839 trans->alloc_exclude_nr = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05001840 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -04001841 finish_current_insert(trans, extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -04001842 pending_ret = del_pending_extents(trans, extent_root);
Chris Masonf510cfe2007-10-15 16:14:48 -04001843
Chris Masone37c9e62007-05-09 20:13:14 -04001844 if (ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001845 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001846 }
1847 if (pending_ret) {
Chris Mason037e6392007-03-07 11:50:24 -05001848 return pending_ret;
Chris Masone37c9e62007-05-09 20:13:14 -04001849 }
Chris Mason26b80032007-08-08 20:17:12 -04001850
1851update_block:
Chris Mason0b86a832008-03-24 15:01:56 -04001852 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0);
Chris Masonf5947062008-02-04 10:10:13 -05001853 if (ret) {
1854 printk("update block group failed for %Lu %Lu\n",
1855 ins->objectid, ins->offset);
1856 BUG();
1857 }
Chris Mason037e6392007-03-07 11:50:24 -05001858 return 0;
Chris Masonfec577f2007-02-26 10:40:21 -05001859}
1860
1861/*
1862 * helper function to allocate a block for a given tree
1863 * returns the tree buffer or NULL.
1864 */
Chris Mason5f39d392007-10-15 16:14:19 -04001865struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Masondb945352007-10-15 16:15:53 -04001866 struct btrfs_root *root,
Chris Mason7bb86312007-12-11 09:25:06 -05001867 u32 blocksize,
1868 u64 root_objectid, u64 hint,
1869 u64 empty_size)
1870{
1871 u64 ref_generation;
1872
1873 if (root->ref_cows)
1874 ref_generation = trans->transid;
1875 else
1876 ref_generation = 0;
1877
1878
1879 return __btrfs_alloc_free_block(trans, root, blocksize, root_objectid,
1880 ref_generation, 0, 0, hint, empty_size);
1881}
1882
1883/*
1884 * helper function to allocate a block for a given tree
1885 * returns the tree buffer or NULL.
1886 */
1887struct extent_buffer *__btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
1888 struct btrfs_root *root,
1889 u32 blocksize,
1890 u64 root_objectid,
1891 u64 ref_generation,
1892 u64 first_objectid,
1893 int level,
1894 u64 hint,
Chris Mason5f39d392007-10-15 16:14:19 -04001895 u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05001896{
Chris Masone2fa7222007-03-12 16:22:34 -04001897 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05001898 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04001899 struct extent_buffer *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05001900
Chris Mason98d20f62008-04-14 09:46:10 -04001901 ret = btrfs_alloc_extent(trans, root, blocksize, blocksize,
Chris Mason7bb86312007-12-11 09:25:06 -05001902 root_objectid, ref_generation,
Chris Masonf6dbff52007-12-13 11:13:32 -05001903 level, first_objectid, empty_size, hint,
Chris Masondb945352007-10-15 16:15:53 -04001904 (u64)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05001905 if (ret) {
Chris Mason54aa1f42007-06-22 14:16:25 -04001906 BUG_ON(ret > 0);
1907 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05001908 }
Chris Masondb945352007-10-15 16:15:53 -04001909 buf = btrfs_find_create_tree_block(root, ins.objectid, blocksize);
Chris Mason54aa1f42007-06-22 14:16:25 -04001910 if (!buf) {
Chris Mason7bb86312007-12-11 09:25:06 -05001911 btrfs_free_extent(trans, root, ins.objectid, blocksize,
1912 root->root_key.objectid, ref_generation,
1913 0, 0, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001914 return ERR_PTR(-ENOMEM);
1915 }
Chris Mason55c69072008-01-09 15:55:33 -05001916 btrfs_set_header_generation(buf, trans->transid);
1917 clean_tree_block(trans, root, buf);
Chris Mason5f39d392007-10-15 16:14:19 -04001918 btrfs_set_buffer_uptodate(buf);
Chris Mason55c69072008-01-09 15:55:33 -05001919
1920 if (PageDirty(buf->first_page)) {
1921 printk("page %lu dirty\n", buf->first_page->index);
1922 WARN_ON(1);
1923 }
1924
Chris Mason5f39d392007-10-15 16:14:19 -04001925 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
1926 buf->start + buf->len - 1, GFP_NOFS);
Chris Mason9afbb0b2008-02-15 13:19:35 -05001927 if (!btrfs_test_opt(root, SSD))
1928 btrfs_set_buffer_defrag(buf);
Chris Masond3c2fdc2007-09-17 10:58:06 -04001929 trans->blocks_used++;
Chris Masonfec577f2007-02-26 10:40:21 -05001930 return buf;
1931}
Chris Masona28ec192007-03-06 20:08:01 -05001932
Chris Mason98ed5172008-01-03 10:01:48 -05001933static int noinline drop_leaf_ref(struct btrfs_trans_handle *trans,
1934 struct btrfs_root *root,
1935 struct extent_buffer *leaf)
Chris Mason6407bf62007-03-27 06:33:00 -04001936{
Chris Mason7bb86312007-12-11 09:25:06 -05001937 u64 leaf_owner;
1938 u64 leaf_generation;
Chris Mason5f39d392007-10-15 16:14:19 -04001939 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04001940 struct btrfs_file_extent_item *fi;
1941 int i;
1942 int nritems;
1943 int ret;
1944
Chris Mason5f39d392007-10-15 16:14:19 -04001945 BUG_ON(!btrfs_is_leaf(leaf));
1946 nritems = btrfs_header_nritems(leaf);
Chris Mason7bb86312007-12-11 09:25:06 -05001947 leaf_owner = btrfs_header_owner(leaf);
1948 leaf_generation = btrfs_header_generation(leaf);
1949
Chris Mason6407bf62007-03-27 06:33:00 -04001950 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04001951 u64 disk_bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -04001952
1953 btrfs_item_key_to_cpu(leaf, &key, i);
1954 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -04001955 continue;
1956 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001957 if (btrfs_file_extent_type(leaf, fi) ==
1958 BTRFS_FILE_EXTENT_INLINE)
Chris Mason236454d2007-04-19 13:37:44 -04001959 continue;
Chris Mason6407bf62007-03-27 06:33:00 -04001960 /*
1961 * FIXME make sure to insert a trans record that
1962 * repeats the snapshot del on crash
1963 */
Chris Masondb945352007-10-15 16:15:53 -04001964 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1965 if (disk_bytenr == 0)
Chris Mason3a686372007-05-24 13:35:57 -04001966 continue;
Chris Masondb945352007-10-15 16:15:53 -04001967 ret = btrfs_free_extent(trans, root, disk_bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -05001968 btrfs_file_extent_disk_num_bytes(leaf, fi),
1969 leaf_owner, leaf_generation,
1970 key.objectid, key.offset, 0);
Chris Mason6407bf62007-03-27 06:33:00 -04001971 BUG_ON(ret);
1972 }
1973 return 0;
1974}
1975
Chris Mason98ed5172008-01-03 10:01:48 -05001976static void noinline reada_walk_down(struct btrfs_root *root,
Chris Masonbea495e2008-01-24 16:13:14 -05001977 struct extent_buffer *node,
1978 int slot)
Chris Masone0115992007-06-19 16:23:05 -04001979{
Chris Masondb945352007-10-15 16:15:53 -04001980 u64 bytenr;
Chris Masonbea495e2008-01-24 16:13:14 -05001981 u64 last = 0;
1982 u32 nritems;
Chris Masone0115992007-06-19 16:23:05 -04001983 u32 refs;
Chris Masondb945352007-10-15 16:15:53 -04001984 u32 blocksize;
Chris Masonbea495e2008-01-24 16:13:14 -05001985 int ret;
1986 int i;
1987 int level;
1988 int skipped = 0;
Chris Masone0115992007-06-19 16:23:05 -04001989
Chris Mason5f39d392007-10-15 16:14:19 -04001990 nritems = btrfs_header_nritems(node);
Chris Masondb945352007-10-15 16:15:53 -04001991 level = btrfs_header_level(node);
Chris Masonbea495e2008-01-24 16:13:14 -05001992 if (level)
1993 return;
1994
1995 for (i = slot; i < nritems && skipped < 32; i++) {
Chris Masondb945352007-10-15 16:15:53 -04001996 bytenr = btrfs_node_blockptr(node, i);
Chris Masonbea495e2008-01-24 16:13:14 -05001997 if (last && ((bytenr > last && bytenr - last > 32 * 1024) ||
1998 (last > bytenr && last - bytenr > 32 * 1024))) {
1999 skipped++;
Chris Masone0115992007-06-19 16:23:05 -04002000 continue;
Chris Masonbea495e2008-01-24 16:13:14 -05002001 }
2002 blocksize = btrfs_level_size(root, level - 1);
2003 if (i != slot) {
2004 ret = lookup_extent_ref(NULL, root, bytenr,
2005 blocksize, &refs);
2006 BUG_ON(ret);
2007 if (refs != 1) {
2008 skipped++;
2009 continue;
2010 }
2011 }
Chris Mason409eb952007-08-08 20:17:12 -04002012 mutex_unlock(&root->fs_info->fs_mutex);
Chris Masondb945352007-10-15 16:15:53 -04002013 ret = readahead_tree_block(root, bytenr, blocksize);
Chris Masonbea495e2008-01-24 16:13:14 -05002014 last = bytenr + blocksize;
Chris Mason409eb952007-08-08 20:17:12 -04002015 cond_resched();
2016 mutex_lock(&root->fs_info->fs_mutex);
Chris Masone0115992007-06-19 16:23:05 -04002017 if (ret)
2018 break;
2019 }
2020}
2021
Chris Mason9aca1d52007-03-13 11:09:37 -04002022/*
2023 * helper function for drop_snapshot, this walks down the tree dropping ref
2024 * counts as it goes.
2025 */
Chris Mason98ed5172008-01-03 10:01:48 -05002026static int noinline walk_down_tree(struct btrfs_trans_handle *trans,
2027 struct btrfs_root *root,
2028 struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05002029{
Chris Mason7bb86312007-12-11 09:25:06 -05002030 u64 root_owner;
2031 u64 root_gen;
2032 u64 bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -04002033 struct extent_buffer *next;
2034 struct extent_buffer *cur;
Chris Mason7bb86312007-12-11 09:25:06 -05002035 struct extent_buffer *parent;
Chris Masondb945352007-10-15 16:15:53 -04002036 u32 blocksize;
Chris Mason20524f02007-03-10 06:35:47 -05002037 int ret;
2038 u32 refs;
2039
Chris Mason5caf2a02007-04-02 11:20:42 -04002040 WARN_ON(*level < 0);
2041 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason5f39d392007-10-15 16:14:19 -04002042 ret = lookup_extent_ref(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04002043 path->nodes[*level]->start,
2044 path->nodes[*level]->len, &refs);
Chris Mason20524f02007-03-10 06:35:47 -05002045 BUG_ON(ret);
2046 if (refs > 1)
2047 goto out;
Chris Masone0115992007-06-19 16:23:05 -04002048
Chris Mason9aca1d52007-03-13 11:09:37 -04002049 /*
2050 * walk down to the last node level and free all the leaves
2051 */
Chris Mason6407bf62007-03-27 06:33:00 -04002052 while(*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04002053 WARN_ON(*level < 0);
2054 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -05002055 cur = path->nodes[*level];
Chris Masone0115992007-06-19 16:23:05 -04002056
Chris Mason5f39d392007-10-15 16:14:19 -04002057 if (btrfs_header_level(cur) != *level)
Chris Mason2c90e5d2007-04-02 10:50:19 -04002058 WARN_ON(1);
Chris Masone0115992007-06-19 16:23:05 -04002059
Chris Mason7518a232007-03-12 12:01:18 -04002060 if (path->slots[*level] >=
Chris Mason5f39d392007-10-15 16:14:19 -04002061 btrfs_header_nritems(cur))
Chris Mason20524f02007-03-10 06:35:47 -05002062 break;
Chris Mason6407bf62007-03-27 06:33:00 -04002063 if (*level == 0) {
2064 ret = drop_leaf_ref(trans, root, cur);
2065 BUG_ON(ret);
2066 break;
2067 }
Chris Masondb945352007-10-15 16:15:53 -04002068 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
2069 blocksize = btrfs_level_size(root, *level - 1);
2070 ret = lookup_extent_ref(trans, root, bytenr, blocksize, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -04002071 BUG_ON(ret);
2072 if (refs != 1) {
Chris Mason7bb86312007-12-11 09:25:06 -05002073 parent = path->nodes[*level];
2074 root_owner = btrfs_header_owner(parent);
2075 root_gen = btrfs_header_generation(parent);
Chris Mason20524f02007-03-10 06:35:47 -05002076 path->slots[*level]++;
Chris Masondb945352007-10-15 16:15:53 -04002077 ret = btrfs_free_extent(trans, root, bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -05002078 blocksize, root_owner,
2079 root_gen, 0, 0, 1);
Chris Mason20524f02007-03-10 06:35:47 -05002080 BUG_ON(ret);
2081 continue;
2082 }
Chris Masondb945352007-10-15 16:15:53 -04002083 next = btrfs_find_tree_block(root, bytenr, blocksize);
Chris Mason5f39d392007-10-15 16:14:19 -04002084 if (!next || !btrfs_buffer_uptodate(next)) {
2085 free_extent_buffer(next);
Chris Masonbea495e2008-01-24 16:13:14 -05002086 reada_walk_down(root, cur, path->slots[*level]);
Chris Masone9d0b132007-08-10 14:06:19 -04002087
Chris Mason8790d502008-04-03 16:29:03 -04002088 mutex_unlock(&root->fs_info->fs_mutex);
2089 next = read_tree_block(root, bytenr, blocksize);
2090 mutex_lock(&root->fs_info->fs_mutex);
2091
2092 /* we've dropped the lock, double check */
Chris Masondb945352007-10-15 16:15:53 -04002093 ret = lookup_extent_ref(trans, root, bytenr,
2094 blocksize, &refs);
Chris Masone9d0b132007-08-10 14:06:19 -04002095 BUG_ON(ret);
2096 if (refs != 1) {
Chris Mason7bb86312007-12-11 09:25:06 -05002097 parent = path->nodes[*level];
2098 root_owner = btrfs_header_owner(parent);
2099 root_gen = btrfs_header_generation(parent);
2100
Chris Masone9d0b132007-08-10 14:06:19 -04002101 path->slots[*level]++;
Chris Mason5f39d392007-10-15 16:14:19 -04002102 free_extent_buffer(next);
Chris Mason7bb86312007-12-11 09:25:06 -05002103 ret = btrfs_free_extent(trans, root, bytenr,
2104 blocksize,
2105 root_owner,
2106 root_gen, 0, 0, 1);
Chris Masone9d0b132007-08-10 14:06:19 -04002107 BUG_ON(ret);
2108 continue;
2109 }
Chris Mason0999df52008-04-01 13:48:14 -04002110 } else if (next) {
2111 btrfs_verify_block_csum(root, next);
Chris Masone9d0b132007-08-10 14:06:19 -04002112 }
Chris Mason5caf2a02007-04-02 11:20:42 -04002113 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -04002114 if (path->nodes[*level-1])
Chris Mason5f39d392007-10-15 16:14:19 -04002115 free_extent_buffer(path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -05002116 path->nodes[*level-1] = next;
Chris Mason5f39d392007-10-15 16:14:19 -04002117 *level = btrfs_header_level(next);
Chris Mason20524f02007-03-10 06:35:47 -05002118 path->slots[*level] = 0;
2119 }
2120out:
Chris Mason5caf2a02007-04-02 11:20:42 -04002121 WARN_ON(*level < 0);
2122 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason7bb86312007-12-11 09:25:06 -05002123
2124 if (path->nodes[*level] == root->node) {
2125 root_owner = root->root_key.objectid;
2126 parent = path->nodes[*level];
2127 } else {
2128 parent = path->nodes[*level + 1];
2129 root_owner = btrfs_header_owner(parent);
2130 }
2131
2132 root_gen = btrfs_header_generation(parent);
Chris Masondb945352007-10-15 16:15:53 -04002133 ret = btrfs_free_extent(trans, root, path->nodes[*level]->start,
Chris Mason7bb86312007-12-11 09:25:06 -05002134 path->nodes[*level]->len,
2135 root_owner, root_gen, 0, 0, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002136 free_extent_buffer(path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -05002137 path->nodes[*level] = NULL;
2138 *level += 1;
2139 BUG_ON(ret);
2140 return 0;
2141}
2142
Chris Mason9aca1d52007-03-13 11:09:37 -04002143/*
2144 * helper for dropping snapshots. This walks back up the tree in the path
2145 * to find the first node higher up where we haven't yet gone through
2146 * all the slots
2147 */
Chris Mason98ed5172008-01-03 10:01:48 -05002148static int noinline walk_up_tree(struct btrfs_trans_handle *trans,
2149 struct btrfs_root *root,
2150 struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05002151{
Chris Mason7bb86312007-12-11 09:25:06 -05002152 u64 root_owner;
2153 u64 root_gen;
2154 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05002155 int i;
2156 int slot;
2157 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04002158
Chris Mason234b63a2007-03-13 10:46:10 -04002159 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -05002160 slot = path->slots[i];
Chris Mason5f39d392007-10-15 16:14:19 -04002161 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
2162 struct extent_buffer *node;
2163 struct btrfs_disk_key disk_key;
2164 node = path->nodes[i];
Chris Mason20524f02007-03-10 06:35:47 -05002165 path->slots[i]++;
2166 *level = i;
Chris Mason9f3a7422007-08-07 15:52:19 -04002167 WARN_ON(*level == 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002168 btrfs_node_key(node, &disk_key, path->slots[i]);
Chris Mason9f3a7422007-08-07 15:52:19 -04002169 memcpy(&root_item->drop_progress,
Chris Mason5f39d392007-10-15 16:14:19 -04002170 &disk_key, sizeof(disk_key));
Chris Mason9f3a7422007-08-07 15:52:19 -04002171 root_item->drop_level = i;
Chris Mason20524f02007-03-10 06:35:47 -05002172 return 0;
2173 } else {
Chris Mason7bb86312007-12-11 09:25:06 -05002174 if (path->nodes[*level] == root->node) {
2175 root_owner = root->root_key.objectid;
2176 root_gen =
2177 btrfs_header_generation(path->nodes[*level]);
2178 } else {
2179 struct extent_buffer *node;
2180 node = path->nodes[*level + 1];
2181 root_owner = btrfs_header_owner(node);
2182 root_gen = btrfs_header_generation(node);
2183 }
Chris Masone089f052007-03-16 16:20:31 -04002184 ret = btrfs_free_extent(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04002185 path->nodes[*level]->start,
Chris Mason7bb86312007-12-11 09:25:06 -05002186 path->nodes[*level]->len,
2187 root_owner, root_gen, 0, 0, 1);
Chris Mason6407bf62007-03-27 06:33:00 -04002188 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04002189 free_extent_buffer(path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -04002190 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -05002191 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -05002192 }
2193 }
2194 return 1;
2195}
2196
Chris Mason9aca1d52007-03-13 11:09:37 -04002197/*
2198 * drop the reference count on the tree rooted at 'snap'. This traverses
2199 * the tree freeing any blocks that have a ref count of zero after being
2200 * decremented.
2201 */
Chris Masone089f052007-03-16 16:20:31 -04002202int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason9f3a7422007-08-07 15:52:19 -04002203 *root)
Chris Mason20524f02007-03-10 06:35:47 -05002204{
Chris Mason3768f362007-03-13 16:47:54 -04002205 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -04002206 int wret;
Chris Mason20524f02007-03-10 06:35:47 -05002207 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -04002208 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -05002209 int i;
2210 int orig_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04002211 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05002212
Chris Mason5caf2a02007-04-02 11:20:42 -04002213 path = btrfs_alloc_path();
2214 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05002215
Chris Mason5f39d392007-10-15 16:14:19 -04002216 level = btrfs_header_level(root->node);
Chris Mason20524f02007-03-10 06:35:47 -05002217 orig_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04002218 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
2219 path->nodes[level] = root->node;
Chris Masonf510cfe2007-10-15 16:14:48 -04002220 extent_buffer_get(root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -04002221 path->slots[level] = 0;
2222 } else {
2223 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04002224 struct btrfs_disk_key found_key;
2225 struct extent_buffer *node;
Chris Mason6702ed42007-08-07 16:15:09 -04002226
Chris Mason9f3a7422007-08-07 15:52:19 -04002227 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Chris Mason6702ed42007-08-07 16:15:09 -04002228 level = root_item->drop_level;
2229 path->lowest_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04002230 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason6702ed42007-08-07 16:15:09 -04002231 if (wret < 0) {
Chris Mason9f3a7422007-08-07 15:52:19 -04002232 ret = wret;
2233 goto out;
2234 }
Chris Mason5f39d392007-10-15 16:14:19 -04002235 node = path->nodes[level];
2236 btrfs_node_key(node, &found_key, path->slots[level]);
2237 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
2238 sizeof(found_key)));
Chris Mason9f3a7422007-08-07 15:52:19 -04002239 }
Chris Mason20524f02007-03-10 06:35:47 -05002240 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -04002241 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04002242 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05002243 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04002244 if (wret < 0)
2245 ret = wret;
2246
Chris Mason5caf2a02007-04-02 11:20:42 -04002247 wret = walk_up_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04002248 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05002249 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04002250 if (wret < 0)
2251 ret = wret;
Chris Mason409eb952007-08-08 20:17:12 -04002252 ret = -EAGAIN;
Chris Mason409eb952007-08-08 20:17:12 -04002253 break;
Chris Mason20524f02007-03-10 06:35:47 -05002254 }
Chris Mason83e15a22007-03-12 09:03:27 -04002255 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -04002256 if (path->nodes[i]) {
Chris Mason5f39d392007-10-15 16:14:19 -04002257 free_extent_buffer(path->nodes[i]);
Chris Mason0f827312007-10-15 16:18:56 -04002258 path->nodes[i] = NULL;
Chris Mason83e15a22007-03-12 09:03:27 -04002259 }
Chris Mason20524f02007-03-10 06:35:47 -05002260 }
Chris Mason9f3a7422007-08-07 15:52:19 -04002261out:
Chris Mason5caf2a02007-04-02 11:20:42 -04002262 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -04002263 return ret;
Chris Mason20524f02007-03-10 06:35:47 -05002264}
Chris Mason9078a3e2007-04-26 16:46:15 -04002265
Chris Masonbe744172007-05-06 10:15:01 -04002266int btrfs_free_block_groups(struct btrfs_fs_info *info)
2267{
Chris Masonf510cfe2007-10-15 16:14:48 -04002268 u64 start;
2269 u64 end;
Yanb97f9202007-11-01 11:28:41 -04002270 u64 ptr;
Chris Mason96b51792007-10-15 16:15:19 -04002271 int ret;
Chris Mason96b51792007-10-15 16:15:19 -04002272 while(1) {
2273 ret = find_first_extent_bit(&info->block_group_cache, 0,
2274 &start, &end, (unsigned int)-1);
2275 if (ret)
2276 break;
Yanb97f9202007-11-01 11:28:41 -04002277 ret = get_state_private(&info->block_group_cache, start, &ptr);
2278 if (!ret)
2279 kfree((void *)(unsigned long)ptr);
Chris Mason96b51792007-10-15 16:15:19 -04002280 clear_extent_bits(&info->block_group_cache, start,
2281 end, (unsigned int)-1, GFP_NOFS);
2282 }
Chris Masone37c9e62007-05-09 20:13:14 -04002283 while(1) {
Chris Masonf510cfe2007-10-15 16:14:48 -04002284 ret = find_first_extent_bit(&info->free_space_cache, 0,
2285 &start, &end, EXTENT_DIRTY);
2286 if (ret)
Chris Masone37c9e62007-05-09 20:13:14 -04002287 break;
Chris Masonf510cfe2007-10-15 16:14:48 -04002288 clear_extent_dirty(&info->free_space_cache, start,
2289 end, GFP_NOFS);
Chris Masone37c9e62007-05-09 20:13:14 -04002290 }
Chris Masonbe744172007-05-06 10:15:01 -04002291 return 0;
2292}
2293
Chris Mason8e7bf942008-04-28 09:02:36 -04002294static unsigned long calc_ra(unsigned long start, unsigned long last,
2295 unsigned long nr)
2296{
2297 return min(last, start + nr - 1);
2298}
2299
Chris Mason98ed5172008-01-03 10:01:48 -05002300static int noinline relocate_inode_pages(struct inode *inode, u64 start,
2301 u64 len)
Chris Masonedbd8d42007-12-21 16:27:24 -05002302{
2303 u64 page_start;
2304 u64 page_end;
Chris Masonedbd8d42007-12-21 16:27:24 -05002305 unsigned long last_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05002306 unsigned long i;
2307 struct page *page;
Chris Masond1310b22008-01-24 16:13:08 -05002308 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason4313b392008-01-03 09:08:48 -05002309 struct file_ra_state *ra;
Chris Mason8e7bf942008-04-28 09:02:36 -04002310 unsigned long total_read = 0;
2311 unsigned long ra_pages;
Chris Mason4313b392008-01-03 09:08:48 -05002312
2313 ra = kzalloc(sizeof(*ra), GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05002314
2315 mutex_lock(&inode->i_mutex);
Chris Mason4313b392008-01-03 09:08:48 -05002316 i = start >> PAGE_CACHE_SHIFT;
Chris Masonedbd8d42007-12-21 16:27:24 -05002317 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
2318
Chris Mason8e7bf942008-04-28 09:02:36 -04002319 ra_pages = BTRFS_I(inode)->root->fs_info->bdi.ra_pages;
2320
Chris Mason4313b392008-01-03 09:08:48 -05002321 file_ra_state_init(ra, inode->i_mapping);
Chris Masonedbd8d42007-12-21 16:27:24 -05002322
Chris Mason4313b392008-01-03 09:08:48 -05002323 for (; i <= last_index; i++) {
Chris Mason8e7bf942008-04-28 09:02:36 -04002324 if (total_read % ra_pages == 0) {
2325 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
2326 calc_ra(i, last_index, ra_pages));
2327 }
2328 total_read++;
Chris Masonedbd8d42007-12-21 16:27:24 -05002329 page = grab_cache_page(inode->i_mapping, i);
2330 if (!page)
2331 goto out_unlock;
2332 if (!PageUptodate(page)) {
2333 btrfs_readpage(NULL, page);
2334 lock_page(page);
2335 if (!PageUptodate(page)) {
2336 unlock_page(page);
2337 page_cache_release(page);
2338 goto out_unlock;
2339 }
2340 }
Chris Masonec44a352008-04-28 15:29:52 -04002341#if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
2342 ClearPageDirty(page);
2343#else
2344 cancel_dirty_page(page, PAGE_CACHE_SIZE);
2345#endif
2346 wait_on_page_writeback(page);
2347 set_page_extent_mapped(page);
Chris Masonedbd8d42007-12-21 16:27:24 -05002348 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2349 page_end = page_start + PAGE_CACHE_SIZE - 1;
2350
Chris Masond1310b22008-01-24 16:13:08 -05002351 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05002352
Chris Masonec44a352008-04-28 15:29:52 -04002353 set_page_dirty(page);
Chris Masond1310b22008-01-24 16:13:08 -05002354 set_extent_delalloc(io_tree, page_start,
Chris Masonedbd8d42007-12-21 16:27:24 -05002355 page_end, GFP_NOFS);
2356
Chris Masond1310b22008-01-24 16:13:08 -05002357 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05002358 unlock_page(page);
2359 page_cache_release(page);
Chris Masonec44a352008-04-28 15:29:52 -04002360 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
Chris Masonedbd8d42007-12-21 16:27:24 -05002361 }
2362
2363out_unlock:
Chris Masonec44a352008-04-28 15:29:52 -04002364 kfree(ra);
Chris Masonedbd8d42007-12-21 16:27:24 -05002365 mutex_unlock(&inode->i_mutex);
2366 return 0;
2367}
2368
Chris Mason4313b392008-01-03 09:08:48 -05002369/*
2370 * note, this releases the path
2371 */
Chris Mason98ed5172008-01-03 10:01:48 -05002372static int noinline relocate_one_reference(struct btrfs_root *extent_root,
Chris Masonedbd8d42007-12-21 16:27:24 -05002373 struct btrfs_path *path,
Chris Mason4313b392008-01-03 09:08:48 -05002374 struct btrfs_key *extent_key)
Chris Masonedbd8d42007-12-21 16:27:24 -05002375{
2376 struct inode *inode;
2377 struct btrfs_root *found_root;
Chris Mason4313b392008-01-03 09:08:48 -05002378 struct btrfs_key *root_location;
2379 struct btrfs_extent_ref *ref;
2380 u64 ref_root;
2381 u64 ref_gen;
2382 u64 ref_objectid;
2383 u64 ref_offset;
Chris Masonedbd8d42007-12-21 16:27:24 -05002384 int ret;
2385
Chris Mason4313b392008-01-03 09:08:48 -05002386 ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
2387 struct btrfs_extent_ref);
2388 ref_root = btrfs_ref_root(path->nodes[0], ref);
2389 ref_gen = btrfs_ref_generation(path->nodes[0], ref);
2390 ref_objectid = btrfs_ref_objectid(path->nodes[0], ref);
2391 ref_offset = btrfs_ref_offset(path->nodes[0], ref);
2392 btrfs_release_path(extent_root, path);
2393
2394 root_location = kmalloc(sizeof(*root_location), GFP_NOFS);
2395 root_location->objectid = ref_root;
Chris Masonedbd8d42007-12-21 16:27:24 -05002396 if (ref_gen == 0)
Chris Mason4313b392008-01-03 09:08:48 -05002397 root_location->offset = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05002398 else
Chris Mason4313b392008-01-03 09:08:48 -05002399 root_location->offset = (u64)-1;
2400 root_location->type = BTRFS_ROOT_ITEM_KEY;
Chris Masonedbd8d42007-12-21 16:27:24 -05002401
2402 found_root = btrfs_read_fs_root_no_name(extent_root->fs_info,
Chris Mason4313b392008-01-03 09:08:48 -05002403 root_location);
Chris Masonedbd8d42007-12-21 16:27:24 -05002404 BUG_ON(!found_root);
Chris Mason4313b392008-01-03 09:08:48 -05002405 kfree(root_location);
Chris Masonedbd8d42007-12-21 16:27:24 -05002406
2407 if (ref_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
2408 mutex_unlock(&extent_root->fs_info->fs_mutex);
2409 inode = btrfs_iget_locked(extent_root->fs_info->sb,
2410 ref_objectid, found_root);
2411 if (inode->i_state & I_NEW) {
2412 /* the inode and parent dir are two different roots */
2413 BTRFS_I(inode)->root = found_root;
2414 BTRFS_I(inode)->location.objectid = ref_objectid;
2415 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
2416 BTRFS_I(inode)->location.offset = 0;
2417 btrfs_read_locked_inode(inode);
2418 unlock_new_inode(inode);
2419
2420 }
2421 /* this can happen if the reference is not against
2422 * the latest version of the tree root
2423 */
2424 if (is_bad_inode(inode)) {
2425 mutex_lock(&extent_root->fs_info->fs_mutex);
2426 goto out;
2427 }
2428 relocate_inode_pages(inode, ref_offset, extent_key->offset);
Chris Masonedbd8d42007-12-21 16:27:24 -05002429 iput(inode);
2430 mutex_lock(&extent_root->fs_info->fs_mutex);
2431 } else {
2432 struct btrfs_trans_handle *trans;
2433 struct btrfs_key found_key;
2434 struct extent_buffer *eb;
2435 int level;
2436 int i;
2437
2438 trans = btrfs_start_transaction(found_root, 1);
2439 eb = read_tree_block(found_root, extent_key->objectid,
2440 extent_key->offset);
2441 level = btrfs_header_level(eb);
2442
2443 if (level == 0)
2444 btrfs_item_key_to_cpu(eb, &found_key, 0);
2445 else
2446 btrfs_node_key_to_cpu(eb, &found_key, 0);
2447
2448 free_extent_buffer(eb);
2449
2450 path->lowest_level = level;
Chris Mason8f662a72008-01-02 10:01:11 -05002451 path->reada = 2;
Chris Masonedbd8d42007-12-21 16:27:24 -05002452 ret = btrfs_search_slot(trans, found_root, &found_key, path,
2453 0, 1);
2454 path->lowest_level = 0;
2455 for (i = level; i < BTRFS_MAX_LEVEL; i++) {
2456 if (!path->nodes[i])
2457 break;
2458 free_extent_buffer(path->nodes[i]);
2459 path->nodes[i] = NULL;
2460 }
2461 btrfs_release_path(found_root, path);
2462 btrfs_end_transaction(trans, found_root);
2463 }
2464
2465out:
2466 return 0;
2467}
2468
Chris Mason98ed5172008-01-03 10:01:48 -05002469static int noinline relocate_one_extent(struct btrfs_root *extent_root,
2470 struct btrfs_path *path,
2471 struct btrfs_key *extent_key)
Chris Masonedbd8d42007-12-21 16:27:24 -05002472{
2473 struct btrfs_key key;
2474 struct btrfs_key found_key;
Chris Masonedbd8d42007-12-21 16:27:24 -05002475 struct extent_buffer *leaf;
Chris Masonedbd8d42007-12-21 16:27:24 -05002476 u32 nritems;
2477 u32 item_size;
2478 int ret = 0;
2479
2480 key.objectid = extent_key->objectid;
2481 key.type = BTRFS_EXTENT_REF_KEY;
2482 key.offset = 0;
2483
2484 while(1) {
2485 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
2486
Chris Masonedbd8d42007-12-21 16:27:24 -05002487 if (ret < 0)
2488 goto out;
2489
2490 ret = 0;
2491 leaf = path->nodes[0];
2492 nritems = btrfs_header_nritems(leaf);
2493 if (path->slots[0] == nritems)
2494 goto out;
2495
2496 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2497 if (found_key.objectid != extent_key->objectid)
2498 break;
2499
2500 if (found_key.type != BTRFS_EXTENT_REF_KEY)
2501 break;
2502
2503 key.offset = found_key.offset + 1;
2504 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
2505
Chris Mason4313b392008-01-03 09:08:48 -05002506 ret = relocate_one_reference(extent_root, path, extent_key);
Chris Masonedbd8d42007-12-21 16:27:24 -05002507 if (ret)
2508 goto out;
2509 }
2510 ret = 0;
2511out:
2512 btrfs_release_path(extent_root, path);
2513 return ret;
2514}
2515
Chris Masonec44a352008-04-28 15:29:52 -04002516static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
2517{
2518 u64 num_devices;
2519 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
2520 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
2521
2522 num_devices = btrfs_super_num_devices(&root->fs_info->super_copy);
2523 if (num_devices == 1) {
2524 stripped |= BTRFS_BLOCK_GROUP_DUP;
2525 stripped = flags & ~stripped;
2526
2527 /* turn raid0 into single device chunks */
2528 if (flags & BTRFS_BLOCK_GROUP_RAID0)
2529 return stripped;
2530
2531 /* turn mirroring into duplication */
2532 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
2533 BTRFS_BLOCK_GROUP_RAID10))
2534 return stripped | BTRFS_BLOCK_GROUP_DUP;
2535 return flags;
2536 } else {
2537 /* they already had raid on here, just return */
2538 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
2539 (flags & BTRFS_BLOCK_GROUP_RAID1)) {
2540 }
2541 if (flags & stripped)
2542 return flags;
2543
2544 stripped |= BTRFS_BLOCK_GROUP_DUP;
2545 stripped = flags & ~stripped;
2546
2547 /* switch duplicated blocks with raid1 */
2548 if (flags & BTRFS_BLOCK_GROUP_DUP)
2549 return stripped | BTRFS_BLOCK_GROUP_RAID1;
2550
2551 /* turn single device chunks into raid0 */
2552 return stripped | BTRFS_BLOCK_GROUP_RAID0;
2553 }
2554 return flags;
2555}
2556
Chris Mason8f18cf12008-04-25 16:53:30 -04002557int btrfs_shrink_extent_tree(struct btrfs_root *root, u64 shrink_start)
Chris Masonedbd8d42007-12-21 16:27:24 -05002558{
2559 struct btrfs_trans_handle *trans;
2560 struct btrfs_root *tree_root = root->fs_info->tree_root;
2561 struct btrfs_path *path;
2562 u64 cur_byte;
2563 u64 total_found;
Chris Mason8f18cf12008-04-25 16:53:30 -04002564 u64 shrink_last_byte;
Chris Masonec44a352008-04-28 15:29:52 -04002565 u64 new_alloc_flags;
Chris Mason8f18cf12008-04-25 16:53:30 -04002566 struct btrfs_block_group_cache *shrink_block_group;
Chris Masonedbd8d42007-12-21 16:27:24 -05002567 struct btrfs_fs_info *info = root->fs_info;
Chris Masonedbd8d42007-12-21 16:27:24 -05002568 struct btrfs_key key;
Yan73e48b22008-01-03 14:14:39 -05002569 struct btrfs_key found_key;
Chris Masonedbd8d42007-12-21 16:27:24 -05002570 struct extent_buffer *leaf;
2571 u32 nritems;
2572 int ret;
Chris Mason725c8462008-01-04 16:47:16 -05002573 int progress = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05002574
Chris Mason8f18cf12008-04-25 16:53:30 -04002575 shrink_block_group = btrfs_lookup_block_group(root->fs_info,
2576 shrink_start);
2577 BUG_ON(!shrink_block_group);
2578
2579 shrink_last_byte = shrink_start + shrink_block_group->key.offset;
2580
2581 shrink_block_group->space_info->total_bytes -=
2582 shrink_block_group->key.offset;
Chris Masonedbd8d42007-12-21 16:27:24 -05002583 path = btrfs_alloc_path();
2584 root = root->fs_info->extent_root;
Chris Mason8f662a72008-01-02 10:01:11 -05002585 path->reada = 2;
Chris Masonedbd8d42007-12-21 16:27:24 -05002586
2587again:
Chris Masonec44a352008-04-28 15:29:52 -04002588 if (btrfs_block_group_used(&shrink_block_group->item) > 0) {
2589 trans = btrfs_start_transaction(root, 1);
2590 new_alloc_flags = update_block_group_flags(root,
2591 shrink_block_group->flags);
2592 do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason8f18cf12008-04-25 16:53:30 -04002593 btrfs_block_group_used(&shrink_block_group->item) +
Chris Masonec44a352008-04-28 15:29:52 -04002594 2 * 1024 * 1024, new_alloc_flags);
2595 btrfs_end_transaction(trans, root);
2596 }
Chris Mason8f18cf12008-04-25 16:53:30 -04002597 shrink_block_group->ro = 1;
2598
Chris Masonedbd8d42007-12-21 16:27:24 -05002599 total_found = 0;
Chris Mason8f18cf12008-04-25 16:53:30 -04002600 key.objectid = shrink_start;
Chris Masonedbd8d42007-12-21 16:27:24 -05002601 key.offset = 0;
2602 key.type = 0;
Yan73e48b22008-01-03 14:14:39 -05002603 cur_byte = key.objectid;
Chris Mason4313b392008-01-03 09:08:48 -05002604
Yan73e48b22008-01-03 14:14:39 -05002605 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2606 if (ret < 0)
2607 goto out;
2608
Chris Mason0b86a832008-03-24 15:01:56 -04002609 ret = btrfs_previous_item(root, path, 0, BTRFS_EXTENT_ITEM_KEY);
Yan73e48b22008-01-03 14:14:39 -05002610 if (ret < 0)
2611 goto out;
Chris Mason8f18cf12008-04-25 16:53:30 -04002612
Yan73e48b22008-01-03 14:14:39 -05002613 if (ret == 0) {
2614 leaf = path->nodes[0];
2615 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04002616 if (found_key.objectid + found_key.offset > shrink_start &&
2617 found_key.objectid < shrink_last_byte) {
Yan73e48b22008-01-03 14:14:39 -05002618 cur_byte = found_key.objectid;
2619 key.objectid = cur_byte;
2620 }
2621 }
2622 btrfs_release_path(root, path);
2623
2624 while(1) {
Chris Masonedbd8d42007-12-21 16:27:24 -05002625 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2626 if (ret < 0)
2627 goto out;
Yan73e48b22008-01-03 14:14:39 -05002628
Chris Masonedbd8d42007-12-21 16:27:24 -05002629 leaf = path->nodes[0];
Chris Masonedbd8d42007-12-21 16:27:24 -05002630 nritems = btrfs_header_nritems(leaf);
Yan73e48b22008-01-03 14:14:39 -05002631next:
2632 if (path->slots[0] >= nritems) {
2633 ret = btrfs_next_leaf(root, path);
2634 if (ret < 0)
2635 goto out;
2636 if (ret == 1) {
2637 ret = 0;
2638 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05002639 }
Yan73e48b22008-01-03 14:14:39 -05002640 leaf = path->nodes[0];
2641 nritems = btrfs_header_nritems(leaf);
2642 }
2643
2644 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason725c8462008-01-04 16:47:16 -05002645
Chris Mason8f18cf12008-04-25 16:53:30 -04002646 if (found_key.objectid >= shrink_last_byte)
2647 break;
2648
Chris Mason725c8462008-01-04 16:47:16 -05002649 if (progress && need_resched()) {
2650 memcpy(&key, &found_key, sizeof(key));
2651 mutex_unlock(&root->fs_info->fs_mutex);
2652 cond_resched();
2653 mutex_lock(&root->fs_info->fs_mutex);
2654 btrfs_release_path(root, path);
2655 btrfs_search_slot(NULL, root, &key, path, 0, 0);
2656 progress = 0;
2657 goto next;
2658 }
2659 progress = 1;
2660
Yan73e48b22008-01-03 14:14:39 -05002661 if (btrfs_key_type(&found_key) != BTRFS_EXTENT_ITEM_KEY ||
2662 found_key.objectid + found_key.offset <= cur_byte) {
2663 path->slots[0]++;
Chris Masonedbd8d42007-12-21 16:27:24 -05002664 goto next;
2665 }
Yan73e48b22008-01-03 14:14:39 -05002666
Chris Masonedbd8d42007-12-21 16:27:24 -05002667 total_found++;
2668 cur_byte = found_key.objectid + found_key.offset;
2669 key.objectid = cur_byte;
2670 btrfs_release_path(root, path);
2671 ret = relocate_one_extent(root, path, &found_key);
2672 }
2673
2674 btrfs_release_path(root, path);
2675
2676 if (total_found > 0) {
2677 trans = btrfs_start_transaction(tree_root, 1);
2678 btrfs_commit_transaction(trans, tree_root);
2679
2680 mutex_unlock(&root->fs_info->fs_mutex);
2681 btrfs_clean_old_snapshots(tree_root);
2682 mutex_lock(&root->fs_info->fs_mutex);
2683
2684 trans = btrfs_start_transaction(tree_root, 1);
2685 btrfs_commit_transaction(trans, tree_root);
2686 goto again;
2687 }
2688
Chris Mason8f18cf12008-04-25 16:53:30 -04002689 /*
2690 * we've freed all the extents, now remove the block
2691 * group item from the tree
2692 */
Chris Masonedbd8d42007-12-21 16:27:24 -05002693 trans = btrfs_start_transaction(root, 1);
Chris Mason8f18cf12008-04-25 16:53:30 -04002694 memcpy(&key, &shrink_block_group->key, sizeof(key));
Chris Mason4313b392008-01-03 09:08:48 -05002695
Chris Mason8f18cf12008-04-25 16:53:30 -04002696 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2697 if (ret > 0)
2698 ret = -EIO;
2699 if (ret < 0)
2700 goto out;
Yan73e48b22008-01-03 14:14:39 -05002701
Chris Mason8f18cf12008-04-25 16:53:30 -04002702 leaf = path->nodes[0];
2703 nritems = btrfs_header_nritems(leaf);
2704 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2705 kfree(shrink_block_group);
Chris Mason1372f8e2008-01-04 09:34:54 -05002706
Chris Mason8f18cf12008-04-25 16:53:30 -04002707 clear_extent_bits(&info->block_group_cache, found_key.objectid,
2708 found_key.objectid + found_key.offset - 1,
2709 (unsigned int)-1, GFP_NOFS);
Yan73e48b22008-01-03 14:14:39 -05002710
Chris Mason8f18cf12008-04-25 16:53:30 -04002711 btrfs_del_item(trans, root, path);
2712 clear_extent_dirty(&info->free_space_cache,
2713 shrink_start, shrink_last_byte - 1,
Chris Masonedbd8d42007-12-21 16:27:24 -05002714 GFP_NOFS);
2715 btrfs_commit_transaction(trans, root);
2716out:
2717 btrfs_free_path(path);
2718 return ret;
2719}
2720
Chris Mason0b86a832008-03-24 15:01:56 -04002721int find_first_block_group(struct btrfs_root *root, struct btrfs_path *path,
2722 struct btrfs_key *key)
2723{
2724 int ret;
2725 struct btrfs_key found_key;
2726 struct extent_buffer *leaf;
2727 int slot;
2728
2729 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
2730 if (ret < 0)
2731 return ret;
2732 while(1) {
2733 slot = path->slots[0];
2734 leaf = path->nodes[0];
2735 if (slot >= btrfs_header_nritems(leaf)) {
2736 ret = btrfs_next_leaf(root, path);
2737 if (ret == 0)
2738 continue;
2739 if (ret < 0)
2740 goto error;
2741 break;
2742 }
2743 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2744
2745 if (found_key.objectid >= key->objectid &&
2746 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY)
2747 return 0;
2748 path->slots[0]++;
2749 }
2750 ret = -ENOENT;
2751error:
2752 return ret;
2753}
2754
Chris Mason9078a3e2007-04-26 16:46:15 -04002755int btrfs_read_block_groups(struct btrfs_root *root)
2756{
2757 struct btrfs_path *path;
2758 int ret;
Chris Mason96b51792007-10-15 16:15:19 -04002759 int bit;
Chris Mason9078a3e2007-04-26 16:46:15 -04002760 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04002761 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04002762 struct btrfs_space_info *space_info;
Chris Masond1310b22008-01-24 16:13:08 -05002763 struct extent_io_tree *block_group_cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04002764 struct btrfs_key key;
2765 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04002766 struct extent_buffer *leaf;
Chris Mason96b51792007-10-15 16:15:19 -04002767
2768 block_group_cache = &info->block_group_cache;
Chris Masonbe744172007-05-06 10:15:01 -04002769 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04002770 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04002771 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04002772 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04002773 path = btrfs_alloc_path();
2774 if (!path)
2775 return -ENOMEM;
2776
2777 while(1) {
Chris Mason0b86a832008-03-24 15:01:56 -04002778 ret = find_first_block_group(root, path, &key);
2779 if (ret > 0) {
2780 ret = 0;
2781 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04002782 }
Chris Mason0b86a832008-03-24 15:01:56 -04002783 if (ret != 0)
2784 goto error;
2785
Chris Mason5f39d392007-10-15 16:14:19 -04002786 leaf = path->nodes[0];
2787 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04002788 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04002789 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04002790 ret = -ENOMEM;
Chris Mason9078a3e2007-04-26 16:46:15 -04002791 break;
2792 }
Chris Mason3e1ad542007-05-07 20:03:49 -04002793
Chris Mason5f39d392007-10-15 16:14:19 -04002794 read_extent_buffer(leaf, &cache->item,
2795 btrfs_item_ptr_offset(leaf, path->slots[0]),
2796 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04002797 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04002798
Chris Mason9078a3e2007-04-26 16:46:15 -04002799 key.objectid = found_key.objectid + found_key.offset;
2800 btrfs_release_path(root, path);
Chris Mason0b86a832008-03-24 15:01:56 -04002801 cache->flags = btrfs_block_group_flags(&cache->item);
2802 bit = 0;
2803 if (cache->flags & BTRFS_BLOCK_GROUP_DATA) {
Chris Mason96b51792007-10-15 16:15:19 -04002804 bit = BLOCK_GROUP_DATA;
Chris Mason0b86a832008-03-24 15:01:56 -04002805 } else if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) {
2806 bit = BLOCK_GROUP_SYSTEM;
2807 } else if (cache->flags & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason96b51792007-10-15 16:15:19 -04002808 bit = BLOCK_GROUP_METADATA;
Chris Mason31f3c992007-04-30 15:25:45 -04002809 }
Chris Mason8790d502008-04-03 16:29:03 -04002810 set_avail_alloc_bits(info, cache->flags);
Chris Mason96b51792007-10-15 16:15:19 -04002811
Chris Mason6324fbf2008-03-24 15:01:59 -04002812 ret = update_space_info(info, cache->flags, found_key.offset,
2813 btrfs_block_group_used(&cache->item),
2814 &space_info);
2815 BUG_ON(ret);
2816 cache->space_info = space_info;
2817
Chris Mason96b51792007-10-15 16:15:19 -04002818 /* use EXTENT_LOCKED to prevent merging */
2819 set_extent_bits(block_group_cache, found_key.objectid,
2820 found_key.objectid + found_key.offset - 1,
2821 bit | EXTENT_LOCKED, GFP_NOFS);
2822 set_state_private(block_group_cache, found_key.objectid,
Jens Axboeae2f5412007-10-19 09:22:59 -04002823 (unsigned long)cache);
Chris Mason96b51792007-10-15 16:15:19 -04002824
Chris Mason9078a3e2007-04-26 16:46:15 -04002825 if (key.objectid >=
Chris Masondb945352007-10-15 16:15:53 -04002826 btrfs_super_total_bytes(&info->super_copy))
Chris Mason9078a3e2007-04-26 16:46:15 -04002827 break;
2828 }
Chris Mason0b86a832008-03-24 15:01:56 -04002829 ret = 0;
2830error:
Chris Mason9078a3e2007-04-26 16:46:15 -04002831 btrfs_free_path(path);
Chris Mason0b86a832008-03-24 15:01:56 -04002832 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04002833}
Chris Mason6324fbf2008-03-24 15:01:59 -04002834
2835int btrfs_make_block_group(struct btrfs_trans_handle *trans,
2836 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04002837 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04002838 u64 size)
2839{
2840 int ret;
2841 int bit = 0;
2842 struct btrfs_root *extent_root;
2843 struct btrfs_block_group_cache *cache;
2844 struct extent_io_tree *block_group_cache;
2845
2846 extent_root = root->fs_info->extent_root;
2847 block_group_cache = &root->fs_info->block_group_cache;
2848
Chris Mason8f18cf12008-04-25 16:53:30 -04002849 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason6324fbf2008-03-24 15:01:59 -04002850 BUG_ON(!cache);
Chris Masone17cade2008-04-15 15:41:47 -04002851 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04002852 cache->key.offset = size;
Chris Masone17cade2008-04-15 15:41:47 -04002853
Chris Mason6324fbf2008-03-24 15:01:59 -04002854 btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY);
2855 memset(&cache->item, 0, sizeof(cache->item));
2856 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04002857 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
2858 cache->flags = type;
2859 btrfs_set_block_group_flags(&cache->item, type);
2860
2861 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
2862 &cache->space_info);
2863 BUG_ON(ret);
2864
Chris Masond18a2c42008-04-04 15:40:00 -04002865 bit = block_group_state_bits(type);
Chris Masone17cade2008-04-15 15:41:47 -04002866 set_extent_bits(block_group_cache, chunk_offset,
2867 chunk_offset + size - 1,
Chris Mason6324fbf2008-03-24 15:01:59 -04002868 bit | EXTENT_LOCKED, GFP_NOFS);
Chris Mason6324fbf2008-03-24 15:01:59 -04002869
Chris Masone17cade2008-04-15 15:41:47 -04002870 set_state_private(block_group_cache, chunk_offset,
2871 (unsigned long)cache);
Chris Mason6324fbf2008-03-24 15:01:59 -04002872 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
2873 sizeof(cache->item));
2874 BUG_ON(ret);
2875
2876 finish_current_insert(trans, extent_root);
2877 ret = del_pending_extents(trans, extent_root);
2878 BUG_ON(ret);
Chris Masond18a2c42008-04-04 15:40:00 -04002879 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason6324fbf2008-03-24 15:01:59 -04002880 return 0;
2881}