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