blob: 2ce89bfc881592a9e846860ea83d476c6d5efe93 [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
Josef Bacik0af3d002010-06-21 14:48:16 -040036struct inode *lookup_free_space_inode(struct btrfs_root *root,
37 struct btrfs_block_group_cache
38 *block_group, struct btrfs_path *path)
39{
40 struct btrfs_key key;
41 struct btrfs_key location;
42 struct btrfs_disk_key disk_key;
43 struct btrfs_free_space_header *header;
44 struct extent_buffer *leaf;
45 struct inode *inode = NULL;
46 int ret;
47
48 spin_lock(&block_group->lock);
49 if (block_group->inode)
50 inode = igrab(block_group->inode);
51 spin_unlock(&block_group->lock);
52 if (inode)
53 return inode;
54
55 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
56 key.offset = block_group->key.objectid;
57 key.type = 0;
58
59 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
60 if (ret < 0)
61 return ERR_PTR(ret);
62 if (ret > 0) {
63 btrfs_release_path(root, path);
64 return ERR_PTR(-ENOENT);
65 }
66
67 leaf = path->nodes[0];
68 header = btrfs_item_ptr(leaf, path->slots[0],
69 struct btrfs_free_space_header);
70 btrfs_free_space_key(leaf, header, &disk_key);
71 btrfs_disk_key_to_cpu(&location, &disk_key);
72 btrfs_release_path(root, path);
73
74 inode = btrfs_iget(root->fs_info->sb, &location, root, NULL);
75 if (!inode)
76 return ERR_PTR(-ENOENT);
77 if (IS_ERR(inode))
78 return inode;
79 if (is_bad_inode(inode)) {
80 iput(inode);
81 return ERR_PTR(-ENOENT);
82 }
83
Miao Xieadae52b2011-03-31 09:43:23 +000084 inode->i_mapping->flags &= ~__GFP_FS;
85
Josef Bacik0af3d002010-06-21 14:48:16 -040086 spin_lock(&block_group->lock);
87 if (!root->fs_info->closing) {
88 block_group->inode = igrab(inode);
89 block_group->iref = 1;
90 }
91 spin_unlock(&block_group->lock);
92
93 return inode;
94}
95
96int create_free_space_inode(struct btrfs_root *root,
97 struct btrfs_trans_handle *trans,
98 struct btrfs_block_group_cache *block_group,
99 struct btrfs_path *path)
100{
101 struct btrfs_key key;
102 struct btrfs_disk_key disk_key;
103 struct btrfs_free_space_header *header;
104 struct btrfs_inode_item *inode_item;
105 struct extent_buffer *leaf;
106 u64 objectid;
107 int ret;
108
Li Zefan581bb052011-04-20 10:06:11 +0800109 ret = btrfs_find_free_objectid(root, &objectid);
Josef Bacik0af3d002010-06-21 14:48:16 -0400110 if (ret < 0)
111 return ret;
112
113 ret = btrfs_insert_empty_inode(trans, root, path, objectid);
114 if (ret)
115 return ret;
116
117 leaf = path->nodes[0];
118 inode_item = btrfs_item_ptr(leaf, path->slots[0],
119 struct btrfs_inode_item);
120 btrfs_item_key(leaf, &disk_key, path->slots[0]);
121 memset_extent_buffer(leaf, 0, (unsigned long)inode_item,
122 sizeof(*inode_item));
123 btrfs_set_inode_generation(leaf, inode_item, trans->transid);
124 btrfs_set_inode_size(leaf, inode_item, 0);
125 btrfs_set_inode_nbytes(leaf, inode_item, 0);
126 btrfs_set_inode_uid(leaf, inode_item, 0);
127 btrfs_set_inode_gid(leaf, inode_item, 0);
128 btrfs_set_inode_mode(leaf, inode_item, S_IFREG | 0600);
129 btrfs_set_inode_flags(leaf, inode_item, BTRFS_INODE_NOCOMPRESS |
130 BTRFS_INODE_PREALLOC | BTRFS_INODE_NODATASUM);
131 btrfs_set_inode_nlink(leaf, inode_item, 1);
132 btrfs_set_inode_transid(leaf, inode_item, trans->transid);
133 btrfs_set_inode_block_group(leaf, inode_item,
134 block_group->key.objectid);
135 btrfs_mark_buffer_dirty(leaf);
136 btrfs_release_path(root, path);
137
138 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
139 key.offset = block_group->key.objectid;
140 key.type = 0;
141
142 ret = btrfs_insert_empty_item(trans, root, path, &key,
143 sizeof(struct btrfs_free_space_header));
144 if (ret < 0) {
145 btrfs_release_path(root, path);
146 return ret;
147 }
148 leaf = path->nodes[0];
149 header = btrfs_item_ptr(leaf, path->slots[0],
150 struct btrfs_free_space_header);
151 memset_extent_buffer(leaf, 0, (unsigned long)header, sizeof(*header));
152 btrfs_set_free_space_key(leaf, header, &disk_key);
153 btrfs_mark_buffer_dirty(leaf);
154 btrfs_release_path(root, path);
155
156 return 0;
157}
158
159int btrfs_truncate_free_space_cache(struct btrfs_root *root,
160 struct btrfs_trans_handle *trans,
161 struct btrfs_path *path,
162 struct inode *inode)
163{
164 loff_t oldsize;
165 int ret = 0;
166
167 trans->block_rsv = root->orphan_block_rsv;
168 ret = btrfs_block_rsv_check(trans, root,
169 root->orphan_block_rsv,
170 0, 5);
171 if (ret)
172 return ret;
173
174 oldsize = i_size_read(inode);
175 btrfs_i_size_write(inode, 0);
176 truncate_pagecache(inode, oldsize, 0);
177
178 /*
179 * We don't need an orphan item because truncating the free space cache
180 * will never be split across transactions.
181 */
182 ret = btrfs_truncate_inode_items(trans, root, inode,
183 0, BTRFS_EXTENT_DATA_KEY);
184 if (ret) {
185 WARN_ON(1);
186 return ret;
187 }
188
189 return btrfs_update_inode(trans, root, inode);
190}
191
Josef Bacik9d66e232010-08-25 16:54:15 -0400192static int readahead_cache(struct inode *inode)
193{
194 struct file_ra_state *ra;
195 unsigned long last_index;
196
197 ra = kzalloc(sizeof(*ra), GFP_NOFS);
198 if (!ra)
199 return -ENOMEM;
200
201 file_ra_state_init(ra, inode->i_mapping);
202 last_index = (i_size_read(inode) - 1) >> PAGE_CACHE_SHIFT;
203
204 page_cache_sync_readahead(inode->i_mapping, ra, NULL, 0, last_index);
205
206 kfree(ra);
207
208 return 0;
209}
210
211int load_free_space_cache(struct btrfs_fs_info *fs_info,
212 struct btrfs_block_group_cache *block_group)
213{
Li Zefan34d52cb2011-03-29 13:46:06 +0800214 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik9d66e232010-08-25 16:54:15 -0400215 struct btrfs_root *root = fs_info->tree_root;
216 struct inode *inode;
217 struct btrfs_free_space_header *header;
218 struct extent_buffer *leaf;
219 struct page *page;
220 struct btrfs_path *path;
221 u32 *checksums = NULL, *crc;
222 char *disk_crcs = NULL;
223 struct btrfs_key key;
224 struct list_head bitmaps;
225 u64 num_entries;
226 u64 num_bitmaps;
227 u64 generation;
Josef Bacik43be2142011-04-01 14:55:00 +0000228 u64 used = btrfs_block_group_used(&block_group->item);
Josef Bacik9d66e232010-08-25 16:54:15 -0400229 u32 cur_crc = ~(u32)0;
230 pgoff_t index = 0;
231 unsigned long first_page_offset;
232 int num_checksums;
233 int ret = 0;
234
235 /*
236 * If we're unmounting then just return, since this does a search on the
237 * normal root and not the commit root and we could deadlock.
238 */
239 smp_mb();
240 if (fs_info->closing)
241 return 0;
242
243 /*
244 * If this block group has been marked to be cleared for one reason or
245 * another then we can't trust the on disk cache, so just return.
246 */
247 spin_lock(&block_group->lock);
248 if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) {
Josef Bacik9d66e232010-08-25 16:54:15 -0400249 spin_unlock(&block_group->lock);
250 return 0;
251 }
252 spin_unlock(&block_group->lock);
253
254 INIT_LIST_HEAD(&bitmaps);
255
256 path = btrfs_alloc_path();
257 if (!path)
258 return 0;
259
260 inode = lookup_free_space_inode(root, block_group, path);
261 if (IS_ERR(inode)) {
262 btrfs_free_path(path);
263 return 0;
264 }
265
266 /* Nothing in the space cache, goodbye */
267 if (!i_size_read(inode)) {
268 btrfs_free_path(path);
269 goto out;
270 }
271
272 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
273 key.offset = block_group->key.objectid;
274 key.type = 0;
275
276 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
277 if (ret) {
278 btrfs_free_path(path);
279 goto out;
280 }
281
282 leaf = path->nodes[0];
283 header = btrfs_item_ptr(leaf, path->slots[0],
284 struct btrfs_free_space_header);
285 num_entries = btrfs_free_space_entries(leaf, header);
286 num_bitmaps = btrfs_free_space_bitmaps(leaf, header);
287 generation = btrfs_free_space_generation(leaf, header);
288 btrfs_free_path(path);
289
290 if (BTRFS_I(inode)->generation != generation) {
291 printk(KERN_ERR "btrfs: free space inode generation (%llu) did"
292 " not match free space cache generation (%llu) for "
293 "block group %llu\n",
294 (unsigned long long)BTRFS_I(inode)->generation,
295 (unsigned long long)generation,
296 (unsigned long long)block_group->key.objectid);
Josef Bacik2b209822010-12-03 13:17:53 -0500297 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400298 }
299
300 if (!num_entries)
301 goto out;
302
303 /* Setup everything for doing checksumming */
304 num_checksums = i_size_read(inode) / PAGE_CACHE_SIZE;
305 checksums = crc = kzalloc(sizeof(u32) * num_checksums, GFP_NOFS);
306 if (!checksums)
307 goto out;
308 first_page_offset = (sizeof(u32) * num_checksums) + sizeof(u64);
309 disk_crcs = kzalloc(first_page_offset, GFP_NOFS);
310 if (!disk_crcs)
311 goto out;
312
313 ret = readahead_cache(inode);
314 if (ret) {
315 ret = 0;
316 goto out;
317 }
318
319 while (1) {
320 struct btrfs_free_space_entry *entry;
321 struct btrfs_free_space *e;
322 void *addr;
323 unsigned long offset = 0;
324 unsigned long start_offset = 0;
325 int need_loop = 0;
326
327 if (!num_entries && !num_bitmaps)
328 break;
329
330 if (index == 0) {
331 start_offset = first_page_offset;
332 offset = start_offset;
333 }
334
335 page = grab_cache_page(inode->i_mapping, index);
336 if (!page) {
337 ret = 0;
338 goto free_cache;
339 }
340
341 if (!PageUptodate(page)) {
342 btrfs_readpage(NULL, page);
343 lock_page(page);
344 if (!PageUptodate(page)) {
345 unlock_page(page);
346 page_cache_release(page);
347 printk(KERN_ERR "btrfs: error reading free "
348 "space cache: %llu\n",
349 (unsigned long long)
350 block_group->key.objectid);
351 goto free_cache;
352 }
353 }
354 addr = kmap(page);
355
356 if (index == 0) {
357 u64 *gen;
358
359 memcpy(disk_crcs, addr, first_page_offset);
360 gen = addr + (sizeof(u32) * num_checksums);
361 if (*gen != BTRFS_I(inode)->generation) {
362 printk(KERN_ERR "btrfs: space cache generation"
363 " (%llu) does not match inode (%llu) "
364 "for block group %llu\n",
365 (unsigned long long)*gen,
366 (unsigned long long)
367 BTRFS_I(inode)->generation,
368 (unsigned long long)
369 block_group->key.objectid);
370 kunmap(page);
371 unlock_page(page);
372 page_cache_release(page);
373 goto free_cache;
374 }
375 crc = (u32 *)disk_crcs;
376 }
377 entry = addr + start_offset;
378
379 /* First lets check our crc before we do anything fun */
380 cur_crc = ~(u32)0;
381 cur_crc = btrfs_csum_data(root, addr + start_offset, cur_crc,
382 PAGE_CACHE_SIZE - start_offset);
383 btrfs_csum_final(cur_crc, (char *)&cur_crc);
384 if (cur_crc != *crc) {
385 printk(KERN_ERR "btrfs: crc mismatch for page %lu in "
386 "block group %llu\n", index,
387 (unsigned long long)block_group->key.objectid);
388 kunmap(page);
389 unlock_page(page);
390 page_cache_release(page);
391 goto free_cache;
392 }
393 crc++;
394
395 while (1) {
396 if (!num_entries)
397 break;
398
399 need_loop = 1;
Josef Bacikdc89e982011-01-28 17:05:48 -0500400 e = kmem_cache_zalloc(btrfs_free_space_cachep,
401 GFP_NOFS);
Josef Bacik9d66e232010-08-25 16:54:15 -0400402 if (!e) {
403 kunmap(page);
404 unlock_page(page);
405 page_cache_release(page);
406 goto free_cache;
407 }
408
409 e->offset = le64_to_cpu(entry->offset);
410 e->bytes = le64_to_cpu(entry->bytes);
411 if (!e->bytes) {
412 kunmap(page);
Josef Bacikdc89e982011-01-28 17:05:48 -0500413 kmem_cache_free(btrfs_free_space_cachep, e);
Josef Bacik9d66e232010-08-25 16:54:15 -0400414 unlock_page(page);
415 page_cache_release(page);
416 goto free_cache;
417 }
418
419 if (entry->type == BTRFS_FREE_SPACE_EXTENT) {
Li Zefan34d52cb2011-03-29 13:46:06 +0800420 spin_lock(&ctl->tree_lock);
421 ret = link_free_space(ctl, e);
422 spin_unlock(&ctl->tree_lock);
Josef Bacik9d66e232010-08-25 16:54:15 -0400423 BUG_ON(ret);
424 } else {
425 e->bitmap = kzalloc(PAGE_CACHE_SIZE, GFP_NOFS);
426 if (!e->bitmap) {
427 kunmap(page);
Josef Bacikdc89e982011-01-28 17:05:48 -0500428 kmem_cache_free(
429 btrfs_free_space_cachep, e);
Josef Bacik9d66e232010-08-25 16:54:15 -0400430 unlock_page(page);
431 page_cache_release(page);
432 goto free_cache;
433 }
Li Zefan34d52cb2011-03-29 13:46:06 +0800434 spin_lock(&ctl->tree_lock);
435 ret = link_free_space(ctl, e);
436 ctl->total_bitmaps++;
437 ctl->op->recalc_thresholds(ctl);
438 spin_unlock(&ctl->tree_lock);
Josef Bacik9d66e232010-08-25 16:54:15 -0400439 list_add_tail(&e->list, &bitmaps);
440 }
441
442 num_entries--;
443 offset += sizeof(struct btrfs_free_space_entry);
444 if (offset + sizeof(struct btrfs_free_space_entry) >=
445 PAGE_CACHE_SIZE)
446 break;
447 entry++;
448 }
449
450 /*
451 * We read an entry out of this page, we need to move on to the
452 * next page.
453 */
454 if (need_loop) {
455 kunmap(page);
456 goto next;
457 }
458
459 /*
460 * We add the bitmaps at the end of the entries in order that
461 * the bitmap entries are added to the cache.
462 */
463 e = list_entry(bitmaps.next, struct btrfs_free_space, list);
464 list_del_init(&e->list);
465 memcpy(e->bitmap, addr, PAGE_CACHE_SIZE);
466 kunmap(page);
467 num_bitmaps--;
468next:
469 unlock_page(page);
470 page_cache_release(page);
471 index++;
472 }
473
Li Zefan34d52cb2011-03-29 13:46:06 +0800474 spin_lock(&ctl->tree_lock);
475 if (ctl->free_space != (block_group->key.offset - used -
476 block_group->bytes_super)) {
477 spin_unlock(&ctl->tree_lock);
Josef Bacik43be2142011-04-01 14:55:00 +0000478 printk(KERN_ERR "block group %llu has an wrong amount of free "
479 "space\n", block_group->key.objectid);
480 ret = 0;
481 goto free_cache;
482 }
Li Zefan34d52cb2011-03-29 13:46:06 +0800483 spin_unlock(&ctl->tree_lock);
Josef Bacik43be2142011-04-01 14:55:00 +0000484
Josef Bacik9d66e232010-08-25 16:54:15 -0400485 ret = 1;
486out:
487 kfree(checksums);
488 kfree(disk_crcs);
489 iput(inode);
490 return ret;
491
492free_cache:
493 /* This cache is bogus, make sure it gets cleared */
494 spin_lock(&block_group->lock);
495 block_group->disk_cache_state = BTRFS_DC_CLEAR;
496 spin_unlock(&block_group->lock);
497 btrfs_remove_free_space_cache(block_group);
498 goto out;
499}
500
Josef Bacik0cb59c92010-07-02 12:14:14 -0400501int btrfs_write_out_cache(struct btrfs_root *root,
502 struct btrfs_trans_handle *trans,
503 struct btrfs_block_group_cache *block_group,
504 struct btrfs_path *path)
505{
Li Zefan34d52cb2011-03-29 13:46:06 +0800506 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400507 struct btrfs_free_space_header *header;
508 struct extent_buffer *leaf;
509 struct inode *inode;
510 struct rb_node *node;
511 struct list_head *pos, *n;
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400512 struct page **pages;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400513 struct page *page;
514 struct extent_state *cached_state = NULL;
Josef Bacik43be2142011-04-01 14:55:00 +0000515 struct btrfs_free_cluster *cluster = NULL;
516 struct extent_io_tree *unpin = NULL;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400517 struct list_head bitmap_list;
518 struct btrfs_key key;
Josef Bacik43be2142011-04-01 14:55:00 +0000519 u64 start, end, len;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400520 u64 bytes = 0;
521 u32 *crc, *checksums;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400522 unsigned long first_page_offset;
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400523 int index = 0, num_pages = 0;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400524 int entries = 0;
525 int bitmaps = 0;
526 int ret = 0;
Josef Bacik43be2142011-04-01 14:55:00 +0000527 bool next_page = false;
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400528 bool out_of_space = false;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400529
530 root = root->fs_info->tree_root;
531
532 INIT_LIST_HEAD(&bitmap_list);
533
534 spin_lock(&block_group->lock);
535 if (block_group->disk_cache_state < BTRFS_DC_SETUP) {
536 spin_unlock(&block_group->lock);
537 return 0;
538 }
539 spin_unlock(&block_group->lock);
540
541 inode = lookup_free_space_inode(root, block_group, path);
542 if (IS_ERR(inode))
543 return 0;
544
545 if (!i_size_read(inode)) {
546 iput(inode);
547 return 0;
548 }
549
Li Zefan34d52cb2011-03-29 13:46:06 +0800550 node = rb_first(&ctl->free_space_offset);
Josef Bacik2b209822010-12-03 13:17:53 -0500551 if (!node) {
552 iput(inode);
553 return 0;
554 }
555
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400556 num_pages = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
557 PAGE_CACHE_SHIFT;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400558 filemap_write_and_wait(inode->i_mapping);
559 btrfs_wait_ordered_range(inode, inode->i_size &
560 ~(root->sectorsize - 1), (u64)-1);
561
562 /* We need a checksum per page. */
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400563 crc = checksums = kzalloc(sizeof(u32) * num_pages, GFP_NOFS);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400564 if (!crc) {
565 iput(inode);
566 return 0;
567 }
568
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400569 pages = kzalloc(sizeof(struct page *) * num_pages, GFP_NOFS);
570 if (!pages) {
571 kfree(crc);
572 iput(inode);
573 return 0;
574 }
575
Josef Bacik0cb59c92010-07-02 12:14:14 -0400576 /* Since the first page has all of our checksums and our generation we
577 * need to calculate the offset into the page that we can start writing
578 * our entries.
579 */
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400580 first_page_offset = (sizeof(u32) * num_pages) + sizeof(u64);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400581
Josef Bacik43be2142011-04-01 14:55:00 +0000582 /* Get the cluster for this block_group if it exists */
583 if (!list_empty(&block_group->cluster_list))
584 cluster = list_entry(block_group->cluster_list.next,
585 struct btrfs_free_cluster,
586 block_group_list);
587
588 /*
589 * We shouldn't have switched the pinned extents yet so this is the
590 * right one
591 */
592 unpin = root->fs_info->pinned_extents;
593
Josef Bacik0cb59c92010-07-02 12:14:14 -0400594 /*
595 * Lock all pages first so we can lock the extent safely.
596 *
597 * NOTE: Because we hold the ref the entire time we're going to write to
598 * the page find_get_page should never fail, so we don't do a check
599 * after find_get_page at this point. Just putting this here so people
600 * know and don't freak out.
601 */
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400602 while (index < num_pages) {
Josef Bacik0cb59c92010-07-02 12:14:14 -0400603 page = grab_cache_page(inode->i_mapping, index);
604 if (!page) {
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400605 int i;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400606
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400607 for (i = 0; i < num_pages; i++) {
608 unlock_page(pages[i]);
609 page_cache_release(pages[i]);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400610 }
611 goto out_free;
612 }
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400613 pages[index] = page;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400614 index++;
615 }
616
617 index = 0;
618 lock_extent_bits(&BTRFS_I(inode)->io_tree, 0, i_size_read(inode) - 1,
619 0, &cached_state, GFP_NOFS);
620
Josef Bacik43be2142011-04-01 14:55:00 +0000621 /*
622 * When searching for pinned extents, we need to start at our start
623 * offset.
624 */
625 start = block_group->key.objectid;
626
Josef Bacik0cb59c92010-07-02 12:14:14 -0400627 /* Write out the extent entries */
628 do {
629 struct btrfs_free_space_entry *entry;
630 void *addr;
631 unsigned long offset = 0;
632 unsigned long start_offset = 0;
633
Josef Bacik43be2142011-04-01 14:55:00 +0000634 next_page = false;
635
Josef Bacik0cb59c92010-07-02 12:14:14 -0400636 if (index == 0) {
637 start_offset = first_page_offset;
638 offset = start_offset;
639 }
640
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400641 if (index >= num_pages) {
642 out_of_space = true;
643 break;
644 }
645
646 page = pages[index];
Josef Bacik0cb59c92010-07-02 12:14:14 -0400647
648 addr = kmap(page);
649 entry = addr + start_offset;
650
651 memset(addr, 0, PAGE_CACHE_SIZE);
Josef Bacik43be2142011-04-01 14:55:00 +0000652 while (node && !next_page) {
Josef Bacik0cb59c92010-07-02 12:14:14 -0400653 struct btrfs_free_space *e;
654
655 e = rb_entry(node, struct btrfs_free_space, offset_index);
656 entries++;
657
658 entry->offset = cpu_to_le64(e->offset);
659 entry->bytes = cpu_to_le64(e->bytes);
660 if (e->bitmap) {
661 entry->type = BTRFS_FREE_SPACE_BITMAP;
662 list_add_tail(&e->list, &bitmap_list);
663 bitmaps++;
664 } else {
665 entry->type = BTRFS_FREE_SPACE_EXTENT;
666 }
667 node = rb_next(node);
Josef Bacik43be2142011-04-01 14:55:00 +0000668 if (!node && cluster) {
669 node = rb_first(&cluster->root);
670 cluster = NULL;
671 }
Josef Bacik0cb59c92010-07-02 12:14:14 -0400672 offset += sizeof(struct btrfs_free_space_entry);
673 if (offset + sizeof(struct btrfs_free_space_entry) >=
674 PAGE_CACHE_SIZE)
Josef Bacik43be2142011-04-01 14:55:00 +0000675 next_page = true;
676 entry++;
677 }
678
679 /*
680 * We want to add any pinned extents to our free space cache
681 * so we don't leak the space
682 */
683 while (!next_page && (start < block_group->key.objectid +
684 block_group->key.offset)) {
685 ret = find_first_extent_bit(unpin, start, &start, &end,
686 EXTENT_DIRTY);
687 if (ret) {
688 ret = 0;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400689 break;
Josef Bacik43be2142011-04-01 14:55:00 +0000690 }
691
692 /* This pinned extent is out of our range */
693 if (start >= block_group->key.objectid +
694 block_group->key.offset)
695 break;
696
697 len = block_group->key.objectid +
698 block_group->key.offset - start;
699 len = min(len, end + 1 - start);
700
701 entries++;
702 entry->offset = cpu_to_le64(start);
703 entry->bytes = cpu_to_le64(len);
704 entry->type = BTRFS_FREE_SPACE_EXTENT;
705
706 start = end + 1;
707 offset += sizeof(struct btrfs_free_space_entry);
708 if (offset + sizeof(struct btrfs_free_space_entry) >=
709 PAGE_CACHE_SIZE)
710 next_page = true;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400711 entry++;
712 }
713 *crc = ~(u32)0;
714 *crc = btrfs_csum_data(root, addr + start_offset, *crc,
715 PAGE_CACHE_SIZE - start_offset);
716 kunmap(page);
717
718 btrfs_csum_final(*crc, (char *)crc);
719 crc++;
720
721 bytes += PAGE_CACHE_SIZE;
722
Josef Bacik0cb59c92010-07-02 12:14:14 -0400723 index++;
Josef Bacik43be2142011-04-01 14:55:00 +0000724 } while (node || next_page);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400725
726 /* Write out the bitmaps */
727 list_for_each_safe(pos, n, &bitmap_list) {
728 void *addr;
729 struct btrfs_free_space *entry =
730 list_entry(pos, struct btrfs_free_space, list);
731
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400732 if (index >= num_pages) {
733 out_of_space = true;
734 break;
735 }
Chris Masonf65647c2011-04-18 08:55:34 -0400736 page = pages[index];
Josef Bacik0cb59c92010-07-02 12:14:14 -0400737
738 addr = kmap(page);
739 memcpy(addr, entry->bitmap, PAGE_CACHE_SIZE);
740 *crc = ~(u32)0;
741 *crc = btrfs_csum_data(root, addr, *crc, PAGE_CACHE_SIZE);
742 kunmap(page);
743 btrfs_csum_final(*crc, (char *)crc);
744 crc++;
745 bytes += PAGE_CACHE_SIZE;
746
Josef Bacik0cb59c92010-07-02 12:14:14 -0400747 list_del_init(&entry->list);
748 index++;
749 }
750
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400751 if (out_of_space) {
752 btrfs_drop_pages(pages, num_pages);
753 unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
754 i_size_read(inode) - 1, &cached_state,
755 GFP_NOFS);
756 ret = 0;
757 goto out_free;
758 }
759
Josef Bacik0cb59c92010-07-02 12:14:14 -0400760 /* Zero out the rest of the pages just to make sure */
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400761 while (index < num_pages) {
Josef Bacik0cb59c92010-07-02 12:14:14 -0400762 void *addr;
763
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400764 page = pages[index];
Josef Bacik0cb59c92010-07-02 12:14:14 -0400765 addr = kmap(page);
766 memset(addr, 0, PAGE_CACHE_SIZE);
767 kunmap(page);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400768 bytes += PAGE_CACHE_SIZE;
769 index++;
770 }
771
Josef Bacik0cb59c92010-07-02 12:14:14 -0400772 /* Write the checksums and trans id to the first page */
773 {
774 void *addr;
775 u64 *gen;
776
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400777 page = pages[0];
Josef Bacik0cb59c92010-07-02 12:14:14 -0400778
779 addr = kmap(page);
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400780 memcpy(addr, checksums, sizeof(u32) * num_pages);
781 gen = addr + (sizeof(u32) * num_pages);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400782 *gen = trans->transid;
783 kunmap(page);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400784 }
Josef Bacik0cb59c92010-07-02 12:14:14 -0400785
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400786 ret = btrfs_dirty_pages(root, inode, pages, num_pages, 0,
787 bytes, &cached_state);
788 btrfs_drop_pages(pages, num_pages);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400789 unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
790 i_size_read(inode) - 1, &cached_state, GFP_NOFS);
791
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400792 if (ret) {
793 ret = 0;
794 goto out_free;
795 }
796
797 BTRFS_I(inode)->generation = trans->transid;
798
Josef Bacik0cb59c92010-07-02 12:14:14 -0400799 filemap_write_and_wait(inode->i_mapping);
800
801 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
802 key.offset = block_group->key.objectid;
803 key.type = 0;
804
805 ret = btrfs_search_slot(trans, root, &key, path, 1, 1);
806 if (ret < 0) {
807 ret = 0;
808 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, bytes - 1,
809 EXTENT_DIRTY | EXTENT_DELALLOC |
810 EXTENT_DO_ACCOUNTING, 0, 0, NULL, GFP_NOFS);
811 goto out_free;
812 }
813 leaf = path->nodes[0];
814 if (ret > 0) {
815 struct btrfs_key found_key;
816 BUG_ON(!path->slots[0]);
817 path->slots[0]--;
818 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
819 if (found_key.objectid != BTRFS_FREE_SPACE_OBJECTID ||
820 found_key.offset != block_group->key.objectid) {
821 ret = 0;
822 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, bytes - 1,
823 EXTENT_DIRTY | EXTENT_DELALLOC |
824 EXTENT_DO_ACCOUNTING, 0, 0, NULL,
825 GFP_NOFS);
826 btrfs_release_path(root, path);
827 goto out_free;
828 }
829 }
830 header = btrfs_item_ptr(leaf, path->slots[0],
831 struct btrfs_free_space_header);
832 btrfs_set_free_space_entries(leaf, header, entries);
833 btrfs_set_free_space_bitmaps(leaf, header, bitmaps);
834 btrfs_set_free_space_generation(leaf, header, trans->transid);
835 btrfs_mark_buffer_dirty(leaf);
836 btrfs_release_path(root, path);
837
838 ret = 1;
839
840out_free:
841 if (ret == 0) {
842 invalidate_inode_pages2_range(inode->i_mapping, 0, index);
843 spin_lock(&block_group->lock);
844 block_group->disk_cache_state = BTRFS_DC_ERROR;
845 spin_unlock(&block_group->lock);
846 BTRFS_I(inode)->generation = 0;
847 }
848 kfree(checksums);
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400849 kfree(pages);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400850 btrfs_update_inode(trans, root, inode);
851 iput(inode);
852 return ret;
853}
854
Li Zefan34d52cb2011-03-29 13:46:06 +0800855static inline unsigned long offset_to_bit(u64 bitmap_start, u32 unit,
Josef Bacik96303082009-07-13 21:29:25 -0400856 u64 offset)
857{
858 BUG_ON(offset < bitmap_start);
859 offset -= bitmap_start;
Li Zefan34d52cb2011-03-29 13:46:06 +0800860 return (unsigned long)(div_u64(offset, unit));
Josef Bacik96303082009-07-13 21:29:25 -0400861}
862
Li Zefan34d52cb2011-03-29 13:46:06 +0800863static inline unsigned long bytes_to_bits(u64 bytes, u32 unit)
Josef Bacik96303082009-07-13 21:29:25 -0400864{
Li Zefan34d52cb2011-03-29 13:46:06 +0800865 return (unsigned long)(div_u64(bytes, unit));
Josef Bacik96303082009-07-13 21:29:25 -0400866}
867
Li Zefan34d52cb2011-03-29 13:46:06 +0800868static inline u64 offset_to_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -0400869 u64 offset)
870{
871 u64 bitmap_start;
872 u64 bytes_per_bitmap;
873
Li Zefan34d52cb2011-03-29 13:46:06 +0800874 bytes_per_bitmap = BITS_PER_BITMAP * ctl->unit;
875 bitmap_start = offset - ctl->start;
Josef Bacik96303082009-07-13 21:29:25 -0400876 bitmap_start = div64_u64(bitmap_start, bytes_per_bitmap);
877 bitmap_start *= bytes_per_bitmap;
Li Zefan34d52cb2011-03-29 13:46:06 +0800878 bitmap_start += ctl->start;
Josef Bacik96303082009-07-13 21:29:25 -0400879
880 return bitmap_start;
881}
Josef Bacik0f9dd462008-09-23 13:14:11 -0400882
883static int tree_insert_offset(struct rb_root *root, u64 offset,
Josef Bacik96303082009-07-13 21:29:25 -0400884 struct rb_node *node, int bitmap)
Josef Bacik0f9dd462008-09-23 13:14:11 -0400885{
886 struct rb_node **p = &root->rb_node;
887 struct rb_node *parent = NULL;
888 struct btrfs_free_space *info;
889
890 while (*p) {
891 parent = *p;
892 info = rb_entry(parent, struct btrfs_free_space, offset_index);
893
Josef Bacik96303082009-07-13 21:29:25 -0400894 if (offset < info->offset) {
Josef Bacik0f9dd462008-09-23 13:14:11 -0400895 p = &(*p)->rb_left;
Josef Bacik96303082009-07-13 21:29:25 -0400896 } else if (offset > info->offset) {
Josef Bacik0f9dd462008-09-23 13:14:11 -0400897 p = &(*p)->rb_right;
Josef Bacik96303082009-07-13 21:29:25 -0400898 } else {
899 /*
900 * we could have a bitmap entry and an extent entry
901 * share the same offset. If this is the case, we want
902 * the extent entry to always be found first if we do a
903 * linear search through the tree, since we want to have
904 * the quickest allocation time, and allocating from an
905 * extent is faster than allocating from a bitmap. So
906 * if we're inserting a bitmap and we find an entry at
907 * this offset, we want to go right, or after this entry
908 * logically. If we are inserting an extent and we've
909 * found a bitmap, we want to go left, or before
910 * logically.
911 */
912 if (bitmap) {
913 WARN_ON(info->bitmap);
914 p = &(*p)->rb_right;
915 } else {
916 WARN_ON(!info->bitmap);
917 p = &(*p)->rb_left;
918 }
919 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400920 }
921
922 rb_link_node(node, parent, p);
923 rb_insert_color(node, root);
924
925 return 0;
926}
927
928/*
Josef Bacik70cb0742009-04-03 10:14:19 -0400929 * searches the tree for the given offset.
930 *
Josef Bacik96303082009-07-13 21:29:25 -0400931 * fuzzy - If this is set, then we are trying to make an allocation, and we just
932 * want a section that has at least bytes size and comes at or after the given
933 * offset.
Josef Bacik0f9dd462008-09-23 13:14:11 -0400934 */
Josef Bacik96303082009-07-13 21:29:25 -0400935static struct btrfs_free_space *
Li Zefan34d52cb2011-03-29 13:46:06 +0800936tree_search_offset(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -0400937 u64 offset, int bitmap_only, int fuzzy)
Josef Bacik0f9dd462008-09-23 13:14:11 -0400938{
Li Zefan34d52cb2011-03-29 13:46:06 +0800939 struct rb_node *n = ctl->free_space_offset.rb_node;
Josef Bacik96303082009-07-13 21:29:25 -0400940 struct btrfs_free_space *entry, *prev = NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400941
Josef Bacik96303082009-07-13 21:29:25 -0400942 /* find entry that is closest to the 'offset' */
943 while (1) {
944 if (!n) {
945 entry = NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400946 break;
947 }
Josef Bacik96303082009-07-13 21:29:25 -0400948
949 entry = rb_entry(n, struct btrfs_free_space, offset_index);
950 prev = entry;
951
952 if (offset < entry->offset)
953 n = n->rb_left;
954 else if (offset > entry->offset)
955 n = n->rb_right;
956 else
957 break;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400958 }
959
Josef Bacik96303082009-07-13 21:29:25 -0400960 if (bitmap_only) {
961 if (!entry)
962 return NULL;
963 if (entry->bitmap)
964 return entry;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400965
Josef Bacik96303082009-07-13 21:29:25 -0400966 /*
967 * bitmap entry and extent entry may share same offset,
968 * in that case, bitmap entry comes after extent entry.
969 */
970 n = rb_next(n);
971 if (!n)
972 return NULL;
973 entry = rb_entry(n, struct btrfs_free_space, offset_index);
974 if (entry->offset != offset)
975 return NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400976
Josef Bacik96303082009-07-13 21:29:25 -0400977 WARN_ON(!entry->bitmap);
978 return entry;
979 } else if (entry) {
980 if (entry->bitmap) {
Josef Bacik0f9dd462008-09-23 13:14:11 -0400981 /*
Josef Bacik96303082009-07-13 21:29:25 -0400982 * if previous extent entry covers the offset,
983 * we should return it instead of the bitmap entry
Josef Bacik0f9dd462008-09-23 13:14:11 -0400984 */
Josef Bacik96303082009-07-13 21:29:25 -0400985 n = &entry->offset_index;
986 while (1) {
987 n = rb_prev(n);
988 if (!n)
989 break;
990 prev = rb_entry(n, struct btrfs_free_space,
991 offset_index);
992 if (!prev->bitmap) {
993 if (prev->offset + prev->bytes > offset)
994 entry = prev;
995 break;
996 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400997 }
Josef Bacik96303082009-07-13 21:29:25 -0400998 }
999 return entry;
1000 }
1001
1002 if (!prev)
1003 return NULL;
1004
1005 /* find last entry before the 'offset' */
1006 entry = prev;
1007 if (entry->offset > offset) {
1008 n = rb_prev(&entry->offset_index);
1009 if (n) {
1010 entry = rb_entry(n, struct btrfs_free_space,
1011 offset_index);
1012 BUG_ON(entry->offset > offset);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001013 } else {
Josef Bacik96303082009-07-13 21:29:25 -04001014 if (fuzzy)
1015 return entry;
1016 else
1017 return NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001018 }
1019 }
1020
Josef Bacik96303082009-07-13 21:29:25 -04001021 if (entry->bitmap) {
1022 n = &entry->offset_index;
1023 while (1) {
1024 n = rb_prev(n);
1025 if (!n)
1026 break;
1027 prev = rb_entry(n, struct btrfs_free_space,
1028 offset_index);
1029 if (!prev->bitmap) {
1030 if (prev->offset + prev->bytes > offset)
1031 return prev;
1032 break;
1033 }
1034 }
Li Zefan34d52cb2011-03-29 13:46:06 +08001035 if (entry->offset + BITS_PER_BITMAP * ctl->unit > offset)
Josef Bacik96303082009-07-13 21:29:25 -04001036 return entry;
1037 } else if (entry->offset + entry->bytes > offset)
1038 return entry;
1039
1040 if (!fuzzy)
1041 return NULL;
1042
1043 while (1) {
1044 if (entry->bitmap) {
1045 if (entry->offset + BITS_PER_BITMAP *
Li Zefan34d52cb2011-03-29 13:46:06 +08001046 ctl->unit > offset)
Josef Bacik96303082009-07-13 21:29:25 -04001047 break;
1048 } else {
1049 if (entry->offset + entry->bytes > offset)
1050 break;
1051 }
1052
1053 n = rb_next(&entry->offset_index);
1054 if (!n)
1055 return NULL;
1056 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1057 }
1058 return entry;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001059}
1060
Li Zefanf333adb2010-11-09 14:57:39 +08001061static inline void
Li Zefan34d52cb2011-03-29 13:46:06 +08001062__unlink_free_space(struct btrfs_free_space_ctl *ctl,
Li Zefanf333adb2010-11-09 14:57:39 +08001063 struct btrfs_free_space *info)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001064{
Li Zefan34d52cb2011-03-29 13:46:06 +08001065 rb_erase(&info->offset_index, &ctl->free_space_offset);
1066 ctl->free_extents--;
Li Zefanf333adb2010-11-09 14:57:39 +08001067}
1068
Li Zefan34d52cb2011-03-29 13:46:06 +08001069static void unlink_free_space(struct btrfs_free_space_ctl *ctl,
Li Zefanf333adb2010-11-09 14:57:39 +08001070 struct btrfs_free_space *info)
1071{
Li Zefan34d52cb2011-03-29 13:46:06 +08001072 __unlink_free_space(ctl, info);
1073 ctl->free_space -= info->bytes;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001074}
1075
Li Zefan34d52cb2011-03-29 13:46:06 +08001076static int link_free_space(struct btrfs_free_space_ctl *ctl,
Josef Bacik0f9dd462008-09-23 13:14:11 -04001077 struct btrfs_free_space *info)
1078{
1079 int ret = 0;
1080
Josef Bacik96303082009-07-13 21:29:25 -04001081 BUG_ON(!info->bitmap && !info->bytes);
Li Zefan34d52cb2011-03-29 13:46:06 +08001082 ret = tree_insert_offset(&ctl->free_space_offset, info->offset,
Josef Bacik96303082009-07-13 21:29:25 -04001083 &info->offset_index, (info->bitmap != NULL));
Josef Bacik0f9dd462008-09-23 13:14:11 -04001084 if (ret)
1085 return ret;
1086
Li Zefan34d52cb2011-03-29 13:46:06 +08001087 ctl->free_space += info->bytes;
1088 ctl->free_extents++;
Josef Bacik96303082009-07-13 21:29:25 -04001089 return ret;
1090}
1091
Li Zefan34d52cb2011-03-29 13:46:06 +08001092static void recalculate_thresholds(struct btrfs_free_space_ctl *ctl)
Josef Bacik96303082009-07-13 21:29:25 -04001093{
Li Zefan34d52cb2011-03-29 13:46:06 +08001094 struct btrfs_block_group_cache *block_group = ctl->private;
Josef Bacik25891f72009-09-11 16:11:20 -04001095 u64 max_bytes;
1096 u64 bitmap_bytes;
1097 u64 extent_bytes;
Li Zefan8eb2d822010-11-09 14:48:01 +08001098 u64 size = block_group->key.offset;
Li Zefan34d52cb2011-03-29 13:46:06 +08001099 u64 bytes_per_bg = BITS_PER_BITMAP * block_group->sectorsize;
1100 int max_bitmaps = div64_u64(size + bytes_per_bg - 1, bytes_per_bg);
1101
1102 BUG_ON(ctl->total_bitmaps > max_bitmaps);
Josef Bacik96303082009-07-13 21:29:25 -04001103
1104 /*
1105 * The goal is to keep the total amount of memory used per 1gb of space
1106 * at or below 32k, so we need to adjust how much memory we allow to be
1107 * used by extent based free space tracking
1108 */
Li Zefan8eb2d822010-11-09 14:48:01 +08001109 if (size < 1024 * 1024 * 1024)
1110 max_bytes = MAX_CACHE_BYTES_PER_GIG;
1111 else
1112 max_bytes = MAX_CACHE_BYTES_PER_GIG *
1113 div64_u64(size, 1024 * 1024 * 1024);
Josef Bacik96303082009-07-13 21:29:25 -04001114
Josef Bacik25891f72009-09-11 16:11:20 -04001115 /*
1116 * we want to account for 1 more bitmap than what we have so we can make
1117 * sure we don't go over our overall goal of MAX_CACHE_BYTES_PER_GIG as
1118 * we add more bitmaps.
1119 */
Li Zefan34d52cb2011-03-29 13:46:06 +08001120 bitmap_bytes = (ctl->total_bitmaps + 1) * PAGE_CACHE_SIZE;
Josef Bacik96303082009-07-13 21:29:25 -04001121
Josef Bacik25891f72009-09-11 16:11:20 -04001122 if (bitmap_bytes >= max_bytes) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001123 ctl->extents_thresh = 0;
Josef Bacik25891f72009-09-11 16:11:20 -04001124 return;
Josef Bacik96303082009-07-13 21:29:25 -04001125 }
Josef Bacik25891f72009-09-11 16:11:20 -04001126
1127 /*
1128 * we want the extent entry threshold to always be at most 1/2 the maxw
1129 * bytes we can have, or whatever is less than that.
1130 */
1131 extent_bytes = max_bytes - bitmap_bytes;
1132 extent_bytes = min_t(u64, extent_bytes, div64_u64(max_bytes, 2));
1133
Li Zefan34d52cb2011-03-29 13:46:06 +08001134 ctl->extents_thresh =
Josef Bacik25891f72009-09-11 16:11:20 -04001135 div64_u64(extent_bytes, (sizeof(struct btrfs_free_space)));
Josef Bacik96303082009-07-13 21:29:25 -04001136}
1137
Li Zefan34d52cb2011-03-29 13:46:06 +08001138static void bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
Josef Bacik817d52f2009-07-13 21:29:25 -04001139 struct btrfs_free_space *info, u64 offset,
1140 u64 bytes)
Josef Bacik96303082009-07-13 21:29:25 -04001141{
Li Zefanf38b6e72011-03-14 13:40:51 +08001142 unsigned long start, count;
Josef Bacik96303082009-07-13 21:29:25 -04001143
Li Zefan34d52cb2011-03-29 13:46:06 +08001144 start = offset_to_bit(info->offset, ctl->unit, offset);
1145 count = bytes_to_bits(bytes, ctl->unit);
Li Zefanf38b6e72011-03-14 13:40:51 +08001146 BUG_ON(start + count > BITS_PER_BITMAP);
Josef Bacik96303082009-07-13 21:29:25 -04001147
Li Zefanf38b6e72011-03-14 13:40:51 +08001148 bitmap_clear(info->bitmap, start, count);
Josef Bacik96303082009-07-13 21:29:25 -04001149
1150 info->bytes -= bytes;
Li Zefan34d52cb2011-03-29 13:46:06 +08001151 ctl->free_space -= bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001152}
1153
Li Zefan34d52cb2011-03-29 13:46:06 +08001154static void bitmap_set_bits(struct btrfs_free_space_ctl *ctl,
Josef Bacik817d52f2009-07-13 21:29:25 -04001155 struct btrfs_free_space *info, u64 offset,
1156 u64 bytes)
Josef Bacik96303082009-07-13 21:29:25 -04001157{
Li Zefanf38b6e72011-03-14 13:40:51 +08001158 unsigned long start, count;
Josef Bacik96303082009-07-13 21:29:25 -04001159
Li Zefan34d52cb2011-03-29 13:46:06 +08001160 start = offset_to_bit(info->offset, ctl->unit, offset);
1161 count = bytes_to_bits(bytes, ctl->unit);
Li Zefanf38b6e72011-03-14 13:40:51 +08001162 BUG_ON(start + count > BITS_PER_BITMAP);
Josef Bacik96303082009-07-13 21:29:25 -04001163
Li Zefanf38b6e72011-03-14 13:40:51 +08001164 bitmap_set(info->bitmap, start, count);
Josef Bacik96303082009-07-13 21:29:25 -04001165
1166 info->bytes += bytes;
Li Zefan34d52cb2011-03-29 13:46:06 +08001167 ctl->free_space += bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001168}
1169
Li Zefan34d52cb2011-03-29 13:46:06 +08001170static int search_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001171 struct btrfs_free_space *bitmap_info, u64 *offset,
1172 u64 *bytes)
1173{
1174 unsigned long found_bits = 0;
1175 unsigned long bits, i;
1176 unsigned long next_zero;
1177
Li Zefan34d52cb2011-03-29 13:46:06 +08001178 i = offset_to_bit(bitmap_info->offset, ctl->unit,
Josef Bacik96303082009-07-13 21:29:25 -04001179 max_t(u64, *offset, bitmap_info->offset));
Li Zefan34d52cb2011-03-29 13:46:06 +08001180 bits = bytes_to_bits(*bytes, ctl->unit);
Josef Bacik96303082009-07-13 21:29:25 -04001181
1182 for (i = find_next_bit(bitmap_info->bitmap, BITS_PER_BITMAP, i);
1183 i < BITS_PER_BITMAP;
1184 i = find_next_bit(bitmap_info->bitmap, BITS_PER_BITMAP, i + 1)) {
1185 next_zero = find_next_zero_bit(bitmap_info->bitmap,
1186 BITS_PER_BITMAP, i);
1187 if ((next_zero - i) >= bits) {
1188 found_bits = next_zero - i;
1189 break;
1190 }
1191 i = next_zero;
1192 }
1193
1194 if (found_bits) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001195 *offset = (u64)(i * ctl->unit) + bitmap_info->offset;
1196 *bytes = (u64)(found_bits) * ctl->unit;
Josef Bacik96303082009-07-13 21:29:25 -04001197 return 0;
1198 }
1199
1200 return -1;
1201}
1202
Li Zefan34d52cb2011-03-29 13:46:06 +08001203static struct btrfs_free_space *
1204find_free_space(struct btrfs_free_space_ctl *ctl, u64 *offset, u64 *bytes)
Josef Bacik96303082009-07-13 21:29:25 -04001205{
1206 struct btrfs_free_space *entry;
1207 struct rb_node *node;
1208 int ret;
1209
Li Zefan34d52cb2011-03-29 13:46:06 +08001210 if (!ctl->free_space_offset.rb_node)
Josef Bacik96303082009-07-13 21:29:25 -04001211 return NULL;
1212
Li Zefan34d52cb2011-03-29 13:46:06 +08001213 entry = tree_search_offset(ctl, offset_to_bitmap(ctl, *offset), 0, 1);
Josef Bacik96303082009-07-13 21:29:25 -04001214 if (!entry)
1215 return NULL;
1216
1217 for (node = &entry->offset_index; node; node = rb_next(node)) {
1218 entry = rb_entry(node, struct btrfs_free_space, offset_index);
1219 if (entry->bytes < *bytes)
1220 continue;
1221
1222 if (entry->bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001223 ret = search_bitmap(ctl, entry, offset, bytes);
Josef Bacik96303082009-07-13 21:29:25 -04001224 if (!ret)
1225 return entry;
1226 continue;
1227 }
1228
1229 *offset = entry->offset;
1230 *bytes = entry->bytes;
1231 return entry;
1232 }
1233
1234 return NULL;
1235}
1236
Li Zefan34d52cb2011-03-29 13:46:06 +08001237static void add_new_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001238 struct btrfs_free_space *info, u64 offset)
1239{
Li Zefan34d52cb2011-03-29 13:46:06 +08001240 info->offset = offset_to_bitmap(ctl, offset);
Josef Bacikf019f422009-09-11 16:11:20 -04001241 info->bytes = 0;
Li Zefan34d52cb2011-03-29 13:46:06 +08001242 link_free_space(ctl, info);
1243 ctl->total_bitmaps++;
Josef Bacik96303082009-07-13 21:29:25 -04001244
Li Zefan34d52cb2011-03-29 13:46:06 +08001245 ctl->op->recalc_thresholds(ctl);
Josef Bacik96303082009-07-13 21:29:25 -04001246}
1247
Li Zefan34d52cb2011-03-29 13:46:06 +08001248static void free_bitmap(struct btrfs_free_space_ctl *ctl,
Li Zefanedf6e2d2010-11-09 14:50:07 +08001249 struct btrfs_free_space *bitmap_info)
1250{
Li Zefan34d52cb2011-03-29 13:46:06 +08001251 unlink_free_space(ctl, bitmap_info);
Li Zefanedf6e2d2010-11-09 14:50:07 +08001252 kfree(bitmap_info->bitmap);
Josef Bacikdc89e982011-01-28 17:05:48 -05001253 kmem_cache_free(btrfs_free_space_cachep, bitmap_info);
Li Zefan34d52cb2011-03-29 13:46:06 +08001254 ctl->total_bitmaps--;
1255 ctl->op->recalc_thresholds(ctl);
Li Zefanedf6e2d2010-11-09 14:50:07 +08001256}
1257
Li Zefan34d52cb2011-03-29 13:46:06 +08001258static noinline int remove_from_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001259 struct btrfs_free_space *bitmap_info,
1260 u64 *offset, u64 *bytes)
1261{
1262 u64 end;
Josef Bacik6606bb92009-07-31 11:03:58 -04001263 u64 search_start, search_bytes;
1264 int ret;
Josef Bacik96303082009-07-13 21:29:25 -04001265
1266again:
Li Zefan34d52cb2011-03-29 13:46:06 +08001267 end = bitmap_info->offset + (u64)(BITS_PER_BITMAP * ctl->unit) - 1;
Josef Bacik96303082009-07-13 21:29:25 -04001268
Josef Bacik6606bb92009-07-31 11:03:58 -04001269 /*
1270 * XXX - this can go away after a few releases.
1271 *
1272 * since the only user of btrfs_remove_free_space is the tree logging
1273 * stuff, and the only way to test that is under crash conditions, we
1274 * want to have this debug stuff here just in case somethings not
1275 * working. Search the bitmap for the space we are trying to use to
1276 * make sure its actually there. If its not there then we need to stop
1277 * because something has gone wrong.
1278 */
1279 search_start = *offset;
1280 search_bytes = *bytes;
Josef Bacik13dbc082011-02-03 02:39:52 +00001281 search_bytes = min(search_bytes, end - search_start + 1);
Li Zefan34d52cb2011-03-29 13:46:06 +08001282 ret = search_bitmap(ctl, bitmap_info, &search_start, &search_bytes);
Josef Bacik6606bb92009-07-31 11:03:58 -04001283 BUG_ON(ret < 0 || search_start != *offset);
1284
Josef Bacik96303082009-07-13 21:29:25 -04001285 if (*offset > bitmap_info->offset && *offset + *bytes > end) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001286 bitmap_clear_bits(ctl, bitmap_info, *offset, end - *offset + 1);
Josef Bacik96303082009-07-13 21:29:25 -04001287 *bytes -= end - *offset + 1;
1288 *offset = end + 1;
1289 } else if (*offset >= bitmap_info->offset && *offset + *bytes <= end) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001290 bitmap_clear_bits(ctl, bitmap_info, *offset, *bytes);
Josef Bacik96303082009-07-13 21:29:25 -04001291 *bytes = 0;
1292 }
1293
1294 if (*bytes) {
Josef Bacik6606bb92009-07-31 11:03:58 -04001295 struct rb_node *next = rb_next(&bitmap_info->offset_index);
Li Zefanedf6e2d2010-11-09 14:50:07 +08001296 if (!bitmap_info->bytes)
Li Zefan34d52cb2011-03-29 13:46:06 +08001297 free_bitmap(ctl, bitmap_info);
Josef Bacik96303082009-07-13 21:29:25 -04001298
Josef Bacik6606bb92009-07-31 11:03:58 -04001299 /*
1300 * no entry after this bitmap, but we still have bytes to
1301 * remove, so something has gone wrong.
1302 */
1303 if (!next)
Josef Bacik96303082009-07-13 21:29:25 -04001304 return -EINVAL;
1305
Josef Bacik6606bb92009-07-31 11:03:58 -04001306 bitmap_info = rb_entry(next, struct btrfs_free_space,
1307 offset_index);
1308
1309 /*
1310 * if the next entry isn't a bitmap we need to return to let the
1311 * extent stuff do its work.
1312 */
Josef Bacik96303082009-07-13 21:29:25 -04001313 if (!bitmap_info->bitmap)
1314 return -EAGAIN;
1315
Josef Bacik6606bb92009-07-31 11:03:58 -04001316 /*
1317 * Ok the next item is a bitmap, but it may not actually hold
1318 * the information for the rest of this free space stuff, so
1319 * look for it, and if we don't find it return so we can try
1320 * everything over again.
1321 */
1322 search_start = *offset;
1323 search_bytes = *bytes;
Li Zefan34d52cb2011-03-29 13:46:06 +08001324 ret = search_bitmap(ctl, bitmap_info, &search_start,
Josef Bacik6606bb92009-07-31 11:03:58 -04001325 &search_bytes);
1326 if (ret < 0 || search_start != *offset)
1327 return -EAGAIN;
1328
Josef Bacik96303082009-07-13 21:29:25 -04001329 goto again;
Li Zefanedf6e2d2010-11-09 14:50:07 +08001330 } else if (!bitmap_info->bytes)
Li Zefan34d52cb2011-03-29 13:46:06 +08001331 free_bitmap(ctl, bitmap_info);
Josef Bacik96303082009-07-13 21:29:25 -04001332
1333 return 0;
1334}
1335
Li Zefan34d52cb2011-03-29 13:46:06 +08001336static bool use_bitmap(struct btrfs_free_space_ctl *ctl,
1337 struct btrfs_free_space *info)
Josef Bacik96303082009-07-13 21:29:25 -04001338{
Li Zefan34d52cb2011-03-29 13:46:06 +08001339 struct btrfs_block_group_cache *block_group = ctl->private;
Josef Bacik96303082009-07-13 21:29:25 -04001340
1341 /*
1342 * If we are below the extents threshold then we can add this as an
1343 * extent, and don't have to deal with the bitmap
1344 */
Li Zefan34d52cb2011-03-29 13:46:06 +08001345 if (ctl->free_extents < ctl->extents_thresh) {
Josef Bacik32cb0842011-03-18 16:16:21 -04001346 /*
1347 * If this block group has some small extents we don't want to
1348 * use up all of our free slots in the cache with them, we want
1349 * to reserve them to larger extents, however if we have plent
1350 * of cache left then go ahead an dadd them, no sense in adding
1351 * the overhead of a bitmap if we don't have to.
1352 */
1353 if (info->bytes <= block_group->sectorsize * 4) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001354 if (ctl->free_extents * 2 <= ctl->extents_thresh)
1355 return false;
Josef Bacik32cb0842011-03-18 16:16:21 -04001356 } else {
Li Zefan34d52cb2011-03-29 13:46:06 +08001357 return false;
Josef Bacik32cb0842011-03-18 16:16:21 -04001358 }
1359 }
Josef Bacik96303082009-07-13 21:29:25 -04001360
1361 /*
1362 * some block groups are so tiny they can't be enveloped by a bitmap, so
1363 * don't even bother to create a bitmap for this
1364 */
1365 if (BITS_PER_BITMAP * block_group->sectorsize >
1366 block_group->key.offset)
Li Zefan34d52cb2011-03-29 13:46:06 +08001367 return false;
1368
1369 return true;
1370}
1371
1372static int insert_into_bitmap(struct btrfs_free_space_ctl *ctl,
1373 struct btrfs_free_space *info)
1374{
1375 struct btrfs_free_space *bitmap_info;
1376 int added = 0;
1377 u64 bytes, offset, end;
1378 int ret;
Josef Bacik96303082009-07-13 21:29:25 -04001379
1380 bytes = info->bytes;
1381 offset = info->offset;
1382
Li Zefan34d52cb2011-03-29 13:46:06 +08001383 if (!ctl->op->use_bitmap(ctl, info))
1384 return 0;
1385
Josef Bacik96303082009-07-13 21:29:25 -04001386again:
Li Zefan34d52cb2011-03-29 13:46:06 +08001387 bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
Josef Bacik96303082009-07-13 21:29:25 -04001388 1, 0);
1389 if (!bitmap_info) {
1390 BUG_ON(added);
1391 goto new_bitmap;
1392 }
1393
Li Zefan34d52cb2011-03-29 13:46:06 +08001394 end = bitmap_info->offset + (u64)(BITS_PER_BITMAP * ctl->unit);
Josef Bacik96303082009-07-13 21:29:25 -04001395
1396 if (offset >= bitmap_info->offset && offset + bytes > end) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001397 bitmap_set_bits(ctl, bitmap_info, offset, end - offset);
Josef Bacik96303082009-07-13 21:29:25 -04001398 bytes -= end - offset;
1399 offset = end;
1400 added = 0;
1401 } else if (offset >= bitmap_info->offset && offset + bytes <= end) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001402 bitmap_set_bits(ctl, bitmap_info, offset, bytes);
Josef Bacik96303082009-07-13 21:29:25 -04001403 bytes = 0;
1404 } else {
1405 BUG();
1406 }
1407
1408 if (!bytes) {
1409 ret = 1;
1410 goto out;
1411 } else
1412 goto again;
1413
1414new_bitmap:
1415 if (info && info->bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001416 add_new_bitmap(ctl, info, offset);
Josef Bacik96303082009-07-13 21:29:25 -04001417 added = 1;
1418 info = NULL;
1419 goto again;
1420 } else {
Li Zefan34d52cb2011-03-29 13:46:06 +08001421 spin_unlock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04001422
1423 /* no pre-allocated info, allocate a new one */
1424 if (!info) {
Josef Bacikdc89e982011-01-28 17:05:48 -05001425 info = kmem_cache_zalloc(btrfs_free_space_cachep,
1426 GFP_NOFS);
Josef Bacik96303082009-07-13 21:29:25 -04001427 if (!info) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001428 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04001429 ret = -ENOMEM;
1430 goto out;
1431 }
1432 }
1433
1434 /* allocate the bitmap */
1435 info->bitmap = kzalloc(PAGE_CACHE_SIZE, GFP_NOFS);
Li Zefan34d52cb2011-03-29 13:46:06 +08001436 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04001437 if (!info->bitmap) {
1438 ret = -ENOMEM;
1439 goto out;
1440 }
1441 goto again;
1442 }
1443
1444out:
1445 if (info) {
1446 if (info->bitmap)
1447 kfree(info->bitmap);
Josef Bacikdc89e982011-01-28 17:05:48 -05001448 kmem_cache_free(btrfs_free_space_cachep, info);
Josef Bacik96303082009-07-13 21:29:25 -04001449 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04001450
1451 return ret;
1452}
1453
Li Zefan34d52cb2011-03-29 13:46:06 +08001454bool try_merge_free_space(struct btrfs_free_space_ctl *ctl,
Li Zefanf333adb2010-11-09 14:57:39 +08001455 struct btrfs_free_space *info, bool update_stat)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001456{
Li Zefan120d66e2010-11-09 14:56:50 +08001457 struct btrfs_free_space *left_info;
1458 struct btrfs_free_space *right_info;
1459 bool merged = false;
1460 u64 offset = info->offset;
1461 u64 bytes = info->bytes;
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001462
Josef Bacik0f9dd462008-09-23 13:14:11 -04001463 /*
1464 * first we want to see if there is free space adjacent to the range we
1465 * are adding, if there is remove that struct and add a new one to
1466 * cover the entire range
1467 */
Li Zefan34d52cb2011-03-29 13:46:06 +08001468 right_info = tree_search_offset(ctl, offset + bytes, 0, 0);
Josef Bacik96303082009-07-13 21:29:25 -04001469 if (right_info && rb_prev(&right_info->offset_index))
1470 left_info = rb_entry(rb_prev(&right_info->offset_index),
1471 struct btrfs_free_space, offset_index);
1472 else
Li Zefan34d52cb2011-03-29 13:46:06 +08001473 left_info = tree_search_offset(ctl, offset - 1, 0, 0);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001474
Josef Bacik96303082009-07-13 21:29:25 -04001475 if (right_info && !right_info->bitmap) {
Li Zefanf333adb2010-11-09 14:57:39 +08001476 if (update_stat)
Li Zefan34d52cb2011-03-29 13:46:06 +08001477 unlink_free_space(ctl, right_info);
Li Zefanf333adb2010-11-09 14:57:39 +08001478 else
Li Zefan34d52cb2011-03-29 13:46:06 +08001479 __unlink_free_space(ctl, right_info);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001480 info->bytes += right_info->bytes;
Josef Bacikdc89e982011-01-28 17:05:48 -05001481 kmem_cache_free(btrfs_free_space_cachep, right_info);
Li Zefan120d66e2010-11-09 14:56:50 +08001482 merged = true;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001483 }
1484
Josef Bacik96303082009-07-13 21:29:25 -04001485 if (left_info && !left_info->bitmap &&
1486 left_info->offset + left_info->bytes == offset) {
Li Zefanf333adb2010-11-09 14:57:39 +08001487 if (update_stat)
Li Zefan34d52cb2011-03-29 13:46:06 +08001488 unlink_free_space(ctl, left_info);
Li Zefanf333adb2010-11-09 14:57:39 +08001489 else
Li Zefan34d52cb2011-03-29 13:46:06 +08001490 __unlink_free_space(ctl, left_info);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001491 info->offset = left_info->offset;
1492 info->bytes += left_info->bytes;
Josef Bacikdc89e982011-01-28 17:05:48 -05001493 kmem_cache_free(btrfs_free_space_cachep, left_info);
Li Zefan120d66e2010-11-09 14:56:50 +08001494 merged = true;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001495 }
1496
Li Zefan120d66e2010-11-09 14:56:50 +08001497 return merged;
1498}
1499
Li Zefan581bb052011-04-20 10:06:11 +08001500int __btrfs_add_free_space(struct btrfs_free_space_ctl *ctl,
1501 u64 offset, u64 bytes)
Li Zefan120d66e2010-11-09 14:56:50 +08001502{
1503 struct btrfs_free_space *info;
1504 int ret = 0;
1505
Josef Bacikdc89e982011-01-28 17:05:48 -05001506 info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS);
Li Zefan120d66e2010-11-09 14:56:50 +08001507 if (!info)
1508 return -ENOMEM;
1509
1510 info->offset = offset;
1511 info->bytes = bytes;
1512
Li Zefan34d52cb2011-03-29 13:46:06 +08001513 spin_lock(&ctl->tree_lock);
Li Zefan120d66e2010-11-09 14:56:50 +08001514
Li Zefan34d52cb2011-03-29 13:46:06 +08001515 if (try_merge_free_space(ctl, info, true))
Li Zefan120d66e2010-11-09 14:56:50 +08001516 goto link;
1517
1518 /*
1519 * There was no extent directly to the left or right of this new
1520 * extent then we know we're going to have to allocate a new extent, so
1521 * before we do that see if we need to drop this into a bitmap
1522 */
Li Zefan34d52cb2011-03-29 13:46:06 +08001523 ret = insert_into_bitmap(ctl, info);
Li Zefan120d66e2010-11-09 14:56:50 +08001524 if (ret < 0) {
1525 goto out;
1526 } else if (ret) {
1527 ret = 0;
1528 goto out;
1529 }
1530link:
Li Zefan34d52cb2011-03-29 13:46:06 +08001531 ret = link_free_space(ctl, info);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001532 if (ret)
Josef Bacikdc89e982011-01-28 17:05:48 -05001533 kmem_cache_free(btrfs_free_space_cachep, info);
Josef Bacik96303082009-07-13 21:29:25 -04001534out:
Li Zefan34d52cb2011-03-29 13:46:06 +08001535 spin_unlock(&ctl->tree_lock);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001536
Josef Bacik0f9dd462008-09-23 13:14:11 -04001537 if (ret) {
Josef Bacik96303082009-07-13 21:29:25 -04001538 printk(KERN_CRIT "btrfs: unable to add free space :%d\n", ret);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04001539 BUG_ON(ret == -EEXIST);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001540 }
1541
Josef Bacik0f9dd462008-09-23 13:14:11 -04001542 return ret;
1543}
1544
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001545int btrfs_remove_free_space(struct btrfs_block_group_cache *block_group,
1546 u64 offset, u64 bytes)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001547{
Li Zefan34d52cb2011-03-29 13:46:06 +08001548 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001549 struct btrfs_free_space *info;
Josef Bacik96303082009-07-13 21:29:25 -04001550 struct btrfs_free_space *next_info = NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001551 int ret = 0;
1552
Li Zefan34d52cb2011-03-29 13:46:06 +08001553 spin_lock(&ctl->tree_lock);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001554
Josef Bacik96303082009-07-13 21:29:25 -04001555again:
Li Zefan34d52cb2011-03-29 13:46:06 +08001556 info = tree_search_offset(ctl, offset, 0, 0);
Josef Bacik96303082009-07-13 21:29:25 -04001557 if (!info) {
Josef Bacik6606bb92009-07-31 11:03:58 -04001558 /*
1559 * oops didn't find an extent that matched the space we wanted
1560 * to remove, look for a bitmap instead
1561 */
Li Zefan34d52cb2011-03-29 13:46:06 +08001562 info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
Josef Bacik6606bb92009-07-31 11:03:58 -04001563 1, 0);
1564 if (!info) {
1565 WARN_ON(1);
1566 goto out_lock;
1567 }
Josef Bacik96303082009-07-13 21:29:25 -04001568 }
1569
1570 if (info->bytes < bytes && rb_next(&info->offset_index)) {
1571 u64 end;
1572 next_info = rb_entry(rb_next(&info->offset_index),
1573 struct btrfs_free_space,
1574 offset_index);
1575
1576 if (next_info->bitmap)
Li Zefan34d52cb2011-03-29 13:46:06 +08001577 end = next_info->offset +
1578 BITS_PER_BITMAP * ctl->unit - 1;
Josef Bacik96303082009-07-13 21:29:25 -04001579 else
1580 end = next_info->offset + next_info->bytes;
1581
1582 if (next_info->bytes < bytes ||
1583 next_info->offset > offset || offset > end) {
1584 printk(KERN_CRIT "Found free space at %llu, size %llu,"
1585 " trying to use %llu\n",
1586 (unsigned long long)info->offset,
1587 (unsigned long long)info->bytes,
1588 (unsigned long long)bytes);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001589 WARN_ON(1);
1590 ret = -EINVAL;
Josef Bacik96303082009-07-13 21:29:25 -04001591 goto out_lock;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001592 }
Josef Bacik96303082009-07-13 21:29:25 -04001593
1594 info = next_info;
1595 }
1596
1597 if (info->bytes == bytes) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001598 unlink_free_space(ctl, info);
Josef Bacik96303082009-07-13 21:29:25 -04001599 if (info->bitmap) {
1600 kfree(info->bitmap);
Li Zefan34d52cb2011-03-29 13:46:06 +08001601 ctl->total_bitmaps--;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001602 }
Josef Bacikdc89e982011-01-28 17:05:48 -05001603 kmem_cache_free(btrfs_free_space_cachep, info);
Josef Bacik96303082009-07-13 21:29:25 -04001604 goto out_lock;
1605 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04001606
Josef Bacik96303082009-07-13 21:29:25 -04001607 if (!info->bitmap && info->offset == offset) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001608 unlink_free_space(ctl, info);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001609 info->offset += bytes;
1610 info->bytes -= bytes;
Li Zefan34d52cb2011-03-29 13:46:06 +08001611 link_free_space(ctl, info);
Josef Bacik96303082009-07-13 21:29:25 -04001612 goto out_lock;
1613 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04001614
Josef Bacik96303082009-07-13 21:29:25 -04001615 if (!info->bitmap && info->offset <= offset &&
1616 info->offset + info->bytes >= offset + bytes) {
Chris Mason9b49c9b2008-09-24 11:23:25 -04001617 u64 old_start = info->offset;
1618 /*
1619 * we're freeing space in the middle of the info,
1620 * this can happen during tree log replay
1621 *
1622 * first unlink the old info and then
1623 * insert it again after the hole we're creating
1624 */
Li Zefan34d52cb2011-03-29 13:46:06 +08001625 unlink_free_space(ctl, info);
Chris Mason9b49c9b2008-09-24 11:23:25 -04001626 if (offset + bytes < info->offset + info->bytes) {
1627 u64 old_end = info->offset + info->bytes;
1628
1629 info->offset = offset + bytes;
1630 info->bytes = old_end - info->offset;
Li Zefan34d52cb2011-03-29 13:46:06 +08001631 ret = link_free_space(ctl, info);
Josef Bacik96303082009-07-13 21:29:25 -04001632 WARN_ON(ret);
1633 if (ret)
1634 goto out_lock;
Chris Mason9b49c9b2008-09-24 11:23:25 -04001635 } else {
1636 /* the hole we're creating ends at the end
1637 * of the info struct, just free the info
1638 */
Josef Bacikdc89e982011-01-28 17:05:48 -05001639 kmem_cache_free(btrfs_free_space_cachep, info);
Chris Mason9b49c9b2008-09-24 11:23:25 -04001640 }
Li Zefan34d52cb2011-03-29 13:46:06 +08001641 spin_unlock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04001642
1643 /* step two, insert a new info struct to cover
1644 * anything before the hole
Chris Mason9b49c9b2008-09-24 11:23:25 -04001645 */
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001646 ret = btrfs_add_free_space(block_group, old_start,
1647 offset - old_start);
Josef Bacik96303082009-07-13 21:29:25 -04001648 WARN_ON(ret);
1649 goto out;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001650 }
Josef Bacik96303082009-07-13 21:29:25 -04001651
Li Zefan34d52cb2011-03-29 13:46:06 +08001652 ret = remove_from_bitmap(ctl, info, &offset, &bytes);
Josef Bacik96303082009-07-13 21:29:25 -04001653 if (ret == -EAGAIN)
1654 goto again;
1655 BUG_ON(ret);
1656out_lock:
Li Zefan34d52cb2011-03-29 13:46:06 +08001657 spin_unlock(&ctl->tree_lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001658out:
Josef Bacik25179202008-10-29 14:49:05 -04001659 return ret;
1660}
1661
Josef Bacik0f9dd462008-09-23 13:14:11 -04001662void btrfs_dump_free_space(struct btrfs_block_group_cache *block_group,
1663 u64 bytes)
1664{
Li Zefan34d52cb2011-03-29 13:46:06 +08001665 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001666 struct btrfs_free_space *info;
1667 struct rb_node *n;
1668 int count = 0;
1669
Li Zefan34d52cb2011-03-29 13:46:06 +08001670 for (n = rb_first(&ctl->free_space_offset); n; n = rb_next(n)) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001671 info = rb_entry(n, struct btrfs_free_space, offset_index);
1672 if (info->bytes >= bytes)
1673 count++;
Josef Bacik96303082009-07-13 21:29:25 -04001674 printk(KERN_CRIT "entry offset %llu, bytes %llu, bitmap %s\n",
Joel Becker21380932009-04-21 12:38:29 -07001675 (unsigned long long)info->offset,
Josef Bacik96303082009-07-13 21:29:25 -04001676 (unsigned long long)info->bytes,
1677 (info->bitmap) ? "yes" : "no");
Josef Bacik0f9dd462008-09-23 13:14:11 -04001678 }
Josef Bacik96303082009-07-13 21:29:25 -04001679 printk(KERN_INFO "block group has cluster?: %s\n",
1680 list_empty(&block_group->cluster_list) ? "no" : "yes");
Josef Bacik0f9dd462008-09-23 13:14:11 -04001681 printk(KERN_INFO "%d blocks of free space at or bigger than bytes is"
1682 "\n", count);
1683}
1684
Li Zefan34d52cb2011-03-29 13:46:06 +08001685static struct btrfs_free_space_op free_space_op = {
1686 .recalc_thresholds = recalculate_thresholds,
1687 .use_bitmap = use_bitmap,
1688};
1689
1690void btrfs_init_free_space_ctl(struct btrfs_block_group_cache *block_group)
1691{
1692 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
1693
1694 spin_lock_init(&ctl->tree_lock);
1695 ctl->unit = block_group->sectorsize;
1696 ctl->start = block_group->key.objectid;
1697 ctl->private = block_group;
1698 ctl->op = &free_space_op;
1699
1700 /*
1701 * we only want to have 32k of ram per block group for keeping
1702 * track of free space, and if we pass 1/2 of that we want to
1703 * start converting things over to using bitmaps
1704 */
1705 ctl->extents_thresh = ((1024 * 32) / 2) /
1706 sizeof(struct btrfs_free_space);
1707}
1708
Chris Masonfa9c0d792009-04-03 09:47:43 -04001709/*
1710 * for a given cluster, put all of its extents back into the free
1711 * space cache. If the block group passed doesn't match the block group
1712 * pointed to by the cluster, someone else raced in and freed the
1713 * cluster already. In that case, we just return without changing anything
1714 */
1715static int
1716__btrfs_return_cluster_to_free_space(
1717 struct btrfs_block_group_cache *block_group,
1718 struct btrfs_free_cluster *cluster)
1719{
Li Zefan34d52cb2011-03-29 13:46:06 +08001720 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04001721 struct btrfs_free_space *entry;
1722 struct rb_node *node;
1723
1724 spin_lock(&cluster->lock);
1725 if (cluster->block_group != block_group)
1726 goto out;
1727
Josef Bacik96303082009-07-13 21:29:25 -04001728 cluster->block_group = NULL;
Chris Masonfa9c0d792009-04-03 09:47:43 -04001729 cluster->window_start = 0;
Josef Bacik96303082009-07-13 21:29:25 -04001730 list_del_init(&cluster->block_group_list);
Josef Bacik96303082009-07-13 21:29:25 -04001731
Chris Masonfa9c0d792009-04-03 09:47:43 -04001732 node = rb_first(&cluster->root);
Josef Bacik96303082009-07-13 21:29:25 -04001733 while (node) {
Josef Bacik4e69b592011-03-21 10:11:24 -04001734 bool bitmap;
1735
Chris Masonfa9c0d792009-04-03 09:47:43 -04001736 entry = rb_entry(node, struct btrfs_free_space, offset_index);
1737 node = rb_next(&entry->offset_index);
1738 rb_erase(&entry->offset_index, &cluster->root);
Josef Bacik4e69b592011-03-21 10:11:24 -04001739
1740 bitmap = (entry->bitmap != NULL);
1741 if (!bitmap)
Li Zefan34d52cb2011-03-29 13:46:06 +08001742 try_merge_free_space(ctl, entry, false);
1743 tree_insert_offset(&ctl->free_space_offset,
Josef Bacik4e69b592011-03-21 10:11:24 -04001744 entry->offset, &entry->offset_index, bitmap);
Chris Masonfa9c0d792009-04-03 09:47:43 -04001745 }
Eric Paris6bef4d32010-02-23 19:43:04 +00001746 cluster->root = RB_ROOT;
Josef Bacik96303082009-07-13 21:29:25 -04001747
Chris Masonfa9c0d792009-04-03 09:47:43 -04001748out:
1749 spin_unlock(&cluster->lock);
Josef Bacik96303082009-07-13 21:29:25 -04001750 btrfs_put_block_group(block_group);
Chris Masonfa9c0d792009-04-03 09:47:43 -04001751 return 0;
1752}
1753
Li Zefan581bb052011-04-20 10:06:11 +08001754void __btrfs_remove_free_space_cache(struct btrfs_free_space_ctl *ctl)
1755{
1756 struct btrfs_free_space *info;
1757 struct rb_node *node;
1758
1759 spin_lock(&ctl->tree_lock);
1760 while ((node = rb_last(&ctl->free_space_offset)) != NULL) {
1761 info = rb_entry(node, struct btrfs_free_space, offset_index);
1762 unlink_free_space(ctl, info);
1763 kfree(info->bitmap);
1764 kmem_cache_free(btrfs_free_space_cachep, info);
1765 if (need_resched()) {
1766 spin_unlock(&ctl->tree_lock);
1767 cond_resched();
1768 spin_lock(&ctl->tree_lock);
1769 }
1770 }
1771 spin_unlock(&ctl->tree_lock);
1772}
1773
Josef Bacik0f9dd462008-09-23 13:14:11 -04001774void btrfs_remove_free_space_cache(struct btrfs_block_group_cache *block_group)
1775{
Li Zefan34d52cb2011-03-29 13:46:06 +08001776 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04001777 struct btrfs_free_cluster *cluster;
Josef Bacik96303082009-07-13 21:29:25 -04001778 struct list_head *head;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001779
Li Zefan34d52cb2011-03-29 13:46:06 +08001780 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04001781 while ((head = block_group->cluster_list.next) !=
1782 &block_group->cluster_list) {
1783 cluster = list_entry(head, struct btrfs_free_cluster,
1784 block_group_list);
Chris Masonfa9c0d792009-04-03 09:47:43 -04001785
1786 WARN_ON(cluster->block_group != block_group);
1787 __btrfs_return_cluster_to_free_space(block_group, cluster);
Josef Bacik96303082009-07-13 21:29:25 -04001788 if (need_resched()) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001789 spin_unlock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04001790 cond_resched();
Li Zefan34d52cb2011-03-29 13:46:06 +08001791 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04001792 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04001793 }
Li Zefan34d52cb2011-03-29 13:46:06 +08001794 spin_unlock(&ctl->tree_lock);
Li Zefan581bb052011-04-20 10:06:11 +08001795
1796 __btrfs_remove_free_space_cache(ctl);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001797}
1798
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001799u64 btrfs_find_space_for_alloc(struct btrfs_block_group_cache *block_group,
1800 u64 offset, u64 bytes, u64 empty_size)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001801{
Li Zefan34d52cb2011-03-29 13:46:06 +08001802 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001803 struct btrfs_free_space *entry = NULL;
Josef Bacik96303082009-07-13 21:29:25 -04001804 u64 bytes_search = bytes + empty_size;
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001805 u64 ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001806
Li Zefan34d52cb2011-03-29 13:46:06 +08001807 spin_lock(&ctl->tree_lock);
1808 entry = find_free_space(ctl, &offset, &bytes_search);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001809 if (!entry)
Josef Bacik96303082009-07-13 21:29:25 -04001810 goto out;
1811
1812 ret = offset;
1813 if (entry->bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001814 bitmap_clear_bits(ctl, entry, offset, bytes);
Li Zefanedf6e2d2010-11-09 14:50:07 +08001815 if (!entry->bytes)
Li Zefan34d52cb2011-03-29 13:46:06 +08001816 free_bitmap(ctl, entry);
Josef Bacik96303082009-07-13 21:29:25 -04001817 } else {
Li Zefan34d52cb2011-03-29 13:46:06 +08001818 unlink_free_space(ctl, entry);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001819 entry->offset += bytes;
1820 entry->bytes -= bytes;
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001821 if (!entry->bytes)
Josef Bacikdc89e982011-01-28 17:05:48 -05001822 kmem_cache_free(btrfs_free_space_cachep, entry);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001823 else
Li Zefan34d52cb2011-03-29 13:46:06 +08001824 link_free_space(ctl, entry);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001825 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04001826
Josef Bacik96303082009-07-13 21:29:25 -04001827out:
Li Zefan34d52cb2011-03-29 13:46:06 +08001828 spin_unlock(&ctl->tree_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04001829
Josef Bacik0f9dd462008-09-23 13:14:11 -04001830 return ret;
1831}
Chris Masonfa9c0d792009-04-03 09:47:43 -04001832
1833/*
1834 * given a cluster, put all of its extents back into the free space
1835 * cache. If a block group is passed, this function will only free
1836 * a cluster that belongs to the passed block group.
1837 *
1838 * Otherwise, it'll get a reference on the block group pointed to by the
1839 * cluster and remove the cluster from it.
1840 */
1841int btrfs_return_cluster_to_free_space(
1842 struct btrfs_block_group_cache *block_group,
1843 struct btrfs_free_cluster *cluster)
1844{
Li Zefan34d52cb2011-03-29 13:46:06 +08001845 struct btrfs_free_space_ctl *ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04001846 int ret;
1847
1848 /* first, get a safe pointer to the block group */
1849 spin_lock(&cluster->lock);
1850 if (!block_group) {
1851 block_group = cluster->block_group;
1852 if (!block_group) {
1853 spin_unlock(&cluster->lock);
1854 return 0;
1855 }
1856 } else if (cluster->block_group != block_group) {
1857 /* someone else has already freed it don't redo their work */
1858 spin_unlock(&cluster->lock);
1859 return 0;
1860 }
1861 atomic_inc(&block_group->count);
1862 spin_unlock(&cluster->lock);
1863
Li Zefan34d52cb2011-03-29 13:46:06 +08001864 ctl = block_group->free_space_ctl;
1865
Chris Masonfa9c0d792009-04-03 09:47:43 -04001866 /* now return any extents the cluster had on it */
Li Zefan34d52cb2011-03-29 13:46:06 +08001867 spin_lock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04001868 ret = __btrfs_return_cluster_to_free_space(block_group, cluster);
Li Zefan34d52cb2011-03-29 13:46:06 +08001869 spin_unlock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04001870
1871 /* finally drop our ref */
1872 btrfs_put_block_group(block_group);
1873 return ret;
1874}
1875
Josef Bacik96303082009-07-13 21:29:25 -04001876static u64 btrfs_alloc_from_bitmap(struct btrfs_block_group_cache *block_group,
1877 struct btrfs_free_cluster *cluster,
Josef Bacik4e69b592011-03-21 10:11:24 -04001878 struct btrfs_free_space *entry,
Josef Bacik96303082009-07-13 21:29:25 -04001879 u64 bytes, u64 min_start)
1880{
Li Zefan34d52cb2011-03-29 13:46:06 +08001881 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik96303082009-07-13 21:29:25 -04001882 int err;
1883 u64 search_start = cluster->window_start;
1884 u64 search_bytes = bytes;
1885 u64 ret = 0;
1886
Josef Bacik96303082009-07-13 21:29:25 -04001887 search_start = min_start;
1888 search_bytes = bytes;
1889
Li Zefan34d52cb2011-03-29 13:46:06 +08001890 err = search_bitmap(ctl, entry, &search_start, &search_bytes);
Josef Bacik96303082009-07-13 21:29:25 -04001891 if (err)
Josef Bacik4e69b592011-03-21 10:11:24 -04001892 return 0;
Josef Bacik96303082009-07-13 21:29:25 -04001893
1894 ret = search_start;
Li Zefan34d52cb2011-03-29 13:46:06 +08001895 bitmap_clear_bits(ctl, entry, ret, bytes);
Josef Bacik96303082009-07-13 21:29:25 -04001896
1897 return ret;
1898}
1899
Chris Masonfa9c0d792009-04-03 09:47:43 -04001900/*
1901 * given a cluster, try to allocate 'bytes' from it, returns 0
1902 * if it couldn't find anything suitably large, or a logical disk offset
1903 * if things worked out
1904 */
1905u64 btrfs_alloc_from_cluster(struct btrfs_block_group_cache *block_group,
1906 struct btrfs_free_cluster *cluster, u64 bytes,
1907 u64 min_start)
1908{
Li Zefan34d52cb2011-03-29 13:46:06 +08001909 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04001910 struct btrfs_free_space *entry = NULL;
1911 struct rb_node *node;
1912 u64 ret = 0;
1913
1914 spin_lock(&cluster->lock);
1915 if (bytes > cluster->max_size)
1916 goto out;
1917
1918 if (cluster->block_group != block_group)
1919 goto out;
1920
1921 node = rb_first(&cluster->root);
1922 if (!node)
1923 goto out;
1924
1925 entry = rb_entry(node, struct btrfs_free_space, offset_index);
Chris Masonfa9c0d792009-04-03 09:47:43 -04001926 while(1) {
Josef Bacik4e69b592011-03-21 10:11:24 -04001927 if (entry->bytes < bytes ||
1928 (!entry->bitmap && entry->offset < min_start)) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04001929 struct rb_node *node;
1930
1931 node = rb_next(&entry->offset_index);
1932 if (!node)
1933 break;
1934 entry = rb_entry(node, struct btrfs_free_space,
1935 offset_index);
1936 continue;
1937 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04001938
Josef Bacik4e69b592011-03-21 10:11:24 -04001939 if (entry->bitmap) {
1940 ret = btrfs_alloc_from_bitmap(block_group,
1941 cluster, entry, bytes,
1942 min_start);
1943 if (ret == 0) {
1944 struct rb_node *node;
1945 node = rb_next(&entry->offset_index);
1946 if (!node)
1947 break;
1948 entry = rb_entry(node, struct btrfs_free_space,
1949 offset_index);
1950 continue;
1951 }
1952 } else {
1953
1954 ret = entry->offset;
1955
1956 entry->offset += bytes;
1957 entry->bytes -= bytes;
1958 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04001959
Li Zefan5e71b5d2010-11-09 14:55:34 +08001960 if (entry->bytes == 0)
Chris Masonfa9c0d792009-04-03 09:47:43 -04001961 rb_erase(&entry->offset_index, &cluster->root);
Chris Masonfa9c0d792009-04-03 09:47:43 -04001962 break;
1963 }
1964out:
1965 spin_unlock(&cluster->lock);
Josef Bacik96303082009-07-13 21:29:25 -04001966
Li Zefan5e71b5d2010-11-09 14:55:34 +08001967 if (!ret)
1968 return 0;
1969
Li Zefan34d52cb2011-03-29 13:46:06 +08001970 spin_lock(&ctl->tree_lock);
Li Zefan5e71b5d2010-11-09 14:55:34 +08001971
Li Zefan34d52cb2011-03-29 13:46:06 +08001972 ctl->free_space -= bytes;
Li Zefan5e71b5d2010-11-09 14:55:34 +08001973 if (entry->bytes == 0) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001974 ctl->free_extents--;
Josef Bacik4e69b592011-03-21 10:11:24 -04001975 if (entry->bitmap) {
1976 kfree(entry->bitmap);
Li Zefan34d52cb2011-03-29 13:46:06 +08001977 ctl->total_bitmaps--;
1978 ctl->op->recalc_thresholds(ctl);
Josef Bacik4e69b592011-03-21 10:11:24 -04001979 }
Josef Bacikdc89e982011-01-28 17:05:48 -05001980 kmem_cache_free(btrfs_free_space_cachep, entry);
Li Zefan5e71b5d2010-11-09 14:55:34 +08001981 }
1982
Li Zefan34d52cb2011-03-29 13:46:06 +08001983 spin_unlock(&ctl->tree_lock);
Li Zefan5e71b5d2010-11-09 14:55:34 +08001984
Chris Masonfa9c0d792009-04-03 09:47:43 -04001985 return ret;
1986}
1987
Josef Bacik96303082009-07-13 21:29:25 -04001988static int btrfs_bitmap_cluster(struct btrfs_block_group_cache *block_group,
1989 struct btrfs_free_space *entry,
1990 struct btrfs_free_cluster *cluster,
1991 u64 offset, u64 bytes, u64 min_bytes)
1992{
Li Zefan34d52cb2011-03-29 13:46:06 +08001993 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik96303082009-07-13 21:29:25 -04001994 unsigned long next_zero;
1995 unsigned long i;
1996 unsigned long search_bits;
1997 unsigned long total_bits;
1998 unsigned long found_bits;
1999 unsigned long start = 0;
2000 unsigned long total_found = 0;
Josef Bacik4e69b592011-03-21 10:11:24 -04002001 int ret;
Josef Bacik96303082009-07-13 21:29:25 -04002002 bool found = false;
2003
2004 i = offset_to_bit(entry->offset, block_group->sectorsize,
2005 max_t(u64, offset, entry->offset));
Josef Bacikd0a365e2011-03-18 15:27:43 -04002006 search_bits = bytes_to_bits(bytes, block_group->sectorsize);
2007 total_bits = bytes_to_bits(min_bytes, block_group->sectorsize);
Josef Bacik96303082009-07-13 21:29:25 -04002008
2009again:
2010 found_bits = 0;
2011 for (i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, i);
2012 i < BITS_PER_BITMAP;
2013 i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, i + 1)) {
2014 next_zero = find_next_zero_bit(entry->bitmap,
2015 BITS_PER_BITMAP, i);
2016 if (next_zero - i >= search_bits) {
2017 found_bits = next_zero - i;
2018 break;
2019 }
2020 i = next_zero;
2021 }
2022
2023 if (!found_bits)
Josef Bacik4e69b592011-03-21 10:11:24 -04002024 return -ENOSPC;
Josef Bacik96303082009-07-13 21:29:25 -04002025
2026 if (!found) {
2027 start = i;
2028 found = true;
2029 }
2030
2031 total_found += found_bits;
2032
2033 if (cluster->max_size < found_bits * block_group->sectorsize)
2034 cluster->max_size = found_bits * block_group->sectorsize;
2035
2036 if (total_found < total_bits) {
2037 i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, next_zero);
2038 if (i - start > total_bits * 2) {
2039 total_found = 0;
2040 cluster->max_size = 0;
2041 found = false;
2042 }
2043 goto again;
2044 }
2045
2046 cluster->window_start = start * block_group->sectorsize +
2047 entry->offset;
Li Zefan34d52cb2011-03-29 13:46:06 +08002048 rb_erase(&entry->offset_index, &ctl->free_space_offset);
Josef Bacik4e69b592011-03-21 10:11:24 -04002049 ret = tree_insert_offset(&cluster->root, entry->offset,
2050 &entry->offset_index, 1);
2051 BUG_ON(ret);
Josef Bacik96303082009-07-13 21:29:25 -04002052
2053 return 0;
2054}
2055
Chris Masonfa9c0d792009-04-03 09:47:43 -04002056/*
Josef Bacik4e69b592011-03-21 10:11:24 -04002057 * This searches the block group for just extents to fill the cluster with.
2058 */
2059static int setup_cluster_no_bitmap(struct btrfs_block_group_cache *block_group,
2060 struct btrfs_free_cluster *cluster,
2061 u64 offset, u64 bytes, u64 min_bytes)
2062{
Li Zefan34d52cb2011-03-29 13:46:06 +08002063 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik4e69b592011-03-21 10:11:24 -04002064 struct btrfs_free_space *first = NULL;
2065 struct btrfs_free_space *entry = NULL;
2066 struct btrfs_free_space *prev = NULL;
2067 struct btrfs_free_space *last;
2068 struct rb_node *node;
2069 u64 window_start;
2070 u64 window_free;
2071 u64 max_extent;
2072 u64 max_gap = 128 * 1024;
2073
Li Zefan34d52cb2011-03-29 13:46:06 +08002074 entry = tree_search_offset(ctl, offset, 0, 1);
Josef Bacik4e69b592011-03-21 10:11:24 -04002075 if (!entry)
2076 return -ENOSPC;
2077
2078 /*
2079 * We don't want bitmaps, so just move along until we find a normal
2080 * extent entry.
2081 */
2082 while (entry->bitmap) {
2083 node = rb_next(&entry->offset_index);
2084 if (!node)
2085 return -ENOSPC;
2086 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2087 }
2088
2089 window_start = entry->offset;
2090 window_free = entry->bytes;
2091 max_extent = entry->bytes;
2092 first = entry;
2093 last = entry;
2094 prev = entry;
2095
2096 while (window_free <= min_bytes) {
2097 node = rb_next(&entry->offset_index);
2098 if (!node)
2099 return -ENOSPC;
2100 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2101
2102 if (entry->bitmap)
2103 continue;
2104 /*
2105 * we haven't filled the empty size and the window is
2106 * very large. reset and try again
2107 */
2108 if (entry->offset - (prev->offset + prev->bytes) > max_gap ||
2109 entry->offset - window_start > (min_bytes * 2)) {
2110 first = entry;
2111 window_start = entry->offset;
2112 window_free = entry->bytes;
2113 last = entry;
2114 max_extent = entry->bytes;
2115 } else {
2116 last = entry;
2117 window_free += entry->bytes;
2118 if (entry->bytes > max_extent)
2119 max_extent = entry->bytes;
2120 }
2121 prev = entry;
2122 }
2123
2124 cluster->window_start = first->offset;
2125
2126 node = &first->offset_index;
2127
2128 /*
2129 * now we've found our entries, pull them out of the free space
2130 * cache and put them into the cluster rbtree
2131 */
2132 do {
2133 int ret;
2134
2135 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2136 node = rb_next(&entry->offset_index);
2137 if (entry->bitmap)
2138 continue;
2139
Li Zefan34d52cb2011-03-29 13:46:06 +08002140 rb_erase(&entry->offset_index, &ctl->free_space_offset);
Josef Bacik4e69b592011-03-21 10:11:24 -04002141 ret = tree_insert_offset(&cluster->root, entry->offset,
2142 &entry->offset_index, 0);
2143 BUG_ON(ret);
2144 } while (node && entry != last);
2145
2146 cluster->max_size = max_extent;
2147
2148 return 0;
2149}
2150
2151/*
2152 * This specifically looks for bitmaps that may work in the cluster, we assume
2153 * that we have already failed to find extents that will work.
2154 */
2155static int setup_cluster_bitmap(struct btrfs_block_group_cache *block_group,
2156 struct btrfs_free_cluster *cluster,
2157 u64 offset, u64 bytes, u64 min_bytes)
2158{
Li Zefan34d52cb2011-03-29 13:46:06 +08002159 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik4e69b592011-03-21 10:11:24 -04002160 struct btrfs_free_space *entry;
2161 struct rb_node *node;
2162 int ret = -ENOSPC;
2163
Li Zefan34d52cb2011-03-29 13:46:06 +08002164 if (ctl->total_bitmaps == 0)
Josef Bacik4e69b592011-03-21 10:11:24 -04002165 return -ENOSPC;
2166
Li Zefan34d52cb2011-03-29 13:46:06 +08002167 entry = tree_search_offset(ctl, offset_to_bitmap(ctl, offset), 0, 1);
Josef Bacik4e69b592011-03-21 10:11:24 -04002168 if (!entry)
2169 return -ENOSPC;
2170
2171 node = &entry->offset_index;
2172 do {
2173 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2174 node = rb_next(&entry->offset_index);
2175 if (!entry->bitmap)
2176 continue;
2177 if (entry->bytes < min_bytes)
2178 continue;
2179 ret = btrfs_bitmap_cluster(block_group, entry, cluster, offset,
2180 bytes, min_bytes);
2181 } while (ret && node);
2182
2183 return ret;
2184}
2185
2186/*
Chris Masonfa9c0d792009-04-03 09:47:43 -04002187 * here we try to find a cluster of blocks in a block group. The goal
2188 * is to find at least bytes free and up to empty_size + bytes free.
2189 * We might not find them all in one contiguous area.
2190 *
2191 * returns zero and sets up cluster if things worked out, otherwise
2192 * it returns -enospc
2193 */
2194int btrfs_find_space_cluster(struct btrfs_trans_handle *trans,
Chris Mason451d7582009-06-09 20:28:34 -04002195 struct btrfs_root *root,
Chris Masonfa9c0d792009-04-03 09:47:43 -04002196 struct btrfs_block_group_cache *block_group,
2197 struct btrfs_free_cluster *cluster,
2198 u64 offset, u64 bytes, u64 empty_size)
2199{
Li Zefan34d52cb2011-03-29 13:46:06 +08002200 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002201 u64 min_bytes;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002202 int ret;
2203
2204 /* for metadata, allow allocates with more holes */
Chris Mason451d7582009-06-09 20:28:34 -04002205 if (btrfs_test_opt(root, SSD_SPREAD)) {
2206 min_bytes = bytes + empty_size;
2207 } else if (block_group->flags & BTRFS_BLOCK_GROUP_METADATA) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04002208 /*
2209 * we want to do larger allocations when we are
2210 * flushing out the delayed refs, it helps prevent
2211 * making more work as we go along.
2212 */
2213 if (trans->transaction->delayed_refs.flushing)
2214 min_bytes = max(bytes, (bytes + empty_size) >> 1);
2215 else
2216 min_bytes = max(bytes, (bytes + empty_size) >> 4);
2217 } else
2218 min_bytes = max(bytes, (bytes + empty_size) >> 2);
2219
Li Zefan34d52cb2011-03-29 13:46:06 +08002220 spin_lock(&ctl->tree_lock);
Josef Bacik7d0d2e82011-03-18 15:13:42 -04002221
2222 /*
2223 * If we know we don't have enough space to make a cluster don't even
2224 * bother doing all the work to try and find one.
2225 */
Li Zefan34d52cb2011-03-29 13:46:06 +08002226 if (ctl->free_space < min_bytes) {
2227 spin_unlock(&ctl->tree_lock);
Josef Bacik7d0d2e82011-03-18 15:13:42 -04002228 return -ENOSPC;
2229 }
2230
Chris Masonfa9c0d792009-04-03 09:47:43 -04002231 spin_lock(&cluster->lock);
2232
2233 /* someone already found a cluster, hooray */
2234 if (cluster->block_group) {
2235 ret = 0;
2236 goto out;
2237 }
Josef Bacik4e69b592011-03-21 10:11:24 -04002238
2239 ret = setup_cluster_no_bitmap(block_group, cluster, offset, bytes,
2240 min_bytes);
2241 if (ret)
2242 ret = setup_cluster_bitmap(block_group, cluster, offset,
2243 bytes, min_bytes);
2244
2245 if (!ret) {
2246 atomic_inc(&block_group->count);
2247 list_add_tail(&cluster->block_group_list,
2248 &block_group->cluster_list);
2249 cluster->block_group = block_group;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002250 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04002251out:
2252 spin_unlock(&cluster->lock);
Li Zefan34d52cb2011-03-29 13:46:06 +08002253 spin_unlock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002254
2255 return ret;
2256}
2257
2258/*
2259 * simple code to zero out a cluster
2260 */
2261void btrfs_init_free_cluster(struct btrfs_free_cluster *cluster)
2262{
2263 spin_lock_init(&cluster->lock);
2264 spin_lock_init(&cluster->refill_lock);
Eric Paris6bef4d32010-02-23 19:43:04 +00002265 cluster->root = RB_ROOT;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002266 cluster->max_size = 0;
2267 INIT_LIST_HEAD(&cluster->block_group_list);
2268 cluster->block_group = NULL;
2269}
2270
Li Dongyangf7039b12011-03-24 10:24:28 +00002271int btrfs_trim_block_group(struct btrfs_block_group_cache *block_group,
2272 u64 *trimmed, u64 start, u64 end, u64 minlen)
2273{
Li Zefan34d52cb2011-03-29 13:46:06 +08002274 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Li Dongyangf7039b12011-03-24 10:24:28 +00002275 struct btrfs_free_space *entry = NULL;
2276 struct btrfs_fs_info *fs_info = block_group->fs_info;
2277 u64 bytes = 0;
2278 u64 actually_trimmed;
2279 int ret = 0;
2280
2281 *trimmed = 0;
2282
2283 while (start < end) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002284 spin_lock(&ctl->tree_lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00002285
Li Zefan34d52cb2011-03-29 13:46:06 +08002286 if (ctl->free_space < minlen) {
2287 spin_unlock(&ctl->tree_lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00002288 break;
2289 }
2290
Li Zefan34d52cb2011-03-29 13:46:06 +08002291 entry = tree_search_offset(ctl, start, 0, 1);
Li Dongyangf7039b12011-03-24 10:24:28 +00002292 if (!entry)
Li Zefan34d52cb2011-03-29 13:46:06 +08002293 entry = tree_search_offset(ctl,
2294 offset_to_bitmap(ctl, start),
Li Dongyangf7039b12011-03-24 10:24:28 +00002295 1, 1);
2296
2297 if (!entry || entry->offset >= end) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002298 spin_unlock(&ctl->tree_lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00002299 break;
2300 }
2301
2302 if (entry->bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002303 ret = search_bitmap(ctl, entry, &start, &bytes);
Li Dongyangf7039b12011-03-24 10:24:28 +00002304 if (!ret) {
2305 if (start >= end) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002306 spin_unlock(&ctl->tree_lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00002307 break;
2308 }
2309 bytes = min(bytes, end - start);
Li Zefan34d52cb2011-03-29 13:46:06 +08002310 bitmap_clear_bits(ctl, entry, start, bytes);
Li Dongyangf7039b12011-03-24 10:24:28 +00002311 if (entry->bytes == 0)
Li Zefan34d52cb2011-03-29 13:46:06 +08002312 free_bitmap(ctl, entry);
Li Dongyangf7039b12011-03-24 10:24:28 +00002313 } else {
2314 start = entry->offset + BITS_PER_BITMAP *
2315 block_group->sectorsize;
Li Zefan34d52cb2011-03-29 13:46:06 +08002316 spin_unlock(&ctl->tree_lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00002317 ret = 0;
2318 continue;
2319 }
2320 } else {
2321 start = entry->offset;
2322 bytes = min(entry->bytes, end - start);
Li Zefan34d52cb2011-03-29 13:46:06 +08002323 unlink_free_space(ctl, entry);
Li Dongyangf7039b12011-03-24 10:24:28 +00002324 kfree(entry);
2325 }
2326
Li Zefan34d52cb2011-03-29 13:46:06 +08002327 spin_unlock(&ctl->tree_lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00002328
2329 if (bytes >= minlen) {
2330 int update_ret;
2331 update_ret = btrfs_update_reserved_bytes(block_group,
2332 bytes, 1, 1);
2333
2334 ret = btrfs_error_discard_extent(fs_info->extent_root,
2335 start,
2336 bytes,
2337 &actually_trimmed);
2338
Li Zefan34d52cb2011-03-29 13:46:06 +08002339 btrfs_add_free_space(block_group, start, bytes);
Li Dongyangf7039b12011-03-24 10:24:28 +00002340 if (!update_ret)
2341 btrfs_update_reserved_bytes(block_group,
2342 bytes, 0, 1);
2343
2344 if (ret)
2345 break;
2346 *trimmed += actually_trimmed;
2347 }
2348 start += bytes;
2349 bytes = 0;
2350
2351 if (fatal_signal_pending(current)) {
2352 ret = -ERESTARTSYS;
2353 break;
2354 }
2355
2356 cond_resched();
2357 }
2358
2359 return ret;
2360}
Li Zefan581bb052011-04-20 10:06:11 +08002361
2362/*
2363 * Find the left-most item in the cache tree, and then return the
2364 * smallest inode number in the item.
2365 *
2366 * Note: the returned inode number may not be the smallest one in
2367 * the tree, if the left-most item is a bitmap.
2368 */
2369u64 btrfs_find_ino_for_alloc(struct btrfs_root *fs_root)
2370{
2371 struct btrfs_free_space_ctl *ctl = fs_root->free_ino_ctl;
2372 struct btrfs_free_space *entry = NULL;
2373 u64 ino = 0;
2374
2375 spin_lock(&ctl->tree_lock);
2376
2377 if (RB_EMPTY_ROOT(&ctl->free_space_offset))
2378 goto out;
2379
2380 entry = rb_entry(rb_first(&ctl->free_space_offset),
2381 struct btrfs_free_space, offset_index);
2382
2383 if (!entry->bitmap) {
2384 ino = entry->offset;
2385
2386 unlink_free_space(ctl, entry);
2387 entry->offset++;
2388 entry->bytes--;
2389 if (!entry->bytes)
2390 kmem_cache_free(btrfs_free_space_cachep, entry);
2391 else
2392 link_free_space(ctl, entry);
2393 } else {
2394 u64 offset = 0;
2395 u64 count = 1;
2396 int ret;
2397
2398 ret = search_bitmap(ctl, entry, &offset, &count);
2399 BUG_ON(ret);
2400
2401 ino = offset;
2402 bitmap_clear_bits(ctl, entry, offset, 1);
2403 if (entry->bytes == 0)
2404 free_bitmap(ctl, entry);
2405 }
2406out:
2407 spin_unlock(&ctl->tree_lock);
2408
2409 return ino;
2410}