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 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 32 | static int link_free_space(struct btrfs_free_space_ctl *ctl, |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 33 | struct btrfs_free_space *info); |
| 34 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 35 | struct inode *lookup_free_space_inode(struct btrfs_root *root, |
| 36 | struct btrfs_block_group_cache |
| 37 | *block_group, struct btrfs_path *path) |
| 38 | { |
| 39 | struct btrfs_key key; |
| 40 | struct btrfs_key location; |
| 41 | struct btrfs_disk_key disk_key; |
| 42 | struct btrfs_free_space_header *header; |
| 43 | struct extent_buffer *leaf; |
| 44 | struct inode *inode = NULL; |
| 45 | int ret; |
| 46 | |
| 47 | spin_lock(&block_group->lock); |
| 48 | if (block_group->inode) |
| 49 | inode = igrab(block_group->inode); |
| 50 | spin_unlock(&block_group->lock); |
| 51 | if (inode) |
| 52 | return inode; |
| 53 | |
| 54 | key.objectid = BTRFS_FREE_SPACE_OBJECTID; |
| 55 | key.offset = block_group->key.objectid; |
| 56 | key.type = 0; |
| 57 | |
| 58 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 59 | if (ret < 0) |
| 60 | return ERR_PTR(ret); |
| 61 | if (ret > 0) { |
| 62 | btrfs_release_path(root, path); |
| 63 | return ERR_PTR(-ENOENT); |
| 64 | } |
| 65 | |
| 66 | leaf = path->nodes[0]; |
| 67 | header = btrfs_item_ptr(leaf, path->slots[0], |
| 68 | struct btrfs_free_space_header); |
| 69 | btrfs_free_space_key(leaf, header, &disk_key); |
| 70 | btrfs_disk_key_to_cpu(&location, &disk_key); |
| 71 | btrfs_release_path(root, path); |
| 72 | |
| 73 | inode = btrfs_iget(root->fs_info->sb, &location, root, NULL); |
| 74 | if (!inode) |
| 75 | return ERR_PTR(-ENOENT); |
| 76 | if (IS_ERR(inode)) |
| 77 | return inode; |
| 78 | if (is_bad_inode(inode)) { |
| 79 | iput(inode); |
| 80 | return ERR_PTR(-ENOENT); |
| 81 | } |
| 82 | |
Miao Xie | adae52b | 2011-03-31 09:43:23 +0000 | [diff] [blame] | 83 | inode->i_mapping->flags &= ~__GFP_FS; |
| 84 | |
Josef Bacik | 0af3d00 | 2010-06-21 14:48:16 -0400 | [diff] [blame] | 85 | spin_lock(&block_group->lock); |
| 86 | if (!root->fs_info->closing) { |
| 87 | block_group->inode = igrab(inode); |
| 88 | block_group->iref = 1; |
| 89 | } |
| 90 | spin_unlock(&block_group->lock); |
| 91 | |
| 92 | return inode; |
| 93 | } |
| 94 | |
| 95 | int create_free_space_inode(struct btrfs_root *root, |
| 96 | struct btrfs_trans_handle *trans, |
| 97 | struct btrfs_block_group_cache *block_group, |
| 98 | struct btrfs_path *path) |
| 99 | { |
| 100 | struct btrfs_key key; |
| 101 | struct btrfs_disk_key disk_key; |
| 102 | struct btrfs_free_space_header *header; |
| 103 | struct btrfs_inode_item *inode_item; |
| 104 | struct extent_buffer *leaf; |
| 105 | u64 objectid; |
| 106 | int ret; |
| 107 | |
| 108 | ret = btrfs_find_free_objectid(trans, root, 0, &objectid); |
| 109 | if (ret < 0) |
| 110 | return ret; |
| 111 | |
| 112 | ret = btrfs_insert_empty_inode(trans, root, path, objectid); |
| 113 | if (ret) |
| 114 | return ret; |
| 115 | |
| 116 | leaf = path->nodes[0]; |
| 117 | inode_item = btrfs_item_ptr(leaf, path->slots[0], |
| 118 | struct btrfs_inode_item); |
| 119 | btrfs_item_key(leaf, &disk_key, path->slots[0]); |
| 120 | memset_extent_buffer(leaf, 0, (unsigned long)inode_item, |
| 121 | sizeof(*inode_item)); |
| 122 | btrfs_set_inode_generation(leaf, inode_item, trans->transid); |
| 123 | btrfs_set_inode_size(leaf, inode_item, 0); |
| 124 | btrfs_set_inode_nbytes(leaf, inode_item, 0); |
| 125 | btrfs_set_inode_uid(leaf, inode_item, 0); |
| 126 | btrfs_set_inode_gid(leaf, inode_item, 0); |
| 127 | btrfs_set_inode_mode(leaf, inode_item, S_IFREG | 0600); |
| 128 | btrfs_set_inode_flags(leaf, inode_item, BTRFS_INODE_NOCOMPRESS | |
| 129 | BTRFS_INODE_PREALLOC | BTRFS_INODE_NODATASUM); |
| 130 | btrfs_set_inode_nlink(leaf, inode_item, 1); |
| 131 | btrfs_set_inode_transid(leaf, inode_item, trans->transid); |
| 132 | btrfs_set_inode_block_group(leaf, inode_item, |
| 133 | block_group->key.objectid); |
| 134 | btrfs_mark_buffer_dirty(leaf); |
| 135 | btrfs_release_path(root, path); |
| 136 | |
| 137 | key.objectid = BTRFS_FREE_SPACE_OBJECTID; |
| 138 | key.offset = block_group->key.objectid; |
| 139 | key.type = 0; |
| 140 | |
| 141 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 142 | sizeof(struct btrfs_free_space_header)); |
| 143 | if (ret < 0) { |
| 144 | btrfs_release_path(root, path); |
| 145 | return ret; |
| 146 | } |
| 147 | leaf = path->nodes[0]; |
| 148 | header = btrfs_item_ptr(leaf, path->slots[0], |
| 149 | struct btrfs_free_space_header); |
| 150 | memset_extent_buffer(leaf, 0, (unsigned long)header, sizeof(*header)); |
| 151 | btrfs_set_free_space_key(leaf, header, &disk_key); |
| 152 | btrfs_mark_buffer_dirty(leaf); |
| 153 | btrfs_release_path(root, path); |
| 154 | |
| 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | int btrfs_truncate_free_space_cache(struct btrfs_root *root, |
| 159 | struct btrfs_trans_handle *trans, |
| 160 | struct btrfs_path *path, |
| 161 | struct inode *inode) |
| 162 | { |
| 163 | loff_t oldsize; |
| 164 | int ret = 0; |
| 165 | |
| 166 | trans->block_rsv = root->orphan_block_rsv; |
| 167 | ret = btrfs_block_rsv_check(trans, root, |
| 168 | root->orphan_block_rsv, |
| 169 | 0, 5); |
| 170 | if (ret) |
| 171 | return ret; |
| 172 | |
| 173 | oldsize = i_size_read(inode); |
| 174 | btrfs_i_size_write(inode, 0); |
| 175 | truncate_pagecache(inode, oldsize, 0); |
| 176 | |
| 177 | /* |
| 178 | * We don't need an orphan item because truncating the free space cache |
| 179 | * will never be split across transactions. |
| 180 | */ |
| 181 | ret = btrfs_truncate_inode_items(trans, root, inode, |
| 182 | 0, BTRFS_EXTENT_DATA_KEY); |
| 183 | if (ret) { |
| 184 | WARN_ON(1); |
| 185 | return ret; |
| 186 | } |
| 187 | |
| 188 | return btrfs_update_inode(trans, root, inode); |
| 189 | } |
| 190 | |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 191 | static int readahead_cache(struct inode *inode) |
| 192 | { |
| 193 | struct file_ra_state *ra; |
| 194 | unsigned long last_index; |
| 195 | |
| 196 | ra = kzalloc(sizeof(*ra), GFP_NOFS); |
| 197 | if (!ra) |
| 198 | return -ENOMEM; |
| 199 | |
| 200 | file_ra_state_init(ra, inode->i_mapping); |
| 201 | last_index = (i_size_read(inode) - 1) >> PAGE_CACHE_SHIFT; |
| 202 | |
| 203 | page_cache_sync_readahead(inode->i_mapping, ra, NULL, 0, last_index); |
| 204 | |
| 205 | kfree(ra); |
| 206 | |
| 207 | return 0; |
| 208 | } |
| 209 | |
| 210 | int load_free_space_cache(struct btrfs_fs_info *fs_info, |
| 211 | struct btrfs_block_group_cache *block_group) |
| 212 | { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 213 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 214 | struct btrfs_root *root = fs_info->tree_root; |
| 215 | struct inode *inode; |
| 216 | struct btrfs_free_space_header *header; |
| 217 | struct extent_buffer *leaf; |
| 218 | struct page *page; |
| 219 | struct btrfs_path *path; |
| 220 | u32 *checksums = NULL, *crc; |
| 221 | char *disk_crcs = NULL; |
| 222 | struct btrfs_key key; |
| 223 | struct list_head bitmaps; |
| 224 | u64 num_entries; |
| 225 | u64 num_bitmaps; |
| 226 | u64 generation; |
Josef Bacik | 43be214 | 2011-04-01 14:55:00 +0000 | [diff] [blame] | 227 | u64 used = btrfs_block_group_used(&block_group->item); |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 228 | u32 cur_crc = ~(u32)0; |
| 229 | pgoff_t index = 0; |
| 230 | unsigned long first_page_offset; |
| 231 | int num_checksums; |
| 232 | int ret = 0; |
| 233 | |
| 234 | /* |
| 235 | * If we're unmounting then just return, since this does a search on the |
| 236 | * normal root and not the commit root and we could deadlock. |
| 237 | */ |
| 238 | smp_mb(); |
| 239 | if (fs_info->closing) |
| 240 | return 0; |
| 241 | |
| 242 | /* |
| 243 | * If this block group has been marked to be cleared for one reason or |
| 244 | * another then we can't trust the on disk cache, so just return. |
| 245 | */ |
| 246 | spin_lock(&block_group->lock); |
| 247 | if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) { |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 248 | spin_unlock(&block_group->lock); |
| 249 | return 0; |
| 250 | } |
| 251 | spin_unlock(&block_group->lock); |
| 252 | |
| 253 | INIT_LIST_HEAD(&bitmaps); |
| 254 | |
| 255 | path = btrfs_alloc_path(); |
| 256 | if (!path) |
| 257 | return 0; |
| 258 | |
| 259 | inode = lookup_free_space_inode(root, block_group, path); |
| 260 | if (IS_ERR(inode)) { |
| 261 | btrfs_free_path(path); |
| 262 | return 0; |
| 263 | } |
| 264 | |
| 265 | /* Nothing in the space cache, goodbye */ |
| 266 | if (!i_size_read(inode)) { |
| 267 | btrfs_free_path(path); |
| 268 | goto out; |
| 269 | } |
| 270 | |
| 271 | key.objectid = BTRFS_FREE_SPACE_OBJECTID; |
| 272 | key.offset = block_group->key.objectid; |
| 273 | key.type = 0; |
| 274 | |
| 275 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 276 | if (ret) { |
| 277 | btrfs_free_path(path); |
| 278 | goto out; |
| 279 | } |
| 280 | |
| 281 | leaf = path->nodes[0]; |
| 282 | header = btrfs_item_ptr(leaf, path->slots[0], |
| 283 | struct btrfs_free_space_header); |
| 284 | num_entries = btrfs_free_space_entries(leaf, header); |
| 285 | num_bitmaps = btrfs_free_space_bitmaps(leaf, header); |
| 286 | generation = btrfs_free_space_generation(leaf, header); |
| 287 | btrfs_free_path(path); |
| 288 | |
| 289 | if (BTRFS_I(inode)->generation != generation) { |
| 290 | printk(KERN_ERR "btrfs: free space inode generation (%llu) did" |
| 291 | " not match free space cache generation (%llu) for " |
| 292 | "block group %llu\n", |
| 293 | (unsigned long long)BTRFS_I(inode)->generation, |
| 294 | (unsigned long long)generation, |
| 295 | (unsigned long long)block_group->key.objectid); |
Josef Bacik | 2b20982 | 2010-12-03 13:17:53 -0500 | [diff] [blame] | 296 | goto free_cache; |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | if (!num_entries) |
| 300 | goto out; |
| 301 | |
| 302 | /* Setup everything for doing checksumming */ |
| 303 | num_checksums = i_size_read(inode) / PAGE_CACHE_SIZE; |
| 304 | checksums = crc = kzalloc(sizeof(u32) * num_checksums, GFP_NOFS); |
| 305 | if (!checksums) |
| 306 | goto out; |
| 307 | first_page_offset = (sizeof(u32) * num_checksums) + sizeof(u64); |
| 308 | disk_crcs = kzalloc(first_page_offset, GFP_NOFS); |
| 309 | if (!disk_crcs) |
| 310 | goto out; |
| 311 | |
| 312 | ret = readahead_cache(inode); |
| 313 | if (ret) { |
| 314 | ret = 0; |
| 315 | goto out; |
| 316 | } |
| 317 | |
| 318 | while (1) { |
| 319 | struct btrfs_free_space_entry *entry; |
| 320 | struct btrfs_free_space *e; |
| 321 | void *addr; |
| 322 | unsigned long offset = 0; |
| 323 | unsigned long start_offset = 0; |
| 324 | int need_loop = 0; |
| 325 | |
| 326 | if (!num_entries && !num_bitmaps) |
| 327 | break; |
| 328 | |
| 329 | if (index == 0) { |
| 330 | start_offset = first_page_offset; |
| 331 | offset = start_offset; |
| 332 | } |
| 333 | |
| 334 | page = grab_cache_page(inode->i_mapping, index); |
| 335 | if (!page) { |
| 336 | ret = 0; |
| 337 | goto free_cache; |
| 338 | } |
| 339 | |
| 340 | if (!PageUptodate(page)) { |
| 341 | btrfs_readpage(NULL, page); |
| 342 | lock_page(page); |
| 343 | if (!PageUptodate(page)) { |
| 344 | unlock_page(page); |
| 345 | page_cache_release(page); |
| 346 | printk(KERN_ERR "btrfs: error reading free " |
| 347 | "space cache: %llu\n", |
| 348 | (unsigned long long) |
| 349 | block_group->key.objectid); |
| 350 | goto free_cache; |
| 351 | } |
| 352 | } |
| 353 | addr = kmap(page); |
| 354 | |
| 355 | if (index == 0) { |
| 356 | u64 *gen; |
| 357 | |
| 358 | memcpy(disk_crcs, addr, first_page_offset); |
| 359 | gen = addr + (sizeof(u32) * num_checksums); |
| 360 | if (*gen != BTRFS_I(inode)->generation) { |
| 361 | printk(KERN_ERR "btrfs: space cache generation" |
| 362 | " (%llu) does not match inode (%llu) " |
| 363 | "for block group %llu\n", |
| 364 | (unsigned long long)*gen, |
| 365 | (unsigned long long) |
| 366 | BTRFS_I(inode)->generation, |
| 367 | (unsigned long long) |
| 368 | block_group->key.objectid); |
| 369 | kunmap(page); |
| 370 | unlock_page(page); |
| 371 | page_cache_release(page); |
| 372 | goto free_cache; |
| 373 | } |
| 374 | crc = (u32 *)disk_crcs; |
| 375 | } |
| 376 | entry = addr + start_offset; |
| 377 | |
| 378 | /* First lets check our crc before we do anything fun */ |
| 379 | cur_crc = ~(u32)0; |
| 380 | cur_crc = btrfs_csum_data(root, addr + start_offset, cur_crc, |
| 381 | PAGE_CACHE_SIZE - start_offset); |
| 382 | btrfs_csum_final(cur_crc, (char *)&cur_crc); |
| 383 | if (cur_crc != *crc) { |
| 384 | printk(KERN_ERR "btrfs: crc mismatch for page %lu in " |
| 385 | "block group %llu\n", index, |
| 386 | (unsigned long long)block_group->key.objectid); |
| 387 | kunmap(page); |
| 388 | unlock_page(page); |
| 389 | page_cache_release(page); |
| 390 | goto free_cache; |
| 391 | } |
| 392 | crc++; |
| 393 | |
| 394 | while (1) { |
| 395 | if (!num_entries) |
| 396 | break; |
| 397 | |
| 398 | need_loop = 1; |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 399 | e = kmem_cache_zalloc(btrfs_free_space_cachep, |
| 400 | GFP_NOFS); |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 401 | if (!e) { |
| 402 | kunmap(page); |
| 403 | unlock_page(page); |
| 404 | page_cache_release(page); |
| 405 | goto free_cache; |
| 406 | } |
| 407 | |
| 408 | e->offset = le64_to_cpu(entry->offset); |
| 409 | e->bytes = le64_to_cpu(entry->bytes); |
| 410 | if (!e->bytes) { |
| 411 | kunmap(page); |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 412 | kmem_cache_free(btrfs_free_space_cachep, e); |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 413 | unlock_page(page); |
| 414 | page_cache_release(page); |
| 415 | goto free_cache; |
| 416 | } |
| 417 | |
| 418 | if (entry->type == BTRFS_FREE_SPACE_EXTENT) { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 419 | spin_lock(&ctl->tree_lock); |
| 420 | ret = link_free_space(ctl, e); |
| 421 | spin_unlock(&ctl->tree_lock); |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 422 | BUG_ON(ret); |
| 423 | } else { |
| 424 | e->bitmap = kzalloc(PAGE_CACHE_SIZE, GFP_NOFS); |
| 425 | if (!e->bitmap) { |
| 426 | kunmap(page); |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 427 | kmem_cache_free( |
| 428 | btrfs_free_space_cachep, e); |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 429 | unlock_page(page); |
| 430 | page_cache_release(page); |
| 431 | goto free_cache; |
| 432 | } |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 433 | spin_lock(&ctl->tree_lock); |
| 434 | ret = link_free_space(ctl, e); |
| 435 | ctl->total_bitmaps++; |
| 436 | ctl->op->recalc_thresholds(ctl); |
| 437 | spin_unlock(&ctl->tree_lock); |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 438 | list_add_tail(&e->list, &bitmaps); |
| 439 | } |
| 440 | |
| 441 | num_entries--; |
| 442 | offset += sizeof(struct btrfs_free_space_entry); |
| 443 | if (offset + sizeof(struct btrfs_free_space_entry) >= |
| 444 | PAGE_CACHE_SIZE) |
| 445 | break; |
| 446 | entry++; |
| 447 | } |
| 448 | |
| 449 | /* |
| 450 | * We read an entry out of this page, we need to move on to the |
| 451 | * next page. |
| 452 | */ |
| 453 | if (need_loop) { |
| 454 | kunmap(page); |
| 455 | goto next; |
| 456 | } |
| 457 | |
| 458 | /* |
| 459 | * We add the bitmaps at the end of the entries in order that |
| 460 | * the bitmap entries are added to the cache. |
| 461 | */ |
| 462 | e = list_entry(bitmaps.next, struct btrfs_free_space, list); |
| 463 | list_del_init(&e->list); |
| 464 | memcpy(e->bitmap, addr, PAGE_CACHE_SIZE); |
| 465 | kunmap(page); |
| 466 | num_bitmaps--; |
| 467 | next: |
| 468 | unlock_page(page); |
| 469 | page_cache_release(page); |
| 470 | index++; |
| 471 | } |
| 472 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 473 | spin_lock(&ctl->tree_lock); |
| 474 | if (ctl->free_space != (block_group->key.offset - used - |
| 475 | block_group->bytes_super)) { |
| 476 | spin_unlock(&ctl->tree_lock); |
Josef Bacik | 43be214 | 2011-04-01 14:55:00 +0000 | [diff] [blame] | 477 | printk(KERN_ERR "block group %llu has an wrong amount of free " |
| 478 | "space\n", block_group->key.objectid); |
| 479 | ret = 0; |
| 480 | goto free_cache; |
| 481 | } |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 482 | spin_unlock(&ctl->tree_lock); |
Josef Bacik | 43be214 | 2011-04-01 14:55:00 +0000 | [diff] [blame] | 483 | |
Josef Bacik | 9d66e23 | 2010-08-25 16:54:15 -0400 | [diff] [blame] | 484 | ret = 1; |
| 485 | out: |
| 486 | kfree(checksums); |
| 487 | kfree(disk_crcs); |
| 488 | iput(inode); |
| 489 | return ret; |
| 490 | |
| 491 | free_cache: |
| 492 | /* This cache is bogus, make sure it gets cleared */ |
| 493 | spin_lock(&block_group->lock); |
| 494 | block_group->disk_cache_state = BTRFS_DC_CLEAR; |
| 495 | spin_unlock(&block_group->lock); |
| 496 | btrfs_remove_free_space_cache(block_group); |
| 497 | goto out; |
| 498 | } |
| 499 | |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 500 | int btrfs_write_out_cache(struct btrfs_root *root, |
| 501 | struct btrfs_trans_handle *trans, |
| 502 | struct btrfs_block_group_cache *block_group, |
| 503 | struct btrfs_path *path) |
| 504 | { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 505 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
Josef Bacik | 0cb59c9 | 2010-07-02 12:14:14 -0400 | [diff] [blame] | 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 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 549 | node = rb_first(&ctl->free_space_offset); |
Josef Bacik | 2b20982 | 2010-12-03 13:17:53 -0500 | [diff] [blame] | 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 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 854 | static inline unsigned long offset_to_bit(u64 bitmap_start, u32 unit, |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 855 | u64 offset) |
| 856 | { |
| 857 | BUG_ON(offset < bitmap_start); |
| 858 | offset -= bitmap_start; |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 859 | return (unsigned long)(div_u64(offset, unit)); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 860 | } |
| 861 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 862 | static inline unsigned long bytes_to_bits(u64 bytes, u32 unit) |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 863 | { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 864 | return (unsigned long)(div_u64(bytes, unit)); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 865 | } |
| 866 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 867 | static inline u64 offset_to_bitmap(struct btrfs_free_space_ctl *ctl, |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 868 | u64 offset) |
| 869 | { |
| 870 | u64 bitmap_start; |
| 871 | u64 bytes_per_bitmap; |
| 872 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 873 | bytes_per_bitmap = BITS_PER_BITMAP * ctl->unit; |
| 874 | bitmap_start = offset - ctl->start; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 875 | bitmap_start = div64_u64(bitmap_start, bytes_per_bitmap); |
| 876 | bitmap_start *= bytes_per_bitmap; |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 877 | bitmap_start += ctl->start; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 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 * |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 935 | tree_search_offset(struct btrfs_free_space_ctl *ctl, |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 936 | u64 offset, int bitmap_only, int fuzzy) |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 937 | { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 938 | struct rb_node *n = ctl->free_space_offset.rb_node; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 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 | } |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1034 | if (entry->offset + BITS_PER_BITMAP * ctl->unit > offset) |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1035 | return entry; |
| 1036 | } else if (entry->offset + entry->bytes > offset) |
| 1037 | return entry; |
| 1038 | |
| 1039 | if (!fuzzy) |
| 1040 | return NULL; |
| 1041 | |
| 1042 | while (1) { |
| 1043 | if (entry->bitmap) { |
| 1044 | if (entry->offset + BITS_PER_BITMAP * |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1045 | ctl->unit > offset) |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1046 | break; |
| 1047 | } else { |
| 1048 | if (entry->offset + entry->bytes > offset) |
| 1049 | break; |
| 1050 | } |
| 1051 | |
| 1052 | n = rb_next(&entry->offset_index); |
| 1053 | if (!n) |
| 1054 | return NULL; |
| 1055 | entry = rb_entry(n, struct btrfs_free_space, offset_index); |
| 1056 | } |
| 1057 | return entry; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1058 | } |
| 1059 | |
Li Zefan | f333adb | 2010-11-09 14:57:39 +0800 | [diff] [blame] | 1060 | static inline void |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1061 | __unlink_free_space(struct btrfs_free_space_ctl *ctl, |
Li Zefan | f333adb | 2010-11-09 14:57:39 +0800 | [diff] [blame] | 1062 | struct btrfs_free_space *info) |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1063 | { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1064 | rb_erase(&info->offset_index, &ctl->free_space_offset); |
| 1065 | ctl->free_extents--; |
Li Zefan | f333adb | 2010-11-09 14:57:39 +0800 | [diff] [blame] | 1066 | } |
| 1067 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1068 | static void unlink_free_space(struct btrfs_free_space_ctl *ctl, |
Li Zefan | f333adb | 2010-11-09 14:57:39 +0800 | [diff] [blame] | 1069 | struct btrfs_free_space *info) |
| 1070 | { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1071 | __unlink_free_space(ctl, info); |
| 1072 | ctl->free_space -= info->bytes; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1073 | } |
| 1074 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1075 | static int link_free_space(struct btrfs_free_space_ctl *ctl, |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1076 | struct btrfs_free_space *info) |
| 1077 | { |
| 1078 | int ret = 0; |
| 1079 | |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1080 | BUG_ON(!info->bitmap && !info->bytes); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1081 | ret = tree_insert_offset(&ctl->free_space_offset, info->offset, |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1082 | &info->offset_index, (info->bitmap != NULL)); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1083 | if (ret) |
| 1084 | return ret; |
| 1085 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1086 | ctl->free_space += info->bytes; |
| 1087 | ctl->free_extents++; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1088 | return ret; |
| 1089 | } |
| 1090 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1091 | static void recalculate_thresholds(struct btrfs_free_space_ctl *ctl) |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1092 | { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1093 | struct btrfs_block_group_cache *block_group = ctl->private; |
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; |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1098 | u64 bytes_per_bg = BITS_PER_BITMAP * block_group->sectorsize; |
| 1099 | int max_bitmaps = div64_u64(size + bytes_per_bg - 1, bytes_per_bg); |
| 1100 | |
| 1101 | BUG_ON(ctl->total_bitmaps > max_bitmaps); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1102 | |
| 1103 | /* |
| 1104 | * The goal is to keep the total amount of memory used per 1gb of space |
| 1105 | * at or below 32k, so we need to adjust how much memory we allow to be |
| 1106 | * used by extent based free space tracking |
| 1107 | */ |
Li Zefan | 8eb2d82 | 2010-11-09 14:48:01 +0800 | [diff] [blame] | 1108 | if (size < 1024 * 1024 * 1024) |
| 1109 | max_bytes = MAX_CACHE_BYTES_PER_GIG; |
| 1110 | else |
| 1111 | max_bytes = MAX_CACHE_BYTES_PER_GIG * |
| 1112 | div64_u64(size, 1024 * 1024 * 1024); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1113 | |
Josef Bacik | 25891f7 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 1114 | /* |
| 1115 | * we want to account for 1 more bitmap than what we have so we can make |
| 1116 | * sure we don't go over our overall goal of MAX_CACHE_BYTES_PER_GIG as |
| 1117 | * we add more bitmaps. |
| 1118 | */ |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1119 | bitmap_bytes = (ctl->total_bitmaps + 1) * PAGE_CACHE_SIZE; |
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 | if (bitmap_bytes >= max_bytes) { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1122 | ctl->extents_thresh = 0; |
Josef Bacik | 25891f7 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 1123 | return; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1124 | } |
Josef Bacik | 25891f7 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 1125 | |
| 1126 | /* |
| 1127 | * we want the extent entry threshold to always be at most 1/2 the maxw |
| 1128 | * bytes we can have, or whatever is less than that. |
| 1129 | */ |
| 1130 | extent_bytes = max_bytes - bitmap_bytes; |
| 1131 | extent_bytes = min_t(u64, extent_bytes, div64_u64(max_bytes, 2)); |
| 1132 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1133 | ctl->extents_thresh = |
Josef Bacik | 25891f7 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 1134 | div64_u64(extent_bytes, (sizeof(struct btrfs_free_space))); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1135 | } |
| 1136 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1137 | static void bitmap_clear_bits(struct btrfs_free_space_ctl *ctl, |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1138 | struct btrfs_free_space *info, u64 offset, |
| 1139 | u64 bytes) |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1140 | { |
Li Zefan | f38b6e7 | 2011-03-14 13:40:51 +0800 | [diff] [blame] | 1141 | unsigned long start, count; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1142 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1143 | start = offset_to_bit(info->offset, ctl->unit, offset); |
| 1144 | count = bytes_to_bits(bytes, ctl->unit); |
Li Zefan | f38b6e7 | 2011-03-14 13:40:51 +0800 | [diff] [blame] | 1145 | BUG_ON(start + count > BITS_PER_BITMAP); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1146 | |
Li Zefan | f38b6e7 | 2011-03-14 13:40:51 +0800 | [diff] [blame] | 1147 | bitmap_clear(info->bitmap, start, count); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1148 | |
| 1149 | info->bytes -= bytes; |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1150 | ctl->free_space -= bytes; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1151 | } |
| 1152 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1153 | static void bitmap_set_bits(struct btrfs_free_space_ctl *ctl, |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1154 | struct btrfs_free_space *info, u64 offset, |
| 1155 | u64 bytes) |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1156 | { |
Li Zefan | f38b6e7 | 2011-03-14 13:40:51 +0800 | [diff] [blame] | 1157 | unsigned long start, count; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1158 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1159 | start = offset_to_bit(info->offset, ctl->unit, offset); |
| 1160 | count = bytes_to_bits(bytes, ctl->unit); |
Li Zefan | f38b6e7 | 2011-03-14 13:40:51 +0800 | [diff] [blame] | 1161 | BUG_ON(start + count > BITS_PER_BITMAP); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1162 | |
Li Zefan | f38b6e7 | 2011-03-14 13:40:51 +0800 | [diff] [blame] | 1163 | bitmap_set(info->bitmap, start, count); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1164 | |
| 1165 | info->bytes += bytes; |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1166 | ctl->free_space += bytes; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1167 | } |
| 1168 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1169 | static int search_bitmap(struct btrfs_free_space_ctl *ctl, |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 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 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1177 | i = offset_to_bit(bitmap_info->offset, ctl->unit, |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1178 | max_t(u64, *offset, bitmap_info->offset)); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1179 | bits = bytes_to_bits(*bytes, ctl->unit); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 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) { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1194 | *offset = (u64)(i * ctl->unit) + bitmap_info->offset; |
| 1195 | *bytes = (u64)(found_bits) * ctl->unit; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1196 | return 0; |
| 1197 | } |
| 1198 | |
| 1199 | return -1; |
| 1200 | } |
| 1201 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1202 | static struct btrfs_free_space * |
| 1203 | find_free_space(struct btrfs_free_space_ctl *ctl, u64 *offset, u64 *bytes) |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1204 | { |
| 1205 | struct btrfs_free_space *entry; |
| 1206 | struct rb_node *node; |
| 1207 | int ret; |
| 1208 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1209 | if (!ctl->free_space_offset.rb_node) |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1210 | return NULL; |
| 1211 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1212 | entry = tree_search_offset(ctl, offset_to_bitmap(ctl, *offset), 0, 1); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1213 | if (!entry) |
| 1214 | return NULL; |
| 1215 | |
| 1216 | for (node = &entry->offset_index; node; node = rb_next(node)) { |
| 1217 | entry = rb_entry(node, struct btrfs_free_space, offset_index); |
| 1218 | if (entry->bytes < *bytes) |
| 1219 | continue; |
| 1220 | |
| 1221 | if (entry->bitmap) { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1222 | ret = search_bitmap(ctl, entry, offset, bytes); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1223 | if (!ret) |
| 1224 | return entry; |
| 1225 | continue; |
| 1226 | } |
| 1227 | |
| 1228 | *offset = entry->offset; |
| 1229 | *bytes = entry->bytes; |
| 1230 | return entry; |
| 1231 | } |
| 1232 | |
| 1233 | return NULL; |
| 1234 | } |
| 1235 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1236 | static void add_new_bitmap(struct btrfs_free_space_ctl *ctl, |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1237 | struct btrfs_free_space *info, u64 offset) |
| 1238 | { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1239 | info->offset = offset_to_bitmap(ctl, offset); |
Josef Bacik | f019f42 | 2009-09-11 16:11:20 -0400 | [diff] [blame] | 1240 | info->bytes = 0; |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1241 | link_free_space(ctl, info); |
| 1242 | ctl->total_bitmaps++; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1243 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1244 | ctl->op->recalc_thresholds(ctl); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1245 | } |
| 1246 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1247 | static void free_bitmap(struct btrfs_free_space_ctl *ctl, |
Li Zefan | edf6e2d | 2010-11-09 14:50:07 +0800 | [diff] [blame] | 1248 | struct btrfs_free_space *bitmap_info) |
| 1249 | { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1250 | unlink_free_space(ctl, bitmap_info); |
Li Zefan | edf6e2d | 2010-11-09 14:50:07 +0800 | [diff] [blame] | 1251 | kfree(bitmap_info->bitmap); |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 1252 | kmem_cache_free(btrfs_free_space_cachep, bitmap_info); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1253 | ctl->total_bitmaps--; |
| 1254 | ctl->op->recalc_thresholds(ctl); |
Li Zefan | edf6e2d | 2010-11-09 14:50:07 +0800 | [diff] [blame] | 1255 | } |
| 1256 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1257 | static noinline int remove_from_bitmap(struct btrfs_free_space_ctl *ctl, |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1258 | struct btrfs_free_space *bitmap_info, |
| 1259 | u64 *offset, u64 *bytes) |
| 1260 | { |
| 1261 | u64 end; |
Josef Bacik | 6606bb9 | 2009-07-31 11:03:58 -0400 | [diff] [blame] | 1262 | u64 search_start, search_bytes; |
| 1263 | int ret; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1264 | |
| 1265 | again: |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1266 | end = bitmap_info->offset + (u64)(BITS_PER_BITMAP * ctl->unit) - 1; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1267 | |
Josef Bacik | 6606bb9 | 2009-07-31 11:03:58 -0400 | [diff] [blame] | 1268 | /* |
| 1269 | * XXX - this can go away after a few releases. |
| 1270 | * |
| 1271 | * since the only user of btrfs_remove_free_space is the tree logging |
| 1272 | * stuff, and the only way to test that is under crash conditions, we |
| 1273 | * want to have this debug stuff here just in case somethings not |
| 1274 | * working. Search the bitmap for the space we are trying to use to |
| 1275 | * make sure its actually there. If its not there then we need to stop |
| 1276 | * because something has gone wrong. |
| 1277 | */ |
| 1278 | search_start = *offset; |
| 1279 | search_bytes = *bytes; |
Josef Bacik | 13dbc08 | 2011-02-03 02:39:52 +0000 | [diff] [blame] | 1280 | search_bytes = min(search_bytes, end - search_start + 1); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1281 | ret = search_bitmap(ctl, bitmap_info, &search_start, &search_bytes); |
Josef Bacik | 6606bb9 | 2009-07-31 11:03:58 -0400 | [diff] [blame] | 1282 | BUG_ON(ret < 0 || search_start != *offset); |
| 1283 | |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1284 | if (*offset > bitmap_info->offset && *offset + *bytes > end) { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1285 | bitmap_clear_bits(ctl, bitmap_info, *offset, end - *offset + 1); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1286 | *bytes -= end - *offset + 1; |
| 1287 | *offset = end + 1; |
| 1288 | } else if (*offset >= bitmap_info->offset && *offset + *bytes <= end) { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1289 | bitmap_clear_bits(ctl, bitmap_info, *offset, *bytes); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1290 | *bytes = 0; |
| 1291 | } |
| 1292 | |
| 1293 | if (*bytes) { |
Josef Bacik | 6606bb9 | 2009-07-31 11:03:58 -0400 | [diff] [blame] | 1294 | struct rb_node *next = rb_next(&bitmap_info->offset_index); |
Li Zefan | edf6e2d | 2010-11-09 14:50:07 +0800 | [diff] [blame] | 1295 | if (!bitmap_info->bytes) |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1296 | free_bitmap(ctl, bitmap_info); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1297 | |
Josef Bacik | 6606bb9 | 2009-07-31 11:03:58 -0400 | [diff] [blame] | 1298 | /* |
| 1299 | * no entry after this bitmap, but we still have bytes to |
| 1300 | * remove, so something has gone wrong. |
| 1301 | */ |
| 1302 | if (!next) |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1303 | return -EINVAL; |
| 1304 | |
Josef Bacik | 6606bb9 | 2009-07-31 11:03:58 -0400 | [diff] [blame] | 1305 | bitmap_info = rb_entry(next, struct btrfs_free_space, |
| 1306 | offset_index); |
| 1307 | |
| 1308 | /* |
| 1309 | * if the next entry isn't a bitmap we need to return to let the |
| 1310 | * extent stuff do its work. |
| 1311 | */ |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1312 | if (!bitmap_info->bitmap) |
| 1313 | return -EAGAIN; |
| 1314 | |
Josef Bacik | 6606bb9 | 2009-07-31 11:03:58 -0400 | [diff] [blame] | 1315 | /* |
| 1316 | * Ok the next item is a bitmap, but it may not actually hold |
| 1317 | * the information for the rest of this free space stuff, so |
| 1318 | * look for it, and if we don't find it return so we can try |
| 1319 | * everything over again. |
| 1320 | */ |
| 1321 | search_start = *offset; |
| 1322 | search_bytes = *bytes; |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1323 | ret = search_bitmap(ctl, bitmap_info, &search_start, |
Josef Bacik | 6606bb9 | 2009-07-31 11:03:58 -0400 | [diff] [blame] | 1324 | &search_bytes); |
| 1325 | if (ret < 0 || search_start != *offset) |
| 1326 | return -EAGAIN; |
| 1327 | |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1328 | goto again; |
Li Zefan | edf6e2d | 2010-11-09 14:50:07 +0800 | [diff] [blame] | 1329 | } else if (!bitmap_info->bytes) |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1330 | free_bitmap(ctl, bitmap_info); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1331 | |
| 1332 | return 0; |
| 1333 | } |
| 1334 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1335 | static bool use_bitmap(struct btrfs_free_space_ctl *ctl, |
| 1336 | struct btrfs_free_space *info) |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1337 | { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1338 | struct btrfs_block_group_cache *block_group = ctl->private; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1339 | |
| 1340 | /* |
| 1341 | * If we are below the extents threshold then we can add this as an |
| 1342 | * extent, and don't have to deal with the bitmap |
| 1343 | */ |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1344 | if (ctl->free_extents < ctl->extents_thresh) { |
Josef Bacik | 32cb084 | 2011-03-18 16:16:21 -0400 | [diff] [blame] | 1345 | /* |
| 1346 | * If this block group has some small extents we don't want to |
| 1347 | * use up all of our free slots in the cache with them, we want |
| 1348 | * to reserve them to larger extents, however if we have plent |
| 1349 | * of cache left then go ahead an dadd them, no sense in adding |
| 1350 | * the overhead of a bitmap if we don't have to. |
| 1351 | */ |
| 1352 | if (info->bytes <= block_group->sectorsize * 4) { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1353 | if (ctl->free_extents * 2 <= ctl->extents_thresh) |
| 1354 | return false; |
Josef Bacik | 32cb084 | 2011-03-18 16:16:21 -0400 | [diff] [blame] | 1355 | } else { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1356 | return false; |
Josef Bacik | 32cb084 | 2011-03-18 16:16:21 -0400 | [diff] [blame] | 1357 | } |
| 1358 | } |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1359 | |
| 1360 | /* |
| 1361 | * some block groups are so tiny they can't be enveloped by a bitmap, so |
| 1362 | * don't even bother to create a bitmap for this |
| 1363 | */ |
| 1364 | if (BITS_PER_BITMAP * block_group->sectorsize > |
| 1365 | block_group->key.offset) |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1366 | return false; |
| 1367 | |
| 1368 | return true; |
| 1369 | } |
| 1370 | |
| 1371 | static int insert_into_bitmap(struct btrfs_free_space_ctl *ctl, |
| 1372 | struct btrfs_free_space *info) |
| 1373 | { |
| 1374 | struct btrfs_free_space *bitmap_info; |
| 1375 | int added = 0; |
| 1376 | u64 bytes, offset, end; |
| 1377 | int ret; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1378 | |
| 1379 | bytes = info->bytes; |
| 1380 | offset = info->offset; |
| 1381 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1382 | if (!ctl->op->use_bitmap(ctl, info)) |
| 1383 | return 0; |
| 1384 | |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1385 | again: |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1386 | bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset), |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1387 | 1, 0); |
| 1388 | if (!bitmap_info) { |
| 1389 | BUG_ON(added); |
| 1390 | goto new_bitmap; |
| 1391 | } |
| 1392 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1393 | end = bitmap_info->offset + (u64)(BITS_PER_BITMAP * ctl->unit); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1394 | |
| 1395 | if (offset >= bitmap_info->offset && offset + bytes > end) { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1396 | bitmap_set_bits(ctl, bitmap_info, offset, end - offset); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1397 | bytes -= end - offset; |
| 1398 | offset = end; |
| 1399 | added = 0; |
| 1400 | } else if (offset >= bitmap_info->offset && offset + bytes <= end) { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1401 | bitmap_set_bits(ctl, bitmap_info, offset, bytes); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1402 | bytes = 0; |
| 1403 | } else { |
| 1404 | BUG(); |
| 1405 | } |
| 1406 | |
| 1407 | if (!bytes) { |
| 1408 | ret = 1; |
| 1409 | goto out; |
| 1410 | } else |
| 1411 | goto again; |
| 1412 | |
| 1413 | new_bitmap: |
| 1414 | if (info && info->bitmap) { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1415 | add_new_bitmap(ctl, info, offset); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1416 | added = 1; |
| 1417 | info = NULL; |
| 1418 | goto again; |
| 1419 | } else { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1420 | spin_unlock(&ctl->tree_lock); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1421 | |
| 1422 | /* no pre-allocated info, allocate a new one */ |
| 1423 | if (!info) { |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 1424 | info = kmem_cache_zalloc(btrfs_free_space_cachep, |
| 1425 | GFP_NOFS); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1426 | if (!info) { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1427 | spin_lock(&ctl->tree_lock); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1428 | ret = -ENOMEM; |
| 1429 | goto out; |
| 1430 | } |
| 1431 | } |
| 1432 | |
| 1433 | /* allocate the bitmap */ |
| 1434 | info->bitmap = kzalloc(PAGE_CACHE_SIZE, GFP_NOFS); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1435 | spin_lock(&ctl->tree_lock); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1436 | if (!info->bitmap) { |
| 1437 | ret = -ENOMEM; |
| 1438 | goto out; |
| 1439 | } |
| 1440 | goto again; |
| 1441 | } |
| 1442 | |
| 1443 | out: |
| 1444 | if (info) { |
| 1445 | if (info->bitmap) |
| 1446 | kfree(info->bitmap); |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 1447 | kmem_cache_free(btrfs_free_space_cachep, info); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1448 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1449 | |
| 1450 | return ret; |
| 1451 | } |
| 1452 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1453 | bool try_merge_free_space(struct btrfs_free_space_ctl *ctl, |
Li Zefan | f333adb | 2010-11-09 14:57:39 +0800 | [diff] [blame] | 1454 | struct btrfs_free_space *info, bool update_stat) |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1455 | { |
Li Zefan | 120d66e | 2010-11-09 14:56:50 +0800 | [diff] [blame] | 1456 | struct btrfs_free_space *left_info; |
| 1457 | struct btrfs_free_space *right_info; |
| 1458 | bool merged = false; |
| 1459 | u64 offset = info->offset; |
| 1460 | u64 bytes = info->bytes; |
Josef Bacik | 6226cb0a | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 1461 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1462 | /* |
| 1463 | * first we want to see if there is free space adjacent to the range we |
| 1464 | * are adding, if there is remove that struct and add a new one to |
| 1465 | * cover the entire range |
| 1466 | */ |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1467 | right_info = tree_search_offset(ctl, offset + bytes, 0, 0); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1468 | if (right_info && rb_prev(&right_info->offset_index)) |
| 1469 | left_info = rb_entry(rb_prev(&right_info->offset_index), |
| 1470 | struct btrfs_free_space, offset_index); |
| 1471 | else |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1472 | left_info = tree_search_offset(ctl, offset - 1, 0, 0); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1473 | |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1474 | if (right_info && !right_info->bitmap) { |
Li Zefan | f333adb | 2010-11-09 14:57:39 +0800 | [diff] [blame] | 1475 | if (update_stat) |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1476 | unlink_free_space(ctl, right_info); |
Li Zefan | f333adb | 2010-11-09 14:57:39 +0800 | [diff] [blame] | 1477 | else |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1478 | __unlink_free_space(ctl, right_info); |
Josef Bacik | 6226cb0a | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 1479 | info->bytes += right_info->bytes; |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 1480 | kmem_cache_free(btrfs_free_space_cachep, right_info); |
Li Zefan | 120d66e | 2010-11-09 14:56:50 +0800 | [diff] [blame] | 1481 | merged = true; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1482 | } |
| 1483 | |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1484 | if (left_info && !left_info->bitmap && |
| 1485 | left_info->offset + left_info->bytes == offset) { |
Li Zefan | f333adb | 2010-11-09 14:57:39 +0800 | [diff] [blame] | 1486 | if (update_stat) |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1487 | unlink_free_space(ctl, left_info); |
Li Zefan | f333adb | 2010-11-09 14:57:39 +0800 | [diff] [blame] | 1488 | else |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1489 | __unlink_free_space(ctl, left_info); |
Josef Bacik | 6226cb0a | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 1490 | info->offset = left_info->offset; |
| 1491 | info->bytes += left_info->bytes; |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 1492 | kmem_cache_free(btrfs_free_space_cachep, left_info); |
Li Zefan | 120d66e | 2010-11-09 14:56:50 +0800 | [diff] [blame] | 1493 | merged = true; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1494 | } |
| 1495 | |
Li Zefan | 120d66e | 2010-11-09 14:56:50 +0800 | [diff] [blame] | 1496 | return merged; |
| 1497 | } |
| 1498 | |
| 1499 | int btrfs_add_free_space(struct btrfs_block_group_cache *block_group, |
| 1500 | u64 offset, u64 bytes) |
| 1501 | { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1502 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
Li Zefan | 120d66e | 2010-11-09 14:56:50 +0800 | [diff] [blame] | 1503 | struct btrfs_free_space *info; |
| 1504 | int ret = 0; |
| 1505 | |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 1506 | info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS); |
Li Zefan | 120d66e | 2010-11-09 14:56:50 +0800 | [diff] [blame] | 1507 | if (!info) |
| 1508 | return -ENOMEM; |
| 1509 | |
| 1510 | info->offset = offset; |
| 1511 | info->bytes = bytes; |
| 1512 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1513 | spin_lock(&ctl->tree_lock); |
Li Zefan | 120d66e | 2010-11-09 14:56:50 +0800 | [diff] [blame] | 1514 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1515 | if (try_merge_free_space(ctl, info, true)) |
Li Zefan | 120d66e | 2010-11-09 14:56:50 +0800 | [diff] [blame] | 1516 | goto link; |
| 1517 | |
| 1518 | /* |
| 1519 | * There was no extent directly to the left or right of this new |
| 1520 | * extent then we know we're going to have to allocate a new extent, so |
| 1521 | * before we do that see if we need to drop this into a bitmap |
| 1522 | */ |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1523 | ret = insert_into_bitmap(ctl, info); |
Li Zefan | 120d66e | 2010-11-09 14:56:50 +0800 | [diff] [blame] | 1524 | if (ret < 0) { |
| 1525 | goto out; |
| 1526 | } else if (ret) { |
| 1527 | ret = 0; |
| 1528 | goto out; |
| 1529 | } |
| 1530 | link: |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1531 | ret = link_free_space(ctl, info); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1532 | if (ret) |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 1533 | kmem_cache_free(btrfs_free_space_cachep, info); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1534 | out: |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1535 | spin_unlock(&ctl->tree_lock); |
Josef Bacik | 6226cb0a | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 1536 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1537 | if (ret) { |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1538 | printk(KERN_CRIT "btrfs: unable to add free space :%d\n", ret); |
Stoyan Gaydarov | c293498 | 2009-04-02 17:05:11 -0400 | [diff] [blame] | 1539 | BUG_ON(ret == -EEXIST); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1540 | } |
| 1541 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1542 | return ret; |
| 1543 | } |
| 1544 | |
Josef Bacik | 6226cb0a | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 1545 | int btrfs_remove_free_space(struct btrfs_block_group_cache *block_group, |
| 1546 | u64 offset, u64 bytes) |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1547 | { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1548 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1549 | struct btrfs_free_space *info; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1550 | struct btrfs_free_space *next_info = NULL; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1551 | int ret = 0; |
| 1552 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1553 | spin_lock(&ctl->tree_lock); |
Josef Bacik | 6226cb0a | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 1554 | |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1555 | again: |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1556 | info = tree_search_offset(ctl, offset, 0, 0); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1557 | if (!info) { |
Josef Bacik | 6606bb9 | 2009-07-31 11:03:58 -0400 | [diff] [blame] | 1558 | /* |
| 1559 | * oops didn't find an extent that matched the space we wanted |
| 1560 | * to remove, look for a bitmap instead |
| 1561 | */ |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1562 | info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset), |
Josef Bacik | 6606bb9 | 2009-07-31 11:03:58 -0400 | [diff] [blame] | 1563 | 1, 0); |
| 1564 | if (!info) { |
| 1565 | WARN_ON(1); |
| 1566 | goto out_lock; |
| 1567 | } |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1568 | } |
| 1569 | |
| 1570 | if (info->bytes < bytes && rb_next(&info->offset_index)) { |
| 1571 | u64 end; |
| 1572 | next_info = rb_entry(rb_next(&info->offset_index), |
| 1573 | struct btrfs_free_space, |
| 1574 | offset_index); |
| 1575 | |
| 1576 | if (next_info->bitmap) |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1577 | end = next_info->offset + |
| 1578 | BITS_PER_BITMAP * ctl->unit - 1; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1579 | else |
| 1580 | end = next_info->offset + next_info->bytes; |
| 1581 | |
| 1582 | if (next_info->bytes < bytes || |
| 1583 | next_info->offset > offset || offset > end) { |
| 1584 | printk(KERN_CRIT "Found free space at %llu, size %llu," |
| 1585 | " trying to use %llu\n", |
| 1586 | (unsigned long long)info->offset, |
| 1587 | (unsigned long long)info->bytes, |
| 1588 | (unsigned long long)bytes); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1589 | WARN_ON(1); |
| 1590 | ret = -EINVAL; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1591 | goto out_lock; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1592 | } |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1593 | |
| 1594 | info = next_info; |
| 1595 | } |
| 1596 | |
| 1597 | if (info->bytes == bytes) { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1598 | unlink_free_space(ctl, info); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1599 | if (info->bitmap) { |
| 1600 | kfree(info->bitmap); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1601 | ctl->total_bitmaps--; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1602 | } |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 1603 | kmem_cache_free(btrfs_free_space_cachep, info); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1604 | goto out_lock; |
| 1605 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1606 | |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1607 | if (!info->bitmap && info->offset == offset) { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1608 | unlink_free_space(ctl, info); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1609 | info->offset += bytes; |
| 1610 | info->bytes -= bytes; |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1611 | link_free_space(ctl, info); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1612 | goto out_lock; |
| 1613 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1614 | |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1615 | if (!info->bitmap && info->offset <= offset && |
| 1616 | info->offset + info->bytes >= offset + bytes) { |
Chris Mason | 9b49c9b | 2008-09-24 11:23:25 -0400 | [diff] [blame] | 1617 | u64 old_start = info->offset; |
| 1618 | /* |
| 1619 | * we're freeing space in the middle of the info, |
| 1620 | * this can happen during tree log replay |
| 1621 | * |
| 1622 | * first unlink the old info and then |
| 1623 | * insert it again after the hole we're creating |
| 1624 | */ |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1625 | unlink_free_space(ctl, info); |
Chris Mason | 9b49c9b | 2008-09-24 11:23:25 -0400 | [diff] [blame] | 1626 | if (offset + bytes < info->offset + info->bytes) { |
| 1627 | u64 old_end = info->offset + info->bytes; |
| 1628 | |
| 1629 | info->offset = offset + bytes; |
| 1630 | info->bytes = old_end - info->offset; |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1631 | ret = link_free_space(ctl, info); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1632 | WARN_ON(ret); |
| 1633 | if (ret) |
| 1634 | goto out_lock; |
Chris Mason | 9b49c9b | 2008-09-24 11:23:25 -0400 | [diff] [blame] | 1635 | } else { |
| 1636 | /* the hole we're creating ends at the end |
| 1637 | * of the info struct, just free the info |
| 1638 | */ |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 1639 | kmem_cache_free(btrfs_free_space_cachep, info); |
Chris Mason | 9b49c9b | 2008-09-24 11:23:25 -0400 | [diff] [blame] | 1640 | } |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1641 | spin_unlock(&ctl->tree_lock); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1642 | |
| 1643 | /* step two, insert a new info struct to cover |
| 1644 | * anything before the hole |
Chris Mason | 9b49c9b | 2008-09-24 11:23:25 -0400 | [diff] [blame] | 1645 | */ |
Josef Bacik | 6226cb0a | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 1646 | ret = btrfs_add_free_space(block_group, old_start, |
| 1647 | offset - old_start); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1648 | WARN_ON(ret); |
| 1649 | goto out; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1650 | } |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1651 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1652 | ret = remove_from_bitmap(ctl, info, &offset, &bytes); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1653 | if (ret == -EAGAIN) |
| 1654 | goto again; |
| 1655 | BUG_ON(ret); |
| 1656 | out_lock: |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1657 | spin_unlock(&ctl->tree_lock); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1658 | out: |
Josef Bacik | 2517920 | 2008-10-29 14:49:05 -0400 | [diff] [blame] | 1659 | return ret; |
| 1660 | } |
| 1661 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1662 | void btrfs_dump_free_space(struct btrfs_block_group_cache *block_group, |
| 1663 | u64 bytes) |
| 1664 | { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1665 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1666 | struct btrfs_free_space *info; |
| 1667 | struct rb_node *n; |
| 1668 | int count = 0; |
| 1669 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1670 | for (n = rb_first(&ctl->free_space_offset); n; n = rb_next(n)) { |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1671 | info = rb_entry(n, struct btrfs_free_space, offset_index); |
| 1672 | if (info->bytes >= bytes) |
| 1673 | count++; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1674 | printk(KERN_CRIT "entry offset %llu, bytes %llu, bitmap %s\n", |
Joel Becker | 2138093 | 2009-04-21 12:38:29 -0700 | [diff] [blame] | 1675 | (unsigned long long)info->offset, |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1676 | (unsigned long long)info->bytes, |
| 1677 | (info->bitmap) ? "yes" : "no"); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1678 | } |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1679 | printk(KERN_INFO "block group has cluster?: %s\n", |
| 1680 | list_empty(&block_group->cluster_list) ? "no" : "yes"); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1681 | printk(KERN_INFO "%d blocks of free space at or bigger than bytes is" |
| 1682 | "\n", count); |
| 1683 | } |
| 1684 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1685 | static struct btrfs_free_space_op free_space_op = { |
| 1686 | .recalc_thresholds = recalculate_thresholds, |
| 1687 | .use_bitmap = use_bitmap, |
| 1688 | }; |
| 1689 | |
| 1690 | void btrfs_init_free_space_ctl(struct btrfs_block_group_cache *block_group) |
| 1691 | { |
| 1692 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
| 1693 | |
| 1694 | spin_lock_init(&ctl->tree_lock); |
| 1695 | ctl->unit = block_group->sectorsize; |
| 1696 | ctl->start = block_group->key.objectid; |
| 1697 | ctl->private = block_group; |
| 1698 | ctl->op = &free_space_op; |
| 1699 | |
| 1700 | /* |
| 1701 | * we only want to have 32k of ram per block group for keeping |
| 1702 | * track of free space, and if we pass 1/2 of that we want to |
| 1703 | * start converting things over to using bitmaps |
| 1704 | */ |
| 1705 | ctl->extents_thresh = ((1024 * 32) / 2) / |
| 1706 | sizeof(struct btrfs_free_space); |
| 1707 | } |
| 1708 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 1709 | /* |
| 1710 | * for a given cluster, put all of its extents back into the free |
| 1711 | * space cache. If the block group passed doesn't match the block group |
| 1712 | * pointed to by the cluster, someone else raced in and freed the |
| 1713 | * cluster already. In that case, we just return without changing anything |
| 1714 | */ |
| 1715 | static int |
| 1716 | __btrfs_return_cluster_to_free_space( |
| 1717 | struct btrfs_block_group_cache *block_group, |
| 1718 | struct btrfs_free_cluster *cluster) |
| 1719 | { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1720 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 1721 | struct btrfs_free_space *entry; |
| 1722 | struct rb_node *node; |
| 1723 | |
| 1724 | spin_lock(&cluster->lock); |
| 1725 | if (cluster->block_group != block_group) |
| 1726 | goto out; |
| 1727 | |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1728 | cluster->block_group = NULL; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 1729 | cluster->window_start = 0; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1730 | list_del_init(&cluster->block_group_list); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1731 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 1732 | node = rb_first(&cluster->root); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1733 | while (node) { |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 1734 | bool bitmap; |
| 1735 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 1736 | entry = rb_entry(node, struct btrfs_free_space, offset_index); |
| 1737 | node = rb_next(&entry->offset_index); |
| 1738 | rb_erase(&entry->offset_index, &cluster->root); |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 1739 | |
| 1740 | bitmap = (entry->bitmap != NULL); |
| 1741 | if (!bitmap) |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1742 | try_merge_free_space(ctl, entry, false); |
| 1743 | tree_insert_offset(&ctl->free_space_offset, |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 1744 | entry->offset, &entry->offset_index, bitmap); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 1745 | } |
Eric Paris | 6bef4d3 | 2010-02-23 19:43:04 +0000 | [diff] [blame] | 1746 | cluster->root = RB_ROOT; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1747 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 1748 | out: |
| 1749 | spin_unlock(&cluster->lock); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1750 | btrfs_put_block_group(block_group); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 1751 | return 0; |
| 1752 | } |
| 1753 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1754 | void btrfs_remove_free_space_cache(struct btrfs_block_group_cache *block_group) |
| 1755 | { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1756 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1757 | struct btrfs_free_space *info; |
| 1758 | struct rb_node *node; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 1759 | struct btrfs_free_cluster *cluster; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1760 | struct list_head *head; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1761 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1762 | spin_lock(&ctl->tree_lock); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1763 | while ((head = block_group->cluster_list.next) != |
| 1764 | &block_group->cluster_list) { |
| 1765 | cluster = list_entry(head, struct btrfs_free_cluster, |
| 1766 | block_group_list); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 1767 | |
| 1768 | WARN_ON(cluster->block_group != block_group); |
| 1769 | __btrfs_return_cluster_to_free_space(block_group, cluster); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1770 | if (need_resched()) { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1771 | spin_unlock(&ctl->tree_lock); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1772 | cond_resched(); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1773 | spin_lock(&ctl->tree_lock); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1774 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 1775 | } |
| 1776 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1777 | while ((node = rb_last(&ctl->free_space_offset)) != NULL) { |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1778 | info = rb_entry(node, struct btrfs_free_space, offset_index); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1779 | unlink_free_space(ctl, info); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1780 | if (info->bitmap) |
| 1781 | kfree(info->bitmap); |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 1782 | kmem_cache_free(btrfs_free_space_cachep, info); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1783 | if (need_resched()) { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1784 | spin_unlock(&ctl->tree_lock); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1785 | cond_resched(); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1786 | spin_lock(&ctl->tree_lock); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1787 | } |
| 1788 | } |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1789 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1790 | spin_unlock(&ctl->tree_lock); |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1791 | } |
| 1792 | |
Josef Bacik | 6226cb0a | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 1793 | u64 btrfs_find_space_for_alloc(struct btrfs_block_group_cache *block_group, |
| 1794 | u64 offset, u64 bytes, u64 empty_size) |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1795 | { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1796 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
Josef Bacik | 6226cb0a | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 1797 | struct btrfs_free_space *entry = NULL; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1798 | u64 bytes_search = bytes + empty_size; |
Josef Bacik | 6226cb0a | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 1799 | u64 ret = 0; |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1800 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1801 | spin_lock(&ctl->tree_lock); |
| 1802 | entry = find_free_space(ctl, &offset, &bytes_search); |
Josef Bacik | 6226cb0a | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 1803 | if (!entry) |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1804 | goto out; |
| 1805 | |
| 1806 | ret = offset; |
| 1807 | if (entry->bitmap) { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1808 | bitmap_clear_bits(ctl, entry, offset, bytes); |
Li Zefan | edf6e2d | 2010-11-09 14:50:07 +0800 | [diff] [blame] | 1809 | if (!entry->bytes) |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1810 | free_bitmap(ctl, entry); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1811 | } else { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1812 | unlink_free_space(ctl, entry); |
Josef Bacik | 6226cb0a | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 1813 | entry->offset += bytes; |
| 1814 | entry->bytes -= bytes; |
Josef Bacik | 6226cb0a | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 1815 | if (!entry->bytes) |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 1816 | kmem_cache_free(btrfs_free_space_cachep, entry); |
Josef Bacik | 6226cb0a | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 1817 | else |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1818 | link_free_space(ctl, entry); |
Josef Bacik | 6226cb0a | 2009-04-03 10:14:18 -0400 | [diff] [blame] | 1819 | } |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1820 | |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1821 | out: |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1822 | spin_unlock(&ctl->tree_lock); |
Josef Bacik | 817d52f | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1823 | |
Josef Bacik | 0f9dd46 | 2008-09-23 13:14:11 -0400 | [diff] [blame] | 1824 | return ret; |
| 1825 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 1826 | |
| 1827 | /* |
| 1828 | * given a cluster, put all of its extents back into the free space |
| 1829 | * cache. If a block group is passed, this function will only free |
| 1830 | * a cluster that belongs to the passed block group. |
| 1831 | * |
| 1832 | * Otherwise, it'll get a reference on the block group pointed to by the |
| 1833 | * cluster and remove the cluster from it. |
| 1834 | */ |
| 1835 | int btrfs_return_cluster_to_free_space( |
| 1836 | struct btrfs_block_group_cache *block_group, |
| 1837 | struct btrfs_free_cluster *cluster) |
| 1838 | { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1839 | struct btrfs_free_space_ctl *ctl; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 1840 | int ret; |
| 1841 | |
| 1842 | /* first, get a safe pointer to the block group */ |
| 1843 | spin_lock(&cluster->lock); |
| 1844 | if (!block_group) { |
| 1845 | block_group = cluster->block_group; |
| 1846 | if (!block_group) { |
| 1847 | spin_unlock(&cluster->lock); |
| 1848 | return 0; |
| 1849 | } |
| 1850 | } else if (cluster->block_group != block_group) { |
| 1851 | /* someone else has already freed it don't redo their work */ |
| 1852 | spin_unlock(&cluster->lock); |
| 1853 | return 0; |
| 1854 | } |
| 1855 | atomic_inc(&block_group->count); |
| 1856 | spin_unlock(&cluster->lock); |
| 1857 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1858 | ctl = block_group->free_space_ctl; |
| 1859 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 1860 | /* now return any extents the cluster had on it */ |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1861 | spin_lock(&ctl->tree_lock); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 1862 | ret = __btrfs_return_cluster_to_free_space(block_group, cluster); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1863 | spin_unlock(&ctl->tree_lock); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 1864 | |
| 1865 | /* finally drop our ref */ |
| 1866 | btrfs_put_block_group(block_group); |
| 1867 | return ret; |
| 1868 | } |
| 1869 | |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1870 | static u64 btrfs_alloc_from_bitmap(struct btrfs_block_group_cache *block_group, |
| 1871 | struct btrfs_free_cluster *cluster, |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 1872 | struct btrfs_free_space *entry, |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1873 | u64 bytes, u64 min_start) |
| 1874 | { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1875 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1876 | int err; |
| 1877 | u64 search_start = cluster->window_start; |
| 1878 | u64 search_bytes = bytes; |
| 1879 | u64 ret = 0; |
| 1880 | |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1881 | search_start = min_start; |
| 1882 | search_bytes = bytes; |
| 1883 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1884 | err = search_bitmap(ctl, entry, &search_start, &search_bytes); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1885 | if (err) |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 1886 | return 0; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1887 | |
| 1888 | ret = search_start; |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1889 | bitmap_clear_bits(ctl, entry, ret, bytes); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1890 | |
| 1891 | return ret; |
| 1892 | } |
| 1893 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 1894 | /* |
| 1895 | * given a cluster, try to allocate 'bytes' from it, returns 0 |
| 1896 | * if it couldn't find anything suitably large, or a logical disk offset |
| 1897 | * if things worked out |
| 1898 | */ |
| 1899 | u64 btrfs_alloc_from_cluster(struct btrfs_block_group_cache *block_group, |
| 1900 | struct btrfs_free_cluster *cluster, u64 bytes, |
| 1901 | u64 min_start) |
| 1902 | { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1903 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 1904 | struct btrfs_free_space *entry = NULL; |
| 1905 | struct rb_node *node; |
| 1906 | u64 ret = 0; |
| 1907 | |
| 1908 | spin_lock(&cluster->lock); |
| 1909 | if (bytes > cluster->max_size) |
| 1910 | goto out; |
| 1911 | |
| 1912 | if (cluster->block_group != block_group) |
| 1913 | goto out; |
| 1914 | |
| 1915 | node = rb_first(&cluster->root); |
| 1916 | if (!node) |
| 1917 | goto out; |
| 1918 | |
| 1919 | entry = rb_entry(node, struct btrfs_free_space, offset_index); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 1920 | while(1) { |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 1921 | if (entry->bytes < bytes || |
| 1922 | (!entry->bitmap && entry->offset < min_start)) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 1923 | struct rb_node *node; |
| 1924 | |
| 1925 | node = rb_next(&entry->offset_index); |
| 1926 | if (!node) |
| 1927 | break; |
| 1928 | entry = rb_entry(node, struct btrfs_free_space, |
| 1929 | offset_index); |
| 1930 | continue; |
| 1931 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 1932 | |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 1933 | if (entry->bitmap) { |
| 1934 | ret = btrfs_alloc_from_bitmap(block_group, |
| 1935 | cluster, entry, bytes, |
| 1936 | min_start); |
| 1937 | if (ret == 0) { |
| 1938 | struct rb_node *node; |
| 1939 | node = rb_next(&entry->offset_index); |
| 1940 | if (!node) |
| 1941 | break; |
| 1942 | entry = rb_entry(node, struct btrfs_free_space, |
| 1943 | offset_index); |
| 1944 | continue; |
| 1945 | } |
| 1946 | } else { |
| 1947 | |
| 1948 | ret = entry->offset; |
| 1949 | |
| 1950 | entry->offset += bytes; |
| 1951 | entry->bytes -= bytes; |
| 1952 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 1953 | |
Li Zefan | 5e71b5d | 2010-11-09 14:55:34 +0800 | [diff] [blame] | 1954 | if (entry->bytes == 0) |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 1955 | rb_erase(&entry->offset_index, &cluster->root); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 1956 | break; |
| 1957 | } |
| 1958 | out: |
| 1959 | spin_unlock(&cluster->lock); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1960 | |
Li Zefan | 5e71b5d | 2010-11-09 14:55:34 +0800 | [diff] [blame] | 1961 | if (!ret) |
| 1962 | return 0; |
| 1963 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1964 | spin_lock(&ctl->tree_lock); |
Li Zefan | 5e71b5d | 2010-11-09 14:55:34 +0800 | [diff] [blame] | 1965 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1966 | ctl->free_space -= bytes; |
Li Zefan | 5e71b5d | 2010-11-09 14:55:34 +0800 | [diff] [blame] | 1967 | if (entry->bytes == 0) { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1968 | ctl->free_extents--; |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 1969 | if (entry->bitmap) { |
| 1970 | kfree(entry->bitmap); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1971 | ctl->total_bitmaps--; |
| 1972 | ctl->op->recalc_thresholds(ctl); |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 1973 | } |
Josef Bacik | dc89e98 | 2011-01-28 17:05:48 -0500 | [diff] [blame] | 1974 | kmem_cache_free(btrfs_free_space_cachep, entry); |
Li Zefan | 5e71b5d | 2010-11-09 14:55:34 +0800 | [diff] [blame] | 1975 | } |
| 1976 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1977 | spin_unlock(&ctl->tree_lock); |
Li Zefan | 5e71b5d | 2010-11-09 14:55:34 +0800 | [diff] [blame] | 1978 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 1979 | return ret; |
| 1980 | } |
| 1981 | |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1982 | static int btrfs_bitmap_cluster(struct btrfs_block_group_cache *block_group, |
| 1983 | struct btrfs_free_space *entry, |
| 1984 | struct btrfs_free_cluster *cluster, |
| 1985 | u64 offset, u64 bytes, u64 min_bytes) |
| 1986 | { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 1987 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1988 | unsigned long next_zero; |
| 1989 | unsigned long i; |
| 1990 | unsigned long search_bits; |
| 1991 | unsigned long total_bits; |
| 1992 | unsigned long found_bits; |
| 1993 | unsigned long start = 0; |
| 1994 | unsigned long total_found = 0; |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 1995 | int ret; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 1996 | bool found = false; |
| 1997 | |
| 1998 | i = offset_to_bit(entry->offset, block_group->sectorsize, |
| 1999 | max_t(u64, offset, entry->offset)); |
Josef Bacik | d0a365e | 2011-03-18 15:27:43 -0400 | [diff] [blame] | 2000 | search_bits = bytes_to_bits(bytes, block_group->sectorsize); |
| 2001 | total_bits = bytes_to_bits(min_bytes, block_group->sectorsize); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2002 | |
| 2003 | again: |
| 2004 | found_bits = 0; |
| 2005 | for (i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, i); |
| 2006 | i < BITS_PER_BITMAP; |
| 2007 | i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, i + 1)) { |
| 2008 | next_zero = find_next_zero_bit(entry->bitmap, |
| 2009 | BITS_PER_BITMAP, i); |
| 2010 | if (next_zero - i >= search_bits) { |
| 2011 | found_bits = next_zero - i; |
| 2012 | break; |
| 2013 | } |
| 2014 | i = next_zero; |
| 2015 | } |
| 2016 | |
| 2017 | if (!found_bits) |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 2018 | return -ENOSPC; |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2019 | |
| 2020 | if (!found) { |
| 2021 | start = i; |
| 2022 | found = true; |
| 2023 | } |
| 2024 | |
| 2025 | total_found += found_bits; |
| 2026 | |
| 2027 | if (cluster->max_size < found_bits * block_group->sectorsize) |
| 2028 | cluster->max_size = found_bits * block_group->sectorsize; |
| 2029 | |
| 2030 | if (total_found < total_bits) { |
| 2031 | i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, next_zero); |
| 2032 | if (i - start > total_bits * 2) { |
| 2033 | total_found = 0; |
| 2034 | cluster->max_size = 0; |
| 2035 | found = false; |
| 2036 | } |
| 2037 | goto again; |
| 2038 | } |
| 2039 | |
| 2040 | cluster->window_start = start * block_group->sectorsize + |
| 2041 | entry->offset; |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 2042 | rb_erase(&entry->offset_index, &ctl->free_space_offset); |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 2043 | ret = tree_insert_offset(&cluster->root, entry->offset, |
| 2044 | &entry->offset_index, 1); |
| 2045 | BUG_ON(ret); |
Josef Bacik | 9630308 | 2009-07-13 21:29:25 -0400 | [diff] [blame] | 2046 | |
| 2047 | return 0; |
| 2048 | } |
| 2049 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2050 | /* |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 2051 | * This searches the block group for just extents to fill the cluster with. |
| 2052 | */ |
| 2053 | static int setup_cluster_no_bitmap(struct btrfs_block_group_cache *block_group, |
| 2054 | struct btrfs_free_cluster *cluster, |
| 2055 | u64 offset, u64 bytes, u64 min_bytes) |
| 2056 | { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 2057 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 2058 | struct btrfs_free_space *first = NULL; |
| 2059 | struct btrfs_free_space *entry = NULL; |
| 2060 | struct btrfs_free_space *prev = NULL; |
| 2061 | struct btrfs_free_space *last; |
| 2062 | struct rb_node *node; |
| 2063 | u64 window_start; |
| 2064 | u64 window_free; |
| 2065 | u64 max_extent; |
| 2066 | u64 max_gap = 128 * 1024; |
| 2067 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 2068 | entry = tree_search_offset(ctl, offset, 0, 1); |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 2069 | if (!entry) |
| 2070 | return -ENOSPC; |
| 2071 | |
| 2072 | /* |
| 2073 | * We don't want bitmaps, so just move along until we find a normal |
| 2074 | * extent entry. |
| 2075 | */ |
| 2076 | while (entry->bitmap) { |
| 2077 | node = rb_next(&entry->offset_index); |
| 2078 | if (!node) |
| 2079 | return -ENOSPC; |
| 2080 | entry = rb_entry(node, struct btrfs_free_space, offset_index); |
| 2081 | } |
| 2082 | |
| 2083 | window_start = entry->offset; |
| 2084 | window_free = entry->bytes; |
| 2085 | max_extent = entry->bytes; |
| 2086 | first = entry; |
| 2087 | last = entry; |
| 2088 | prev = entry; |
| 2089 | |
| 2090 | while (window_free <= min_bytes) { |
| 2091 | node = rb_next(&entry->offset_index); |
| 2092 | if (!node) |
| 2093 | return -ENOSPC; |
| 2094 | entry = rb_entry(node, struct btrfs_free_space, offset_index); |
| 2095 | |
| 2096 | if (entry->bitmap) |
| 2097 | continue; |
| 2098 | /* |
| 2099 | * we haven't filled the empty size and the window is |
| 2100 | * very large. reset and try again |
| 2101 | */ |
| 2102 | if (entry->offset - (prev->offset + prev->bytes) > max_gap || |
| 2103 | entry->offset - window_start > (min_bytes * 2)) { |
| 2104 | first = entry; |
| 2105 | window_start = entry->offset; |
| 2106 | window_free = entry->bytes; |
| 2107 | last = entry; |
| 2108 | max_extent = entry->bytes; |
| 2109 | } else { |
| 2110 | last = entry; |
| 2111 | window_free += entry->bytes; |
| 2112 | if (entry->bytes > max_extent) |
| 2113 | max_extent = entry->bytes; |
| 2114 | } |
| 2115 | prev = entry; |
| 2116 | } |
| 2117 | |
| 2118 | cluster->window_start = first->offset; |
| 2119 | |
| 2120 | node = &first->offset_index; |
| 2121 | |
| 2122 | /* |
| 2123 | * now we've found our entries, pull them out of the free space |
| 2124 | * cache and put them into the cluster rbtree |
| 2125 | */ |
| 2126 | do { |
| 2127 | int ret; |
| 2128 | |
| 2129 | entry = rb_entry(node, struct btrfs_free_space, offset_index); |
| 2130 | node = rb_next(&entry->offset_index); |
| 2131 | if (entry->bitmap) |
| 2132 | continue; |
| 2133 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 2134 | rb_erase(&entry->offset_index, &ctl->free_space_offset); |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 2135 | ret = tree_insert_offset(&cluster->root, entry->offset, |
| 2136 | &entry->offset_index, 0); |
| 2137 | BUG_ON(ret); |
| 2138 | } while (node && entry != last); |
| 2139 | |
| 2140 | cluster->max_size = max_extent; |
| 2141 | |
| 2142 | return 0; |
| 2143 | } |
| 2144 | |
| 2145 | /* |
| 2146 | * This specifically looks for bitmaps that may work in the cluster, we assume |
| 2147 | * that we have already failed to find extents that will work. |
| 2148 | */ |
| 2149 | static int setup_cluster_bitmap(struct btrfs_block_group_cache *block_group, |
| 2150 | struct btrfs_free_cluster *cluster, |
| 2151 | u64 offset, u64 bytes, u64 min_bytes) |
| 2152 | { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 2153 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 2154 | struct btrfs_free_space *entry; |
| 2155 | struct rb_node *node; |
| 2156 | int ret = -ENOSPC; |
| 2157 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 2158 | if (ctl->total_bitmaps == 0) |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 2159 | return -ENOSPC; |
| 2160 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 2161 | entry = tree_search_offset(ctl, offset_to_bitmap(ctl, offset), 0, 1); |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 2162 | if (!entry) |
| 2163 | return -ENOSPC; |
| 2164 | |
| 2165 | node = &entry->offset_index; |
| 2166 | do { |
| 2167 | entry = rb_entry(node, struct btrfs_free_space, offset_index); |
| 2168 | node = rb_next(&entry->offset_index); |
| 2169 | if (!entry->bitmap) |
| 2170 | continue; |
| 2171 | if (entry->bytes < min_bytes) |
| 2172 | continue; |
| 2173 | ret = btrfs_bitmap_cluster(block_group, entry, cluster, offset, |
| 2174 | bytes, min_bytes); |
| 2175 | } while (ret && node); |
| 2176 | |
| 2177 | return ret; |
| 2178 | } |
| 2179 | |
| 2180 | /* |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2181 | * here we try to find a cluster of blocks in a block group. The goal |
| 2182 | * is to find at least bytes free and up to empty_size + bytes free. |
| 2183 | * We might not find them all in one contiguous area. |
| 2184 | * |
| 2185 | * returns zero and sets up cluster if things worked out, otherwise |
| 2186 | * it returns -enospc |
| 2187 | */ |
| 2188 | int btrfs_find_space_cluster(struct btrfs_trans_handle *trans, |
Chris Mason | 451d758 | 2009-06-09 20:28:34 -0400 | [diff] [blame] | 2189 | struct btrfs_root *root, |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2190 | struct btrfs_block_group_cache *block_group, |
| 2191 | struct btrfs_free_cluster *cluster, |
| 2192 | u64 offset, u64 bytes, u64 empty_size) |
| 2193 | { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 2194 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2195 | u64 min_bytes; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2196 | int ret; |
| 2197 | |
| 2198 | /* for metadata, allow allocates with more holes */ |
Chris Mason | 451d758 | 2009-06-09 20:28:34 -0400 | [diff] [blame] | 2199 | if (btrfs_test_opt(root, SSD_SPREAD)) { |
| 2200 | min_bytes = bytes + empty_size; |
| 2201 | } else if (block_group->flags & BTRFS_BLOCK_GROUP_METADATA) { |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2202 | /* |
| 2203 | * we want to do larger allocations when we are |
| 2204 | * flushing out the delayed refs, it helps prevent |
| 2205 | * making more work as we go along. |
| 2206 | */ |
| 2207 | if (trans->transaction->delayed_refs.flushing) |
| 2208 | min_bytes = max(bytes, (bytes + empty_size) >> 1); |
| 2209 | else |
| 2210 | min_bytes = max(bytes, (bytes + empty_size) >> 4); |
| 2211 | } else |
| 2212 | min_bytes = max(bytes, (bytes + empty_size) >> 2); |
| 2213 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 2214 | spin_lock(&ctl->tree_lock); |
Josef Bacik | 7d0d2e8 | 2011-03-18 15:13:42 -0400 | [diff] [blame] | 2215 | |
| 2216 | /* |
| 2217 | * If we know we don't have enough space to make a cluster don't even |
| 2218 | * bother doing all the work to try and find one. |
| 2219 | */ |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 2220 | if (ctl->free_space < min_bytes) { |
| 2221 | spin_unlock(&ctl->tree_lock); |
Josef Bacik | 7d0d2e8 | 2011-03-18 15:13:42 -0400 | [diff] [blame] | 2222 | return -ENOSPC; |
| 2223 | } |
| 2224 | |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2225 | spin_lock(&cluster->lock); |
| 2226 | |
| 2227 | /* someone already found a cluster, hooray */ |
| 2228 | if (cluster->block_group) { |
| 2229 | ret = 0; |
| 2230 | goto out; |
| 2231 | } |
Josef Bacik | 4e69b59 | 2011-03-21 10:11:24 -0400 | [diff] [blame] | 2232 | |
| 2233 | ret = setup_cluster_no_bitmap(block_group, cluster, offset, bytes, |
| 2234 | min_bytes); |
| 2235 | if (ret) |
| 2236 | ret = setup_cluster_bitmap(block_group, cluster, offset, |
| 2237 | bytes, min_bytes); |
| 2238 | |
| 2239 | if (!ret) { |
| 2240 | atomic_inc(&block_group->count); |
| 2241 | list_add_tail(&cluster->block_group_list, |
| 2242 | &block_group->cluster_list); |
| 2243 | cluster->block_group = block_group; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2244 | } |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2245 | out: |
| 2246 | spin_unlock(&cluster->lock); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 2247 | spin_unlock(&ctl->tree_lock); |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2248 | |
| 2249 | return ret; |
| 2250 | } |
| 2251 | |
| 2252 | /* |
| 2253 | * simple code to zero out a cluster |
| 2254 | */ |
| 2255 | void btrfs_init_free_cluster(struct btrfs_free_cluster *cluster) |
| 2256 | { |
| 2257 | spin_lock_init(&cluster->lock); |
| 2258 | spin_lock_init(&cluster->refill_lock); |
Eric Paris | 6bef4d3 | 2010-02-23 19:43:04 +0000 | [diff] [blame] | 2259 | cluster->root = RB_ROOT; |
Chris Mason | fa9c0d79 | 2009-04-03 09:47:43 -0400 | [diff] [blame] | 2260 | cluster->max_size = 0; |
| 2261 | INIT_LIST_HEAD(&cluster->block_group_list); |
| 2262 | cluster->block_group = NULL; |
| 2263 | } |
| 2264 | |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 2265 | int btrfs_trim_block_group(struct btrfs_block_group_cache *block_group, |
| 2266 | u64 *trimmed, u64 start, u64 end, u64 minlen) |
| 2267 | { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 2268 | struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl; |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 2269 | struct btrfs_free_space *entry = NULL; |
| 2270 | struct btrfs_fs_info *fs_info = block_group->fs_info; |
| 2271 | u64 bytes = 0; |
| 2272 | u64 actually_trimmed; |
| 2273 | int ret = 0; |
| 2274 | |
| 2275 | *trimmed = 0; |
| 2276 | |
| 2277 | while (start < end) { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 2278 | spin_lock(&ctl->tree_lock); |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 2279 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 2280 | if (ctl->free_space < minlen) { |
| 2281 | spin_unlock(&ctl->tree_lock); |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 2282 | break; |
| 2283 | } |
| 2284 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 2285 | entry = tree_search_offset(ctl, start, 0, 1); |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 2286 | if (!entry) |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 2287 | entry = tree_search_offset(ctl, |
| 2288 | offset_to_bitmap(ctl, start), |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 2289 | 1, 1); |
| 2290 | |
| 2291 | if (!entry || entry->offset >= end) { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 2292 | spin_unlock(&ctl->tree_lock); |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 2293 | break; |
| 2294 | } |
| 2295 | |
| 2296 | if (entry->bitmap) { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 2297 | ret = search_bitmap(ctl, entry, &start, &bytes); |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 2298 | if (!ret) { |
| 2299 | if (start >= end) { |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 2300 | spin_unlock(&ctl->tree_lock); |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 2301 | break; |
| 2302 | } |
| 2303 | bytes = min(bytes, end - start); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 2304 | bitmap_clear_bits(ctl, entry, start, bytes); |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 2305 | if (entry->bytes == 0) |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 2306 | free_bitmap(ctl, entry); |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 2307 | } else { |
| 2308 | start = entry->offset + BITS_PER_BITMAP * |
| 2309 | block_group->sectorsize; |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 2310 | spin_unlock(&ctl->tree_lock); |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 2311 | ret = 0; |
| 2312 | continue; |
| 2313 | } |
| 2314 | } else { |
| 2315 | start = entry->offset; |
| 2316 | bytes = min(entry->bytes, end - start); |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 2317 | unlink_free_space(ctl, entry); |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 2318 | kfree(entry); |
| 2319 | } |
| 2320 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 2321 | spin_unlock(&ctl->tree_lock); |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 2322 | |
| 2323 | if (bytes >= minlen) { |
| 2324 | int update_ret; |
| 2325 | update_ret = btrfs_update_reserved_bytes(block_group, |
| 2326 | bytes, 1, 1); |
| 2327 | |
| 2328 | ret = btrfs_error_discard_extent(fs_info->extent_root, |
| 2329 | start, |
| 2330 | bytes, |
| 2331 | &actually_trimmed); |
| 2332 | |
Li Zefan | 34d52cb | 2011-03-29 13:46:06 +0800 | [diff] [blame^] | 2333 | btrfs_add_free_space(block_group, start, bytes); |
Li Dongyang | f7039b1 | 2011-03-24 10:24:28 +0000 | [diff] [blame] | 2334 | if (!update_ret) |
| 2335 | btrfs_update_reserved_bytes(block_group, |
| 2336 | bytes, 0, 1); |
| 2337 | |
| 2338 | if (ret) |
| 2339 | break; |
| 2340 | *trimmed += actually_trimmed; |
| 2341 | } |
| 2342 | start += bytes; |
| 2343 | bytes = 0; |
| 2344 | |
| 2345 | if (fatal_signal_pending(current)) { |
| 2346 | ret = -ERESTARTSYS; |
| 2347 | break; |
| 2348 | } |
| 2349 | |
| 2350 | cond_resched(); |
| 2351 | } |
| 2352 | |
| 2353 | return ret; |
| 2354 | } |