blob: fcbdcef6ca28386389ccf59e61e3a979db4b3d8e [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) {
56 btrfs_release_path(root, path);
57 return ERR_PTR(-ENOENT);
58 }
59
60 leaf = path->nodes[0];
61 header = btrfs_item_ptr(leaf, path->slots[0],
62 struct btrfs_free_space_header);
63 btrfs_free_space_key(leaf, header, &disk_key);
64 btrfs_disk_key_to_cpu(&location, &disk_key);
65 btrfs_release_path(root, path);
66
67 inode = btrfs_iget(root->fs_info->sb, &location, root, NULL);
68 if (!inode)
69 return ERR_PTR(-ENOENT);
70 if (IS_ERR(inode))
71 return inode;
72 if (is_bad_inode(inode)) {
73 iput(inode);
74 return ERR_PTR(-ENOENT);
75 }
76
Miao 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);
143 btrfs_release_path(root, path);
144
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) {
152 btrfs_release_path(root, path);
153 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);
161 btrfs_release_path(root, path);
162
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
212 return btrfs_update_inode(trans, root, inode);
213}
214
Josef Bacik9d66e232010-08-25 16:54:15 -0400215static int readahead_cache(struct inode *inode)
216{
217 struct file_ra_state *ra;
218 unsigned long last_index;
219
220 ra = kzalloc(sizeof(*ra), GFP_NOFS);
221 if (!ra)
222 return -ENOMEM;
223
224 file_ra_state_init(ra, inode->i_mapping);
225 last_index = (i_size_read(inode) - 1) >> PAGE_CACHE_SHIFT;
226
227 page_cache_sync_readahead(inode->i_mapping, ra, NULL, 0, last_index);
228
229 kfree(ra);
230
231 return 0;
232}
233
Li Zefan0414efa2011-04-20 10:20:14 +0800234int __load_free_space_cache(struct btrfs_root *root, struct inode *inode,
235 struct btrfs_free_space_ctl *ctl,
236 struct btrfs_path *path, u64 offset)
Josef Bacik9d66e232010-08-25 16:54:15 -0400237{
Josef Bacik9d66e232010-08-25 16:54:15 -0400238 struct btrfs_free_space_header *header;
239 struct extent_buffer *leaf;
240 struct page *page;
Josef Bacik9d66e232010-08-25 16:54:15 -0400241 u32 *checksums = NULL, *crc;
242 char *disk_crcs = NULL;
243 struct btrfs_key key;
244 struct list_head bitmaps;
245 u64 num_entries;
246 u64 num_bitmaps;
247 u64 generation;
248 u32 cur_crc = ~(u32)0;
249 pgoff_t index = 0;
250 unsigned long first_page_offset;
251 int num_checksums;
Li Zefan0414efa2011-04-20 10:20:14 +0800252 int ret = 0, ret2;
Josef Bacik9d66e232010-08-25 16:54:15 -0400253
254 INIT_LIST_HEAD(&bitmaps);
255
Josef Bacik9d66e232010-08-25 16:54:15 -0400256 /* Nothing in the space cache, goodbye */
Li Zefan0414efa2011-04-20 10:20:14 +0800257 if (!i_size_read(inode))
Josef Bacik9d66e232010-08-25 16:54:15 -0400258 goto out;
Josef Bacik9d66e232010-08-25 16:54:15 -0400259
260 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
Li Zefan0414efa2011-04-20 10:20:14 +0800261 key.offset = offset;
Josef Bacik9d66e232010-08-25 16:54:15 -0400262 key.type = 0;
263
264 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Li Zefan0414efa2011-04-20 10:20:14 +0800265 if (ret < 0)
266 goto out;
267 else if (ret > 0) {
268 btrfs_release_path(root, path);
269 ret = 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400270 goto out;
271 }
272
Li Zefan0414efa2011-04-20 10:20:14 +0800273 ret = -1;
274
Josef Bacik9d66e232010-08-25 16:54:15 -0400275 leaf = path->nodes[0];
276 header = btrfs_item_ptr(leaf, path->slots[0],
277 struct btrfs_free_space_header);
278 num_entries = btrfs_free_space_entries(leaf, header);
279 num_bitmaps = btrfs_free_space_bitmaps(leaf, header);
280 generation = btrfs_free_space_generation(leaf, header);
Li Zefan0414efa2011-04-20 10:20:14 +0800281 btrfs_release_path(root, path);
Josef Bacik9d66e232010-08-25 16:54:15 -0400282
283 if (BTRFS_I(inode)->generation != generation) {
284 printk(KERN_ERR "btrfs: free space inode generation (%llu) did"
Li Zefan0414efa2011-04-20 10:20:14 +0800285 " not match free space cache generation (%llu)\n",
Josef Bacik9d66e232010-08-25 16:54:15 -0400286 (unsigned long long)BTRFS_I(inode)->generation,
Li Zefan0414efa2011-04-20 10:20:14 +0800287 (unsigned long long)generation);
288 goto out;
Josef Bacik9d66e232010-08-25 16:54:15 -0400289 }
290
291 if (!num_entries)
292 goto out;
293
294 /* Setup everything for doing checksumming */
295 num_checksums = i_size_read(inode) / PAGE_CACHE_SIZE;
296 checksums = crc = kzalloc(sizeof(u32) * num_checksums, GFP_NOFS);
297 if (!checksums)
298 goto out;
299 first_page_offset = (sizeof(u32) * num_checksums) + sizeof(u64);
300 disk_crcs = kzalloc(first_page_offset, GFP_NOFS);
301 if (!disk_crcs)
302 goto out;
303
304 ret = readahead_cache(inode);
Li Zefan0414efa2011-04-20 10:20:14 +0800305 if (ret)
Josef Bacik9d66e232010-08-25 16:54:15 -0400306 goto out;
Josef Bacik9d66e232010-08-25 16:54:15 -0400307
308 while (1) {
309 struct btrfs_free_space_entry *entry;
310 struct btrfs_free_space *e;
311 void *addr;
312 unsigned long offset = 0;
313 unsigned long start_offset = 0;
314 int need_loop = 0;
315
316 if (!num_entries && !num_bitmaps)
317 break;
318
319 if (index == 0) {
320 start_offset = first_page_offset;
321 offset = start_offset;
322 }
323
324 page = grab_cache_page(inode->i_mapping, index);
Li Zefan0414efa2011-04-20 10:20:14 +0800325 if (!page)
Josef Bacik9d66e232010-08-25 16:54:15 -0400326 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400327
328 if (!PageUptodate(page)) {
329 btrfs_readpage(NULL, page);
330 lock_page(page);
331 if (!PageUptodate(page)) {
332 unlock_page(page);
333 page_cache_release(page);
334 printk(KERN_ERR "btrfs: error reading free "
Li Zefan0414efa2011-04-20 10:20:14 +0800335 "space cache\n");
Josef Bacik9d66e232010-08-25 16:54:15 -0400336 goto free_cache;
337 }
338 }
339 addr = kmap(page);
340
341 if (index == 0) {
342 u64 *gen;
343
344 memcpy(disk_crcs, addr, first_page_offset);
345 gen = addr + (sizeof(u32) * num_checksums);
346 if (*gen != BTRFS_I(inode)->generation) {
347 printk(KERN_ERR "btrfs: space cache generation"
Li Zefan0414efa2011-04-20 10:20:14 +0800348 " (%llu) does not match inode (%llu)\n",
Josef Bacik9d66e232010-08-25 16:54:15 -0400349 (unsigned long long)*gen,
350 (unsigned long long)
Li Zefan0414efa2011-04-20 10:20:14 +0800351 BTRFS_I(inode)->generation);
Josef Bacik9d66e232010-08-25 16:54:15 -0400352 kunmap(page);
353 unlock_page(page);
354 page_cache_release(page);
355 goto free_cache;
356 }
357 crc = (u32 *)disk_crcs;
358 }
359 entry = addr + start_offset;
360
361 /* First lets check our crc before we do anything fun */
362 cur_crc = ~(u32)0;
363 cur_crc = btrfs_csum_data(root, addr + start_offset, cur_crc,
364 PAGE_CACHE_SIZE - start_offset);
365 btrfs_csum_final(cur_crc, (char *)&cur_crc);
366 if (cur_crc != *crc) {
Li Zefan0414efa2011-04-20 10:20:14 +0800367 printk(KERN_ERR "btrfs: crc mismatch for page %lu\n",
368 index);
Josef Bacik9d66e232010-08-25 16:54:15 -0400369 kunmap(page);
370 unlock_page(page);
371 page_cache_release(page);
372 goto free_cache;
373 }
374 crc++;
375
376 while (1) {
377 if (!num_entries)
378 break;
379
380 need_loop = 1;
Josef Bacikdc89e982011-01-28 17:05:48 -0500381 e = kmem_cache_zalloc(btrfs_free_space_cachep,
382 GFP_NOFS);
Josef Bacik9d66e232010-08-25 16:54:15 -0400383 if (!e) {
384 kunmap(page);
385 unlock_page(page);
386 page_cache_release(page);
387 goto free_cache;
388 }
389
390 e->offset = le64_to_cpu(entry->offset);
391 e->bytes = le64_to_cpu(entry->bytes);
392 if (!e->bytes) {
393 kunmap(page);
Josef Bacikdc89e982011-01-28 17:05:48 -0500394 kmem_cache_free(btrfs_free_space_cachep, e);
Josef Bacik9d66e232010-08-25 16:54:15 -0400395 unlock_page(page);
396 page_cache_release(page);
397 goto free_cache;
398 }
399
400 if (entry->type == BTRFS_FREE_SPACE_EXTENT) {
Li Zefan34d52cb2011-03-29 13:46:06 +0800401 spin_lock(&ctl->tree_lock);
402 ret = link_free_space(ctl, e);
403 spin_unlock(&ctl->tree_lock);
Josef Bacik9d66e232010-08-25 16:54:15 -0400404 BUG_ON(ret);
405 } else {
406 e->bitmap = kzalloc(PAGE_CACHE_SIZE, GFP_NOFS);
407 if (!e->bitmap) {
408 kunmap(page);
Josef Bacikdc89e982011-01-28 17:05:48 -0500409 kmem_cache_free(
410 btrfs_free_space_cachep, e);
Josef Bacik9d66e232010-08-25 16:54:15 -0400411 unlock_page(page);
412 page_cache_release(page);
413 goto free_cache;
414 }
Li Zefan34d52cb2011-03-29 13:46:06 +0800415 spin_lock(&ctl->tree_lock);
Li Zefan0414efa2011-04-20 10:20:14 +0800416 ret2 = link_free_space(ctl, e);
Li Zefan34d52cb2011-03-29 13:46:06 +0800417 ctl->total_bitmaps++;
418 ctl->op->recalc_thresholds(ctl);
419 spin_unlock(&ctl->tree_lock);
Josef Bacik9d66e232010-08-25 16:54:15 -0400420 list_add_tail(&e->list, &bitmaps);
421 }
422
423 num_entries--;
424 offset += sizeof(struct btrfs_free_space_entry);
425 if (offset + sizeof(struct btrfs_free_space_entry) >=
426 PAGE_CACHE_SIZE)
427 break;
428 entry++;
429 }
430
431 /*
432 * We read an entry out of this page, we need to move on to the
433 * next page.
434 */
435 if (need_loop) {
436 kunmap(page);
437 goto next;
438 }
439
440 /*
441 * We add the bitmaps at the end of the entries in order that
442 * the bitmap entries are added to the cache.
443 */
444 e = list_entry(bitmaps.next, struct btrfs_free_space, list);
445 list_del_init(&e->list);
446 memcpy(e->bitmap, addr, PAGE_CACHE_SIZE);
447 kunmap(page);
448 num_bitmaps--;
449next:
450 unlock_page(page);
451 page_cache_release(page);
452 index++;
453 }
454
455 ret = 1;
456out:
457 kfree(checksums);
458 kfree(disk_crcs);
Josef Bacik9d66e232010-08-25 16:54:15 -0400459 return ret;
Josef Bacik9d66e232010-08-25 16:54:15 -0400460free_cache:
Li Zefan0414efa2011-04-20 10:20:14 +0800461 __btrfs_remove_free_space_cache(ctl);
Josef Bacik9d66e232010-08-25 16:54:15 -0400462 goto out;
463}
464
Li Zefan0414efa2011-04-20 10:20:14 +0800465int load_free_space_cache(struct btrfs_fs_info *fs_info,
466 struct btrfs_block_group_cache *block_group)
Josef Bacik0cb59c92010-07-02 12:14:14 -0400467{
Li Zefan34d52cb2011-03-29 13:46:06 +0800468 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Li Zefan0414efa2011-04-20 10:20:14 +0800469 struct btrfs_root *root = fs_info->tree_root;
470 struct inode *inode;
471 struct btrfs_path *path;
472 int ret;
473 bool matched;
474 u64 used = btrfs_block_group_used(&block_group->item);
475
476 /*
477 * If we're unmounting then just return, since this does a search on the
478 * normal root and not the commit root and we could deadlock.
479 */
480 smp_mb();
481 if (fs_info->closing)
482 return 0;
483
484 /*
485 * If this block group has been marked to be cleared for one reason or
486 * another then we can't trust the on disk cache, so just return.
487 */
488 spin_lock(&block_group->lock);
489 if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) {
490 spin_unlock(&block_group->lock);
491 return 0;
492 }
493 spin_unlock(&block_group->lock);
494
495 path = btrfs_alloc_path();
496 if (!path)
497 return 0;
498
499 inode = lookup_free_space_inode(root, block_group, path);
500 if (IS_ERR(inode)) {
501 btrfs_free_path(path);
502 return 0;
503 }
504
505 ret = __load_free_space_cache(fs_info->tree_root, inode, ctl,
506 path, block_group->key.objectid);
507 btrfs_free_path(path);
508 if (ret <= 0)
509 goto out;
510
511 spin_lock(&ctl->tree_lock);
512 matched = (ctl->free_space == (block_group->key.offset - used -
513 block_group->bytes_super));
514 spin_unlock(&ctl->tree_lock);
515
516 if (!matched) {
517 __btrfs_remove_free_space_cache(ctl);
518 printk(KERN_ERR "block group %llu has an wrong amount of free "
519 "space\n", block_group->key.objectid);
520 ret = -1;
521 }
522out:
523 if (ret < 0) {
524 /* This cache is bogus, make sure it gets cleared */
525 spin_lock(&block_group->lock);
526 block_group->disk_cache_state = BTRFS_DC_CLEAR;
527 spin_unlock(&block_group->lock);
528
529 printk(KERN_ERR "btrfs: failed to load free space cache "
530 "for block group %llu\n", block_group->key.objectid);
531 }
532
533 iput(inode);
534 return ret;
535}
536
537int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode,
538 struct btrfs_free_space_ctl *ctl,
539 struct btrfs_block_group_cache *block_group,
540 struct btrfs_trans_handle *trans,
541 struct btrfs_path *path, u64 offset)
542{
Josef Bacik0cb59c92010-07-02 12:14:14 -0400543 struct btrfs_free_space_header *header;
544 struct extent_buffer *leaf;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400545 struct rb_node *node;
546 struct list_head *pos, *n;
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400547 struct page **pages;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400548 struct page *page;
549 struct extent_state *cached_state = NULL;
Josef Bacik43be2142011-04-01 14:55:00 +0000550 struct btrfs_free_cluster *cluster = NULL;
551 struct extent_io_tree *unpin = NULL;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400552 struct list_head bitmap_list;
553 struct btrfs_key key;
Josef Bacik43be2142011-04-01 14:55:00 +0000554 u64 start, end, len;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400555 u64 bytes = 0;
556 u32 *crc, *checksums;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400557 unsigned long first_page_offset;
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400558 int index = 0, num_pages = 0;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400559 int entries = 0;
560 int bitmaps = 0;
Li Zefan0414efa2011-04-20 10:20:14 +0800561 int ret = -1;
Josef Bacik43be2142011-04-01 14:55:00 +0000562 bool next_page = false;
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400563 bool out_of_space = false;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400564
Josef Bacik0cb59c92010-07-02 12:14:14 -0400565 INIT_LIST_HEAD(&bitmap_list);
566
Li Zefan34d52cb2011-03-29 13:46:06 +0800567 node = rb_first(&ctl->free_space_offset);
Li Zefan0414efa2011-04-20 10:20:14 +0800568 if (!node)
Josef Bacik2b209822010-12-03 13:17:53 -0500569 return 0;
Li Zefan0414efa2011-04-20 10:20:14 +0800570
571 if (!i_size_read(inode))
572 return -1;
Josef Bacik2b209822010-12-03 13:17:53 -0500573
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400574 num_pages = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
575 PAGE_CACHE_SHIFT;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400576 filemap_write_and_wait(inode->i_mapping);
577 btrfs_wait_ordered_range(inode, inode->i_size &
578 ~(root->sectorsize - 1), (u64)-1);
579
580 /* We need a checksum per page. */
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400581 crc = checksums = kzalloc(sizeof(u32) * num_pages, GFP_NOFS);
Li Zefan0414efa2011-04-20 10:20:14 +0800582 if (!crc)
583 return -1;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400584
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400585 pages = kzalloc(sizeof(struct page *) * num_pages, GFP_NOFS);
586 if (!pages) {
587 kfree(crc);
Li Zefan0414efa2011-04-20 10:20:14 +0800588 return -1;
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400589 }
590
Josef Bacik0cb59c92010-07-02 12:14:14 -0400591 /* Since the first page has all of our checksums and our generation we
592 * need to calculate the offset into the page that we can start writing
593 * our entries.
594 */
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400595 first_page_offset = (sizeof(u32) * num_pages) + sizeof(u64);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400596
Josef Bacik43be2142011-04-01 14:55:00 +0000597 /* Get the cluster for this block_group if it exists */
Li Zefan0414efa2011-04-20 10:20:14 +0800598 if (block_group && !list_empty(&block_group->cluster_list))
Josef Bacik43be2142011-04-01 14:55:00 +0000599 cluster = list_entry(block_group->cluster_list.next,
600 struct btrfs_free_cluster,
601 block_group_list);
602
603 /*
604 * We shouldn't have switched the pinned extents yet so this is the
605 * right one
606 */
607 unpin = root->fs_info->pinned_extents;
608
Josef Bacik0cb59c92010-07-02 12:14:14 -0400609 /*
610 * Lock all pages first so we can lock the extent safely.
611 *
612 * NOTE: Because we hold the ref the entire time we're going to write to
613 * the page find_get_page should never fail, so we don't do a check
614 * after find_get_page at this point. Just putting this here so people
615 * know and don't freak out.
616 */
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400617 while (index < num_pages) {
Josef Bacik0cb59c92010-07-02 12:14:14 -0400618 page = grab_cache_page(inode->i_mapping, index);
619 if (!page) {
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400620 int i;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400621
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400622 for (i = 0; i < num_pages; i++) {
623 unlock_page(pages[i]);
624 page_cache_release(pages[i]);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400625 }
626 goto out_free;
627 }
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400628 pages[index] = page;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400629 index++;
630 }
631
632 index = 0;
633 lock_extent_bits(&BTRFS_I(inode)->io_tree, 0, i_size_read(inode) - 1,
634 0, &cached_state, GFP_NOFS);
635
Josef Bacik43be2142011-04-01 14:55:00 +0000636 /*
637 * When searching for pinned extents, we need to start at our start
638 * offset.
639 */
Li Zefan0414efa2011-04-20 10:20:14 +0800640 if (block_group)
641 start = block_group->key.objectid;
Josef Bacik43be2142011-04-01 14:55:00 +0000642
Josef Bacik0cb59c92010-07-02 12:14:14 -0400643 /* Write out the extent entries */
644 do {
645 struct btrfs_free_space_entry *entry;
646 void *addr;
647 unsigned long offset = 0;
648 unsigned long start_offset = 0;
649
Josef Bacik43be2142011-04-01 14:55:00 +0000650 next_page = false;
651
Josef Bacik0cb59c92010-07-02 12:14:14 -0400652 if (index == 0) {
653 start_offset = first_page_offset;
654 offset = start_offset;
655 }
656
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400657 if (index >= num_pages) {
658 out_of_space = true;
659 break;
660 }
661
662 page = pages[index];
Josef Bacik0cb59c92010-07-02 12:14:14 -0400663
664 addr = kmap(page);
665 entry = addr + start_offset;
666
667 memset(addr, 0, PAGE_CACHE_SIZE);
Josef Bacik43be2142011-04-01 14:55:00 +0000668 while (node && !next_page) {
Josef Bacik0cb59c92010-07-02 12:14:14 -0400669 struct btrfs_free_space *e;
670
671 e = rb_entry(node, struct btrfs_free_space, offset_index);
672 entries++;
673
674 entry->offset = cpu_to_le64(e->offset);
675 entry->bytes = cpu_to_le64(e->bytes);
676 if (e->bitmap) {
677 entry->type = BTRFS_FREE_SPACE_BITMAP;
678 list_add_tail(&e->list, &bitmap_list);
679 bitmaps++;
680 } else {
681 entry->type = BTRFS_FREE_SPACE_EXTENT;
682 }
683 node = rb_next(node);
Josef Bacik43be2142011-04-01 14:55:00 +0000684 if (!node && cluster) {
685 node = rb_first(&cluster->root);
686 cluster = NULL;
687 }
Josef Bacik0cb59c92010-07-02 12:14:14 -0400688 offset += sizeof(struct btrfs_free_space_entry);
689 if (offset + sizeof(struct btrfs_free_space_entry) >=
690 PAGE_CACHE_SIZE)
Josef Bacik43be2142011-04-01 14:55:00 +0000691 next_page = true;
692 entry++;
693 }
694
695 /*
696 * We want to add any pinned extents to our free space cache
697 * so we don't leak the space
698 */
Li Zefan0414efa2011-04-20 10:20:14 +0800699 while (block_group && !next_page &&
700 (start < block_group->key.objectid +
701 block_group->key.offset)) {
Josef Bacik43be2142011-04-01 14:55:00 +0000702 ret = find_first_extent_bit(unpin, start, &start, &end,
703 EXTENT_DIRTY);
704 if (ret) {
705 ret = 0;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400706 break;
Josef Bacik43be2142011-04-01 14:55:00 +0000707 }
708
709 /* This pinned extent is out of our range */
710 if (start >= block_group->key.objectid +
711 block_group->key.offset)
712 break;
713
714 len = block_group->key.objectid +
715 block_group->key.offset - start;
716 len = min(len, end + 1 - start);
717
718 entries++;
719 entry->offset = cpu_to_le64(start);
720 entry->bytes = cpu_to_le64(len);
721 entry->type = BTRFS_FREE_SPACE_EXTENT;
722
723 start = end + 1;
724 offset += sizeof(struct btrfs_free_space_entry);
725 if (offset + sizeof(struct btrfs_free_space_entry) >=
726 PAGE_CACHE_SIZE)
727 next_page = true;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400728 entry++;
729 }
730 *crc = ~(u32)0;
731 *crc = btrfs_csum_data(root, addr + start_offset, *crc,
732 PAGE_CACHE_SIZE - start_offset);
733 kunmap(page);
734
735 btrfs_csum_final(*crc, (char *)crc);
736 crc++;
737
738 bytes += PAGE_CACHE_SIZE;
739
Josef Bacik0cb59c92010-07-02 12:14:14 -0400740 index++;
Josef Bacik43be2142011-04-01 14:55:00 +0000741 } while (node || next_page);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400742
743 /* Write out the bitmaps */
744 list_for_each_safe(pos, n, &bitmap_list) {
745 void *addr;
746 struct btrfs_free_space *entry =
747 list_entry(pos, struct btrfs_free_space, list);
748
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400749 if (index >= num_pages) {
750 out_of_space = true;
751 break;
752 }
Chris Masonf65647c2011-04-18 08:55:34 -0400753 page = pages[index];
Josef Bacik0cb59c92010-07-02 12:14:14 -0400754
755 addr = kmap(page);
756 memcpy(addr, entry->bitmap, PAGE_CACHE_SIZE);
757 *crc = ~(u32)0;
758 *crc = btrfs_csum_data(root, addr, *crc, PAGE_CACHE_SIZE);
759 kunmap(page);
760 btrfs_csum_final(*crc, (char *)crc);
761 crc++;
762 bytes += PAGE_CACHE_SIZE;
763
Josef Bacik0cb59c92010-07-02 12:14:14 -0400764 list_del_init(&entry->list);
765 index++;
766 }
767
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400768 if (out_of_space) {
769 btrfs_drop_pages(pages, num_pages);
770 unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
771 i_size_read(inode) - 1, &cached_state,
772 GFP_NOFS);
773 ret = 0;
774 goto out_free;
775 }
776
Josef Bacik0cb59c92010-07-02 12:14:14 -0400777 /* Zero out the rest of the pages just to make sure */
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400778 while (index < num_pages) {
Josef Bacik0cb59c92010-07-02 12:14:14 -0400779 void *addr;
780
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400781 page = pages[index];
Josef Bacik0cb59c92010-07-02 12:14:14 -0400782 addr = kmap(page);
783 memset(addr, 0, PAGE_CACHE_SIZE);
784 kunmap(page);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400785 bytes += PAGE_CACHE_SIZE;
786 index++;
787 }
788
Josef Bacik0cb59c92010-07-02 12:14:14 -0400789 /* Write the checksums and trans id to the first page */
790 {
791 void *addr;
792 u64 *gen;
793
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400794 page = pages[0];
Josef Bacik0cb59c92010-07-02 12:14:14 -0400795
796 addr = kmap(page);
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400797 memcpy(addr, checksums, sizeof(u32) * num_pages);
798 gen = addr + (sizeof(u32) * num_pages);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400799 *gen = trans->transid;
800 kunmap(page);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400801 }
Josef Bacik0cb59c92010-07-02 12:14:14 -0400802
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400803 ret = btrfs_dirty_pages(root, inode, pages, num_pages, 0,
804 bytes, &cached_state);
805 btrfs_drop_pages(pages, num_pages);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400806 unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
807 i_size_read(inode) - 1, &cached_state, GFP_NOFS);
808
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400809 if (ret) {
810 ret = 0;
811 goto out_free;
812 }
813
814 BTRFS_I(inode)->generation = trans->transid;
815
Josef Bacik0cb59c92010-07-02 12:14:14 -0400816 filemap_write_and_wait(inode->i_mapping);
817
818 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
Li Zefan0414efa2011-04-20 10:20:14 +0800819 key.offset = offset;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400820 key.type = 0;
821
822 ret = btrfs_search_slot(trans, root, &key, path, 1, 1);
823 if (ret < 0) {
Li Zefan0414efa2011-04-20 10:20:14 +0800824 ret = -1;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400825 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, bytes - 1,
826 EXTENT_DIRTY | EXTENT_DELALLOC |
827 EXTENT_DO_ACCOUNTING, 0, 0, NULL, GFP_NOFS);
828 goto out_free;
829 }
830 leaf = path->nodes[0];
831 if (ret > 0) {
832 struct btrfs_key found_key;
833 BUG_ON(!path->slots[0]);
834 path->slots[0]--;
835 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
836 if (found_key.objectid != BTRFS_FREE_SPACE_OBJECTID ||
Li Zefan0414efa2011-04-20 10:20:14 +0800837 found_key.offset != offset) {
838 ret = -1;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400839 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, bytes - 1,
840 EXTENT_DIRTY | EXTENT_DELALLOC |
841 EXTENT_DO_ACCOUNTING, 0, 0, NULL,
842 GFP_NOFS);
843 btrfs_release_path(root, path);
844 goto out_free;
845 }
846 }
847 header = btrfs_item_ptr(leaf, path->slots[0],
848 struct btrfs_free_space_header);
849 btrfs_set_free_space_entries(leaf, header, entries);
850 btrfs_set_free_space_bitmaps(leaf, header, bitmaps);
851 btrfs_set_free_space_generation(leaf, header, trans->transid);
852 btrfs_mark_buffer_dirty(leaf);
853 btrfs_release_path(root, path);
854
855 ret = 1;
856
857out_free:
Li Zefan0414efa2011-04-20 10:20:14 +0800858 if (ret != 1) {
Josef Bacik0cb59c92010-07-02 12:14:14 -0400859 invalidate_inode_pages2_range(inode->i_mapping, 0, index);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400860 BTRFS_I(inode)->generation = 0;
861 }
862 kfree(checksums);
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400863 kfree(pages);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400864 btrfs_update_inode(trans, root, inode);
Li Zefan0414efa2011-04-20 10:20:14 +0800865 return ret;
866}
867
868int btrfs_write_out_cache(struct btrfs_root *root,
869 struct btrfs_trans_handle *trans,
870 struct btrfs_block_group_cache *block_group,
871 struct btrfs_path *path)
872{
873 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
874 struct inode *inode;
875 int ret = 0;
876
877 root = root->fs_info->tree_root;
878
879 spin_lock(&block_group->lock);
880 if (block_group->disk_cache_state < BTRFS_DC_SETUP) {
881 spin_unlock(&block_group->lock);
882 return 0;
883 }
884 spin_unlock(&block_group->lock);
885
886 inode = lookup_free_space_inode(root, block_group, path);
887 if (IS_ERR(inode))
888 return 0;
889
890 ret = __btrfs_write_out_cache(root, inode, ctl, block_group, trans,
891 path, block_group->key.objectid);
892 if (ret < 0) {
893 spin_lock(&block_group->lock);
894 block_group->disk_cache_state = BTRFS_DC_ERROR;
895 spin_unlock(&block_group->lock);
896
897 printk(KERN_ERR "btrfs: failed to write free space cace "
898 "for block group %llu\n", block_group->key.objectid);
899 }
900
Josef Bacik0cb59c92010-07-02 12:14:14 -0400901 iput(inode);
902 return ret;
903}
904
Li Zefan34d52cb2011-03-29 13:46:06 +0800905static inline unsigned long offset_to_bit(u64 bitmap_start, u32 unit,
Josef Bacik96303082009-07-13 21:29:25 -0400906 u64 offset)
907{
908 BUG_ON(offset < bitmap_start);
909 offset -= bitmap_start;
Li Zefan34d52cb2011-03-29 13:46:06 +0800910 return (unsigned long)(div_u64(offset, unit));
Josef Bacik96303082009-07-13 21:29:25 -0400911}
912
Li Zefan34d52cb2011-03-29 13:46:06 +0800913static inline unsigned long bytes_to_bits(u64 bytes, u32 unit)
Josef Bacik96303082009-07-13 21:29:25 -0400914{
Li Zefan34d52cb2011-03-29 13:46:06 +0800915 return (unsigned long)(div_u64(bytes, unit));
Josef Bacik96303082009-07-13 21:29:25 -0400916}
917
Li Zefan34d52cb2011-03-29 13:46:06 +0800918static inline u64 offset_to_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -0400919 u64 offset)
920{
921 u64 bitmap_start;
922 u64 bytes_per_bitmap;
923
Li Zefan34d52cb2011-03-29 13:46:06 +0800924 bytes_per_bitmap = BITS_PER_BITMAP * ctl->unit;
925 bitmap_start = offset - ctl->start;
Josef Bacik96303082009-07-13 21:29:25 -0400926 bitmap_start = div64_u64(bitmap_start, bytes_per_bitmap);
927 bitmap_start *= bytes_per_bitmap;
Li Zefan34d52cb2011-03-29 13:46:06 +0800928 bitmap_start += ctl->start;
Josef Bacik96303082009-07-13 21:29:25 -0400929
930 return bitmap_start;
931}
Josef Bacik0f9dd462008-09-23 13:14:11 -0400932
933static int tree_insert_offset(struct rb_root *root, u64 offset,
Josef Bacik96303082009-07-13 21:29:25 -0400934 struct rb_node *node, int bitmap)
Josef Bacik0f9dd462008-09-23 13:14:11 -0400935{
936 struct rb_node **p = &root->rb_node;
937 struct rb_node *parent = NULL;
938 struct btrfs_free_space *info;
939
940 while (*p) {
941 parent = *p;
942 info = rb_entry(parent, struct btrfs_free_space, offset_index);
943
Josef Bacik96303082009-07-13 21:29:25 -0400944 if (offset < info->offset) {
Josef Bacik0f9dd462008-09-23 13:14:11 -0400945 p = &(*p)->rb_left;
Josef Bacik96303082009-07-13 21:29:25 -0400946 } else if (offset > info->offset) {
Josef Bacik0f9dd462008-09-23 13:14:11 -0400947 p = &(*p)->rb_right;
Josef Bacik96303082009-07-13 21:29:25 -0400948 } else {
949 /*
950 * we could have a bitmap entry and an extent entry
951 * share the same offset. If this is the case, we want
952 * the extent entry to always be found first if we do a
953 * linear search through the tree, since we want to have
954 * the quickest allocation time, and allocating from an
955 * extent is faster than allocating from a bitmap. So
956 * if we're inserting a bitmap and we find an entry at
957 * this offset, we want to go right, or after this entry
958 * logically. If we are inserting an extent and we've
959 * found a bitmap, we want to go left, or before
960 * logically.
961 */
962 if (bitmap) {
963 WARN_ON(info->bitmap);
964 p = &(*p)->rb_right;
965 } else {
966 WARN_ON(!info->bitmap);
967 p = &(*p)->rb_left;
968 }
969 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400970 }
971
972 rb_link_node(node, parent, p);
973 rb_insert_color(node, root);
974
975 return 0;
976}
977
978/*
Josef Bacik70cb0742009-04-03 10:14:19 -0400979 * searches the tree for the given offset.
980 *
Josef Bacik96303082009-07-13 21:29:25 -0400981 * fuzzy - If this is set, then we are trying to make an allocation, and we just
982 * want a section that has at least bytes size and comes at or after the given
983 * offset.
Josef Bacik0f9dd462008-09-23 13:14:11 -0400984 */
Josef Bacik96303082009-07-13 21:29:25 -0400985static struct btrfs_free_space *
Li Zefan34d52cb2011-03-29 13:46:06 +0800986tree_search_offset(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -0400987 u64 offset, int bitmap_only, int fuzzy)
Josef Bacik0f9dd462008-09-23 13:14:11 -0400988{
Li Zefan34d52cb2011-03-29 13:46:06 +0800989 struct rb_node *n = ctl->free_space_offset.rb_node;
Josef Bacik96303082009-07-13 21:29:25 -0400990 struct btrfs_free_space *entry, *prev = NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400991
Josef Bacik96303082009-07-13 21:29:25 -0400992 /* find entry that is closest to the 'offset' */
993 while (1) {
994 if (!n) {
995 entry = NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400996 break;
997 }
Josef Bacik96303082009-07-13 21:29:25 -0400998
999 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1000 prev = entry;
1001
1002 if (offset < entry->offset)
1003 n = n->rb_left;
1004 else if (offset > entry->offset)
1005 n = n->rb_right;
1006 else
1007 break;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001008 }
1009
Josef Bacik96303082009-07-13 21:29:25 -04001010 if (bitmap_only) {
1011 if (!entry)
1012 return NULL;
1013 if (entry->bitmap)
1014 return entry;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001015
Josef Bacik96303082009-07-13 21:29:25 -04001016 /*
1017 * bitmap entry and extent entry may share same offset,
1018 * in that case, bitmap entry comes after extent entry.
1019 */
1020 n = rb_next(n);
1021 if (!n)
1022 return NULL;
1023 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1024 if (entry->offset != offset)
1025 return NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001026
Josef Bacik96303082009-07-13 21:29:25 -04001027 WARN_ON(!entry->bitmap);
1028 return entry;
1029 } else if (entry) {
1030 if (entry->bitmap) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001031 /*
Josef Bacik96303082009-07-13 21:29:25 -04001032 * if previous extent entry covers the offset,
1033 * we should return it instead of the bitmap entry
Josef Bacik0f9dd462008-09-23 13:14:11 -04001034 */
Josef Bacik96303082009-07-13 21:29:25 -04001035 n = &entry->offset_index;
1036 while (1) {
1037 n = rb_prev(n);
1038 if (!n)
1039 break;
1040 prev = rb_entry(n, struct btrfs_free_space,
1041 offset_index);
1042 if (!prev->bitmap) {
1043 if (prev->offset + prev->bytes > offset)
1044 entry = prev;
1045 break;
1046 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04001047 }
Josef Bacik96303082009-07-13 21:29:25 -04001048 }
1049 return entry;
1050 }
1051
1052 if (!prev)
1053 return NULL;
1054
1055 /* find last entry before the 'offset' */
1056 entry = prev;
1057 if (entry->offset > offset) {
1058 n = rb_prev(&entry->offset_index);
1059 if (n) {
1060 entry = rb_entry(n, struct btrfs_free_space,
1061 offset_index);
1062 BUG_ON(entry->offset > offset);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001063 } else {
Josef Bacik96303082009-07-13 21:29:25 -04001064 if (fuzzy)
1065 return entry;
1066 else
1067 return NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001068 }
1069 }
1070
Josef Bacik96303082009-07-13 21:29:25 -04001071 if (entry->bitmap) {
1072 n = &entry->offset_index;
1073 while (1) {
1074 n = rb_prev(n);
1075 if (!n)
1076 break;
1077 prev = rb_entry(n, struct btrfs_free_space,
1078 offset_index);
1079 if (!prev->bitmap) {
1080 if (prev->offset + prev->bytes > offset)
1081 return prev;
1082 break;
1083 }
1084 }
Li Zefan34d52cb2011-03-29 13:46:06 +08001085 if (entry->offset + BITS_PER_BITMAP * ctl->unit > offset)
Josef Bacik96303082009-07-13 21:29:25 -04001086 return entry;
1087 } else if (entry->offset + entry->bytes > offset)
1088 return entry;
1089
1090 if (!fuzzy)
1091 return NULL;
1092
1093 while (1) {
1094 if (entry->bitmap) {
1095 if (entry->offset + BITS_PER_BITMAP *
Li Zefan34d52cb2011-03-29 13:46:06 +08001096 ctl->unit > offset)
Josef Bacik96303082009-07-13 21:29:25 -04001097 break;
1098 } else {
1099 if (entry->offset + entry->bytes > offset)
1100 break;
1101 }
1102
1103 n = rb_next(&entry->offset_index);
1104 if (!n)
1105 return NULL;
1106 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1107 }
1108 return entry;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001109}
1110
Li Zefanf333adb2010-11-09 14:57:39 +08001111static inline void
Li Zefan34d52cb2011-03-29 13:46:06 +08001112__unlink_free_space(struct btrfs_free_space_ctl *ctl,
Li Zefanf333adb2010-11-09 14:57:39 +08001113 struct btrfs_free_space *info)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001114{
Li Zefan34d52cb2011-03-29 13:46:06 +08001115 rb_erase(&info->offset_index, &ctl->free_space_offset);
1116 ctl->free_extents--;
Li Zefanf333adb2010-11-09 14:57:39 +08001117}
1118
Li Zefan34d52cb2011-03-29 13:46:06 +08001119static void unlink_free_space(struct btrfs_free_space_ctl *ctl,
Li Zefanf333adb2010-11-09 14:57:39 +08001120 struct btrfs_free_space *info)
1121{
Li Zefan34d52cb2011-03-29 13:46:06 +08001122 __unlink_free_space(ctl, info);
1123 ctl->free_space -= info->bytes;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001124}
1125
Li Zefan34d52cb2011-03-29 13:46:06 +08001126static int link_free_space(struct btrfs_free_space_ctl *ctl,
Josef Bacik0f9dd462008-09-23 13:14:11 -04001127 struct btrfs_free_space *info)
1128{
1129 int ret = 0;
1130
Josef Bacik96303082009-07-13 21:29:25 -04001131 BUG_ON(!info->bitmap && !info->bytes);
Li Zefan34d52cb2011-03-29 13:46:06 +08001132 ret = tree_insert_offset(&ctl->free_space_offset, info->offset,
Josef Bacik96303082009-07-13 21:29:25 -04001133 &info->offset_index, (info->bitmap != NULL));
Josef Bacik0f9dd462008-09-23 13:14:11 -04001134 if (ret)
1135 return ret;
1136
Li Zefan34d52cb2011-03-29 13:46:06 +08001137 ctl->free_space += info->bytes;
1138 ctl->free_extents++;
Josef Bacik96303082009-07-13 21:29:25 -04001139 return ret;
1140}
1141
Li Zefan34d52cb2011-03-29 13:46:06 +08001142static void recalculate_thresholds(struct btrfs_free_space_ctl *ctl)
Josef Bacik96303082009-07-13 21:29:25 -04001143{
Li Zefan34d52cb2011-03-29 13:46:06 +08001144 struct btrfs_block_group_cache *block_group = ctl->private;
Josef Bacik25891f72009-09-11 16:11:20 -04001145 u64 max_bytes;
1146 u64 bitmap_bytes;
1147 u64 extent_bytes;
Li Zefan8eb2d822010-11-09 14:48:01 +08001148 u64 size = block_group->key.offset;
Li Zefan34d52cb2011-03-29 13:46:06 +08001149 u64 bytes_per_bg = BITS_PER_BITMAP * block_group->sectorsize;
1150 int max_bitmaps = div64_u64(size + bytes_per_bg - 1, bytes_per_bg);
1151
1152 BUG_ON(ctl->total_bitmaps > max_bitmaps);
Josef Bacik96303082009-07-13 21:29:25 -04001153
1154 /*
1155 * The goal is to keep the total amount of memory used per 1gb of space
1156 * at or below 32k, so we need to adjust how much memory we allow to be
1157 * used by extent based free space tracking
1158 */
Li Zefan8eb2d822010-11-09 14:48:01 +08001159 if (size < 1024 * 1024 * 1024)
1160 max_bytes = MAX_CACHE_BYTES_PER_GIG;
1161 else
1162 max_bytes = MAX_CACHE_BYTES_PER_GIG *
1163 div64_u64(size, 1024 * 1024 * 1024);
Josef Bacik96303082009-07-13 21:29:25 -04001164
Josef Bacik25891f72009-09-11 16:11:20 -04001165 /*
1166 * we want to account for 1 more bitmap than what we have so we can make
1167 * sure we don't go over our overall goal of MAX_CACHE_BYTES_PER_GIG as
1168 * we add more bitmaps.
1169 */
Li Zefan34d52cb2011-03-29 13:46:06 +08001170 bitmap_bytes = (ctl->total_bitmaps + 1) * PAGE_CACHE_SIZE;
Josef Bacik96303082009-07-13 21:29:25 -04001171
Josef Bacik25891f72009-09-11 16:11:20 -04001172 if (bitmap_bytes >= max_bytes) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001173 ctl->extents_thresh = 0;
Josef Bacik25891f72009-09-11 16:11:20 -04001174 return;
Josef Bacik96303082009-07-13 21:29:25 -04001175 }
Josef Bacik25891f72009-09-11 16:11:20 -04001176
1177 /*
1178 * we want the extent entry threshold to always be at most 1/2 the maxw
1179 * bytes we can have, or whatever is less than that.
1180 */
1181 extent_bytes = max_bytes - bitmap_bytes;
1182 extent_bytes = min_t(u64, extent_bytes, div64_u64(max_bytes, 2));
1183
Li Zefan34d52cb2011-03-29 13:46:06 +08001184 ctl->extents_thresh =
Josef Bacik25891f72009-09-11 16:11:20 -04001185 div64_u64(extent_bytes, (sizeof(struct btrfs_free_space)));
Josef Bacik96303082009-07-13 21:29:25 -04001186}
1187
Li Zefan34d52cb2011-03-29 13:46:06 +08001188static void bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
Josef Bacik817d52f2009-07-13 21:29:25 -04001189 struct btrfs_free_space *info, u64 offset,
1190 u64 bytes)
Josef Bacik96303082009-07-13 21:29:25 -04001191{
Li Zefanf38b6e72011-03-14 13:40:51 +08001192 unsigned long start, count;
Josef Bacik96303082009-07-13 21:29:25 -04001193
Li Zefan34d52cb2011-03-29 13:46:06 +08001194 start = offset_to_bit(info->offset, ctl->unit, offset);
1195 count = bytes_to_bits(bytes, ctl->unit);
Li Zefanf38b6e72011-03-14 13:40:51 +08001196 BUG_ON(start + count > BITS_PER_BITMAP);
Josef Bacik96303082009-07-13 21:29:25 -04001197
Li Zefanf38b6e72011-03-14 13:40:51 +08001198 bitmap_clear(info->bitmap, start, count);
Josef Bacik96303082009-07-13 21:29:25 -04001199
1200 info->bytes -= bytes;
Li Zefan34d52cb2011-03-29 13:46:06 +08001201 ctl->free_space -= bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001202}
1203
Li Zefan34d52cb2011-03-29 13:46:06 +08001204static void bitmap_set_bits(struct btrfs_free_space_ctl *ctl,
Josef Bacik817d52f2009-07-13 21:29:25 -04001205 struct btrfs_free_space *info, u64 offset,
1206 u64 bytes)
Josef Bacik96303082009-07-13 21:29:25 -04001207{
Li Zefanf38b6e72011-03-14 13:40:51 +08001208 unsigned long start, count;
Josef Bacik96303082009-07-13 21:29:25 -04001209
Li Zefan34d52cb2011-03-29 13:46:06 +08001210 start = offset_to_bit(info->offset, ctl->unit, offset);
1211 count = bytes_to_bits(bytes, ctl->unit);
Li Zefanf38b6e72011-03-14 13:40:51 +08001212 BUG_ON(start + count > BITS_PER_BITMAP);
Josef Bacik96303082009-07-13 21:29:25 -04001213
Li Zefanf38b6e72011-03-14 13:40:51 +08001214 bitmap_set(info->bitmap, start, count);
Josef Bacik96303082009-07-13 21:29:25 -04001215
1216 info->bytes += bytes;
Li Zefan34d52cb2011-03-29 13:46:06 +08001217 ctl->free_space += bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001218}
1219
Li Zefan34d52cb2011-03-29 13:46:06 +08001220static int search_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001221 struct btrfs_free_space *bitmap_info, u64 *offset,
1222 u64 *bytes)
1223{
1224 unsigned long found_bits = 0;
1225 unsigned long bits, i;
1226 unsigned long next_zero;
1227
Li Zefan34d52cb2011-03-29 13:46:06 +08001228 i = offset_to_bit(bitmap_info->offset, ctl->unit,
Josef Bacik96303082009-07-13 21:29:25 -04001229 max_t(u64, *offset, bitmap_info->offset));
Li Zefan34d52cb2011-03-29 13:46:06 +08001230 bits = bytes_to_bits(*bytes, ctl->unit);
Josef Bacik96303082009-07-13 21:29:25 -04001231
1232 for (i = find_next_bit(bitmap_info->bitmap, BITS_PER_BITMAP, i);
1233 i < BITS_PER_BITMAP;
1234 i = find_next_bit(bitmap_info->bitmap, BITS_PER_BITMAP, i + 1)) {
1235 next_zero = find_next_zero_bit(bitmap_info->bitmap,
1236 BITS_PER_BITMAP, i);
1237 if ((next_zero - i) >= bits) {
1238 found_bits = next_zero - i;
1239 break;
1240 }
1241 i = next_zero;
1242 }
1243
1244 if (found_bits) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001245 *offset = (u64)(i * ctl->unit) + bitmap_info->offset;
1246 *bytes = (u64)(found_bits) * ctl->unit;
Josef Bacik96303082009-07-13 21:29:25 -04001247 return 0;
1248 }
1249
1250 return -1;
1251}
1252
Li Zefan34d52cb2011-03-29 13:46:06 +08001253static struct btrfs_free_space *
1254find_free_space(struct btrfs_free_space_ctl *ctl, u64 *offset, u64 *bytes)
Josef Bacik96303082009-07-13 21:29:25 -04001255{
1256 struct btrfs_free_space *entry;
1257 struct rb_node *node;
1258 int ret;
1259
Li Zefan34d52cb2011-03-29 13:46:06 +08001260 if (!ctl->free_space_offset.rb_node)
Josef Bacik96303082009-07-13 21:29:25 -04001261 return NULL;
1262
Li Zefan34d52cb2011-03-29 13:46:06 +08001263 entry = tree_search_offset(ctl, offset_to_bitmap(ctl, *offset), 0, 1);
Josef Bacik96303082009-07-13 21:29:25 -04001264 if (!entry)
1265 return NULL;
1266
1267 for (node = &entry->offset_index; node; node = rb_next(node)) {
1268 entry = rb_entry(node, struct btrfs_free_space, offset_index);
1269 if (entry->bytes < *bytes)
1270 continue;
1271
1272 if (entry->bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001273 ret = search_bitmap(ctl, entry, offset, bytes);
Josef Bacik96303082009-07-13 21:29:25 -04001274 if (!ret)
1275 return entry;
1276 continue;
1277 }
1278
1279 *offset = entry->offset;
1280 *bytes = entry->bytes;
1281 return entry;
1282 }
1283
1284 return NULL;
1285}
1286
Li Zefan34d52cb2011-03-29 13:46:06 +08001287static void add_new_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001288 struct btrfs_free_space *info, u64 offset)
1289{
Li Zefan34d52cb2011-03-29 13:46:06 +08001290 info->offset = offset_to_bitmap(ctl, offset);
Josef Bacikf019f422009-09-11 16:11:20 -04001291 info->bytes = 0;
Li Zefan34d52cb2011-03-29 13:46:06 +08001292 link_free_space(ctl, info);
1293 ctl->total_bitmaps++;
Josef Bacik96303082009-07-13 21:29:25 -04001294
Li Zefan34d52cb2011-03-29 13:46:06 +08001295 ctl->op->recalc_thresholds(ctl);
Josef Bacik96303082009-07-13 21:29:25 -04001296}
1297
Li Zefan34d52cb2011-03-29 13:46:06 +08001298static void free_bitmap(struct btrfs_free_space_ctl *ctl,
Li Zefanedf6e2d2010-11-09 14:50:07 +08001299 struct btrfs_free_space *bitmap_info)
1300{
Li Zefan34d52cb2011-03-29 13:46:06 +08001301 unlink_free_space(ctl, bitmap_info);
Li Zefanedf6e2d2010-11-09 14:50:07 +08001302 kfree(bitmap_info->bitmap);
Josef Bacikdc89e982011-01-28 17:05:48 -05001303 kmem_cache_free(btrfs_free_space_cachep, bitmap_info);
Li Zefan34d52cb2011-03-29 13:46:06 +08001304 ctl->total_bitmaps--;
1305 ctl->op->recalc_thresholds(ctl);
Li Zefanedf6e2d2010-11-09 14:50:07 +08001306}
1307
Li Zefan34d52cb2011-03-29 13:46:06 +08001308static noinline int remove_from_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001309 struct btrfs_free_space *bitmap_info,
1310 u64 *offset, u64 *bytes)
1311{
1312 u64 end;
Josef Bacik6606bb92009-07-31 11:03:58 -04001313 u64 search_start, search_bytes;
1314 int ret;
Josef Bacik96303082009-07-13 21:29:25 -04001315
1316again:
Li Zefan34d52cb2011-03-29 13:46:06 +08001317 end = bitmap_info->offset + (u64)(BITS_PER_BITMAP * ctl->unit) - 1;
Josef Bacik96303082009-07-13 21:29:25 -04001318
Josef Bacik6606bb92009-07-31 11:03:58 -04001319 /*
1320 * XXX - this can go away after a few releases.
1321 *
1322 * since the only user of btrfs_remove_free_space is the tree logging
1323 * stuff, and the only way to test that is under crash conditions, we
1324 * want to have this debug stuff here just in case somethings not
1325 * working. Search the bitmap for the space we are trying to use to
1326 * make sure its actually there. If its not there then we need to stop
1327 * because something has gone wrong.
1328 */
1329 search_start = *offset;
1330 search_bytes = *bytes;
Josef Bacik13dbc082011-02-03 02:39:52 +00001331 search_bytes = min(search_bytes, end - search_start + 1);
Li Zefan34d52cb2011-03-29 13:46:06 +08001332 ret = search_bitmap(ctl, bitmap_info, &search_start, &search_bytes);
Josef Bacik6606bb92009-07-31 11:03:58 -04001333 BUG_ON(ret < 0 || search_start != *offset);
1334
Josef Bacik96303082009-07-13 21:29:25 -04001335 if (*offset > bitmap_info->offset && *offset + *bytes > end) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001336 bitmap_clear_bits(ctl, bitmap_info, *offset, end - *offset + 1);
Josef Bacik96303082009-07-13 21:29:25 -04001337 *bytes -= end - *offset + 1;
1338 *offset = end + 1;
1339 } else if (*offset >= bitmap_info->offset && *offset + *bytes <= end) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001340 bitmap_clear_bits(ctl, bitmap_info, *offset, *bytes);
Josef Bacik96303082009-07-13 21:29:25 -04001341 *bytes = 0;
1342 }
1343
1344 if (*bytes) {
Josef Bacik6606bb92009-07-31 11:03:58 -04001345 struct rb_node *next = rb_next(&bitmap_info->offset_index);
Li Zefanedf6e2d2010-11-09 14:50:07 +08001346 if (!bitmap_info->bytes)
Li Zefan34d52cb2011-03-29 13:46:06 +08001347 free_bitmap(ctl, bitmap_info);
Josef Bacik96303082009-07-13 21:29:25 -04001348
Josef Bacik6606bb92009-07-31 11:03:58 -04001349 /*
1350 * no entry after this bitmap, but we still have bytes to
1351 * remove, so something has gone wrong.
1352 */
1353 if (!next)
Josef Bacik96303082009-07-13 21:29:25 -04001354 return -EINVAL;
1355
Josef Bacik6606bb92009-07-31 11:03:58 -04001356 bitmap_info = rb_entry(next, struct btrfs_free_space,
1357 offset_index);
1358
1359 /*
1360 * if the next entry isn't a bitmap we need to return to let the
1361 * extent stuff do its work.
1362 */
Josef Bacik96303082009-07-13 21:29:25 -04001363 if (!bitmap_info->bitmap)
1364 return -EAGAIN;
1365
Josef Bacik6606bb92009-07-31 11:03:58 -04001366 /*
1367 * Ok the next item is a bitmap, but it may not actually hold
1368 * the information for the rest of this free space stuff, so
1369 * look for it, and if we don't find it return so we can try
1370 * everything over again.
1371 */
1372 search_start = *offset;
1373 search_bytes = *bytes;
Li Zefan34d52cb2011-03-29 13:46:06 +08001374 ret = search_bitmap(ctl, bitmap_info, &search_start,
Josef Bacik6606bb92009-07-31 11:03:58 -04001375 &search_bytes);
1376 if (ret < 0 || search_start != *offset)
1377 return -EAGAIN;
1378
Josef Bacik96303082009-07-13 21:29:25 -04001379 goto again;
Li Zefanedf6e2d2010-11-09 14:50:07 +08001380 } else if (!bitmap_info->bytes)
Li Zefan34d52cb2011-03-29 13:46:06 +08001381 free_bitmap(ctl, bitmap_info);
Josef Bacik96303082009-07-13 21:29:25 -04001382
1383 return 0;
1384}
1385
Li Zefan34d52cb2011-03-29 13:46:06 +08001386static bool use_bitmap(struct btrfs_free_space_ctl *ctl,
1387 struct btrfs_free_space *info)
Josef Bacik96303082009-07-13 21:29:25 -04001388{
Li Zefan34d52cb2011-03-29 13:46:06 +08001389 struct btrfs_block_group_cache *block_group = ctl->private;
Josef Bacik96303082009-07-13 21:29:25 -04001390
1391 /*
1392 * If we are below the extents threshold then we can add this as an
1393 * extent, and don't have to deal with the bitmap
1394 */
Li Zefan34d52cb2011-03-29 13:46:06 +08001395 if (ctl->free_extents < ctl->extents_thresh) {
Josef Bacik32cb0842011-03-18 16:16:21 -04001396 /*
1397 * If this block group has some small extents we don't want to
1398 * use up all of our free slots in the cache with them, we want
1399 * to reserve them to larger extents, however if we have plent
1400 * of cache left then go ahead an dadd them, no sense in adding
1401 * the overhead of a bitmap if we don't have to.
1402 */
1403 if (info->bytes <= block_group->sectorsize * 4) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001404 if (ctl->free_extents * 2 <= ctl->extents_thresh)
1405 return false;
Josef Bacik32cb0842011-03-18 16:16:21 -04001406 } else {
Li Zefan34d52cb2011-03-29 13:46:06 +08001407 return false;
Josef Bacik32cb0842011-03-18 16:16:21 -04001408 }
1409 }
Josef Bacik96303082009-07-13 21:29:25 -04001410
1411 /*
1412 * some block groups are so tiny they can't be enveloped by a bitmap, so
1413 * don't even bother to create a bitmap for this
1414 */
1415 if (BITS_PER_BITMAP * block_group->sectorsize >
1416 block_group->key.offset)
Li Zefan34d52cb2011-03-29 13:46:06 +08001417 return false;
1418
1419 return true;
1420}
1421
1422static int insert_into_bitmap(struct btrfs_free_space_ctl *ctl,
1423 struct btrfs_free_space *info)
1424{
1425 struct btrfs_free_space *bitmap_info;
1426 int added = 0;
1427 u64 bytes, offset, end;
1428 int ret;
Josef Bacik96303082009-07-13 21:29:25 -04001429
1430 bytes = info->bytes;
1431 offset = info->offset;
1432
Li Zefan34d52cb2011-03-29 13:46:06 +08001433 if (!ctl->op->use_bitmap(ctl, info))
1434 return 0;
1435
Josef Bacik96303082009-07-13 21:29:25 -04001436again:
Li Zefan34d52cb2011-03-29 13:46:06 +08001437 bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
Josef Bacik96303082009-07-13 21:29:25 -04001438 1, 0);
1439 if (!bitmap_info) {
1440 BUG_ON(added);
1441 goto new_bitmap;
1442 }
1443
Li Zefan34d52cb2011-03-29 13:46:06 +08001444 end = bitmap_info->offset + (u64)(BITS_PER_BITMAP * ctl->unit);
Josef Bacik96303082009-07-13 21:29:25 -04001445
1446 if (offset >= bitmap_info->offset && offset + bytes > end) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001447 bitmap_set_bits(ctl, bitmap_info, offset, end - offset);
Josef Bacik96303082009-07-13 21:29:25 -04001448 bytes -= end - offset;
1449 offset = end;
1450 added = 0;
1451 } else if (offset >= bitmap_info->offset && offset + bytes <= end) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001452 bitmap_set_bits(ctl, bitmap_info, offset, bytes);
Josef Bacik96303082009-07-13 21:29:25 -04001453 bytes = 0;
1454 } else {
1455 BUG();
1456 }
1457
1458 if (!bytes) {
1459 ret = 1;
1460 goto out;
1461 } else
1462 goto again;
1463
1464new_bitmap:
1465 if (info && info->bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001466 add_new_bitmap(ctl, info, offset);
Josef Bacik96303082009-07-13 21:29:25 -04001467 added = 1;
1468 info = NULL;
1469 goto again;
1470 } else {
Li Zefan34d52cb2011-03-29 13:46:06 +08001471 spin_unlock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04001472
1473 /* no pre-allocated info, allocate a new one */
1474 if (!info) {
Josef Bacikdc89e982011-01-28 17:05:48 -05001475 info = kmem_cache_zalloc(btrfs_free_space_cachep,
1476 GFP_NOFS);
Josef Bacik96303082009-07-13 21:29:25 -04001477 if (!info) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001478 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04001479 ret = -ENOMEM;
1480 goto out;
1481 }
1482 }
1483
1484 /* allocate the bitmap */
1485 info->bitmap = kzalloc(PAGE_CACHE_SIZE, GFP_NOFS);
Li Zefan34d52cb2011-03-29 13:46:06 +08001486 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04001487 if (!info->bitmap) {
1488 ret = -ENOMEM;
1489 goto out;
1490 }
1491 goto again;
1492 }
1493
1494out:
1495 if (info) {
1496 if (info->bitmap)
1497 kfree(info->bitmap);
Josef Bacikdc89e982011-01-28 17:05:48 -05001498 kmem_cache_free(btrfs_free_space_cachep, info);
Josef Bacik96303082009-07-13 21:29:25 -04001499 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04001500
1501 return ret;
1502}
1503
Li Zefan34d52cb2011-03-29 13:46:06 +08001504bool try_merge_free_space(struct btrfs_free_space_ctl *ctl,
Li Zefanf333adb2010-11-09 14:57:39 +08001505 struct btrfs_free_space *info, bool update_stat)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001506{
Li Zefan120d66e2010-11-09 14:56:50 +08001507 struct btrfs_free_space *left_info;
1508 struct btrfs_free_space *right_info;
1509 bool merged = false;
1510 u64 offset = info->offset;
1511 u64 bytes = info->bytes;
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001512
Josef Bacik0f9dd462008-09-23 13:14:11 -04001513 /*
1514 * first we want to see if there is free space adjacent to the range we
1515 * are adding, if there is remove that struct and add a new one to
1516 * cover the entire range
1517 */
Li Zefan34d52cb2011-03-29 13:46:06 +08001518 right_info = tree_search_offset(ctl, offset + bytes, 0, 0);
Josef Bacik96303082009-07-13 21:29:25 -04001519 if (right_info && rb_prev(&right_info->offset_index))
1520 left_info = rb_entry(rb_prev(&right_info->offset_index),
1521 struct btrfs_free_space, offset_index);
1522 else
Li Zefan34d52cb2011-03-29 13:46:06 +08001523 left_info = tree_search_offset(ctl, offset - 1, 0, 0);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001524
Josef Bacik96303082009-07-13 21:29:25 -04001525 if (right_info && !right_info->bitmap) {
Li Zefanf333adb2010-11-09 14:57:39 +08001526 if (update_stat)
Li Zefan34d52cb2011-03-29 13:46:06 +08001527 unlink_free_space(ctl, right_info);
Li Zefanf333adb2010-11-09 14:57:39 +08001528 else
Li Zefan34d52cb2011-03-29 13:46:06 +08001529 __unlink_free_space(ctl, right_info);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001530 info->bytes += right_info->bytes;
Josef Bacikdc89e982011-01-28 17:05:48 -05001531 kmem_cache_free(btrfs_free_space_cachep, right_info);
Li Zefan120d66e2010-11-09 14:56:50 +08001532 merged = true;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001533 }
1534
Josef Bacik96303082009-07-13 21:29:25 -04001535 if (left_info && !left_info->bitmap &&
1536 left_info->offset + left_info->bytes == offset) {
Li Zefanf333adb2010-11-09 14:57:39 +08001537 if (update_stat)
Li Zefan34d52cb2011-03-29 13:46:06 +08001538 unlink_free_space(ctl, left_info);
Li Zefanf333adb2010-11-09 14:57:39 +08001539 else
Li Zefan34d52cb2011-03-29 13:46:06 +08001540 __unlink_free_space(ctl, left_info);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001541 info->offset = left_info->offset;
1542 info->bytes += left_info->bytes;
Josef Bacikdc89e982011-01-28 17:05:48 -05001543 kmem_cache_free(btrfs_free_space_cachep, left_info);
Li Zefan120d66e2010-11-09 14:56:50 +08001544 merged = true;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001545 }
1546
Li Zefan120d66e2010-11-09 14:56:50 +08001547 return merged;
1548}
1549
Li Zefan581bb052011-04-20 10:06:11 +08001550int __btrfs_add_free_space(struct btrfs_free_space_ctl *ctl,
1551 u64 offset, u64 bytes)
Li Zefan120d66e2010-11-09 14:56:50 +08001552{
1553 struct btrfs_free_space *info;
1554 int ret = 0;
1555
Josef Bacikdc89e982011-01-28 17:05:48 -05001556 info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS);
Li Zefan120d66e2010-11-09 14:56:50 +08001557 if (!info)
1558 return -ENOMEM;
1559
1560 info->offset = offset;
1561 info->bytes = bytes;
1562
Li Zefan34d52cb2011-03-29 13:46:06 +08001563 spin_lock(&ctl->tree_lock);
Li Zefan120d66e2010-11-09 14:56:50 +08001564
Li Zefan34d52cb2011-03-29 13:46:06 +08001565 if (try_merge_free_space(ctl, info, true))
Li Zefan120d66e2010-11-09 14:56:50 +08001566 goto link;
1567
1568 /*
1569 * There was no extent directly to the left or right of this new
1570 * extent then we know we're going to have to allocate a new extent, so
1571 * before we do that see if we need to drop this into a bitmap
1572 */
Li Zefan34d52cb2011-03-29 13:46:06 +08001573 ret = insert_into_bitmap(ctl, info);
Li Zefan120d66e2010-11-09 14:56:50 +08001574 if (ret < 0) {
1575 goto out;
1576 } else if (ret) {
1577 ret = 0;
1578 goto out;
1579 }
1580link:
Li Zefan34d52cb2011-03-29 13:46:06 +08001581 ret = link_free_space(ctl, info);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001582 if (ret)
Josef Bacikdc89e982011-01-28 17:05:48 -05001583 kmem_cache_free(btrfs_free_space_cachep, info);
Josef Bacik96303082009-07-13 21:29:25 -04001584out:
Li Zefan34d52cb2011-03-29 13:46:06 +08001585 spin_unlock(&ctl->tree_lock);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001586
Josef Bacik0f9dd462008-09-23 13:14:11 -04001587 if (ret) {
Josef Bacik96303082009-07-13 21:29:25 -04001588 printk(KERN_CRIT "btrfs: unable to add free space :%d\n", ret);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04001589 BUG_ON(ret == -EEXIST);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001590 }
1591
Josef Bacik0f9dd462008-09-23 13:14:11 -04001592 return ret;
1593}
1594
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001595int btrfs_remove_free_space(struct btrfs_block_group_cache *block_group,
1596 u64 offset, u64 bytes)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001597{
Li Zefan34d52cb2011-03-29 13:46:06 +08001598 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001599 struct btrfs_free_space *info;
Josef Bacik96303082009-07-13 21:29:25 -04001600 struct btrfs_free_space *next_info = NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001601 int ret = 0;
1602
Li Zefan34d52cb2011-03-29 13:46:06 +08001603 spin_lock(&ctl->tree_lock);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001604
Josef Bacik96303082009-07-13 21:29:25 -04001605again:
Li Zefan34d52cb2011-03-29 13:46:06 +08001606 info = tree_search_offset(ctl, offset, 0, 0);
Josef Bacik96303082009-07-13 21:29:25 -04001607 if (!info) {
Josef Bacik6606bb92009-07-31 11:03:58 -04001608 /*
1609 * oops didn't find an extent that matched the space we wanted
1610 * to remove, look for a bitmap instead
1611 */
Li Zefan34d52cb2011-03-29 13:46:06 +08001612 info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
Josef Bacik6606bb92009-07-31 11:03:58 -04001613 1, 0);
1614 if (!info) {
1615 WARN_ON(1);
1616 goto out_lock;
1617 }
Josef Bacik96303082009-07-13 21:29:25 -04001618 }
1619
1620 if (info->bytes < bytes && rb_next(&info->offset_index)) {
1621 u64 end;
1622 next_info = rb_entry(rb_next(&info->offset_index),
1623 struct btrfs_free_space,
1624 offset_index);
1625
1626 if (next_info->bitmap)
Li Zefan34d52cb2011-03-29 13:46:06 +08001627 end = next_info->offset +
1628 BITS_PER_BITMAP * ctl->unit - 1;
Josef Bacik96303082009-07-13 21:29:25 -04001629 else
1630 end = next_info->offset + next_info->bytes;
1631
1632 if (next_info->bytes < bytes ||
1633 next_info->offset > offset || offset > end) {
1634 printk(KERN_CRIT "Found free space at %llu, size %llu,"
1635 " trying to use %llu\n",
1636 (unsigned long long)info->offset,
1637 (unsigned long long)info->bytes,
1638 (unsigned long long)bytes);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001639 WARN_ON(1);
1640 ret = -EINVAL;
Josef Bacik96303082009-07-13 21:29:25 -04001641 goto out_lock;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001642 }
Josef Bacik96303082009-07-13 21:29:25 -04001643
1644 info = next_info;
1645 }
1646
1647 if (info->bytes == bytes) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001648 unlink_free_space(ctl, info);
Josef Bacik96303082009-07-13 21:29:25 -04001649 if (info->bitmap) {
1650 kfree(info->bitmap);
Li Zefan34d52cb2011-03-29 13:46:06 +08001651 ctl->total_bitmaps--;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001652 }
Josef Bacikdc89e982011-01-28 17:05:48 -05001653 kmem_cache_free(btrfs_free_space_cachep, info);
Josef Bacik96303082009-07-13 21:29:25 -04001654 goto out_lock;
1655 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04001656
Josef Bacik96303082009-07-13 21:29:25 -04001657 if (!info->bitmap && info->offset == offset) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001658 unlink_free_space(ctl, info);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001659 info->offset += bytes;
1660 info->bytes -= bytes;
Li Zefan34d52cb2011-03-29 13:46:06 +08001661 link_free_space(ctl, info);
Josef Bacik96303082009-07-13 21:29:25 -04001662 goto out_lock;
1663 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04001664
Josef Bacik96303082009-07-13 21:29:25 -04001665 if (!info->bitmap && info->offset <= offset &&
1666 info->offset + info->bytes >= offset + bytes) {
Chris Mason9b49c9b2008-09-24 11:23:25 -04001667 u64 old_start = info->offset;
1668 /*
1669 * we're freeing space in the middle of the info,
1670 * this can happen during tree log replay
1671 *
1672 * first unlink the old info and then
1673 * insert it again after the hole we're creating
1674 */
Li Zefan34d52cb2011-03-29 13:46:06 +08001675 unlink_free_space(ctl, info);
Chris Mason9b49c9b2008-09-24 11:23:25 -04001676 if (offset + bytes < info->offset + info->bytes) {
1677 u64 old_end = info->offset + info->bytes;
1678
1679 info->offset = offset + bytes;
1680 info->bytes = old_end - info->offset;
Li Zefan34d52cb2011-03-29 13:46:06 +08001681 ret = link_free_space(ctl, info);
Josef Bacik96303082009-07-13 21:29:25 -04001682 WARN_ON(ret);
1683 if (ret)
1684 goto out_lock;
Chris Mason9b49c9b2008-09-24 11:23:25 -04001685 } else {
1686 /* the hole we're creating ends at the end
1687 * of the info struct, just free the info
1688 */
Josef Bacikdc89e982011-01-28 17:05:48 -05001689 kmem_cache_free(btrfs_free_space_cachep, info);
Chris Mason9b49c9b2008-09-24 11:23:25 -04001690 }
Li Zefan34d52cb2011-03-29 13:46:06 +08001691 spin_unlock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04001692
1693 /* step two, insert a new info struct to cover
1694 * anything before the hole
Chris Mason9b49c9b2008-09-24 11:23:25 -04001695 */
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001696 ret = btrfs_add_free_space(block_group, old_start,
1697 offset - old_start);
Josef Bacik96303082009-07-13 21:29:25 -04001698 WARN_ON(ret);
1699 goto out;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001700 }
Josef Bacik96303082009-07-13 21:29:25 -04001701
Li Zefan34d52cb2011-03-29 13:46:06 +08001702 ret = remove_from_bitmap(ctl, info, &offset, &bytes);
Josef Bacik96303082009-07-13 21:29:25 -04001703 if (ret == -EAGAIN)
1704 goto again;
1705 BUG_ON(ret);
1706out_lock:
Li Zefan34d52cb2011-03-29 13:46:06 +08001707 spin_unlock(&ctl->tree_lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001708out:
Josef Bacik25179202008-10-29 14:49:05 -04001709 return ret;
1710}
1711
Josef Bacik0f9dd462008-09-23 13:14:11 -04001712void btrfs_dump_free_space(struct btrfs_block_group_cache *block_group,
1713 u64 bytes)
1714{
Li Zefan34d52cb2011-03-29 13:46:06 +08001715 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001716 struct btrfs_free_space *info;
1717 struct rb_node *n;
1718 int count = 0;
1719
Li Zefan34d52cb2011-03-29 13:46:06 +08001720 for (n = rb_first(&ctl->free_space_offset); n; n = rb_next(n)) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001721 info = rb_entry(n, struct btrfs_free_space, offset_index);
1722 if (info->bytes >= bytes)
1723 count++;
Josef Bacik96303082009-07-13 21:29:25 -04001724 printk(KERN_CRIT "entry offset %llu, bytes %llu, bitmap %s\n",
Joel Becker21380932009-04-21 12:38:29 -07001725 (unsigned long long)info->offset,
Josef Bacik96303082009-07-13 21:29:25 -04001726 (unsigned long long)info->bytes,
1727 (info->bitmap) ? "yes" : "no");
Josef Bacik0f9dd462008-09-23 13:14:11 -04001728 }
Josef Bacik96303082009-07-13 21:29:25 -04001729 printk(KERN_INFO "block group has cluster?: %s\n",
1730 list_empty(&block_group->cluster_list) ? "no" : "yes");
Josef Bacik0f9dd462008-09-23 13:14:11 -04001731 printk(KERN_INFO "%d blocks of free space at or bigger than bytes is"
1732 "\n", count);
1733}
1734
Li Zefan34d52cb2011-03-29 13:46:06 +08001735static struct btrfs_free_space_op free_space_op = {
1736 .recalc_thresholds = recalculate_thresholds,
1737 .use_bitmap = use_bitmap,
1738};
1739
1740void btrfs_init_free_space_ctl(struct btrfs_block_group_cache *block_group)
1741{
1742 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
1743
1744 spin_lock_init(&ctl->tree_lock);
1745 ctl->unit = block_group->sectorsize;
1746 ctl->start = block_group->key.objectid;
1747 ctl->private = block_group;
1748 ctl->op = &free_space_op;
1749
1750 /*
1751 * we only want to have 32k of ram per block group for keeping
1752 * track of free space, and if we pass 1/2 of that we want to
1753 * start converting things over to using bitmaps
1754 */
1755 ctl->extents_thresh = ((1024 * 32) / 2) /
1756 sizeof(struct btrfs_free_space);
1757}
1758
Chris Masonfa9c0d792009-04-03 09:47:43 -04001759/*
1760 * for a given cluster, put all of its extents back into the free
1761 * space cache. If the block group passed doesn't match the block group
1762 * pointed to by the cluster, someone else raced in and freed the
1763 * cluster already. In that case, we just return without changing anything
1764 */
1765static int
1766__btrfs_return_cluster_to_free_space(
1767 struct btrfs_block_group_cache *block_group,
1768 struct btrfs_free_cluster *cluster)
1769{
Li Zefan34d52cb2011-03-29 13:46:06 +08001770 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04001771 struct btrfs_free_space *entry;
1772 struct rb_node *node;
1773
1774 spin_lock(&cluster->lock);
1775 if (cluster->block_group != block_group)
1776 goto out;
1777
Josef Bacik96303082009-07-13 21:29:25 -04001778 cluster->block_group = NULL;
Chris Masonfa9c0d792009-04-03 09:47:43 -04001779 cluster->window_start = 0;
Josef Bacik96303082009-07-13 21:29:25 -04001780 list_del_init(&cluster->block_group_list);
Josef Bacik96303082009-07-13 21:29:25 -04001781
Chris Masonfa9c0d792009-04-03 09:47:43 -04001782 node = rb_first(&cluster->root);
Josef Bacik96303082009-07-13 21:29:25 -04001783 while (node) {
Josef Bacik4e69b592011-03-21 10:11:24 -04001784 bool bitmap;
1785
Chris Masonfa9c0d792009-04-03 09:47:43 -04001786 entry = rb_entry(node, struct btrfs_free_space, offset_index);
1787 node = rb_next(&entry->offset_index);
1788 rb_erase(&entry->offset_index, &cluster->root);
Josef Bacik4e69b592011-03-21 10:11:24 -04001789
1790 bitmap = (entry->bitmap != NULL);
1791 if (!bitmap)
Li Zefan34d52cb2011-03-29 13:46:06 +08001792 try_merge_free_space(ctl, entry, false);
1793 tree_insert_offset(&ctl->free_space_offset,
Josef Bacik4e69b592011-03-21 10:11:24 -04001794 entry->offset, &entry->offset_index, bitmap);
Chris Masonfa9c0d792009-04-03 09:47:43 -04001795 }
Eric Paris6bef4d32010-02-23 19:43:04 +00001796 cluster->root = RB_ROOT;
Josef Bacik96303082009-07-13 21:29:25 -04001797
Chris Masonfa9c0d792009-04-03 09:47:43 -04001798out:
1799 spin_unlock(&cluster->lock);
Josef Bacik96303082009-07-13 21:29:25 -04001800 btrfs_put_block_group(block_group);
Chris Masonfa9c0d792009-04-03 09:47:43 -04001801 return 0;
1802}
1803
Li Zefan581bb052011-04-20 10:06:11 +08001804void __btrfs_remove_free_space_cache(struct btrfs_free_space_ctl *ctl)
1805{
1806 struct btrfs_free_space *info;
1807 struct rb_node *node;
1808
1809 spin_lock(&ctl->tree_lock);
1810 while ((node = rb_last(&ctl->free_space_offset)) != NULL) {
1811 info = rb_entry(node, struct btrfs_free_space, offset_index);
1812 unlink_free_space(ctl, info);
1813 kfree(info->bitmap);
1814 kmem_cache_free(btrfs_free_space_cachep, info);
1815 if (need_resched()) {
1816 spin_unlock(&ctl->tree_lock);
1817 cond_resched();
1818 spin_lock(&ctl->tree_lock);
1819 }
1820 }
1821 spin_unlock(&ctl->tree_lock);
1822}
1823
Josef Bacik0f9dd462008-09-23 13:14:11 -04001824void btrfs_remove_free_space_cache(struct btrfs_block_group_cache *block_group)
1825{
Li Zefan34d52cb2011-03-29 13:46:06 +08001826 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04001827 struct btrfs_free_cluster *cluster;
Josef Bacik96303082009-07-13 21:29:25 -04001828 struct list_head *head;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001829
Li Zefan34d52cb2011-03-29 13:46:06 +08001830 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04001831 while ((head = block_group->cluster_list.next) !=
1832 &block_group->cluster_list) {
1833 cluster = list_entry(head, struct btrfs_free_cluster,
1834 block_group_list);
Chris Masonfa9c0d792009-04-03 09:47:43 -04001835
1836 WARN_ON(cluster->block_group != block_group);
1837 __btrfs_return_cluster_to_free_space(block_group, cluster);
Josef Bacik96303082009-07-13 21:29:25 -04001838 if (need_resched()) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001839 spin_unlock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04001840 cond_resched();
Li Zefan34d52cb2011-03-29 13:46:06 +08001841 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04001842 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04001843 }
Li Zefan34d52cb2011-03-29 13:46:06 +08001844 spin_unlock(&ctl->tree_lock);
Li Zefan581bb052011-04-20 10:06:11 +08001845
1846 __btrfs_remove_free_space_cache(ctl);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001847}
1848
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001849u64 btrfs_find_space_for_alloc(struct btrfs_block_group_cache *block_group,
1850 u64 offset, u64 bytes, u64 empty_size)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001851{
Li Zefan34d52cb2011-03-29 13:46:06 +08001852 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001853 struct btrfs_free_space *entry = NULL;
Josef Bacik96303082009-07-13 21:29:25 -04001854 u64 bytes_search = bytes + empty_size;
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001855 u64 ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001856
Li Zefan34d52cb2011-03-29 13:46:06 +08001857 spin_lock(&ctl->tree_lock);
1858 entry = find_free_space(ctl, &offset, &bytes_search);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001859 if (!entry)
Josef Bacik96303082009-07-13 21:29:25 -04001860 goto out;
1861
1862 ret = offset;
1863 if (entry->bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001864 bitmap_clear_bits(ctl, entry, offset, bytes);
Li Zefanedf6e2d2010-11-09 14:50:07 +08001865 if (!entry->bytes)
Li Zefan34d52cb2011-03-29 13:46:06 +08001866 free_bitmap(ctl, entry);
Josef Bacik96303082009-07-13 21:29:25 -04001867 } else {
Li Zefan34d52cb2011-03-29 13:46:06 +08001868 unlink_free_space(ctl, entry);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001869 entry->offset += bytes;
1870 entry->bytes -= bytes;
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001871 if (!entry->bytes)
Josef Bacikdc89e982011-01-28 17:05:48 -05001872 kmem_cache_free(btrfs_free_space_cachep, entry);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001873 else
Li Zefan34d52cb2011-03-29 13:46:06 +08001874 link_free_space(ctl, entry);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001875 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04001876
Josef Bacik96303082009-07-13 21:29:25 -04001877out:
Li Zefan34d52cb2011-03-29 13:46:06 +08001878 spin_unlock(&ctl->tree_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04001879
Josef Bacik0f9dd462008-09-23 13:14:11 -04001880 return ret;
1881}
Chris Masonfa9c0d792009-04-03 09:47:43 -04001882
1883/*
1884 * given a cluster, put all of its extents back into the free space
1885 * cache. If a block group is passed, this function will only free
1886 * a cluster that belongs to the passed block group.
1887 *
1888 * Otherwise, it'll get a reference on the block group pointed to by the
1889 * cluster and remove the cluster from it.
1890 */
1891int btrfs_return_cluster_to_free_space(
1892 struct btrfs_block_group_cache *block_group,
1893 struct btrfs_free_cluster *cluster)
1894{
Li Zefan34d52cb2011-03-29 13:46:06 +08001895 struct btrfs_free_space_ctl *ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04001896 int ret;
1897
1898 /* first, get a safe pointer to the block group */
1899 spin_lock(&cluster->lock);
1900 if (!block_group) {
1901 block_group = cluster->block_group;
1902 if (!block_group) {
1903 spin_unlock(&cluster->lock);
1904 return 0;
1905 }
1906 } else if (cluster->block_group != block_group) {
1907 /* someone else has already freed it don't redo their work */
1908 spin_unlock(&cluster->lock);
1909 return 0;
1910 }
1911 atomic_inc(&block_group->count);
1912 spin_unlock(&cluster->lock);
1913
Li Zefan34d52cb2011-03-29 13:46:06 +08001914 ctl = block_group->free_space_ctl;
1915
Chris Masonfa9c0d792009-04-03 09:47:43 -04001916 /* now return any extents the cluster had on it */
Li Zefan34d52cb2011-03-29 13:46:06 +08001917 spin_lock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04001918 ret = __btrfs_return_cluster_to_free_space(block_group, cluster);
Li Zefan34d52cb2011-03-29 13:46:06 +08001919 spin_unlock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04001920
1921 /* finally drop our ref */
1922 btrfs_put_block_group(block_group);
1923 return ret;
1924}
1925
Josef Bacik96303082009-07-13 21:29:25 -04001926static u64 btrfs_alloc_from_bitmap(struct btrfs_block_group_cache *block_group,
1927 struct btrfs_free_cluster *cluster,
Josef Bacik4e69b592011-03-21 10:11:24 -04001928 struct btrfs_free_space *entry,
Josef Bacik96303082009-07-13 21:29:25 -04001929 u64 bytes, u64 min_start)
1930{
Li Zefan34d52cb2011-03-29 13:46:06 +08001931 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik96303082009-07-13 21:29:25 -04001932 int err;
1933 u64 search_start = cluster->window_start;
1934 u64 search_bytes = bytes;
1935 u64 ret = 0;
1936
Josef Bacik96303082009-07-13 21:29:25 -04001937 search_start = min_start;
1938 search_bytes = bytes;
1939
Li Zefan34d52cb2011-03-29 13:46:06 +08001940 err = search_bitmap(ctl, entry, &search_start, &search_bytes);
Josef Bacik96303082009-07-13 21:29:25 -04001941 if (err)
Josef Bacik4e69b592011-03-21 10:11:24 -04001942 return 0;
Josef Bacik96303082009-07-13 21:29:25 -04001943
1944 ret = search_start;
Li Zefan34d52cb2011-03-29 13:46:06 +08001945 bitmap_clear_bits(ctl, entry, ret, bytes);
Josef Bacik96303082009-07-13 21:29:25 -04001946
1947 return ret;
1948}
1949
Chris Masonfa9c0d792009-04-03 09:47:43 -04001950/*
1951 * given a cluster, try to allocate 'bytes' from it, returns 0
1952 * if it couldn't find anything suitably large, or a logical disk offset
1953 * if things worked out
1954 */
1955u64 btrfs_alloc_from_cluster(struct btrfs_block_group_cache *block_group,
1956 struct btrfs_free_cluster *cluster, u64 bytes,
1957 u64 min_start)
1958{
Li Zefan34d52cb2011-03-29 13:46:06 +08001959 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04001960 struct btrfs_free_space *entry = NULL;
1961 struct rb_node *node;
1962 u64 ret = 0;
1963
1964 spin_lock(&cluster->lock);
1965 if (bytes > cluster->max_size)
1966 goto out;
1967
1968 if (cluster->block_group != block_group)
1969 goto out;
1970
1971 node = rb_first(&cluster->root);
1972 if (!node)
1973 goto out;
1974
1975 entry = rb_entry(node, struct btrfs_free_space, offset_index);
Chris Masonfa9c0d792009-04-03 09:47:43 -04001976 while(1) {
Josef Bacik4e69b592011-03-21 10:11:24 -04001977 if (entry->bytes < bytes ||
1978 (!entry->bitmap && entry->offset < min_start)) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04001979 struct rb_node *node;
1980
1981 node = rb_next(&entry->offset_index);
1982 if (!node)
1983 break;
1984 entry = rb_entry(node, struct btrfs_free_space,
1985 offset_index);
1986 continue;
1987 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04001988
Josef Bacik4e69b592011-03-21 10:11:24 -04001989 if (entry->bitmap) {
1990 ret = btrfs_alloc_from_bitmap(block_group,
1991 cluster, entry, bytes,
1992 min_start);
1993 if (ret == 0) {
1994 struct rb_node *node;
1995 node = rb_next(&entry->offset_index);
1996 if (!node)
1997 break;
1998 entry = rb_entry(node, struct btrfs_free_space,
1999 offset_index);
2000 continue;
2001 }
2002 } else {
2003
2004 ret = entry->offset;
2005
2006 entry->offset += bytes;
2007 entry->bytes -= bytes;
2008 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04002009
Li Zefan5e71b5d2010-11-09 14:55:34 +08002010 if (entry->bytes == 0)
Chris Masonfa9c0d792009-04-03 09:47:43 -04002011 rb_erase(&entry->offset_index, &cluster->root);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002012 break;
2013 }
2014out:
2015 spin_unlock(&cluster->lock);
Josef Bacik96303082009-07-13 21:29:25 -04002016
Li Zefan5e71b5d2010-11-09 14:55:34 +08002017 if (!ret)
2018 return 0;
2019
Li Zefan34d52cb2011-03-29 13:46:06 +08002020 spin_lock(&ctl->tree_lock);
Li Zefan5e71b5d2010-11-09 14:55:34 +08002021
Li Zefan34d52cb2011-03-29 13:46:06 +08002022 ctl->free_space -= bytes;
Li Zefan5e71b5d2010-11-09 14:55:34 +08002023 if (entry->bytes == 0) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002024 ctl->free_extents--;
Josef Bacik4e69b592011-03-21 10:11:24 -04002025 if (entry->bitmap) {
2026 kfree(entry->bitmap);
Li Zefan34d52cb2011-03-29 13:46:06 +08002027 ctl->total_bitmaps--;
2028 ctl->op->recalc_thresholds(ctl);
Josef Bacik4e69b592011-03-21 10:11:24 -04002029 }
Josef Bacikdc89e982011-01-28 17:05:48 -05002030 kmem_cache_free(btrfs_free_space_cachep, entry);
Li Zefan5e71b5d2010-11-09 14:55:34 +08002031 }
2032
Li Zefan34d52cb2011-03-29 13:46:06 +08002033 spin_unlock(&ctl->tree_lock);
Li Zefan5e71b5d2010-11-09 14:55:34 +08002034
Chris Masonfa9c0d792009-04-03 09:47:43 -04002035 return ret;
2036}
2037
Josef Bacik96303082009-07-13 21:29:25 -04002038static int btrfs_bitmap_cluster(struct btrfs_block_group_cache *block_group,
2039 struct btrfs_free_space *entry,
2040 struct btrfs_free_cluster *cluster,
2041 u64 offset, u64 bytes, u64 min_bytes)
2042{
Li Zefan34d52cb2011-03-29 13:46:06 +08002043 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik96303082009-07-13 21:29:25 -04002044 unsigned long next_zero;
2045 unsigned long i;
2046 unsigned long search_bits;
2047 unsigned long total_bits;
2048 unsigned long found_bits;
2049 unsigned long start = 0;
2050 unsigned long total_found = 0;
Josef Bacik4e69b592011-03-21 10:11:24 -04002051 int ret;
Josef Bacik96303082009-07-13 21:29:25 -04002052 bool found = false;
2053
2054 i = offset_to_bit(entry->offset, block_group->sectorsize,
2055 max_t(u64, offset, entry->offset));
Josef Bacikd0a365e2011-03-18 15:27:43 -04002056 search_bits = bytes_to_bits(bytes, block_group->sectorsize);
2057 total_bits = bytes_to_bits(min_bytes, block_group->sectorsize);
Josef Bacik96303082009-07-13 21:29:25 -04002058
2059again:
2060 found_bits = 0;
2061 for (i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, i);
2062 i < BITS_PER_BITMAP;
2063 i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, i + 1)) {
2064 next_zero = find_next_zero_bit(entry->bitmap,
2065 BITS_PER_BITMAP, i);
2066 if (next_zero - i >= search_bits) {
2067 found_bits = next_zero - i;
2068 break;
2069 }
2070 i = next_zero;
2071 }
2072
2073 if (!found_bits)
Josef Bacik4e69b592011-03-21 10:11:24 -04002074 return -ENOSPC;
Josef Bacik96303082009-07-13 21:29:25 -04002075
2076 if (!found) {
2077 start = i;
2078 found = true;
2079 }
2080
2081 total_found += found_bits;
2082
2083 if (cluster->max_size < found_bits * block_group->sectorsize)
2084 cluster->max_size = found_bits * block_group->sectorsize;
2085
2086 if (total_found < total_bits) {
2087 i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, next_zero);
2088 if (i - start > total_bits * 2) {
2089 total_found = 0;
2090 cluster->max_size = 0;
2091 found = false;
2092 }
2093 goto again;
2094 }
2095
2096 cluster->window_start = start * block_group->sectorsize +
2097 entry->offset;
Li Zefan34d52cb2011-03-29 13:46:06 +08002098 rb_erase(&entry->offset_index, &ctl->free_space_offset);
Josef Bacik4e69b592011-03-21 10:11:24 -04002099 ret = tree_insert_offset(&cluster->root, entry->offset,
2100 &entry->offset_index, 1);
2101 BUG_ON(ret);
Josef Bacik96303082009-07-13 21:29:25 -04002102
2103 return 0;
2104}
2105
Chris Masonfa9c0d792009-04-03 09:47:43 -04002106/*
Josef Bacik4e69b592011-03-21 10:11:24 -04002107 * This searches the block group for just extents to fill the cluster with.
2108 */
2109static int setup_cluster_no_bitmap(struct btrfs_block_group_cache *block_group,
2110 struct btrfs_free_cluster *cluster,
2111 u64 offset, u64 bytes, u64 min_bytes)
2112{
Li Zefan34d52cb2011-03-29 13:46:06 +08002113 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik4e69b592011-03-21 10:11:24 -04002114 struct btrfs_free_space *first = NULL;
2115 struct btrfs_free_space *entry = NULL;
2116 struct btrfs_free_space *prev = NULL;
2117 struct btrfs_free_space *last;
2118 struct rb_node *node;
2119 u64 window_start;
2120 u64 window_free;
2121 u64 max_extent;
2122 u64 max_gap = 128 * 1024;
2123
Li Zefan34d52cb2011-03-29 13:46:06 +08002124 entry = tree_search_offset(ctl, offset, 0, 1);
Josef Bacik4e69b592011-03-21 10:11:24 -04002125 if (!entry)
2126 return -ENOSPC;
2127
2128 /*
2129 * We don't want bitmaps, so just move along until we find a normal
2130 * extent entry.
2131 */
2132 while (entry->bitmap) {
2133 node = rb_next(&entry->offset_index);
2134 if (!node)
2135 return -ENOSPC;
2136 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2137 }
2138
2139 window_start = entry->offset;
2140 window_free = entry->bytes;
2141 max_extent = entry->bytes;
2142 first = entry;
2143 last = entry;
2144 prev = entry;
2145
2146 while (window_free <= min_bytes) {
2147 node = rb_next(&entry->offset_index);
2148 if (!node)
2149 return -ENOSPC;
2150 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2151
2152 if (entry->bitmap)
2153 continue;
2154 /*
2155 * we haven't filled the empty size and the window is
2156 * very large. reset and try again
2157 */
2158 if (entry->offset - (prev->offset + prev->bytes) > max_gap ||
2159 entry->offset - window_start > (min_bytes * 2)) {
2160 first = entry;
2161 window_start = entry->offset;
2162 window_free = entry->bytes;
2163 last = entry;
2164 max_extent = entry->bytes;
2165 } else {
2166 last = entry;
2167 window_free += entry->bytes;
2168 if (entry->bytes > max_extent)
2169 max_extent = entry->bytes;
2170 }
2171 prev = entry;
2172 }
2173
2174 cluster->window_start = first->offset;
2175
2176 node = &first->offset_index;
2177
2178 /*
2179 * now we've found our entries, pull them out of the free space
2180 * cache and put them into the cluster rbtree
2181 */
2182 do {
2183 int ret;
2184
2185 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2186 node = rb_next(&entry->offset_index);
2187 if (entry->bitmap)
2188 continue;
2189
Li Zefan34d52cb2011-03-29 13:46:06 +08002190 rb_erase(&entry->offset_index, &ctl->free_space_offset);
Josef Bacik4e69b592011-03-21 10:11:24 -04002191 ret = tree_insert_offset(&cluster->root, entry->offset,
2192 &entry->offset_index, 0);
2193 BUG_ON(ret);
2194 } while (node && entry != last);
2195
2196 cluster->max_size = max_extent;
2197
2198 return 0;
2199}
2200
2201/*
2202 * This specifically looks for bitmaps that may work in the cluster, we assume
2203 * that we have already failed to find extents that will work.
2204 */
2205static int setup_cluster_bitmap(struct btrfs_block_group_cache *block_group,
2206 struct btrfs_free_cluster *cluster,
2207 u64 offset, u64 bytes, u64 min_bytes)
2208{
Li Zefan34d52cb2011-03-29 13:46:06 +08002209 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik4e69b592011-03-21 10:11:24 -04002210 struct btrfs_free_space *entry;
2211 struct rb_node *node;
2212 int ret = -ENOSPC;
2213
Li Zefan34d52cb2011-03-29 13:46:06 +08002214 if (ctl->total_bitmaps == 0)
Josef Bacik4e69b592011-03-21 10:11:24 -04002215 return -ENOSPC;
2216
Li Zefan34d52cb2011-03-29 13:46:06 +08002217 entry = tree_search_offset(ctl, offset_to_bitmap(ctl, offset), 0, 1);
Josef Bacik4e69b592011-03-21 10:11:24 -04002218 if (!entry)
2219 return -ENOSPC;
2220
2221 node = &entry->offset_index;
2222 do {
2223 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2224 node = rb_next(&entry->offset_index);
2225 if (!entry->bitmap)
2226 continue;
2227 if (entry->bytes < min_bytes)
2228 continue;
2229 ret = btrfs_bitmap_cluster(block_group, entry, cluster, offset,
2230 bytes, min_bytes);
2231 } while (ret && node);
2232
2233 return ret;
2234}
2235
2236/*
Chris Masonfa9c0d792009-04-03 09:47:43 -04002237 * here we try to find a cluster of blocks in a block group. The goal
2238 * is to find at least bytes free and up to empty_size + bytes free.
2239 * We might not find them all in one contiguous area.
2240 *
2241 * returns zero and sets up cluster if things worked out, otherwise
2242 * it returns -enospc
2243 */
2244int btrfs_find_space_cluster(struct btrfs_trans_handle *trans,
Chris Mason451d7582009-06-09 20:28:34 -04002245 struct btrfs_root *root,
Chris Masonfa9c0d792009-04-03 09:47:43 -04002246 struct btrfs_block_group_cache *block_group,
2247 struct btrfs_free_cluster *cluster,
2248 u64 offset, u64 bytes, u64 empty_size)
2249{
Li Zefan34d52cb2011-03-29 13:46:06 +08002250 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002251 u64 min_bytes;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002252 int ret;
2253
2254 /* for metadata, allow allocates with more holes */
Chris Mason451d7582009-06-09 20:28:34 -04002255 if (btrfs_test_opt(root, SSD_SPREAD)) {
2256 min_bytes = bytes + empty_size;
2257 } else if (block_group->flags & BTRFS_BLOCK_GROUP_METADATA) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04002258 /*
2259 * we want to do larger allocations when we are
2260 * flushing out the delayed refs, it helps prevent
2261 * making more work as we go along.
2262 */
2263 if (trans->transaction->delayed_refs.flushing)
2264 min_bytes = max(bytes, (bytes + empty_size) >> 1);
2265 else
2266 min_bytes = max(bytes, (bytes + empty_size) >> 4);
2267 } else
2268 min_bytes = max(bytes, (bytes + empty_size) >> 2);
2269
Li Zefan34d52cb2011-03-29 13:46:06 +08002270 spin_lock(&ctl->tree_lock);
Josef Bacik7d0d2e82011-03-18 15:13:42 -04002271
2272 /*
2273 * If we know we don't have enough space to make a cluster don't even
2274 * bother doing all the work to try and find one.
2275 */
Li Zefan34d52cb2011-03-29 13:46:06 +08002276 if (ctl->free_space < min_bytes) {
2277 spin_unlock(&ctl->tree_lock);
Josef Bacik7d0d2e82011-03-18 15:13:42 -04002278 return -ENOSPC;
2279 }
2280
Chris Masonfa9c0d792009-04-03 09:47:43 -04002281 spin_lock(&cluster->lock);
2282
2283 /* someone already found a cluster, hooray */
2284 if (cluster->block_group) {
2285 ret = 0;
2286 goto out;
2287 }
Josef Bacik4e69b592011-03-21 10:11:24 -04002288
2289 ret = setup_cluster_no_bitmap(block_group, cluster, offset, bytes,
2290 min_bytes);
2291 if (ret)
2292 ret = setup_cluster_bitmap(block_group, cluster, offset,
2293 bytes, min_bytes);
2294
2295 if (!ret) {
2296 atomic_inc(&block_group->count);
2297 list_add_tail(&cluster->block_group_list,
2298 &block_group->cluster_list);
2299 cluster->block_group = block_group;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002300 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04002301out:
2302 spin_unlock(&cluster->lock);
Li Zefan34d52cb2011-03-29 13:46:06 +08002303 spin_unlock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002304
2305 return ret;
2306}
2307
2308/*
2309 * simple code to zero out a cluster
2310 */
2311void btrfs_init_free_cluster(struct btrfs_free_cluster *cluster)
2312{
2313 spin_lock_init(&cluster->lock);
2314 spin_lock_init(&cluster->refill_lock);
Eric Paris6bef4d32010-02-23 19:43:04 +00002315 cluster->root = RB_ROOT;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002316 cluster->max_size = 0;
2317 INIT_LIST_HEAD(&cluster->block_group_list);
2318 cluster->block_group = NULL;
2319}
2320
Li Dongyangf7039b12011-03-24 10:24:28 +00002321int btrfs_trim_block_group(struct btrfs_block_group_cache *block_group,
2322 u64 *trimmed, u64 start, u64 end, u64 minlen)
2323{
Li Zefan34d52cb2011-03-29 13:46:06 +08002324 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Li Dongyangf7039b12011-03-24 10:24:28 +00002325 struct btrfs_free_space *entry = NULL;
2326 struct btrfs_fs_info *fs_info = block_group->fs_info;
2327 u64 bytes = 0;
2328 u64 actually_trimmed;
2329 int ret = 0;
2330
2331 *trimmed = 0;
2332
2333 while (start < end) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002334 spin_lock(&ctl->tree_lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00002335
Li Zefan34d52cb2011-03-29 13:46:06 +08002336 if (ctl->free_space < minlen) {
2337 spin_unlock(&ctl->tree_lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00002338 break;
2339 }
2340
Li Zefan34d52cb2011-03-29 13:46:06 +08002341 entry = tree_search_offset(ctl, start, 0, 1);
Li Dongyangf7039b12011-03-24 10:24:28 +00002342 if (!entry)
Li Zefan34d52cb2011-03-29 13:46:06 +08002343 entry = tree_search_offset(ctl,
2344 offset_to_bitmap(ctl, start),
Li Dongyangf7039b12011-03-24 10:24:28 +00002345 1, 1);
2346
2347 if (!entry || entry->offset >= end) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002348 spin_unlock(&ctl->tree_lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00002349 break;
2350 }
2351
2352 if (entry->bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002353 ret = search_bitmap(ctl, entry, &start, &bytes);
Li Dongyangf7039b12011-03-24 10:24:28 +00002354 if (!ret) {
2355 if (start >= end) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002356 spin_unlock(&ctl->tree_lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00002357 break;
2358 }
2359 bytes = min(bytes, end - start);
Li Zefan34d52cb2011-03-29 13:46:06 +08002360 bitmap_clear_bits(ctl, entry, start, bytes);
Li Dongyangf7039b12011-03-24 10:24:28 +00002361 if (entry->bytes == 0)
Li Zefan34d52cb2011-03-29 13:46:06 +08002362 free_bitmap(ctl, entry);
Li Dongyangf7039b12011-03-24 10:24:28 +00002363 } else {
2364 start = entry->offset + BITS_PER_BITMAP *
2365 block_group->sectorsize;
Li Zefan34d52cb2011-03-29 13:46:06 +08002366 spin_unlock(&ctl->tree_lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00002367 ret = 0;
2368 continue;
2369 }
2370 } else {
2371 start = entry->offset;
2372 bytes = min(entry->bytes, end - start);
Li Zefan34d52cb2011-03-29 13:46:06 +08002373 unlink_free_space(ctl, entry);
Li Dongyangf7039b12011-03-24 10:24:28 +00002374 kfree(entry);
2375 }
2376
Li Zefan34d52cb2011-03-29 13:46:06 +08002377 spin_unlock(&ctl->tree_lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00002378
2379 if (bytes >= minlen) {
2380 int update_ret;
2381 update_ret = btrfs_update_reserved_bytes(block_group,
2382 bytes, 1, 1);
2383
2384 ret = btrfs_error_discard_extent(fs_info->extent_root,
2385 start,
2386 bytes,
2387 &actually_trimmed);
2388
Li Zefan34d52cb2011-03-29 13:46:06 +08002389 btrfs_add_free_space(block_group, start, bytes);
Li Dongyangf7039b12011-03-24 10:24:28 +00002390 if (!update_ret)
2391 btrfs_update_reserved_bytes(block_group,
2392 bytes, 0, 1);
2393
2394 if (ret)
2395 break;
2396 *trimmed += actually_trimmed;
2397 }
2398 start += bytes;
2399 bytes = 0;
2400
2401 if (fatal_signal_pending(current)) {
2402 ret = -ERESTARTSYS;
2403 break;
2404 }
2405
2406 cond_resched();
2407 }
2408
2409 return ret;
2410}
Li Zefan581bb052011-04-20 10:06:11 +08002411
2412/*
2413 * Find the left-most item in the cache tree, and then return the
2414 * smallest inode number in the item.
2415 *
2416 * Note: the returned inode number may not be the smallest one in
2417 * the tree, if the left-most item is a bitmap.
2418 */
2419u64 btrfs_find_ino_for_alloc(struct btrfs_root *fs_root)
2420{
2421 struct btrfs_free_space_ctl *ctl = fs_root->free_ino_ctl;
2422 struct btrfs_free_space *entry = NULL;
2423 u64 ino = 0;
2424
2425 spin_lock(&ctl->tree_lock);
2426
2427 if (RB_EMPTY_ROOT(&ctl->free_space_offset))
2428 goto out;
2429
2430 entry = rb_entry(rb_first(&ctl->free_space_offset),
2431 struct btrfs_free_space, offset_index);
2432
2433 if (!entry->bitmap) {
2434 ino = entry->offset;
2435
2436 unlink_free_space(ctl, entry);
2437 entry->offset++;
2438 entry->bytes--;
2439 if (!entry->bytes)
2440 kmem_cache_free(btrfs_free_space_cachep, entry);
2441 else
2442 link_free_space(ctl, entry);
2443 } else {
2444 u64 offset = 0;
2445 u64 count = 1;
2446 int ret;
2447
2448 ret = search_bitmap(ctl, entry, &offset, &count);
2449 BUG_ON(ret);
2450
2451 ino = offset;
2452 bitmap_clear_bits(ctl, entry, offset, 1);
2453 if (entry->bytes == 0)
2454 free_bitmap(ctl, entry);
2455 }
2456out:
2457 spin_unlock(&ctl->tree_lock);
2458
2459 return ino;
2460}