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