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