Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1 | /* |
| 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 Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 19 | #include <linux/pagemap.h> |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 20 | #include <linux/sched.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 21 | #include <linux/slab.h> |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 22 | #include <linux/math64.h> |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 23 | #include "ctree.h" |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 24 | #include "free-space-cache.h" |
| 25 | #include "transaction.h" |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 26 | #include "disk-io.h" |
Josef Bacik | 43be214 | 2011-04-01 14:55:00 +0000 | [diff] [blame] | 27 | #include "extent_io.h" |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 28 | |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 29 | #define BITS_PER_BITMAP (PAGE_CACHE_SIZE * 8) |
| 30 | #define MAX_CACHE_BYTES_PER_GIG (32 * 1024) |
| 31 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 32 | static void recalculate_thresholds(struct btrfs_block_group_cache |
| 33 | *block_group); |
| 34 | static int link_free_space(struct btrfs_block_group_cache *block_group, |
| 35 | struct btrfs_free_space *info); |
| 36 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 37 | struct inode *lookup_free_space_inode(struct btrfs_root *root, |
| 38 | struct btrfs_block_group_cache |
| 39 | *block_group, struct btrfs_path *path) |
| 40 | { |
| 41 | struct btrfs_key key; |
| 42 | struct btrfs_key location; |
| 43 | struct btrfs_disk_key disk_key; |
| 44 | struct btrfs_free_space_header *header; |
| 45 | struct extent_buffer *leaf; |
| 46 | struct inode *inode = NULL; |
| 47 | int ret; |
| 48 | |
| 49 | spin_lock(&block_group->lock); |
| 50 | if (block_group->inode) |
| 51 | inode = igrab(block_group->inode); |
| 52 | spin_unlock(&block_group->lock); |
| 53 | if (inode) |
| 54 | return inode; |
| 55 | |
| 56 | key.objectid = BTRFS_FREE_SPACE_OBJECTID; |
| 57 | key.offset = block_group->key.objectid; |
| 58 | key.type = 0; |
| 59 | |
| 60 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 61 | if (ret < 0) |
| 62 | return ERR_PTR(ret); |
| 63 | if (ret > 0) { |
| 64 | btrfs_release_path(root, path); |
| 65 | return ERR_PTR(-ENOENT); |
| 66 | } |
| 67 | |
| 68 | leaf = path->nodes[0]; |
| 69 | header = btrfs_item_ptr(leaf, path->slots[0], |
| 70 | struct btrfs_free_space_header); |
| 71 | btrfs_free_space_key(leaf, header, &disk_key); |
| 72 | btrfs_disk_key_to_cpu(&location, &disk_key); |
| 73 | btrfs_release_path(root, path); |
| 74 | |
| 75 | inode = btrfs_iget(root->fs_info->sb, &location, root, NULL); |
| 76 | if (!inode) |
| 77 | return ERR_PTR(-ENOENT); |
| 78 | if (IS_ERR(inode)) |
| 79 | return inode; |
| 80 | if (is_bad_inode(inode)) { |
| 81 | iput(inode); |
| 82 | return ERR_PTR(-ENOENT); |
| 83 | } |
| 84 | |
Miao Xie | adae52b | 2011-03-31 09:43:23 +0000 | [diff] [blame] | 85 | inode->i_mapping->flags &= ~__GFP_FS; |
| 86 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 87 | spin_lock(&block_group->lock); |
| 88 | if (!root->fs_info->closing) { |
| 89 | block_group->inode = igrab(inode); |
| 90 | block_group->iref = 1; |
| 91 | } |
| 92 | spin_unlock(&block_group->lock); |
| 93 | |
| 94 | return inode; |
| 95 | } |
| 96 | |
| 97 | int create_free_space_inode(struct btrfs_root *root, |
| 98 | struct btrfs_trans_handle *trans, |
| 99 | struct btrfs_block_group_cache *block_group, |
| 100 | struct btrfs_path *path) |
| 101 | { |
| 102 | struct btrfs_key key; |
| 103 | struct btrfs_disk_key disk_key; |
| 104 | struct btrfs_free_space_header *header; |
| 105 | struct btrfs_inode_item *inode_item; |
| 106 | struct extent_buffer *leaf; |
| 107 | u64 objectid; |
| 108 | int ret; |
| 109 | |
| 110 | ret = btrfs_find_free_objectid(trans, root, 0, &objectid); |
| 111 | if (ret < 0) |
| 112 | return ret; |
| 113 | |
| 114 | ret = btrfs_insert_empty_inode(trans, root, path, objectid); |
| 115 | if (ret) |
| 116 | return ret; |
| 117 | |
| 118 | leaf = path->nodes[0]; |
| 119 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
| 120 | struct btrfs_inode_item); |
| 121 | btrfs_item_key(leaf, &disk_key, path->slots[0]); |
| 122 | memset_extent_buffer(leaf, 0, (unsigned long)inode_item, |
| 123 | sizeof(*inode_item)); |
| 124 | btrfs_set_inode_generation(leaf, inode_item, trans->transid); |
| 125 | btrfs_set_inode_size(leaf, inode_item, 0); |
| 126 | btrfs_set_inode_nbytes(leaf, inode_item, 0); |
| 127 | btrfs_set_inode_uid(leaf, inode_item, 0); |
| 128 | btrfs_set_inode_gid(leaf, inode_item, 0); |
| 129 | btrfs_set_inode_mode(leaf, inode_item, S_IFREG | 0600); |
| 130 | btrfs_set_inode_flags(leaf, inode_item, BTRFS_INODE_NOCOMPRESS | |
| 131 | BTRFS_INODE_PREALLOC | BTRFS_INODE_NODATASUM); |
| 132 | btrfs_set_inode_nlink(leaf, inode_item, 1); |
| 133 | btrfs_set_inode_transid(leaf, inode_item, trans->transid); |
| 134 | btrfs_set_inode_block_group(leaf, inode_item, |
| 135 | block_group->key.objectid); |
| 136 | btrfs_mark_buffer_dirty(leaf); |
| 137 | btrfs_release_path(root, path); |
| 138 | |
| 139 | key.objectid = BTRFS_FREE_SPACE_OBJECTID; |
| 140 | key.offset = block_group->key.objectid; |
| 141 | key.type = 0; |
| 142 | |
| 143 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 144 | sizeof(struct btrfs_free_space_header)); |
| 145 | if (ret < 0) { |
| 146 | btrfs_release_path(root, path); |
| 147 | return ret; |
| 148 | } |
| 149 | leaf = path->nodes[0]; |
| 150 | header = btrfs_item_ptr(leaf, path->slots[0], |
| 151 | struct btrfs_free_space_header); |
| 152 | memset_extent_buffer(leaf, 0, (unsigned long)header, sizeof(*header)); |
| 153 | btrfs_set_free_space_key(leaf, header, &disk_key); |
| 154 | btrfs_mark_buffer_dirty(leaf); |
| 155 | btrfs_release_path(root, path); |
| 156 | |
| 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | int btrfs_truncate_free_space_cache(struct btrfs_root *root, |
| 161 | struct btrfs_trans_handle *trans, |
| 162 | struct btrfs_path *path, |
| 163 | struct inode *inode) |
| 164 | { |
| 165 | loff_t oldsize; |
| 166 | int ret = 0; |
| 167 | |
| 168 | trans->block_rsv = root->orphan_block_rsv; |
| 169 | ret = btrfs_block_rsv_check(trans, root, |
| 170 | root->orphan_block_rsv, |
| 171 | 0, 5); |
| 172 | if (ret) |
| 173 | return ret; |
| 174 | |
| 175 | oldsize = i_size_read(inode); |
| 176 | btrfs_i_size_write(inode, 0); |
| 177 | truncate_pagecache(inode, oldsize, 0); |
| 178 | |
| 179 | /* |
| 180 | * We don't need an orphan item because truncating the free space cache |
| 181 | * will never be split across transactions. |
| 182 | */ |
| 183 | ret = btrfs_truncate_inode_items(trans, root, inode, |
| 184 | 0, BTRFS_EXTENT_DATA_KEY); |
| 185 | if (ret) { |
| 186 | WARN_ON(1); |
| 187 | return ret; |
| 188 | } |
| 189 | |
| 190 | return btrfs_update_inode(trans, root, inode); |
| 191 | } |
| 192 | |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 193 | static int readahead_cache(struct inode *inode) |
| 194 | { |
| 195 | struct file_ra_state *ra; |
| 196 | unsigned long last_index; |
| 197 | |
| 198 | ra = kzalloc(sizeof(*ra), GFP_NOFS); |
| 199 | if (!ra) |
| 200 | return -ENOMEM; |
| 201 | |
| 202 | file_ra_state_init(ra, inode->i_mapping); |
| 203 | last_index = (i_size_read(inode) - 1) >> PAGE_CACHE_SHIFT; |
| 204 | |
| 205 | page_cache_sync_readahead(inode->i_mapping, ra, NULL, 0, last_index); |
| 206 | |
| 207 | kfree(ra); |
| 208 | |
| 209 | return 0; |
| 210 | } |
| 211 | |
| 212 | int load_free_space_cache(struct btrfs_fs_info *fs_info, |
| 213 | struct btrfs_block_group_cache *block_group) |
| 214 | { |
| 215 | struct btrfs_root *root = fs_info->tree_root; |
| 216 | struct inode *inode; |
| 217 | struct btrfs_free_space_header *header; |
| 218 | struct extent_buffer *leaf; |
| 219 | struct page *page; |
| 220 | struct btrfs_path *path; |
| 221 | u32 *checksums = NULL, *crc; |
| 222 | char *disk_crcs = NULL; |
| 223 | struct btrfs_key key; |
| 224 | struct list_head bitmaps; |
| 225 | u64 num_entries; |
| 226 | u64 num_bitmaps; |
| 227 | u64 generation; |
Josef Bacik | 43be214 | 2011-04-01 14:55:00 +0000 | [diff] [blame] | 228 | u64 used = btrfs_block_group_used(&block_group->item); |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 229 | u32 cur_crc = ~(u32)0; |
| 230 | pgoff_t index = 0; |
| 231 | unsigned long first_page_offset; |
| 232 | int num_checksums; |
| 233 | int ret = 0; |
| 234 | |
| 235 | /* |
| 236 | * If we're unmounting then just return, since this does a search on the |
| 237 | * normal root and not the commit root and we could deadlock. |
| 238 | */ |
| 239 | smp_mb(); |
| 240 | if (fs_info->closing) |
| 241 | return 0; |
| 242 | |
| 243 | /* |
| 244 | * If this block group has been marked to be cleared for one reason or |
| 245 | * another then we can't trust the on disk cache, so just return. |
| 246 | */ |
| 247 | spin_lock(&block_group->lock); |
| 248 | if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) { |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 249 | spin_unlock(&block_group->lock); |
| 250 | return 0; |
| 251 | } |
| 252 | spin_unlock(&block_group->lock); |
| 253 | |
| 254 | INIT_LIST_HEAD(&bitmaps); |
| 255 | |
| 256 | path = btrfs_alloc_path(); |
| 257 | if (!path) |
| 258 | return 0; |
| 259 | |
| 260 | inode = lookup_free_space_inode(root, block_group, path); |
| 261 | if (IS_ERR(inode)) { |
| 262 | btrfs_free_path(path); |
| 263 | return 0; |
| 264 | } |
| 265 | |
| 266 | /* Nothing in the space cache, goodbye */ |
| 267 | if (!i_size_read(inode)) { |
| 268 | btrfs_free_path(path); |
| 269 | goto out; |
| 270 | } |
| 271 | |
| 272 | key.objectid = BTRFS_FREE_SPACE_OBJECTID; |
| 273 | key.offset = block_group->key.objectid; |
| 274 | key.type = 0; |
| 275 | |
| 276 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 277 | if (ret) { |
| 278 | btrfs_free_path(path); |
| 279 | goto out; |
| 280 | } |
| 281 | |
| 282 | leaf = path->nodes[0]; |
| 283 | header = btrfs_item_ptr(leaf, path->slots[0], |
| 284 | struct btrfs_free_space_header); |
| 285 | num_entries = btrfs_free_space_entries(leaf, header); |
| 286 | num_bitmaps = btrfs_free_space_bitmaps(leaf, header); |
| 287 | generation = btrfs_free_space_generation(leaf, header); |
| 288 | btrfs_free_path(path); |
| 289 | |
| 290 | if (BTRFS_I(inode)->generation != generation) { |
| 291 | printk(KERN_ERR "btrfs: free space inode generation (%llu) did" |
| 292 | " not match free space cache generation (%llu) for " |
| 293 | "block group %llu\n", |
| 294 | (unsigned long long)BTRFS_I(inode)->generation, |
| 295 | (unsigned long long)generation, |
| 296 | (unsigned long long)block_group->key.objectid); |
Josef Bacik | 2b20982 | 2010-12-03 13:17:53 -0500 | [diff] [blame] | 297 | goto free_cache; |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | if (!num_entries) |
| 301 | goto out; |
| 302 | |
| 303 | /* Setup everything for doing checksumming */ |
| 304 | num_checksums = i_size_read(inode) / PAGE_CACHE_SIZE; |
| 305 | checksums = crc = kzalloc(sizeof(u32) * num_checksums, GFP_NOFS); |
| 306 | if (!checksums) |
| 307 | goto out; |
| 308 | first_page_offset = (sizeof(u32) * num_checksums) + sizeof(u64); |
| 309 | disk_crcs = kzalloc(first_page_offset, GFP_NOFS); |
| 310 | if (!disk_crcs) |
| 311 | goto out; |
| 312 | |
| 313 | ret = readahead_cache(inode); |
| 314 | if (ret) { |
| 315 | ret = 0; |
| 316 | goto out; |
| 317 | } |
| 318 | |
| 319 | while (1) { |
| 320 | struct btrfs_free_space_entry *entry; |
| 321 | struct btrfs_free_space *e; |
| 322 | void *addr; |
| 323 | unsigned long offset = 0; |
| 324 | unsigned long start_offset = 0; |
| 325 | int need_loop = 0; |
| 326 | |
| 327 | if (!num_entries && !num_bitmaps) |
| 328 | break; |
| 329 | |
| 330 | if (index == 0) { |
| 331 | start_offset = first_page_offset; |
| 332 | offset = start_offset; |
| 333 | } |
| 334 | |
| 335 | page = grab_cache_page(inode->i_mapping, index); |
| 336 | if (!page) { |
| 337 | ret = 0; |
| 338 | goto free_cache; |
| 339 | } |
| 340 | |
| 341 | if (!PageUptodate(page)) { |
| 342 | btrfs_readpage(NULL, page); |
| 343 | lock_page(page); |
| 344 | if (!PageUptodate(page)) { |
| 345 | unlock_page(page); |
| 346 | page_cache_release(page); |
| 347 | printk(KERN_ERR "btrfs: error reading free " |
| 348 | "space cache: %llu\n", |
| 349 | (unsigned long long) |
| 350 | block_group->key.objectid); |
| 351 | goto free_cache; |
| 352 | } |
| 353 | } |
| 354 | addr = kmap(page); |
| 355 | |
| 356 | if (index == 0) { |
| 357 | u64 *gen; |
| 358 | |
| 359 | memcpy(disk_crcs, addr, first_page_offset); |
| 360 | gen = addr + (sizeof(u32) * num_checksums); |
| 361 | if (*gen != BTRFS_I(inode)->generation) { |
| 362 | printk(KERN_ERR "btrfs: space cache generation" |
| 363 | " (%llu) does not match inode (%llu) " |
| 364 | "for block group %llu\n", |
| 365 | (unsigned long long)*gen, |
| 366 | (unsigned long long) |
| 367 | BTRFS_I(inode)->generation, |
| 368 | (unsigned long long) |
| 369 | block_group->key.objectid); |
| 370 | kunmap(page); |
| 371 | unlock_page(page); |
| 372 | page_cache_release(page); |
| 373 | goto free_cache; |
| 374 | } |
| 375 | crc = (u32 *)disk_crcs; |
| 376 | } |
| 377 | entry = addr + start_offset; |
| 378 | |
| 379 | /* First lets check our crc before we do anything fun */ |
| 380 | cur_crc = ~(u32)0; |
| 381 | cur_crc = btrfs_csum_data(root, addr + start_offset, cur_crc, |
| 382 | PAGE_CACHE_SIZE - start_offset); |
| 383 | btrfs_csum_final(cur_crc, (char *)&cur_crc); |
| 384 | if (cur_crc != *crc) { |
| 385 | printk(KERN_ERR "btrfs: crc mismatch for page %lu in " |
| 386 | "block group %llu\n", index, |
| 387 | (unsigned long long)block_group->key.objectid); |
| 388 | kunmap(page); |
| 389 | unlock_page(page); |
| 390 | page_cache_release(page); |
| 391 | goto free_cache; |
| 392 | } |
| 393 | crc++; |
| 394 | |
| 395 | while (1) { |
| 396 | if (!num_entries) |
| 397 | break; |
| 398 | |
| 399 | need_loop = 1; |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 400 | e = kmem_cache_zalloc(btrfs_free_space_cachep, |
| 401 | GFP_NOFS); |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 402 | if (!e) { |
| 403 | kunmap(page); |
| 404 | unlock_page(page); |
| 405 | page_cache_release(page); |
| 406 | goto free_cache; |
| 407 | } |
| 408 | |
| 409 | e->offset = le64_to_cpu(entry->offset); |
| 410 | e->bytes = le64_to_cpu(entry->bytes); |
| 411 | if (!e->bytes) { |
| 412 | kunmap(page); |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 413 | kmem_cache_free(btrfs_free_space_cachep, e); |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 414 | unlock_page(page); |
| 415 | page_cache_release(page); |
| 416 | goto free_cache; |
| 417 | } |
| 418 | |
| 419 | if (entry->type == BTRFS_FREE_SPACE_EXTENT) { |
| 420 | spin_lock(&block_group->tree_lock); |
| 421 | ret = link_free_space(block_group, e); |
| 422 | spin_unlock(&block_group->tree_lock); |
| 423 | BUG_ON(ret); |
| 424 | } else { |
| 425 | e->bitmap = kzalloc(PAGE_CACHE_SIZE, GFP_NOFS); |
| 426 | if (!e->bitmap) { |
| 427 | kunmap(page); |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 428 | kmem_cache_free( |
| 429 | btrfs_free_space_cachep, e); |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 430 | unlock_page(page); |
| 431 | page_cache_release(page); |
| 432 | goto free_cache; |
| 433 | } |
| 434 | spin_lock(&block_group->tree_lock); |
| 435 | ret = link_free_space(block_group, e); |
| 436 | block_group->total_bitmaps++; |
| 437 | recalculate_thresholds(block_group); |
| 438 | spin_unlock(&block_group->tree_lock); |
| 439 | list_add_tail(&e->list, &bitmaps); |
| 440 | } |
| 441 | |
| 442 | num_entries--; |
| 443 | offset += sizeof(struct btrfs_free_space_entry); |
| 444 | if (offset + sizeof(struct btrfs_free_space_entry) >= |
| 445 | PAGE_CACHE_SIZE) |
| 446 | break; |
| 447 | entry++; |
| 448 | } |
| 449 | |
| 450 | /* |
| 451 | * We read an entry out of this page, we need to move on to the |
| 452 | * next page. |
| 453 | */ |
| 454 | if (need_loop) { |
| 455 | kunmap(page); |
| 456 | goto next; |
| 457 | } |
| 458 | |
| 459 | /* |
| 460 | * We add the bitmaps at the end of the entries in order that |
| 461 | * the bitmap entries are added to the cache. |
| 462 | */ |
| 463 | e = list_entry(bitmaps.next, struct btrfs_free_space, list); |
| 464 | list_del_init(&e->list); |
| 465 | memcpy(e->bitmap, addr, PAGE_CACHE_SIZE); |
| 466 | kunmap(page); |
| 467 | num_bitmaps--; |
| 468 | next: |
| 469 | unlock_page(page); |
| 470 | page_cache_release(page); |
| 471 | index++; |
| 472 | } |
| 473 | |
Josef Bacik | 43be214 | 2011-04-01 14:55:00 +0000 | [diff] [blame] | 474 | spin_lock(&block_group->tree_lock); |
| 475 | if (block_group->free_space != (block_group->key.offset - used - |
| 476 | block_group->bytes_super)) { |
| 477 | spin_unlock(&block_group->tree_lock); |
| 478 | printk(KERN_ERR "block group %llu has an wrong amount of free " |
| 479 | "space\n", block_group->key.objectid); |
| 480 | ret = 0; |
| 481 | goto free_cache; |
| 482 | } |
| 483 | spin_unlock(&block_group->tree_lock); |
| 484 | |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 485 | ret = 1; |
| 486 | out: |
| 487 | kfree(checksums); |
| 488 | kfree(disk_crcs); |
| 489 | iput(inode); |
| 490 | return ret; |
| 491 | |
| 492 | free_cache: |
| 493 | /* This cache is bogus, make sure it gets cleared */ |
| 494 | spin_lock(&block_group->lock); |
| 495 | block_group->disk_cache_state = BTRFS_DC_CLEAR; |
| 496 | spin_unlock(&block_group->lock); |
| 497 | btrfs_remove_free_space_cache(block_group); |
| 498 | goto out; |
| 499 | } |
| 500 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 501 | int btrfs_write_out_cache(struct btrfs_root *root, |
| 502 | struct btrfs_trans_handle *trans, |
| 503 | struct btrfs_block_group_cache *block_group, |
| 504 | struct btrfs_path *path) |
| 505 | { |
| 506 | struct btrfs_free_space_header *header; |
| 507 | struct extent_buffer *leaf; |
| 508 | struct inode *inode; |
| 509 | struct rb_node *node; |
| 510 | struct list_head *pos, *n; |
Josef Bacik | be1a12a | 2011-04-06 13:05:22 -0400 | [diff] [blame] | 511 | struct page **pages; |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 512 | struct page *page; |
| 513 | struct extent_state *cached_state = NULL; |
Josef Bacik | 43be214 | 2011-04-01 14:55:00 +0000 | [diff] [blame] | 514 | struct btrfs_free_cluster *cluster = NULL; |
| 515 | struct extent_io_tree *unpin = NULL; |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 516 | struct list_head bitmap_list; |
| 517 | struct btrfs_key key; |
Josef Bacik | 43be214 | 2011-04-01 14:55:00 +0000 | [diff] [blame] | 518 | u64 start, end, len; |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 519 | u64 bytes = 0; |
| 520 | u32 *crc, *checksums; |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 521 | unsigned long first_page_offset; |
Josef Bacik | be1a12a | 2011-04-06 13:05:22 -0400 | [diff] [blame] | 522 | int index = 0, num_pages = 0; |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 523 | int entries = 0; |
| 524 | int bitmaps = 0; |
| 525 | int ret = 0; |
Josef Bacik | 43be214 | 2011-04-01 14:55:00 +0000 | [diff] [blame] | 526 | bool next_page = false; |
Josef Bacik | be1a12a | 2011-04-06 13:05:22 -0400 | [diff] [blame] | 527 | bool out_of_space = false; |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 528 | |
| 529 | root = root->fs_info->tree_root; |
| 530 | |
| 531 | INIT_LIST_HEAD(&bitmap_list); |
| 532 | |
| 533 | spin_lock(&block_group->lock); |
| 534 | if (block_group->disk_cache_state < BTRFS_DC_SETUP) { |
| 535 | spin_unlock(&block_group->lock); |
| 536 | return 0; |
| 537 | } |
| 538 | spin_unlock(&block_group->lock); |
| 539 | |
| 540 | inode = lookup_free_space_inode(root, block_group, path); |
| 541 | if (IS_ERR(inode)) |
| 542 | return 0; |
| 543 | |
| 544 | if (!i_size_read(inode)) { |
| 545 | iput(inode); |
| 546 | return 0; |
| 547 | } |
| 548 | |
Josef Bacik | 2b20982 | 2010-12-03 13:17:53 -0500 | [diff] [blame] | 549 | node = rb_first(&block_group->free_space_offset); |
| 550 | if (!node) { |
| 551 | iput(inode); |
| 552 | return 0; |
| 553 | } |
| 554 | |
Josef Bacik | be1a12a | 2011-04-06 13:05:22 -0400 | [diff] [blame] | 555 | num_pages = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >> |
| 556 | PAGE_CACHE_SHIFT; |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 557 | filemap_write_and_wait(inode->i_mapping); |
| 558 | btrfs_wait_ordered_range(inode, inode->i_size & |
| 559 | ~(root->sectorsize - 1), (u64)-1); |
| 560 | |
| 561 | /* We need a checksum per page. */ |
Josef Bacik | be1a12a | 2011-04-06 13:05:22 -0400 | [diff] [blame] | 562 | crc = checksums = kzalloc(sizeof(u32) * num_pages, GFP_NOFS); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 563 | if (!crc) { |
| 564 | iput(inode); |
| 565 | return 0; |
| 566 | } |
| 567 | |
Josef Bacik | be1a12a | 2011-04-06 13:05:22 -0400 | [diff] [blame] | 568 | pages = kzalloc(sizeof(struct page *) * num_pages, GFP_NOFS); |
| 569 | if (!pages) { |
| 570 | kfree(crc); |
| 571 | iput(inode); |
| 572 | return 0; |
| 573 | } |
| 574 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 575 | /* Since the first page has all of our checksums and our generation we |
| 576 | * need to calculate the offset into the page that we can start writing |
| 577 | * our entries. |
| 578 | */ |
Josef Bacik | be1a12a | 2011-04-06 13:05:22 -0400 | [diff] [blame] | 579 | first_page_offset = (sizeof(u32) * num_pages) + sizeof(u64); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 580 | |
Josef Bacik | 43be214 | 2011-04-01 14:55:00 +0000 | [diff] [blame] | 581 | /* Get the cluster for this block_group if it exists */ |
| 582 | if (!list_empty(&block_group->cluster_list)) |
| 583 | cluster = list_entry(block_group->cluster_list.next, |
| 584 | struct btrfs_free_cluster, |
| 585 | block_group_list); |
| 586 | |
| 587 | /* |
| 588 | * We shouldn't have switched the pinned extents yet so this is the |
| 589 | * right one |
| 590 | */ |
| 591 | unpin = root->fs_info->pinned_extents; |
| 592 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 593 | /* |
| 594 | * Lock all pages first so we can lock the extent safely. |
| 595 | * |
| 596 | * NOTE: Because we hold the ref the entire time we're going to write to |
| 597 | * the page find_get_page should never fail, so we don't do a check |
| 598 | * after find_get_page at this point. Just putting this here so people |
| 599 | * know and don't freak out. |
| 600 | */ |
Josef Bacik | be1a12a | 2011-04-06 13:05:22 -0400 | [diff] [blame] | 601 | while (index < num_pages) { |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 602 | page = grab_cache_page(inode->i_mapping, index); |
| 603 | if (!page) { |
Josef Bacik | be1a12a | 2011-04-06 13:05:22 -0400 | [diff] [blame] | 604 | int i; |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 605 | |
Josef Bacik | be1a12a | 2011-04-06 13:05:22 -0400 | [diff] [blame] | 606 | for (i = 0; i < num_pages; i++) { |
| 607 | unlock_page(pages[i]); |
| 608 | page_cache_release(pages[i]); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 609 | } |
| 610 | goto out_free; |
| 611 | } |
Josef Bacik | be1a12a | 2011-04-06 13:05:22 -0400 | [diff] [blame] | 612 | pages[index] = page; |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 613 | index++; |
| 614 | } |
| 615 | |
| 616 | index = 0; |
| 617 | lock_extent_bits(&BTRFS_I(inode)->io_tree, 0, i_size_read(inode) - 1, |
| 618 | 0, &cached_state, GFP_NOFS); |
| 619 | |
Josef Bacik | 43be214 | 2011-04-01 14:55:00 +0000 | [diff] [blame] | 620 | /* |
| 621 | * When searching for pinned extents, we need to start at our start |
| 622 | * offset. |
| 623 | */ |
| 624 | start = block_group->key.objectid; |
| 625 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 626 | /* Write out the extent entries */ |
| 627 | do { |
| 628 | struct btrfs_free_space_entry *entry; |
| 629 | void *addr; |
| 630 | unsigned long offset = 0; |
| 631 | unsigned long start_offset = 0; |
| 632 | |
Josef Bacik | 43be214 | 2011-04-01 14:55:00 +0000 | [diff] [blame] | 633 | next_page = false; |
| 634 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 635 | if (index == 0) { |
| 636 | start_offset = first_page_offset; |
| 637 | offset = start_offset; |
| 638 | } |
| 639 | |
Josef Bacik | be1a12a | 2011-04-06 13:05:22 -0400 | [diff] [blame] | 640 | if (index >= num_pages) { |
| 641 | out_of_space = true; |
| 642 | break; |
| 643 | } |
| 644 | |
| 645 | page = pages[index]; |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 646 | |
| 647 | addr = kmap(page); |
| 648 | entry = addr + start_offset; |
| 649 | |
| 650 | memset(addr, 0, PAGE_CACHE_SIZE); |
Josef Bacik | 43be214 | 2011-04-01 14:55:00 +0000 | [diff] [blame] | 651 | while (node && !next_page) { |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 652 | struct btrfs_free_space *e; |
| 653 | |
| 654 | e = rb_entry(node, struct btrfs_free_space, offset_index); |
| 655 | entries++; |
| 656 | |
| 657 | entry->offset = cpu_to_le64(e->offset); |
| 658 | entry->bytes = cpu_to_le64(e->bytes); |
| 659 | if (e->bitmap) { |
| 660 | entry->type = BTRFS_FREE_SPACE_BITMAP; |
| 661 | list_add_tail(&e->list, &bitmap_list); |
| 662 | bitmaps++; |
| 663 | } else { |
| 664 | entry->type = BTRFS_FREE_SPACE_EXTENT; |
| 665 | } |
| 666 | node = rb_next(node); |
Josef Bacik | 43be214 | 2011-04-01 14:55:00 +0000 | [diff] [blame] | 667 | if (!node && cluster) { |
| 668 | node = rb_first(&cluster->root); |
| 669 | cluster = NULL; |
| 670 | } |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 671 | offset += sizeof(struct btrfs_free_space_entry); |
| 672 | if (offset + sizeof(struct btrfs_free_space_entry) >= |
| 673 | PAGE_CACHE_SIZE) |
Josef Bacik | 43be214 | 2011-04-01 14:55:00 +0000 | [diff] [blame] | 674 | next_page = true; |
| 675 | entry++; |
| 676 | } |
| 677 | |
| 678 | /* |
| 679 | * We want to add any pinned extents to our free space cache |
| 680 | * so we don't leak the space |
| 681 | */ |
| 682 | while (!next_page && (start < block_group->key.objectid + |
| 683 | block_group->key.offset)) { |
| 684 | ret = find_first_extent_bit(unpin, start, &start, &end, |
| 685 | EXTENT_DIRTY); |
| 686 | if (ret) { |
| 687 | ret = 0; |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 688 | break; |
Josef Bacik | 43be214 | 2011-04-01 14:55:00 +0000 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | /* This pinned extent is out of our range */ |
| 692 | if (start >= block_group->key.objectid + |
| 693 | block_group->key.offset) |
| 694 | break; |
| 695 | |
| 696 | len = block_group->key.objectid + |
| 697 | block_group->key.offset - start; |
| 698 | len = min(len, end + 1 - start); |
| 699 | |
| 700 | entries++; |
| 701 | entry->offset = cpu_to_le64(start); |
| 702 | entry->bytes = cpu_to_le64(len); |
| 703 | entry->type = BTRFS_FREE_SPACE_EXTENT; |
| 704 | |
| 705 | start = end + 1; |
| 706 | offset += sizeof(struct btrfs_free_space_entry); |
| 707 | if (offset + sizeof(struct btrfs_free_space_entry) >= |
| 708 | PAGE_CACHE_SIZE) |
| 709 | next_page = true; |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 710 | entry++; |
| 711 | } |
| 712 | *crc = ~(u32)0; |
| 713 | *crc = btrfs_csum_data(root, addr + start_offset, *crc, |
| 714 | PAGE_CACHE_SIZE - start_offset); |
| 715 | kunmap(page); |
| 716 | |
| 717 | btrfs_csum_final(*crc, (char *)crc); |
| 718 | crc++; |
| 719 | |
| 720 | bytes += PAGE_CACHE_SIZE; |
| 721 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 722 | index++; |
Josef Bacik | 43be214 | 2011-04-01 14:55:00 +0000 | [diff] [blame] | 723 | } while (node || next_page); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 724 | |
| 725 | /* Write out the bitmaps */ |
| 726 | list_for_each_safe(pos, n, &bitmap_list) { |
| 727 | void *addr; |
| 728 | struct btrfs_free_space *entry = |
| 729 | list_entry(pos, struct btrfs_free_space, list); |
| 730 | |
Josef Bacik | be1a12a | 2011-04-06 13:05:22 -0400 | [diff] [blame] | 731 | if (index >= num_pages) { |
| 732 | out_of_space = true; |
| 733 | break; |
| 734 | } |
Chris Mason | f65647c | 2011-04-18 08:55:34 -0400 | [diff] [blame] | 735 | page = pages[index]; |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 736 | |
| 737 | addr = kmap(page); |
| 738 | memcpy(addr, entry->bitmap, PAGE_CACHE_SIZE); |
| 739 | *crc = ~(u32)0; |
| 740 | *crc = btrfs_csum_data(root, addr, *crc, PAGE_CACHE_SIZE); |
| 741 | kunmap(page); |
| 742 | btrfs_csum_final(*crc, (char *)crc); |
| 743 | crc++; |
| 744 | bytes += PAGE_CACHE_SIZE; |
| 745 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 746 | list_del_init(&entry->list); |
| 747 | index++; |
| 748 | } |
| 749 | |
Josef Bacik | be1a12a | 2011-04-06 13:05:22 -0400 | [diff] [blame] | 750 | if (out_of_space) { |
| 751 | btrfs_drop_pages(pages, num_pages); |
| 752 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0, |
| 753 | i_size_read(inode) - 1, &cached_state, |
| 754 | GFP_NOFS); |
| 755 | ret = 0; |
| 756 | goto out_free; |
| 757 | } |
| 758 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 759 | /* Zero out the rest of the pages just to make sure */ |
Josef Bacik | be1a12a | 2011-04-06 13:05:22 -0400 | [diff] [blame] | 760 | while (index < num_pages) { |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 761 | void *addr; |
| 762 | |
Josef Bacik | be1a12a | 2011-04-06 13:05:22 -0400 | [diff] [blame] | 763 | page = pages[index]; |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 764 | addr = kmap(page); |
| 765 | memset(addr, 0, PAGE_CACHE_SIZE); |
| 766 | kunmap(page); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 767 | bytes += PAGE_CACHE_SIZE; |
| 768 | index++; |
| 769 | } |
| 770 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 771 | /* Write the checksums and trans id to the first page */ |
| 772 | { |
| 773 | void *addr; |
| 774 | u64 *gen; |
| 775 | |
Josef Bacik | be1a12a | 2011-04-06 13:05:22 -0400 | [diff] [blame] | 776 | page = pages[0]; |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 777 | |
| 778 | addr = kmap(page); |
Josef Bacik | be1a12a | 2011-04-06 13:05:22 -0400 | [diff] [blame] | 779 | memcpy(addr, checksums, sizeof(u32) * num_pages); |
| 780 | gen = addr + (sizeof(u32) * num_pages); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 781 | *gen = trans->transid; |
| 782 | kunmap(page); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 783 | } |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 784 | |
Josef Bacik | be1a12a | 2011-04-06 13:05:22 -0400 | [diff] [blame] | 785 | ret = btrfs_dirty_pages(root, inode, pages, num_pages, 0, |
| 786 | bytes, &cached_state); |
| 787 | btrfs_drop_pages(pages, num_pages); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 788 | unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0, |
| 789 | i_size_read(inode) - 1, &cached_state, GFP_NOFS); |
| 790 | |
Josef Bacik | be1a12a | 2011-04-06 13:05:22 -0400 | [diff] [blame] | 791 | if (ret) { |
| 792 | ret = 0; |
| 793 | goto out_free; |
| 794 | } |
| 795 | |
| 796 | BTRFS_I(inode)->generation = trans->transid; |
| 797 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 798 | filemap_write_and_wait(inode->i_mapping); |
| 799 | |
| 800 | key.objectid = BTRFS_FREE_SPACE_OBJECTID; |
| 801 | key.offset = block_group->key.objectid; |
| 802 | key.type = 0; |
| 803 | |
| 804 | ret = btrfs_search_slot(trans, root, &key, path, 1, 1); |
| 805 | if (ret < 0) { |
| 806 | ret = 0; |
| 807 | clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, bytes - 1, |
| 808 | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 809 | EXTENT_DO_ACCOUNTING, 0, 0, NULL, GFP_NOFS); |
| 810 | goto out_free; |
| 811 | } |
| 812 | leaf = path->nodes[0]; |
| 813 | if (ret > 0) { |
| 814 | struct btrfs_key found_key; |
| 815 | BUG_ON(!path->slots[0]); |
| 816 | path->slots[0]--; |
| 817 | btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]); |
| 818 | if (found_key.objectid != BTRFS_FREE_SPACE_OBJECTID || |
| 819 | found_key.offset != block_group->key.objectid) { |
| 820 | ret = 0; |
| 821 | clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, bytes - 1, |
| 822 | EXTENT_DIRTY | EXTENT_DELALLOC | |
| 823 | EXTENT_DO_ACCOUNTING, 0, 0, NULL, |
| 824 | GFP_NOFS); |
| 825 | btrfs_release_path(root, path); |
| 826 | goto out_free; |
| 827 | } |
| 828 | } |
| 829 | header = btrfs_item_ptr(leaf, path->slots[0], |
| 830 | struct btrfs_free_space_header); |
| 831 | btrfs_set_free_space_entries(leaf, header, entries); |
| 832 | btrfs_set_free_space_bitmaps(leaf, header, bitmaps); |
| 833 | btrfs_set_free_space_generation(leaf, header, trans->transid); |
| 834 | btrfs_mark_buffer_dirty(leaf); |
| 835 | btrfs_release_path(root, path); |
| 836 | |
| 837 | ret = 1; |
| 838 | |
| 839 | out_free: |
| 840 | if (ret == 0) { |
| 841 | invalidate_inode_pages2_range(inode->i_mapping, 0, index); |
| 842 | spin_lock(&block_group->lock); |
| 843 | block_group->disk_cache_state = BTRFS_DC_ERROR; |
| 844 | spin_unlock(&block_group->lock); |
| 845 | BTRFS_I(inode)->generation = 0; |
| 846 | } |
| 847 | kfree(checksums); |
Josef Bacik | be1a12a | 2011-04-06 13:05:22 -0400 | [diff] [blame] | 848 | kfree(pages); |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 849 | btrfs_update_inode(trans, root, inode); |
| 850 | iput(inode); |
| 851 | return ret; |
| 852 | } |
| 853 | |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 854 | static inline unsigned long offset_to_bit(u64 bitmap_start, u64 sectorsize, |
| 855 | u64 offset) |
| 856 | { |
| 857 | BUG_ON(offset < bitmap_start); |
| 858 | offset -= bitmap_start; |
| 859 | return (unsigned long)(div64_u64(offset, sectorsize)); |
| 860 | } |
| 861 | |
| 862 | static inline unsigned long bytes_to_bits(u64 bytes, u64 sectorsize) |
| 863 | { |
| 864 | return (unsigned long)(div64_u64(bytes, sectorsize)); |
| 865 | } |
| 866 | |
| 867 | static inline u64 offset_to_bitmap(struct btrfs_block_group_cache *block_group, |
| 868 | u64 offset) |
| 869 | { |
| 870 | u64 bitmap_start; |
| 871 | u64 bytes_per_bitmap; |
| 872 | |
| 873 | bytes_per_bitmap = BITS_PER_BITMAP * block_group->sectorsize; |
| 874 | bitmap_start = offset - block_group->key.objectid; |
| 875 | bitmap_start = div64_u64(bitmap_start, bytes_per_bitmap); |
| 876 | bitmap_start *= bytes_per_bitmap; |
| 877 | bitmap_start += block_group->key.objectid; |
| 878 | |
| 879 | return bitmap_start; |
| 880 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 881 | |
| 882 | static int tree_insert_offset(struct rb_root *root, u64 offset, |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 883 | struct rb_node *node, int bitmap) |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 884 | { |
| 885 | struct rb_node **p = &root->rb_node; |
| 886 | struct rb_node *parent = NULL; |
| 887 | struct btrfs_free_space *info; |
| 888 | |
| 889 | while (*p) { |
| 890 | parent = *p; |
| 891 | info = rb_entry(parent, struct btrfs_free_space, offset_index); |
| 892 | |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 893 | if (offset < info->offset) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 894 | p = &(*p)->rb_left; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 895 | } else if (offset > info->offset) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 896 | p = &(*p)->rb_right; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 897 | } else { |
| 898 | /* |
| 899 | * we could have a bitmap entry and an extent entry |
| 900 | * share the same offset. If this is the case, we want |
| 901 | * the extent entry to always be found first if we do a |
| 902 | * linear search through the tree, since we want to have |
| 903 | * the quickest allocation time, and allocating from an |
| 904 | * extent is faster than allocating from a bitmap. So |
| 905 | * if we're inserting a bitmap and we find an entry at |
| 906 | * this offset, we want to go right, or after this entry |
| 907 | * logically. If we are inserting an extent and we've |
| 908 | * found a bitmap, we want to go left, or before |
| 909 | * logically. |
| 910 | */ |
| 911 | if (bitmap) { |
| 912 | WARN_ON(info->bitmap); |
| 913 | p = &(*p)->rb_right; |
| 914 | } else { |
| 915 | WARN_ON(!info->bitmap); |
| 916 | p = &(*p)->rb_left; |
| 917 | } |
| 918 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 919 | } |
| 920 | |
| 921 | rb_link_node(node, parent, p); |
| 922 | rb_insert_color(node, root); |
| 923 | |
| 924 | return 0; |
| 925 | } |
| 926 | |
| 927 | /* |
Josef Bacik | 70cb074 | 2009-04-03 10:14:19 -0400 | [diff] [blame] | 928 | * searches the tree for the given offset. |
| 929 | * |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 930 | * fuzzy - If this is set, then we are trying to make an allocation, and we just |
| 931 | * want a section that has at least bytes size and comes at or after the given |
| 932 | * offset. |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 933 | */ |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 934 | static struct btrfs_free_space * |
| 935 | tree_search_offset(struct btrfs_block_group_cache *block_group, |
| 936 | u64 offset, int bitmap_only, int fuzzy) |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 937 | { |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 938 | struct rb_node *n = block_group->free_space_offset.rb_node; |
| 939 | struct btrfs_free_space *entry, *prev = NULL; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 940 | |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 941 | /* find entry that is closest to the 'offset' */ |
| 942 | while (1) { |
| 943 | if (!n) { |
| 944 | entry = NULL; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 945 | break; |
| 946 | } |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 947 | |
| 948 | entry = rb_entry(n, struct btrfs_free_space, offset_index); |
| 949 | prev = entry; |
| 950 | |
| 951 | if (offset < entry->offset) |
| 952 | n = n->rb_left; |
| 953 | else if (offset > entry->offset) |
| 954 | n = n->rb_right; |
| 955 | else |
| 956 | break; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 957 | } |
| 958 | |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 959 | if (bitmap_only) { |
| 960 | if (!entry) |
| 961 | return NULL; |
| 962 | if (entry->bitmap) |
| 963 | return entry; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 964 | |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 965 | /* |
| 966 | * bitmap entry and extent entry may share same offset, |
| 967 | * in that case, bitmap entry comes after extent entry. |
| 968 | */ |
| 969 | n = rb_next(n); |
| 970 | if (!n) |
| 971 | return NULL; |
| 972 | entry = rb_entry(n, struct btrfs_free_space, offset_index); |
| 973 | if (entry->offset != offset) |
| 974 | return NULL; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 975 | |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 976 | WARN_ON(!entry->bitmap); |
| 977 | return entry; |
| 978 | } else if (entry) { |
| 979 | if (entry->bitmap) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 980 | /* |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 981 | * if previous extent entry covers the offset, |
| 982 | * we should return it instead of the bitmap entry |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 983 | */ |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 984 | n = &entry->offset_index; |
| 985 | while (1) { |
| 986 | n = rb_prev(n); |
| 987 | if (!n) |
| 988 | break; |
| 989 | prev = rb_entry(n, struct btrfs_free_space, |
| 990 | offset_index); |
| 991 | if (!prev->bitmap) { |
| 992 | if (prev->offset + prev->bytes > offset) |
| 993 | entry = prev; |
| 994 | break; |
| 995 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 996 | } |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 997 | } |
| 998 | return entry; |
| 999 | } |
| 1000 | |
| 1001 | if (!prev) |
| 1002 | return NULL; |
| 1003 | |
| 1004 | /* find last entry before the 'offset' */ |
| 1005 | entry = prev; |
| 1006 | if (entry->offset > offset) { |
| 1007 | n = rb_prev(&entry->offset_index); |
| 1008 | if (n) { |
| 1009 | entry = rb_entry(n, struct btrfs_free_space, |
| 1010 | offset_index); |
| 1011 | BUG_ON(entry->offset > offset); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1012 | } else { |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1013 | if (fuzzy) |
| 1014 | return entry; |
| 1015 | else |
| 1016 | return NULL; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1017 | } |
| 1018 | } |
| 1019 | |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1020 | if (entry->bitmap) { |
| 1021 | n = &entry->offset_index; |
| 1022 | while (1) { |
| 1023 | n = rb_prev(n); |
| 1024 | if (!n) |
| 1025 | break; |
| 1026 | prev = rb_entry(n, struct btrfs_free_space, |
| 1027 | offset_index); |
| 1028 | if (!prev->bitmap) { |
| 1029 | if (prev->offset + prev->bytes > offset) |
| 1030 | return prev; |
| 1031 | break; |
| 1032 | } |
| 1033 | } |
| 1034 | if (entry->offset + BITS_PER_BITMAP * |
| 1035 | block_group->sectorsize > offset) |
| 1036 | return entry; |
| 1037 | } else if (entry->offset + entry->bytes > offset) |
| 1038 | return entry; |
| 1039 | |
| 1040 | if (!fuzzy) |
| 1041 | return NULL; |
| 1042 | |
| 1043 | while (1) { |
| 1044 | if (entry->bitmap) { |
| 1045 | if (entry->offset + BITS_PER_BITMAP * |
| 1046 | block_group->sectorsize > offset) |
| 1047 | break; |
| 1048 | } else { |
| 1049 | if (entry->offset + entry->bytes > offset) |
| 1050 | break; |
| 1051 | } |
| 1052 | |
| 1053 | n = rb_next(&entry->offset_index); |
| 1054 | if (!n) |
| 1055 | return NULL; |
| 1056 | entry = rb_entry(n, struct btrfs_free_space, offset_index); |
| 1057 | } |
| 1058 | return entry; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1059 | } |
| 1060 | |
Li Zefan | f333adb | 2010-11-09 14:57:39 +0800 | [diff] [blame] | 1061 | static inline void |
| 1062 | __unlink_free_space(struct btrfs_block_group_cache *block_group, |
| 1063 | struct btrfs_free_space *info) |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1064 | { |
| 1065 | rb_erase(&info->offset_index, &block_group->free_space_offset); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1066 | block_group->free_extents--; |
Li Zefan | f333adb | 2010-11-09 14:57:39 +0800 | [diff] [blame] | 1067 | } |
| 1068 | |
| 1069 | static void unlink_free_space(struct btrfs_block_group_cache *block_group, |
| 1070 | struct btrfs_free_space *info) |
| 1071 | { |
| 1072 | __unlink_free_space(block_group, info); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1073 | block_group->free_space -= info->bytes; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1074 | } |
| 1075 | |
| 1076 | static int link_free_space(struct btrfs_block_group_cache *block_group, |
| 1077 | struct btrfs_free_space *info) |
| 1078 | { |
| 1079 | int ret = 0; |
| 1080 | |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1081 | BUG_ON(!info->bitmap && !info->bytes); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1082 | ret = tree_insert_offset(&block_group->free_space_offset, info->offset, |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1083 | &info->offset_index, (info->bitmap != NULL)); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1084 | if (ret) |
| 1085 | return ret; |
| 1086 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1087 | block_group->free_space += info->bytes; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1088 | block_group->free_extents++; |
| 1089 | return ret; |
| 1090 | } |
| 1091 | |
| 1092 | static void recalculate_thresholds(struct btrfs_block_group_cache *block_group) |
| 1093 | { |
Josef Bacik | 25891f7 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 1094 | u64 max_bytes; |
| 1095 | u64 bitmap_bytes; |
| 1096 | u64 extent_bytes; |
Li Zefan | 8eb2d82 | 2010-11-09 14:48:01 +0800 | [diff] [blame] | 1097 | u64 size = block_group->key.offset; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1098 | |
| 1099 | /* |
| 1100 | * The goal is to keep the total amount of memory used per 1gb of space |
| 1101 | * at or below 32k, so we need to adjust how much memory we allow to be |
| 1102 | * used by extent based free space tracking |
| 1103 | */ |
Li Zefan | 8eb2d82 | 2010-11-09 14:48:01 +0800 | [diff] [blame] | 1104 | if (size < 1024 * 1024 * 1024) |
| 1105 | max_bytes = MAX_CACHE_BYTES_PER_GIG; |
| 1106 | else |
| 1107 | max_bytes = MAX_CACHE_BYTES_PER_GIG * |
| 1108 | div64_u64(size, 1024 * 1024 * 1024); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1109 | |
Josef Bacik | 25891f7 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 1110 | /* |
| 1111 | * we want to account for 1 more bitmap than what we have so we can make |
| 1112 | * sure we don't go over our overall goal of MAX_CACHE_BYTES_PER_GIG as |
| 1113 | * we add more bitmaps. |
| 1114 | */ |
| 1115 | bitmap_bytes = (block_group->total_bitmaps + 1) * PAGE_CACHE_SIZE; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1116 | |
Josef Bacik | 25891f7 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 1117 | if (bitmap_bytes >= max_bytes) { |
| 1118 | block_group->extents_thresh = 0; |
| 1119 | return; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1120 | } |
Josef Bacik | 25891f7 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 1121 | |
| 1122 | /* |
| 1123 | * we want the extent entry threshold to always be at most 1/2 the maxw |
| 1124 | * bytes we can have, or whatever is less than that. |
| 1125 | */ |
| 1126 | extent_bytes = max_bytes - bitmap_bytes; |
| 1127 | extent_bytes = min_t(u64, extent_bytes, div64_u64(max_bytes, 2)); |
| 1128 | |
| 1129 | block_group->extents_thresh = |
| 1130 | div64_u64(extent_bytes, (sizeof(struct btrfs_free_space))); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1131 | } |
| 1132 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1133 | static void bitmap_clear_bits(struct btrfs_block_group_cache *block_group, |
| 1134 | struct btrfs_free_space *info, u64 offset, |
| 1135 | u64 bytes) |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1136 | { |
| 1137 | unsigned long start, end; |
| 1138 | unsigned long i; |
| 1139 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1140 | start = offset_to_bit(info->offset, block_group->sectorsize, offset); |
| 1141 | end = start + bytes_to_bits(bytes, block_group->sectorsize); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1142 | BUG_ON(end > BITS_PER_BITMAP); |
| 1143 | |
| 1144 | for (i = start; i < end; i++) |
| 1145 | clear_bit(i, info->bitmap); |
| 1146 | |
| 1147 | info->bytes -= bytes; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1148 | block_group->free_space -= bytes; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1149 | } |
| 1150 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1151 | static void bitmap_set_bits(struct btrfs_block_group_cache *block_group, |
| 1152 | struct btrfs_free_space *info, u64 offset, |
| 1153 | u64 bytes) |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1154 | { |
| 1155 | unsigned long start, end; |
| 1156 | unsigned long i; |
| 1157 | |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1158 | start = offset_to_bit(info->offset, block_group->sectorsize, offset); |
| 1159 | end = start + bytes_to_bits(bytes, block_group->sectorsize); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1160 | BUG_ON(end > BITS_PER_BITMAP); |
| 1161 | |
| 1162 | for (i = start; i < end; i++) |
| 1163 | set_bit(i, info->bitmap); |
| 1164 | |
| 1165 | info->bytes += bytes; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1166 | block_group->free_space += bytes; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1167 | } |
| 1168 | |
| 1169 | static int search_bitmap(struct btrfs_block_group_cache *block_group, |
| 1170 | struct btrfs_free_space *bitmap_info, u64 *offset, |
| 1171 | u64 *bytes) |
| 1172 | { |
| 1173 | unsigned long found_bits = 0; |
| 1174 | unsigned long bits, i; |
| 1175 | unsigned long next_zero; |
| 1176 | |
| 1177 | i = offset_to_bit(bitmap_info->offset, block_group->sectorsize, |
| 1178 | max_t(u64, *offset, bitmap_info->offset)); |
| 1179 | bits = bytes_to_bits(*bytes, block_group->sectorsize); |
| 1180 | |
| 1181 | for (i = find_next_bit(bitmap_info->bitmap, BITS_PER_BITMAP, i); |
| 1182 | i < BITS_PER_BITMAP; |
| 1183 | i = find_next_bit(bitmap_info->bitmap, BITS_PER_BITMAP, i + 1)) { |
| 1184 | next_zero = find_next_zero_bit(bitmap_info->bitmap, |
| 1185 | BITS_PER_BITMAP, i); |
| 1186 | if ((next_zero - i) >= bits) { |
| 1187 | found_bits = next_zero - i; |
| 1188 | break; |
| 1189 | } |
| 1190 | i = next_zero; |
| 1191 | } |
| 1192 | |
| 1193 | if (found_bits) { |
| 1194 | *offset = (u64)(i * block_group->sectorsize) + |
| 1195 | bitmap_info->offset; |
| 1196 | *bytes = (u64)(found_bits) * block_group->sectorsize; |
| 1197 | return 0; |
| 1198 | } |
| 1199 | |
| 1200 | return -1; |
| 1201 | } |
| 1202 | |
| 1203 | static struct btrfs_free_space *find_free_space(struct btrfs_block_group_cache |
| 1204 | *block_group, u64 *offset, |
| 1205 | u64 *bytes, int debug) |
| 1206 | { |
| 1207 | struct btrfs_free_space *entry; |
| 1208 | struct rb_node *node; |
| 1209 | int ret; |
| 1210 | |
| 1211 | if (!block_group->free_space_offset.rb_node) |
| 1212 | return NULL; |
| 1213 | |
| 1214 | entry = tree_search_offset(block_group, |
| 1215 | offset_to_bitmap(block_group, *offset), |
| 1216 | 0, 1); |
| 1217 | if (!entry) |
| 1218 | return NULL; |
| 1219 | |
| 1220 | for (node = &entry->offset_index; node; node = rb_next(node)) { |
| 1221 | entry = rb_entry(node, struct btrfs_free_space, offset_index); |
| 1222 | if (entry->bytes < *bytes) |
| 1223 | continue; |
| 1224 | |
| 1225 | if (entry->bitmap) { |
| 1226 | ret = search_bitmap(block_group, entry, offset, bytes); |
| 1227 | if (!ret) |
| 1228 | return entry; |
| 1229 | continue; |
| 1230 | } |
| 1231 | |
| 1232 | *offset = entry->offset; |
| 1233 | *bytes = entry->bytes; |
| 1234 | return entry; |
| 1235 | } |
| 1236 | |
| 1237 | return NULL; |
| 1238 | } |
| 1239 | |
| 1240 | static void add_new_bitmap(struct btrfs_block_group_cache *block_group, |
| 1241 | struct btrfs_free_space *info, u64 offset) |
| 1242 | { |
| 1243 | u64 bytes_per_bg = BITS_PER_BITMAP * block_group->sectorsize; |
| 1244 | int max_bitmaps = (int)div64_u64(block_group->key.offset + |
| 1245 | bytes_per_bg - 1, bytes_per_bg); |
| 1246 | BUG_ON(block_group->total_bitmaps >= max_bitmaps); |
| 1247 | |
| 1248 | info->offset = offset_to_bitmap(block_group, offset); |
Josef Bacik | f019f42 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 1249 | info->bytes = 0; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1250 | link_free_space(block_group, info); |
| 1251 | block_group->total_bitmaps++; |
| 1252 | |
| 1253 | recalculate_thresholds(block_group); |
| 1254 | } |
| 1255 | |
Li Zefan | edf6e2d | 2010-11-09 14:50:07 +0800 | [diff] [blame] | 1256 | static void free_bitmap(struct btrfs_block_group_cache *block_group, |
| 1257 | struct btrfs_free_space *bitmap_info) |
| 1258 | { |
| 1259 | unlink_free_space(block_group, bitmap_info); |
| 1260 | kfree(bitmap_info->bitmap); |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 1261 | kmem_cache_free(btrfs_free_space_cachep, bitmap_info); |
Li Zefan | edf6e2d | 2010-11-09 14:50:07 +0800 | [diff] [blame] | 1262 | block_group->total_bitmaps--; |
| 1263 | recalculate_thresholds(block_group); |
| 1264 | } |
| 1265 | |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1266 | static noinline int remove_from_bitmap(struct btrfs_block_group_cache *block_group, |
| 1267 | struct btrfs_free_space *bitmap_info, |
| 1268 | u64 *offset, u64 *bytes) |
| 1269 | { |
| 1270 | u64 end; |
Josef Bacik | 6606bb9 | 2009-07-31 11:03:58 -0400 | [diff] [blame] | 1271 | u64 search_start, search_bytes; |
| 1272 | int ret; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1273 | |
| 1274 | again: |
| 1275 | end = bitmap_info->offset + |
| 1276 | (u64)(BITS_PER_BITMAP * block_group->sectorsize) - 1; |
| 1277 | |
Josef Bacik | 6606bb9 | 2009-07-31 11:03:58 -0400 | [diff] [blame] | 1278 | /* |
| 1279 | * XXX - this can go away after a few releases. |
| 1280 | * |
| 1281 | * since the only user of btrfs_remove_free_space is the tree logging |
| 1282 | * stuff, and the only way to test that is under crash conditions, we |
| 1283 | * want to have this debug stuff here just in case somethings not |
| 1284 | * working. Search the bitmap for the space we are trying to use to |
| 1285 | * make sure its actually there. If its not there then we need to stop |
| 1286 | * because something has gone wrong. |
| 1287 | */ |
| 1288 | search_start = *offset; |
| 1289 | search_bytes = *bytes; |
Josef Bacik | 13dbc08 | 2011-02-03 02:39:52 +0000 | [diff] [blame] | 1290 | search_bytes = min(search_bytes, end - search_start + 1); |
Josef Bacik | 6606bb9 | 2009-07-31 11:03:58 -0400 | [diff] [blame] | 1291 | ret = search_bitmap(block_group, bitmap_info, &search_start, |
| 1292 | &search_bytes); |
| 1293 | BUG_ON(ret < 0 || search_start != *offset); |
| 1294 | |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1295 | if (*offset > bitmap_info->offset && *offset + *bytes > end) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1296 | bitmap_clear_bits(block_group, bitmap_info, *offset, |
| 1297 | end - *offset + 1); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1298 | *bytes -= end - *offset + 1; |
| 1299 | *offset = end + 1; |
| 1300 | } else if (*offset >= bitmap_info->offset && *offset + *bytes <= end) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1301 | bitmap_clear_bits(block_group, bitmap_info, *offset, *bytes); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1302 | *bytes = 0; |
| 1303 | } |
| 1304 | |
| 1305 | if (*bytes) { |
Josef Bacik | 6606bb9 | 2009-07-31 11:03:58 -0400 | [diff] [blame] | 1306 | struct rb_node *next = rb_next(&bitmap_info->offset_index); |
Li Zefan | edf6e2d | 2010-11-09 14:50:07 +0800 | [diff] [blame] | 1307 | if (!bitmap_info->bytes) |
| 1308 | free_bitmap(block_group, bitmap_info); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1309 | |
Josef Bacik | 6606bb9 | 2009-07-31 11:03:58 -0400 | [diff] [blame] | 1310 | /* |
| 1311 | * no entry after this bitmap, but we still have bytes to |
| 1312 | * remove, so something has gone wrong. |
| 1313 | */ |
| 1314 | if (!next) |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1315 | return -EINVAL; |
| 1316 | |
Josef Bacik | 6606bb9 | 2009-07-31 11:03:58 -0400 | [diff] [blame] | 1317 | bitmap_info = rb_entry(next, struct btrfs_free_space, |
| 1318 | offset_index); |
| 1319 | |
| 1320 | /* |
| 1321 | * if the next entry isn't a bitmap we need to return to let the |
| 1322 | * extent stuff do its work. |
| 1323 | */ |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1324 | if (!bitmap_info->bitmap) |
| 1325 | return -EAGAIN; |
| 1326 | |
Josef Bacik | 6606bb9 | 2009-07-31 11:03:58 -0400 | [diff] [blame] | 1327 | /* |
| 1328 | * Ok the next item is a bitmap, but it may not actually hold |
| 1329 | * the information for the rest of this free space stuff, so |
| 1330 | * look for it, and if we don't find it return so we can try |
| 1331 | * everything over again. |
| 1332 | */ |
| 1333 | search_start = *offset; |
| 1334 | search_bytes = *bytes; |
| 1335 | ret = search_bitmap(block_group, bitmap_info, &search_start, |
| 1336 | &search_bytes); |
| 1337 | if (ret < 0 || search_start != *offset) |
| 1338 | return -EAGAIN; |
| 1339 | |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1340 | goto again; |
Li Zefan | edf6e2d | 2010-11-09 14:50:07 +0800 | [diff] [blame] | 1341 | } else if (!bitmap_info->bytes) |
| 1342 | free_bitmap(block_group, bitmap_info); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1343 | |
| 1344 | return 0; |
| 1345 | } |
| 1346 | |
| 1347 | static int insert_into_bitmap(struct btrfs_block_group_cache *block_group, |
| 1348 | struct btrfs_free_space *info) |
| 1349 | { |
| 1350 | struct btrfs_free_space *bitmap_info; |
| 1351 | int added = 0; |
| 1352 | u64 bytes, offset, end; |
| 1353 | int ret; |
| 1354 | |
| 1355 | /* |
| 1356 | * If we are below the extents threshold then we can add this as an |
| 1357 | * extent, and don't have to deal with the bitmap |
| 1358 | */ |
Josef Bacik | 32cb084 | 2011-03-18 16:16:21 -0400 | [diff] [blame] | 1359 | if (block_group->free_extents < block_group->extents_thresh) { |
| 1360 | /* |
| 1361 | * If this block group has some small extents we don't want to |
| 1362 | * use up all of our free slots in the cache with them, we want |
| 1363 | * to reserve them to larger extents, however if we have plent |
| 1364 | * of cache left then go ahead an dadd them, no sense in adding |
| 1365 | * the overhead of a bitmap if we don't have to. |
| 1366 | */ |
| 1367 | if (info->bytes <= block_group->sectorsize * 4) { |
| 1368 | if (block_group->free_extents * 2 <= |
| 1369 | block_group->extents_thresh) |
| 1370 | return 0; |
| 1371 | } else { |
| 1372 | return 0; |
| 1373 | } |
| 1374 | } |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1375 | |
| 1376 | /* |
| 1377 | * some block groups are so tiny they can't be enveloped by a bitmap, so |
| 1378 | * don't even bother to create a bitmap for this |
| 1379 | */ |
| 1380 | if (BITS_PER_BITMAP * block_group->sectorsize > |
| 1381 | block_group->key.offset) |
| 1382 | return 0; |
| 1383 | |
| 1384 | bytes = info->bytes; |
| 1385 | offset = info->offset; |
| 1386 | |
| 1387 | again: |
| 1388 | bitmap_info = tree_search_offset(block_group, |
| 1389 | offset_to_bitmap(block_group, offset), |
| 1390 | 1, 0); |
| 1391 | if (!bitmap_info) { |
| 1392 | BUG_ON(added); |
| 1393 | goto new_bitmap; |
| 1394 | } |
| 1395 | |
| 1396 | end = bitmap_info->offset + |
| 1397 | (u64)(BITS_PER_BITMAP * block_group->sectorsize); |
| 1398 | |
| 1399 | if (offset >= bitmap_info->offset && offset + bytes > end) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1400 | bitmap_set_bits(block_group, bitmap_info, offset, |
| 1401 | end - offset); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1402 | bytes -= end - offset; |
| 1403 | offset = end; |
| 1404 | added = 0; |
| 1405 | } else if (offset >= bitmap_info->offset && offset + bytes <= end) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1406 | bitmap_set_bits(block_group, bitmap_info, offset, bytes); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1407 | bytes = 0; |
| 1408 | } else { |
| 1409 | BUG(); |
| 1410 | } |
| 1411 | |
| 1412 | if (!bytes) { |
| 1413 | ret = 1; |
| 1414 | goto out; |
| 1415 | } else |
| 1416 | goto again; |
| 1417 | |
| 1418 | new_bitmap: |
| 1419 | if (info && info->bitmap) { |
| 1420 | add_new_bitmap(block_group, info, offset); |
| 1421 | added = 1; |
| 1422 | info = NULL; |
| 1423 | goto again; |
| 1424 | } else { |
| 1425 | spin_unlock(&block_group->tree_lock); |
| 1426 | |
| 1427 | /* no pre-allocated info, allocate a new one */ |
| 1428 | if (!info) { |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 1429 | info = kmem_cache_zalloc(btrfs_free_space_cachep, |
| 1430 | GFP_NOFS); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1431 | if (!info) { |
| 1432 | spin_lock(&block_group->tree_lock); |
| 1433 | ret = -ENOMEM; |
| 1434 | goto out; |
| 1435 | } |
| 1436 | } |
| 1437 | |
| 1438 | /* allocate the bitmap */ |
| 1439 | info->bitmap = kzalloc(PAGE_CACHE_SIZE, GFP_NOFS); |
| 1440 | spin_lock(&block_group->tree_lock); |
| 1441 | if (!info->bitmap) { |
| 1442 | ret = -ENOMEM; |
| 1443 | goto out; |
| 1444 | } |
| 1445 | goto again; |
| 1446 | } |
| 1447 | |
| 1448 | out: |
| 1449 | if (info) { |
| 1450 | if (info->bitmap) |
| 1451 | kfree(info->bitmap); |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 1452 | kmem_cache_free(btrfs_free_space_cachep, info); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1453 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1454 | |
| 1455 | return ret; |
| 1456 | } |
| 1457 | |
Li Zefan | 120d66e | 2010-11-09 14:56:50 +0800 | [diff] [blame] | 1458 | bool try_merge_free_space(struct btrfs_block_group_cache *block_group, |
Li Zefan | f333adb | 2010-11-09 14:57:39 +0800 | [diff] [blame] | 1459 | struct btrfs_free_space *info, bool update_stat) |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1460 | { |
Li Zefan | 120d66e | 2010-11-09 14:56:50 +0800 | [diff] [blame] | 1461 | struct btrfs_free_space *left_info; |
| 1462 | struct btrfs_free_space *right_info; |
| 1463 | bool merged = false; |
| 1464 | u64 offset = info->offset; |
| 1465 | u64 bytes = info->bytes; |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 1466 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1467 | /* |
| 1468 | * first we want to see if there is free space adjacent to the range we |
| 1469 | * are adding, if there is remove that struct and add a new one to |
| 1470 | * cover the entire range |
| 1471 | */ |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1472 | right_info = tree_search_offset(block_group, offset + bytes, 0, 0); |
| 1473 | if (right_info && rb_prev(&right_info->offset_index)) |
| 1474 | left_info = rb_entry(rb_prev(&right_info->offset_index), |
| 1475 | struct btrfs_free_space, offset_index); |
| 1476 | else |
| 1477 | left_info = tree_search_offset(block_group, offset - 1, 0, 0); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1478 | |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1479 | if (right_info && !right_info->bitmap) { |
Li Zefan | f333adb | 2010-11-09 14:57:39 +0800 | [diff] [blame] | 1480 | if (update_stat) |
| 1481 | unlink_free_space(block_group, right_info); |
| 1482 | else |
| 1483 | __unlink_free_space(block_group, right_info); |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 1484 | info->bytes += right_info->bytes; |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 1485 | kmem_cache_free(btrfs_free_space_cachep, right_info); |
Li Zefan | 120d66e | 2010-11-09 14:56:50 +0800 | [diff] [blame] | 1486 | merged = true; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1487 | } |
| 1488 | |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1489 | if (left_info && !left_info->bitmap && |
| 1490 | left_info->offset + left_info->bytes == offset) { |
Li Zefan | f333adb | 2010-11-09 14:57:39 +0800 | [diff] [blame] | 1491 | if (update_stat) |
| 1492 | unlink_free_space(block_group, left_info); |
| 1493 | else |
| 1494 | __unlink_free_space(block_group, left_info); |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 1495 | info->offset = left_info->offset; |
| 1496 | info->bytes += left_info->bytes; |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 1497 | kmem_cache_free(btrfs_free_space_cachep, left_info); |
Li Zefan | 120d66e | 2010-11-09 14:56:50 +0800 | [diff] [blame] | 1498 | merged = true; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1499 | } |
| 1500 | |
Li Zefan | 120d66e | 2010-11-09 14:56:50 +0800 | [diff] [blame] | 1501 | return merged; |
| 1502 | } |
| 1503 | |
| 1504 | int btrfs_add_free_space(struct btrfs_block_group_cache *block_group, |
| 1505 | u64 offset, u64 bytes) |
| 1506 | { |
| 1507 | struct btrfs_free_space *info; |
| 1508 | int ret = 0; |
| 1509 | |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 1510 | info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS); |
Li Zefan | 120d66e | 2010-11-09 14:56:50 +0800 | [diff] [blame] | 1511 | if (!info) |
| 1512 | return -ENOMEM; |
| 1513 | |
| 1514 | info->offset = offset; |
| 1515 | info->bytes = bytes; |
| 1516 | |
| 1517 | spin_lock(&block_group->tree_lock); |
| 1518 | |
Li Zefan | f333adb | 2010-11-09 14:57:39 +0800 | [diff] [blame] | 1519 | if (try_merge_free_space(block_group, info, true)) |
Li Zefan | 120d66e | 2010-11-09 14:56:50 +0800 | [diff] [blame] | 1520 | goto link; |
| 1521 | |
| 1522 | /* |
| 1523 | * There was no extent directly to the left or right of this new |
| 1524 | * extent then we know we're going to have to allocate a new extent, so |
| 1525 | * before we do that see if we need to drop this into a bitmap |
| 1526 | */ |
| 1527 | ret = insert_into_bitmap(block_group, info); |
| 1528 | if (ret < 0) { |
| 1529 | goto out; |
| 1530 | } else if (ret) { |
| 1531 | ret = 0; |
| 1532 | goto out; |
| 1533 | } |
| 1534 | link: |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1535 | ret = link_free_space(block_group, info); |
| 1536 | if (ret) |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 1537 | kmem_cache_free(btrfs_free_space_cachep, info); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1538 | out: |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 1539 | spin_unlock(&block_group->tree_lock); |
| 1540 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1541 | if (ret) { |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1542 | printk(KERN_CRIT "btrfs: unable to add free space :%d\n", ret); |
Stoyan Gaydarov | c293498 | 2009-04-02 17:05:11 -0400 | [diff] [blame] | 1543 | BUG_ON(ret == -EEXIST); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1544 | } |
| 1545 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1546 | return ret; |
| 1547 | } |
| 1548 | |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 1549 | int btrfs_remove_free_space(struct btrfs_block_group_cache *block_group, |
| 1550 | u64 offset, u64 bytes) |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1551 | { |
| 1552 | struct btrfs_free_space *info; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1553 | struct btrfs_free_space *next_info = NULL; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1554 | int ret = 0; |
| 1555 | |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 1556 | spin_lock(&block_group->tree_lock); |
| 1557 | |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1558 | again: |
| 1559 | info = tree_search_offset(block_group, offset, 0, 0); |
| 1560 | if (!info) { |
Josef Bacik | 6606bb9 | 2009-07-31 11:03:58 -0400 | [diff] [blame] | 1561 | /* |
| 1562 | * oops didn't find an extent that matched the space we wanted |
| 1563 | * to remove, look for a bitmap instead |
| 1564 | */ |
| 1565 | info = tree_search_offset(block_group, |
| 1566 | offset_to_bitmap(block_group, offset), |
| 1567 | 1, 0); |
| 1568 | if (!info) { |
| 1569 | WARN_ON(1); |
| 1570 | goto out_lock; |
| 1571 | } |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1572 | } |
| 1573 | |
| 1574 | if (info->bytes < bytes && rb_next(&info->offset_index)) { |
| 1575 | u64 end; |
| 1576 | next_info = rb_entry(rb_next(&info->offset_index), |
| 1577 | struct btrfs_free_space, |
| 1578 | offset_index); |
| 1579 | |
| 1580 | if (next_info->bitmap) |
| 1581 | end = next_info->offset + BITS_PER_BITMAP * |
| 1582 | block_group->sectorsize - 1; |
| 1583 | else |
| 1584 | end = next_info->offset + next_info->bytes; |
| 1585 | |
| 1586 | if (next_info->bytes < bytes || |
| 1587 | next_info->offset > offset || offset > end) { |
| 1588 | printk(KERN_CRIT "Found free space at %llu, size %llu," |
| 1589 | " trying to use %llu\n", |
| 1590 | (unsigned long long)info->offset, |
| 1591 | (unsigned long long)info->bytes, |
| 1592 | (unsigned long long)bytes); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1593 | WARN_ON(1); |
| 1594 | ret = -EINVAL; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1595 | goto out_lock; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1596 | } |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1597 | |
| 1598 | info = next_info; |
| 1599 | } |
| 1600 | |
| 1601 | if (info->bytes == bytes) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1602 | unlink_free_space(block_group, info); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1603 | if (info->bitmap) { |
| 1604 | kfree(info->bitmap); |
| 1605 | block_group->total_bitmaps--; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1606 | } |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 1607 | kmem_cache_free(btrfs_free_space_cachep, info); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1608 | goto out_lock; |
| 1609 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1610 | |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1611 | if (!info->bitmap && info->offset == offset) { |
| 1612 | unlink_free_space(block_group, info); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1613 | info->offset += bytes; |
| 1614 | info->bytes -= bytes; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1615 | link_free_space(block_group, info); |
| 1616 | goto out_lock; |
| 1617 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1618 | |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1619 | if (!info->bitmap && info->offset <= offset && |
| 1620 | info->offset + info->bytes >= offset + bytes) { |
Chris Mason | 9b49c9b | 2008-09-24 11:23:25 -0400 | [diff] [blame] | 1621 | u64 old_start = info->offset; |
| 1622 | /* |
| 1623 | * we're freeing space in the middle of the info, |
| 1624 | * this can happen during tree log replay |
| 1625 | * |
| 1626 | * first unlink the old info and then |
| 1627 | * insert it again after the hole we're creating |
| 1628 | */ |
| 1629 | unlink_free_space(block_group, info); |
| 1630 | if (offset + bytes < info->offset + info->bytes) { |
| 1631 | u64 old_end = info->offset + info->bytes; |
| 1632 | |
| 1633 | info->offset = offset + bytes; |
| 1634 | info->bytes = old_end - info->offset; |
| 1635 | ret = link_free_space(block_group, info); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1636 | WARN_ON(ret); |
| 1637 | if (ret) |
| 1638 | goto out_lock; |
Chris Mason | 9b49c9b | 2008-09-24 11:23:25 -0400 | [diff] [blame] | 1639 | } else { |
| 1640 | /* the hole we're creating ends at the end |
| 1641 | * of the info struct, just free the info |
| 1642 | */ |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 1643 | kmem_cache_free(btrfs_free_space_cachep, info); |
Chris Mason | 9b49c9b | 2008-09-24 11:23:25 -0400 | [diff] [blame] | 1644 | } |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 1645 | spin_unlock(&block_group->tree_lock); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1646 | |
| 1647 | /* step two, insert a new info struct to cover |
| 1648 | * anything before the hole |
Chris Mason | 9b49c9b | 2008-09-24 11:23:25 -0400 | [diff] [blame] | 1649 | */ |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 1650 | ret = btrfs_add_free_space(block_group, old_start, |
| 1651 | offset - old_start); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1652 | WARN_ON(ret); |
| 1653 | goto out; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1654 | } |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1655 | |
| 1656 | ret = remove_from_bitmap(block_group, info, &offset, &bytes); |
| 1657 | if (ret == -EAGAIN) |
| 1658 | goto again; |
| 1659 | BUG_ON(ret); |
| 1660 | out_lock: |
| 1661 | spin_unlock(&block_group->tree_lock); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1662 | out: |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1663 | return ret; |
| 1664 | } |
| 1665 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1666 | void btrfs_dump_free_space(struct btrfs_block_group_cache *block_group, |
| 1667 | u64 bytes) |
| 1668 | { |
| 1669 | struct btrfs_free_space *info; |
| 1670 | struct rb_node *n; |
| 1671 | int count = 0; |
| 1672 | |
| 1673 | for (n = rb_first(&block_group->free_space_offset); n; n = rb_next(n)) { |
| 1674 | info = rb_entry(n, struct btrfs_free_space, offset_index); |
| 1675 | if (info->bytes >= bytes) |
| 1676 | count++; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1677 | printk(KERN_CRIT "entry offset %llu, bytes %llu, bitmap %s\n", |
Joel Becker | 2138093 | 2009-04-21 12:38:29 -0700 | [diff] [blame] | 1678 | (unsigned long long)info->offset, |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1679 | (unsigned long long)info->bytes, |
| 1680 | (info->bitmap) ? "yes" : "no"); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1681 | } |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1682 | printk(KERN_INFO "block group has cluster?: %s\n", |
| 1683 | list_empty(&block_group->cluster_list) ? "no" : "yes"); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1684 | printk(KERN_INFO "%d blocks of free space at or bigger than bytes is" |
| 1685 | "\n", count); |
| 1686 | } |
| 1687 | |
| 1688 | u64 btrfs_block_group_free_space(struct btrfs_block_group_cache *block_group) |
| 1689 | { |
| 1690 | struct btrfs_free_space *info; |
| 1691 | struct rb_node *n; |
| 1692 | u64 ret = 0; |
| 1693 | |
| 1694 | for (n = rb_first(&block_group->free_space_offset); n; |
| 1695 | n = rb_next(n)) { |
| 1696 | info = rb_entry(n, struct btrfs_free_space, offset_index); |
| 1697 | ret += info->bytes; |
| 1698 | } |
| 1699 | |
| 1700 | return ret; |
| 1701 | } |
| 1702 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 1703 | /* |
| 1704 | * for a given cluster, put all of its extents back into the free |
| 1705 | * space cache. If the block group passed doesn't match the block group |
| 1706 | * pointed to by the cluster, someone else raced in and freed the |
| 1707 | * cluster already. In that case, we just return without changing anything |
| 1708 | */ |
| 1709 | static int |
| 1710 | __btrfs_return_cluster_to_free_space( |
| 1711 | struct btrfs_block_group_cache *block_group, |
| 1712 | struct btrfs_free_cluster *cluster) |
| 1713 | { |
| 1714 | struct btrfs_free_space *entry; |
| 1715 | struct rb_node *node; |
| 1716 | |
| 1717 | spin_lock(&cluster->lock); |
| 1718 | if (cluster->block_group != block_group) |
| 1719 | goto out; |
| 1720 | |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1721 | cluster->block_group = NULL; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 1722 | cluster->window_start = 0; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1723 | list_del_init(&cluster->block_group_list); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1724 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 1725 | node = rb_first(&cluster->root); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1726 | while (node) { |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 1727 | bool bitmap; |
| 1728 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 1729 | entry = rb_entry(node, struct btrfs_free_space, offset_index); |
| 1730 | node = rb_next(&entry->offset_index); |
| 1731 | rb_erase(&entry->offset_index, &cluster->root); |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 1732 | |
| 1733 | bitmap = (entry->bitmap != NULL); |
| 1734 | if (!bitmap) |
| 1735 | try_merge_free_space(block_group, entry, false); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1736 | tree_insert_offset(&block_group->free_space_offset, |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 1737 | entry->offset, &entry->offset_index, bitmap); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 1738 | } |
Eric Paris | 6bef4d3 | 2010-02-23 19:43:04 +0000 | [diff] [blame] | 1739 | cluster->root = RB_ROOT; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1740 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 1741 | out: |
| 1742 | spin_unlock(&cluster->lock); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1743 | btrfs_put_block_group(block_group); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 1744 | return 0; |
| 1745 | } |
| 1746 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1747 | void btrfs_remove_free_space_cache(struct btrfs_block_group_cache *block_group) |
| 1748 | { |
| 1749 | struct btrfs_free_space *info; |
| 1750 | struct rb_node *node; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 1751 | struct btrfs_free_cluster *cluster; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1752 | struct list_head *head; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1753 | |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 1754 | spin_lock(&block_group->tree_lock); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1755 | while ((head = block_group->cluster_list.next) != |
| 1756 | &block_group->cluster_list) { |
| 1757 | cluster = list_entry(head, struct btrfs_free_cluster, |
| 1758 | block_group_list); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 1759 | |
| 1760 | WARN_ON(cluster->block_group != block_group); |
| 1761 | __btrfs_return_cluster_to_free_space(block_group, cluster); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1762 | if (need_resched()) { |
| 1763 | spin_unlock(&block_group->tree_lock); |
| 1764 | cond_resched(); |
| 1765 | spin_lock(&block_group->tree_lock); |
| 1766 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 1767 | } |
| 1768 | |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1769 | while ((node = rb_last(&block_group->free_space_offset)) != NULL) { |
| 1770 | info = rb_entry(node, struct btrfs_free_space, offset_index); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1771 | unlink_free_space(block_group, info); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1772 | if (info->bitmap) |
| 1773 | kfree(info->bitmap); |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 1774 | kmem_cache_free(btrfs_free_space_cachep, info); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1775 | if (need_resched()) { |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 1776 | spin_unlock(&block_group->tree_lock); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1777 | cond_resched(); |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 1778 | spin_lock(&block_group->tree_lock); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1779 | } |
| 1780 | } |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1781 | |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 1782 | spin_unlock(&block_group->tree_lock); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1783 | } |
| 1784 | |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 1785 | u64 btrfs_find_space_for_alloc(struct btrfs_block_group_cache *block_group, |
| 1786 | u64 offset, u64 bytes, u64 empty_size) |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1787 | { |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 1788 | struct btrfs_free_space *entry = NULL; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1789 | u64 bytes_search = bytes + empty_size; |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 1790 | u64 ret = 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1791 | |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 1792 | spin_lock(&block_group->tree_lock); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1793 | entry = find_free_space(block_group, &offset, &bytes_search, 0); |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 1794 | if (!entry) |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1795 | goto out; |
| 1796 | |
| 1797 | ret = offset; |
| 1798 | if (entry->bitmap) { |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1799 | bitmap_clear_bits(block_group, entry, offset, bytes); |
Li Zefan | edf6e2d | 2010-11-09 14:50:07 +0800 | [diff] [blame] | 1800 | if (!entry->bytes) |
| 1801 | free_bitmap(block_group, entry); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1802 | } else { |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 1803 | unlink_free_space(block_group, entry); |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 1804 | entry->offset += bytes; |
| 1805 | entry->bytes -= bytes; |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 1806 | if (!entry->bytes) |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 1807 | kmem_cache_free(btrfs_free_space_cachep, entry); |
Josef Bacik | 6226cb0 | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 1808 | else |
| 1809 | link_free_space(block_group, entry); |
| 1810 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1811 | |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1812 | out: |
| 1813 | spin_unlock(&block_group->tree_lock); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1814 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1815 | return ret; |
| 1816 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 1817 | |
| 1818 | /* |
| 1819 | * given a cluster, put all of its extents back into the free space |
| 1820 | * cache. If a block group is passed, this function will only free |
| 1821 | * a cluster that belongs to the passed block group. |
| 1822 | * |
| 1823 | * Otherwise, it'll get a reference on the block group pointed to by the |
| 1824 | * cluster and remove the cluster from it. |
| 1825 | */ |
| 1826 | int btrfs_return_cluster_to_free_space( |
| 1827 | struct btrfs_block_group_cache *block_group, |
| 1828 | struct btrfs_free_cluster *cluster) |
| 1829 | { |
| 1830 | int ret; |
| 1831 | |
| 1832 | /* first, get a safe pointer to the block group */ |
| 1833 | spin_lock(&cluster->lock); |
| 1834 | if (!block_group) { |
| 1835 | block_group = cluster->block_group; |
| 1836 | if (!block_group) { |
| 1837 | spin_unlock(&cluster->lock); |
| 1838 | return 0; |
| 1839 | } |
| 1840 | } else if (cluster->block_group != block_group) { |
| 1841 | /* someone else has already freed it don't redo their work */ |
| 1842 | spin_unlock(&cluster->lock); |
| 1843 | return 0; |
| 1844 | } |
| 1845 | atomic_inc(&block_group->count); |
| 1846 | spin_unlock(&cluster->lock); |
| 1847 | |
| 1848 | /* now return any extents the cluster had on it */ |
| 1849 | spin_lock(&block_group->tree_lock); |
| 1850 | ret = __btrfs_return_cluster_to_free_space(block_group, cluster); |
| 1851 | spin_unlock(&block_group->tree_lock); |
| 1852 | |
| 1853 | /* finally drop our ref */ |
| 1854 | btrfs_put_block_group(block_group); |
| 1855 | return ret; |
| 1856 | } |
| 1857 | |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1858 | static u64 btrfs_alloc_from_bitmap(struct btrfs_block_group_cache *block_group, |
| 1859 | struct btrfs_free_cluster *cluster, |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 1860 | struct btrfs_free_space *entry, |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1861 | u64 bytes, u64 min_start) |
| 1862 | { |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1863 | int err; |
| 1864 | u64 search_start = cluster->window_start; |
| 1865 | u64 search_bytes = bytes; |
| 1866 | u64 ret = 0; |
| 1867 | |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1868 | search_start = min_start; |
| 1869 | search_bytes = bytes; |
| 1870 | |
| 1871 | err = search_bitmap(block_group, entry, &search_start, |
| 1872 | &search_bytes); |
| 1873 | if (err) |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 1874 | return 0; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1875 | |
| 1876 | ret = search_start; |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1877 | bitmap_clear_bits(block_group, entry, ret, bytes); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1878 | |
| 1879 | return ret; |
| 1880 | } |
| 1881 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 1882 | /* |
| 1883 | * given a cluster, try to allocate 'bytes' from it, returns 0 |
| 1884 | * if it couldn't find anything suitably large, or a logical disk offset |
| 1885 | * if things worked out |
| 1886 | */ |
| 1887 | u64 btrfs_alloc_from_cluster(struct btrfs_block_group_cache *block_group, |
| 1888 | struct btrfs_free_cluster *cluster, u64 bytes, |
| 1889 | u64 min_start) |
| 1890 | { |
| 1891 | struct btrfs_free_space *entry = NULL; |
| 1892 | struct rb_node *node; |
| 1893 | u64 ret = 0; |
| 1894 | |
| 1895 | spin_lock(&cluster->lock); |
| 1896 | if (bytes > cluster->max_size) |
| 1897 | goto out; |
| 1898 | |
| 1899 | if (cluster->block_group != block_group) |
| 1900 | goto out; |
| 1901 | |
| 1902 | node = rb_first(&cluster->root); |
| 1903 | if (!node) |
| 1904 | goto out; |
| 1905 | |
| 1906 | entry = rb_entry(node, struct btrfs_free_space, offset_index); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 1907 | while(1) { |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 1908 | if (entry->bytes < bytes || |
| 1909 | (!entry->bitmap && entry->offset < min_start)) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 1910 | struct rb_node *node; |
| 1911 | |
| 1912 | node = rb_next(&entry->offset_index); |
| 1913 | if (!node) |
| 1914 | break; |
| 1915 | entry = rb_entry(node, struct btrfs_free_space, |
| 1916 | offset_index); |
| 1917 | continue; |
| 1918 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 1919 | |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 1920 | if (entry->bitmap) { |
| 1921 | ret = btrfs_alloc_from_bitmap(block_group, |
| 1922 | cluster, entry, bytes, |
| 1923 | min_start); |
| 1924 | if (ret == 0) { |
| 1925 | struct rb_node *node; |
| 1926 | node = rb_next(&entry->offset_index); |
| 1927 | if (!node) |
| 1928 | break; |
| 1929 | entry = rb_entry(node, struct btrfs_free_space, |
| 1930 | offset_index); |
| 1931 | continue; |
| 1932 | } |
| 1933 | } else { |
| 1934 | |
| 1935 | ret = entry->offset; |
| 1936 | |
| 1937 | entry->offset += bytes; |
| 1938 | entry->bytes -= bytes; |
| 1939 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 1940 | |
Li Zefan | 5e71b5d | 2010-11-09 14:55:34 +0800 | [diff] [blame] | 1941 | if (entry->bytes == 0) |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 1942 | rb_erase(&entry->offset_index, &cluster->root); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 1943 | break; |
| 1944 | } |
| 1945 | out: |
| 1946 | spin_unlock(&cluster->lock); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1947 | |
Li Zefan | 5e71b5d | 2010-11-09 14:55:34 +0800 | [diff] [blame] | 1948 | if (!ret) |
| 1949 | return 0; |
| 1950 | |
| 1951 | spin_lock(&block_group->tree_lock); |
| 1952 | |
| 1953 | block_group->free_space -= bytes; |
| 1954 | if (entry->bytes == 0) { |
| 1955 | block_group->free_extents--; |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 1956 | if (entry->bitmap) { |
| 1957 | kfree(entry->bitmap); |
| 1958 | block_group->total_bitmaps--; |
| 1959 | recalculate_thresholds(block_group); |
| 1960 | } |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 1961 | kmem_cache_free(btrfs_free_space_cachep, entry); |
Li Zefan | 5e71b5d | 2010-11-09 14:55:34 +0800 | [diff] [blame] | 1962 | } |
| 1963 | |
| 1964 | spin_unlock(&block_group->tree_lock); |
| 1965 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 1966 | return ret; |
| 1967 | } |
| 1968 | |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1969 | static int btrfs_bitmap_cluster(struct btrfs_block_group_cache *block_group, |
| 1970 | struct btrfs_free_space *entry, |
| 1971 | struct btrfs_free_cluster *cluster, |
| 1972 | u64 offset, u64 bytes, u64 min_bytes) |
| 1973 | { |
| 1974 | unsigned long next_zero; |
| 1975 | unsigned long i; |
| 1976 | unsigned long search_bits; |
| 1977 | unsigned long total_bits; |
| 1978 | unsigned long found_bits; |
| 1979 | unsigned long start = 0; |
| 1980 | unsigned long total_found = 0; |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 1981 | int ret; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1982 | bool found = false; |
| 1983 | |
| 1984 | i = offset_to_bit(entry->offset, block_group->sectorsize, |
| 1985 | max_t(u64, offset, entry->offset)); |
Josef Bacik | d0a365e | 2011-03-18 15:27:43 -0400 | [diff] [blame] | 1986 | search_bits = bytes_to_bits(bytes, block_group->sectorsize); |
| 1987 | total_bits = bytes_to_bits(min_bytes, block_group->sectorsize); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1988 | |
| 1989 | again: |
| 1990 | found_bits = 0; |
| 1991 | for (i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, i); |
| 1992 | i < BITS_PER_BITMAP; |
| 1993 | i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, i + 1)) { |
| 1994 | next_zero = find_next_zero_bit(entry->bitmap, |
| 1995 | BITS_PER_BITMAP, i); |
| 1996 | if (next_zero - i >= search_bits) { |
| 1997 | found_bits = next_zero - i; |
| 1998 | break; |
| 1999 | } |
| 2000 | i = next_zero; |
| 2001 | } |
| 2002 | |
| 2003 | if (!found_bits) |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 2004 | return -ENOSPC; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2005 | |
| 2006 | if (!found) { |
| 2007 | start = i; |
| 2008 | found = true; |
| 2009 | } |
| 2010 | |
| 2011 | total_found += found_bits; |
| 2012 | |
| 2013 | if (cluster->max_size < found_bits * block_group->sectorsize) |
| 2014 | cluster->max_size = found_bits * block_group->sectorsize; |
| 2015 | |
| 2016 | if (total_found < total_bits) { |
| 2017 | i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, next_zero); |
| 2018 | if (i - start > total_bits * 2) { |
| 2019 | total_found = 0; |
| 2020 | cluster->max_size = 0; |
| 2021 | found = false; |
| 2022 | } |
| 2023 | goto again; |
| 2024 | } |
| 2025 | |
| 2026 | cluster->window_start = start * block_group->sectorsize + |
| 2027 | entry->offset; |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 2028 | rb_erase(&entry->offset_index, &block_group->free_space_offset); |
| 2029 | ret = tree_insert_offset(&cluster->root, entry->offset, |
| 2030 | &entry->offset_index, 1); |
| 2031 | BUG_ON(ret); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2032 | |
| 2033 | return 0; |
| 2034 | } |
| 2035 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2036 | /* |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 2037 | * This searches the block group for just extents to fill the cluster with. |
| 2038 | */ |
| 2039 | static int setup_cluster_no_bitmap(struct btrfs_block_group_cache *block_group, |
| 2040 | struct btrfs_free_cluster *cluster, |
| 2041 | u64 offset, u64 bytes, u64 min_bytes) |
| 2042 | { |
| 2043 | struct btrfs_free_space *first = NULL; |
| 2044 | struct btrfs_free_space *entry = NULL; |
| 2045 | struct btrfs_free_space *prev = NULL; |
| 2046 | struct btrfs_free_space *last; |
| 2047 | struct rb_node *node; |
| 2048 | u64 window_start; |
| 2049 | u64 window_free; |
| 2050 | u64 max_extent; |
| 2051 | u64 max_gap = 128 * 1024; |
| 2052 | |
| 2053 | entry = tree_search_offset(block_group, offset, 0, 1); |
| 2054 | if (!entry) |
| 2055 | return -ENOSPC; |
| 2056 | |
| 2057 | /* |
| 2058 | * We don't want bitmaps, so just move along until we find a normal |
| 2059 | * extent entry. |
| 2060 | */ |
| 2061 | while (entry->bitmap) { |
| 2062 | node = rb_next(&entry->offset_index); |
| 2063 | if (!node) |
| 2064 | return -ENOSPC; |
| 2065 | entry = rb_entry(node, struct btrfs_free_space, offset_index); |
| 2066 | } |
| 2067 | |
| 2068 | window_start = entry->offset; |
| 2069 | window_free = entry->bytes; |
| 2070 | max_extent = entry->bytes; |
| 2071 | first = entry; |
| 2072 | last = entry; |
| 2073 | prev = entry; |
| 2074 | |
| 2075 | while (window_free <= min_bytes) { |
| 2076 | node = rb_next(&entry->offset_index); |
| 2077 | if (!node) |
| 2078 | return -ENOSPC; |
| 2079 | entry = rb_entry(node, struct btrfs_free_space, offset_index); |
| 2080 | |
| 2081 | if (entry->bitmap) |
| 2082 | continue; |
| 2083 | /* |
| 2084 | * we haven't filled the empty size and the window is |
| 2085 | * very large. reset and try again |
| 2086 | */ |
| 2087 | if (entry->offset - (prev->offset + prev->bytes) > max_gap || |
| 2088 | entry->offset - window_start > (min_bytes * 2)) { |
| 2089 | first = entry; |
| 2090 | window_start = entry->offset; |
| 2091 | window_free = entry->bytes; |
| 2092 | last = entry; |
| 2093 | max_extent = entry->bytes; |
| 2094 | } else { |
| 2095 | last = entry; |
| 2096 | window_free += entry->bytes; |
| 2097 | if (entry->bytes > max_extent) |
| 2098 | max_extent = entry->bytes; |
| 2099 | } |
| 2100 | prev = entry; |
| 2101 | } |
| 2102 | |
| 2103 | cluster->window_start = first->offset; |
| 2104 | |
| 2105 | node = &first->offset_index; |
| 2106 | |
| 2107 | /* |
| 2108 | * now we've found our entries, pull them out of the free space |
| 2109 | * cache and put them into the cluster rbtree |
| 2110 | */ |
| 2111 | do { |
| 2112 | int ret; |
| 2113 | |
| 2114 | entry = rb_entry(node, struct btrfs_free_space, offset_index); |
| 2115 | node = rb_next(&entry->offset_index); |
| 2116 | if (entry->bitmap) |
| 2117 | continue; |
| 2118 | |
| 2119 | rb_erase(&entry->offset_index, &block_group->free_space_offset); |
| 2120 | ret = tree_insert_offset(&cluster->root, entry->offset, |
| 2121 | &entry->offset_index, 0); |
| 2122 | BUG_ON(ret); |
| 2123 | } while (node && entry != last); |
| 2124 | |
| 2125 | cluster->max_size = max_extent; |
| 2126 | |
| 2127 | return 0; |
| 2128 | } |
| 2129 | |
| 2130 | /* |
| 2131 | * This specifically looks for bitmaps that may work in the cluster, we assume |
| 2132 | * that we have already failed to find extents that will work. |
| 2133 | */ |
| 2134 | static int setup_cluster_bitmap(struct btrfs_block_group_cache *block_group, |
| 2135 | struct btrfs_free_cluster *cluster, |
| 2136 | u64 offset, u64 bytes, u64 min_bytes) |
| 2137 | { |
| 2138 | struct btrfs_free_space *entry; |
| 2139 | struct rb_node *node; |
| 2140 | int ret = -ENOSPC; |
| 2141 | |
| 2142 | if (block_group->total_bitmaps == 0) |
| 2143 | return -ENOSPC; |
| 2144 | |
| 2145 | entry = tree_search_offset(block_group, |
| 2146 | offset_to_bitmap(block_group, offset), |
| 2147 | 0, 1); |
| 2148 | if (!entry) |
| 2149 | return -ENOSPC; |
| 2150 | |
| 2151 | node = &entry->offset_index; |
| 2152 | do { |
| 2153 | entry = rb_entry(node, struct btrfs_free_space, offset_index); |
| 2154 | node = rb_next(&entry->offset_index); |
| 2155 | if (!entry->bitmap) |
| 2156 | continue; |
| 2157 | if (entry->bytes < min_bytes) |
| 2158 | continue; |
| 2159 | ret = btrfs_bitmap_cluster(block_group, entry, cluster, offset, |
| 2160 | bytes, min_bytes); |
| 2161 | } while (ret && node); |
| 2162 | |
| 2163 | return ret; |
| 2164 | } |
| 2165 | |
| 2166 | /* |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2167 | * here we try to find a cluster of blocks in a block group. The goal |
| 2168 | * is to find at least bytes free and up to empty_size + bytes free. |
| 2169 | * We might not find them all in one contiguous area. |
| 2170 | * |
| 2171 | * returns zero and sets up cluster if things worked out, otherwise |
| 2172 | * it returns -enospc |
| 2173 | */ |
| 2174 | int btrfs_find_space_cluster(struct btrfs_trans_handle *trans, |
Chris Mason | 451d758 | 2009-06-09 20:28:34 -0400 | [diff] [blame] | 2175 | struct btrfs_root *root, |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2176 | struct btrfs_block_group_cache *block_group, |
| 2177 | struct btrfs_free_cluster *cluster, |
| 2178 | u64 offset, u64 bytes, u64 empty_size) |
| 2179 | { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2180 | u64 min_bytes; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2181 | int ret; |
| 2182 | |
| 2183 | /* for metadata, allow allocates with more holes */ |
Chris Mason | 451d758 | 2009-06-09 20:28:34 -0400 | [diff] [blame] | 2184 | if (btrfs_test_opt(root, SSD_SPREAD)) { |
| 2185 | min_bytes = bytes + empty_size; |
| 2186 | } else if (block_group->flags & BTRFS_BLOCK_GROUP_METADATA) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2187 | /* |
| 2188 | * we want to do larger allocations when we are |
| 2189 | * flushing out the delayed refs, it helps prevent |
| 2190 | * making more work as we go along. |
| 2191 | */ |
| 2192 | if (trans->transaction->delayed_refs.flushing) |
| 2193 | min_bytes = max(bytes, (bytes + empty_size) >> 1); |
| 2194 | else |
| 2195 | min_bytes = max(bytes, (bytes + empty_size) >> 4); |
| 2196 | } else |
| 2197 | min_bytes = max(bytes, (bytes + empty_size) >> 2); |
| 2198 | |
| 2199 | spin_lock(&block_group->tree_lock); |
Josef Bacik | 7d0d2e8 | 2011-03-18 15:13:42 -0400 | [diff] [blame] | 2200 | |
| 2201 | /* |
| 2202 | * If we know we don't have enough space to make a cluster don't even |
| 2203 | * bother doing all the work to try and find one. |
| 2204 | */ |
| 2205 | if (block_group->free_space < min_bytes) { |
| 2206 | spin_unlock(&block_group->tree_lock); |
| 2207 | return -ENOSPC; |
| 2208 | } |
| 2209 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2210 | spin_lock(&cluster->lock); |
| 2211 | |
| 2212 | /* someone already found a cluster, hooray */ |
| 2213 | if (cluster->block_group) { |
| 2214 | ret = 0; |
| 2215 | goto out; |
| 2216 | } |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 2217 | |
| 2218 | ret = setup_cluster_no_bitmap(block_group, cluster, offset, bytes, |
| 2219 | min_bytes); |
| 2220 | if (ret) |
| 2221 | ret = setup_cluster_bitmap(block_group, cluster, offset, |
| 2222 | bytes, min_bytes); |
| 2223 | |
| 2224 | if (!ret) { |
| 2225 | atomic_inc(&block_group->count); |
| 2226 | list_add_tail(&cluster->block_group_list, |
| 2227 | &block_group->cluster_list); |
| 2228 | cluster->block_group = block_group; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2229 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2230 | out: |
| 2231 | spin_unlock(&cluster->lock); |
| 2232 | spin_unlock(&block_group->tree_lock); |
| 2233 | |
| 2234 | return ret; |
| 2235 | } |
| 2236 | |
| 2237 | /* |
| 2238 | * simple code to zero out a cluster |
| 2239 | */ |
| 2240 | void btrfs_init_free_cluster(struct btrfs_free_cluster *cluster) |
| 2241 | { |
| 2242 | spin_lock_init(&cluster->lock); |
| 2243 | spin_lock_init(&cluster->refill_lock); |
Eric Paris | 6bef4d3 | 2010-02-23 19:43:04 +0000 | [diff] [blame] | 2244 | cluster->root = RB_ROOT; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2245 | cluster->max_size = 0; |
| 2246 | INIT_LIST_HEAD(&cluster->block_group_list); |
| 2247 | cluster->block_group = NULL; |
| 2248 | } |
| 2249 | |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 2250 | int btrfs_trim_block_group(struct btrfs_block_group_cache *block_group, |
| 2251 | u64 *trimmed, u64 start, u64 end, u64 minlen) |
| 2252 | { |
| 2253 | struct btrfs_free_space *entry = NULL; |
| 2254 | struct btrfs_fs_info *fs_info = block_group->fs_info; |
| 2255 | u64 bytes = 0; |
| 2256 | u64 actually_trimmed; |
| 2257 | int ret = 0; |
| 2258 | |
| 2259 | *trimmed = 0; |
| 2260 | |
| 2261 | while (start < end) { |
| 2262 | spin_lock(&block_group->tree_lock); |
| 2263 | |
| 2264 | if (block_group->free_space < minlen) { |
| 2265 | spin_unlock(&block_group->tree_lock); |
| 2266 | break; |
| 2267 | } |
| 2268 | |
| 2269 | entry = tree_search_offset(block_group, start, 0, 1); |
| 2270 | if (!entry) |
| 2271 | entry = tree_search_offset(block_group, |
| 2272 | offset_to_bitmap(block_group, |
| 2273 | start), |
| 2274 | 1, 1); |
| 2275 | |
| 2276 | if (!entry || entry->offset >= end) { |
| 2277 | spin_unlock(&block_group->tree_lock); |
| 2278 | break; |
| 2279 | } |
| 2280 | |
| 2281 | if (entry->bitmap) { |
| 2282 | ret = search_bitmap(block_group, entry, &start, &bytes); |
| 2283 | if (!ret) { |
| 2284 | if (start >= end) { |
| 2285 | spin_unlock(&block_group->tree_lock); |
| 2286 | break; |
| 2287 | } |
| 2288 | bytes = min(bytes, end - start); |
| 2289 | bitmap_clear_bits(block_group, entry, |
| 2290 | start, bytes); |
| 2291 | if (entry->bytes == 0) |
| 2292 | free_bitmap(block_group, entry); |
| 2293 | } else { |
| 2294 | start = entry->offset + BITS_PER_BITMAP * |
| 2295 | block_group->sectorsize; |
| 2296 | spin_unlock(&block_group->tree_lock); |
| 2297 | ret = 0; |
| 2298 | continue; |
| 2299 | } |
| 2300 | } else { |
| 2301 | start = entry->offset; |
| 2302 | bytes = min(entry->bytes, end - start); |
| 2303 | unlink_free_space(block_group, entry); |
| 2304 | kfree(entry); |
| 2305 | } |
| 2306 | |
| 2307 | spin_unlock(&block_group->tree_lock); |
| 2308 | |
| 2309 | if (bytes >= minlen) { |
| 2310 | int update_ret; |
| 2311 | update_ret = btrfs_update_reserved_bytes(block_group, |
| 2312 | bytes, 1, 1); |
| 2313 | |
| 2314 | ret = btrfs_error_discard_extent(fs_info->extent_root, |
| 2315 | start, |
| 2316 | bytes, |
| 2317 | &actually_trimmed); |
| 2318 | |
| 2319 | btrfs_add_free_space(block_group, |
| 2320 | start, bytes); |
| 2321 | if (!update_ret) |
| 2322 | btrfs_update_reserved_bytes(block_group, |
| 2323 | bytes, 0, 1); |
| 2324 | |
| 2325 | if (ret) |
| 2326 | break; |
| 2327 | *trimmed += actually_trimmed; |
| 2328 | } |
| 2329 | start += bytes; |
| 2330 | bytes = 0; |
| 2331 | |
| 2332 | if (fatal_signal_pending(current)) { |
| 2333 | ret = -ERESTARTSYS; |
| 2334 | break; |
| 2335 | } |
| 2336 | |
| 2337 | cond_resched(); |
| 2338 | } |
| 2339 | |
| 2340 | return ret; |
| 2341 | } |