2 * Copyright (C) 2008 Red Hat. All rights reserved.
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.
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.
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.
19 #include "kerncompat.h"
21 #include "free-space-cache.h"
22 #include "transaction.h"
24 #include "extent_io.h"
31 * Kernel always uses PAGE_CACHE_SIZE for sectorsize, but we don't have
32 * anything like that in userspace and have to get the value from the
35 #define BITS_PER_BITMAP(sectorsize) ((sectorsize) * 8)
36 #define MAX_CACHE_BYTES_PER_GIG SZ_32K
38 static int link_free_space(struct btrfs_free_space_ctl *ctl,
39 struct btrfs_free_space *info);
40 static void merge_space_tree(struct btrfs_free_space_ctl *ctl);
45 struct btrfs_root *root;
50 unsigned check_crcs:1;
53 static int io_ctl_init(struct io_ctl *io_ctl, u64 size, u64 ino,
54 struct btrfs_root *root)
56 memset(io_ctl, 0, sizeof(struct io_ctl));
57 io_ctl->num_pages = (size + root->sectorsize - 1) / root->sectorsize;
58 io_ctl->buffer = kzalloc(size, GFP_NOFS);
61 io_ctl->total_size = size;
63 if (ino != BTRFS_FREE_INO_OBJECTID)
64 io_ctl->check_crcs = 1;
68 static void io_ctl_free(struct io_ctl *io_ctl)
70 kfree(io_ctl->buffer);
73 static void io_ctl_unmap_page(struct io_ctl *io_ctl)
81 static void io_ctl_map_page(struct io_ctl *io_ctl, int clear)
83 BUG_ON(io_ctl->index >= io_ctl->num_pages);
84 io_ctl->cur = io_ctl->buffer + (io_ctl->index++ * io_ctl->root->sectorsize);
85 io_ctl->orig = io_ctl->cur;
86 io_ctl->size = io_ctl->root->sectorsize;
88 memset(io_ctl->cur, 0, io_ctl->root->sectorsize);
91 static void io_ctl_drop_pages(struct io_ctl *io_ctl)
93 io_ctl_unmap_page(io_ctl);
96 static int io_ctl_prepare_pages(struct io_ctl *io_ctl, struct btrfs_root *root,
97 struct btrfs_path *path, u64 ino)
99 struct extent_buffer *leaf;
100 struct btrfs_file_extent_item *fi;
101 struct btrfs_key key;
107 key.type = BTRFS_EXTENT_DATA_KEY;
110 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
113 "Couldn't find file extent item for free space inode"
115 btrfs_release_path(path);
119 while (total_read < io_ctl->total_size) {
120 if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
121 ret = btrfs_next_leaf(root, path);
127 leaf = path->nodes[0];
129 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
130 if (key.objectid != ino) {
135 if (key.type != BTRFS_EXTENT_DATA_KEY) {
140 fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
141 struct btrfs_file_extent_item);
142 if (btrfs_file_extent_type(path->nodes[0], fi) !=
143 BTRFS_FILE_EXTENT_REG) {
144 fprintf(stderr, "Not the file extent type we wanted\n");
149 bytenr = btrfs_file_extent_disk_bytenr(leaf, fi) +
150 btrfs_file_extent_offset(leaf, fi);
151 len = btrfs_file_extent_num_bytes(leaf, fi);
152 ret = read_data_from_disk(root->fs_info,
153 io_ctl->buffer + key.offset, bytenr,
161 btrfs_release_path(path);
165 static int io_ctl_check_generation(struct io_ctl *io_ctl, u64 generation)
170 * Skip the crc area. If we don't check crcs then we just have a 64bit
171 * chunk at the front of the first page.
173 if (io_ctl->check_crcs) {
174 io_ctl->cur += sizeof(u32) * io_ctl->num_pages;
175 io_ctl->size -= sizeof(u64) +
176 (sizeof(u32) * io_ctl->num_pages);
178 io_ctl->cur += sizeof(u64);
179 io_ctl->size -= sizeof(u64) * 2;
183 if (le64_to_cpu(*gen) != generation) {
184 printk("btrfs: space cache generation "
185 "(%Lu) does not match inode (%Lu)\n", *gen,
187 io_ctl_unmap_page(io_ctl);
190 io_ctl->cur += sizeof(u64);
194 static int io_ctl_check_crc(struct io_ctl *io_ctl, int index)
200 if (!io_ctl->check_crcs) {
201 io_ctl_map_page(io_ctl, 0);
206 offset = sizeof(u32) * io_ctl->num_pages;
208 tmp = io_ctl->buffer;
212 io_ctl_map_page(io_ctl, 0);
213 crc = crc32c(crc, io_ctl->orig + offset, io_ctl->root->sectorsize - offset);
214 btrfs_csum_final(crc, (u8 *)&crc);
216 printk("btrfs: csum mismatch on free space cache\n");
217 io_ctl_unmap_page(io_ctl);
224 static int io_ctl_read_entry(struct io_ctl *io_ctl,
225 struct btrfs_free_space *entry, u8 *type)
227 struct btrfs_free_space_entry *e;
231 ret = io_ctl_check_crc(io_ctl, io_ctl->index);
237 entry->offset = le64_to_cpu(e->offset);
238 entry->bytes = le64_to_cpu(e->bytes);
240 io_ctl->cur += sizeof(struct btrfs_free_space_entry);
241 io_ctl->size -= sizeof(struct btrfs_free_space_entry);
243 if (io_ctl->size >= sizeof(struct btrfs_free_space_entry))
246 io_ctl_unmap_page(io_ctl);
251 static int io_ctl_read_bitmap(struct io_ctl *io_ctl,
252 struct btrfs_free_space *entry)
256 ret = io_ctl_check_crc(io_ctl, io_ctl->index);
260 memcpy(entry->bitmap, io_ctl->cur, io_ctl->root->sectorsize);
261 io_ctl_unmap_page(io_ctl);
267 static int __load_free_space_cache(struct btrfs_root *root,
268 struct btrfs_free_space_ctl *ctl,
269 struct btrfs_path *path, u64 offset)
271 struct btrfs_free_space_header *header;
272 struct btrfs_inode_item *inode_item;
273 struct extent_buffer *leaf;
274 struct io_ctl io_ctl;
275 struct btrfs_key key;
276 struct btrfs_key inode_location;
277 struct btrfs_disk_key disk_key;
278 struct btrfs_free_space *e, *n;
279 struct list_head bitmaps;
287 INIT_LIST_HEAD(&bitmaps);
289 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
293 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
296 } else if (ret > 0) {
297 btrfs_release_path(path);
301 leaf = path->nodes[0];
302 header = btrfs_item_ptr(leaf, path->slots[0],
303 struct btrfs_free_space_header);
304 num_entries = btrfs_free_space_entries(leaf, header);
305 num_bitmaps = btrfs_free_space_bitmaps(leaf, header);
306 generation = btrfs_free_space_generation(leaf, header);
307 btrfs_free_space_key(leaf, header, &disk_key);
308 btrfs_disk_key_to_cpu(&inode_location, &disk_key);
309 btrfs_release_path(path);
311 ret = btrfs_search_slot(NULL, root, &inode_location, path, 0, 0);
313 fprintf(stderr, "Couldn't find free space inode %d\n", ret);
317 leaf = path->nodes[0];
318 inode_item = btrfs_item_ptr(leaf, path->slots[0],
319 struct btrfs_inode_item);
321 inode_size = btrfs_inode_size(leaf, inode_item);
322 if (!inode_size || !btrfs_inode_generation(leaf, inode_item)) {
323 btrfs_release_path(path);
327 if (btrfs_inode_generation(leaf, inode_item) != generation) {
329 "free space inode generation (%llu) did not match "
330 "free space cache generation (%llu)\n",
331 (unsigned long long)btrfs_inode_generation(leaf,
333 (unsigned long long)generation);
334 btrfs_release_path(path);
338 btrfs_release_path(path);
343 ret = io_ctl_init(&io_ctl, inode_size, inode_location.objectid, root);
347 ret = io_ctl_prepare_pages(&io_ctl, root, path,
348 inode_location.objectid);
352 ret = io_ctl_check_crc(&io_ctl, 0);
356 ret = io_ctl_check_generation(&io_ctl, generation);
360 while (num_entries) {
361 e = calloc(1, sizeof(*e));
365 ret = io_ctl_read_entry(&io_ctl, e, &type);
376 if (type == BTRFS_FREE_SPACE_EXTENT) {
377 ret = link_free_space(ctl, e);
380 "Duplicate entries in free space cache\n");
385 BUG_ON(!num_bitmaps);
387 e->bitmap = kzalloc(ctl->sectorsize, GFP_NOFS);
392 ret = link_free_space(ctl, e);
393 ctl->total_bitmaps++;
396 "Duplicate entries in free space cache\n");
401 list_add_tail(&e->list, &bitmaps);
407 io_ctl_unmap_page(&io_ctl);
410 * We add the bitmaps at the end of the entries in order that
411 * the bitmap entries are added to the cache.
413 list_for_each_entry_safe(e, n, &bitmaps, list) {
414 list_del_init(&e->list);
415 ret = io_ctl_read_bitmap(&io_ctl, e);
420 io_ctl_drop_pages(&io_ctl);
421 merge_space_tree(ctl);
424 io_ctl_free(&io_ctl);
427 io_ctl_drop_pages(&io_ctl);
428 __btrfs_remove_free_space_cache(ctl);
432 int load_free_space_cache(struct btrfs_fs_info *fs_info,
433 struct btrfs_block_group_cache *block_group)
435 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
436 struct btrfs_path *path;
437 u64 used = btrfs_block_group_used(&block_group->item);
441 path = btrfs_alloc_path();
445 ret = __load_free_space_cache(fs_info->tree_root, ctl, path,
446 block_group->key.objectid);
447 btrfs_free_path(path);
449 matched = (ctl->free_space == (block_group->key.offset - used -
450 block_group->bytes_super));
451 if (ret == 1 && !matched) {
452 __btrfs_remove_free_space_cache(ctl);
454 "block group %llu has wrong amount of free space\n",
455 block_group->key.objectid);
463 "failed to load free space cache for block group %llu\n",
464 block_group->key.objectid);
470 static inline unsigned long offset_to_bit(u64 bitmap_start, u32 unit,
473 BUG_ON(offset < bitmap_start);
474 offset -= bitmap_start;
475 return (unsigned long)(offset / unit);
478 static inline unsigned long bytes_to_bits(u64 bytes, u32 unit)
480 return (unsigned long)(bytes / unit);
483 static int tree_insert_offset(struct rb_root *root, u64 offset,
484 struct rb_node *node, int bitmap)
486 struct rb_node **p = &root->rb_node;
487 struct rb_node *parent = NULL;
488 struct btrfs_free_space *info;
492 info = rb_entry(parent, struct btrfs_free_space, offset_index);
494 if (offset < info->offset) {
496 } else if (offset > info->offset) {
500 * we could have a bitmap entry and an extent entry
501 * share the same offset. If this is the case, we want
502 * the extent entry to always be found first if we do a
503 * linear search through the tree, since we want to have
504 * the quickest allocation time, and allocating from an
505 * extent is faster than allocating from a bitmap. So
506 * if we're inserting a bitmap and we find an entry at
507 * this offset, we want to go right, or after this entry
508 * logically. If we are inserting an extent and we've
509 * found a bitmap, we want to go left, or before
524 rb_link_node(node, parent, p);
525 rb_insert_color(node, root);
531 * searches the tree for the given offset.
533 * fuzzy - If this is set, then we are trying to make an allocation, and we just
534 * want a section that has at least bytes size and comes at or after the given
537 static struct btrfs_free_space *
538 tree_search_offset(struct btrfs_free_space_ctl *ctl,
539 u64 offset, int bitmap_only, int fuzzy)
541 struct rb_node *n = ctl->free_space_offset.rb_node;
542 struct btrfs_free_space *entry, *prev = NULL;
543 u32 sectorsize = ctl->sectorsize;
545 /* find entry that is closest to the 'offset' */
552 entry = rb_entry(n, struct btrfs_free_space, offset_index);
555 if (offset < entry->offset)
557 else if (offset > entry->offset)
570 * bitmap entry and extent entry may share same offset,
571 * in that case, bitmap entry comes after extent entry.
576 entry = rb_entry(n, struct btrfs_free_space, offset_index);
577 if (entry->offset != offset)
580 WARN_ON(!entry->bitmap);
585 * if previous extent entry covers the offset,
586 * we should return it instead of the bitmap entry
588 n = rb_prev(&entry->offset_index);
590 prev = rb_entry(n, struct btrfs_free_space,
593 prev->offset + prev->bytes > offset)
603 /* find last entry before the 'offset' */
605 if (entry->offset > offset) {
606 n = rb_prev(&entry->offset_index);
608 entry = rb_entry(n, struct btrfs_free_space,
610 BUG_ON(entry->offset > offset);
620 n = rb_prev(&entry->offset_index);
622 prev = rb_entry(n, struct btrfs_free_space,
625 prev->offset + prev->bytes > offset)
628 if (entry->offset + BITS_PER_BITMAP(sectorsize) * ctl->unit > offset)
630 } else if (entry->offset + entry->bytes > offset)
638 if (entry->offset + BITS_PER_BITMAP(sectorsize) *
642 if (entry->offset + entry->bytes > offset)
646 n = rb_next(&entry->offset_index);
649 entry = rb_entry(n, struct btrfs_free_space, offset_index);
654 void unlink_free_space(struct btrfs_free_space_ctl *ctl,
655 struct btrfs_free_space *info)
657 rb_erase(&info->offset_index, &ctl->free_space_offset);
659 ctl->free_space -= info->bytes;
662 static int link_free_space(struct btrfs_free_space_ctl *ctl,
663 struct btrfs_free_space *info)
667 BUG_ON(!info->bitmap && !info->bytes);
668 ret = tree_insert_offset(&ctl->free_space_offset, info->offset,
669 &info->offset_index, (info->bitmap != NULL));
673 ctl->free_space += info->bytes;
678 static int search_bitmap(struct btrfs_free_space_ctl *ctl,
679 struct btrfs_free_space *bitmap_info, u64 *offset,
682 unsigned long found_bits = 0;
683 unsigned long bits, i;
684 unsigned long next_zero;
685 u32 sectorsize = ctl->sectorsize;
687 i = offset_to_bit(bitmap_info->offset, ctl->unit,
688 max_t(u64, *offset, bitmap_info->offset));
689 bits = bytes_to_bits(*bytes, ctl->unit);
691 for_each_set_bit_from(i, bitmap_info->bitmap, BITS_PER_BITMAP(sectorsize)) {
692 next_zero = find_next_zero_bit(bitmap_info->bitmap,
693 BITS_PER_BITMAP(sectorsize), i);
694 if ((next_zero - i) >= bits) {
695 found_bits = next_zero - i;
702 *offset = (u64)(i * ctl->unit) + bitmap_info->offset;
703 *bytes = (u64)(found_bits) * ctl->unit;
710 struct btrfs_free_space *
711 btrfs_find_free_space(struct btrfs_free_space_ctl *ctl, u64 offset, u64 bytes)
713 return tree_search_offset(ctl, offset, 0, 0);
716 static void try_merge_free_space(struct btrfs_free_space_ctl *ctl,
717 struct btrfs_free_space *info)
719 struct btrfs_free_space *left_info;
720 struct btrfs_free_space *right_info;
721 u64 offset = info->offset;
722 u64 bytes = info->bytes;
725 * first we want to see if there is free space adjacent to the range we
726 * are adding, if there is remove that struct and add a new one to
727 * cover the entire range
729 right_info = tree_search_offset(ctl, offset + bytes, 0, 0);
730 if (right_info && rb_prev(&right_info->offset_index))
731 left_info = rb_entry(rb_prev(&right_info->offset_index),
732 struct btrfs_free_space, offset_index);
734 left_info = tree_search_offset(ctl, offset - 1, 0, 0);
736 if (right_info && !right_info->bitmap) {
737 unlink_free_space(ctl, right_info);
738 info->bytes += right_info->bytes;
742 if (left_info && !left_info->bitmap &&
743 left_info->offset + left_info->bytes == offset) {
744 unlink_free_space(ctl, left_info);
745 info->offset = left_info->offset;
746 info->bytes += left_info->bytes;
751 void btrfs_dump_free_space(struct btrfs_block_group_cache *block_group,
754 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
755 struct btrfs_free_space *info;
759 for (n = rb_first(&ctl->free_space_offset); n; n = rb_next(n)) {
760 info = rb_entry(n, struct btrfs_free_space, offset_index);
761 if (info->bytes >= bytes && !block_group->ro)
763 printk("entry offset %llu, bytes %llu, bitmap %s\n",
764 (unsigned long long)info->offset,
765 (unsigned long long)info->bytes,
766 (info->bitmap) ? "yes" : "no");
768 printk("%d blocks of free space at or bigger than bytes is \n", count);
771 int btrfs_init_free_space_ctl(struct btrfs_block_group_cache *block_group,
774 struct btrfs_free_space_ctl *ctl;
776 ctl = calloc(1, sizeof(*ctl));
780 ctl->sectorsize = sectorsize;
781 ctl->unit = sectorsize;
782 ctl->start = block_group->key.objectid;
783 ctl->private = block_group;
784 block_group->free_space_ctl = ctl;
789 void __btrfs_remove_free_space_cache(struct btrfs_free_space_ctl *ctl)
791 struct btrfs_free_space *info;
792 struct rb_node *node;
794 while ((node = rb_last(&ctl->free_space_offset)) != NULL) {
795 info = rb_entry(node, struct btrfs_free_space, offset_index);
796 unlink_free_space(ctl, info);
802 void btrfs_remove_free_space_cache(struct btrfs_block_group_cache *block_group)
804 __btrfs_remove_free_space_cache(block_group->free_space_ctl);
807 int btrfs_add_free_space(struct btrfs_free_space_ctl *ctl, u64 offset,
810 struct btrfs_free_space *info;
813 info = calloc(1, sizeof(*info));
817 info->offset = offset;
820 try_merge_free_space(ctl, info);
822 ret = link_free_space(ctl, info);
824 printk(KERN_CRIT "btrfs: unable to add free space :%d\n", ret);
825 BUG_ON(ret == -EEXIST);
832 * Merges all the free space cache and kills the bitmap entries since we just
833 * want to use the free space cache to verify it's correct, no reason to keep
834 * the bitmaps around to confuse things.
836 static void merge_space_tree(struct btrfs_free_space_ctl *ctl)
838 struct btrfs_free_space *e, *prev = NULL;
841 u32 sectorsize = ctl->sectorsize;
845 for (n = rb_first(&ctl->free_space_offset); n; n = rb_next(n)) {
846 e = rb_entry(n, struct btrfs_free_space, offset_index);
848 u64 offset = e->offset, bytes = ctl->unit;
851 end = e->offset + (u64)(BITS_PER_BITMAP(sectorsize) * ctl->unit);
853 unlink_free_space(ctl, e);
854 while (!(search_bitmap(ctl, e, &offset, &bytes))) {
855 ret = btrfs_add_free_space(ctl, offset,
869 if (prev->offset + prev->bytes == e->offset) {
870 unlink_free_space(ctl, prev);
871 unlink_free_space(ctl, e);
872 prev->bytes += e->bytes;
874 link_free_space(ctl, prev);
882 int btrfs_clear_free_space_cache(struct btrfs_fs_info *fs_info,
883 struct btrfs_block_group_cache *bg)
885 struct btrfs_trans_handle *trans;
886 struct btrfs_root *tree_root = fs_info->tree_root;
887 struct btrfs_path path;
888 struct btrfs_key key;
889 struct btrfs_disk_key location;
890 struct btrfs_free_space_header *sc_header;
891 struct extent_buffer *node;
896 trans = btrfs_start_transaction(tree_root, 1);
898 return PTR_ERR(trans);
900 btrfs_init_path(&path);
902 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
904 key.offset = bg->key.objectid;
906 ret = btrfs_search_slot(trans, tree_root, &key, &path, -1, 1);
914 node = path.nodes[0];
915 slot = path.slots[0];
916 sc_header = btrfs_item_ptr(node, slot, struct btrfs_free_space_header);
917 btrfs_free_space_key(node, sc_header, &location);
918 ino = location.objectid;
920 /* Delete the free space header, as we have the ino to continue */
921 ret = btrfs_del_item(trans, tree_root, &path);
923 error("failed to remove free space header for block group %llu: %d",
924 bg->key.objectid, ret);
927 btrfs_release_path(&path);
929 /* Iterate from the end of the free space cache inode */
931 key.type = BTRFS_EXTENT_DATA_KEY;
932 key.offset = (u64)-1;
933 ret = btrfs_search_slot(trans, tree_root, &key, &path, -1, 1);
935 error("failed to locate free space cache extent for block group %llu: %d",
936 bg->key.objectid, ret);
940 struct btrfs_file_extent_item *fi;
944 ret = btrfs_previous_item(tree_root, &path, ino,
945 BTRFS_EXTENT_DATA_KEY);
952 "failed to locate free space cache extent for block group %llu: %d",
953 bg->key.objectid, ret);
956 node = path.nodes[0];
957 slot = path.slots[0];
958 btrfs_item_key_to_cpu(node, &key, slot);
959 fi = btrfs_item_ptr(node, slot, struct btrfs_file_extent_item);
960 disk_bytenr = btrfs_file_extent_disk_bytenr(node, fi);
961 disk_num_bytes = btrfs_file_extent_disk_num_bytes(node, fi);
963 ret = btrfs_free_extent(trans, tree_root, disk_bytenr,
964 disk_num_bytes, 0, tree_root->objectid,
967 error("failed to remove backref for disk bytenr %llu: %d",
971 ret = btrfs_del_item(trans, tree_root, &path);
974 "failed to remove free space extent data for ino %llu offset %llu: %d",
975 ino, key.offset, ret);
979 btrfs_release_path(&path);
981 /* Now delete free space cache inode item */
983 key.type = BTRFS_INODE_ITEM_KEY;
986 ret = btrfs_search_slot(trans, tree_root, &key, &path, -1, 1);
988 warning("free space inode %llu not found, ignore", ino);
991 "failed to locate free space cache inode %llu for block group %llu: %d",
992 ino, bg->key.objectid, ret);
995 ret = btrfs_del_item(trans, tree_root, &path);
998 "failed to delete free space cache inode %llu for block group %llu: %d",
999 ino, bg->key.objectid, ret);
1002 btrfs_release_path(&path);
1004 btrfs_commit_transaction(trans, tree_root);