blob: 3bde17ff14c08e3522f95f9ba151042419b4f523 [file] [log] [blame]
Josef Bacik0f9dd462008-09-23 13:14:11 -04001/*
2 * Copyright (C) 2008 Red Hat. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public
6 * License v2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 * Boston, MA 021110-1307, USA.
17 */
18
Josef Bacik96303082009-07-13 21:29:25 -040019#include <linux/pagemap.h>
Josef Bacik0f9dd462008-09-23 13:14:11 -040020#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/slab.h>
Josef Bacik96303082009-07-13 21:29:25 -040022#include <linux/math64.h>
Josef Bacik6ab60602011-08-08 08:24:46 -040023#include <linux/ratelimit.h>
Josef Bacik0f9dd462008-09-23 13:14:11 -040024#include "ctree.h"
Chris Masonfa9c0d792009-04-03 09:47:43 -040025#include "free-space-cache.h"
26#include "transaction.h"
Josef Bacik0af3d002010-06-21 14:48:16 -040027#include "disk-io.h"
Josef Bacik43be2142011-04-01 14:55:00 +000028#include "extent_io.h"
Li Zefan581bb052011-04-20 10:06:11 +080029#include "inode-map.h"
Chris Masonfa9c0d792009-04-03 09:47:43 -040030
Josef Bacik96303082009-07-13 21:29:25 -040031#define BITS_PER_BITMAP (PAGE_CACHE_SIZE * 8)
32#define MAX_CACHE_BYTES_PER_GIG (32 * 1024)
33
Li Zefan34d52cb2011-03-29 13:46:06 +080034static int link_free_space(struct btrfs_free_space_ctl *ctl,
Josef Bacik0cb59c92010-07-02 12:14:14 -040035 struct btrfs_free_space *info);
36
Li Zefan0414efa2011-04-20 10:20:14 +080037static struct inode *__lookup_free_space_inode(struct btrfs_root *root,
38 struct btrfs_path *path,
39 u64 offset)
Josef Bacik0af3d002010-06-21 14:48:16 -040040{
41 struct btrfs_key key;
42 struct btrfs_key location;
43 struct btrfs_disk_key disk_key;
44 struct btrfs_free_space_header *header;
45 struct extent_buffer *leaf;
46 struct inode *inode = NULL;
47 int ret;
48
Josef Bacik0af3d002010-06-21 14:48:16 -040049 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
Li Zefan0414efa2011-04-20 10:20:14 +080050 key.offset = offset;
Josef Bacik0af3d002010-06-21 14:48:16 -040051 key.type = 0;
52
53 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
54 if (ret < 0)
55 return ERR_PTR(ret);
56 if (ret > 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +020057 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -040058 return ERR_PTR(-ENOENT);
59 }
60
61 leaf = path->nodes[0];
62 header = btrfs_item_ptr(leaf, path->slots[0],
63 struct btrfs_free_space_header);
64 btrfs_free_space_key(leaf, header, &disk_key);
65 btrfs_disk_key_to_cpu(&location, &disk_key);
David Sterbab3b4aa72011-04-21 01:20:15 +020066 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -040067
68 inode = btrfs_iget(root->fs_info->sb, &location, root, NULL);
69 if (!inode)
70 return ERR_PTR(-ENOENT);
71 if (IS_ERR(inode))
72 return inode;
73 if (is_bad_inode(inode)) {
74 iput(inode);
75 return ERR_PTR(-ENOENT);
76 }
77
Miao Xieadae52b2011-03-31 09:43:23 +000078 inode->i_mapping->flags &= ~__GFP_FS;
79
Li Zefan0414efa2011-04-20 10:20:14 +080080 return inode;
81}
82
83struct inode *lookup_free_space_inode(struct btrfs_root *root,
84 struct btrfs_block_group_cache
85 *block_group, struct btrfs_path *path)
86{
87 struct inode *inode = NULL;
88
89 spin_lock(&block_group->lock);
90 if (block_group->inode)
91 inode = igrab(block_group->inode);
92 spin_unlock(&block_group->lock);
93 if (inode)
94 return inode;
95
96 inode = __lookup_free_space_inode(root, path,
97 block_group->key.objectid);
98 if (IS_ERR(inode))
99 return inode;
100
Josef Bacik0af3d002010-06-21 14:48:16 -0400101 spin_lock(&block_group->lock);
Josef Bacik2f356122011-06-10 15:31:13 -0400102 if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) {
103 printk(KERN_INFO "Old style space inode found, converting.\n");
104 BTRFS_I(inode)->flags &= ~BTRFS_INODE_NODATASUM;
105 block_group->disk_cache_state = BTRFS_DC_CLEAR;
106 }
107
Josef Bacik300e4f82011-08-29 14:06:00 -0400108 if (!block_group->iref) {
Josef Bacik0af3d002010-06-21 14:48:16 -0400109 block_group->inode = igrab(inode);
110 block_group->iref = 1;
111 }
112 spin_unlock(&block_group->lock);
113
114 return inode;
115}
116
Li Zefan0414efa2011-04-20 10:20:14 +0800117int __create_free_space_inode(struct btrfs_root *root,
118 struct btrfs_trans_handle *trans,
119 struct btrfs_path *path, u64 ino, u64 offset)
Josef Bacik0af3d002010-06-21 14:48:16 -0400120{
121 struct btrfs_key key;
122 struct btrfs_disk_key disk_key;
123 struct btrfs_free_space_header *header;
124 struct btrfs_inode_item *inode_item;
125 struct extent_buffer *leaf;
Josef Bacik0af3d002010-06-21 14:48:16 -0400126 int ret;
127
Li Zefan0414efa2011-04-20 10:20:14 +0800128 ret = btrfs_insert_empty_inode(trans, root, path, ino);
Josef Bacik0af3d002010-06-21 14:48:16 -0400129 if (ret)
130 return ret;
131
132 leaf = path->nodes[0];
133 inode_item = btrfs_item_ptr(leaf, path->slots[0],
134 struct btrfs_inode_item);
135 btrfs_item_key(leaf, &disk_key, path->slots[0]);
136 memset_extent_buffer(leaf, 0, (unsigned long)inode_item,
137 sizeof(*inode_item));
138 btrfs_set_inode_generation(leaf, inode_item, trans->transid);
139 btrfs_set_inode_size(leaf, inode_item, 0);
140 btrfs_set_inode_nbytes(leaf, inode_item, 0);
141 btrfs_set_inode_uid(leaf, inode_item, 0);
142 btrfs_set_inode_gid(leaf, inode_item, 0);
143 btrfs_set_inode_mode(leaf, inode_item, S_IFREG | 0600);
144 btrfs_set_inode_flags(leaf, inode_item, BTRFS_INODE_NOCOMPRESS |
Josef Bacik2f356122011-06-10 15:31:13 -0400145 BTRFS_INODE_PREALLOC);
Josef Bacik0af3d002010-06-21 14:48:16 -0400146 btrfs_set_inode_nlink(leaf, inode_item, 1);
147 btrfs_set_inode_transid(leaf, inode_item, trans->transid);
Li Zefan0414efa2011-04-20 10:20:14 +0800148 btrfs_set_inode_block_group(leaf, inode_item, offset);
Josef Bacik0af3d002010-06-21 14:48:16 -0400149 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +0200150 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -0400151
152 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
Li Zefan0414efa2011-04-20 10:20:14 +0800153 key.offset = offset;
Josef Bacik0af3d002010-06-21 14:48:16 -0400154 key.type = 0;
155
156 ret = btrfs_insert_empty_item(trans, root, path, &key,
157 sizeof(struct btrfs_free_space_header));
158 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200159 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -0400160 return ret;
161 }
162 leaf = path->nodes[0];
163 header = btrfs_item_ptr(leaf, path->slots[0],
164 struct btrfs_free_space_header);
165 memset_extent_buffer(leaf, 0, (unsigned long)header, sizeof(*header));
166 btrfs_set_free_space_key(leaf, header, &disk_key);
167 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +0200168 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -0400169
170 return 0;
171}
172
Li Zefan0414efa2011-04-20 10:20:14 +0800173int create_free_space_inode(struct btrfs_root *root,
174 struct btrfs_trans_handle *trans,
175 struct btrfs_block_group_cache *block_group,
176 struct btrfs_path *path)
177{
178 int ret;
179 u64 ino;
180
181 ret = btrfs_find_free_objectid(root, &ino);
182 if (ret < 0)
183 return ret;
184
185 return __create_free_space_inode(root, trans, path, ino,
186 block_group->key.objectid);
187}
188
Josef Bacik0af3d002010-06-21 14:48:16 -0400189int btrfs_truncate_free_space_cache(struct btrfs_root *root,
190 struct btrfs_trans_handle *trans,
191 struct btrfs_path *path,
192 struct inode *inode)
193{
Liu Bo65450aa2011-09-11 10:52:24 -0400194 struct btrfs_block_rsv *rsv;
Josef Bacik0af3d002010-06-21 14:48:16 -0400195 loff_t oldsize;
196 int ret = 0;
197
Liu Bo65450aa2011-09-11 10:52:24 -0400198 rsv = trans->block_rsv;
Josef Bacik0af3d002010-06-21 14:48:16 -0400199 trans->block_rsv = root->orphan_block_rsv;
200 ret = btrfs_block_rsv_check(trans, root,
201 root->orphan_block_rsv,
Josef Bacik482e6dc2011-08-19 10:31:56 -0400202 0, 5, 0);
Josef Bacik0af3d002010-06-21 14:48:16 -0400203 if (ret)
204 return ret;
205
206 oldsize = i_size_read(inode);
207 btrfs_i_size_write(inode, 0);
208 truncate_pagecache(inode, oldsize, 0);
209
210 /*
211 * We don't need an orphan item because truncating the free space cache
212 * will never be split across transactions.
213 */
214 ret = btrfs_truncate_inode_items(trans, root, inode,
215 0, BTRFS_EXTENT_DATA_KEY);
Liu Bo65450aa2011-09-11 10:52:24 -0400216
217 trans->block_rsv = rsv;
Josef Bacik0af3d002010-06-21 14:48:16 -0400218 if (ret) {
219 WARN_ON(1);
220 return ret;
221 }
222
Li Zefan82d59022011-04-20 10:33:24 +0800223 ret = btrfs_update_inode(trans, root, inode);
224 return ret;
Josef Bacik0af3d002010-06-21 14:48:16 -0400225}
226
Josef Bacik9d66e232010-08-25 16:54:15 -0400227static int readahead_cache(struct inode *inode)
228{
229 struct file_ra_state *ra;
230 unsigned long last_index;
231
232 ra = kzalloc(sizeof(*ra), GFP_NOFS);
233 if (!ra)
234 return -ENOMEM;
235
236 file_ra_state_init(ra, inode->i_mapping);
237 last_index = (i_size_read(inode) - 1) >> PAGE_CACHE_SHIFT;
238
239 page_cache_sync_readahead(inode->i_mapping, ra, NULL, 0, last_index);
240
241 kfree(ra);
242
243 return 0;
244}
245
Li Zefan0414efa2011-04-20 10:20:14 +0800246int __load_free_space_cache(struct btrfs_root *root, struct inode *inode,
247 struct btrfs_free_space_ctl *ctl,
248 struct btrfs_path *path, u64 offset)
Josef Bacik9d66e232010-08-25 16:54:15 -0400249{
Josef Bacik9d66e232010-08-25 16:54:15 -0400250 struct btrfs_free_space_header *header;
251 struct extent_buffer *leaf;
252 struct page *page;
Josef Bacik9d66e232010-08-25 16:54:15 -0400253 struct btrfs_key key;
254 struct list_head bitmaps;
255 u64 num_entries;
256 u64 num_bitmaps;
257 u64 generation;
Josef Bacik9d66e232010-08-25 16:54:15 -0400258 pgoff_t index = 0;
Josef Bacikf6a39822011-06-06 10:50:35 -0400259 int ret = 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400260
261 INIT_LIST_HEAD(&bitmaps);
262
Josef Bacik9d66e232010-08-25 16:54:15 -0400263 /* Nothing in the space cache, goodbye */
Li Zefan0414efa2011-04-20 10:20:14 +0800264 if (!i_size_read(inode))
Josef Bacik9d66e232010-08-25 16:54:15 -0400265 goto out;
Josef Bacik9d66e232010-08-25 16:54:15 -0400266
267 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
Li Zefan0414efa2011-04-20 10:20:14 +0800268 key.offset = offset;
Josef Bacik9d66e232010-08-25 16:54:15 -0400269 key.type = 0;
270
271 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Li Zefan0414efa2011-04-20 10:20:14 +0800272 if (ret < 0)
273 goto out;
274 else if (ret > 0) {
Chris Mason945d8962011-05-22 12:33:42 -0400275 btrfs_release_path(path);
Li Zefan0414efa2011-04-20 10:20:14 +0800276 ret = 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400277 goto out;
278 }
279
Li Zefan0414efa2011-04-20 10:20:14 +0800280 ret = -1;
281
Josef Bacik9d66e232010-08-25 16:54:15 -0400282 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);
Chris Mason945d8962011-05-22 12:33:42 -0400288 btrfs_release_path(path);
Josef Bacik9d66e232010-08-25 16:54:15 -0400289
290 if (BTRFS_I(inode)->generation != generation) {
291 printk(KERN_ERR "btrfs: free space inode generation (%llu) did"
Li Zefan0414efa2011-04-20 10:20:14 +0800292 " not match free space cache generation (%llu)\n",
Josef Bacik9d66e232010-08-25 16:54:15 -0400293 (unsigned long long)BTRFS_I(inode)->generation,
Li Zefan0414efa2011-04-20 10:20:14 +0800294 (unsigned long long)generation);
295 goto out;
Josef Bacik9d66e232010-08-25 16:54:15 -0400296 }
297
298 if (!num_entries)
299 goto out;
300
Josef Bacik9d66e232010-08-25 16:54:15 -0400301 ret = readahead_cache(inode);
Li Zefan0414efa2011-04-20 10:20:14 +0800302 if (ret)
Josef Bacik9d66e232010-08-25 16:54:15 -0400303 goto out;
Josef Bacik9d66e232010-08-25 16:54:15 -0400304
305 while (1) {
306 struct btrfs_free_space_entry *entry;
307 struct btrfs_free_space *e;
308 void *addr;
309 unsigned long offset = 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400310 int need_loop = 0;
311
312 if (!num_entries && !num_bitmaps)
313 break;
314
Josef Bacika94733d2011-07-11 10:47:06 -0400315 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
Li Zefan0414efa2011-04-20 10:20:14 +0800316 if (!page)
Josef Bacik9d66e232010-08-25 16:54:15 -0400317 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400318
319 if (!PageUptodate(page)) {
320 btrfs_readpage(NULL, page);
321 lock_page(page);
322 if (!PageUptodate(page)) {
323 unlock_page(page);
324 page_cache_release(page);
325 printk(KERN_ERR "btrfs: error reading free "
Li Zefan0414efa2011-04-20 10:20:14 +0800326 "space cache\n");
Josef Bacik9d66e232010-08-25 16:54:15 -0400327 goto free_cache;
328 }
329 }
330 addr = kmap(page);
331
332 if (index == 0) {
333 u64 *gen;
334
Josef Bacik2f356122011-06-10 15:31:13 -0400335 /*
336 * We put a bogus crc in the front of the first page in
337 * case old kernels try to mount a fs with the new
338 * format to make sure they discard the cache.
339 */
340 addr += sizeof(u64);
341 offset += sizeof(u64);
342
343 gen = addr;
Josef Bacik9d66e232010-08-25 16:54:15 -0400344 if (*gen != BTRFS_I(inode)->generation) {
Josef Bacik6ab60602011-08-08 08:24:46 -0400345 printk_ratelimited(KERN_ERR "btrfs: space cache"
346 " generation (%llu) does not match "
347 "inode (%llu)\n",
348 (unsigned long long)*gen,
349 (unsigned long long)
350 BTRFS_I(inode)->generation);
Josef Bacik9d66e232010-08-25 16:54:15 -0400351 kunmap(page);
352 unlock_page(page);
353 page_cache_release(page);
354 goto free_cache;
355 }
Josef Bacik2f356122011-06-10 15:31:13 -0400356 addr += sizeof(u64);
357 offset += sizeof(u64);
Josef Bacik9d66e232010-08-25 16:54:15 -0400358 }
Josef Bacik2f356122011-06-10 15:31:13 -0400359 entry = addr;
Josef Bacik9d66e232010-08-25 16:54:15 -0400360
361 while (1) {
362 if (!num_entries)
363 break;
364
365 need_loop = 1;
Josef Bacikdc89e982011-01-28 17:05:48 -0500366 e = kmem_cache_zalloc(btrfs_free_space_cachep,
367 GFP_NOFS);
Josef Bacik9d66e232010-08-25 16:54:15 -0400368 if (!e) {
369 kunmap(page);
370 unlock_page(page);
371 page_cache_release(page);
372 goto free_cache;
373 }
374
375 e->offset = le64_to_cpu(entry->offset);
376 e->bytes = le64_to_cpu(entry->bytes);
377 if (!e->bytes) {
378 kunmap(page);
Josef Bacikdc89e982011-01-28 17:05:48 -0500379 kmem_cache_free(btrfs_free_space_cachep, e);
Josef Bacik9d66e232010-08-25 16:54:15 -0400380 unlock_page(page);
381 page_cache_release(page);
382 goto free_cache;
383 }
384
385 if (entry->type == BTRFS_FREE_SPACE_EXTENT) {
Li Zefan34d52cb2011-03-29 13:46:06 +0800386 spin_lock(&ctl->tree_lock);
387 ret = link_free_space(ctl, e);
388 spin_unlock(&ctl->tree_lock);
Josef Bacik207dde82011-05-13 14:49:23 -0400389 if (ret) {
390 printk(KERN_ERR "Duplicate entries in "
391 "free space cache, dumping\n");
392 kunmap(page);
393 unlock_page(page);
394 page_cache_release(page);
395 goto free_cache;
396 }
Josef Bacik9d66e232010-08-25 16:54:15 -0400397 } else {
398 e->bitmap = kzalloc(PAGE_CACHE_SIZE, GFP_NOFS);
399 if (!e->bitmap) {
400 kunmap(page);
Josef Bacikdc89e982011-01-28 17:05:48 -0500401 kmem_cache_free(
402 btrfs_free_space_cachep, e);
Josef Bacik9d66e232010-08-25 16:54:15 -0400403 unlock_page(page);
404 page_cache_release(page);
405 goto free_cache;
406 }
Li Zefan34d52cb2011-03-29 13:46:06 +0800407 spin_lock(&ctl->tree_lock);
Josef Bacikf6a39822011-06-06 10:50:35 -0400408 ret = link_free_space(ctl, e);
Li Zefan34d52cb2011-03-29 13:46:06 +0800409 ctl->total_bitmaps++;
410 ctl->op->recalc_thresholds(ctl);
411 spin_unlock(&ctl->tree_lock);
Josef Bacik207dde82011-05-13 14:49:23 -0400412 if (ret) {
413 printk(KERN_ERR "Duplicate entries in "
414 "free space cache, dumping\n");
415 kunmap(page);
416 unlock_page(page);
417 page_cache_release(page);
418 goto free_cache;
419 }
Josef Bacikf6a39822011-06-06 10:50:35 -0400420 list_add_tail(&e->list, &bitmaps);
Josef Bacik9d66e232010-08-25 16:54:15 -0400421 }
422
423 num_entries--;
424 offset += sizeof(struct btrfs_free_space_entry);
425 if (offset + sizeof(struct btrfs_free_space_entry) >=
426 PAGE_CACHE_SIZE)
427 break;
428 entry++;
429 }
430
431 /*
432 * We read an entry out of this page, we need to move on to the
433 * next page.
434 */
435 if (need_loop) {
436 kunmap(page);
437 goto next;
438 }
439
440 /*
441 * We add the bitmaps at the end of the entries in order that
442 * the bitmap entries are added to the cache.
443 */
444 e = list_entry(bitmaps.next, struct btrfs_free_space, list);
445 list_del_init(&e->list);
446 memcpy(e->bitmap, addr, PAGE_CACHE_SIZE);
447 kunmap(page);
448 num_bitmaps--;
449next:
450 unlock_page(page);
451 page_cache_release(page);
452 index++;
453 }
454
455 ret = 1;
456out:
Josef Bacik9d66e232010-08-25 16:54:15 -0400457 return ret;
Josef Bacik9d66e232010-08-25 16:54:15 -0400458free_cache:
Li Zefan0414efa2011-04-20 10:20:14 +0800459 __btrfs_remove_free_space_cache(ctl);
Josef Bacik9d66e232010-08-25 16:54:15 -0400460 goto out;
461}
462
Li Zefan0414efa2011-04-20 10:20:14 +0800463int load_free_space_cache(struct btrfs_fs_info *fs_info,
464 struct btrfs_block_group_cache *block_group)
Josef Bacik0cb59c92010-07-02 12:14:14 -0400465{
Li Zefan34d52cb2011-03-29 13:46:06 +0800466 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Li Zefan0414efa2011-04-20 10:20:14 +0800467 struct btrfs_root *root = fs_info->tree_root;
468 struct inode *inode;
469 struct btrfs_path *path;
470 int ret;
471 bool matched;
472 u64 used = btrfs_block_group_used(&block_group->item);
473
474 /*
475 * If we're unmounting then just return, since this does a search on the
476 * normal root and not the commit root and we could deadlock.
477 */
David Sterba7841cb22011-05-31 18:07:27 +0200478 if (btrfs_fs_closing(fs_info))
Li Zefan0414efa2011-04-20 10:20:14 +0800479 return 0;
480
481 /*
482 * If this block group has been marked to be cleared for one reason or
483 * another then we can't trust the on disk cache, so just return.
484 */
485 spin_lock(&block_group->lock);
486 if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) {
487 spin_unlock(&block_group->lock);
488 return 0;
489 }
490 spin_unlock(&block_group->lock);
491
492 path = btrfs_alloc_path();
493 if (!path)
494 return 0;
495
496 inode = lookup_free_space_inode(root, block_group, path);
497 if (IS_ERR(inode)) {
498 btrfs_free_path(path);
499 return 0;
500 }
501
502 ret = __load_free_space_cache(fs_info->tree_root, inode, ctl,
503 path, block_group->key.objectid);
504 btrfs_free_path(path);
505 if (ret <= 0)
506 goto out;
507
508 spin_lock(&ctl->tree_lock);
509 matched = (ctl->free_space == (block_group->key.offset - used -
510 block_group->bytes_super));
511 spin_unlock(&ctl->tree_lock);
512
513 if (!matched) {
514 __btrfs_remove_free_space_cache(ctl);
515 printk(KERN_ERR "block group %llu has an wrong amount of free "
516 "space\n", block_group->key.objectid);
517 ret = -1;
518 }
519out:
520 if (ret < 0) {
521 /* This cache is bogus, make sure it gets cleared */
522 spin_lock(&block_group->lock);
523 block_group->disk_cache_state = BTRFS_DC_CLEAR;
524 spin_unlock(&block_group->lock);
Li Zefan82d59022011-04-20 10:33:24 +0800525 ret = 0;
Li Zefan0414efa2011-04-20 10:20:14 +0800526
527 printk(KERN_ERR "btrfs: failed to load free space cache "
528 "for block group %llu\n", block_group->key.objectid);
529 }
530
531 iput(inode);
532 return ret;
533}
534
Josef Bacikc09544e2011-08-30 10:19:10 -0400535/**
536 * __btrfs_write_out_cache - write out cached info to an inode
537 * @root - the root the inode belongs to
538 * @ctl - the free space cache we are going to write out
539 * @block_group - the block_group for this cache if it belongs to a block_group
540 * @trans - the trans handle
541 * @path - the path to use
542 * @offset - the offset for the key we'll insert
543 *
544 * This function writes out a free space cache struct to disk for quick recovery
545 * on mount. This will return 0 if it was successfull in writing the cache out,
546 * and -1 if it was not.
547 */
Li Zefan0414efa2011-04-20 10:20:14 +0800548int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode,
549 struct btrfs_free_space_ctl *ctl,
550 struct btrfs_block_group_cache *block_group,
551 struct btrfs_trans_handle *trans,
552 struct btrfs_path *path, u64 offset)
Josef Bacik0cb59c92010-07-02 12:14:14 -0400553{
554 struct btrfs_free_space_header *header;
555 struct extent_buffer *leaf;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400556 struct rb_node *node;
557 struct list_head *pos, *n;
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400558 struct page **pages;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400559 struct page *page;
560 struct extent_state *cached_state = NULL;
Josef Bacik43be2142011-04-01 14:55:00 +0000561 struct btrfs_free_cluster *cluster = NULL;
562 struct extent_io_tree *unpin = NULL;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400563 struct list_head bitmap_list;
564 struct btrfs_key key;
Josef Bacik43be2142011-04-01 14:55:00 +0000565 u64 start, end, len;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400566 u64 bytes = 0;
Josef Bacik2f356122011-06-10 15:31:13 -0400567 u32 crc = ~(u32)0;
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400568 int index = 0, num_pages = 0;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400569 int entries = 0;
570 int bitmaps = 0;
Josef Bacikc09544e2011-08-30 10:19:10 -0400571 int ret;
572 int err = -1;
Josef Bacik43be2142011-04-01 14:55:00 +0000573 bool next_page = false;
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400574 bool out_of_space = false;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400575
Josef Bacik0cb59c92010-07-02 12:14:14 -0400576 INIT_LIST_HEAD(&bitmap_list);
577
Li Zefan34d52cb2011-03-29 13:46:06 +0800578 node = rb_first(&ctl->free_space_offset);
Li Zefan0414efa2011-04-20 10:20:14 +0800579 if (!node)
Josef Bacikc09544e2011-08-30 10:19:10 -0400580 return -1;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400581
Li Zefan0414efa2011-04-20 10:20:14 +0800582 if (!i_size_read(inode))
583 return -1;
Josef Bacik2b209822010-12-03 13:17:53 -0500584
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400585 num_pages = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
586 PAGE_CACHE_SHIFT;
Chris Mason211f96c2011-06-03 01:26:53 -0400587
Josef Bacik0cb59c92010-07-02 12:14:14 -0400588 filemap_write_and_wait(inode->i_mapping);
589 btrfs_wait_ordered_range(inode, inode->i_size &
590 ~(root->sectorsize - 1), (u64)-1);
591
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400592 pages = kzalloc(sizeof(struct page *) * num_pages, GFP_NOFS);
Josef Bacik2f356122011-06-10 15:31:13 -0400593 if (!pages)
Li Zefan0414efa2011-04-20 10:20:14 +0800594 return -1;
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400595
Josef Bacik43be2142011-04-01 14:55:00 +0000596 /* Get the cluster for this block_group if it exists */
Li Zefan0414efa2011-04-20 10:20:14 +0800597 if (block_group && !list_empty(&block_group->cluster_list))
Josef Bacik43be2142011-04-01 14:55:00 +0000598 cluster = list_entry(block_group->cluster_list.next,
599 struct btrfs_free_cluster,
600 block_group_list);
601
602 /*
603 * We shouldn't have switched the pinned extents yet so this is the
604 * right one
605 */
606 unpin = root->fs_info->pinned_extents;
607
Josef Bacik0cb59c92010-07-02 12:14:14 -0400608 /*
609 * Lock all pages first so we can lock the extent safely.
610 *
611 * NOTE: Because we hold the ref the entire time we're going to write to
612 * the page find_get_page should never fail, so we don't do a check
613 * after find_get_page at this point. Just putting this here so people
614 * know and don't freak out.
615 */
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400616 while (index < num_pages) {
Josef Bacika94733d2011-07-11 10:47:06 -0400617 page = find_or_create_page(inode->i_mapping, index, GFP_NOFS);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400618 if (!page) {
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400619 int i;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400620
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400621 for (i = 0; i < num_pages; i++) {
622 unlock_page(pages[i]);
623 page_cache_release(pages[i]);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400624 }
Josef Bacik2f356122011-06-10 15:31:13 -0400625 goto out;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400626 }
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400627 pages[index] = page;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400628 index++;
629 }
630
631 index = 0;
632 lock_extent_bits(&BTRFS_I(inode)->io_tree, 0, i_size_read(inode) - 1,
633 0, &cached_state, GFP_NOFS);
634
Josef Bacik43be2142011-04-01 14:55:00 +0000635 /*
636 * When searching for pinned extents, we need to start at our start
637 * offset.
638 */
Li Zefan0414efa2011-04-20 10:20:14 +0800639 if (block_group)
640 start = block_group->key.objectid;
Josef Bacik43be2142011-04-01 14:55:00 +0000641
Josef Bacik0cb59c92010-07-02 12:14:14 -0400642 /* Write out the extent entries */
643 do {
644 struct btrfs_free_space_entry *entry;
Josef Bacik2f356122011-06-10 15:31:13 -0400645 void *addr, *orig;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400646 unsigned long offset = 0;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400647
Josef Bacik43be2142011-04-01 14:55:00 +0000648 next_page = false;
649
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400650 if (index >= num_pages) {
651 out_of_space = true;
652 break;
653 }
654
655 page = pages[index];
Josef Bacik0cb59c92010-07-02 12:14:14 -0400656
Josef Bacik2f356122011-06-10 15:31:13 -0400657 orig = addr = kmap(page);
658 if (index == 0) {
659 u64 *gen;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400660
Josef Bacik2f356122011-06-10 15:31:13 -0400661 /*
662 * We're going to put in a bogus crc for this page to
663 * make sure that old kernels who aren't aware of this
664 * format will be sure to discard the cache.
665 */
666 addr += sizeof(u64);
667 offset += sizeof(u64);
668
669 gen = addr;
670 *gen = trans->transid;
671 addr += sizeof(u64);
672 offset += sizeof(u64);
673 }
674 entry = addr;
675
676 memset(addr, 0, PAGE_CACHE_SIZE - offset);
Josef Bacik43be2142011-04-01 14:55:00 +0000677 while (node && !next_page) {
Josef Bacik0cb59c92010-07-02 12:14:14 -0400678 struct btrfs_free_space *e;
679
680 e = rb_entry(node, struct btrfs_free_space, offset_index);
681 entries++;
682
683 entry->offset = cpu_to_le64(e->offset);
684 entry->bytes = cpu_to_le64(e->bytes);
685 if (e->bitmap) {
686 entry->type = BTRFS_FREE_SPACE_BITMAP;
687 list_add_tail(&e->list, &bitmap_list);
688 bitmaps++;
689 } else {
690 entry->type = BTRFS_FREE_SPACE_EXTENT;
691 }
692 node = rb_next(node);
Josef Bacik43be2142011-04-01 14:55:00 +0000693 if (!node && cluster) {
694 node = rb_first(&cluster->root);
695 cluster = NULL;
696 }
Josef Bacik0cb59c92010-07-02 12:14:14 -0400697 offset += sizeof(struct btrfs_free_space_entry);
698 if (offset + sizeof(struct btrfs_free_space_entry) >=
699 PAGE_CACHE_SIZE)
Josef Bacik43be2142011-04-01 14:55:00 +0000700 next_page = true;
701 entry++;
702 }
703
704 /*
705 * We want to add any pinned extents to our free space cache
706 * so we don't leak the space
707 */
Li Zefan0414efa2011-04-20 10:20:14 +0800708 while (block_group && !next_page &&
709 (start < block_group->key.objectid +
710 block_group->key.offset)) {
Josef Bacik43be2142011-04-01 14:55:00 +0000711 ret = find_first_extent_bit(unpin, start, &start, &end,
712 EXTENT_DIRTY);
713 if (ret) {
714 ret = 0;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400715 break;
Josef Bacik43be2142011-04-01 14:55:00 +0000716 }
717
718 /* This pinned extent is out of our range */
719 if (start >= block_group->key.objectid +
720 block_group->key.offset)
721 break;
722
723 len = block_group->key.objectid +
724 block_group->key.offset - start;
725 len = min(len, end + 1 - start);
726
727 entries++;
728 entry->offset = cpu_to_le64(start);
729 entry->bytes = cpu_to_le64(len);
730 entry->type = BTRFS_FREE_SPACE_EXTENT;
731
732 start = end + 1;
733 offset += sizeof(struct btrfs_free_space_entry);
734 if (offset + sizeof(struct btrfs_free_space_entry) >=
735 PAGE_CACHE_SIZE)
736 next_page = true;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400737 entry++;
738 }
Josef Bacik0cb59c92010-07-02 12:14:14 -0400739
Josef Bacik2f356122011-06-10 15:31:13 -0400740 /* Generate bogus crc value */
741 if (index == 0) {
742 u32 *tmp;
743 crc = btrfs_csum_data(root, orig + sizeof(u64), crc,
744 PAGE_CACHE_SIZE - sizeof(u64));
745 btrfs_csum_final(crc, (char *)&crc);
746 crc++;
747 tmp = orig;
748 *tmp = crc;
749 }
750
751 kunmap(page);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400752
753 bytes += PAGE_CACHE_SIZE;
754
Josef Bacik0cb59c92010-07-02 12:14:14 -0400755 index++;
Josef Bacik43be2142011-04-01 14:55:00 +0000756 } while (node || next_page);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400757
758 /* Write out the bitmaps */
759 list_for_each_safe(pos, n, &bitmap_list) {
760 void *addr;
761 struct btrfs_free_space *entry =
762 list_entry(pos, struct btrfs_free_space, list);
763
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400764 if (index >= num_pages) {
765 out_of_space = true;
766 break;
767 }
Chris Masonf65647c2011-04-18 08:55:34 -0400768 page = pages[index];
Josef Bacik0cb59c92010-07-02 12:14:14 -0400769
770 addr = kmap(page);
771 memcpy(addr, entry->bitmap, PAGE_CACHE_SIZE);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400772 kunmap(page);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400773 bytes += PAGE_CACHE_SIZE;
774
Josef Bacik0cb59c92010-07-02 12:14:14 -0400775 list_del_init(&entry->list);
776 index++;
777 }
778
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400779 if (out_of_space) {
780 btrfs_drop_pages(pages, num_pages);
781 unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
782 i_size_read(inode) - 1, &cached_state,
783 GFP_NOFS);
Josef Bacik2f356122011-06-10 15:31:13 -0400784 goto out;
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400785 }
786
Josef Bacik0cb59c92010-07-02 12:14:14 -0400787 /* Zero out the rest of the pages just to make sure */
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400788 while (index < num_pages) {
Josef Bacik0cb59c92010-07-02 12:14:14 -0400789 void *addr;
790
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400791 page = pages[index];
Josef Bacik0cb59c92010-07-02 12:14:14 -0400792 addr = kmap(page);
793 memset(addr, 0, PAGE_CACHE_SIZE);
794 kunmap(page);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400795 bytes += PAGE_CACHE_SIZE;
796 index++;
797 }
798
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400799 ret = btrfs_dirty_pages(root, inode, pages, num_pages, 0,
800 bytes, &cached_state);
801 btrfs_drop_pages(pages, num_pages);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400802 unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
803 i_size_read(inode) - 1, &cached_state, GFP_NOFS);
804
Josef Bacikc09544e2011-08-30 10:19:10 -0400805 if (ret)
Josef Bacik2f356122011-06-10 15:31:13 -0400806 goto out;
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400807
808 BTRFS_I(inode)->generation = trans->transid;
809
Josef Bacik0cb59c92010-07-02 12:14:14 -0400810 filemap_write_and_wait(inode->i_mapping);
811
812 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
Li Zefan0414efa2011-04-20 10:20:14 +0800813 key.offset = offset;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400814 key.type = 0;
815
Josef Bacika9b5fcd2011-08-19 12:06:12 -0400816 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400817 if (ret < 0) {
Josef Bacik0cb59c92010-07-02 12:14:14 -0400818 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, bytes - 1,
819 EXTENT_DIRTY | EXTENT_DELALLOC |
820 EXTENT_DO_ACCOUNTING, 0, 0, NULL, GFP_NOFS);
Josef Bacik2f356122011-06-10 15:31:13 -0400821 goto out;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400822 }
823 leaf = path->nodes[0];
824 if (ret > 0) {
825 struct btrfs_key found_key;
826 BUG_ON(!path->slots[0]);
827 path->slots[0]--;
828 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
829 if (found_key.objectid != BTRFS_FREE_SPACE_OBJECTID ||
Li Zefan0414efa2011-04-20 10:20:14 +0800830 found_key.offset != offset) {
Josef Bacik0cb59c92010-07-02 12:14:14 -0400831 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, bytes - 1,
832 EXTENT_DIRTY | EXTENT_DELALLOC |
833 EXTENT_DO_ACCOUNTING, 0, 0, NULL,
834 GFP_NOFS);
David Sterbab3b4aa72011-04-21 01:20:15 +0200835 btrfs_release_path(path);
Josef Bacik2f356122011-06-10 15:31:13 -0400836 goto out;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400837 }
838 }
839 header = btrfs_item_ptr(leaf, path->slots[0],
840 struct btrfs_free_space_header);
841 btrfs_set_free_space_entries(leaf, header, entries);
842 btrfs_set_free_space_bitmaps(leaf, header, bitmaps);
843 btrfs_set_free_space_generation(leaf, header, trans->transid);
844 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +0200845 btrfs_release_path(path);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400846
Josef Bacikc09544e2011-08-30 10:19:10 -0400847 err = 0;
Josef Bacik2f356122011-06-10 15:31:13 -0400848out:
Chris Mason211f96c2011-06-03 01:26:53 -0400849 kfree(pages);
Josef Bacikc09544e2011-08-30 10:19:10 -0400850 if (err) {
Josef Bacik0cb59c92010-07-02 12:14:14 -0400851 invalidate_inode_pages2_range(inode->i_mapping, 0, index);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400852 BTRFS_I(inode)->generation = 0;
853 }
Josef Bacik0cb59c92010-07-02 12:14:14 -0400854 btrfs_update_inode(trans, root, inode);
Josef Bacikc09544e2011-08-30 10:19:10 -0400855 return err;
Li Zefan0414efa2011-04-20 10:20:14 +0800856}
857
858int btrfs_write_out_cache(struct btrfs_root *root,
859 struct btrfs_trans_handle *trans,
860 struct btrfs_block_group_cache *block_group,
861 struct btrfs_path *path)
862{
863 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
864 struct inode *inode;
865 int ret = 0;
866
867 root = root->fs_info->tree_root;
868
869 spin_lock(&block_group->lock);
870 if (block_group->disk_cache_state < BTRFS_DC_SETUP) {
871 spin_unlock(&block_group->lock);
872 return 0;
873 }
874 spin_unlock(&block_group->lock);
875
876 inode = lookup_free_space_inode(root, block_group, path);
877 if (IS_ERR(inode))
878 return 0;
879
880 ret = __btrfs_write_out_cache(root, inode, ctl, block_group, trans,
881 path, block_group->key.objectid);
Josef Bacikc09544e2011-08-30 10:19:10 -0400882 if (ret) {
883 btrfs_delalloc_release_metadata(inode, inode->i_size);
Li Zefan0414efa2011-04-20 10:20:14 +0800884 spin_lock(&block_group->lock);
885 block_group->disk_cache_state = BTRFS_DC_ERROR;
886 spin_unlock(&block_group->lock);
Li Zefan82d59022011-04-20 10:33:24 +0800887 ret = 0;
Josef Bacikc09544e2011-08-30 10:19:10 -0400888#ifdef DEBUG
Li Zefan0414efa2011-04-20 10:20:14 +0800889 printk(KERN_ERR "btrfs: failed to write free space cace "
890 "for block group %llu\n", block_group->key.objectid);
Josef Bacikc09544e2011-08-30 10:19:10 -0400891#endif
Li Zefan0414efa2011-04-20 10:20:14 +0800892 }
893
Josef Bacik0cb59c92010-07-02 12:14:14 -0400894 iput(inode);
895 return ret;
896}
897
Li Zefan34d52cb2011-03-29 13:46:06 +0800898static inline unsigned long offset_to_bit(u64 bitmap_start, u32 unit,
Josef Bacik96303082009-07-13 21:29:25 -0400899 u64 offset)
900{
901 BUG_ON(offset < bitmap_start);
902 offset -= bitmap_start;
Li Zefan34d52cb2011-03-29 13:46:06 +0800903 return (unsigned long)(div_u64(offset, unit));
Josef Bacik96303082009-07-13 21:29:25 -0400904}
905
Li Zefan34d52cb2011-03-29 13:46:06 +0800906static inline unsigned long bytes_to_bits(u64 bytes, u32 unit)
Josef Bacik96303082009-07-13 21:29:25 -0400907{
Li Zefan34d52cb2011-03-29 13:46:06 +0800908 return (unsigned long)(div_u64(bytes, unit));
Josef Bacik96303082009-07-13 21:29:25 -0400909}
910
Li Zefan34d52cb2011-03-29 13:46:06 +0800911static inline u64 offset_to_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -0400912 u64 offset)
913{
914 u64 bitmap_start;
915 u64 bytes_per_bitmap;
916
Li Zefan34d52cb2011-03-29 13:46:06 +0800917 bytes_per_bitmap = BITS_PER_BITMAP * ctl->unit;
918 bitmap_start = offset - ctl->start;
Josef Bacik96303082009-07-13 21:29:25 -0400919 bitmap_start = div64_u64(bitmap_start, bytes_per_bitmap);
920 bitmap_start *= bytes_per_bitmap;
Li Zefan34d52cb2011-03-29 13:46:06 +0800921 bitmap_start += ctl->start;
Josef Bacik96303082009-07-13 21:29:25 -0400922
923 return bitmap_start;
924}
Josef Bacik0f9dd462008-09-23 13:14:11 -0400925
926static int tree_insert_offset(struct rb_root *root, u64 offset,
Josef Bacik96303082009-07-13 21:29:25 -0400927 struct rb_node *node, int bitmap)
Josef Bacik0f9dd462008-09-23 13:14:11 -0400928{
929 struct rb_node **p = &root->rb_node;
930 struct rb_node *parent = NULL;
931 struct btrfs_free_space *info;
932
933 while (*p) {
934 parent = *p;
935 info = rb_entry(parent, struct btrfs_free_space, offset_index);
936
Josef Bacik96303082009-07-13 21:29:25 -0400937 if (offset < info->offset) {
Josef Bacik0f9dd462008-09-23 13:14:11 -0400938 p = &(*p)->rb_left;
Josef Bacik96303082009-07-13 21:29:25 -0400939 } else if (offset > info->offset) {
Josef Bacik0f9dd462008-09-23 13:14:11 -0400940 p = &(*p)->rb_right;
Josef Bacik96303082009-07-13 21:29:25 -0400941 } else {
942 /*
943 * we could have a bitmap entry and an extent entry
944 * share the same offset. If this is the case, we want
945 * the extent entry to always be found first if we do a
946 * linear search through the tree, since we want to have
947 * the quickest allocation time, and allocating from an
948 * extent is faster than allocating from a bitmap. So
949 * if we're inserting a bitmap and we find an entry at
950 * this offset, we want to go right, or after this entry
951 * logically. If we are inserting an extent and we've
952 * found a bitmap, we want to go left, or before
953 * logically.
954 */
955 if (bitmap) {
Josef Bacik207dde82011-05-13 14:49:23 -0400956 if (info->bitmap) {
957 WARN_ON_ONCE(1);
958 return -EEXIST;
959 }
Josef Bacik96303082009-07-13 21:29:25 -0400960 p = &(*p)->rb_right;
961 } else {
Josef Bacik207dde82011-05-13 14:49:23 -0400962 if (!info->bitmap) {
963 WARN_ON_ONCE(1);
964 return -EEXIST;
965 }
Josef Bacik96303082009-07-13 21:29:25 -0400966 p = &(*p)->rb_left;
967 }
968 }
Josef Bacik0f9dd462008-09-23 13:14:11 -0400969 }
970
971 rb_link_node(node, parent, p);
972 rb_insert_color(node, root);
973
974 return 0;
975}
976
977/*
Josef Bacik70cb0742009-04-03 10:14:19 -0400978 * searches the tree for the given offset.
979 *
Josef Bacik96303082009-07-13 21:29:25 -0400980 * fuzzy - If this is set, then we are trying to make an allocation, and we just
981 * want a section that has at least bytes size and comes at or after the given
982 * offset.
Josef Bacik0f9dd462008-09-23 13:14:11 -0400983 */
Josef Bacik96303082009-07-13 21:29:25 -0400984static struct btrfs_free_space *
Li Zefan34d52cb2011-03-29 13:46:06 +0800985tree_search_offset(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -0400986 u64 offset, int bitmap_only, int fuzzy)
Josef Bacik0f9dd462008-09-23 13:14:11 -0400987{
Li Zefan34d52cb2011-03-29 13:46:06 +0800988 struct rb_node *n = ctl->free_space_offset.rb_node;
Josef Bacik96303082009-07-13 21:29:25 -0400989 struct btrfs_free_space *entry, *prev = NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400990
Josef Bacik96303082009-07-13 21:29:25 -0400991 /* find entry that is closest to the 'offset' */
992 while (1) {
993 if (!n) {
994 entry = NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -0400995 break;
996 }
Josef Bacik96303082009-07-13 21:29:25 -0400997
998 entry = rb_entry(n, struct btrfs_free_space, offset_index);
999 prev = entry;
1000
1001 if (offset < entry->offset)
1002 n = n->rb_left;
1003 else if (offset > entry->offset)
1004 n = n->rb_right;
1005 else
1006 break;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001007 }
1008
Josef Bacik96303082009-07-13 21:29:25 -04001009 if (bitmap_only) {
1010 if (!entry)
1011 return NULL;
1012 if (entry->bitmap)
1013 return entry;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001014
Josef Bacik96303082009-07-13 21:29:25 -04001015 /*
1016 * bitmap entry and extent entry may share same offset,
1017 * in that case, bitmap entry comes after extent entry.
1018 */
1019 n = rb_next(n);
1020 if (!n)
1021 return NULL;
1022 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1023 if (entry->offset != offset)
1024 return NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001025
Josef Bacik96303082009-07-13 21:29:25 -04001026 WARN_ON(!entry->bitmap);
1027 return entry;
1028 } else if (entry) {
1029 if (entry->bitmap) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001030 /*
Josef Bacik96303082009-07-13 21:29:25 -04001031 * if previous extent entry covers the offset,
1032 * we should return it instead of the bitmap entry
Josef Bacik0f9dd462008-09-23 13:14:11 -04001033 */
Josef Bacik96303082009-07-13 21:29:25 -04001034 n = &entry->offset_index;
1035 while (1) {
1036 n = rb_prev(n);
1037 if (!n)
1038 break;
1039 prev = rb_entry(n, struct btrfs_free_space,
1040 offset_index);
1041 if (!prev->bitmap) {
1042 if (prev->offset + prev->bytes > offset)
1043 entry = prev;
1044 break;
1045 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04001046 }
Josef Bacik96303082009-07-13 21:29:25 -04001047 }
1048 return entry;
1049 }
1050
1051 if (!prev)
1052 return NULL;
1053
1054 /* find last entry before the 'offset' */
1055 entry = prev;
1056 if (entry->offset > offset) {
1057 n = rb_prev(&entry->offset_index);
1058 if (n) {
1059 entry = rb_entry(n, struct btrfs_free_space,
1060 offset_index);
1061 BUG_ON(entry->offset > offset);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001062 } else {
Josef Bacik96303082009-07-13 21:29:25 -04001063 if (fuzzy)
1064 return entry;
1065 else
1066 return NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001067 }
1068 }
1069
Josef Bacik96303082009-07-13 21:29:25 -04001070 if (entry->bitmap) {
1071 n = &entry->offset_index;
1072 while (1) {
1073 n = rb_prev(n);
1074 if (!n)
1075 break;
1076 prev = rb_entry(n, struct btrfs_free_space,
1077 offset_index);
1078 if (!prev->bitmap) {
1079 if (prev->offset + prev->bytes > offset)
1080 return prev;
1081 break;
1082 }
1083 }
Li Zefan34d52cb2011-03-29 13:46:06 +08001084 if (entry->offset + BITS_PER_BITMAP * ctl->unit > offset)
Josef Bacik96303082009-07-13 21:29:25 -04001085 return entry;
1086 } else if (entry->offset + entry->bytes > offset)
1087 return entry;
1088
1089 if (!fuzzy)
1090 return NULL;
1091
1092 while (1) {
1093 if (entry->bitmap) {
1094 if (entry->offset + BITS_PER_BITMAP *
Li Zefan34d52cb2011-03-29 13:46:06 +08001095 ctl->unit > offset)
Josef Bacik96303082009-07-13 21:29:25 -04001096 break;
1097 } else {
1098 if (entry->offset + entry->bytes > offset)
1099 break;
1100 }
1101
1102 n = rb_next(&entry->offset_index);
1103 if (!n)
1104 return NULL;
1105 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1106 }
1107 return entry;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001108}
1109
Li Zefanf333adb2010-11-09 14:57:39 +08001110static inline void
Li Zefan34d52cb2011-03-29 13:46:06 +08001111__unlink_free_space(struct btrfs_free_space_ctl *ctl,
Li Zefanf333adb2010-11-09 14:57:39 +08001112 struct btrfs_free_space *info)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001113{
Li Zefan34d52cb2011-03-29 13:46:06 +08001114 rb_erase(&info->offset_index, &ctl->free_space_offset);
1115 ctl->free_extents--;
Li Zefanf333adb2010-11-09 14:57:39 +08001116}
1117
Li Zefan34d52cb2011-03-29 13:46:06 +08001118static void unlink_free_space(struct btrfs_free_space_ctl *ctl,
Li Zefanf333adb2010-11-09 14:57:39 +08001119 struct btrfs_free_space *info)
1120{
Li Zefan34d52cb2011-03-29 13:46:06 +08001121 __unlink_free_space(ctl, info);
1122 ctl->free_space -= info->bytes;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001123}
1124
Li Zefan34d52cb2011-03-29 13:46:06 +08001125static int link_free_space(struct btrfs_free_space_ctl *ctl,
Josef Bacik0f9dd462008-09-23 13:14:11 -04001126 struct btrfs_free_space *info)
1127{
1128 int ret = 0;
1129
Josef Bacik96303082009-07-13 21:29:25 -04001130 BUG_ON(!info->bitmap && !info->bytes);
Li Zefan34d52cb2011-03-29 13:46:06 +08001131 ret = tree_insert_offset(&ctl->free_space_offset, info->offset,
Josef Bacik96303082009-07-13 21:29:25 -04001132 &info->offset_index, (info->bitmap != NULL));
Josef Bacik0f9dd462008-09-23 13:14:11 -04001133 if (ret)
1134 return ret;
1135
Li Zefan34d52cb2011-03-29 13:46:06 +08001136 ctl->free_space += info->bytes;
1137 ctl->free_extents++;
Josef Bacik96303082009-07-13 21:29:25 -04001138 return ret;
1139}
1140
Li Zefan34d52cb2011-03-29 13:46:06 +08001141static void recalculate_thresholds(struct btrfs_free_space_ctl *ctl)
Josef Bacik96303082009-07-13 21:29:25 -04001142{
Li Zefan34d52cb2011-03-29 13:46:06 +08001143 struct btrfs_block_group_cache *block_group = ctl->private;
Josef Bacik25891f72009-09-11 16:11:20 -04001144 u64 max_bytes;
1145 u64 bitmap_bytes;
1146 u64 extent_bytes;
Li Zefan8eb2d822010-11-09 14:48:01 +08001147 u64 size = block_group->key.offset;
Li Zefan34d52cb2011-03-29 13:46:06 +08001148 u64 bytes_per_bg = BITS_PER_BITMAP * block_group->sectorsize;
1149 int max_bitmaps = div64_u64(size + bytes_per_bg - 1, bytes_per_bg);
1150
1151 BUG_ON(ctl->total_bitmaps > max_bitmaps);
Josef Bacik96303082009-07-13 21:29:25 -04001152
1153 /*
1154 * The goal is to keep the total amount of memory used per 1gb of space
1155 * at or below 32k, so we need to adjust how much memory we allow to be
1156 * used by extent based free space tracking
1157 */
Li Zefan8eb2d822010-11-09 14:48:01 +08001158 if (size < 1024 * 1024 * 1024)
1159 max_bytes = MAX_CACHE_BYTES_PER_GIG;
1160 else
1161 max_bytes = MAX_CACHE_BYTES_PER_GIG *
1162 div64_u64(size, 1024 * 1024 * 1024);
Josef Bacik96303082009-07-13 21:29:25 -04001163
Josef Bacik25891f72009-09-11 16:11:20 -04001164 /*
1165 * we want to account for 1 more bitmap than what we have so we can make
1166 * sure we don't go over our overall goal of MAX_CACHE_BYTES_PER_GIG as
1167 * we add more bitmaps.
1168 */
Li Zefan34d52cb2011-03-29 13:46:06 +08001169 bitmap_bytes = (ctl->total_bitmaps + 1) * PAGE_CACHE_SIZE;
Josef Bacik96303082009-07-13 21:29:25 -04001170
Josef Bacik25891f72009-09-11 16:11:20 -04001171 if (bitmap_bytes >= max_bytes) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001172 ctl->extents_thresh = 0;
Josef Bacik25891f72009-09-11 16:11:20 -04001173 return;
Josef Bacik96303082009-07-13 21:29:25 -04001174 }
Josef Bacik25891f72009-09-11 16:11:20 -04001175
1176 /*
1177 * we want the extent entry threshold to always be at most 1/2 the maxw
1178 * bytes we can have, or whatever is less than that.
1179 */
1180 extent_bytes = max_bytes - bitmap_bytes;
1181 extent_bytes = min_t(u64, extent_bytes, div64_u64(max_bytes, 2));
1182
Li Zefan34d52cb2011-03-29 13:46:06 +08001183 ctl->extents_thresh =
Josef Bacik25891f72009-09-11 16:11:20 -04001184 div64_u64(extent_bytes, (sizeof(struct btrfs_free_space)));
Josef Bacik96303082009-07-13 21:29:25 -04001185}
1186
Miao Xiebb3ac5a2011-08-05 09:32:35 +00001187static inline void __bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
1188 struct btrfs_free_space *info,
1189 u64 offset, u64 bytes)
Josef Bacik96303082009-07-13 21:29:25 -04001190{
Li Zefanf38b6e72011-03-14 13:40:51 +08001191 unsigned long start, count;
Josef Bacik96303082009-07-13 21:29:25 -04001192
Li Zefan34d52cb2011-03-29 13:46:06 +08001193 start = offset_to_bit(info->offset, ctl->unit, offset);
1194 count = bytes_to_bits(bytes, ctl->unit);
Li Zefanf38b6e72011-03-14 13:40:51 +08001195 BUG_ON(start + count > BITS_PER_BITMAP);
Josef Bacik96303082009-07-13 21:29:25 -04001196
Li Zefanf38b6e72011-03-14 13:40:51 +08001197 bitmap_clear(info->bitmap, start, count);
Josef Bacik96303082009-07-13 21:29:25 -04001198
1199 info->bytes -= bytes;
Miao Xiebb3ac5a2011-08-05 09:32:35 +00001200}
1201
1202static void bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
1203 struct btrfs_free_space *info, u64 offset,
1204 u64 bytes)
1205{
1206 __bitmap_clear_bits(ctl, info, offset, bytes);
Li Zefan34d52cb2011-03-29 13:46:06 +08001207 ctl->free_space -= bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001208}
1209
Li Zefan34d52cb2011-03-29 13:46:06 +08001210static void bitmap_set_bits(struct btrfs_free_space_ctl *ctl,
Josef Bacik817d52f2009-07-13 21:29:25 -04001211 struct btrfs_free_space *info, u64 offset,
1212 u64 bytes)
Josef Bacik96303082009-07-13 21:29:25 -04001213{
Li Zefanf38b6e72011-03-14 13:40:51 +08001214 unsigned long start, count;
Josef Bacik96303082009-07-13 21:29:25 -04001215
Li Zefan34d52cb2011-03-29 13:46:06 +08001216 start = offset_to_bit(info->offset, ctl->unit, offset);
1217 count = bytes_to_bits(bytes, ctl->unit);
Li Zefanf38b6e72011-03-14 13:40:51 +08001218 BUG_ON(start + count > BITS_PER_BITMAP);
Josef Bacik96303082009-07-13 21:29:25 -04001219
Li Zefanf38b6e72011-03-14 13:40:51 +08001220 bitmap_set(info->bitmap, start, count);
Josef Bacik96303082009-07-13 21:29:25 -04001221
1222 info->bytes += bytes;
Li Zefan34d52cb2011-03-29 13:46:06 +08001223 ctl->free_space += bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001224}
1225
Li Zefan34d52cb2011-03-29 13:46:06 +08001226static int search_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001227 struct btrfs_free_space *bitmap_info, u64 *offset,
1228 u64 *bytes)
1229{
1230 unsigned long found_bits = 0;
1231 unsigned long bits, i;
1232 unsigned long next_zero;
1233
Li Zefan34d52cb2011-03-29 13:46:06 +08001234 i = offset_to_bit(bitmap_info->offset, ctl->unit,
Josef Bacik96303082009-07-13 21:29:25 -04001235 max_t(u64, *offset, bitmap_info->offset));
Li Zefan34d52cb2011-03-29 13:46:06 +08001236 bits = bytes_to_bits(*bytes, ctl->unit);
Josef Bacik96303082009-07-13 21:29:25 -04001237
1238 for (i = find_next_bit(bitmap_info->bitmap, BITS_PER_BITMAP, i);
1239 i < BITS_PER_BITMAP;
1240 i = find_next_bit(bitmap_info->bitmap, BITS_PER_BITMAP, i + 1)) {
1241 next_zero = find_next_zero_bit(bitmap_info->bitmap,
1242 BITS_PER_BITMAP, i);
1243 if ((next_zero - i) >= bits) {
1244 found_bits = next_zero - i;
1245 break;
1246 }
1247 i = next_zero;
1248 }
1249
1250 if (found_bits) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001251 *offset = (u64)(i * ctl->unit) + bitmap_info->offset;
1252 *bytes = (u64)(found_bits) * ctl->unit;
Josef Bacik96303082009-07-13 21:29:25 -04001253 return 0;
1254 }
1255
1256 return -1;
1257}
1258
Li Zefan34d52cb2011-03-29 13:46:06 +08001259static struct btrfs_free_space *
1260find_free_space(struct btrfs_free_space_ctl *ctl, u64 *offset, u64 *bytes)
Josef Bacik96303082009-07-13 21:29:25 -04001261{
1262 struct btrfs_free_space *entry;
1263 struct rb_node *node;
1264 int ret;
1265
Li Zefan34d52cb2011-03-29 13:46:06 +08001266 if (!ctl->free_space_offset.rb_node)
Josef Bacik96303082009-07-13 21:29:25 -04001267 return NULL;
1268
Li Zefan34d52cb2011-03-29 13:46:06 +08001269 entry = tree_search_offset(ctl, offset_to_bitmap(ctl, *offset), 0, 1);
Josef Bacik96303082009-07-13 21:29:25 -04001270 if (!entry)
1271 return NULL;
1272
1273 for (node = &entry->offset_index; node; node = rb_next(node)) {
1274 entry = rb_entry(node, struct btrfs_free_space, offset_index);
1275 if (entry->bytes < *bytes)
1276 continue;
1277
1278 if (entry->bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001279 ret = search_bitmap(ctl, entry, offset, bytes);
Josef Bacik96303082009-07-13 21:29:25 -04001280 if (!ret)
1281 return entry;
1282 continue;
1283 }
1284
1285 *offset = entry->offset;
1286 *bytes = entry->bytes;
1287 return entry;
1288 }
1289
1290 return NULL;
1291}
1292
Li Zefan34d52cb2011-03-29 13:46:06 +08001293static void add_new_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001294 struct btrfs_free_space *info, u64 offset)
1295{
Li Zefan34d52cb2011-03-29 13:46:06 +08001296 info->offset = offset_to_bitmap(ctl, offset);
Josef Bacikf019f422009-09-11 16:11:20 -04001297 info->bytes = 0;
Li Zefan34d52cb2011-03-29 13:46:06 +08001298 link_free_space(ctl, info);
1299 ctl->total_bitmaps++;
Josef Bacik96303082009-07-13 21:29:25 -04001300
Li Zefan34d52cb2011-03-29 13:46:06 +08001301 ctl->op->recalc_thresholds(ctl);
Josef Bacik96303082009-07-13 21:29:25 -04001302}
1303
Li Zefan34d52cb2011-03-29 13:46:06 +08001304static void free_bitmap(struct btrfs_free_space_ctl *ctl,
Li Zefanedf6e2d2010-11-09 14:50:07 +08001305 struct btrfs_free_space *bitmap_info)
1306{
Li Zefan34d52cb2011-03-29 13:46:06 +08001307 unlink_free_space(ctl, bitmap_info);
Li Zefanedf6e2d2010-11-09 14:50:07 +08001308 kfree(bitmap_info->bitmap);
Josef Bacikdc89e982011-01-28 17:05:48 -05001309 kmem_cache_free(btrfs_free_space_cachep, bitmap_info);
Li Zefan34d52cb2011-03-29 13:46:06 +08001310 ctl->total_bitmaps--;
1311 ctl->op->recalc_thresholds(ctl);
Li Zefanedf6e2d2010-11-09 14:50:07 +08001312}
1313
Li Zefan34d52cb2011-03-29 13:46:06 +08001314static noinline int remove_from_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001315 struct btrfs_free_space *bitmap_info,
1316 u64 *offset, u64 *bytes)
1317{
1318 u64 end;
Josef Bacik6606bb92009-07-31 11:03:58 -04001319 u64 search_start, search_bytes;
1320 int ret;
Josef Bacik96303082009-07-13 21:29:25 -04001321
1322again:
Li Zefan34d52cb2011-03-29 13:46:06 +08001323 end = bitmap_info->offset + (u64)(BITS_PER_BITMAP * ctl->unit) - 1;
Josef Bacik96303082009-07-13 21:29:25 -04001324
Josef Bacik6606bb92009-07-31 11:03:58 -04001325 /*
1326 * XXX - this can go away after a few releases.
1327 *
1328 * since the only user of btrfs_remove_free_space is the tree logging
1329 * stuff, and the only way to test that is under crash conditions, we
1330 * want to have this debug stuff here just in case somethings not
1331 * working. Search the bitmap for the space we are trying to use to
1332 * make sure its actually there. If its not there then we need to stop
1333 * because something has gone wrong.
1334 */
1335 search_start = *offset;
1336 search_bytes = *bytes;
Josef Bacik13dbc082011-02-03 02:39:52 +00001337 search_bytes = min(search_bytes, end - search_start + 1);
Li Zefan34d52cb2011-03-29 13:46:06 +08001338 ret = search_bitmap(ctl, bitmap_info, &search_start, &search_bytes);
Josef Bacik6606bb92009-07-31 11:03:58 -04001339 BUG_ON(ret < 0 || search_start != *offset);
1340
Josef Bacik96303082009-07-13 21:29:25 -04001341 if (*offset > bitmap_info->offset && *offset + *bytes > end) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001342 bitmap_clear_bits(ctl, bitmap_info, *offset, end - *offset + 1);
Josef Bacik96303082009-07-13 21:29:25 -04001343 *bytes -= end - *offset + 1;
1344 *offset = end + 1;
1345 } else if (*offset >= bitmap_info->offset && *offset + *bytes <= end) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001346 bitmap_clear_bits(ctl, bitmap_info, *offset, *bytes);
Josef Bacik96303082009-07-13 21:29:25 -04001347 *bytes = 0;
1348 }
1349
1350 if (*bytes) {
Josef Bacik6606bb92009-07-31 11:03:58 -04001351 struct rb_node *next = rb_next(&bitmap_info->offset_index);
Li Zefanedf6e2d2010-11-09 14:50:07 +08001352 if (!bitmap_info->bytes)
Li Zefan34d52cb2011-03-29 13:46:06 +08001353 free_bitmap(ctl, bitmap_info);
Josef Bacik96303082009-07-13 21:29:25 -04001354
Josef Bacik6606bb92009-07-31 11:03:58 -04001355 /*
1356 * no entry after this bitmap, but we still have bytes to
1357 * remove, so something has gone wrong.
1358 */
1359 if (!next)
Josef Bacik96303082009-07-13 21:29:25 -04001360 return -EINVAL;
1361
Josef Bacik6606bb92009-07-31 11:03:58 -04001362 bitmap_info = rb_entry(next, struct btrfs_free_space,
1363 offset_index);
1364
1365 /*
1366 * if the next entry isn't a bitmap we need to return to let the
1367 * extent stuff do its work.
1368 */
Josef Bacik96303082009-07-13 21:29:25 -04001369 if (!bitmap_info->bitmap)
1370 return -EAGAIN;
1371
Josef Bacik6606bb92009-07-31 11:03:58 -04001372 /*
1373 * Ok the next item is a bitmap, but it may not actually hold
1374 * the information for the rest of this free space stuff, so
1375 * look for it, and if we don't find it return so we can try
1376 * everything over again.
1377 */
1378 search_start = *offset;
1379 search_bytes = *bytes;
Li Zefan34d52cb2011-03-29 13:46:06 +08001380 ret = search_bitmap(ctl, bitmap_info, &search_start,
Josef Bacik6606bb92009-07-31 11:03:58 -04001381 &search_bytes);
1382 if (ret < 0 || search_start != *offset)
1383 return -EAGAIN;
1384
Josef Bacik96303082009-07-13 21:29:25 -04001385 goto again;
Li Zefanedf6e2d2010-11-09 14:50:07 +08001386 } else if (!bitmap_info->bytes)
Li Zefan34d52cb2011-03-29 13:46:06 +08001387 free_bitmap(ctl, bitmap_info);
Josef Bacik96303082009-07-13 21:29:25 -04001388
1389 return 0;
1390}
1391
Josef Bacik2cdc3422011-05-27 14:07:49 -04001392static u64 add_bytes_to_bitmap(struct btrfs_free_space_ctl *ctl,
1393 struct btrfs_free_space *info, u64 offset,
1394 u64 bytes)
1395{
1396 u64 bytes_to_set = 0;
1397 u64 end;
1398
1399 end = info->offset + (u64)(BITS_PER_BITMAP * ctl->unit);
1400
1401 bytes_to_set = min(end - offset, bytes);
1402
1403 bitmap_set_bits(ctl, info, offset, bytes_to_set);
1404
1405 return bytes_to_set;
1406
1407}
1408
Li Zefan34d52cb2011-03-29 13:46:06 +08001409static bool use_bitmap(struct btrfs_free_space_ctl *ctl,
1410 struct btrfs_free_space *info)
Josef Bacik96303082009-07-13 21:29:25 -04001411{
Li Zefan34d52cb2011-03-29 13:46:06 +08001412 struct btrfs_block_group_cache *block_group = ctl->private;
Josef Bacik96303082009-07-13 21:29:25 -04001413
1414 /*
1415 * If we are below the extents threshold then we can add this as an
1416 * extent, and don't have to deal with the bitmap
1417 */
Li Zefan34d52cb2011-03-29 13:46:06 +08001418 if (ctl->free_extents < ctl->extents_thresh) {
Josef Bacik32cb0842011-03-18 16:16:21 -04001419 /*
1420 * If this block group has some small extents we don't want to
1421 * use up all of our free slots in the cache with them, we want
1422 * to reserve them to larger extents, however if we have plent
1423 * of cache left then go ahead an dadd them, no sense in adding
1424 * the overhead of a bitmap if we don't have to.
1425 */
1426 if (info->bytes <= block_group->sectorsize * 4) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001427 if (ctl->free_extents * 2 <= ctl->extents_thresh)
1428 return false;
Josef Bacik32cb0842011-03-18 16:16:21 -04001429 } else {
Li Zefan34d52cb2011-03-29 13:46:06 +08001430 return false;
Josef Bacik32cb0842011-03-18 16:16:21 -04001431 }
1432 }
Josef Bacik96303082009-07-13 21:29:25 -04001433
1434 /*
1435 * some block groups are so tiny they can't be enveloped by a bitmap, so
1436 * don't even bother to create a bitmap for this
1437 */
1438 if (BITS_PER_BITMAP * block_group->sectorsize >
1439 block_group->key.offset)
Li Zefan34d52cb2011-03-29 13:46:06 +08001440 return false;
1441
1442 return true;
1443}
1444
Josef Bacik2cdc3422011-05-27 14:07:49 -04001445static struct btrfs_free_space_op free_space_op = {
1446 .recalc_thresholds = recalculate_thresholds,
1447 .use_bitmap = use_bitmap,
1448};
1449
Li Zefan34d52cb2011-03-29 13:46:06 +08001450static int insert_into_bitmap(struct btrfs_free_space_ctl *ctl,
1451 struct btrfs_free_space *info)
1452{
1453 struct btrfs_free_space *bitmap_info;
Josef Bacik2cdc3422011-05-27 14:07:49 -04001454 struct btrfs_block_group_cache *block_group = NULL;
Li Zefan34d52cb2011-03-29 13:46:06 +08001455 int added = 0;
Josef Bacik2cdc3422011-05-27 14:07:49 -04001456 u64 bytes, offset, bytes_added;
Li Zefan34d52cb2011-03-29 13:46:06 +08001457 int ret;
Josef Bacik96303082009-07-13 21:29:25 -04001458
1459 bytes = info->bytes;
1460 offset = info->offset;
1461
Li Zefan34d52cb2011-03-29 13:46:06 +08001462 if (!ctl->op->use_bitmap(ctl, info))
1463 return 0;
1464
Josef Bacik2cdc3422011-05-27 14:07:49 -04001465 if (ctl->op == &free_space_op)
1466 block_group = ctl->private;
Chris Mason38e87882011-06-10 16:36:57 -04001467again:
Josef Bacik2cdc3422011-05-27 14:07:49 -04001468 /*
1469 * Since we link bitmaps right into the cluster we need to see if we
1470 * have a cluster here, and if so and it has our bitmap we need to add
1471 * the free space to that bitmap.
1472 */
1473 if (block_group && !list_empty(&block_group->cluster_list)) {
1474 struct btrfs_free_cluster *cluster;
1475 struct rb_node *node;
1476 struct btrfs_free_space *entry;
1477
1478 cluster = list_entry(block_group->cluster_list.next,
1479 struct btrfs_free_cluster,
1480 block_group_list);
1481 spin_lock(&cluster->lock);
1482 node = rb_first(&cluster->root);
1483 if (!node) {
1484 spin_unlock(&cluster->lock);
Chris Mason38e87882011-06-10 16:36:57 -04001485 goto no_cluster_bitmap;
Josef Bacik2cdc3422011-05-27 14:07:49 -04001486 }
1487
1488 entry = rb_entry(node, struct btrfs_free_space, offset_index);
1489 if (!entry->bitmap) {
1490 spin_unlock(&cluster->lock);
Chris Mason38e87882011-06-10 16:36:57 -04001491 goto no_cluster_bitmap;
Josef Bacik2cdc3422011-05-27 14:07:49 -04001492 }
1493
1494 if (entry->offset == offset_to_bitmap(ctl, offset)) {
1495 bytes_added = add_bytes_to_bitmap(ctl, entry,
1496 offset, bytes);
1497 bytes -= bytes_added;
1498 offset += bytes_added;
1499 }
1500 spin_unlock(&cluster->lock);
1501 if (!bytes) {
1502 ret = 1;
1503 goto out;
1504 }
1505 }
Chris Mason38e87882011-06-10 16:36:57 -04001506
1507no_cluster_bitmap:
Li Zefan34d52cb2011-03-29 13:46:06 +08001508 bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
Josef Bacik96303082009-07-13 21:29:25 -04001509 1, 0);
1510 if (!bitmap_info) {
1511 BUG_ON(added);
1512 goto new_bitmap;
1513 }
1514
Josef Bacik2cdc3422011-05-27 14:07:49 -04001515 bytes_added = add_bytes_to_bitmap(ctl, bitmap_info, offset, bytes);
1516 bytes -= bytes_added;
1517 offset += bytes_added;
1518 added = 0;
Josef Bacik96303082009-07-13 21:29:25 -04001519
1520 if (!bytes) {
1521 ret = 1;
1522 goto out;
1523 } else
1524 goto again;
1525
1526new_bitmap:
1527 if (info && info->bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001528 add_new_bitmap(ctl, info, offset);
Josef Bacik96303082009-07-13 21:29:25 -04001529 added = 1;
1530 info = NULL;
1531 goto again;
1532 } else {
Li Zefan34d52cb2011-03-29 13:46:06 +08001533 spin_unlock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04001534
1535 /* no pre-allocated info, allocate a new one */
1536 if (!info) {
Josef Bacikdc89e982011-01-28 17:05:48 -05001537 info = kmem_cache_zalloc(btrfs_free_space_cachep,
1538 GFP_NOFS);
Josef Bacik96303082009-07-13 21:29:25 -04001539 if (!info) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001540 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04001541 ret = -ENOMEM;
1542 goto out;
1543 }
1544 }
1545
1546 /* allocate the bitmap */
1547 info->bitmap = kzalloc(PAGE_CACHE_SIZE, GFP_NOFS);
Li Zefan34d52cb2011-03-29 13:46:06 +08001548 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04001549 if (!info->bitmap) {
1550 ret = -ENOMEM;
1551 goto out;
1552 }
1553 goto again;
1554 }
1555
1556out:
1557 if (info) {
1558 if (info->bitmap)
1559 kfree(info->bitmap);
Josef Bacikdc89e982011-01-28 17:05:48 -05001560 kmem_cache_free(btrfs_free_space_cachep, info);
Josef Bacik96303082009-07-13 21:29:25 -04001561 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04001562
1563 return ret;
1564}
1565
Chris Mason945d8962011-05-22 12:33:42 -04001566static bool try_merge_free_space(struct btrfs_free_space_ctl *ctl,
Li Zefanf333adb2010-11-09 14:57:39 +08001567 struct btrfs_free_space *info, bool update_stat)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001568{
Li Zefan120d66e2010-11-09 14:56:50 +08001569 struct btrfs_free_space *left_info;
1570 struct btrfs_free_space *right_info;
1571 bool merged = false;
1572 u64 offset = info->offset;
1573 u64 bytes = info->bytes;
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001574
Josef Bacik0f9dd462008-09-23 13:14:11 -04001575 /*
1576 * first we want to see if there is free space adjacent to the range we
1577 * are adding, if there is remove that struct and add a new one to
1578 * cover the entire range
1579 */
Li Zefan34d52cb2011-03-29 13:46:06 +08001580 right_info = tree_search_offset(ctl, offset + bytes, 0, 0);
Josef Bacik96303082009-07-13 21:29:25 -04001581 if (right_info && rb_prev(&right_info->offset_index))
1582 left_info = rb_entry(rb_prev(&right_info->offset_index),
1583 struct btrfs_free_space, offset_index);
1584 else
Li Zefan34d52cb2011-03-29 13:46:06 +08001585 left_info = tree_search_offset(ctl, offset - 1, 0, 0);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001586
Josef Bacik96303082009-07-13 21:29:25 -04001587 if (right_info && !right_info->bitmap) {
Li Zefanf333adb2010-11-09 14:57:39 +08001588 if (update_stat)
Li Zefan34d52cb2011-03-29 13:46:06 +08001589 unlink_free_space(ctl, right_info);
Li Zefanf333adb2010-11-09 14:57:39 +08001590 else
Li Zefan34d52cb2011-03-29 13:46:06 +08001591 __unlink_free_space(ctl, right_info);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001592 info->bytes += right_info->bytes;
Josef Bacikdc89e982011-01-28 17:05:48 -05001593 kmem_cache_free(btrfs_free_space_cachep, right_info);
Li Zefan120d66e2010-11-09 14:56:50 +08001594 merged = true;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001595 }
1596
Josef Bacik96303082009-07-13 21:29:25 -04001597 if (left_info && !left_info->bitmap &&
1598 left_info->offset + left_info->bytes == offset) {
Li Zefanf333adb2010-11-09 14:57:39 +08001599 if (update_stat)
Li Zefan34d52cb2011-03-29 13:46:06 +08001600 unlink_free_space(ctl, left_info);
Li Zefanf333adb2010-11-09 14:57:39 +08001601 else
Li Zefan34d52cb2011-03-29 13:46:06 +08001602 __unlink_free_space(ctl, left_info);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001603 info->offset = left_info->offset;
1604 info->bytes += left_info->bytes;
Josef Bacikdc89e982011-01-28 17:05:48 -05001605 kmem_cache_free(btrfs_free_space_cachep, left_info);
Li Zefan120d66e2010-11-09 14:56:50 +08001606 merged = true;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001607 }
1608
Li Zefan120d66e2010-11-09 14:56:50 +08001609 return merged;
1610}
1611
Li Zefan581bb052011-04-20 10:06:11 +08001612int __btrfs_add_free_space(struct btrfs_free_space_ctl *ctl,
1613 u64 offset, u64 bytes)
Li Zefan120d66e2010-11-09 14:56:50 +08001614{
1615 struct btrfs_free_space *info;
1616 int ret = 0;
1617
Josef Bacikdc89e982011-01-28 17:05:48 -05001618 info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS);
Li Zefan120d66e2010-11-09 14:56:50 +08001619 if (!info)
1620 return -ENOMEM;
1621
1622 info->offset = offset;
1623 info->bytes = bytes;
1624
Li Zefan34d52cb2011-03-29 13:46:06 +08001625 spin_lock(&ctl->tree_lock);
Li Zefan120d66e2010-11-09 14:56:50 +08001626
Li Zefan34d52cb2011-03-29 13:46:06 +08001627 if (try_merge_free_space(ctl, info, true))
Li Zefan120d66e2010-11-09 14:56:50 +08001628 goto link;
1629
1630 /*
1631 * There was no extent directly to the left or right of this new
1632 * extent then we know we're going to have to allocate a new extent, so
1633 * before we do that see if we need to drop this into a bitmap
1634 */
Li Zefan34d52cb2011-03-29 13:46:06 +08001635 ret = insert_into_bitmap(ctl, info);
Li Zefan120d66e2010-11-09 14:56:50 +08001636 if (ret < 0) {
1637 goto out;
1638 } else if (ret) {
1639 ret = 0;
1640 goto out;
1641 }
1642link:
Li Zefan34d52cb2011-03-29 13:46:06 +08001643 ret = link_free_space(ctl, info);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001644 if (ret)
Josef Bacikdc89e982011-01-28 17:05:48 -05001645 kmem_cache_free(btrfs_free_space_cachep, info);
Josef Bacik96303082009-07-13 21:29:25 -04001646out:
Li Zefan34d52cb2011-03-29 13:46:06 +08001647 spin_unlock(&ctl->tree_lock);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001648
Josef Bacik0f9dd462008-09-23 13:14:11 -04001649 if (ret) {
Josef Bacik96303082009-07-13 21:29:25 -04001650 printk(KERN_CRIT "btrfs: unable to add free space :%d\n", ret);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04001651 BUG_ON(ret == -EEXIST);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001652 }
1653
Josef Bacik0f9dd462008-09-23 13:14:11 -04001654 return ret;
1655}
1656
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001657int btrfs_remove_free_space(struct btrfs_block_group_cache *block_group,
1658 u64 offset, u64 bytes)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001659{
Li Zefan34d52cb2011-03-29 13:46:06 +08001660 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001661 struct btrfs_free_space *info;
Josef Bacik96303082009-07-13 21:29:25 -04001662 struct btrfs_free_space *next_info = NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001663 int ret = 0;
1664
Li Zefan34d52cb2011-03-29 13:46:06 +08001665 spin_lock(&ctl->tree_lock);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001666
Josef Bacik96303082009-07-13 21:29:25 -04001667again:
Li Zefan34d52cb2011-03-29 13:46:06 +08001668 info = tree_search_offset(ctl, offset, 0, 0);
Josef Bacik96303082009-07-13 21:29:25 -04001669 if (!info) {
Josef Bacik6606bb92009-07-31 11:03:58 -04001670 /*
1671 * oops didn't find an extent that matched the space we wanted
1672 * to remove, look for a bitmap instead
1673 */
Li Zefan34d52cb2011-03-29 13:46:06 +08001674 info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
Josef Bacik6606bb92009-07-31 11:03:58 -04001675 1, 0);
1676 if (!info) {
1677 WARN_ON(1);
1678 goto out_lock;
1679 }
Josef Bacik96303082009-07-13 21:29:25 -04001680 }
1681
1682 if (info->bytes < bytes && rb_next(&info->offset_index)) {
1683 u64 end;
1684 next_info = rb_entry(rb_next(&info->offset_index),
1685 struct btrfs_free_space,
1686 offset_index);
1687
1688 if (next_info->bitmap)
Li Zefan34d52cb2011-03-29 13:46:06 +08001689 end = next_info->offset +
1690 BITS_PER_BITMAP * ctl->unit - 1;
Josef Bacik96303082009-07-13 21:29:25 -04001691 else
1692 end = next_info->offset + next_info->bytes;
1693
1694 if (next_info->bytes < bytes ||
1695 next_info->offset > offset || offset > end) {
1696 printk(KERN_CRIT "Found free space at %llu, size %llu,"
1697 " trying to use %llu\n",
1698 (unsigned long long)info->offset,
1699 (unsigned long long)info->bytes,
1700 (unsigned long long)bytes);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001701 WARN_ON(1);
1702 ret = -EINVAL;
Josef Bacik96303082009-07-13 21:29:25 -04001703 goto out_lock;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001704 }
Josef Bacik96303082009-07-13 21:29:25 -04001705
1706 info = next_info;
1707 }
1708
1709 if (info->bytes == bytes) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001710 unlink_free_space(ctl, info);
Josef Bacik96303082009-07-13 21:29:25 -04001711 if (info->bitmap) {
1712 kfree(info->bitmap);
Li Zefan34d52cb2011-03-29 13:46:06 +08001713 ctl->total_bitmaps--;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001714 }
Josef Bacikdc89e982011-01-28 17:05:48 -05001715 kmem_cache_free(btrfs_free_space_cachep, info);
Josef Bacik96303082009-07-13 21:29:25 -04001716 goto out_lock;
1717 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04001718
Josef Bacik96303082009-07-13 21:29:25 -04001719 if (!info->bitmap && info->offset == offset) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001720 unlink_free_space(ctl, info);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001721 info->offset += bytes;
1722 info->bytes -= bytes;
Li Zefan34d52cb2011-03-29 13:46:06 +08001723 link_free_space(ctl, info);
Josef Bacik96303082009-07-13 21:29:25 -04001724 goto out_lock;
1725 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04001726
Josef Bacik96303082009-07-13 21:29:25 -04001727 if (!info->bitmap && info->offset <= offset &&
1728 info->offset + info->bytes >= offset + bytes) {
Chris Mason9b49c9b2008-09-24 11:23:25 -04001729 u64 old_start = info->offset;
1730 /*
1731 * we're freeing space in the middle of the info,
1732 * this can happen during tree log replay
1733 *
1734 * first unlink the old info and then
1735 * insert it again after the hole we're creating
1736 */
Li Zefan34d52cb2011-03-29 13:46:06 +08001737 unlink_free_space(ctl, info);
Chris Mason9b49c9b2008-09-24 11:23:25 -04001738 if (offset + bytes < info->offset + info->bytes) {
1739 u64 old_end = info->offset + info->bytes;
1740
1741 info->offset = offset + bytes;
1742 info->bytes = old_end - info->offset;
Li Zefan34d52cb2011-03-29 13:46:06 +08001743 ret = link_free_space(ctl, info);
Josef Bacik96303082009-07-13 21:29:25 -04001744 WARN_ON(ret);
1745 if (ret)
1746 goto out_lock;
Chris Mason9b49c9b2008-09-24 11:23:25 -04001747 } else {
1748 /* the hole we're creating ends at the end
1749 * of the info struct, just free the info
1750 */
Josef Bacikdc89e982011-01-28 17:05:48 -05001751 kmem_cache_free(btrfs_free_space_cachep, info);
Chris Mason9b49c9b2008-09-24 11:23:25 -04001752 }
Li Zefan34d52cb2011-03-29 13:46:06 +08001753 spin_unlock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04001754
1755 /* step two, insert a new info struct to cover
1756 * anything before the hole
Chris Mason9b49c9b2008-09-24 11:23:25 -04001757 */
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001758 ret = btrfs_add_free_space(block_group, old_start,
1759 offset - old_start);
Josef Bacik96303082009-07-13 21:29:25 -04001760 WARN_ON(ret);
1761 goto out;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001762 }
Josef Bacik96303082009-07-13 21:29:25 -04001763
Li Zefan34d52cb2011-03-29 13:46:06 +08001764 ret = remove_from_bitmap(ctl, info, &offset, &bytes);
Josef Bacik96303082009-07-13 21:29:25 -04001765 if (ret == -EAGAIN)
1766 goto again;
1767 BUG_ON(ret);
1768out_lock:
Li Zefan34d52cb2011-03-29 13:46:06 +08001769 spin_unlock(&ctl->tree_lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001770out:
Josef Bacik25179202008-10-29 14:49:05 -04001771 return ret;
1772}
1773
Josef Bacik0f9dd462008-09-23 13:14:11 -04001774void btrfs_dump_free_space(struct btrfs_block_group_cache *block_group,
1775 u64 bytes)
1776{
Li Zefan34d52cb2011-03-29 13:46:06 +08001777 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001778 struct btrfs_free_space *info;
1779 struct rb_node *n;
1780 int count = 0;
1781
Li Zefan34d52cb2011-03-29 13:46:06 +08001782 for (n = rb_first(&ctl->free_space_offset); n; n = rb_next(n)) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001783 info = rb_entry(n, struct btrfs_free_space, offset_index);
1784 if (info->bytes >= bytes)
1785 count++;
Josef Bacik96303082009-07-13 21:29:25 -04001786 printk(KERN_CRIT "entry offset %llu, bytes %llu, bitmap %s\n",
Joel Becker21380932009-04-21 12:38:29 -07001787 (unsigned long long)info->offset,
Josef Bacik96303082009-07-13 21:29:25 -04001788 (unsigned long long)info->bytes,
1789 (info->bitmap) ? "yes" : "no");
Josef Bacik0f9dd462008-09-23 13:14:11 -04001790 }
Josef Bacik96303082009-07-13 21:29:25 -04001791 printk(KERN_INFO "block group has cluster?: %s\n",
1792 list_empty(&block_group->cluster_list) ? "no" : "yes");
Josef Bacik0f9dd462008-09-23 13:14:11 -04001793 printk(KERN_INFO "%d blocks of free space at or bigger than bytes is"
1794 "\n", count);
1795}
1796
Li Zefan34d52cb2011-03-29 13:46:06 +08001797void btrfs_init_free_space_ctl(struct btrfs_block_group_cache *block_group)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001798{
Li Zefan34d52cb2011-03-29 13:46:06 +08001799 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001800
Li Zefan34d52cb2011-03-29 13:46:06 +08001801 spin_lock_init(&ctl->tree_lock);
1802 ctl->unit = block_group->sectorsize;
1803 ctl->start = block_group->key.objectid;
1804 ctl->private = block_group;
1805 ctl->op = &free_space_op;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001806
Li Zefan34d52cb2011-03-29 13:46:06 +08001807 /*
1808 * we only want to have 32k of ram per block group for keeping
1809 * track of free space, and if we pass 1/2 of that we want to
1810 * start converting things over to using bitmaps
1811 */
1812 ctl->extents_thresh = ((1024 * 32) / 2) /
1813 sizeof(struct btrfs_free_space);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001814}
1815
Chris Masonfa9c0d792009-04-03 09:47:43 -04001816/*
1817 * for a given cluster, put all of its extents back into the free
1818 * space cache. If the block group passed doesn't match the block group
1819 * pointed to by the cluster, someone else raced in and freed the
1820 * cluster already. In that case, we just return without changing anything
1821 */
1822static int
1823__btrfs_return_cluster_to_free_space(
1824 struct btrfs_block_group_cache *block_group,
1825 struct btrfs_free_cluster *cluster)
1826{
Li Zefan34d52cb2011-03-29 13:46:06 +08001827 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04001828 struct btrfs_free_space *entry;
1829 struct rb_node *node;
1830
1831 spin_lock(&cluster->lock);
1832 if (cluster->block_group != block_group)
1833 goto out;
1834
Josef Bacik96303082009-07-13 21:29:25 -04001835 cluster->block_group = NULL;
Chris Masonfa9c0d792009-04-03 09:47:43 -04001836 cluster->window_start = 0;
Josef Bacik96303082009-07-13 21:29:25 -04001837 list_del_init(&cluster->block_group_list);
Josef Bacik96303082009-07-13 21:29:25 -04001838
Chris Masonfa9c0d792009-04-03 09:47:43 -04001839 node = rb_first(&cluster->root);
Josef Bacik96303082009-07-13 21:29:25 -04001840 while (node) {
Josef Bacik4e69b592011-03-21 10:11:24 -04001841 bool bitmap;
1842
Chris Masonfa9c0d792009-04-03 09:47:43 -04001843 entry = rb_entry(node, struct btrfs_free_space, offset_index);
1844 node = rb_next(&entry->offset_index);
1845 rb_erase(&entry->offset_index, &cluster->root);
Josef Bacik4e69b592011-03-21 10:11:24 -04001846
1847 bitmap = (entry->bitmap != NULL);
1848 if (!bitmap)
Li Zefan34d52cb2011-03-29 13:46:06 +08001849 try_merge_free_space(ctl, entry, false);
1850 tree_insert_offset(&ctl->free_space_offset,
Josef Bacik4e69b592011-03-21 10:11:24 -04001851 entry->offset, &entry->offset_index, bitmap);
Chris Masonfa9c0d792009-04-03 09:47:43 -04001852 }
Eric Paris6bef4d32010-02-23 19:43:04 +00001853 cluster->root = RB_ROOT;
Josef Bacik96303082009-07-13 21:29:25 -04001854
Chris Masonfa9c0d792009-04-03 09:47:43 -04001855out:
1856 spin_unlock(&cluster->lock);
Josef Bacik96303082009-07-13 21:29:25 -04001857 btrfs_put_block_group(block_group);
Chris Masonfa9c0d792009-04-03 09:47:43 -04001858 return 0;
1859}
1860
Chris Mason09655372011-05-21 09:27:38 -04001861void __btrfs_remove_free_space_cache_locked(struct btrfs_free_space_ctl *ctl)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001862{
1863 struct btrfs_free_space *info;
1864 struct rb_node *node;
Li Zefan581bb052011-04-20 10:06:11 +08001865
Li Zefan581bb052011-04-20 10:06:11 +08001866 while ((node = rb_last(&ctl->free_space_offset)) != NULL) {
1867 info = rb_entry(node, struct btrfs_free_space, offset_index);
Josef Bacik9b90f512011-06-24 16:02:51 +00001868 if (!info->bitmap) {
1869 unlink_free_space(ctl, info);
1870 kmem_cache_free(btrfs_free_space_cachep, info);
1871 } else {
1872 free_bitmap(ctl, info);
1873 }
Li Zefan581bb052011-04-20 10:06:11 +08001874 if (need_resched()) {
1875 spin_unlock(&ctl->tree_lock);
1876 cond_resched();
1877 spin_lock(&ctl->tree_lock);
1878 }
1879 }
Chris Mason09655372011-05-21 09:27:38 -04001880}
1881
1882void __btrfs_remove_free_space_cache(struct btrfs_free_space_ctl *ctl)
1883{
1884 spin_lock(&ctl->tree_lock);
1885 __btrfs_remove_free_space_cache_locked(ctl);
Li Zefan581bb052011-04-20 10:06:11 +08001886 spin_unlock(&ctl->tree_lock);
1887}
1888
Josef Bacik0f9dd462008-09-23 13:14:11 -04001889void btrfs_remove_free_space_cache(struct btrfs_block_group_cache *block_group)
1890{
Li Zefan34d52cb2011-03-29 13:46:06 +08001891 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04001892 struct btrfs_free_cluster *cluster;
Josef Bacik96303082009-07-13 21:29:25 -04001893 struct list_head *head;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001894
Li Zefan34d52cb2011-03-29 13:46:06 +08001895 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04001896 while ((head = block_group->cluster_list.next) !=
1897 &block_group->cluster_list) {
1898 cluster = list_entry(head, struct btrfs_free_cluster,
1899 block_group_list);
Chris Masonfa9c0d792009-04-03 09:47:43 -04001900
1901 WARN_ON(cluster->block_group != block_group);
1902 __btrfs_return_cluster_to_free_space(block_group, cluster);
Josef Bacik96303082009-07-13 21:29:25 -04001903 if (need_resched()) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001904 spin_unlock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04001905 cond_resched();
Li Zefan34d52cb2011-03-29 13:46:06 +08001906 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04001907 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04001908 }
Chris Mason09655372011-05-21 09:27:38 -04001909 __btrfs_remove_free_space_cache_locked(ctl);
Li Zefan34d52cb2011-03-29 13:46:06 +08001910 spin_unlock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04001911
Josef Bacik0f9dd462008-09-23 13:14:11 -04001912}
1913
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001914u64 btrfs_find_space_for_alloc(struct btrfs_block_group_cache *block_group,
1915 u64 offset, u64 bytes, u64 empty_size)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001916{
Li Zefan34d52cb2011-03-29 13:46:06 +08001917 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001918 struct btrfs_free_space *entry = NULL;
Josef Bacik96303082009-07-13 21:29:25 -04001919 u64 bytes_search = bytes + empty_size;
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001920 u64 ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001921
Li Zefan34d52cb2011-03-29 13:46:06 +08001922 spin_lock(&ctl->tree_lock);
1923 entry = find_free_space(ctl, &offset, &bytes_search);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001924 if (!entry)
Josef Bacik96303082009-07-13 21:29:25 -04001925 goto out;
1926
1927 ret = offset;
1928 if (entry->bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001929 bitmap_clear_bits(ctl, entry, offset, bytes);
Li Zefanedf6e2d2010-11-09 14:50:07 +08001930 if (!entry->bytes)
Li Zefan34d52cb2011-03-29 13:46:06 +08001931 free_bitmap(ctl, entry);
Josef Bacik96303082009-07-13 21:29:25 -04001932 } else {
Li Zefan34d52cb2011-03-29 13:46:06 +08001933 unlink_free_space(ctl, entry);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001934 entry->offset += bytes;
1935 entry->bytes -= bytes;
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001936 if (!entry->bytes)
Josef Bacikdc89e982011-01-28 17:05:48 -05001937 kmem_cache_free(btrfs_free_space_cachep, entry);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001938 else
Li Zefan34d52cb2011-03-29 13:46:06 +08001939 link_free_space(ctl, entry);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001940 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04001941
Josef Bacik96303082009-07-13 21:29:25 -04001942out:
Li Zefan34d52cb2011-03-29 13:46:06 +08001943 spin_unlock(&ctl->tree_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04001944
Josef Bacik0f9dd462008-09-23 13:14:11 -04001945 return ret;
1946}
Chris Masonfa9c0d792009-04-03 09:47:43 -04001947
1948/*
1949 * given a cluster, put all of its extents back into the free space
1950 * cache. If a block group is passed, this function will only free
1951 * a cluster that belongs to the passed block group.
1952 *
1953 * Otherwise, it'll get a reference on the block group pointed to by the
1954 * cluster and remove the cluster from it.
1955 */
1956int btrfs_return_cluster_to_free_space(
1957 struct btrfs_block_group_cache *block_group,
1958 struct btrfs_free_cluster *cluster)
1959{
Li Zefan34d52cb2011-03-29 13:46:06 +08001960 struct btrfs_free_space_ctl *ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04001961 int ret;
1962
1963 /* first, get a safe pointer to the block group */
1964 spin_lock(&cluster->lock);
1965 if (!block_group) {
1966 block_group = cluster->block_group;
1967 if (!block_group) {
1968 spin_unlock(&cluster->lock);
1969 return 0;
1970 }
1971 } else if (cluster->block_group != block_group) {
1972 /* someone else has already freed it don't redo their work */
1973 spin_unlock(&cluster->lock);
1974 return 0;
1975 }
1976 atomic_inc(&block_group->count);
1977 spin_unlock(&cluster->lock);
1978
Li Zefan34d52cb2011-03-29 13:46:06 +08001979 ctl = block_group->free_space_ctl;
1980
Chris Masonfa9c0d792009-04-03 09:47:43 -04001981 /* now return any extents the cluster had on it */
Li Zefan34d52cb2011-03-29 13:46:06 +08001982 spin_lock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04001983 ret = __btrfs_return_cluster_to_free_space(block_group, cluster);
Li Zefan34d52cb2011-03-29 13:46:06 +08001984 spin_unlock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04001985
1986 /* finally drop our ref */
1987 btrfs_put_block_group(block_group);
1988 return ret;
1989}
1990
Josef Bacik96303082009-07-13 21:29:25 -04001991static u64 btrfs_alloc_from_bitmap(struct btrfs_block_group_cache *block_group,
1992 struct btrfs_free_cluster *cluster,
Josef Bacik4e69b592011-03-21 10:11:24 -04001993 struct btrfs_free_space *entry,
Josef Bacik96303082009-07-13 21:29:25 -04001994 u64 bytes, u64 min_start)
1995{
Li Zefan34d52cb2011-03-29 13:46:06 +08001996 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik96303082009-07-13 21:29:25 -04001997 int err;
1998 u64 search_start = cluster->window_start;
1999 u64 search_bytes = bytes;
2000 u64 ret = 0;
2001
Josef Bacik96303082009-07-13 21:29:25 -04002002 search_start = min_start;
2003 search_bytes = bytes;
2004
Li Zefan34d52cb2011-03-29 13:46:06 +08002005 err = search_bitmap(ctl, entry, &search_start, &search_bytes);
Josef Bacik96303082009-07-13 21:29:25 -04002006 if (err)
Josef Bacik4e69b592011-03-21 10:11:24 -04002007 return 0;
Josef Bacik96303082009-07-13 21:29:25 -04002008
2009 ret = search_start;
Miao Xiebb3ac5a2011-08-05 09:32:35 +00002010 __bitmap_clear_bits(ctl, entry, ret, bytes);
Josef Bacik96303082009-07-13 21:29:25 -04002011
2012 return ret;
2013}
2014
Chris Masonfa9c0d792009-04-03 09:47:43 -04002015/*
2016 * given a cluster, try to allocate 'bytes' from it, returns 0
2017 * if it couldn't find anything suitably large, or a logical disk offset
2018 * if things worked out
2019 */
2020u64 btrfs_alloc_from_cluster(struct btrfs_block_group_cache *block_group,
2021 struct btrfs_free_cluster *cluster, u64 bytes,
2022 u64 min_start)
2023{
Li Zefan34d52cb2011-03-29 13:46:06 +08002024 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002025 struct btrfs_free_space *entry = NULL;
2026 struct rb_node *node;
2027 u64 ret = 0;
2028
2029 spin_lock(&cluster->lock);
2030 if (bytes > cluster->max_size)
2031 goto out;
2032
2033 if (cluster->block_group != block_group)
2034 goto out;
2035
2036 node = rb_first(&cluster->root);
2037 if (!node)
2038 goto out;
2039
2040 entry = rb_entry(node, struct btrfs_free_space, offset_index);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002041 while(1) {
Josef Bacik4e69b592011-03-21 10:11:24 -04002042 if (entry->bytes < bytes ||
2043 (!entry->bitmap && entry->offset < min_start)) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04002044 node = rb_next(&entry->offset_index);
2045 if (!node)
2046 break;
2047 entry = rb_entry(node, struct btrfs_free_space,
2048 offset_index);
2049 continue;
2050 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04002051
Josef Bacik4e69b592011-03-21 10:11:24 -04002052 if (entry->bitmap) {
2053 ret = btrfs_alloc_from_bitmap(block_group,
2054 cluster, entry, bytes,
2055 min_start);
2056 if (ret == 0) {
Josef Bacik4e69b592011-03-21 10:11:24 -04002057 node = rb_next(&entry->offset_index);
2058 if (!node)
2059 break;
2060 entry = rb_entry(node, struct btrfs_free_space,
2061 offset_index);
2062 continue;
2063 }
2064 } else {
Josef Bacik4e69b592011-03-21 10:11:24 -04002065 ret = entry->offset;
2066
2067 entry->offset += bytes;
2068 entry->bytes -= bytes;
2069 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04002070
Li Zefan5e71b5d2010-11-09 14:55:34 +08002071 if (entry->bytes == 0)
Chris Masonfa9c0d792009-04-03 09:47:43 -04002072 rb_erase(&entry->offset_index, &cluster->root);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002073 break;
2074 }
2075out:
2076 spin_unlock(&cluster->lock);
Josef Bacik96303082009-07-13 21:29:25 -04002077
Li Zefan5e71b5d2010-11-09 14:55:34 +08002078 if (!ret)
2079 return 0;
2080
Li Zefan34d52cb2011-03-29 13:46:06 +08002081 spin_lock(&ctl->tree_lock);
Li Zefan5e71b5d2010-11-09 14:55:34 +08002082
Li Zefan34d52cb2011-03-29 13:46:06 +08002083 ctl->free_space -= bytes;
Li Zefan5e71b5d2010-11-09 14:55:34 +08002084 if (entry->bytes == 0) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002085 ctl->free_extents--;
Josef Bacik4e69b592011-03-21 10:11:24 -04002086 if (entry->bitmap) {
2087 kfree(entry->bitmap);
Li Zefan34d52cb2011-03-29 13:46:06 +08002088 ctl->total_bitmaps--;
2089 ctl->op->recalc_thresholds(ctl);
Josef Bacik4e69b592011-03-21 10:11:24 -04002090 }
Josef Bacikdc89e982011-01-28 17:05:48 -05002091 kmem_cache_free(btrfs_free_space_cachep, entry);
Li Zefan5e71b5d2010-11-09 14:55:34 +08002092 }
2093
Li Zefan34d52cb2011-03-29 13:46:06 +08002094 spin_unlock(&ctl->tree_lock);
Li Zefan5e71b5d2010-11-09 14:55:34 +08002095
Chris Masonfa9c0d792009-04-03 09:47:43 -04002096 return ret;
2097}
2098
Josef Bacik96303082009-07-13 21:29:25 -04002099static int btrfs_bitmap_cluster(struct btrfs_block_group_cache *block_group,
2100 struct btrfs_free_space *entry,
2101 struct btrfs_free_cluster *cluster,
2102 u64 offset, u64 bytes, u64 min_bytes)
2103{
Li Zefan34d52cb2011-03-29 13:46:06 +08002104 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik96303082009-07-13 21:29:25 -04002105 unsigned long next_zero;
2106 unsigned long i;
2107 unsigned long search_bits;
2108 unsigned long total_bits;
2109 unsigned long found_bits;
2110 unsigned long start = 0;
2111 unsigned long total_found = 0;
Josef Bacik4e69b592011-03-21 10:11:24 -04002112 int ret;
Josef Bacik96303082009-07-13 21:29:25 -04002113 bool found = false;
2114
2115 i = offset_to_bit(entry->offset, block_group->sectorsize,
2116 max_t(u64, offset, entry->offset));
Josef Bacikd0a365e2011-03-18 15:27:43 -04002117 search_bits = bytes_to_bits(bytes, block_group->sectorsize);
2118 total_bits = bytes_to_bits(min_bytes, block_group->sectorsize);
Josef Bacik96303082009-07-13 21:29:25 -04002119
2120again:
2121 found_bits = 0;
2122 for (i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, i);
2123 i < BITS_PER_BITMAP;
2124 i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, i + 1)) {
2125 next_zero = find_next_zero_bit(entry->bitmap,
2126 BITS_PER_BITMAP, i);
2127 if (next_zero - i >= search_bits) {
2128 found_bits = next_zero - i;
2129 break;
2130 }
2131 i = next_zero;
2132 }
2133
2134 if (!found_bits)
Josef Bacik4e69b592011-03-21 10:11:24 -04002135 return -ENOSPC;
Josef Bacik96303082009-07-13 21:29:25 -04002136
2137 if (!found) {
2138 start = i;
2139 found = true;
2140 }
2141
2142 total_found += found_bits;
2143
2144 if (cluster->max_size < found_bits * block_group->sectorsize)
2145 cluster->max_size = found_bits * block_group->sectorsize;
2146
2147 if (total_found < total_bits) {
2148 i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, next_zero);
2149 if (i - start > total_bits * 2) {
2150 total_found = 0;
2151 cluster->max_size = 0;
2152 found = false;
2153 }
2154 goto again;
2155 }
2156
2157 cluster->window_start = start * block_group->sectorsize +
2158 entry->offset;
Li Zefan34d52cb2011-03-29 13:46:06 +08002159 rb_erase(&entry->offset_index, &ctl->free_space_offset);
Josef Bacik4e69b592011-03-21 10:11:24 -04002160 ret = tree_insert_offset(&cluster->root, entry->offset,
2161 &entry->offset_index, 1);
2162 BUG_ON(ret);
Josef Bacik96303082009-07-13 21:29:25 -04002163
2164 return 0;
2165}
2166
Chris Masonfa9c0d792009-04-03 09:47:43 -04002167/*
Josef Bacik4e69b592011-03-21 10:11:24 -04002168 * This searches the block group for just extents to fill the cluster with.
2169 */
Josef Bacik3de85bb2011-05-25 13:07:37 -04002170static noinline int
2171setup_cluster_no_bitmap(struct btrfs_block_group_cache *block_group,
2172 struct btrfs_free_cluster *cluster,
2173 struct list_head *bitmaps, u64 offset, u64 bytes,
2174 u64 min_bytes)
Josef Bacik4e69b592011-03-21 10:11:24 -04002175{
Li Zefan34d52cb2011-03-29 13:46:06 +08002176 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik4e69b592011-03-21 10:11:24 -04002177 struct btrfs_free_space *first = NULL;
2178 struct btrfs_free_space *entry = NULL;
2179 struct btrfs_free_space *prev = NULL;
2180 struct btrfs_free_space *last;
2181 struct rb_node *node;
2182 u64 window_start;
2183 u64 window_free;
2184 u64 max_extent;
2185 u64 max_gap = 128 * 1024;
2186
Li Zefan34d52cb2011-03-29 13:46:06 +08002187 entry = tree_search_offset(ctl, offset, 0, 1);
Josef Bacik4e69b592011-03-21 10:11:24 -04002188 if (!entry)
2189 return -ENOSPC;
2190
2191 /*
2192 * We don't want bitmaps, so just move along until we find a normal
2193 * extent entry.
2194 */
2195 while (entry->bitmap) {
Josef Bacik86d4a772011-05-25 13:03:16 -04002196 if (list_empty(&entry->list))
2197 list_add_tail(&entry->list, bitmaps);
Josef Bacik4e69b592011-03-21 10:11:24 -04002198 node = rb_next(&entry->offset_index);
2199 if (!node)
2200 return -ENOSPC;
2201 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2202 }
2203
2204 window_start = entry->offset;
2205 window_free = entry->bytes;
2206 max_extent = entry->bytes;
2207 first = entry;
2208 last = entry;
2209 prev = entry;
2210
2211 while (window_free <= min_bytes) {
2212 node = rb_next(&entry->offset_index);
2213 if (!node)
2214 return -ENOSPC;
2215 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2216
Josef Bacik86d4a772011-05-25 13:03:16 -04002217 if (entry->bitmap) {
2218 if (list_empty(&entry->list))
2219 list_add_tail(&entry->list, bitmaps);
Josef Bacik4e69b592011-03-21 10:11:24 -04002220 continue;
Josef Bacik86d4a772011-05-25 13:03:16 -04002221 }
2222
Josef Bacik4e69b592011-03-21 10:11:24 -04002223 /*
2224 * we haven't filled the empty size and the window is
2225 * very large. reset and try again
2226 */
2227 if (entry->offset - (prev->offset + prev->bytes) > max_gap ||
2228 entry->offset - window_start > (min_bytes * 2)) {
2229 first = entry;
2230 window_start = entry->offset;
2231 window_free = entry->bytes;
2232 last = entry;
2233 max_extent = entry->bytes;
2234 } else {
2235 last = entry;
2236 window_free += entry->bytes;
2237 if (entry->bytes > max_extent)
2238 max_extent = entry->bytes;
2239 }
2240 prev = entry;
2241 }
2242
2243 cluster->window_start = first->offset;
2244
2245 node = &first->offset_index;
2246
2247 /*
2248 * now we've found our entries, pull them out of the free space
2249 * cache and put them into the cluster rbtree
2250 */
2251 do {
2252 int ret;
2253
2254 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2255 node = rb_next(&entry->offset_index);
2256 if (entry->bitmap)
2257 continue;
2258
Li Zefan34d52cb2011-03-29 13:46:06 +08002259 rb_erase(&entry->offset_index, &ctl->free_space_offset);
Josef Bacik4e69b592011-03-21 10:11:24 -04002260 ret = tree_insert_offset(&cluster->root, entry->offset,
2261 &entry->offset_index, 0);
2262 BUG_ON(ret);
2263 } while (node && entry != last);
2264
2265 cluster->max_size = max_extent;
2266
2267 return 0;
2268}
2269
2270/*
2271 * This specifically looks for bitmaps that may work in the cluster, we assume
2272 * that we have already failed to find extents that will work.
2273 */
Josef Bacik3de85bb2011-05-25 13:07:37 -04002274static noinline int
2275setup_cluster_bitmap(struct btrfs_block_group_cache *block_group,
2276 struct btrfs_free_cluster *cluster,
2277 struct list_head *bitmaps, u64 offset, u64 bytes,
2278 u64 min_bytes)
Josef Bacik4e69b592011-03-21 10:11:24 -04002279{
Li Zefan34d52cb2011-03-29 13:46:06 +08002280 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik4e69b592011-03-21 10:11:24 -04002281 struct btrfs_free_space *entry;
2282 struct rb_node *node;
2283 int ret = -ENOSPC;
2284
Li Zefan34d52cb2011-03-29 13:46:06 +08002285 if (ctl->total_bitmaps == 0)
Josef Bacik4e69b592011-03-21 10:11:24 -04002286 return -ENOSPC;
2287
Josef Bacik86d4a772011-05-25 13:03:16 -04002288 /*
2289 * First check our cached list of bitmaps and see if there is an entry
2290 * here that will work.
2291 */
2292 list_for_each_entry(entry, bitmaps, list) {
2293 if (entry->bytes < min_bytes)
2294 continue;
2295 ret = btrfs_bitmap_cluster(block_group, entry, cluster, offset,
2296 bytes, min_bytes);
2297 if (!ret)
2298 return 0;
2299 }
2300
2301 /*
2302 * If we do have entries on our list and we are here then we didn't find
2303 * anything, so go ahead and get the next entry after the last entry in
2304 * this list and start the search from there.
2305 */
2306 if (!list_empty(bitmaps)) {
2307 entry = list_entry(bitmaps->prev, struct btrfs_free_space,
2308 list);
2309 node = rb_next(&entry->offset_index);
2310 if (!node)
2311 return -ENOSPC;
2312 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2313 goto search;
2314 }
2315
Li Zefan34d52cb2011-03-29 13:46:06 +08002316 entry = tree_search_offset(ctl, offset_to_bitmap(ctl, offset), 0, 1);
Josef Bacik4e69b592011-03-21 10:11:24 -04002317 if (!entry)
2318 return -ENOSPC;
2319
Josef Bacik86d4a772011-05-25 13:03:16 -04002320search:
Josef Bacik4e69b592011-03-21 10:11:24 -04002321 node = &entry->offset_index;
2322 do {
2323 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2324 node = rb_next(&entry->offset_index);
2325 if (!entry->bitmap)
2326 continue;
2327 if (entry->bytes < min_bytes)
2328 continue;
2329 ret = btrfs_bitmap_cluster(block_group, entry, cluster, offset,
2330 bytes, min_bytes);
2331 } while (ret && node);
2332
2333 return ret;
2334}
2335
2336/*
Chris Masonfa9c0d792009-04-03 09:47:43 -04002337 * here we try to find a cluster of blocks in a block group. The goal
2338 * is to find at least bytes free and up to empty_size + bytes free.
2339 * We might not find them all in one contiguous area.
2340 *
2341 * returns zero and sets up cluster if things worked out, otherwise
2342 * it returns -enospc
2343 */
2344int btrfs_find_space_cluster(struct btrfs_trans_handle *trans,
Chris Mason451d7582009-06-09 20:28:34 -04002345 struct btrfs_root *root,
Chris Masonfa9c0d792009-04-03 09:47:43 -04002346 struct btrfs_block_group_cache *block_group,
2347 struct btrfs_free_cluster *cluster,
2348 u64 offset, u64 bytes, u64 empty_size)
2349{
Li Zefan34d52cb2011-03-29 13:46:06 +08002350 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik86d4a772011-05-25 13:03:16 -04002351 struct list_head bitmaps;
2352 struct btrfs_free_space *entry, *tmp;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002353 u64 min_bytes;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002354 int ret;
2355
2356 /* for metadata, allow allocates with more holes */
Chris Mason451d7582009-06-09 20:28:34 -04002357 if (btrfs_test_opt(root, SSD_SPREAD)) {
2358 min_bytes = bytes + empty_size;
2359 } else if (block_group->flags & BTRFS_BLOCK_GROUP_METADATA) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04002360 /*
2361 * we want to do larger allocations when we are
2362 * flushing out the delayed refs, it helps prevent
2363 * making more work as we go along.
2364 */
2365 if (trans->transaction->delayed_refs.flushing)
2366 min_bytes = max(bytes, (bytes + empty_size) >> 1);
2367 else
2368 min_bytes = max(bytes, (bytes + empty_size) >> 4);
2369 } else
2370 min_bytes = max(bytes, (bytes + empty_size) >> 2);
2371
Li Zefan34d52cb2011-03-29 13:46:06 +08002372 spin_lock(&ctl->tree_lock);
Josef Bacik7d0d2e82011-03-18 15:13:42 -04002373
2374 /*
2375 * If we know we don't have enough space to make a cluster don't even
2376 * bother doing all the work to try and find one.
2377 */
Li Zefan34d52cb2011-03-29 13:46:06 +08002378 if (ctl->free_space < min_bytes) {
2379 spin_unlock(&ctl->tree_lock);
Josef Bacik7d0d2e82011-03-18 15:13:42 -04002380 return -ENOSPC;
2381 }
2382
Chris Masonfa9c0d792009-04-03 09:47:43 -04002383 spin_lock(&cluster->lock);
2384
2385 /* someone already found a cluster, hooray */
2386 if (cluster->block_group) {
2387 ret = 0;
2388 goto out;
2389 }
Josef Bacik4e69b592011-03-21 10:11:24 -04002390
Josef Bacik86d4a772011-05-25 13:03:16 -04002391 INIT_LIST_HEAD(&bitmaps);
2392 ret = setup_cluster_no_bitmap(block_group, cluster, &bitmaps, offset,
2393 bytes, min_bytes);
Josef Bacik4e69b592011-03-21 10:11:24 -04002394 if (ret)
Josef Bacik86d4a772011-05-25 13:03:16 -04002395 ret = setup_cluster_bitmap(block_group, cluster, &bitmaps,
2396 offset, bytes, min_bytes);
2397
2398 /* Clear our temporary list */
2399 list_for_each_entry_safe(entry, tmp, &bitmaps, list)
2400 list_del_init(&entry->list);
Josef Bacik4e69b592011-03-21 10:11:24 -04002401
2402 if (!ret) {
2403 atomic_inc(&block_group->count);
2404 list_add_tail(&cluster->block_group_list,
2405 &block_group->cluster_list);
2406 cluster->block_group = block_group;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002407 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04002408out:
2409 spin_unlock(&cluster->lock);
Li Zefan34d52cb2011-03-29 13:46:06 +08002410 spin_unlock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002411
2412 return ret;
2413}
2414
2415/*
2416 * simple code to zero out a cluster
2417 */
2418void btrfs_init_free_cluster(struct btrfs_free_cluster *cluster)
2419{
2420 spin_lock_init(&cluster->lock);
2421 spin_lock_init(&cluster->refill_lock);
Eric Paris6bef4d32010-02-23 19:43:04 +00002422 cluster->root = RB_ROOT;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002423 cluster->max_size = 0;
2424 INIT_LIST_HEAD(&cluster->block_group_list);
2425 cluster->block_group = NULL;
2426}
2427
Li Dongyangf7039b12011-03-24 10:24:28 +00002428int btrfs_trim_block_group(struct btrfs_block_group_cache *block_group,
2429 u64 *trimmed, u64 start, u64 end, u64 minlen)
2430{
Li Zefan34d52cb2011-03-29 13:46:06 +08002431 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Li Dongyangf7039b12011-03-24 10:24:28 +00002432 struct btrfs_free_space *entry = NULL;
2433 struct btrfs_fs_info *fs_info = block_group->fs_info;
2434 u64 bytes = 0;
2435 u64 actually_trimmed;
2436 int ret = 0;
2437
2438 *trimmed = 0;
2439
2440 while (start < end) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002441 spin_lock(&ctl->tree_lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00002442
Li Zefan34d52cb2011-03-29 13:46:06 +08002443 if (ctl->free_space < minlen) {
2444 spin_unlock(&ctl->tree_lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00002445 break;
2446 }
2447
Li Zefan34d52cb2011-03-29 13:46:06 +08002448 entry = tree_search_offset(ctl, start, 0, 1);
Li Dongyangf7039b12011-03-24 10:24:28 +00002449 if (!entry)
Li Zefan34d52cb2011-03-29 13:46:06 +08002450 entry = tree_search_offset(ctl,
2451 offset_to_bitmap(ctl, start),
Li Dongyangf7039b12011-03-24 10:24:28 +00002452 1, 1);
2453
2454 if (!entry || entry->offset >= end) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002455 spin_unlock(&ctl->tree_lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00002456 break;
2457 }
2458
2459 if (entry->bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002460 ret = search_bitmap(ctl, entry, &start, &bytes);
Li Dongyangf7039b12011-03-24 10:24:28 +00002461 if (!ret) {
2462 if (start >= end) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002463 spin_unlock(&ctl->tree_lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00002464 break;
2465 }
2466 bytes = min(bytes, end - start);
Li Zefan34d52cb2011-03-29 13:46:06 +08002467 bitmap_clear_bits(ctl, entry, start, bytes);
Li Dongyangf7039b12011-03-24 10:24:28 +00002468 if (entry->bytes == 0)
Li Zefan34d52cb2011-03-29 13:46:06 +08002469 free_bitmap(ctl, entry);
Li Dongyangf7039b12011-03-24 10:24:28 +00002470 } else {
2471 start = entry->offset + BITS_PER_BITMAP *
2472 block_group->sectorsize;
Li Zefan34d52cb2011-03-29 13:46:06 +08002473 spin_unlock(&ctl->tree_lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00002474 ret = 0;
2475 continue;
2476 }
2477 } else {
2478 start = entry->offset;
2479 bytes = min(entry->bytes, end - start);
Li Zefan34d52cb2011-03-29 13:46:06 +08002480 unlink_free_space(ctl, entry);
Li Zefanf789b682011-04-25 19:43:52 -04002481 kmem_cache_free(btrfs_free_space_cachep, entry);
Li Dongyangf7039b12011-03-24 10:24:28 +00002482 }
2483
Li Zefan34d52cb2011-03-29 13:46:06 +08002484 spin_unlock(&ctl->tree_lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00002485
2486 if (bytes >= minlen) {
Josef Bacikfb25e912011-07-26 17:00:46 -04002487 struct btrfs_space_info *space_info;
2488 int update = 0;
2489
2490 space_info = block_group->space_info;
2491 spin_lock(&space_info->lock);
2492 spin_lock(&block_group->lock);
2493 if (!block_group->ro) {
2494 block_group->reserved += bytes;
2495 space_info->bytes_reserved += bytes;
2496 update = 1;
2497 }
2498 spin_unlock(&block_group->lock);
2499 spin_unlock(&space_info->lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00002500
2501 ret = btrfs_error_discard_extent(fs_info->extent_root,
2502 start,
2503 bytes,
2504 &actually_trimmed);
2505
Li Zefan34d52cb2011-03-29 13:46:06 +08002506 btrfs_add_free_space(block_group, start, bytes);
Josef Bacikfb25e912011-07-26 17:00:46 -04002507 if (update) {
2508 spin_lock(&space_info->lock);
2509 spin_lock(&block_group->lock);
2510 if (block_group->ro)
2511 space_info->bytes_readonly += bytes;
2512 block_group->reserved -= bytes;
2513 space_info->bytes_reserved -= bytes;
2514 spin_unlock(&space_info->lock);
2515 spin_unlock(&block_group->lock);
2516 }
Li Dongyangf7039b12011-03-24 10:24:28 +00002517
2518 if (ret)
2519 break;
2520 *trimmed += actually_trimmed;
2521 }
2522 start += bytes;
2523 bytes = 0;
2524
2525 if (fatal_signal_pending(current)) {
2526 ret = -ERESTARTSYS;
2527 break;
2528 }
2529
2530 cond_resched();
2531 }
2532
2533 return ret;
2534}
Li Zefan581bb052011-04-20 10:06:11 +08002535
2536/*
2537 * Find the left-most item in the cache tree, and then return the
2538 * smallest inode number in the item.
2539 *
2540 * Note: the returned inode number may not be the smallest one in
2541 * the tree, if the left-most item is a bitmap.
2542 */
2543u64 btrfs_find_ino_for_alloc(struct btrfs_root *fs_root)
2544{
2545 struct btrfs_free_space_ctl *ctl = fs_root->free_ino_ctl;
2546 struct btrfs_free_space *entry = NULL;
2547 u64 ino = 0;
2548
2549 spin_lock(&ctl->tree_lock);
2550
2551 if (RB_EMPTY_ROOT(&ctl->free_space_offset))
2552 goto out;
2553
2554 entry = rb_entry(rb_first(&ctl->free_space_offset),
2555 struct btrfs_free_space, offset_index);
2556
2557 if (!entry->bitmap) {
2558 ino = entry->offset;
2559
2560 unlink_free_space(ctl, entry);
2561 entry->offset++;
2562 entry->bytes--;
2563 if (!entry->bytes)
2564 kmem_cache_free(btrfs_free_space_cachep, entry);
2565 else
2566 link_free_space(ctl, entry);
2567 } else {
2568 u64 offset = 0;
2569 u64 count = 1;
2570 int ret;
2571
2572 ret = search_bitmap(ctl, entry, &offset, &count);
2573 BUG_ON(ret);
2574
2575 ino = offset;
2576 bitmap_clear_bits(ctl, entry, offset, 1);
2577 if (entry->bytes == 0)
2578 free_bitmap(ctl, entry);
2579 }
2580out:
2581 spin_unlock(&ctl->tree_lock);
2582
2583 return ino;
2584}
Li Zefan82d59022011-04-20 10:33:24 +08002585
2586struct inode *lookup_free_ino_inode(struct btrfs_root *root,
2587 struct btrfs_path *path)
2588{
2589 struct inode *inode = NULL;
2590
2591 spin_lock(&root->cache_lock);
2592 if (root->cache_inode)
2593 inode = igrab(root->cache_inode);
2594 spin_unlock(&root->cache_lock);
2595 if (inode)
2596 return inode;
2597
2598 inode = __lookup_free_space_inode(root, path, 0);
2599 if (IS_ERR(inode))
2600 return inode;
2601
2602 spin_lock(&root->cache_lock);
David Sterba7841cb22011-05-31 18:07:27 +02002603 if (!btrfs_fs_closing(root->fs_info))
Li Zefan82d59022011-04-20 10:33:24 +08002604 root->cache_inode = igrab(inode);
2605 spin_unlock(&root->cache_lock);
2606
2607 return inode;
2608}
2609
2610int create_free_ino_inode(struct btrfs_root *root,
2611 struct btrfs_trans_handle *trans,
2612 struct btrfs_path *path)
2613{
2614 return __create_free_space_inode(root, trans, path,
2615 BTRFS_FREE_INO_OBJECTID, 0);
2616}
2617
2618int load_free_ino_cache(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
2619{
2620 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
2621 struct btrfs_path *path;
2622 struct inode *inode;
2623 int ret = 0;
2624 u64 root_gen = btrfs_root_generation(&root->root_item);
2625
Chris Mason4b9465c2011-06-03 09:36:29 -04002626 if (!btrfs_test_opt(root, INODE_MAP_CACHE))
2627 return 0;
2628
Li Zefan82d59022011-04-20 10:33:24 +08002629 /*
2630 * If we're unmounting then just return, since this does a search on the
2631 * normal root and not the commit root and we could deadlock.
2632 */
David Sterba7841cb22011-05-31 18:07:27 +02002633 if (btrfs_fs_closing(fs_info))
Li Zefan82d59022011-04-20 10:33:24 +08002634 return 0;
2635
2636 path = btrfs_alloc_path();
2637 if (!path)
2638 return 0;
2639
2640 inode = lookup_free_ino_inode(root, path);
2641 if (IS_ERR(inode))
2642 goto out;
2643
2644 if (root_gen != BTRFS_I(inode)->generation)
2645 goto out_put;
2646
2647 ret = __load_free_space_cache(root, inode, ctl, path, 0);
2648
2649 if (ret < 0)
2650 printk(KERN_ERR "btrfs: failed to load free ino cache for "
2651 "root %llu\n", root->root_key.objectid);
2652out_put:
2653 iput(inode);
2654out:
2655 btrfs_free_path(path);
2656 return ret;
2657}
2658
2659int btrfs_write_out_ino_cache(struct btrfs_root *root,
2660 struct btrfs_trans_handle *trans,
2661 struct btrfs_path *path)
2662{
2663 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
2664 struct inode *inode;
2665 int ret;
2666
Chris Mason4b9465c2011-06-03 09:36:29 -04002667 if (!btrfs_test_opt(root, INODE_MAP_CACHE))
2668 return 0;
2669
Li Zefan82d59022011-04-20 10:33:24 +08002670 inode = lookup_free_ino_inode(root, path);
2671 if (IS_ERR(inode))
2672 return 0;
2673
2674 ret = __btrfs_write_out_cache(root, inode, ctl, NULL, trans, path, 0);
Josef Bacikc09544e2011-08-30 10:19:10 -04002675 if (ret) {
2676 btrfs_delalloc_release_metadata(inode, inode->i_size);
2677#ifdef DEBUG
Li Zefan82d59022011-04-20 10:33:24 +08002678 printk(KERN_ERR "btrfs: failed to write free ino cache "
2679 "for root %llu\n", root->root_key.objectid);
Josef Bacikc09544e2011-08-30 10:19:10 -04002680#endif
2681 }
Li Zefan82d59022011-04-20 10:33:24 +08002682
2683 iput(inode);
2684 return ret;
2685}