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