blob: bffa5c4a633b0e44d48370ccca645e9068e0bf3b [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 Bacik0f9dd462008-09-23 13:14:11 -040023#include "ctree.h"
Chris Masonfa9c0d792009-04-03 09:47:43 -040024#include "free-space-cache.h"
25#include "transaction.h"
Josef Bacik0af3d002010-06-21 14:48:16 -040026#include "disk-io.h"
Josef Bacik43be2142011-04-01 14:55:00 +000027#include "extent_io.h"
Li Zefan581bb052011-04-20 10:06:11 +080028#include "inode-map.h"
Chris Masonfa9c0d792009-04-03 09:47:43 -040029
Josef Bacik96303082009-07-13 21:29:25 -040030#define BITS_PER_BITMAP (PAGE_CACHE_SIZE * 8)
31#define MAX_CACHE_BYTES_PER_GIG (32 * 1024)
32
Li Zefan34d52cb2011-03-29 13:46:06 +080033static int link_free_space(struct btrfs_free_space_ctl *ctl,
Josef Bacik0cb59c92010-07-02 12:14:14 -040034 struct btrfs_free_space *info);
35
Li Zefan0414efa2011-04-20 10:20:14 +080036static struct inode *__lookup_free_space_inode(struct btrfs_root *root,
37 struct btrfs_path *path,
38 u64 offset)
Josef Bacik0af3d002010-06-21 14:48:16 -040039{
40 struct btrfs_key key;
41 struct btrfs_key location;
42 struct btrfs_disk_key disk_key;
43 struct btrfs_free_space_header *header;
44 struct extent_buffer *leaf;
45 struct inode *inode = NULL;
46 int ret;
47
Josef Bacik0af3d002010-06-21 14:48:16 -040048 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
Li Zefan0414efa2011-04-20 10:20:14 +080049 key.offset = offset;
Josef Bacik0af3d002010-06-21 14:48:16 -040050 key.type = 0;
51
52 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
53 if (ret < 0)
54 return ERR_PTR(ret);
55 if (ret > 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +020056 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -040057 return ERR_PTR(-ENOENT);
58 }
59
60 leaf = path->nodes[0];
61 header = btrfs_item_ptr(leaf, path->slots[0],
62 struct btrfs_free_space_header);
63 btrfs_free_space_key(leaf, header, &disk_key);
64 btrfs_disk_key_to_cpu(&location, &disk_key);
David Sterbab3b4aa72011-04-21 01:20:15 +020065 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -040066
67 inode = btrfs_iget(root->fs_info->sb, &location, root, NULL);
68 if (!inode)
69 return ERR_PTR(-ENOENT);
70 if (IS_ERR(inode))
71 return inode;
72 if (is_bad_inode(inode)) {
73 iput(inode);
74 return ERR_PTR(-ENOENT);
75 }
76
Miao Xieadae52b2011-03-31 09:43:23 +000077 inode->i_mapping->flags &= ~__GFP_FS;
78
Li Zefan0414efa2011-04-20 10:20:14 +080079 return inode;
80}
81
82struct inode *lookup_free_space_inode(struct btrfs_root *root,
83 struct btrfs_block_group_cache
84 *block_group, struct btrfs_path *path)
85{
86 struct inode *inode = NULL;
87
88 spin_lock(&block_group->lock);
89 if (block_group->inode)
90 inode = igrab(block_group->inode);
91 spin_unlock(&block_group->lock);
92 if (inode)
93 return inode;
94
95 inode = __lookup_free_space_inode(root, path,
96 block_group->key.objectid);
97 if (IS_ERR(inode))
98 return inode;
99
Josef Bacik0af3d002010-06-21 14:48:16 -0400100 spin_lock(&block_group->lock);
101 if (!root->fs_info->closing) {
102 block_group->inode = igrab(inode);
103 block_group->iref = 1;
104 }
105 spin_unlock(&block_group->lock);
106
107 return inode;
108}
109
Li Zefan0414efa2011-04-20 10:20:14 +0800110int __create_free_space_inode(struct btrfs_root *root,
111 struct btrfs_trans_handle *trans,
112 struct btrfs_path *path, u64 ino, u64 offset)
Josef Bacik0af3d002010-06-21 14:48:16 -0400113{
114 struct btrfs_key key;
115 struct btrfs_disk_key disk_key;
116 struct btrfs_free_space_header *header;
117 struct btrfs_inode_item *inode_item;
118 struct extent_buffer *leaf;
Josef Bacik0af3d002010-06-21 14:48:16 -0400119 int ret;
120
Li Zefan0414efa2011-04-20 10:20:14 +0800121 ret = btrfs_insert_empty_inode(trans, root, path, ino);
Josef Bacik0af3d002010-06-21 14:48:16 -0400122 if (ret)
123 return ret;
124
125 leaf = path->nodes[0];
126 inode_item = btrfs_item_ptr(leaf, path->slots[0],
127 struct btrfs_inode_item);
128 btrfs_item_key(leaf, &disk_key, path->slots[0]);
129 memset_extent_buffer(leaf, 0, (unsigned long)inode_item,
130 sizeof(*inode_item));
131 btrfs_set_inode_generation(leaf, inode_item, trans->transid);
132 btrfs_set_inode_size(leaf, inode_item, 0);
133 btrfs_set_inode_nbytes(leaf, inode_item, 0);
134 btrfs_set_inode_uid(leaf, inode_item, 0);
135 btrfs_set_inode_gid(leaf, inode_item, 0);
136 btrfs_set_inode_mode(leaf, inode_item, S_IFREG | 0600);
137 btrfs_set_inode_flags(leaf, inode_item, BTRFS_INODE_NOCOMPRESS |
138 BTRFS_INODE_PREALLOC | BTRFS_INODE_NODATASUM);
139 btrfs_set_inode_nlink(leaf, inode_item, 1);
140 btrfs_set_inode_transid(leaf, inode_item, trans->transid);
Li Zefan0414efa2011-04-20 10:20:14 +0800141 btrfs_set_inode_block_group(leaf, inode_item, offset);
Josef Bacik0af3d002010-06-21 14:48:16 -0400142 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +0200143 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -0400144
145 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
Li Zefan0414efa2011-04-20 10:20:14 +0800146 key.offset = offset;
Josef Bacik0af3d002010-06-21 14:48:16 -0400147 key.type = 0;
148
149 ret = btrfs_insert_empty_item(trans, root, path, &key,
150 sizeof(struct btrfs_free_space_header));
151 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200152 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -0400153 return ret;
154 }
155 leaf = path->nodes[0];
156 header = btrfs_item_ptr(leaf, path->slots[0],
157 struct btrfs_free_space_header);
158 memset_extent_buffer(leaf, 0, (unsigned long)header, sizeof(*header));
159 btrfs_set_free_space_key(leaf, header, &disk_key);
160 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +0200161 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -0400162
163 return 0;
164}
165
Li Zefan0414efa2011-04-20 10:20:14 +0800166int create_free_space_inode(struct btrfs_root *root,
167 struct btrfs_trans_handle *trans,
168 struct btrfs_block_group_cache *block_group,
169 struct btrfs_path *path)
170{
171 int ret;
172 u64 ino;
173
174 ret = btrfs_find_free_objectid(root, &ino);
175 if (ret < 0)
176 return ret;
177
178 return __create_free_space_inode(root, trans, path, ino,
179 block_group->key.objectid);
180}
181
Josef Bacik0af3d002010-06-21 14:48:16 -0400182int btrfs_truncate_free_space_cache(struct btrfs_root *root,
183 struct btrfs_trans_handle *trans,
184 struct btrfs_path *path,
185 struct inode *inode)
186{
187 loff_t oldsize;
188 int ret = 0;
189
190 trans->block_rsv = root->orphan_block_rsv;
191 ret = btrfs_block_rsv_check(trans, root,
192 root->orphan_block_rsv,
193 0, 5);
194 if (ret)
195 return ret;
196
197 oldsize = i_size_read(inode);
198 btrfs_i_size_write(inode, 0);
199 truncate_pagecache(inode, oldsize, 0);
200
201 /*
202 * We don't need an orphan item because truncating the free space cache
203 * will never be split across transactions.
204 */
205 ret = btrfs_truncate_inode_items(trans, root, inode,
206 0, BTRFS_EXTENT_DATA_KEY);
207 if (ret) {
208 WARN_ON(1);
209 return ret;
210 }
211
Li Zefan82d59022011-04-20 10:33:24 +0800212 ret = btrfs_update_inode(trans, root, inode);
213 return ret;
Josef Bacik0af3d002010-06-21 14:48:16 -0400214}
215
Josef Bacik9d66e232010-08-25 16:54:15 -0400216static int readahead_cache(struct inode *inode)
217{
218 struct file_ra_state *ra;
219 unsigned long last_index;
220
221 ra = kzalloc(sizeof(*ra), GFP_NOFS);
222 if (!ra)
223 return -ENOMEM;
224
225 file_ra_state_init(ra, inode->i_mapping);
226 last_index = (i_size_read(inode) - 1) >> PAGE_CACHE_SHIFT;
227
228 page_cache_sync_readahead(inode->i_mapping, ra, NULL, 0, last_index);
229
230 kfree(ra);
231
232 return 0;
233}
234
Li Zefan0414efa2011-04-20 10:20:14 +0800235int __load_free_space_cache(struct btrfs_root *root, struct inode *inode,
236 struct btrfs_free_space_ctl *ctl,
237 struct btrfs_path *path, u64 offset)
Josef Bacik9d66e232010-08-25 16:54:15 -0400238{
Josef Bacik9d66e232010-08-25 16:54:15 -0400239 struct btrfs_free_space_header *header;
240 struct extent_buffer *leaf;
241 struct page *page;
Josef Bacik9d66e232010-08-25 16:54:15 -0400242 u32 *checksums = NULL, *crc;
243 char *disk_crcs = NULL;
244 struct btrfs_key key;
245 struct list_head bitmaps;
246 u64 num_entries;
247 u64 num_bitmaps;
248 u64 generation;
249 u32 cur_crc = ~(u32)0;
250 pgoff_t index = 0;
251 unsigned long first_page_offset;
252 int num_checksums;
Li Zefan0414efa2011-04-20 10:20:14 +0800253 int ret = 0, ret2;
Josef Bacik9d66e232010-08-25 16:54:15 -0400254
255 INIT_LIST_HEAD(&bitmaps);
256
Josef Bacik9d66e232010-08-25 16:54:15 -0400257 /* Nothing in the space cache, goodbye */
Li Zefan0414efa2011-04-20 10:20:14 +0800258 if (!i_size_read(inode))
Josef Bacik9d66e232010-08-25 16:54:15 -0400259 goto out;
Josef Bacik9d66e232010-08-25 16:54:15 -0400260
261 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
Li Zefan0414efa2011-04-20 10:20:14 +0800262 key.offset = offset;
Josef Bacik9d66e232010-08-25 16:54:15 -0400263 key.type = 0;
264
265 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Li Zefan0414efa2011-04-20 10:20:14 +0800266 if (ret < 0)
267 goto out;
268 else if (ret > 0) {
Chris Mason945d8962011-05-22 12:33:42 -0400269 btrfs_release_path(path);
Li Zefan0414efa2011-04-20 10:20:14 +0800270 ret = 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400271 goto out;
272 }
273
Li Zefan0414efa2011-04-20 10:20:14 +0800274 ret = -1;
275
Josef Bacik9d66e232010-08-25 16:54:15 -0400276 leaf = path->nodes[0];
277 header = btrfs_item_ptr(leaf, path->slots[0],
278 struct btrfs_free_space_header);
279 num_entries = btrfs_free_space_entries(leaf, header);
280 num_bitmaps = btrfs_free_space_bitmaps(leaf, header);
281 generation = btrfs_free_space_generation(leaf, header);
Chris Mason945d8962011-05-22 12:33:42 -0400282 btrfs_release_path(path);
Josef Bacik9d66e232010-08-25 16:54:15 -0400283
284 if (BTRFS_I(inode)->generation != generation) {
285 printk(KERN_ERR "btrfs: free space inode generation (%llu) did"
Li Zefan0414efa2011-04-20 10:20:14 +0800286 " not match free space cache generation (%llu)\n",
Josef Bacik9d66e232010-08-25 16:54:15 -0400287 (unsigned long long)BTRFS_I(inode)->generation,
Li Zefan0414efa2011-04-20 10:20:14 +0800288 (unsigned long long)generation);
289 goto out;
Josef Bacik9d66e232010-08-25 16:54:15 -0400290 }
291
292 if (!num_entries)
293 goto out;
294
295 /* Setup everything for doing checksumming */
296 num_checksums = i_size_read(inode) / PAGE_CACHE_SIZE;
297 checksums = crc = kzalloc(sizeof(u32) * num_checksums, GFP_NOFS);
298 if (!checksums)
299 goto out;
300 first_page_offset = (sizeof(u32) * num_checksums) + sizeof(u64);
301 disk_crcs = kzalloc(first_page_offset, GFP_NOFS);
302 if (!disk_crcs)
303 goto out;
304
305 ret = readahead_cache(inode);
Li Zefan0414efa2011-04-20 10:20:14 +0800306 if (ret)
Josef Bacik9d66e232010-08-25 16:54:15 -0400307 goto out;
Josef Bacik9d66e232010-08-25 16:54:15 -0400308
309 while (1) {
310 struct btrfs_free_space_entry *entry;
311 struct btrfs_free_space *e;
312 void *addr;
313 unsigned long offset = 0;
314 unsigned long start_offset = 0;
315 int need_loop = 0;
316
317 if (!num_entries && !num_bitmaps)
318 break;
319
320 if (index == 0) {
321 start_offset = first_page_offset;
322 offset = start_offset;
323 }
324
325 page = grab_cache_page(inode->i_mapping, index);
Li Zefan0414efa2011-04-20 10:20:14 +0800326 if (!page)
Josef Bacik9d66e232010-08-25 16:54:15 -0400327 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400328
329 if (!PageUptodate(page)) {
330 btrfs_readpage(NULL, page);
331 lock_page(page);
332 if (!PageUptodate(page)) {
333 unlock_page(page);
334 page_cache_release(page);
335 printk(KERN_ERR "btrfs: error reading free "
Li Zefan0414efa2011-04-20 10:20:14 +0800336 "space cache\n");
Josef Bacik9d66e232010-08-25 16:54:15 -0400337 goto free_cache;
338 }
339 }
340 addr = kmap(page);
341
342 if (index == 0) {
343 u64 *gen;
344
345 memcpy(disk_crcs, addr, first_page_offset);
346 gen = addr + (sizeof(u32) * num_checksums);
347 if (*gen != BTRFS_I(inode)->generation) {
348 printk(KERN_ERR "btrfs: space cache generation"
Li Zefan0414efa2011-04-20 10:20:14 +0800349 " (%llu) does not match inode (%llu)\n",
Josef Bacik9d66e232010-08-25 16:54:15 -0400350 (unsigned long long)*gen,
351 (unsigned long long)
Li Zefan0414efa2011-04-20 10:20:14 +0800352 BTRFS_I(inode)->generation);
Josef Bacik9d66e232010-08-25 16:54:15 -0400353 kunmap(page);
354 unlock_page(page);
355 page_cache_release(page);
356 goto free_cache;
357 }
358 crc = (u32 *)disk_crcs;
359 }
360 entry = addr + start_offset;
361
362 /* First lets check our crc before we do anything fun */
363 cur_crc = ~(u32)0;
364 cur_crc = btrfs_csum_data(root, addr + start_offset, cur_crc,
365 PAGE_CACHE_SIZE - start_offset);
366 btrfs_csum_final(cur_crc, (char *)&cur_crc);
367 if (cur_crc != *crc) {
Li Zefan0414efa2011-04-20 10:20:14 +0800368 printk(KERN_ERR "btrfs: crc mismatch for page %lu\n",
369 index);
Josef Bacik9d66e232010-08-25 16:54:15 -0400370 kunmap(page);
371 unlock_page(page);
372 page_cache_release(page);
373 goto free_cache;
374 }
375 crc++;
376
377 while (1) {
378 if (!num_entries)
379 break;
380
381 need_loop = 1;
Josef Bacikdc89e982011-01-28 17:05:48 -0500382 e = kmem_cache_zalloc(btrfs_free_space_cachep,
383 GFP_NOFS);
Josef Bacik9d66e232010-08-25 16:54:15 -0400384 if (!e) {
385 kunmap(page);
386 unlock_page(page);
387 page_cache_release(page);
388 goto free_cache;
389 }
390
391 e->offset = le64_to_cpu(entry->offset);
392 e->bytes = le64_to_cpu(entry->bytes);
393 if (!e->bytes) {
394 kunmap(page);
Josef Bacikdc89e982011-01-28 17:05:48 -0500395 kmem_cache_free(btrfs_free_space_cachep, e);
Josef Bacik9d66e232010-08-25 16:54:15 -0400396 unlock_page(page);
397 page_cache_release(page);
398 goto free_cache;
399 }
400
401 if (entry->type == BTRFS_FREE_SPACE_EXTENT) {
Li Zefan34d52cb2011-03-29 13:46:06 +0800402 spin_lock(&ctl->tree_lock);
403 ret = link_free_space(ctl, e);
404 spin_unlock(&ctl->tree_lock);
Josef Bacik207dde82011-05-13 14:49:23 -0400405 if (ret) {
406 printk(KERN_ERR "Duplicate entries in "
407 "free space cache, dumping\n");
408 kunmap(page);
409 unlock_page(page);
410 page_cache_release(page);
411 goto free_cache;
412 }
Josef Bacik9d66e232010-08-25 16:54:15 -0400413 } else {
414 e->bitmap = kzalloc(PAGE_CACHE_SIZE, GFP_NOFS);
415 if (!e->bitmap) {
416 kunmap(page);
Josef Bacikdc89e982011-01-28 17:05:48 -0500417 kmem_cache_free(
418 btrfs_free_space_cachep, e);
Josef Bacik9d66e232010-08-25 16:54:15 -0400419 unlock_page(page);
420 page_cache_release(page);
421 goto free_cache;
422 }
Li Zefan34d52cb2011-03-29 13:46:06 +0800423 spin_lock(&ctl->tree_lock);
Li Zefan0414efa2011-04-20 10:20:14 +0800424 ret2 = link_free_space(ctl, e);
Li Zefan34d52cb2011-03-29 13:46:06 +0800425 ctl->total_bitmaps++;
426 ctl->op->recalc_thresholds(ctl);
427 spin_unlock(&ctl->tree_lock);
Josef Bacik9d66e232010-08-25 16:54:15 -0400428 list_add_tail(&e->list, &bitmaps);
Josef Bacik207dde82011-05-13 14:49:23 -0400429 if (ret) {
430 printk(KERN_ERR "Duplicate entries in "
431 "free space cache, dumping\n");
432 kunmap(page);
433 unlock_page(page);
434 page_cache_release(page);
435 goto free_cache;
436 }
Josef Bacik9d66e232010-08-25 16:54:15 -0400437 }
438
439 num_entries--;
440 offset += sizeof(struct btrfs_free_space_entry);
441 if (offset + sizeof(struct btrfs_free_space_entry) >=
442 PAGE_CACHE_SIZE)
443 break;
444 entry++;
445 }
446
447 /*
448 * We read an entry out of this page, we need to move on to the
449 * next page.
450 */
451 if (need_loop) {
452 kunmap(page);
453 goto next;
454 }
455
456 /*
457 * We add the bitmaps at the end of the entries in order that
458 * the bitmap entries are added to the cache.
459 */
460 e = list_entry(bitmaps.next, struct btrfs_free_space, list);
461 list_del_init(&e->list);
462 memcpy(e->bitmap, addr, PAGE_CACHE_SIZE);
463 kunmap(page);
464 num_bitmaps--;
465next:
466 unlock_page(page);
467 page_cache_release(page);
468 index++;
469 }
470
471 ret = 1;
472out:
473 kfree(checksums);
474 kfree(disk_crcs);
Josef Bacik9d66e232010-08-25 16:54:15 -0400475 return ret;
Josef Bacik9d66e232010-08-25 16:54:15 -0400476free_cache:
Li Zefan0414efa2011-04-20 10:20:14 +0800477 __btrfs_remove_free_space_cache(ctl);
Josef Bacik9d66e232010-08-25 16:54:15 -0400478 goto out;
479}
480
Li Zefan0414efa2011-04-20 10:20:14 +0800481int load_free_space_cache(struct btrfs_fs_info *fs_info,
482 struct btrfs_block_group_cache *block_group)
Josef Bacik0cb59c92010-07-02 12:14:14 -0400483{
Li Zefan34d52cb2011-03-29 13:46:06 +0800484 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Li Zefan0414efa2011-04-20 10:20:14 +0800485 struct btrfs_root *root = fs_info->tree_root;
486 struct inode *inode;
487 struct btrfs_path *path;
488 int ret;
489 bool matched;
490 u64 used = btrfs_block_group_used(&block_group->item);
491
492 /*
493 * If we're unmounting then just return, since this does a search on the
494 * normal root and not the commit root and we could deadlock.
495 */
496 smp_mb();
497 if (fs_info->closing)
498 return 0;
499
500 /*
501 * If this block group has been marked to be cleared for one reason or
502 * another then we can't trust the on disk cache, so just return.
503 */
504 spin_lock(&block_group->lock);
505 if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) {
506 spin_unlock(&block_group->lock);
507 return 0;
508 }
509 spin_unlock(&block_group->lock);
510
511 path = btrfs_alloc_path();
512 if (!path)
513 return 0;
514
515 inode = lookup_free_space_inode(root, block_group, path);
516 if (IS_ERR(inode)) {
517 btrfs_free_path(path);
518 return 0;
519 }
520
521 ret = __load_free_space_cache(fs_info->tree_root, inode, ctl,
522 path, block_group->key.objectid);
523 btrfs_free_path(path);
524 if (ret <= 0)
525 goto out;
526
527 spin_lock(&ctl->tree_lock);
528 matched = (ctl->free_space == (block_group->key.offset - used -
529 block_group->bytes_super));
530 spin_unlock(&ctl->tree_lock);
531
532 if (!matched) {
533 __btrfs_remove_free_space_cache(ctl);
534 printk(KERN_ERR "block group %llu has an wrong amount of free "
535 "space\n", block_group->key.objectid);
536 ret = -1;
537 }
538out:
539 if (ret < 0) {
540 /* This cache is bogus, make sure it gets cleared */
541 spin_lock(&block_group->lock);
542 block_group->disk_cache_state = BTRFS_DC_CLEAR;
543 spin_unlock(&block_group->lock);
Li Zefan82d59022011-04-20 10:33:24 +0800544 ret = 0;
Li Zefan0414efa2011-04-20 10:20:14 +0800545
546 printk(KERN_ERR "btrfs: failed to load free space cache "
547 "for block group %llu\n", block_group->key.objectid);
548 }
549
550 iput(inode);
551 return ret;
552}
553
554int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode,
555 struct btrfs_free_space_ctl *ctl,
556 struct btrfs_block_group_cache *block_group,
557 struct btrfs_trans_handle *trans,
558 struct btrfs_path *path, u64 offset)
Josef Bacik0cb59c92010-07-02 12:14:14 -0400559{
560 struct btrfs_free_space_header *header;
561 struct extent_buffer *leaf;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400562 struct rb_node *node;
563 struct list_head *pos, *n;
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400564 struct page **pages;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400565 struct page *page;
566 struct extent_state *cached_state = NULL;
Josef Bacik43be2142011-04-01 14:55:00 +0000567 struct btrfs_free_cluster *cluster = NULL;
568 struct extent_io_tree *unpin = NULL;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400569 struct list_head bitmap_list;
570 struct btrfs_key key;
Josef Bacik43be2142011-04-01 14:55:00 +0000571 u64 start, end, len;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400572 u64 bytes = 0;
573 u32 *crc, *checksums;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400574 unsigned long first_page_offset;
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400575 int index = 0, num_pages = 0;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400576 int entries = 0;
577 int bitmaps = 0;
Li Zefan0414efa2011-04-20 10:20:14 +0800578 int ret = -1;
Josef Bacik43be2142011-04-01 14:55:00 +0000579 bool next_page = false;
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400580 bool out_of_space = false;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400581
Josef Bacik0cb59c92010-07-02 12:14:14 -0400582 INIT_LIST_HEAD(&bitmap_list);
583
Li Zefan34d52cb2011-03-29 13:46:06 +0800584 node = rb_first(&ctl->free_space_offset);
Li Zefan0414efa2011-04-20 10:20:14 +0800585 if (!node)
Josef Bacik0cb59c92010-07-02 12:14:14 -0400586 return 0;
587
Li Zefan0414efa2011-04-20 10:20:14 +0800588 if (!i_size_read(inode))
589 return -1;
Josef Bacik2b209822010-12-03 13:17:53 -0500590
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400591 num_pages = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
592 PAGE_CACHE_SHIFT;
Chris Mason211f96c2011-06-03 01:26:53 -0400593
594 /* Since the first page has all of our checksums and our generation we
595 * need to calculate the offset into the page that we can start writing
596 * our entries.
597 */
598 first_page_offset = (sizeof(u32) * num_pages) + sizeof(u64);
599
Josef Bacik0cb59c92010-07-02 12:14:14 -0400600 filemap_write_and_wait(inode->i_mapping);
601 btrfs_wait_ordered_range(inode, inode->i_size &
602 ~(root->sectorsize - 1), (u64)-1);
603
Chris Mason211f96c2011-06-03 01:26:53 -0400604 /* make sure we don't overflow that first page */
605 if (first_page_offset + sizeof(struct btrfs_free_space_entry) >= PAGE_CACHE_SIZE) {
606 /* this is really the same as running out of space, where we also return 0 */
607 printk(KERN_CRIT "Btrfs: free space cache was too big for the crc page\n");
608 ret = 0;
609 goto out_update;
610 }
611
Josef Bacik0cb59c92010-07-02 12:14:14 -0400612 /* We need a checksum per page. */
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400613 crc = checksums = kzalloc(sizeof(u32) * num_pages, GFP_NOFS);
Li Zefan0414efa2011-04-20 10:20:14 +0800614 if (!crc)
615 return -1;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400616
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400617 pages = kzalloc(sizeof(struct page *) * num_pages, GFP_NOFS);
618 if (!pages) {
619 kfree(crc);
Li Zefan0414efa2011-04-20 10:20:14 +0800620 return -1;
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400621 }
622
Josef Bacik43be2142011-04-01 14:55:00 +0000623 /* Get the cluster for this block_group if it exists */
Li Zefan0414efa2011-04-20 10:20:14 +0800624 if (block_group && !list_empty(&block_group->cluster_list))
Josef Bacik43be2142011-04-01 14:55:00 +0000625 cluster = list_entry(block_group->cluster_list.next,
626 struct btrfs_free_cluster,
627 block_group_list);
628
629 /*
630 * We shouldn't have switched the pinned extents yet so this is the
631 * right one
632 */
633 unpin = root->fs_info->pinned_extents;
634
Josef Bacik0cb59c92010-07-02 12:14:14 -0400635 /*
636 * Lock all pages first so we can lock the extent safely.
637 *
638 * NOTE: Because we hold the ref the entire time we're going to write to
639 * the page find_get_page should never fail, so we don't do a check
640 * after find_get_page at this point. Just putting this here so people
641 * know and don't freak out.
642 */
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400643 while (index < num_pages) {
Josef Bacik0cb59c92010-07-02 12:14:14 -0400644 page = grab_cache_page(inode->i_mapping, index);
645 if (!page) {
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400646 int i;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400647
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400648 for (i = 0; i < num_pages; i++) {
649 unlock_page(pages[i]);
650 page_cache_release(pages[i]);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400651 }
652 goto out_free;
653 }
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400654 pages[index] = page;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400655 index++;
656 }
657
658 index = 0;
659 lock_extent_bits(&BTRFS_I(inode)->io_tree, 0, i_size_read(inode) - 1,
660 0, &cached_state, GFP_NOFS);
661
Josef Bacik43be2142011-04-01 14:55:00 +0000662 /*
663 * When searching for pinned extents, we need to start at our start
664 * offset.
665 */
Li Zefan0414efa2011-04-20 10:20:14 +0800666 if (block_group)
667 start = block_group->key.objectid;
Josef Bacik43be2142011-04-01 14:55:00 +0000668
Josef Bacik0cb59c92010-07-02 12:14:14 -0400669 /* Write out the extent entries */
670 do {
671 struct btrfs_free_space_entry *entry;
672 void *addr;
673 unsigned long offset = 0;
674 unsigned long start_offset = 0;
675
Josef Bacik43be2142011-04-01 14:55:00 +0000676 next_page = false;
677
Josef Bacik0cb59c92010-07-02 12:14:14 -0400678 if (index == 0) {
679 start_offset = first_page_offset;
680 offset = start_offset;
681 }
682
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400683 if (index >= num_pages) {
684 out_of_space = true;
685 break;
686 }
687
688 page = pages[index];
Josef Bacik0cb59c92010-07-02 12:14:14 -0400689
690 addr = kmap(page);
691 entry = addr + start_offset;
692
693 memset(addr, 0, PAGE_CACHE_SIZE);
Josef Bacik43be2142011-04-01 14:55:00 +0000694 while (node && !next_page) {
Josef Bacik0cb59c92010-07-02 12:14:14 -0400695 struct btrfs_free_space *e;
696
697 e = rb_entry(node, struct btrfs_free_space, offset_index);
698 entries++;
699
700 entry->offset = cpu_to_le64(e->offset);
701 entry->bytes = cpu_to_le64(e->bytes);
702 if (e->bitmap) {
703 entry->type = BTRFS_FREE_SPACE_BITMAP;
704 list_add_tail(&e->list, &bitmap_list);
705 bitmaps++;
706 } else {
707 entry->type = BTRFS_FREE_SPACE_EXTENT;
708 }
709 node = rb_next(node);
Josef Bacik43be2142011-04-01 14:55:00 +0000710 if (!node && cluster) {
711 node = rb_first(&cluster->root);
712 cluster = NULL;
713 }
Josef Bacik0cb59c92010-07-02 12:14:14 -0400714 offset += sizeof(struct btrfs_free_space_entry);
715 if (offset + sizeof(struct btrfs_free_space_entry) >=
716 PAGE_CACHE_SIZE)
Josef Bacik43be2142011-04-01 14:55:00 +0000717 next_page = true;
718 entry++;
719 }
720
721 /*
722 * We want to add any pinned extents to our free space cache
723 * so we don't leak the space
724 */
Li Zefan0414efa2011-04-20 10:20:14 +0800725 while (block_group && !next_page &&
726 (start < block_group->key.objectid +
727 block_group->key.offset)) {
Josef Bacik43be2142011-04-01 14:55:00 +0000728 ret = find_first_extent_bit(unpin, start, &start, &end,
729 EXTENT_DIRTY);
730 if (ret) {
731 ret = 0;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400732 break;
Josef Bacik43be2142011-04-01 14:55:00 +0000733 }
734
735 /* This pinned extent is out of our range */
736 if (start >= block_group->key.objectid +
737 block_group->key.offset)
738 break;
739
740 len = block_group->key.objectid +
741 block_group->key.offset - start;
742 len = min(len, end + 1 - start);
743
744 entries++;
745 entry->offset = cpu_to_le64(start);
746 entry->bytes = cpu_to_le64(len);
747 entry->type = BTRFS_FREE_SPACE_EXTENT;
748
749 start = end + 1;
750 offset += sizeof(struct btrfs_free_space_entry);
751 if (offset + sizeof(struct btrfs_free_space_entry) >=
752 PAGE_CACHE_SIZE)
753 next_page = true;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400754 entry++;
755 }
756 *crc = ~(u32)0;
757 *crc = btrfs_csum_data(root, addr + start_offset, *crc,
758 PAGE_CACHE_SIZE - start_offset);
759 kunmap(page);
760
761 btrfs_csum_final(*crc, (char *)crc);
762 crc++;
763
764 bytes += PAGE_CACHE_SIZE;
765
Josef Bacik0cb59c92010-07-02 12:14:14 -0400766 index++;
Josef Bacik43be2142011-04-01 14:55:00 +0000767 } while (node || next_page);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400768
769 /* Write out the bitmaps */
770 list_for_each_safe(pos, n, &bitmap_list) {
771 void *addr;
772 struct btrfs_free_space *entry =
773 list_entry(pos, struct btrfs_free_space, list);
774
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400775 if (index >= num_pages) {
776 out_of_space = true;
777 break;
778 }
Chris Masonf65647c2011-04-18 08:55:34 -0400779 page = pages[index];
Josef Bacik0cb59c92010-07-02 12:14:14 -0400780
781 addr = kmap(page);
782 memcpy(addr, entry->bitmap, PAGE_CACHE_SIZE);
783 *crc = ~(u32)0;
784 *crc = btrfs_csum_data(root, addr, *crc, PAGE_CACHE_SIZE);
785 kunmap(page);
786 btrfs_csum_final(*crc, (char *)crc);
787 crc++;
788 bytes += PAGE_CACHE_SIZE;
789
Josef Bacik0cb59c92010-07-02 12:14:14 -0400790 list_del_init(&entry->list);
791 index++;
792 }
793
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400794 if (out_of_space) {
795 btrfs_drop_pages(pages, num_pages);
796 unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
797 i_size_read(inode) - 1, &cached_state,
798 GFP_NOFS);
799 ret = 0;
800 goto out_free;
801 }
802
Josef Bacik0cb59c92010-07-02 12:14:14 -0400803 /* Zero out the rest of the pages just to make sure */
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400804 while (index < num_pages) {
Josef Bacik0cb59c92010-07-02 12:14:14 -0400805 void *addr;
806
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400807 page = pages[index];
Josef Bacik0cb59c92010-07-02 12:14:14 -0400808 addr = kmap(page);
809 memset(addr, 0, PAGE_CACHE_SIZE);
810 kunmap(page);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400811 bytes += PAGE_CACHE_SIZE;
812 index++;
813 }
814
Josef Bacik0cb59c92010-07-02 12:14:14 -0400815 /* Write the checksums and trans id to the first page */
816 {
817 void *addr;
818 u64 *gen;
819
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400820 page = pages[0];
Josef Bacik0cb59c92010-07-02 12:14:14 -0400821
822 addr = kmap(page);
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400823 memcpy(addr, checksums, sizeof(u32) * num_pages);
824 gen = addr + (sizeof(u32) * num_pages);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400825 *gen = trans->transid;
826 kunmap(page);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400827 }
Josef Bacik0cb59c92010-07-02 12:14:14 -0400828
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400829 ret = btrfs_dirty_pages(root, inode, pages, num_pages, 0,
830 bytes, &cached_state);
831 btrfs_drop_pages(pages, num_pages);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400832 unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
833 i_size_read(inode) - 1, &cached_state, GFP_NOFS);
834
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400835 if (ret) {
836 ret = 0;
837 goto out_free;
838 }
839
840 BTRFS_I(inode)->generation = trans->transid;
841
Josef Bacik0cb59c92010-07-02 12:14:14 -0400842 filemap_write_and_wait(inode->i_mapping);
843
844 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
Li Zefan0414efa2011-04-20 10:20:14 +0800845 key.offset = offset;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400846 key.type = 0;
847
848 ret = btrfs_search_slot(trans, root, &key, path, 1, 1);
849 if (ret < 0) {
Li Zefan0414efa2011-04-20 10:20:14 +0800850 ret = -1;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400851 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, bytes - 1,
852 EXTENT_DIRTY | EXTENT_DELALLOC |
853 EXTENT_DO_ACCOUNTING, 0, 0, NULL, GFP_NOFS);
854 goto out_free;
855 }
856 leaf = path->nodes[0];
857 if (ret > 0) {
858 struct btrfs_key found_key;
859 BUG_ON(!path->slots[0]);
860 path->slots[0]--;
861 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
862 if (found_key.objectid != BTRFS_FREE_SPACE_OBJECTID ||
Li Zefan0414efa2011-04-20 10:20:14 +0800863 found_key.offset != offset) {
864 ret = -1;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400865 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, bytes - 1,
866 EXTENT_DIRTY | EXTENT_DELALLOC |
867 EXTENT_DO_ACCOUNTING, 0, 0, NULL,
868 GFP_NOFS);
David Sterbab3b4aa72011-04-21 01:20:15 +0200869 btrfs_release_path(path);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400870 goto out_free;
871 }
872 }
873 header = btrfs_item_ptr(leaf, path->slots[0],
874 struct btrfs_free_space_header);
875 btrfs_set_free_space_entries(leaf, header, entries);
876 btrfs_set_free_space_bitmaps(leaf, header, bitmaps);
877 btrfs_set_free_space_generation(leaf, header, trans->transid);
878 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +0200879 btrfs_release_path(path);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400880
881 ret = 1;
882
883out_free:
Chris Mason211f96c2011-06-03 01:26:53 -0400884 kfree(checksums);
885 kfree(pages);
886
887out_update:
Li Zefan0414efa2011-04-20 10:20:14 +0800888 if (ret != 1) {
Josef Bacik0cb59c92010-07-02 12:14:14 -0400889 invalidate_inode_pages2_range(inode->i_mapping, 0, index);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400890 BTRFS_I(inode)->generation = 0;
891 }
Josef Bacik0cb59c92010-07-02 12:14:14 -0400892 btrfs_update_inode(trans, root, inode);
Li Zefan0414efa2011-04-20 10:20:14 +0800893 return ret;
894}
895
896int btrfs_write_out_cache(struct btrfs_root *root,
897 struct btrfs_trans_handle *trans,
898 struct btrfs_block_group_cache *block_group,
899 struct btrfs_path *path)
900{
901 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
902 struct inode *inode;
903 int ret = 0;
904
905 root = root->fs_info->tree_root;
906
907 spin_lock(&block_group->lock);
908 if (block_group->disk_cache_state < BTRFS_DC_SETUP) {
909 spin_unlock(&block_group->lock);
910 return 0;
911 }
912 spin_unlock(&block_group->lock);
913
914 inode = lookup_free_space_inode(root, block_group, path);
915 if (IS_ERR(inode))
916 return 0;
917
918 ret = __btrfs_write_out_cache(root, inode, ctl, block_group, trans,
919 path, block_group->key.objectid);
920 if (ret < 0) {
921 spin_lock(&block_group->lock);
922 block_group->disk_cache_state = BTRFS_DC_ERROR;
923 spin_unlock(&block_group->lock);
Li Zefan82d59022011-04-20 10:33:24 +0800924 ret = 0;
Li Zefan0414efa2011-04-20 10:20:14 +0800925
926 printk(KERN_ERR "btrfs: failed to write free space cace "
927 "for block group %llu\n", block_group->key.objectid);
928 }
929
Josef Bacik0cb59c92010-07-02 12:14:14 -0400930 iput(inode);
931 return ret;
932}
933
Li Zefan34d52cb2011-03-29 13:46:06 +0800934static inline unsigned long offset_to_bit(u64 bitmap_start, u32 unit,
Josef Bacik96303082009-07-13 21:29:25 -0400935 u64 offset)
936{
937 BUG_ON(offset < bitmap_start);
938 offset -= bitmap_start;
Li Zefan34d52cb2011-03-29 13:46:06 +0800939 return (unsigned long)(div_u64(offset, unit));
Josef Bacik96303082009-07-13 21:29:25 -0400940}
941
Li Zefan34d52cb2011-03-29 13:46:06 +0800942static inline unsigned long bytes_to_bits(u64 bytes, u32 unit)
Josef Bacik96303082009-07-13 21:29:25 -0400943{
Li Zefan34d52cb2011-03-29 13:46:06 +0800944 return (unsigned long)(div_u64(bytes, unit));
Josef Bacik96303082009-07-13 21:29:25 -0400945}
946
Li Zefan34d52cb2011-03-29 13:46:06 +0800947static inline u64 offset_to_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -0400948 u64 offset)
949{
950 u64 bitmap_start;
951 u64 bytes_per_bitmap;
952
Li Zefan34d52cb2011-03-29 13:46:06 +0800953 bytes_per_bitmap = BITS_PER_BITMAP * ctl->unit;
954 bitmap_start = offset - ctl->start;
Josef Bacik96303082009-07-13 21:29:25 -0400955 bitmap_start = div64_u64(bitmap_start, bytes_per_bitmap);
956 bitmap_start *= bytes_per_bitmap;
Li Zefan34d52cb2011-03-29 13:46:06 +0800957 bitmap_start += ctl->start;
Josef Bacik96303082009-07-13 21:29:25 -0400958
959 return bitmap_start;
960}
Josef Bacik0f9dd462008-09-23 13:14:11 -0400961
962static int tree_insert_offset(struct rb_root *root, u64 offset,
Josef Bacik96303082009-07-13 21:29:25 -0400963 struct rb_node *node, int bitmap)
Josef Bacik0f9dd462008-09-23 13:14:11 -0400964{
965 struct rb_node **p = &root->rb_node;
966 struct rb_node *parent = NULL;
967 struct btrfs_free_space *info;
968
969 while (*p) {
970 parent = *p;
971 info = rb_entry(parent, struct btrfs_free_space, offset_index);
972
Josef Bacik96303082009-07-13 21:29:25 -0400973 if (offset < info->offset) {
Josef Bacik0f9dd462008-09-23 13:14:11 -0400974 p = &(*p)->rb_left;
Josef Bacik96303082009-07-13 21:29:25 -0400975 } else if (offset > info->offset) {
Josef Bacik0f9dd462008-09-23 13:14:11 -0400976 p = &(*p)->rb_right;
Josef Bacik96303082009-07-13 21:29:25 -0400977 } else {
978 /*
979 * we could have a bitmap entry and an extent entry
980 * share the same offset. If this is the case, we want
981 * the extent entry to always be found first if we do a
982 * linear search through the tree, since we want to have
983 * the quickest allocation time, and allocating from an
984 * extent is faster than allocating from a bitmap. So
985 * if we're inserting a bitmap and we find an entry at
986 * this offset, we want to go right, or after this entry
987 * logically. If we are inserting an extent and we've
988 * found a bitmap, we want to go left, or before
989 * logically.
990 */
991 if (bitmap) {
Josef Bacik207dde82011-05-13 14:49:23 -0400992 if (info->bitmap) {
993 WARN_ON_ONCE(1);
994 return -EEXIST;
995 }
Josef Bacik96303082009-07-13 21:29:25 -0400996 p = &(*p)->rb_right;
997 } else {
Josef Bacik207dde82011-05-13 14:49:23 -0400998 if (!info->bitmap) {
999 WARN_ON_ONCE(1);
1000 return -EEXIST;
1001 }
Josef Bacik96303082009-07-13 21:29:25 -04001002 p = &(*p)->rb_left;
1003 }
1004 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04001005 }
1006
1007 rb_link_node(node, parent, p);
1008 rb_insert_color(node, root);
1009
1010 return 0;
1011}
1012
1013/*
Josef Bacik70cb0742009-04-03 10:14:19 -04001014 * searches the tree for the given offset.
1015 *
Josef Bacik96303082009-07-13 21:29:25 -04001016 * fuzzy - If this is set, then we are trying to make an allocation, and we just
1017 * want a section that has at least bytes size and comes at or after the given
1018 * offset.
Josef Bacik0f9dd462008-09-23 13:14:11 -04001019 */
Josef Bacik96303082009-07-13 21:29:25 -04001020static struct btrfs_free_space *
Li Zefan34d52cb2011-03-29 13:46:06 +08001021tree_search_offset(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001022 u64 offset, int bitmap_only, int fuzzy)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001023{
Li Zefan34d52cb2011-03-29 13:46:06 +08001024 struct rb_node *n = ctl->free_space_offset.rb_node;
Josef Bacik96303082009-07-13 21:29:25 -04001025 struct btrfs_free_space *entry, *prev = NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001026
Josef Bacik96303082009-07-13 21:29:25 -04001027 /* find entry that is closest to the 'offset' */
1028 while (1) {
1029 if (!n) {
1030 entry = NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001031 break;
1032 }
Josef Bacik96303082009-07-13 21:29:25 -04001033
1034 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1035 prev = entry;
1036
1037 if (offset < entry->offset)
1038 n = n->rb_left;
1039 else if (offset > entry->offset)
1040 n = n->rb_right;
1041 else
1042 break;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001043 }
1044
Josef Bacik96303082009-07-13 21:29:25 -04001045 if (bitmap_only) {
1046 if (!entry)
1047 return NULL;
1048 if (entry->bitmap)
1049 return entry;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001050
Josef Bacik96303082009-07-13 21:29:25 -04001051 /*
1052 * bitmap entry and extent entry may share same offset,
1053 * in that case, bitmap entry comes after extent entry.
1054 */
1055 n = rb_next(n);
1056 if (!n)
1057 return NULL;
1058 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1059 if (entry->offset != offset)
1060 return NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001061
Josef Bacik96303082009-07-13 21:29:25 -04001062 WARN_ON(!entry->bitmap);
1063 return entry;
1064 } else if (entry) {
1065 if (entry->bitmap) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001066 /*
Josef Bacik96303082009-07-13 21:29:25 -04001067 * if previous extent entry covers the offset,
1068 * we should return it instead of the bitmap entry
Josef Bacik0f9dd462008-09-23 13:14:11 -04001069 */
Josef Bacik96303082009-07-13 21:29:25 -04001070 n = &entry->offset_index;
1071 while (1) {
1072 n = rb_prev(n);
1073 if (!n)
1074 break;
1075 prev = rb_entry(n, struct btrfs_free_space,
1076 offset_index);
1077 if (!prev->bitmap) {
1078 if (prev->offset + prev->bytes > offset)
1079 entry = prev;
1080 break;
1081 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04001082 }
Josef Bacik96303082009-07-13 21:29:25 -04001083 }
1084 return entry;
1085 }
1086
1087 if (!prev)
1088 return NULL;
1089
1090 /* find last entry before the 'offset' */
1091 entry = prev;
1092 if (entry->offset > offset) {
1093 n = rb_prev(&entry->offset_index);
1094 if (n) {
1095 entry = rb_entry(n, struct btrfs_free_space,
1096 offset_index);
1097 BUG_ON(entry->offset > offset);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001098 } else {
Josef Bacik96303082009-07-13 21:29:25 -04001099 if (fuzzy)
1100 return entry;
1101 else
1102 return NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001103 }
1104 }
1105
Josef Bacik96303082009-07-13 21:29:25 -04001106 if (entry->bitmap) {
1107 n = &entry->offset_index;
1108 while (1) {
1109 n = rb_prev(n);
1110 if (!n)
1111 break;
1112 prev = rb_entry(n, struct btrfs_free_space,
1113 offset_index);
1114 if (!prev->bitmap) {
1115 if (prev->offset + prev->bytes > offset)
1116 return prev;
1117 break;
1118 }
1119 }
Li Zefan34d52cb2011-03-29 13:46:06 +08001120 if (entry->offset + BITS_PER_BITMAP * ctl->unit > offset)
Josef Bacik96303082009-07-13 21:29:25 -04001121 return entry;
1122 } else if (entry->offset + entry->bytes > offset)
1123 return entry;
1124
1125 if (!fuzzy)
1126 return NULL;
1127
1128 while (1) {
1129 if (entry->bitmap) {
1130 if (entry->offset + BITS_PER_BITMAP *
Li Zefan34d52cb2011-03-29 13:46:06 +08001131 ctl->unit > offset)
Josef Bacik96303082009-07-13 21:29:25 -04001132 break;
1133 } else {
1134 if (entry->offset + entry->bytes > offset)
1135 break;
1136 }
1137
1138 n = rb_next(&entry->offset_index);
1139 if (!n)
1140 return NULL;
1141 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1142 }
1143 return entry;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001144}
1145
Li Zefanf333adb2010-11-09 14:57:39 +08001146static inline void
Li Zefan34d52cb2011-03-29 13:46:06 +08001147__unlink_free_space(struct btrfs_free_space_ctl *ctl,
Li Zefanf333adb2010-11-09 14:57:39 +08001148 struct btrfs_free_space *info)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001149{
Li Zefan34d52cb2011-03-29 13:46:06 +08001150 rb_erase(&info->offset_index, &ctl->free_space_offset);
1151 ctl->free_extents--;
Li Zefanf333adb2010-11-09 14:57:39 +08001152}
1153
Li Zefan34d52cb2011-03-29 13:46:06 +08001154static void unlink_free_space(struct btrfs_free_space_ctl *ctl,
Li Zefanf333adb2010-11-09 14:57:39 +08001155 struct btrfs_free_space *info)
1156{
Li Zefan34d52cb2011-03-29 13:46:06 +08001157 __unlink_free_space(ctl, info);
1158 ctl->free_space -= info->bytes;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001159}
1160
Li Zefan34d52cb2011-03-29 13:46:06 +08001161static int link_free_space(struct btrfs_free_space_ctl *ctl,
Josef Bacik0f9dd462008-09-23 13:14:11 -04001162 struct btrfs_free_space *info)
1163{
1164 int ret = 0;
1165
Josef Bacik96303082009-07-13 21:29:25 -04001166 BUG_ON(!info->bitmap && !info->bytes);
Li Zefan34d52cb2011-03-29 13:46:06 +08001167 ret = tree_insert_offset(&ctl->free_space_offset, info->offset,
Josef Bacik96303082009-07-13 21:29:25 -04001168 &info->offset_index, (info->bitmap != NULL));
Josef Bacik0f9dd462008-09-23 13:14:11 -04001169 if (ret)
1170 return ret;
1171
Li Zefan34d52cb2011-03-29 13:46:06 +08001172 ctl->free_space += info->bytes;
1173 ctl->free_extents++;
Josef Bacik96303082009-07-13 21:29:25 -04001174 return ret;
1175}
1176
Li Zefan34d52cb2011-03-29 13:46:06 +08001177static void recalculate_thresholds(struct btrfs_free_space_ctl *ctl)
Josef Bacik96303082009-07-13 21:29:25 -04001178{
Li Zefan34d52cb2011-03-29 13:46:06 +08001179 struct btrfs_block_group_cache *block_group = ctl->private;
Josef Bacik25891f72009-09-11 16:11:20 -04001180 u64 max_bytes;
1181 u64 bitmap_bytes;
1182 u64 extent_bytes;
Li Zefan8eb2d822010-11-09 14:48:01 +08001183 u64 size = block_group->key.offset;
Li Zefan34d52cb2011-03-29 13:46:06 +08001184 u64 bytes_per_bg = BITS_PER_BITMAP * block_group->sectorsize;
1185 int max_bitmaps = div64_u64(size + bytes_per_bg - 1, bytes_per_bg);
1186
1187 BUG_ON(ctl->total_bitmaps > max_bitmaps);
Josef Bacik96303082009-07-13 21:29:25 -04001188
1189 /*
1190 * The goal is to keep the total amount of memory used per 1gb of space
1191 * at or below 32k, so we need to adjust how much memory we allow to be
1192 * used by extent based free space tracking
1193 */
Li Zefan8eb2d822010-11-09 14:48:01 +08001194 if (size < 1024 * 1024 * 1024)
1195 max_bytes = MAX_CACHE_BYTES_PER_GIG;
1196 else
1197 max_bytes = MAX_CACHE_BYTES_PER_GIG *
1198 div64_u64(size, 1024 * 1024 * 1024);
Josef Bacik96303082009-07-13 21:29:25 -04001199
Josef Bacik25891f72009-09-11 16:11:20 -04001200 /*
1201 * we want to account for 1 more bitmap than what we have so we can make
1202 * sure we don't go over our overall goal of MAX_CACHE_BYTES_PER_GIG as
1203 * we add more bitmaps.
1204 */
Li Zefan34d52cb2011-03-29 13:46:06 +08001205 bitmap_bytes = (ctl->total_bitmaps + 1) * PAGE_CACHE_SIZE;
Josef Bacik96303082009-07-13 21:29:25 -04001206
Josef Bacik25891f72009-09-11 16:11:20 -04001207 if (bitmap_bytes >= max_bytes) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001208 ctl->extents_thresh = 0;
Josef Bacik25891f72009-09-11 16:11:20 -04001209 return;
Josef Bacik96303082009-07-13 21:29:25 -04001210 }
Josef Bacik25891f72009-09-11 16:11:20 -04001211
1212 /*
1213 * we want the extent entry threshold to always be at most 1/2 the maxw
1214 * bytes we can have, or whatever is less than that.
1215 */
1216 extent_bytes = max_bytes - bitmap_bytes;
1217 extent_bytes = min_t(u64, extent_bytes, div64_u64(max_bytes, 2));
1218
Li Zefan34d52cb2011-03-29 13:46:06 +08001219 ctl->extents_thresh =
Josef Bacik25891f72009-09-11 16:11:20 -04001220 div64_u64(extent_bytes, (sizeof(struct btrfs_free_space)));
Josef Bacik96303082009-07-13 21:29:25 -04001221}
1222
Li Zefan34d52cb2011-03-29 13:46:06 +08001223static void bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
Josef Bacik817d52f2009-07-13 21:29:25 -04001224 struct btrfs_free_space *info, u64 offset,
1225 u64 bytes)
Josef Bacik96303082009-07-13 21:29:25 -04001226{
Li Zefanf38b6e72011-03-14 13:40:51 +08001227 unsigned long start, count;
Josef Bacik96303082009-07-13 21:29:25 -04001228
Li Zefan34d52cb2011-03-29 13:46:06 +08001229 start = offset_to_bit(info->offset, ctl->unit, offset);
1230 count = bytes_to_bits(bytes, ctl->unit);
Li Zefanf38b6e72011-03-14 13:40:51 +08001231 BUG_ON(start + count > BITS_PER_BITMAP);
Josef Bacik96303082009-07-13 21:29:25 -04001232
Li Zefanf38b6e72011-03-14 13:40:51 +08001233 bitmap_clear(info->bitmap, start, count);
Josef Bacik96303082009-07-13 21:29:25 -04001234
1235 info->bytes -= bytes;
Li Zefan34d52cb2011-03-29 13:46:06 +08001236 ctl->free_space -= bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001237}
1238
Li Zefan34d52cb2011-03-29 13:46:06 +08001239static void bitmap_set_bits(struct btrfs_free_space_ctl *ctl,
Josef Bacik817d52f2009-07-13 21:29:25 -04001240 struct btrfs_free_space *info, u64 offset,
1241 u64 bytes)
Josef Bacik96303082009-07-13 21:29:25 -04001242{
Li Zefanf38b6e72011-03-14 13:40:51 +08001243 unsigned long start, count;
Josef Bacik96303082009-07-13 21:29:25 -04001244
Li Zefan34d52cb2011-03-29 13:46:06 +08001245 start = offset_to_bit(info->offset, ctl->unit, offset);
1246 count = bytes_to_bits(bytes, ctl->unit);
Li Zefanf38b6e72011-03-14 13:40:51 +08001247 BUG_ON(start + count > BITS_PER_BITMAP);
Josef Bacik96303082009-07-13 21:29:25 -04001248
Li Zefanf38b6e72011-03-14 13:40:51 +08001249 bitmap_set(info->bitmap, start, count);
Josef Bacik96303082009-07-13 21:29:25 -04001250
1251 info->bytes += bytes;
Li Zefan34d52cb2011-03-29 13:46:06 +08001252 ctl->free_space += bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001253}
1254
Li Zefan34d52cb2011-03-29 13:46:06 +08001255static int search_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001256 struct btrfs_free_space *bitmap_info, u64 *offset,
1257 u64 *bytes)
1258{
1259 unsigned long found_bits = 0;
1260 unsigned long bits, i;
1261 unsigned long next_zero;
1262
Li Zefan34d52cb2011-03-29 13:46:06 +08001263 i = offset_to_bit(bitmap_info->offset, ctl->unit,
Josef Bacik96303082009-07-13 21:29:25 -04001264 max_t(u64, *offset, bitmap_info->offset));
Li Zefan34d52cb2011-03-29 13:46:06 +08001265 bits = bytes_to_bits(*bytes, ctl->unit);
Josef Bacik96303082009-07-13 21:29:25 -04001266
1267 for (i = find_next_bit(bitmap_info->bitmap, BITS_PER_BITMAP, i);
1268 i < BITS_PER_BITMAP;
1269 i = find_next_bit(bitmap_info->bitmap, BITS_PER_BITMAP, i + 1)) {
1270 next_zero = find_next_zero_bit(bitmap_info->bitmap,
1271 BITS_PER_BITMAP, i);
1272 if ((next_zero - i) >= bits) {
1273 found_bits = next_zero - i;
1274 break;
1275 }
1276 i = next_zero;
1277 }
1278
1279 if (found_bits) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001280 *offset = (u64)(i * ctl->unit) + bitmap_info->offset;
1281 *bytes = (u64)(found_bits) * ctl->unit;
Josef Bacik96303082009-07-13 21:29:25 -04001282 return 0;
1283 }
1284
1285 return -1;
1286}
1287
Li Zefan34d52cb2011-03-29 13:46:06 +08001288static struct btrfs_free_space *
1289find_free_space(struct btrfs_free_space_ctl *ctl, u64 *offset, u64 *bytes)
Josef Bacik96303082009-07-13 21:29:25 -04001290{
1291 struct btrfs_free_space *entry;
1292 struct rb_node *node;
1293 int ret;
1294
Li Zefan34d52cb2011-03-29 13:46:06 +08001295 if (!ctl->free_space_offset.rb_node)
Josef Bacik96303082009-07-13 21:29:25 -04001296 return NULL;
1297
Li Zefan34d52cb2011-03-29 13:46:06 +08001298 entry = tree_search_offset(ctl, offset_to_bitmap(ctl, *offset), 0, 1);
Josef Bacik96303082009-07-13 21:29:25 -04001299 if (!entry)
1300 return NULL;
1301
1302 for (node = &entry->offset_index; node; node = rb_next(node)) {
1303 entry = rb_entry(node, struct btrfs_free_space, offset_index);
1304 if (entry->bytes < *bytes)
1305 continue;
1306
1307 if (entry->bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001308 ret = search_bitmap(ctl, entry, offset, bytes);
Josef Bacik96303082009-07-13 21:29:25 -04001309 if (!ret)
1310 return entry;
1311 continue;
1312 }
1313
1314 *offset = entry->offset;
1315 *bytes = entry->bytes;
1316 return entry;
1317 }
1318
1319 return NULL;
1320}
1321
Li Zefan34d52cb2011-03-29 13:46:06 +08001322static void add_new_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001323 struct btrfs_free_space *info, u64 offset)
1324{
Li Zefan34d52cb2011-03-29 13:46:06 +08001325 info->offset = offset_to_bitmap(ctl, offset);
Josef Bacikf019f422009-09-11 16:11:20 -04001326 info->bytes = 0;
Li Zefan34d52cb2011-03-29 13:46:06 +08001327 link_free_space(ctl, info);
1328 ctl->total_bitmaps++;
Josef Bacik96303082009-07-13 21:29:25 -04001329
Li Zefan34d52cb2011-03-29 13:46:06 +08001330 ctl->op->recalc_thresholds(ctl);
Josef Bacik96303082009-07-13 21:29:25 -04001331}
1332
Li Zefan34d52cb2011-03-29 13:46:06 +08001333static void free_bitmap(struct btrfs_free_space_ctl *ctl,
Li Zefanedf6e2d2010-11-09 14:50:07 +08001334 struct btrfs_free_space *bitmap_info)
1335{
Li Zefan34d52cb2011-03-29 13:46:06 +08001336 unlink_free_space(ctl, bitmap_info);
Li Zefanedf6e2d2010-11-09 14:50:07 +08001337 kfree(bitmap_info->bitmap);
Josef Bacikdc89e982011-01-28 17:05:48 -05001338 kmem_cache_free(btrfs_free_space_cachep, bitmap_info);
Li Zefan34d52cb2011-03-29 13:46:06 +08001339 ctl->total_bitmaps--;
1340 ctl->op->recalc_thresholds(ctl);
Li Zefanedf6e2d2010-11-09 14:50:07 +08001341}
1342
Li Zefan34d52cb2011-03-29 13:46:06 +08001343static noinline int remove_from_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001344 struct btrfs_free_space *bitmap_info,
1345 u64 *offset, u64 *bytes)
1346{
1347 u64 end;
Josef Bacik6606bb92009-07-31 11:03:58 -04001348 u64 search_start, search_bytes;
1349 int ret;
Josef Bacik96303082009-07-13 21:29:25 -04001350
1351again:
Li Zefan34d52cb2011-03-29 13:46:06 +08001352 end = bitmap_info->offset + (u64)(BITS_PER_BITMAP * ctl->unit) - 1;
Josef Bacik96303082009-07-13 21:29:25 -04001353
Josef Bacik6606bb92009-07-31 11:03:58 -04001354 /*
1355 * XXX - this can go away after a few releases.
1356 *
1357 * since the only user of btrfs_remove_free_space is the tree logging
1358 * stuff, and the only way to test that is under crash conditions, we
1359 * want to have this debug stuff here just in case somethings not
1360 * working. Search the bitmap for the space we are trying to use to
1361 * make sure its actually there. If its not there then we need to stop
1362 * because something has gone wrong.
1363 */
1364 search_start = *offset;
1365 search_bytes = *bytes;
Josef Bacik13dbc082011-02-03 02:39:52 +00001366 search_bytes = min(search_bytes, end - search_start + 1);
Li Zefan34d52cb2011-03-29 13:46:06 +08001367 ret = search_bitmap(ctl, bitmap_info, &search_start, &search_bytes);
Josef Bacik6606bb92009-07-31 11:03:58 -04001368 BUG_ON(ret < 0 || search_start != *offset);
1369
Josef Bacik96303082009-07-13 21:29:25 -04001370 if (*offset > bitmap_info->offset && *offset + *bytes > end) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001371 bitmap_clear_bits(ctl, bitmap_info, *offset, end - *offset + 1);
Josef Bacik96303082009-07-13 21:29:25 -04001372 *bytes -= end - *offset + 1;
1373 *offset = end + 1;
1374 } else if (*offset >= bitmap_info->offset && *offset + *bytes <= end) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001375 bitmap_clear_bits(ctl, bitmap_info, *offset, *bytes);
Josef Bacik96303082009-07-13 21:29:25 -04001376 *bytes = 0;
1377 }
1378
1379 if (*bytes) {
Josef Bacik6606bb92009-07-31 11:03:58 -04001380 struct rb_node *next = rb_next(&bitmap_info->offset_index);
Li Zefanedf6e2d2010-11-09 14:50:07 +08001381 if (!bitmap_info->bytes)
Li Zefan34d52cb2011-03-29 13:46:06 +08001382 free_bitmap(ctl, bitmap_info);
Josef Bacik96303082009-07-13 21:29:25 -04001383
Josef Bacik6606bb92009-07-31 11:03:58 -04001384 /*
1385 * no entry after this bitmap, but we still have bytes to
1386 * remove, so something has gone wrong.
1387 */
1388 if (!next)
Josef Bacik96303082009-07-13 21:29:25 -04001389 return -EINVAL;
1390
Josef Bacik6606bb92009-07-31 11:03:58 -04001391 bitmap_info = rb_entry(next, struct btrfs_free_space,
1392 offset_index);
1393
1394 /*
1395 * if the next entry isn't a bitmap we need to return to let the
1396 * extent stuff do its work.
1397 */
Josef Bacik96303082009-07-13 21:29:25 -04001398 if (!bitmap_info->bitmap)
1399 return -EAGAIN;
1400
Josef Bacik6606bb92009-07-31 11:03:58 -04001401 /*
1402 * Ok the next item is a bitmap, but it may not actually hold
1403 * the information for the rest of this free space stuff, so
1404 * look for it, and if we don't find it return so we can try
1405 * everything over again.
1406 */
1407 search_start = *offset;
1408 search_bytes = *bytes;
Li Zefan34d52cb2011-03-29 13:46:06 +08001409 ret = search_bitmap(ctl, bitmap_info, &search_start,
Josef Bacik6606bb92009-07-31 11:03:58 -04001410 &search_bytes);
1411 if (ret < 0 || search_start != *offset)
1412 return -EAGAIN;
1413
Josef Bacik96303082009-07-13 21:29:25 -04001414 goto again;
Li Zefanedf6e2d2010-11-09 14:50:07 +08001415 } else if (!bitmap_info->bytes)
Li Zefan34d52cb2011-03-29 13:46:06 +08001416 free_bitmap(ctl, bitmap_info);
Josef Bacik96303082009-07-13 21:29:25 -04001417
1418 return 0;
1419}
1420
Li Zefan34d52cb2011-03-29 13:46:06 +08001421static bool use_bitmap(struct btrfs_free_space_ctl *ctl,
1422 struct btrfs_free_space *info)
Josef Bacik96303082009-07-13 21:29:25 -04001423{
Li Zefan34d52cb2011-03-29 13:46:06 +08001424 struct btrfs_block_group_cache *block_group = ctl->private;
Josef Bacik96303082009-07-13 21:29:25 -04001425
1426 /*
1427 * If we are below the extents threshold then we can add this as an
1428 * extent, and don't have to deal with the bitmap
1429 */
Li Zefan34d52cb2011-03-29 13:46:06 +08001430 if (ctl->free_extents < ctl->extents_thresh) {
Josef Bacik32cb0842011-03-18 16:16:21 -04001431 /*
1432 * If this block group has some small extents we don't want to
1433 * use up all of our free slots in the cache with them, we want
1434 * to reserve them to larger extents, however if we have plent
1435 * of cache left then go ahead an dadd them, no sense in adding
1436 * the overhead of a bitmap if we don't have to.
1437 */
1438 if (info->bytes <= block_group->sectorsize * 4) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001439 if (ctl->free_extents * 2 <= ctl->extents_thresh)
1440 return false;
Josef Bacik32cb0842011-03-18 16:16:21 -04001441 } else {
Li Zefan34d52cb2011-03-29 13:46:06 +08001442 return false;
Josef Bacik32cb0842011-03-18 16:16:21 -04001443 }
1444 }
Josef Bacik96303082009-07-13 21:29:25 -04001445
1446 /*
1447 * some block groups are so tiny they can't be enveloped by a bitmap, so
1448 * don't even bother to create a bitmap for this
1449 */
1450 if (BITS_PER_BITMAP * block_group->sectorsize >
1451 block_group->key.offset)
Li Zefan34d52cb2011-03-29 13:46:06 +08001452 return false;
1453
1454 return true;
1455}
1456
1457static int insert_into_bitmap(struct btrfs_free_space_ctl *ctl,
1458 struct btrfs_free_space *info)
1459{
1460 struct btrfs_free_space *bitmap_info;
1461 int added = 0;
1462 u64 bytes, offset, end;
1463 int ret;
Josef Bacik96303082009-07-13 21:29:25 -04001464
1465 bytes = info->bytes;
1466 offset = info->offset;
1467
Li Zefan34d52cb2011-03-29 13:46:06 +08001468 if (!ctl->op->use_bitmap(ctl, info))
1469 return 0;
1470
Josef Bacik96303082009-07-13 21:29:25 -04001471again:
Li Zefan34d52cb2011-03-29 13:46:06 +08001472 bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
Josef Bacik96303082009-07-13 21:29:25 -04001473 1, 0);
1474 if (!bitmap_info) {
1475 BUG_ON(added);
1476 goto new_bitmap;
1477 }
1478
Li Zefan34d52cb2011-03-29 13:46:06 +08001479 end = bitmap_info->offset + (u64)(BITS_PER_BITMAP * ctl->unit);
Josef Bacik96303082009-07-13 21:29:25 -04001480
1481 if (offset >= bitmap_info->offset && offset + bytes > end) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001482 bitmap_set_bits(ctl, bitmap_info, offset, end - offset);
Josef Bacik96303082009-07-13 21:29:25 -04001483 bytes -= end - offset;
1484 offset = end;
1485 added = 0;
1486 } else if (offset >= bitmap_info->offset && offset + bytes <= end) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001487 bitmap_set_bits(ctl, bitmap_info, offset, bytes);
Josef Bacik96303082009-07-13 21:29:25 -04001488 bytes = 0;
1489 } else {
1490 BUG();
1491 }
1492
1493 if (!bytes) {
1494 ret = 1;
1495 goto out;
1496 } else
1497 goto again;
1498
1499new_bitmap:
1500 if (info && info->bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001501 add_new_bitmap(ctl, info, offset);
Josef Bacik96303082009-07-13 21:29:25 -04001502 added = 1;
1503 info = NULL;
1504 goto again;
1505 } else {
Li Zefan34d52cb2011-03-29 13:46:06 +08001506 spin_unlock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04001507
1508 /* no pre-allocated info, allocate a new one */
1509 if (!info) {
Josef Bacikdc89e982011-01-28 17:05:48 -05001510 info = kmem_cache_zalloc(btrfs_free_space_cachep,
1511 GFP_NOFS);
Josef Bacik96303082009-07-13 21:29:25 -04001512 if (!info) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001513 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04001514 ret = -ENOMEM;
1515 goto out;
1516 }
1517 }
1518
1519 /* allocate the bitmap */
1520 info->bitmap = kzalloc(PAGE_CACHE_SIZE, GFP_NOFS);
Li Zefan34d52cb2011-03-29 13:46:06 +08001521 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04001522 if (!info->bitmap) {
1523 ret = -ENOMEM;
1524 goto out;
1525 }
1526 goto again;
1527 }
1528
1529out:
1530 if (info) {
1531 if (info->bitmap)
1532 kfree(info->bitmap);
Josef Bacikdc89e982011-01-28 17:05:48 -05001533 kmem_cache_free(btrfs_free_space_cachep, info);
Josef Bacik96303082009-07-13 21:29:25 -04001534 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04001535
1536 return ret;
1537}
1538
Chris Mason945d8962011-05-22 12:33:42 -04001539static bool try_merge_free_space(struct btrfs_free_space_ctl *ctl,
Li Zefanf333adb2010-11-09 14:57:39 +08001540 struct btrfs_free_space *info, bool update_stat)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001541{
Li Zefan120d66e2010-11-09 14:56:50 +08001542 struct btrfs_free_space *left_info;
1543 struct btrfs_free_space *right_info;
1544 bool merged = false;
1545 u64 offset = info->offset;
1546 u64 bytes = info->bytes;
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001547
Josef Bacik0f9dd462008-09-23 13:14:11 -04001548 /*
1549 * first we want to see if there is free space adjacent to the range we
1550 * are adding, if there is remove that struct and add a new one to
1551 * cover the entire range
1552 */
Li Zefan34d52cb2011-03-29 13:46:06 +08001553 right_info = tree_search_offset(ctl, offset + bytes, 0, 0);
Josef Bacik96303082009-07-13 21:29:25 -04001554 if (right_info && rb_prev(&right_info->offset_index))
1555 left_info = rb_entry(rb_prev(&right_info->offset_index),
1556 struct btrfs_free_space, offset_index);
1557 else
Li Zefan34d52cb2011-03-29 13:46:06 +08001558 left_info = tree_search_offset(ctl, offset - 1, 0, 0);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001559
Josef Bacik96303082009-07-13 21:29:25 -04001560 if (right_info && !right_info->bitmap) {
Li Zefanf333adb2010-11-09 14:57:39 +08001561 if (update_stat)
Li Zefan34d52cb2011-03-29 13:46:06 +08001562 unlink_free_space(ctl, right_info);
Li Zefanf333adb2010-11-09 14:57:39 +08001563 else
Li Zefan34d52cb2011-03-29 13:46:06 +08001564 __unlink_free_space(ctl, right_info);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001565 info->bytes += right_info->bytes;
Josef Bacikdc89e982011-01-28 17:05:48 -05001566 kmem_cache_free(btrfs_free_space_cachep, right_info);
Li Zefan120d66e2010-11-09 14:56:50 +08001567 merged = true;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001568 }
1569
Josef Bacik96303082009-07-13 21:29:25 -04001570 if (left_info && !left_info->bitmap &&
1571 left_info->offset + left_info->bytes == offset) {
Li Zefanf333adb2010-11-09 14:57:39 +08001572 if (update_stat)
Li Zefan34d52cb2011-03-29 13:46:06 +08001573 unlink_free_space(ctl, left_info);
Li Zefanf333adb2010-11-09 14:57:39 +08001574 else
Li Zefan34d52cb2011-03-29 13:46:06 +08001575 __unlink_free_space(ctl, left_info);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001576 info->offset = left_info->offset;
1577 info->bytes += left_info->bytes;
Josef Bacikdc89e982011-01-28 17:05:48 -05001578 kmem_cache_free(btrfs_free_space_cachep, left_info);
Li Zefan120d66e2010-11-09 14:56:50 +08001579 merged = true;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001580 }
1581
Li Zefan120d66e2010-11-09 14:56:50 +08001582 return merged;
1583}
1584
Li Zefan581bb052011-04-20 10:06:11 +08001585int __btrfs_add_free_space(struct btrfs_free_space_ctl *ctl,
1586 u64 offset, u64 bytes)
Li Zefan120d66e2010-11-09 14:56:50 +08001587{
1588 struct btrfs_free_space *info;
1589 int ret = 0;
1590
Josef Bacikdc89e982011-01-28 17:05:48 -05001591 info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS);
Li Zefan120d66e2010-11-09 14:56:50 +08001592 if (!info)
1593 return -ENOMEM;
1594
1595 info->offset = offset;
1596 info->bytes = bytes;
1597
Li Zefan34d52cb2011-03-29 13:46:06 +08001598 spin_lock(&ctl->tree_lock);
Li Zefan120d66e2010-11-09 14:56:50 +08001599
Li Zefan34d52cb2011-03-29 13:46:06 +08001600 if (try_merge_free_space(ctl, info, true))
Li Zefan120d66e2010-11-09 14:56:50 +08001601 goto link;
1602
1603 /*
1604 * There was no extent directly to the left or right of this new
1605 * extent then we know we're going to have to allocate a new extent, so
1606 * before we do that see if we need to drop this into a bitmap
1607 */
Li Zefan34d52cb2011-03-29 13:46:06 +08001608 ret = insert_into_bitmap(ctl, info);
Li Zefan120d66e2010-11-09 14:56:50 +08001609 if (ret < 0) {
1610 goto out;
1611 } else if (ret) {
1612 ret = 0;
1613 goto out;
1614 }
1615link:
Li Zefan34d52cb2011-03-29 13:46:06 +08001616 ret = link_free_space(ctl, info);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001617 if (ret)
Josef Bacikdc89e982011-01-28 17:05:48 -05001618 kmem_cache_free(btrfs_free_space_cachep, info);
Josef Bacik96303082009-07-13 21:29:25 -04001619out:
Li Zefan34d52cb2011-03-29 13:46:06 +08001620 spin_unlock(&ctl->tree_lock);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001621
Josef Bacik0f9dd462008-09-23 13:14:11 -04001622 if (ret) {
Josef Bacik96303082009-07-13 21:29:25 -04001623 printk(KERN_CRIT "btrfs: unable to add free space :%d\n", ret);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04001624 BUG_ON(ret == -EEXIST);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001625 }
1626
Josef Bacik0f9dd462008-09-23 13:14:11 -04001627 return ret;
1628}
1629
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001630int btrfs_remove_free_space(struct btrfs_block_group_cache *block_group,
1631 u64 offset, u64 bytes)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001632{
Li Zefan34d52cb2011-03-29 13:46:06 +08001633 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001634 struct btrfs_free_space *info;
Josef Bacik96303082009-07-13 21:29:25 -04001635 struct btrfs_free_space *next_info = NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001636 int ret = 0;
1637
Li Zefan34d52cb2011-03-29 13:46:06 +08001638 spin_lock(&ctl->tree_lock);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001639
Josef Bacik96303082009-07-13 21:29:25 -04001640again:
Li Zefan34d52cb2011-03-29 13:46:06 +08001641 info = tree_search_offset(ctl, offset, 0, 0);
Josef Bacik96303082009-07-13 21:29:25 -04001642 if (!info) {
Josef Bacik6606bb92009-07-31 11:03:58 -04001643 /*
1644 * oops didn't find an extent that matched the space we wanted
1645 * to remove, look for a bitmap instead
1646 */
Li Zefan34d52cb2011-03-29 13:46:06 +08001647 info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
Josef Bacik6606bb92009-07-31 11:03:58 -04001648 1, 0);
1649 if (!info) {
1650 WARN_ON(1);
1651 goto out_lock;
1652 }
Josef Bacik96303082009-07-13 21:29:25 -04001653 }
1654
1655 if (info->bytes < bytes && rb_next(&info->offset_index)) {
1656 u64 end;
1657 next_info = rb_entry(rb_next(&info->offset_index),
1658 struct btrfs_free_space,
1659 offset_index);
1660
1661 if (next_info->bitmap)
Li Zefan34d52cb2011-03-29 13:46:06 +08001662 end = next_info->offset +
1663 BITS_PER_BITMAP * ctl->unit - 1;
Josef Bacik96303082009-07-13 21:29:25 -04001664 else
1665 end = next_info->offset + next_info->bytes;
1666
1667 if (next_info->bytes < bytes ||
1668 next_info->offset > offset || offset > end) {
1669 printk(KERN_CRIT "Found free space at %llu, size %llu,"
1670 " trying to use %llu\n",
1671 (unsigned long long)info->offset,
1672 (unsigned long long)info->bytes,
1673 (unsigned long long)bytes);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001674 WARN_ON(1);
1675 ret = -EINVAL;
Josef Bacik96303082009-07-13 21:29:25 -04001676 goto out_lock;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001677 }
Josef Bacik96303082009-07-13 21:29:25 -04001678
1679 info = next_info;
1680 }
1681
1682 if (info->bytes == bytes) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001683 unlink_free_space(ctl, info);
Josef Bacik96303082009-07-13 21:29:25 -04001684 if (info->bitmap) {
1685 kfree(info->bitmap);
Li Zefan34d52cb2011-03-29 13:46:06 +08001686 ctl->total_bitmaps--;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001687 }
Josef Bacikdc89e982011-01-28 17:05:48 -05001688 kmem_cache_free(btrfs_free_space_cachep, info);
Josef Bacik96303082009-07-13 21:29:25 -04001689 goto out_lock;
1690 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04001691
Josef Bacik96303082009-07-13 21:29:25 -04001692 if (!info->bitmap && info->offset == offset) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001693 unlink_free_space(ctl, info);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001694 info->offset += bytes;
1695 info->bytes -= bytes;
Li Zefan34d52cb2011-03-29 13:46:06 +08001696 link_free_space(ctl, info);
Josef Bacik96303082009-07-13 21:29:25 -04001697 goto out_lock;
1698 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04001699
Josef Bacik96303082009-07-13 21:29:25 -04001700 if (!info->bitmap && info->offset <= offset &&
1701 info->offset + info->bytes >= offset + bytes) {
Chris Mason9b49c9b2008-09-24 11:23:25 -04001702 u64 old_start = info->offset;
1703 /*
1704 * we're freeing space in the middle of the info,
1705 * this can happen during tree log replay
1706 *
1707 * first unlink the old info and then
1708 * insert it again after the hole we're creating
1709 */
Li Zefan34d52cb2011-03-29 13:46:06 +08001710 unlink_free_space(ctl, info);
Chris Mason9b49c9b2008-09-24 11:23:25 -04001711 if (offset + bytes < info->offset + info->bytes) {
1712 u64 old_end = info->offset + info->bytes;
1713
1714 info->offset = offset + bytes;
1715 info->bytes = old_end - info->offset;
Li Zefan34d52cb2011-03-29 13:46:06 +08001716 ret = link_free_space(ctl, info);
Josef Bacik96303082009-07-13 21:29:25 -04001717 WARN_ON(ret);
1718 if (ret)
1719 goto out_lock;
Chris Mason9b49c9b2008-09-24 11:23:25 -04001720 } else {
1721 /* the hole we're creating ends at the end
1722 * of the info struct, just free the info
1723 */
Josef Bacikdc89e982011-01-28 17:05:48 -05001724 kmem_cache_free(btrfs_free_space_cachep, info);
Chris Mason9b49c9b2008-09-24 11:23:25 -04001725 }
Li Zefan34d52cb2011-03-29 13:46:06 +08001726 spin_unlock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04001727
1728 /* step two, insert a new info struct to cover
1729 * anything before the hole
Chris Mason9b49c9b2008-09-24 11:23:25 -04001730 */
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001731 ret = btrfs_add_free_space(block_group, old_start,
1732 offset - old_start);
Josef Bacik96303082009-07-13 21:29:25 -04001733 WARN_ON(ret);
1734 goto out;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001735 }
Josef Bacik96303082009-07-13 21:29:25 -04001736
Li Zefan34d52cb2011-03-29 13:46:06 +08001737 ret = remove_from_bitmap(ctl, info, &offset, &bytes);
Josef Bacik96303082009-07-13 21:29:25 -04001738 if (ret == -EAGAIN)
1739 goto again;
1740 BUG_ON(ret);
1741out_lock:
Li Zefan34d52cb2011-03-29 13:46:06 +08001742 spin_unlock(&ctl->tree_lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001743out:
Josef Bacik25179202008-10-29 14:49:05 -04001744 return ret;
1745}
1746
Josef Bacik0f9dd462008-09-23 13:14:11 -04001747void btrfs_dump_free_space(struct btrfs_block_group_cache *block_group,
1748 u64 bytes)
1749{
Li Zefan34d52cb2011-03-29 13:46:06 +08001750 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001751 struct btrfs_free_space *info;
1752 struct rb_node *n;
1753 int count = 0;
1754
Li Zefan34d52cb2011-03-29 13:46:06 +08001755 for (n = rb_first(&ctl->free_space_offset); n; n = rb_next(n)) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001756 info = rb_entry(n, struct btrfs_free_space, offset_index);
1757 if (info->bytes >= bytes)
1758 count++;
Josef Bacik96303082009-07-13 21:29:25 -04001759 printk(KERN_CRIT "entry offset %llu, bytes %llu, bitmap %s\n",
Joel Becker21380932009-04-21 12:38:29 -07001760 (unsigned long long)info->offset,
Josef Bacik96303082009-07-13 21:29:25 -04001761 (unsigned long long)info->bytes,
1762 (info->bitmap) ? "yes" : "no");
Josef Bacik0f9dd462008-09-23 13:14:11 -04001763 }
Josef Bacik96303082009-07-13 21:29:25 -04001764 printk(KERN_INFO "block group has cluster?: %s\n",
1765 list_empty(&block_group->cluster_list) ? "no" : "yes");
Josef Bacik0f9dd462008-09-23 13:14:11 -04001766 printk(KERN_INFO "%d blocks of free space at or bigger than bytes is"
1767 "\n", count);
1768}
1769
Li Zefan34d52cb2011-03-29 13:46:06 +08001770static struct btrfs_free_space_op free_space_op = {
1771 .recalc_thresholds = recalculate_thresholds,
1772 .use_bitmap = use_bitmap,
1773};
1774
1775void btrfs_init_free_space_ctl(struct btrfs_block_group_cache *block_group)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001776{
Li Zefan34d52cb2011-03-29 13:46:06 +08001777 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001778
Li Zefan34d52cb2011-03-29 13:46:06 +08001779 spin_lock_init(&ctl->tree_lock);
1780 ctl->unit = block_group->sectorsize;
1781 ctl->start = block_group->key.objectid;
1782 ctl->private = block_group;
1783 ctl->op = &free_space_op;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001784
Li Zefan34d52cb2011-03-29 13:46:06 +08001785 /*
1786 * we only want to have 32k of ram per block group for keeping
1787 * track of free space, and if we pass 1/2 of that we want to
1788 * start converting things over to using bitmaps
1789 */
1790 ctl->extents_thresh = ((1024 * 32) / 2) /
1791 sizeof(struct btrfs_free_space);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001792}
1793
Chris Masonfa9c0d792009-04-03 09:47:43 -04001794/*
1795 * for a given cluster, put all of its extents back into the free
1796 * space cache. If the block group passed doesn't match the block group
1797 * pointed to by the cluster, someone else raced in and freed the
1798 * cluster already. In that case, we just return without changing anything
1799 */
1800static int
1801__btrfs_return_cluster_to_free_space(
1802 struct btrfs_block_group_cache *block_group,
1803 struct btrfs_free_cluster *cluster)
1804{
Li Zefan34d52cb2011-03-29 13:46:06 +08001805 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04001806 struct btrfs_free_space *entry;
1807 struct rb_node *node;
1808
1809 spin_lock(&cluster->lock);
1810 if (cluster->block_group != block_group)
1811 goto out;
1812
Josef Bacik96303082009-07-13 21:29:25 -04001813 cluster->block_group = NULL;
Chris Masonfa9c0d792009-04-03 09:47:43 -04001814 cluster->window_start = 0;
Josef Bacik96303082009-07-13 21:29:25 -04001815 list_del_init(&cluster->block_group_list);
Josef Bacik96303082009-07-13 21:29:25 -04001816
Chris Masonfa9c0d792009-04-03 09:47:43 -04001817 node = rb_first(&cluster->root);
Josef Bacik96303082009-07-13 21:29:25 -04001818 while (node) {
Josef Bacik4e69b592011-03-21 10:11:24 -04001819 bool bitmap;
1820
Chris Masonfa9c0d792009-04-03 09:47:43 -04001821 entry = rb_entry(node, struct btrfs_free_space, offset_index);
1822 node = rb_next(&entry->offset_index);
1823 rb_erase(&entry->offset_index, &cluster->root);
Josef Bacik4e69b592011-03-21 10:11:24 -04001824
1825 bitmap = (entry->bitmap != NULL);
1826 if (!bitmap)
Li Zefan34d52cb2011-03-29 13:46:06 +08001827 try_merge_free_space(ctl, entry, false);
1828 tree_insert_offset(&ctl->free_space_offset,
Josef Bacik4e69b592011-03-21 10:11:24 -04001829 entry->offset, &entry->offset_index, bitmap);
Chris Masonfa9c0d792009-04-03 09:47:43 -04001830 }
Eric Paris6bef4d32010-02-23 19:43:04 +00001831 cluster->root = RB_ROOT;
Josef Bacik96303082009-07-13 21:29:25 -04001832
Chris Masonfa9c0d792009-04-03 09:47:43 -04001833out:
1834 spin_unlock(&cluster->lock);
Josef Bacik96303082009-07-13 21:29:25 -04001835 btrfs_put_block_group(block_group);
Chris Masonfa9c0d792009-04-03 09:47:43 -04001836 return 0;
1837}
1838
Chris Mason09655372011-05-21 09:27:38 -04001839void __btrfs_remove_free_space_cache_locked(struct btrfs_free_space_ctl *ctl)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001840{
1841 struct btrfs_free_space *info;
1842 struct rb_node *node;
Li Zefan581bb052011-04-20 10:06:11 +08001843
Li Zefan581bb052011-04-20 10:06:11 +08001844 while ((node = rb_last(&ctl->free_space_offset)) != NULL) {
1845 info = rb_entry(node, struct btrfs_free_space, offset_index);
1846 unlink_free_space(ctl, info);
1847 kfree(info->bitmap);
1848 kmem_cache_free(btrfs_free_space_cachep, info);
1849 if (need_resched()) {
1850 spin_unlock(&ctl->tree_lock);
1851 cond_resched();
1852 spin_lock(&ctl->tree_lock);
1853 }
1854 }
Chris Mason09655372011-05-21 09:27:38 -04001855}
1856
1857void __btrfs_remove_free_space_cache(struct btrfs_free_space_ctl *ctl)
1858{
1859 spin_lock(&ctl->tree_lock);
1860 __btrfs_remove_free_space_cache_locked(ctl);
Li Zefan581bb052011-04-20 10:06:11 +08001861 spin_unlock(&ctl->tree_lock);
1862}
1863
Josef Bacik0f9dd462008-09-23 13:14:11 -04001864void btrfs_remove_free_space_cache(struct btrfs_block_group_cache *block_group)
1865{
Li Zefan34d52cb2011-03-29 13:46:06 +08001866 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04001867 struct btrfs_free_cluster *cluster;
Josef Bacik96303082009-07-13 21:29:25 -04001868 struct list_head *head;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001869
Li Zefan34d52cb2011-03-29 13:46:06 +08001870 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04001871 while ((head = block_group->cluster_list.next) !=
1872 &block_group->cluster_list) {
1873 cluster = list_entry(head, struct btrfs_free_cluster,
1874 block_group_list);
Chris Masonfa9c0d792009-04-03 09:47:43 -04001875
1876 WARN_ON(cluster->block_group != block_group);
1877 __btrfs_return_cluster_to_free_space(block_group, cluster);
Josef Bacik96303082009-07-13 21:29:25 -04001878 if (need_resched()) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001879 spin_unlock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04001880 cond_resched();
Li Zefan34d52cb2011-03-29 13:46:06 +08001881 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04001882 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04001883 }
Chris Mason09655372011-05-21 09:27:38 -04001884 __btrfs_remove_free_space_cache_locked(ctl);
Li Zefan34d52cb2011-03-29 13:46:06 +08001885 spin_unlock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04001886
Josef Bacik0f9dd462008-09-23 13:14:11 -04001887}
1888
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001889u64 btrfs_find_space_for_alloc(struct btrfs_block_group_cache *block_group,
1890 u64 offset, u64 bytes, u64 empty_size)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001891{
Li Zefan34d52cb2011-03-29 13:46:06 +08001892 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001893 struct btrfs_free_space *entry = NULL;
Josef Bacik96303082009-07-13 21:29:25 -04001894 u64 bytes_search = bytes + empty_size;
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001895 u64 ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001896
Li Zefan34d52cb2011-03-29 13:46:06 +08001897 spin_lock(&ctl->tree_lock);
1898 entry = find_free_space(ctl, &offset, &bytes_search);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001899 if (!entry)
Josef Bacik96303082009-07-13 21:29:25 -04001900 goto out;
1901
1902 ret = offset;
1903 if (entry->bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001904 bitmap_clear_bits(ctl, entry, offset, bytes);
Li Zefanedf6e2d2010-11-09 14:50:07 +08001905 if (!entry->bytes)
Li Zefan34d52cb2011-03-29 13:46:06 +08001906 free_bitmap(ctl, entry);
Josef Bacik96303082009-07-13 21:29:25 -04001907 } else {
Li Zefan34d52cb2011-03-29 13:46:06 +08001908 unlink_free_space(ctl, entry);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001909 entry->offset += bytes;
1910 entry->bytes -= bytes;
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001911 if (!entry->bytes)
Josef Bacikdc89e982011-01-28 17:05:48 -05001912 kmem_cache_free(btrfs_free_space_cachep, entry);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001913 else
Li Zefan34d52cb2011-03-29 13:46:06 +08001914 link_free_space(ctl, entry);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001915 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04001916
Josef Bacik96303082009-07-13 21:29:25 -04001917out:
Li Zefan34d52cb2011-03-29 13:46:06 +08001918 spin_unlock(&ctl->tree_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04001919
Josef Bacik0f9dd462008-09-23 13:14:11 -04001920 return ret;
1921}
Chris Masonfa9c0d792009-04-03 09:47:43 -04001922
1923/*
1924 * given a cluster, put all of its extents back into the free space
1925 * cache. If a block group is passed, this function will only free
1926 * a cluster that belongs to the passed block group.
1927 *
1928 * Otherwise, it'll get a reference on the block group pointed to by the
1929 * cluster and remove the cluster from it.
1930 */
1931int btrfs_return_cluster_to_free_space(
1932 struct btrfs_block_group_cache *block_group,
1933 struct btrfs_free_cluster *cluster)
1934{
Li Zefan34d52cb2011-03-29 13:46:06 +08001935 struct btrfs_free_space_ctl *ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04001936 int ret;
1937
1938 /* first, get a safe pointer to the block group */
1939 spin_lock(&cluster->lock);
1940 if (!block_group) {
1941 block_group = cluster->block_group;
1942 if (!block_group) {
1943 spin_unlock(&cluster->lock);
1944 return 0;
1945 }
1946 } else if (cluster->block_group != block_group) {
1947 /* someone else has already freed it don't redo their work */
1948 spin_unlock(&cluster->lock);
1949 return 0;
1950 }
1951 atomic_inc(&block_group->count);
1952 spin_unlock(&cluster->lock);
1953
Li Zefan34d52cb2011-03-29 13:46:06 +08001954 ctl = block_group->free_space_ctl;
1955
Chris Masonfa9c0d792009-04-03 09:47:43 -04001956 /* now return any extents the cluster had on it */
Li Zefan34d52cb2011-03-29 13:46:06 +08001957 spin_lock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04001958 ret = __btrfs_return_cluster_to_free_space(block_group, cluster);
Li Zefan34d52cb2011-03-29 13:46:06 +08001959 spin_unlock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04001960
1961 /* finally drop our ref */
1962 btrfs_put_block_group(block_group);
1963 return ret;
1964}
1965
Josef Bacik96303082009-07-13 21:29:25 -04001966static u64 btrfs_alloc_from_bitmap(struct btrfs_block_group_cache *block_group,
1967 struct btrfs_free_cluster *cluster,
Josef Bacik4e69b592011-03-21 10:11:24 -04001968 struct btrfs_free_space *entry,
Josef Bacik96303082009-07-13 21:29:25 -04001969 u64 bytes, u64 min_start)
1970{
Li Zefan34d52cb2011-03-29 13:46:06 +08001971 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik96303082009-07-13 21:29:25 -04001972 int err;
1973 u64 search_start = cluster->window_start;
1974 u64 search_bytes = bytes;
1975 u64 ret = 0;
1976
Josef Bacik96303082009-07-13 21:29:25 -04001977 search_start = min_start;
1978 search_bytes = bytes;
1979
Li Zefan34d52cb2011-03-29 13:46:06 +08001980 err = search_bitmap(ctl, entry, &search_start, &search_bytes);
Josef Bacik96303082009-07-13 21:29:25 -04001981 if (err)
Josef Bacik4e69b592011-03-21 10:11:24 -04001982 return 0;
Josef Bacik96303082009-07-13 21:29:25 -04001983
1984 ret = search_start;
Li Zefan34d52cb2011-03-29 13:46:06 +08001985 bitmap_clear_bits(ctl, entry, ret, bytes);
Josef Bacik96303082009-07-13 21:29:25 -04001986
1987 return ret;
1988}
1989
Chris Masonfa9c0d792009-04-03 09:47:43 -04001990/*
1991 * given a cluster, try to allocate 'bytes' from it, returns 0
1992 * if it couldn't find anything suitably large, or a logical disk offset
1993 * if things worked out
1994 */
1995u64 btrfs_alloc_from_cluster(struct btrfs_block_group_cache *block_group,
1996 struct btrfs_free_cluster *cluster, u64 bytes,
1997 u64 min_start)
1998{
Li Zefan34d52cb2011-03-29 13:46:06 +08001999 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002000 struct btrfs_free_space *entry = NULL;
2001 struct rb_node *node;
2002 u64 ret = 0;
2003
2004 spin_lock(&cluster->lock);
2005 if (bytes > cluster->max_size)
2006 goto out;
2007
2008 if (cluster->block_group != block_group)
2009 goto out;
2010
2011 node = rb_first(&cluster->root);
2012 if (!node)
2013 goto out;
2014
2015 entry = rb_entry(node, struct btrfs_free_space, offset_index);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002016 while(1) {
Josef Bacik4e69b592011-03-21 10:11:24 -04002017 if (entry->bytes < bytes ||
2018 (!entry->bitmap && entry->offset < min_start)) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04002019 node = rb_next(&entry->offset_index);
2020 if (!node)
2021 break;
2022 entry = rb_entry(node, struct btrfs_free_space,
2023 offset_index);
2024 continue;
2025 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04002026
Josef Bacik4e69b592011-03-21 10:11:24 -04002027 if (entry->bitmap) {
2028 ret = btrfs_alloc_from_bitmap(block_group,
2029 cluster, entry, bytes,
2030 min_start);
2031 if (ret == 0) {
Josef Bacik4e69b592011-03-21 10:11:24 -04002032 node = rb_next(&entry->offset_index);
2033 if (!node)
2034 break;
2035 entry = rb_entry(node, struct btrfs_free_space,
2036 offset_index);
2037 continue;
2038 }
2039 } else {
2040
2041 ret = entry->offset;
2042
2043 entry->offset += bytes;
2044 entry->bytes -= bytes;
2045 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04002046
Li Zefan5e71b5d2010-11-09 14:55:34 +08002047 if (entry->bytes == 0)
Chris Masonfa9c0d792009-04-03 09:47:43 -04002048 rb_erase(&entry->offset_index, &cluster->root);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002049 break;
2050 }
2051out:
2052 spin_unlock(&cluster->lock);
Josef Bacik96303082009-07-13 21:29:25 -04002053
Li Zefan5e71b5d2010-11-09 14:55:34 +08002054 if (!ret)
2055 return 0;
2056
Li Zefan34d52cb2011-03-29 13:46:06 +08002057 spin_lock(&ctl->tree_lock);
Li Zefan5e71b5d2010-11-09 14:55:34 +08002058
Li Zefan34d52cb2011-03-29 13:46:06 +08002059 ctl->free_space -= bytes;
Li Zefan5e71b5d2010-11-09 14:55:34 +08002060 if (entry->bytes == 0) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002061 ctl->free_extents--;
Josef Bacik4e69b592011-03-21 10:11:24 -04002062 if (entry->bitmap) {
2063 kfree(entry->bitmap);
Li Zefan34d52cb2011-03-29 13:46:06 +08002064 ctl->total_bitmaps--;
2065 ctl->op->recalc_thresholds(ctl);
Josef Bacik4e69b592011-03-21 10:11:24 -04002066 }
Josef Bacikdc89e982011-01-28 17:05:48 -05002067 kmem_cache_free(btrfs_free_space_cachep, entry);
Li Zefan5e71b5d2010-11-09 14:55:34 +08002068 }
2069
Li Zefan34d52cb2011-03-29 13:46:06 +08002070 spin_unlock(&ctl->tree_lock);
Li Zefan5e71b5d2010-11-09 14:55:34 +08002071
Chris Masonfa9c0d792009-04-03 09:47:43 -04002072 return ret;
2073}
2074
Josef Bacik96303082009-07-13 21:29:25 -04002075static int btrfs_bitmap_cluster(struct btrfs_block_group_cache *block_group,
2076 struct btrfs_free_space *entry,
2077 struct btrfs_free_cluster *cluster,
2078 u64 offset, u64 bytes, u64 min_bytes)
2079{
Li Zefan34d52cb2011-03-29 13:46:06 +08002080 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik96303082009-07-13 21:29:25 -04002081 unsigned long next_zero;
2082 unsigned long i;
2083 unsigned long search_bits;
2084 unsigned long total_bits;
2085 unsigned long found_bits;
2086 unsigned long start = 0;
2087 unsigned long total_found = 0;
Josef Bacik4e69b592011-03-21 10:11:24 -04002088 int ret;
Josef Bacik96303082009-07-13 21:29:25 -04002089 bool found = false;
2090
2091 i = offset_to_bit(entry->offset, block_group->sectorsize,
2092 max_t(u64, offset, entry->offset));
Josef Bacikd0a365e2011-03-18 15:27:43 -04002093 search_bits = bytes_to_bits(bytes, block_group->sectorsize);
2094 total_bits = bytes_to_bits(min_bytes, block_group->sectorsize);
Josef Bacik96303082009-07-13 21:29:25 -04002095
2096again:
2097 found_bits = 0;
2098 for (i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, i);
2099 i < BITS_PER_BITMAP;
2100 i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, i + 1)) {
2101 next_zero = find_next_zero_bit(entry->bitmap,
2102 BITS_PER_BITMAP, i);
2103 if (next_zero - i >= search_bits) {
2104 found_bits = next_zero - i;
2105 break;
2106 }
2107 i = next_zero;
2108 }
2109
2110 if (!found_bits)
Josef Bacik4e69b592011-03-21 10:11:24 -04002111 return -ENOSPC;
Josef Bacik96303082009-07-13 21:29:25 -04002112
2113 if (!found) {
2114 start = i;
2115 found = true;
2116 }
2117
2118 total_found += found_bits;
2119
2120 if (cluster->max_size < found_bits * block_group->sectorsize)
2121 cluster->max_size = found_bits * block_group->sectorsize;
2122
2123 if (total_found < total_bits) {
2124 i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, next_zero);
2125 if (i - start > total_bits * 2) {
2126 total_found = 0;
2127 cluster->max_size = 0;
2128 found = false;
2129 }
2130 goto again;
2131 }
2132
2133 cluster->window_start = start * block_group->sectorsize +
2134 entry->offset;
Li Zefan34d52cb2011-03-29 13:46:06 +08002135 rb_erase(&entry->offset_index, &ctl->free_space_offset);
Josef Bacik4e69b592011-03-21 10:11:24 -04002136 ret = tree_insert_offset(&cluster->root, entry->offset,
2137 &entry->offset_index, 1);
2138 BUG_ON(ret);
Josef Bacik96303082009-07-13 21:29:25 -04002139
2140 return 0;
2141}
2142
Chris Masonfa9c0d792009-04-03 09:47:43 -04002143/*
Josef Bacik4e69b592011-03-21 10:11:24 -04002144 * This searches the block group for just extents to fill the cluster with.
2145 */
2146static int setup_cluster_no_bitmap(struct btrfs_block_group_cache *block_group,
2147 struct btrfs_free_cluster *cluster,
2148 u64 offset, u64 bytes, u64 min_bytes)
2149{
Li Zefan34d52cb2011-03-29 13:46:06 +08002150 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik4e69b592011-03-21 10:11:24 -04002151 struct btrfs_free_space *first = NULL;
2152 struct btrfs_free_space *entry = NULL;
2153 struct btrfs_free_space *prev = NULL;
2154 struct btrfs_free_space *last;
2155 struct rb_node *node;
2156 u64 window_start;
2157 u64 window_free;
2158 u64 max_extent;
2159 u64 max_gap = 128 * 1024;
2160
Li Zefan34d52cb2011-03-29 13:46:06 +08002161 entry = tree_search_offset(ctl, offset, 0, 1);
Josef Bacik4e69b592011-03-21 10:11:24 -04002162 if (!entry)
2163 return -ENOSPC;
2164
2165 /*
2166 * We don't want bitmaps, so just move along until we find a normal
2167 * extent entry.
2168 */
2169 while (entry->bitmap) {
2170 node = rb_next(&entry->offset_index);
2171 if (!node)
2172 return -ENOSPC;
2173 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2174 }
2175
2176 window_start = entry->offset;
2177 window_free = entry->bytes;
2178 max_extent = entry->bytes;
2179 first = entry;
2180 last = entry;
2181 prev = entry;
2182
2183 while (window_free <= min_bytes) {
2184 node = rb_next(&entry->offset_index);
2185 if (!node)
2186 return -ENOSPC;
2187 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2188
2189 if (entry->bitmap)
2190 continue;
2191 /*
2192 * we haven't filled the empty size and the window is
2193 * very large. reset and try again
2194 */
2195 if (entry->offset - (prev->offset + prev->bytes) > max_gap ||
2196 entry->offset - window_start > (min_bytes * 2)) {
2197 first = entry;
2198 window_start = entry->offset;
2199 window_free = entry->bytes;
2200 last = entry;
2201 max_extent = entry->bytes;
2202 } else {
2203 last = entry;
2204 window_free += entry->bytes;
2205 if (entry->bytes > max_extent)
2206 max_extent = entry->bytes;
2207 }
2208 prev = entry;
2209 }
2210
2211 cluster->window_start = first->offset;
2212
2213 node = &first->offset_index;
2214
2215 /*
2216 * now we've found our entries, pull them out of the free space
2217 * cache and put them into the cluster rbtree
2218 */
2219 do {
2220 int ret;
2221
2222 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2223 node = rb_next(&entry->offset_index);
2224 if (entry->bitmap)
2225 continue;
2226
Li Zefan34d52cb2011-03-29 13:46:06 +08002227 rb_erase(&entry->offset_index, &ctl->free_space_offset);
Josef Bacik4e69b592011-03-21 10:11:24 -04002228 ret = tree_insert_offset(&cluster->root, entry->offset,
2229 &entry->offset_index, 0);
2230 BUG_ON(ret);
2231 } while (node && entry != last);
2232
2233 cluster->max_size = max_extent;
2234
2235 return 0;
2236}
2237
2238/*
2239 * This specifically looks for bitmaps that may work in the cluster, we assume
2240 * that we have already failed to find extents that will work.
2241 */
2242static int setup_cluster_bitmap(struct btrfs_block_group_cache *block_group,
2243 struct btrfs_free_cluster *cluster,
2244 u64 offset, u64 bytes, u64 min_bytes)
2245{
Li Zefan34d52cb2011-03-29 13:46:06 +08002246 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik4e69b592011-03-21 10:11:24 -04002247 struct btrfs_free_space *entry;
2248 struct rb_node *node;
2249 int ret = -ENOSPC;
2250
Li Zefan34d52cb2011-03-29 13:46:06 +08002251 if (ctl->total_bitmaps == 0)
Josef Bacik4e69b592011-03-21 10:11:24 -04002252 return -ENOSPC;
2253
Li Zefan34d52cb2011-03-29 13:46:06 +08002254 entry = tree_search_offset(ctl, offset_to_bitmap(ctl, offset), 0, 1);
Josef Bacik4e69b592011-03-21 10:11:24 -04002255 if (!entry)
2256 return -ENOSPC;
2257
2258 node = &entry->offset_index;
2259 do {
2260 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2261 node = rb_next(&entry->offset_index);
2262 if (!entry->bitmap)
2263 continue;
2264 if (entry->bytes < min_bytes)
2265 continue;
2266 ret = btrfs_bitmap_cluster(block_group, entry, cluster, offset,
2267 bytes, min_bytes);
2268 } while (ret && node);
2269
2270 return ret;
2271}
2272
2273/*
Chris Masonfa9c0d792009-04-03 09:47:43 -04002274 * here we try to find a cluster of blocks in a block group. The goal
2275 * is to find at least bytes free and up to empty_size + bytes free.
2276 * We might not find them all in one contiguous area.
2277 *
2278 * returns zero and sets up cluster if things worked out, otherwise
2279 * it returns -enospc
2280 */
2281int btrfs_find_space_cluster(struct btrfs_trans_handle *trans,
Chris Mason451d7582009-06-09 20:28:34 -04002282 struct btrfs_root *root,
Chris Masonfa9c0d792009-04-03 09:47:43 -04002283 struct btrfs_block_group_cache *block_group,
2284 struct btrfs_free_cluster *cluster,
2285 u64 offset, u64 bytes, u64 empty_size)
2286{
Li Zefan34d52cb2011-03-29 13:46:06 +08002287 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002288 u64 min_bytes;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002289 int ret;
2290
2291 /* for metadata, allow allocates with more holes */
Chris Mason451d7582009-06-09 20:28:34 -04002292 if (btrfs_test_opt(root, SSD_SPREAD)) {
2293 min_bytes = bytes + empty_size;
2294 } else if (block_group->flags & BTRFS_BLOCK_GROUP_METADATA) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04002295 /*
2296 * we want to do larger allocations when we are
2297 * flushing out the delayed refs, it helps prevent
2298 * making more work as we go along.
2299 */
2300 if (trans->transaction->delayed_refs.flushing)
2301 min_bytes = max(bytes, (bytes + empty_size) >> 1);
2302 else
2303 min_bytes = max(bytes, (bytes + empty_size) >> 4);
2304 } else
2305 min_bytes = max(bytes, (bytes + empty_size) >> 2);
2306
Li Zefan34d52cb2011-03-29 13:46:06 +08002307 spin_lock(&ctl->tree_lock);
Josef Bacik7d0d2e82011-03-18 15:13:42 -04002308
2309 /*
2310 * If we know we don't have enough space to make a cluster don't even
2311 * bother doing all the work to try and find one.
2312 */
Li Zefan34d52cb2011-03-29 13:46:06 +08002313 if (ctl->free_space < min_bytes) {
2314 spin_unlock(&ctl->tree_lock);
Josef Bacik7d0d2e82011-03-18 15:13:42 -04002315 return -ENOSPC;
2316 }
2317
Chris Masonfa9c0d792009-04-03 09:47:43 -04002318 spin_lock(&cluster->lock);
2319
2320 /* someone already found a cluster, hooray */
2321 if (cluster->block_group) {
2322 ret = 0;
2323 goto out;
2324 }
Josef Bacik4e69b592011-03-21 10:11:24 -04002325
2326 ret = setup_cluster_no_bitmap(block_group, cluster, offset, bytes,
2327 min_bytes);
2328 if (ret)
2329 ret = setup_cluster_bitmap(block_group, cluster, offset,
2330 bytes, min_bytes);
2331
2332 if (!ret) {
2333 atomic_inc(&block_group->count);
2334 list_add_tail(&cluster->block_group_list,
2335 &block_group->cluster_list);
2336 cluster->block_group = block_group;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002337 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04002338out:
2339 spin_unlock(&cluster->lock);
Li Zefan34d52cb2011-03-29 13:46:06 +08002340 spin_unlock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002341
2342 return ret;
2343}
2344
2345/*
2346 * simple code to zero out a cluster
2347 */
2348void btrfs_init_free_cluster(struct btrfs_free_cluster *cluster)
2349{
2350 spin_lock_init(&cluster->lock);
2351 spin_lock_init(&cluster->refill_lock);
Eric Paris6bef4d32010-02-23 19:43:04 +00002352 cluster->root = RB_ROOT;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002353 cluster->max_size = 0;
2354 INIT_LIST_HEAD(&cluster->block_group_list);
2355 cluster->block_group = NULL;
2356}
2357
Li Dongyangf7039b12011-03-24 10:24:28 +00002358int btrfs_trim_block_group(struct btrfs_block_group_cache *block_group,
2359 u64 *trimmed, u64 start, u64 end, u64 minlen)
2360{
Li Zefan34d52cb2011-03-29 13:46:06 +08002361 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Li Dongyangf7039b12011-03-24 10:24:28 +00002362 struct btrfs_free_space *entry = NULL;
2363 struct btrfs_fs_info *fs_info = block_group->fs_info;
2364 u64 bytes = 0;
2365 u64 actually_trimmed;
2366 int ret = 0;
2367
2368 *trimmed = 0;
2369
2370 while (start < end) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002371 spin_lock(&ctl->tree_lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00002372
Li Zefan34d52cb2011-03-29 13:46:06 +08002373 if (ctl->free_space < minlen) {
2374 spin_unlock(&ctl->tree_lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00002375 break;
2376 }
2377
Li Zefan34d52cb2011-03-29 13:46:06 +08002378 entry = tree_search_offset(ctl, start, 0, 1);
Li Dongyangf7039b12011-03-24 10:24:28 +00002379 if (!entry)
Li Zefan34d52cb2011-03-29 13:46:06 +08002380 entry = tree_search_offset(ctl,
2381 offset_to_bitmap(ctl, start),
Li Dongyangf7039b12011-03-24 10:24:28 +00002382 1, 1);
2383
2384 if (!entry || entry->offset >= end) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002385 spin_unlock(&ctl->tree_lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00002386 break;
2387 }
2388
2389 if (entry->bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002390 ret = search_bitmap(ctl, entry, &start, &bytes);
Li Dongyangf7039b12011-03-24 10:24:28 +00002391 if (!ret) {
2392 if (start >= end) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002393 spin_unlock(&ctl->tree_lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00002394 break;
2395 }
2396 bytes = min(bytes, end - start);
Li Zefan34d52cb2011-03-29 13:46:06 +08002397 bitmap_clear_bits(ctl, entry, start, bytes);
Li Dongyangf7039b12011-03-24 10:24:28 +00002398 if (entry->bytes == 0)
Li Zefan34d52cb2011-03-29 13:46:06 +08002399 free_bitmap(ctl, entry);
Li Dongyangf7039b12011-03-24 10:24:28 +00002400 } else {
2401 start = entry->offset + BITS_PER_BITMAP *
2402 block_group->sectorsize;
Li Zefan34d52cb2011-03-29 13:46:06 +08002403 spin_unlock(&ctl->tree_lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00002404 ret = 0;
2405 continue;
2406 }
2407 } else {
2408 start = entry->offset;
2409 bytes = min(entry->bytes, end - start);
Li Zefan34d52cb2011-03-29 13:46:06 +08002410 unlink_free_space(ctl, entry);
Li Zefanf789b682011-04-25 19:43:52 -04002411 kmem_cache_free(btrfs_free_space_cachep, entry);
Li Dongyangf7039b12011-03-24 10:24:28 +00002412 }
2413
Li Zefan34d52cb2011-03-29 13:46:06 +08002414 spin_unlock(&ctl->tree_lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00002415
2416 if (bytes >= minlen) {
2417 int update_ret;
2418 update_ret = btrfs_update_reserved_bytes(block_group,
2419 bytes, 1, 1);
2420
2421 ret = btrfs_error_discard_extent(fs_info->extent_root,
2422 start,
2423 bytes,
2424 &actually_trimmed);
2425
Li Zefan34d52cb2011-03-29 13:46:06 +08002426 btrfs_add_free_space(block_group, start, bytes);
Li Dongyangf7039b12011-03-24 10:24:28 +00002427 if (!update_ret)
2428 btrfs_update_reserved_bytes(block_group,
2429 bytes, 0, 1);
2430
2431 if (ret)
2432 break;
2433 *trimmed += actually_trimmed;
2434 }
2435 start += bytes;
2436 bytes = 0;
2437
2438 if (fatal_signal_pending(current)) {
2439 ret = -ERESTARTSYS;
2440 break;
2441 }
2442
2443 cond_resched();
2444 }
2445
2446 return ret;
2447}
Li Zefan581bb052011-04-20 10:06:11 +08002448
2449/*
2450 * Find the left-most item in the cache tree, and then return the
2451 * smallest inode number in the item.
2452 *
2453 * Note: the returned inode number may not be the smallest one in
2454 * the tree, if the left-most item is a bitmap.
2455 */
2456u64 btrfs_find_ino_for_alloc(struct btrfs_root *fs_root)
2457{
2458 struct btrfs_free_space_ctl *ctl = fs_root->free_ino_ctl;
2459 struct btrfs_free_space *entry = NULL;
2460 u64 ino = 0;
2461
2462 spin_lock(&ctl->tree_lock);
2463
2464 if (RB_EMPTY_ROOT(&ctl->free_space_offset))
2465 goto out;
2466
2467 entry = rb_entry(rb_first(&ctl->free_space_offset),
2468 struct btrfs_free_space, offset_index);
2469
2470 if (!entry->bitmap) {
2471 ino = entry->offset;
2472
2473 unlink_free_space(ctl, entry);
2474 entry->offset++;
2475 entry->bytes--;
2476 if (!entry->bytes)
2477 kmem_cache_free(btrfs_free_space_cachep, entry);
2478 else
2479 link_free_space(ctl, entry);
2480 } else {
2481 u64 offset = 0;
2482 u64 count = 1;
2483 int ret;
2484
2485 ret = search_bitmap(ctl, entry, &offset, &count);
2486 BUG_ON(ret);
2487
2488 ino = offset;
2489 bitmap_clear_bits(ctl, entry, offset, 1);
2490 if (entry->bytes == 0)
2491 free_bitmap(ctl, entry);
2492 }
2493out:
2494 spin_unlock(&ctl->tree_lock);
2495
2496 return ino;
2497}
Li Zefan82d59022011-04-20 10:33:24 +08002498
2499struct inode *lookup_free_ino_inode(struct btrfs_root *root,
2500 struct btrfs_path *path)
2501{
2502 struct inode *inode = NULL;
2503
2504 spin_lock(&root->cache_lock);
2505 if (root->cache_inode)
2506 inode = igrab(root->cache_inode);
2507 spin_unlock(&root->cache_lock);
2508 if (inode)
2509 return inode;
2510
2511 inode = __lookup_free_space_inode(root, path, 0);
2512 if (IS_ERR(inode))
2513 return inode;
2514
2515 spin_lock(&root->cache_lock);
2516 if (!root->fs_info->closing)
2517 root->cache_inode = igrab(inode);
2518 spin_unlock(&root->cache_lock);
2519
2520 return inode;
2521}
2522
2523int create_free_ino_inode(struct btrfs_root *root,
2524 struct btrfs_trans_handle *trans,
2525 struct btrfs_path *path)
2526{
2527 return __create_free_space_inode(root, trans, path,
2528 BTRFS_FREE_INO_OBJECTID, 0);
2529}
2530
2531int load_free_ino_cache(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
2532{
2533 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
2534 struct btrfs_path *path;
2535 struct inode *inode;
2536 int ret = 0;
2537 u64 root_gen = btrfs_root_generation(&root->root_item);
2538
Chris Mason4b9465c2011-06-03 09:36:29 -04002539 if (!btrfs_test_opt(root, INODE_MAP_CACHE))
2540 return 0;
2541
Li Zefan82d59022011-04-20 10:33:24 +08002542 /*
2543 * If we're unmounting then just return, since this does a search on the
2544 * normal root and not the commit root and we could deadlock.
2545 */
2546 smp_mb();
2547 if (fs_info->closing)
2548 return 0;
2549
2550 path = btrfs_alloc_path();
2551 if (!path)
2552 return 0;
2553
2554 inode = lookup_free_ino_inode(root, path);
2555 if (IS_ERR(inode))
2556 goto out;
2557
2558 if (root_gen != BTRFS_I(inode)->generation)
2559 goto out_put;
2560
2561 ret = __load_free_space_cache(root, inode, ctl, path, 0);
2562
2563 if (ret < 0)
2564 printk(KERN_ERR "btrfs: failed to load free ino cache for "
2565 "root %llu\n", root->root_key.objectid);
2566out_put:
2567 iput(inode);
2568out:
2569 btrfs_free_path(path);
2570 return ret;
2571}
2572
2573int btrfs_write_out_ino_cache(struct btrfs_root *root,
2574 struct btrfs_trans_handle *trans,
2575 struct btrfs_path *path)
2576{
2577 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
2578 struct inode *inode;
2579 int ret;
2580
Chris Mason4b9465c2011-06-03 09:36:29 -04002581 if (!btrfs_test_opt(root, INODE_MAP_CACHE))
2582 return 0;
2583
Li Zefan82d59022011-04-20 10:33:24 +08002584 inode = lookup_free_ino_inode(root, path);
2585 if (IS_ERR(inode))
2586 return 0;
2587
2588 ret = __btrfs_write_out_cache(root, inode, ctl, NULL, trans, path, 0);
2589 if (ret < 0)
2590 printk(KERN_ERR "btrfs: failed to write free ino cache "
2591 "for root %llu\n", root->root_key.objectid);
2592
2593 iput(inode);
2594 return ret;
2595}