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