blob: 1c10ffc837c863825624e11ec6d9ec9791b6c700 [file] [log] [blame]
Chris Mason6cbd5572007-06-12 09:07:21 -04001/*
2 * Copyright (C) 2007 Oracle. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
Zach Brownec6b9102007-07-11 10:00:37 -040018#include <linux/sched.h>
Chris Masonedbd8d42007-12-21 16:27:24 -050019#include <linux/pagemap.h>
Chris Masonec44a352008-04-28 15:29:52 -040020#include <linux/writeback.h>
David Woodhouse21af8042008-08-12 14:13:26 +010021#include <linux/blkdev.h>
Chris Mason74493f72007-12-11 09:25:06 -050022#include "hash.h"
Miguela5eb62e2008-04-11 15:45:51 -040023#include "crc32c.h"
Chris Masonfec577f2007-02-26 10:40:21 -050024#include "ctree.h"
25#include "disk-io.h"
26#include "print-tree.h"
Chris Masone089f052007-03-16 16:20:31 -040027#include "transaction.h"
Chris Mason0b86a832008-03-24 15:01:56 -040028#include "volumes.h"
Chris Mason925baed2008-06-25 16:01:30 -040029#include "locking.h"
Yan Zheng31153d82008-07-28 15:32:19 -040030#include "ref-cache.h"
Chris Masonfec577f2007-02-26 10:40:21 -050031
Chris Mason0b86a832008-03-24 15:01:56 -040032#define BLOCK_GROUP_DATA EXTENT_WRITEBACK
Chris Mason96b51792007-10-15 16:15:19 -040033#define BLOCK_GROUP_METADATA EXTENT_UPTODATE
Chris Mason0b86a832008-03-24 15:01:56 -040034#define BLOCK_GROUP_SYSTEM EXTENT_NEW
35
Chris Mason96b51792007-10-15 16:15:19 -040036#define BLOCK_GROUP_DIRTY EXTENT_DIRTY
37
Chris Masone089f052007-03-16 16:20:31 -040038static int finish_current_insert(struct btrfs_trans_handle *trans, struct
39 btrfs_root *extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -040040static int del_pending_extents(struct btrfs_trans_handle *trans, struct
41 btrfs_root *extent_root);
Chris Mason925baed2008-06-25 16:01:30 -040042static struct btrfs_block_group_cache *
43__btrfs_find_block_group(struct btrfs_root *root,
44 struct btrfs_block_group_cache *hint,
45 u64 search_start, int data, int owner);
Yand548ee52008-01-03 13:56:30 -050046
Chris Mason925baed2008-06-25 16:01:30 -040047void maybe_lock_mutex(struct btrfs_root *root)
48{
49 if (root != root->fs_info->extent_root &&
50 root != root->fs_info->chunk_root &&
51 root != root->fs_info->dev_root) {
52 mutex_lock(&root->fs_info->alloc_mutex);
53 }
54}
55
56void maybe_unlock_mutex(struct btrfs_root *root)
57{
58 if (root != root->fs_info->extent_root &&
59 root != root->fs_info->chunk_root &&
60 root != root->fs_info->dev_root) {
61 mutex_unlock(&root->fs_info->alloc_mutex);
62 }
63}
Chris Masonfec577f2007-02-26 10:40:21 -050064
Chris Masone37c9e62007-05-09 20:13:14 -040065static int cache_block_group(struct btrfs_root *root,
66 struct btrfs_block_group_cache *block_group)
67{
68 struct btrfs_path *path;
Josef Bacikef8bbdf2008-09-23 13:14:11 -040069 int ret = 0;
Chris Masone37c9e62007-05-09 20:13:14 -040070 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -040071 struct extent_buffer *leaf;
Chris Masond1310b22008-01-24 16:13:08 -050072 struct extent_io_tree *free_space_cache;
Chris Masone37c9e62007-05-09 20:13:14 -040073 int slot;
Chris Masone37c9e62007-05-09 20:13:14 -040074 u64 last = 0;
75 u64 hole_size;
Yan7d7d6062007-09-14 16:15:28 -040076 u64 first_free;
Chris Masone37c9e62007-05-09 20:13:14 -040077 int found = 0;
78
Chris Mason00f5c792007-11-30 10:09:33 -050079 if (!block_group)
80 return 0;
81
Chris Masone37c9e62007-05-09 20:13:14 -040082 root = root->fs_info->extent_root;
Chris Masonf510cfe2007-10-15 16:14:48 -040083 free_space_cache = &root->fs_info->free_space_cache;
Chris Masone37c9e62007-05-09 20:13:14 -040084
85 if (block_group->cached)
86 return 0;
Chris Masonf510cfe2007-10-15 16:14:48 -040087
Chris Masone37c9e62007-05-09 20:13:14 -040088 path = btrfs_alloc_path();
89 if (!path)
90 return -ENOMEM;
Yan7d7d6062007-09-14 16:15:28 -040091
Chris Mason2cc58cf2007-08-27 16:49:44 -040092 path->reada = 2;
Chris Mason5cd57b22008-06-25 16:01:30 -040093 /*
94 * we get into deadlocks with paths held by callers of this function.
95 * since the alloc_mutex is protecting things right now, just
96 * skip the locking here
97 */
98 path->skip_locking = 1;
Yan7d7d6062007-09-14 16:15:28 -040099 first_free = block_group->key.objectid;
Chris Masone37c9e62007-05-09 20:13:14 -0400100 key.objectid = block_group->key.objectid;
Chris Masone37c9e62007-05-09 20:13:14 -0400101 key.offset = 0;
102 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
103 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
104 if (ret < 0)
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400105 goto err;
Chris Mason0b86a832008-03-24 15:01:56 -0400106 ret = btrfs_previous_item(root, path, 0, BTRFS_EXTENT_ITEM_KEY);
Yand548ee52008-01-03 13:56:30 -0500107 if (ret < 0)
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400108 goto err;
Yand548ee52008-01-03 13:56:30 -0500109 if (ret == 0) {
110 leaf = path->nodes[0];
111 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
112 if (key.objectid + key.offset > first_free)
113 first_free = key.objectid + key.offset;
114 }
Chris Masone37c9e62007-05-09 20:13:14 -0400115 while(1) {
Chris Mason5f39d392007-10-15 16:14:19 -0400116 leaf = path->nodes[0];
Chris Masone37c9e62007-05-09 20:13:14 -0400117 slot = path->slots[0];
Chris Mason5f39d392007-10-15 16:14:19 -0400118 if (slot >= btrfs_header_nritems(leaf)) {
Chris Masone37c9e62007-05-09 20:13:14 -0400119 ret = btrfs_next_leaf(root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -0400120 if (ret < 0)
121 goto err;
Chris Masonde428b62007-05-18 13:28:27 -0400122 if (ret == 0) {
Chris Masone37c9e62007-05-09 20:13:14 -0400123 continue;
Chris Masonde428b62007-05-18 13:28:27 -0400124 } else {
Chris Masone37c9e62007-05-09 20:13:14 -0400125 break;
126 }
127 }
Chris Mason5f39d392007-10-15 16:14:19 -0400128 btrfs_item_key_to_cpu(leaf, &key, slot);
Yan7d7d6062007-09-14 16:15:28 -0400129 if (key.objectid < block_group->key.objectid) {
Yan7d7d6062007-09-14 16:15:28 -0400130 goto next;
131 }
Chris Masone37c9e62007-05-09 20:13:14 -0400132 if (key.objectid >= block_group->key.objectid +
133 block_group->key.offset) {
Yan7d7d6062007-09-14 16:15:28 -0400134 break;
135 }
136
137 if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) {
138 if (!found) {
139 last = first_free;
140 found = 1;
Chris Masone37c9e62007-05-09 20:13:14 -0400141 }
Chris Masonf510cfe2007-10-15 16:14:48 -0400142 if (key.objectid > last) {
143 hole_size = key.objectid - last;
144 set_extent_dirty(free_space_cache, last,
145 last + hole_size - 1,
146 GFP_NOFS);
Chris Masone37c9e62007-05-09 20:13:14 -0400147 }
Yan7d7d6062007-09-14 16:15:28 -0400148 last = key.objectid + key.offset;
Chris Masone37c9e62007-05-09 20:13:14 -0400149 }
Yan7d7d6062007-09-14 16:15:28 -0400150next:
Chris Masone37c9e62007-05-09 20:13:14 -0400151 path->slots[0]++;
152 }
153
Yan7d7d6062007-09-14 16:15:28 -0400154 if (!found)
155 last = first_free;
156 if (block_group->key.objectid +
157 block_group->key.offset > last) {
158 hole_size = block_group->key.objectid +
159 block_group->key.offset - last;
Chris Masonf510cfe2007-10-15 16:14:48 -0400160 set_extent_dirty(free_space_cache, last,
161 last + hole_size - 1, GFP_NOFS);
Yan7d7d6062007-09-14 16:15:28 -0400162 }
Chris Masone37c9e62007-05-09 20:13:14 -0400163 block_group->cached = 1;
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400164 ret = 0;
Chris Mason54aa1f42007-06-22 14:16:25 -0400165err:
Chris Masone37c9e62007-05-09 20:13:14 -0400166 btrfs_free_path(path);
Josef Bacikef8bbdf2008-09-23 13:14:11 -0400167 return ret;
Chris Masone37c9e62007-05-09 20:13:14 -0400168}
169
Chris Mason0ef3e662008-05-24 14:04:53 -0400170struct btrfs_block_group_cache *btrfs_lookup_first_block_group(struct
171 btrfs_fs_info *info,
172 u64 bytenr)
173{
174 struct extent_io_tree *block_group_cache;
175 struct btrfs_block_group_cache *block_group = NULL;
176 u64 ptr;
177 u64 start;
178 u64 end;
179 int ret;
180
181 bytenr = max_t(u64, bytenr,
182 BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE);
183 block_group_cache = &info->block_group_cache;
184 ret = find_first_extent_bit(block_group_cache,
185 bytenr, &start, &end,
186 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA |
187 BLOCK_GROUP_SYSTEM);
188 if (ret) {
189 return NULL;
190 }
191 ret = get_state_private(block_group_cache, start, &ptr);
192 if (ret)
193 return NULL;
194
195 block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
196 return block_group;
197}
198
Chris Mason5276aed2007-06-11 21:33:38 -0400199struct btrfs_block_group_cache *btrfs_lookup_block_group(struct
200 btrfs_fs_info *info,
Chris Masondb945352007-10-15 16:15:53 -0400201 u64 bytenr)
Chris Masonbe744172007-05-06 10:15:01 -0400202{
Chris Masond1310b22008-01-24 16:13:08 -0500203 struct extent_io_tree *block_group_cache;
Chris Mason96b51792007-10-15 16:15:19 -0400204 struct btrfs_block_group_cache *block_group = NULL;
205 u64 ptr;
206 u64 start;
207 u64 end;
Chris Masonbe744172007-05-06 10:15:01 -0400208 int ret;
209
Chris Masona061fc82008-05-07 11:43:44 -0400210 bytenr = max_t(u64, bytenr,
211 BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE);
Chris Mason96b51792007-10-15 16:15:19 -0400212 block_group_cache = &info->block_group_cache;
213 ret = find_first_extent_bit(block_group_cache,
Chris Masondb945352007-10-15 16:15:53 -0400214 bytenr, &start, &end,
Chris Mason0b86a832008-03-24 15:01:56 -0400215 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA |
216 BLOCK_GROUP_SYSTEM);
Chris Masonbe744172007-05-06 10:15:01 -0400217 if (ret) {
Chris Mason96b51792007-10-15 16:15:19 -0400218 return NULL;
Chris Masonbe744172007-05-06 10:15:01 -0400219 }
Chris Mason96b51792007-10-15 16:15:19 -0400220 ret = get_state_private(block_group_cache, start, &ptr);
221 if (ret)
222 return NULL;
223
Jens Axboeae2f5412007-10-19 09:22:59 -0400224 block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr;
Yan5cf66422007-11-16 14:57:09 -0500225 if (block_group->key.objectid <= bytenr && bytenr <
Chris Mason96b51792007-10-15 16:15:19 -0400226 block_group->key.objectid + block_group->key.offset)
227 return block_group;
Chris Masonbe744172007-05-06 10:15:01 -0400228 return NULL;
229}
Chris Mason0b86a832008-03-24 15:01:56 -0400230
231static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits)
232{
Chris Mason593060d2008-03-25 16:50:33 -0400233 return (cache->flags & bits) == bits;
Chris Mason0b86a832008-03-24 15:01:56 -0400234}
235
236static int noinline find_search_start(struct btrfs_root *root,
Chris Mason98ed5172008-01-03 10:01:48 -0500237 struct btrfs_block_group_cache **cache_ret,
Chris Mason0ef3e662008-05-24 14:04:53 -0400238 u64 *start_ret, u64 num, int data)
Chris Masone37c9e62007-05-09 20:13:14 -0400239{
Chris Masone37c9e62007-05-09 20:13:14 -0400240 int ret;
241 struct btrfs_block_group_cache *cache = *cache_ret;
Chris Masond7fc6402008-02-18 12:12:38 -0500242 struct extent_io_tree *free_space_cache;
Chris Mason7d1660d2008-03-24 15:02:03 -0400243 struct extent_state *state;
Chris Masone19caa52007-10-15 16:17:44 -0400244 u64 last;
Chris Masonf510cfe2007-10-15 16:14:48 -0400245 u64 start = 0;
Chris Mason257d0ce2007-11-07 21:08:16 -0500246 u64 cache_miss = 0;
Chris Masonc31f8832008-01-08 15:46:31 -0500247 u64 total_fs_bytes;
Chris Mason0b86a832008-03-24 15:01:56 -0400248 u64 search_start = *start_ret;
Chris Masonf84a8b32007-11-06 10:26:29 -0500249 int wrapped = 0;
Chris Masone37c9e62007-05-09 20:13:14 -0400250
Chris Mason7d9eb122008-07-08 14:19:17 -0400251 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
Chris Masonc31f8832008-01-08 15:46:31 -0500252 total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
Chris Masond7fc6402008-02-18 12:12:38 -0500253 free_space_cache = &root->fs_info->free_space_cache;
254
Chris Mason0ef3e662008-05-24 14:04:53 -0400255 if (!cache)
Chris Mason54aa1f42007-06-22 14:16:25 -0400256 goto out;
Chris Masonf84a8b32007-11-06 10:26:29 -0500257
Chris Mason0ef3e662008-05-24 14:04:53 -0400258again:
259 ret = cache_block_group(root, cache);
260 if (ret) {
261 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -0400262 }
Chris Masone19caa52007-10-15 16:17:44 -0400263
Chris Mason0ef3e662008-05-24 14:04:53 -0400264 last = max(search_start, cache->key.objectid);
265 if (!block_group_bits(cache, data) || cache->ro)
266 goto new_group;
267
Chris Mason7d1660d2008-03-24 15:02:03 -0400268 spin_lock_irq(&free_space_cache->lock);
269 state = find_first_extent_bit_state(free_space_cache, last, EXTENT_DIRTY);
Chris Masone37c9e62007-05-09 20:13:14 -0400270 while(1) {
Chris Mason7d1660d2008-03-24 15:02:03 -0400271 if (!state) {
Chris Mason257d0ce2007-11-07 21:08:16 -0500272 if (!cache_miss)
273 cache_miss = last;
Chris Mason7d1660d2008-03-24 15:02:03 -0400274 spin_unlock_irq(&free_space_cache->lock);
Chris Masone19caa52007-10-15 16:17:44 -0400275 goto new_group;
276 }
Chris Masonf510cfe2007-10-15 16:14:48 -0400277
Chris Mason7d1660d2008-03-24 15:02:03 -0400278 start = max(last, state->start);
279 last = state->end + 1;
Chris Mason257d0ce2007-11-07 21:08:16 -0500280 if (last - start < num) {
Chris Mason7d1660d2008-03-24 15:02:03 -0400281 do {
282 state = extent_state_next(state);
283 } while(state && !(state->state & EXTENT_DIRTY));
Chris Masonf510cfe2007-10-15 16:14:48 -0400284 continue;
Chris Mason257d0ce2007-11-07 21:08:16 -0500285 }
Chris Mason7d1660d2008-03-24 15:02:03 -0400286 spin_unlock_irq(&free_space_cache->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -0400287 if (cache->ro) {
Chris Mason8f18cf12008-04-25 16:53:30 -0400288 goto new_group;
Chris Mason0ef3e662008-05-24 14:04:53 -0400289 }
Chris Mason0b86a832008-03-24 15:01:56 -0400290 if (start + num > cache->key.objectid + cache->key.offset)
Chris Masone37c9e62007-05-09 20:13:14 -0400291 goto new_group;
Chris Mason8790d502008-04-03 16:29:03 -0400292 if (!block_group_bits(cache, data)) {
Chris Mason611f0e02008-04-03 16:29:03 -0400293 printk("block group bits don't match %Lu %d\n", cache->flags, data);
Chris Mason8790d502008-04-03 16:29:03 -0400294 }
Chris Mason0b86a832008-03-24 15:01:56 -0400295 *start_ret = start;
296 return 0;
Chris Mason8790d502008-04-03 16:29:03 -0400297 }
298out:
Chris Mason1a2b2ac2007-12-04 13:18:24 -0500299 cache = btrfs_lookup_block_group(root->fs_info, search_start);
300 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -0400301 printk("Unable to find block group for %Lu\n", search_start);
Chris Mason1a2b2ac2007-12-04 13:18:24 -0500302 WARN_ON(1);
Chris Mason1a2b2ac2007-12-04 13:18:24 -0500303 }
Chris Mason0b86a832008-03-24 15:01:56 -0400304 return -ENOSPC;
Chris Masone37c9e62007-05-09 20:13:14 -0400305
306new_group:
Chris Masone19caa52007-10-15 16:17:44 -0400307 last = cache->key.objectid + cache->key.offset;
Chris Masonf84a8b32007-11-06 10:26:29 -0500308wrapped:
Chris Mason0ef3e662008-05-24 14:04:53 -0400309 cache = btrfs_lookup_first_block_group(root->fs_info, last);
Chris Masonc31f8832008-01-08 15:46:31 -0500310 if (!cache || cache->key.objectid >= total_fs_bytes) {
Chris Mason0e4de582007-11-26 10:55:49 -0500311no_cache:
Chris Masonf84a8b32007-11-06 10:26:29 -0500312 if (!wrapped) {
313 wrapped = 1;
314 last = search_start;
Chris Masonf84a8b32007-11-06 10:26:29 -0500315 goto wrapped;
316 }
Chris Mason1a2b2ac2007-12-04 13:18:24 -0500317 goto out;
Chris Masone37c9e62007-05-09 20:13:14 -0400318 }
Chris Mason257d0ce2007-11-07 21:08:16 -0500319 if (cache_miss && !cache->cached) {
320 cache_block_group(root, cache);
321 last = cache_miss;
Chris Mason0ef3e662008-05-24 14:04:53 -0400322 cache = btrfs_lookup_first_block_group(root->fs_info, last);
Chris Mason257d0ce2007-11-07 21:08:16 -0500323 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400324 cache_miss = 0;
Chris Masonc286ac42008-07-22 23:06:41 -0400325 cache = btrfs_find_block_group(root, cache, last, data, 0);
Chris Mason0e4de582007-11-26 10:55:49 -0500326 if (!cache)
327 goto no_cache;
Chris Masone37c9e62007-05-09 20:13:14 -0400328 *cache_ret = cache;
329 goto again;
330}
331
Chris Mason84f54cf2007-06-12 07:43:08 -0400332static u64 div_factor(u64 num, int factor)
333{
Chris Mason257d0ce2007-11-07 21:08:16 -0500334 if (factor == 10)
335 return num;
Chris Mason84f54cf2007-06-12 07:43:08 -0400336 num *= factor;
337 do_div(num, 10);
338 return num;
339}
340
Chris Mason6324fbf2008-03-24 15:01:59 -0400341static int block_group_state_bits(u64 flags)
342{
343 int bits = 0;
344 if (flags & BTRFS_BLOCK_GROUP_DATA)
345 bits |= BLOCK_GROUP_DATA;
346 if (flags & BTRFS_BLOCK_GROUP_METADATA)
347 bits |= BLOCK_GROUP_METADATA;
348 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
349 bits |= BLOCK_GROUP_SYSTEM;
350 return bits;
351}
352
Chris Mason925baed2008-06-25 16:01:30 -0400353static struct btrfs_block_group_cache *
354__btrfs_find_block_group(struct btrfs_root *root,
355 struct btrfs_block_group_cache *hint,
356 u64 search_start, int data, int owner)
Chris Masoncd1bc462007-04-27 10:08:34 -0400357{
Chris Mason96b51792007-10-15 16:15:19 -0400358 struct btrfs_block_group_cache *cache;
Chris Masond1310b22008-01-24 16:13:08 -0500359 struct extent_io_tree *block_group_cache;
Chris Mason31f3c992007-04-30 15:25:45 -0400360 struct btrfs_block_group_cache *found_group = NULL;
Chris Masoncd1bc462007-04-27 10:08:34 -0400361 struct btrfs_fs_info *info = root->fs_info;
362 u64 used;
Chris Mason31f3c992007-04-30 15:25:45 -0400363 u64 last = 0;
Chris Mason96b51792007-10-15 16:15:19 -0400364 u64 start;
365 u64 end;
366 u64 free_check;
367 u64 ptr;
368 int bit;
Chris Masoncd1bc462007-04-27 10:08:34 -0400369 int ret;
Chris Mason31f3c992007-04-30 15:25:45 -0400370 int full_search = 0;
Chris Masonbce4eae2008-04-24 14:42:46 -0400371 int factor = 10;
Chris Mason0ef3e662008-05-24 14:04:53 -0400372 int wrapped = 0;
Chris Masonde428b62007-05-18 13:28:27 -0400373
Chris Mason96b51792007-10-15 16:15:19 -0400374 block_group_cache = &info->block_group_cache;
375
Chris Masona236aed2008-04-29 09:38:00 -0400376 if (data & BTRFS_BLOCK_GROUP_METADATA)
377 factor = 9;
Chris Masonbe744172007-05-06 10:15:01 -0400378
Chris Mason6324fbf2008-03-24 15:01:59 -0400379 bit = block_group_state_bits(data);
Chris Masonbe744172007-05-06 10:15:01 -0400380
Chris Mason0ef3e662008-05-24 14:04:53 -0400381 if (search_start) {
Chris Masonbe744172007-05-06 10:15:01 -0400382 struct btrfs_block_group_cache *shint;
Chris Mason0ef3e662008-05-24 14:04:53 -0400383 shint = btrfs_lookup_first_block_group(info, search_start);
Chris Mason8f18cf12008-04-25 16:53:30 -0400384 if (shint && block_group_bits(shint, data) && !shint->ro) {
Chris Masonc286ac42008-07-22 23:06:41 -0400385 spin_lock(&shint->lock);
Chris Masonbe744172007-05-06 10:15:01 -0400386 used = btrfs_block_group_used(&shint->item);
Yan324ae4d2007-11-16 14:57:08 -0500387 if (used + shint->pinned <
388 div_factor(shint->key.offset, factor)) {
Chris Masonc286ac42008-07-22 23:06:41 -0400389 spin_unlock(&shint->lock);
Chris Masonbe744172007-05-06 10:15:01 -0400390 return shint;
391 }
Chris Masonc286ac42008-07-22 23:06:41 -0400392 spin_unlock(&shint->lock);
Chris Masonbe744172007-05-06 10:15:01 -0400393 }
394 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400395 if (hint && !hint->ro && block_group_bits(hint, data)) {
Chris Masonc286ac42008-07-22 23:06:41 -0400396 spin_lock(&hint->lock);
Chris Mason31f3c992007-04-30 15:25:45 -0400397 used = btrfs_block_group_used(&hint->item);
Yan324ae4d2007-11-16 14:57:08 -0500398 if (used + hint->pinned <
399 div_factor(hint->key.offset, factor)) {
Chris Masonc286ac42008-07-22 23:06:41 -0400400 spin_unlock(&hint->lock);
Chris Mason31f3c992007-04-30 15:25:45 -0400401 return hint;
402 }
Chris Masonc286ac42008-07-22 23:06:41 -0400403 spin_unlock(&hint->lock);
Chris Masone19caa52007-10-15 16:17:44 -0400404 last = hint->key.objectid + hint->key.offset;
Chris Mason31f3c992007-04-30 15:25:45 -0400405 } else {
Chris Masone37c9e62007-05-09 20:13:14 -0400406 if (hint)
Chris Mason0ef3e662008-05-24 14:04:53 -0400407 last = max(hint->key.objectid, search_start);
Chris Masone37c9e62007-05-09 20:13:14 -0400408 else
Chris Mason0ef3e662008-05-24 14:04:53 -0400409 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400410 }
Chris Mason31f3c992007-04-30 15:25:45 -0400411again:
Chris Masoncd1bc462007-04-27 10:08:34 -0400412 while(1) {
Chris Mason96b51792007-10-15 16:15:19 -0400413 ret = find_first_extent_bit(block_group_cache, last,
414 &start, &end, bit);
415 if (ret)
Chris Masoncd1bc462007-04-27 10:08:34 -0400416 break;
Chris Mason96b51792007-10-15 16:15:19 -0400417
418 ret = get_state_private(block_group_cache, start, &ptr);
Chris Mason0ef3e662008-05-24 14:04:53 -0400419 if (ret) {
420 last = end + 1;
421 continue;
422 }
Chris Mason96b51792007-10-15 16:15:19 -0400423
Jens Axboeae2f5412007-10-19 09:22:59 -0400424 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
Chris Masonc286ac42008-07-22 23:06:41 -0400425 spin_lock(&cache->lock);
Chris Mason96b51792007-10-15 16:15:19 -0400426 last = cache->key.objectid + cache->key.offset;
427 used = btrfs_block_group_used(&cache->item);
428
Chris Mason8f18cf12008-04-25 16:53:30 -0400429 if (!cache->ro && block_group_bits(cache, data)) {
Chris Mason0ef3e662008-05-24 14:04:53 -0400430 free_check = div_factor(cache->key.offset, factor);
Chris Mason8790d502008-04-03 16:29:03 -0400431 if (used + cache->pinned < free_check) {
432 found_group = cache;
Chris Masonc286ac42008-07-22 23:06:41 -0400433 spin_unlock(&cache->lock);
Chris Mason8790d502008-04-03 16:29:03 -0400434 goto found;
435 }
Chris Masoncd1bc462007-04-27 10:08:34 -0400436 }
Chris Masonc286ac42008-07-22 23:06:41 -0400437 spin_unlock(&cache->lock);
Chris Masonde428b62007-05-18 13:28:27 -0400438 cond_resched();
Chris Masoncd1bc462007-04-27 10:08:34 -0400439 }
Chris Mason0ef3e662008-05-24 14:04:53 -0400440 if (!wrapped) {
441 last = search_start;
442 wrapped = 1;
443 goto again;
444 }
445 if (!full_search && factor < 10) {
Chris Masonbe744172007-05-06 10:15:01 -0400446 last = search_start;
Chris Mason31f3c992007-04-30 15:25:45 -0400447 full_search = 1;
Chris Mason0ef3e662008-05-24 14:04:53 -0400448 factor = 10;
Chris Mason31f3c992007-04-30 15:25:45 -0400449 goto again;
450 }
Chris Masonbe744172007-05-06 10:15:01 -0400451found:
Chris Mason31f3c992007-04-30 15:25:45 -0400452 return found_group;
Chris Masoncd1bc462007-04-27 10:08:34 -0400453}
454
Chris Mason925baed2008-06-25 16:01:30 -0400455struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root,
456 struct btrfs_block_group_cache
457 *hint, u64 search_start,
458 int data, int owner)
459{
460
461 struct btrfs_block_group_cache *ret;
Chris Mason925baed2008-06-25 16:01:30 -0400462 ret = __btrfs_find_block_group(root, hint, search_start, data, owner);
Chris Mason925baed2008-06-25 16:01:30 -0400463 return ret;
464}
Chris Mason7bb86312007-12-11 09:25:06 -0500465static u64 hash_extent_ref(u64 root_objectid, u64 ref_generation,
Chris Mason74493f72007-12-11 09:25:06 -0500466 u64 owner, u64 owner_offset)
467{
468 u32 high_crc = ~(u32)0;
469 u32 low_crc = ~(u32)0;
470 __le64 lenum;
Chris Mason74493f72007-12-11 09:25:06 -0500471 lenum = cpu_to_le64(root_objectid);
Miguela5eb62e2008-04-11 15:45:51 -0400472 high_crc = btrfs_crc32c(high_crc, &lenum, sizeof(lenum));
Chris Mason7bb86312007-12-11 09:25:06 -0500473 lenum = cpu_to_le64(ref_generation);
Miguela5eb62e2008-04-11 15:45:51 -0400474 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
Chris Mason21a49892008-02-01 14:51:59 -0500475 if (owner >= BTRFS_FIRST_FREE_OBJECTID) {
476 lenum = cpu_to_le64(owner);
Miguela5eb62e2008-04-11 15:45:51 -0400477 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
Chris Mason21a49892008-02-01 14:51:59 -0500478 lenum = cpu_to_le64(owner_offset);
Miguela5eb62e2008-04-11 15:45:51 -0400479 low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum));
Chris Mason21a49892008-02-01 14:51:59 -0500480 }
Chris Mason74493f72007-12-11 09:25:06 -0500481 return ((u64)high_crc << 32) | (u64)low_crc;
482}
483
Chris Mason7bb86312007-12-11 09:25:06 -0500484static int match_extent_ref(struct extent_buffer *leaf,
485 struct btrfs_extent_ref *disk_ref,
486 struct btrfs_extent_ref *cpu_ref)
487{
488 int ret;
489 int len;
490
491 if (cpu_ref->objectid)
492 len = sizeof(*cpu_ref);
493 else
494 len = 2 * sizeof(u64);
495 ret = memcmp_extent_buffer(leaf, cpu_ref, (unsigned long)disk_ref,
496 len);
497 return ret == 0;
498}
499
Chris Masone02119d2008-09-05 16:13:11 -0400500/* simple helper to search for an existing extent at a given offset */
501int btrfs_lookup_extent(struct btrfs_root *root, struct btrfs_path *path,
502 u64 start, u64 len)
503{
504 int ret;
505 struct btrfs_key key;
506
507 maybe_lock_mutex(root);
508 key.objectid = start;
509 key.offset = len;
510 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
511 ret = btrfs_search_slot(NULL, root->fs_info->extent_root, &key, path,
512 0, 0);
513 maybe_unlock_mutex(root);
514 return ret;
515}
516
Chris Mason98ed5172008-01-03 10:01:48 -0500517static int noinline lookup_extent_backref(struct btrfs_trans_handle *trans,
518 struct btrfs_root *root,
519 struct btrfs_path *path, u64 bytenr,
520 u64 root_objectid,
521 u64 ref_generation, u64 owner,
522 u64 owner_offset, int del)
Chris Mason7bb86312007-12-11 09:25:06 -0500523{
524 u64 hash;
525 struct btrfs_key key;
526 struct btrfs_key found_key;
527 struct btrfs_extent_ref ref;
528 struct extent_buffer *leaf;
529 struct btrfs_extent_ref *disk_ref;
530 int ret;
531 int ret2;
532
533 btrfs_set_stack_ref_root(&ref, root_objectid);
534 btrfs_set_stack_ref_generation(&ref, ref_generation);
535 btrfs_set_stack_ref_objectid(&ref, owner);
536 btrfs_set_stack_ref_offset(&ref, owner_offset);
537
538 hash = hash_extent_ref(root_objectid, ref_generation, owner,
539 owner_offset);
540 key.offset = hash;
541 key.objectid = bytenr;
542 key.type = BTRFS_EXTENT_REF_KEY;
543
544 while (1) {
545 ret = btrfs_search_slot(trans, root, &key, path,
546 del ? -1 : 0, del);
547 if (ret < 0)
548 goto out;
549 leaf = path->nodes[0];
550 if (ret != 0) {
551 u32 nritems = btrfs_header_nritems(leaf);
552 if (path->slots[0] >= nritems) {
553 ret2 = btrfs_next_leaf(root, path);
554 if (ret2)
555 goto out;
556 leaf = path->nodes[0];
557 }
558 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
559 if (found_key.objectid != bytenr ||
560 found_key.type != BTRFS_EXTENT_REF_KEY)
561 goto out;
562 key.offset = found_key.offset;
563 if (del) {
564 btrfs_release_path(root, path);
565 continue;
566 }
567 }
568 disk_ref = btrfs_item_ptr(path->nodes[0],
569 path->slots[0],
570 struct btrfs_extent_ref);
571 if (match_extent_ref(path->nodes[0], disk_ref, &ref)) {
572 ret = 0;
573 goto out;
574 }
575 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
576 key.offset = found_key.offset + 1;
577 btrfs_release_path(root, path);
578 }
579out:
580 return ret;
581}
582
Chris Masond8d5f3e2007-12-11 12:42:00 -0500583/*
584 * Back reference rules. Back refs have three main goals:
585 *
586 * 1) differentiate between all holders of references to an extent so that
587 * when a reference is dropped we can make sure it was a valid reference
588 * before freeing the extent.
589 *
590 * 2) Provide enough information to quickly find the holders of an extent
591 * if we notice a given block is corrupted or bad.
592 *
593 * 3) Make it easy to migrate blocks for FS shrinking or storage pool
594 * maintenance. This is actually the same as #2, but with a slightly
595 * different use case.
596 *
597 * File extents can be referenced by:
598 *
599 * - multiple snapshots, subvolumes, or different generations in one subvol
600 * - different files inside a single subvolume (in theory, not implemented yet)
601 * - different offsets inside a file (bookend extents in file.c)
602 *
603 * The extent ref structure has fields for:
604 *
605 * - Objectid of the subvolume root
606 * - Generation number of the tree holding the reference
607 * - objectid of the file holding the reference
608 * - offset in the file corresponding to the key holding the reference
609 *
610 * When a file extent is allocated the fields are filled in:
611 * (root_key.objectid, trans->transid, inode objectid, offset in file)
612 *
613 * When a leaf is cow'd new references are added for every file extent found
614 * in the leaf. It looks the same as the create case, but trans->transid
615 * will be different when the block is cow'd.
616 *
617 * (root_key.objectid, trans->transid, inode objectid, offset in file)
618 *
619 * When a file extent is removed either during snapshot deletion or file
620 * truncation, the corresponding back reference is found
621 * by searching for:
622 *
623 * (btrfs_header_owner(leaf), btrfs_header_generation(leaf),
624 * inode objectid, offset in file)
625 *
626 * Btree extents can be referenced by:
627 *
628 * - Different subvolumes
629 * - Different generations of the same subvolume
630 *
631 * Storing sufficient information for a full reverse mapping of a btree
632 * block would require storing the lowest key of the block in the backref,
633 * and it would require updating that lowest key either before write out or
634 * every time it changed. Instead, the objectid of the lowest key is stored
635 * along with the level of the tree block. This provides a hint
636 * about where in the btree the block can be found. Searches through the
637 * btree only need to look for a pointer to that block, so they stop one
638 * level higher than the level recorded in the backref.
639 *
640 * Some btrees do not do reference counting on their extents. These
641 * include the extent tree and the tree of tree roots. Backrefs for these
642 * trees always have a generation of zero.
643 *
644 * When a tree block is created, back references are inserted:
645 *
Chris Masonf6dbff52007-12-13 11:13:32 -0500646 * (root->root_key.objectid, trans->transid or zero, level, lowest_key_objectid)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500647 *
648 * When a tree block is cow'd in a reference counted root,
649 * new back references are added for all the blocks it points to.
650 * These are of the form (trans->transid will have increased since creation):
651 *
Chris Masonf6dbff52007-12-13 11:13:32 -0500652 * (root->root_key.objectid, trans->transid, level, lowest_key_objectid)
Chris Masond8d5f3e2007-12-11 12:42:00 -0500653 *
654 * Because the lowest_key_objectid and the level are just hints
655 * they are not used when backrefs are deleted. When a backref is deleted:
656 *
657 * if backref was for a tree root:
658 * root_objectid = root->root_key.objectid
659 * else
660 * root_objectid = btrfs_header_owner(parent)
661 *
662 * (root_objectid, btrfs_header_generation(parent) or zero, 0, 0)
663 *
664 * Back Reference Key hashing:
665 *
666 * Back references have four fields, each 64 bits long. Unfortunately,
667 * This is hashed into a single 64 bit number and placed into the key offset.
668 * The key objectid corresponds to the first byte in the extent, and the
669 * key type is set to BTRFS_EXTENT_REF_KEY
670 */
Chris Mason7bb86312007-12-11 09:25:06 -0500671int btrfs_insert_extent_backref(struct btrfs_trans_handle *trans,
672 struct btrfs_root *root,
673 struct btrfs_path *path, u64 bytenr,
674 u64 root_objectid, u64 ref_generation,
675 u64 owner, u64 owner_offset)
Chris Mason74493f72007-12-11 09:25:06 -0500676{
677 u64 hash;
678 struct btrfs_key key;
679 struct btrfs_extent_ref ref;
Chris Mason7bb86312007-12-11 09:25:06 -0500680 struct btrfs_extent_ref *disk_ref;
Chris Mason74493f72007-12-11 09:25:06 -0500681 int ret;
682
683 btrfs_set_stack_ref_root(&ref, root_objectid);
Chris Mason7bb86312007-12-11 09:25:06 -0500684 btrfs_set_stack_ref_generation(&ref, ref_generation);
Chris Mason74493f72007-12-11 09:25:06 -0500685 btrfs_set_stack_ref_objectid(&ref, owner);
686 btrfs_set_stack_ref_offset(&ref, owner_offset);
687
Chris Mason7bb86312007-12-11 09:25:06 -0500688 hash = hash_extent_ref(root_objectid, ref_generation, owner,
689 owner_offset);
Chris Mason74493f72007-12-11 09:25:06 -0500690 key.offset = hash;
691 key.objectid = bytenr;
692 key.type = BTRFS_EXTENT_REF_KEY;
693
694 ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(ref));
695 while (ret == -EEXIST) {
Chris Mason7bb86312007-12-11 09:25:06 -0500696 disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
697 struct btrfs_extent_ref);
698 if (match_extent_ref(path->nodes[0], disk_ref, &ref))
699 goto out;
700 key.offset++;
701 btrfs_release_path(root, path);
702 ret = btrfs_insert_empty_item(trans, root, path, &key,
703 sizeof(ref));
Chris Mason74493f72007-12-11 09:25:06 -0500704 }
Chris Mason7bb86312007-12-11 09:25:06 -0500705 if (ret)
706 goto out;
707 disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
708 struct btrfs_extent_ref);
709 write_extent_buffer(path->nodes[0], &ref, (unsigned long)disk_ref,
710 sizeof(ref));
711 btrfs_mark_buffer_dirty(path->nodes[0]);
712out:
713 btrfs_release_path(root, path);
714 return ret;
Chris Mason74493f72007-12-11 09:25:06 -0500715}
716
Chris Mason925baed2008-06-25 16:01:30 -0400717static int __btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
Chris Masonb18c6682007-04-17 13:26:50 -0400718 struct btrfs_root *root,
Chris Mason74493f72007-12-11 09:25:06 -0500719 u64 bytenr, u64 num_bytes,
Chris Mason7bb86312007-12-11 09:25:06 -0500720 u64 root_objectid, u64 ref_generation,
Chris Mason74493f72007-12-11 09:25:06 -0500721 u64 owner, u64 owner_offset)
Chris Mason02217ed2007-03-02 16:08:05 -0500722{
Chris Mason5caf2a02007-04-02 11:20:42 -0400723 struct btrfs_path *path;
Chris Mason02217ed2007-03-02 16:08:05 -0500724 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400725 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400726 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -0400727 struct btrfs_extent_item *item;
Chris Masoncf27e1e2007-03-13 09:49:06 -0400728 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -0500729
Chris Masondb945352007-10-15 16:15:53 -0400730 WARN_ON(num_bytes < root->sectorsize);
Chris Mason5caf2a02007-04-02 11:20:42 -0400731 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -0400732 if (!path)
733 return -ENOMEM;
Chris Mason26b80032007-08-08 20:17:12 -0400734
Chris Mason3c12ac72008-04-21 12:01:38 -0400735 path->reada = 1;
Chris Masondb945352007-10-15 16:15:53 -0400736 key.objectid = bytenr;
Chris Mason62e27492007-03-15 12:56:47 -0400737 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masondb945352007-10-15 16:15:53 -0400738 key.offset = num_bytes;
Chris Mason5caf2a02007-04-02 11:20:42 -0400739 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400740 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -0400741 if (ret < 0)
742 return ret;
Chris Masona429e512007-04-18 16:15:28 -0400743 if (ret != 0) {
Chris Masona28ec192007-03-06 20:08:01 -0500744 BUG();
Chris Masona429e512007-04-18 16:15:28 -0400745 }
Chris Mason02217ed2007-03-02 16:08:05 -0500746 BUG_ON(ret != 0);
Chris Mason5f39d392007-10-15 16:14:19 -0400747 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -0400748 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400749 refs = btrfs_extent_refs(l, item);
750 btrfs_set_extent_refs(l, item, refs + 1);
Chris Mason5caf2a02007-04-02 11:20:42 -0400751 btrfs_mark_buffer_dirty(path->nodes[0]);
Chris Masona28ec192007-03-06 20:08:01 -0500752
Chris Mason5caf2a02007-04-02 11:20:42 -0400753 btrfs_release_path(root->fs_info->extent_root, path);
Chris Mason7bb86312007-12-11 09:25:06 -0500754
Chris Mason3c12ac72008-04-21 12:01:38 -0400755 path->reada = 1;
Chris Mason7bb86312007-12-11 09:25:06 -0500756 ret = btrfs_insert_extent_backref(trans, root->fs_info->extent_root,
757 path, bytenr, root_objectid,
758 ref_generation, owner, owner_offset);
759 BUG_ON(ret);
Chris Mason9f5fae22007-03-20 14:38:32 -0400760 finish_current_insert(trans, root->fs_info->extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -0400761 del_pending_extents(trans, root->fs_info->extent_root);
Chris Mason74493f72007-12-11 09:25:06 -0500762
763 btrfs_free_path(path);
Chris Mason02217ed2007-03-02 16:08:05 -0500764 return 0;
765}
766
Chris Mason925baed2008-06-25 16:01:30 -0400767int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
768 struct btrfs_root *root,
769 u64 bytenr, u64 num_bytes,
770 u64 root_objectid, u64 ref_generation,
771 u64 owner, u64 owner_offset)
772{
773 int ret;
774
775 mutex_lock(&root->fs_info->alloc_mutex);
776 ret = __btrfs_inc_extent_ref(trans, root, bytenr, num_bytes,
777 root_objectid, ref_generation,
778 owner, owner_offset);
779 mutex_unlock(&root->fs_info->alloc_mutex);
780 return ret;
781}
782
Chris Masone9d0b132007-08-10 14:06:19 -0400783int btrfs_extent_post_op(struct btrfs_trans_handle *trans,
784 struct btrfs_root *root)
785{
786 finish_current_insert(trans, root->fs_info->extent_root);
787 del_pending_extents(trans, root->fs_info->extent_root);
788 return 0;
789}
790
Chris Masonb18c6682007-04-17 13:26:50 -0400791static int lookup_extent_ref(struct btrfs_trans_handle *trans,
Chris Masondb945352007-10-15 16:15:53 -0400792 struct btrfs_root *root, u64 bytenr,
793 u64 num_bytes, u32 *refs)
Chris Masona28ec192007-03-06 20:08:01 -0500794{
Chris Mason5caf2a02007-04-02 11:20:42 -0400795 struct btrfs_path *path;
Chris Masona28ec192007-03-06 20:08:01 -0500796 int ret;
Chris Masone2fa7222007-03-12 16:22:34 -0400797 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -0400798 struct extent_buffer *l;
Chris Mason234b63a2007-03-13 10:46:10 -0400799 struct btrfs_extent_item *item;
Chris Mason5caf2a02007-04-02 11:20:42 -0400800
Chris Masondb945352007-10-15 16:15:53 -0400801 WARN_ON(num_bytes < root->sectorsize);
Chris Mason5caf2a02007-04-02 11:20:42 -0400802 path = btrfs_alloc_path();
Chris Mason3c12ac72008-04-21 12:01:38 -0400803 path->reada = 1;
Chris Masondb945352007-10-15 16:15:53 -0400804 key.objectid = bytenr;
805 key.offset = num_bytes;
Chris Mason62e27492007-03-15 12:56:47 -0400806 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Mason5caf2a02007-04-02 11:20:42 -0400807 ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path,
Chris Mason9f5fae22007-03-20 14:38:32 -0400808 0, 0);
Chris Mason54aa1f42007-06-22 14:16:25 -0400809 if (ret < 0)
810 goto out;
Chris Mason5f39d392007-10-15 16:14:19 -0400811 if (ret != 0) {
812 btrfs_print_leaf(root, path->nodes[0]);
Chris Masondb945352007-10-15 16:15:53 -0400813 printk("failed to find block number %Lu\n", bytenr);
Chris Masona28ec192007-03-06 20:08:01 -0500814 BUG();
Chris Mason5f39d392007-10-15 16:14:19 -0400815 }
816 l = path->nodes[0];
Chris Mason5caf2a02007-04-02 11:20:42 -0400817 item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -0400818 *refs = btrfs_extent_refs(l, item);
Chris Mason54aa1f42007-06-22 14:16:25 -0400819out:
Chris Mason5caf2a02007-04-02 11:20:42 -0400820 btrfs_free_path(path);
Chris Masona28ec192007-03-06 20:08:01 -0500821 return 0;
822}
823
Yan Zhengf321e492008-07-30 09:26:11 -0400824
825static int get_reference_status(struct btrfs_root *root, u64 bytenr,
826 u64 parent_gen, u64 ref_objectid,
827 u64 *min_generation, u32 *ref_count)
Chris Masonbe20aa92007-12-17 20:14:01 -0500828{
829 struct btrfs_root *extent_root = root->fs_info->extent_root;
830 struct btrfs_path *path;
Yan Zhengf321e492008-07-30 09:26:11 -0400831 struct extent_buffer *leaf;
832 struct btrfs_extent_ref *ref_item;
Chris Masonbe20aa92007-12-17 20:14:01 -0500833 struct btrfs_key key;
834 struct btrfs_key found_key;
Yan Zhengf321e492008-07-30 09:26:11 -0400835 u64 root_objectid = root->root_key.objectid;
836 u64 ref_generation;
837 u32 nritems;
838 int ret;
Chris Masonbe20aa92007-12-17 20:14:01 -0500839
Chris Masonbe20aa92007-12-17 20:14:01 -0500840 key.objectid = bytenr;
841 key.offset = 0;
Yan Zhengf321e492008-07-30 09:26:11 -0400842 key.type = BTRFS_EXTENT_ITEM_KEY;
Chris Masonbe20aa92007-12-17 20:14:01 -0500843
Yan Zhengf321e492008-07-30 09:26:11 -0400844 path = btrfs_alloc_path();
845 mutex_lock(&root->fs_info->alloc_mutex);
Chris Masonbe20aa92007-12-17 20:14:01 -0500846 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
847 if (ret < 0)
848 goto out;
849 BUG_ON(ret == 0);
850
Yan Zhengf321e492008-07-30 09:26:11 -0400851 leaf = path->nodes[0];
852 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -0500853
854 if (found_key.objectid != bytenr ||
855 found_key.type != BTRFS_EXTENT_ITEM_KEY) {
Yan Zhengf321e492008-07-30 09:26:11 -0400856 ret = 1;
Chris Masonbe20aa92007-12-17 20:14:01 -0500857 goto out;
858 }
859
Yan Zhengf321e492008-07-30 09:26:11 -0400860 *ref_count = 0;
861 *min_generation = (u64)-1;
862
Chris Masonbe20aa92007-12-17 20:14:01 -0500863 while (1) {
Yan Zhengf321e492008-07-30 09:26:11 -0400864 leaf = path->nodes[0];
865 nritems = btrfs_header_nritems(leaf);
Chris Masonbe20aa92007-12-17 20:14:01 -0500866 if (path->slots[0] >= nritems) {
867 ret = btrfs_next_leaf(extent_root, path);
Yan Zhengf321e492008-07-30 09:26:11 -0400868 if (ret < 0)
869 goto out;
Chris Masonbe20aa92007-12-17 20:14:01 -0500870 if (ret == 0)
871 continue;
872 break;
873 }
Yan Zhengf321e492008-07-30 09:26:11 -0400874 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masonbe20aa92007-12-17 20:14:01 -0500875 if (found_key.objectid != bytenr)
876 break;
Chris Masonbd098352008-01-03 13:23:19 -0500877
Chris Masonbe20aa92007-12-17 20:14:01 -0500878 if (found_key.type != BTRFS_EXTENT_REF_KEY) {
879 path->slots[0]++;
880 continue;
881 }
882
Yan Zhengf321e492008-07-30 09:26:11 -0400883 ref_item = btrfs_item_ptr(leaf, path->slots[0],
Chris Masonbe20aa92007-12-17 20:14:01 -0500884 struct btrfs_extent_ref);
Yan Zhengf321e492008-07-30 09:26:11 -0400885 ref_generation = btrfs_ref_generation(leaf, ref_item);
886 /*
887 * For (parent_gen > 0 && parent_gen > ref_gen):
888 *
Yanbcc63ab2008-07-30 16:29:20 -0400889 * we reach here through the oldest root, therefore
890 * all other reference from same snapshot should have
Yan Zhengf321e492008-07-30 09:26:11 -0400891 * a larger generation.
892 */
893 if ((root_objectid != btrfs_ref_root(leaf, ref_item)) ||
894 (parent_gen > 0 && parent_gen > ref_generation) ||
895 (ref_objectid >= BTRFS_FIRST_FREE_OBJECTID &&
896 ref_objectid != btrfs_ref_objectid(leaf, ref_item))) {
897 if (ref_count)
898 *ref_count = 2;
899 break;
900 }
Chris Masonbe20aa92007-12-17 20:14:01 -0500901
Yan Zhengf321e492008-07-30 09:26:11 -0400902 *ref_count = 1;
903 if (*min_generation > ref_generation)
904 *min_generation = ref_generation;
905
Chris Masonbe20aa92007-12-17 20:14:01 -0500906 path->slots[0]++;
907 }
Yan Zhengf321e492008-07-30 09:26:11 -0400908 ret = 0;
Chris Masonbe20aa92007-12-17 20:14:01 -0500909out:
Chris Mason925baed2008-06-25 16:01:30 -0400910 mutex_unlock(&root->fs_info->alloc_mutex);
Yan Zhengf321e492008-07-30 09:26:11 -0400911 btrfs_free_path(path);
912 return ret;
913}
914
Yan Zheng7ea394f2008-08-05 13:05:02 -0400915int btrfs_cross_ref_exists(struct btrfs_trans_handle *trans,
916 struct btrfs_root *root,
Yan Zhengf321e492008-07-30 09:26:11 -0400917 struct btrfs_key *key, u64 bytenr)
918{
Yan Zhengf321e492008-07-30 09:26:11 -0400919 struct btrfs_root *old_root;
920 struct btrfs_path *path = NULL;
921 struct extent_buffer *eb;
922 struct btrfs_file_extent_item *item;
923 u64 ref_generation;
924 u64 min_generation;
925 u64 extent_start;
926 u32 ref_count;
927 int level;
928 int ret;
929
Yan Zheng7ea394f2008-08-05 13:05:02 -0400930 BUG_ON(trans == NULL);
Yan Zhengf321e492008-07-30 09:26:11 -0400931 BUG_ON(key->type != BTRFS_EXTENT_DATA_KEY);
932 ret = get_reference_status(root, bytenr, 0, key->objectid,
933 &min_generation, &ref_count);
934 if (ret)
935 return ret;
936
937 if (ref_count != 1)
938 return 1;
939
Yan Zhengf321e492008-07-30 09:26:11 -0400940 old_root = root->dirty_root->root;
941 ref_generation = old_root->root_key.offset;
942
943 /* all references are created in running transaction */
944 if (min_generation > ref_generation) {
945 ret = 0;
946 goto out;
947 }
948
949 path = btrfs_alloc_path();
950 if (!path) {
951 ret = -ENOMEM;
952 goto out;
953 }
954
955 path->skip_locking = 1;
956 /* if no item found, the extent is referenced by other snapshot */
957 ret = btrfs_search_slot(NULL, old_root, key, path, 0, 0);
958 if (ret)
959 goto out;
960
961 eb = path->nodes[0];
962 item = btrfs_item_ptr(eb, path->slots[0],
963 struct btrfs_file_extent_item);
964 if (btrfs_file_extent_type(eb, item) != BTRFS_FILE_EXTENT_REG ||
965 btrfs_file_extent_disk_bytenr(eb, item) != bytenr) {
966 ret = 1;
967 goto out;
968 }
969
970 for (level = BTRFS_MAX_LEVEL - 1; level >= -1; level--) {
971 if (level >= 0) {
972 eb = path->nodes[level];
973 if (!eb)
974 continue;
975 extent_start = eb->start;
Yanbcc63ab2008-07-30 16:29:20 -0400976 } else
Yan Zhengf321e492008-07-30 09:26:11 -0400977 extent_start = bytenr;
978
979 ret = get_reference_status(root, extent_start, ref_generation,
980 0, &min_generation, &ref_count);
981 if (ret)
982 goto out;
983
984 if (ref_count != 1) {
985 ret = 1;
986 goto out;
987 }
988 if (level >= 0)
989 ref_generation = btrfs_header_generation(eb);
990 }
991 ret = 0;
992out:
993 if (path)
994 btrfs_free_path(path);
Yan Zhengf321e492008-07-30 09:26:11 -0400995 return ret;
Chris Masonbe20aa92007-12-17 20:14:01 -0500996}
Chris Masonc5739bb2007-04-10 09:27:04 -0400997
Chris Masone089f052007-03-16 16:20:31 -0400998int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
Yan Zheng31153d82008-07-28 15:32:19 -0400999 struct extent_buffer *buf, int cache_ref)
Chris Mason02217ed2007-03-02 16:08:05 -05001000{
Chris Masondb945352007-10-15 16:15:53 -04001001 u64 bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -04001002 u32 nritems;
1003 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04001004 struct btrfs_file_extent_item *fi;
Chris Mason02217ed2007-03-02 16:08:05 -05001005 int i;
Chris Masondb945352007-10-15 16:15:53 -04001006 int level;
Chris Mason6407bf62007-03-27 06:33:00 -04001007 int ret;
Chris Mason54aa1f42007-06-22 14:16:25 -04001008 int faili;
Yan Zheng31153d82008-07-28 15:32:19 -04001009 int nr_file_extents = 0;
Chris Masona28ec192007-03-06 20:08:01 -05001010
Chris Mason3768f362007-03-13 16:47:54 -04001011 if (!root->ref_cows)
Chris Masona28ec192007-03-06 20:08:01 -05001012 return 0;
Chris Mason5f39d392007-10-15 16:14:19 -04001013
Chris Masondb945352007-10-15 16:15:53 -04001014 level = btrfs_header_level(buf);
Chris Mason5f39d392007-10-15 16:14:19 -04001015 nritems = btrfs_header_nritems(buf);
1016 for (i = 0; i < nritems; i++) {
Chris Masone34a5b42008-07-22 12:08:37 -04001017 cond_resched();
Chris Masondb945352007-10-15 16:15:53 -04001018 if (level == 0) {
1019 u64 disk_bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -04001020 btrfs_item_key_to_cpu(buf, &key, i);
1021 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -04001022 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001023 fi = btrfs_item_ptr(buf, i,
Chris Mason6407bf62007-03-27 06:33:00 -04001024 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001025 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason236454d2007-04-19 13:37:44 -04001026 BTRFS_FILE_EXTENT_INLINE)
1027 continue;
Chris Masondb945352007-10-15 16:15:53 -04001028 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1029 if (disk_bytenr == 0)
Chris Mason3a686372007-05-24 13:35:57 -04001030 continue;
Chris Mason4a096752008-07-21 10:29:44 -04001031
Yan Zheng31153d82008-07-28 15:32:19 -04001032 if (buf != root->commit_root)
1033 nr_file_extents++;
1034
Chris Mason4a096752008-07-21 10:29:44 -04001035 mutex_lock(&root->fs_info->alloc_mutex);
Chris Mason925baed2008-06-25 16:01:30 -04001036 ret = __btrfs_inc_extent_ref(trans, root, disk_bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -05001037 btrfs_file_extent_disk_num_bytes(buf, fi),
1038 root->root_key.objectid, trans->transid,
1039 key.objectid, key.offset);
Chris Mason4a096752008-07-21 10:29:44 -04001040 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Mason54aa1f42007-06-22 14:16:25 -04001041 if (ret) {
1042 faili = i;
Chris Mason4a096752008-07-21 10:29:44 -04001043 WARN_ON(1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001044 goto fail;
1045 }
Chris Mason6407bf62007-03-27 06:33:00 -04001046 } else {
Chris Masondb945352007-10-15 16:15:53 -04001047 bytenr = btrfs_node_blockptr(buf, i);
Chris Mason6caab482007-12-13 09:48:07 -05001048 btrfs_node_key_to_cpu(buf, &key, i);
Chris Mason4a096752008-07-21 10:29:44 -04001049
1050 mutex_lock(&root->fs_info->alloc_mutex);
Chris Mason925baed2008-06-25 16:01:30 -04001051 ret = __btrfs_inc_extent_ref(trans, root, bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -05001052 btrfs_level_size(root, level - 1),
1053 root->root_key.objectid,
Chris Masonf6dbff52007-12-13 11:13:32 -05001054 trans->transid,
1055 level - 1, key.objectid);
Chris Mason4a096752008-07-21 10:29:44 -04001056 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Mason54aa1f42007-06-22 14:16:25 -04001057 if (ret) {
1058 faili = i;
Chris Mason4a096752008-07-21 10:29:44 -04001059 WARN_ON(1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001060 goto fail;
1061 }
Chris Mason6407bf62007-03-27 06:33:00 -04001062 }
Chris Mason02217ed2007-03-02 16:08:05 -05001063 }
Yan Zheng31153d82008-07-28 15:32:19 -04001064 /* cache orignal leaf block's references */
1065 if (level == 0 && cache_ref && buf != root->commit_root) {
1066 struct btrfs_leaf_ref *ref;
1067 struct btrfs_extent_info *info;
1068
Yanbcc63ab2008-07-30 16:29:20 -04001069 ref = btrfs_alloc_leaf_ref(root, nr_file_extents);
Yan Zheng31153d82008-07-28 15:32:19 -04001070 if (!ref) {
1071 WARN_ON(1);
1072 goto out;
1073 }
1074
Chris Mason47ac14f2008-07-31 09:46:18 -04001075 ref->root_gen = root->root_key.offset;
Yan Zheng31153d82008-07-28 15:32:19 -04001076 ref->bytenr = buf->start;
1077 ref->owner = btrfs_header_owner(buf);
1078 ref->generation = btrfs_header_generation(buf);
1079 ref->nritems = nr_file_extents;
1080 info = ref->extents;
Yanbcc63ab2008-07-30 16:29:20 -04001081
Yan Zheng31153d82008-07-28 15:32:19 -04001082 for (i = 0; nr_file_extents > 0 && i < nritems; i++) {
1083 u64 disk_bytenr;
1084 btrfs_item_key_to_cpu(buf, &key, i);
1085 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
1086 continue;
1087 fi = btrfs_item_ptr(buf, i,
1088 struct btrfs_file_extent_item);
1089 if (btrfs_file_extent_type(buf, fi) ==
1090 BTRFS_FILE_EXTENT_INLINE)
1091 continue;
1092 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1093 if (disk_bytenr == 0)
1094 continue;
1095
1096 info->bytenr = disk_bytenr;
1097 info->num_bytes =
1098 btrfs_file_extent_disk_num_bytes(buf, fi);
1099 info->objectid = key.objectid;
1100 info->offset = key.offset;
1101 info++;
1102 }
1103
1104 BUG_ON(!root->ref_tree);
1105 ret = btrfs_add_leaf_ref(root, ref);
1106 WARN_ON(ret);
Yanbcc63ab2008-07-30 16:29:20 -04001107 btrfs_free_leaf_ref(root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04001108 }
1109out:
Chris Mason02217ed2007-03-02 16:08:05 -05001110 return 0;
Chris Mason54aa1f42007-06-22 14:16:25 -04001111fail:
Chris Masonccd467d2007-06-28 15:57:36 -04001112 WARN_ON(1);
Chris Mason7bb86312007-12-11 09:25:06 -05001113#if 0
Chris Mason54aa1f42007-06-22 14:16:25 -04001114 for (i =0; i < faili; i++) {
Chris Masondb945352007-10-15 16:15:53 -04001115 if (level == 0) {
1116 u64 disk_bytenr;
Chris Mason5f39d392007-10-15 16:14:19 -04001117 btrfs_item_key_to_cpu(buf, &key, i);
1118 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason54aa1f42007-06-22 14:16:25 -04001119 continue;
Chris Mason5f39d392007-10-15 16:14:19 -04001120 fi = btrfs_item_ptr(buf, i,
Chris Mason54aa1f42007-06-22 14:16:25 -04001121 struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001122 if (btrfs_file_extent_type(buf, fi) ==
Chris Mason54aa1f42007-06-22 14:16:25 -04001123 BTRFS_FILE_EXTENT_INLINE)
1124 continue;
Chris Masondb945352007-10-15 16:15:53 -04001125 disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi);
1126 if (disk_bytenr == 0)
Chris Mason54aa1f42007-06-22 14:16:25 -04001127 continue;
Chris Masondb945352007-10-15 16:15:53 -04001128 err = btrfs_free_extent(trans, root, disk_bytenr,
1129 btrfs_file_extent_disk_num_bytes(buf,
Chris Mason5f39d392007-10-15 16:14:19 -04001130 fi), 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001131 BUG_ON(err);
1132 } else {
Chris Masondb945352007-10-15 16:15:53 -04001133 bytenr = btrfs_node_blockptr(buf, i);
1134 err = btrfs_free_extent(trans, root, bytenr,
1135 btrfs_level_size(root, level - 1), 0);
Chris Mason54aa1f42007-06-22 14:16:25 -04001136 BUG_ON(err);
1137 }
1138 }
Chris Mason7bb86312007-12-11 09:25:06 -05001139#endif
Chris Mason54aa1f42007-06-22 14:16:25 -04001140 return ret;
Chris Mason02217ed2007-03-02 16:08:05 -05001141}
1142
Chris Mason9078a3e2007-04-26 16:46:15 -04001143static int write_one_cache_group(struct btrfs_trans_handle *trans,
1144 struct btrfs_root *root,
1145 struct btrfs_path *path,
1146 struct btrfs_block_group_cache *cache)
1147{
1148 int ret;
1149 int pending_ret;
1150 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04001151 unsigned long bi;
1152 struct extent_buffer *leaf;
Chris Mason9078a3e2007-04-26 16:46:15 -04001153
Chris Mason9078a3e2007-04-26 16:46:15 -04001154 ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1);
Chris Mason54aa1f42007-06-22 14:16:25 -04001155 if (ret < 0)
1156 goto fail;
Chris Mason9078a3e2007-04-26 16:46:15 -04001157 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04001158
1159 leaf = path->nodes[0];
1160 bi = btrfs_item_ptr_offset(leaf, path->slots[0]);
1161 write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item));
1162 btrfs_mark_buffer_dirty(leaf);
Chris Mason9078a3e2007-04-26 16:46:15 -04001163 btrfs_release_path(extent_root, path);
Chris Mason54aa1f42007-06-22 14:16:25 -04001164fail:
Chris Mason9078a3e2007-04-26 16:46:15 -04001165 finish_current_insert(trans, extent_root);
1166 pending_ret = del_pending_extents(trans, extent_root);
1167 if (ret)
1168 return ret;
1169 if (pending_ret)
1170 return pending_ret;
1171 return 0;
1172
1173}
1174
Chris Mason96b51792007-10-15 16:15:19 -04001175int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans,
1176 struct btrfs_root *root)
Chris Mason9078a3e2007-04-26 16:46:15 -04001177{
Chris Masond1310b22008-01-24 16:13:08 -05001178 struct extent_io_tree *block_group_cache;
Chris Mason96b51792007-10-15 16:15:19 -04001179 struct btrfs_block_group_cache *cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04001180 int ret;
1181 int err = 0;
1182 int werr = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04001183 struct btrfs_path *path;
Chris Mason96b51792007-10-15 16:15:19 -04001184 u64 last = 0;
1185 u64 start;
1186 u64 end;
1187 u64 ptr;
Chris Mason9078a3e2007-04-26 16:46:15 -04001188
Chris Mason96b51792007-10-15 16:15:19 -04001189 block_group_cache = &root->fs_info->block_group_cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04001190 path = btrfs_alloc_path();
1191 if (!path)
1192 return -ENOMEM;
1193
Chris Mason925baed2008-06-25 16:01:30 -04001194 mutex_lock(&root->fs_info->alloc_mutex);
Chris Mason9078a3e2007-04-26 16:46:15 -04001195 while(1) {
Chris Mason96b51792007-10-15 16:15:19 -04001196 ret = find_first_extent_bit(block_group_cache, last,
1197 &start, &end, BLOCK_GROUP_DIRTY);
1198 if (ret)
Chris Mason9078a3e2007-04-26 16:46:15 -04001199 break;
Chris Mason54aa1f42007-06-22 14:16:25 -04001200
Chris Mason96b51792007-10-15 16:15:19 -04001201 last = end + 1;
1202 ret = get_state_private(block_group_cache, start, &ptr);
1203 if (ret)
1204 break;
Jens Axboeae2f5412007-10-19 09:22:59 -04001205 cache = (struct btrfs_block_group_cache *)(unsigned long)ptr;
Chris Mason96b51792007-10-15 16:15:19 -04001206 err = write_one_cache_group(trans, root,
1207 path, cache);
1208 /*
1209 * if we fail to write the cache group, we want
1210 * to keep it marked dirty in hopes that a later
1211 * write will work
1212 */
1213 if (err) {
1214 werr = err;
1215 continue;
Chris Mason9078a3e2007-04-26 16:46:15 -04001216 }
Chris Mason96b51792007-10-15 16:15:19 -04001217 clear_extent_bits(block_group_cache, start, end,
1218 BLOCK_GROUP_DIRTY, GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04001219 }
1220 btrfs_free_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04001221 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Mason9078a3e2007-04-26 16:46:15 -04001222 return werr;
1223}
1224
Chris Mason6324fbf2008-03-24 15:01:59 -04001225static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info,
1226 u64 flags)
1227{
1228 struct list_head *head = &info->space_info;
1229 struct list_head *cur;
1230 struct btrfs_space_info *found;
1231 list_for_each(cur, head) {
1232 found = list_entry(cur, struct btrfs_space_info, list);
1233 if (found->flags == flags)
1234 return found;
1235 }
1236 return NULL;
1237
1238}
1239
Chris Mason593060d2008-03-25 16:50:33 -04001240static int update_space_info(struct btrfs_fs_info *info, u64 flags,
1241 u64 total_bytes, u64 bytes_used,
1242 struct btrfs_space_info **space_info)
1243{
1244 struct btrfs_space_info *found;
1245
1246 found = __find_space_info(info, flags);
1247 if (found) {
1248 found->total_bytes += total_bytes;
1249 found->bytes_used += bytes_used;
Chris Mason8f18cf12008-04-25 16:53:30 -04001250 found->full = 0;
Chris Mason593060d2008-03-25 16:50:33 -04001251 *space_info = found;
1252 return 0;
1253 }
1254 found = kmalloc(sizeof(*found), GFP_NOFS);
1255 if (!found)
1256 return -ENOMEM;
1257
1258 list_add(&found->list, &info->space_info);
1259 found->flags = flags;
1260 found->total_bytes = total_bytes;
1261 found->bytes_used = bytes_used;
1262 found->bytes_pinned = 0;
1263 found->full = 0;
Chris Mason0ef3e662008-05-24 14:04:53 -04001264 found->force_alloc = 0;
Chris Mason593060d2008-03-25 16:50:33 -04001265 *space_info = found;
1266 return 0;
1267}
1268
Chris Mason8790d502008-04-03 16:29:03 -04001269static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags)
1270{
1271 u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 |
Chris Mason611f0e02008-04-03 16:29:03 -04001272 BTRFS_BLOCK_GROUP_RAID1 |
Chris Mason321aecc2008-04-16 10:49:51 -04001273 BTRFS_BLOCK_GROUP_RAID10 |
Chris Mason611f0e02008-04-03 16:29:03 -04001274 BTRFS_BLOCK_GROUP_DUP);
Chris Mason8790d502008-04-03 16:29:03 -04001275 if (extra_flags) {
1276 if (flags & BTRFS_BLOCK_GROUP_DATA)
1277 fs_info->avail_data_alloc_bits |= extra_flags;
1278 if (flags & BTRFS_BLOCK_GROUP_METADATA)
1279 fs_info->avail_metadata_alloc_bits |= extra_flags;
1280 if (flags & BTRFS_BLOCK_GROUP_SYSTEM)
1281 fs_info->avail_system_alloc_bits |= extra_flags;
1282 }
1283}
Chris Mason593060d2008-03-25 16:50:33 -04001284
Chris Masona061fc82008-05-07 11:43:44 -04001285static u64 reduce_alloc_profile(struct btrfs_root *root, u64 flags)
Chris Masonec44a352008-04-28 15:29:52 -04001286{
Chris Masona061fc82008-05-07 11:43:44 -04001287 u64 num_devices = root->fs_info->fs_devices->num_devices;
1288
1289 if (num_devices == 1)
1290 flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0);
1291 if (num_devices < 4)
1292 flags &= ~BTRFS_BLOCK_GROUP_RAID10;
1293
Chris Masonec44a352008-04-28 15:29:52 -04001294 if ((flags & BTRFS_BLOCK_GROUP_DUP) &&
1295 (flags & (BTRFS_BLOCK_GROUP_RAID1 |
Chris Masona061fc82008-05-07 11:43:44 -04001296 BTRFS_BLOCK_GROUP_RAID10))) {
Chris Masonec44a352008-04-28 15:29:52 -04001297 flags &= ~BTRFS_BLOCK_GROUP_DUP;
Chris Masona061fc82008-05-07 11:43:44 -04001298 }
Chris Masonec44a352008-04-28 15:29:52 -04001299
1300 if ((flags & BTRFS_BLOCK_GROUP_RAID1) &&
Chris Masona061fc82008-05-07 11:43:44 -04001301 (flags & BTRFS_BLOCK_GROUP_RAID10)) {
Chris Masonec44a352008-04-28 15:29:52 -04001302 flags &= ~BTRFS_BLOCK_GROUP_RAID1;
Chris Masona061fc82008-05-07 11:43:44 -04001303 }
Chris Masonec44a352008-04-28 15:29:52 -04001304
1305 if ((flags & BTRFS_BLOCK_GROUP_RAID0) &&
1306 ((flags & BTRFS_BLOCK_GROUP_RAID1) |
1307 (flags & BTRFS_BLOCK_GROUP_RAID10) |
1308 (flags & BTRFS_BLOCK_GROUP_DUP)))
1309 flags &= ~BTRFS_BLOCK_GROUP_RAID0;
1310 return flags;
1311}
1312
Chris Mason6324fbf2008-03-24 15:01:59 -04001313static int do_chunk_alloc(struct btrfs_trans_handle *trans,
1314 struct btrfs_root *extent_root, u64 alloc_bytes,
Chris Mason0ef3e662008-05-24 14:04:53 -04001315 u64 flags, int force)
Chris Mason6324fbf2008-03-24 15:01:59 -04001316{
1317 struct btrfs_space_info *space_info;
1318 u64 thresh;
1319 u64 start;
1320 u64 num_bytes;
1321 int ret;
1322
Chris Masona061fc82008-05-07 11:43:44 -04001323 flags = reduce_alloc_profile(extent_root, flags);
Chris Masonec44a352008-04-28 15:29:52 -04001324
Chris Mason6324fbf2008-03-24 15:01:59 -04001325 space_info = __find_space_info(extent_root->fs_info, flags);
Chris Mason593060d2008-03-25 16:50:33 -04001326 if (!space_info) {
1327 ret = update_space_info(extent_root->fs_info, flags,
1328 0, 0, &space_info);
1329 BUG_ON(ret);
1330 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001331 BUG_ON(!space_info);
1332
Chris Mason0ef3e662008-05-24 14:04:53 -04001333 if (space_info->force_alloc) {
1334 force = 1;
1335 space_info->force_alloc = 0;
1336 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001337 if (space_info->full)
Chris Mason925baed2008-06-25 16:01:30 -04001338 goto out;
Chris Mason6324fbf2008-03-24 15:01:59 -04001339
Chris Mason8790d502008-04-03 16:29:03 -04001340 thresh = div_factor(space_info->total_bytes, 6);
Chris Mason0ef3e662008-05-24 14:04:53 -04001341 if (!force &&
1342 (space_info->bytes_used + space_info->bytes_pinned + alloc_bytes) <
Chris Mason6324fbf2008-03-24 15:01:59 -04001343 thresh)
Chris Mason925baed2008-06-25 16:01:30 -04001344 goto out;
Chris Mason6324fbf2008-03-24 15:01:59 -04001345
Chris Mason925baed2008-06-25 16:01:30 -04001346 mutex_lock(&extent_root->fs_info->chunk_mutex);
Chris Mason6324fbf2008-03-24 15:01:59 -04001347 ret = btrfs_alloc_chunk(trans, extent_root, &start, &num_bytes, flags);
1348 if (ret == -ENOSPC) {
1349printk("space info full %Lu\n", flags);
1350 space_info->full = 1;
Chris Masona74a4b92008-06-25 16:01:31 -04001351 goto out_unlock;
Chris Mason6324fbf2008-03-24 15:01:59 -04001352 }
Chris Mason6324fbf2008-03-24 15:01:59 -04001353 BUG_ON(ret);
1354
1355 ret = btrfs_make_block_group(trans, extent_root, 0, flags,
Chris Masone17cade2008-04-15 15:41:47 -04001356 BTRFS_FIRST_CHUNK_TREE_OBJECTID, start, num_bytes);
Chris Mason6324fbf2008-03-24 15:01:59 -04001357 BUG_ON(ret);
Chris Masona74a4b92008-06-25 16:01:31 -04001358out_unlock:
Chris Mason333db942008-06-25 16:01:30 -04001359 mutex_unlock(&extent_root->fs_info->chunk_mutex);
Chris Masona74a4b92008-06-25 16:01:31 -04001360out:
Chris Mason6324fbf2008-03-24 15:01:59 -04001361 return 0;
1362}
1363
Chris Mason9078a3e2007-04-26 16:46:15 -04001364static int update_block_group(struct btrfs_trans_handle *trans,
1365 struct btrfs_root *root,
Chris Masondb945352007-10-15 16:15:53 -04001366 u64 bytenr, u64 num_bytes, int alloc,
Chris Mason0b86a832008-03-24 15:01:56 -04001367 int mark_free)
Chris Mason9078a3e2007-04-26 16:46:15 -04001368{
1369 struct btrfs_block_group_cache *cache;
1370 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04001371 u64 total = num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04001372 u64 old_val;
Chris Masondb945352007-10-15 16:15:53 -04001373 u64 byte_in_group;
Chris Mason96b51792007-10-15 16:15:19 -04001374 u64 start;
1375 u64 end;
Chris Mason3e1ad542007-05-07 20:03:49 -04001376
Chris Mason7d9eb122008-07-08 14:19:17 -04001377 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
Chris Mason9078a3e2007-04-26 16:46:15 -04001378 while(total) {
Chris Masondb945352007-10-15 16:15:53 -04001379 cache = btrfs_lookup_block_group(info, bytenr);
Chris Mason3e1ad542007-05-07 20:03:49 -04001380 if (!cache) {
Chris Mason9078a3e2007-04-26 16:46:15 -04001381 return -1;
Chris Masoncd1bc462007-04-27 10:08:34 -04001382 }
Chris Masondb945352007-10-15 16:15:53 -04001383 byte_in_group = bytenr - cache->key.objectid;
1384 WARN_ON(byte_in_group > cache->key.offset);
Chris Mason96b51792007-10-15 16:15:19 -04001385 start = cache->key.objectid;
1386 end = start + cache->key.offset - 1;
1387 set_extent_bits(&info->block_group_cache, start, end,
1388 BLOCK_GROUP_DIRTY, GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04001389
Chris Masonc286ac42008-07-22 23:06:41 -04001390 spin_lock(&cache->lock);
Chris Mason9078a3e2007-04-26 16:46:15 -04001391 old_val = btrfs_block_group_used(&cache->item);
Chris Masondb945352007-10-15 16:15:53 -04001392 num_bytes = min(total, cache->key.offset - byte_in_group);
Chris Masoncd1bc462007-04-27 10:08:34 -04001393 if (alloc) {
Chris Masondb945352007-10-15 16:15:53 -04001394 old_val += num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04001395 cache->space_info->bytes_used += num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04001396 btrfs_set_block_group_used(&cache->item, old_val);
1397 spin_unlock(&cache->lock);
Chris Masoncd1bc462007-04-27 10:08:34 -04001398 } else {
Chris Masondb945352007-10-15 16:15:53 -04001399 old_val -= num_bytes;
Chris Mason6324fbf2008-03-24 15:01:59 -04001400 cache->space_info->bytes_used -= num_bytes;
Chris Masonc286ac42008-07-22 23:06:41 -04001401 btrfs_set_block_group_used(&cache->item, old_val);
1402 spin_unlock(&cache->lock);
Chris Masonf510cfe2007-10-15 16:14:48 -04001403 if (mark_free) {
1404 set_extent_dirty(&info->free_space_cache,
Chris Masondb945352007-10-15 16:15:53 -04001405 bytenr, bytenr + num_bytes - 1,
Chris Masonf510cfe2007-10-15 16:14:48 -04001406 GFP_NOFS);
Chris Masone37c9e62007-05-09 20:13:14 -04001407 }
Chris Masoncd1bc462007-04-27 10:08:34 -04001408 }
Chris Masondb945352007-10-15 16:15:53 -04001409 total -= num_bytes;
1410 bytenr += num_bytes;
Chris Mason9078a3e2007-04-26 16:46:15 -04001411 }
1412 return 0;
1413}
Chris Mason6324fbf2008-03-24 15:01:59 -04001414
Chris Masona061fc82008-05-07 11:43:44 -04001415static u64 first_logical_byte(struct btrfs_root *root, u64 search_start)
1416{
1417 u64 start;
1418 u64 end;
1419 int ret;
1420 ret = find_first_extent_bit(&root->fs_info->block_group_cache,
1421 search_start, &start, &end,
1422 BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA |
1423 BLOCK_GROUP_SYSTEM);
1424 if (ret)
1425 return 0;
1426 return start;
1427}
1428
1429
Chris Masone02119d2008-09-05 16:13:11 -04001430int btrfs_update_pinned_extents(struct btrfs_root *root,
Yan324ae4d2007-11-16 14:57:08 -05001431 u64 bytenr, u64 num, int pin)
1432{
1433 u64 len;
1434 struct btrfs_block_group_cache *cache;
1435 struct btrfs_fs_info *fs_info = root->fs_info;
1436
Chris Mason7d9eb122008-07-08 14:19:17 -04001437 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
Yan324ae4d2007-11-16 14:57:08 -05001438 if (pin) {
1439 set_extent_dirty(&fs_info->pinned_extents,
1440 bytenr, bytenr + num - 1, GFP_NOFS);
1441 } else {
1442 clear_extent_dirty(&fs_info->pinned_extents,
1443 bytenr, bytenr + num - 1, GFP_NOFS);
1444 }
1445 while (num > 0) {
1446 cache = btrfs_lookup_block_group(fs_info, bytenr);
Chris Masona061fc82008-05-07 11:43:44 -04001447 if (!cache) {
1448 u64 first = first_logical_byte(root, bytenr);
1449 WARN_ON(first < bytenr);
1450 len = min(first - bytenr, num);
1451 } else {
1452 len = min(num, cache->key.offset -
1453 (bytenr - cache->key.objectid));
1454 }
Yan324ae4d2007-11-16 14:57:08 -05001455 if (pin) {
Chris Masona061fc82008-05-07 11:43:44 -04001456 if (cache) {
Chris Masonc286ac42008-07-22 23:06:41 -04001457 spin_lock(&cache->lock);
Chris Masona061fc82008-05-07 11:43:44 -04001458 cache->pinned += len;
1459 cache->space_info->bytes_pinned += len;
Chris Masonc286ac42008-07-22 23:06:41 -04001460 spin_unlock(&cache->lock);
Chris Masona061fc82008-05-07 11:43:44 -04001461 }
Yan324ae4d2007-11-16 14:57:08 -05001462 fs_info->total_pinned += len;
1463 } else {
Chris Masona061fc82008-05-07 11:43:44 -04001464 if (cache) {
Chris Masonc286ac42008-07-22 23:06:41 -04001465 spin_lock(&cache->lock);
Chris Masona061fc82008-05-07 11:43:44 -04001466 cache->pinned -= len;
1467 cache->space_info->bytes_pinned -= len;
Chris Masonc286ac42008-07-22 23:06:41 -04001468 spin_unlock(&cache->lock);
Chris Masona061fc82008-05-07 11:43:44 -04001469 }
Yan324ae4d2007-11-16 14:57:08 -05001470 fs_info->total_pinned -= len;
1471 }
1472 bytenr += len;
1473 num -= len;
1474 }
1475 return 0;
1476}
Chris Mason9078a3e2007-04-26 16:46:15 -04001477
Chris Masond1310b22008-01-24 16:13:08 -05001478int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy)
Chris Masonccd467d2007-06-28 15:57:36 -04001479{
Chris Masonccd467d2007-06-28 15:57:36 -04001480 u64 last = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -04001481 u64 start;
1482 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -05001483 struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents;
Chris Masonccd467d2007-06-28 15:57:36 -04001484 int ret;
Chris Masonccd467d2007-06-28 15:57:36 -04001485
1486 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001487 ret = find_first_extent_bit(pinned_extents, last,
1488 &start, &end, EXTENT_DIRTY);
1489 if (ret)
Chris Masonccd467d2007-06-28 15:57:36 -04001490 break;
Chris Mason1a5bc162007-10-15 16:15:26 -04001491 set_extent_dirty(copy, start, end, GFP_NOFS);
1492 last = end + 1;
Chris Masonccd467d2007-06-28 15:57:36 -04001493 }
1494 return 0;
1495}
1496
1497int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans,
1498 struct btrfs_root *root,
Chris Masond1310b22008-01-24 16:13:08 -05001499 struct extent_io_tree *unpin)
Chris Masona28ec192007-03-06 20:08:01 -05001500{
Chris Mason1a5bc162007-10-15 16:15:26 -04001501 u64 start;
1502 u64 end;
Chris Masona28ec192007-03-06 20:08:01 -05001503 int ret;
Chris Masond1310b22008-01-24 16:13:08 -05001504 struct extent_io_tree *free_space_cache;
Chris Masonf510cfe2007-10-15 16:14:48 -04001505 free_space_cache = &root->fs_info->free_space_cache;
Chris Masona28ec192007-03-06 20:08:01 -05001506
Chris Mason925baed2008-06-25 16:01:30 -04001507 mutex_lock(&root->fs_info->alloc_mutex);
Chris Masona28ec192007-03-06 20:08:01 -05001508 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001509 ret = find_first_extent_bit(unpin, 0, &start, &end,
1510 EXTENT_DIRTY);
1511 if (ret)
Chris Masona28ec192007-03-06 20:08:01 -05001512 break;
Chris Masone02119d2008-09-05 16:13:11 -04001513 btrfs_update_pinned_extents(root, start, end + 1 - start, 0);
Chris Mason1a5bc162007-10-15 16:15:26 -04001514 clear_extent_dirty(unpin, start, end, GFP_NOFS);
1515 set_extent_dirty(free_space_cache, start, end, GFP_NOFS);
Chris Masonc286ac42008-07-22 23:06:41 -04001516 if (need_resched()) {
1517 mutex_unlock(&root->fs_info->alloc_mutex);
1518 cond_resched();
1519 mutex_lock(&root->fs_info->alloc_mutex);
1520 }
Chris Masona28ec192007-03-06 20:08:01 -05001521 }
Chris Mason925baed2008-06-25 16:01:30 -04001522 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Masona28ec192007-03-06 20:08:01 -05001523 return 0;
1524}
1525
Chris Mason98ed5172008-01-03 10:01:48 -05001526static int finish_current_insert(struct btrfs_trans_handle *trans,
1527 struct btrfs_root *extent_root)
Chris Mason037e6392007-03-07 11:50:24 -05001528{
Chris Mason7bb86312007-12-11 09:25:06 -05001529 u64 start;
1530 u64 end;
1531 struct btrfs_fs_info *info = extent_root->fs_info;
Chris Masond8d5f3e2007-12-11 12:42:00 -05001532 struct extent_buffer *eb;
Chris Mason7bb86312007-12-11 09:25:06 -05001533 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -04001534 struct btrfs_key ins;
Chris Masond8d5f3e2007-12-11 12:42:00 -05001535 struct btrfs_disk_key first;
Chris Mason234b63a2007-03-13 10:46:10 -04001536 struct btrfs_extent_item extent_item;
Chris Mason037e6392007-03-07 11:50:24 -05001537 int ret;
Chris Masond8d5f3e2007-12-11 12:42:00 -05001538 int level;
Chris Mason1a5bc162007-10-15 16:15:26 -04001539 int err = 0;
Chris Mason037e6392007-03-07 11:50:24 -05001540
Chris Mason7d9eb122008-07-08 14:19:17 -04001541 WARN_ON(!mutex_is_locked(&extent_root->fs_info->alloc_mutex));
Chris Mason5f39d392007-10-15 16:14:19 -04001542 btrfs_set_stack_extent_refs(&extent_item, 1);
Chris Mason62e27492007-03-15 12:56:47 -04001543 btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY);
Chris Mason7bb86312007-12-11 09:25:06 -05001544 path = btrfs_alloc_path();
Chris Mason037e6392007-03-07 11:50:24 -05001545
Chris Mason26b80032007-08-08 20:17:12 -04001546 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001547 ret = find_first_extent_bit(&info->extent_ins, 0, &start,
1548 &end, EXTENT_LOCKED);
1549 if (ret)
Chris Mason26b80032007-08-08 20:17:12 -04001550 break;
1551
Chris Mason1a5bc162007-10-15 16:15:26 -04001552 ins.objectid = start;
1553 ins.offset = end + 1 - start;
1554 err = btrfs_insert_item(trans, extent_root, &ins,
1555 &extent_item, sizeof(extent_item));
1556 clear_extent_bits(&info->extent_ins, start, end, EXTENT_LOCKED,
1557 GFP_NOFS);
Chris Masonc286ac42008-07-22 23:06:41 -04001558
Chris Masone02119d2008-09-05 16:13:11 -04001559 eb = btrfs_find_create_tree_block(extent_root, ins.objectid,
Chris Masonc286ac42008-07-22 23:06:41 -04001560 ins.offset);
1561
Chris Masone02119d2008-09-05 16:13:11 -04001562 if (!btrfs_buffer_uptodate(eb, trans->transid))
Chris Masonc286ac42008-07-22 23:06:41 -04001563 btrfs_read_buffer(eb, trans->transid);
Chris Masonc286ac42008-07-22 23:06:41 -04001564
Chris Mason925baed2008-06-25 16:01:30 -04001565 btrfs_tree_lock(eb);
Chris Masond8d5f3e2007-12-11 12:42:00 -05001566 level = btrfs_header_level(eb);
1567 if (level == 0) {
1568 btrfs_item_key(eb, &first, 0);
1569 } else {
1570 btrfs_node_key(eb, &first, 0);
1571 }
Chris Mason925baed2008-06-25 16:01:30 -04001572 btrfs_tree_unlock(eb);
1573 free_extent_buffer(eb);
1574 /*
1575 * the first key is just a hint, so the race we've created
1576 * against reading it is fine
1577 */
Chris Mason7bb86312007-12-11 09:25:06 -05001578 err = btrfs_insert_extent_backref(trans, extent_root, path,
1579 start, extent_root->root_key.objectid,
Chris Masonf6dbff52007-12-13 11:13:32 -05001580 0, level,
1581 btrfs_disk_key_objectid(&first));
Chris Mason7bb86312007-12-11 09:25:06 -05001582 BUG_ON(err);
Chris Masonc286ac42008-07-22 23:06:41 -04001583 if (need_resched()) {
1584 mutex_unlock(&extent_root->fs_info->alloc_mutex);
1585 cond_resched();
1586 mutex_lock(&extent_root->fs_info->alloc_mutex);
1587 }
Chris Mason037e6392007-03-07 11:50:24 -05001588 }
Chris Mason7bb86312007-12-11 09:25:06 -05001589 btrfs_free_path(path);
Chris Mason037e6392007-03-07 11:50:24 -05001590 return 0;
1591}
1592
Chris Masondb945352007-10-15 16:15:53 -04001593static int pin_down_bytes(struct btrfs_root *root, u64 bytenr, u32 num_bytes,
Chris Mason4bef0842008-09-08 11:18:08 -04001594 int is_data, int pending)
Chris Masone20d96d2007-03-22 12:13:20 -04001595{
Chris Mason1a5bc162007-10-15 16:15:26 -04001596 int err = 0;
Chris Mason78fae272007-03-25 11:35:08 -04001597
Chris Mason7d9eb122008-07-08 14:19:17 -04001598 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
Chris Masonf4b9aa82007-03-27 11:05:53 -04001599 if (!pending) {
Chris Mason925baed2008-06-25 16:01:30 -04001600 struct extent_buffer *buf;
Chris Mason4bef0842008-09-08 11:18:08 -04001601
1602 if (is_data)
1603 goto pinit;
1604
Chris Masondb945352007-10-15 16:15:53 -04001605 buf = btrfs_find_tree_block(root, bytenr, num_bytes);
Chris Mason5f39d392007-10-15 16:14:19 -04001606 if (buf) {
Chris Masone02119d2008-09-05 16:13:11 -04001607 /* we can reuse a block if it hasn't been written
1608 * and it is from this transaction. We can't
1609 * reuse anything from the tree log root because
1610 * it has tiny sub-transactions.
1611 */
Yan974e35a2008-07-24 12:18:16 -04001612 if (btrfs_buffer_uptodate(buf, 0) &&
1613 btrfs_try_tree_lock(buf)) {
Chris Mason2c90e5d2007-04-02 10:50:19 -04001614 u64 transid =
1615 root->fs_info->running_transaction->transid;
Chris Masondc17ff82008-01-08 15:46:30 -05001616 u64 header_transid =
1617 btrfs_header_generation(buf);
Chris Masone02119d2008-09-05 16:13:11 -04001618 if (btrfs_header_owner(buf) !=
1619 BTRFS_TREE_LOG_OBJECTID &&
1620 header_transid == transid &&
Chris Mason6bc34672008-04-04 15:40:00 -04001621 !btrfs_header_flag(buf,
1622 BTRFS_HEADER_FLAG_WRITTEN)) {
Chris Mason55c69072008-01-09 15:55:33 -05001623 clean_tree_block(NULL, root, buf);
Chris Mason925baed2008-06-25 16:01:30 -04001624 btrfs_tree_unlock(buf);
Chris Mason5f39d392007-10-15 16:14:19 -04001625 free_extent_buffer(buf);
Yanc5492282007-11-06 10:25:25 -05001626 return 1;
Chris Mason2c90e5d2007-04-02 10:50:19 -04001627 }
Chris Mason925baed2008-06-25 16:01:30 -04001628 btrfs_tree_unlock(buf);
Chris Masonf4b9aa82007-03-27 11:05:53 -04001629 }
Chris Mason5f39d392007-10-15 16:14:19 -04001630 free_extent_buffer(buf);
Chris Mason8ef97622007-03-26 10:15:30 -04001631 }
Chris Mason4bef0842008-09-08 11:18:08 -04001632pinit:
Chris Masone02119d2008-09-05 16:13:11 -04001633 btrfs_update_pinned_extents(root, bytenr, num_bytes, 1);
Chris Masonf4b9aa82007-03-27 11:05:53 -04001634 } else {
Chris Mason1a5bc162007-10-15 16:15:26 -04001635 set_extent_bits(&root->fs_info->pending_del,
Chris Masondb945352007-10-15 16:15:53 -04001636 bytenr, bytenr + num_bytes - 1,
1637 EXTENT_LOCKED, GFP_NOFS);
Chris Masonf4b9aa82007-03-27 11:05:53 -04001638 }
Chris Masonbe744172007-05-06 10:15:01 -04001639 BUG_ON(err < 0);
Chris Masone20d96d2007-03-22 12:13:20 -04001640 return 0;
1641}
1642
Chris Masona28ec192007-03-06 20:08:01 -05001643/*
1644 * remove an extent from the root, returns 0 on success
1645 */
Chris Masone089f052007-03-16 16:20:31 -04001646static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason7bb86312007-12-11 09:25:06 -05001647 *root, u64 bytenr, u64 num_bytes,
1648 u64 root_objectid, u64 ref_generation,
1649 u64 owner_objectid, u64 owner_offset, int pin,
Chris Masone37c9e62007-05-09 20:13:14 -04001650 int mark_free)
Chris Masona28ec192007-03-06 20:08:01 -05001651{
Chris Mason5caf2a02007-04-02 11:20:42 -04001652 struct btrfs_path *path;
Chris Masone2fa7222007-03-12 16:22:34 -04001653 struct btrfs_key key;
Chris Mason1261ec42007-03-20 20:35:03 -04001654 struct btrfs_fs_info *info = root->fs_info;
1655 struct btrfs_root *extent_root = info->extent_root;
Chris Mason5f39d392007-10-15 16:14:19 -04001656 struct extent_buffer *leaf;
Chris Masona28ec192007-03-06 20:08:01 -05001657 int ret;
Chris Mason952fcca2008-02-18 16:33:44 -05001658 int extent_slot = 0;
1659 int found_extent = 0;
1660 int num_to_del = 1;
Chris Mason234b63a2007-03-13 10:46:10 -04001661 struct btrfs_extent_item *ei;
Chris Masoncf27e1e2007-03-13 09:49:06 -04001662 u32 refs;
Chris Mason037e6392007-03-07 11:50:24 -05001663
Chris Mason7d9eb122008-07-08 14:19:17 -04001664 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
Chris Masondb945352007-10-15 16:15:53 -04001665 key.objectid = bytenr;
Chris Mason62e27492007-03-15 12:56:47 -04001666 btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY);
Chris Masondb945352007-10-15 16:15:53 -04001667 key.offset = num_bytes;
Chris Mason5caf2a02007-04-02 11:20:42 -04001668 path = btrfs_alloc_path();
Chris Mason54aa1f42007-06-22 14:16:25 -04001669 if (!path)
1670 return -ENOMEM;
1671
Chris Mason3c12ac72008-04-21 12:01:38 -04001672 path->reada = 1;
Chris Mason7bb86312007-12-11 09:25:06 -05001673 ret = lookup_extent_backref(trans, extent_root, path,
1674 bytenr, root_objectid,
1675 ref_generation,
1676 owner_objectid, owner_offset, 1);
1677 if (ret == 0) {
Chris Mason952fcca2008-02-18 16:33:44 -05001678 struct btrfs_key found_key;
1679 extent_slot = path->slots[0];
1680 while(extent_slot > 0) {
1681 extent_slot--;
1682 btrfs_item_key_to_cpu(path->nodes[0], &found_key,
1683 extent_slot);
1684 if (found_key.objectid != bytenr)
1685 break;
1686 if (found_key.type == BTRFS_EXTENT_ITEM_KEY &&
1687 found_key.offset == num_bytes) {
1688 found_extent = 1;
1689 break;
1690 }
1691 if (path->slots[0] - extent_slot > 5)
1692 break;
1693 }
1694 if (!found_extent)
1695 ret = btrfs_del_item(trans, extent_root, path);
Chris Mason7bb86312007-12-11 09:25:06 -05001696 } else {
1697 btrfs_print_leaf(extent_root, path->nodes[0]);
1698 WARN_ON(1);
1699 printk("Unable to find ref byte nr %Lu root %Lu "
1700 " gen %Lu owner %Lu offset %Lu\n", bytenr,
1701 root_objectid, ref_generation, owner_objectid,
1702 owner_offset);
1703 }
Chris Mason952fcca2008-02-18 16:33:44 -05001704 if (!found_extent) {
1705 btrfs_release_path(extent_root, path);
1706 ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1);
1707 if (ret < 0)
1708 return ret;
1709 BUG_ON(ret);
1710 extent_slot = path->slots[0];
1711 }
Chris Mason5f39d392007-10-15 16:14:19 -04001712
1713 leaf = path->nodes[0];
Chris Mason952fcca2008-02-18 16:33:44 -05001714 ei = btrfs_item_ptr(leaf, extent_slot,
Chris Mason123abc82007-03-14 14:14:43 -04001715 struct btrfs_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001716 refs = btrfs_extent_refs(leaf, ei);
1717 BUG_ON(refs == 0);
1718 refs -= 1;
1719 btrfs_set_extent_refs(leaf, ei, refs);
Chris Mason952fcca2008-02-18 16:33:44 -05001720
Chris Mason5f39d392007-10-15 16:14:19 -04001721 btrfs_mark_buffer_dirty(leaf);
1722
Chris Mason952fcca2008-02-18 16:33:44 -05001723 if (refs == 0 && found_extent && path->slots[0] == extent_slot + 1) {
1724 /* if the back ref and the extent are next to each other
1725 * they get deleted below in one shot
1726 */
1727 path->slots[0] = extent_slot;
1728 num_to_del = 2;
1729 } else if (found_extent) {
1730 /* otherwise delete the extent back ref */
1731 ret = btrfs_del_item(trans, extent_root, path);
1732 BUG_ON(ret);
1733 /* if refs are 0, we need to setup the path for deletion */
1734 if (refs == 0) {
1735 btrfs_release_path(extent_root, path);
1736 ret = btrfs_search_slot(trans, extent_root, &key, path,
1737 -1, 1);
1738 if (ret < 0)
1739 return ret;
1740 BUG_ON(ret);
1741 }
1742 }
1743
Chris Masoncf27e1e2007-03-13 09:49:06 -04001744 if (refs == 0) {
Chris Masondb945352007-10-15 16:15:53 -04001745 u64 super_used;
1746 u64 root_used;
David Woodhouse21af8042008-08-12 14:13:26 +01001747#ifdef BIO_RW_DISCARD
1748 u64 map_length = num_bytes;
1749 struct btrfs_multi_bio *multi = NULL;
1750#endif
Chris Mason78fae272007-03-25 11:35:08 -04001751
1752 if (pin) {
Chris Mason4bef0842008-09-08 11:18:08 -04001753 ret = pin_down_bytes(root, bytenr, num_bytes,
1754 owner_objectid >= BTRFS_FIRST_FREE_OBJECTID, 0);
Yanc5492282007-11-06 10:25:25 -05001755 if (ret > 0)
1756 mark_free = 1;
1757 BUG_ON(ret < 0);
Chris Mason78fae272007-03-25 11:35:08 -04001758 }
1759
Josef Bacik58176a92007-08-29 15:47:34 -04001760 /* block accounting for super block */
Chris Masona2135012008-06-25 16:01:30 -04001761 spin_lock_irq(&info->delalloc_lock);
Chris Masondb945352007-10-15 16:15:53 -04001762 super_used = btrfs_super_bytes_used(&info->super_copy);
1763 btrfs_set_super_bytes_used(&info->super_copy,
1764 super_used - num_bytes);
Chris Masona2135012008-06-25 16:01:30 -04001765 spin_unlock_irq(&info->delalloc_lock);
Josef Bacik58176a92007-08-29 15:47:34 -04001766
1767 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04001768 root_used = btrfs_root_used(&root->root_item);
Chris Mason5f39d392007-10-15 16:14:19 -04001769 btrfs_set_root_used(&root->root_item,
Chris Masondb945352007-10-15 16:15:53 -04001770 root_used - num_bytes);
Chris Mason952fcca2008-02-18 16:33:44 -05001771 ret = btrfs_del_items(trans, extent_root, path, path->slots[0],
1772 num_to_del);
Chris Mason54aa1f42007-06-22 14:16:25 -04001773 if (ret) {
1774 return ret;
1775 }
Chris Masondb945352007-10-15 16:15:53 -04001776 ret = update_block_group(trans, root, bytenr, num_bytes, 0,
Chris Mason0b86a832008-03-24 15:01:56 -04001777 mark_free);
Chris Mason9078a3e2007-04-26 16:46:15 -04001778 BUG_ON(ret);
David Woodhouse21af8042008-08-12 14:13:26 +01001779
1780#ifdef BIO_RW_DISCARD
1781 /* Tell the block device(s) that the sectors can be discarded */
1782 ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
1783 bytenr, &map_length, &multi, 0);
1784 if (!ret) {
1785 struct btrfs_bio_stripe *stripe = multi->stripes;
1786 int i;
1787
1788 if (map_length > num_bytes)
1789 map_length = num_bytes;
1790
1791 for (i = 0; i < multi->num_stripes; i++, stripe++) {
1792 blkdev_issue_discard(stripe->dev->bdev,
1793 stripe->physical >> 9,
1794 map_length >> 9);
1795 }
1796 kfree(multi);
1797 }
1798#endif
Chris Masona28ec192007-03-06 20:08:01 -05001799 }
Chris Mason5caf2a02007-04-02 11:20:42 -04001800 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -04001801 finish_current_insert(trans, extent_root);
Chris Masona28ec192007-03-06 20:08:01 -05001802 return ret;
1803}
1804
1805/*
Chris Masonfec577f2007-02-26 10:40:21 -05001806 * find all the blocks marked as pending in the radix tree and remove
1807 * them from the extent map
1808 */
Chris Masone089f052007-03-16 16:20:31 -04001809static int del_pending_extents(struct btrfs_trans_handle *trans, struct
1810 btrfs_root *extent_root)
Chris Masonfec577f2007-02-26 10:40:21 -05001811{
1812 int ret;
Chris Masone20d96d2007-03-22 12:13:20 -04001813 int err = 0;
Chris Mason1a5bc162007-10-15 16:15:26 -04001814 u64 start;
1815 u64 end;
Chris Masond1310b22008-01-24 16:13:08 -05001816 struct extent_io_tree *pending_del;
1817 struct extent_io_tree *pinned_extents;
Chris Mason8ef97622007-03-26 10:15:30 -04001818
Chris Mason7d9eb122008-07-08 14:19:17 -04001819 WARN_ON(!mutex_is_locked(&extent_root->fs_info->alloc_mutex));
Chris Mason1a5bc162007-10-15 16:15:26 -04001820 pending_del = &extent_root->fs_info->pending_del;
1821 pinned_extents = &extent_root->fs_info->pinned_extents;
Chris Masonfec577f2007-02-26 10:40:21 -05001822
1823 while(1) {
Chris Mason1a5bc162007-10-15 16:15:26 -04001824 ret = find_first_extent_bit(pending_del, 0, &start, &end,
1825 EXTENT_LOCKED);
1826 if (ret)
Chris Masonfec577f2007-02-26 10:40:21 -05001827 break;
Chris Mason1a5bc162007-10-15 16:15:26 -04001828 clear_extent_bits(pending_del, start, end, EXTENT_LOCKED,
1829 GFP_NOFS);
Chris Masonc286ac42008-07-22 23:06:41 -04001830 if (!test_range_bit(&extent_root->fs_info->extent_ins,
1831 start, end, EXTENT_LOCKED, 0)) {
Chris Masone02119d2008-09-05 16:13:11 -04001832 btrfs_update_pinned_extents(extent_root, start,
Chris Masonc286ac42008-07-22 23:06:41 -04001833 end + 1 - start, 1);
1834 ret = __free_extent(trans, extent_root,
1835 start, end + 1 - start,
1836 extent_root->root_key.objectid,
1837 0, 0, 0, 0, 0);
1838 } else {
1839 clear_extent_bits(&extent_root->fs_info->extent_ins,
1840 start, end, EXTENT_LOCKED, GFP_NOFS);
1841 }
Chris Mason1a5bc162007-10-15 16:15:26 -04001842 if (ret)
1843 err = ret;
Chris Masonc286ac42008-07-22 23:06:41 -04001844
1845 if (need_resched()) {
1846 mutex_unlock(&extent_root->fs_info->alloc_mutex);
1847 cond_resched();
1848 mutex_lock(&extent_root->fs_info->alloc_mutex);
1849 }
Chris Masonfec577f2007-02-26 10:40:21 -05001850 }
Chris Masone20d96d2007-03-22 12:13:20 -04001851 return err;
Chris Masonfec577f2007-02-26 10:40:21 -05001852}
1853
1854/*
1855 * remove an extent from the root, returns 0 on success
1856 */
Chris Mason925baed2008-06-25 16:01:30 -04001857static int __btrfs_free_extent(struct btrfs_trans_handle *trans,
1858 struct btrfs_root *root, u64 bytenr,
1859 u64 num_bytes, u64 root_objectid,
1860 u64 ref_generation, u64 owner_objectid,
1861 u64 owner_offset, int pin)
Chris Masonfec577f2007-02-26 10:40:21 -05001862{
Chris Mason9f5fae22007-03-20 14:38:32 -04001863 struct btrfs_root *extent_root = root->fs_info->extent_root;
Chris Masonfec577f2007-02-26 10:40:21 -05001864 int pending_ret;
1865 int ret;
Chris Masona28ec192007-03-06 20:08:01 -05001866
Chris Masondb945352007-10-15 16:15:53 -04001867 WARN_ON(num_bytes < root->sectorsize);
Chris Mason7bb86312007-12-11 09:25:06 -05001868 if (!root->ref_cows)
1869 ref_generation = 0;
1870
Chris Masona28ec192007-03-06 20:08:01 -05001871 if (root == extent_root) {
Chris Mason4bef0842008-09-08 11:18:08 -04001872 pin_down_bytes(root, bytenr, num_bytes, 0, 1);
Chris Masona28ec192007-03-06 20:08:01 -05001873 return 0;
1874 }
Chris Mason4bef0842008-09-08 11:18:08 -04001875 /* if metadata always pin */
Chris Masond00aff02008-09-11 15:54:42 -04001876 if (owner_objectid < BTRFS_FIRST_FREE_OBJECTID) {
1877 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
1878 /* btrfs_free_reserved_extent */
1879 set_extent_dirty(&root->fs_info->free_space_cache,
1880 bytenr, bytenr + num_bytes - 1, GFP_NOFS);
1881 return 0;
1882 }
Chris Mason4bef0842008-09-08 11:18:08 -04001883 pin = 1;
Chris Masond00aff02008-09-11 15:54:42 -04001884 }
Chris Mason4bef0842008-09-08 11:18:08 -04001885
1886 /* if data pin when any transaction has committed this */
1887 if (ref_generation != trans->transid)
1888 pin = 1;
1889
Chris Mason7bb86312007-12-11 09:25:06 -05001890 ret = __free_extent(trans, root, bytenr, num_bytes, root_objectid,
1891 ref_generation, owner_objectid, owner_offset,
1892 pin, pin == 0);
Chris Masonee6e6502008-07-17 12:54:40 -04001893
1894 finish_current_insert(trans, root->fs_info->extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -04001895 pending_ret = del_pending_extents(trans, root->fs_info->extent_root);
Chris Masonfec577f2007-02-26 10:40:21 -05001896 return ret ? ret : pending_ret;
1897}
1898
Chris Mason925baed2008-06-25 16:01:30 -04001899int btrfs_free_extent(struct btrfs_trans_handle *trans,
1900 struct btrfs_root *root, u64 bytenr,
1901 u64 num_bytes, u64 root_objectid,
1902 u64 ref_generation, u64 owner_objectid,
1903 u64 owner_offset, int pin)
1904{
1905 int ret;
1906
1907 maybe_lock_mutex(root);
1908 ret = __btrfs_free_extent(trans, root, bytenr, num_bytes,
1909 root_objectid, ref_generation,
1910 owner_objectid, owner_offset, pin);
1911 maybe_unlock_mutex(root);
1912 return ret;
1913}
1914
Chris Mason87ee04e2007-11-30 11:30:34 -05001915static u64 stripe_align(struct btrfs_root *root, u64 val)
1916{
1917 u64 mask = ((u64)root->stripesize - 1);
1918 u64 ret = (val + mask) & ~mask;
1919 return ret;
1920}
1921
Chris Masonfec577f2007-02-26 10:40:21 -05001922/*
1923 * walks the btree of allocated extents and find a hole of a given size.
1924 * The key ins is changed to record the hole:
1925 * ins->objectid == block start
Chris Mason62e27492007-03-15 12:56:47 -04001926 * ins->flags = BTRFS_EXTENT_ITEM_KEY
Chris Masonfec577f2007-02-26 10:40:21 -05001927 * ins->offset == number of blocks
1928 * Any available blocks before search_start are skipped.
1929 */
Chris Mason98ed5172008-01-03 10:01:48 -05001930static int noinline find_free_extent(struct btrfs_trans_handle *trans,
1931 struct btrfs_root *orig_root,
1932 u64 num_bytes, u64 empty_size,
1933 u64 search_start, u64 search_end,
1934 u64 hint_byte, struct btrfs_key *ins,
1935 u64 exclude_start, u64 exclude_nr,
1936 int data)
Chris Masonfec577f2007-02-26 10:40:21 -05001937{
Chris Mason87ee04e2007-11-30 11:30:34 -05001938 int ret;
Chris Masona061fc82008-05-07 11:43:44 -04001939 u64 orig_search_start;
Chris Mason9f5fae22007-03-20 14:38:32 -04001940 struct btrfs_root * root = orig_root->fs_info->extent_root;
Chris Masonf2458e12007-04-25 15:52:25 -04001941 struct btrfs_fs_info *info = root->fs_info;
Chris Masondb945352007-10-15 16:15:53 -04001942 u64 total_needed = num_bytes;
Chris Mason239b14b2008-03-24 15:02:07 -04001943 u64 *last_ptr = NULL;
Chris Masonbe08c1b2007-05-03 09:06:49 -04001944 struct btrfs_block_group_cache *block_group;
Chris Masonbe744172007-05-06 10:15:01 -04001945 int full_scan = 0;
Chris Masonfbdc7622007-05-30 10:22:12 -04001946 int wrapped = 0;
Chris Mason0ef3e662008-05-24 14:04:53 -04001947 int chunk_alloc_done = 0;
Chris Mason239b14b2008-03-24 15:02:07 -04001948 int empty_cluster = 2 * 1024 * 1024;
Chris Mason0ef3e662008-05-24 14:04:53 -04001949 int allowed_chunk_alloc = 0;
Chris Masonfec577f2007-02-26 10:40:21 -05001950
Chris Masondb945352007-10-15 16:15:53 -04001951 WARN_ON(num_bytes < root->sectorsize);
Chris Masonb1a4d962007-04-04 15:27:52 -04001952 btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY);
1953
Chris Mason0ef3e662008-05-24 14:04:53 -04001954 if (orig_root->ref_cows || empty_size)
1955 allowed_chunk_alloc = 1;
1956
Chris Mason239b14b2008-03-24 15:02:07 -04001957 if (data & BTRFS_BLOCK_GROUP_METADATA) {
1958 last_ptr = &root->fs_info->last_alloc;
Chris Mason8790d502008-04-03 16:29:03 -04001959 empty_cluster = 256 * 1024;
Chris Mason239b14b2008-03-24 15:02:07 -04001960 }
1961
1962 if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD)) {
1963 last_ptr = &root->fs_info->last_data_alloc;
1964 }
Chris Masone02119d2008-09-05 16:13:11 -04001965 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
1966 last_ptr = &root->fs_info->last_log_alloc;
1967 if (!last_ptr == 0 && root->fs_info->last_alloc) {
1968 *last_ptr = root->fs_info->last_alloc + empty_cluster;
1969 }
1970 }
Chris Mason239b14b2008-03-24 15:02:07 -04001971
1972 if (last_ptr) {
1973 if (*last_ptr)
1974 hint_byte = *last_ptr;
1975 else {
1976 empty_size += empty_cluster;
1977 }
1978 }
1979
Chris Masona061fc82008-05-07 11:43:44 -04001980 search_start = max(search_start, first_logical_byte(root, 0));
1981 orig_search_start = search_start;
1982
Chris Mason7f93bf82008-03-24 15:01:28 -04001983 if (search_end == (u64)-1)
1984 search_end = btrfs_super_total_bytes(&info->super_copy);
Chris Mason0b86a832008-03-24 15:01:56 -04001985
Chris Masondb945352007-10-15 16:15:53 -04001986 if (hint_byte) {
Chris Mason0ef3e662008-05-24 14:04:53 -04001987 block_group = btrfs_lookup_first_block_group(info, hint_byte);
Chris Mason1a2b2ac2007-12-04 13:18:24 -05001988 if (!block_group)
1989 hint_byte = search_start;
Chris Masonc286ac42008-07-22 23:06:41 -04001990 block_group = btrfs_find_block_group(root, block_group,
Chris Masondb945352007-10-15 16:15:53 -04001991 hint_byte, data, 1);
Chris Mason239b14b2008-03-24 15:02:07 -04001992 if (last_ptr && *last_ptr == 0 && block_group)
1993 hint_byte = block_group->key.objectid;
Chris Masonbe744172007-05-06 10:15:01 -04001994 } else {
Chris Masonc286ac42008-07-22 23:06:41 -04001995 block_group = btrfs_find_block_group(root,
Chris Mason1a2b2ac2007-12-04 13:18:24 -05001996 trans->block_group,
1997 search_start, data, 1);
Chris Masonbe744172007-05-06 10:15:01 -04001998 }
Chris Mason239b14b2008-03-24 15:02:07 -04001999 search_start = max(search_start, hint_byte);
Chris Masonbe744172007-05-06 10:15:01 -04002000
Chris Mason6702ed42007-08-07 16:15:09 -04002001 total_needed += empty_size;
Chris Mason0b86a832008-03-24 15:01:56 -04002002
Chris Masonbe744172007-05-06 10:15:01 -04002003check_failed:
Chris Mason70b043f2007-12-13 09:02:46 -05002004 if (!block_group) {
Chris Mason0ef3e662008-05-24 14:04:53 -04002005 block_group = btrfs_lookup_first_block_group(info,
2006 search_start);
Chris Mason70b043f2007-12-13 09:02:46 -05002007 if (!block_group)
Chris Mason0ef3e662008-05-24 14:04:53 -04002008 block_group = btrfs_lookup_first_block_group(info,
Chris Mason70b043f2007-12-13 09:02:46 -05002009 orig_search_start);
2010 }
Chris Mason0ef3e662008-05-24 14:04:53 -04002011 if (full_scan && !chunk_alloc_done) {
2012 if (allowed_chunk_alloc) {
2013 do_chunk_alloc(trans, root,
2014 num_bytes + 2 * 1024 * 1024, data, 1);
2015 allowed_chunk_alloc = 0;
2016 } else if (block_group && block_group_bits(block_group, data)) {
2017 block_group->space_info->force_alloc = 1;
2018 }
2019 chunk_alloc_done = 1;
2020 }
Chris Mason0b86a832008-03-24 15:01:56 -04002021 ret = find_search_start(root, &block_group, &search_start,
2022 total_needed, data);
Chris Mason239b14b2008-03-24 15:02:07 -04002023 if (ret == -ENOSPC && last_ptr && *last_ptr) {
2024 *last_ptr = 0;
Chris Mason0ef3e662008-05-24 14:04:53 -04002025 block_group = btrfs_lookup_first_block_group(info,
2026 orig_search_start);
Chris Mason239b14b2008-03-24 15:02:07 -04002027 search_start = orig_search_start;
2028 ret = find_search_start(root, &block_group, &search_start,
2029 total_needed, data);
2030 }
2031 if (ret == -ENOSPC)
2032 goto enospc;
Chris Mason0b86a832008-03-24 15:01:56 -04002033 if (ret)
2034 goto error;
2035
Chris Mason239b14b2008-03-24 15:02:07 -04002036 if (last_ptr && *last_ptr && search_start != *last_ptr) {
2037 *last_ptr = 0;
2038 if (!empty_size) {
2039 empty_size += empty_cluster;
2040 total_needed += empty_size;
2041 }
Chris Mason0ef3e662008-05-24 14:04:53 -04002042 block_group = btrfs_lookup_first_block_group(info,
Chris Mason239b14b2008-03-24 15:02:07 -04002043 orig_search_start);
2044 search_start = orig_search_start;
2045 ret = find_search_start(root, &block_group,
2046 &search_start, total_needed, data);
2047 if (ret == -ENOSPC)
2048 goto enospc;
2049 if (ret)
2050 goto error;
2051 }
2052
Chris Mason87ee04e2007-11-30 11:30:34 -05002053 search_start = stripe_align(root, search_start);
Chris Masonfec577f2007-02-26 10:40:21 -05002054 ins->objectid = search_start;
Chris Mason0b86a832008-03-24 15:01:56 -04002055 ins->offset = num_bytes;
Chris Masone37c9e62007-05-09 20:13:14 -04002056
Chris Masondb945352007-10-15 16:15:53 -04002057 if (ins->objectid + num_bytes >= search_end)
Chris Masoncf675822007-09-17 11:00:51 -04002058 goto enospc;
Chris Mason0b86a832008-03-24 15:01:56 -04002059
2060 if (ins->objectid + num_bytes >
2061 block_group->key.objectid + block_group->key.offset) {
Chris Masone19caa52007-10-15 16:17:44 -04002062 search_start = block_group->key.objectid +
2063 block_group->key.offset;
2064 goto new_group;
2065 }
Chris Mason0b86a832008-03-24 15:01:56 -04002066
Chris Mason1a5bc162007-10-15 16:15:26 -04002067 if (test_range_bit(&info->extent_ins, ins->objectid,
Chris Masondb945352007-10-15 16:15:53 -04002068 ins->objectid + num_bytes -1, EXTENT_LOCKED, 0)) {
2069 search_start = ins->objectid + num_bytes;
Chris Mason1a5bc162007-10-15 16:15:26 -04002070 goto new_group;
2071 }
Chris Mason0b86a832008-03-24 15:01:56 -04002072
Chris Mason1a5bc162007-10-15 16:15:26 -04002073 if (test_range_bit(&info->pinned_extents, ins->objectid,
Chris Masondb945352007-10-15 16:15:53 -04002074 ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) {
2075 search_start = ins->objectid + num_bytes;
Chris Mason1a5bc162007-10-15 16:15:26 -04002076 goto new_group;
Chris Masonfec577f2007-02-26 10:40:21 -05002077 }
Chris Mason0b86a832008-03-24 15:01:56 -04002078
Chris Masondb945352007-10-15 16:15:53 -04002079 if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start &&
Chris Masonf2654de2007-06-26 12:20:46 -04002080 ins->objectid < exclude_start + exclude_nr)) {
2081 search_start = exclude_start + exclude_nr;
2082 goto new_group;
2083 }
Chris Mason0b86a832008-03-24 15:01:56 -04002084
Chris Mason6324fbf2008-03-24 15:01:59 -04002085 if (!(data & BTRFS_BLOCK_GROUP_DATA)) {
Chris Mason5276aed2007-06-11 21:33:38 -04002086 block_group = btrfs_lookup_block_group(info, ins->objectid);
Chris Mason26b80032007-08-08 20:17:12 -04002087 if (block_group)
2088 trans->block_group = block_group;
Chris Masoncd1bc462007-04-27 10:08:34 -04002089 }
Chris Masondb945352007-10-15 16:15:53 -04002090 ins->offset = num_bytes;
Chris Mason239b14b2008-03-24 15:02:07 -04002091 if (last_ptr) {
2092 *last_ptr = ins->objectid + ins->offset;
2093 if (*last_ptr ==
2094 btrfs_super_total_bytes(&root->fs_info->super_copy)) {
2095 *last_ptr = 0;
2096 }
2097 }
Chris Masonfec577f2007-02-26 10:40:21 -05002098 return 0;
Chris Masonbe744172007-05-06 10:15:01 -04002099
2100new_group:
Chris Masondb945352007-10-15 16:15:53 -04002101 if (search_start + num_bytes >= search_end) {
Chris Masoncf675822007-09-17 11:00:51 -04002102enospc:
Chris Masonbe744172007-05-06 10:15:01 -04002103 search_start = orig_search_start;
Chris Masonfbdc7622007-05-30 10:22:12 -04002104 if (full_scan) {
2105 ret = -ENOSPC;
2106 goto error;
2107 }
Chris Mason6702ed42007-08-07 16:15:09 -04002108 if (wrapped) {
2109 if (!full_scan)
2110 total_needed -= empty_size;
Chris Masonfbdc7622007-05-30 10:22:12 -04002111 full_scan = 1;
Chris Mason6702ed42007-08-07 16:15:09 -04002112 } else
Chris Masonfbdc7622007-05-30 10:22:12 -04002113 wrapped = 1;
Chris Masonbe744172007-05-06 10:15:01 -04002114 }
Chris Mason0ef3e662008-05-24 14:04:53 -04002115 block_group = btrfs_lookup_first_block_group(info, search_start);
Chris Masonfbdc7622007-05-30 10:22:12 -04002116 cond_resched();
Chris Masonc286ac42008-07-22 23:06:41 -04002117 block_group = btrfs_find_block_group(root, block_group,
Chris Mason1a2b2ac2007-12-04 13:18:24 -05002118 search_start, data, 0);
Chris Masonbe744172007-05-06 10:15:01 -04002119 goto check_failed;
2120
Chris Mason0f70abe2007-02-28 16:46:22 -05002121error:
Chris Mason0f70abe2007-02-28 16:46:22 -05002122 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05002123}
Chris Masonec44a352008-04-28 15:29:52 -04002124
Chris Masone6dcd2d2008-07-17 12:53:50 -04002125static int __btrfs_reserve_extent(struct btrfs_trans_handle *trans,
2126 struct btrfs_root *root,
2127 u64 num_bytes, u64 min_alloc_size,
2128 u64 empty_size, u64 hint_byte,
2129 u64 search_end, struct btrfs_key *ins,
2130 u64 data)
Chris Masonfec577f2007-02-26 10:40:21 -05002131{
2132 int ret;
Chris Masonfbdc7622007-05-30 10:22:12 -04002133 u64 search_start = 0;
Chris Mason8790d502008-04-03 16:29:03 -04002134 u64 alloc_profile;
Chris Mason1261ec42007-03-20 20:35:03 -04002135 struct btrfs_fs_info *info = root->fs_info;
Chris Mason925baed2008-06-25 16:01:30 -04002136
Chris Mason6324fbf2008-03-24 15:01:59 -04002137 if (data) {
Chris Mason8790d502008-04-03 16:29:03 -04002138 alloc_profile = info->avail_data_alloc_bits &
2139 info->data_alloc_profile;
2140 data = BTRFS_BLOCK_GROUP_DATA | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04002141 } else if (root == root->fs_info->chunk_root) {
Chris Mason8790d502008-04-03 16:29:03 -04002142 alloc_profile = info->avail_system_alloc_bits &
2143 info->system_alloc_profile;
2144 data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04002145 } else {
Chris Mason8790d502008-04-03 16:29:03 -04002146 alloc_profile = info->avail_metadata_alloc_bits &
2147 info->metadata_alloc_profile;
2148 data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile;
Chris Mason6324fbf2008-03-24 15:01:59 -04002149 }
Chris Mason98d20f62008-04-14 09:46:10 -04002150again:
Chris Masona061fc82008-05-07 11:43:44 -04002151 data = reduce_alloc_profile(root, data);
Chris Mason0ef3e662008-05-24 14:04:53 -04002152 /*
2153 * the only place that sets empty_size is btrfs_realloc_node, which
2154 * is not called recursively on allocations
2155 */
2156 if (empty_size || root->ref_cows) {
Chris Mason593060d2008-03-25 16:50:33 -04002157 if (!(data & BTRFS_BLOCK_GROUP_METADATA)) {
Chris Mason6324fbf2008-03-24 15:01:59 -04002158 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04002159 2 * 1024 * 1024,
2160 BTRFS_BLOCK_GROUP_METADATA |
2161 (info->metadata_alloc_profile &
2162 info->avail_metadata_alloc_bits), 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04002163 BUG_ON(ret);
2164 }
2165 ret = do_chunk_alloc(trans, root->fs_info->extent_root,
Chris Mason0ef3e662008-05-24 14:04:53 -04002166 num_bytes + 2 * 1024 * 1024, data, 0);
Chris Mason6324fbf2008-03-24 15:01:59 -04002167 BUG_ON(ret);
2168 }
Chris Mason0b86a832008-03-24 15:01:56 -04002169
Chris Masondb945352007-10-15 16:15:53 -04002170 WARN_ON(num_bytes < root->sectorsize);
2171 ret = find_free_extent(trans, root, num_bytes, empty_size,
2172 search_start, search_end, hint_byte, ins,
Chris Mason26b80032007-08-08 20:17:12 -04002173 trans->alloc_exclude_start,
2174 trans->alloc_exclude_nr, data);
Chris Mason3b951512008-04-17 11:29:12 -04002175
Chris Mason98d20f62008-04-14 09:46:10 -04002176 if (ret == -ENOSPC && num_bytes > min_alloc_size) {
2177 num_bytes = num_bytes >> 1;
2178 num_bytes = max(num_bytes, min_alloc_size);
Chris Mason0ef3e662008-05-24 14:04:53 -04002179 do_chunk_alloc(trans, root->fs_info->extent_root,
2180 num_bytes, data, 1);
Chris Mason98d20f62008-04-14 09:46:10 -04002181 goto again;
2182 }
Chris Masonec44a352008-04-28 15:29:52 -04002183 if (ret) {
2184 printk("allocation failed flags %Lu\n", data);
Chris Mason925baed2008-06-25 16:01:30 -04002185 BUG();
Chris Mason925baed2008-06-25 16:01:30 -04002186 }
Chris Masone6dcd2d2008-07-17 12:53:50 -04002187 clear_extent_dirty(&root->fs_info->free_space_cache,
2188 ins->objectid, ins->objectid + ins->offset - 1,
2189 GFP_NOFS);
2190 return 0;
2191}
2192
Chris Mason65b51a02008-08-01 15:11:20 -04002193int btrfs_free_reserved_extent(struct btrfs_root *root, u64 start, u64 len)
2194{
2195 maybe_lock_mutex(root);
2196 set_extent_dirty(&root->fs_info->free_space_cache,
2197 start, start + len - 1, GFP_NOFS);
2198 maybe_unlock_mutex(root);
2199 return 0;
2200}
2201
Chris Masone6dcd2d2008-07-17 12:53:50 -04002202int btrfs_reserve_extent(struct btrfs_trans_handle *trans,
2203 struct btrfs_root *root,
2204 u64 num_bytes, u64 min_alloc_size,
2205 u64 empty_size, u64 hint_byte,
2206 u64 search_end, struct btrfs_key *ins,
2207 u64 data)
2208{
2209 int ret;
2210 maybe_lock_mutex(root);
2211 ret = __btrfs_reserve_extent(trans, root, num_bytes, min_alloc_size,
2212 empty_size, hint_byte, search_end, ins,
2213 data);
2214 maybe_unlock_mutex(root);
2215 return ret;
2216}
2217
2218static int __btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
2219 struct btrfs_root *root,
2220 u64 root_objectid, u64 ref_generation,
2221 u64 owner, u64 owner_offset,
2222 struct btrfs_key *ins)
2223{
2224 int ret;
2225 int pending_ret;
2226 u64 super_used;
2227 u64 root_used;
2228 u64 num_bytes = ins->offset;
2229 u32 sizes[2];
2230 struct btrfs_fs_info *info = root->fs_info;
2231 struct btrfs_root *extent_root = info->extent_root;
2232 struct btrfs_extent_item *extent_item;
2233 struct btrfs_extent_ref *ref;
2234 struct btrfs_path *path;
2235 struct btrfs_key keys[2];
Chris Masonf2654de2007-06-26 12:20:46 -04002236
Josef Bacik58176a92007-08-29 15:47:34 -04002237 /* block accounting for super block */
Chris Masona2135012008-06-25 16:01:30 -04002238 spin_lock_irq(&info->delalloc_lock);
Chris Masondb945352007-10-15 16:15:53 -04002239 super_used = btrfs_super_bytes_used(&info->super_copy);
2240 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
Chris Masona2135012008-06-25 16:01:30 -04002241 spin_unlock_irq(&info->delalloc_lock);
Chris Mason26b80032007-08-08 20:17:12 -04002242
Josef Bacik58176a92007-08-29 15:47:34 -04002243 /* block accounting for root item */
Chris Masondb945352007-10-15 16:15:53 -04002244 root_used = btrfs_root_used(&root->root_item);
2245 btrfs_set_root_used(&root->root_item, root_used + num_bytes);
Josef Bacik58176a92007-08-29 15:47:34 -04002246
Chris Mason26b80032007-08-08 20:17:12 -04002247 if (root == extent_root) {
Chris Mason1a5bc162007-10-15 16:15:26 -04002248 set_extent_bits(&root->fs_info->extent_ins, ins->objectid,
2249 ins->objectid + ins->offset - 1,
2250 EXTENT_LOCKED, GFP_NOFS);
Chris Mason26b80032007-08-08 20:17:12 -04002251 goto update_block;
2252 }
2253
Chris Mason47e4bb92008-02-01 14:51:59 -05002254 memcpy(&keys[0], ins, sizeof(*ins));
2255 keys[1].offset = hash_extent_ref(root_objectid, ref_generation,
2256 owner, owner_offset);
2257 keys[1].objectid = ins->objectid;
2258 keys[1].type = BTRFS_EXTENT_REF_KEY;
2259 sizes[0] = sizeof(*extent_item);
2260 sizes[1] = sizeof(*ref);
Chris Mason7bb86312007-12-11 09:25:06 -05002261
2262 path = btrfs_alloc_path();
2263 BUG_ON(!path);
Chris Mason47e4bb92008-02-01 14:51:59 -05002264
2265 ret = btrfs_insert_empty_items(trans, extent_root, path, keys,
2266 sizes, 2);
Chris Mason26b80032007-08-08 20:17:12 -04002267
Chris Masonccd467d2007-06-28 15:57:36 -04002268 BUG_ON(ret);
Chris Mason47e4bb92008-02-01 14:51:59 -05002269 extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2270 struct btrfs_extent_item);
2271 btrfs_set_extent_refs(path->nodes[0], extent_item, 1);
2272 ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
2273 struct btrfs_extent_ref);
2274
2275 btrfs_set_ref_root(path->nodes[0], ref, root_objectid);
2276 btrfs_set_ref_generation(path->nodes[0], ref, ref_generation);
2277 btrfs_set_ref_objectid(path->nodes[0], ref, owner);
2278 btrfs_set_ref_offset(path->nodes[0], ref, owner_offset);
2279
2280 btrfs_mark_buffer_dirty(path->nodes[0]);
2281
2282 trans->alloc_exclude_start = 0;
2283 trans->alloc_exclude_nr = 0;
Chris Mason7bb86312007-12-11 09:25:06 -05002284 btrfs_free_path(path);
Chris Masone089f052007-03-16 16:20:31 -04002285 finish_current_insert(trans, extent_root);
Chris Masone20d96d2007-03-22 12:13:20 -04002286 pending_ret = del_pending_extents(trans, extent_root);
Chris Masonf510cfe2007-10-15 16:14:48 -04002287
Chris Mason925baed2008-06-25 16:01:30 -04002288 if (ret)
2289 goto out;
Chris Masone37c9e62007-05-09 20:13:14 -04002290 if (pending_ret) {
Chris Mason925baed2008-06-25 16:01:30 -04002291 ret = pending_ret;
2292 goto out;
Chris Masone37c9e62007-05-09 20:13:14 -04002293 }
Chris Mason26b80032007-08-08 20:17:12 -04002294
2295update_block:
Chris Mason0b86a832008-03-24 15:01:56 -04002296 ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0);
Chris Masonf5947062008-02-04 10:10:13 -05002297 if (ret) {
2298 printk("update block group failed for %Lu %Lu\n",
2299 ins->objectid, ins->offset);
2300 BUG();
2301 }
Chris Mason925baed2008-06-25 16:01:30 -04002302out:
Chris Masone6dcd2d2008-07-17 12:53:50 -04002303 return ret;
2304}
2305
2306int btrfs_alloc_reserved_extent(struct btrfs_trans_handle *trans,
2307 struct btrfs_root *root,
2308 u64 root_objectid, u64 ref_generation,
2309 u64 owner, u64 owner_offset,
2310 struct btrfs_key *ins)
2311{
2312 int ret;
2313 maybe_lock_mutex(root);
2314 ret = __btrfs_alloc_reserved_extent(trans, root, root_objectid,
2315 ref_generation, owner,
2316 owner_offset, ins);
2317 maybe_unlock_mutex(root);
2318 return ret;
2319}
Chris Masone02119d2008-09-05 16:13:11 -04002320
2321/*
2322 * this is used by the tree logging recovery code. It records that
2323 * an extent has been allocated and makes sure to clear the free
2324 * space cache bits as well
2325 */
2326int btrfs_alloc_logged_extent(struct btrfs_trans_handle *trans,
2327 struct btrfs_root *root,
2328 u64 root_objectid, u64 ref_generation,
2329 u64 owner, u64 owner_offset,
2330 struct btrfs_key *ins)
2331{
2332 int ret;
2333 struct btrfs_block_group_cache *block_group;
2334
2335 maybe_lock_mutex(root);
2336 block_group = btrfs_lookup_block_group(root->fs_info, ins->objectid);
2337 cache_block_group(root, block_group);
2338
2339 clear_extent_dirty(&root->fs_info->free_space_cache,
2340 ins->objectid, ins->objectid + ins->offset - 1,
2341 GFP_NOFS);
2342 ret = __btrfs_alloc_reserved_extent(trans, root, root_objectid,
2343 ref_generation, owner,
2344 owner_offset, ins);
2345 maybe_unlock_mutex(root);
2346 return ret;
2347}
2348
Chris Masone6dcd2d2008-07-17 12:53:50 -04002349/*
2350 * finds a free extent and does all the dirty work required for allocation
2351 * returns the key for the extent through ins, and a tree buffer for
2352 * the first block of the extent through buf.
2353 *
2354 * returns 0 if everything worked, non-zero otherwise.
2355 */
2356int btrfs_alloc_extent(struct btrfs_trans_handle *trans,
2357 struct btrfs_root *root,
2358 u64 num_bytes, u64 min_alloc_size,
2359 u64 root_objectid, u64 ref_generation,
2360 u64 owner, u64 owner_offset,
2361 u64 empty_size, u64 hint_byte,
2362 u64 search_end, struct btrfs_key *ins, u64 data)
2363{
2364 int ret;
2365
2366 maybe_lock_mutex(root);
2367
2368 ret = __btrfs_reserve_extent(trans, root, num_bytes,
2369 min_alloc_size, empty_size, hint_byte,
2370 search_end, ins, data);
2371 BUG_ON(ret);
Chris Masond00aff02008-09-11 15:54:42 -04002372 if (root_objectid != BTRFS_TREE_LOG_OBJECTID) {
2373 ret = __btrfs_alloc_reserved_extent(trans, root, root_objectid,
2374 ref_generation, owner,
2375 owner_offset, ins);
2376 BUG_ON(ret);
Chris Masone6dcd2d2008-07-17 12:53:50 -04002377
Chris Masond00aff02008-09-11 15:54:42 -04002378 }
Chris Mason925baed2008-06-25 16:01:30 -04002379 maybe_unlock_mutex(root);
2380 return ret;
Chris Masonfec577f2007-02-26 10:40:21 -05002381}
Chris Mason65b51a02008-08-01 15:11:20 -04002382
2383struct extent_buffer *btrfs_init_new_buffer(struct btrfs_trans_handle *trans,
2384 struct btrfs_root *root,
2385 u64 bytenr, u32 blocksize)
2386{
2387 struct extent_buffer *buf;
2388
2389 buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
2390 if (!buf)
2391 return ERR_PTR(-ENOMEM);
2392 btrfs_set_header_generation(buf, trans->transid);
2393 btrfs_tree_lock(buf);
2394 clean_tree_block(trans, root, buf);
2395 btrfs_set_buffer_uptodate(buf);
Chris Masond0c803c2008-09-11 16:17:57 -04002396 if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
2397 set_extent_dirty(&root->dirty_log_pages, buf->start,
Chris Mason65b51a02008-08-01 15:11:20 -04002398 buf->start + buf->len - 1, GFP_NOFS);
Chris Masond0c803c2008-09-11 16:17:57 -04002399 } else {
2400 set_extent_dirty(&trans->transaction->dirty_pages, buf->start,
2401 buf->start + buf->len - 1, GFP_NOFS);
2402 }
Chris Mason65b51a02008-08-01 15:11:20 -04002403 trans->blocks_used++;
2404 return buf;
2405}
2406
Chris Masonfec577f2007-02-26 10:40:21 -05002407/*
2408 * helper function to allocate a block for a given tree
2409 * returns the tree buffer or NULL.
2410 */
Chris Mason5f39d392007-10-15 16:14:19 -04002411struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans,
Chris Masondb945352007-10-15 16:15:53 -04002412 struct btrfs_root *root,
Chris Mason7bb86312007-12-11 09:25:06 -05002413 u32 blocksize,
Chris Mason7bb86312007-12-11 09:25:06 -05002414 u64 root_objectid,
2415 u64 ref_generation,
2416 u64 first_objectid,
2417 int level,
2418 u64 hint,
Chris Mason5f39d392007-10-15 16:14:19 -04002419 u64 empty_size)
Chris Masonfec577f2007-02-26 10:40:21 -05002420{
Chris Masone2fa7222007-03-12 16:22:34 -04002421 struct btrfs_key ins;
Chris Masonfec577f2007-02-26 10:40:21 -05002422 int ret;
Chris Mason5f39d392007-10-15 16:14:19 -04002423 struct extent_buffer *buf;
Chris Masonfec577f2007-02-26 10:40:21 -05002424
Chris Mason98d20f62008-04-14 09:46:10 -04002425 ret = btrfs_alloc_extent(trans, root, blocksize, blocksize,
Chris Mason7bb86312007-12-11 09:25:06 -05002426 root_objectid, ref_generation,
Chris Masonf6dbff52007-12-13 11:13:32 -05002427 level, first_objectid, empty_size, hint,
Chris Masondb945352007-10-15 16:15:53 -04002428 (u64)-1, &ins, 0);
Chris Masonfec577f2007-02-26 10:40:21 -05002429 if (ret) {
Chris Mason54aa1f42007-06-22 14:16:25 -04002430 BUG_ON(ret > 0);
2431 return ERR_PTR(ret);
Chris Masonfec577f2007-02-26 10:40:21 -05002432 }
Chris Mason55c69072008-01-09 15:55:33 -05002433
Chris Mason65b51a02008-08-01 15:11:20 -04002434 buf = btrfs_init_new_buffer(trans, root, ins.objectid, blocksize);
Chris Masonfec577f2007-02-26 10:40:21 -05002435 return buf;
2436}
Chris Masona28ec192007-03-06 20:08:01 -05002437
Chris Masone02119d2008-09-05 16:13:11 -04002438int btrfs_drop_leaf_ref(struct btrfs_trans_handle *trans,
2439 struct btrfs_root *root, struct extent_buffer *leaf)
Chris Mason6407bf62007-03-27 06:33:00 -04002440{
Chris Mason7bb86312007-12-11 09:25:06 -05002441 u64 leaf_owner;
2442 u64 leaf_generation;
Chris Mason5f39d392007-10-15 16:14:19 -04002443 struct btrfs_key key;
Chris Mason6407bf62007-03-27 06:33:00 -04002444 struct btrfs_file_extent_item *fi;
2445 int i;
2446 int nritems;
2447 int ret;
2448
Chris Mason5f39d392007-10-15 16:14:19 -04002449 BUG_ON(!btrfs_is_leaf(leaf));
2450 nritems = btrfs_header_nritems(leaf);
Chris Mason7bb86312007-12-11 09:25:06 -05002451 leaf_owner = btrfs_header_owner(leaf);
2452 leaf_generation = btrfs_header_generation(leaf);
2453
Chris Mason6407bf62007-03-27 06:33:00 -04002454 for (i = 0; i < nritems; i++) {
Chris Masondb945352007-10-15 16:15:53 -04002455 u64 disk_bytenr;
Chris Masone34a5b42008-07-22 12:08:37 -04002456 cond_resched();
Chris Mason5f39d392007-10-15 16:14:19 -04002457
2458 btrfs_item_key_to_cpu(leaf, &key, i);
2459 if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY)
Chris Mason6407bf62007-03-27 06:33:00 -04002460 continue;
2461 fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item);
Chris Mason5f39d392007-10-15 16:14:19 -04002462 if (btrfs_file_extent_type(leaf, fi) ==
2463 BTRFS_FILE_EXTENT_INLINE)
Chris Mason236454d2007-04-19 13:37:44 -04002464 continue;
Chris Mason6407bf62007-03-27 06:33:00 -04002465 /*
2466 * FIXME make sure to insert a trans record that
2467 * repeats the snapshot del on crash
2468 */
Chris Masondb945352007-10-15 16:15:53 -04002469 disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
2470 if (disk_bytenr == 0)
Chris Mason3a686372007-05-24 13:35:57 -04002471 continue;
Chris Mason4a096752008-07-21 10:29:44 -04002472
2473 mutex_lock(&root->fs_info->alloc_mutex);
Chris Mason925baed2008-06-25 16:01:30 -04002474 ret = __btrfs_free_extent(trans, root, disk_bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -05002475 btrfs_file_extent_disk_num_bytes(leaf, fi),
2476 leaf_owner, leaf_generation,
2477 key.objectid, key.offset, 0);
Chris Mason4a096752008-07-21 10:29:44 -04002478 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Mason2dd3e672008-08-04 08:20:15 -04002479
2480 atomic_inc(&root->fs_info->throttle_gen);
2481 wake_up(&root->fs_info->transaction_throttle);
2482 cond_resched();
2483
Chris Mason6407bf62007-03-27 06:33:00 -04002484 BUG_ON(ret);
2485 }
2486 return 0;
2487}
2488
Chris Masone02119d2008-09-05 16:13:11 -04002489static int noinline cache_drop_leaf_ref(struct btrfs_trans_handle *trans,
2490 struct btrfs_root *root,
2491 struct btrfs_leaf_ref *ref)
Yan Zheng31153d82008-07-28 15:32:19 -04002492{
2493 int i;
2494 int ret;
2495 struct btrfs_extent_info *info = ref->extents;
2496
Yan Zheng31153d82008-07-28 15:32:19 -04002497 for (i = 0; i < ref->nritems; i++) {
2498 mutex_lock(&root->fs_info->alloc_mutex);
2499 ret = __btrfs_free_extent(trans, root,
2500 info->bytenr, info->num_bytes,
2501 ref->owner, ref->generation,
2502 info->objectid, info->offset, 0);
2503 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Mason2dd3e672008-08-04 08:20:15 -04002504
2505 atomic_inc(&root->fs_info->throttle_gen);
2506 wake_up(&root->fs_info->transaction_throttle);
2507 cond_resched();
2508
Yan Zheng31153d82008-07-28 15:32:19 -04002509 BUG_ON(ret);
2510 info++;
2511 }
Yan Zheng31153d82008-07-28 15:32:19 -04002512
2513 return 0;
2514}
2515
Chris Mason333db942008-06-25 16:01:30 -04002516int drop_snap_lookup_refcount(struct btrfs_root *root, u64 start, u64 len,
2517 u32 *refs)
2518{
Chris Mason017e5362008-07-28 15:32:51 -04002519 int ret;
Chris Masonf87f0572008-08-01 11:27:23 -04002520
Chris Mason017e5362008-07-28 15:32:51 -04002521 ret = lookup_extent_ref(NULL, root, start, len, refs);
Chris Masonf87f0572008-08-01 11:27:23 -04002522 BUG_ON(ret);
2523
2524#if 0 // some debugging code in case we see problems here
2525 /* if the refs count is one, it won't get increased again. But
2526 * if the ref count is > 1, someone may be decreasing it at
2527 * the same time we are.
2528 */
2529 if (*refs != 1) {
2530 struct extent_buffer *eb = NULL;
2531 eb = btrfs_find_create_tree_block(root, start, len);
2532 if (eb)
2533 btrfs_tree_lock(eb);
2534
2535 mutex_lock(&root->fs_info->alloc_mutex);
2536 ret = lookup_extent_ref(NULL, root, start, len, refs);
2537 BUG_ON(ret);
2538 mutex_unlock(&root->fs_info->alloc_mutex);
2539
2540 if (eb) {
2541 btrfs_tree_unlock(eb);
2542 free_extent_buffer(eb);
2543 }
2544 if (*refs == 1) {
2545 printk("block %llu went down to one during drop_snap\n",
2546 (unsigned long long)start);
2547 }
2548
2549 }
2550#endif
2551
Chris Masone7a84562008-06-25 16:01:31 -04002552 cond_resched();
Chris Mason017e5362008-07-28 15:32:51 -04002553 return ret;
Chris Mason333db942008-06-25 16:01:30 -04002554}
2555
2556/*
Chris Mason9aca1d52007-03-13 11:09:37 -04002557 * helper function for drop_snapshot, this walks down the tree dropping ref
2558 * counts as it goes.
2559 */
Chris Mason98ed5172008-01-03 10:01:48 -05002560static int noinline walk_down_tree(struct btrfs_trans_handle *trans,
2561 struct btrfs_root *root,
2562 struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05002563{
Chris Mason7bb86312007-12-11 09:25:06 -05002564 u64 root_owner;
2565 u64 root_gen;
2566 u64 bytenr;
Chris Masonca7a79a2008-05-12 12:59:19 -04002567 u64 ptr_gen;
Chris Mason5f39d392007-10-15 16:14:19 -04002568 struct extent_buffer *next;
2569 struct extent_buffer *cur;
Chris Mason7bb86312007-12-11 09:25:06 -05002570 struct extent_buffer *parent;
Yan Zheng31153d82008-07-28 15:32:19 -04002571 struct btrfs_leaf_ref *ref;
Chris Masondb945352007-10-15 16:15:53 -04002572 u32 blocksize;
Chris Mason20524f02007-03-10 06:35:47 -05002573 int ret;
2574 u32 refs;
2575
Chris Mason5caf2a02007-04-02 11:20:42 -04002576 WARN_ON(*level < 0);
2577 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason333db942008-06-25 16:01:30 -04002578 ret = drop_snap_lookup_refcount(root, path->nodes[*level]->start,
Chris Masondb945352007-10-15 16:15:53 -04002579 path->nodes[*level]->len, &refs);
Chris Mason20524f02007-03-10 06:35:47 -05002580 BUG_ON(ret);
2581 if (refs > 1)
2582 goto out;
Chris Masone0115992007-06-19 16:23:05 -04002583
Chris Mason9aca1d52007-03-13 11:09:37 -04002584 /*
2585 * walk down to the last node level and free all the leaves
2586 */
Chris Mason6407bf62007-03-27 06:33:00 -04002587 while(*level >= 0) {
Chris Mason5caf2a02007-04-02 11:20:42 -04002588 WARN_ON(*level < 0);
2589 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason20524f02007-03-10 06:35:47 -05002590 cur = path->nodes[*level];
Chris Masone0115992007-06-19 16:23:05 -04002591
Chris Mason5f39d392007-10-15 16:14:19 -04002592 if (btrfs_header_level(cur) != *level)
Chris Mason2c90e5d2007-04-02 10:50:19 -04002593 WARN_ON(1);
Chris Masone0115992007-06-19 16:23:05 -04002594
Chris Mason7518a232007-03-12 12:01:18 -04002595 if (path->slots[*level] >=
Chris Mason5f39d392007-10-15 16:14:19 -04002596 btrfs_header_nritems(cur))
Chris Mason20524f02007-03-10 06:35:47 -05002597 break;
Chris Mason6407bf62007-03-27 06:33:00 -04002598 if (*level == 0) {
Chris Masone02119d2008-09-05 16:13:11 -04002599 ret = btrfs_drop_leaf_ref(trans, root, cur);
Chris Mason6407bf62007-03-27 06:33:00 -04002600 BUG_ON(ret);
2601 break;
2602 }
Chris Masondb945352007-10-15 16:15:53 -04002603 bytenr = btrfs_node_blockptr(cur, path->slots[*level]);
Chris Masonca7a79a2008-05-12 12:59:19 -04002604 ptr_gen = btrfs_node_ptr_generation(cur, path->slots[*level]);
Chris Masondb945352007-10-15 16:15:53 -04002605 blocksize = btrfs_level_size(root, *level - 1);
Chris Mason925baed2008-06-25 16:01:30 -04002606
Chris Mason333db942008-06-25 16:01:30 -04002607 ret = drop_snap_lookup_refcount(root, bytenr, blocksize, &refs);
Chris Mason6407bf62007-03-27 06:33:00 -04002608 BUG_ON(ret);
2609 if (refs != 1) {
Chris Mason7bb86312007-12-11 09:25:06 -05002610 parent = path->nodes[*level];
2611 root_owner = btrfs_header_owner(parent);
2612 root_gen = btrfs_header_generation(parent);
Chris Mason20524f02007-03-10 06:35:47 -05002613 path->slots[*level]++;
Chris Masonf87f0572008-08-01 11:27:23 -04002614
2615 mutex_lock(&root->fs_info->alloc_mutex);
Chris Mason925baed2008-06-25 16:01:30 -04002616 ret = __btrfs_free_extent(trans, root, bytenr,
Chris Mason7bb86312007-12-11 09:25:06 -05002617 blocksize, root_owner,
2618 root_gen, 0, 0, 1);
Chris Mason20524f02007-03-10 06:35:47 -05002619 BUG_ON(ret);
Chris Masonf87f0572008-08-01 11:27:23 -04002620 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Mason18e35e02008-08-01 13:11:41 -04002621
2622 atomic_inc(&root->fs_info->throttle_gen);
2623 wake_up(&root->fs_info->transaction_throttle);
Chris Mason2dd3e672008-08-04 08:20:15 -04002624 cond_resched();
Chris Mason18e35e02008-08-01 13:11:41 -04002625
Chris Mason20524f02007-03-10 06:35:47 -05002626 continue;
2627 }
Chris Masonf87f0572008-08-01 11:27:23 -04002628 /*
2629 * at this point, we have a single ref, and since the
2630 * only place referencing this extent is a dead root
2631 * the reference count should never go higher.
2632 * So, we don't need to check it again
2633 */
Yan Zheng31153d82008-07-28 15:32:19 -04002634 if (*level == 1) {
2635 struct btrfs_key key;
2636 btrfs_node_key_to_cpu(cur, &key, path->slots[*level]);
Chris Mason017e5362008-07-28 15:32:51 -04002637 ref = btrfs_lookup_leaf_ref(root, bytenr);
Yan Zheng31153d82008-07-28 15:32:19 -04002638 if (ref) {
Chris Masone02119d2008-09-05 16:13:11 -04002639 ret = cache_drop_leaf_ref(trans, root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04002640 BUG_ON(ret);
2641 btrfs_remove_leaf_ref(root, ref);
Yanbcc63ab2008-07-30 16:29:20 -04002642 btrfs_free_leaf_ref(root, ref);
Yan Zheng31153d82008-07-28 15:32:19 -04002643 *level = 0;
2644 break;
2645 }
Chris Mason37d1aee2008-07-31 10:48:37 -04002646 if (printk_ratelimit())
2647 printk("leaf ref miss for bytenr %llu\n",
2648 (unsigned long long)bytenr);
Yan Zheng31153d82008-07-28 15:32:19 -04002649 }
Chris Masondb945352007-10-15 16:15:53 -04002650 next = btrfs_find_tree_block(root, bytenr, blocksize);
Chris Mason1259ab72008-05-12 13:39:03 -04002651 if (!next || !btrfs_buffer_uptodate(next, ptr_gen)) {
Chris Mason5f39d392007-10-15 16:14:19 -04002652 free_extent_buffer(next);
Chris Mason333db942008-06-25 16:01:30 -04002653
Chris Masonca7a79a2008-05-12 12:59:19 -04002654 next = read_tree_block(root, bytenr, blocksize,
2655 ptr_gen);
Chris Masone7a84562008-06-25 16:01:31 -04002656 cond_resched();
Chris Masonf87f0572008-08-01 11:27:23 -04002657#if 0
2658 /*
2659 * this is a debugging check and can go away
2660 * the ref should never go all the way down to 1
2661 * at this point
2662 */
Chris Masone6dcd2d2008-07-17 12:53:50 -04002663 ret = lookup_extent_ref(NULL, root, bytenr, blocksize,
2664 &refs);
Chris Masone9d0b132007-08-10 14:06:19 -04002665 BUG_ON(ret);
Chris Masonf87f0572008-08-01 11:27:23 -04002666 WARN_ON(refs != 1);
2667#endif
Chris Masone9d0b132007-08-10 14:06:19 -04002668 }
Chris Mason5caf2a02007-04-02 11:20:42 -04002669 WARN_ON(*level <= 0);
Chris Mason83e15a22007-03-12 09:03:27 -04002670 if (path->nodes[*level-1])
Chris Mason5f39d392007-10-15 16:14:19 -04002671 free_extent_buffer(path->nodes[*level-1]);
Chris Mason20524f02007-03-10 06:35:47 -05002672 path->nodes[*level-1] = next;
Chris Mason5f39d392007-10-15 16:14:19 -04002673 *level = btrfs_header_level(next);
Chris Mason20524f02007-03-10 06:35:47 -05002674 path->slots[*level] = 0;
Chris Mason2dd3e672008-08-04 08:20:15 -04002675 cond_resched();
Chris Mason20524f02007-03-10 06:35:47 -05002676 }
2677out:
Chris Mason5caf2a02007-04-02 11:20:42 -04002678 WARN_ON(*level < 0);
2679 WARN_ON(*level >= BTRFS_MAX_LEVEL);
Chris Mason7bb86312007-12-11 09:25:06 -05002680
2681 if (path->nodes[*level] == root->node) {
Chris Mason7bb86312007-12-11 09:25:06 -05002682 parent = path->nodes[*level];
Yan Zheng31153d82008-07-28 15:32:19 -04002683 bytenr = path->nodes[*level]->start;
Chris Mason7bb86312007-12-11 09:25:06 -05002684 } else {
2685 parent = path->nodes[*level + 1];
Yan Zheng31153d82008-07-28 15:32:19 -04002686 bytenr = btrfs_node_blockptr(parent, path->slots[*level + 1]);
Chris Mason7bb86312007-12-11 09:25:06 -05002687 }
2688
Yan Zheng31153d82008-07-28 15:32:19 -04002689 blocksize = btrfs_level_size(root, *level);
2690 root_owner = btrfs_header_owner(parent);
Chris Mason7bb86312007-12-11 09:25:06 -05002691 root_gen = btrfs_header_generation(parent);
Yan Zheng31153d82008-07-28 15:32:19 -04002692
Chris Masonf87f0572008-08-01 11:27:23 -04002693 mutex_lock(&root->fs_info->alloc_mutex);
Yan Zheng31153d82008-07-28 15:32:19 -04002694 ret = __btrfs_free_extent(trans, root, bytenr, blocksize,
2695 root_owner, root_gen, 0, 0, 1);
Chris Mason5f39d392007-10-15 16:14:19 -04002696 free_extent_buffer(path->nodes[*level]);
Chris Mason20524f02007-03-10 06:35:47 -05002697 path->nodes[*level] = NULL;
2698 *level += 1;
2699 BUG_ON(ret);
Chris Mason925baed2008-06-25 16:01:30 -04002700 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Masonf87f0572008-08-01 11:27:23 -04002701
Chris Masone7a84562008-06-25 16:01:31 -04002702 cond_resched();
Chris Mason20524f02007-03-10 06:35:47 -05002703 return 0;
2704}
2705
Chris Mason9aca1d52007-03-13 11:09:37 -04002706/*
2707 * helper for dropping snapshots. This walks back up the tree in the path
2708 * to find the first node higher up where we haven't yet gone through
2709 * all the slots
2710 */
Chris Mason98ed5172008-01-03 10:01:48 -05002711static int noinline walk_up_tree(struct btrfs_trans_handle *trans,
2712 struct btrfs_root *root,
2713 struct btrfs_path *path, int *level)
Chris Mason20524f02007-03-10 06:35:47 -05002714{
Chris Mason7bb86312007-12-11 09:25:06 -05002715 u64 root_owner;
2716 u64 root_gen;
2717 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05002718 int i;
2719 int slot;
2720 int ret;
Chris Mason9f3a7422007-08-07 15:52:19 -04002721
Chris Mason234b63a2007-03-13 10:46:10 -04002722 for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) {
Chris Mason20524f02007-03-10 06:35:47 -05002723 slot = path->slots[i];
Chris Mason5f39d392007-10-15 16:14:19 -04002724 if (slot < btrfs_header_nritems(path->nodes[i]) - 1) {
2725 struct extent_buffer *node;
2726 struct btrfs_disk_key disk_key;
2727 node = path->nodes[i];
Chris Mason20524f02007-03-10 06:35:47 -05002728 path->slots[i]++;
2729 *level = i;
Chris Mason9f3a7422007-08-07 15:52:19 -04002730 WARN_ON(*level == 0);
Chris Mason5f39d392007-10-15 16:14:19 -04002731 btrfs_node_key(node, &disk_key, path->slots[i]);
Chris Mason9f3a7422007-08-07 15:52:19 -04002732 memcpy(&root_item->drop_progress,
Chris Mason5f39d392007-10-15 16:14:19 -04002733 &disk_key, sizeof(disk_key));
Chris Mason9f3a7422007-08-07 15:52:19 -04002734 root_item->drop_level = i;
Chris Mason20524f02007-03-10 06:35:47 -05002735 return 0;
2736 } else {
Chris Mason7bb86312007-12-11 09:25:06 -05002737 if (path->nodes[*level] == root->node) {
2738 root_owner = root->root_key.objectid;
2739 root_gen =
2740 btrfs_header_generation(path->nodes[*level]);
2741 } else {
2742 struct extent_buffer *node;
2743 node = path->nodes[*level + 1];
2744 root_owner = btrfs_header_owner(node);
2745 root_gen = btrfs_header_generation(node);
2746 }
Chris Masone089f052007-03-16 16:20:31 -04002747 ret = btrfs_free_extent(trans, root,
Chris Masondb945352007-10-15 16:15:53 -04002748 path->nodes[*level]->start,
Chris Mason7bb86312007-12-11 09:25:06 -05002749 path->nodes[*level]->len,
2750 root_owner, root_gen, 0, 0, 1);
Chris Mason6407bf62007-03-27 06:33:00 -04002751 BUG_ON(ret);
Chris Mason5f39d392007-10-15 16:14:19 -04002752 free_extent_buffer(path->nodes[*level]);
Chris Mason83e15a22007-03-12 09:03:27 -04002753 path->nodes[*level] = NULL;
Chris Mason20524f02007-03-10 06:35:47 -05002754 *level = i + 1;
Chris Mason20524f02007-03-10 06:35:47 -05002755 }
2756 }
2757 return 1;
2758}
2759
Chris Mason9aca1d52007-03-13 11:09:37 -04002760/*
2761 * drop the reference count on the tree rooted at 'snap'. This traverses
2762 * the tree freeing any blocks that have a ref count of zero after being
2763 * decremented.
2764 */
Chris Masone089f052007-03-16 16:20:31 -04002765int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
Chris Mason9f3a7422007-08-07 15:52:19 -04002766 *root)
Chris Mason20524f02007-03-10 06:35:47 -05002767{
Chris Mason3768f362007-03-13 16:47:54 -04002768 int ret = 0;
Chris Mason9aca1d52007-03-13 11:09:37 -04002769 int wret;
Chris Mason20524f02007-03-10 06:35:47 -05002770 int level;
Chris Mason5caf2a02007-04-02 11:20:42 -04002771 struct btrfs_path *path;
Chris Mason20524f02007-03-10 06:35:47 -05002772 int i;
2773 int orig_level;
Chris Mason9f3a7422007-08-07 15:52:19 -04002774 struct btrfs_root_item *root_item = &root->root_item;
Chris Mason20524f02007-03-10 06:35:47 -05002775
Chris Masona2135012008-06-25 16:01:30 -04002776 WARN_ON(!mutex_is_locked(&root->fs_info->drop_mutex));
Chris Mason5caf2a02007-04-02 11:20:42 -04002777 path = btrfs_alloc_path();
2778 BUG_ON(!path);
Chris Mason20524f02007-03-10 06:35:47 -05002779
Chris Mason5f39d392007-10-15 16:14:19 -04002780 level = btrfs_header_level(root->node);
Chris Mason20524f02007-03-10 06:35:47 -05002781 orig_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04002782 if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) {
2783 path->nodes[level] = root->node;
Chris Masonf510cfe2007-10-15 16:14:48 -04002784 extent_buffer_get(root->node);
Chris Mason9f3a7422007-08-07 15:52:19 -04002785 path->slots[level] = 0;
2786 } else {
2787 struct btrfs_key key;
Chris Mason5f39d392007-10-15 16:14:19 -04002788 struct btrfs_disk_key found_key;
2789 struct extent_buffer *node;
Chris Mason6702ed42007-08-07 16:15:09 -04002790
Chris Mason9f3a7422007-08-07 15:52:19 -04002791 btrfs_disk_key_to_cpu(&key, &root_item->drop_progress);
Chris Mason6702ed42007-08-07 16:15:09 -04002792 level = root_item->drop_level;
2793 path->lowest_level = level;
Chris Mason9f3a7422007-08-07 15:52:19 -04002794 wret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Chris Mason6702ed42007-08-07 16:15:09 -04002795 if (wret < 0) {
Chris Mason9f3a7422007-08-07 15:52:19 -04002796 ret = wret;
2797 goto out;
2798 }
Chris Mason5f39d392007-10-15 16:14:19 -04002799 node = path->nodes[level];
2800 btrfs_node_key(node, &found_key, path->slots[level]);
2801 WARN_ON(memcmp(&found_key, &root_item->drop_progress,
2802 sizeof(found_key)));
Chris Mason7d9eb122008-07-08 14:19:17 -04002803 /*
2804 * unlock our path, this is safe because only this
2805 * function is allowed to delete this snapshot
2806 */
Chris Mason925baed2008-06-25 16:01:30 -04002807 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
2808 if (path->nodes[i] && path->locks[i]) {
2809 path->locks[i] = 0;
2810 btrfs_tree_unlock(path->nodes[i]);
2811 }
2812 }
Chris Mason9f3a7422007-08-07 15:52:19 -04002813 }
Chris Mason20524f02007-03-10 06:35:47 -05002814 while(1) {
Chris Mason5caf2a02007-04-02 11:20:42 -04002815 wret = walk_down_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04002816 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05002817 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04002818 if (wret < 0)
2819 ret = wret;
2820
Chris Mason5caf2a02007-04-02 11:20:42 -04002821 wret = walk_up_tree(trans, root, path, &level);
Chris Mason9aca1d52007-03-13 11:09:37 -04002822 if (wret > 0)
Chris Mason20524f02007-03-10 06:35:47 -05002823 break;
Chris Mason9aca1d52007-03-13 11:09:37 -04002824 if (wret < 0)
2825 ret = wret;
Chris Masone7a84562008-06-25 16:01:31 -04002826 if (trans->transaction->in_commit) {
2827 ret = -EAGAIN;
2828 break;
2829 }
Chris Mason18e35e02008-08-01 13:11:41 -04002830 atomic_inc(&root->fs_info->throttle_gen);
Chris Mason017e5362008-07-28 15:32:51 -04002831 wake_up(&root->fs_info->transaction_throttle);
Chris Mason20524f02007-03-10 06:35:47 -05002832 }
Chris Mason83e15a22007-03-12 09:03:27 -04002833 for (i = 0; i <= orig_level; i++) {
Chris Mason5caf2a02007-04-02 11:20:42 -04002834 if (path->nodes[i]) {
Chris Mason5f39d392007-10-15 16:14:19 -04002835 free_extent_buffer(path->nodes[i]);
Chris Mason0f827312007-10-15 16:18:56 -04002836 path->nodes[i] = NULL;
Chris Mason83e15a22007-03-12 09:03:27 -04002837 }
Chris Mason20524f02007-03-10 06:35:47 -05002838 }
Chris Mason9f3a7422007-08-07 15:52:19 -04002839out:
Chris Mason5caf2a02007-04-02 11:20:42 -04002840 btrfs_free_path(path);
Chris Mason9aca1d52007-03-13 11:09:37 -04002841 return ret;
Chris Mason20524f02007-03-10 06:35:47 -05002842}
Chris Mason9078a3e2007-04-26 16:46:15 -04002843
Chris Masonbe744172007-05-06 10:15:01 -04002844int btrfs_free_block_groups(struct btrfs_fs_info *info)
2845{
Chris Masonf510cfe2007-10-15 16:14:48 -04002846 u64 start;
2847 u64 end;
Yanb97f9202007-11-01 11:28:41 -04002848 u64 ptr;
Chris Mason96b51792007-10-15 16:15:19 -04002849 int ret;
Chris Mason925baed2008-06-25 16:01:30 -04002850
2851 mutex_lock(&info->alloc_mutex);
Chris Mason96b51792007-10-15 16:15:19 -04002852 while(1) {
2853 ret = find_first_extent_bit(&info->block_group_cache, 0,
2854 &start, &end, (unsigned int)-1);
2855 if (ret)
2856 break;
Yanb97f9202007-11-01 11:28:41 -04002857 ret = get_state_private(&info->block_group_cache, start, &ptr);
2858 if (!ret)
2859 kfree((void *)(unsigned long)ptr);
Chris Mason96b51792007-10-15 16:15:19 -04002860 clear_extent_bits(&info->block_group_cache, start,
2861 end, (unsigned int)-1, GFP_NOFS);
2862 }
Chris Masone37c9e62007-05-09 20:13:14 -04002863 while(1) {
Chris Masonf510cfe2007-10-15 16:14:48 -04002864 ret = find_first_extent_bit(&info->free_space_cache, 0,
2865 &start, &end, EXTENT_DIRTY);
2866 if (ret)
Chris Masone37c9e62007-05-09 20:13:14 -04002867 break;
Chris Masonf510cfe2007-10-15 16:14:48 -04002868 clear_extent_dirty(&info->free_space_cache, start,
2869 end, GFP_NOFS);
Chris Masone37c9e62007-05-09 20:13:14 -04002870 }
Chris Mason925baed2008-06-25 16:01:30 -04002871 mutex_unlock(&info->alloc_mutex);
Chris Masonbe744172007-05-06 10:15:01 -04002872 return 0;
2873}
2874
Chris Mason8e7bf942008-04-28 09:02:36 -04002875static unsigned long calc_ra(unsigned long start, unsigned long last,
2876 unsigned long nr)
2877{
2878 return min(last, start + nr - 1);
2879}
2880
Chris Mason98ed5172008-01-03 10:01:48 -05002881static int noinline relocate_inode_pages(struct inode *inode, u64 start,
2882 u64 len)
Chris Masonedbd8d42007-12-21 16:27:24 -05002883{
2884 u64 page_start;
2885 u64 page_end;
Chris Masonedbd8d42007-12-21 16:27:24 -05002886 unsigned long last_index;
Chris Masonedbd8d42007-12-21 16:27:24 -05002887 unsigned long i;
2888 struct page *page;
Chris Masond1310b22008-01-24 16:13:08 -05002889 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
Chris Mason4313b392008-01-03 09:08:48 -05002890 struct file_ra_state *ra;
Chris Mason8e7bf942008-04-28 09:02:36 -04002891 unsigned long total_read = 0;
2892 unsigned long ra_pages;
Chris Mason3eaa2882008-07-24 11:57:52 -04002893 struct btrfs_ordered_extent *ordered;
Chris Masona061fc82008-05-07 11:43:44 -04002894 struct btrfs_trans_handle *trans;
Chris Mason4313b392008-01-03 09:08:48 -05002895
2896 ra = kzalloc(sizeof(*ra), GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05002897
2898 mutex_lock(&inode->i_mutex);
Chris Mason4313b392008-01-03 09:08:48 -05002899 i = start >> PAGE_CACHE_SHIFT;
Chris Masonedbd8d42007-12-21 16:27:24 -05002900 last_index = (start + len - 1) >> PAGE_CACHE_SHIFT;
2901
Chris Mason8e7bf942008-04-28 09:02:36 -04002902 ra_pages = BTRFS_I(inode)->root->fs_info->bdi.ra_pages;
2903
Chris Mason4313b392008-01-03 09:08:48 -05002904 file_ra_state_init(ra, inode->i_mapping);
Chris Masonedbd8d42007-12-21 16:27:24 -05002905
Chris Mason4313b392008-01-03 09:08:48 -05002906 for (; i <= last_index; i++) {
Chris Mason8e7bf942008-04-28 09:02:36 -04002907 if (total_read % ra_pages == 0) {
2908 btrfs_force_ra(inode->i_mapping, ra, NULL, i,
2909 calc_ra(i, last_index, ra_pages));
2910 }
2911 total_read++;
Chris Mason3eaa2882008-07-24 11:57:52 -04002912again:
2913 if (((u64)i << PAGE_CACHE_SHIFT) > i_size_read(inode))
Chris Masona061fc82008-05-07 11:43:44 -04002914 goto truncate_racing;
Chris Masonedbd8d42007-12-21 16:27:24 -05002915 page = grab_cache_page(inode->i_mapping, i);
Chris Masona061fc82008-05-07 11:43:44 -04002916 if (!page) {
Chris Masonedbd8d42007-12-21 16:27:24 -05002917 goto out_unlock;
Chris Masona061fc82008-05-07 11:43:44 -04002918 }
Chris Masonedbd8d42007-12-21 16:27:24 -05002919 if (!PageUptodate(page)) {
2920 btrfs_readpage(NULL, page);
2921 lock_page(page);
2922 if (!PageUptodate(page)) {
2923 unlock_page(page);
2924 page_cache_release(page);
2925 goto out_unlock;
2926 }
2927 }
Chris Masonec44a352008-04-28 15:29:52 -04002928 wait_on_page_writeback(page);
Chris Mason3eaa2882008-07-24 11:57:52 -04002929
Chris Masonedbd8d42007-12-21 16:27:24 -05002930 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2931 page_end = page_start + PAGE_CACHE_SIZE - 1;
Chris Masond1310b22008-01-24 16:13:08 -05002932 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05002933
Chris Mason3eaa2882008-07-24 11:57:52 -04002934 ordered = btrfs_lookup_ordered_extent(inode, page_start);
2935 if (ordered) {
2936 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
2937 unlock_page(page);
2938 page_cache_release(page);
2939 btrfs_start_ordered_extent(inode, ordered, 1);
2940 btrfs_put_ordered_extent(ordered);
2941 goto again;
2942 }
2943 set_page_extent_mapped(page);
2944
Chris Masonf87f0572008-08-01 11:27:23 -04002945 /*
2946 * make sure page_mkwrite is called for this page if userland
2947 * wants to change it from mmap
2948 */
2949 clear_page_dirty_for_io(page);
Chris Mason3eaa2882008-07-24 11:57:52 -04002950
Chris Masonea8c2812008-08-04 23:17:27 -04002951 btrfs_set_extent_delalloc(inode, page_start, page_end);
Chris Masona061fc82008-05-07 11:43:44 -04002952 set_page_dirty(page);
Chris Masonedbd8d42007-12-21 16:27:24 -05002953
Chris Masond1310b22008-01-24 16:13:08 -05002954 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05002955 unlock_page(page);
2956 page_cache_release(page);
2957 }
2958
2959out_unlock:
Chris Mason3eaa2882008-07-24 11:57:52 -04002960 /* we have to start the IO in order to get the ordered extents
2961 * instantiated. This allows the relocation to code to wait
2962 * for all the ordered extents to hit the disk.
2963 *
2964 * Otherwise, it would constantly loop over the same extents
2965 * because the old ones don't get deleted until the IO is
2966 * started
2967 */
2968 btrfs_fdatawrite_range(inode->i_mapping, start, start + len - 1,
2969 WB_SYNC_NONE);
Chris Masonec44a352008-04-28 15:29:52 -04002970 kfree(ra);
Chris Masona061fc82008-05-07 11:43:44 -04002971 trans = btrfs_start_transaction(BTRFS_I(inode)->root, 1);
2972 if (trans) {
Chris Masona061fc82008-05-07 11:43:44 -04002973 btrfs_end_transaction(trans, BTRFS_I(inode)->root);
2974 mark_inode_dirty(inode);
2975 }
Chris Masonedbd8d42007-12-21 16:27:24 -05002976 mutex_unlock(&inode->i_mutex);
2977 return 0;
Chris Masona061fc82008-05-07 11:43:44 -04002978
2979truncate_racing:
2980 vmtruncate(inode, inode->i_size);
2981 balance_dirty_pages_ratelimited_nr(inode->i_mapping,
2982 total_read);
2983 goto out_unlock;
Chris Masonedbd8d42007-12-21 16:27:24 -05002984}
2985
Chris Mason4313b392008-01-03 09:08:48 -05002986/*
Chris Masonbf4ef672008-05-08 13:26:18 -04002987 * The back references tell us which tree holds a ref on a block,
2988 * but it is possible for the tree root field in the reference to
2989 * reflect the original root before a snapshot was made. In this
2990 * case we should search through all the children of a given root
2991 * to find potential holders of references on a block.
2992 *
2993 * Instead, we do something a little less fancy and just search
2994 * all the roots for a given key/block combination.
2995 */
2996static int find_root_for_ref(struct btrfs_root *root,
2997 struct btrfs_path *path,
2998 struct btrfs_key *key0,
2999 int level,
3000 int file_key,
3001 struct btrfs_root **found_root,
3002 u64 bytenr)
3003{
3004 struct btrfs_key root_location;
3005 struct btrfs_root *cur_root = *found_root;
3006 struct btrfs_file_extent_item *file_extent;
3007 u64 root_search_start = BTRFS_FS_TREE_OBJECTID;
3008 u64 found_bytenr;
3009 int ret;
Chris Masonbf4ef672008-05-08 13:26:18 -04003010
3011 root_location.offset = (u64)-1;
3012 root_location.type = BTRFS_ROOT_ITEM_KEY;
3013 path->lowest_level = level;
3014 path->reada = 0;
3015 while(1) {
3016 ret = btrfs_search_slot(NULL, cur_root, key0, path, 0, 0);
3017 found_bytenr = 0;
3018 if (ret == 0 && file_key) {
3019 struct extent_buffer *leaf = path->nodes[0];
3020 file_extent = btrfs_item_ptr(leaf, path->slots[0],
3021 struct btrfs_file_extent_item);
3022 if (btrfs_file_extent_type(leaf, file_extent) ==
3023 BTRFS_FILE_EXTENT_REG) {
3024 found_bytenr =
3025 btrfs_file_extent_disk_bytenr(leaf,
3026 file_extent);
3027 }
Chris Mason323da792008-05-09 11:46:48 -04003028 } else if (!file_key) {
Chris Masonbf4ef672008-05-08 13:26:18 -04003029 if (path->nodes[level])
3030 found_bytenr = path->nodes[level]->start;
3031 }
3032
Chris Masonbf4ef672008-05-08 13:26:18 -04003033 btrfs_release_path(cur_root, path);
3034
3035 if (found_bytenr == bytenr) {
3036 *found_root = cur_root;
3037 ret = 0;
3038 goto out;
3039 }
3040 ret = btrfs_search_root(root->fs_info->tree_root,
3041 root_search_start, &root_search_start);
3042 if (ret)
3043 break;
3044
3045 root_location.objectid = root_search_start;
3046 cur_root = btrfs_read_fs_root_no_name(root->fs_info,
3047 &root_location);
3048 if (!cur_root) {
3049 ret = 1;
3050 break;
3051 }
3052 }
3053out:
3054 path->lowest_level = 0;
3055 return ret;
3056}
3057
3058/*
Chris Mason4313b392008-01-03 09:08:48 -05003059 * note, this releases the path
3060 */
Chris Mason98ed5172008-01-03 10:01:48 -05003061static int noinline relocate_one_reference(struct btrfs_root *extent_root,
Chris Masonedbd8d42007-12-21 16:27:24 -05003062 struct btrfs_path *path,
Chris Mason0ef3e662008-05-24 14:04:53 -04003063 struct btrfs_key *extent_key,
3064 u64 *last_file_objectid,
3065 u64 *last_file_offset,
3066 u64 *last_file_root,
3067 u64 last_extent)
Chris Masonedbd8d42007-12-21 16:27:24 -05003068{
3069 struct inode *inode;
3070 struct btrfs_root *found_root;
Chris Masonbf4ef672008-05-08 13:26:18 -04003071 struct btrfs_key root_location;
3072 struct btrfs_key found_key;
Chris Mason4313b392008-01-03 09:08:48 -05003073 struct btrfs_extent_ref *ref;
3074 u64 ref_root;
3075 u64 ref_gen;
3076 u64 ref_objectid;
3077 u64 ref_offset;
Chris Masonedbd8d42007-12-21 16:27:24 -05003078 int ret;
Chris Masonbf4ef672008-05-08 13:26:18 -04003079 int level;
Chris Masonedbd8d42007-12-21 16:27:24 -05003080
Chris Mason7d9eb122008-07-08 14:19:17 -04003081 WARN_ON(!mutex_is_locked(&extent_root->fs_info->alloc_mutex));
3082
Chris Mason4313b392008-01-03 09:08:48 -05003083 ref = btrfs_item_ptr(path->nodes[0], path->slots[0],
3084 struct btrfs_extent_ref);
3085 ref_root = btrfs_ref_root(path->nodes[0], ref);
3086 ref_gen = btrfs_ref_generation(path->nodes[0], ref);
3087 ref_objectid = btrfs_ref_objectid(path->nodes[0], ref);
3088 ref_offset = btrfs_ref_offset(path->nodes[0], ref);
3089 btrfs_release_path(extent_root, path);
3090
Chris Masonbf4ef672008-05-08 13:26:18 -04003091 root_location.objectid = ref_root;
Chris Masonedbd8d42007-12-21 16:27:24 -05003092 if (ref_gen == 0)
Chris Masonbf4ef672008-05-08 13:26:18 -04003093 root_location.offset = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05003094 else
Chris Masonbf4ef672008-05-08 13:26:18 -04003095 root_location.offset = (u64)-1;
3096 root_location.type = BTRFS_ROOT_ITEM_KEY;
Chris Masonedbd8d42007-12-21 16:27:24 -05003097
3098 found_root = btrfs_read_fs_root_no_name(extent_root->fs_info,
Chris Masonbf4ef672008-05-08 13:26:18 -04003099 &root_location);
Chris Masonedbd8d42007-12-21 16:27:24 -05003100 BUG_ON(!found_root);
Chris Mason7d9eb122008-07-08 14:19:17 -04003101 mutex_unlock(&extent_root->fs_info->alloc_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05003102
3103 if (ref_objectid >= BTRFS_FIRST_FREE_OBJECTID) {
Chris Masonbf4ef672008-05-08 13:26:18 -04003104 found_key.objectid = ref_objectid;
3105 found_key.type = BTRFS_EXTENT_DATA_KEY;
3106 found_key.offset = ref_offset;
3107 level = 0;
3108
Chris Mason0ef3e662008-05-24 14:04:53 -04003109 if (last_extent == extent_key->objectid &&
3110 *last_file_objectid == ref_objectid &&
3111 *last_file_offset == ref_offset &&
3112 *last_file_root == ref_root)
3113 goto out;
3114
Chris Masonbf4ef672008-05-08 13:26:18 -04003115 ret = find_root_for_ref(extent_root, path, &found_key,
3116 level, 1, &found_root,
3117 extent_key->objectid);
3118
3119 if (ret)
3120 goto out;
3121
Chris Mason0ef3e662008-05-24 14:04:53 -04003122 if (last_extent == extent_key->objectid &&
3123 *last_file_objectid == ref_objectid &&
3124 *last_file_offset == ref_offset &&
3125 *last_file_root == ref_root)
3126 goto out;
3127
Chris Masonedbd8d42007-12-21 16:27:24 -05003128 inode = btrfs_iget_locked(extent_root->fs_info->sb,
3129 ref_objectid, found_root);
3130 if (inode->i_state & I_NEW) {
3131 /* the inode and parent dir are two different roots */
3132 BTRFS_I(inode)->root = found_root;
3133 BTRFS_I(inode)->location.objectid = ref_objectid;
3134 BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
3135 BTRFS_I(inode)->location.offset = 0;
3136 btrfs_read_locked_inode(inode);
3137 unlock_new_inode(inode);
3138
3139 }
3140 /* this can happen if the reference is not against
3141 * the latest version of the tree root
3142 */
Chris Mason7d9eb122008-07-08 14:19:17 -04003143 if (is_bad_inode(inode))
Chris Masonedbd8d42007-12-21 16:27:24 -05003144 goto out;
Chris Mason7d9eb122008-07-08 14:19:17 -04003145
Chris Mason0ef3e662008-05-24 14:04:53 -04003146 *last_file_objectid = inode->i_ino;
3147 *last_file_root = found_root->root_key.objectid;
3148 *last_file_offset = ref_offset;
3149
Chris Masonedbd8d42007-12-21 16:27:24 -05003150 relocate_inode_pages(inode, ref_offset, extent_key->offset);
Chris Masonedbd8d42007-12-21 16:27:24 -05003151 iput(inode);
Chris Masonedbd8d42007-12-21 16:27:24 -05003152 } else {
3153 struct btrfs_trans_handle *trans;
Chris Masonedbd8d42007-12-21 16:27:24 -05003154 struct extent_buffer *eb;
Chris Mason7d9eb122008-07-08 14:19:17 -04003155 int needs_lock = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05003156
Chris Masonedbd8d42007-12-21 16:27:24 -05003157 eb = read_tree_block(found_root, extent_key->objectid,
Chris Masonca7a79a2008-05-12 12:59:19 -04003158 extent_key->offset, 0);
Chris Mason925baed2008-06-25 16:01:30 -04003159 btrfs_tree_lock(eb);
Chris Masonedbd8d42007-12-21 16:27:24 -05003160 level = btrfs_header_level(eb);
3161
3162 if (level == 0)
3163 btrfs_item_key_to_cpu(eb, &found_key, 0);
3164 else
3165 btrfs_node_key_to_cpu(eb, &found_key, 0);
3166
Chris Mason925baed2008-06-25 16:01:30 -04003167 btrfs_tree_unlock(eb);
Chris Masonedbd8d42007-12-21 16:27:24 -05003168 free_extent_buffer(eb);
3169
Chris Masonbf4ef672008-05-08 13:26:18 -04003170 ret = find_root_for_ref(extent_root, path, &found_key,
3171 level, 0, &found_root,
3172 extent_key->objectid);
3173
3174 if (ret)
3175 goto out;
3176
Chris Mason7d9eb122008-07-08 14:19:17 -04003177 /*
3178 * right here almost anything could happen to our key,
3179 * but that's ok. The cow below will either relocate it
3180 * or someone else will have relocated it. Either way,
3181 * it is in a different spot than it was before and
3182 * we're happy.
3183 */
3184
Chris Masonbf4ef672008-05-08 13:26:18 -04003185 trans = btrfs_start_transaction(found_root, 1);
3186
Chris Mason7d9eb122008-07-08 14:19:17 -04003187 if (found_root == extent_root->fs_info->extent_root ||
3188 found_root == extent_root->fs_info->chunk_root ||
3189 found_root == extent_root->fs_info->dev_root) {
3190 needs_lock = 1;
3191 mutex_lock(&extent_root->fs_info->alloc_mutex);
3192 }
3193
Chris Masonedbd8d42007-12-21 16:27:24 -05003194 path->lowest_level = level;
Chris Mason8f662a72008-01-02 10:01:11 -05003195 path->reada = 2;
Chris Masonedbd8d42007-12-21 16:27:24 -05003196 ret = btrfs_search_slot(trans, found_root, &found_key, path,
3197 0, 1);
3198 path->lowest_level = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05003199 btrfs_release_path(found_root, path);
Chris Mason7d9eb122008-07-08 14:19:17 -04003200
Chris Mason0ef3e662008-05-24 14:04:53 -04003201 if (found_root == found_root->fs_info->extent_root)
3202 btrfs_extent_post_op(trans, found_root);
Chris Mason7d9eb122008-07-08 14:19:17 -04003203 if (needs_lock)
3204 mutex_unlock(&extent_root->fs_info->alloc_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05003205
Chris Mason7d9eb122008-07-08 14:19:17 -04003206 btrfs_end_transaction(trans, found_root);
3207
3208 }
Chris Masonedbd8d42007-12-21 16:27:24 -05003209out:
Chris Mason7d9eb122008-07-08 14:19:17 -04003210 mutex_lock(&extent_root->fs_info->alloc_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05003211 return 0;
3212}
3213
Chris Masona061fc82008-05-07 11:43:44 -04003214static int noinline del_extent_zero(struct btrfs_root *extent_root,
3215 struct btrfs_path *path,
3216 struct btrfs_key *extent_key)
3217{
3218 int ret;
3219 struct btrfs_trans_handle *trans;
3220
3221 trans = btrfs_start_transaction(extent_root, 1);
3222 ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1);
3223 if (ret > 0) {
3224 ret = -EIO;
3225 goto out;
3226 }
3227 if (ret < 0)
3228 goto out;
3229 ret = btrfs_del_item(trans, extent_root, path);
3230out:
3231 btrfs_end_transaction(trans, extent_root);
3232 return ret;
3233}
3234
Chris Mason98ed5172008-01-03 10:01:48 -05003235static int noinline relocate_one_extent(struct btrfs_root *extent_root,
3236 struct btrfs_path *path,
3237 struct btrfs_key *extent_key)
Chris Masonedbd8d42007-12-21 16:27:24 -05003238{
3239 struct btrfs_key key;
3240 struct btrfs_key found_key;
Chris Masonedbd8d42007-12-21 16:27:24 -05003241 struct extent_buffer *leaf;
Chris Mason0ef3e662008-05-24 14:04:53 -04003242 u64 last_file_objectid = 0;
3243 u64 last_file_root = 0;
3244 u64 last_file_offset = (u64)-1;
3245 u64 last_extent = 0;
Chris Masonedbd8d42007-12-21 16:27:24 -05003246 u32 nritems;
3247 u32 item_size;
3248 int ret = 0;
3249
Chris Masona061fc82008-05-07 11:43:44 -04003250 if (extent_key->objectid == 0) {
3251 ret = del_extent_zero(extent_root, path, extent_key);
3252 goto out;
3253 }
Chris Masonedbd8d42007-12-21 16:27:24 -05003254 key.objectid = extent_key->objectid;
3255 key.type = BTRFS_EXTENT_REF_KEY;
3256 key.offset = 0;
3257
3258 while(1) {
3259 ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0);
3260
Chris Masonedbd8d42007-12-21 16:27:24 -05003261 if (ret < 0)
3262 goto out;
3263
3264 ret = 0;
3265 leaf = path->nodes[0];
3266 nritems = btrfs_header_nritems(leaf);
Chris Masona061fc82008-05-07 11:43:44 -04003267 if (path->slots[0] == nritems) {
3268 ret = btrfs_next_leaf(extent_root, path);
3269 if (ret > 0) {
3270 ret = 0;
3271 goto out;
3272 }
3273 if (ret < 0)
3274 goto out;
Chris Masonbf4ef672008-05-08 13:26:18 -04003275 leaf = path->nodes[0];
Chris Masona061fc82008-05-07 11:43:44 -04003276 }
Chris Masonedbd8d42007-12-21 16:27:24 -05003277
3278 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Masona061fc82008-05-07 11:43:44 -04003279 if (found_key.objectid != extent_key->objectid) {
Chris Masonedbd8d42007-12-21 16:27:24 -05003280 break;
Chris Masona061fc82008-05-07 11:43:44 -04003281 }
Chris Masonedbd8d42007-12-21 16:27:24 -05003282
Chris Masona061fc82008-05-07 11:43:44 -04003283 if (found_key.type != BTRFS_EXTENT_REF_KEY) {
Chris Masonedbd8d42007-12-21 16:27:24 -05003284 break;
Chris Masona061fc82008-05-07 11:43:44 -04003285 }
Chris Masonedbd8d42007-12-21 16:27:24 -05003286
3287 key.offset = found_key.offset + 1;
3288 item_size = btrfs_item_size_nr(leaf, path->slots[0]);
3289
Chris Mason0ef3e662008-05-24 14:04:53 -04003290 ret = relocate_one_reference(extent_root, path, extent_key,
3291 &last_file_objectid,
3292 &last_file_offset,
3293 &last_file_root, last_extent);
Chris Masonedbd8d42007-12-21 16:27:24 -05003294 if (ret)
3295 goto out;
Chris Mason0ef3e662008-05-24 14:04:53 -04003296 last_extent = extent_key->objectid;
Chris Masonedbd8d42007-12-21 16:27:24 -05003297 }
3298 ret = 0;
3299out:
3300 btrfs_release_path(extent_root, path);
3301 return ret;
3302}
3303
Chris Masonec44a352008-04-28 15:29:52 -04003304static u64 update_block_group_flags(struct btrfs_root *root, u64 flags)
3305{
3306 u64 num_devices;
3307 u64 stripped = BTRFS_BLOCK_GROUP_RAID0 |
3308 BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10;
3309
Chris Masona061fc82008-05-07 11:43:44 -04003310 num_devices = root->fs_info->fs_devices->num_devices;
Chris Masonec44a352008-04-28 15:29:52 -04003311 if (num_devices == 1) {
3312 stripped |= BTRFS_BLOCK_GROUP_DUP;
3313 stripped = flags & ~stripped;
3314
3315 /* turn raid0 into single device chunks */
3316 if (flags & BTRFS_BLOCK_GROUP_RAID0)
3317 return stripped;
3318
3319 /* turn mirroring into duplication */
3320 if (flags & (BTRFS_BLOCK_GROUP_RAID1 |
3321 BTRFS_BLOCK_GROUP_RAID10))
3322 return stripped | BTRFS_BLOCK_GROUP_DUP;
3323 return flags;
3324 } else {
3325 /* they already had raid on here, just return */
Chris Masonec44a352008-04-28 15:29:52 -04003326 if (flags & stripped)
3327 return flags;
3328
3329 stripped |= BTRFS_BLOCK_GROUP_DUP;
3330 stripped = flags & ~stripped;
3331
3332 /* switch duplicated blocks with raid1 */
3333 if (flags & BTRFS_BLOCK_GROUP_DUP)
3334 return stripped | BTRFS_BLOCK_GROUP_RAID1;
3335
3336 /* turn single device chunks into raid0 */
3337 return stripped | BTRFS_BLOCK_GROUP_RAID0;
3338 }
3339 return flags;
3340}
3341
Chris Mason0ef3e662008-05-24 14:04:53 -04003342int __alloc_chunk_for_shrink(struct btrfs_root *root,
3343 struct btrfs_block_group_cache *shrink_block_group,
3344 int force)
3345{
3346 struct btrfs_trans_handle *trans;
3347 u64 new_alloc_flags;
3348 u64 calc;
3349
Chris Masonc286ac42008-07-22 23:06:41 -04003350 spin_lock(&shrink_block_group->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04003351 if (btrfs_block_group_used(&shrink_block_group->item) > 0) {
Chris Masonc286ac42008-07-22 23:06:41 -04003352 spin_unlock(&shrink_block_group->lock);
Chris Mason7d9eb122008-07-08 14:19:17 -04003353 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Masonc286ac42008-07-22 23:06:41 -04003354
Chris Mason0ef3e662008-05-24 14:04:53 -04003355 trans = btrfs_start_transaction(root, 1);
Chris Mason7d9eb122008-07-08 14:19:17 -04003356 mutex_lock(&root->fs_info->alloc_mutex);
Chris Masonc286ac42008-07-22 23:06:41 -04003357 spin_lock(&shrink_block_group->lock);
Chris Mason7d9eb122008-07-08 14:19:17 -04003358
Chris Mason0ef3e662008-05-24 14:04:53 -04003359 new_alloc_flags = update_block_group_flags(root,
3360 shrink_block_group->flags);
3361 if (new_alloc_flags != shrink_block_group->flags) {
3362 calc =
3363 btrfs_block_group_used(&shrink_block_group->item);
3364 } else {
3365 calc = shrink_block_group->key.offset;
3366 }
Chris Masonc286ac42008-07-22 23:06:41 -04003367 spin_unlock(&shrink_block_group->lock);
3368
Chris Mason0ef3e662008-05-24 14:04:53 -04003369 do_chunk_alloc(trans, root->fs_info->extent_root,
3370 calc + 2 * 1024 * 1024, new_alloc_flags, force);
Chris Mason7d9eb122008-07-08 14:19:17 -04003371
3372 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Mason0ef3e662008-05-24 14:04:53 -04003373 btrfs_end_transaction(trans, root);
Chris Mason7d9eb122008-07-08 14:19:17 -04003374 mutex_lock(&root->fs_info->alloc_mutex);
Chris Masonc286ac42008-07-22 23:06:41 -04003375 } else
3376 spin_unlock(&shrink_block_group->lock);
Chris Mason0ef3e662008-05-24 14:04:53 -04003377 return 0;
3378}
3379
Chris Mason8f18cf12008-04-25 16:53:30 -04003380int btrfs_shrink_extent_tree(struct btrfs_root *root, u64 shrink_start)
Chris Masonedbd8d42007-12-21 16:27:24 -05003381{
3382 struct btrfs_trans_handle *trans;
3383 struct btrfs_root *tree_root = root->fs_info->tree_root;
3384 struct btrfs_path *path;
3385 u64 cur_byte;
3386 u64 total_found;
Chris Mason8f18cf12008-04-25 16:53:30 -04003387 u64 shrink_last_byte;
3388 struct btrfs_block_group_cache *shrink_block_group;
Chris Masonedbd8d42007-12-21 16:27:24 -05003389 struct btrfs_fs_info *info = root->fs_info;
Chris Masonedbd8d42007-12-21 16:27:24 -05003390 struct btrfs_key key;
Yan73e48b22008-01-03 14:14:39 -05003391 struct btrfs_key found_key;
Chris Masonedbd8d42007-12-21 16:27:24 -05003392 struct extent_buffer *leaf;
3393 u32 nritems;
3394 int ret;
Chris Masona061fc82008-05-07 11:43:44 -04003395 int progress;
Chris Masonedbd8d42007-12-21 16:27:24 -05003396
Chris Mason925baed2008-06-25 16:01:30 -04003397 mutex_lock(&root->fs_info->alloc_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04003398 shrink_block_group = btrfs_lookup_block_group(root->fs_info,
3399 shrink_start);
3400 BUG_ON(!shrink_block_group);
3401
Chris Mason0ef3e662008-05-24 14:04:53 -04003402 shrink_last_byte = shrink_block_group->key.objectid +
3403 shrink_block_group->key.offset;
Chris Mason8f18cf12008-04-25 16:53:30 -04003404
3405 shrink_block_group->space_info->total_bytes -=
3406 shrink_block_group->key.offset;
Chris Masonedbd8d42007-12-21 16:27:24 -05003407 path = btrfs_alloc_path();
3408 root = root->fs_info->extent_root;
Chris Mason8f662a72008-01-02 10:01:11 -05003409 path->reada = 2;
Chris Masonedbd8d42007-12-21 16:27:24 -05003410
Chris Mason323da792008-05-09 11:46:48 -04003411 printk("btrfs relocating block group %llu flags %llu\n",
3412 (unsigned long long)shrink_start,
3413 (unsigned long long)shrink_block_group->flags);
3414
Chris Mason0ef3e662008-05-24 14:04:53 -04003415 __alloc_chunk_for_shrink(root, shrink_block_group, 1);
Chris Mason323da792008-05-09 11:46:48 -04003416
Chris Mason0ef3e662008-05-24 14:04:53 -04003417again:
3418
Chris Mason8f18cf12008-04-25 16:53:30 -04003419 shrink_block_group->ro = 1;
3420
Chris Masonedbd8d42007-12-21 16:27:24 -05003421 total_found = 0;
Chris Masona061fc82008-05-07 11:43:44 -04003422 progress = 0;
Chris Mason8f18cf12008-04-25 16:53:30 -04003423 key.objectid = shrink_start;
Chris Masonedbd8d42007-12-21 16:27:24 -05003424 key.offset = 0;
3425 key.type = 0;
Yan73e48b22008-01-03 14:14:39 -05003426 cur_byte = key.objectid;
Chris Mason4313b392008-01-03 09:08:48 -05003427
Chris Masonea8c2812008-08-04 23:17:27 -04003428 mutex_unlock(&root->fs_info->alloc_mutex);
3429
3430 btrfs_start_delalloc_inodes(root);
Yan Zheng7ea394f2008-08-05 13:05:02 -04003431 btrfs_wait_ordered_extents(tree_root, 0);
Chris Masonea8c2812008-08-04 23:17:27 -04003432
3433 mutex_lock(&root->fs_info->alloc_mutex);
3434
Yan73e48b22008-01-03 14:14:39 -05003435 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3436 if (ret < 0)
3437 goto out;
3438
Chris Mason0b86a832008-03-24 15:01:56 -04003439 ret = btrfs_previous_item(root, path, 0, BTRFS_EXTENT_ITEM_KEY);
Yan73e48b22008-01-03 14:14:39 -05003440 if (ret < 0)
3441 goto out;
Chris Mason8f18cf12008-04-25 16:53:30 -04003442
Yan73e48b22008-01-03 14:14:39 -05003443 if (ret == 0) {
3444 leaf = path->nodes[0];
3445 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04003446 if (found_key.objectid + found_key.offset > shrink_start &&
3447 found_key.objectid < shrink_last_byte) {
Yan73e48b22008-01-03 14:14:39 -05003448 cur_byte = found_key.objectid;
3449 key.objectid = cur_byte;
3450 }
3451 }
3452 btrfs_release_path(root, path);
3453
3454 while(1) {
Chris Masonedbd8d42007-12-21 16:27:24 -05003455 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3456 if (ret < 0)
3457 goto out;
Yan73e48b22008-01-03 14:14:39 -05003458
Chris Mason7d9eb122008-07-08 14:19:17 -04003459next:
Chris Masonedbd8d42007-12-21 16:27:24 -05003460 leaf = path->nodes[0];
Chris Masonedbd8d42007-12-21 16:27:24 -05003461 nritems = btrfs_header_nritems(leaf);
Yan73e48b22008-01-03 14:14:39 -05003462 if (path->slots[0] >= nritems) {
3463 ret = btrfs_next_leaf(root, path);
3464 if (ret < 0)
3465 goto out;
3466 if (ret == 1) {
3467 ret = 0;
3468 break;
Chris Masonedbd8d42007-12-21 16:27:24 -05003469 }
Yan73e48b22008-01-03 14:14:39 -05003470 leaf = path->nodes[0];
3471 nritems = btrfs_header_nritems(leaf);
3472 }
3473
3474 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason725c8462008-01-04 16:47:16 -05003475
Chris Mason8f18cf12008-04-25 16:53:30 -04003476 if (found_key.objectid >= shrink_last_byte)
3477 break;
3478
Chris Mason725c8462008-01-04 16:47:16 -05003479 if (progress && need_resched()) {
3480 memcpy(&key, &found_key, sizeof(key));
Chris Mason725c8462008-01-04 16:47:16 -05003481 cond_resched();
Chris Mason725c8462008-01-04 16:47:16 -05003482 btrfs_release_path(root, path);
3483 btrfs_search_slot(NULL, root, &key, path, 0, 0);
3484 progress = 0;
3485 goto next;
3486 }
3487 progress = 1;
3488
Yan73e48b22008-01-03 14:14:39 -05003489 if (btrfs_key_type(&found_key) != BTRFS_EXTENT_ITEM_KEY ||
3490 found_key.objectid + found_key.offset <= cur_byte) {
Chris Mason0ef3e662008-05-24 14:04:53 -04003491 memcpy(&key, &found_key, sizeof(key));
3492 key.offset++;
Yan73e48b22008-01-03 14:14:39 -05003493 path->slots[0]++;
Chris Masonedbd8d42007-12-21 16:27:24 -05003494 goto next;
3495 }
Yan73e48b22008-01-03 14:14:39 -05003496
Chris Masonedbd8d42007-12-21 16:27:24 -05003497 total_found++;
3498 cur_byte = found_key.objectid + found_key.offset;
3499 key.objectid = cur_byte;
3500 btrfs_release_path(root, path);
3501 ret = relocate_one_extent(root, path, &found_key);
Chris Mason0ef3e662008-05-24 14:04:53 -04003502 __alloc_chunk_for_shrink(root, shrink_block_group, 0);
Chris Masonedbd8d42007-12-21 16:27:24 -05003503 }
3504
3505 btrfs_release_path(root, path);
3506
3507 if (total_found > 0) {
Chris Mason323da792008-05-09 11:46:48 -04003508 printk("btrfs relocate found %llu last extent was %llu\n",
3509 (unsigned long long)total_found,
3510 (unsigned long long)found_key.objectid);
Chris Mason7d9eb122008-07-08 14:19:17 -04003511 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05003512 trans = btrfs_start_transaction(tree_root, 1);
3513 btrfs_commit_transaction(trans, tree_root);
3514
Chris Masonedbd8d42007-12-21 16:27:24 -05003515 btrfs_clean_old_snapshots(tree_root);
Chris Masonedbd8d42007-12-21 16:27:24 -05003516
Chris Masonea8c2812008-08-04 23:17:27 -04003517 btrfs_start_delalloc_inodes(root);
Yan Zheng7ea394f2008-08-05 13:05:02 -04003518 btrfs_wait_ordered_extents(tree_root, 0);
Chris Mason3eaa2882008-07-24 11:57:52 -04003519
Chris Masonedbd8d42007-12-21 16:27:24 -05003520 trans = btrfs_start_transaction(tree_root, 1);
3521 btrfs_commit_transaction(trans, tree_root);
Chris Mason7d9eb122008-07-08 14:19:17 -04003522 mutex_lock(&root->fs_info->alloc_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05003523 goto again;
3524 }
3525
Chris Mason8f18cf12008-04-25 16:53:30 -04003526 /*
3527 * we've freed all the extents, now remove the block
3528 * group item from the tree
3529 */
Chris Mason7d9eb122008-07-08 14:19:17 -04003530 mutex_unlock(&root->fs_info->alloc_mutex);
3531
Chris Masonedbd8d42007-12-21 16:27:24 -05003532 trans = btrfs_start_transaction(root, 1);
Chris Masonc286ac42008-07-22 23:06:41 -04003533
Chris Mason7d9eb122008-07-08 14:19:17 -04003534 mutex_lock(&root->fs_info->alloc_mutex);
Chris Mason8f18cf12008-04-25 16:53:30 -04003535 memcpy(&key, &shrink_block_group->key, sizeof(key));
Chris Mason4313b392008-01-03 09:08:48 -05003536
Chris Mason8f18cf12008-04-25 16:53:30 -04003537 ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3538 if (ret > 0)
3539 ret = -EIO;
Josef Bacik8e8a1e32008-07-24 12:17:14 -04003540 if (ret < 0) {
3541 btrfs_end_transaction(trans, root);
Chris Mason8f18cf12008-04-25 16:53:30 -04003542 goto out;
Josef Bacik8e8a1e32008-07-24 12:17:14 -04003543 }
Yan73e48b22008-01-03 14:14:39 -05003544
Chris Mason0ef3e662008-05-24 14:04:53 -04003545 clear_extent_bits(&info->block_group_cache, key.objectid,
3546 key.objectid + key.offset - 1,
Chris Mason8f18cf12008-04-25 16:53:30 -04003547 (unsigned int)-1, GFP_NOFS);
Yan73e48b22008-01-03 14:14:39 -05003548
Chris Mason0ef3e662008-05-24 14:04:53 -04003549
3550 clear_extent_bits(&info->free_space_cache,
3551 key.objectid, key.objectid + key.offset - 1,
3552 (unsigned int)-1, GFP_NOFS);
3553
Chris Masond7a029a2008-08-04 23:17:26 -04003554 /*
Chris Mason0ef3e662008-05-24 14:04:53 -04003555 memset(shrink_block_group, 0, sizeof(*shrink_block_group));
3556 kfree(shrink_block_group);
Chris Masond7a029a2008-08-04 23:17:26 -04003557 */
Chris Mason0ef3e662008-05-24 14:04:53 -04003558
Chris Mason8f18cf12008-04-25 16:53:30 -04003559 btrfs_del_item(trans, root, path);
Chris Mason7d9eb122008-07-08 14:19:17 -04003560 btrfs_release_path(root, path);
3561 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05003562 btrfs_commit_transaction(trans, root);
Chris Mason0ef3e662008-05-24 14:04:53 -04003563
Chris Mason7d9eb122008-07-08 14:19:17 -04003564 mutex_lock(&root->fs_info->alloc_mutex);
3565
Chris Mason0ef3e662008-05-24 14:04:53 -04003566 /* the code to unpin extents might set a few bits in the free
3567 * space cache for this range again
3568 */
3569 clear_extent_bits(&info->free_space_cache,
3570 key.objectid, key.objectid + key.offset - 1,
3571 (unsigned int)-1, GFP_NOFS);
Chris Masonedbd8d42007-12-21 16:27:24 -05003572out:
3573 btrfs_free_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04003574 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Masonedbd8d42007-12-21 16:27:24 -05003575 return ret;
3576}
3577
Chris Mason0b86a832008-03-24 15:01:56 -04003578int find_first_block_group(struct btrfs_root *root, struct btrfs_path *path,
3579 struct btrfs_key *key)
3580{
Chris Mason925baed2008-06-25 16:01:30 -04003581 int ret = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003582 struct btrfs_key found_key;
3583 struct extent_buffer *leaf;
3584 int slot;
3585
3586 ret = btrfs_search_slot(NULL, root, key, path, 0, 0);
3587 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04003588 goto out;
3589
Chris Mason0b86a832008-03-24 15:01:56 -04003590 while(1) {
3591 slot = path->slots[0];
3592 leaf = path->nodes[0];
3593 if (slot >= btrfs_header_nritems(leaf)) {
3594 ret = btrfs_next_leaf(root, path);
3595 if (ret == 0)
3596 continue;
3597 if (ret < 0)
Chris Mason925baed2008-06-25 16:01:30 -04003598 goto out;
Chris Mason0b86a832008-03-24 15:01:56 -04003599 break;
3600 }
3601 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3602
3603 if (found_key.objectid >= key->objectid &&
Chris Mason925baed2008-06-25 16:01:30 -04003604 found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) {
3605 ret = 0;
3606 goto out;
3607 }
Chris Mason0b86a832008-03-24 15:01:56 -04003608 path->slots[0]++;
3609 }
3610 ret = -ENOENT;
Chris Mason925baed2008-06-25 16:01:30 -04003611out:
Chris Mason0b86a832008-03-24 15:01:56 -04003612 return ret;
3613}
3614
Chris Mason9078a3e2007-04-26 16:46:15 -04003615int btrfs_read_block_groups(struct btrfs_root *root)
3616{
3617 struct btrfs_path *path;
3618 int ret;
Chris Mason96b51792007-10-15 16:15:19 -04003619 int bit;
Chris Mason9078a3e2007-04-26 16:46:15 -04003620 struct btrfs_block_group_cache *cache;
Chris Masonbe744172007-05-06 10:15:01 -04003621 struct btrfs_fs_info *info = root->fs_info;
Chris Mason6324fbf2008-03-24 15:01:59 -04003622 struct btrfs_space_info *space_info;
Chris Masond1310b22008-01-24 16:13:08 -05003623 struct extent_io_tree *block_group_cache;
Chris Mason9078a3e2007-04-26 16:46:15 -04003624 struct btrfs_key key;
3625 struct btrfs_key found_key;
Chris Mason5f39d392007-10-15 16:14:19 -04003626 struct extent_buffer *leaf;
Chris Mason96b51792007-10-15 16:15:19 -04003627
3628 block_group_cache = &info->block_group_cache;
Chris Masonbe744172007-05-06 10:15:01 -04003629 root = info->extent_root;
Chris Mason9078a3e2007-04-26 16:46:15 -04003630 key.objectid = 0;
Chris Mason0b86a832008-03-24 15:01:56 -04003631 key.offset = 0;
Chris Mason9078a3e2007-04-26 16:46:15 -04003632 btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason9078a3e2007-04-26 16:46:15 -04003633 path = btrfs_alloc_path();
3634 if (!path)
3635 return -ENOMEM;
3636
Chris Mason925baed2008-06-25 16:01:30 -04003637 mutex_lock(&root->fs_info->alloc_mutex);
Chris Mason9078a3e2007-04-26 16:46:15 -04003638 while(1) {
Chris Mason0b86a832008-03-24 15:01:56 -04003639 ret = find_first_block_group(root, path, &key);
3640 if (ret > 0) {
3641 ret = 0;
3642 goto error;
Chris Mason9078a3e2007-04-26 16:46:15 -04003643 }
Chris Mason0b86a832008-03-24 15:01:56 -04003644 if (ret != 0)
3645 goto error;
3646
Chris Mason5f39d392007-10-15 16:14:19 -04003647 leaf = path->nodes[0];
3648 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
Chris Mason8f18cf12008-04-25 16:53:30 -04003649 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04003650 if (!cache) {
Chris Mason0b86a832008-03-24 15:01:56 -04003651 ret = -ENOMEM;
Chris Mason9078a3e2007-04-26 16:46:15 -04003652 break;
3653 }
Chris Mason3e1ad542007-05-07 20:03:49 -04003654
Chris Masonc286ac42008-07-22 23:06:41 -04003655 spin_lock_init(&cache->lock);
Chris Mason5f39d392007-10-15 16:14:19 -04003656 read_extent_buffer(leaf, &cache->item,
3657 btrfs_item_ptr_offset(leaf, path->slots[0]),
3658 sizeof(cache->item));
Chris Mason9078a3e2007-04-26 16:46:15 -04003659 memcpy(&cache->key, &found_key, sizeof(found_key));
Chris Mason0b86a832008-03-24 15:01:56 -04003660
Chris Mason9078a3e2007-04-26 16:46:15 -04003661 key.objectid = found_key.objectid + found_key.offset;
3662 btrfs_release_path(root, path);
Chris Mason0b86a832008-03-24 15:01:56 -04003663 cache->flags = btrfs_block_group_flags(&cache->item);
3664 bit = 0;
3665 if (cache->flags & BTRFS_BLOCK_GROUP_DATA) {
Chris Mason96b51792007-10-15 16:15:19 -04003666 bit = BLOCK_GROUP_DATA;
Chris Mason0b86a832008-03-24 15:01:56 -04003667 } else if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) {
3668 bit = BLOCK_GROUP_SYSTEM;
3669 } else if (cache->flags & BTRFS_BLOCK_GROUP_METADATA) {
Chris Mason96b51792007-10-15 16:15:19 -04003670 bit = BLOCK_GROUP_METADATA;
Chris Mason31f3c992007-04-30 15:25:45 -04003671 }
Chris Mason8790d502008-04-03 16:29:03 -04003672 set_avail_alloc_bits(info, cache->flags);
Chris Mason96b51792007-10-15 16:15:19 -04003673
Chris Mason6324fbf2008-03-24 15:01:59 -04003674 ret = update_space_info(info, cache->flags, found_key.offset,
3675 btrfs_block_group_used(&cache->item),
3676 &space_info);
3677 BUG_ON(ret);
3678 cache->space_info = space_info;
3679
Chris Mason96b51792007-10-15 16:15:19 -04003680 /* use EXTENT_LOCKED to prevent merging */
3681 set_extent_bits(block_group_cache, found_key.objectid,
3682 found_key.objectid + found_key.offset - 1,
Chris Masonc286ac42008-07-22 23:06:41 -04003683 EXTENT_LOCKED, GFP_NOFS);
Chris Mason96b51792007-10-15 16:15:19 -04003684 set_state_private(block_group_cache, found_key.objectid,
Jens Axboeae2f5412007-10-19 09:22:59 -04003685 (unsigned long)cache);
Chris Masonc286ac42008-07-22 23:06:41 -04003686 set_extent_bits(block_group_cache, found_key.objectid,
3687 found_key.objectid + found_key.offset - 1,
3688 bit | EXTENT_LOCKED, GFP_NOFS);
Chris Mason9078a3e2007-04-26 16:46:15 -04003689 if (key.objectid >=
Chris Masondb945352007-10-15 16:15:53 -04003690 btrfs_super_total_bytes(&info->super_copy))
Chris Mason9078a3e2007-04-26 16:46:15 -04003691 break;
3692 }
Chris Mason0b86a832008-03-24 15:01:56 -04003693 ret = 0;
3694error:
Chris Mason9078a3e2007-04-26 16:46:15 -04003695 btrfs_free_path(path);
Chris Mason925baed2008-06-25 16:01:30 -04003696 mutex_unlock(&root->fs_info->alloc_mutex);
Chris Mason0b86a832008-03-24 15:01:56 -04003697 return ret;
Chris Mason9078a3e2007-04-26 16:46:15 -04003698}
Chris Mason6324fbf2008-03-24 15:01:59 -04003699
3700int btrfs_make_block_group(struct btrfs_trans_handle *trans,
3701 struct btrfs_root *root, u64 bytes_used,
Chris Masone17cade2008-04-15 15:41:47 -04003702 u64 type, u64 chunk_objectid, u64 chunk_offset,
Chris Mason6324fbf2008-03-24 15:01:59 -04003703 u64 size)
3704{
3705 int ret;
3706 int bit = 0;
3707 struct btrfs_root *extent_root;
3708 struct btrfs_block_group_cache *cache;
3709 struct extent_io_tree *block_group_cache;
3710
Chris Mason7d9eb122008-07-08 14:19:17 -04003711 WARN_ON(!mutex_is_locked(&root->fs_info->alloc_mutex));
Chris Mason6324fbf2008-03-24 15:01:59 -04003712 extent_root = root->fs_info->extent_root;
3713 block_group_cache = &root->fs_info->block_group_cache;
3714
Chris Masone02119d2008-09-05 16:13:11 -04003715 root->fs_info->last_trans_new_blockgroup = trans->transid;
3716
Chris Mason8f18cf12008-04-25 16:53:30 -04003717 cache = kzalloc(sizeof(*cache), GFP_NOFS);
Chris Mason6324fbf2008-03-24 15:01:59 -04003718 BUG_ON(!cache);
Chris Masone17cade2008-04-15 15:41:47 -04003719 cache->key.objectid = chunk_offset;
Chris Mason6324fbf2008-03-24 15:01:59 -04003720 cache->key.offset = size;
Chris Masonc286ac42008-07-22 23:06:41 -04003721 spin_lock_init(&cache->lock);
Chris Mason6324fbf2008-03-24 15:01:59 -04003722 btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY);
Chris Mason0ef3e662008-05-24 14:04:53 -04003723
Chris Mason6324fbf2008-03-24 15:01:59 -04003724 btrfs_set_block_group_used(&cache->item, bytes_used);
Chris Mason6324fbf2008-03-24 15:01:59 -04003725 btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid);
3726 cache->flags = type;
3727 btrfs_set_block_group_flags(&cache->item, type);
3728
3729 ret = update_space_info(root->fs_info, cache->flags, size, bytes_used,
3730 &cache->space_info);
3731 BUG_ON(ret);
3732
Chris Masond18a2c42008-04-04 15:40:00 -04003733 bit = block_group_state_bits(type);
Chris Masone17cade2008-04-15 15:41:47 -04003734 set_extent_bits(block_group_cache, chunk_offset,
3735 chunk_offset + size - 1,
Chris Masonc286ac42008-07-22 23:06:41 -04003736 EXTENT_LOCKED, GFP_NOFS);
Chris Masone17cade2008-04-15 15:41:47 -04003737 set_state_private(block_group_cache, chunk_offset,
3738 (unsigned long)cache);
Chris Masonc286ac42008-07-22 23:06:41 -04003739 set_extent_bits(block_group_cache, chunk_offset,
3740 chunk_offset + size - 1,
3741 bit | EXTENT_LOCKED, GFP_NOFS);
3742
Chris Mason6324fbf2008-03-24 15:01:59 -04003743 ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item,
3744 sizeof(cache->item));
3745 BUG_ON(ret);
3746
3747 finish_current_insert(trans, extent_root);
3748 ret = del_pending_extents(trans, extent_root);
3749 BUG_ON(ret);
Chris Masond18a2c42008-04-04 15:40:00 -04003750 set_avail_alloc_bits(extent_root->fs_info, type);
Chris Mason925baed2008-06-25 16:01:30 -04003751
Chris Mason6324fbf2008-03-24 15:01:59 -04003752 return 0;
3753}