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