]>
Commit | Line | Data |
---|---|---|
6cbd5570 CM |
1 | /* |
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 | */ | |
ec6b910f | 18 | #include <linux/sched.h> |
edbd8d4e | 19 | #include <linux/pagemap.h> |
ec44a35c | 20 | #include <linux/writeback.h> |
74493f7a | 21 | #include "hash.h" |
a5eb62e3 | 22 | #include "crc32c.h" |
fec577fb CM |
23 | #include "ctree.h" |
24 | #include "disk-io.h" | |
25 | #include "print-tree.h" | |
e089f05c | 26 | #include "transaction.h" |
0b86a832 | 27 | #include "volumes.h" |
fec577fb | 28 | |
0b86a832 | 29 | #define BLOCK_GROUP_DATA EXTENT_WRITEBACK |
96b5179d | 30 | #define BLOCK_GROUP_METADATA EXTENT_UPTODATE |
0b86a832 CM |
31 | #define BLOCK_GROUP_SYSTEM EXTENT_NEW |
32 | ||
96b5179d CM |
33 | #define BLOCK_GROUP_DIRTY EXTENT_DIRTY |
34 | ||
e089f05c CM |
35 | static int finish_current_insert(struct btrfs_trans_handle *trans, struct |
36 | btrfs_root *extent_root); | |
e20d96d6 CM |
37 | static int del_pending_extents(struct btrfs_trans_handle *trans, struct |
38 | btrfs_root *extent_root); | |
d548ee51 | 39 | |
fec577fb | 40 | |
e37c9e69 CM |
41 | static int cache_block_group(struct btrfs_root *root, |
42 | struct btrfs_block_group_cache *block_group) | |
43 | { | |
44 | struct btrfs_path *path; | |
45 | int ret; | |
46 | struct btrfs_key key; | |
5f39d397 | 47 | struct extent_buffer *leaf; |
d1310b2e | 48 | struct extent_io_tree *free_space_cache; |
e37c9e69 | 49 | int slot; |
e37c9e69 CM |
50 | u64 last = 0; |
51 | u64 hole_size; | |
7d7d6068 | 52 | u64 first_free; |
e37c9e69 CM |
53 | int found = 0; |
54 | ||
00f5c795 CM |
55 | if (!block_group) |
56 | return 0; | |
57 | ||
e37c9e69 | 58 | root = root->fs_info->extent_root; |
f510cfec | 59 | free_space_cache = &root->fs_info->free_space_cache; |
e37c9e69 CM |
60 | |
61 | if (block_group->cached) | |
62 | return 0; | |
f510cfec | 63 | |
e37c9e69 CM |
64 | path = btrfs_alloc_path(); |
65 | if (!path) | |
66 | return -ENOMEM; | |
7d7d6068 | 67 | |
2cc58cf2 | 68 | path->reada = 2; |
7d7d6068 | 69 | first_free = block_group->key.objectid; |
e37c9e69 | 70 | key.objectid = block_group->key.objectid; |
e37c9e69 CM |
71 | key.offset = 0; |
72 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); | |
73 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
74 | if (ret < 0) | |
75 | return ret; | |
0b86a832 | 76 | ret = btrfs_previous_item(root, path, 0, BTRFS_EXTENT_ITEM_KEY); |
d548ee51 Y |
77 | if (ret < 0) |
78 | return ret; | |
79 | if (ret == 0) { | |
80 | leaf = path->nodes[0]; | |
81 | btrfs_item_key_to_cpu(leaf, &key, path->slots[0]); | |
82 | if (key.objectid + key.offset > first_free) | |
83 | first_free = key.objectid + key.offset; | |
84 | } | |
e37c9e69 | 85 | while(1) { |
5f39d397 | 86 | leaf = path->nodes[0]; |
e37c9e69 | 87 | slot = path->slots[0]; |
5f39d397 | 88 | if (slot >= btrfs_header_nritems(leaf)) { |
e37c9e69 | 89 | ret = btrfs_next_leaf(root, path); |
54aa1f4d CM |
90 | if (ret < 0) |
91 | goto err; | |
de428b63 | 92 | if (ret == 0) { |
e37c9e69 | 93 | continue; |
de428b63 | 94 | } else { |
e37c9e69 CM |
95 | break; |
96 | } | |
97 | } | |
5f39d397 | 98 | btrfs_item_key_to_cpu(leaf, &key, slot); |
7d7d6068 | 99 | if (key.objectid < block_group->key.objectid) { |
7d7d6068 Y |
100 | goto next; |
101 | } | |
e37c9e69 CM |
102 | if (key.objectid >= block_group->key.objectid + |
103 | block_group->key.offset) { | |
e37c9e69 CM |
104 | break; |
105 | } | |
7d7d6068 | 106 | |
e37c9e69 CM |
107 | if (btrfs_key_type(&key) == BTRFS_EXTENT_ITEM_KEY) { |
108 | if (!found) { | |
7d7d6068 | 109 | last = first_free; |
e37c9e69 | 110 | found = 1; |
e37c9e69 | 111 | } |
f510cfec CM |
112 | if (key.objectid > last) { |
113 | hole_size = key.objectid - last; | |
114 | set_extent_dirty(free_space_cache, last, | |
115 | last + hole_size - 1, | |
116 | GFP_NOFS); | |
7d7d6068 Y |
117 | } |
118 | last = key.objectid + key.offset; | |
e37c9e69 | 119 | } |
7d7d6068 | 120 | next: |
e37c9e69 CM |
121 | path->slots[0]++; |
122 | } | |
123 | ||
7d7d6068 Y |
124 | if (!found) |
125 | last = first_free; | |
126 | if (block_group->key.objectid + | |
127 | block_group->key.offset > last) { | |
128 | hole_size = block_group->key.objectid + | |
129 | block_group->key.offset - last; | |
f510cfec CM |
130 | set_extent_dirty(free_space_cache, last, |
131 | last + hole_size - 1, GFP_NOFS); | |
7d7d6068 | 132 | } |
e37c9e69 | 133 | block_group->cached = 1; |
54aa1f4d | 134 | err: |
e37c9e69 CM |
135 | btrfs_free_path(path); |
136 | return 0; | |
137 | } | |
138 | ||
5276aeda CM |
139 | struct btrfs_block_group_cache *btrfs_lookup_block_group(struct |
140 | btrfs_fs_info *info, | |
db94535d | 141 | u64 bytenr) |
be744175 | 142 | { |
d1310b2e | 143 | struct extent_io_tree *block_group_cache; |
96b5179d CM |
144 | struct btrfs_block_group_cache *block_group = NULL; |
145 | u64 ptr; | |
146 | u64 start; | |
147 | u64 end; | |
be744175 CM |
148 | int ret; |
149 | ||
a061fc8d CM |
150 | bytenr = max_t(u64, bytenr, |
151 | BTRFS_SUPER_INFO_OFFSET + BTRFS_SUPER_INFO_SIZE); | |
96b5179d CM |
152 | block_group_cache = &info->block_group_cache; |
153 | ret = find_first_extent_bit(block_group_cache, | |
db94535d | 154 | bytenr, &start, &end, |
0b86a832 CM |
155 | BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA | |
156 | BLOCK_GROUP_SYSTEM); | |
be744175 | 157 | if (ret) { |
96b5179d | 158 | return NULL; |
be744175 | 159 | } |
96b5179d CM |
160 | ret = get_state_private(block_group_cache, start, &ptr); |
161 | if (ret) | |
162 | return NULL; | |
163 | ||
ae2f5411 | 164 | block_group = (struct btrfs_block_group_cache *)(unsigned long)ptr; |
5cf66426 | 165 | if (block_group->key.objectid <= bytenr && bytenr < |
96b5179d CM |
166 | block_group->key.objectid + block_group->key.offset) |
167 | return block_group; | |
be744175 CM |
168 | return NULL; |
169 | } | |
0b86a832 CM |
170 | |
171 | static int block_group_bits(struct btrfs_block_group_cache *cache, u64 bits) | |
172 | { | |
593060d7 | 173 | return (cache->flags & bits) == bits; |
0b86a832 CM |
174 | } |
175 | ||
176 | static int noinline find_search_start(struct btrfs_root *root, | |
98ed5174 | 177 | struct btrfs_block_group_cache **cache_ret, |
0b86a832 | 178 | u64 *start_ret, int num, int data) |
e37c9e69 | 179 | { |
e37c9e69 CM |
180 | int ret; |
181 | struct btrfs_block_group_cache *cache = *cache_ret; | |
d7fc640e | 182 | struct extent_io_tree *free_space_cache; |
7d1660d4 | 183 | struct extent_state *state; |
e19caa5f | 184 | u64 last; |
f510cfec | 185 | u64 start = 0; |
257d0ce3 | 186 | u64 cache_miss = 0; |
c31f8830 | 187 | u64 total_fs_bytes; |
0b86a832 | 188 | u64 search_start = *start_ret; |
f84a8b36 | 189 | int wrapped = 0; |
e37c9e69 | 190 | |
0b86a832 | 191 | if (!cache) |
1a2b2ac7 | 192 | goto out; |
8f18cf13 | 193 | |
c31f8830 | 194 | total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy); |
d7fc640e CM |
195 | free_space_cache = &root->fs_info->free_space_cache; |
196 | ||
e37c9e69 | 197 | again: |
54aa1f4d CM |
198 | ret = cache_block_group(root, cache); |
199 | if (ret) | |
200 | goto out; | |
f84a8b36 | 201 | |
e19caa5f | 202 | last = max(search_start, cache->key.objectid); |
8f18cf13 | 203 | if (!block_group_bits(cache, data) || cache->ro) { |
0b86a832 CM |
204 | goto new_group; |
205 | } | |
e19caa5f | 206 | |
7d1660d4 CM |
207 | spin_lock_irq(&free_space_cache->lock); |
208 | state = find_first_extent_bit_state(free_space_cache, last, EXTENT_DIRTY); | |
e37c9e69 | 209 | while(1) { |
7d1660d4 | 210 | if (!state) { |
257d0ce3 CM |
211 | if (!cache_miss) |
212 | cache_miss = last; | |
7d1660d4 | 213 | spin_unlock_irq(&free_space_cache->lock); |
e19caa5f CM |
214 | goto new_group; |
215 | } | |
f510cfec | 216 | |
7d1660d4 CM |
217 | start = max(last, state->start); |
218 | last = state->end + 1; | |
257d0ce3 CM |
219 | if (last - start < num) { |
220 | if (last == cache->key.objectid + cache->key.offset) | |
221 | cache_miss = start; | |
7d1660d4 CM |
222 | do { |
223 | state = extent_state_next(state); | |
224 | } while(state && !(state->state & EXTENT_DIRTY)); | |
f510cfec | 225 | continue; |
257d0ce3 | 226 | } |
7d1660d4 | 227 | spin_unlock_irq(&free_space_cache->lock); |
8f18cf13 CM |
228 | if (cache->ro) |
229 | goto new_group; | |
0b86a832 | 230 | if (start + num > cache->key.objectid + cache->key.offset) |
e37c9e69 | 231 | goto new_group; |
c31f8830 CM |
232 | if (start + num > total_fs_bytes) |
233 | goto new_group; | |
8790d502 | 234 | if (!block_group_bits(cache, data)) { |
611f0e00 | 235 | printk("block group bits don't match %Lu %d\n", cache->flags, data); |
8790d502 | 236 | } |
0b86a832 CM |
237 | *start_ret = start; |
238 | return 0; | |
8790d502 CM |
239 | } |
240 | out: | |
1a2b2ac7 CM |
241 | cache = btrfs_lookup_block_group(root->fs_info, search_start); |
242 | if (!cache) { | |
0b86a832 | 243 | printk("Unable to find block group for %Lu\n", search_start); |
1a2b2ac7 | 244 | WARN_ON(1); |
1a2b2ac7 | 245 | } |
0b86a832 | 246 | return -ENOSPC; |
e37c9e69 CM |
247 | |
248 | new_group: | |
e19caa5f | 249 | last = cache->key.objectid + cache->key.offset; |
f84a8b36 | 250 | wrapped: |
e19caa5f | 251 | cache = btrfs_lookup_block_group(root->fs_info, last); |
c31f8830 | 252 | if (!cache || cache->key.objectid >= total_fs_bytes) { |
0e4de584 | 253 | no_cache: |
f84a8b36 CM |
254 | if (!wrapped) { |
255 | wrapped = 1; | |
256 | last = search_start; | |
f84a8b36 CM |
257 | goto wrapped; |
258 | } | |
1a2b2ac7 | 259 | goto out; |
e37c9e69 | 260 | } |
257d0ce3 CM |
261 | if (cache_miss && !cache->cached) { |
262 | cache_block_group(root, cache); | |
263 | last = cache_miss; | |
257d0ce3 CM |
264 | cache = btrfs_lookup_block_group(root->fs_info, last); |
265 | } | |
1a2b2ac7 | 266 | cache = btrfs_find_block_group(root, cache, last, data, 0); |
0e4de584 CM |
267 | if (!cache) |
268 | goto no_cache; | |
e37c9e69 | 269 | *cache_ret = cache; |
257d0ce3 | 270 | cache_miss = 0; |
e37c9e69 CM |
271 | goto again; |
272 | } | |
273 | ||
84f54cfa CM |
274 | static u64 div_factor(u64 num, int factor) |
275 | { | |
257d0ce3 CM |
276 | if (factor == 10) |
277 | return num; | |
84f54cfa CM |
278 | num *= factor; |
279 | do_div(num, 10); | |
280 | return num; | |
281 | } | |
282 | ||
6324fbf3 CM |
283 | static int block_group_state_bits(u64 flags) |
284 | { | |
285 | int bits = 0; | |
286 | if (flags & BTRFS_BLOCK_GROUP_DATA) | |
287 | bits |= BLOCK_GROUP_DATA; | |
288 | if (flags & BTRFS_BLOCK_GROUP_METADATA) | |
289 | bits |= BLOCK_GROUP_METADATA; | |
290 | if (flags & BTRFS_BLOCK_GROUP_SYSTEM) | |
291 | bits |= BLOCK_GROUP_SYSTEM; | |
292 | return bits; | |
293 | } | |
294 | ||
31f3c99b CM |
295 | struct btrfs_block_group_cache *btrfs_find_block_group(struct btrfs_root *root, |
296 | struct btrfs_block_group_cache | |
be744175 | 297 | *hint, u64 search_start, |
de428b63 | 298 | int data, int owner) |
cd1bc465 | 299 | { |
96b5179d | 300 | struct btrfs_block_group_cache *cache; |
d1310b2e | 301 | struct extent_io_tree *block_group_cache; |
31f3c99b | 302 | struct btrfs_block_group_cache *found_group = NULL; |
cd1bc465 CM |
303 | struct btrfs_fs_info *info = root->fs_info; |
304 | u64 used; | |
31f3c99b CM |
305 | u64 last = 0; |
306 | u64 hint_last; | |
96b5179d CM |
307 | u64 start; |
308 | u64 end; | |
309 | u64 free_check; | |
310 | u64 ptr; | |
c31f8830 | 311 | u64 total_fs_bytes; |
96b5179d | 312 | int bit; |
cd1bc465 | 313 | int ret; |
31f3c99b | 314 | int full_search = 0; |
bce4eae9 | 315 | int factor = 10; |
de428b63 | 316 | |
96b5179d | 317 | block_group_cache = &info->block_group_cache; |
c31f8830 | 318 | total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy); |
96b5179d | 319 | |
a236aed1 CM |
320 | if (data & BTRFS_BLOCK_GROUP_METADATA) |
321 | factor = 9; | |
be744175 | 322 | |
6324fbf3 | 323 | bit = block_group_state_bits(data); |
be744175 | 324 | |
c31f8830 | 325 | if (search_start && search_start < total_fs_bytes) { |
be744175 | 326 | struct btrfs_block_group_cache *shint; |
5276aeda | 327 | shint = btrfs_lookup_block_group(info, search_start); |
8f18cf13 | 328 | if (shint && block_group_bits(shint, data) && !shint->ro) { |
be744175 | 329 | used = btrfs_block_group_used(&shint->item); |
324ae4df Y |
330 | if (used + shint->pinned < |
331 | div_factor(shint->key.offset, factor)) { | |
be744175 CM |
332 | return shint; |
333 | } | |
334 | } | |
335 | } | |
8f18cf13 | 336 | if (hint && !hint->ro && block_group_bits(hint, data) && |
0b86a832 | 337 | hint->key.objectid < total_fs_bytes) { |
31f3c99b | 338 | used = btrfs_block_group_used(&hint->item); |
324ae4df Y |
339 | if (used + hint->pinned < |
340 | div_factor(hint->key.offset, factor)) { | |
31f3c99b CM |
341 | return hint; |
342 | } | |
e19caa5f | 343 | last = hint->key.objectid + hint->key.offset; |
31f3c99b CM |
344 | hint_last = last; |
345 | } else { | |
e37c9e69 CM |
346 | if (hint) |
347 | hint_last = max(hint->key.objectid, search_start); | |
348 | else | |
349 | hint_last = search_start; | |
350 | ||
c31f8830 CM |
351 | if (hint_last >= total_fs_bytes) |
352 | hint_last = search_start; | |
e37c9e69 | 353 | last = hint_last; |
31f3c99b | 354 | } |
31f3c99b | 355 | again: |
cd1bc465 | 356 | while(1) { |
96b5179d CM |
357 | ret = find_first_extent_bit(block_group_cache, last, |
358 | &start, &end, bit); | |
359 | if (ret) | |
cd1bc465 | 360 | break; |
96b5179d CM |
361 | |
362 | ret = get_state_private(block_group_cache, start, &ptr); | |
363 | if (ret) | |
364 | break; | |
365 | ||
ae2f5411 | 366 | cache = (struct btrfs_block_group_cache *)(unsigned long)ptr; |
96b5179d CM |
367 | last = cache->key.objectid + cache->key.offset; |
368 | used = btrfs_block_group_used(&cache->item); | |
369 | ||
c31f8830 CM |
370 | if (cache->key.objectid > total_fs_bytes) |
371 | break; | |
372 | ||
8f18cf13 | 373 | if (!cache->ro && block_group_bits(cache, data)) { |
8790d502 CM |
374 | if (full_search) |
375 | free_check = cache->key.offset; | |
376 | else | |
377 | free_check = div_factor(cache->key.offset, | |
378 | factor); | |
6324fbf3 | 379 | |
8790d502 CM |
380 | if (used + cache->pinned < free_check) { |
381 | found_group = cache; | |
382 | goto found; | |
383 | } | |
6324fbf3 | 384 | } |
de428b63 | 385 | cond_resched(); |
cd1bc465 | 386 | } |
31f3c99b | 387 | if (!full_search) { |
be744175 | 388 | last = search_start; |
31f3c99b CM |
389 | full_search = 1; |
390 | goto again; | |
391 | } | |
be744175 | 392 | found: |
31f3c99b | 393 | return found_group; |
cd1bc465 CM |
394 | } |
395 | ||
7bb86316 | 396 | static u64 hash_extent_ref(u64 root_objectid, u64 ref_generation, |
74493f7a CM |
397 | u64 owner, u64 owner_offset) |
398 | { | |
399 | u32 high_crc = ~(u32)0; | |
400 | u32 low_crc = ~(u32)0; | |
401 | __le64 lenum; | |
74493f7a | 402 | lenum = cpu_to_le64(root_objectid); |
a5eb62e3 | 403 | high_crc = btrfs_crc32c(high_crc, &lenum, sizeof(lenum)); |
7bb86316 | 404 | lenum = cpu_to_le64(ref_generation); |
a5eb62e3 | 405 | low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum)); |
21a4989d CM |
406 | if (owner >= BTRFS_FIRST_FREE_OBJECTID) { |
407 | lenum = cpu_to_le64(owner); | |
a5eb62e3 | 408 | low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum)); |
21a4989d | 409 | lenum = cpu_to_le64(owner_offset); |
a5eb62e3 | 410 | low_crc = btrfs_crc32c(low_crc, &lenum, sizeof(lenum)); |
21a4989d | 411 | } |
74493f7a CM |
412 | return ((u64)high_crc << 32) | (u64)low_crc; |
413 | } | |
414 | ||
7bb86316 CM |
415 | static int match_extent_ref(struct extent_buffer *leaf, |
416 | struct btrfs_extent_ref *disk_ref, | |
417 | struct btrfs_extent_ref *cpu_ref) | |
418 | { | |
419 | int ret; | |
420 | int len; | |
421 | ||
422 | if (cpu_ref->objectid) | |
423 | len = sizeof(*cpu_ref); | |
424 | else | |
425 | len = 2 * sizeof(u64); | |
426 | ret = memcmp_extent_buffer(leaf, cpu_ref, (unsigned long)disk_ref, | |
427 | len); | |
428 | return ret == 0; | |
429 | } | |
430 | ||
98ed5174 CM |
431 | static int noinline lookup_extent_backref(struct btrfs_trans_handle *trans, |
432 | struct btrfs_root *root, | |
433 | struct btrfs_path *path, u64 bytenr, | |
434 | u64 root_objectid, | |
435 | u64 ref_generation, u64 owner, | |
436 | u64 owner_offset, int del) | |
74493f7a CM |
437 | { |
438 | u64 hash; | |
439 | struct btrfs_key key; | |
7bb86316 | 440 | struct btrfs_key found_key; |
74493f7a | 441 | struct btrfs_extent_ref ref; |
7bb86316 CM |
442 | struct extent_buffer *leaf; |
443 | struct btrfs_extent_ref *disk_ref; | |
444 | int ret; | |
445 | int ret2; | |
446 | ||
447 | btrfs_set_stack_ref_root(&ref, root_objectid); | |
448 | btrfs_set_stack_ref_generation(&ref, ref_generation); | |
449 | btrfs_set_stack_ref_objectid(&ref, owner); | |
450 | btrfs_set_stack_ref_offset(&ref, owner_offset); | |
451 | ||
452 | hash = hash_extent_ref(root_objectid, ref_generation, owner, | |
453 | owner_offset); | |
454 | key.offset = hash; | |
455 | key.objectid = bytenr; | |
456 | key.type = BTRFS_EXTENT_REF_KEY; | |
457 | ||
458 | while (1) { | |
459 | ret = btrfs_search_slot(trans, root, &key, path, | |
460 | del ? -1 : 0, del); | |
461 | if (ret < 0) | |
462 | goto out; | |
463 | leaf = path->nodes[0]; | |
464 | if (ret != 0) { | |
465 | u32 nritems = btrfs_header_nritems(leaf); | |
466 | if (path->slots[0] >= nritems) { | |
467 | ret2 = btrfs_next_leaf(root, path); | |
468 | if (ret2) | |
469 | goto out; | |
470 | leaf = path->nodes[0]; | |
471 | } | |
472 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
473 | if (found_key.objectid != bytenr || | |
474 | found_key.type != BTRFS_EXTENT_REF_KEY) | |
475 | goto out; | |
476 | key.offset = found_key.offset; | |
477 | if (del) { | |
478 | btrfs_release_path(root, path); | |
479 | continue; | |
480 | } | |
481 | } | |
482 | disk_ref = btrfs_item_ptr(path->nodes[0], | |
483 | path->slots[0], | |
484 | struct btrfs_extent_ref); | |
485 | if (match_extent_ref(path->nodes[0], disk_ref, &ref)) { | |
486 | ret = 0; | |
487 | goto out; | |
488 | } | |
489 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
490 | key.offset = found_key.offset + 1; | |
491 | btrfs_release_path(root, path); | |
492 | } | |
493 | out: | |
494 | return ret; | |
495 | } | |
496 | ||
d8d5f3e1 CM |
497 | /* |
498 | * Back reference rules. Back refs have three main goals: | |
499 | * | |
500 | * 1) differentiate between all holders of references to an extent so that | |
501 | * when a reference is dropped we can make sure it was a valid reference | |
502 | * before freeing the extent. | |
503 | * | |
504 | * 2) Provide enough information to quickly find the holders of an extent | |
505 | * if we notice a given block is corrupted or bad. | |
506 | * | |
507 | * 3) Make it easy to migrate blocks for FS shrinking or storage pool | |
508 | * maintenance. This is actually the same as #2, but with a slightly | |
509 | * different use case. | |
510 | * | |
511 | * File extents can be referenced by: | |
512 | * | |
513 | * - multiple snapshots, subvolumes, or different generations in one subvol | |
514 | * - different files inside a single subvolume (in theory, not implemented yet) | |
515 | * - different offsets inside a file (bookend extents in file.c) | |
516 | * | |
517 | * The extent ref structure has fields for: | |
518 | * | |
519 | * - Objectid of the subvolume root | |
520 | * - Generation number of the tree holding the reference | |
521 | * - objectid of the file holding the reference | |
522 | * - offset in the file corresponding to the key holding the reference | |
523 | * | |
524 | * When a file extent is allocated the fields are filled in: | |
525 | * (root_key.objectid, trans->transid, inode objectid, offset in file) | |
526 | * | |
527 | * When a leaf is cow'd new references are added for every file extent found | |
528 | * in the leaf. It looks the same as the create case, but trans->transid | |
529 | * will be different when the block is cow'd. | |
530 | * | |
531 | * (root_key.objectid, trans->transid, inode objectid, offset in file) | |
532 | * | |
533 | * When a file extent is removed either during snapshot deletion or file | |
534 | * truncation, the corresponding back reference is found | |
535 | * by searching for: | |
536 | * | |
537 | * (btrfs_header_owner(leaf), btrfs_header_generation(leaf), | |
538 | * inode objectid, offset in file) | |
539 | * | |
540 | * Btree extents can be referenced by: | |
541 | * | |
542 | * - Different subvolumes | |
543 | * - Different generations of the same subvolume | |
544 | * | |
545 | * Storing sufficient information for a full reverse mapping of a btree | |
546 | * block would require storing the lowest key of the block in the backref, | |
547 | * and it would require updating that lowest key either before write out or | |
548 | * every time it changed. Instead, the objectid of the lowest key is stored | |
549 | * along with the level of the tree block. This provides a hint | |
550 | * about where in the btree the block can be found. Searches through the | |
551 | * btree only need to look for a pointer to that block, so they stop one | |
552 | * level higher than the level recorded in the backref. | |
553 | * | |
554 | * Some btrees do not do reference counting on their extents. These | |
555 | * include the extent tree and the tree of tree roots. Backrefs for these | |
556 | * trees always have a generation of zero. | |
557 | * | |
558 | * When a tree block is created, back references are inserted: | |
559 | * | |
f6dbff55 | 560 | * (root->root_key.objectid, trans->transid or zero, level, lowest_key_objectid) |
d8d5f3e1 CM |
561 | * |
562 | * When a tree block is cow'd in a reference counted root, | |
563 | * new back references are added for all the blocks it points to. | |
564 | * These are of the form (trans->transid will have increased since creation): | |
565 | * | |
f6dbff55 | 566 | * (root->root_key.objectid, trans->transid, level, lowest_key_objectid) |
d8d5f3e1 CM |
567 | * |
568 | * Because the lowest_key_objectid and the level are just hints | |
569 | * they are not used when backrefs are deleted. When a backref is deleted: | |
570 | * | |
571 | * if backref was for a tree root: | |
572 | * root_objectid = root->root_key.objectid | |
573 | * else | |
574 | * root_objectid = btrfs_header_owner(parent) | |
575 | * | |
576 | * (root_objectid, btrfs_header_generation(parent) or zero, 0, 0) | |
577 | * | |
578 | * Back Reference Key hashing: | |
579 | * | |
580 | * Back references have four fields, each 64 bits long. Unfortunately, | |
581 | * This is hashed into a single 64 bit number and placed into the key offset. | |
582 | * The key objectid corresponds to the first byte in the extent, and the | |
583 | * key type is set to BTRFS_EXTENT_REF_KEY | |
584 | */ | |
7bb86316 CM |
585 | int btrfs_insert_extent_backref(struct btrfs_trans_handle *trans, |
586 | struct btrfs_root *root, | |
587 | struct btrfs_path *path, u64 bytenr, | |
588 | u64 root_objectid, u64 ref_generation, | |
589 | u64 owner, u64 owner_offset) | |
590 | { | |
591 | u64 hash; | |
592 | struct btrfs_key key; | |
593 | struct btrfs_extent_ref ref; | |
594 | struct btrfs_extent_ref *disk_ref; | |
74493f7a CM |
595 | int ret; |
596 | ||
597 | btrfs_set_stack_ref_root(&ref, root_objectid); | |
7bb86316 | 598 | btrfs_set_stack_ref_generation(&ref, ref_generation); |
74493f7a CM |
599 | btrfs_set_stack_ref_objectid(&ref, owner); |
600 | btrfs_set_stack_ref_offset(&ref, owner_offset); | |
601 | ||
7bb86316 CM |
602 | hash = hash_extent_ref(root_objectid, ref_generation, owner, |
603 | owner_offset); | |
74493f7a CM |
604 | key.offset = hash; |
605 | key.objectid = bytenr; | |
606 | key.type = BTRFS_EXTENT_REF_KEY; | |
607 | ||
608 | ret = btrfs_insert_empty_item(trans, root, path, &key, sizeof(ref)); | |
609 | while (ret == -EEXIST) { | |
7bb86316 CM |
610 | disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0], |
611 | struct btrfs_extent_ref); | |
612 | if (match_extent_ref(path->nodes[0], disk_ref, &ref)) | |
613 | goto out; | |
614 | key.offset++; | |
615 | btrfs_release_path(root, path); | |
616 | ret = btrfs_insert_empty_item(trans, root, path, &key, | |
617 | sizeof(ref)); | |
74493f7a | 618 | } |
7bb86316 CM |
619 | if (ret) |
620 | goto out; | |
621 | disk_ref = btrfs_item_ptr(path->nodes[0], path->slots[0], | |
622 | struct btrfs_extent_ref); | |
623 | write_extent_buffer(path->nodes[0], &ref, (unsigned long)disk_ref, | |
624 | sizeof(ref)); | |
625 | btrfs_mark_buffer_dirty(path->nodes[0]); | |
626 | out: | |
627 | btrfs_release_path(root, path); | |
628 | return ret; | |
74493f7a CM |
629 | } |
630 | ||
b18c6685 CM |
631 | int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans, |
632 | struct btrfs_root *root, | |
74493f7a | 633 | u64 bytenr, u64 num_bytes, |
7bb86316 | 634 | u64 root_objectid, u64 ref_generation, |
74493f7a | 635 | u64 owner, u64 owner_offset) |
02217ed2 | 636 | { |
5caf2a00 | 637 | struct btrfs_path *path; |
02217ed2 | 638 | int ret; |
e2fa7227 | 639 | struct btrfs_key key; |
5f39d397 | 640 | struct extent_buffer *l; |
234b63a0 | 641 | struct btrfs_extent_item *item; |
cf27e1ee | 642 | u32 refs; |
037e6390 | 643 | |
db94535d | 644 | WARN_ON(num_bytes < root->sectorsize); |
5caf2a00 | 645 | path = btrfs_alloc_path(); |
54aa1f4d CM |
646 | if (!path) |
647 | return -ENOMEM; | |
26b8003f | 648 | |
3c12ac72 | 649 | path->reada = 1; |
db94535d | 650 | key.objectid = bytenr; |
62e2749e | 651 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
db94535d | 652 | key.offset = num_bytes; |
5caf2a00 | 653 | ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path, |
9f5fae2f | 654 | 0, 1); |
54aa1f4d CM |
655 | if (ret < 0) |
656 | return ret; | |
a429e513 | 657 | if (ret != 0) { |
a28ec197 | 658 | BUG(); |
a429e513 | 659 | } |
02217ed2 | 660 | BUG_ON(ret != 0); |
5f39d397 | 661 | l = path->nodes[0]; |
5caf2a00 | 662 | item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item); |
5f39d397 CM |
663 | refs = btrfs_extent_refs(l, item); |
664 | btrfs_set_extent_refs(l, item, refs + 1); | |
5caf2a00 | 665 | btrfs_mark_buffer_dirty(path->nodes[0]); |
a28ec197 | 666 | |
5caf2a00 | 667 | btrfs_release_path(root->fs_info->extent_root, path); |
7bb86316 | 668 | |
3c12ac72 | 669 | path->reada = 1; |
7bb86316 CM |
670 | ret = btrfs_insert_extent_backref(trans, root->fs_info->extent_root, |
671 | path, bytenr, root_objectid, | |
672 | ref_generation, owner, owner_offset); | |
673 | BUG_ON(ret); | |
9f5fae2f | 674 | finish_current_insert(trans, root->fs_info->extent_root); |
e20d96d6 | 675 | del_pending_extents(trans, root->fs_info->extent_root); |
74493f7a CM |
676 | |
677 | btrfs_free_path(path); | |
02217ed2 CM |
678 | return 0; |
679 | } | |
680 | ||
e9d0b13b CM |
681 | int btrfs_extent_post_op(struct btrfs_trans_handle *trans, |
682 | struct btrfs_root *root) | |
683 | { | |
684 | finish_current_insert(trans, root->fs_info->extent_root); | |
685 | del_pending_extents(trans, root->fs_info->extent_root); | |
686 | return 0; | |
687 | } | |
688 | ||
b18c6685 | 689 | static int lookup_extent_ref(struct btrfs_trans_handle *trans, |
db94535d CM |
690 | struct btrfs_root *root, u64 bytenr, |
691 | u64 num_bytes, u32 *refs) | |
a28ec197 | 692 | { |
5caf2a00 | 693 | struct btrfs_path *path; |
a28ec197 | 694 | int ret; |
e2fa7227 | 695 | struct btrfs_key key; |
5f39d397 | 696 | struct extent_buffer *l; |
234b63a0 | 697 | struct btrfs_extent_item *item; |
5caf2a00 | 698 | |
db94535d | 699 | WARN_ON(num_bytes < root->sectorsize); |
5caf2a00 | 700 | path = btrfs_alloc_path(); |
3c12ac72 | 701 | path->reada = 1; |
db94535d CM |
702 | key.objectid = bytenr; |
703 | key.offset = num_bytes; | |
62e2749e | 704 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
5caf2a00 | 705 | ret = btrfs_search_slot(trans, root->fs_info->extent_root, &key, path, |
9f5fae2f | 706 | 0, 0); |
54aa1f4d CM |
707 | if (ret < 0) |
708 | goto out; | |
5f39d397 CM |
709 | if (ret != 0) { |
710 | btrfs_print_leaf(root, path->nodes[0]); | |
db94535d | 711 | printk("failed to find block number %Lu\n", bytenr); |
a28ec197 | 712 | BUG(); |
5f39d397 CM |
713 | } |
714 | l = path->nodes[0]; | |
5caf2a00 | 715 | item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item); |
5f39d397 | 716 | *refs = btrfs_extent_refs(l, item); |
54aa1f4d | 717 | out: |
5caf2a00 | 718 | btrfs_free_path(path); |
a28ec197 CM |
719 | return 0; |
720 | } | |
721 | ||
be20aa9d CM |
722 | u32 btrfs_count_snapshots_in_path(struct btrfs_root *root, |
723 | struct btrfs_path *count_path, | |
a68d5933 | 724 | u64 expected_owner, |
be20aa9d CM |
725 | u64 first_extent) |
726 | { | |
727 | struct btrfs_root *extent_root = root->fs_info->extent_root; | |
728 | struct btrfs_path *path; | |
729 | u64 bytenr; | |
730 | u64 found_objectid; | |
a68d5933 | 731 | u64 found_owner; |
56b453c9 | 732 | u64 root_objectid = root->root_key.objectid; |
be20aa9d CM |
733 | u32 total_count = 0; |
734 | u32 cur_count; | |
be20aa9d CM |
735 | u32 nritems; |
736 | int ret; | |
737 | struct btrfs_key key; | |
738 | struct btrfs_key found_key; | |
739 | struct extent_buffer *l; | |
740 | struct btrfs_extent_item *item; | |
741 | struct btrfs_extent_ref *ref_item; | |
742 | int level = -1; | |
743 | ||
744 | path = btrfs_alloc_path(); | |
745 | again: | |
746 | if (level == -1) | |
747 | bytenr = first_extent; | |
748 | else | |
749 | bytenr = count_path->nodes[level]->start; | |
750 | ||
751 | cur_count = 0; | |
752 | key.objectid = bytenr; | |
753 | key.offset = 0; | |
754 | ||
755 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); | |
756 | ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); | |
757 | if (ret < 0) | |
758 | goto out; | |
759 | BUG_ON(ret == 0); | |
760 | ||
761 | l = path->nodes[0]; | |
762 | btrfs_item_key_to_cpu(l, &found_key, path->slots[0]); | |
763 | ||
764 | if (found_key.objectid != bytenr || | |
765 | found_key.type != BTRFS_EXTENT_ITEM_KEY) { | |
766 | goto out; | |
767 | } | |
768 | ||
769 | item = btrfs_item_ptr(l, path->slots[0], struct btrfs_extent_item); | |
be20aa9d | 770 | while (1) { |
bd09835d | 771 | l = path->nodes[0]; |
be20aa9d CM |
772 | nritems = btrfs_header_nritems(l); |
773 | if (path->slots[0] >= nritems) { | |
774 | ret = btrfs_next_leaf(extent_root, path); | |
775 | if (ret == 0) | |
776 | continue; | |
777 | break; | |
778 | } | |
779 | btrfs_item_key_to_cpu(l, &found_key, path->slots[0]); | |
780 | if (found_key.objectid != bytenr) | |
781 | break; | |
bd09835d | 782 | |
be20aa9d CM |
783 | if (found_key.type != BTRFS_EXTENT_REF_KEY) { |
784 | path->slots[0]++; | |
785 | continue; | |
786 | } | |
787 | ||
788 | cur_count++; | |
789 | ref_item = btrfs_item_ptr(l, path->slots[0], | |
790 | struct btrfs_extent_ref); | |
791 | found_objectid = btrfs_ref_root(l, ref_item); | |
792 | ||
56b453c9 CM |
793 | if (found_objectid != root_objectid) { |
794 | total_count = 2; | |
4313b399 | 795 | goto out; |
56b453c9 | 796 | } |
a68d5933 CM |
797 | if (level == -1) { |
798 | found_owner = btrfs_ref_objectid(l, ref_item); | |
799 | if (found_owner != expected_owner) { | |
800 | total_count = 2; | |
801 | goto out; | |
802 | } | |
803 | } | |
56b453c9 | 804 | total_count = 1; |
be20aa9d CM |
805 | path->slots[0]++; |
806 | } | |
807 | if (cur_count == 0) { | |
808 | total_count = 0; | |
809 | goto out; | |
810 | } | |
be20aa9d CM |
811 | if (level >= 0 && root->node == count_path->nodes[level]) |
812 | goto out; | |
813 | level++; | |
814 | btrfs_release_path(root, path); | |
815 | goto again; | |
816 | ||
817 | out: | |
818 | btrfs_free_path(path); | |
819 | return total_count; | |
be20aa9d | 820 | } |
c5739bba | 821 | int btrfs_inc_root_ref(struct btrfs_trans_handle *trans, |
7bb86316 | 822 | struct btrfs_root *root, u64 owner_objectid) |
c5739bba | 823 | { |
7bb86316 CM |
824 | u64 generation; |
825 | u64 key_objectid; | |
826 | u64 level; | |
827 | u32 nritems; | |
828 | struct btrfs_disk_key disk_key; | |
829 | ||
830 | level = btrfs_header_level(root->node); | |
831 | generation = trans->transid; | |
832 | nritems = btrfs_header_nritems(root->node); | |
833 | if (nritems > 0) { | |
834 | if (level == 0) | |
835 | btrfs_item_key(root->node, &disk_key, 0); | |
836 | else | |
837 | btrfs_node_key(root->node, &disk_key, 0); | |
838 | key_objectid = btrfs_disk_key_objectid(&disk_key); | |
839 | } else { | |
840 | key_objectid = 0; | |
841 | } | |
db94535d | 842 | return btrfs_inc_extent_ref(trans, root, root->node->start, |
7bb86316 | 843 | root->node->len, owner_objectid, |
f6dbff55 | 844 | generation, level, key_objectid); |
c5739bba CM |
845 | } |
846 | ||
e089f05c | 847 | int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root, |
5f39d397 | 848 | struct extent_buffer *buf) |
02217ed2 | 849 | { |
db94535d | 850 | u64 bytenr; |
5f39d397 CM |
851 | u32 nritems; |
852 | struct btrfs_key key; | |
6407bf6d | 853 | struct btrfs_file_extent_item *fi; |
02217ed2 | 854 | int i; |
db94535d | 855 | int level; |
6407bf6d | 856 | int ret; |
54aa1f4d | 857 | int faili; |
a28ec197 | 858 | |
3768f368 | 859 | if (!root->ref_cows) |
a28ec197 | 860 | return 0; |
5f39d397 | 861 | |
db94535d | 862 | level = btrfs_header_level(buf); |
5f39d397 CM |
863 | nritems = btrfs_header_nritems(buf); |
864 | for (i = 0; i < nritems; i++) { | |
db94535d CM |
865 | if (level == 0) { |
866 | u64 disk_bytenr; | |
5f39d397 CM |
867 | btrfs_item_key_to_cpu(buf, &key, i); |
868 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) | |
6407bf6d | 869 | continue; |
5f39d397 | 870 | fi = btrfs_item_ptr(buf, i, |
6407bf6d | 871 | struct btrfs_file_extent_item); |
5f39d397 | 872 | if (btrfs_file_extent_type(buf, fi) == |
236454df CM |
873 | BTRFS_FILE_EXTENT_INLINE) |
874 | continue; | |
db94535d CM |
875 | disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi); |
876 | if (disk_bytenr == 0) | |
3a686375 | 877 | continue; |
db94535d | 878 | ret = btrfs_inc_extent_ref(trans, root, disk_bytenr, |
7bb86316 CM |
879 | btrfs_file_extent_disk_num_bytes(buf, fi), |
880 | root->root_key.objectid, trans->transid, | |
881 | key.objectid, key.offset); | |
54aa1f4d CM |
882 | if (ret) { |
883 | faili = i; | |
884 | goto fail; | |
885 | } | |
6407bf6d | 886 | } else { |
db94535d | 887 | bytenr = btrfs_node_blockptr(buf, i); |
6caab489 | 888 | btrfs_node_key_to_cpu(buf, &key, i); |
db94535d | 889 | ret = btrfs_inc_extent_ref(trans, root, bytenr, |
7bb86316 CM |
890 | btrfs_level_size(root, level - 1), |
891 | root->root_key.objectid, | |
f6dbff55 CM |
892 | trans->transid, |
893 | level - 1, key.objectid); | |
54aa1f4d CM |
894 | if (ret) { |
895 | faili = i; | |
896 | goto fail; | |
897 | } | |
6407bf6d | 898 | } |
02217ed2 CM |
899 | } |
900 | return 0; | |
54aa1f4d | 901 | fail: |
ccd467d6 | 902 | WARN_ON(1); |
7bb86316 | 903 | #if 0 |
54aa1f4d | 904 | for (i =0; i < faili; i++) { |
db94535d CM |
905 | if (level == 0) { |
906 | u64 disk_bytenr; | |
5f39d397 CM |
907 | btrfs_item_key_to_cpu(buf, &key, i); |
908 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) | |
54aa1f4d | 909 | continue; |
5f39d397 | 910 | fi = btrfs_item_ptr(buf, i, |
54aa1f4d | 911 | struct btrfs_file_extent_item); |
5f39d397 | 912 | if (btrfs_file_extent_type(buf, fi) == |
54aa1f4d CM |
913 | BTRFS_FILE_EXTENT_INLINE) |
914 | continue; | |
db94535d CM |
915 | disk_bytenr = btrfs_file_extent_disk_bytenr(buf, fi); |
916 | if (disk_bytenr == 0) | |
54aa1f4d | 917 | continue; |
db94535d CM |
918 | err = btrfs_free_extent(trans, root, disk_bytenr, |
919 | btrfs_file_extent_disk_num_bytes(buf, | |
5f39d397 | 920 | fi), 0); |
54aa1f4d CM |
921 | BUG_ON(err); |
922 | } else { | |
db94535d CM |
923 | bytenr = btrfs_node_blockptr(buf, i); |
924 | err = btrfs_free_extent(trans, root, bytenr, | |
925 | btrfs_level_size(root, level - 1), 0); | |
54aa1f4d CM |
926 | BUG_ON(err); |
927 | } | |
928 | } | |
7bb86316 | 929 | #endif |
54aa1f4d | 930 | return ret; |
02217ed2 CM |
931 | } |
932 | ||
9078a3e1 CM |
933 | static int write_one_cache_group(struct btrfs_trans_handle *trans, |
934 | struct btrfs_root *root, | |
935 | struct btrfs_path *path, | |
936 | struct btrfs_block_group_cache *cache) | |
937 | { | |
938 | int ret; | |
939 | int pending_ret; | |
940 | struct btrfs_root *extent_root = root->fs_info->extent_root; | |
5f39d397 CM |
941 | unsigned long bi; |
942 | struct extent_buffer *leaf; | |
9078a3e1 | 943 | |
9078a3e1 | 944 | ret = btrfs_search_slot(trans, extent_root, &cache->key, path, 0, 1); |
54aa1f4d CM |
945 | if (ret < 0) |
946 | goto fail; | |
9078a3e1 | 947 | BUG_ON(ret); |
5f39d397 CM |
948 | |
949 | leaf = path->nodes[0]; | |
950 | bi = btrfs_item_ptr_offset(leaf, path->slots[0]); | |
951 | write_extent_buffer(leaf, &cache->item, bi, sizeof(cache->item)); | |
952 | btrfs_mark_buffer_dirty(leaf); | |
9078a3e1 | 953 | btrfs_release_path(extent_root, path); |
54aa1f4d | 954 | fail: |
9078a3e1 CM |
955 | finish_current_insert(trans, extent_root); |
956 | pending_ret = del_pending_extents(trans, extent_root); | |
957 | if (ret) | |
958 | return ret; | |
959 | if (pending_ret) | |
960 | return pending_ret; | |
961 | return 0; | |
962 | ||
963 | } | |
964 | ||
96b5179d CM |
965 | int btrfs_write_dirty_block_groups(struct btrfs_trans_handle *trans, |
966 | struct btrfs_root *root) | |
9078a3e1 | 967 | { |
d1310b2e | 968 | struct extent_io_tree *block_group_cache; |
96b5179d | 969 | struct btrfs_block_group_cache *cache; |
9078a3e1 CM |
970 | int ret; |
971 | int err = 0; | |
972 | int werr = 0; | |
9078a3e1 | 973 | struct btrfs_path *path; |
96b5179d CM |
974 | u64 last = 0; |
975 | u64 start; | |
976 | u64 end; | |
977 | u64 ptr; | |
9078a3e1 | 978 | |
96b5179d | 979 | block_group_cache = &root->fs_info->block_group_cache; |
9078a3e1 CM |
980 | path = btrfs_alloc_path(); |
981 | if (!path) | |
982 | return -ENOMEM; | |
983 | ||
984 | while(1) { | |
96b5179d CM |
985 | ret = find_first_extent_bit(block_group_cache, last, |
986 | &start, &end, BLOCK_GROUP_DIRTY); | |
987 | if (ret) | |
9078a3e1 | 988 | break; |
54aa1f4d | 989 | |
96b5179d CM |
990 | last = end + 1; |
991 | ret = get_state_private(block_group_cache, start, &ptr); | |
992 | if (ret) | |
993 | break; | |
ae2f5411 | 994 | cache = (struct btrfs_block_group_cache *)(unsigned long)ptr; |
96b5179d CM |
995 | err = write_one_cache_group(trans, root, |
996 | path, cache); | |
997 | /* | |
998 | * if we fail to write the cache group, we want | |
999 | * to keep it marked dirty in hopes that a later | |
1000 | * write will work | |
1001 | */ | |
1002 | if (err) { | |
1003 | werr = err; | |
1004 | continue; | |
9078a3e1 | 1005 | } |
96b5179d CM |
1006 | clear_extent_bits(block_group_cache, start, end, |
1007 | BLOCK_GROUP_DIRTY, GFP_NOFS); | |
9078a3e1 CM |
1008 | } |
1009 | btrfs_free_path(path); | |
1010 | return werr; | |
1011 | } | |
1012 | ||
6324fbf3 CM |
1013 | static struct btrfs_space_info *__find_space_info(struct btrfs_fs_info *info, |
1014 | u64 flags) | |
1015 | { | |
1016 | struct list_head *head = &info->space_info; | |
1017 | struct list_head *cur; | |
1018 | struct btrfs_space_info *found; | |
1019 | list_for_each(cur, head) { | |
1020 | found = list_entry(cur, struct btrfs_space_info, list); | |
1021 | if (found->flags == flags) | |
1022 | return found; | |
1023 | } | |
1024 | return NULL; | |
1025 | ||
1026 | } | |
1027 | ||
593060d7 CM |
1028 | static int update_space_info(struct btrfs_fs_info *info, u64 flags, |
1029 | u64 total_bytes, u64 bytes_used, | |
1030 | struct btrfs_space_info **space_info) | |
1031 | { | |
1032 | struct btrfs_space_info *found; | |
1033 | ||
1034 | found = __find_space_info(info, flags); | |
1035 | if (found) { | |
1036 | found->total_bytes += total_bytes; | |
1037 | found->bytes_used += bytes_used; | |
8f18cf13 | 1038 | found->full = 0; |
593060d7 CM |
1039 | WARN_ON(found->total_bytes < found->bytes_used); |
1040 | *space_info = found; | |
1041 | return 0; | |
1042 | } | |
1043 | found = kmalloc(sizeof(*found), GFP_NOFS); | |
1044 | if (!found) | |
1045 | return -ENOMEM; | |
1046 | ||
1047 | list_add(&found->list, &info->space_info); | |
1048 | found->flags = flags; | |
1049 | found->total_bytes = total_bytes; | |
1050 | found->bytes_used = bytes_used; | |
1051 | found->bytes_pinned = 0; | |
1052 | found->full = 0; | |
1053 | *space_info = found; | |
1054 | return 0; | |
1055 | } | |
1056 | ||
8790d502 CM |
1057 | static void set_avail_alloc_bits(struct btrfs_fs_info *fs_info, u64 flags) |
1058 | { | |
1059 | u64 extra_flags = flags & (BTRFS_BLOCK_GROUP_RAID0 | | |
611f0e00 | 1060 | BTRFS_BLOCK_GROUP_RAID1 | |
321aecc6 | 1061 | BTRFS_BLOCK_GROUP_RAID10 | |
611f0e00 | 1062 | BTRFS_BLOCK_GROUP_DUP); |
8790d502 CM |
1063 | if (extra_flags) { |
1064 | if (flags & BTRFS_BLOCK_GROUP_DATA) | |
1065 | fs_info->avail_data_alloc_bits |= extra_flags; | |
1066 | if (flags & BTRFS_BLOCK_GROUP_METADATA) | |
1067 | fs_info->avail_metadata_alloc_bits |= extra_flags; | |
1068 | if (flags & BTRFS_BLOCK_GROUP_SYSTEM) | |
1069 | fs_info->avail_system_alloc_bits |= extra_flags; | |
1070 | } | |
1071 | } | |
593060d7 | 1072 | |
a061fc8d | 1073 | static u64 reduce_alloc_profile(struct btrfs_root *root, u64 flags) |
ec44a35c | 1074 | { |
a061fc8d CM |
1075 | u64 num_devices = root->fs_info->fs_devices->num_devices; |
1076 | ||
1077 | if (num_devices == 1) | |
1078 | flags &= ~(BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID0); | |
1079 | if (num_devices < 4) | |
1080 | flags &= ~BTRFS_BLOCK_GROUP_RAID10; | |
1081 | ||
ec44a35c CM |
1082 | if ((flags & BTRFS_BLOCK_GROUP_DUP) && |
1083 | (flags & (BTRFS_BLOCK_GROUP_RAID1 | | |
a061fc8d | 1084 | BTRFS_BLOCK_GROUP_RAID10))) { |
ec44a35c | 1085 | flags &= ~BTRFS_BLOCK_GROUP_DUP; |
a061fc8d | 1086 | } |
ec44a35c CM |
1087 | |
1088 | if ((flags & BTRFS_BLOCK_GROUP_RAID1) && | |
a061fc8d | 1089 | (flags & BTRFS_BLOCK_GROUP_RAID10)) { |
ec44a35c | 1090 | flags &= ~BTRFS_BLOCK_GROUP_RAID1; |
a061fc8d | 1091 | } |
ec44a35c CM |
1092 | |
1093 | if ((flags & BTRFS_BLOCK_GROUP_RAID0) && | |
1094 | ((flags & BTRFS_BLOCK_GROUP_RAID1) | | |
1095 | (flags & BTRFS_BLOCK_GROUP_RAID10) | | |
1096 | (flags & BTRFS_BLOCK_GROUP_DUP))) | |
1097 | flags &= ~BTRFS_BLOCK_GROUP_RAID0; | |
1098 | return flags; | |
1099 | } | |
1100 | ||
6324fbf3 CM |
1101 | static int do_chunk_alloc(struct btrfs_trans_handle *trans, |
1102 | struct btrfs_root *extent_root, u64 alloc_bytes, | |
1103 | u64 flags) | |
1104 | { | |
1105 | struct btrfs_space_info *space_info; | |
1106 | u64 thresh; | |
1107 | u64 start; | |
1108 | u64 num_bytes; | |
1109 | int ret; | |
1110 | ||
a061fc8d | 1111 | flags = reduce_alloc_profile(extent_root, flags); |
ec44a35c | 1112 | |
6324fbf3 | 1113 | space_info = __find_space_info(extent_root->fs_info, flags); |
593060d7 CM |
1114 | if (!space_info) { |
1115 | ret = update_space_info(extent_root->fs_info, flags, | |
1116 | 0, 0, &space_info); | |
1117 | BUG_ON(ret); | |
1118 | } | |
6324fbf3 CM |
1119 | BUG_ON(!space_info); |
1120 | ||
1121 | if (space_info->full) | |
1122 | return 0; | |
1123 | ||
8790d502 | 1124 | thresh = div_factor(space_info->total_bytes, 6); |
6324fbf3 CM |
1125 | if ((space_info->bytes_used + space_info->bytes_pinned + alloc_bytes) < |
1126 | thresh) | |
1127 | return 0; | |
1128 | ||
1129 | ret = btrfs_alloc_chunk(trans, extent_root, &start, &num_bytes, flags); | |
1130 | if (ret == -ENOSPC) { | |
1131 | printk("space info full %Lu\n", flags); | |
1132 | space_info->full = 1; | |
1133 | return 0; | |
1134 | } | |
1135 | ||
1136 | BUG_ON(ret); | |
1137 | ||
1138 | ret = btrfs_make_block_group(trans, extent_root, 0, flags, | |
e17cade2 | 1139 | BTRFS_FIRST_CHUNK_TREE_OBJECTID, start, num_bytes); |
6324fbf3 | 1140 | BUG_ON(ret); |
593060d7 | 1141 | |
6324fbf3 CM |
1142 | return 0; |
1143 | } | |
1144 | ||
9078a3e1 CM |
1145 | static int update_block_group(struct btrfs_trans_handle *trans, |
1146 | struct btrfs_root *root, | |
db94535d | 1147 | u64 bytenr, u64 num_bytes, int alloc, |
0b86a832 | 1148 | int mark_free) |
9078a3e1 CM |
1149 | { |
1150 | struct btrfs_block_group_cache *cache; | |
1151 | struct btrfs_fs_info *info = root->fs_info; | |
db94535d | 1152 | u64 total = num_bytes; |
9078a3e1 | 1153 | u64 old_val; |
db94535d | 1154 | u64 byte_in_group; |
96b5179d CM |
1155 | u64 start; |
1156 | u64 end; | |
3e1ad54f | 1157 | |
9078a3e1 | 1158 | while(total) { |
db94535d | 1159 | cache = btrfs_lookup_block_group(info, bytenr); |
3e1ad54f | 1160 | if (!cache) { |
9078a3e1 | 1161 | return -1; |
cd1bc465 | 1162 | } |
db94535d CM |
1163 | byte_in_group = bytenr - cache->key.objectid; |
1164 | WARN_ON(byte_in_group > cache->key.offset); | |
96b5179d CM |
1165 | start = cache->key.objectid; |
1166 | end = start + cache->key.offset - 1; | |
1167 | set_extent_bits(&info->block_group_cache, start, end, | |
1168 | BLOCK_GROUP_DIRTY, GFP_NOFS); | |
9078a3e1 CM |
1169 | |
1170 | old_val = btrfs_block_group_used(&cache->item); | |
db94535d | 1171 | num_bytes = min(total, cache->key.offset - byte_in_group); |
cd1bc465 | 1172 | if (alloc) { |
db94535d | 1173 | old_val += num_bytes; |
6324fbf3 | 1174 | cache->space_info->bytes_used += num_bytes; |
cd1bc465 | 1175 | } else { |
db94535d | 1176 | old_val -= num_bytes; |
6324fbf3 | 1177 | cache->space_info->bytes_used -= num_bytes; |
f510cfec CM |
1178 | if (mark_free) { |
1179 | set_extent_dirty(&info->free_space_cache, | |
db94535d | 1180 | bytenr, bytenr + num_bytes - 1, |
f510cfec | 1181 | GFP_NOFS); |
e37c9e69 | 1182 | } |
cd1bc465 | 1183 | } |
9078a3e1 | 1184 | btrfs_set_block_group_used(&cache->item, old_val); |
db94535d CM |
1185 | total -= num_bytes; |
1186 | bytenr += num_bytes; | |
9078a3e1 CM |
1187 | } |
1188 | return 0; | |
1189 | } | |
6324fbf3 | 1190 | |
a061fc8d CM |
1191 | static u64 first_logical_byte(struct btrfs_root *root, u64 search_start) |
1192 | { | |
1193 | u64 start; | |
1194 | u64 end; | |
1195 | int ret; | |
1196 | ret = find_first_extent_bit(&root->fs_info->block_group_cache, | |
1197 | search_start, &start, &end, | |
1198 | BLOCK_GROUP_DATA | BLOCK_GROUP_METADATA | | |
1199 | BLOCK_GROUP_SYSTEM); | |
1200 | if (ret) | |
1201 | return 0; | |
1202 | return start; | |
1203 | } | |
1204 | ||
1205 | ||
324ae4df Y |
1206 | static int update_pinned_extents(struct btrfs_root *root, |
1207 | u64 bytenr, u64 num, int pin) | |
1208 | { | |
1209 | u64 len; | |
1210 | struct btrfs_block_group_cache *cache; | |
1211 | struct btrfs_fs_info *fs_info = root->fs_info; | |
1212 | ||
1213 | if (pin) { | |
1214 | set_extent_dirty(&fs_info->pinned_extents, | |
1215 | bytenr, bytenr + num - 1, GFP_NOFS); | |
1216 | } else { | |
1217 | clear_extent_dirty(&fs_info->pinned_extents, | |
1218 | bytenr, bytenr + num - 1, GFP_NOFS); | |
1219 | } | |
1220 | while (num > 0) { | |
1221 | cache = btrfs_lookup_block_group(fs_info, bytenr); | |
a061fc8d CM |
1222 | if (!cache) { |
1223 | u64 first = first_logical_byte(root, bytenr); | |
1224 | WARN_ON(first < bytenr); | |
1225 | len = min(first - bytenr, num); | |
1226 | } else { | |
1227 | len = min(num, cache->key.offset - | |
1228 | (bytenr - cache->key.objectid)); | |
1229 | } | |
324ae4df | 1230 | if (pin) { |
a061fc8d CM |
1231 | if (cache) { |
1232 | cache->pinned += len; | |
1233 | cache->space_info->bytes_pinned += len; | |
1234 | } | |
324ae4df Y |
1235 | fs_info->total_pinned += len; |
1236 | } else { | |
a061fc8d CM |
1237 | if (cache) { |
1238 | cache->pinned -= len; | |
1239 | cache->space_info->bytes_pinned -= len; | |
1240 | } | |
324ae4df Y |
1241 | fs_info->total_pinned -= len; |
1242 | } | |
1243 | bytenr += len; | |
1244 | num -= len; | |
1245 | } | |
1246 | return 0; | |
1247 | } | |
9078a3e1 | 1248 | |
d1310b2e | 1249 | int btrfs_copy_pinned(struct btrfs_root *root, struct extent_io_tree *copy) |
ccd467d6 | 1250 | { |
ccd467d6 | 1251 | u64 last = 0; |
1a5bc167 CM |
1252 | u64 start; |
1253 | u64 end; | |
d1310b2e | 1254 | struct extent_io_tree *pinned_extents = &root->fs_info->pinned_extents; |
ccd467d6 | 1255 | int ret; |
ccd467d6 CM |
1256 | |
1257 | while(1) { | |
1a5bc167 CM |
1258 | ret = find_first_extent_bit(pinned_extents, last, |
1259 | &start, &end, EXTENT_DIRTY); | |
1260 | if (ret) | |
ccd467d6 | 1261 | break; |
1a5bc167 CM |
1262 | set_extent_dirty(copy, start, end, GFP_NOFS); |
1263 | last = end + 1; | |
ccd467d6 CM |
1264 | } |
1265 | return 0; | |
1266 | } | |
1267 | ||
1268 | int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans, | |
1269 | struct btrfs_root *root, | |
d1310b2e | 1270 | struct extent_io_tree *unpin) |
a28ec197 | 1271 | { |
1a5bc167 CM |
1272 | u64 start; |
1273 | u64 end; | |
a28ec197 | 1274 | int ret; |
d1310b2e | 1275 | struct extent_io_tree *free_space_cache; |
f510cfec | 1276 | free_space_cache = &root->fs_info->free_space_cache; |
a28ec197 CM |
1277 | |
1278 | while(1) { | |
1a5bc167 CM |
1279 | ret = find_first_extent_bit(unpin, 0, &start, &end, |
1280 | EXTENT_DIRTY); | |
1281 | if (ret) | |
a28ec197 | 1282 | break; |
324ae4df | 1283 | update_pinned_extents(root, start, end + 1 - start, 0); |
1a5bc167 CM |
1284 | clear_extent_dirty(unpin, start, end, GFP_NOFS); |
1285 | set_extent_dirty(free_space_cache, start, end, GFP_NOFS); | |
a28ec197 CM |
1286 | } |
1287 | return 0; | |
1288 | } | |
1289 | ||
98ed5174 CM |
1290 | static int finish_current_insert(struct btrfs_trans_handle *trans, |
1291 | struct btrfs_root *extent_root) | |
037e6390 | 1292 | { |
7bb86316 CM |
1293 | u64 start; |
1294 | u64 end; | |
1295 | struct btrfs_fs_info *info = extent_root->fs_info; | |
d8d5f3e1 | 1296 | struct extent_buffer *eb; |
7bb86316 | 1297 | struct btrfs_path *path; |
e2fa7227 | 1298 | struct btrfs_key ins; |
d8d5f3e1 | 1299 | struct btrfs_disk_key first; |
234b63a0 | 1300 | struct btrfs_extent_item extent_item; |
037e6390 | 1301 | int ret; |
d8d5f3e1 | 1302 | int level; |
1a5bc167 | 1303 | int err = 0; |
037e6390 | 1304 | |
5f39d397 | 1305 | btrfs_set_stack_extent_refs(&extent_item, 1); |
62e2749e | 1306 | btrfs_set_key_type(&ins, BTRFS_EXTENT_ITEM_KEY); |
7bb86316 | 1307 | path = btrfs_alloc_path(); |
037e6390 | 1308 | |
26b8003f | 1309 | while(1) { |
1a5bc167 CM |
1310 | ret = find_first_extent_bit(&info->extent_ins, 0, &start, |
1311 | &end, EXTENT_LOCKED); | |
1312 | if (ret) | |
26b8003f CM |
1313 | break; |
1314 | ||
1a5bc167 CM |
1315 | ins.objectid = start; |
1316 | ins.offset = end + 1 - start; | |
1317 | err = btrfs_insert_item(trans, extent_root, &ins, | |
1318 | &extent_item, sizeof(extent_item)); | |
1319 | clear_extent_bits(&info->extent_ins, start, end, EXTENT_LOCKED, | |
1320 | GFP_NOFS); | |
d8d5f3e1 CM |
1321 | eb = read_tree_block(extent_root, ins.objectid, ins.offset); |
1322 | level = btrfs_header_level(eb); | |
1323 | if (level == 0) { | |
1324 | btrfs_item_key(eb, &first, 0); | |
1325 | } else { | |
1326 | btrfs_node_key(eb, &first, 0); | |
1327 | } | |
7bb86316 CM |
1328 | err = btrfs_insert_extent_backref(trans, extent_root, path, |
1329 | start, extent_root->root_key.objectid, | |
f6dbff55 CM |
1330 | 0, level, |
1331 | btrfs_disk_key_objectid(&first)); | |
7bb86316 | 1332 | BUG_ON(err); |
d8d5f3e1 | 1333 | free_extent_buffer(eb); |
037e6390 | 1334 | } |
7bb86316 | 1335 | btrfs_free_path(path); |
037e6390 CM |
1336 | return 0; |
1337 | } | |
1338 | ||
db94535d CM |
1339 | static int pin_down_bytes(struct btrfs_root *root, u64 bytenr, u32 num_bytes, |
1340 | int pending) | |
e20d96d6 | 1341 | { |
1a5bc167 | 1342 | int err = 0; |
5f39d397 | 1343 | struct extent_buffer *buf; |
8ef97622 | 1344 | |
f4b9aa8d | 1345 | if (!pending) { |
db94535d | 1346 | buf = btrfs_find_tree_block(root, bytenr, num_bytes); |
5f39d397 CM |
1347 | if (buf) { |
1348 | if (btrfs_buffer_uptodate(buf)) { | |
2c90e5d6 CM |
1349 | u64 transid = |
1350 | root->fs_info->running_transaction->transid; | |
dc17ff8f CM |
1351 | u64 header_transid = |
1352 | btrfs_header_generation(buf); | |
6bc34676 CM |
1353 | if (header_transid == transid && |
1354 | !btrfs_header_flag(buf, | |
1355 | BTRFS_HEADER_FLAG_WRITTEN)) { | |
55c69072 | 1356 | clean_tree_block(NULL, root, buf); |
5f39d397 | 1357 | free_extent_buffer(buf); |
c549228f | 1358 | return 1; |
2c90e5d6 | 1359 | } |
f4b9aa8d | 1360 | } |
5f39d397 | 1361 | free_extent_buffer(buf); |
8ef97622 | 1362 | } |
324ae4df | 1363 | update_pinned_extents(root, bytenr, num_bytes, 1); |
f4b9aa8d | 1364 | } else { |
1a5bc167 | 1365 | set_extent_bits(&root->fs_info->pending_del, |
db94535d CM |
1366 | bytenr, bytenr + num_bytes - 1, |
1367 | EXTENT_LOCKED, GFP_NOFS); | |
f4b9aa8d | 1368 | } |
be744175 | 1369 | BUG_ON(err < 0); |
e20d96d6 CM |
1370 | return 0; |
1371 | } | |
1372 | ||
fec577fb | 1373 | /* |
a28ec197 | 1374 | * remove an extent from the root, returns 0 on success |
fec577fb | 1375 | */ |
e089f05c | 1376 | static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root |
7bb86316 CM |
1377 | *root, u64 bytenr, u64 num_bytes, |
1378 | u64 root_objectid, u64 ref_generation, | |
1379 | u64 owner_objectid, u64 owner_offset, int pin, | |
e37c9e69 | 1380 | int mark_free) |
a28ec197 | 1381 | { |
5caf2a00 | 1382 | struct btrfs_path *path; |
e2fa7227 | 1383 | struct btrfs_key key; |
1261ec42 CM |
1384 | struct btrfs_fs_info *info = root->fs_info; |
1385 | struct btrfs_root *extent_root = info->extent_root; | |
5f39d397 | 1386 | struct extent_buffer *leaf; |
a28ec197 | 1387 | int ret; |
952fccac CM |
1388 | int extent_slot = 0; |
1389 | int found_extent = 0; | |
1390 | int num_to_del = 1; | |
234b63a0 | 1391 | struct btrfs_extent_item *ei; |
cf27e1ee | 1392 | u32 refs; |
037e6390 | 1393 | |
db94535d | 1394 | key.objectid = bytenr; |
62e2749e | 1395 | btrfs_set_key_type(&key, BTRFS_EXTENT_ITEM_KEY); |
db94535d | 1396 | key.offset = num_bytes; |
5caf2a00 | 1397 | path = btrfs_alloc_path(); |
54aa1f4d CM |
1398 | if (!path) |
1399 | return -ENOMEM; | |
5f26f772 | 1400 | |
3c12ac72 | 1401 | path->reada = 1; |
7bb86316 CM |
1402 | ret = lookup_extent_backref(trans, extent_root, path, |
1403 | bytenr, root_objectid, | |
1404 | ref_generation, | |
1405 | owner_objectid, owner_offset, 1); | |
1406 | if (ret == 0) { | |
952fccac CM |
1407 | struct btrfs_key found_key; |
1408 | extent_slot = path->slots[0]; | |
1409 | while(extent_slot > 0) { | |
1410 | extent_slot--; | |
1411 | btrfs_item_key_to_cpu(path->nodes[0], &found_key, | |
1412 | extent_slot); | |
1413 | if (found_key.objectid != bytenr) | |
1414 | break; | |
1415 | if (found_key.type == BTRFS_EXTENT_ITEM_KEY && | |
1416 | found_key.offset == num_bytes) { | |
1417 | found_extent = 1; | |
1418 | break; | |
1419 | } | |
1420 | if (path->slots[0] - extent_slot > 5) | |
1421 | break; | |
1422 | } | |
1423 | if (!found_extent) | |
1424 | ret = btrfs_del_item(trans, extent_root, path); | |
7bb86316 CM |
1425 | } else { |
1426 | btrfs_print_leaf(extent_root, path->nodes[0]); | |
1427 | WARN_ON(1); | |
1428 | printk("Unable to find ref byte nr %Lu root %Lu " | |
1429 | " gen %Lu owner %Lu offset %Lu\n", bytenr, | |
1430 | root_objectid, ref_generation, owner_objectid, | |
1431 | owner_offset); | |
1432 | } | |
952fccac CM |
1433 | if (!found_extent) { |
1434 | btrfs_release_path(extent_root, path); | |
1435 | ret = btrfs_search_slot(trans, extent_root, &key, path, -1, 1); | |
1436 | if (ret < 0) | |
1437 | return ret; | |
1438 | BUG_ON(ret); | |
1439 | extent_slot = path->slots[0]; | |
1440 | } | |
5f39d397 CM |
1441 | |
1442 | leaf = path->nodes[0]; | |
952fccac | 1443 | ei = btrfs_item_ptr(leaf, extent_slot, |
123abc88 | 1444 | struct btrfs_extent_item); |
5f39d397 CM |
1445 | refs = btrfs_extent_refs(leaf, ei); |
1446 | BUG_ON(refs == 0); | |
1447 | refs -= 1; | |
1448 | btrfs_set_extent_refs(leaf, ei, refs); | |
952fccac | 1449 | |
5f39d397 CM |
1450 | btrfs_mark_buffer_dirty(leaf); |
1451 | ||
952fccac CM |
1452 | if (refs == 0 && found_extent && path->slots[0] == extent_slot + 1) { |
1453 | /* if the back ref and the extent are next to each other | |
1454 | * they get deleted below in one shot | |
1455 | */ | |
1456 | path->slots[0] = extent_slot; | |
1457 | num_to_del = 2; | |
1458 | } else if (found_extent) { | |
1459 | /* otherwise delete the extent back ref */ | |
1460 | ret = btrfs_del_item(trans, extent_root, path); | |
1461 | BUG_ON(ret); | |
1462 | /* if refs are 0, we need to setup the path for deletion */ | |
1463 | if (refs == 0) { | |
1464 | btrfs_release_path(extent_root, path); | |
1465 | ret = btrfs_search_slot(trans, extent_root, &key, path, | |
1466 | -1, 1); | |
1467 | if (ret < 0) | |
1468 | return ret; | |
1469 | BUG_ON(ret); | |
1470 | } | |
1471 | } | |
1472 | ||
cf27e1ee | 1473 | if (refs == 0) { |
db94535d CM |
1474 | u64 super_used; |
1475 | u64 root_used; | |
78fae27e CM |
1476 | |
1477 | if (pin) { | |
db94535d | 1478 | ret = pin_down_bytes(root, bytenr, num_bytes, 0); |
c549228f Y |
1479 | if (ret > 0) |
1480 | mark_free = 1; | |
1481 | BUG_ON(ret < 0); | |
78fae27e CM |
1482 | } |
1483 | ||
58176a96 | 1484 | /* block accounting for super block */ |
db94535d CM |
1485 | super_used = btrfs_super_bytes_used(&info->super_copy); |
1486 | btrfs_set_super_bytes_used(&info->super_copy, | |
1487 | super_used - num_bytes); | |
58176a96 JB |
1488 | |
1489 | /* block accounting for root item */ | |
db94535d | 1490 | root_used = btrfs_root_used(&root->root_item); |
5f39d397 | 1491 | btrfs_set_root_used(&root->root_item, |
db94535d | 1492 | root_used - num_bytes); |
952fccac CM |
1493 | ret = btrfs_del_items(trans, extent_root, path, path->slots[0], |
1494 | num_to_del); | |
54aa1f4d CM |
1495 | if (ret) { |
1496 | return ret; | |
1497 | } | |
db94535d | 1498 | ret = update_block_group(trans, root, bytenr, num_bytes, 0, |
0b86a832 | 1499 | mark_free); |
9078a3e1 | 1500 | BUG_ON(ret); |
a28ec197 | 1501 | } |
5caf2a00 | 1502 | btrfs_free_path(path); |
e089f05c | 1503 | finish_current_insert(trans, extent_root); |
a28ec197 CM |
1504 | return ret; |
1505 | } | |
1506 | ||
a28ec197 CM |
1507 | /* |
1508 | * find all the blocks marked as pending in the radix tree and remove | |
1509 | * them from the extent map | |
1510 | */ | |
e089f05c CM |
1511 | static int del_pending_extents(struct btrfs_trans_handle *trans, struct |
1512 | btrfs_root *extent_root) | |
a28ec197 CM |
1513 | { |
1514 | int ret; | |
e20d96d6 | 1515 | int err = 0; |
1a5bc167 CM |
1516 | u64 start; |
1517 | u64 end; | |
d1310b2e CM |
1518 | struct extent_io_tree *pending_del; |
1519 | struct extent_io_tree *pinned_extents; | |
8ef97622 | 1520 | |
1a5bc167 CM |
1521 | pending_del = &extent_root->fs_info->pending_del; |
1522 | pinned_extents = &extent_root->fs_info->pinned_extents; | |
a28ec197 CM |
1523 | |
1524 | while(1) { | |
1a5bc167 CM |
1525 | ret = find_first_extent_bit(pending_del, 0, &start, &end, |
1526 | EXTENT_LOCKED); | |
1527 | if (ret) | |
a28ec197 | 1528 | break; |
324ae4df | 1529 | update_pinned_extents(extent_root, start, end + 1 - start, 1); |
1a5bc167 CM |
1530 | clear_extent_bits(pending_del, start, end, EXTENT_LOCKED, |
1531 | GFP_NOFS); | |
1532 | ret = __free_extent(trans, extent_root, | |
7bb86316 CM |
1533 | start, end + 1 - start, |
1534 | extent_root->root_key.objectid, | |
1535 | 0, 0, 0, 0, 0); | |
1a5bc167 CM |
1536 | if (ret) |
1537 | err = ret; | |
fec577fb | 1538 | } |
e20d96d6 | 1539 | return err; |
fec577fb CM |
1540 | } |
1541 | ||
1542 | /* | |
1543 | * remove an extent from the root, returns 0 on success | |
1544 | */ | |
e089f05c | 1545 | int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_root |
7bb86316 CM |
1546 | *root, u64 bytenr, u64 num_bytes, |
1547 | u64 root_objectid, u64 ref_generation, | |
1548 | u64 owner_objectid, u64 owner_offset, int pin) | |
fec577fb | 1549 | { |
9f5fae2f | 1550 | struct btrfs_root *extent_root = root->fs_info->extent_root; |
fec577fb CM |
1551 | int pending_ret; |
1552 | int ret; | |
a28ec197 | 1553 | |
db94535d | 1554 | WARN_ON(num_bytes < root->sectorsize); |
7bb86316 CM |
1555 | if (!root->ref_cows) |
1556 | ref_generation = 0; | |
1557 | ||
fec577fb | 1558 | if (root == extent_root) { |
db94535d | 1559 | pin_down_bytes(root, bytenr, num_bytes, 1); |
fec577fb CM |
1560 | return 0; |
1561 | } | |
7bb86316 CM |
1562 | ret = __free_extent(trans, root, bytenr, num_bytes, root_objectid, |
1563 | ref_generation, owner_objectid, owner_offset, | |
1564 | pin, pin == 0); | |
e20d96d6 | 1565 | pending_ret = del_pending_extents(trans, root->fs_info->extent_root); |
fec577fb CM |
1566 | return ret ? ret : pending_ret; |
1567 | } | |
1568 | ||
87ee04eb CM |
1569 | static u64 stripe_align(struct btrfs_root *root, u64 val) |
1570 | { | |
1571 | u64 mask = ((u64)root->stripesize - 1); | |
1572 | u64 ret = (val + mask) & ~mask; | |
1573 | return ret; | |
1574 | } | |
1575 | ||
fec577fb CM |
1576 | /* |
1577 | * walks the btree of allocated extents and find a hole of a given size. | |
1578 | * The key ins is changed to record the hole: | |
1579 | * ins->objectid == block start | |
62e2749e | 1580 | * ins->flags = BTRFS_EXTENT_ITEM_KEY |
fec577fb CM |
1581 | * ins->offset == number of blocks |
1582 | * Any available blocks before search_start are skipped. | |
1583 | */ | |
98ed5174 CM |
1584 | static int noinline find_free_extent(struct btrfs_trans_handle *trans, |
1585 | struct btrfs_root *orig_root, | |
1586 | u64 num_bytes, u64 empty_size, | |
1587 | u64 search_start, u64 search_end, | |
1588 | u64 hint_byte, struct btrfs_key *ins, | |
1589 | u64 exclude_start, u64 exclude_nr, | |
1590 | int data) | |
fec577fb | 1591 | { |
87ee04eb | 1592 | int ret; |
a061fc8d | 1593 | u64 orig_search_start; |
9f5fae2f | 1594 | struct btrfs_root * root = orig_root->fs_info->extent_root; |
f2458e1d | 1595 | struct btrfs_fs_info *info = root->fs_info; |
db94535d | 1596 | u64 total_needed = num_bytes; |
239b14b3 | 1597 | u64 *last_ptr = NULL; |
be08c1b9 | 1598 | struct btrfs_block_group_cache *block_group; |
be744175 | 1599 | int full_scan = 0; |
fbdc762b | 1600 | int wrapped = 0; |
239b14b3 | 1601 | int empty_cluster = 2 * 1024 * 1024; |
fec577fb | 1602 | |
db94535d | 1603 | WARN_ON(num_bytes < root->sectorsize); |
b1a4d965 CM |
1604 | btrfs_set_key_type(ins, BTRFS_EXTENT_ITEM_KEY); |
1605 | ||
239b14b3 CM |
1606 | if (data & BTRFS_BLOCK_GROUP_METADATA) { |
1607 | last_ptr = &root->fs_info->last_alloc; | |
8790d502 | 1608 | empty_cluster = 256 * 1024; |
239b14b3 CM |
1609 | } |
1610 | ||
1611 | if ((data & BTRFS_BLOCK_GROUP_DATA) && btrfs_test_opt(root, SSD)) { | |
1612 | last_ptr = &root->fs_info->last_data_alloc; | |
1613 | } | |
1614 | ||
1615 | if (last_ptr) { | |
1616 | if (*last_ptr) | |
1617 | hint_byte = *last_ptr; | |
1618 | else { | |
1619 | empty_size += empty_cluster; | |
1620 | } | |
1621 | } | |
1622 | ||
a061fc8d CM |
1623 | search_start = max(search_start, first_logical_byte(root, 0)); |
1624 | orig_search_start = search_start; | |
1625 | ||
7f93bf8d CM |
1626 | if (search_end == (u64)-1) |
1627 | search_end = btrfs_super_total_bytes(&info->super_copy); | |
0b86a832 | 1628 | |
db94535d CM |
1629 | if (hint_byte) { |
1630 | block_group = btrfs_lookup_block_group(info, hint_byte); | |
1a2b2ac7 CM |
1631 | if (!block_group) |
1632 | hint_byte = search_start; | |
be744175 | 1633 | block_group = btrfs_find_block_group(root, block_group, |
db94535d | 1634 | hint_byte, data, 1); |
239b14b3 CM |
1635 | if (last_ptr && *last_ptr == 0 && block_group) |
1636 | hint_byte = block_group->key.objectid; | |
be744175 CM |
1637 | } else { |
1638 | block_group = btrfs_find_block_group(root, | |
1a2b2ac7 CM |
1639 | trans->block_group, |
1640 | search_start, data, 1); | |
be744175 | 1641 | } |
239b14b3 | 1642 | search_start = max(search_start, hint_byte); |
be744175 | 1643 | |
6702ed49 | 1644 | total_needed += empty_size; |
0b86a832 | 1645 | |
be744175 | 1646 | check_failed: |
70b043f0 CM |
1647 | if (!block_group) { |
1648 | block_group = btrfs_lookup_block_group(info, search_start); | |
1649 | if (!block_group) | |
1650 | block_group = btrfs_lookup_block_group(info, | |
1651 | orig_search_start); | |
1652 | } | |
0b86a832 CM |
1653 | ret = find_search_start(root, &block_group, &search_start, |
1654 | total_needed, data); | |
239b14b3 CM |
1655 | if (ret == -ENOSPC && last_ptr && *last_ptr) { |
1656 | *last_ptr = 0; | |
1657 | block_group = btrfs_lookup_block_group(info, | |
1658 | orig_search_start); | |
1659 | search_start = orig_search_start; | |
1660 | ret = find_search_start(root, &block_group, &search_start, | |
1661 | total_needed, data); | |
1662 | } | |
1663 | if (ret == -ENOSPC) | |
1664 | goto enospc; | |
0b86a832 | 1665 | if (ret) |
d548ee51 | 1666 | goto error; |
e19caa5f | 1667 | |
239b14b3 CM |
1668 | if (last_ptr && *last_ptr && search_start != *last_ptr) { |
1669 | *last_ptr = 0; | |
1670 | if (!empty_size) { | |
1671 | empty_size += empty_cluster; | |
1672 | total_needed += empty_size; | |
1673 | } | |
1674 | block_group = btrfs_lookup_block_group(info, | |
1675 | orig_search_start); | |
1676 | search_start = orig_search_start; | |
1677 | ret = find_search_start(root, &block_group, | |
1678 | &search_start, total_needed, data); | |
1679 | if (ret == -ENOSPC) | |
1680 | goto enospc; | |
1681 | if (ret) | |
1682 | goto error; | |
1683 | } | |
1684 | ||
0b86a832 CM |
1685 | search_start = stripe_align(root, search_start); |
1686 | ins->objectid = search_start; | |
1687 | ins->offset = num_bytes; | |
e37c9e69 | 1688 | |
db94535d | 1689 | if (ins->objectid + num_bytes >= search_end) |
cf67582b | 1690 | goto enospc; |
0b86a832 CM |
1691 | |
1692 | if (ins->objectid + num_bytes > | |
1693 | block_group->key.objectid + block_group->key.offset) { | |
e19caa5f CM |
1694 | search_start = block_group->key.objectid + |
1695 | block_group->key.offset; | |
1696 | goto new_group; | |
1697 | } | |
0b86a832 | 1698 | |
1a5bc167 | 1699 | if (test_range_bit(&info->extent_ins, ins->objectid, |
db94535d CM |
1700 | ins->objectid + num_bytes -1, EXTENT_LOCKED, 0)) { |
1701 | search_start = ins->objectid + num_bytes; | |
1a5bc167 CM |
1702 | goto new_group; |
1703 | } | |
0b86a832 | 1704 | |
1a5bc167 | 1705 | if (test_range_bit(&info->pinned_extents, ins->objectid, |
db94535d CM |
1706 | ins->objectid + num_bytes -1, EXTENT_DIRTY, 0)) { |
1707 | search_start = ins->objectid + num_bytes; | |
1a5bc167 | 1708 | goto new_group; |
fec577fb | 1709 | } |
0b86a832 | 1710 | |
db94535d | 1711 | if (exclude_nr > 0 && (ins->objectid + num_bytes > exclude_start && |
f2654de4 CM |
1712 | ins->objectid < exclude_start + exclude_nr)) { |
1713 | search_start = exclude_start + exclude_nr; | |
1714 | goto new_group; | |
1715 | } | |
0b86a832 | 1716 | |
6324fbf3 | 1717 | if (!(data & BTRFS_BLOCK_GROUP_DATA)) { |
5276aeda | 1718 | block_group = btrfs_lookup_block_group(info, ins->objectid); |
26b8003f CM |
1719 | if (block_group) |
1720 | trans->block_group = block_group; | |
f2458e1d | 1721 | } |
db94535d | 1722 | ins->offset = num_bytes; |
239b14b3 CM |
1723 | if (last_ptr) { |
1724 | *last_ptr = ins->objectid + ins->offset; | |
1725 | if (*last_ptr == | |
1726 | btrfs_super_total_bytes(&root->fs_info->super_copy)) { | |
1727 | *last_ptr = 0; | |
1728 | } | |
1729 | } | |
fec577fb | 1730 | return 0; |
be744175 CM |
1731 | |
1732 | new_group: | |
db94535d | 1733 | if (search_start + num_bytes >= search_end) { |
cf67582b | 1734 | enospc: |
be744175 | 1735 | search_start = orig_search_start; |
fbdc762b CM |
1736 | if (full_scan) { |
1737 | ret = -ENOSPC; | |
1738 | goto error; | |
1739 | } | |
6702ed49 CM |
1740 | if (wrapped) { |
1741 | if (!full_scan) | |
1742 | total_needed -= empty_size; | |
fbdc762b | 1743 | full_scan = 1; |
6702ed49 | 1744 | } else |
fbdc762b | 1745 | wrapped = 1; |
be744175 | 1746 | } |
5276aeda | 1747 | block_group = btrfs_lookup_block_group(info, search_start); |
fbdc762b | 1748 | cond_resched(); |
1a2b2ac7 CM |
1749 | block_group = btrfs_find_block_group(root, block_group, |
1750 | search_start, data, 0); | |
be744175 CM |
1751 | goto check_failed; |
1752 | ||
0f70abe2 | 1753 | error: |
0f70abe2 | 1754 | return ret; |
fec577fb | 1755 | } |
ec44a35c | 1756 | |
fec577fb CM |
1757 | /* |
1758 | * finds a free extent and does all the dirty work required for allocation | |
1759 | * returns the key for the extent through ins, and a tree buffer for | |
1760 | * the first block of the extent through buf. | |
1761 | * | |
1762 | * returns 0 if everything worked, non-zero otherwise. | |
1763 | */ | |
4d775673 | 1764 | int btrfs_alloc_extent(struct btrfs_trans_handle *trans, |
7bb86316 | 1765 | struct btrfs_root *root, |
98d20f67 CM |
1766 | u64 num_bytes, u64 min_alloc_size, |
1767 | u64 root_objectid, u64 ref_generation, | |
7bb86316 CM |
1768 | u64 owner, u64 owner_offset, |
1769 | u64 empty_size, u64 hint_byte, | |
ec44a35c | 1770 | u64 search_end, struct btrfs_key *ins, u64 data) |
fec577fb CM |
1771 | { |
1772 | int ret; | |
1773 | int pending_ret; | |
c31f8830 CM |
1774 | u64 super_used; |
1775 | u64 root_used; | |
fbdc762b | 1776 | u64 search_start = 0; |
8790d502 | 1777 | u64 alloc_profile; |
47e4bb98 | 1778 | u32 sizes[2]; |
1261ec42 CM |
1779 | struct btrfs_fs_info *info = root->fs_info; |
1780 | struct btrfs_root *extent_root = info->extent_root; | |
47e4bb98 CM |
1781 | struct btrfs_extent_item *extent_item; |
1782 | struct btrfs_extent_ref *ref; | |
7bb86316 | 1783 | struct btrfs_path *path; |
47e4bb98 | 1784 | struct btrfs_key keys[2]; |
8f662a76 | 1785 | |
6324fbf3 | 1786 | if (data) { |
8790d502 CM |
1787 | alloc_profile = info->avail_data_alloc_bits & |
1788 | info->data_alloc_profile; | |
1789 | data = BTRFS_BLOCK_GROUP_DATA | alloc_profile; | |
6324fbf3 | 1790 | } else if (root == root->fs_info->chunk_root) { |
8790d502 CM |
1791 | alloc_profile = info->avail_system_alloc_bits & |
1792 | info->system_alloc_profile; | |
1793 | data = BTRFS_BLOCK_GROUP_SYSTEM | alloc_profile; | |
6324fbf3 | 1794 | } else { |
8790d502 CM |
1795 | alloc_profile = info->avail_metadata_alloc_bits & |
1796 | info->metadata_alloc_profile; | |
1797 | data = BTRFS_BLOCK_GROUP_METADATA | alloc_profile; | |
6324fbf3 | 1798 | } |
98d20f67 | 1799 | again: |
a061fc8d | 1800 | data = reduce_alloc_profile(root, data); |
3bf3d9e9 | 1801 | if (root->ref_cows) { |
593060d7 | 1802 | if (!(data & BTRFS_BLOCK_GROUP_METADATA)) { |
6324fbf3 | 1803 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, |
239b14b3 | 1804 | 2 * 1024 * 1024, |
593060d7 | 1805 | BTRFS_BLOCK_GROUP_METADATA | |
8790d502 CM |
1806 | (info->metadata_alloc_profile & |
1807 | info->avail_metadata_alloc_bits)); | |
6324fbf3 CM |
1808 | BUG_ON(ret); |
1809 | } | |
1810 | ret = do_chunk_alloc(trans, root->fs_info->extent_root, | |
8790d502 | 1811 | num_bytes + 2 * 1024 * 1024, data); |
6324fbf3 CM |
1812 | BUG_ON(ret); |
1813 | } | |
0b86a832 | 1814 | |
db94535d CM |
1815 | WARN_ON(num_bytes < root->sectorsize); |
1816 | ret = find_free_extent(trans, root, num_bytes, empty_size, | |
1817 | search_start, search_end, hint_byte, ins, | |
26b8003f CM |
1818 | trans->alloc_exclude_start, |
1819 | trans->alloc_exclude_nr, data); | |
3b951516 | 1820 | |
98d20f67 CM |
1821 | if (ret == -ENOSPC && num_bytes > min_alloc_size) { |
1822 | num_bytes = num_bytes >> 1; | |
1823 | num_bytes = max(num_bytes, min_alloc_size); | |
1824 | goto again; | |
1825 | } | |
ec44a35c CM |
1826 | if (ret) { |
1827 | printk("allocation failed flags %Lu\n", data); | |
1828 | } | |
ccd467d6 | 1829 | BUG_ON(ret); |
f2654de4 CM |
1830 | if (ret) |
1831 | return ret; | |
fec577fb | 1832 | |
58176a96 | 1833 | /* block accounting for super block */ |
db94535d CM |
1834 | super_used = btrfs_super_bytes_used(&info->super_copy); |
1835 | btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes); | |
26b8003f | 1836 | |
58176a96 | 1837 | /* block accounting for root item */ |
db94535d CM |
1838 | root_used = btrfs_root_used(&root->root_item); |
1839 | btrfs_set_root_used(&root->root_item, root_used + num_bytes); | |
58176a96 | 1840 | |
f510cfec CM |
1841 | clear_extent_dirty(&root->fs_info->free_space_cache, |
1842 | ins->objectid, ins->objectid + ins->offset - 1, | |
1843 | GFP_NOFS); | |
1844 | ||
26b8003f | 1845 | if (root == extent_root) { |
1a5bc167 CM |
1846 | set_extent_bits(&root->fs_info->extent_ins, ins->objectid, |
1847 | ins->objectid + ins->offset - 1, | |
1848 | EXTENT_LOCKED, GFP_NOFS); | |
26b8003f CM |
1849 | goto update_block; |
1850 | } | |
1851 | ||
1852 | WARN_ON(trans->alloc_exclude_nr); | |
1853 | trans->alloc_exclude_start = ins->objectid; | |
1854 | trans->alloc_exclude_nr = ins->offset; | |
037e6390 | 1855 | |
47e4bb98 CM |
1856 | memcpy(&keys[0], ins, sizeof(*ins)); |
1857 | keys[1].offset = hash_extent_ref(root_objectid, ref_generation, | |
1858 | owner, owner_offset); | |
1859 | keys[1].objectid = ins->objectid; | |
1860 | keys[1].type = BTRFS_EXTENT_REF_KEY; | |
1861 | sizes[0] = sizeof(*extent_item); | |
1862 | sizes[1] = sizeof(*ref); | |
7bb86316 CM |
1863 | |
1864 | path = btrfs_alloc_path(); | |
1865 | BUG_ON(!path); | |
47e4bb98 CM |
1866 | |
1867 | ret = btrfs_insert_empty_items(trans, extent_root, path, keys, | |
1868 | sizes, 2); | |
26b8003f | 1869 | |
ccd467d6 | 1870 | BUG_ON(ret); |
47e4bb98 CM |
1871 | extent_item = btrfs_item_ptr(path->nodes[0], path->slots[0], |
1872 | struct btrfs_extent_item); | |
1873 | btrfs_set_extent_refs(path->nodes[0], extent_item, 1); | |
1874 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1, | |
1875 | struct btrfs_extent_ref); | |
1876 | ||
1877 | btrfs_set_ref_root(path->nodes[0], ref, root_objectid); | |
1878 | btrfs_set_ref_generation(path->nodes[0], ref, ref_generation); | |
1879 | btrfs_set_ref_objectid(path->nodes[0], ref, owner); | |
1880 | btrfs_set_ref_offset(path->nodes[0], ref, owner_offset); | |
1881 | ||
1882 | btrfs_mark_buffer_dirty(path->nodes[0]); | |
1883 | ||
1884 | trans->alloc_exclude_start = 0; | |
1885 | trans->alloc_exclude_nr = 0; | |
7bb86316 | 1886 | btrfs_free_path(path); |
e089f05c | 1887 | finish_current_insert(trans, extent_root); |
e20d96d6 | 1888 | pending_ret = del_pending_extents(trans, extent_root); |
f510cfec | 1889 | |
e37c9e69 | 1890 | if (ret) { |
037e6390 | 1891 | return ret; |
e37c9e69 CM |
1892 | } |
1893 | if (pending_ret) { | |
037e6390 | 1894 | return pending_ret; |
e37c9e69 | 1895 | } |
26b8003f CM |
1896 | |
1897 | update_block: | |
0b86a832 | 1898 | ret = update_block_group(trans, root, ins->objectid, ins->offset, 1, 0); |
f5947066 CM |
1899 | if (ret) { |
1900 | printk("update block group failed for %Lu %Lu\n", | |
1901 | ins->objectid, ins->offset); | |
1902 | BUG(); | |
1903 | } | |
037e6390 | 1904 | return 0; |
fec577fb CM |
1905 | } |
1906 | ||
1907 | /* | |
1908 | * helper function to allocate a block for a given tree | |
1909 | * returns the tree buffer or NULL. | |
1910 | */ | |
5f39d397 | 1911 | struct extent_buffer *btrfs_alloc_free_block(struct btrfs_trans_handle *trans, |
db94535d | 1912 | struct btrfs_root *root, |
7bb86316 CM |
1913 | u32 blocksize, |
1914 | u64 root_objectid, u64 hint, | |
1915 | u64 empty_size) | |
1916 | { | |
1917 | u64 ref_generation; | |
1918 | ||
1919 | if (root->ref_cows) | |
1920 | ref_generation = trans->transid; | |
1921 | else | |
1922 | ref_generation = 0; | |
1923 | ||
1924 | ||
1925 | return __btrfs_alloc_free_block(trans, root, blocksize, root_objectid, | |
1926 | ref_generation, 0, 0, hint, empty_size); | |
1927 | } | |
1928 | ||
1929 | /* | |
1930 | * helper function to allocate a block for a given tree | |
1931 | * returns the tree buffer or NULL. | |
1932 | */ | |
1933 | struct extent_buffer *__btrfs_alloc_free_block(struct btrfs_trans_handle *trans, | |
1934 | struct btrfs_root *root, | |
1935 | u32 blocksize, | |
1936 | u64 root_objectid, | |
1937 | u64 ref_generation, | |
1938 | u64 first_objectid, | |
1939 | int level, | |
1940 | u64 hint, | |
5f39d397 | 1941 | u64 empty_size) |
fec577fb | 1942 | { |
e2fa7227 | 1943 | struct btrfs_key ins; |
fec577fb | 1944 | int ret; |
5f39d397 | 1945 | struct extent_buffer *buf; |
fec577fb | 1946 | |
98d20f67 | 1947 | ret = btrfs_alloc_extent(trans, root, blocksize, blocksize, |
7bb86316 | 1948 | root_objectid, ref_generation, |
f6dbff55 | 1949 | level, first_objectid, empty_size, hint, |
db94535d | 1950 | (u64)-1, &ins, 0); |
fec577fb | 1951 | if (ret) { |
54aa1f4d CM |
1952 | BUG_ON(ret > 0); |
1953 | return ERR_PTR(ret); | |
fec577fb | 1954 | } |
db94535d | 1955 | buf = btrfs_find_create_tree_block(root, ins.objectid, blocksize); |
54aa1f4d | 1956 | if (!buf) { |
7bb86316 CM |
1957 | btrfs_free_extent(trans, root, ins.objectid, blocksize, |
1958 | root->root_key.objectid, ref_generation, | |
1959 | 0, 0, 0); | |
54aa1f4d CM |
1960 | return ERR_PTR(-ENOMEM); |
1961 | } | |
55c69072 CM |
1962 | btrfs_set_header_generation(buf, trans->transid); |
1963 | clean_tree_block(trans, root, buf); | |
5f39d397 | 1964 | btrfs_set_buffer_uptodate(buf); |
55c69072 CM |
1965 | |
1966 | if (PageDirty(buf->first_page)) { | |
1967 | printk("page %lu dirty\n", buf->first_page->index); | |
1968 | WARN_ON(1); | |
1969 | } | |
1970 | ||
5f39d397 CM |
1971 | set_extent_dirty(&trans->transaction->dirty_pages, buf->start, |
1972 | buf->start + buf->len - 1, GFP_NOFS); | |
9afbb0b7 CM |
1973 | if (!btrfs_test_opt(root, SSD)) |
1974 | btrfs_set_buffer_defrag(buf); | |
d3c2fdcf | 1975 | trans->blocks_used++; |
fec577fb CM |
1976 | return buf; |
1977 | } | |
a28ec197 | 1978 | |
98ed5174 CM |
1979 | static int noinline drop_leaf_ref(struct btrfs_trans_handle *trans, |
1980 | struct btrfs_root *root, | |
1981 | struct extent_buffer *leaf) | |
6407bf6d | 1982 | { |
7bb86316 CM |
1983 | u64 leaf_owner; |
1984 | u64 leaf_generation; | |
5f39d397 | 1985 | struct btrfs_key key; |
6407bf6d CM |
1986 | struct btrfs_file_extent_item *fi; |
1987 | int i; | |
1988 | int nritems; | |
1989 | int ret; | |
1990 | ||
5f39d397 CM |
1991 | BUG_ON(!btrfs_is_leaf(leaf)); |
1992 | nritems = btrfs_header_nritems(leaf); | |
7bb86316 CM |
1993 | leaf_owner = btrfs_header_owner(leaf); |
1994 | leaf_generation = btrfs_header_generation(leaf); | |
1995 | ||
6407bf6d | 1996 | for (i = 0; i < nritems; i++) { |
db94535d | 1997 | u64 disk_bytenr; |
5f39d397 CM |
1998 | |
1999 | btrfs_item_key_to_cpu(leaf, &key, i); | |
2000 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) | |
6407bf6d CM |
2001 | continue; |
2002 | fi = btrfs_item_ptr(leaf, i, struct btrfs_file_extent_item); | |
5f39d397 CM |
2003 | if (btrfs_file_extent_type(leaf, fi) == |
2004 | BTRFS_FILE_EXTENT_INLINE) | |
236454df | 2005 | continue; |
6407bf6d CM |
2006 | /* |
2007 | * FIXME make sure to insert a trans record that | |
2008 | * repeats the snapshot del on crash | |
2009 | */ | |
db94535d CM |
2010 | disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi); |
2011 | if (disk_bytenr == 0) | |
3a686375 | 2012 | continue; |
db94535d | 2013 | ret = btrfs_free_extent(trans, root, disk_bytenr, |
7bb86316 CM |
2014 | btrfs_file_extent_disk_num_bytes(leaf, fi), |
2015 | leaf_owner, leaf_generation, | |
2016 | key.objectid, key.offset, 0); | |
6407bf6d CM |
2017 | BUG_ON(ret); |
2018 | } | |
2019 | return 0; | |
2020 | } | |
2021 | ||
98ed5174 | 2022 | static void noinline reada_walk_down(struct btrfs_root *root, |
bea495e5 CM |
2023 | struct extent_buffer *node, |
2024 | int slot) | |
e011599b | 2025 | { |
db94535d | 2026 | u64 bytenr; |
bea495e5 CM |
2027 | u64 last = 0; |
2028 | u32 nritems; | |
e011599b | 2029 | u32 refs; |
db94535d | 2030 | u32 blocksize; |
bea495e5 CM |
2031 | int ret; |
2032 | int i; | |
2033 | int level; | |
2034 | int skipped = 0; | |
e011599b | 2035 | |
5f39d397 | 2036 | nritems = btrfs_header_nritems(node); |
db94535d | 2037 | level = btrfs_header_level(node); |
bea495e5 CM |
2038 | if (level) |
2039 | return; | |
2040 | ||
2041 | for (i = slot; i < nritems && skipped < 32; i++) { | |
db94535d | 2042 | bytenr = btrfs_node_blockptr(node, i); |
bea495e5 CM |
2043 | if (last && ((bytenr > last && bytenr - last > 32 * 1024) || |
2044 | (last > bytenr && last - bytenr > 32 * 1024))) { | |
2045 | skipped++; | |
e011599b | 2046 | continue; |
bea495e5 CM |
2047 | } |
2048 | blocksize = btrfs_level_size(root, level - 1); | |
2049 | if (i != slot) { | |
2050 | ret = lookup_extent_ref(NULL, root, bytenr, | |
2051 | blocksize, &refs); | |
2052 | BUG_ON(ret); | |
2053 | if (refs != 1) { | |
2054 | skipped++; | |
2055 | continue; | |
2056 | } | |
2057 | } | |
409eb95d | 2058 | mutex_unlock(&root->fs_info->fs_mutex); |
db94535d | 2059 | ret = readahead_tree_block(root, bytenr, blocksize); |
bea495e5 | 2060 | last = bytenr + blocksize; |
409eb95d CM |
2061 | cond_resched(); |
2062 | mutex_lock(&root->fs_info->fs_mutex); | |
e011599b CM |
2063 | if (ret) |
2064 | break; | |
2065 | } | |
2066 | } | |
2067 | ||
9aca1d51 CM |
2068 | /* |
2069 | * helper function for drop_snapshot, this walks down the tree dropping ref | |
2070 | * counts as it goes. | |
2071 | */ | |
98ed5174 CM |
2072 | static int noinline walk_down_tree(struct btrfs_trans_handle *trans, |
2073 | struct btrfs_root *root, | |
2074 | struct btrfs_path *path, int *level) | |
20524f02 | 2075 | { |
7bb86316 CM |
2076 | u64 root_owner; |
2077 | u64 root_gen; | |
2078 | u64 bytenr; | |
5f39d397 CM |
2079 | struct extent_buffer *next; |
2080 | struct extent_buffer *cur; | |
7bb86316 | 2081 | struct extent_buffer *parent; |
db94535d | 2082 | u32 blocksize; |
20524f02 CM |
2083 | int ret; |
2084 | u32 refs; | |
2085 | ||
5caf2a00 CM |
2086 | WARN_ON(*level < 0); |
2087 | WARN_ON(*level >= BTRFS_MAX_LEVEL); | |
5f39d397 | 2088 | ret = lookup_extent_ref(trans, root, |
db94535d CM |
2089 | path->nodes[*level]->start, |
2090 | path->nodes[*level]->len, &refs); | |
20524f02 CM |
2091 | BUG_ON(ret); |
2092 | if (refs > 1) | |
2093 | goto out; | |
e011599b | 2094 | |
9aca1d51 CM |
2095 | /* |
2096 | * walk down to the last node level and free all the leaves | |
2097 | */ | |
6407bf6d | 2098 | while(*level >= 0) { |
5caf2a00 CM |
2099 | WARN_ON(*level < 0); |
2100 | WARN_ON(*level >= BTRFS_MAX_LEVEL); | |
20524f02 | 2101 | cur = path->nodes[*level]; |
e011599b | 2102 | |
5f39d397 | 2103 | if (btrfs_header_level(cur) != *level) |
2c90e5d6 | 2104 | WARN_ON(1); |
e011599b | 2105 | |
7518a238 | 2106 | if (path->slots[*level] >= |
5f39d397 | 2107 | btrfs_header_nritems(cur)) |
20524f02 | 2108 | break; |
6407bf6d CM |
2109 | if (*level == 0) { |
2110 | ret = drop_leaf_ref(trans, root, cur); | |
2111 | BUG_ON(ret); | |
2112 | break; | |
2113 | } | |
db94535d CM |
2114 | bytenr = btrfs_node_blockptr(cur, path->slots[*level]); |
2115 | blocksize = btrfs_level_size(root, *level - 1); | |
2116 | ret = lookup_extent_ref(trans, root, bytenr, blocksize, &refs); | |
6407bf6d CM |
2117 | BUG_ON(ret); |
2118 | if (refs != 1) { | |
7bb86316 CM |
2119 | parent = path->nodes[*level]; |
2120 | root_owner = btrfs_header_owner(parent); | |
2121 | root_gen = btrfs_header_generation(parent); | |
20524f02 | 2122 | path->slots[*level]++; |
db94535d | 2123 | ret = btrfs_free_extent(trans, root, bytenr, |
7bb86316 CM |
2124 | blocksize, root_owner, |
2125 | root_gen, 0, 0, 1); | |
20524f02 CM |
2126 | BUG_ON(ret); |
2127 | continue; | |
2128 | } | |
db94535d | 2129 | next = btrfs_find_tree_block(root, bytenr, blocksize); |
5f39d397 CM |
2130 | if (!next || !btrfs_buffer_uptodate(next)) { |
2131 | free_extent_buffer(next); | |
bea495e5 | 2132 | reada_walk_down(root, cur, path->slots[*level]); |
8790d502 CM |
2133 | |
2134 | mutex_unlock(&root->fs_info->fs_mutex); | |
db94535d | 2135 | next = read_tree_block(root, bytenr, blocksize); |
8790d502 | 2136 | mutex_lock(&root->fs_info->fs_mutex); |
e9d0b13b | 2137 | |
8790d502 | 2138 | /* we've dropped the lock, double check */ |
db94535d CM |
2139 | ret = lookup_extent_ref(trans, root, bytenr, |
2140 | blocksize, &refs); | |
e9d0b13b CM |
2141 | BUG_ON(ret); |
2142 | if (refs != 1) { | |
7bb86316 CM |
2143 | parent = path->nodes[*level]; |
2144 | root_owner = btrfs_header_owner(parent); | |
2145 | root_gen = btrfs_header_generation(parent); | |
2146 | ||
e9d0b13b | 2147 | path->slots[*level]++; |
5f39d397 | 2148 | free_extent_buffer(next); |
7bb86316 CM |
2149 | ret = btrfs_free_extent(trans, root, bytenr, |
2150 | blocksize, | |
2151 | root_owner, | |
2152 | root_gen, 0, 0, 1); | |
e9d0b13b CM |
2153 | BUG_ON(ret); |
2154 | continue; | |
2155 | } | |
0999df54 CM |
2156 | } else if (next) { |
2157 | btrfs_verify_block_csum(root, next); | |
e9d0b13b | 2158 | } |
5caf2a00 | 2159 | WARN_ON(*level <= 0); |
83e15a28 | 2160 | if (path->nodes[*level-1]) |
5f39d397 | 2161 | free_extent_buffer(path->nodes[*level-1]); |
20524f02 | 2162 | path->nodes[*level-1] = next; |
5f39d397 | 2163 | *level = btrfs_header_level(next); |
20524f02 CM |
2164 | path->slots[*level] = 0; |
2165 | } | |
2166 | out: | |
5caf2a00 CM |
2167 | WARN_ON(*level < 0); |
2168 | WARN_ON(*level >= BTRFS_MAX_LEVEL); | |
7bb86316 CM |
2169 | |
2170 | if (path->nodes[*level] == root->node) { | |
2171 | root_owner = root->root_key.objectid; | |
2172 | parent = path->nodes[*level]; | |
2173 | } else { | |
2174 | parent = path->nodes[*level + 1]; | |
2175 | root_owner = btrfs_header_owner(parent); | |
2176 | } | |
2177 | ||
2178 | root_gen = btrfs_header_generation(parent); | |
db94535d | 2179 | ret = btrfs_free_extent(trans, root, path->nodes[*level]->start, |
7bb86316 CM |
2180 | path->nodes[*level]->len, |
2181 | root_owner, root_gen, 0, 0, 1); | |
5f39d397 | 2182 | free_extent_buffer(path->nodes[*level]); |
20524f02 CM |
2183 | path->nodes[*level] = NULL; |
2184 | *level += 1; | |
2185 | BUG_ON(ret); | |
2186 | return 0; | |
2187 | } | |
2188 | ||
9aca1d51 CM |
2189 | /* |
2190 | * helper for dropping snapshots. This walks back up the tree in the path | |
2191 | * to find the first node higher up where we haven't yet gone through | |
2192 | * all the slots | |
2193 | */ | |
98ed5174 CM |
2194 | static int noinline walk_up_tree(struct btrfs_trans_handle *trans, |
2195 | struct btrfs_root *root, | |
2196 | struct btrfs_path *path, int *level) | |
20524f02 | 2197 | { |
7bb86316 CM |
2198 | u64 root_owner; |
2199 | u64 root_gen; | |
2200 | struct btrfs_root_item *root_item = &root->root_item; | |
20524f02 CM |
2201 | int i; |
2202 | int slot; | |
2203 | int ret; | |
9f3a7427 | 2204 | |
234b63a0 | 2205 | for(i = *level; i < BTRFS_MAX_LEVEL - 1 && path->nodes[i]; i++) { |
20524f02 | 2206 | slot = path->slots[i]; |
5f39d397 CM |
2207 | if (slot < btrfs_header_nritems(path->nodes[i]) - 1) { |
2208 | struct extent_buffer *node; | |
2209 | struct btrfs_disk_key disk_key; | |
2210 | node = path->nodes[i]; | |
20524f02 CM |
2211 | path->slots[i]++; |
2212 | *level = i; | |
9f3a7427 | 2213 | WARN_ON(*level == 0); |
5f39d397 | 2214 | btrfs_node_key(node, &disk_key, path->slots[i]); |
9f3a7427 | 2215 | memcpy(&root_item->drop_progress, |
5f39d397 | 2216 | &disk_key, sizeof(disk_key)); |
9f3a7427 | 2217 | root_item->drop_level = i; |
20524f02 CM |
2218 | return 0; |
2219 | } else { | |
7bb86316 CM |
2220 | if (path->nodes[*level] == root->node) { |
2221 | root_owner = root->root_key.objectid; | |
2222 | root_gen = | |
2223 | btrfs_header_generation(path->nodes[*level]); | |
2224 | } else { | |
2225 | struct extent_buffer *node; | |
2226 | node = path->nodes[*level + 1]; | |
2227 | root_owner = btrfs_header_owner(node); | |
2228 | root_gen = btrfs_header_generation(node); | |
2229 | } | |
e089f05c | 2230 | ret = btrfs_free_extent(trans, root, |
db94535d | 2231 | path->nodes[*level]->start, |
7bb86316 CM |
2232 | path->nodes[*level]->len, |
2233 | root_owner, root_gen, 0, 0, 1); | |
6407bf6d | 2234 | BUG_ON(ret); |
5f39d397 | 2235 | free_extent_buffer(path->nodes[*level]); |
83e15a28 | 2236 | path->nodes[*level] = NULL; |
20524f02 | 2237 | *level = i + 1; |
20524f02 CM |
2238 | } |
2239 | } | |
2240 | return 1; | |
2241 | } | |
2242 | ||
9aca1d51 CM |
2243 | /* |
2244 | * drop the reference count on the tree rooted at 'snap'. This traverses | |
2245 | * the tree freeing any blocks that have a ref count of zero after being | |
2246 | * decremented. | |
2247 | */ | |
e089f05c | 2248 | int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root |
9f3a7427 | 2249 | *root) |
20524f02 | 2250 | { |
3768f368 | 2251 | int ret = 0; |
9aca1d51 | 2252 | int wret; |
20524f02 | 2253 | int level; |
5caf2a00 | 2254 | struct btrfs_path *path; |
20524f02 CM |
2255 | int i; |
2256 | int orig_level; | |
9f3a7427 | 2257 | struct btrfs_root_item *root_item = &root->root_item; |
20524f02 | 2258 | |
5caf2a00 CM |
2259 | path = btrfs_alloc_path(); |
2260 | BUG_ON(!path); | |
20524f02 | 2261 | |
5f39d397 | 2262 | level = btrfs_header_level(root->node); |
20524f02 | 2263 | orig_level = level; |
9f3a7427 CM |
2264 | if (btrfs_disk_key_objectid(&root_item->drop_progress) == 0) { |
2265 | path->nodes[level] = root->node; | |
f510cfec | 2266 | extent_buffer_get(root->node); |
9f3a7427 CM |
2267 | path->slots[level] = 0; |
2268 | } else { | |
2269 | struct btrfs_key key; | |
5f39d397 CM |
2270 | struct btrfs_disk_key found_key; |
2271 | struct extent_buffer *node; | |
6702ed49 | 2272 | |
9f3a7427 | 2273 | btrfs_disk_key_to_cpu(&key, &root_item->drop_progress); |
6702ed49 CM |
2274 | level = root_item->drop_level; |
2275 | path->lowest_level = level; | |
9f3a7427 | 2276 | wret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
6702ed49 | 2277 | if (wret < 0) { |
9f3a7427 CM |
2278 | ret = wret; |
2279 | goto out; | |
2280 | } | |
5f39d397 CM |
2281 | node = path->nodes[level]; |
2282 | btrfs_node_key(node, &found_key, path->slots[level]); | |
2283 | WARN_ON(memcmp(&found_key, &root_item->drop_progress, | |
2284 | sizeof(found_key))); | |
9f3a7427 | 2285 | } |
20524f02 | 2286 | while(1) { |
5caf2a00 | 2287 | wret = walk_down_tree(trans, root, path, &level); |
9aca1d51 | 2288 | if (wret > 0) |
20524f02 | 2289 | break; |
9aca1d51 CM |
2290 | if (wret < 0) |
2291 | ret = wret; | |
2292 | ||
5caf2a00 | 2293 | wret = walk_up_tree(trans, root, path, &level); |
9aca1d51 | 2294 | if (wret > 0) |
20524f02 | 2295 | break; |
9aca1d51 CM |
2296 | if (wret < 0) |
2297 | ret = wret; | |
409eb95d | 2298 | ret = -EAGAIN; |
409eb95d | 2299 | break; |
20524f02 | 2300 | } |
83e15a28 | 2301 | for (i = 0; i <= orig_level; i++) { |
5caf2a00 | 2302 | if (path->nodes[i]) { |
5f39d397 | 2303 | free_extent_buffer(path->nodes[i]); |
0f82731f | 2304 | path->nodes[i] = NULL; |
83e15a28 | 2305 | } |
20524f02 | 2306 | } |
9f3a7427 | 2307 | out: |
5caf2a00 | 2308 | btrfs_free_path(path); |
9aca1d51 | 2309 | return ret; |
20524f02 | 2310 | } |
9078a3e1 | 2311 | |
96b5179d | 2312 | int btrfs_free_block_groups(struct btrfs_fs_info *info) |
9078a3e1 | 2313 | { |
96b5179d CM |
2314 | u64 start; |
2315 | u64 end; | |
b97f9203 | 2316 | u64 ptr; |
9078a3e1 | 2317 | int ret; |
9078a3e1 | 2318 | while(1) { |
96b5179d CM |
2319 | ret = find_first_extent_bit(&info->block_group_cache, 0, |
2320 | &start, &end, (unsigned int)-1); | |
2321 | if (ret) | |
9078a3e1 | 2322 | break; |
b97f9203 Y |
2323 | ret = get_state_private(&info->block_group_cache, start, &ptr); |
2324 | if (!ret) | |
2325 | kfree((void *)(unsigned long)ptr); | |
96b5179d CM |
2326 | clear_extent_bits(&info->block_group_cache, start, |
2327 | end, (unsigned int)-1, GFP_NOFS); | |
9078a3e1 | 2328 | } |
e37c9e69 | 2329 | while(1) { |
f510cfec CM |
2330 | ret = find_first_extent_bit(&info->free_space_cache, 0, |
2331 | &start, &end, EXTENT_DIRTY); | |
2332 | if (ret) | |
e37c9e69 | 2333 | break; |
f510cfec CM |
2334 | clear_extent_dirty(&info->free_space_cache, start, |
2335 | end, GFP_NOFS); | |
e37c9e69 | 2336 | } |
be744175 CM |
2337 | return 0; |
2338 | } | |
2339 | ||
8e7bf94f CM |
2340 | static unsigned long calc_ra(unsigned long start, unsigned long last, |
2341 | unsigned long nr) | |
2342 | { | |
2343 | return min(last, start + nr - 1); | |
2344 | } | |
2345 | ||
98ed5174 CM |
2346 | static int noinline relocate_inode_pages(struct inode *inode, u64 start, |
2347 | u64 len) | |
edbd8d4e CM |
2348 | { |
2349 | u64 page_start; | |
2350 | u64 page_end; | |
edbd8d4e | 2351 | unsigned long last_index; |
edbd8d4e CM |
2352 | unsigned long i; |
2353 | struct page *page; | |
d1310b2e | 2354 | struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; |
4313b399 | 2355 | struct file_ra_state *ra; |
8e7bf94f CM |
2356 | unsigned long total_read = 0; |
2357 | unsigned long ra_pages; | |
a061fc8d | 2358 | struct btrfs_trans_handle *trans; |
4313b399 CM |
2359 | |
2360 | ra = kzalloc(sizeof(*ra), GFP_NOFS); | |
edbd8d4e CM |
2361 | |
2362 | mutex_lock(&inode->i_mutex); | |
4313b399 | 2363 | i = start >> PAGE_CACHE_SHIFT; |
edbd8d4e CM |
2364 | last_index = (start + len - 1) >> PAGE_CACHE_SHIFT; |
2365 | ||
8e7bf94f CM |
2366 | ra_pages = BTRFS_I(inode)->root->fs_info->bdi.ra_pages; |
2367 | ||
4313b399 | 2368 | file_ra_state_init(ra, inode->i_mapping); |
edbd8d4e | 2369 | |
4313b399 | 2370 | for (; i <= last_index; i++) { |
8e7bf94f CM |
2371 | if (total_read % ra_pages == 0) { |
2372 | btrfs_force_ra(inode->i_mapping, ra, NULL, i, | |
2373 | calc_ra(i, last_index, ra_pages)); | |
2374 | } | |
2375 | total_read++; | |
a061fc8d CM |
2376 | if (((u64)i << PAGE_CACHE_SHIFT) > inode->i_size) |
2377 | goto truncate_racing; | |
2378 | ||
edbd8d4e | 2379 | page = grab_cache_page(inode->i_mapping, i); |
a061fc8d | 2380 | if (!page) { |
edbd8d4e | 2381 | goto out_unlock; |
a061fc8d | 2382 | } |
edbd8d4e CM |
2383 | if (!PageUptodate(page)) { |
2384 | btrfs_readpage(NULL, page); | |
2385 | lock_page(page); | |
2386 | if (!PageUptodate(page)) { | |
2387 | unlock_page(page); | |
2388 | page_cache_release(page); | |
2389 | goto out_unlock; | |
2390 | } | |
2391 | } | |
ec44a35c CM |
2392 | #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18) |
2393 | ClearPageDirty(page); | |
2394 | #else | |
2395 | cancel_dirty_page(page, PAGE_CACHE_SIZE); | |
2396 | #endif | |
2397 | wait_on_page_writeback(page); | |
2398 | set_page_extent_mapped(page); | |
edbd8d4e CM |
2399 | page_start = (u64)page->index << PAGE_CACHE_SHIFT; |
2400 | page_end = page_start + PAGE_CACHE_SIZE - 1; | |
2401 | ||
d1310b2e | 2402 | lock_extent(io_tree, page_start, page_end, GFP_NOFS); |
edbd8d4e | 2403 | |
d1310b2e | 2404 | set_extent_delalloc(io_tree, page_start, |
edbd8d4e | 2405 | page_end, GFP_NOFS); |
a061fc8d | 2406 | set_page_dirty(page); |
edbd8d4e | 2407 | |
d1310b2e | 2408 | unlock_extent(io_tree, page_start, page_end, GFP_NOFS); |
edbd8d4e CM |
2409 | unlock_page(page); |
2410 | page_cache_release(page); | |
2411 | } | |
a061fc8d CM |
2412 | balance_dirty_pages_ratelimited_nr(inode->i_mapping, |
2413 | total_read); | |
edbd8d4e CM |
2414 | |
2415 | out_unlock: | |
ec44a35c | 2416 | kfree(ra); |
a061fc8d CM |
2417 | trans = btrfs_start_transaction(BTRFS_I(inode)->root, 1); |
2418 | if (trans) { | |
2419 | btrfs_add_ordered_inode(inode); | |
2420 | btrfs_end_transaction(trans, BTRFS_I(inode)->root); | |
2421 | mark_inode_dirty(inode); | |
2422 | } | |
edbd8d4e CM |
2423 | mutex_unlock(&inode->i_mutex); |
2424 | return 0; | |
a061fc8d CM |
2425 | |
2426 | truncate_racing: | |
2427 | vmtruncate(inode, inode->i_size); | |
2428 | balance_dirty_pages_ratelimited_nr(inode->i_mapping, | |
2429 | total_read); | |
2430 | goto out_unlock; | |
edbd8d4e CM |
2431 | } |
2432 | ||
bf4ef679 CM |
2433 | /* |
2434 | * The back references tell us which tree holds a ref on a block, | |
2435 | * but it is possible for the tree root field in the reference to | |
2436 | * reflect the original root before a snapshot was made. In this | |
2437 | * case we should search through all the children of a given root | |
2438 | * to find potential holders of references on a block. | |
2439 | * | |
2440 | * Instead, we do something a little less fancy and just search | |
2441 | * all the roots for a given key/block combination. | |
2442 | */ | |
2443 | static int find_root_for_ref(struct btrfs_root *root, | |
2444 | struct btrfs_path *path, | |
2445 | struct btrfs_key *key0, | |
2446 | int level, | |
2447 | int file_key, | |
2448 | struct btrfs_root **found_root, | |
2449 | u64 bytenr) | |
2450 | { | |
2451 | struct btrfs_key root_location; | |
2452 | struct btrfs_root *cur_root = *found_root; | |
2453 | struct btrfs_file_extent_item *file_extent; | |
2454 | u64 root_search_start = BTRFS_FS_TREE_OBJECTID; | |
2455 | u64 found_bytenr; | |
2456 | int ret; | |
2457 | int i; | |
2458 | ||
2459 | root_location.offset = (u64)-1; | |
2460 | root_location.type = BTRFS_ROOT_ITEM_KEY; | |
2461 | path->lowest_level = level; | |
2462 | path->reada = 0; | |
2463 | while(1) { | |
2464 | ret = btrfs_search_slot(NULL, cur_root, key0, path, 0, 0); | |
2465 | found_bytenr = 0; | |
2466 | if (ret == 0 && file_key) { | |
2467 | struct extent_buffer *leaf = path->nodes[0]; | |
2468 | file_extent = btrfs_item_ptr(leaf, path->slots[0], | |
2469 | struct btrfs_file_extent_item); | |
2470 | if (btrfs_file_extent_type(leaf, file_extent) == | |
2471 | BTRFS_FILE_EXTENT_REG) { | |
2472 | found_bytenr = | |
2473 | btrfs_file_extent_disk_bytenr(leaf, | |
2474 | file_extent); | |
2475 | } | |
2476 | } else if (ret == 0) { | |
2477 | if (path->nodes[level]) | |
2478 | found_bytenr = path->nodes[level]->start; | |
2479 | } | |
2480 | ||
2481 | for (i = level; i < BTRFS_MAX_LEVEL; i++) { | |
2482 | if (!path->nodes[i]) | |
2483 | break; | |
2484 | free_extent_buffer(path->nodes[i]); | |
2485 | path->nodes[i] = NULL; | |
2486 | } | |
2487 | btrfs_release_path(cur_root, path); | |
2488 | ||
2489 | if (found_bytenr == bytenr) { | |
2490 | *found_root = cur_root; | |
2491 | ret = 0; | |
2492 | goto out; | |
2493 | } | |
2494 | ret = btrfs_search_root(root->fs_info->tree_root, | |
2495 | root_search_start, &root_search_start); | |
2496 | if (ret) | |
2497 | break; | |
2498 | ||
2499 | root_location.objectid = root_search_start; | |
2500 | cur_root = btrfs_read_fs_root_no_name(root->fs_info, | |
2501 | &root_location); | |
2502 | if (!cur_root) { | |
2503 | ret = 1; | |
2504 | break; | |
2505 | } | |
2506 | } | |
2507 | out: | |
2508 | path->lowest_level = 0; | |
2509 | return ret; | |
2510 | } | |
2511 | ||
4313b399 CM |
2512 | /* |
2513 | * note, this releases the path | |
2514 | */ | |
98ed5174 | 2515 | static int noinline relocate_one_reference(struct btrfs_root *extent_root, |
edbd8d4e | 2516 | struct btrfs_path *path, |
4313b399 | 2517 | struct btrfs_key *extent_key) |
edbd8d4e CM |
2518 | { |
2519 | struct inode *inode; | |
2520 | struct btrfs_root *found_root; | |
bf4ef679 CM |
2521 | struct btrfs_key root_location; |
2522 | struct btrfs_key found_key; | |
4313b399 CM |
2523 | struct btrfs_extent_ref *ref; |
2524 | u64 ref_root; | |
2525 | u64 ref_gen; | |
2526 | u64 ref_objectid; | |
2527 | u64 ref_offset; | |
edbd8d4e | 2528 | int ret; |
bf4ef679 | 2529 | int level; |
edbd8d4e | 2530 | |
4313b399 CM |
2531 | ref = btrfs_item_ptr(path->nodes[0], path->slots[0], |
2532 | struct btrfs_extent_ref); | |
2533 | ref_root = btrfs_ref_root(path->nodes[0], ref); | |
2534 | ref_gen = btrfs_ref_generation(path->nodes[0], ref); | |
2535 | ref_objectid = btrfs_ref_objectid(path->nodes[0], ref); | |
2536 | ref_offset = btrfs_ref_offset(path->nodes[0], ref); | |
2537 | btrfs_release_path(extent_root, path); | |
2538 | ||
bf4ef679 | 2539 | root_location.objectid = ref_root; |
edbd8d4e | 2540 | if (ref_gen == 0) |
bf4ef679 | 2541 | root_location.offset = 0; |
edbd8d4e | 2542 | else |
bf4ef679 CM |
2543 | root_location.offset = (u64)-1; |
2544 | root_location.type = BTRFS_ROOT_ITEM_KEY; | |
edbd8d4e CM |
2545 | |
2546 | found_root = btrfs_read_fs_root_no_name(extent_root->fs_info, | |
bf4ef679 | 2547 | &root_location); |
edbd8d4e CM |
2548 | BUG_ON(!found_root); |
2549 | ||
2550 | if (ref_objectid >= BTRFS_FIRST_FREE_OBJECTID) { | |
bf4ef679 CM |
2551 | found_key.objectid = ref_objectid; |
2552 | found_key.type = BTRFS_EXTENT_DATA_KEY; | |
2553 | found_key.offset = ref_offset; | |
2554 | level = 0; | |
2555 | ||
2556 | ret = find_root_for_ref(extent_root, path, &found_key, | |
2557 | level, 1, &found_root, | |
2558 | extent_key->objectid); | |
2559 | ||
2560 | if (ret) | |
2561 | goto out; | |
2562 | ||
edbd8d4e CM |
2563 | mutex_unlock(&extent_root->fs_info->fs_mutex); |
2564 | inode = btrfs_iget_locked(extent_root->fs_info->sb, | |
2565 | ref_objectid, found_root); | |
2566 | if (inode->i_state & I_NEW) { | |
2567 | /* the inode and parent dir are two different roots */ | |
2568 | BTRFS_I(inode)->root = found_root; | |
2569 | BTRFS_I(inode)->location.objectid = ref_objectid; | |
2570 | BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY; | |
2571 | BTRFS_I(inode)->location.offset = 0; | |
2572 | btrfs_read_locked_inode(inode); | |
2573 | unlock_new_inode(inode); | |
2574 | ||
2575 | } | |
2576 | /* this can happen if the reference is not against | |
2577 | * the latest version of the tree root | |
2578 | */ | |
2579 | if (is_bad_inode(inode)) { | |
2580 | mutex_lock(&extent_root->fs_info->fs_mutex); | |
2581 | goto out; | |
2582 | } | |
2583 | relocate_inode_pages(inode, ref_offset, extent_key->offset); | |
edbd8d4e CM |
2584 | iput(inode); |
2585 | mutex_lock(&extent_root->fs_info->fs_mutex); | |
2586 | } else { | |
2587 | struct btrfs_trans_handle *trans; | |
edbd8d4e | 2588 | struct extent_buffer *eb; |
edbd8d4e CM |
2589 | int i; |
2590 | ||
edbd8d4e CM |
2591 | eb = read_tree_block(found_root, extent_key->objectid, |
2592 | extent_key->offset); | |
2593 | level = btrfs_header_level(eb); | |
2594 | ||
2595 | if (level == 0) | |
2596 | btrfs_item_key_to_cpu(eb, &found_key, 0); | |
2597 | else | |
2598 | btrfs_node_key_to_cpu(eb, &found_key, 0); | |
2599 | ||
2600 | free_extent_buffer(eb); | |
2601 | ||
bf4ef679 CM |
2602 | ret = find_root_for_ref(extent_root, path, &found_key, |
2603 | level, 0, &found_root, | |
2604 | extent_key->objectid); | |
2605 | ||
2606 | if (ret) | |
2607 | goto out; | |
2608 | ||
2609 | trans = btrfs_start_transaction(found_root, 1); | |
2610 | ||
edbd8d4e | 2611 | path->lowest_level = level; |
8f662a76 | 2612 | path->reada = 2; |
edbd8d4e CM |
2613 | ret = btrfs_search_slot(trans, found_root, &found_key, path, |
2614 | 0, 1); | |
2615 | path->lowest_level = 0; | |
2616 | for (i = level; i < BTRFS_MAX_LEVEL; i++) { | |
2617 | if (!path->nodes[i]) | |
2618 | break; | |
2619 | free_extent_buffer(path->nodes[i]); | |
2620 | path->nodes[i] = NULL; | |
2621 | } | |
2622 | btrfs_release_path(found_root, path); | |
2623 | btrfs_end_transaction(trans, found_root); | |
2624 | } | |
2625 | ||
2626 | out: | |
2627 | return 0; | |
2628 | } | |
2629 | ||
a061fc8d CM |
2630 | static int noinline del_extent_zero(struct btrfs_root *extent_root, |
2631 | struct btrfs_path *path, | |
2632 | struct btrfs_key *extent_key) | |
2633 | { | |
2634 | int ret; | |
2635 | struct btrfs_trans_handle *trans; | |
2636 | ||
2637 | trans = btrfs_start_transaction(extent_root, 1); | |
2638 | ret = btrfs_search_slot(trans, extent_root, extent_key, path, -1, 1); | |
2639 | if (ret > 0) { | |
2640 | ret = -EIO; | |
2641 | goto out; | |
2642 | } | |
2643 | if (ret < 0) | |
2644 | goto out; | |
2645 | ret = btrfs_del_item(trans, extent_root, path); | |
2646 | out: | |
2647 | btrfs_end_transaction(trans, extent_root); | |
2648 | return ret; | |
2649 | } | |
2650 | ||
98ed5174 CM |
2651 | static int noinline relocate_one_extent(struct btrfs_root *extent_root, |
2652 | struct btrfs_path *path, | |
2653 | struct btrfs_key *extent_key) | |
edbd8d4e CM |
2654 | { |
2655 | struct btrfs_key key; | |
2656 | struct btrfs_key found_key; | |
edbd8d4e | 2657 | struct extent_buffer *leaf; |
edbd8d4e CM |
2658 | u32 nritems; |
2659 | u32 item_size; | |
2660 | int ret = 0; | |
2661 | ||
a061fc8d CM |
2662 | if (extent_key->objectid == 0) { |
2663 | ret = del_extent_zero(extent_root, path, extent_key); | |
2664 | goto out; | |
2665 | } | |
edbd8d4e CM |
2666 | key.objectid = extent_key->objectid; |
2667 | key.type = BTRFS_EXTENT_REF_KEY; | |
2668 | key.offset = 0; | |
2669 | ||
2670 | while(1) { | |
2671 | ret = btrfs_search_slot(NULL, extent_root, &key, path, 0, 0); | |
2672 | ||
edbd8d4e CM |
2673 | if (ret < 0) |
2674 | goto out; | |
2675 | ||
2676 | ret = 0; | |
2677 | leaf = path->nodes[0]; | |
2678 | nritems = btrfs_header_nritems(leaf); | |
a061fc8d CM |
2679 | if (path->slots[0] == nritems) { |
2680 | ret = btrfs_next_leaf(extent_root, path); | |
2681 | if (ret > 0) { | |
2682 | ret = 0; | |
2683 | goto out; | |
2684 | } | |
2685 | if (ret < 0) | |
2686 | goto out; | |
bf4ef679 | 2687 | leaf = path->nodes[0]; |
a061fc8d | 2688 | } |
edbd8d4e CM |
2689 | |
2690 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
a061fc8d | 2691 | if (found_key.objectid != extent_key->objectid) { |
edbd8d4e | 2692 | break; |
a061fc8d | 2693 | } |
edbd8d4e | 2694 | |
a061fc8d | 2695 | if (found_key.type != BTRFS_EXTENT_REF_KEY) { |
edbd8d4e | 2696 | break; |
a061fc8d | 2697 | } |
edbd8d4e CM |
2698 | |
2699 | key.offset = found_key.offset + 1; | |
2700 | item_size = btrfs_item_size_nr(leaf, path->slots[0]); | |
2701 | ||
4313b399 | 2702 | ret = relocate_one_reference(extent_root, path, extent_key); |
edbd8d4e CM |
2703 | if (ret) |
2704 | goto out; | |
2705 | } | |
2706 | ret = 0; | |
2707 | out: | |
2708 | btrfs_release_path(extent_root, path); | |
2709 | return ret; | |
2710 | } | |
2711 | ||
ec44a35c CM |
2712 | static u64 update_block_group_flags(struct btrfs_root *root, u64 flags) |
2713 | { | |
2714 | u64 num_devices; | |
2715 | u64 stripped = BTRFS_BLOCK_GROUP_RAID0 | | |
2716 | BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10; | |
2717 | ||
a061fc8d | 2718 | num_devices = root->fs_info->fs_devices->num_devices; |
ec44a35c CM |
2719 | if (num_devices == 1) { |
2720 | stripped |= BTRFS_BLOCK_GROUP_DUP; | |
2721 | stripped = flags & ~stripped; | |
2722 | ||
2723 | /* turn raid0 into single device chunks */ | |
2724 | if (flags & BTRFS_BLOCK_GROUP_RAID0) | |
2725 | return stripped; | |
2726 | ||
2727 | /* turn mirroring into duplication */ | |
2728 | if (flags & (BTRFS_BLOCK_GROUP_RAID1 | | |
2729 | BTRFS_BLOCK_GROUP_RAID10)) | |
2730 | return stripped | BTRFS_BLOCK_GROUP_DUP; | |
2731 | return flags; | |
2732 | } else { | |
2733 | /* they already had raid on here, just return */ | |
ec44a35c CM |
2734 | if (flags & stripped) |
2735 | return flags; | |
2736 | ||
2737 | stripped |= BTRFS_BLOCK_GROUP_DUP; | |
2738 | stripped = flags & ~stripped; | |
2739 | ||
2740 | /* switch duplicated blocks with raid1 */ | |
2741 | if (flags & BTRFS_BLOCK_GROUP_DUP) | |
2742 | return stripped | BTRFS_BLOCK_GROUP_RAID1; | |
2743 | ||
2744 | /* turn single device chunks into raid0 */ | |
2745 | return stripped | BTRFS_BLOCK_GROUP_RAID0; | |
2746 | } | |
2747 | return flags; | |
2748 | } | |
2749 | ||
8f18cf13 | 2750 | int btrfs_shrink_extent_tree(struct btrfs_root *root, u64 shrink_start) |
edbd8d4e CM |
2751 | { |
2752 | struct btrfs_trans_handle *trans; | |
2753 | struct btrfs_root *tree_root = root->fs_info->tree_root; | |
2754 | struct btrfs_path *path; | |
2755 | u64 cur_byte; | |
2756 | u64 total_found; | |
8f18cf13 | 2757 | u64 shrink_last_byte; |
ec44a35c | 2758 | u64 new_alloc_flags; |
8f18cf13 | 2759 | struct btrfs_block_group_cache *shrink_block_group; |
edbd8d4e | 2760 | struct btrfs_fs_info *info = root->fs_info; |
edbd8d4e | 2761 | struct btrfs_key key; |
73e48b27 | 2762 | struct btrfs_key found_key; |
edbd8d4e CM |
2763 | struct extent_buffer *leaf; |
2764 | u32 nritems; | |
2765 | int ret; | |
a061fc8d | 2766 | int progress; |
edbd8d4e | 2767 | |
8f18cf13 CM |
2768 | shrink_block_group = btrfs_lookup_block_group(root->fs_info, |
2769 | shrink_start); | |
2770 | BUG_ON(!shrink_block_group); | |
2771 | ||
2772 | shrink_last_byte = shrink_start + shrink_block_group->key.offset; | |
2773 | ||
2774 | shrink_block_group->space_info->total_bytes -= | |
2775 | shrink_block_group->key.offset; | |
edbd8d4e CM |
2776 | path = btrfs_alloc_path(); |
2777 | root = root->fs_info->extent_root; | |
8f662a76 | 2778 | path->reada = 2; |
edbd8d4e CM |
2779 | |
2780 | again: | |
ec44a35c CM |
2781 | if (btrfs_block_group_used(&shrink_block_group->item) > 0) { |
2782 | trans = btrfs_start_transaction(root, 1); | |
2783 | new_alloc_flags = update_block_group_flags(root, | |
2784 | shrink_block_group->flags); | |
2785 | do_chunk_alloc(trans, root->fs_info->extent_root, | |
8f18cf13 | 2786 | btrfs_block_group_used(&shrink_block_group->item) + |
ec44a35c CM |
2787 | 2 * 1024 * 1024, new_alloc_flags); |
2788 | btrfs_end_transaction(trans, root); | |
2789 | } | |
8f18cf13 CM |
2790 | shrink_block_group->ro = 1; |
2791 | ||
edbd8d4e | 2792 | total_found = 0; |
a061fc8d | 2793 | progress = 0; |
8f18cf13 | 2794 | key.objectid = shrink_start; |
edbd8d4e CM |
2795 | key.offset = 0; |
2796 | key.type = 0; | |
73e48b27 | 2797 | cur_byte = key.objectid; |
4313b399 | 2798 | |
73e48b27 Y |
2799 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
2800 | if (ret < 0) | |
2801 | goto out; | |
2802 | ||
0b86a832 | 2803 | ret = btrfs_previous_item(root, path, 0, BTRFS_EXTENT_ITEM_KEY); |
73e48b27 Y |
2804 | if (ret < 0) |
2805 | goto out; | |
8f18cf13 | 2806 | |
73e48b27 Y |
2807 | if (ret == 0) { |
2808 | leaf = path->nodes[0]; | |
2809 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
8f18cf13 CM |
2810 | if (found_key.objectid + found_key.offset > shrink_start && |
2811 | found_key.objectid < shrink_last_byte) { | |
73e48b27 Y |
2812 | cur_byte = found_key.objectid; |
2813 | key.objectid = cur_byte; | |
2814 | } | |
2815 | } | |
2816 | btrfs_release_path(root, path); | |
2817 | ||
2818 | while(1) { | |
edbd8d4e CM |
2819 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
2820 | if (ret < 0) | |
2821 | goto out; | |
73e48b27 | 2822 | |
edbd8d4e | 2823 | leaf = path->nodes[0]; |
73e48b27 Y |
2824 | nritems = btrfs_header_nritems(leaf); |
2825 | next: | |
2826 | if (path->slots[0] >= nritems) { | |
2827 | ret = btrfs_next_leaf(root, path); | |
2828 | if (ret < 0) | |
2829 | goto out; | |
2830 | if (ret == 1) { | |
2831 | ret = 0; | |
2832 | break; | |
edbd8d4e | 2833 | } |
73e48b27 Y |
2834 | leaf = path->nodes[0]; |
2835 | nritems = btrfs_header_nritems(leaf); | |
edbd8d4e | 2836 | } |
73e48b27 Y |
2837 | |
2838 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
725c8463 | 2839 | |
8f18cf13 CM |
2840 | if (found_key.objectid >= shrink_last_byte) |
2841 | break; | |
2842 | ||
725c8463 CM |
2843 | if (progress && need_resched()) { |
2844 | memcpy(&key, &found_key, sizeof(key)); | |
2845 | mutex_unlock(&root->fs_info->fs_mutex); | |
2846 | cond_resched(); | |
2847 | mutex_lock(&root->fs_info->fs_mutex); | |
2848 | btrfs_release_path(root, path); | |
2849 | btrfs_search_slot(NULL, root, &key, path, 0, 0); | |
2850 | progress = 0; | |
2851 | goto next; | |
2852 | } | |
2853 | progress = 1; | |
2854 | ||
73e48b27 Y |
2855 | if (btrfs_key_type(&found_key) != BTRFS_EXTENT_ITEM_KEY || |
2856 | found_key.objectid + found_key.offset <= cur_byte) { | |
edbd8d4e | 2857 | path->slots[0]++; |
edbd8d4e CM |
2858 | goto next; |
2859 | } | |
73e48b27 | 2860 | |
edbd8d4e CM |
2861 | total_found++; |
2862 | cur_byte = found_key.objectid + found_key.offset; | |
2863 | key.objectid = cur_byte; | |
2864 | btrfs_release_path(root, path); | |
2865 | ret = relocate_one_extent(root, path, &found_key); | |
2866 | } | |
2867 | ||
2868 | btrfs_release_path(root, path); | |
2869 | ||
2870 | if (total_found > 0) { | |
2871 | trans = btrfs_start_transaction(tree_root, 1); | |
2872 | btrfs_commit_transaction(trans, tree_root); | |
2873 | ||
2874 | mutex_unlock(&root->fs_info->fs_mutex); | |
2875 | btrfs_clean_old_snapshots(tree_root); | |
2876 | mutex_lock(&root->fs_info->fs_mutex); | |
2877 | ||
2878 | trans = btrfs_start_transaction(tree_root, 1); | |
2879 | btrfs_commit_transaction(trans, tree_root); | |
2880 | goto again; | |
2881 | } | |
2882 | ||
8f18cf13 CM |
2883 | /* |
2884 | * we've freed all the extents, now remove the block | |
2885 | * group item from the tree | |
2886 | */ | |
edbd8d4e | 2887 | trans = btrfs_start_transaction(root, 1); |
8f18cf13 | 2888 | memcpy(&key, &shrink_block_group->key, sizeof(key)); |
1372f8e6 | 2889 | |
8f18cf13 CM |
2890 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
2891 | if (ret > 0) | |
2892 | ret = -EIO; | |
2893 | if (ret < 0) | |
2894 | goto out; | |
73e48b27 | 2895 | |
8f18cf13 CM |
2896 | leaf = path->nodes[0]; |
2897 | nritems = btrfs_header_nritems(leaf); | |
2898 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
2899 | kfree(shrink_block_group); | |
edbd8d4e | 2900 | |
8f18cf13 CM |
2901 | clear_extent_bits(&info->block_group_cache, found_key.objectid, |
2902 | found_key.objectid + found_key.offset - 1, | |
2903 | (unsigned int)-1, GFP_NOFS); | |
edbd8d4e | 2904 | |
8f18cf13 CM |
2905 | btrfs_del_item(trans, root, path); |
2906 | clear_extent_dirty(&info->free_space_cache, | |
2907 | shrink_start, shrink_last_byte - 1, | |
edbd8d4e CM |
2908 | GFP_NOFS); |
2909 | btrfs_commit_transaction(trans, root); | |
2910 | out: | |
2911 | btrfs_free_path(path); | |
2912 | return ret; | |
2913 | } | |
2914 | ||
0b86a832 CM |
2915 | int find_first_block_group(struct btrfs_root *root, struct btrfs_path *path, |
2916 | struct btrfs_key *key) | |
2917 | { | |
2918 | int ret; | |
2919 | struct btrfs_key found_key; | |
2920 | struct extent_buffer *leaf; | |
2921 | int slot; | |
edbd8d4e | 2922 | |
0b86a832 CM |
2923 | ret = btrfs_search_slot(NULL, root, key, path, 0, 0); |
2924 | if (ret < 0) | |
2925 | return ret; | |
2926 | while(1) { | |
2927 | slot = path->slots[0]; | |
edbd8d4e | 2928 | leaf = path->nodes[0]; |
0b86a832 CM |
2929 | if (slot >= btrfs_header_nritems(leaf)) { |
2930 | ret = btrfs_next_leaf(root, path); | |
2931 | if (ret == 0) | |
2932 | continue; | |
2933 | if (ret < 0) | |
2934 | goto error; | |
2935 | break; | |
edbd8d4e | 2936 | } |
0b86a832 | 2937 | btrfs_item_key_to_cpu(leaf, &found_key, slot); |
edbd8d4e | 2938 | |
0b86a832 CM |
2939 | if (found_key.objectid >= key->objectid && |
2940 | found_key.type == BTRFS_BLOCK_GROUP_ITEM_KEY) | |
2941 | return 0; | |
2942 | path->slots[0]++; | |
edbd8d4e | 2943 | } |
0b86a832 CM |
2944 | ret = -ENOENT; |
2945 | error: | |
2946 | return ret; | |
edbd8d4e CM |
2947 | } |
2948 | ||
9078a3e1 CM |
2949 | int btrfs_read_block_groups(struct btrfs_root *root) |
2950 | { | |
2951 | struct btrfs_path *path; | |
2952 | int ret; | |
96b5179d | 2953 | int bit; |
9078a3e1 | 2954 | struct btrfs_block_group_cache *cache; |
be744175 | 2955 | struct btrfs_fs_info *info = root->fs_info; |
6324fbf3 | 2956 | struct btrfs_space_info *space_info; |
d1310b2e | 2957 | struct extent_io_tree *block_group_cache; |
9078a3e1 CM |
2958 | struct btrfs_key key; |
2959 | struct btrfs_key found_key; | |
5f39d397 | 2960 | struct extent_buffer *leaf; |
96b5179d CM |
2961 | |
2962 | block_group_cache = &info->block_group_cache; | |
be744175 | 2963 | root = info->extent_root; |
9078a3e1 | 2964 | key.objectid = 0; |
0b86a832 | 2965 | key.offset = 0; |
9078a3e1 | 2966 | btrfs_set_key_type(&key, BTRFS_BLOCK_GROUP_ITEM_KEY); |
9078a3e1 CM |
2967 | path = btrfs_alloc_path(); |
2968 | if (!path) | |
2969 | return -ENOMEM; | |
2970 | ||
2971 | while(1) { | |
0b86a832 CM |
2972 | ret = find_first_block_group(root, path, &key); |
2973 | if (ret > 0) { | |
2974 | ret = 0; | |
2975 | goto error; | |
9078a3e1 | 2976 | } |
0b86a832 CM |
2977 | if (ret != 0) |
2978 | goto error; | |
2979 | ||
5f39d397 CM |
2980 | leaf = path->nodes[0]; |
2981 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); | |
8f18cf13 | 2982 | cache = kzalloc(sizeof(*cache), GFP_NOFS); |
9078a3e1 | 2983 | if (!cache) { |
0b86a832 | 2984 | ret = -ENOMEM; |
9078a3e1 CM |
2985 | break; |
2986 | } | |
3e1ad54f | 2987 | |
5f39d397 CM |
2988 | read_extent_buffer(leaf, &cache->item, |
2989 | btrfs_item_ptr_offset(leaf, path->slots[0]), | |
2990 | sizeof(cache->item)); | |
9078a3e1 | 2991 | memcpy(&cache->key, &found_key, sizeof(found_key)); |
0b86a832 | 2992 | |
9078a3e1 CM |
2993 | key.objectid = found_key.objectid + found_key.offset; |
2994 | btrfs_release_path(root, path); | |
0b86a832 CM |
2995 | cache->flags = btrfs_block_group_flags(&cache->item); |
2996 | bit = 0; | |
2997 | if (cache->flags & BTRFS_BLOCK_GROUP_DATA) { | |
96b5179d | 2998 | bit = BLOCK_GROUP_DATA; |
0b86a832 CM |
2999 | } else if (cache->flags & BTRFS_BLOCK_GROUP_SYSTEM) { |
3000 | bit = BLOCK_GROUP_SYSTEM; | |
3001 | } else if (cache->flags & BTRFS_BLOCK_GROUP_METADATA) { | |
96b5179d | 3002 | bit = BLOCK_GROUP_METADATA; |
31f3c99b | 3003 | } |
8790d502 | 3004 | set_avail_alloc_bits(info, cache->flags); |
96b5179d | 3005 | |
6324fbf3 CM |
3006 | ret = update_space_info(info, cache->flags, found_key.offset, |
3007 | btrfs_block_group_used(&cache->item), | |
3008 | &space_info); | |
3009 | BUG_ON(ret); | |
3010 | cache->space_info = space_info; | |
3011 | ||
96b5179d CM |
3012 | /* use EXTENT_LOCKED to prevent merging */ |
3013 | set_extent_bits(block_group_cache, found_key.objectid, | |
3014 | found_key.objectid + found_key.offset - 1, | |
3015 | bit | EXTENT_LOCKED, GFP_NOFS); | |
3016 | set_state_private(block_group_cache, found_key.objectid, | |
ae2f5411 | 3017 | (unsigned long)cache); |
96b5179d | 3018 | |
9078a3e1 | 3019 | if (key.objectid >= |
db94535d | 3020 | btrfs_super_total_bytes(&info->super_copy)) |
9078a3e1 CM |
3021 | break; |
3022 | } | |
0b86a832 CM |
3023 | ret = 0; |
3024 | error: | |
9078a3e1 | 3025 | btrfs_free_path(path); |
0b86a832 | 3026 | return ret; |
9078a3e1 | 3027 | } |
6324fbf3 CM |
3028 | |
3029 | int btrfs_make_block_group(struct btrfs_trans_handle *trans, | |
3030 | struct btrfs_root *root, u64 bytes_used, | |
e17cade2 | 3031 | u64 type, u64 chunk_objectid, u64 chunk_offset, |
6324fbf3 CM |
3032 | u64 size) |
3033 | { | |
3034 | int ret; | |
3035 | int bit = 0; | |
3036 | struct btrfs_root *extent_root; | |
3037 | struct btrfs_block_group_cache *cache; | |
3038 | struct extent_io_tree *block_group_cache; | |
3039 | ||
3040 | extent_root = root->fs_info->extent_root; | |
3041 | block_group_cache = &root->fs_info->block_group_cache; | |
3042 | ||
8f18cf13 | 3043 | cache = kzalloc(sizeof(*cache), GFP_NOFS); |
6324fbf3 | 3044 | BUG_ON(!cache); |
e17cade2 | 3045 | cache->key.objectid = chunk_offset; |
6324fbf3 | 3046 | cache->key.offset = size; |
e17cade2 | 3047 | |
6324fbf3 CM |
3048 | btrfs_set_key_type(&cache->key, BTRFS_BLOCK_GROUP_ITEM_KEY); |
3049 | memset(&cache->item, 0, sizeof(cache->item)); | |
3050 | btrfs_set_block_group_used(&cache->item, bytes_used); | |
6324fbf3 CM |
3051 | btrfs_set_block_group_chunk_objectid(&cache->item, chunk_objectid); |
3052 | cache->flags = type; | |
3053 | btrfs_set_block_group_flags(&cache->item, type); | |
3054 | ||
3055 | ret = update_space_info(root->fs_info, cache->flags, size, bytes_used, | |
3056 | &cache->space_info); | |
3057 | BUG_ON(ret); | |
3058 | ||
d18a2c44 | 3059 | bit = block_group_state_bits(type); |
e17cade2 CM |
3060 | set_extent_bits(block_group_cache, chunk_offset, |
3061 | chunk_offset + size - 1, | |
6324fbf3 | 3062 | bit | EXTENT_LOCKED, GFP_NOFS); |
6324fbf3 | 3063 | |
e17cade2 CM |
3064 | set_state_private(block_group_cache, chunk_offset, |
3065 | (unsigned long)cache); | |
6324fbf3 CM |
3066 | ret = btrfs_insert_item(trans, extent_root, &cache->key, &cache->item, |
3067 | sizeof(cache->item)); | |
3068 | BUG_ON(ret); | |
3069 | ||
3070 | finish_current_insert(trans, extent_root); | |
3071 | ret = del_pending_extents(trans, extent_root); | |
3072 | BUG_ON(ret); | |
d18a2c44 | 3073 | set_avail_alloc_bits(extent_root->fs_info, type); |
6324fbf3 CM |
3074 | return 0; |
3075 | } |