blob: ffc42ef44711e3bf5c1334396ebb8a4a716f0440 [file] [log] [blame]
Josef Bacik0f9dd462008-09-23 13:14:11 -04001/*
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 Bacik96303082009-07-13 21:29:25 -040019#include <linux/pagemap.h>
Josef Bacik0f9dd462008-09-23 13:14:11 -040020#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Josef Bacik96303082009-07-13 21:29:25 -040022#include <linux/math64.h>
Josef Bacik6ab60602011-08-08 08:24:46 -040023#include <linux/ratelimit.h>
Josef Bacik0f9dd462008-09-23 13:14:11 -040024#include "ctree.h"
Chris Masonfa9c0d72009-04-03 09:47:43 -040025#include "free-space-cache.h"
26#include "transaction.h"
Josef Bacik0af3d002010-06-21 14:48:16 -040027#include "disk-io.h"
Josef Bacik43be2142011-04-01 14:55:00 +000028#include "extent_io.h"
Li Zefan581bb052011-04-20 10:06:11 +080029#include "inode-map.h"
Chris Masonfa9c0d72009-04-03 09:47:43 -040030
Josef Bacik96303082009-07-13 21:29:25 -040031#define BITS_PER_BITMAP (PAGE_CACHE_SIZE * 8)
32#define MAX_CACHE_BYTES_PER_GIG (32 * 1024)
33
Li Zefan34d52cb6c2011-03-29 13:46:06 +080034static int link_free_space(struct btrfs_free_space_ctl *ctl,
Josef Bacik0cb59c92010-07-02 12:14:14 -040035 struct btrfs_free_space *info);
36
Li Zefan0414efa2011-04-20 10:20:14 +080037static struct inode *__lookup_free_space_inode(struct btrfs_root *root,
38 struct btrfs_path *path,
39 u64 offset)
Josef Bacik0af3d002010-06-21 14:48:16 -040040{
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 Bacik0af3d002010-06-21 14:48:16 -040049 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
Li Zefan0414efa2011-04-20 10:20:14 +080050 key.offset = offset;
Josef Bacik0af3d002010-06-21 14:48:16 -040051 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 Sterbab3b4aa72011-04-21 01:20:15 +020057 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -040058 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 Sterbab3b4aa72011-04-21 01:20:15 +020066 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -040067
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 Xieadae52b2011-03-31 09:43:23 +000078 inode->i_mapping->flags &= ~__GFP_FS;
79
Li Zefan0414efa2011-04-20 10:20:14 +080080 return inode;
81}
82
83struct 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;
88
89 spin_lock(&block_group->lock);
90 if (block_group->inode)
91 inode = igrab(block_group->inode);
92 spin_unlock(&block_group->lock);
93 if (inode)
94 return inode;
95
96 inode = __lookup_free_space_inode(root, path,
97 block_group->key.objectid);
98 if (IS_ERR(inode))
99 return inode;
100
Josef Bacik0af3d002010-06-21 14:48:16 -0400101 spin_lock(&block_group->lock);
Josef Bacik2f356122011-06-10 15:31:13 -0400102 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) {
103 printk(KERN_INFO "Old style space inode found, converting.\n");
104 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NODATASUM;
105 block_group->disk_cache_state = BTRFS_DC_CLEAR;
106 }
107
Josef Bacik300e4f82011-08-29 14:06:00 -0400108 if (!block_group->iref) {
Josef Bacik0af3d002010-06-21 14:48:16 -0400109 block_group->inode = igrab(inode);
110 block_group->iref = 1;
111 }
112 spin_unlock(&block_group->lock);
113
114 return inode;
115}
116
Li Zefan0414efa2011-04-20 10:20:14 +0800117int __create_free_space_inode(struct btrfs_root *root,
118 struct btrfs_trans_handle *trans,
119 struct btrfs_path *path, u64 ino, u64 offset)
Josef Bacik0af3d002010-06-21 14:48:16 -0400120{
121 struct btrfs_key key;
122 struct btrfs_disk_key disk_key;
123 struct btrfs_free_space_header *header;
124 struct btrfs_inode_item *inode_item;
125 struct extent_buffer *leaf;
Josef Bacik0af3d002010-06-21 14:48:16 -0400126 int ret;
127
Li Zefan0414efa2011-04-20 10:20:14 +0800128 ret = btrfs_insert_empty_inode(trans, root, path, ino);
Josef Bacik0af3d002010-06-21 14:48:16 -0400129 if (ret)
130 return ret;
131
132 leaf = path->nodes[0];
133 inode_item = btrfs_item_ptr(leaf, path->slots[0],
134 struct btrfs_inode_item);
135 btrfs_item_key(leaf, &disk_key, path->slots[0]);
136 memset_extent_buffer(leaf, 0, (unsigned long)inode_item,
137 sizeof(*inode_item));
138 btrfs_set_inode_generation(leaf, inode_item, trans->transid);
139 btrfs_set_inode_size(leaf, inode_item, 0);
140 btrfs_set_inode_nbytes(leaf, inode_item, 0);
141 btrfs_set_inode_uid(leaf, inode_item, 0);
142 btrfs_set_inode_gid(leaf, inode_item, 0);
143 btrfs_set_inode_mode(leaf, inode_item, S_IFREG | 0600);
144 btrfs_set_inode_flags(leaf, inode_item, BTRFS_INODE_NOCOMPRESS |
Josef Bacik2f356122011-06-10 15:31:13 -0400145 BTRFS_INODE_PREALLOC);
Josef Bacik0af3d002010-06-21 14:48:16 -0400146 btrfs_set_inode_nlink(leaf, inode_item, 1);
147 btrfs_set_inode_transid(leaf, inode_item, trans->transid);
Li Zefan0414efa2011-04-20 10:20:14 +0800148 btrfs_set_inode_block_group(leaf, inode_item, offset);
Josef Bacik0af3d002010-06-21 14:48:16 -0400149 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +0200150 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -0400151
152 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
Li Zefan0414efa2011-04-20 10:20:14 +0800153 key.offset = offset;
Josef Bacik0af3d002010-06-21 14:48:16 -0400154 key.type = 0;
155
156 ret = btrfs_insert_empty_item(trans, root, path, &key,
157 sizeof(struct btrfs_free_space_header));
158 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200159 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -0400160 return ret;
161 }
162 leaf = path->nodes[0];
163 header = btrfs_item_ptr(leaf, path->slots[0],
164 struct btrfs_free_space_header);
165 memset_extent_buffer(leaf, 0, (unsigned long)header, sizeof(*header));
166 btrfs_set_free_space_key(leaf, header, &disk_key);
167 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +0200168 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -0400169
170 return 0;
171}
172
Li Zefan0414efa2011-04-20 10:20:14 +0800173int create_free_space_inode(struct btrfs_root *root,
174 struct btrfs_trans_handle *trans,
175 struct btrfs_block_group_cache *block_group,
176 struct btrfs_path *path)
177{
178 int ret;
179 u64 ino;
180
181 ret = btrfs_find_free_objectid(root, &ino);
182 if (ret < 0)
183 return ret;
184
185 return __create_free_space_inode(root, trans, path, ino,
186 block_group->key.objectid);
187}
188
Josef Bacik0af3d002010-06-21 14:48:16 -0400189int btrfs_truncate_free_space_cache(struct btrfs_root *root,
190 struct btrfs_trans_handle *trans,
191 struct btrfs_path *path,
192 struct inode *inode)
193{
Liu Bo65450aa2011-09-11 10:52:24 -0400194 struct btrfs_block_rsv *rsv;
Josef Bacik0af3d002010-06-21 14:48:16 -0400195 loff_t oldsize;
196 int ret = 0;
197
Liu Bo65450aa2011-09-11 10:52:24 -0400198 rsv = trans->block_rsv;
Josef Bacik0af3d002010-06-21 14:48:16 -0400199 trans->block_rsv = root->orphan_block_rsv;
Josef Bacik4a92b1b2011-08-30 12:34:28 -0400200 ret = btrfs_block_rsv_check(root, root->orphan_block_rsv, 0, 5, 0);
Josef Bacik0af3d002010-06-21 14:48:16 -0400201 if (ret)
202 return ret;
203
204 oldsize = i_size_read(inode);
205 btrfs_i_size_write(inode, 0);
206 truncate_pagecache(inode, oldsize, 0);
207
208 /*
209 * We don't need an orphan item because truncating the free space cache
210 * will never be split across transactions.
211 */
212 ret = btrfs_truncate_inode_items(trans, root, inode,
213 0, BTRFS_EXTENT_DATA_KEY);
Liu Bo65450aa2011-09-11 10:52:24 -0400214
215 trans->block_rsv = rsv;
Josef Bacik0af3d002010-06-21 14:48:16 -0400216 if (ret) {
217 WARN_ON(1);
218 return ret;
219 }
220
Li Zefan82d59022011-04-20 10:33:24 +0800221 ret = btrfs_update_inode(trans, root, inode);
222 return ret;
Josef Bacik0af3d002010-06-21 14:48:16 -0400223}
224
Josef Bacik9d66e232010-08-25 16:54:15 -0400225static int readahead_cache(struct inode *inode)
226{
227 struct file_ra_state *ra;
228 unsigned long last_index;
229
230 ra = kzalloc(sizeof(*ra), GFP_NOFS);
231 if (!ra)
232 return -ENOMEM;
233
234 file_ra_state_init(ra, inode->i_mapping);
235 last_index = (i_size_read(inode) - 1) >> PAGE_CACHE_SHIFT;
236
237 page_cache_sync_readahead(inode->i_mapping, ra, NULL, 0, last_index);
238
239 kfree(ra);
240
241 return 0;
242}
243
Li Zefan0414efa2011-04-20 10:20:14 +0800244int __load_free_space_cache(struct btrfs_root *root, struct inode *inode,
245 struct btrfs_free_space_ctl *ctl,
246 struct btrfs_path *path, u64 offset)
Josef Bacik9d66e232010-08-25 16:54:15 -0400247{
Josef Bacik9d66e232010-08-25 16:54:15 -0400248 struct btrfs_free_space_header *header;
249 struct extent_buffer *leaf;
250 struct page *page;
Josef Bacik9d66e232010-08-25 16:54:15 -0400251 struct btrfs_key key;
252 struct list_head bitmaps;
253 u64 num_entries;
254 u64 num_bitmaps;
255 u64 generation;
Josef Bacik9d66e232010-08-25 16:54:15 -0400256 pgoff_t index = 0;
Josef Bacikf6a39822011-06-06 10:50:35 -0400257 int ret = 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400258
259 INIT_LIST_HEAD(&bitmaps);
260
Josef Bacik9d66e232010-08-25 16:54:15 -0400261 /* Nothing in the space cache, goodbye */
Li Zefan0414efa2011-04-20 10:20:14 +0800262 if (!i_size_read(inode))
Josef Bacik9d66e232010-08-25 16:54:15 -0400263 goto out;
Josef Bacik9d66e232010-08-25 16:54:15 -0400264
265 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
Li Zefan0414efa2011-04-20 10:20:14 +0800266 key.offset = offset;
Josef Bacik9d66e232010-08-25 16:54:15 -0400267 key.type = 0;
268
269 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Li Zefan0414efa2011-04-20 10:20:14 +0800270 if (ret < 0)
271 goto out;
272 else if (ret > 0) {
Chris Mason945d8962011-05-22 12:33:42 -0400273 btrfs_release_path(path);
Li Zefan0414efa2011-04-20 10:20:14 +0800274 ret = 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400275 goto out;
276 }
277
Li Zefan0414efa2011-04-20 10:20:14 +0800278 ret = -1;
279
Josef Bacik9d66e232010-08-25 16:54:15 -0400280 leaf = path->nodes[0];
281 header = btrfs_item_ptr(leaf, path->slots[0],
282 struct btrfs_free_space_header);
283 num_entries = btrfs_free_space_entries(leaf, header);
284 num_bitmaps = btrfs_free_space_bitmaps(leaf, header);
285 generation = btrfs_free_space_generation(leaf, header);
Chris Mason945d8962011-05-22 12:33:42 -0400286 btrfs_release_path(path);
Josef Bacik9d66e232010-08-25 16:54:15 -0400287
288 if (BTRFS_I(inode)->generation != generation) {
289 printk(KERN_ERR "btrfs: free space inode generation (%llu) did"
Li Zefan0414efa2011-04-20 10:20:14 +0800290 " not match free space cache generation (%llu)\n",
Josef Bacik9d66e232010-08-25 16:54:15 -0400291 (unsigned long long)BTRFS_I(inode)->generation,
Li Zefan0414efa2011-04-20 10:20:14 +0800292 (unsigned long long)generation);
293 goto out;
Josef Bacik9d66e232010-08-25 16:54:15 -0400294 }
295
296 if (!num_entries)
297 goto out;
298
Josef Bacik9d66e232010-08-25 16:54:15 -0400299 ret = readahead_cache(inode);
Li Zefan0414efa2011-04-20 10:20:14 +0800300 if (ret)
Josef Bacik9d66e232010-08-25 16:54:15 -0400301 goto out;
Josef Bacik9d66e232010-08-25 16:54:15 -0400302
303 while (1) {
304 struct btrfs_free_space_entry *entry;
305 struct btrfs_free_space *e;
306 void *addr;
307 unsigned long offset = 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400308 int need_loop = 0;
309
310 if (!num_entries && !num_bitmaps)
311 break;
312
Josef Bacika94733d2011-07-11 10:47:06 -0400313 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
Li Zefan0414efa2011-04-20 10:20:14 +0800314 if (!page)
Josef Bacik9d66e232010-08-25 16:54:15 -0400315 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400316
317 if (!PageUptodate(page)) {
318 btrfs_readpage(NULL, page);
319 lock_page(page);
320 if (!PageUptodate(page)) {
321 unlock_page(page);
322 page_cache_release(page);
323 printk(KERN_ERR "btrfs: error reading free "
Li Zefan0414efa2011-04-20 10:20:14 +0800324 "space cache\n");
Josef Bacik9d66e232010-08-25 16:54:15 -0400325 goto free_cache;
326 }
327 }
328 addr = kmap(page);
329
330 if (index == 0) {
331 u64 *gen;
332
Josef Bacik2f356122011-06-10 15:31:13 -0400333 /*
334 * We put a bogus crc in the front of the first page in
335 * case old kernels try to mount a fs with the new
336 * format to make sure they discard the cache.
337 */
338 addr += sizeof(u64);
339 offset += sizeof(u64);
340
341 gen = addr;
Josef Bacik9d66e232010-08-25 16:54:15 -0400342 if (*gen != BTRFS_I(inode)->generation) {
Josef Bacik6ab60602011-08-08 08:24:46 -0400343 printk_ratelimited(KERN_ERR "btrfs: space cache"
344 " generation (%llu) does not match "
345 "inode (%llu)\n",
346 (unsigned long long)*gen,
347 (unsigned long long)
348 BTRFS_I(inode)->generation);
Josef Bacik9d66e232010-08-25 16:54:15 -0400349 kunmap(page);
350 unlock_page(page);
351 page_cache_release(page);
352 goto free_cache;
353 }
Josef Bacik2f356122011-06-10 15:31:13 -0400354 addr += sizeof(u64);
355 offset += sizeof(u64);
Josef Bacik9d66e232010-08-25 16:54:15 -0400356 }
Josef Bacik2f356122011-06-10 15:31:13 -0400357 entry = addr;
Josef Bacik9d66e232010-08-25 16:54:15 -0400358
359 while (1) {
360 if (!num_entries)
361 break;
362
363 need_loop = 1;
Josef Bacikdc89e982011-01-28 17:05:48 -0500364 e = kmem_cache_zalloc(btrfs_free_space_cachep,
365 GFP_NOFS);
Josef Bacik9d66e232010-08-25 16:54:15 -0400366 if (!e) {
367 kunmap(page);
368 unlock_page(page);
369 page_cache_release(page);
370 goto free_cache;
371 }
372
373 e->offset = le64_to_cpu(entry->offset);
374 e->bytes = le64_to_cpu(entry->bytes);
375 if (!e->bytes) {
376 kunmap(page);
Josef Bacikdc89e982011-01-28 17:05:48 -0500377 kmem_cache_free(btrfs_free_space_cachep, e);
Josef Bacik9d66e232010-08-25 16:54:15 -0400378 unlock_page(page);
379 page_cache_release(page);
380 goto free_cache;
381 }
382
383 if (entry->type == BTRFS_FREE_SPACE_EXTENT) {
Li Zefan34d52cb6c2011-03-29 13:46:06 +0800384 spin_lock(&ctl->tree_lock);
385 ret = link_free_space(ctl, e);
386 spin_unlock(&ctl->tree_lock);
Josef Bacik207dde82011-05-13 14:49:23 -0400387 if (ret) {
388 printk(KERN_ERR "Duplicate entries in "
389 "free space cache, dumping\n");
390 kunmap(page);
391 unlock_page(page);
392 page_cache_release(page);
393 goto free_cache;
394 }
Josef Bacik9d66e232010-08-25 16:54:15 -0400395 } else {
396 e->bitmap = kzalloc(PAGE_CACHE_SIZE, GFP_NOFS);
397 if (!e->bitmap) {
398 kunmap(page);
Josef Bacikdc89e982011-01-28 17:05:48 -0500399 kmem_cache_free(
400 btrfs_free_space_cachep, e);
Josef Bacik9d66e232010-08-25 16:54:15 -0400401 unlock_page(page);
402 page_cache_release(page);
403 goto free_cache;
404 }
Li Zefan34d52cb6c2011-03-29 13:46:06 +0800405 spin_lock(&ctl->tree_lock);
Josef Bacikf6a39822011-06-06 10:50:35 -0400406 ret = link_free_space(ctl, e);
Li Zefan34d52cb6c2011-03-29 13:46:06 +0800407 ctl->total_bitmaps++;
408 ctl->op->recalc_thresholds(ctl);
409 spin_unlock(&ctl->tree_lock);
Josef Bacik207dde82011-05-13 14:49:23 -0400410 if (ret) {
411 printk(KERN_ERR "Duplicate entries in "
412 "free space cache, dumping\n");
413 kunmap(page);
414 unlock_page(page);
415 page_cache_release(page);
416 goto free_cache;
417 }
Josef Bacikf6a39822011-06-06 10:50:35 -0400418 list_add_tail(&e->list, &bitmaps);
Josef Bacik9d66e232010-08-25 16:54:15 -0400419 }
420
421 num_entries--;
422 offset += sizeof(struct btrfs_free_space_entry);
423 if (offset + sizeof(struct btrfs_free_space_entry) >=
424 PAGE_CACHE_SIZE)
425 break;
426 entry++;
427 }
428
429 /*
430 * We read an entry out of this page, we need to move on to the
431 * next page.
432 */
433 if (need_loop) {
434 kunmap(page);
435 goto next;
436 }
437
438 /*
439 * We add the bitmaps at the end of the entries in order that
440 * the bitmap entries are added to the cache.
441 */
442 e = list_entry(bitmaps.next, struct btrfs_free_space, list);
443 list_del_init(&e->list);
444 memcpy(e->bitmap, addr, PAGE_CACHE_SIZE);
445 kunmap(page);
446 num_bitmaps--;
447next:
448 unlock_page(page);
449 page_cache_release(page);
450 index++;
451 }
452
453 ret = 1;
454out:
Josef Bacik9d66e232010-08-25 16:54:15 -0400455 return ret;
Josef Bacik9d66e232010-08-25 16:54:15 -0400456free_cache:
Li Zefan0414efa2011-04-20 10:20:14 +0800457 __btrfs_remove_free_space_cache(ctl);
Josef Bacik9d66e232010-08-25 16:54:15 -0400458 goto out;
459}
460
Li Zefan0414efa2011-04-20 10:20:14 +0800461int load_free_space_cache(struct btrfs_fs_info *fs_info,
462 struct btrfs_block_group_cache *block_group)
Josef Bacik0cb59c92010-07-02 12:14:14 -0400463{
Li Zefan34d52cb6c2011-03-29 13:46:06 +0800464 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Li Zefan0414efa2011-04-20 10:20:14 +0800465 struct btrfs_root *root = fs_info->tree_root;
466 struct inode *inode;
467 struct btrfs_path *path;
468 int ret;
469 bool matched;
470 u64 used = btrfs_block_group_used(&block_group->item);
471
472 /*
473 * If we're unmounting then just return, since this does a search on the
474 * normal root and not the commit root and we could deadlock.
475 */
David Sterba7841cb22011-05-31 18:07:27 +0200476 if (btrfs_fs_closing(fs_info))
Li Zefan0414efa2011-04-20 10:20:14 +0800477 return 0;
478
479 /*
480 * If this block group has been marked to be cleared for one reason or
481 * another then we can't trust the on disk cache, so just return.
482 */
483 spin_lock(&block_group->lock);
484 if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) {
485 spin_unlock(&block_group->lock);
486 return 0;
487 }
488 spin_unlock(&block_group->lock);
489
490 path = btrfs_alloc_path();
491 if (!path)
492 return 0;
493
494 inode = lookup_free_space_inode(root, block_group, path);
495 if (IS_ERR(inode)) {
496 btrfs_free_path(path);
497 return 0;
498 }
499
500 ret = __load_free_space_cache(fs_info->tree_root, inode, ctl,
501 path, block_group->key.objectid);
502 btrfs_free_path(path);
503 if (ret <= 0)
504 goto out;
505
506 spin_lock(&ctl->tree_lock);
507 matched = (ctl->free_space == (block_group->key.offset - used -
508 block_group->bytes_super));
509 spin_unlock(&ctl->tree_lock);
510
511 if (!matched) {
512 __btrfs_remove_free_space_cache(ctl);
513 printk(KERN_ERR "block group %llu has an wrong amount of free "
514 "space\n", block_group->key.objectid);
515 ret = -1;
516 }
517out:
518 if (ret < 0) {
519 /* This cache is bogus, make sure it gets cleared */
520 spin_lock(&block_group->lock);
521 block_group->disk_cache_state = BTRFS_DC_CLEAR;
522 spin_unlock(&block_group->lock);
Li Zefan82d59022011-04-20 10:33:24 +0800523 ret = 0;
Li Zefan0414efa2011-04-20 10:20:14 +0800524
525 printk(KERN_ERR "btrfs: failed to load free space cache "
526 "for block group %llu\n", block_group->key.objectid);
527 }
528
529 iput(inode);
530 return ret;
531}
532
Josef Bacikc09544e2011-08-30 10:19:10 -0400533/**
534 * __btrfs_write_out_cache - write out cached info to an inode
535 * @root - the root the inode belongs to
536 * @ctl - the free space cache we are going to write out
537 * @block_group - the block_group for this cache if it belongs to a block_group
538 * @trans - the trans handle
539 * @path - the path to use
540 * @offset - the offset for the key we'll insert
541 *
542 * This function writes out a free space cache struct to disk for quick recovery
543 * on mount. This will return 0 if it was successfull in writing the cache out,
544 * and -1 if it was not.
545 */
Li Zefan0414efa2011-04-20 10:20:14 +0800546int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode,
547 struct btrfs_free_space_ctl *ctl,
548 struct btrfs_block_group_cache *block_group,
549 struct btrfs_trans_handle *trans,
550 struct btrfs_path *path, u64 offset)
Josef Bacik0cb59c92010-07-02 12:14:14 -0400551{
552 struct btrfs_free_space_header *header;
553 struct extent_buffer *leaf;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400554 struct rb_node *node;
555 struct list_head *pos, *n;
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400556 struct page **pages;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400557 struct page *page;
558 struct extent_state *cached_state = NULL;
Josef Bacik43be2142011-04-01 14:55:00 +0000559 struct btrfs_free_cluster *cluster = NULL;
560 struct extent_io_tree *unpin = NULL;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400561 struct list_head bitmap_list;
562 struct btrfs_key key;
Josef Bacik43be2142011-04-01 14:55:00 +0000563 u64 start, end, len;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400564 u64 bytes = 0;
Josef Bacik2f356122011-06-10 15:31:13 -0400565 u32 crc = ~(u32)0;
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400566 int index = 0, num_pages = 0;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400567 int entries = 0;
568 int bitmaps = 0;
Josef Bacikc09544e2011-08-30 10:19:10 -0400569 int ret;
570 int err = -1;
Josef Bacik43be2142011-04-01 14:55:00 +0000571 bool next_page = false;
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400572 bool out_of_space = false;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400573
Josef Bacik0cb59c92010-07-02 12:14:14 -0400574 INIT_LIST_HEAD(&bitmap_list);
575
Li Zefan34d52cb6c2011-03-29 13:46:06 +0800576 node = rb_first(&ctl->free_space_offset);
Li Zefan0414efa2011-04-20 10:20:14 +0800577 if (!node)
Josef Bacikc09544e2011-08-30 10:19:10 -0400578 return -1;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400579
Li Zefan0414efa2011-04-20 10:20:14 +0800580 if (!i_size_read(inode))
581 return -1;
Josef Bacik2b209822010-12-03 13:17:53 -0500582
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400583 num_pages = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
584 PAGE_CACHE_SHIFT;
Chris Mason211f96c2011-06-03 01:26:53 -0400585
Josef Bacik0cb59c92010-07-02 12:14:14 -0400586 filemap_write_and_wait(inode->i_mapping);
587 btrfs_wait_ordered_range(inode, inode->i_size &
588 ~(root->sectorsize - 1), (u64)-1);
589
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400590 pages = kzalloc(sizeof(struct page *) * num_pages, GFP_NOFS);
Josef Bacik2f356122011-06-10 15:31:13 -0400591 if (!pages)
Li Zefan0414efa2011-04-20 10:20:14 +0800592 return -1;
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400593
Josef Bacik43be2142011-04-01 14:55:00 +0000594 /* Get the cluster for this block_group if it exists */
Li Zefan0414efa2011-04-20 10:20:14 +0800595 if (block_group && !list_empty(&block_group->cluster_list))
Josef Bacik43be2142011-04-01 14:55:00 +0000596 cluster = list_entry(block_group->cluster_list.next,
597 struct btrfs_free_cluster,
598 block_group_list);
599
600 /*
601 * We shouldn't have switched the pinned extents yet so this is the
602 * right one
603 */
604 unpin = root->fs_info->pinned_extents;
605
Josef Bacik0cb59c92010-07-02 12:14:14 -0400606 /*
607 * Lock all pages first so we can lock the extent safely.
608 *
609 * NOTE: Because we hold the ref the entire time we're going to write to
610 * the page find_get_page should never fail, so we don't do a check
611 * after find_get_page at this point. Just putting this here so people
612 * know and don't freak out.
613 */
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400614 while (index < num_pages) {
Josef Bacika94733d2011-07-11 10:47:06 -0400615 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400616 if (!page) {
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400617 int i;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400618
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400619 for (i = 0; i < num_pages; i++) {
620 unlock_page(pages[i]);
621 page_cache_release(pages[i]);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400622 }
Josef Bacik2f356122011-06-10 15:31:13 -0400623 goto out;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400624 }
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400625 pages[index] = page;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400626 index++;
627 }
628
629 index = 0;
630 lock_extent_bits(&BTRFS_I(inode)->io_tree, 0, i_size_read(inode) - 1,
631 0, &cached_state, GFP_NOFS);
632
Josef Bacik43be2142011-04-01 14:55:00 +0000633 /*
634 * When searching for pinned extents, we need to start at our start
635 * offset.
636 */
Li Zefan0414efa2011-04-20 10:20:14 +0800637 if (block_group)
638 start = block_group->key.objectid;
Josef Bacik43be2142011-04-01 14:55:00 +0000639
Josef Bacik0cb59c92010-07-02 12:14:14 -0400640 /* Write out the extent entries */
641 do {
642 struct btrfs_free_space_entry *entry;
Josef Bacik2f356122011-06-10 15:31:13 -0400643 void *addr, *orig;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400644 unsigned long offset = 0;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400645
Josef Bacik43be2142011-04-01 14:55:00 +0000646 next_page = false;
647
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400648 if (index >= num_pages) {
649 out_of_space = true;
650 break;
651 }
652
653 page = pages[index];
Josef Bacik0cb59c92010-07-02 12:14:14 -0400654
Josef Bacik2f356122011-06-10 15:31:13 -0400655 orig = addr = kmap(page);
656 if (index == 0) {
657 u64 *gen;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400658
Josef Bacik2f356122011-06-10 15:31:13 -0400659 /*
660 * We're going to put in a bogus crc for this page to
661 * make sure that old kernels who aren't aware of this
662 * format will be sure to discard the cache.
663 */
664 addr += sizeof(u64);
665 offset += sizeof(u64);
666
667 gen = addr;
668 *gen = trans->transid;
669 addr += sizeof(u64);
670 offset += sizeof(u64);
671 }
672 entry = addr;
673
674 memset(addr, 0, PAGE_CACHE_SIZE - offset);
Josef Bacik43be2142011-04-01 14:55:00 +0000675 while (node && !next_page) {
Josef Bacik0cb59c92010-07-02 12:14:14 -0400676 struct btrfs_free_space *e;
677
678 e = rb_entry(node, struct btrfs_free_space, offset_index);
679 entries++;
680
681 entry->offset = cpu_to_le64(e->offset);
682 entry->bytes = cpu_to_le64(e->bytes);
683 if (e->bitmap) {
684 entry->type = BTRFS_FREE_SPACE_BITMAP;
685 list_add_tail(&e->list, &bitmap_list);
686 bitmaps++;
687 } else {
688 entry->type = BTRFS_FREE_SPACE_EXTENT;
689 }
690 node = rb_next(node);
Josef Bacik43be2142011-04-01 14:55:00 +0000691 if (!node && cluster) {
692 node = rb_first(&cluster->root);
693 cluster = NULL;
694 }
Josef Bacik0cb59c92010-07-02 12:14:14 -0400695 offset += sizeof(struct btrfs_free_space_entry);
696 if (offset + sizeof(struct btrfs_free_space_entry) >=
697 PAGE_CACHE_SIZE)
Josef Bacik43be2142011-04-01 14:55:00 +0000698 next_page = true;
699 entry++;
700 }
701
702 /*
703 * We want to add any pinned extents to our free space cache
704 * so we don't leak the space
705 */
Li Zefan0414efa2011-04-20 10:20:14 +0800706 while (block_group && !next_page &&
707 (start < block_group->key.objectid +
708 block_group->key.offset)) {
Josef Bacik43be2142011-04-01 14:55:00 +0000709 ret = find_first_extent_bit(unpin, start, &start, &end,
710 EXTENT_DIRTY);
711 if (ret) {
712 ret = 0;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400713 break;
Josef Bacik43be2142011-04-01 14:55:00 +0000714 }
715
716 /* This pinned extent is out of our range */
717 if (start >= block_group->key.objectid +
718 block_group->key.offset)
719 break;
720
721 len = block_group->key.objectid +
722 block_group->key.offset - start;
723 len = min(len, end + 1 - start);
724
725 entries++;
726 entry->offset = cpu_to_le64(start);
727 entry->bytes = cpu_to_le64(len);
728 entry->type = BTRFS_FREE_SPACE_EXTENT;
729
730 start = end + 1;
731 offset += sizeof(struct btrfs_free_space_entry);
732 if (offset + sizeof(struct btrfs_free_space_entry) >=
733 PAGE_CACHE_SIZE)
734 next_page = true;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400735 entry++;
736 }
Josef Bacik0cb59c92010-07-02 12:14:14 -0400737
Josef Bacik2f356122011-06-10 15:31:13 -0400738 /* Generate bogus crc value */
739 if (index == 0) {
740 u32 *tmp;
741 crc = btrfs_csum_data(root, orig + sizeof(u64), crc,
742 PAGE_CACHE_SIZE - sizeof(u64));
743 btrfs_csum_final(crc, (char *)&crc);
744 crc++;
745 tmp = orig;
746 *tmp = crc;
747 }
748
749 kunmap(page);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400750
751 bytes += PAGE_CACHE_SIZE;
752
Josef Bacik0cb59c92010-07-02 12:14:14 -0400753 index++;
Josef Bacik43be2142011-04-01 14:55:00 +0000754 } while (node || next_page);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400755
756 /* Write out the bitmaps */
757 list_for_each_safe(pos, n, &bitmap_list) {
758 void *addr;
759 struct btrfs_free_space *entry =
760 list_entry(pos, struct btrfs_free_space, list);
761
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400762 if (index >= num_pages) {
763 out_of_space = true;
764 break;
765 }
Chris Masonf65647c2011-04-18 08:55:34 -0400766 page = pages[index];
Josef Bacik0cb59c92010-07-02 12:14:14 -0400767
768 addr = kmap(page);
769 memcpy(addr, entry->bitmap, PAGE_CACHE_SIZE);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400770 kunmap(page);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400771 bytes += PAGE_CACHE_SIZE;
772
Josef Bacik0cb59c92010-07-02 12:14:14 -0400773 list_del_init(&entry->list);
774 index++;
775 }
776
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400777 if (out_of_space) {
778 btrfs_drop_pages(pages, num_pages);
779 unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
780 i_size_read(inode) - 1, &cached_state,
781 GFP_NOFS);
Josef Bacik2f356122011-06-10 15:31:13 -0400782 goto out;
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400783 }
784
Josef Bacik0cb59c92010-07-02 12:14:14 -0400785 /* Zero out the rest of the pages just to make sure */
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400786 while (index < num_pages) {
Josef Bacik0cb59c92010-07-02 12:14:14 -0400787 void *addr;
788
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400789 page = pages[index];
Josef Bacik0cb59c92010-07-02 12:14:14 -0400790 addr = kmap(page);
791 memset(addr, 0, PAGE_CACHE_SIZE);
792 kunmap(page);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400793 bytes += PAGE_CACHE_SIZE;
794 index++;
795 }
796
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400797 ret = btrfs_dirty_pages(root, inode, pages, num_pages, 0,
798 bytes, &cached_state);
799 btrfs_drop_pages(pages, num_pages);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400800 unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
801 i_size_read(inode) - 1, &cached_state, GFP_NOFS);
802
Josef Bacikc09544e2011-08-30 10:19:10 -0400803 if (ret)
Josef Bacik2f356122011-06-10 15:31:13 -0400804 goto out;
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400805
806 BTRFS_I(inode)->generation = trans->transid;
807
Josef Bacik0cb59c92010-07-02 12:14:14 -0400808 filemap_write_and_wait(inode->i_mapping);
809
810 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
Li Zefan0414efa2011-04-20 10:20:14 +0800811 key.offset = offset;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400812 key.type = 0;
813
Josef Bacika9b5fcd2011-08-19 12:06:12 -0400814 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400815 if (ret < 0) {
Josef Bacik0cb59c92010-07-02 12:14:14 -0400816 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, bytes - 1,
817 EXTENT_DIRTY | EXTENT_DELALLOC |
818 EXTENT_DO_ACCOUNTING, 0, 0, NULL, GFP_NOFS);
Josef Bacik2f356122011-06-10 15:31:13 -0400819 goto out;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400820 }
821 leaf = path->nodes[0];
822 if (ret > 0) {
823 struct btrfs_key found_key;
824 BUG_ON(!path->slots[0]);
825 path->slots[0]--;
826 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
827 if (found_key.objectid != BTRFS_FREE_SPACE_OBJECTID ||
Li Zefan0414efa2011-04-20 10:20:14 +0800828 found_key.offset != offset) {
Josef Bacik0cb59c92010-07-02 12:14:14 -0400829 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, bytes - 1,
830 EXTENT_DIRTY | EXTENT_DELALLOC |
831 EXTENT_DO_ACCOUNTING, 0, 0, NULL,
832 GFP_NOFS);
David Sterbab3b4aa72011-04-21 01:20:15 +0200833 btrfs_release_path(path);
Josef Bacik2f356122011-06-10 15:31:13 -0400834 goto out;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400835 }
836 }
837 header = btrfs_item_ptr(leaf, path->slots[0],
838 struct btrfs_free_space_header);
839 btrfs_set_free_space_entries(leaf, header, entries);
840 btrfs_set_free_space_bitmaps(leaf, header, bitmaps);
841 btrfs_set_free_space_generation(leaf, header, trans->transid);
842 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +0200843 btrfs_release_path(path);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400844
Josef Bacikc09544e2011-08-30 10:19:10 -0400845 err = 0;
Josef Bacik2f356122011-06-10 15:31:13 -0400846out:
Chris Mason211f96c2011-06-03 01:26:53 -0400847 kfree(pages);
Josef Bacikc09544e2011-08-30 10:19:10 -0400848 if (err) {
Josef Bacik0cb59c92010-07-02 12:14:14 -0400849 invalidate_inode_pages2_range(inode->i_mapping, 0, index);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400850 BTRFS_I(inode)->generation = 0;
851 }
Josef Bacik0cb59c92010-07-02 12:14:14 -0400852 btrfs_update_inode(trans, root, inode);
Josef Bacikc09544e2011-08-30 10:19:10 -0400853 return err;
Li Zefan0414efa2011-04-20 10:20:14 +0800854}
855
856int btrfs_write_out_cache(struct btrfs_root *root,
857 struct btrfs_trans_handle *trans,
858 struct btrfs_block_group_cache *block_group,
859 struct btrfs_path *path)
860{
861 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
862 struct inode *inode;
863 int ret = 0;
864
865 root = root->fs_info->tree_root;
866
867 spin_lock(&block_group->lock);
868 if (block_group->disk_cache_state < BTRFS_DC_SETUP) {
869 spin_unlock(&block_group->lock);
870 return 0;
871 }
872 spin_unlock(&block_group->lock);
873
874 inode = lookup_free_space_inode(root, block_group, path);
875 if (IS_ERR(inode))
876 return 0;
877
878 ret = __btrfs_write_out_cache(root, inode, ctl, block_group, trans,
879 path, block_group->key.objectid);
Josef Bacikc09544e2011-08-30 10:19:10 -0400880 if (ret) {
881 btrfs_delalloc_release_metadata(inode, inode->i_size);
Li Zefan0414efa2011-04-20 10:20:14 +0800882 spin_lock(&block_group->lock);
883 block_group->disk_cache_state = BTRFS_DC_ERROR;
884 spin_unlock(&block_group->lock);
Li Zefan82d59022011-04-20 10:33:24 +0800885 ret = 0;
Josef Bacikc09544e2011-08-30 10:19:10 -0400886#ifdef DEBUG
Li Zefan0414efa2011-04-20 10:20:14 +0800887 printk(KERN_ERR "btrfs: failed to write free space cace "
888 "for block group %llu\n", block_group->key.objectid);
Josef Bacikc09544e2011-08-30 10:19:10 -0400889#endif
Li Zefan0414efa2011-04-20 10:20:14 +0800890 }
891
Josef Bacik0cb59c92010-07-02 12:14:14 -0400892 iput(inode);
893 return ret;
894}
895
Li Zefan34d52cb6c2011-03-29 13:46:06 +0800896static inline unsigned long offset_to_bit(u64 bitmap_start, u32 unit,
Josef Bacik96303082009-07-13 21:29:25 -0400897 u64 offset)
898{
899 BUG_ON(offset < bitmap_start);
900 offset -= bitmap_start;
Li Zefan34d52cb6c2011-03-29 13:46:06 +0800901 return (unsigned long)(div_u64(offset, unit));
Josef Bacik96303082009-07-13 21:29:25 -0400902}
903
Li Zefan34d52cb6c2011-03-29 13:46:06 +0800904static inline unsigned long bytes_to_bits(u64 bytes, u32 unit)
Josef Bacik96303082009-07-13 21:29:25 -0400905{
Li Zefan34d52cb6c2011-03-29 13:46:06 +0800906 return (unsigned long)(div_u64(bytes, unit));
Josef Bacik96303082009-07-13 21:29:25 -0400907}
908
Li Zefan34d52cb6c2011-03-29 13:46:06 +0800909static inline u64 offset_to_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -0400910 u64 offset)
911{
912 u64 bitmap_start;
913 u64 bytes_per_bitmap;
914
Li Zefan34d52cb6c2011-03-29 13:46:06 +0800915 bytes_per_bitmap = BITS_PER_BITMAP * ctl->unit;
916 bitmap_start = offset - ctl->start;
Josef Bacik96303082009-07-13 21:29:25 -0400917 bitmap_start = div64_u64(bitmap_start, bytes_per_bitmap);
918 bitmap_start *= bytes_per_bitmap;
Li Zefan34d52cb6c2011-03-29 13:46:06 +0800919 bitmap_start += ctl->start;
Josef Bacik96303082009-07-13 21:29:25 -0400920
921 return bitmap_start;
922}
Josef Bacik0f9dd462008-09-23 13:14:11 -0400923
924static int tree_insert_offset(struct rb_root *root, u64 offset,
Josef Bacik96303082009-07-13 21:29:25 -0400925 struct rb_node *node, int bitmap)
Josef Bacik0f9dd462008-09-23 13:14:11 -0400926{
927 struct rb_node **p = &root->rb_node;
928 struct rb_node *parent = NULL;
929 struct btrfs_free_space *info;
930
931 while (*p) {
932 parent = *p;
933 info = rb_entry(parent, struct btrfs_free_space, offset_index);
934
Josef Bacik96303082009-07-13 21:29:25 -0400935 if (offset < info->offset) {
Josef Bacik0f9dd462008-09-23 13:14:11 -0400936 p = &(*p)->rb_left;
Josef Bacik96303082009-07-13 21:29:25 -0400937 } else if (offset > info->offset) {
Josef Bacik0f9dd462008-09-23 13:14:11 -0400938 p = &(*p)->rb_right;
Josef Bacik96303082009-07-13 21:29:25 -0400939 } else {
940 /*
941 * we could have a bitmap entry and an extent entry
942 * share the same offset. If this is the case, we want
943 * the extent entry to always be found first if we do a
944 * linear search through the tree, since we want to have
945 * the quickest allocation time, and allocating from an
946 * extent is faster than allocating from a bitmap. So
947 * if we're inserting a bitmap and we find an entry at
948 * this offset, we want to go right, or after this entry
949 * logically. If we are inserting an extent and we've
950 * found a bitmap, we want to go left, or before
951 * logically.
952 */
953 if (bitmap) {
Josef Bacik207dde82011-05-13 14:49:23 -0400954 if (info->bitmap) {
955 WARN_ON_ONCE(1);
956 return -EEXIST;
957 }
Josef Bacik96303082009-07-13 21:29:25 -0400958 p = &(*p)->rb_right;
959 } else {
Josef Bacik207dde82011-05-13 14:49:23 -0400960 if (!info->bitmap) {
961 WARN_ON_ONCE(1);
962 return -EEXIST;
963 }
Josef Bacik96303082009-07-13 21:29:25 -0400964 p = &(*p)->rb_left;
965 }
966 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400967 }
968
969 rb_link_node(node, parent, p);
970 rb_insert_color(node, root);
971
972 return 0;
973}
974
975/*
Josef Bacik70cb0742009-04-03 10:14:19 -0400976 * searches the tree for the given offset.
977 *
Josef Bacik96303082009-07-13 21:29:25 -0400978 * fuzzy - If this is set, then we are trying to make an allocation, and we just
979 * want a section that has at least bytes size and comes at or after the given
980 * offset.
Josef Bacik0f9dd462008-09-23 13:14:11 -0400981 */
Josef Bacik96303082009-07-13 21:29:25 -0400982static struct btrfs_free_space *
Li Zefan34d52cb6c2011-03-29 13:46:06 +0800983tree_search_offset(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -0400984 u64 offset, int bitmap_only, int fuzzy)
Josef Bacik0f9dd462008-09-23 13:14:11 -0400985{
Li Zefan34d52cb6c2011-03-29 13:46:06 +0800986 struct rb_node *n = ctl->free_space_offset.rb_node;
Josef Bacik96303082009-07-13 21:29:25 -0400987 struct btrfs_free_space *entry, *prev = NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400988
Josef Bacik96303082009-07-13 21:29:25 -0400989 /* find entry that is closest to the 'offset' */
990 while (1) {
991 if (!n) {
992 entry = NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400993 break;
994 }
Josef Bacik96303082009-07-13 21:29:25 -0400995
996 entry = rb_entry(n, struct btrfs_free_space, offset_index);
997 prev = entry;
998
999 if (offset < entry->offset)
1000 n = n->rb_left;
1001 else if (offset > entry->offset)
1002 n = n->rb_right;
1003 else
1004 break;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001005 }
1006
Josef Bacik96303082009-07-13 21:29:25 -04001007 if (bitmap_only) {
1008 if (!entry)
1009 return NULL;
1010 if (entry->bitmap)
1011 return entry;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001012
Josef Bacik96303082009-07-13 21:29:25 -04001013 /*
1014 * bitmap entry and extent entry may share same offset,
1015 * in that case, bitmap entry comes after extent entry.
1016 */
1017 n = rb_next(n);
1018 if (!n)
1019 return NULL;
1020 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1021 if (entry->offset != offset)
1022 return NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001023
Josef Bacik96303082009-07-13 21:29:25 -04001024 WARN_ON(!entry->bitmap);
1025 return entry;
1026 } else if (entry) {
1027 if (entry->bitmap) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001028 /*
Josef Bacik96303082009-07-13 21:29:25 -04001029 * if previous extent entry covers the offset,
1030 * we should return it instead of the bitmap entry
Josef Bacik0f9dd462008-09-23 13:14:11 -04001031 */
Josef Bacik96303082009-07-13 21:29:25 -04001032 n = &entry->offset_index;
1033 while (1) {
1034 n = rb_prev(n);
1035 if (!n)
1036 break;
1037 prev = rb_entry(n, struct btrfs_free_space,
1038 offset_index);
1039 if (!prev->bitmap) {
1040 if (prev->offset + prev->bytes > offset)
1041 entry = prev;
1042 break;
1043 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04001044 }
Josef Bacik96303082009-07-13 21:29:25 -04001045 }
1046 return entry;
1047 }
1048
1049 if (!prev)
1050 return NULL;
1051
1052 /* find last entry before the 'offset' */
1053 entry = prev;
1054 if (entry->offset > offset) {
1055 n = rb_prev(&entry->offset_index);
1056 if (n) {
1057 entry = rb_entry(n, struct btrfs_free_space,
1058 offset_index);
1059 BUG_ON(entry->offset > offset);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001060 } else {
Josef Bacik96303082009-07-13 21:29:25 -04001061 if (fuzzy)
1062 return entry;
1063 else
1064 return NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001065 }
1066 }
1067
Josef Bacik96303082009-07-13 21:29:25 -04001068 if (entry->bitmap) {
1069 n = &entry->offset_index;
1070 while (1) {
1071 n = rb_prev(n);
1072 if (!n)
1073 break;
1074 prev = rb_entry(n, struct btrfs_free_space,
1075 offset_index);
1076 if (!prev->bitmap) {
1077 if (prev->offset + prev->bytes > offset)
1078 return prev;
1079 break;
1080 }
1081 }
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001082 if (entry->offset + BITS_PER_BITMAP * ctl->unit > offset)
Josef Bacik96303082009-07-13 21:29:25 -04001083 return entry;
1084 } else if (entry->offset + entry->bytes > offset)
1085 return entry;
1086
1087 if (!fuzzy)
1088 return NULL;
1089
1090 while (1) {
1091 if (entry->bitmap) {
1092 if (entry->offset + BITS_PER_BITMAP *
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001093 ctl->unit > offset)
Josef Bacik96303082009-07-13 21:29:25 -04001094 break;
1095 } else {
1096 if (entry->offset + entry->bytes > offset)
1097 break;
1098 }
1099
1100 n = rb_next(&entry->offset_index);
1101 if (!n)
1102 return NULL;
1103 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1104 }
1105 return entry;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001106}
1107
Li Zefanf333adb2010-11-09 14:57:39 +08001108static inline void
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001109__unlink_free_space(struct btrfs_free_space_ctl *ctl,
Li Zefanf333adb2010-11-09 14:57:39 +08001110 struct btrfs_free_space *info)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001111{
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001112 rb_erase(&info->offset_index, &ctl->free_space_offset);
1113 ctl->free_extents--;
Li Zefanf333adb2010-11-09 14:57:39 +08001114}
1115
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001116static void unlink_free_space(struct btrfs_free_space_ctl *ctl,
Li Zefanf333adb2010-11-09 14:57:39 +08001117 struct btrfs_free_space *info)
1118{
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001119 __unlink_free_space(ctl, info);
1120 ctl->free_space -= info->bytes;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001121}
1122
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001123static int link_free_space(struct btrfs_free_space_ctl *ctl,
Josef Bacik0f9dd462008-09-23 13:14:11 -04001124 struct btrfs_free_space *info)
1125{
1126 int ret = 0;
1127
Josef Bacik96303082009-07-13 21:29:25 -04001128 BUG_ON(!info->bitmap && !info->bytes);
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001129 ret = tree_insert_offset(&ctl->free_space_offset, info->offset,
Josef Bacik96303082009-07-13 21:29:25 -04001130 &info->offset_index, (info->bitmap != NULL));
Josef Bacik0f9dd462008-09-23 13:14:11 -04001131 if (ret)
1132 return ret;
1133
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001134 ctl->free_space += info->bytes;
1135 ctl->free_extents++;
Josef Bacik96303082009-07-13 21:29:25 -04001136 return ret;
1137}
1138
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001139static void recalculate_thresholds(struct btrfs_free_space_ctl *ctl)
Josef Bacik96303082009-07-13 21:29:25 -04001140{
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001141 struct btrfs_block_group_cache *block_group = ctl->private;
Josef Bacik25891f72009-09-11 16:11:20 -04001142 u64 max_bytes;
1143 u64 bitmap_bytes;
1144 u64 extent_bytes;
Li Zefan8eb2d822010-11-09 14:48:01 +08001145 u64 size = block_group->key.offset;
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001146 u64 bytes_per_bg = BITS_PER_BITMAP * block_group->sectorsize;
1147 int max_bitmaps = div64_u64(size + bytes_per_bg - 1, bytes_per_bg);
1148
1149 BUG_ON(ctl->total_bitmaps > max_bitmaps);
Josef Bacik96303082009-07-13 21:29:25 -04001150
1151 /*
1152 * The goal is to keep the total amount of memory used per 1gb of space
1153 * at or below 32k, so we need to adjust how much memory we allow to be
1154 * used by extent based free space tracking
1155 */
Li Zefan8eb2d822010-11-09 14:48:01 +08001156 if (size < 1024 * 1024 * 1024)
1157 max_bytes = MAX_CACHE_BYTES_PER_GIG;
1158 else
1159 max_bytes = MAX_CACHE_BYTES_PER_GIG *
1160 div64_u64(size, 1024 * 1024 * 1024);
Josef Bacik96303082009-07-13 21:29:25 -04001161
Josef Bacik25891f72009-09-11 16:11:20 -04001162 /*
1163 * we want to account for 1 more bitmap than what we have so we can make
1164 * sure we don't go over our overall goal of MAX_CACHE_BYTES_PER_GIG as
1165 * we add more bitmaps.
1166 */
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001167 bitmap_bytes = (ctl->total_bitmaps + 1) * PAGE_CACHE_SIZE;
Josef Bacik96303082009-07-13 21:29:25 -04001168
Josef Bacik25891f72009-09-11 16:11:20 -04001169 if (bitmap_bytes >= max_bytes) {
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001170 ctl->extents_thresh = 0;
Josef Bacik25891f72009-09-11 16:11:20 -04001171 return;
Josef Bacik96303082009-07-13 21:29:25 -04001172 }
Josef Bacik25891f72009-09-11 16:11:20 -04001173
1174 /*
1175 * we want the extent entry threshold to always be at most 1/2 the maxw
1176 * bytes we can have, or whatever is less than that.
1177 */
1178 extent_bytes = max_bytes - bitmap_bytes;
1179 extent_bytes = min_t(u64, extent_bytes, div64_u64(max_bytes, 2));
1180
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001181 ctl->extents_thresh =
Josef Bacik25891f72009-09-11 16:11:20 -04001182 div64_u64(extent_bytes, (sizeof(struct btrfs_free_space)));
Josef Bacik96303082009-07-13 21:29:25 -04001183}
1184
Miao Xiebb3ac5a2011-08-05 09:32:35 +00001185static inline void __bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
1186 struct btrfs_free_space *info,
1187 u64 offset, u64 bytes)
Josef Bacik96303082009-07-13 21:29:25 -04001188{
Li Zefanf38b6e72011-03-14 13:40:51 +08001189 unsigned long start, count;
Josef Bacik96303082009-07-13 21:29:25 -04001190
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001191 start = offset_to_bit(info->offset, ctl->unit, offset);
1192 count = bytes_to_bits(bytes, ctl->unit);
Li Zefanf38b6e72011-03-14 13:40:51 +08001193 BUG_ON(start + count > BITS_PER_BITMAP);
Josef Bacik96303082009-07-13 21:29:25 -04001194
Li Zefanf38b6e72011-03-14 13:40:51 +08001195 bitmap_clear(info->bitmap, start, count);
Josef Bacik96303082009-07-13 21:29:25 -04001196
1197 info->bytes -= bytes;
Miao Xiebb3ac5a2011-08-05 09:32:35 +00001198}
1199
1200static void bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
1201 struct btrfs_free_space *info, u64 offset,
1202 u64 bytes)
1203{
1204 __bitmap_clear_bits(ctl, info, offset, bytes);
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001205 ctl->free_space -= bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001206}
1207
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001208static void bitmap_set_bits(struct btrfs_free_space_ctl *ctl,
Josef Bacik817d52f2009-07-13 21:29:25 -04001209 struct btrfs_free_space *info, u64 offset,
1210 u64 bytes)
Josef Bacik96303082009-07-13 21:29:25 -04001211{
Li Zefanf38b6e72011-03-14 13:40:51 +08001212 unsigned long start, count;
Josef Bacik96303082009-07-13 21:29:25 -04001213
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001214 start = offset_to_bit(info->offset, ctl->unit, offset);
1215 count = bytes_to_bits(bytes, ctl->unit);
Li Zefanf38b6e72011-03-14 13:40:51 +08001216 BUG_ON(start + count > BITS_PER_BITMAP);
Josef Bacik96303082009-07-13 21:29:25 -04001217
Li Zefanf38b6e72011-03-14 13:40:51 +08001218 bitmap_set(info->bitmap, start, count);
Josef Bacik96303082009-07-13 21:29:25 -04001219
1220 info->bytes += bytes;
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001221 ctl->free_space += bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001222}
1223
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001224static int search_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001225 struct btrfs_free_space *bitmap_info, u64 *offset,
1226 u64 *bytes)
1227{
1228 unsigned long found_bits = 0;
1229 unsigned long bits, i;
1230 unsigned long next_zero;
1231
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001232 i = offset_to_bit(bitmap_info->offset, ctl->unit,
Josef Bacik96303082009-07-13 21:29:25 -04001233 max_t(u64, *offset, bitmap_info->offset));
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001234 bits = bytes_to_bits(*bytes, ctl->unit);
Josef Bacik96303082009-07-13 21:29:25 -04001235
1236 for (i = find_next_bit(bitmap_info->bitmap, BITS_PER_BITMAP, i);
1237 i < BITS_PER_BITMAP;
1238 i = find_next_bit(bitmap_info->bitmap, BITS_PER_BITMAP, i + 1)) {
1239 next_zero = find_next_zero_bit(bitmap_info->bitmap,
1240 BITS_PER_BITMAP, i);
1241 if ((next_zero - i) >= bits) {
1242 found_bits = next_zero - i;
1243 break;
1244 }
1245 i = next_zero;
1246 }
1247
1248 if (found_bits) {
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001249 *offset = (u64)(i * ctl->unit) + bitmap_info->offset;
1250 *bytes = (u64)(found_bits) * ctl->unit;
Josef Bacik96303082009-07-13 21:29:25 -04001251 return 0;
1252 }
1253
1254 return -1;
1255}
1256
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001257static struct btrfs_free_space *
1258find_free_space(struct btrfs_free_space_ctl *ctl, u64 *offset, u64 *bytes)
Josef Bacik96303082009-07-13 21:29:25 -04001259{
1260 struct btrfs_free_space *entry;
1261 struct rb_node *node;
1262 int ret;
1263
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001264 if (!ctl->free_space_offset.rb_node)
Josef Bacik96303082009-07-13 21:29:25 -04001265 return NULL;
1266
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001267 entry = tree_search_offset(ctl, offset_to_bitmap(ctl, *offset), 0, 1);
Josef Bacik96303082009-07-13 21:29:25 -04001268 if (!entry)
1269 return NULL;
1270
1271 for (node = &entry->offset_index; node; node = rb_next(node)) {
1272 entry = rb_entry(node, struct btrfs_free_space, offset_index);
1273 if (entry->bytes < *bytes)
1274 continue;
1275
1276 if (entry->bitmap) {
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001277 ret = search_bitmap(ctl, entry, offset, bytes);
Josef Bacik96303082009-07-13 21:29:25 -04001278 if (!ret)
1279 return entry;
1280 continue;
1281 }
1282
1283 *offset = entry->offset;
1284 *bytes = entry->bytes;
1285 return entry;
1286 }
1287
1288 return NULL;
1289}
1290
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001291static void add_new_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001292 struct btrfs_free_space *info, u64 offset)
1293{
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001294 info->offset = offset_to_bitmap(ctl, offset);
Josef Bacikf019f422009-09-11 16:11:20 -04001295 info->bytes = 0;
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001296 link_free_space(ctl, info);
1297 ctl->total_bitmaps++;
Josef Bacik96303082009-07-13 21:29:25 -04001298
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001299 ctl->op->recalc_thresholds(ctl);
Josef Bacik96303082009-07-13 21:29:25 -04001300}
1301
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001302static void free_bitmap(struct btrfs_free_space_ctl *ctl,
Li Zefanedf6e2d2010-11-09 14:50:07 +08001303 struct btrfs_free_space *bitmap_info)
1304{
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001305 unlink_free_space(ctl, bitmap_info);
Li Zefanedf6e2d2010-11-09 14:50:07 +08001306 kfree(bitmap_info->bitmap);
Josef Bacikdc89e982011-01-28 17:05:48 -05001307 kmem_cache_free(btrfs_free_space_cachep, bitmap_info);
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001308 ctl->total_bitmaps--;
1309 ctl->op->recalc_thresholds(ctl);
Li Zefanedf6e2d2010-11-09 14:50:07 +08001310}
1311
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001312static noinline int remove_from_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001313 struct btrfs_free_space *bitmap_info,
1314 u64 *offset, u64 *bytes)
1315{
1316 u64 end;
Josef Bacik6606bb92009-07-31 11:03:58 -04001317 u64 search_start, search_bytes;
1318 int ret;
Josef Bacik96303082009-07-13 21:29:25 -04001319
1320again:
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001321 end = bitmap_info->offset + (u64)(BITS_PER_BITMAP * ctl->unit) - 1;
Josef Bacik96303082009-07-13 21:29:25 -04001322
Josef Bacik6606bb92009-07-31 11:03:58 -04001323 /*
1324 * XXX - this can go away after a few releases.
1325 *
1326 * since the only user of btrfs_remove_free_space is the tree logging
1327 * stuff, and the only way to test that is under crash conditions, we
1328 * want to have this debug stuff here just in case somethings not
1329 * working. Search the bitmap for the space we are trying to use to
1330 * make sure its actually there. If its not there then we need to stop
1331 * because something has gone wrong.
1332 */
1333 search_start = *offset;
1334 search_bytes = *bytes;
Josef Bacik13dbc082011-02-03 02:39:52 +00001335 search_bytes = min(search_bytes, end - search_start + 1);
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001336 ret = search_bitmap(ctl, bitmap_info, &search_start, &search_bytes);
Josef Bacik6606bb92009-07-31 11:03:58 -04001337 BUG_ON(ret < 0 || search_start != *offset);
1338
Josef Bacik96303082009-07-13 21:29:25 -04001339 if (*offset > bitmap_info->offset && *offset + *bytes > end) {
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001340 bitmap_clear_bits(ctl, bitmap_info, *offset, end - *offset + 1);
Josef Bacik96303082009-07-13 21:29:25 -04001341 *bytes -= end - *offset + 1;
1342 *offset = end + 1;
1343 } else if (*offset >= bitmap_info->offset && *offset + *bytes <= end) {
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001344 bitmap_clear_bits(ctl, bitmap_info, *offset, *bytes);
Josef Bacik96303082009-07-13 21:29:25 -04001345 *bytes = 0;
1346 }
1347
1348 if (*bytes) {
Josef Bacik6606bb92009-07-31 11:03:58 -04001349 struct rb_node *next = rb_next(&bitmap_info->offset_index);
Li Zefanedf6e2d2010-11-09 14:50:07 +08001350 if (!bitmap_info->bytes)
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001351 free_bitmap(ctl, bitmap_info);
Josef Bacik96303082009-07-13 21:29:25 -04001352
Josef Bacik6606bb92009-07-31 11:03:58 -04001353 /*
1354 * no entry after this bitmap, but we still have bytes to
1355 * remove, so something has gone wrong.
1356 */
1357 if (!next)
Josef Bacik96303082009-07-13 21:29:25 -04001358 return -EINVAL;
1359
Josef Bacik6606bb92009-07-31 11:03:58 -04001360 bitmap_info = rb_entry(next, struct btrfs_free_space,
1361 offset_index);
1362
1363 /*
1364 * if the next entry isn't a bitmap we need to return to let the
1365 * extent stuff do its work.
1366 */
Josef Bacik96303082009-07-13 21:29:25 -04001367 if (!bitmap_info->bitmap)
1368 return -EAGAIN;
1369
Josef Bacik6606bb92009-07-31 11:03:58 -04001370 /*
1371 * Ok the next item is a bitmap, but it may not actually hold
1372 * the information for the rest of this free space stuff, so
1373 * look for it, and if we don't find it return so we can try
1374 * everything over again.
1375 */
1376 search_start = *offset;
1377 search_bytes = *bytes;
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001378 ret = search_bitmap(ctl, bitmap_info, &search_start,
Josef Bacik6606bb92009-07-31 11:03:58 -04001379 &search_bytes);
1380 if (ret < 0 || search_start != *offset)
1381 return -EAGAIN;
1382
Josef Bacik96303082009-07-13 21:29:25 -04001383 goto again;
Li Zefanedf6e2d2010-11-09 14:50:07 +08001384 } else if (!bitmap_info->bytes)
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001385 free_bitmap(ctl, bitmap_info);
Josef Bacik96303082009-07-13 21:29:25 -04001386
1387 return 0;
1388}
1389
Josef Bacik2cdc3422011-05-27 14:07:49 -04001390static u64 add_bytes_to_bitmap(struct btrfs_free_space_ctl *ctl,
1391 struct btrfs_free_space *info, u64 offset,
1392 u64 bytes)
1393{
1394 u64 bytes_to_set = 0;
1395 u64 end;
1396
1397 end = info->offset + (u64)(BITS_PER_BITMAP * ctl->unit);
1398
1399 bytes_to_set = min(end - offset, bytes);
1400
1401 bitmap_set_bits(ctl, info, offset, bytes_to_set);
1402
1403 return bytes_to_set;
1404
1405}
1406
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001407static bool use_bitmap(struct btrfs_free_space_ctl *ctl,
1408 struct btrfs_free_space *info)
Josef Bacik96303082009-07-13 21:29:25 -04001409{
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001410 struct btrfs_block_group_cache *block_group = ctl->private;
Josef Bacik96303082009-07-13 21:29:25 -04001411
1412 /*
1413 * If we are below the extents threshold then we can add this as an
1414 * extent, and don't have to deal with the bitmap
1415 */
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001416 if (ctl->free_extents < ctl->extents_thresh) {
Josef Bacik32cb0842011-03-18 16:16:21 -04001417 /*
1418 * If this block group has some small extents we don't want to
1419 * use up all of our free slots in the cache with them, we want
1420 * to reserve them to larger extents, however if we have plent
1421 * of cache left then go ahead an dadd them, no sense in adding
1422 * the overhead of a bitmap if we don't have to.
1423 */
1424 if (info->bytes <= block_group->sectorsize * 4) {
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001425 if (ctl->free_extents * 2 <= ctl->extents_thresh)
1426 return false;
Josef Bacik32cb0842011-03-18 16:16:21 -04001427 } else {
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001428 return false;
Josef Bacik32cb0842011-03-18 16:16:21 -04001429 }
1430 }
Josef Bacik96303082009-07-13 21:29:25 -04001431
1432 /*
1433 * some block groups are so tiny they can't be enveloped by a bitmap, so
1434 * don't even bother to create a bitmap for this
1435 */
1436 if (BITS_PER_BITMAP * block_group->sectorsize >
1437 block_group->key.offset)
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001438 return false;
1439
1440 return true;
1441}
1442
Josef Bacik2cdc3422011-05-27 14:07:49 -04001443static struct btrfs_free_space_op free_space_op = {
1444 .recalc_thresholds = recalculate_thresholds,
1445 .use_bitmap = use_bitmap,
1446};
1447
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001448static int insert_into_bitmap(struct btrfs_free_space_ctl *ctl,
1449 struct btrfs_free_space *info)
1450{
1451 struct btrfs_free_space *bitmap_info;
Josef Bacik2cdc3422011-05-27 14:07:49 -04001452 struct btrfs_block_group_cache *block_group = NULL;
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001453 int added = 0;
Josef Bacik2cdc3422011-05-27 14:07:49 -04001454 u64 bytes, offset, bytes_added;
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001455 int ret;
Josef Bacik96303082009-07-13 21:29:25 -04001456
1457 bytes = info->bytes;
1458 offset = info->offset;
1459
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001460 if (!ctl->op->use_bitmap(ctl, info))
1461 return 0;
1462
Josef Bacik2cdc3422011-05-27 14:07:49 -04001463 if (ctl->op == &free_space_op)
1464 block_group = ctl->private;
Chris Mason38e87882011-06-10 16:36:57 -04001465again:
Josef Bacik2cdc3422011-05-27 14:07:49 -04001466 /*
1467 * Since we link bitmaps right into the cluster we need to see if we
1468 * have a cluster here, and if so and it has our bitmap we need to add
1469 * the free space to that bitmap.
1470 */
1471 if (block_group && !list_empty(&block_group->cluster_list)) {
1472 struct btrfs_free_cluster *cluster;
1473 struct rb_node *node;
1474 struct btrfs_free_space *entry;
1475
1476 cluster = list_entry(block_group->cluster_list.next,
1477 struct btrfs_free_cluster,
1478 block_group_list);
1479 spin_lock(&cluster->lock);
1480 node = rb_first(&cluster->root);
1481 if (!node) {
1482 spin_unlock(&cluster->lock);
Chris Mason38e87882011-06-10 16:36:57 -04001483 goto no_cluster_bitmap;
Josef Bacik2cdc3422011-05-27 14:07:49 -04001484 }
1485
1486 entry = rb_entry(node, struct btrfs_free_space, offset_index);
1487 if (!entry->bitmap) {
1488 spin_unlock(&cluster->lock);
Chris Mason38e87882011-06-10 16:36:57 -04001489 goto no_cluster_bitmap;
Josef Bacik2cdc3422011-05-27 14:07:49 -04001490 }
1491
1492 if (entry->offset == offset_to_bitmap(ctl, offset)) {
1493 bytes_added = add_bytes_to_bitmap(ctl, entry,
1494 offset, bytes);
1495 bytes -= bytes_added;
1496 offset += bytes_added;
1497 }
1498 spin_unlock(&cluster->lock);
1499 if (!bytes) {
1500 ret = 1;
1501 goto out;
1502 }
1503 }
Chris Mason38e87882011-06-10 16:36:57 -04001504
1505no_cluster_bitmap:
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001506 bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
Josef Bacik96303082009-07-13 21:29:25 -04001507 1, 0);
1508 if (!bitmap_info) {
1509 BUG_ON(added);
1510 goto new_bitmap;
1511 }
1512
Josef Bacik2cdc3422011-05-27 14:07:49 -04001513 bytes_added = add_bytes_to_bitmap(ctl, bitmap_info, offset, bytes);
1514 bytes -= bytes_added;
1515 offset += bytes_added;
1516 added = 0;
Josef Bacik96303082009-07-13 21:29:25 -04001517
1518 if (!bytes) {
1519 ret = 1;
1520 goto out;
1521 } else
1522 goto again;
1523
1524new_bitmap:
1525 if (info && info->bitmap) {
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001526 add_new_bitmap(ctl, info, offset);
Josef Bacik96303082009-07-13 21:29:25 -04001527 added = 1;
1528 info = NULL;
1529 goto again;
1530 } else {
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001531 spin_unlock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04001532
1533 /* no pre-allocated info, allocate a new one */
1534 if (!info) {
Josef Bacikdc89e982011-01-28 17:05:48 -05001535 info = kmem_cache_zalloc(btrfs_free_space_cachep,
1536 GFP_NOFS);
Josef Bacik96303082009-07-13 21:29:25 -04001537 if (!info) {
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001538 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04001539 ret = -ENOMEM;
1540 goto out;
1541 }
1542 }
1543
1544 /* allocate the bitmap */
1545 info->bitmap = kzalloc(PAGE_CACHE_SIZE, GFP_NOFS);
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001546 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04001547 if (!info->bitmap) {
1548 ret = -ENOMEM;
1549 goto out;
1550 }
1551 goto again;
1552 }
1553
1554out:
1555 if (info) {
1556 if (info->bitmap)
1557 kfree(info->bitmap);
Josef Bacikdc89e982011-01-28 17:05:48 -05001558 kmem_cache_free(btrfs_free_space_cachep, info);
Josef Bacik96303082009-07-13 21:29:25 -04001559 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04001560
1561 return ret;
1562}
1563
Chris Mason945d8962011-05-22 12:33:42 -04001564static bool try_merge_free_space(struct btrfs_free_space_ctl *ctl,
Li Zefanf333adb2010-11-09 14:57:39 +08001565 struct btrfs_free_space *info, bool update_stat)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001566{
Li Zefan120d66e2010-11-09 14:56:50 +08001567 struct btrfs_free_space *left_info;
1568 struct btrfs_free_space *right_info;
1569 bool merged = false;
1570 u64 offset = info->offset;
1571 u64 bytes = info->bytes;
Josef Bacik6226cb02009-04-03 10:14:18 -04001572
Josef Bacik0f9dd462008-09-23 13:14:11 -04001573 /*
1574 * first we want to see if there is free space adjacent to the range we
1575 * are adding, if there is remove that struct and add a new one to
1576 * cover the entire range
1577 */
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001578 right_info = tree_search_offset(ctl, offset + bytes, 0, 0);
Josef Bacik96303082009-07-13 21:29:25 -04001579 if (right_info && rb_prev(&right_info->offset_index))
1580 left_info = rb_entry(rb_prev(&right_info->offset_index),
1581 struct btrfs_free_space, offset_index);
1582 else
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001583 left_info = tree_search_offset(ctl, offset - 1, 0, 0);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001584
Josef Bacik96303082009-07-13 21:29:25 -04001585 if (right_info && !right_info->bitmap) {
Li Zefanf333adb2010-11-09 14:57:39 +08001586 if (update_stat)
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001587 unlink_free_space(ctl, right_info);
Li Zefanf333adb2010-11-09 14:57:39 +08001588 else
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001589 __unlink_free_space(ctl, right_info);
Josef Bacik6226cb02009-04-03 10:14:18 -04001590 info->bytes += right_info->bytes;
Josef Bacikdc89e982011-01-28 17:05:48 -05001591 kmem_cache_free(btrfs_free_space_cachep, right_info);
Li Zefan120d66e2010-11-09 14:56:50 +08001592 merged = true;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001593 }
1594
Josef Bacik96303082009-07-13 21:29:25 -04001595 if (left_info && !left_info->bitmap &&
1596 left_info->offset + left_info->bytes == offset) {
Li Zefanf333adb2010-11-09 14:57:39 +08001597 if (update_stat)
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001598 unlink_free_space(ctl, left_info);
Li Zefanf333adb2010-11-09 14:57:39 +08001599 else
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001600 __unlink_free_space(ctl, left_info);
Josef Bacik6226cb02009-04-03 10:14:18 -04001601 info->offset = left_info->offset;
1602 info->bytes += left_info->bytes;
Josef Bacikdc89e982011-01-28 17:05:48 -05001603 kmem_cache_free(btrfs_free_space_cachep, left_info);
Li Zefan120d66e2010-11-09 14:56:50 +08001604 merged = true;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001605 }
1606
Li Zefan120d66e2010-11-09 14:56:50 +08001607 return merged;
1608}
1609
Li Zefan581bb052011-04-20 10:06:11 +08001610int __btrfs_add_free_space(struct btrfs_free_space_ctl *ctl,
1611 u64 offset, u64 bytes)
Li Zefan120d66e2010-11-09 14:56:50 +08001612{
1613 struct btrfs_free_space *info;
1614 int ret = 0;
1615
Josef Bacikdc89e982011-01-28 17:05:48 -05001616 info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS);
Li Zefan120d66e2010-11-09 14:56:50 +08001617 if (!info)
1618 return -ENOMEM;
1619
1620 info->offset = offset;
1621 info->bytes = bytes;
1622
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001623 spin_lock(&ctl->tree_lock);
Li Zefan120d66e2010-11-09 14:56:50 +08001624
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001625 if (try_merge_free_space(ctl, info, true))
Li Zefan120d66e2010-11-09 14:56:50 +08001626 goto link;
1627
1628 /*
1629 * There was no extent directly to the left or right of this new
1630 * extent then we know we're going to have to allocate a new extent, so
1631 * before we do that see if we need to drop this into a bitmap
1632 */
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001633 ret = insert_into_bitmap(ctl, info);
Li Zefan120d66e2010-11-09 14:56:50 +08001634 if (ret < 0) {
1635 goto out;
1636 } else if (ret) {
1637 ret = 0;
1638 goto out;
1639 }
1640link:
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001641 ret = link_free_space(ctl, info);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001642 if (ret)
Josef Bacikdc89e982011-01-28 17:05:48 -05001643 kmem_cache_free(btrfs_free_space_cachep, info);
Josef Bacik96303082009-07-13 21:29:25 -04001644out:
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001645 spin_unlock(&ctl->tree_lock);
Josef Bacik6226cb02009-04-03 10:14:18 -04001646
Josef Bacik0f9dd462008-09-23 13:14:11 -04001647 if (ret) {
Josef Bacik96303082009-07-13 21:29:25 -04001648 printk(KERN_CRIT "btrfs: unable to add free space :%d\n", ret);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04001649 BUG_ON(ret == -EEXIST);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001650 }
1651
Josef Bacik0f9dd462008-09-23 13:14:11 -04001652 return ret;
1653}
1654
Josef Bacik6226cb02009-04-03 10:14:18 -04001655int btrfs_remove_free_space(struct btrfs_block_group_cache *block_group,
1656 u64 offset, u64 bytes)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001657{
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001658 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001659 struct btrfs_free_space *info;
Josef Bacik96303082009-07-13 21:29:25 -04001660 struct btrfs_free_space *next_info = NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001661 int ret = 0;
1662
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001663 spin_lock(&ctl->tree_lock);
Josef Bacik6226cb02009-04-03 10:14:18 -04001664
Josef Bacik96303082009-07-13 21:29:25 -04001665again:
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001666 info = tree_search_offset(ctl, offset, 0, 0);
Josef Bacik96303082009-07-13 21:29:25 -04001667 if (!info) {
Josef Bacik6606bb92009-07-31 11:03:58 -04001668 /*
1669 * oops didn't find an extent that matched the space we wanted
1670 * to remove, look for a bitmap instead
1671 */
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001672 info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
Josef Bacik6606bb92009-07-31 11:03:58 -04001673 1, 0);
1674 if (!info) {
1675 WARN_ON(1);
1676 goto out_lock;
1677 }
Josef Bacik96303082009-07-13 21:29:25 -04001678 }
1679
1680 if (info->bytes < bytes && rb_next(&info->offset_index)) {
1681 u64 end;
1682 next_info = rb_entry(rb_next(&info->offset_index),
1683 struct btrfs_free_space,
1684 offset_index);
1685
1686 if (next_info->bitmap)
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001687 end = next_info->offset +
1688 BITS_PER_BITMAP * ctl->unit - 1;
Josef Bacik96303082009-07-13 21:29:25 -04001689 else
1690 end = next_info->offset + next_info->bytes;
1691
1692 if (next_info->bytes < bytes ||
1693 next_info->offset > offset || offset > end) {
1694 printk(KERN_CRIT "Found free space at %llu, size %llu,"
1695 " trying to use %llu\n",
1696 (unsigned long long)info->offset,
1697 (unsigned long long)info->bytes,
1698 (unsigned long long)bytes);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001699 WARN_ON(1);
1700 ret = -EINVAL;
Josef Bacik96303082009-07-13 21:29:25 -04001701 goto out_lock;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001702 }
Josef Bacik96303082009-07-13 21:29:25 -04001703
1704 info = next_info;
1705 }
1706
1707 if (info->bytes == bytes) {
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001708 unlink_free_space(ctl, info);
Josef Bacik96303082009-07-13 21:29:25 -04001709 if (info->bitmap) {
1710 kfree(info->bitmap);
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001711 ctl->total_bitmaps--;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001712 }
Josef Bacikdc89e982011-01-28 17:05:48 -05001713 kmem_cache_free(btrfs_free_space_cachep, info);
Josef Bacik96303082009-07-13 21:29:25 -04001714 goto out_lock;
1715 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04001716
Josef Bacik96303082009-07-13 21:29:25 -04001717 if (!info->bitmap && info->offset == offset) {
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001718 unlink_free_space(ctl, info);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001719 info->offset += bytes;
1720 info->bytes -= bytes;
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001721 link_free_space(ctl, info);
Josef Bacik96303082009-07-13 21:29:25 -04001722 goto out_lock;
1723 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04001724
Josef Bacik96303082009-07-13 21:29:25 -04001725 if (!info->bitmap && info->offset <= offset &&
1726 info->offset + info->bytes >= offset + bytes) {
Chris Mason9b49c9b2008-09-24 11:23:25 -04001727 u64 old_start = info->offset;
1728 /*
1729 * we're freeing space in the middle of the info,
1730 * this can happen during tree log replay
1731 *
1732 * first unlink the old info and then
1733 * insert it again after the hole we're creating
1734 */
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001735 unlink_free_space(ctl, info);
Chris Mason9b49c9b2008-09-24 11:23:25 -04001736 if (offset + bytes < info->offset + info->bytes) {
1737 u64 old_end = info->offset + info->bytes;
1738
1739 info->offset = offset + bytes;
1740 info->bytes = old_end - info->offset;
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001741 ret = link_free_space(ctl, info);
Josef Bacik96303082009-07-13 21:29:25 -04001742 WARN_ON(ret);
1743 if (ret)
1744 goto out_lock;
Chris Mason9b49c9b2008-09-24 11:23:25 -04001745 } else {
1746 /* the hole we're creating ends at the end
1747 * of the info struct, just free the info
1748 */
Josef Bacikdc89e982011-01-28 17:05:48 -05001749 kmem_cache_free(btrfs_free_space_cachep, info);
Chris Mason9b49c9b2008-09-24 11:23:25 -04001750 }
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001751 spin_unlock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04001752
1753 /* step two, insert a new info struct to cover
1754 * anything before the hole
Chris Mason9b49c9b2008-09-24 11:23:25 -04001755 */
Josef Bacik6226cb02009-04-03 10:14:18 -04001756 ret = btrfs_add_free_space(block_group, old_start,
1757 offset - old_start);
Josef Bacik96303082009-07-13 21:29:25 -04001758 WARN_ON(ret);
1759 goto out;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001760 }
Josef Bacik96303082009-07-13 21:29:25 -04001761
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001762 ret = remove_from_bitmap(ctl, info, &offset, &bytes);
Josef Bacik96303082009-07-13 21:29:25 -04001763 if (ret == -EAGAIN)
1764 goto again;
1765 BUG_ON(ret);
1766out_lock:
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001767 spin_unlock(&ctl->tree_lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001768out:
Josef Bacik25179202008-10-29 14:49:05 -04001769 return ret;
1770}
1771
Josef Bacik0f9dd462008-09-23 13:14:11 -04001772void btrfs_dump_free_space(struct btrfs_block_group_cache *block_group,
1773 u64 bytes)
1774{
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001775 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001776 struct btrfs_free_space *info;
1777 struct rb_node *n;
1778 int count = 0;
1779
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001780 for (n = rb_first(&ctl->free_space_offset); n; n = rb_next(n)) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001781 info = rb_entry(n, struct btrfs_free_space, offset_index);
1782 if (info->bytes >= bytes)
1783 count++;
Josef Bacik96303082009-07-13 21:29:25 -04001784 printk(KERN_CRIT "entry offset %llu, bytes %llu, bitmap %s\n",
Joel Becker21380932009-04-21 12:38:29 -07001785 (unsigned long long)info->offset,
Josef Bacik96303082009-07-13 21:29:25 -04001786 (unsigned long long)info->bytes,
1787 (info->bitmap) ? "yes" : "no");
Josef Bacik0f9dd462008-09-23 13:14:11 -04001788 }
Josef Bacik96303082009-07-13 21:29:25 -04001789 printk(KERN_INFO "block group has cluster?: %s\n",
1790 list_empty(&block_group->cluster_list) ? "no" : "yes");
Josef Bacik0f9dd462008-09-23 13:14:11 -04001791 printk(KERN_INFO "%d blocks of free space at or bigger than bytes is"
1792 "\n", count);
1793}
1794
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001795void btrfs_init_free_space_ctl(struct btrfs_block_group_cache *block_group)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001796{
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001797 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001798
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001799 spin_lock_init(&ctl->tree_lock);
1800 ctl->unit = block_group->sectorsize;
1801 ctl->start = block_group->key.objectid;
1802 ctl->private = block_group;
1803 ctl->op = &free_space_op;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001804
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001805 /*
1806 * we only want to have 32k of ram per block group for keeping
1807 * track of free space, and if we pass 1/2 of that we want to
1808 * start converting things over to using bitmaps
1809 */
1810 ctl->extents_thresh = ((1024 * 32) / 2) /
1811 sizeof(struct btrfs_free_space);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001812}
1813
Chris Masonfa9c0d72009-04-03 09:47:43 -04001814/*
1815 * for a given cluster, put all of its extents back into the free
1816 * space cache. If the block group passed doesn't match the block group
1817 * pointed to by the cluster, someone else raced in and freed the
1818 * cluster already. In that case, we just return without changing anything
1819 */
1820static int
1821__btrfs_return_cluster_to_free_space(
1822 struct btrfs_block_group_cache *block_group,
1823 struct btrfs_free_cluster *cluster)
1824{
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001825 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Masonfa9c0d72009-04-03 09:47:43 -04001826 struct btrfs_free_space *entry;
1827 struct rb_node *node;
1828
1829 spin_lock(&cluster->lock);
1830 if (cluster->block_group != block_group)
1831 goto out;
1832
Josef Bacik96303082009-07-13 21:29:25 -04001833 cluster->block_group = NULL;
Chris Masonfa9c0d72009-04-03 09:47:43 -04001834 cluster->window_start = 0;
Josef Bacik96303082009-07-13 21:29:25 -04001835 list_del_init(&cluster->block_group_list);
Josef Bacik96303082009-07-13 21:29:25 -04001836
Chris Masonfa9c0d72009-04-03 09:47:43 -04001837 node = rb_first(&cluster->root);
Josef Bacik96303082009-07-13 21:29:25 -04001838 while (node) {
Josef Bacik4e69b592011-03-21 10:11:24 -04001839 bool bitmap;
1840
Chris Masonfa9c0d72009-04-03 09:47:43 -04001841 entry = rb_entry(node, struct btrfs_free_space, offset_index);
1842 node = rb_next(&entry->offset_index);
1843 rb_erase(&entry->offset_index, &cluster->root);
Josef Bacik4e69b592011-03-21 10:11:24 -04001844
1845 bitmap = (entry->bitmap != NULL);
1846 if (!bitmap)
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001847 try_merge_free_space(ctl, entry, false);
1848 tree_insert_offset(&ctl->free_space_offset,
Josef Bacik4e69b592011-03-21 10:11:24 -04001849 entry->offset, &entry->offset_index, bitmap);
Chris Masonfa9c0d72009-04-03 09:47:43 -04001850 }
Eric Paris6bef4d32010-02-23 19:43:04 +00001851 cluster->root = RB_ROOT;
Josef Bacik96303082009-07-13 21:29:25 -04001852
Chris Masonfa9c0d72009-04-03 09:47:43 -04001853out:
1854 spin_unlock(&cluster->lock);
Josef Bacik96303082009-07-13 21:29:25 -04001855 btrfs_put_block_group(block_group);
Chris Masonfa9c0d72009-04-03 09:47:43 -04001856 return 0;
1857}
1858
Chris Mason09655372011-05-21 09:27:38 -04001859void __btrfs_remove_free_space_cache_locked(struct btrfs_free_space_ctl *ctl)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001860{
1861 struct btrfs_free_space *info;
1862 struct rb_node *node;
Li Zefan581bb052011-04-20 10:06:11 +08001863
Li Zefan581bb052011-04-20 10:06:11 +08001864 while ((node = rb_last(&ctl->free_space_offset)) != NULL) {
1865 info = rb_entry(node, struct btrfs_free_space, offset_index);
Josef Bacik9b90f512011-06-24 16:02:51 +00001866 if (!info->bitmap) {
1867 unlink_free_space(ctl, info);
1868 kmem_cache_free(btrfs_free_space_cachep, info);
1869 } else {
1870 free_bitmap(ctl, info);
1871 }
Li Zefan581bb052011-04-20 10:06:11 +08001872 if (need_resched()) {
1873 spin_unlock(&ctl->tree_lock);
1874 cond_resched();
1875 spin_lock(&ctl->tree_lock);
1876 }
1877 }
Chris Mason09655372011-05-21 09:27:38 -04001878}
1879
1880void __btrfs_remove_free_space_cache(struct btrfs_free_space_ctl *ctl)
1881{
1882 spin_lock(&ctl->tree_lock);
1883 __btrfs_remove_free_space_cache_locked(ctl);
Li Zefan581bb052011-04-20 10:06:11 +08001884 spin_unlock(&ctl->tree_lock);
1885}
1886
Josef Bacik0f9dd462008-09-23 13:14:11 -04001887void btrfs_remove_free_space_cache(struct btrfs_block_group_cache *block_group)
1888{
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001889 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Masonfa9c0d72009-04-03 09:47:43 -04001890 struct btrfs_free_cluster *cluster;
Josef Bacik96303082009-07-13 21:29:25 -04001891 struct list_head *head;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001892
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001893 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04001894 while ((head = block_group->cluster_list.next) !=
1895 &block_group->cluster_list) {
1896 cluster = list_entry(head, struct btrfs_free_cluster,
1897 block_group_list);
Chris Masonfa9c0d72009-04-03 09:47:43 -04001898
1899 WARN_ON(cluster->block_group != block_group);
1900 __btrfs_return_cluster_to_free_space(block_group, cluster);
Josef Bacik96303082009-07-13 21:29:25 -04001901 if (need_resched()) {
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001902 spin_unlock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04001903 cond_resched();
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001904 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04001905 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04001906 }
Chris Mason09655372011-05-21 09:27:38 -04001907 __btrfs_remove_free_space_cache_locked(ctl);
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001908 spin_unlock(&ctl->tree_lock);
Chris Masonfa9c0d72009-04-03 09:47:43 -04001909
Josef Bacik0f9dd462008-09-23 13:14:11 -04001910}
1911
Josef Bacik6226cb02009-04-03 10:14:18 -04001912u64 btrfs_find_space_for_alloc(struct btrfs_block_group_cache *block_group,
1913 u64 offset, u64 bytes, u64 empty_size)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001914{
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001915 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik6226cb02009-04-03 10:14:18 -04001916 struct btrfs_free_space *entry = NULL;
Josef Bacik96303082009-07-13 21:29:25 -04001917 u64 bytes_search = bytes + empty_size;
Josef Bacik6226cb02009-04-03 10:14:18 -04001918 u64 ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001919
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001920 spin_lock(&ctl->tree_lock);
1921 entry = find_free_space(ctl, &offset, &bytes_search);
Josef Bacik6226cb02009-04-03 10:14:18 -04001922 if (!entry)
Josef Bacik96303082009-07-13 21:29:25 -04001923 goto out;
1924
1925 ret = offset;
1926 if (entry->bitmap) {
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001927 bitmap_clear_bits(ctl, entry, offset, bytes);
Li Zefanedf6e2d2010-11-09 14:50:07 +08001928 if (!entry->bytes)
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001929 free_bitmap(ctl, entry);
Josef Bacik96303082009-07-13 21:29:25 -04001930 } else {
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001931 unlink_free_space(ctl, entry);
Josef Bacik6226cb02009-04-03 10:14:18 -04001932 entry->offset += bytes;
1933 entry->bytes -= bytes;
Josef Bacik6226cb02009-04-03 10:14:18 -04001934 if (!entry->bytes)
Josef Bacikdc89e982011-01-28 17:05:48 -05001935 kmem_cache_free(btrfs_free_space_cachep, entry);
Josef Bacik6226cb02009-04-03 10:14:18 -04001936 else
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001937 link_free_space(ctl, entry);
Josef Bacik6226cb02009-04-03 10:14:18 -04001938 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04001939
Josef Bacik96303082009-07-13 21:29:25 -04001940out:
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001941 spin_unlock(&ctl->tree_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04001942
Josef Bacik0f9dd462008-09-23 13:14:11 -04001943 return ret;
1944}
Chris Masonfa9c0d72009-04-03 09:47:43 -04001945
1946/*
1947 * given a cluster, put all of its extents back into the free space
1948 * cache. If a block group is passed, this function will only free
1949 * a cluster that belongs to the passed block group.
1950 *
1951 * Otherwise, it'll get a reference on the block group pointed to by the
1952 * cluster and remove the cluster from it.
1953 */
1954int btrfs_return_cluster_to_free_space(
1955 struct btrfs_block_group_cache *block_group,
1956 struct btrfs_free_cluster *cluster)
1957{
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001958 struct btrfs_free_space_ctl *ctl;
Chris Masonfa9c0d72009-04-03 09:47:43 -04001959 int ret;
1960
1961 /* first, get a safe pointer to the block group */
1962 spin_lock(&cluster->lock);
1963 if (!block_group) {
1964 block_group = cluster->block_group;
1965 if (!block_group) {
1966 spin_unlock(&cluster->lock);
1967 return 0;
1968 }
1969 } else if (cluster->block_group != block_group) {
1970 /* someone else has already freed it don't redo their work */
1971 spin_unlock(&cluster->lock);
1972 return 0;
1973 }
1974 atomic_inc(&block_group->count);
1975 spin_unlock(&cluster->lock);
1976
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001977 ctl = block_group->free_space_ctl;
1978
Chris Masonfa9c0d72009-04-03 09:47:43 -04001979 /* now return any extents the cluster had on it */
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001980 spin_lock(&ctl->tree_lock);
Chris Masonfa9c0d72009-04-03 09:47:43 -04001981 ret = __btrfs_return_cluster_to_free_space(block_group, cluster);
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001982 spin_unlock(&ctl->tree_lock);
Chris Masonfa9c0d72009-04-03 09:47:43 -04001983
1984 /* finally drop our ref */
1985 btrfs_put_block_group(block_group);
1986 return ret;
1987}
1988
Josef Bacik96303082009-07-13 21:29:25 -04001989static u64 btrfs_alloc_from_bitmap(struct btrfs_block_group_cache *block_group,
1990 struct btrfs_free_cluster *cluster,
Josef Bacik4e69b592011-03-21 10:11:24 -04001991 struct btrfs_free_space *entry,
Josef Bacik96303082009-07-13 21:29:25 -04001992 u64 bytes, u64 min_start)
1993{
Li Zefan34d52cb6c2011-03-29 13:46:06 +08001994 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik96303082009-07-13 21:29:25 -04001995 int err;
1996 u64 search_start = cluster->window_start;
1997 u64 search_bytes = bytes;
1998 u64 ret = 0;
1999
Josef Bacik96303082009-07-13 21:29:25 -04002000 search_start = min_start;
2001 search_bytes = bytes;
2002
Li Zefan34d52cb6c2011-03-29 13:46:06 +08002003 err = search_bitmap(ctl, entry, &search_start, &search_bytes);
Josef Bacik96303082009-07-13 21:29:25 -04002004 if (err)
Josef Bacik4e69b592011-03-21 10:11:24 -04002005 return 0;
Josef Bacik96303082009-07-13 21:29:25 -04002006
2007 ret = search_start;
Miao Xiebb3ac5a2011-08-05 09:32:35 +00002008 __bitmap_clear_bits(ctl, entry, ret, bytes);
Josef Bacik96303082009-07-13 21:29:25 -04002009
2010 return ret;
2011}
2012
Chris Masonfa9c0d72009-04-03 09:47:43 -04002013/*
2014 * given a cluster, try to allocate 'bytes' from it, returns 0
2015 * if it couldn't find anything suitably large, or a logical disk offset
2016 * if things worked out
2017 */
2018u64 btrfs_alloc_from_cluster(struct btrfs_block_group_cache *block_group,
2019 struct btrfs_free_cluster *cluster, u64 bytes,
2020 u64 min_start)
2021{
Li Zefan34d52cb6c2011-03-29 13:46:06 +08002022 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Masonfa9c0d72009-04-03 09:47:43 -04002023 struct btrfs_free_space *entry = NULL;
2024 struct rb_node *node;
2025 u64 ret = 0;
2026
2027 spin_lock(&cluster->lock);
2028 if (bytes > cluster->max_size)
2029 goto out;
2030
2031 if (cluster->block_group != block_group)
2032 goto out;
2033
2034 node = rb_first(&cluster->root);
2035 if (!node)
2036 goto out;
2037
2038 entry = rb_entry(node, struct btrfs_free_space, offset_index);
Chris Masonfa9c0d72009-04-03 09:47:43 -04002039 while(1) {
Josef Bacik4e69b592011-03-21 10:11:24 -04002040 if (entry->bytes < bytes ||
2041 (!entry->bitmap && entry->offset < min_start)) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04002042 node = rb_next(&entry->offset_index);
2043 if (!node)
2044 break;
2045 entry = rb_entry(node, struct btrfs_free_space,
2046 offset_index);
2047 continue;
2048 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04002049
Josef Bacik4e69b592011-03-21 10:11:24 -04002050 if (entry->bitmap) {
2051 ret = btrfs_alloc_from_bitmap(block_group,
2052 cluster, entry, bytes,
2053 min_start);
2054 if (ret == 0) {
Josef Bacik4e69b592011-03-21 10:11:24 -04002055 node = rb_next(&entry->offset_index);
2056 if (!node)
2057 break;
2058 entry = rb_entry(node, struct btrfs_free_space,
2059 offset_index);
2060 continue;
2061 }
2062 } else {
Josef Bacik4e69b592011-03-21 10:11:24 -04002063 ret = entry->offset;
2064
2065 entry->offset += bytes;
2066 entry->bytes -= bytes;
2067 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04002068
Li Zefan5e71b5d2010-11-09 14:55:34 +08002069 if (entry->bytes == 0)
Chris Masonfa9c0d72009-04-03 09:47:43 -04002070 rb_erase(&entry->offset_index, &cluster->root);
Chris Masonfa9c0d72009-04-03 09:47:43 -04002071 break;
2072 }
2073out:
2074 spin_unlock(&cluster->lock);
Josef Bacik96303082009-07-13 21:29:25 -04002075
Li Zefan5e71b5d2010-11-09 14:55:34 +08002076 if (!ret)
2077 return 0;
2078
Li Zefan34d52cb6c2011-03-29 13:46:06 +08002079 spin_lock(&ctl->tree_lock);
Li Zefan5e71b5d2010-11-09 14:55:34 +08002080
Li Zefan34d52cb6c2011-03-29 13:46:06 +08002081 ctl->free_space -= bytes;
Li Zefan5e71b5d2010-11-09 14:55:34 +08002082 if (entry->bytes == 0) {
Li Zefan34d52cb6c2011-03-29 13:46:06 +08002083 ctl->free_extents--;
Josef Bacik4e69b592011-03-21 10:11:24 -04002084 if (entry->bitmap) {
2085 kfree(entry->bitmap);
Li Zefan34d52cb6c2011-03-29 13:46:06 +08002086 ctl->total_bitmaps--;
2087 ctl->op->recalc_thresholds(ctl);
Josef Bacik4e69b592011-03-21 10:11:24 -04002088 }
Josef Bacikdc89e982011-01-28 17:05:48 -05002089 kmem_cache_free(btrfs_free_space_cachep, entry);
Li Zefan5e71b5d2010-11-09 14:55:34 +08002090 }
2091
Li Zefan34d52cb6c2011-03-29 13:46:06 +08002092 spin_unlock(&ctl->tree_lock);
Li Zefan5e71b5d2010-11-09 14:55:34 +08002093
Chris Masonfa9c0d72009-04-03 09:47:43 -04002094 return ret;
2095}
2096
Josef Bacik96303082009-07-13 21:29:25 -04002097static int btrfs_bitmap_cluster(struct btrfs_block_group_cache *block_group,
2098 struct btrfs_free_space *entry,
2099 struct btrfs_free_cluster *cluster,
2100 u64 offset, u64 bytes, u64 min_bytes)
2101{
Li Zefan34d52cb6c2011-03-29 13:46:06 +08002102 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik96303082009-07-13 21:29:25 -04002103 unsigned long next_zero;
2104 unsigned long i;
2105 unsigned long search_bits;
2106 unsigned long total_bits;
2107 unsigned long found_bits;
2108 unsigned long start = 0;
2109 unsigned long total_found = 0;
Josef Bacik4e69b592011-03-21 10:11:24 -04002110 int ret;
Josef Bacik96303082009-07-13 21:29:25 -04002111 bool found = false;
2112
2113 i = offset_to_bit(entry->offset, block_group->sectorsize,
2114 max_t(u64, offset, entry->offset));
Josef Bacikd0a365e2011-03-18 15:27:43 -04002115 search_bits = bytes_to_bits(bytes, block_group->sectorsize);
2116 total_bits = bytes_to_bits(min_bytes, block_group->sectorsize);
Josef Bacik96303082009-07-13 21:29:25 -04002117
2118again:
2119 found_bits = 0;
2120 for (i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, i);
2121 i < BITS_PER_BITMAP;
2122 i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, i + 1)) {
2123 next_zero = find_next_zero_bit(entry->bitmap,
2124 BITS_PER_BITMAP, i);
2125 if (next_zero - i >= search_bits) {
2126 found_bits = next_zero - i;
2127 break;
2128 }
2129 i = next_zero;
2130 }
2131
2132 if (!found_bits)
Josef Bacik4e69b592011-03-21 10:11:24 -04002133 return -ENOSPC;
Josef Bacik96303082009-07-13 21:29:25 -04002134
2135 if (!found) {
2136 start = i;
2137 found = true;
2138 }
2139
2140 total_found += found_bits;
2141
2142 if (cluster->max_size < found_bits * block_group->sectorsize)
2143 cluster->max_size = found_bits * block_group->sectorsize;
2144
2145 if (total_found < total_bits) {
2146 i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, next_zero);
2147 if (i - start > total_bits * 2) {
2148 total_found = 0;
2149 cluster->max_size = 0;
2150 found = false;
2151 }
2152 goto again;
2153 }
2154
2155 cluster->window_start = start * block_group->sectorsize +
2156 entry->offset;
Li Zefan34d52cb6c2011-03-29 13:46:06 +08002157 rb_erase(&entry->offset_index, &ctl->free_space_offset);
Josef Bacik4e69b592011-03-21 10:11:24 -04002158 ret = tree_insert_offset(&cluster->root, entry->offset,
2159 &entry->offset_index, 1);
2160 BUG_ON(ret);
Josef Bacik96303082009-07-13 21:29:25 -04002161
2162 return 0;
2163}
2164
Chris Masonfa9c0d72009-04-03 09:47:43 -04002165/*
Josef Bacik4e69b592011-03-21 10:11:24 -04002166 * This searches the block group for just extents to fill the cluster with.
2167 */
Josef Bacik3de85bb2011-05-25 13:07:37 -04002168static noinline int
2169setup_cluster_no_bitmap(struct btrfs_block_group_cache *block_group,
2170 struct btrfs_free_cluster *cluster,
2171 struct list_head *bitmaps, u64 offset, u64 bytes,
2172 u64 min_bytes)
Josef Bacik4e69b592011-03-21 10:11:24 -04002173{
Li Zefan34d52cb6c2011-03-29 13:46:06 +08002174 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik4e69b592011-03-21 10:11:24 -04002175 struct btrfs_free_space *first = NULL;
2176 struct btrfs_free_space *entry = NULL;
2177 struct btrfs_free_space *prev = NULL;
2178 struct btrfs_free_space *last;
2179 struct rb_node *node;
2180 u64 window_start;
2181 u64 window_free;
2182 u64 max_extent;
2183 u64 max_gap = 128 * 1024;
2184
Li Zefan34d52cb6c2011-03-29 13:46:06 +08002185 entry = tree_search_offset(ctl, offset, 0, 1);
Josef Bacik4e69b592011-03-21 10:11:24 -04002186 if (!entry)
2187 return -ENOSPC;
2188
2189 /*
2190 * We don't want bitmaps, so just move along until we find a normal
2191 * extent entry.
2192 */
2193 while (entry->bitmap) {
Josef Bacik86d4a772011-05-25 13:03:16 -04002194 if (list_empty(&entry->list))
2195 list_add_tail(&entry->list, bitmaps);
Josef Bacik4e69b592011-03-21 10:11:24 -04002196 node = rb_next(&entry->offset_index);
2197 if (!node)
2198 return -ENOSPC;
2199 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2200 }
2201
2202 window_start = entry->offset;
2203 window_free = entry->bytes;
2204 max_extent = entry->bytes;
2205 first = entry;
2206 last = entry;
2207 prev = entry;
2208
2209 while (window_free <= min_bytes) {
2210 node = rb_next(&entry->offset_index);
2211 if (!node)
2212 return -ENOSPC;
2213 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2214
Josef Bacik86d4a772011-05-25 13:03:16 -04002215 if (entry->bitmap) {
2216 if (list_empty(&entry->list))
2217 list_add_tail(&entry->list, bitmaps);
Josef Bacik4e69b592011-03-21 10:11:24 -04002218 continue;
Josef Bacik86d4a772011-05-25 13:03:16 -04002219 }
2220
Josef Bacik4e69b592011-03-21 10:11:24 -04002221 /*
2222 * we haven't filled the empty size and the window is
2223 * very large. reset and try again
2224 */
2225 if (entry->offset - (prev->offset + prev->bytes) > max_gap ||
2226 entry->offset - window_start > (min_bytes * 2)) {
2227 first = entry;
2228 window_start = entry->offset;
2229 window_free = entry->bytes;
2230 last = entry;
2231 max_extent = entry->bytes;
2232 } else {
2233 last = entry;
2234 window_free += entry->bytes;
2235 if (entry->bytes > max_extent)
2236 max_extent = entry->bytes;
2237 }
2238 prev = entry;
2239 }
2240
2241 cluster->window_start = first->offset;
2242
2243 node = &first->offset_index;
2244
2245 /*
2246 * now we've found our entries, pull them out of the free space
2247 * cache and put them into the cluster rbtree
2248 */
2249 do {
2250 int ret;
2251
2252 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2253 node = rb_next(&entry->offset_index);
2254 if (entry->bitmap)
2255 continue;
2256
Li Zefan34d52cb6c2011-03-29 13:46:06 +08002257 rb_erase(&entry->offset_index, &ctl->free_space_offset);
Josef Bacik4e69b592011-03-21 10:11:24 -04002258 ret = tree_insert_offset(&cluster->root, entry->offset,
2259 &entry->offset_index, 0);
2260 BUG_ON(ret);
2261 } while (node && entry != last);
2262
2263 cluster->max_size = max_extent;
2264
2265 return 0;
2266}
2267
2268/*
2269 * This specifically looks for bitmaps that may work in the cluster, we assume
2270 * that we have already failed to find extents that will work.
2271 */
Josef Bacik3de85bb2011-05-25 13:07:37 -04002272static noinline int
2273setup_cluster_bitmap(struct btrfs_block_group_cache *block_group,
2274 struct btrfs_free_cluster *cluster,
2275 struct list_head *bitmaps, u64 offset, u64 bytes,
2276 u64 min_bytes)
Josef Bacik4e69b592011-03-21 10:11:24 -04002277{
Li Zefan34d52cb6c2011-03-29 13:46:06 +08002278 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik4e69b592011-03-21 10:11:24 -04002279 struct btrfs_free_space *entry;
2280 struct rb_node *node;
2281 int ret = -ENOSPC;
2282
Li Zefan34d52cb6c2011-03-29 13:46:06 +08002283 if (ctl->total_bitmaps == 0)
Josef Bacik4e69b592011-03-21 10:11:24 -04002284 return -ENOSPC;
2285
Josef Bacik86d4a772011-05-25 13:03:16 -04002286 /*
2287 * First check our cached list of bitmaps and see if there is an entry
2288 * here that will work.
2289 */
2290 list_for_each_entry(entry, bitmaps, list) {
2291 if (entry->bytes < min_bytes)
2292 continue;
2293 ret = btrfs_bitmap_cluster(block_group, entry, cluster, offset,
2294 bytes, min_bytes);
2295 if (!ret)
2296 return 0;
2297 }
2298
2299 /*
2300 * If we do have entries on our list and we are here then we didn't find
2301 * anything, so go ahead and get the next entry after the last entry in
2302 * this list and start the search from there.
2303 */
2304 if (!list_empty(bitmaps)) {
2305 entry = list_entry(bitmaps->prev, struct btrfs_free_space,
2306 list);
2307 node = rb_next(&entry->offset_index);
2308 if (!node)
2309 return -ENOSPC;
2310 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2311 goto search;
2312 }
2313
Li Zefan34d52cb6c2011-03-29 13:46:06 +08002314 entry = tree_search_offset(ctl, offset_to_bitmap(ctl, offset), 0, 1);
Josef Bacik4e69b592011-03-21 10:11:24 -04002315 if (!entry)
2316 return -ENOSPC;
2317
Josef Bacik86d4a772011-05-25 13:03:16 -04002318search:
Josef Bacik4e69b592011-03-21 10:11:24 -04002319 node = &entry->offset_index;
2320 do {
2321 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2322 node = rb_next(&entry->offset_index);
2323 if (!entry->bitmap)
2324 continue;
2325 if (entry->bytes < min_bytes)
2326 continue;
2327 ret = btrfs_bitmap_cluster(block_group, entry, cluster, offset,
2328 bytes, min_bytes);
2329 } while (ret && node);
2330
2331 return ret;
2332}
2333
2334/*
Chris Masonfa9c0d72009-04-03 09:47:43 -04002335 * here we try to find a cluster of blocks in a block group. The goal
2336 * is to find at least bytes free and up to empty_size + bytes free.
2337 * We might not find them all in one contiguous area.
2338 *
2339 * returns zero and sets up cluster if things worked out, otherwise
2340 * it returns -enospc
2341 */
2342int btrfs_find_space_cluster(struct btrfs_trans_handle *trans,
Chris Mason451d7582009-06-09 20:28:34 -04002343 struct btrfs_root *root,
Chris Masonfa9c0d72009-04-03 09:47:43 -04002344 struct btrfs_block_group_cache *block_group,
2345 struct btrfs_free_cluster *cluster,
2346 u64 offset, u64 bytes, u64 empty_size)
2347{
Li Zefan34d52cb6c2011-03-29 13:46:06 +08002348 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik86d4a772011-05-25 13:03:16 -04002349 struct list_head bitmaps;
2350 struct btrfs_free_space *entry, *tmp;
Chris Masonfa9c0d72009-04-03 09:47:43 -04002351 u64 min_bytes;
Chris Masonfa9c0d72009-04-03 09:47:43 -04002352 int ret;
2353
2354 /* for metadata, allow allocates with more holes */
Chris Mason451d7582009-06-09 20:28:34 -04002355 if (btrfs_test_opt(root, SSD_SPREAD)) {
2356 min_bytes = bytes + empty_size;
2357 } else if (block_group->flags & BTRFS_BLOCK_GROUP_METADATA) {
Chris Masonfa9c0d72009-04-03 09:47:43 -04002358 /*
2359 * we want to do larger allocations when we are
2360 * flushing out the delayed refs, it helps prevent
2361 * making more work as we go along.
2362 */
2363 if (trans->transaction->delayed_refs.flushing)
2364 min_bytes = max(bytes, (bytes + empty_size) >> 1);
2365 else
2366 min_bytes = max(bytes, (bytes + empty_size) >> 4);
2367 } else
2368 min_bytes = max(bytes, (bytes + empty_size) >> 2);
2369
Li Zefan34d52cb6c2011-03-29 13:46:06 +08002370 spin_lock(&ctl->tree_lock);
Josef Bacik7d0d2e82011-03-18 15:13:42 -04002371
2372 /*
2373 * If we know we don't have enough space to make a cluster don't even
2374 * bother doing all the work to try and find one.
2375 */
Li Zefan34d52cb6c2011-03-29 13:46:06 +08002376 if (ctl->free_space < min_bytes) {
2377 spin_unlock(&ctl->tree_lock);
Josef Bacik7d0d2e82011-03-18 15:13:42 -04002378 return -ENOSPC;
2379 }
2380
Chris Masonfa9c0d72009-04-03 09:47:43 -04002381 spin_lock(&cluster->lock);
2382
2383 /* someone already found a cluster, hooray */
2384 if (cluster->block_group) {
2385 ret = 0;
2386 goto out;
2387 }
Josef Bacik4e69b592011-03-21 10:11:24 -04002388
Josef Bacik86d4a772011-05-25 13:03:16 -04002389 INIT_LIST_HEAD(&bitmaps);
2390 ret = setup_cluster_no_bitmap(block_group, cluster, &bitmaps, offset,
2391 bytes, min_bytes);
Josef Bacik4e69b592011-03-21 10:11:24 -04002392 if (ret)
Josef Bacik86d4a772011-05-25 13:03:16 -04002393 ret = setup_cluster_bitmap(block_group, cluster, &bitmaps,
2394 offset, bytes, min_bytes);
2395
2396 /* Clear our temporary list */
2397 list_for_each_entry_safe(entry, tmp, &bitmaps, list)
2398 list_del_init(&entry->list);
Josef Bacik4e69b592011-03-21 10:11:24 -04002399
2400 if (!ret) {
2401 atomic_inc(&block_group->count);
2402 list_add_tail(&cluster->block_group_list,
2403 &block_group->cluster_list);
2404 cluster->block_group = block_group;
Chris Masonfa9c0d72009-04-03 09:47:43 -04002405 }
Chris Masonfa9c0d72009-04-03 09:47:43 -04002406out:
2407 spin_unlock(&cluster->lock);
Li Zefan34d52cb6c2011-03-29 13:46:06 +08002408 spin_unlock(&ctl->tree_lock);
Chris Masonfa9c0d72009-04-03 09:47:43 -04002409
2410 return ret;
2411}
2412
2413/*
2414 * simple code to zero out a cluster
2415 */
2416void btrfs_init_free_cluster(struct btrfs_free_cluster *cluster)
2417{
2418 spin_lock_init(&cluster->lock);
2419 spin_lock_init(&cluster->refill_lock);
Eric Paris6bef4d32010-02-23 19:43:04 +00002420 cluster->root = RB_ROOT;
Chris Masonfa9c0d72009-04-03 09:47:43 -04002421 cluster->max_size = 0;
2422 INIT_LIST_HEAD(&cluster->block_group_list);
2423 cluster->block_group = NULL;
2424}
2425
Li Dongyangf7039b12011-03-24 10:24:28 +00002426int btrfs_trim_block_group(struct btrfs_block_group_cache *block_group,
2427 u64 *trimmed, u64 start, u64 end, u64 minlen)
2428{
Li Zefan34d52cb6c2011-03-29 13:46:06 +08002429 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Li Dongyangf7039b12011-03-24 10:24:28 +00002430 struct btrfs_free_space *entry = NULL;
2431 struct btrfs_fs_info *fs_info = block_group->fs_info;
2432 u64 bytes = 0;
2433 u64 actually_trimmed;
2434 int ret = 0;
2435
2436 *trimmed = 0;
2437
2438 while (start < end) {
Li Zefan34d52cb6c2011-03-29 13:46:06 +08002439 spin_lock(&ctl->tree_lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00002440
Li Zefan34d52cb6c2011-03-29 13:46:06 +08002441 if (ctl->free_space < minlen) {
2442 spin_unlock(&ctl->tree_lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00002443 break;
2444 }
2445
Li Zefan34d52cb6c2011-03-29 13:46:06 +08002446 entry = tree_search_offset(ctl, start, 0, 1);
Li Dongyangf7039b12011-03-24 10:24:28 +00002447 if (!entry)
Li Zefan34d52cb6c2011-03-29 13:46:06 +08002448 entry = tree_search_offset(ctl,
2449 offset_to_bitmap(ctl, start),
Li Dongyangf7039b12011-03-24 10:24:28 +00002450 1, 1);
2451
2452 if (!entry || entry->offset >= end) {
Li Zefan34d52cb6c2011-03-29 13:46:06 +08002453 spin_unlock(&ctl->tree_lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00002454 break;
2455 }
2456
2457 if (entry->bitmap) {
Li Zefan34d52cb6c2011-03-29 13:46:06 +08002458 ret = search_bitmap(ctl, entry, &start, &bytes);
Li Dongyangf7039b12011-03-24 10:24:28 +00002459 if (!ret) {
2460 if (start >= end) {
Li Zefan34d52cb6c2011-03-29 13:46:06 +08002461 spin_unlock(&ctl->tree_lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00002462 break;
2463 }
2464 bytes = min(bytes, end - start);
Li Zefan34d52cb6c2011-03-29 13:46:06 +08002465 bitmap_clear_bits(ctl, entry, start, bytes);
Li Dongyangf7039b12011-03-24 10:24:28 +00002466 if (entry->bytes == 0)
Li Zefan34d52cb6c2011-03-29 13:46:06 +08002467 free_bitmap(ctl, entry);
Li Dongyangf7039b12011-03-24 10:24:28 +00002468 } else {
2469 start = entry->offset + BITS_PER_BITMAP *
2470 block_group->sectorsize;
Li Zefan34d52cb6c2011-03-29 13:46:06 +08002471 spin_unlock(&ctl->tree_lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00002472 ret = 0;
2473 continue;
2474 }
2475 } else {
2476 start = entry->offset;
2477 bytes = min(entry->bytes, end - start);
Li Zefan34d52cb6c2011-03-29 13:46:06 +08002478 unlink_free_space(ctl, entry);
Li Zefanf789b682011-04-25 19:43:52 -04002479 kmem_cache_free(btrfs_free_space_cachep, entry);
Li Dongyangf7039b12011-03-24 10:24:28 +00002480 }
2481
Li Zefan34d52cb6c2011-03-29 13:46:06 +08002482 spin_unlock(&ctl->tree_lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00002483
2484 if (bytes >= minlen) {
Josef Bacikfb25e912011-07-26 17:00:46 -04002485 struct btrfs_space_info *space_info;
2486 int update = 0;
2487
2488 space_info = block_group->space_info;
2489 spin_lock(&space_info->lock);
2490 spin_lock(&block_group->lock);
2491 if (!block_group->ro) {
2492 block_group->reserved += bytes;
2493 space_info->bytes_reserved += bytes;
2494 update = 1;
2495 }
2496 spin_unlock(&block_group->lock);
2497 spin_unlock(&space_info->lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00002498
2499 ret = btrfs_error_discard_extent(fs_info->extent_root,
2500 start,
2501 bytes,
2502 &actually_trimmed);
2503
Li Zefan34d52cb6c2011-03-29 13:46:06 +08002504 btrfs_add_free_space(block_group, start, bytes);
Josef Bacikfb25e912011-07-26 17:00:46 -04002505 if (update) {
2506 spin_lock(&space_info->lock);
2507 spin_lock(&block_group->lock);
2508 if (block_group->ro)
2509 space_info->bytes_readonly += bytes;
2510 block_group->reserved -= bytes;
2511 space_info->bytes_reserved -= bytes;
2512 spin_unlock(&space_info->lock);
2513 spin_unlock(&block_group->lock);
2514 }
Li Dongyangf7039b12011-03-24 10:24:28 +00002515
2516 if (ret)
2517 break;
2518 *trimmed += actually_trimmed;
2519 }
2520 start += bytes;
2521 bytes = 0;
2522
2523 if (fatal_signal_pending(current)) {
2524 ret = -ERESTARTSYS;
2525 break;
2526 }
2527
2528 cond_resched();
2529 }
2530
2531 return ret;
2532}
Li Zefan581bb052011-04-20 10:06:11 +08002533
2534/*
2535 * Find the left-most item in the cache tree, and then return the
2536 * smallest inode number in the item.
2537 *
2538 * Note: the returned inode number may not be the smallest one in
2539 * the tree, if the left-most item is a bitmap.
2540 */
2541u64 btrfs_find_ino_for_alloc(struct btrfs_root *fs_root)
2542{
2543 struct btrfs_free_space_ctl *ctl = fs_root->free_ino_ctl;
2544 struct btrfs_free_space *entry = NULL;
2545 u64 ino = 0;
2546
2547 spin_lock(&ctl->tree_lock);
2548
2549 if (RB_EMPTY_ROOT(&ctl->free_space_offset))
2550 goto out;
2551
2552 entry = rb_entry(rb_first(&ctl->free_space_offset),
2553 struct btrfs_free_space, offset_index);
2554
2555 if (!entry->bitmap) {
2556 ino = entry->offset;
2557
2558 unlink_free_space(ctl, entry);
2559 entry->offset++;
2560 entry->bytes--;
2561 if (!entry->bytes)
2562 kmem_cache_free(btrfs_free_space_cachep, entry);
2563 else
2564 link_free_space(ctl, entry);
2565 } else {
2566 u64 offset = 0;
2567 u64 count = 1;
2568 int ret;
2569
2570 ret = search_bitmap(ctl, entry, &offset, &count);
2571 BUG_ON(ret);
2572
2573 ino = offset;
2574 bitmap_clear_bits(ctl, entry, offset, 1);
2575 if (entry->bytes == 0)
2576 free_bitmap(ctl, entry);
2577 }
2578out:
2579 spin_unlock(&ctl->tree_lock);
2580
2581 return ino;
2582}
Li Zefan82d59022011-04-20 10:33:24 +08002583
2584struct inode *lookup_free_ino_inode(struct btrfs_root *root,
2585 struct btrfs_path *path)
2586{
2587 struct inode *inode = NULL;
2588
2589 spin_lock(&root->cache_lock);
2590 if (root->cache_inode)
2591 inode = igrab(root->cache_inode);
2592 spin_unlock(&root->cache_lock);
2593 if (inode)
2594 return inode;
2595
2596 inode = __lookup_free_space_inode(root, path, 0);
2597 if (IS_ERR(inode))
2598 return inode;
2599
2600 spin_lock(&root->cache_lock);
David Sterba7841cb22011-05-31 18:07:27 +02002601 if (!btrfs_fs_closing(root->fs_info))
Li Zefan82d59022011-04-20 10:33:24 +08002602 root->cache_inode = igrab(inode);
2603 spin_unlock(&root->cache_lock);
2604
2605 return inode;
2606}
2607
2608int create_free_ino_inode(struct btrfs_root *root,
2609 struct btrfs_trans_handle *trans,
2610 struct btrfs_path *path)
2611{
2612 return __create_free_space_inode(root, trans, path,
2613 BTRFS_FREE_INO_OBJECTID, 0);
2614}
2615
2616int load_free_ino_cache(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
2617{
2618 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
2619 struct btrfs_path *path;
2620 struct inode *inode;
2621 int ret = 0;
2622 u64 root_gen = btrfs_root_generation(&root->root_item);
2623
Chris Mason4b9465c2011-06-03 09:36:29 -04002624 if (!btrfs_test_opt(root, INODE_MAP_CACHE))
2625 return 0;
2626
Li Zefan82d59022011-04-20 10:33:24 +08002627 /*
2628 * If we're unmounting then just return, since this does a search on the
2629 * normal root and not the commit root and we could deadlock.
2630 */
David Sterba7841cb22011-05-31 18:07:27 +02002631 if (btrfs_fs_closing(fs_info))
Li Zefan82d59022011-04-20 10:33:24 +08002632 return 0;
2633
2634 path = btrfs_alloc_path();
2635 if (!path)
2636 return 0;
2637
2638 inode = lookup_free_ino_inode(root, path);
2639 if (IS_ERR(inode))
2640 goto out;
2641
2642 if (root_gen != BTRFS_I(inode)->generation)
2643 goto out_put;
2644
2645 ret = __load_free_space_cache(root, inode, ctl, path, 0);
2646
2647 if (ret < 0)
2648 printk(KERN_ERR "btrfs: failed to load free ino cache for "
2649 "root %llu\n", root->root_key.objectid);
2650out_put:
2651 iput(inode);
2652out:
2653 btrfs_free_path(path);
2654 return ret;
2655}
2656
2657int btrfs_write_out_ino_cache(struct btrfs_root *root,
2658 struct btrfs_trans_handle *trans,
2659 struct btrfs_path *path)
2660{
2661 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
2662 struct inode *inode;
2663 int ret;
2664
Chris Mason4b9465c2011-06-03 09:36:29 -04002665 if (!btrfs_test_opt(root, INODE_MAP_CACHE))
2666 return 0;
2667
Li Zefan82d59022011-04-20 10:33:24 +08002668 inode = lookup_free_ino_inode(root, path);
2669 if (IS_ERR(inode))
2670 return 0;
2671
2672 ret = __btrfs_write_out_cache(root, inode, ctl, NULL, trans, path, 0);
Josef Bacikc09544e2011-08-30 10:19:10 -04002673 if (ret) {
2674 btrfs_delalloc_release_metadata(inode, inode->i_size);
2675#ifdef DEBUG
Li Zefan82d59022011-04-20 10:33:24 +08002676 printk(KERN_ERR "btrfs: failed to write free ino cache "
2677 "for root %llu\n", root->root_key.objectid);
Josef Bacikc09544e2011-08-30 10:19:10 -04002678#endif
2679 }
Li Zefan82d59022011-04-20 10:33:24 +08002680
2681 iput(inode);
2682 return ret;
2683}