blob: 5c2caad76212d2b5bfcc4329e255978525d6f5ed [file] [log] [blame]
Josef Bacik0f9dd462008-09-23 13:14:11 -04001/*
2 * Copyright (C) 2008 Red Hat. 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 */
18
Josef Bacik96303082009-07-13 21:29:25 -040019#include <linux/pagemap.h>
Josef Bacik0f9dd462008-09-23 13:14:11 -040020#include <linux/sched.h>
Josef Bacik96303082009-07-13 21:29:25 -040021#include <linux/math64.h>
Josef Bacik0f9dd462008-09-23 13:14:11 -040022#include "ctree.h"
Chris Masonfa9c0d792009-04-03 09:47:43 -040023#include "free-space-cache.h"
24#include "transaction.h"
25
Josef Bacik96303082009-07-13 21:29:25 -040026#define BITS_PER_BITMAP (PAGE_CACHE_SIZE * 8)
27#define MAX_CACHE_BYTES_PER_GIG (32 * 1024)
28
29static inline unsigned long offset_to_bit(u64 bitmap_start, u64 sectorsize,
30 u64 offset)
31{
32 BUG_ON(offset < bitmap_start);
33 offset -= bitmap_start;
34 return (unsigned long)(div64_u64(offset, sectorsize));
35}
36
37static inline unsigned long bytes_to_bits(u64 bytes, u64 sectorsize)
38{
39 return (unsigned long)(div64_u64(bytes, sectorsize));
40}
41
42static inline u64 offset_to_bitmap(struct btrfs_block_group_cache *block_group,
43 u64 offset)
44{
45 u64 bitmap_start;
46 u64 bytes_per_bitmap;
47
48 bytes_per_bitmap = BITS_PER_BITMAP * block_group->sectorsize;
49 bitmap_start = offset - block_group->key.objectid;
50 bitmap_start = div64_u64(bitmap_start, bytes_per_bitmap);
51 bitmap_start *= bytes_per_bitmap;
52 bitmap_start += block_group->key.objectid;
53
54 return bitmap_start;
55}
Josef Bacik0f9dd462008-09-23 13:14:11 -040056
57static int tree_insert_offset(struct rb_root *root, u64 offset,
Josef Bacik96303082009-07-13 21:29:25 -040058 struct rb_node *node, int bitmap)
Josef Bacik0f9dd462008-09-23 13:14:11 -040059{
60 struct rb_node **p = &root->rb_node;
61 struct rb_node *parent = NULL;
62 struct btrfs_free_space *info;
63
64 while (*p) {
65 parent = *p;
66 info = rb_entry(parent, struct btrfs_free_space, offset_index);
67
Josef Bacik96303082009-07-13 21:29:25 -040068 if (offset < info->offset) {
Josef Bacik0f9dd462008-09-23 13:14:11 -040069 p = &(*p)->rb_left;
Josef Bacik96303082009-07-13 21:29:25 -040070 } else if (offset > info->offset) {
Josef Bacik0f9dd462008-09-23 13:14:11 -040071 p = &(*p)->rb_right;
Josef Bacik96303082009-07-13 21:29:25 -040072 } else {
73 /*
74 * we could have a bitmap entry and an extent entry
75 * share the same offset. If this is the case, we want
76 * the extent entry to always be found first if we do a
77 * linear search through the tree, since we want to have
78 * the quickest allocation time, and allocating from an
79 * extent is faster than allocating from a bitmap. So
80 * if we're inserting a bitmap and we find an entry at
81 * this offset, we want to go right, or after this entry
82 * logically. If we are inserting an extent and we've
83 * found a bitmap, we want to go left, or before
84 * logically.
85 */
86 if (bitmap) {
87 WARN_ON(info->bitmap);
88 p = &(*p)->rb_right;
89 } else {
90 WARN_ON(!info->bitmap);
91 p = &(*p)->rb_left;
92 }
93 }
Josef Bacik0f9dd462008-09-23 13:14:11 -040094 }
95
96 rb_link_node(node, parent, p);
97 rb_insert_color(node, root);
98
99 return 0;
100}
101
102/*
Josef Bacik70cb0742009-04-03 10:14:19 -0400103 * searches the tree for the given offset.
104 *
Josef Bacik96303082009-07-13 21:29:25 -0400105 * fuzzy - If this is set, then we are trying to make an allocation, and we just
106 * want a section that has at least bytes size and comes at or after the given
107 * offset.
Josef Bacik0f9dd462008-09-23 13:14:11 -0400108 */
Josef Bacik96303082009-07-13 21:29:25 -0400109static struct btrfs_free_space *
110tree_search_offset(struct btrfs_block_group_cache *block_group,
111 u64 offset, int bitmap_only, int fuzzy)
Josef Bacik0f9dd462008-09-23 13:14:11 -0400112{
Josef Bacik96303082009-07-13 21:29:25 -0400113 struct rb_node *n = block_group->free_space_offset.rb_node;
114 struct btrfs_free_space *entry, *prev = NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400115
Josef Bacik96303082009-07-13 21:29:25 -0400116 /* find entry that is closest to the 'offset' */
117 while (1) {
118 if (!n) {
119 entry = NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400120 break;
121 }
Josef Bacik96303082009-07-13 21:29:25 -0400122
123 entry = rb_entry(n, struct btrfs_free_space, offset_index);
124 prev = entry;
125
126 if (offset < entry->offset)
127 n = n->rb_left;
128 else if (offset > entry->offset)
129 n = n->rb_right;
130 else
131 break;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400132 }
133
Josef Bacik96303082009-07-13 21:29:25 -0400134 if (bitmap_only) {
135 if (!entry)
136 return NULL;
137 if (entry->bitmap)
138 return entry;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400139
Josef Bacik96303082009-07-13 21:29:25 -0400140 /*
141 * bitmap entry and extent entry may share same offset,
142 * in that case, bitmap entry comes after extent entry.
143 */
144 n = rb_next(n);
145 if (!n)
146 return NULL;
147 entry = rb_entry(n, struct btrfs_free_space, offset_index);
148 if (entry->offset != offset)
149 return NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400150
Josef Bacik96303082009-07-13 21:29:25 -0400151 WARN_ON(!entry->bitmap);
152 return entry;
153 } else if (entry) {
154 if (entry->bitmap) {
Josef Bacik0f9dd462008-09-23 13:14:11 -0400155 /*
Josef Bacik96303082009-07-13 21:29:25 -0400156 * if previous extent entry covers the offset,
157 * we should return it instead of the bitmap entry
Josef Bacik0f9dd462008-09-23 13:14:11 -0400158 */
Josef Bacik96303082009-07-13 21:29:25 -0400159 n = &entry->offset_index;
160 while (1) {
161 n = rb_prev(n);
162 if (!n)
163 break;
164 prev = rb_entry(n, struct btrfs_free_space,
165 offset_index);
166 if (!prev->bitmap) {
167 if (prev->offset + prev->bytes > offset)
168 entry = prev;
169 break;
170 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400171 }
Josef Bacik96303082009-07-13 21:29:25 -0400172 }
173 return entry;
174 }
175
176 if (!prev)
177 return NULL;
178
179 /* find last entry before the 'offset' */
180 entry = prev;
181 if (entry->offset > offset) {
182 n = rb_prev(&entry->offset_index);
183 if (n) {
184 entry = rb_entry(n, struct btrfs_free_space,
185 offset_index);
186 BUG_ON(entry->offset > offset);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400187 } else {
Josef Bacik96303082009-07-13 21:29:25 -0400188 if (fuzzy)
189 return entry;
190 else
191 return NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400192 }
193 }
194
Josef Bacik96303082009-07-13 21:29:25 -0400195 if (entry->bitmap) {
196 n = &entry->offset_index;
197 while (1) {
198 n = rb_prev(n);
199 if (!n)
200 break;
201 prev = rb_entry(n, struct btrfs_free_space,
202 offset_index);
203 if (!prev->bitmap) {
204 if (prev->offset + prev->bytes > offset)
205 return prev;
206 break;
207 }
208 }
209 if (entry->offset + BITS_PER_BITMAP *
210 block_group->sectorsize > offset)
211 return entry;
212 } else if (entry->offset + entry->bytes > offset)
213 return entry;
214
215 if (!fuzzy)
216 return NULL;
217
218 while (1) {
219 if (entry->bitmap) {
220 if (entry->offset + BITS_PER_BITMAP *
221 block_group->sectorsize > offset)
222 break;
223 } else {
224 if (entry->offset + entry->bytes > offset)
225 break;
226 }
227
228 n = rb_next(&entry->offset_index);
229 if (!n)
230 return NULL;
231 entry = rb_entry(n, struct btrfs_free_space, offset_index);
232 }
233 return entry;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400234}
235
236static void unlink_free_space(struct btrfs_block_group_cache *block_group,
237 struct btrfs_free_space *info)
238{
239 rb_erase(&info->offset_index, &block_group->free_space_offset);
Josef Bacik96303082009-07-13 21:29:25 -0400240 block_group->free_extents--;
Josef Bacik817d52f2009-07-13 21:29:25 -0400241 block_group->free_space -= info->bytes;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400242}
243
244static int link_free_space(struct btrfs_block_group_cache *block_group,
245 struct btrfs_free_space *info)
246{
247 int ret = 0;
248
Josef Bacik96303082009-07-13 21:29:25 -0400249 BUG_ON(!info->bitmap && !info->bytes);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400250 ret = tree_insert_offset(&block_group->free_space_offset, info->offset,
Josef Bacik96303082009-07-13 21:29:25 -0400251 &info->offset_index, (info->bitmap != NULL));
Josef Bacik0f9dd462008-09-23 13:14:11 -0400252 if (ret)
253 return ret;
254
Josef Bacik817d52f2009-07-13 21:29:25 -0400255 block_group->free_space += info->bytes;
Josef Bacik96303082009-07-13 21:29:25 -0400256 block_group->free_extents++;
257 return ret;
258}
259
260static void recalculate_thresholds(struct btrfs_block_group_cache *block_group)
261{
Josef Bacik25891f72009-09-11 16:11:20 -0400262 u64 max_bytes;
263 u64 bitmap_bytes;
264 u64 extent_bytes;
Josef Bacik96303082009-07-13 21:29:25 -0400265
266 /*
267 * The goal is to keep the total amount of memory used per 1gb of space
268 * at or below 32k, so we need to adjust how much memory we allow to be
269 * used by extent based free space tracking
270 */
271 max_bytes = MAX_CACHE_BYTES_PER_GIG *
272 (div64_u64(block_group->key.offset, 1024 * 1024 * 1024));
273
Josef Bacik25891f72009-09-11 16:11:20 -0400274 /*
275 * we want to account for 1 more bitmap than what we have so we can make
276 * sure we don't go over our overall goal of MAX_CACHE_BYTES_PER_GIG as
277 * we add more bitmaps.
278 */
279 bitmap_bytes = (block_group->total_bitmaps + 1) * PAGE_CACHE_SIZE;
Josef Bacik96303082009-07-13 21:29:25 -0400280
Josef Bacik25891f72009-09-11 16:11:20 -0400281 if (bitmap_bytes >= max_bytes) {
282 block_group->extents_thresh = 0;
283 return;
Josef Bacik96303082009-07-13 21:29:25 -0400284 }
Josef Bacik25891f72009-09-11 16:11:20 -0400285
286 /*
287 * we want the extent entry threshold to always be at most 1/2 the maxw
288 * bytes we can have, or whatever is less than that.
289 */
290 extent_bytes = max_bytes - bitmap_bytes;
291 extent_bytes = min_t(u64, extent_bytes, div64_u64(max_bytes, 2));
292
293 block_group->extents_thresh =
294 div64_u64(extent_bytes, (sizeof(struct btrfs_free_space)));
Josef Bacik96303082009-07-13 21:29:25 -0400295}
296
Josef Bacik817d52f2009-07-13 21:29:25 -0400297static void bitmap_clear_bits(struct btrfs_block_group_cache *block_group,
298 struct btrfs_free_space *info, u64 offset,
299 u64 bytes)
Josef Bacik96303082009-07-13 21:29:25 -0400300{
301 unsigned long start, end;
302 unsigned long i;
303
Josef Bacik817d52f2009-07-13 21:29:25 -0400304 start = offset_to_bit(info->offset, block_group->sectorsize, offset);
305 end = start + bytes_to_bits(bytes, block_group->sectorsize);
Josef Bacik96303082009-07-13 21:29:25 -0400306 BUG_ON(end > BITS_PER_BITMAP);
307
308 for (i = start; i < end; i++)
309 clear_bit(i, info->bitmap);
310
311 info->bytes -= bytes;
Josef Bacik817d52f2009-07-13 21:29:25 -0400312 block_group->free_space -= bytes;
Josef Bacik96303082009-07-13 21:29:25 -0400313}
314
Josef Bacik817d52f2009-07-13 21:29:25 -0400315static void bitmap_set_bits(struct btrfs_block_group_cache *block_group,
316 struct btrfs_free_space *info, u64 offset,
317 u64 bytes)
Josef Bacik96303082009-07-13 21:29:25 -0400318{
319 unsigned long start, end;
320 unsigned long i;
321
Josef Bacik817d52f2009-07-13 21:29:25 -0400322 start = offset_to_bit(info->offset, block_group->sectorsize, offset);
323 end = start + bytes_to_bits(bytes, block_group->sectorsize);
Josef Bacik96303082009-07-13 21:29:25 -0400324 BUG_ON(end > BITS_PER_BITMAP);
325
326 for (i = start; i < end; i++)
327 set_bit(i, info->bitmap);
328
329 info->bytes += bytes;
Josef Bacik817d52f2009-07-13 21:29:25 -0400330 block_group->free_space += bytes;
Josef Bacik96303082009-07-13 21:29:25 -0400331}
332
333static int search_bitmap(struct btrfs_block_group_cache *block_group,
334 struct btrfs_free_space *bitmap_info, u64 *offset,
335 u64 *bytes)
336{
337 unsigned long found_bits = 0;
338 unsigned long bits, i;
339 unsigned long next_zero;
340
341 i = offset_to_bit(bitmap_info->offset, block_group->sectorsize,
342 max_t(u64, *offset, bitmap_info->offset));
343 bits = bytes_to_bits(*bytes, block_group->sectorsize);
344
345 for (i = find_next_bit(bitmap_info->bitmap, BITS_PER_BITMAP, i);
346 i < BITS_PER_BITMAP;
347 i = find_next_bit(bitmap_info->bitmap, BITS_PER_BITMAP, i + 1)) {
348 next_zero = find_next_zero_bit(bitmap_info->bitmap,
349 BITS_PER_BITMAP, i);
350 if ((next_zero - i) >= bits) {
351 found_bits = next_zero - i;
352 break;
353 }
354 i = next_zero;
355 }
356
357 if (found_bits) {
358 *offset = (u64)(i * block_group->sectorsize) +
359 bitmap_info->offset;
360 *bytes = (u64)(found_bits) * block_group->sectorsize;
361 return 0;
362 }
363
364 return -1;
365}
366
367static struct btrfs_free_space *find_free_space(struct btrfs_block_group_cache
368 *block_group, u64 *offset,
369 u64 *bytes, int debug)
370{
371 struct btrfs_free_space *entry;
372 struct rb_node *node;
373 int ret;
374
375 if (!block_group->free_space_offset.rb_node)
376 return NULL;
377
378 entry = tree_search_offset(block_group,
379 offset_to_bitmap(block_group, *offset),
380 0, 1);
381 if (!entry)
382 return NULL;
383
384 for (node = &entry->offset_index; node; node = rb_next(node)) {
385 entry = rb_entry(node, struct btrfs_free_space, offset_index);
386 if (entry->bytes < *bytes)
387 continue;
388
389 if (entry->bitmap) {
390 ret = search_bitmap(block_group, entry, offset, bytes);
391 if (!ret)
392 return entry;
393 continue;
394 }
395
396 *offset = entry->offset;
397 *bytes = entry->bytes;
398 return entry;
399 }
400
401 return NULL;
402}
403
404static void add_new_bitmap(struct btrfs_block_group_cache *block_group,
405 struct btrfs_free_space *info, u64 offset)
406{
407 u64 bytes_per_bg = BITS_PER_BITMAP * block_group->sectorsize;
408 int max_bitmaps = (int)div64_u64(block_group->key.offset +
409 bytes_per_bg - 1, bytes_per_bg);
410 BUG_ON(block_group->total_bitmaps >= max_bitmaps);
411
412 info->offset = offset_to_bitmap(block_group, offset);
Josef Bacikf019f422009-09-11 16:11:20 -0400413 info->bytes = 0;
Josef Bacik96303082009-07-13 21:29:25 -0400414 link_free_space(block_group, info);
415 block_group->total_bitmaps++;
416
417 recalculate_thresholds(block_group);
418}
419
420static noinline int remove_from_bitmap(struct btrfs_block_group_cache *block_group,
421 struct btrfs_free_space *bitmap_info,
422 u64 *offset, u64 *bytes)
423{
424 u64 end;
Josef Bacik6606bb92009-07-31 11:03:58 -0400425 u64 search_start, search_bytes;
426 int ret;
Josef Bacik96303082009-07-13 21:29:25 -0400427
428again:
429 end = bitmap_info->offset +
430 (u64)(BITS_PER_BITMAP * block_group->sectorsize) - 1;
431
Josef Bacik6606bb92009-07-31 11:03:58 -0400432 /*
433 * XXX - this can go away after a few releases.
434 *
435 * since the only user of btrfs_remove_free_space is the tree logging
436 * stuff, and the only way to test that is under crash conditions, we
437 * want to have this debug stuff here just in case somethings not
438 * working. Search the bitmap for the space we are trying to use to
439 * make sure its actually there. If its not there then we need to stop
440 * because something has gone wrong.
441 */
442 search_start = *offset;
443 search_bytes = *bytes;
444 ret = search_bitmap(block_group, bitmap_info, &search_start,
445 &search_bytes);
446 BUG_ON(ret < 0 || search_start != *offset);
447
Josef Bacik96303082009-07-13 21:29:25 -0400448 if (*offset > bitmap_info->offset && *offset + *bytes > end) {
Josef Bacik817d52f2009-07-13 21:29:25 -0400449 bitmap_clear_bits(block_group, bitmap_info, *offset,
450 end - *offset + 1);
Josef Bacik96303082009-07-13 21:29:25 -0400451 *bytes -= end - *offset + 1;
452 *offset = end + 1;
453 } else if (*offset >= bitmap_info->offset && *offset + *bytes <= end) {
Josef Bacik817d52f2009-07-13 21:29:25 -0400454 bitmap_clear_bits(block_group, bitmap_info, *offset, *bytes);
Josef Bacik96303082009-07-13 21:29:25 -0400455 *bytes = 0;
456 }
457
458 if (*bytes) {
Josef Bacik6606bb92009-07-31 11:03:58 -0400459 struct rb_node *next = rb_next(&bitmap_info->offset_index);
Josef Bacik96303082009-07-13 21:29:25 -0400460 if (!bitmap_info->bytes) {
461 unlink_free_space(block_group, bitmap_info);
462 kfree(bitmap_info->bitmap);
463 kfree(bitmap_info);
464 block_group->total_bitmaps--;
465 recalculate_thresholds(block_group);
466 }
467
Josef Bacik6606bb92009-07-31 11:03:58 -0400468 /*
469 * no entry after this bitmap, but we still have bytes to
470 * remove, so something has gone wrong.
471 */
472 if (!next)
Josef Bacik96303082009-07-13 21:29:25 -0400473 return -EINVAL;
474
Josef Bacik6606bb92009-07-31 11:03:58 -0400475 bitmap_info = rb_entry(next, struct btrfs_free_space,
476 offset_index);
477
478 /*
479 * if the next entry isn't a bitmap we need to return to let the
480 * extent stuff do its work.
481 */
Josef Bacik96303082009-07-13 21:29:25 -0400482 if (!bitmap_info->bitmap)
483 return -EAGAIN;
484
Josef Bacik6606bb92009-07-31 11:03:58 -0400485 /*
486 * Ok the next item is a bitmap, but it may not actually hold
487 * the information for the rest of this free space stuff, so
488 * look for it, and if we don't find it return so we can try
489 * everything over again.
490 */
491 search_start = *offset;
492 search_bytes = *bytes;
493 ret = search_bitmap(block_group, bitmap_info, &search_start,
494 &search_bytes);
495 if (ret < 0 || search_start != *offset)
496 return -EAGAIN;
497
Josef Bacik96303082009-07-13 21:29:25 -0400498 goto again;
499 } else if (!bitmap_info->bytes) {
500 unlink_free_space(block_group, bitmap_info);
501 kfree(bitmap_info->bitmap);
502 kfree(bitmap_info);
503 block_group->total_bitmaps--;
504 recalculate_thresholds(block_group);
505 }
506
507 return 0;
508}
509
510static int insert_into_bitmap(struct btrfs_block_group_cache *block_group,
511 struct btrfs_free_space *info)
512{
513 struct btrfs_free_space *bitmap_info;
514 int added = 0;
515 u64 bytes, offset, end;
516 int ret;
517
518 /*
519 * If we are below the extents threshold then we can add this as an
520 * extent, and don't have to deal with the bitmap
521 */
522 if (block_group->free_extents < block_group->extents_thresh &&
523 info->bytes > block_group->sectorsize * 4)
524 return 0;
525
526 /*
527 * some block groups are so tiny they can't be enveloped by a bitmap, so
528 * don't even bother to create a bitmap for this
529 */
530 if (BITS_PER_BITMAP * block_group->sectorsize >
531 block_group->key.offset)
532 return 0;
533
534 bytes = info->bytes;
535 offset = info->offset;
536
537again:
538 bitmap_info = tree_search_offset(block_group,
539 offset_to_bitmap(block_group, offset),
540 1, 0);
541 if (!bitmap_info) {
542 BUG_ON(added);
543 goto new_bitmap;
544 }
545
546 end = bitmap_info->offset +
547 (u64)(BITS_PER_BITMAP * block_group->sectorsize);
548
549 if (offset >= bitmap_info->offset && offset + bytes > end) {
Josef Bacik817d52f2009-07-13 21:29:25 -0400550 bitmap_set_bits(block_group, bitmap_info, offset,
551 end - offset);
Josef Bacik96303082009-07-13 21:29:25 -0400552 bytes -= end - offset;
553 offset = end;
554 added = 0;
555 } else if (offset >= bitmap_info->offset && offset + bytes <= end) {
Josef Bacik817d52f2009-07-13 21:29:25 -0400556 bitmap_set_bits(block_group, bitmap_info, offset, bytes);
Josef Bacik96303082009-07-13 21:29:25 -0400557 bytes = 0;
558 } else {
559 BUG();
560 }
561
562 if (!bytes) {
563 ret = 1;
564 goto out;
565 } else
566 goto again;
567
568new_bitmap:
569 if (info && info->bitmap) {
570 add_new_bitmap(block_group, info, offset);
571 added = 1;
572 info = NULL;
573 goto again;
574 } else {
575 spin_unlock(&block_group->tree_lock);
576
577 /* no pre-allocated info, allocate a new one */
578 if (!info) {
579 info = kzalloc(sizeof(struct btrfs_free_space),
580 GFP_NOFS);
581 if (!info) {
582 spin_lock(&block_group->tree_lock);
583 ret = -ENOMEM;
584 goto out;
585 }
586 }
587
588 /* allocate the bitmap */
589 info->bitmap = kzalloc(PAGE_CACHE_SIZE, GFP_NOFS);
590 spin_lock(&block_group->tree_lock);
591 if (!info->bitmap) {
592 ret = -ENOMEM;
593 goto out;
594 }
595 goto again;
596 }
597
598out:
599 if (info) {
600 if (info->bitmap)
601 kfree(info->bitmap);
602 kfree(info);
603 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400604
605 return ret;
606}
607
Josef Bacik6226cb0a2009-04-03 10:14:18 -0400608int btrfs_add_free_space(struct btrfs_block_group_cache *block_group,
609 u64 offset, u64 bytes)
Josef Bacik0f9dd462008-09-23 13:14:11 -0400610{
Josef Bacik96303082009-07-13 21:29:25 -0400611 struct btrfs_free_space *right_info = NULL;
612 struct btrfs_free_space *left_info = NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400613 struct btrfs_free_space *info = NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400614 int ret = 0;
615
Josef Bacik6226cb0a2009-04-03 10:14:18 -0400616 info = kzalloc(sizeof(struct btrfs_free_space), GFP_NOFS);
617 if (!info)
618 return -ENOMEM;
619
620 info->offset = offset;
621 info->bytes = bytes;
622
623 spin_lock(&block_group->tree_lock);
624
Josef Bacik0f9dd462008-09-23 13:14:11 -0400625 /*
626 * first we want to see if there is free space adjacent to the range we
627 * are adding, if there is remove that struct and add a new one to
628 * cover the entire range
629 */
Josef Bacik96303082009-07-13 21:29:25 -0400630 right_info = tree_search_offset(block_group, offset + bytes, 0, 0);
631 if (right_info && rb_prev(&right_info->offset_index))
632 left_info = rb_entry(rb_prev(&right_info->offset_index),
633 struct btrfs_free_space, offset_index);
634 else
635 left_info = tree_search_offset(block_group, offset - 1, 0, 0);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400636
Josef Bacik96303082009-07-13 21:29:25 -0400637 /*
638 * If there was no extent directly to the left or right of this new
639 * extent then we know we're going to have to allocate a new extent, so
640 * before we do that see if we need to drop this into a bitmap
641 */
642 if ((!left_info || left_info->bitmap) &&
643 (!right_info || right_info->bitmap)) {
644 ret = insert_into_bitmap(block_group, info);
645
646 if (ret < 0) {
647 goto out;
648 } else if (ret) {
649 ret = 0;
650 goto out;
651 }
652 }
653
654 if (right_info && !right_info->bitmap) {
Josef Bacik0f9dd462008-09-23 13:14:11 -0400655 unlink_free_space(block_group, right_info);
Josef Bacik6226cb0a2009-04-03 10:14:18 -0400656 info->bytes += right_info->bytes;
657 kfree(right_info);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400658 }
659
Josef Bacik96303082009-07-13 21:29:25 -0400660 if (left_info && !left_info->bitmap &&
661 left_info->offset + left_info->bytes == offset) {
Josef Bacik0f9dd462008-09-23 13:14:11 -0400662 unlink_free_space(block_group, left_info);
Josef Bacik6226cb0a2009-04-03 10:14:18 -0400663 info->offset = left_info->offset;
664 info->bytes += left_info->bytes;
665 kfree(left_info);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400666 }
667
Josef Bacik0f9dd462008-09-23 13:14:11 -0400668 ret = link_free_space(block_group, info);
669 if (ret)
670 kfree(info);
Josef Bacik96303082009-07-13 21:29:25 -0400671out:
Josef Bacik6226cb0a2009-04-03 10:14:18 -0400672 spin_unlock(&block_group->tree_lock);
673
Josef Bacik0f9dd462008-09-23 13:14:11 -0400674 if (ret) {
Josef Bacik96303082009-07-13 21:29:25 -0400675 printk(KERN_CRIT "btrfs: unable to add free space :%d\n", ret);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -0400676 BUG_ON(ret == -EEXIST);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400677 }
678
Josef Bacik0f9dd462008-09-23 13:14:11 -0400679 return ret;
680}
681
Josef Bacik6226cb0a2009-04-03 10:14:18 -0400682int btrfs_remove_free_space(struct btrfs_block_group_cache *block_group,
683 u64 offset, u64 bytes)
Josef Bacik0f9dd462008-09-23 13:14:11 -0400684{
685 struct btrfs_free_space *info;
Josef Bacik96303082009-07-13 21:29:25 -0400686 struct btrfs_free_space *next_info = NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400687 int ret = 0;
688
Josef Bacik6226cb0a2009-04-03 10:14:18 -0400689 spin_lock(&block_group->tree_lock);
690
Josef Bacik96303082009-07-13 21:29:25 -0400691again:
692 info = tree_search_offset(block_group, offset, 0, 0);
693 if (!info) {
Josef Bacik6606bb92009-07-31 11:03:58 -0400694 /*
695 * oops didn't find an extent that matched the space we wanted
696 * to remove, look for a bitmap instead
697 */
698 info = tree_search_offset(block_group,
699 offset_to_bitmap(block_group, offset),
700 1, 0);
701 if (!info) {
702 WARN_ON(1);
703 goto out_lock;
704 }
Josef Bacik96303082009-07-13 21:29:25 -0400705 }
706
707 if (info->bytes < bytes && rb_next(&info->offset_index)) {
708 u64 end;
709 next_info = rb_entry(rb_next(&info->offset_index),
710 struct btrfs_free_space,
711 offset_index);
712
713 if (next_info->bitmap)
714 end = next_info->offset + BITS_PER_BITMAP *
715 block_group->sectorsize - 1;
716 else
717 end = next_info->offset + next_info->bytes;
718
719 if (next_info->bytes < bytes ||
720 next_info->offset > offset || offset > end) {
721 printk(KERN_CRIT "Found free space at %llu, size %llu,"
722 " trying to use %llu\n",
723 (unsigned long long)info->offset,
724 (unsigned long long)info->bytes,
725 (unsigned long long)bytes);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400726 WARN_ON(1);
727 ret = -EINVAL;
Josef Bacik96303082009-07-13 21:29:25 -0400728 goto out_lock;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400729 }
Josef Bacik96303082009-07-13 21:29:25 -0400730
731 info = next_info;
732 }
733
734 if (info->bytes == bytes) {
Josef Bacik0f9dd462008-09-23 13:14:11 -0400735 unlink_free_space(block_group, info);
Josef Bacik96303082009-07-13 21:29:25 -0400736 if (info->bitmap) {
737 kfree(info->bitmap);
738 block_group->total_bitmaps--;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400739 }
Josef Bacik96303082009-07-13 21:29:25 -0400740 kfree(info);
741 goto out_lock;
742 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400743
Josef Bacik96303082009-07-13 21:29:25 -0400744 if (!info->bitmap && info->offset == offset) {
745 unlink_free_space(block_group, info);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400746 info->offset += bytes;
747 info->bytes -= bytes;
Josef Bacik96303082009-07-13 21:29:25 -0400748 link_free_space(block_group, info);
749 goto out_lock;
750 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400751
Josef Bacik96303082009-07-13 21:29:25 -0400752 if (!info->bitmap && info->offset <= offset &&
753 info->offset + info->bytes >= offset + bytes) {
Chris Mason9b49c9b2008-09-24 11:23:25 -0400754 u64 old_start = info->offset;
755 /*
756 * we're freeing space in the middle of the info,
757 * this can happen during tree log replay
758 *
759 * first unlink the old info and then
760 * insert it again after the hole we're creating
761 */
762 unlink_free_space(block_group, info);
763 if (offset + bytes < info->offset + info->bytes) {
764 u64 old_end = info->offset + info->bytes;
765
766 info->offset = offset + bytes;
767 info->bytes = old_end - info->offset;
768 ret = link_free_space(block_group, info);
Josef Bacik96303082009-07-13 21:29:25 -0400769 WARN_ON(ret);
770 if (ret)
771 goto out_lock;
Chris Mason9b49c9b2008-09-24 11:23:25 -0400772 } else {
773 /* the hole we're creating ends at the end
774 * of the info struct, just free the info
775 */
776 kfree(info);
777 }
Josef Bacik6226cb0a2009-04-03 10:14:18 -0400778 spin_unlock(&block_group->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -0400779
780 /* step two, insert a new info struct to cover
781 * anything before the hole
Chris Mason9b49c9b2008-09-24 11:23:25 -0400782 */
Josef Bacik6226cb0a2009-04-03 10:14:18 -0400783 ret = btrfs_add_free_space(block_group, old_start,
784 offset - old_start);
Josef Bacik96303082009-07-13 21:29:25 -0400785 WARN_ON(ret);
786 goto out;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400787 }
Josef Bacik96303082009-07-13 21:29:25 -0400788
789 ret = remove_from_bitmap(block_group, info, &offset, &bytes);
790 if (ret == -EAGAIN)
791 goto again;
792 BUG_ON(ret);
793out_lock:
794 spin_unlock(&block_group->tree_lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400795out:
Josef Bacik25179202008-10-29 14:49:05 -0400796 return ret;
797}
798
Josef Bacik0f9dd462008-09-23 13:14:11 -0400799void btrfs_dump_free_space(struct btrfs_block_group_cache *block_group,
800 u64 bytes)
801{
802 struct btrfs_free_space *info;
803 struct rb_node *n;
804 int count = 0;
805
806 for (n = rb_first(&block_group->free_space_offset); n; n = rb_next(n)) {
807 info = rb_entry(n, struct btrfs_free_space, offset_index);
808 if (info->bytes >= bytes)
809 count++;
Josef Bacik96303082009-07-13 21:29:25 -0400810 printk(KERN_CRIT "entry offset %llu, bytes %llu, bitmap %s\n",
Joel Becker21380932009-04-21 12:38:29 -0700811 (unsigned long long)info->offset,
Josef Bacik96303082009-07-13 21:29:25 -0400812 (unsigned long long)info->bytes,
813 (info->bitmap) ? "yes" : "no");
Josef Bacik0f9dd462008-09-23 13:14:11 -0400814 }
Josef Bacik96303082009-07-13 21:29:25 -0400815 printk(KERN_INFO "block group has cluster?: %s\n",
816 list_empty(&block_group->cluster_list) ? "no" : "yes");
Josef Bacik0f9dd462008-09-23 13:14:11 -0400817 printk(KERN_INFO "%d blocks of free space at or bigger than bytes is"
818 "\n", count);
819}
820
821u64 btrfs_block_group_free_space(struct btrfs_block_group_cache *block_group)
822{
823 struct btrfs_free_space *info;
824 struct rb_node *n;
825 u64 ret = 0;
826
827 for (n = rb_first(&block_group->free_space_offset); n;
828 n = rb_next(n)) {
829 info = rb_entry(n, struct btrfs_free_space, offset_index);
830 ret += info->bytes;
831 }
832
833 return ret;
834}
835
Chris Masonfa9c0d792009-04-03 09:47:43 -0400836/*
837 * for a given cluster, put all of its extents back into the free
838 * space cache. If the block group passed doesn't match the block group
839 * pointed to by the cluster, someone else raced in and freed the
840 * cluster already. In that case, we just return without changing anything
841 */
842static int
843__btrfs_return_cluster_to_free_space(
844 struct btrfs_block_group_cache *block_group,
845 struct btrfs_free_cluster *cluster)
846{
847 struct btrfs_free_space *entry;
848 struct rb_node *node;
Josef Bacik96303082009-07-13 21:29:25 -0400849 bool bitmap;
Chris Masonfa9c0d792009-04-03 09:47:43 -0400850
851 spin_lock(&cluster->lock);
852 if (cluster->block_group != block_group)
853 goto out;
854
Josef Bacik96303082009-07-13 21:29:25 -0400855 bitmap = cluster->points_to_bitmap;
856 cluster->block_group = NULL;
Chris Masonfa9c0d792009-04-03 09:47:43 -0400857 cluster->window_start = 0;
Josef Bacik96303082009-07-13 21:29:25 -0400858 list_del_init(&cluster->block_group_list);
859 cluster->points_to_bitmap = false;
860
861 if (bitmap)
862 goto out;
863
Chris Masonfa9c0d792009-04-03 09:47:43 -0400864 node = rb_first(&cluster->root);
Josef Bacik96303082009-07-13 21:29:25 -0400865 while (node) {
Chris Masonfa9c0d792009-04-03 09:47:43 -0400866 entry = rb_entry(node, struct btrfs_free_space, offset_index);
867 node = rb_next(&entry->offset_index);
868 rb_erase(&entry->offset_index, &cluster->root);
Josef Bacik96303082009-07-13 21:29:25 -0400869 BUG_ON(entry->bitmap);
870 tree_insert_offset(&block_group->free_space_offset,
871 entry->offset, &entry->offset_index, 0);
Chris Masonfa9c0d792009-04-03 09:47:43 -0400872 }
Chris Masonfa9c0d792009-04-03 09:47:43 -0400873 cluster->root.rb_node = NULL;
Josef Bacik96303082009-07-13 21:29:25 -0400874
Chris Masonfa9c0d792009-04-03 09:47:43 -0400875out:
876 spin_unlock(&cluster->lock);
Josef Bacik96303082009-07-13 21:29:25 -0400877 btrfs_put_block_group(block_group);
Chris Masonfa9c0d792009-04-03 09:47:43 -0400878 return 0;
879}
880
Josef Bacik0f9dd462008-09-23 13:14:11 -0400881void btrfs_remove_free_space_cache(struct btrfs_block_group_cache *block_group)
882{
883 struct btrfs_free_space *info;
884 struct rb_node *node;
Chris Masonfa9c0d792009-04-03 09:47:43 -0400885 struct btrfs_free_cluster *cluster;
Josef Bacik96303082009-07-13 21:29:25 -0400886 struct list_head *head;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400887
Josef Bacik6226cb0a2009-04-03 10:14:18 -0400888 spin_lock(&block_group->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -0400889 while ((head = block_group->cluster_list.next) !=
890 &block_group->cluster_list) {
891 cluster = list_entry(head, struct btrfs_free_cluster,
892 block_group_list);
Chris Masonfa9c0d792009-04-03 09:47:43 -0400893
894 WARN_ON(cluster->block_group != block_group);
895 __btrfs_return_cluster_to_free_space(block_group, cluster);
Josef Bacik96303082009-07-13 21:29:25 -0400896 if (need_resched()) {
897 spin_unlock(&block_group->tree_lock);
898 cond_resched();
899 spin_lock(&block_group->tree_lock);
900 }
Chris Masonfa9c0d792009-04-03 09:47:43 -0400901 }
902
Josef Bacik96303082009-07-13 21:29:25 -0400903 while ((node = rb_last(&block_group->free_space_offset)) != NULL) {
904 info = rb_entry(node, struct btrfs_free_space, offset_index);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400905 unlink_free_space(block_group, info);
Josef Bacik96303082009-07-13 21:29:25 -0400906 if (info->bitmap)
907 kfree(info->bitmap);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400908 kfree(info);
909 if (need_resched()) {
Josef Bacik6226cb0a2009-04-03 10:14:18 -0400910 spin_unlock(&block_group->tree_lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400911 cond_resched();
Josef Bacik6226cb0a2009-04-03 10:14:18 -0400912 spin_lock(&block_group->tree_lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400913 }
914 }
Josef Bacik96303082009-07-13 21:29:25 -0400915
Josef Bacik6226cb0a2009-04-03 10:14:18 -0400916 spin_unlock(&block_group->tree_lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400917}
918
Josef Bacik6226cb0a2009-04-03 10:14:18 -0400919u64 btrfs_find_space_for_alloc(struct btrfs_block_group_cache *block_group,
920 u64 offset, u64 bytes, u64 empty_size)
Josef Bacik0f9dd462008-09-23 13:14:11 -0400921{
Josef Bacik6226cb0a2009-04-03 10:14:18 -0400922 struct btrfs_free_space *entry = NULL;
Josef Bacik96303082009-07-13 21:29:25 -0400923 u64 bytes_search = bytes + empty_size;
Josef Bacik6226cb0a2009-04-03 10:14:18 -0400924 u64 ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400925
Josef Bacik6226cb0a2009-04-03 10:14:18 -0400926 spin_lock(&block_group->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -0400927 entry = find_free_space(block_group, &offset, &bytes_search, 0);
Josef Bacik6226cb0a2009-04-03 10:14:18 -0400928 if (!entry)
Josef Bacik96303082009-07-13 21:29:25 -0400929 goto out;
930
931 ret = offset;
932 if (entry->bitmap) {
Josef Bacik817d52f2009-07-13 21:29:25 -0400933 bitmap_clear_bits(block_group, entry, offset, bytes);
Josef Bacik96303082009-07-13 21:29:25 -0400934 if (!entry->bytes) {
935 unlink_free_space(block_group, entry);
936 kfree(entry->bitmap);
937 kfree(entry);
938 block_group->total_bitmaps--;
939 recalculate_thresholds(block_group);
940 }
941 } else {
Josef Bacik6226cb0a2009-04-03 10:14:18 -0400942 unlink_free_space(block_group, entry);
Josef Bacik6226cb0a2009-04-03 10:14:18 -0400943 entry->offset += bytes;
944 entry->bytes -= bytes;
Josef Bacik6226cb0a2009-04-03 10:14:18 -0400945 if (!entry->bytes)
946 kfree(entry);
947 else
948 link_free_space(block_group, entry);
949 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400950
Josef Bacik96303082009-07-13 21:29:25 -0400951out:
952 spin_unlock(&block_group->tree_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -0400953
Josef Bacik0f9dd462008-09-23 13:14:11 -0400954 return ret;
955}
Chris Masonfa9c0d792009-04-03 09:47:43 -0400956
957/*
958 * given a cluster, put all of its extents back into the free space
959 * cache. If a block group is passed, this function will only free
960 * a cluster that belongs to the passed block group.
961 *
962 * Otherwise, it'll get a reference on the block group pointed to by the
963 * cluster and remove the cluster from it.
964 */
965int btrfs_return_cluster_to_free_space(
966 struct btrfs_block_group_cache *block_group,
967 struct btrfs_free_cluster *cluster)
968{
969 int ret;
970
971 /* first, get a safe pointer to the block group */
972 spin_lock(&cluster->lock);
973 if (!block_group) {
974 block_group = cluster->block_group;
975 if (!block_group) {
976 spin_unlock(&cluster->lock);
977 return 0;
978 }
979 } else if (cluster->block_group != block_group) {
980 /* someone else has already freed it don't redo their work */
981 spin_unlock(&cluster->lock);
982 return 0;
983 }
984 atomic_inc(&block_group->count);
985 spin_unlock(&cluster->lock);
986
987 /* now return any extents the cluster had on it */
988 spin_lock(&block_group->tree_lock);
989 ret = __btrfs_return_cluster_to_free_space(block_group, cluster);
990 spin_unlock(&block_group->tree_lock);
991
992 /* finally drop our ref */
993 btrfs_put_block_group(block_group);
994 return ret;
995}
996
Josef Bacik96303082009-07-13 21:29:25 -0400997static u64 btrfs_alloc_from_bitmap(struct btrfs_block_group_cache *block_group,
998 struct btrfs_free_cluster *cluster,
999 u64 bytes, u64 min_start)
1000{
1001 struct btrfs_free_space *entry;
1002 int err;
1003 u64 search_start = cluster->window_start;
1004 u64 search_bytes = bytes;
1005 u64 ret = 0;
1006
1007 spin_lock(&block_group->tree_lock);
1008 spin_lock(&cluster->lock);
1009
1010 if (!cluster->points_to_bitmap)
1011 goto out;
1012
1013 if (cluster->block_group != block_group)
1014 goto out;
1015
Josef Bacik6606bb92009-07-31 11:03:58 -04001016 /*
1017 * search_start is the beginning of the bitmap, but at some point it may
1018 * be a good idea to point to the actual start of the free area in the
1019 * bitmap, so do the offset_to_bitmap trick anyway, and set bitmap_only
1020 * to 1 to make sure we get the bitmap entry
1021 */
1022 entry = tree_search_offset(block_group,
1023 offset_to_bitmap(block_group, search_start),
1024 1, 0);
Josef Bacik96303082009-07-13 21:29:25 -04001025 if (!entry || !entry->bitmap)
1026 goto out;
1027
1028 search_start = min_start;
1029 search_bytes = bytes;
1030
1031 err = search_bitmap(block_group, entry, &search_start,
1032 &search_bytes);
1033 if (err)
1034 goto out;
1035
1036 ret = search_start;
Josef Bacik817d52f2009-07-13 21:29:25 -04001037 bitmap_clear_bits(block_group, entry, ret, bytes);
Josef Bacik96303082009-07-13 21:29:25 -04001038out:
1039 spin_unlock(&cluster->lock);
1040 spin_unlock(&block_group->tree_lock);
1041
1042 return ret;
1043}
1044
Chris Masonfa9c0d792009-04-03 09:47:43 -04001045/*
1046 * given a cluster, try to allocate 'bytes' from it, returns 0
1047 * if it couldn't find anything suitably large, or a logical disk offset
1048 * if things worked out
1049 */
1050u64 btrfs_alloc_from_cluster(struct btrfs_block_group_cache *block_group,
1051 struct btrfs_free_cluster *cluster, u64 bytes,
1052 u64 min_start)
1053{
1054 struct btrfs_free_space *entry = NULL;
1055 struct rb_node *node;
1056 u64 ret = 0;
1057
Josef Bacik96303082009-07-13 21:29:25 -04001058 if (cluster->points_to_bitmap)
1059 return btrfs_alloc_from_bitmap(block_group, cluster, bytes,
1060 min_start);
1061
Chris Masonfa9c0d792009-04-03 09:47:43 -04001062 spin_lock(&cluster->lock);
1063 if (bytes > cluster->max_size)
1064 goto out;
1065
1066 if (cluster->block_group != block_group)
1067 goto out;
1068
1069 node = rb_first(&cluster->root);
1070 if (!node)
1071 goto out;
1072
1073 entry = rb_entry(node, struct btrfs_free_space, offset_index);
1074
1075 while(1) {
1076 if (entry->bytes < bytes || entry->offset < min_start) {
1077 struct rb_node *node;
1078
1079 node = rb_next(&entry->offset_index);
1080 if (!node)
1081 break;
1082 entry = rb_entry(node, struct btrfs_free_space,
1083 offset_index);
1084 continue;
1085 }
1086 ret = entry->offset;
1087
1088 entry->offset += bytes;
1089 entry->bytes -= bytes;
1090
1091 if (entry->bytes == 0) {
1092 rb_erase(&entry->offset_index, &cluster->root);
1093 kfree(entry);
1094 }
1095 break;
1096 }
1097out:
1098 spin_unlock(&cluster->lock);
Josef Bacik96303082009-07-13 21:29:25 -04001099
Chris Masonfa9c0d792009-04-03 09:47:43 -04001100 return ret;
1101}
1102
Josef Bacik96303082009-07-13 21:29:25 -04001103static int btrfs_bitmap_cluster(struct btrfs_block_group_cache *block_group,
1104 struct btrfs_free_space *entry,
1105 struct btrfs_free_cluster *cluster,
1106 u64 offset, u64 bytes, u64 min_bytes)
1107{
1108 unsigned long next_zero;
1109 unsigned long i;
1110 unsigned long search_bits;
1111 unsigned long total_bits;
1112 unsigned long found_bits;
1113 unsigned long start = 0;
1114 unsigned long total_found = 0;
1115 bool found = false;
1116
1117 i = offset_to_bit(entry->offset, block_group->sectorsize,
1118 max_t(u64, offset, entry->offset));
1119 search_bits = bytes_to_bits(min_bytes, block_group->sectorsize);
1120 total_bits = bytes_to_bits(bytes, block_group->sectorsize);
1121
1122again:
1123 found_bits = 0;
1124 for (i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, i);
1125 i < BITS_PER_BITMAP;
1126 i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, i + 1)) {
1127 next_zero = find_next_zero_bit(entry->bitmap,
1128 BITS_PER_BITMAP, i);
1129 if (next_zero - i >= search_bits) {
1130 found_bits = next_zero - i;
1131 break;
1132 }
1133 i = next_zero;
1134 }
1135
1136 if (!found_bits)
1137 return -1;
1138
1139 if (!found) {
1140 start = i;
1141 found = true;
1142 }
1143
1144 total_found += found_bits;
1145
1146 if (cluster->max_size < found_bits * block_group->sectorsize)
1147 cluster->max_size = found_bits * block_group->sectorsize;
1148
1149 if (total_found < total_bits) {
1150 i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, next_zero);
1151 if (i - start > total_bits * 2) {
1152 total_found = 0;
1153 cluster->max_size = 0;
1154 found = false;
1155 }
1156 goto again;
1157 }
1158
1159 cluster->window_start = start * block_group->sectorsize +
1160 entry->offset;
1161 cluster->points_to_bitmap = true;
1162
1163 return 0;
1164}
1165
Chris Masonfa9c0d792009-04-03 09:47:43 -04001166/*
1167 * here we try to find a cluster of blocks in a block group. The goal
1168 * is to find at least bytes free and up to empty_size + bytes free.
1169 * We might not find them all in one contiguous area.
1170 *
1171 * returns zero and sets up cluster if things worked out, otherwise
1172 * it returns -enospc
1173 */
1174int btrfs_find_space_cluster(struct btrfs_trans_handle *trans,
Chris Mason451d7582009-06-09 20:28:34 -04001175 struct btrfs_root *root,
Chris Masonfa9c0d792009-04-03 09:47:43 -04001176 struct btrfs_block_group_cache *block_group,
1177 struct btrfs_free_cluster *cluster,
1178 u64 offset, u64 bytes, u64 empty_size)
1179{
1180 struct btrfs_free_space *entry = NULL;
1181 struct rb_node *node;
1182 struct btrfs_free_space *next;
Josef Bacik96303082009-07-13 21:29:25 -04001183 struct btrfs_free_space *last = NULL;
Chris Masonfa9c0d792009-04-03 09:47:43 -04001184 u64 min_bytes;
1185 u64 window_start;
1186 u64 window_free;
1187 u64 max_extent = 0;
Josef Bacik96303082009-07-13 21:29:25 -04001188 bool found_bitmap = false;
Chris Masonfa9c0d792009-04-03 09:47:43 -04001189 int ret;
1190
1191 /* for metadata, allow allocates with more holes */
Chris Mason451d7582009-06-09 20:28:34 -04001192 if (btrfs_test_opt(root, SSD_SPREAD)) {
1193 min_bytes = bytes + empty_size;
1194 } else if (block_group->flags & BTRFS_BLOCK_GROUP_METADATA) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04001195 /*
1196 * we want to do larger allocations when we are
1197 * flushing out the delayed refs, it helps prevent
1198 * making more work as we go along.
1199 */
1200 if (trans->transaction->delayed_refs.flushing)
1201 min_bytes = max(bytes, (bytes + empty_size) >> 1);
1202 else
1203 min_bytes = max(bytes, (bytes + empty_size) >> 4);
1204 } else
1205 min_bytes = max(bytes, (bytes + empty_size) >> 2);
1206
1207 spin_lock(&block_group->tree_lock);
1208 spin_lock(&cluster->lock);
1209
1210 /* someone already found a cluster, hooray */
1211 if (cluster->block_group) {
1212 ret = 0;
1213 goto out;
1214 }
1215again:
Josef Bacik96303082009-07-13 21:29:25 -04001216 entry = tree_search_offset(block_group, offset, found_bitmap, 1);
Chris Masonfa9c0d792009-04-03 09:47:43 -04001217 if (!entry) {
1218 ret = -ENOSPC;
1219 goto out;
1220 }
Josef Bacik96303082009-07-13 21:29:25 -04001221
1222 /*
1223 * If found_bitmap is true, we exhausted our search for extent entries,
1224 * and we just want to search all of the bitmaps that we can find, and
1225 * ignore any extent entries we find.
1226 */
1227 while (entry->bitmap || found_bitmap ||
1228 (!entry->bitmap && entry->bytes < min_bytes)) {
1229 struct rb_node *node = rb_next(&entry->offset_index);
1230
1231 if (entry->bitmap && entry->bytes > bytes + empty_size) {
1232 ret = btrfs_bitmap_cluster(block_group, entry, cluster,
1233 offset, bytes + empty_size,
1234 min_bytes);
1235 if (!ret)
1236 goto got_it;
1237 }
1238
1239 if (!node) {
1240 ret = -ENOSPC;
1241 goto out;
1242 }
1243 entry = rb_entry(node, struct btrfs_free_space, offset_index);
1244 }
1245
1246 /*
1247 * We already searched all the extent entries from the passed in offset
1248 * to the end and didn't find enough space for the cluster, and we also
1249 * didn't find any bitmaps that met our criteria, just go ahead and exit
1250 */
1251 if (found_bitmap) {
1252 ret = -ENOSPC;
1253 goto out;
1254 }
1255
1256 cluster->points_to_bitmap = false;
Chris Masonfa9c0d792009-04-03 09:47:43 -04001257 window_start = entry->offset;
1258 window_free = entry->bytes;
1259 last = entry;
1260 max_extent = entry->bytes;
1261
Josef Bacik96303082009-07-13 21:29:25 -04001262 while (1) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04001263 /* out window is just right, lets fill it */
1264 if (window_free >= bytes + empty_size)
1265 break;
1266
1267 node = rb_next(&last->offset_index);
1268 if (!node) {
Josef Bacik96303082009-07-13 21:29:25 -04001269 if (found_bitmap)
1270 goto again;
Chris Masonfa9c0d792009-04-03 09:47:43 -04001271 ret = -ENOSPC;
1272 goto out;
1273 }
1274 next = rb_entry(node, struct btrfs_free_space, offset_index);
1275
1276 /*
Josef Bacik96303082009-07-13 21:29:25 -04001277 * we found a bitmap, so if this search doesn't result in a
1278 * cluster, we know to go and search again for the bitmaps and
1279 * start looking for space there
1280 */
1281 if (next->bitmap) {
1282 if (!found_bitmap)
1283 offset = next->offset;
1284 found_bitmap = true;
1285 last = next;
1286 continue;
1287 }
1288
1289 /*
Chris Masonfa9c0d792009-04-03 09:47:43 -04001290 * we haven't filled the empty size and the window is
1291 * very large. reset and try again
1292 */
Chris Masonc6044802009-06-09 18:35:15 -04001293 if (next->offset - (last->offset + last->bytes) > 128 * 1024 ||
1294 next->offset - window_start > (bytes + empty_size) * 2) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04001295 entry = next;
1296 window_start = entry->offset;
1297 window_free = entry->bytes;
1298 last = entry;
1299 max_extent = 0;
Chris Masonfa9c0d792009-04-03 09:47:43 -04001300 } else {
1301 last = next;
1302 window_free += next->bytes;
1303 if (entry->bytes > max_extent)
1304 max_extent = entry->bytes;
1305 }
1306 }
1307
1308 cluster->window_start = entry->offset;
1309
1310 /*
1311 * now we've found our entries, pull them out of the free space
1312 * cache and put them into the cluster rbtree
1313 *
1314 * The cluster includes an rbtree, but only uses the offset index
1315 * of each free space cache entry.
1316 */
Josef Bacik96303082009-07-13 21:29:25 -04001317 while (1) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04001318 node = rb_next(&entry->offset_index);
Josef Bacik96303082009-07-13 21:29:25 -04001319 if (entry->bitmap && node) {
1320 entry = rb_entry(node, struct btrfs_free_space,
1321 offset_index);
1322 continue;
1323 } else if (entry->bitmap && !node) {
1324 break;
1325 }
1326
1327 rb_erase(&entry->offset_index, &block_group->free_space_offset);
Chris Masonfa9c0d792009-04-03 09:47:43 -04001328 ret = tree_insert_offset(&cluster->root, entry->offset,
Josef Bacik96303082009-07-13 21:29:25 -04001329 &entry->offset_index, 0);
Chris Masonfa9c0d792009-04-03 09:47:43 -04001330 BUG_ON(ret);
1331
1332 if (!node || entry == last)
1333 break;
1334
1335 entry = rb_entry(node, struct btrfs_free_space, offset_index);
1336 }
Josef Bacik96303082009-07-13 21:29:25 -04001337
Chris Masonfa9c0d792009-04-03 09:47:43 -04001338 cluster->max_size = max_extent;
Josef Bacik96303082009-07-13 21:29:25 -04001339got_it:
1340 ret = 0;
Chris Masonfa9c0d792009-04-03 09:47:43 -04001341 atomic_inc(&block_group->count);
1342 list_add_tail(&cluster->block_group_list, &block_group->cluster_list);
1343 cluster->block_group = block_group;
1344out:
1345 spin_unlock(&cluster->lock);
1346 spin_unlock(&block_group->tree_lock);
1347
1348 return ret;
1349}
1350
1351/*
1352 * simple code to zero out a cluster
1353 */
1354void btrfs_init_free_cluster(struct btrfs_free_cluster *cluster)
1355{
1356 spin_lock_init(&cluster->lock);
1357 spin_lock_init(&cluster->refill_lock);
1358 cluster->root.rb_node = NULL;
1359 cluster->max_size = 0;
Josef Bacik96303082009-07-13 21:29:25 -04001360 cluster->points_to_bitmap = false;
Chris Masonfa9c0d792009-04-03 09:47:43 -04001361 INIT_LIST_HEAD(&cluster->block_group_list);
1362 cluster->block_group = NULL;
1363}
1364