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