blob: 850104f05178c0f6e7aa18ae6cfa11fb91267538 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Josef Bacik96303082009-07-13 21:29:25 -040022#include <linux/math64.h>
Josef Bacik0f9dd462008-09-23 13:14:11 -040023#include "ctree.h"
Chris Masonfa9c0d792009-04-03 09:47:43 -040024#include "free-space-cache.h"
25#include "transaction.h"
Josef Bacik0af3d002010-06-21 14:48:16 -040026#include "disk-io.h"
Chris Masonfa9c0d792009-04-03 09:47:43 -040027
Josef Bacik96303082009-07-13 21:29:25 -040028#define BITS_PER_BITMAP (PAGE_CACHE_SIZE * 8)
29#define MAX_CACHE_BYTES_PER_GIG (32 * 1024)
30
Josef Bacik0cb59c92010-07-02 12:14:14 -040031static void recalculate_thresholds(struct btrfs_block_group_cache
32 *block_group);
33static int link_free_space(struct btrfs_block_group_cache *block_group,
34 struct btrfs_free_space *info);
35
Josef Bacik0af3d002010-06-21 14:48:16 -040036struct inode *lookup_free_space_inode(struct btrfs_root *root,
37 struct btrfs_block_group_cache
38 *block_group, struct btrfs_path *path)
39{
40 struct btrfs_key key;
41 struct btrfs_key location;
42 struct btrfs_disk_key disk_key;
43 struct btrfs_free_space_header *header;
44 struct extent_buffer *leaf;
45 struct inode *inode = NULL;
46 int ret;
47
48 spin_lock(&block_group->lock);
49 if (block_group->inode)
50 inode = igrab(block_group->inode);
51 spin_unlock(&block_group->lock);
52 if (inode)
53 return inode;
54
55 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
56 key.offset = block_group->key.objectid;
57 key.type = 0;
58
59 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
60 if (ret < 0)
61 return ERR_PTR(ret);
62 if (ret > 0) {
63 btrfs_release_path(root, path);
64 return ERR_PTR(-ENOENT);
65 }
66
67 leaf = path->nodes[0];
68 header = btrfs_item_ptr(leaf, path->slots[0],
69 struct btrfs_free_space_header);
70 btrfs_free_space_key(leaf, header, &disk_key);
71 btrfs_disk_key_to_cpu(&location, &disk_key);
72 btrfs_release_path(root, path);
73
74 inode = btrfs_iget(root->fs_info->sb, &location, root, NULL);
75 if (!inode)
76 return ERR_PTR(-ENOENT);
77 if (IS_ERR(inode))
78 return inode;
79 if (is_bad_inode(inode)) {
80 iput(inode);
81 return ERR_PTR(-ENOENT);
82 }
83
84 spin_lock(&block_group->lock);
85 if (!root->fs_info->closing) {
86 block_group->inode = igrab(inode);
87 block_group->iref = 1;
88 }
89 spin_unlock(&block_group->lock);
90
91 return inode;
92}
93
94int create_free_space_inode(struct btrfs_root *root,
95 struct btrfs_trans_handle *trans,
96 struct btrfs_block_group_cache *block_group,
97 struct btrfs_path *path)
98{
99 struct btrfs_key key;
100 struct btrfs_disk_key disk_key;
101 struct btrfs_free_space_header *header;
102 struct btrfs_inode_item *inode_item;
103 struct extent_buffer *leaf;
104 u64 objectid;
105 int ret;
106
107 ret = btrfs_find_free_objectid(trans, root, 0, &objectid);
108 if (ret < 0)
109 return ret;
110
111 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
112 if (ret)
113 return ret;
114
115 leaf = path->nodes[0];
116 inode_item = btrfs_item_ptr(leaf, path->slots[0],
117 struct btrfs_inode_item);
118 btrfs_item_key(leaf, &disk_key, path->slots[0]);
119 memset_extent_buffer(leaf, 0, (unsigned long)inode_item,
120 sizeof(*inode_item));
121 btrfs_set_inode_generation(leaf, inode_item, trans->transid);
122 btrfs_set_inode_size(leaf, inode_item, 0);
123 btrfs_set_inode_nbytes(leaf, inode_item, 0);
124 btrfs_set_inode_uid(leaf, inode_item, 0);
125 btrfs_set_inode_gid(leaf, inode_item, 0);
126 btrfs_set_inode_mode(leaf, inode_item, S_IFREG | 0600);
127 btrfs_set_inode_flags(leaf, inode_item, BTRFS_INODE_NOCOMPRESS |
128 BTRFS_INODE_PREALLOC | BTRFS_INODE_NODATASUM);
129 btrfs_set_inode_nlink(leaf, inode_item, 1);
130 btrfs_set_inode_transid(leaf, inode_item, trans->transid);
131 btrfs_set_inode_block_group(leaf, inode_item,
132 block_group->key.objectid);
133 btrfs_mark_buffer_dirty(leaf);
134 btrfs_release_path(root, path);
135
136 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
137 key.offset = block_group->key.objectid;
138 key.type = 0;
139
140 ret = btrfs_insert_empty_item(trans, root, path, &key,
141 sizeof(struct btrfs_free_space_header));
142 if (ret < 0) {
143 btrfs_release_path(root, path);
144 return ret;
145 }
146 leaf = path->nodes[0];
147 header = btrfs_item_ptr(leaf, path->slots[0],
148 struct btrfs_free_space_header);
149 memset_extent_buffer(leaf, 0, (unsigned long)header, sizeof(*header));
150 btrfs_set_free_space_key(leaf, header, &disk_key);
151 btrfs_mark_buffer_dirty(leaf);
152 btrfs_release_path(root, path);
153
154 return 0;
155}
156
157int btrfs_truncate_free_space_cache(struct btrfs_root *root,
158 struct btrfs_trans_handle *trans,
159 struct btrfs_path *path,
160 struct inode *inode)
161{
162 loff_t oldsize;
163 int ret = 0;
164
165 trans->block_rsv = root->orphan_block_rsv;
166 ret = btrfs_block_rsv_check(trans, root,
167 root->orphan_block_rsv,
168 0, 5);
169 if (ret)
170 return ret;
171
172 oldsize = i_size_read(inode);
173 btrfs_i_size_write(inode, 0);
174 truncate_pagecache(inode, oldsize, 0);
175
176 /*
177 * We don't need an orphan item because truncating the free space cache
178 * will never be split across transactions.
179 */
180 ret = btrfs_truncate_inode_items(trans, root, inode,
181 0, BTRFS_EXTENT_DATA_KEY);
182 if (ret) {
183 WARN_ON(1);
184 return ret;
185 }
186
187 return btrfs_update_inode(trans, root, inode);
188}
189
Josef Bacik9d66e232010-08-25 16:54:15 -0400190static int readahead_cache(struct inode *inode)
191{
192 struct file_ra_state *ra;
193 unsigned long last_index;
194
195 ra = kzalloc(sizeof(*ra), GFP_NOFS);
196 if (!ra)
197 return -ENOMEM;
198
199 file_ra_state_init(ra, inode->i_mapping);
200 last_index = (i_size_read(inode) - 1) >> PAGE_CACHE_SHIFT;
201
202 page_cache_sync_readahead(inode->i_mapping, ra, NULL, 0, last_index);
203
204 kfree(ra);
205
206 return 0;
207}
208
209int load_free_space_cache(struct btrfs_fs_info *fs_info,
210 struct btrfs_block_group_cache *block_group)
211{
212 struct btrfs_root *root = fs_info->tree_root;
213 struct inode *inode;
214 struct btrfs_free_space_header *header;
215 struct extent_buffer *leaf;
216 struct page *page;
217 struct btrfs_path *path;
218 u32 *checksums = NULL, *crc;
219 char *disk_crcs = NULL;
220 struct btrfs_key key;
221 struct list_head bitmaps;
222 u64 num_entries;
223 u64 num_bitmaps;
224 u64 generation;
225 u32 cur_crc = ~(u32)0;
226 pgoff_t index = 0;
227 unsigned long first_page_offset;
228 int num_checksums;
229 int ret = 0;
230
231 /*
232 * If we're unmounting then just return, since this does a search on the
233 * normal root and not the commit root and we could deadlock.
234 */
235 smp_mb();
236 if (fs_info->closing)
237 return 0;
238
239 /*
240 * If this block group has been marked to be cleared for one reason or
241 * another then we can't trust the on disk cache, so just return.
242 */
243 spin_lock(&block_group->lock);
244 if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) {
Josef Bacik9d66e232010-08-25 16:54:15 -0400245 spin_unlock(&block_group->lock);
246 return 0;
247 }
248 spin_unlock(&block_group->lock);
249
250 INIT_LIST_HEAD(&bitmaps);
251
252 path = btrfs_alloc_path();
253 if (!path)
254 return 0;
255
256 inode = lookup_free_space_inode(root, block_group, path);
257 if (IS_ERR(inode)) {
258 btrfs_free_path(path);
259 return 0;
260 }
261
262 /* Nothing in the space cache, goodbye */
263 if (!i_size_read(inode)) {
264 btrfs_free_path(path);
265 goto out;
266 }
267
268 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
269 key.offset = block_group->key.objectid;
270 key.type = 0;
271
272 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
273 if (ret) {
274 btrfs_free_path(path);
275 goto out;
276 }
277
278 leaf = path->nodes[0];
279 header = btrfs_item_ptr(leaf, path->slots[0],
280 struct btrfs_free_space_header);
281 num_entries = btrfs_free_space_entries(leaf, header);
282 num_bitmaps = btrfs_free_space_bitmaps(leaf, header);
283 generation = btrfs_free_space_generation(leaf, header);
284 btrfs_free_path(path);
285
286 if (BTRFS_I(inode)->generation != generation) {
287 printk(KERN_ERR "btrfs: free space inode generation (%llu) did"
288 " not match free space cache generation (%llu) for "
289 "block group %llu\n",
290 (unsigned long long)BTRFS_I(inode)->generation,
291 (unsigned long long)generation,
292 (unsigned long long)block_group->key.objectid);
Josef Bacik2b209822010-12-03 13:17:53 -0500293 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400294 }
295
296 if (!num_entries)
297 goto out;
298
299 /* Setup everything for doing checksumming */
300 num_checksums = i_size_read(inode) / PAGE_CACHE_SIZE;
301 checksums = crc = kzalloc(sizeof(u32) * num_checksums, GFP_NOFS);
302 if (!checksums)
303 goto out;
304 first_page_offset = (sizeof(u32) * num_checksums) + sizeof(u64);
305 disk_crcs = kzalloc(first_page_offset, GFP_NOFS);
306 if (!disk_crcs)
307 goto out;
308
309 ret = readahead_cache(inode);
310 if (ret) {
311 ret = 0;
312 goto out;
313 }
314
315 while (1) {
316 struct btrfs_free_space_entry *entry;
317 struct btrfs_free_space *e;
318 void *addr;
319 unsigned long offset = 0;
320 unsigned long start_offset = 0;
321 int need_loop = 0;
322
323 if (!num_entries && !num_bitmaps)
324 break;
325
326 if (index == 0) {
327 start_offset = first_page_offset;
328 offset = start_offset;
329 }
330
331 page = grab_cache_page(inode->i_mapping, index);
332 if (!page) {
333 ret = 0;
334 goto free_cache;
335 }
336
337 if (!PageUptodate(page)) {
338 btrfs_readpage(NULL, page);
339 lock_page(page);
340 if (!PageUptodate(page)) {
341 unlock_page(page);
342 page_cache_release(page);
343 printk(KERN_ERR "btrfs: error reading free "
344 "space cache: %llu\n",
345 (unsigned long long)
346 block_group->key.objectid);
347 goto free_cache;
348 }
349 }
350 addr = kmap(page);
351
352 if (index == 0) {
353 u64 *gen;
354
355 memcpy(disk_crcs, addr, first_page_offset);
356 gen = addr + (sizeof(u32) * num_checksums);
357 if (*gen != BTRFS_I(inode)->generation) {
358 printk(KERN_ERR "btrfs: space cache generation"
359 " (%llu) does not match inode (%llu) "
360 "for block group %llu\n",
361 (unsigned long long)*gen,
362 (unsigned long long)
363 BTRFS_I(inode)->generation,
364 (unsigned long long)
365 block_group->key.objectid);
366 kunmap(page);
367 unlock_page(page);
368 page_cache_release(page);
369 goto free_cache;
370 }
371 crc = (u32 *)disk_crcs;
372 }
373 entry = addr + start_offset;
374
375 /* First lets check our crc before we do anything fun */
376 cur_crc = ~(u32)0;
377 cur_crc = btrfs_csum_data(root, addr + start_offset, cur_crc,
378 PAGE_CACHE_SIZE - start_offset);
379 btrfs_csum_final(cur_crc, (char *)&cur_crc);
380 if (cur_crc != *crc) {
381 printk(KERN_ERR "btrfs: crc mismatch for page %lu in "
382 "block group %llu\n", index,
383 (unsigned long long)block_group->key.objectid);
384 kunmap(page);
385 unlock_page(page);
386 page_cache_release(page);
387 goto free_cache;
388 }
389 crc++;
390
391 while (1) {
392 if (!num_entries)
393 break;
394
395 need_loop = 1;
396 e = kzalloc(sizeof(struct btrfs_free_space), GFP_NOFS);
397 if (!e) {
398 kunmap(page);
399 unlock_page(page);
400 page_cache_release(page);
401 goto free_cache;
402 }
403
404 e->offset = le64_to_cpu(entry->offset);
405 e->bytes = le64_to_cpu(entry->bytes);
406 if (!e->bytes) {
407 kunmap(page);
408 kfree(e);
409 unlock_page(page);
410 page_cache_release(page);
411 goto free_cache;
412 }
413
414 if (entry->type == BTRFS_FREE_SPACE_EXTENT) {
415 spin_lock(&block_group->tree_lock);
416 ret = link_free_space(block_group, e);
417 spin_unlock(&block_group->tree_lock);
418 BUG_ON(ret);
419 } else {
420 e->bitmap = kzalloc(PAGE_CACHE_SIZE, GFP_NOFS);
421 if (!e->bitmap) {
422 kunmap(page);
423 kfree(e);
424 unlock_page(page);
425 page_cache_release(page);
426 goto free_cache;
427 }
428 spin_lock(&block_group->tree_lock);
429 ret = link_free_space(block_group, e);
430 block_group->total_bitmaps++;
431 recalculate_thresholds(block_group);
432 spin_unlock(&block_group->tree_lock);
433 list_add_tail(&e->list, &bitmaps);
434 }
435
436 num_entries--;
437 offset += sizeof(struct btrfs_free_space_entry);
438 if (offset + sizeof(struct btrfs_free_space_entry) >=
439 PAGE_CACHE_SIZE)
440 break;
441 entry++;
442 }
443
444 /*
445 * We read an entry out of this page, we need to move on to the
446 * next page.
447 */
448 if (need_loop) {
449 kunmap(page);
450 goto next;
451 }
452
453 /*
454 * We add the bitmaps at the end of the entries in order that
455 * the bitmap entries are added to the cache.
456 */
457 e = list_entry(bitmaps.next, struct btrfs_free_space, list);
458 list_del_init(&e->list);
459 memcpy(e->bitmap, addr, PAGE_CACHE_SIZE);
460 kunmap(page);
461 num_bitmaps--;
462next:
463 unlock_page(page);
464 page_cache_release(page);
465 index++;
466 }
467
468 ret = 1;
469out:
470 kfree(checksums);
471 kfree(disk_crcs);
472 iput(inode);
473 return ret;
474
475free_cache:
476 /* This cache is bogus, make sure it gets cleared */
477 spin_lock(&block_group->lock);
478 block_group->disk_cache_state = BTRFS_DC_CLEAR;
479 spin_unlock(&block_group->lock);
480 btrfs_remove_free_space_cache(block_group);
481 goto out;
482}
483
Josef Bacik0cb59c92010-07-02 12:14:14 -0400484int btrfs_write_out_cache(struct btrfs_root *root,
485 struct btrfs_trans_handle *trans,
486 struct btrfs_block_group_cache *block_group,
487 struct btrfs_path *path)
488{
489 struct btrfs_free_space_header *header;
490 struct extent_buffer *leaf;
491 struct inode *inode;
492 struct rb_node *node;
493 struct list_head *pos, *n;
494 struct page *page;
495 struct extent_state *cached_state = NULL;
496 struct list_head bitmap_list;
497 struct btrfs_key key;
498 u64 bytes = 0;
499 u32 *crc, *checksums;
500 pgoff_t index = 0, last_index = 0;
501 unsigned long first_page_offset;
502 int num_checksums;
503 int entries = 0;
504 int bitmaps = 0;
505 int ret = 0;
506
507 root = root->fs_info->tree_root;
508
509 INIT_LIST_HEAD(&bitmap_list);
510
511 spin_lock(&block_group->lock);
512 if (block_group->disk_cache_state < BTRFS_DC_SETUP) {
513 spin_unlock(&block_group->lock);
514 return 0;
515 }
516 spin_unlock(&block_group->lock);
517
518 inode = lookup_free_space_inode(root, block_group, path);
519 if (IS_ERR(inode))
520 return 0;
521
522 if (!i_size_read(inode)) {
523 iput(inode);
524 return 0;
525 }
526
Josef Bacik2b209822010-12-03 13:17:53 -0500527 node = rb_first(&block_group->free_space_offset);
528 if (!node) {
529 iput(inode);
530 return 0;
531 }
532
Josef Bacik0cb59c92010-07-02 12:14:14 -0400533 last_index = (i_size_read(inode) - 1) >> PAGE_CACHE_SHIFT;
534 filemap_write_and_wait(inode->i_mapping);
535 btrfs_wait_ordered_range(inode, inode->i_size &
536 ~(root->sectorsize - 1), (u64)-1);
537
538 /* We need a checksum per page. */
539 num_checksums = i_size_read(inode) / PAGE_CACHE_SIZE;
540 crc = checksums = kzalloc(sizeof(u32) * num_checksums, GFP_NOFS);
541 if (!crc) {
542 iput(inode);
543 return 0;
544 }
545
546 /* Since the first page has all of our checksums and our generation we
547 * need to calculate the offset into the page that we can start writing
548 * our entries.
549 */
550 first_page_offset = (sizeof(u32) * num_checksums) + sizeof(u64);
551
Josef Bacik0cb59c92010-07-02 12:14:14 -0400552 /*
553 * Lock all pages first so we can lock the extent safely.
554 *
555 * NOTE: Because we hold the ref the entire time we're going to write to
556 * the page find_get_page should never fail, so we don't do a check
557 * after find_get_page at this point. Just putting this here so people
558 * know and don't freak out.
559 */
560 while (index <= last_index) {
561 page = grab_cache_page(inode->i_mapping, index);
562 if (!page) {
563 pgoff_t i = 0;
564
565 while (i < index) {
566 page = find_get_page(inode->i_mapping, i);
567 unlock_page(page);
568 page_cache_release(page);
569 page_cache_release(page);
570 i++;
571 }
572 goto out_free;
573 }
574 index++;
575 }
576
577 index = 0;
578 lock_extent_bits(&BTRFS_I(inode)->io_tree, 0, i_size_read(inode) - 1,
579 0, &cached_state, GFP_NOFS);
580
581 /* Write out the extent entries */
582 do {
583 struct btrfs_free_space_entry *entry;
584 void *addr;
585 unsigned long offset = 0;
586 unsigned long start_offset = 0;
587
588 if (index == 0) {
589 start_offset = first_page_offset;
590 offset = start_offset;
591 }
592
593 page = find_get_page(inode->i_mapping, index);
594
595 addr = kmap(page);
596 entry = addr + start_offset;
597
598 memset(addr, 0, PAGE_CACHE_SIZE);
599 while (1) {
600 struct btrfs_free_space *e;
601
602 e = rb_entry(node, struct btrfs_free_space, offset_index);
603 entries++;
604
605 entry->offset = cpu_to_le64(e->offset);
606 entry->bytes = cpu_to_le64(e->bytes);
607 if (e->bitmap) {
608 entry->type = BTRFS_FREE_SPACE_BITMAP;
609 list_add_tail(&e->list, &bitmap_list);
610 bitmaps++;
611 } else {
612 entry->type = BTRFS_FREE_SPACE_EXTENT;
613 }
614 node = rb_next(node);
615 if (!node)
616 break;
617 offset += sizeof(struct btrfs_free_space_entry);
618 if (offset + sizeof(struct btrfs_free_space_entry) >=
619 PAGE_CACHE_SIZE)
620 break;
621 entry++;
622 }
623 *crc = ~(u32)0;
624 *crc = btrfs_csum_data(root, addr + start_offset, *crc,
625 PAGE_CACHE_SIZE - start_offset);
626 kunmap(page);
627
628 btrfs_csum_final(*crc, (char *)crc);
629 crc++;
630
631 bytes += PAGE_CACHE_SIZE;
632
633 ClearPageChecked(page);
634 set_page_extent_mapped(page);
635 SetPageUptodate(page);
636 set_page_dirty(page);
637
638 /*
639 * We need to release our reference we got for grab_cache_page,
640 * except for the first page which will hold our checksums, we
641 * do that below.
642 */
643 if (index != 0) {
644 unlock_page(page);
645 page_cache_release(page);
646 }
647
648 page_cache_release(page);
649
650 index++;
651 } while (node);
652
653 /* Write out the bitmaps */
654 list_for_each_safe(pos, n, &bitmap_list) {
655 void *addr;
656 struct btrfs_free_space *entry =
657 list_entry(pos, struct btrfs_free_space, list);
658
659 page = find_get_page(inode->i_mapping, index);
660
661 addr = kmap(page);
662 memcpy(addr, entry->bitmap, PAGE_CACHE_SIZE);
663 *crc = ~(u32)0;
664 *crc = btrfs_csum_data(root, addr, *crc, PAGE_CACHE_SIZE);
665 kunmap(page);
666 btrfs_csum_final(*crc, (char *)crc);
667 crc++;
668 bytes += PAGE_CACHE_SIZE;
669
670 ClearPageChecked(page);
671 set_page_extent_mapped(page);
672 SetPageUptodate(page);
673 set_page_dirty(page);
674 unlock_page(page);
675 page_cache_release(page);
676 page_cache_release(page);
677 list_del_init(&entry->list);
678 index++;
679 }
680
681 /* Zero out the rest of the pages just to make sure */
682 while (index <= last_index) {
683 void *addr;
684
685 page = find_get_page(inode->i_mapping, index);
686
687 addr = kmap(page);
688 memset(addr, 0, PAGE_CACHE_SIZE);
689 kunmap(page);
690 ClearPageChecked(page);
691 set_page_extent_mapped(page);
692 SetPageUptodate(page);
693 set_page_dirty(page);
694 unlock_page(page);
695 page_cache_release(page);
696 page_cache_release(page);
697 bytes += PAGE_CACHE_SIZE;
698 index++;
699 }
700
701 btrfs_set_extent_delalloc(inode, 0, bytes - 1, &cached_state);
702
703 /* Write the checksums and trans id to the first page */
704 {
705 void *addr;
706 u64 *gen;
707
708 page = find_get_page(inode->i_mapping, 0);
709
710 addr = kmap(page);
711 memcpy(addr, checksums, sizeof(u32) * num_checksums);
712 gen = addr + (sizeof(u32) * num_checksums);
713 *gen = trans->transid;
714 kunmap(page);
715 ClearPageChecked(page);
716 set_page_extent_mapped(page);
717 SetPageUptodate(page);
718 set_page_dirty(page);
719 unlock_page(page);
720 page_cache_release(page);
721 page_cache_release(page);
722 }
723 BTRFS_I(inode)->generation = trans->transid;
724
725 unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
726 i_size_read(inode) - 1, &cached_state, GFP_NOFS);
727
728 filemap_write_and_wait(inode->i_mapping);
729
730 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
731 key.offset = block_group->key.objectid;
732 key.type = 0;
733
734 ret = btrfs_search_slot(trans, root, &key, path, 1, 1);
735 if (ret < 0) {
736 ret = 0;
737 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, bytes - 1,
738 EXTENT_DIRTY | EXTENT_DELALLOC |
739 EXTENT_DO_ACCOUNTING, 0, 0, NULL, GFP_NOFS);
740 goto out_free;
741 }
742 leaf = path->nodes[0];
743 if (ret > 0) {
744 struct btrfs_key found_key;
745 BUG_ON(!path->slots[0]);
746 path->slots[0]--;
747 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
748 if (found_key.objectid != BTRFS_FREE_SPACE_OBJECTID ||
749 found_key.offset != block_group->key.objectid) {
750 ret = 0;
751 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, bytes - 1,
752 EXTENT_DIRTY | EXTENT_DELALLOC |
753 EXTENT_DO_ACCOUNTING, 0, 0, NULL,
754 GFP_NOFS);
755 btrfs_release_path(root, path);
756 goto out_free;
757 }
758 }
759 header = btrfs_item_ptr(leaf, path->slots[0],
760 struct btrfs_free_space_header);
761 btrfs_set_free_space_entries(leaf, header, entries);
762 btrfs_set_free_space_bitmaps(leaf, header, bitmaps);
763 btrfs_set_free_space_generation(leaf, header, trans->transid);
764 btrfs_mark_buffer_dirty(leaf);
765 btrfs_release_path(root, path);
766
767 ret = 1;
768
769out_free:
770 if (ret == 0) {
771 invalidate_inode_pages2_range(inode->i_mapping, 0, index);
772 spin_lock(&block_group->lock);
773 block_group->disk_cache_state = BTRFS_DC_ERROR;
774 spin_unlock(&block_group->lock);
775 BTRFS_I(inode)->generation = 0;
776 }
777 kfree(checksums);
778 btrfs_update_inode(trans, root, inode);
779 iput(inode);
780 return ret;
781}
782
Josef Bacik96303082009-07-13 21:29:25 -0400783static inline unsigned long offset_to_bit(u64 bitmap_start, u64 sectorsize,
784 u64 offset)
785{
786 BUG_ON(offset < bitmap_start);
787 offset -= bitmap_start;
788 return (unsigned long)(div64_u64(offset, sectorsize));
789}
790
791static inline unsigned long bytes_to_bits(u64 bytes, u64 sectorsize)
792{
793 return (unsigned long)(div64_u64(bytes, sectorsize));
794}
795
796static inline u64 offset_to_bitmap(struct btrfs_block_group_cache *block_group,
797 u64 offset)
798{
799 u64 bitmap_start;
800 u64 bytes_per_bitmap;
801
802 bytes_per_bitmap = BITS_PER_BITMAP * block_group->sectorsize;
803 bitmap_start = offset - block_group->key.objectid;
804 bitmap_start = div64_u64(bitmap_start, bytes_per_bitmap);
805 bitmap_start *= bytes_per_bitmap;
806 bitmap_start += block_group->key.objectid;
807
808 return bitmap_start;
809}
Josef Bacik0f9dd462008-09-23 13:14:11 -0400810
811static int tree_insert_offset(struct rb_root *root, u64 offset,
Josef Bacik96303082009-07-13 21:29:25 -0400812 struct rb_node *node, int bitmap)
Josef Bacik0f9dd462008-09-23 13:14:11 -0400813{
814 struct rb_node **p = &root->rb_node;
815 struct rb_node *parent = NULL;
816 struct btrfs_free_space *info;
817
818 while (*p) {
819 parent = *p;
820 info = rb_entry(parent, struct btrfs_free_space, offset_index);
821
Josef Bacik96303082009-07-13 21:29:25 -0400822 if (offset < info->offset) {
Josef Bacik0f9dd462008-09-23 13:14:11 -0400823 p = &(*p)->rb_left;
Josef Bacik96303082009-07-13 21:29:25 -0400824 } else if (offset > info->offset) {
Josef Bacik0f9dd462008-09-23 13:14:11 -0400825 p = &(*p)->rb_right;
Josef Bacik96303082009-07-13 21:29:25 -0400826 } else {
827 /*
828 * we could have a bitmap entry and an extent entry
829 * share the same offset. If this is the case, we want
830 * the extent entry to always be found first if we do a
831 * linear search through the tree, since we want to have
832 * the quickest allocation time, and allocating from an
833 * extent is faster than allocating from a bitmap. So
834 * if we're inserting a bitmap and we find an entry at
835 * this offset, we want to go right, or after this entry
836 * logically. If we are inserting an extent and we've
837 * found a bitmap, we want to go left, or before
838 * logically.
839 */
840 if (bitmap) {
841 WARN_ON(info->bitmap);
842 p = &(*p)->rb_right;
843 } else {
844 WARN_ON(!info->bitmap);
845 p = &(*p)->rb_left;
846 }
847 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400848 }
849
850 rb_link_node(node, parent, p);
851 rb_insert_color(node, root);
852
853 return 0;
854}
855
856/*
Josef Bacik70cb0742009-04-03 10:14:19 -0400857 * searches the tree for the given offset.
858 *
Josef Bacik96303082009-07-13 21:29:25 -0400859 * fuzzy - If this is set, then we are trying to make an allocation, and we just
860 * want a section that has at least bytes size and comes at or after the given
861 * offset.
Josef Bacik0f9dd462008-09-23 13:14:11 -0400862 */
Josef Bacik96303082009-07-13 21:29:25 -0400863static struct btrfs_free_space *
864tree_search_offset(struct btrfs_block_group_cache *block_group,
865 u64 offset, int bitmap_only, int fuzzy)
Josef Bacik0f9dd462008-09-23 13:14:11 -0400866{
Josef Bacik96303082009-07-13 21:29:25 -0400867 struct rb_node *n = block_group->free_space_offset.rb_node;
868 struct btrfs_free_space *entry, *prev = NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400869
Josef Bacik96303082009-07-13 21:29:25 -0400870 /* find entry that is closest to the 'offset' */
871 while (1) {
872 if (!n) {
873 entry = NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400874 break;
875 }
Josef Bacik96303082009-07-13 21:29:25 -0400876
877 entry = rb_entry(n, struct btrfs_free_space, offset_index);
878 prev = entry;
879
880 if (offset < entry->offset)
881 n = n->rb_left;
882 else if (offset > entry->offset)
883 n = n->rb_right;
884 else
885 break;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400886 }
887
Josef Bacik96303082009-07-13 21:29:25 -0400888 if (bitmap_only) {
889 if (!entry)
890 return NULL;
891 if (entry->bitmap)
892 return entry;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400893
Josef Bacik96303082009-07-13 21:29:25 -0400894 /*
895 * bitmap entry and extent entry may share same offset,
896 * in that case, bitmap entry comes after extent entry.
897 */
898 n = rb_next(n);
899 if (!n)
900 return NULL;
901 entry = rb_entry(n, struct btrfs_free_space, offset_index);
902 if (entry->offset != offset)
903 return NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400904
Josef Bacik96303082009-07-13 21:29:25 -0400905 WARN_ON(!entry->bitmap);
906 return entry;
907 } else if (entry) {
908 if (entry->bitmap) {
Josef Bacik0f9dd462008-09-23 13:14:11 -0400909 /*
Josef Bacik96303082009-07-13 21:29:25 -0400910 * if previous extent entry covers the offset,
911 * we should return it instead of the bitmap entry
Josef Bacik0f9dd462008-09-23 13:14:11 -0400912 */
Josef Bacik96303082009-07-13 21:29:25 -0400913 n = &entry->offset_index;
914 while (1) {
915 n = rb_prev(n);
916 if (!n)
917 break;
918 prev = rb_entry(n, struct btrfs_free_space,
919 offset_index);
920 if (!prev->bitmap) {
921 if (prev->offset + prev->bytes > offset)
922 entry = prev;
923 break;
924 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400925 }
Josef Bacik96303082009-07-13 21:29:25 -0400926 }
927 return entry;
928 }
929
930 if (!prev)
931 return NULL;
932
933 /* find last entry before the 'offset' */
934 entry = prev;
935 if (entry->offset > offset) {
936 n = rb_prev(&entry->offset_index);
937 if (n) {
938 entry = rb_entry(n, struct btrfs_free_space,
939 offset_index);
940 BUG_ON(entry->offset > offset);
Josef Bacik0f9dd462008-09-23 13:14:11 -0400941 } else {
Josef Bacik96303082009-07-13 21:29:25 -0400942 if (fuzzy)
943 return entry;
944 else
945 return NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400946 }
947 }
948
Josef Bacik96303082009-07-13 21:29:25 -0400949 if (entry->bitmap) {
950 n = &entry->offset_index;
951 while (1) {
952 n = rb_prev(n);
953 if (!n)
954 break;
955 prev = rb_entry(n, struct btrfs_free_space,
956 offset_index);
957 if (!prev->bitmap) {
958 if (prev->offset + prev->bytes > offset)
959 return prev;
960 break;
961 }
962 }
963 if (entry->offset + BITS_PER_BITMAP *
964 block_group->sectorsize > offset)
965 return entry;
966 } else if (entry->offset + entry->bytes > offset)
967 return entry;
968
969 if (!fuzzy)
970 return NULL;
971
972 while (1) {
973 if (entry->bitmap) {
974 if (entry->offset + BITS_PER_BITMAP *
975 block_group->sectorsize > offset)
976 break;
977 } else {
978 if (entry->offset + entry->bytes > offset)
979 break;
980 }
981
982 n = rb_next(&entry->offset_index);
983 if (!n)
984 return NULL;
985 entry = rb_entry(n, struct btrfs_free_space, offset_index);
986 }
987 return entry;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400988}
989
990static void unlink_free_space(struct btrfs_block_group_cache *block_group,
991 struct btrfs_free_space *info)
992{
993 rb_erase(&info->offset_index, &block_group->free_space_offset);
Josef Bacik96303082009-07-13 21:29:25 -0400994 block_group->free_extents--;
Josef Bacik817d52f2009-07-13 21:29:25 -0400995 block_group->free_space -= info->bytes;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400996}
997
998static int link_free_space(struct btrfs_block_group_cache *block_group,
999 struct btrfs_free_space *info)
1000{
1001 int ret = 0;
1002
Josef Bacik96303082009-07-13 21:29:25 -04001003 BUG_ON(!info->bitmap && !info->bytes);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001004 ret = tree_insert_offset(&block_group->free_space_offset, info->offset,
Josef Bacik96303082009-07-13 21:29:25 -04001005 &info->offset_index, (info->bitmap != NULL));
Josef Bacik0f9dd462008-09-23 13:14:11 -04001006 if (ret)
1007 return ret;
1008
Josef Bacik817d52f2009-07-13 21:29:25 -04001009 block_group->free_space += info->bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001010 block_group->free_extents++;
1011 return ret;
1012}
1013
1014static void recalculate_thresholds(struct btrfs_block_group_cache *block_group)
1015{
Josef Bacik25891f72009-09-11 16:11:20 -04001016 u64 max_bytes;
1017 u64 bitmap_bytes;
1018 u64 extent_bytes;
Li Zefan8eb2d822010-11-09 14:48:01 +08001019 u64 size = block_group->key.offset;
Josef Bacik96303082009-07-13 21:29:25 -04001020
1021 /*
1022 * The goal is to keep the total amount of memory used per 1gb of space
1023 * at or below 32k, so we need to adjust how much memory we allow to be
1024 * used by extent based free space tracking
1025 */
Li Zefan8eb2d822010-11-09 14:48:01 +08001026 if (size < 1024 * 1024 * 1024)
1027 max_bytes = MAX_CACHE_BYTES_PER_GIG;
1028 else
1029 max_bytes = MAX_CACHE_BYTES_PER_GIG *
1030 div64_u64(size, 1024 * 1024 * 1024);
Josef Bacik96303082009-07-13 21:29:25 -04001031
Josef Bacik25891f72009-09-11 16:11:20 -04001032 /*
1033 * we want to account for 1 more bitmap than what we have so we can make
1034 * sure we don't go over our overall goal of MAX_CACHE_BYTES_PER_GIG as
1035 * we add more bitmaps.
1036 */
1037 bitmap_bytes = (block_group->total_bitmaps + 1) * PAGE_CACHE_SIZE;
Josef Bacik96303082009-07-13 21:29:25 -04001038
Josef Bacik25891f72009-09-11 16:11:20 -04001039 if (bitmap_bytes >= max_bytes) {
1040 block_group->extents_thresh = 0;
1041 return;
Josef Bacik96303082009-07-13 21:29:25 -04001042 }
Josef Bacik25891f72009-09-11 16:11:20 -04001043
1044 /*
1045 * we want the extent entry threshold to always be at most 1/2 the maxw
1046 * bytes we can have, or whatever is less than that.
1047 */
1048 extent_bytes = max_bytes - bitmap_bytes;
1049 extent_bytes = min_t(u64, extent_bytes, div64_u64(max_bytes, 2));
1050
1051 block_group->extents_thresh =
1052 div64_u64(extent_bytes, (sizeof(struct btrfs_free_space)));
Josef Bacik96303082009-07-13 21:29:25 -04001053}
1054
Josef Bacik817d52f2009-07-13 21:29:25 -04001055static void bitmap_clear_bits(struct btrfs_block_group_cache *block_group,
1056 struct btrfs_free_space *info, u64 offset,
1057 u64 bytes)
Josef Bacik96303082009-07-13 21:29:25 -04001058{
1059 unsigned long start, end;
1060 unsigned long i;
1061
Josef Bacik817d52f2009-07-13 21:29:25 -04001062 start = offset_to_bit(info->offset, block_group->sectorsize, offset);
1063 end = start + bytes_to_bits(bytes, block_group->sectorsize);
Josef Bacik96303082009-07-13 21:29:25 -04001064 BUG_ON(end > BITS_PER_BITMAP);
1065
1066 for (i = start; i < end; i++)
1067 clear_bit(i, info->bitmap);
1068
1069 info->bytes -= bytes;
Josef Bacik817d52f2009-07-13 21:29:25 -04001070 block_group->free_space -= bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001071}
1072
Josef Bacik817d52f2009-07-13 21:29:25 -04001073static void bitmap_set_bits(struct btrfs_block_group_cache *block_group,
1074 struct btrfs_free_space *info, u64 offset,
1075 u64 bytes)
Josef Bacik96303082009-07-13 21:29:25 -04001076{
1077 unsigned long start, end;
1078 unsigned long i;
1079
Josef Bacik817d52f2009-07-13 21:29:25 -04001080 start = offset_to_bit(info->offset, block_group->sectorsize, offset);
1081 end = start + bytes_to_bits(bytes, block_group->sectorsize);
Josef Bacik96303082009-07-13 21:29:25 -04001082 BUG_ON(end > BITS_PER_BITMAP);
1083
1084 for (i = start; i < end; i++)
1085 set_bit(i, info->bitmap);
1086
1087 info->bytes += bytes;
Josef Bacik817d52f2009-07-13 21:29:25 -04001088 block_group->free_space += bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001089}
1090
1091static int search_bitmap(struct btrfs_block_group_cache *block_group,
1092 struct btrfs_free_space *bitmap_info, u64 *offset,
1093 u64 *bytes)
1094{
1095 unsigned long found_bits = 0;
1096 unsigned long bits, i;
1097 unsigned long next_zero;
1098
1099 i = offset_to_bit(bitmap_info->offset, block_group->sectorsize,
1100 max_t(u64, *offset, bitmap_info->offset));
1101 bits = bytes_to_bits(*bytes, block_group->sectorsize);
1102
1103 for (i = find_next_bit(bitmap_info->bitmap, BITS_PER_BITMAP, i);
1104 i < BITS_PER_BITMAP;
1105 i = find_next_bit(bitmap_info->bitmap, BITS_PER_BITMAP, i + 1)) {
1106 next_zero = find_next_zero_bit(bitmap_info->bitmap,
1107 BITS_PER_BITMAP, i);
1108 if ((next_zero - i) >= bits) {
1109 found_bits = next_zero - i;
1110 break;
1111 }
1112 i = next_zero;
1113 }
1114
1115 if (found_bits) {
1116 *offset = (u64)(i * block_group->sectorsize) +
1117 bitmap_info->offset;
1118 *bytes = (u64)(found_bits) * block_group->sectorsize;
1119 return 0;
1120 }
1121
1122 return -1;
1123}
1124
1125static struct btrfs_free_space *find_free_space(struct btrfs_block_group_cache
1126 *block_group, u64 *offset,
1127 u64 *bytes, int debug)
1128{
1129 struct btrfs_free_space *entry;
1130 struct rb_node *node;
1131 int ret;
1132
1133 if (!block_group->free_space_offset.rb_node)
1134 return NULL;
1135
1136 entry = tree_search_offset(block_group,
1137 offset_to_bitmap(block_group, *offset),
1138 0, 1);
1139 if (!entry)
1140 return NULL;
1141
1142 for (node = &entry->offset_index; node; node = rb_next(node)) {
1143 entry = rb_entry(node, struct btrfs_free_space, offset_index);
1144 if (entry->bytes < *bytes)
1145 continue;
1146
1147 if (entry->bitmap) {
1148 ret = search_bitmap(block_group, entry, offset, bytes);
1149 if (!ret)
1150 return entry;
1151 continue;
1152 }
1153
1154 *offset = entry->offset;
1155 *bytes = entry->bytes;
1156 return entry;
1157 }
1158
1159 return NULL;
1160}
1161
1162static void add_new_bitmap(struct btrfs_block_group_cache *block_group,
1163 struct btrfs_free_space *info, u64 offset)
1164{
1165 u64 bytes_per_bg = BITS_PER_BITMAP * block_group->sectorsize;
1166 int max_bitmaps = (int)div64_u64(block_group->key.offset +
1167 bytes_per_bg - 1, bytes_per_bg);
1168 BUG_ON(block_group->total_bitmaps >= max_bitmaps);
1169
1170 info->offset = offset_to_bitmap(block_group, offset);
Josef Bacikf019f422009-09-11 16:11:20 -04001171 info->bytes = 0;
Josef Bacik96303082009-07-13 21:29:25 -04001172 link_free_space(block_group, info);
1173 block_group->total_bitmaps++;
1174
1175 recalculate_thresholds(block_group);
1176}
1177
Li Zefanedf6e2d2010-11-09 14:50:07 +08001178static void free_bitmap(struct btrfs_block_group_cache *block_group,
1179 struct btrfs_free_space *bitmap_info)
1180{
1181 unlink_free_space(block_group, bitmap_info);
1182 kfree(bitmap_info->bitmap);
1183 kfree(bitmap_info);
1184 block_group->total_bitmaps--;
1185 recalculate_thresholds(block_group);
1186}
1187
Josef Bacik96303082009-07-13 21:29:25 -04001188static noinline int remove_from_bitmap(struct btrfs_block_group_cache *block_group,
1189 struct btrfs_free_space *bitmap_info,
1190 u64 *offset, u64 *bytes)
1191{
1192 u64 end;
Josef Bacik6606bb92009-07-31 11:03:58 -04001193 u64 search_start, search_bytes;
1194 int ret;
Josef Bacik96303082009-07-13 21:29:25 -04001195
1196again:
1197 end = bitmap_info->offset +
1198 (u64)(BITS_PER_BITMAP * block_group->sectorsize) - 1;
1199
Josef Bacik6606bb92009-07-31 11:03:58 -04001200 /*
1201 * XXX - this can go away after a few releases.
1202 *
1203 * since the only user of btrfs_remove_free_space is the tree logging
1204 * stuff, and the only way to test that is under crash conditions, we
1205 * want to have this debug stuff here just in case somethings not
1206 * working. Search the bitmap for the space we are trying to use to
1207 * make sure its actually there. If its not there then we need to stop
1208 * because something has gone wrong.
1209 */
1210 search_start = *offset;
1211 search_bytes = *bytes;
1212 ret = search_bitmap(block_group, bitmap_info, &search_start,
1213 &search_bytes);
1214 BUG_ON(ret < 0 || search_start != *offset);
1215
Josef Bacik96303082009-07-13 21:29:25 -04001216 if (*offset > bitmap_info->offset && *offset + *bytes > end) {
Josef Bacik817d52f2009-07-13 21:29:25 -04001217 bitmap_clear_bits(block_group, bitmap_info, *offset,
1218 end - *offset + 1);
Josef Bacik96303082009-07-13 21:29:25 -04001219 *bytes -= end - *offset + 1;
1220 *offset = end + 1;
1221 } else if (*offset >= bitmap_info->offset && *offset + *bytes <= end) {
Josef Bacik817d52f2009-07-13 21:29:25 -04001222 bitmap_clear_bits(block_group, bitmap_info, *offset, *bytes);
Josef Bacik96303082009-07-13 21:29:25 -04001223 *bytes = 0;
1224 }
1225
1226 if (*bytes) {
Josef Bacik6606bb92009-07-31 11:03:58 -04001227 struct rb_node *next = rb_next(&bitmap_info->offset_index);
Li Zefanedf6e2d2010-11-09 14:50:07 +08001228 if (!bitmap_info->bytes)
1229 free_bitmap(block_group, bitmap_info);
Josef Bacik96303082009-07-13 21:29:25 -04001230
Josef Bacik6606bb92009-07-31 11:03:58 -04001231 /*
1232 * no entry after this bitmap, but we still have bytes to
1233 * remove, so something has gone wrong.
1234 */
1235 if (!next)
Josef Bacik96303082009-07-13 21:29:25 -04001236 return -EINVAL;
1237
Josef Bacik6606bb92009-07-31 11:03:58 -04001238 bitmap_info = rb_entry(next, struct btrfs_free_space,
1239 offset_index);
1240
1241 /*
1242 * if the next entry isn't a bitmap we need to return to let the
1243 * extent stuff do its work.
1244 */
Josef Bacik96303082009-07-13 21:29:25 -04001245 if (!bitmap_info->bitmap)
1246 return -EAGAIN;
1247
Josef Bacik6606bb92009-07-31 11:03:58 -04001248 /*
1249 * Ok the next item is a bitmap, but it may not actually hold
1250 * the information for the rest of this free space stuff, so
1251 * look for it, and if we don't find it return so we can try
1252 * everything over again.
1253 */
1254 search_start = *offset;
1255 search_bytes = *bytes;
1256 ret = search_bitmap(block_group, bitmap_info, &search_start,
1257 &search_bytes);
1258 if (ret < 0 || search_start != *offset)
1259 return -EAGAIN;
1260
Josef Bacik96303082009-07-13 21:29:25 -04001261 goto again;
Li Zefanedf6e2d2010-11-09 14:50:07 +08001262 } else if (!bitmap_info->bytes)
1263 free_bitmap(block_group, bitmap_info);
Josef Bacik96303082009-07-13 21:29:25 -04001264
1265 return 0;
1266}
1267
1268static int insert_into_bitmap(struct btrfs_block_group_cache *block_group,
1269 struct btrfs_free_space *info)
1270{
1271 struct btrfs_free_space *bitmap_info;
1272 int added = 0;
1273 u64 bytes, offset, end;
1274 int ret;
1275
1276 /*
1277 * If we are below the extents threshold then we can add this as an
1278 * extent, and don't have to deal with the bitmap
1279 */
1280 if (block_group->free_extents < block_group->extents_thresh &&
1281 info->bytes > block_group->sectorsize * 4)
1282 return 0;
1283
1284 /*
1285 * some block groups are so tiny they can't be enveloped by a bitmap, so
1286 * don't even bother to create a bitmap for this
1287 */
1288 if (BITS_PER_BITMAP * block_group->sectorsize >
1289 block_group->key.offset)
1290 return 0;
1291
1292 bytes = info->bytes;
1293 offset = info->offset;
1294
1295again:
1296 bitmap_info = tree_search_offset(block_group,
1297 offset_to_bitmap(block_group, offset),
1298 1, 0);
1299 if (!bitmap_info) {
1300 BUG_ON(added);
1301 goto new_bitmap;
1302 }
1303
1304 end = bitmap_info->offset +
1305 (u64)(BITS_PER_BITMAP * block_group->sectorsize);
1306
1307 if (offset >= bitmap_info->offset && offset + bytes > end) {
Josef Bacik817d52f2009-07-13 21:29:25 -04001308 bitmap_set_bits(block_group, bitmap_info, offset,
1309 end - offset);
Josef Bacik96303082009-07-13 21:29:25 -04001310 bytes -= end - offset;
1311 offset = end;
1312 added = 0;
1313 } else if (offset >= bitmap_info->offset && offset + bytes <= end) {
Josef Bacik817d52f2009-07-13 21:29:25 -04001314 bitmap_set_bits(block_group, bitmap_info, offset, bytes);
Josef Bacik96303082009-07-13 21:29:25 -04001315 bytes = 0;
1316 } else {
1317 BUG();
1318 }
1319
1320 if (!bytes) {
1321 ret = 1;
1322 goto out;
1323 } else
1324 goto again;
1325
1326new_bitmap:
1327 if (info && info->bitmap) {
1328 add_new_bitmap(block_group, info, offset);
1329 added = 1;
1330 info = NULL;
1331 goto again;
1332 } else {
1333 spin_unlock(&block_group->tree_lock);
1334
1335 /* no pre-allocated info, allocate a new one */
1336 if (!info) {
1337 info = kzalloc(sizeof(struct btrfs_free_space),
1338 GFP_NOFS);
1339 if (!info) {
1340 spin_lock(&block_group->tree_lock);
1341 ret = -ENOMEM;
1342 goto out;
1343 }
1344 }
1345
1346 /* allocate the bitmap */
1347 info->bitmap = kzalloc(PAGE_CACHE_SIZE, GFP_NOFS);
1348 spin_lock(&block_group->tree_lock);
1349 if (!info->bitmap) {
1350 ret = -ENOMEM;
1351 goto out;
1352 }
1353 goto again;
1354 }
1355
1356out:
1357 if (info) {
1358 if (info->bitmap)
1359 kfree(info->bitmap);
1360 kfree(info);
1361 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04001362
1363 return ret;
1364}
1365
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001366int btrfs_add_free_space(struct btrfs_block_group_cache *block_group,
1367 u64 offset, u64 bytes)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001368{
Josef Bacik96303082009-07-13 21:29:25 -04001369 struct btrfs_free_space *right_info = NULL;
1370 struct btrfs_free_space *left_info = NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001371 struct btrfs_free_space *info = NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001372 int ret = 0;
1373
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001374 info = kzalloc(sizeof(struct btrfs_free_space), GFP_NOFS);
1375 if (!info)
1376 return -ENOMEM;
1377
1378 info->offset = offset;
1379 info->bytes = bytes;
1380
1381 spin_lock(&block_group->tree_lock);
1382
Josef Bacik0f9dd462008-09-23 13:14:11 -04001383 /*
1384 * first we want to see if there is free space adjacent to the range we
1385 * are adding, if there is remove that struct and add a new one to
1386 * cover the entire range
1387 */
Josef Bacik96303082009-07-13 21:29:25 -04001388 right_info = tree_search_offset(block_group, offset + bytes, 0, 0);
1389 if (right_info && rb_prev(&right_info->offset_index))
1390 left_info = rb_entry(rb_prev(&right_info->offset_index),
1391 struct btrfs_free_space, offset_index);
1392 else
1393 left_info = tree_search_offset(block_group, offset - 1, 0, 0);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001394
Josef Bacik96303082009-07-13 21:29:25 -04001395 /*
1396 * If there was no extent directly to the left or right of this new
1397 * extent then we know we're going to have to allocate a new extent, so
1398 * before we do that see if we need to drop this into a bitmap
1399 */
1400 if ((!left_info || left_info->bitmap) &&
1401 (!right_info || right_info->bitmap)) {
1402 ret = insert_into_bitmap(block_group, info);
1403
1404 if (ret < 0) {
1405 goto out;
1406 } else if (ret) {
1407 ret = 0;
1408 goto out;
1409 }
1410 }
1411
1412 if (right_info && !right_info->bitmap) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001413 unlink_free_space(block_group, right_info);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001414 info->bytes += right_info->bytes;
1415 kfree(right_info);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001416 }
1417
Josef Bacik96303082009-07-13 21:29:25 -04001418 if (left_info && !left_info->bitmap &&
1419 left_info->offset + left_info->bytes == offset) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001420 unlink_free_space(block_group, left_info);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001421 info->offset = left_info->offset;
1422 info->bytes += left_info->bytes;
1423 kfree(left_info);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001424 }
1425
Josef Bacik0f9dd462008-09-23 13:14:11 -04001426 ret = link_free_space(block_group, info);
1427 if (ret)
1428 kfree(info);
Josef Bacik96303082009-07-13 21:29:25 -04001429out:
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001430 spin_unlock(&block_group->tree_lock);
1431
Josef Bacik0f9dd462008-09-23 13:14:11 -04001432 if (ret) {
Josef Bacik96303082009-07-13 21:29:25 -04001433 printk(KERN_CRIT "btrfs: unable to add free space :%d\n", ret);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04001434 BUG_ON(ret == -EEXIST);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001435 }
1436
Josef Bacik0f9dd462008-09-23 13:14:11 -04001437 return ret;
1438}
1439
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001440int btrfs_remove_free_space(struct btrfs_block_group_cache *block_group,
1441 u64 offset, u64 bytes)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001442{
1443 struct btrfs_free_space *info;
Josef Bacik96303082009-07-13 21:29:25 -04001444 struct btrfs_free_space *next_info = NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001445 int ret = 0;
1446
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001447 spin_lock(&block_group->tree_lock);
1448
Josef Bacik96303082009-07-13 21:29:25 -04001449again:
1450 info = tree_search_offset(block_group, offset, 0, 0);
1451 if (!info) {
Josef Bacik6606bb92009-07-31 11:03:58 -04001452 /*
1453 * oops didn't find an extent that matched the space we wanted
1454 * to remove, look for a bitmap instead
1455 */
1456 info = tree_search_offset(block_group,
1457 offset_to_bitmap(block_group, offset),
1458 1, 0);
1459 if (!info) {
1460 WARN_ON(1);
1461 goto out_lock;
1462 }
Josef Bacik96303082009-07-13 21:29:25 -04001463 }
1464
1465 if (info->bytes < bytes && rb_next(&info->offset_index)) {
1466 u64 end;
1467 next_info = rb_entry(rb_next(&info->offset_index),
1468 struct btrfs_free_space,
1469 offset_index);
1470
1471 if (next_info->bitmap)
1472 end = next_info->offset + BITS_PER_BITMAP *
1473 block_group->sectorsize - 1;
1474 else
1475 end = next_info->offset + next_info->bytes;
1476
1477 if (next_info->bytes < bytes ||
1478 next_info->offset > offset || offset > end) {
1479 printk(KERN_CRIT "Found free space at %llu, size %llu,"
1480 " trying to use %llu\n",
1481 (unsigned long long)info->offset,
1482 (unsigned long long)info->bytes,
1483 (unsigned long long)bytes);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001484 WARN_ON(1);
1485 ret = -EINVAL;
Josef Bacik96303082009-07-13 21:29:25 -04001486 goto out_lock;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001487 }
Josef Bacik96303082009-07-13 21:29:25 -04001488
1489 info = next_info;
1490 }
1491
1492 if (info->bytes == bytes) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001493 unlink_free_space(block_group, info);
Josef Bacik96303082009-07-13 21:29:25 -04001494 if (info->bitmap) {
1495 kfree(info->bitmap);
1496 block_group->total_bitmaps--;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001497 }
Josef Bacik96303082009-07-13 21:29:25 -04001498 kfree(info);
1499 goto out_lock;
1500 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04001501
Josef Bacik96303082009-07-13 21:29:25 -04001502 if (!info->bitmap && info->offset == offset) {
1503 unlink_free_space(block_group, info);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001504 info->offset += bytes;
1505 info->bytes -= bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001506 link_free_space(block_group, info);
1507 goto out_lock;
1508 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04001509
Josef Bacik96303082009-07-13 21:29:25 -04001510 if (!info->bitmap && info->offset <= offset &&
1511 info->offset + info->bytes >= offset + bytes) {
Chris Mason9b49c9b2008-09-24 11:23:25 -04001512 u64 old_start = info->offset;
1513 /*
1514 * we're freeing space in the middle of the info,
1515 * this can happen during tree log replay
1516 *
1517 * first unlink the old info and then
1518 * insert it again after the hole we're creating
1519 */
1520 unlink_free_space(block_group, info);
1521 if (offset + bytes < info->offset + info->bytes) {
1522 u64 old_end = info->offset + info->bytes;
1523
1524 info->offset = offset + bytes;
1525 info->bytes = old_end - info->offset;
1526 ret = link_free_space(block_group, info);
Josef Bacik96303082009-07-13 21:29:25 -04001527 WARN_ON(ret);
1528 if (ret)
1529 goto out_lock;
Chris Mason9b49c9b2008-09-24 11:23:25 -04001530 } else {
1531 /* the hole we're creating ends at the end
1532 * of the info struct, just free the info
1533 */
1534 kfree(info);
1535 }
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001536 spin_unlock(&block_group->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04001537
1538 /* step two, insert a new info struct to cover
1539 * anything before the hole
Chris Mason9b49c9b2008-09-24 11:23:25 -04001540 */
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001541 ret = btrfs_add_free_space(block_group, old_start,
1542 offset - old_start);
Josef Bacik96303082009-07-13 21:29:25 -04001543 WARN_ON(ret);
1544 goto out;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001545 }
Josef Bacik96303082009-07-13 21:29:25 -04001546
1547 ret = remove_from_bitmap(block_group, info, &offset, &bytes);
1548 if (ret == -EAGAIN)
1549 goto again;
1550 BUG_ON(ret);
1551out_lock:
1552 spin_unlock(&block_group->tree_lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001553out:
Josef Bacik25179202008-10-29 14:49:05 -04001554 return ret;
1555}
1556
Josef Bacik0f9dd462008-09-23 13:14:11 -04001557void btrfs_dump_free_space(struct btrfs_block_group_cache *block_group,
1558 u64 bytes)
1559{
1560 struct btrfs_free_space *info;
1561 struct rb_node *n;
1562 int count = 0;
1563
1564 for (n = rb_first(&block_group->free_space_offset); n; n = rb_next(n)) {
1565 info = rb_entry(n, struct btrfs_free_space, offset_index);
1566 if (info->bytes >= bytes)
1567 count++;
Josef Bacik96303082009-07-13 21:29:25 -04001568 printk(KERN_CRIT "entry offset %llu, bytes %llu, bitmap %s\n",
Joel Becker21380932009-04-21 12:38:29 -07001569 (unsigned long long)info->offset,
Josef Bacik96303082009-07-13 21:29:25 -04001570 (unsigned long long)info->bytes,
1571 (info->bitmap) ? "yes" : "no");
Josef Bacik0f9dd462008-09-23 13:14:11 -04001572 }
Josef Bacik96303082009-07-13 21:29:25 -04001573 printk(KERN_INFO "block group has cluster?: %s\n",
1574 list_empty(&block_group->cluster_list) ? "no" : "yes");
Josef Bacik0f9dd462008-09-23 13:14:11 -04001575 printk(KERN_INFO "%d blocks of free space at or bigger than bytes is"
1576 "\n", count);
1577}
1578
1579u64 btrfs_block_group_free_space(struct btrfs_block_group_cache *block_group)
1580{
1581 struct btrfs_free_space *info;
1582 struct rb_node *n;
1583 u64 ret = 0;
1584
1585 for (n = rb_first(&block_group->free_space_offset); n;
1586 n = rb_next(n)) {
1587 info = rb_entry(n, struct btrfs_free_space, offset_index);
1588 ret += info->bytes;
1589 }
1590
1591 return ret;
1592}
1593
Chris Masonfa9c0d792009-04-03 09:47:43 -04001594/*
1595 * for a given cluster, put all of its extents back into the free
1596 * space cache. If the block group passed doesn't match the block group
1597 * pointed to by the cluster, someone else raced in and freed the
1598 * cluster already. In that case, we just return without changing anything
1599 */
1600static int
1601__btrfs_return_cluster_to_free_space(
1602 struct btrfs_block_group_cache *block_group,
1603 struct btrfs_free_cluster *cluster)
1604{
1605 struct btrfs_free_space *entry;
1606 struct rb_node *node;
Josef Bacik96303082009-07-13 21:29:25 -04001607 bool bitmap;
Chris Masonfa9c0d792009-04-03 09:47:43 -04001608
1609 spin_lock(&cluster->lock);
1610 if (cluster->block_group != block_group)
1611 goto out;
1612
Josef Bacik96303082009-07-13 21:29:25 -04001613 bitmap = cluster->points_to_bitmap;
1614 cluster->block_group = NULL;
Chris Masonfa9c0d792009-04-03 09:47:43 -04001615 cluster->window_start = 0;
Josef Bacik96303082009-07-13 21:29:25 -04001616 list_del_init(&cluster->block_group_list);
1617 cluster->points_to_bitmap = false;
1618
1619 if (bitmap)
1620 goto out;
1621
Chris Masonfa9c0d792009-04-03 09:47:43 -04001622 node = rb_first(&cluster->root);
Josef Bacik96303082009-07-13 21:29:25 -04001623 while (node) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04001624 entry = rb_entry(node, struct btrfs_free_space, offset_index);
1625 node = rb_next(&entry->offset_index);
1626 rb_erase(&entry->offset_index, &cluster->root);
Josef Bacik96303082009-07-13 21:29:25 -04001627 BUG_ON(entry->bitmap);
1628 tree_insert_offset(&block_group->free_space_offset,
1629 entry->offset, &entry->offset_index, 0);
Chris Masonfa9c0d792009-04-03 09:47:43 -04001630 }
Eric Paris6bef4d32010-02-23 19:43:04 +00001631 cluster->root = RB_ROOT;
Josef Bacik96303082009-07-13 21:29:25 -04001632
Chris Masonfa9c0d792009-04-03 09:47:43 -04001633out:
1634 spin_unlock(&cluster->lock);
Josef Bacik96303082009-07-13 21:29:25 -04001635 btrfs_put_block_group(block_group);
Chris Masonfa9c0d792009-04-03 09:47:43 -04001636 return 0;
1637}
1638
Josef Bacik0f9dd462008-09-23 13:14:11 -04001639void btrfs_remove_free_space_cache(struct btrfs_block_group_cache *block_group)
1640{
1641 struct btrfs_free_space *info;
1642 struct rb_node *node;
Chris Masonfa9c0d792009-04-03 09:47:43 -04001643 struct btrfs_free_cluster *cluster;
Josef Bacik96303082009-07-13 21:29:25 -04001644 struct list_head *head;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001645
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001646 spin_lock(&block_group->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04001647 while ((head = block_group->cluster_list.next) !=
1648 &block_group->cluster_list) {
1649 cluster = list_entry(head, struct btrfs_free_cluster,
1650 block_group_list);
Chris Masonfa9c0d792009-04-03 09:47:43 -04001651
1652 WARN_ON(cluster->block_group != block_group);
1653 __btrfs_return_cluster_to_free_space(block_group, cluster);
Josef Bacik96303082009-07-13 21:29:25 -04001654 if (need_resched()) {
1655 spin_unlock(&block_group->tree_lock);
1656 cond_resched();
1657 spin_lock(&block_group->tree_lock);
1658 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04001659 }
1660
Josef Bacik96303082009-07-13 21:29:25 -04001661 while ((node = rb_last(&block_group->free_space_offset)) != NULL) {
1662 info = rb_entry(node, struct btrfs_free_space, offset_index);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001663 unlink_free_space(block_group, info);
Josef Bacik96303082009-07-13 21:29:25 -04001664 if (info->bitmap)
1665 kfree(info->bitmap);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001666 kfree(info);
1667 if (need_resched()) {
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001668 spin_unlock(&block_group->tree_lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001669 cond_resched();
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001670 spin_lock(&block_group->tree_lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001671 }
1672 }
Josef Bacik96303082009-07-13 21:29:25 -04001673
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001674 spin_unlock(&block_group->tree_lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001675}
1676
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001677u64 btrfs_find_space_for_alloc(struct btrfs_block_group_cache *block_group,
1678 u64 offset, u64 bytes, u64 empty_size)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001679{
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001680 struct btrfs_free_space *entry = NULL;
Josef Bacik96303082009-07-13 21:29:25 -04001681 u64 bytes_search = bytes + empty_size;
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001682 u64 ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001683
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001684 spin_lock(&block_group->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04001685 entry = find_free_space(block_group, &offset, &bytes_search, 0);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001686 if (!entry)
Josef Bacik96303082009-07-13 21:29:25 -04001687 goto out;
1688
1689 ret = offset;
1690 if (entry->bitmap) {
Josef Bacik817d52f2009-07-13 21:29:25 -04001691 bitmap_clear_bits(block_group, entry, offset, bytes);
Li Zefanedf6e2d2010-11-09 14:50:07 +08001692 if (!entry->bytes)
1693 free_bitmap(block_group, entry);
Josef Bacik96303082009-07-13 21:29:25 -04001694 } else {
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001695 unlink_free_space(block_group, entry);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001696 entry->offset += bytes;
1697 entry->bytes -= bytes;
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001698 if (!entry->bytes)
1699 kfree(entry);
1700 else
1701 link_free_space(block_group, entry);
1702 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04001703
Josef Bacik96303082009-07-13 21:29:25 -04001704out:
1705 spin_unlock(&block_group->tree_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04001706
Josef Bacik0f9dd462008-09-23 13:14:11 -04001707 return ret;
1708}
Chris Masonfa9c0d792009-04-03 09:47:43 -04001709
1710/*
1711 * given a cluster, put all of its extents back into the free space
1712 * cache. If a block group is passed, this function will only free
1713 * a cluster that belongs to the passed block group.
1714 *
1715 * Otherwise, it'll get a reference on the block group pointed to by the
1716 * cluster and remove the cluster from it.
1717 */
1718int btrfs_return_cluster_to_free_space(
1719 struct btrfs_block_group_cache *block_group,
1720 struct btrfs_free_cluster *cluster)
1721{
1722 int ret;
1723
1724 /* first, get a safe pointer to the block group */
1725 spin_lock(&cluster->lock);
1726 if (!block_group) {
1727 block_group = cluster->block_group;
1728 if (!block_group) {
1729 spin_unlock(&cluster->lock);
1730 return 0;
1731 }
1732 } else if (cluster->block_group != block_group) {
1733 /* someone else has already freed it don't redo their work */
1734 spin_unlock(&cluster->lock);
1735 return 0;
1736 }
1737 atomic_inc(&block_group->count);
1738 spin_unlock(&cluster->lock);
1739
1740 /* now return any extents the cluster had on it */
1741 spin_lock(&block_group->tree_lock);
1742 ret = __btrfs_return_cluster_to_free_space(block_group, cluster);
1743 spin_unlock(&block_group->tree_lock);
1744
1745 /* finally drop our ref */
1746 btrfs_put_block_group(block_group);
1747 return ret;
1748}
1749
Josef Bacik96303082009-07-13 21:29:25 -04001750static u64 btrfs_alloc_from_bitmap(struct btrfs_block_group_cache *block_group,
1751 struct btrfs_free_cluster *cluster,
1752 u64 bytes, u64 min_start)
1753{
1754 struct btrfs_free_space *entry;
1755 int err;
1756 u64 search_start = cluster->window_start;
1757 u64 search_bytes = bytes;
1758 u64 ret = 0;
1759
1760 spin_lock(&block_group->tree_lock);
1761 spin_lock(&cluster->lock);
1762
1763 if (!cluster->points_to_bitmap)
1764 goto out;
1765
1766 if (cluster->block_group != block_group)
1767 goto out;
1768
Josef Bacik6606bb92009-07-31 11:03:58 -04001769 /*
1770 * search_start is the beginning of the bitmap, but at some point it may
1771 * be a good idea to point to the actual start of the free area in the
1772 * bitmap, so do the offset_to_bitmap trick anyway, and set bitmap_only
1773 * to 1 to make sure we get the bitmap entry
1774 */
1775 entry = tree_search_offset(block_group,
1776 offset_to_bitmap(block_group, search_start),
1777 1, 0);
Josef Bacik96303082009-07-13 21:29:25 -04001778 if (!entry || !entry->bitmap)
1779 goto out;
1780
1781 search_start = min_start;
1782 search_bytes = bytes;
1783
1784 err = search_bitmap(block_group, entry, &search_start,
1785 &search_bytes);
1786 if (err)
1787 goto out;
1788
1789 ret = search_start;
Josef Bacik817d52f2009-07-13 21:29:25 -04001790 bitmap_clear_bits(block_group, entry, ret, bytes);
Josef Bacik96303082009-07-13 21:29:25 -04001791out:
1792 spin_unlock(&cluster->lock);
1793 spin_unlock(&block_group->tree_lock);
1794
1795 return ret;
1796}
1797
Chris Masonfa9c0d792009-04-03 09:47:43 -04001798/*
1799 * given a cluster, try to allocate 'bytes' from it, returns 0
1800 * if it couldn't find anything suitably large, or a logical disk offset
1801 * if things worked out
1802 */
1803u64 btrfs_alloc_from_cluster(struct btrfs_block_group_cache *block_group,
1804 struct btrfs_free_cluster *cluster, u64 bytes,
1805 u64 min_start)
1806{
1807 struct btrfs_free_space *entry = NULL;
1808 struct rb_node *node;
1809 u64 ret = 0;
1810
Josef Bacik96303082009-07-13 21:29:25 -04001811 if (cluster->points_to_bitmap)
1812 return btrfs_alloc_from_bitmap(block_group, cluster, bytes,
1813 min_start);
1814
Chris Masonfa9c0d792009-04-03 09:47:43 -04001815 spin_lock(&cluster->lock);
1816 if (bytes > cluster->max_size)
1817 goto out;
1818
1819 if (cluster->block_group != block_group)
1820 goto out;
1821
1822 node = rb_first(&cluster->root);
1823 if (!node)
1824 goto out;
1825
1826 entry = rb_entry(node, struct btrfs_free_space, offset_index);
1827
1828 while(1) {
1829 if (entry->bytes < bytes || entry->offset < min_start) {
1830 struct rb_node *node;
1831
1832 node = rb_next(&entry->offset_index);
1833 if (!node)
1834 break;
1835 entry = rb_entry(node, struct btrfs_free_space,
1836 offset_index);
1837 continue;
1838 }
1839 ret = entry->offset;
1840
1841 entry->offset += bytes;
1842 entry->bytes -= bytes;
1843
1844 if (entry->bytes == 0) {
1845 rb_erase(&entry->offset_index, &cluster->root);
1846 kfree(entry);
1847 }
1848 break;
1849 }
1850out:
1851 spin_unlock(&cluster->lock);
Josef Bacik96303082009-07-13 21:29:25 -04001852
Chris Masonfa9c0d792009-04-03 09:47:43 -04001853 return ret;
1854}
1855
Josef Bacik96303082009-07-13 21:29:25 -04001856static int btrfs_bitmap_cluster(struct btrfs_block_group_cache *block_group,
1857 struct btrfs_free_space *entry,
1858 struct btrfs_free_cluster *cluster,
1859 u64 offset, u64 bytes, u64 min_bytes)
1860{
1861 unsigned long next_zero;
1862 unsigned long i;
1863 unsigned long search_bits;
1864 unsigned long total_bits;
1865 unsigned long found_bits;
1866 unsigned long start = 0;
1867 unsigned long total_found = 0;
1868 bool found = false;
1869
1870 i = offset_to_bit(entry->offset, block_group->sectorsize,
1871 max_t(u64, offset, entry->offset));
1872 search_bits = bytes_to_bits(min_bytes, block_group->sectorsize);
1873 total_bits = bytes_to_bits(bytes, block_group->sectorsize);
1874
1875again:
1876 found_bits = 0;
1877 for (i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, i);
1878 i < BITS_PER_BITMAP;
1879 i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, i + 1)) {
1880 next_zero = find_next_zero_bit(entry->bitmap,
1881 BITS_PER_BITMAP, i);
1882 if (next_zero - i >= search_bits) {
1883 found_bits = next_zero - i;
1884 break;
1885 }
1886 i = next_zero;
1887 }
1888
1889 if (!found_bits)
1890 return -1;
1891
1892 if (!found) {
1893 start = i;
1894 found = true;
1895 }
1896
1897 total_found += found_bits;
1898
1899 if (cluster->max_size < found_bits * block_group->sectorsize)
1900 cluster->max_size = found_bits * block_group->sectorsize;
1901
1902 if (total_found < total_bits) {
1903 i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, next_zero);
1904 if (i - start > total_bits * 2) {
1905 total_found = 0;
1906 cluster->max_size = 0;
1907 found = false;
1908 }
1909 goto again;
1910 }
1911
1912 cluster->window_start = start * block_group->sectorsize +
1913 entry->offset;
1914 cluster->points_to_bitmap = true;
1915
1916 return 0;
1917}
1918
Chris Masonfa9c0d792009-04-03 09:47:43 -04001919/*
1920 * here we try to find a cluster of blocks in a block group. The goal
1921 * is to find at least bytes free and up to empty_size + bytes free.
1922 * We might not find them all in one contiguous area.
1923 *
1924 * returns zero and sets up cluster if things worked out, otherwise
1925 * it returns -enospc
1926 */
1927int btrfs_find_space_cluster(struct btrfs_trans_handle *trans,
Chris Mason451d7582009-06-09 20:28:34 -04001928 struct btrfs_root *root,
Chris Masonfa9c0d792009-04-03 09:47:43 -04001929 struct btrfs_block_group_cache *block_group,
1930 struct btrfs_free_cluster *cluster,
1931 u64 offset, u64 bytes, u64 empty_size)
1932{
1933 struct btrfs_free_space *entry = NULL;
1934 struct rb_node *node;
1935 struct btrfs_free_space *next;
Josef Bacik96303082009-07-13 21:29:25 -04001936 struct btrfs_free_space *last = NULL;
Chris Masonfa9c0d792009-04-03 09:47:43 -04001937 u64 min_bytes;
1938 u64 window_start;
1939 u64 window_free;
1940 u64 max_extent = 0;
Josef Bacik96303082009-07-13 21:29:25 -04001941 bool found_bitmap = false;
Chris Masonfa9c0d792009-04-03 09:47:43 -04001942 int ret;
1943
1944 /* for metadata, allow allocates with more holes */
Chris Mason451d7582009-06-09 20:28:34 -04001945 if (btrfs_test_opt(root, SSD_SPREAD)) {
1946 min_bytes = bytes + empty_size;
1947 } else if (block_group->flags & BTRFS_BLOCK_GROUP_METADATA) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04001948 /*
1949 * we want to do larger allocations when we are
1950 * flushing out the delayed refs, it helps prevent
1951 * making more work as we go along.
1952 */
1953 if (trans->transaction->delayed_refs.flushing)
1954 min_bytes = max(bytes, (bytes + empty_size) >> 1);
1955 else
1956 min_bytes = max(bytes, (bytes + empty_size) >> 4);
1957 } else
1958 min_bytes = max(bytes, (bytes + empty_size) >> 2);
1959
1960 spin_lock(&block_group->tree_lock);
1961 spin_lock(&cluster->lock);
1962
1963 /* someone already found a cluster, hooray */
1964 if (cluster->block_group) {
1965 ret = 0;
1966 goto out;
1967 }
1968again:
Josef Bacik96303082009-07-13 21:29:25 -04001969 entry = tree_search_offset(block_group, offset, found_bitmap, 1);
Chris Masonfa9c0d792009-04-03 09:47:43 -04001970 if (!entry) {
1971 ret = -ENOSPC;
1972 goto out;
1973 }
Josef Bacik96303082009-07-13 21:29:25 -04001974
1975 /*
1976 * If found_bitmap is true, we exhausted our search for extent entries,
1977 * and we just want to search all of the bitmaps that we can find, and
1978 * ignore any extent entries we find.
1979 */
1980 while (entry->bitmap || found_bitmap ||
1981 (!entry->bitmap && entry->bytes < min_bytes)) {
1982 struct rb_node *node = rb_next(&entry->offset_index);
1983
1984 if (entry->bitmap && entry->bytes > bytes + empty_size) {
1985 ret = btrfs_bitmap_cluster(block_group, entry, cluster,
1986 offset, bytes + empty_size,
1987 min_bytes);
1988 if (!ret)
1989 goto got_it;
1990 }
1991
1992 if (!node) {
1993 ret = -ENOSPC;
1994 goto out;
1995 }
1996 entry = rb_entry(node, struct btrfs_free_space, offset_index);
1997 }
1998
1999 /*
2000 * We already searched all the extent entries from the passed in offset
2001 * to the end and didn't find enough space for the cluster, and we also
2002 * didn't find any bitmaps that met our criteria, just go ahead and exit
2003 */
2004 if (found_bitmap) {
2005 ret = -ENOSPC;
2006 goto out;
2007 }
2008
2009 cluster->points_to_bitmap = false;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002010 window_start = entry->offset;
2011 window_free = entry->bytes;
2012 last = entry;
2013 max_extent = entry->bytes;
2014
Josef Bacik96303082009-07-13 21:29:25 -04002015 while (1) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04002016 /* out window is just right, lets fill it */
2017 if (window_free >= bytes + empty_size)
2018 break;
2019
2020 node = rb_next(&last->offset_index);
2021 if (!node) {
Josef Bacik96303082009-07-13 21:29:25 -04002022 if (found_bitmap)
2023 goto again;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002024 ret = -ENOSPC;
2025 goto out;
2026 }
2027 next = rb_entry(node, struct btrfs_free_space, offset_index);
2028
2029 /*
Josef Bacik96303082009-07-13 21:29:25 -04002030 * we found a bitmap, so if this search doesn't result in a
2031 * cluster, we know to go and search again for the bitmaps and
2032 * start looking for space there
2033 */
2034 if (next->bitmap) {
2035 if (!found_bitmap)
2036 offset = next->offset;
2037 found_bitmap = true;
2038 last = next;
2039 continue;
2040 }
2041
2042 /*
Chris Masonfa9c0d792009-04-03 09:47:43 -04002043 * we haven't filled the empty size and the window is
2044 * very large. reset and try again
2045 */
Chris Masonc6044802009-06-09 18:35:15 -04002046 if (next->offset - (last->offset + last->bytes) > 128 * 1024 ||
2047 next->offset - window_start > (bytes + empty_size) * 2) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04002048 entry = next;
2049 window_start = entry->offset;
2050 window_free = entry->bytes;
2051 last = entry;
Josef Bacik01dea1e2009-11-10 21:23:48 -05002052 max_extent = entry->bytes;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002053 } else {
2054 last = next;
2055 window_free += next->bytes;
2056 if (entry->bytes > max_extent)
2057 max_extent = entry->bytes;
2058 }
2059 }
2060
2061 cluster->window_start = entry->offset;
2062
2063 /*
2064 * now we've found our entries, pull them out of the free space
2065 * cache and put them into the cluster rbtree
2066 *
2067 * The cluster includes an rbtree, but only uses the offset index
2068 * of each free space cache entry.
2069 */
Josef Bacik96303082009-07-13 21:29:25 -04002070 while (1) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04002071 node = rb_next(&entry->offset_index);
Josef Bacik96303082009-07-13 21:29:25 -04002072 if (entry->bitmap && node) {
2073 entry = rb_entry(node, struct btrfs_free_space,
2074 offset_index);
2075 continue;
2076 } else if (entry->bitmap && !node) {
2077 break;
2078 }
2079
2080 rb_erase(&entry->offset_index, &block_group->free_space_offset);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002081 ret = tree_insert_offset(&cluster->root, entry->offset,
Josef Bacik96303082009-07-13 21:29:25 -04002082 &entry->offset_index, 0);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002083 BUG_ON(ret);
2084
2085 if (!node || entry == last)
2086 break;
2087
2088 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2089 }
Josef Bacik96303082009-07-13 21:29:25 -04002090
Chris Masonfa9c0d792009-04-03 09:47:43 -04002091 cluster->max_size = max_extent;
Josef Bacik96303082009-07-13 21:29:25 -04002092got_it:
2093 ret = 0;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002094 atomic_inc(&block_group->count);
2095 list_add_tail(&cluster->block_group_list, &block_group->cluster_list);
2096 cluster->block_group = block_group;
2097out:
2098 spin_unlock(&cluster->lock);
2099 spin_unlock(&block_group->tree_lock);
2100
2101 return ret;
2102}
2103
2104/*
2105 * simple code to zero out a cluster
2106 */
2107void btrfs_init_free_cluster(struct btrfs_free_cluster *cluster)
2108{
2109 spin_lock_init(&cluster->lock);
2110 spin_lock_init(&cluster->refill_lock);
Eric Paris6bef4d32010-02-23 19:43:04 +00002111 cluster->root = RB_ROOT;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002112 cluster->max_size = 0;
Josef Bacik96303082009-07-13 21:29:25 -04002113 cluster->points_to_bitmap = false;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002114 INIT_LIST_HEAD(&cluster->block_group_list);
2115 cluster->block_group = NULL;
2116}
2117