blob: 5d40c1ed822550985fb2cee80cc1400b239f16e0 [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;
Josef Bacik5b0e95b2011-10-06 08:58:24 -040088 u32 flags = BTRFS_INODE_NODATASUM | BTRFS_INODE_NODATACOW;
Li Zefan0414efa2011-04-20 10:20:14 +080089
90 spin_lock(&block_group->lock);
91 if (block_group->inode)
92 inode = igrab(block_group->inode);
93 spin_unlock(&block_group->lock);
94 if (inode)
95 return inode;
96
97 inode = __lookup_free_space_inode(root, path,
98 block_group->key.objectid);
99 if (IS_ERR(inode))
100 return inode;
101
Josef Bacik0af3d002010-06-21 14:48:16 -0400102 spin_lock(&block_group->lock);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400103 if (!((BTRFS_I(inode)->flags & flags) == flags)) {
Josef Bacik2f356122011-06-10 15:31:13 -0400104 printk(KERN_INFO "Old style space inode found, converting.\n");
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400105 BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM |
106 BTRFS_INODE_NODATACOW;
Josef Bacik2f356122011-06-10 15:31:13 -0400107 block_group->disk_cache_state = BTRFS_DC_CLEAR;
108 }
109
Josef Bacik300e4f82011-08-29 14:06:00 -0400110 if (!block_group->iref) {
Josef Bacik0af3d002010-06-21 14:48:16 -0400111 block_group->inode = igrab(inode);
112 block_group->iref = 1;
113 }
114 spin_unlock(&block_group->lock);
115
116 return inode;
117}
118
Li Zefan0414efa2011-04-20 10:20:14 +0800119int __create_free_space_inode(struct btrfs_root *root,
120 struct btrfs_trans_handle *trans,
121 struct btrfs_path *path, u64 ino, u64 offset)
Josef Bacik0af3d002010-06-21 14:48:16 -0400122{
123 struct btrfs_key key;
124 struct btrfs_disk_key disk_key;
125 struct btrfs_free_space_header *header;
126 struct btrfs_inode_item *inode_item;
127 struct extent_buffer *leaf;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400128 u64 flags = BTRFS_INODE_NOCOMPRESS | BTRFS_INODE_PREALLOC;
Josef Bacik0af3d002010-06-21 14:48:16 -0400129 int ret;
130
Li Zefan0414efa2011-04-20 10:20:14 +0800131 ret = btrfs_insert_empty_inode(trans, root, path, ino);
Josef Bacik0af3d002010-06-21 14:48:16 -0400132 if (ret)
133 return ret;
134
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400135 /* We inline crc's for the free disk space cache */
136 if (ino != BTRFS_FREE_INO_OBJECTID)
137 flags |= BTRFS_INODE_NODATASUM | BTRFS_INODE_NODATACOW;
138
Josef Bacik0af3d002010-06-21 14:48:16 -0400139 leaf = path->nodes[0];
140 inode_item = btrfs_item_ptr(leaf, path->slots[0],
141 struct btrfs_inode_item);
142 btrfs_item_key(leaf, &disk_key, path->slots[0]);
143 memset_extent_buffer(leaf, 0, (unsigned long)inode_item,
144 sizeof(*inode_item));
145 btrfs_set_inode_generation(leaf, inode_item, trans->transid);
146 btrfs_set_inode_size(leaf, inode_item, 0);
147 btrfs_set_inode_nbytes(leaf, inode_item, 0);
148 btrfs_set_inode_uid(leaf, inode_item, 0);
149 btrfs_set_inode_gid(leaf, inode_item, 0);
150 btrfs_set_inode_mode(leaf, inode_item, S_IFREG | 0600);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400151 btrfs_set_inode_flags(leaf, inode_item, flags);
Josef Bacik0af3d002010-06-21 14:48:16 -0400152 btrfs_set_inode_nlink(leaf, inode_item, 1);
153 btrfs_set_inode_transid(leaf, inode_item, trans->transid);
Li Zefan0414efa2011-04-20 10:20:14 +0800154 btrfs_set_inode_block_group(leaf, inode_item, offset);
Josef Bacik0af3d002010-06-21 14:48:16 -0400155 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +0200156 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -0400157
158 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
Li Zefan0414efa2011-04-20 10:20:14 +0800159 key.offset = offset;
Josef Bacik0af3d002010-06-21 14:48:16 -0400160 key.type = 0;
161
162 ret = btrfs_insert_empty_item(trans, root, path, &key,
163 sizeof(struct btrfs_free_space_header));
164 if (ret < 0) {
David Sterbab3b4aa72011-04-21 01:20:15 +0200165 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -0400166 return ret;
167 }
168 leaf = path->nodes[0];
169 header = btrfs_item_ptr(leaf, path->slots[0],
170 struct btrfs_free_space_header);
171 memset_extent_buffer(leaf, 0, (unsigned long)header, sizeof(*header));
172 btrfs_set_free_space_key(leaf, header, &disk_key);
173 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +0200174 btrfs_release_path(path);
Josef Bacik0af3d002010-06-21 14:48:16 -0400175
176 return 0;
177}
178
Li Zefan0414efa2011-04-20 10:20:14 +0800179int create_free_space_inode(struct btrfs_root *root,
180 struct btrfs_trans_handle *trans,
181 struct btrfs_block_group_cache *block_group,
182 struct btrfs_path *path)
183{
184 int ret;
185 u64 ino;
186
187 ret = btrfs_find_free_objectid(root, &ino);
188 if (ret < 0)
189 return ret;
190
191 return __create_free_space_inode(root, trans, path, ino,
192 block_group->key.objectid);
193}
194
Josef Bacik0af3d002010-06-21 14:48:16 -0400195int btrfs_truncate_free_space_cache(struct btrfs_root *root,
196 struct btrfs_trans_handle *trans,
197 struct btrfs_path *path,
198 struct inode *inode)
199{
Liu Bo65450aa2011-09-11 10:52:24 -0400200 struct btrfs_block_rsv *rsv;
Josef Bacik0af3d002010-06-21 14:48:16 -0400201 loff_t oldsize;
202 int ret = 0;
203
Liu Bo65450aa2011-09-11 10:52:24 -0400204 rsv = trans->block_rsv;
Josef Bacik0af3d002010-06-21 14:48:16 -0400205 trans->block_rsv = root->orphan_block_rsv;
Josef Bacik4a92b1b2011-08-30 12:34:28 -0400206 ret = btrfs_block_rsv_check(root, root->orphan_block_rsv, 0, 5, 0);
Josef Bacik0af3d002010-06-21 14:48:16 -0400207 if (ret)
208 return ret;
209
210 oldsize = i_size_read(inode);
211 btrfs_i_size_write(inode, 0);
212 truncate_pagecache(inode, oldsize, 0);
213
214 /*
215 * We don't need an orphan item because truncating the free space cache
216 * will never be split across transactions.
217 */
218 ret = btrfs_truncate_inode_items(trans, root, inode,
219 0, BTRFS_EXTENT_DATA_KEY);
Liu Bo65450aa2011-09-11 10:52:24 -0400220
221 trans->block_rsv = rsv;
Josef Bacik0af3d002010-06-21 14:48:16 -0400222 if (ret) {
223 WARN_ON(1);
224 return ret;
225 }
226
Li Zefan82d59022011-04-20 10:33:24 +0800227 ret = btrfs_update_inode(trans, root, inode);
228 return ret;
Josef Bacik0af3d002010-06-21 14:48:16 -0400229}
230
Josef Bacik9d66e232010-08-25 16:54:15 -0400231static int readahead_cache(struct inode *inode)
232{
233 struct file_ra_state *ra;
234 unsigned long last_index;
235
236 ra = kzalloc(sizeof(*ra), GFP_NOFS);
237 if (!ra)
238 return -ENOMEM;
239
240 file_ra_state_init(ra, inode->i_mapping);
241 last_index = (i_size_read(inode) - 1) >> PAGE_CACHE_SHIFT;
242
243 page_cache_sync_readahead(inode->i_mapping, ra, NULL, 0, last_index);
244
245 kfree(ra);
246
247 return 0;
248}
249
Josef Bacika67509c2011-10-05 15:18:58 -0400250struct io_ctl {
251 void *cur, *orig;
252 struct page *page;
253 struct page **pages;
254 struct btrfs_root *root;
255 unsigned long size;
256 int index;
257 int num_pages;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400258 unsigned check_crcs:1;
Josef Bacika67509c2011-10-05 15:18:58 -0400259};
260
261static int io_ctl_init(struct io_ctl *io_ctl, struct inode *inode,
262 struct btrfs_root *root)
263{
264 memset(io_ctl, 0, sizeof(struct io_ctl));
265 io_ctl->num_pages = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
266 PAGE_CACHE_SHIFT;
267 io_ctl->pages = kzalloc(sizeof(struct page *) * io_ctl->num_pages,
268 GFP_NOFS);
269 if (!io_ctl->pages)
270 return -ENOMEM;
271 io_ctl->root = root;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400272 if (btrfs_ino(inode) != BTRFS_FREE_INO_OBJECTID)
273 io_ctl->check_crcs = 1;
Josef Bacika67509c2011-10-05 15:18:58 -0400274 return 0;
275}
276
277static void io_ctl_free(struct io_ctl *io_ctl)
278{
279 kfree(io_ctl->pages);
280}
281
282static void io_ctl_unmap_page(struct io_ctl *io_ctl)
283{
284 if (io_ctl->cur) {
285 kunmap(io_ctl->page);
286 io_ctl->cur = NULL;
287 io_ctl->orig = NULL;
288 }
289}
290
291static void io_ctl_map_page(struct io_ctl *io_ctl, int clear)
292{
293 WARN_ON(io_ctl->cur);
294 BUG_ON(io_ctl->index >= io_ctl->num_pages);
295 io_ctl->page = io_ctl->pages[io_ctl->index++];
296 io_ctl->cur = kmap(io_ctl->page);
297 io_ctl->orig = io_ctl->cur;
298 io_ctl->size = PAGE_CACHE_SIZE;
299 if (clear)
300 memset(io_ctl->cur, 0, PAGE_CACHE_SIZE);
301}
302
303static void io_ctl_drop_pages(struct io_ctl *io_ctl)
304{
305 int i;
306
307 io_ctl_unmap_page(io_ctl);
308
309 for (i = 0; i < io_ctl->num_pages; i++) {
310 ClearPageChecked(io_ctl->pages[i]);
311 unlock_page(io_ctl->pages[i]);
312 page_cache_release(io_ctl->pages[i]);
313 }
314}
315
316static int io_ctl_prepare_pages(struct io_ctl *io_ctl, struct inode *inode,
317 int uptodate)
318{
319 struct page *page;
320 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
321 int i;
322
323 for (i = 0; i < io_ctl->num_pages; i++) {
324 page = find_or_create_page(inode->i_mapping, i, mask);
325 if (!page) {
326 io_ctl_drop_pages(io_ctl);
327 return -ENOMEM;
328 }
329 io_ctl->pages[i] = page;
330 if (uptodate && !PageUptodate(page)) {
331 btrfs_readpage(NULL, page);
332 lock_page(page);
333 if (!PageUptodate(page)) {
334 printk(KERN_ERR "btrfs: error reading free "
335 "space cache\n");
336 io_ctl_drop_pages(io_ctl);
337 return -EIO;
338 }
339 }
340 }
341
342 return 0;
343}
344
345static void io_ctl_set_generation(struct io_ctl *io_ctl, u64 generation)
346{
347 u64 *val;
348
349 io_ctl_map_page(io_ctl, 1);
350
351 /*
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400352 * Skip the csum areas. If we don't check crcs then we just have a
353 * 64bit chunk at the front of the first page.
Josef Bacika67509c2011-10-05 15:18:58 -0400354 */
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400355 if (io_ctl->check_crcs) {
356 io_ctl->cur += (sizeof(u32) * io_ctl->num_pages);
357 io_ctl->size -= sizeof(u64) + (sizeof(u32) * io_ctl->num_pages);
358 } else {
359 io_ctl->cur += sizeof(u64);
360 io_ctl->size -= sizeof(u64) * 2;
361 }
Josef Bacika67509c2011-10-05 15:18:58 -0400362
363 val = io_ctl->cur;
364 *val = cpu_to_le64(generation);
365 io_ctl->cur += sizeof(u64);
Josef Bacika67509c2011-10-05 15:18:58 -0400366}
367
368static int io_ctl_check_generation(struct io_ctl *io_ctl, u64 generation)
369{
370 u64 *gen;
371
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400372 /*
373 * Skip the crc area. If we don't check crcs then we just have a 64bit
374 * chunk at the front of the first page.
375 */
376 if (io_ctl->check_crcs) {
377 io_ctl->cur += sizeof(u32) * io_ctl->num_pages;
378 io_ctl->size -= sizeof(u64) +
379 (sizeof(u32) * io_ctl->num_pages);
380 } else {
381 io_ctl->cur += sizeof(u64);
382 io_ctl->size -= sizeof(u64) * 2;
383 }
Josef Bacika67509c2011-10-05 15:18:58 -0400384
Josef Bacika67509c2011-10-05 15:18:58 -0400385 gen = io_ctl->cur;
386 if (le64_to_cpu(*gen) != generation) {
387 printk_ratelimited(KERN_ERR "btrfs: space cache generation "
388 "(%Lu) does not match inode (%Lu)\n", *gen,
389 generation);
390 io_ctl_unmap_page(io_ctl);
391 return -EIO;
392 }
393 io_ctl->cur += sizeof(u64);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400394 return 0;
395}
396
397static void io_ctl_set_crc(struct io_ctl *io_ctl, int index)
398{
399 u32 *tmp;
400 u32 crc = ~(u32)0;
401 unsigned offset = 0;
402
403 if (!io_ctl->check_crcs) {
404 io_ctl_unmap_page(io_ctl);
405 return;
406 }
407
408 if (index == 0)
409 offset = sizeof(u32) * io_ctl->num_pages;;
410
411 crc = btrfs_csum_data(io_ctl->root, io_ctl->orig + offset, crc,
412 PAGE_CACHE_SIZE - offset);
413 btrfs_csum_final(crc, (char *)&crc);
414 io_ctl_unmap_page(io_ctl);
415 tmp = kmap(io_ctl->pages[0]);
416 tmp += index;
417 *tmp = crc;
418 kunmap(io_ctl->pages[0]);
419}
420
421static int io_ctl_check_crc(struct io_ctl *io_ctl, int index)
422{
423 u32 *tmp, val;
424 u32 crc = ~(u32)0;
425 unsigned offset = 0;
426
427 if (!io_ctl->check_crcs) {
428 io_ctl_map_page(io_ctl, 0);
429 return 0;
430 }
431
432 if (index == 0)
433 offset = sizeof(u32) * io_ctl->num_pages;
434
435 tmp = kmap(io_ctl->pages[0]);
436 tmp += index;
437 val = *tmp;
438 kunmap(io_ctl->pages[0]);
439
440 io_ctl_map_page(io_ctl, 0);
441 crc = btrfs_csum_data(io_ctl->root, io_ctl->orig + offset, crc,
442 PAGE_CACHE_SIZE - offset);
443 btrfs_csum_final(crc, (char *)&crc);
444 if (val != crc) {
445 printk_ratelimited(KERN_ERR "btrfs: csum mismatch on free "
446 "space cache\n");
447 io_ctl_unmap_page(io_ctl);
448 return -EIO;
449 }
450
Josef Bacika67509c2011-10-05 15:18:58 -0400451 return 0;
452}
453
454static int io_ctl_add_entry(struct io_ctl *io_ctl, u64 offset, u64 bytes,
455 void *bitmap)
456{
457 struct btrfs_free_space_entry *entry;
458
459 if (!io_ctl->cur)
460 return -ENOSPC;
461
462 entry = io_ctl->cur;
463 entry->offset = cpu_to_le64(offset);
464 entry->bytes = cpu_to_le64(bytes);
465 entry->type = (bitmap) ? BTRFS_FREE_SPACE_BITMAP :
466 BTRFS_FREE_SPACE_EXTENT;
467 io_ctl->cur += sizeof(struct btrfs_free_space_entry);
468 io_ctl->size -= sizeof(struct btrfs_free_space_entry);
469
470 if (io_ctl->size >= sizeof(struct btrfs_free_space_entry))
471 return 0;
472
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400473 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
Josef Bacika67509c2011-10-05 15:18:58 -0400474
475 /* No more pages to map */
476 if (io_ctl->index >= io_ctl->num_pages)
477 return 0;
478
479 /* map the next page */
480 io_ctl_map_page(io_ctl, 1);
481 return 0;
482}
483
484static int io_ctl_add_bitmap(struct io_ctl *io_ctl, void *bitmap)
485{
486 if (!io_ctl->cur)
487 return -ENOSPC;
488
489 /*
490 * If we aren't at the start of the current page, unmap this one and
491 * map the next one if there is any left.
492 */
493 if (io_ctl->cur != io_ctl->orig) {
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400494 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
Josef Bacika67509c2011-10-05 15:18:58 -0400495 if (io_ctl->index >= io_ctl->num_pages)
496 return -ENOSPC;
497 io_ctl_map_page(io_ctl, 0);
498 }
499
500 memcpy(io_ctl->cur, bitmap, PAGE_CACHE_SIZE);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400501 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
Josef Bacika67509c2011-10-05 15:18:58 -0400502 if (io_ctl->index < io_ctl->num_pages)
503 io_ctl_map_page(io_ctl, 0);
504 return 0;
505}
506
507static void io_ctl_zero_remaining_pages(struct io_ctl *io_ctl)
508{
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400509 /*
510 * If we're not on the boundary we know we've modified the page and we
511 * need to crc the page.
512 */
513 if (io_ctl->cur != io_ctl->orig)
514 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
515 else
516 io_ctl_unmap_page(io_ctl);
Josef Bacika67509c2011-10-05 15:18:58 -0400517
518 while (io_ctl->index < io_ctl->num_pages) {
519 io_ctl_map_page(io_ctl, 1);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400520 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
Josef Bacika67509c2011-10-05 15:18:58 -0400521 }
522}
523
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400524static int io_ctl_read_entry(struct io_ctl *io_ctl,
525 struct btrfs_free_space *entry, u8 *type)
Josef Bacika67509c2011-10-05 15:18:58 -0400526{
527 struct btrfs_free_space_entry *e;
Josef Bacika67509c2011-10-05 15:18:58 -0400528
529 e = io_ctl->cur;
530 entry->offset = le64_to_cpu(e->offset);
531 entry->bytes = le64_to_cpu(e->bytes);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400532 *type = e->type;
Josef Bacika67509c2011-10-05 15:18:58 -0400533 io_ctl->cur += sizeof(struct btrfs_free_space_entry);
534 io_ctl->size -= sizeof(struct btrfs_free_space_entry);
535
536 if (io_ctl->size >= sizeof(struct btrfs_free_space_entry))
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400537 return 0;
Josef Bacika67509c2011-10-05 15:18:58 -0400538
539 io_ctl_unmap_page(io_ctl);
540
541 if (io_ctl->index >= io_ctl->num_pages)
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400542 return 0;
Josef Bacika67509c2011-10-05 15:18:58 -0400543
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400544 return io_ctl_check_crc(io_ctl, io_ctl->index);
Josef Bacika67509c2011-10-05 15:18:58 -0400545}
546
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400547static int io_ctl_read_bitmap(struct io_ctl *io_ctl,
548 struct btrfs_free_space *entry)
Josef Bacika67509c2011-10-05 15:18:58 -0400549{
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400550 int ret;
551
552 if (io_ctl->cur && io_ctl->cur != io_ctl->orig)
Josef Bacika67509c2011-10-05 15:18:58 -0400553 io_ctl_unmap_page(io_ctl);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400554
555 ret = io_ctl_check_crc(io_ctl, io_ctl->index);
556 if (ret)
557 return ret;
558
Josef Bacika67509c2011-10-05 15:18:58 -0400559 memcpy(entry->bitmap, io_ctl->cur, PAGE_CACHE_SIZE);
560 io_ctl_unmap_page(io_ctl);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400561
562 return 0;
Josef Bacika67509c2011-10-05 15:18:58 -0400563}
564
Li Zefan0414efa2011-04-20 10:20:14 +0800565int __load_free_space_cache(struct btrfs_root *root, struct inode *inode,
566 struct btrfs_free_space_ctl *ctl,
567 struct btrfs_path *path, u64 offset)
Josef Bacik9d66e232010-08-25 16:54:15 -0400568{
Josef Bacik9d66e232010-08-25 16:54:15 -0400569 struct btrfs_free_space_header *header;
570 struct extent_buffer *leaf;
Josef Bacika67509c2011-10-05 15:18:58 -0400571 struct io_ctl io_ctl;
Josef Bacik9d66e232010-08-25 16:54:15 -0400572 struct btrfs_key key;
Josef Bacika67509c2011-10-05 15:18:58 -0400573 struct btrfs_free_space *e, *n;
Josef Bacik9d66e232010-08-25 16:54:15 -0400574 struct list_head bitmaps;
575 u64 num_entries;
576 u64 num_bitmaps;
577 u64 generation;
Josef Bacika67509c2011-10-05 15:18:58 -0400578 u8 type;
Josef Bacikf6a39822011-06-06 10:50:35 -0400579 int ret = 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400580
581 INIT_LIST_HEAD(&bitmaps);
582
Josef Bacik9d66e232010-08-25 16:54:15 -0400583 /* Nothing in the space cache, goodbye */
Li Zefan0414efa2011-04-20 10:20:14 +0800584 if (!i_size_read(inode))
Josef Bacika67509c2011-10-05 15:18:58 -0400585 return 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400586
587 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
Li Zefan0414efa2011-04-20 10:20:14 +0800588 key.offset = offset;
Josef Bacik9d66e232010-08-25 16:54:15 -0400589 key.type = 0;
590
591 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Li Zefan0414efa2011-04-20 10:20:14 +0800592 if (ret < 0)
Josef Bacika67509c2011-10-05 15:18:58 -0400593 return 0;
Li Zefan0414efa2011-04-20 10:20:14 +0800594 else if (ret > 0) {
Chris Mason945d8962011-05-22 12:33:42 -0400595 btrfs_release_path(path);
Josef Bacika67509c2011-10-05 15:18:58 -0400596 return 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400597 }
598
Li Zefan0414efa2011-04-20 10:20:14 +0800599 ret = -1;
600
Josef Bacik9d66e232010-08-25 16:54:15 -0400601 leaf = path->nodes[0];
602 header = btrfs_item_ptr(leaf, path->slots[0],
603 struct btrfs_free_space_header);
604 num_entries = btrfs_free_space_entries(leaf, header);
605 num_bitmaps = btrfs_free_space_bitmaps(leaf, header);
606 generation = btrfs_free_space_generation(leaf, header);
Chris Mason945d8962011-05-22 12:33:42 -0400607 btrfs_release_path(path);
Josef Bacik9d66e232010-08-25 16:54:15 -0400608
609 if (BTRFS_I(inode)->generation != generation) {
610 printk(KERN_ERR "btrfs: free space inode generation (%llu) did"
Li Zefan0414efa2011-04-20 10:20:14 +0800611 " not match free space cache generation (%llu)\n",
Josef Bacik9d66e232010-08-25 16:54:15 -0400612 (unsigned long long)BTRFS_I(inode)->generation,
Li Zefan0414efa2011-04-20 10:20:14 +0800613 (unsigned long long)generation);
Josef Bacika67509c2011-10-05 15:18:58 -0400614 return 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400615 }
616
617 if (!num_entries)
Josef Bacika67509c2011-10-05 15:18:58 -0400618 return 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400619
Josef Bacika67509c2011-10-05 15:18:58 -0400620 io_ctl_init(&io_ctl, inode, root);
Josef Bacik9d66e232010-08-25 16:54:15 -0400621 ret = readahead_cache(inode);
Li Zefan0414efa2011-04-20 10:20:14 +0800622 if (ret)
Josef Bacik9d66e232010-08-25 16:54:15 -0400623 goto out;
Josef Bacik9d66e232010-08-25 16:54:15 -0400624
Josef Bacika67509c2011-10-05 15:18:58 -0400625 ret = io_ctl_prepare_pages(&io_ctl, inode, 1);
626 if (ret)
627 goto out;
Josef Bacik9d66e232010-08-25 16:54:15 -0400628
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400629 ret = io_ctl_check_crc(&io_ctl, 0);
630 if (ret)
631 goto free_cache;
632
Josef Bacika67509c2011-10-05 15:18:58 -0400633 ret = io_ctl_check_generation(&io_ctl, generation);
634 if (ret)
635 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400636
Josef Bacika67509c2011-10-05 15:18:58 -0400637 while (num_entries) {
638 e = kmem_cache_zalloc(btrfs_free_space_cachep,
639 GFP_NOFS);
640 if (!e)
Josef Bacik9d66e232010-08-25 16:54:15 -0400641 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400642
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400643 ret = io_ctl_read_entry(&io_ctl, e, &type);
644 if (ret) {
645 kmem_cache_free(btrfs_free_space_cachep, e);
646 goto free_cache;
647 }
648
Josef Bacika67509c2011-10-05 15:18:58 -0400649 if (!e->bytes) {
650 kmem_cache_free(btrfs_free_space_cachep, e);
651 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400652 }
Josef Bacik9d66e232010-08-25 16:54:15 -0400653
Josef Bacika67509c2011-10-05 15:18:58 -0400654 if (type == BTRFS_FREE_SPACE_EXTENT) {
655 spin_lock(&ctl->tree_lock);
656 ret = link_free_space(ctl, e);
657 spin_unlock(&ctl->tree_lock);
658 if (ret) {
659 printk(KERN_ERR "Duplicate entries in "
660 "free space cache, dumping\n");
Josef Bacikdc89e982011-01-28 17:05:48 -0500661 kmem_cache_free(btrfs_free_space_cachep, e);
Josef Bacik9d66e232010-08-25 16:54:15 -0400662 goto free_cache;
663 }
Josef Bacika67509c2011-10-05 15:18:58 -0400664 } else {
665 BUG_ON(!num_bitmaps);
666 num_bitmaps--;
667 e->bitmap = kzalloc(PAGE_CACHE_SIZE, GFP_NOFS);
668 if (!e->bitmap) {
669 kmem_cache_free(
670 btrfs_free_space_cachep, e);
671 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400672 }
Josef Bacika67509c2011-10-05 15:18:58 -0400673 spin_lock(&ctl->tree_lock);
674 ret = link_free_space(ctl, e);
675 ctl->total_bitmaps++;
676 ctl->op->recalc_thresholds(ctl);
677 spin_unlock(&ctl->tree_lock);
678 if (ret) {
679 printk(KERN_ERR "Duplicate entries in "
680 "free space cache, dumping\n");
681 kmem_cache_free(btrfs_free_space_cachep, e);
682 goto free_cache;
683 }
684 list_add_tail(&e->list, &bitmaps);
Josef Bacik9d66e232010-08-25 16:54:15 -0400685 }
686
Josef Bacika67509c2011-10-05 15:18:58 -0400687 num_entries--;
Josef Bacik9d66e232010-08-25 16:54:15 -0400688 }
689
Josef Bacika67509c2011-10-05 15:18:58 -0400690 /*
691 * We add the bitmaps at the end of the entries in order that
692 * the bitmap entries are added to the cache.
693 */
694 list_for_each_entry_safe(e, n, &bitmaps, list) {
695 list_del_init(&e->list);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400696 ret = io_ctl_read_bitmap(&io_ctl, e);
697 if (ret)
698 goto free_cache;
Josef Bacika67509c2011-10-05 15:18:58 -0400699 }
700
701 io_ctl_drop_pages(&io_ctl);
Josef Bacik9d66e232010-08-25 16:54:15 -0400702 ret = 1;
703out:
Josef Bacika67509c2011-10-05 15:18:58 -0400704 io_ctl_free(&io_ctl);
Josef Bacik9d66e232010-08-25 16:54:15 -0400705 return ret;
Josef Bacik9d66e232010-08-25 16:54:15 -0400706free_cache:
Josef Bacika67509c2011-10-05 15:18:58 -0400707 io_ctl_drop_pages(&io_ctl);
Li Zefan0414efa2011-04-20 10:20:14 +0800708 __btrfs_remove_free_space_cache(ctl);
Josef Bacik9d66e232010-08-25 16:54:15 -0400709 goto out;
710}
711
Li Zefan0414efa2011-04-20 10:20:14 +0800712int load_free_space_cache(struct btrfs_fs_info *fs_info,
713 struct btrfs_block_group_cache *block_group)
Josef Bacik0cb59c92010-07-02 12:14:14 -0400714{
Li Zefan34d52cb2011-03-29 13:46:06 +0800715 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Li Zefan0414efa2011-04-20 10:20:14 +0800716 struct btrfs_root *root = fs_info->tree_root;
717 struct inode *inode;
718 struct btrfs_path *path;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400719 int ret = 0;
Li Zefan0414efa2011-04-20 10:20:14 +0800720 bool matched;
721 u64 used = btrfs_block_group_used(&block_group->item);
722
723 /*
724 * If we're unmounting then just return, since this does a search on the
725 * normal root and not the commit root and we could deadlock.
726 */
David Sterba7841cb22011-05-31 18:07:27 +0200727 if (btrfs_fs_closing(fs_info))
Li Zefan0414efa2011-04-20 10:20:14 +0800728 return 0;
729
730 /*
731 * If this block group has been marked to be cleared for one reason or
732 * another then we can't trust the on disk cache, so just return.
733 */
734 spin_lock(&block_group->lock);
735 if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) {
736 spin_unlock(&block_group->lock);
737 return 0;
738 }
739 spin_unlock(&block_group->lock);
740
741 path = btrfs_alloc_path();
742 if (!path)
743 return 0;
744
745 inode = lookup_free_space_inode(root, block_group, path);
746 if (IS_ERR(inode)) {
747 btrfs_free_path(path);
748 return 0;
749 }
750
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400751 /* We may have converted the inode and made the cache invalid. */
752 spin_lock(&block_group->lock);
753 if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) {
754 spin_unlock(&block_group->lock);
755 goto out;
756 }
757 spin_unlock(&block_group->lock);
758
Li Zefan0414efa2011-04-20 10:20:14 +0800759 ret = __load_free_space_cache(fs_info->tree_root, inode, ctl,
760 path, block_group->key.objectid);
761 btrfs_free_path(path);
762 if (ret <= 0)
763 goto out;
764
765 spin_lock(&ctl->tree_lock);
766 matched = (ctl->free_space == (block_group->key.offset - used -
767 block_group->bytes_super));
768 spin_unlock(&ctl->tree_lock);
769
770 if (!matched) {
771 __btrfs_remove_free_space_cache(ctl);
772 printk(KERN_ERR "block group %llu has an wrong amount of free "
773 "space\n", block_group->key.objectid);
774 ret = -1;
775 }
776out:
777 if (ret < 0) {
778 /* This cache is bogus, make sure it gets cleared */
779 spin_lock(&block_group->lock);
780 block_group->disk_cache_state = BTRFS_DC_CLEAR;
781 spin_unlock(&block_group->lock);
Li Zefan82d59022011-04-20 10:33:24 +0800782 ret = 0;
Li Zefan0414efa2011-04-20 10:20:14 +0800783
784 printk(KERN_ERR "btrfs: failed to load free space cache "
785 "for block group %llu\n", block_group->key.objectid);
786 }
787
788 iput(inode);
789 return ret;
790}
791
Josef Bacikc09544e2011-08-30 10:19:10 -0400792/**
793 * __btrfs_write_out_cache - write out cached info to an inode
794 * @root - the root the inode belongs to
795 * @ctl - the free space cache we are going to write out
796 * @block_group - the block_group for this cache if it belongs to a block_group
797 * @trans - the trans handle
798 * @path - the path to use
799 * @offset - the offset for the key we'll insert
800 *
801 * This function writes out a free space cache struct to disk for quick recovery
802 * on mount. This will return 0 if it was successfull in writing the cache out,
803 * and -1 if it was not.
804 */
Li Zefan0414efa2011-04-20 10:20:14 +0800805int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode,
806 struct btrfs_free_space_ctl *ctl,
807 struct btrfs_block_group_cache *block_group,
808 struct btrfs_trans_handle *trans,
809 struct btrfs_path *path, u64 offset)
Josef Bacik0cb59c92010-07-02 12:14:14 -0400810{
811 struct btrfs_free_space_header *header;
812 struct extent_buffer *leaf;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400813 struct rb_node *node;
814 struct list_head *pos, *n;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400815 struct extent_state *cached_state = NULL;
Josef Bacik43be2142011-04-01 14:55:00 +0000816 struct btrfs_free_cluster *cluster = NULL;
817 struct extent_io_tree *unpin = NULL;
Josef Bacika67509c2011-10-05 15:18:58 -0400818 struct io_ctl io_ctl;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400819 struct list_head bitmap_list;
820 struct btrfs_key key;
Josef Bacik43be2142011-04-01 14:55:00 +0000821 u64 start, end, len;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400822 int entries = 0;
823 int bitmaps = 0;
Josef Bacikc09544e2011-08-30 10:19:10 -0400824 int ret;
825 int err = -1;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400826
Josef Bacik0cb59c92010-07-02 12:14:14 -0400827 INIT_LIST_HEAD(&bitmap_list);
828
Li Zefan0414efa2011-04-20 10:20:14 +0800829 if (!i_size_read(inode))
830 return -1;
Josef Bacik2b209822010-12-03 13:17:53 -0500831
Josef Bacik0cb59c92010-07-02 12:14:14 -0400832 filemap_write_and_wait(inode->i_mapping);
833 btrfs_wait_ordered_range(inode, inode->i_size &
834 ~(root->sectorsize - 1), (u64)-1);
835
Josef Bacika67509c2011-10-05 15:18:58 -0400836 io_ctl_init(&io_ctl, inode, root);
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400837
Josef Bacik43be2142011-04-01 14:55:00 +0000838 /* Get the cluster for this block_group if it exists */
Li Zefan0414efa2011-04-20 10:20:14 +0800839 if (block_group && !list_empty(&block_group->cluster_list))
Josef Bacik43be2142011-04-01 14:55:00 +0000840 cluster = list_entry(block_group->cluster_list.next,
841 struct btrfs_free_cluster,
842 block_group_list);
843
844 /*
845 * We shouldn't have switched the pinned extents yet so this is the
846 * right one
847 */
848 unpin = root->fs_info->pinned_extents;
849
Josef Bacika67509c2011-10-05 15:18:58 -0400850 /* Lock all pages first so we can lock the extent safely. */
851 io_ctl_prepare_pages(&io_ctl, inode, 0);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400852
Josef Bacik0cb59c92010-07-02 12:14:14 -0400853 lock_extent_bits(&BTRFS_I(inode)->io_tree, 0, i_size_read(inode) - 1,
854 0, &cached_state, GFP_NOFS);
855
Josef Bacik43be2142011-04-01 14:55:00 +0000856 /*
857 * When searching for pinned extents, we need to start at our start
858 * offset.
859 */
Li Zefan0414efa2011-04-20 10:20:14 +0800860 if (block_group)
861 start = block_group->key.objectid;
Josef Bacik43be2142011-04-01 14:55:00 +0000862
Josef Bacikf75b1302011-10-05 10:00:18 -0400863 node = rb_first(&ctl->free_space_offset);
864 if (!node && cluster) {
865 node = rb_first(&cluster->root);
866 cluster = NULL;
867 }
868
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400869 /* Make sure we can fit our crcs into the first page */
870 if (io_ctl.check_crcs &&
871 (io_ctl.num_pages * sizeof(u32)) >= PAGE_CACHE_SIZE) {
872 WARN_ON(1);
873 goto out_nospc;
874 }
875
Josef Bacika67509c2011-10-05 15:18:58 -0400876 io_ctl_set_generation(&io_ctl, trans->transid);
877
Josef Bacik0cb59c92010-07-02 12:14:14 -0400878 /* Write out the extent entries */
Josef Bacika67509c2011-10-05 15:18:58 -0400879 while (node) {
880 struct btrfs_free_space *e;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400881
Josef Bacika67509c2011-10-05 15:18:58 -0400882 e = rb_entry(node, struct btrfs_free_space, offset_index);
883 entries++;
Josef Bacik43be2142011-04-01 14:55:00 +0000884
Josef Bacika67509c2011-10-05 15:18:58 -0400885 ret = io_ctl_add_entry(&io_ctl, e->offset, e->bytes,
886 e->bitmap);
887 if (ret)
888 goto out_nospc;
889
890 if (e->bitmap) {
891 list_add_tail(&e->list, &bitmap_list);
892 bitmaps++;
893 }
894 node = rb_next(node);
895 if (!node && cluster) {
896 node = rb_first(&cluster->root);
897 cluster = NULL;
898 }
899 }
900
901 /*
902 * We want to add any pinned extents to our free space cache
903 * so we don't leak the space
904 */
905 while (block_group && (start < block_group->key.objectid +
906 block_group->key.offset)) {
907 ret = find_first_extent_bit(unpin, start, &start, &end,
908 EXTENT_DIRTY);
909 if (ret) {
910 ret = 0;
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400911 break;
912 }
913
Josef Bacika67509c2011-10-05 15:18:58 -0400914 /* This pinned extent is out of our range */
915 if (start >= block_group->key.objectid +
916 block_group->key.offset)
917 break;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400918
Josef Bacika67509c2011-10-05 15:18:58 -0400919 len = block_group->key.objectid +
920 block_group->key.offset - start;
921 len = min(len, end + 1 - start);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400922
Josef Bacika67509c2011-10-05 15:18:58 -0400923 entries++;
924 ret = io_ctl_add_entry(&io_ctl, start, len, NULL);
925 if (ret)
926 goto out_nospc;
Josef Bacik2f356122011-06-10 15:31:13 -0400927
Josef Bacika67509c2011-10-05 15:18:58 -0400928 start = end + 1;
929 }
Josef Bacik0cb59c92010-07-02 12:14:14 -0400930
931 /* Write out the bitmaps */
932 list_for_each_safe(pos, n, &bitmap_list) {
Josef Bacik0cb59c92010-07-02 12:14:14 -0400933 struct btrfs_free_space *entry =
934 list_entry(pos, struct btrfs_free_space, list);
935
Josef Bacika67509c2011-10-05 15:18:58 -0400936 ret = io_ctl_add_bitmap(&io_ctl, entry->bitmap);
937 if (ret)
938 goto out_nospc;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400939 list_del_init(&entry->list);
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400940 }
941
Josef Bacik0cb59c92010-07-02 12:14:14 -0400942 /* Zero out the rest of the pages just to make sure */
Josef Bacika67509c2011-10-05 15:18:58 -0400943 io_ctl_zero_remaining_pages(&io_ctl);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400944
Josef Bacika67509c2011-10-05 15:18:58 -0400945 ret = btrfs_dirty_pages(root, inode, io_ctl.pages, io_ctl.num_pages,
946 0, i_size_read(inode), &cached_state);
947 io_ctl_drop_pages(&io_ctl);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400948 unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
949 i_size_read(inode) - 1, &cached_state, GFP_NOFS);
950
Josef Bacikc09544e2011-08-30 10:19:10 -0400951 if (ret)
Josef Bacik2f356122011-06-10 15:31:13 -0400952 goto out;
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400953
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400954
Josef Bacik549b4fd2011-10-05 16:33:53 -0400955 ret = filemap_write_and_wait(inode->i_mapping);
956 if (ret)
957 goto out;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400958
959 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
Li Zefan0414efa2011-04-20 10:20:14 +0800960 key.offset = offset;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400961 key.type = 0;
962
Josef Bacika9b5fcd2011-08-19 12:06:12 -0400963 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400964 if (ret < 0) {
Josef Bacika67509c2011-10-05 15:18:58 -0400965 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, inode->i_size - 1,
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400966 EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0, NULL,
967 GFP_NOFS);
Josef Bacik2f356122011-06-10 15:31:13 -0400968 goto out;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400969 }
970 leaf = path->nodes[0];
971 if (ret > 0) {
972 struct btrfs_key found_key;
973 BUG_ON(!path->slots[0]);
974 path->slots[0]--;
975 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
976 if (found_key.objectid != BTRFS_FREE_SPACE_OBJECTID ||
Li Zefan0414efa2011-04-20 10:20:14 +0800977 found_key.offset != offset) {
Josef Bacika67509c2011-10-05 15:18:58 -0400978 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0,
979 inode->i_size - 1,
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400980 EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0,
981 NULL, GFP_NOFS);
David Sterbab3b4aa72011-04-21 01:20:15 +0200982 btrfs_release_path(path);
Josef Bacik2f356122011-06-10 15:31:13 -0400983 goto out;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400984 }
985 }
Josef Bacik549b4fd2011-10-05 16:33:53 -0400986
987 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400988 header = btrfs_item_ptr(leaf, path->slots[0],
989 struct btrfs_free_space_header);
990 btrfs_set_free_space_entries(leaf, header, entries);
991 btrfs_set_free_space_bitmaps(leaf, header, bitmaps);
992 btrfs_set_free_space_generation(leaf, header, trans->transid);
993 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +0200994 btrfs_release_path(path);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400995
Josef Bacikc09544e2011-08-30 10:19:10 -0400996 err = 0;
Josef Bacik2f356122011-06-10 15:31:13 -0400997out:
Josef Bacika67509c2011-10-05 15:18:58 -0400998 io_ctl_free(&io_ctl);
Josef Bacikc09544e2011-08-30 10:19:10 -0400999 if (err) {
Josef Bacika67509c2011-10-05 15:18:58 -04001000 invalidate_inode_pages2(inode->i_mapping);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001001 BTRFS_I(inode)->generation = 0;
1002 }
Josef Bacik0cb59c92010-07-02 12:14:14 -04001003 btrfs_update_inode(trans, root, inode);
Josef Bacikc09544e2011-08-30 10:19:10 -04001004 return err;
Josef Bacika67509c2011-10-05 15:18:58 -04001005
1006out_nospc:
1007 list_for_each_safe(pos, n, &bitmap_list) {
1008 struct btrfs_free_space *entry =
1009 list_entry(pos, struct btrfs_free_space, list);
1010 list_del_init(&entry->list);
1011 }
1012 io_ctl_drop_pages(&io_ctl);
1013 unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
1014 i_size_read(inode) - 1, &cached_state, GFP_NOFS);
1015 goto out;
Li Zefan0414efa2011-04-20 10:20:14 +08001016}
1017
1018int btrfs_write_out_cache(struct btrfs_root *root,
1019 struct btrfs_trans_handle *trans,
1020 struct btrfs_block_group_cache *block_group,
1021 struct btrfs_path *path)
1022{
1023 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
1024 struct inode *inode;
1025 int ret = 0;
1026
1027 root = root->fs_info->tree_root;
1028
1029 spin_lock(&block_group->lock);
1030 if (block_group->disk_cache_state < BTRFS_DC_SETUP) {
1031 spin_unlock(&block_group->lock);
1032 return 0;
1033 }
1034 spin_unlock(&block_group->lock);
1035
1036 inode = lookup_free_space_inode(root, block_group, path);
1037 if (IS_ERR(inode))
1038 return 0;
1039
1040 ret = __btrfs_write_out_cache(root, inode, ctl, block_group, trans,
1041 path, block_group->key.objectid);
Josef Bacikc09544e2011-08-30 10:19:10 -04001042 if (ret) {
Li Zefan0414efa2011-04-20 10:20:14 +08001043 spin_lock(&block_group->lock);
1044 block_group->disk_cache_state = BTRFS_DC_ERROR;
1045 spin_unlock(&block_group->lock);
Li Zefan82d59022011-04-20 10:33:24 +08001046 ret = 0;
Josef Bacikc09544e2011-08-30 10:19:10 -04001047#ifdef DEBUG
Li Zefan0414efa2011-04-20 10:20:14 +08001048 printk(KERN_ERR "btrfs: failed to write free space cace "
1049 "for block group %llu\n", block_group->key.objectid);
Josef Bacikc09544e2011-08-30 10:19:10 -04001050#endif
Li Zefan0414efa2011-04-20 10:20:14 +08001051 }
1052
Josef Bacik0cb59c92010-07-02 12:14:14 -04001053 iput(inode);
1054 return ret;
1055}
1056
Li Zefan34d52cb2011-03-29 13:46:06 +08001057static inline unsigned long offset_to_bit(u64 bitmap_start, u32 unit,
Josef Bacik96303082009-07-13 21:29:25 -04001058 u64 offset)
1059{
1060 BUG_ON(offset < bitmap_start);
1061 offset -= bitmap_start;
Li Zefan34d52cb2011-03-29 13:46:06 +08001062 return (unsigned long)(div_u64(offset, unit));
Josef Bacik96303082009-07-13 21:29:25 -04001063}
1064
Li Zefan34d52cb2011-03-29 13:46:06 +08001065static inline unsigned long bytes_to_bits(u64 bytes, u32 unit)
Josef Bacik96303082009-07-13 21:29:25 -04001066{
Li Zefan34d52cb2011-03-29 13:46:06 +08001067 return (unsigned long)(div_u64(bytes, unit));
Josef Bacik96303082009-07-13 21:29:25 -04001068}
1069
Li Zefan34d52cb2011-03-29 13:46:06 +08001070static inline u64 offset_to_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001071 u64 offset)
1072{
1073 u64 bitmap_start;
1074 u64 bytes_per_bitmap;
1075
Li Zefan34d52cb2011-03-29 13:46:06 +08001076 bytes_per_bitmap = BITS_PER_BITMAP * ctl->unit;
1077 bitmap_start = offset - ctl->start;
Josef Bacik96303082009-07-13 21:29:25 -04001078 bitmap_start = div64_u64(bitmap_start, bytes_per_bitmap);
1079 bitmap_start *= bytes_per_bitmap;
Li Zefan34d52cb2011-03-29 13:46:06 +08001080 bitmap_start += ctl->start;
Josef Bacik96303082009-07-13 21:29:25 -04001081
1082 return bitmap_start;
1083}
Josef Bacik0f9dd462008-09-23 13:14:11 -04001084
1085static int tree_insert_offset(struct rb_root *root, u64 offset,
Josef Bacik96303082009-07-13 21:29:25 -04001086 struct rb_node *node, int bitmap)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001087{
1088 struct rb_node **p = &root->rb_node;
1089 struct rb_node *parent = NULL;
1090 struct btrfs_free_space *info;
1091
1092 while (*p) {
1093 parent = *p;
1094 info = rb_entry(parent, struct btrfs_free_space, offset_index);
1095
Josef Bacik96303082009-07-13 21:29:25 -04001096 if (offset < info->offset) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001097 p = &(*p)->rb_left;
Josef Bacik96303082009-07-13 21:29:25 -04001098 } else if (offset > info->offset) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001099 p = &(*p)->rb_right;
Josef Bacik96303082009-07-13 21:29:25 -04001100 } else {
1101 /*
1102 * we could have a bitmap entry and an extent entry
1103 * share the same offset. If this is the case, we want
1104 * the extent entry to always be found first if we do a
1105 * linear search through the tree, since we want to have
1106 * the quickest allocation time, and allocating from an
1107 * extent is faster than allocating from a bitmap. So
1108 * if we're inserting a bitmap and we find an entry at
1109 * this offset, we want to go right, or after this entry
1110 * logically. If we are inserting an extent and we've
1111 * found a bitmap, we want to go left, or before
1112 * logically.
1113 */
1114 if (bitmap) {
Josef Bacik207dde82011-05-13 14:49:23 -04001115 if (info->bitmap) {
1116 WARN_ON_ONCE(1);
1117 return -EEXIST;
1118 }
Josef Bacik96303082009-07-13 21:29:25 -04001119 p = &(*p)->rb_right;
1120 } else {
Josef Bacik207dde82011-05-13 14:49:23 -04001121 if (!info->bitmap) {
1122 WARN_ON_ONCE(1);
1123 return -EEXIST;
1124 }
Josef Bacik96303082009-07-13 21:29:25 -04001125 p = &(*p)->rb_left;
1126 }
1127 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04001128 }
1129
1130 rb_link_node(node, parent, p);
1131 rb_insert_color(node, root);
1132
1133 return 0;
1134}
1135
1136/*
Josef Bacik70cb0742009-04-03 10:14:19 -04001137 * searches the tree for the given offset.
1138 *
Josef Bacik96303082009-07-13 21:29:25 -04001139 * fuzzy - If this is set, then we are trying to make an allocation, and we just
1140 * want a section that has at least bytes size and comes at or after the given
1141 * offset.
Josef Bacik0f9dd462008-09-23 13:14:11 -04001142 */
Josef Bacik96303082009-07-13 21:29:25 -04001143static struct btrfs_free_space *
Li Zefan34d52cb2011-03-29 13:46:06 +08001144tree_search_offset(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001145 u64 offset, int bitmap_only, int fuzzy)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001146{
Li Zefan34d52cb2011-03-29 13:46:06 +08001147 struct rb_node *n = ctl->free_space_offset.rb_node;
Josef Bacik96303082009-07-13 21:29:25 -04001148 struct btrfs_free_space *entry, *prev = NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001149
Josef Bacik96303082009-07-13 21:29:25 -04001150 /* find entry that is closest to the 'offset' */
1151 while (1) {
1152 if (!n) {
1153 entry = NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001154 break;
1155 }
Josef Bacik96303082009-07-13 21:29:25 -04001156
1157 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1158 prev = entry;
1159
1160 if (offset < entry->offset)
1161 n = n->rb_left;
1162 else if (offset > entry->offset)
1163 n = n->rb_right;
1164 else
1165 break;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001166 }
1167
Josef Bacik96303082009-07-13 21:29:25 -04001168 if (bitmap_only) {
1169 if (!entry)
1170 return NULL;
1171 if (entry->bitmap)
1172 return entry;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001173
Josef Bacik96303082009-07-13 21:29:25 -04001174 /*
1175 * bitmap entry and extent entry may share same offset,
1176 * in that case, bitmap entry comes after extent entry.
1177 */
1178 n = rb_next(n);
1179 if (!n)
1180 return NULL;
1181 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1182 if (entry->offset != offset)
1183 return NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001184
Josef Bacik96303082009-07-13 21:29:25 -04001185 WARN_ON(!entry->bitmap);
1186 return entry;
1187 } else if (entry) {
1188 if (entry->bitmap) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001189 /*
Josef Bacik96303082009-07-13 21:29:25 -04001190 * if previous extent entry covers the offset,
1191 * we should return it instead of the bitmap entry
Josef Bacik0f9dd462008-09-23 13:14:11 -04001192 */
Josef Bacik96303082009-07-13 21:29:25 -04001193 n = &entry->offset_index;
1194 while (1) {
1195 n = rb_prev(n);
1196 if (!n)
1197 break;
1198 prev = rb_entry(n, struct btrfs_free_space,
1199 offset_index);
1200 if (!prev->bitmap) {
1201 if (prev->offset + prev->bytes > offset)
1202 entry = prev;
1203 break;
1204 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04001205 }
Josef Bacik96303082009-07-13 21:29:25 -04001206 }
1207 return entry;
1208 }
1209
1210 if (!prev)
1211 return NULL;
1212
1213 /* find last entry before the 'offset' */
1214 entry = prev;
1215 if (entry->offset > offset) {
1216 n = rb_prev(&entry->offset_index);
1217 if (n) {
1218 entry = rb_entry(n, struct btrfs_free_space,
1219 offset_index);
1220 BUG_ON(entry->offset > offset);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001221 } else {
Josef Bacik96303082009-07-13 21:29:25 -04001222 if (fuzzy)
1223 return entry;
1224 else
1225 return NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001226 }
1227 }
1228
Josef Bacik96303082009-07-13 21:29:25 -04001229 if (entry->bitmap) {
1230 n = &entry->offset_index;
1231 while (1) {
1232 n = rb_prev(n);
1233 if (!n)
1234 break;
1235 prev = rb_entry(n, struct btrfs_free_space,
1236 offset_index);
1237 if (!prev->bitmap) {
1238 if (prev->offset + prev->bytes > offset)
1239 return prev;
1240 break;
1241 }
1242 }
Li Zefan34d52cb2011-03-29 13:46:06 +08001243 if (entry->offset + BITS_PER_BITMAP * ctl->unit > offset)
Josef Bacik96303082009-07-13 21:29:25 -04001244 return entry;
1245 } else if (entry->offset + entry->bytes > offset)
1246 return entry;
1247
1248 if (!fuzzy)
1249 return NULL;
1250
1251 while (1) {
1252 if (entry->bitmap) {
1253 if (entry->offset + BITS_PER_BITMAP *
Li Zefan34d52cb2011-03-29 13:46:06 +08001254 ctl->unit > offset)
Josef Bacik96303082009-07-13 21:29:25 -04001255 break;
1256 } else {
1257 if (entry->offset + entry->bytes > offset)
1258 break;
1259 }
1260
1261 n = rb_next(&entry->offset_index);
1262 if (!n)
1263 return NULL;
1264 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1265 }
1266 return entry;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001267}
1268
Li Zefanf333adb2010-11-09 14:57:39 +08001269static inline void
Li Zefan34d52cb2011-03-29 13:46:06 +08001270__unlink_free_space(struct btrfs_free_space_ctl *ctl,
Li Zefanf333adb2010-11-09 14:57:39 +08001271 struct btrfs_free_space *info)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001272{
Li Zefan34d52cb2011-03-29 13:46:06 +08001273 rb_erase(&info->offset_index, &ctl->free_space_offset);
1274 ctl->free_extents--;
Li Zefanf333adb2010-11-09 14:57:39 +08001275}
1276
Li Zefan34d52cb2011-03-29 13:46:06 +08001277static void unlink_free_space(struct btrfs_free_space_ctl *ctl,
Li Zefanf333adb2010-11-09 14:57:39 +08001278 struct btrfs_free_space *info)
1279{
Li Zefan34d52cb2011-03-29 13:46:06 +08001280 __unlink_free_space(ctl, info);
1281 ctl->free_space -= info->bytes;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001282}
1283
Li Zefan34d52cb2011-03-29 13:46:06 +08001284static int link_free_space(struct btrfs_free_space_ctl *ctl,
Josef Bacik0f9dd462008-09-23 13:14:11 -04001285 struct btrfs_free_space *info)
1286{
1287 int ret = 0;
1288
Josef Bacik96303082009-07-13 21:29:25 -04001289 BUG_ON(!info->bitmap && !info->bytes);
Li Zefan34d52cb2011-03-29 13:46:06 +08001290 ret = tree_insert_offset(&ctl->free_space_offset, info->offset,
Josef Bacik96303082009-07-13 21:29:25 -04001291 &info->offset_index, (info->bitmap != NULL));
Josef Bacik0f9dd462008-09-23 13:14:11 -04001292 if (ret)
1293 return ret;
1294
Li Zefan34d52cb2011-03-29 13:46:06 +08001295 ctl->free_space += info->bytes;
1296 ctl->free_extents++;
Josef Bacik96303082009-07-13 21:29:25 -04001297 return ret;
1298}
1299
Li Zefan34d52cb2011-03-29 13:46:06 +08001300static void recalculate_thresholds(struct btrfs_free_space_ctl *ctl)
Josef Bacik96303082009-07-13 21:29:25 -04001301{
Li Zefan34d52cb2011-03-29 13:46:06 +08001302 struct btrfs_block_group_cache *block_group = ctl->private;
Josef Bacik25891f72009-09-11 16:11:20 -04001303 u64 max_bytes;
1304 u64 bitmap_bytes;
1305 u64 extent_bytes;
Li Zefan8eb2d822010-11-09 14:48:01 +08001306 u64 size = block_group->key.offset;
Li Zefan34d52cb2011-03-29 13:46:06 +08001307 u64 bytes_per_bg = BITS_PER_BITMAP * block_group->sectorsize;
1308 int max_bitmaps = div64_u64(size + bytes_per_bg - 1, bytes_per_bg);
1309
1310 BUG_ON(ctl->total_bitmaps > max_bitmaps);
Josef Bacik96303082009-07-13 21:29:25 -04001311
1312 /*
1313 * The goal is to keep the total amount of memory used per 1gb of space
1314 * at or below 32k, so we need to adjust how much memory we allow to be
1315 * used by extent based free space tracking
1316 */
Li Zefan8eb2d822010-11-09 14:48:01 +08001317 if (size < 1024 * 1024 * 1024)
1318 max_bytes = MAX_CACHE_BYTES_PER_GIG;
1319 else
1320 max_bytes = MAX_CACHE_BYTES_PER_GIG *
1321 div64_u64(size, 1024 * 1024 * 1024);
Josef Bacik96303082009-07-13 21:29:25 -04001322
Josef Bacik25891f72009-09-11 16:11:20 -04001323 /*
1324 * we want to account for 1 more bitmap than what we have so we can make
1325 * sure we don't go over our overall goal of MAX_CACHE_BYTES_PER_GIG as
1326 * we add more bitmaps.
1327 */
Li Zefan34d52cb2011-03-29 13:46:06 +08001328 bitmap_bytes = (ctl->total_bitmaps + 1) * PAGE_CACHE_SIZE;
Josef Bacik96303082009-07-13 21:29:25 -04001329
Josef Bacik25891f72009-09-11 16:11:20 -04001330 if (bitmap_bytes >= max_bytes) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001331 ctl->extents_thresh = 0;
Josef Bacik25891f72009-09-11 16:11:20 -04001332 return;
Josef Bacik96303082009-07-13 21:29:25 -04001333 }
Josef Bacik25891f72009-09-11 16:11:20 -04001334
1335 /*
1336 * we want the extent entry threshold to always be at most 1/2 the maxw
1337 * bytes we can have, or whatever is less than that.
1338 */
1339 extent_bytes = max_bytes - bitmap_bytes;
1340 extent_bytes = min_t(u64, extent_bytes, div64_u64(max_bytes, 2));
1341
Li Zefan34d52cb2011-03-29 13:46:06 +08001342 ctl->extents_thresh =
Josef Bacik25891f72009-09-11 16:11:20 -04001343 div64_u64(extent_bytes, (sizeof(struct btrfs_free_space)));
Josef Bacik96303082009-07-13 21:29:25 -04001344}
1345
Miao Xiebb3ac5a2011-08-05 09:32:35 +00001346static inline void __bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
1347 struct btrfs_free_space *info,
1348 u64 offset, u64 bytes)
Josef Bacik96303082009-07-13 21:29:25 -04001349{
Li Zefanf38b6e72011-03-14 13:40:51 +08001350 unsigned long start, count;
Josef Bacik96303082009-07-13 21:29:25 -04001351
Li Zefan34d52cb2011-03-29 13:46:06 +08001352 start = offset_to_bit(info->offset, ctl->unit, offset);
1353 count = bytes_to_bits(bytes, ctl->unit);
Li Zefanf38b6e72011-03-14 13:40:51 +08001354 BUG_ON(start + count > BITS_PER_BITMAP);
Josef Bacik96303082009-07-13 21:29:25 -04001355
Li Zefanf38b6e72011-03-14 13:40:51 +08001356 bitmap_clear(info->bitmap, start, count);
Josef Bacik96303082009-07-13 21:29:25 -04001357
1358 info->bytes -= bytes;
Miao Xiebb3ac5a2011-08-05 09:32:35 +00001359}
1360
1361static void bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
1362 struct btrfs_free_space *info, u64 offset,
1363 u64 bytes)
1364{
1365 __bitmap_clear_bits(ctl, info, offset, bytes);
Li Zefan34d52cb2011-03-29 13:46:06 +08001366 ctl->free_space -= bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001367}
1368
Li Zefan34d52cb2011-03-29 13:46:06 +08001369static void bitmap_set_bits(struct btrfs_free_space_ctl *ctl,
Josef Bacik817d52f2009-07-13 21:29:25 -04001370 struct btrfs_free_space *info, u64 offset,
1371 u64 bytes)
Josef Bacik96303082009-07-13 21:29:25 -04001372{
Li Zefanf38b6e72011-03-14 13:40:51 +08001373 unsigned long start, count;
Josef Bacik96303082009-07-13 21:29:25 -04001374
Li Zefan34d52cb2011-03-29 13:46:06 +08001375 start = offset_to_bit(info->offset, ctl->unit, offset);
1376 count = bytes_to_bits(bytes, ctl->unit);
Li Zefanf38b6e72011-03-14 13:40:51 +08001377 BUG_ON(start + count > BITS_PER_BITMAP);
Josef Bacik96303082009-07-13 21:29:25 -04001378
Li Zefanf38b6e72011-03-14 13:40:51 +08001379 bitmap_set(info->bitmap, start, count);
Josef Bacik96303082009-07-13 21:29:25 -04001380
1381 info->bytes += bytes;
Li Zefan34d52cb2011-03-29 13:46:06 +08001382 ctl->free_space += bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001383}
1384
Li Zefan34d52cb2011-03-29 13:46:06 +08001385static int search_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001386 struct btrfs_free_space *bitmap_info, u64 *offset,
1387 u64 *bytes)
1388{
1389 unsigned long found_bits = 0;
1390 unsigned long bits, i;
1391 unsigned long next_zero;
1392
Li Zefan34d52cb2011-03-29 13:46:06 +08001393 i = offset_to_bit(bitmap_info->offset, ctl->unit,
Josef Bacik96303082009-07-13 21:29:25 -04001394 max_t(u64, *offset, bitmap_info->offset));
Li Zefan34d52cb2011-03-29 13:46:06 +08001395 bits = bytes_to_bits(*bytes, ctl->unit);
Josef Bacik96303082009-07-13 21:29:25 -04001396
1397 for (i = find_next_bit(bitmap_info->bitmap, BITS_PER_BITMAP, i);
1398 i < BITS_PER_BITMAP;
1399 i = find_next_bit(bitmap_info->bitmap, BITS_PER_BITMAP, i + 1)) {
1400 next_zero = find_next_zero_bit(bitmap_info->bitmap,
1401 BITS_PER_BITMAP, i);
1402 if ((next_zero - i) >= bits) {
1403 found_bits = next_zero - i;
1404 break;
1405 }
1406 i = next_zero;
1407 }
1408
1409 if (found_bits) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001410 *offset = (u64)(i * ctl->unit) + bitmap_info->offset;
1411 *bytes = (u64)(found_bits) * ctl->unit;
Josef Bacik96303082009-07-13 21:29:25 -04001412 return 0;
1413 }
1414
1415 return -1;
1416}
1417
Li Zefan34d52cb2011-03-29 13:46:06 +08001418static struct btrfs_free_space *
1419find_free_space(struct btrfs_free_space_ctl *ctl, u64 *offset, u64 *bytes)
Josef Bacik96303082009-07-13 21:29:25 -04001420{
1421 struct btrfs_free_space *entry;
1422 struct rb_node *node;
1423 int ret;
1424
Li Zefan34d52cb2011-03-29 13:46:06 +08001425 if (!ctl->free_space_offset.rb_node)
Josef Bacik96303082009-07-13 21:29:25 -04001426 return NULL;
1427
Li Zefan34d52cb2011-03-29 13:46:06 +08001428 entry = tree_search_offset(ctl, offset_to_bitmap(ctl, *offset), 0, 1);
Josef Bacik96303082009-07-13 21:29:25 -04001429 if (!entry)
1430 return NULL;
1431
1432 for (node = &entry->offset_index; node; node = rb_next(node)) {
1433 entry = rb_entry(node, struct btrfs_free_space, offset_index);
1434 if (entry->bytes < *bytes)
1435 continue;
1436
1437 if (entry->bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001438 ret = search_bitmap(ctl, entry, offset, bytes);
Josef Bacik96303082009-07-13 21:29:25 -04001439 if (!ret)
1440 return entry;
1441 continue;
1442 }
1443
1444 *offset = entry->offset;
1445 *bytes = entry->bytes;
1446 return entry;
1447 }
1448
1449 return NULL;
1450}
1451
Li Zefan34d52cb2011-03-29 13:46:06 +08001452static void add_new_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001453 struct btrfs_free_space *info, u64 offset)
1454{
Li Zefan34d52cb2011-03-29 13:46:06 +08001455 info->offset = offset_to_bitmap(ctl, offset);
Josef Bacikf019f422009-09-11 16:11:20 -04001456 info->bytes = 0;
Li Zefan34d52cb2011-03-29 13:46:06 +08001457 link_free_space(ctl, info);
1458 ctl->total_bitmaps++;
Josef Bacik96303082009-07-13 21:29:25 -04001459
Li Zefan34d52cb2011-03-29 13:46:06 +08001460 ctl->op->recalc_thresholds(ctl);
Josef Bacik96303082009-07-13 21:29:25 -04001461}
1462
Li Zefan34d52cb2011-03-29 13:46:06 +08001463static void free_bitmap(struct btrfs_free_space_ctl *ctl,
Li Zefanedf6e2d2010-11-09 14:50:07 +08001464 struct btrfs_free_space *bitmap_info)
1465{
Li Zefan34d52cb2011-03-29 13:46:06 +08001466 unlink_free_space(ctl, bitmap_info);
Li Zefanedf6e2d2010-11-09 14:50:07 +08001467 kfree(bitmap_info->bitmap);
Josef Bacikdc89e982011-01-28 17:05:48 -05001468 kmem_cache_free(btrfs_free_space_cachep, bitmap_info);
Li Zefan34d52cb2011-03-29 13:46:06 +08001469 ctl->total_bitmaps--;
1470 ctl->op->recalc_thresholds(ctl);
Li Zefanedf6e2d2010-11-09 14:50:07 +08001471}
1472
Li Zefan34d52cb2011-03-29 13:46:06 +08001473static noinline int remove_from_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001474 struct btrfs_free_space *bitmap_info,
1475 u64 *offset, u64 *bytes)
1476{
1477 u64 end;
Josef Bacik6606bb92009-07-31 11:03:58 -04001478 u64 search_start, search_bytes;
1479 int ret;
Josef Bacik96303082009-07-13 21:29:25 -04001480
1481again:
Li Zefan34d52cb2011-03-29 13:46:06 +08001482 end = bitmap_info->offset + (u64)(BITS_PER_BITMAP * ctl->unit) - 1;
Josef Bacik96303082009-07-13 21:29:25 -04001483
Josef Bacik6606bb92009-07-31 11:03:58 -04001484 /*
1485 * XXX - this can go away after a few releases.
1486 *
1487 * since the only user of btrfs_remove_free_space is the tree logging
1488 * stuff, and the only way to test that is under crash conditions, we
1489 * want to have this debug stuff here just in case somethings not
1490 * working. Search the bitmap for the space we are trying to use to
1491 * make sure its actually there. If its not there then we need to stop
1492 * because something has gone wrong.
1493 */
1494 search_start = *offset;
1495 search_bytes = *bytes;
Josef Bacik13dbc082011-02-03 02:39:52 +00001496 search_bytes = min(search_bytes, end - search_start + 1);
Li Zefan34d52cb2011-03-29 13:46:06 +08001497 ret = search_bitmap(ctl, bitmap_info, &search_start, &search_bytes);
Josef Bacik6606bb92009-07-31 11:03:58 -04001498 BUG_ON(ret < 0 || search_start != *offset);
1499
Josef Bacik96303082009-07-13 21:29:25 -04001500 if (*offset > bitmap_info->offset && *offset + *bytes > end) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001501 bitmap_clear_bits(ctl, bitmap_info, *offset, end - *offset + 1);
Josef Bacik96303082009-07-13 21:29:25 -04001502 *bytes -= end - *offset + 1;
1503 *offset = end + 1;
1504 } else if (*offset >= bitmap_info->offset && *offset + *bytes <= end) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001505 bitmap_clear_bits(ctl, bitmap_info, *offset, *bytes);
Josef Bacik96303082009-07-13 21:29:25 -04001506 *bytes = 0;
1507 }
1508
1509 if (*bytes) {
Josef Bacik6606bb92009-07-31 11:03:58 -04001510 struct rb_node *next = rb_next(&bitmap_info->offset_index);
Li Zefanedf6e2d2010-11-09 14:50:07 +08001511 if (!bitmap_info->bytes)
Li Zefan34d52cb2011-03-29 13:46:06 +08001512 free_bitmap(ctl, bitmap_info);
Josef Bacik96303082009-07-13 21:29:25 -04001513
Josef Bacik6606bb92009-07-31 11:03:58 -04001514 /*
1515 * no entry after this bitmap, but we still have bytes to
1516 * remove, so something has gone wrong.
1517 */
1518 if (!next)
Josef Bacik96303082009-07-13 21:29:25 -04001519 return -EINVAL;
1520
Josef Bacik6606bb92009-07-31 11:03:58 -04001521 bitmap_info = rb_entry(next, struct btrfs_free_space,
1522 offset_index);
1523
1524 /*
1525 * if the next entry isn't a bitmap we need to return to let the
1526 * extent stuff do its work.
1527 */
Josef Bacik96303082009-07-13 21:29:25 -04001528 if (!bitmap_info->bitmap)
1529 return -EAGAIN;
1530
Josef Bacik6606bb92009-07-31 11:03:58 -04001531 /*
1532 * Ok the next item is a bitmap, but it may not actually hold
1533 * the information for the rest of this free space stuff, so
1534 * look for it, and if we don't find it return so we can try
1535 * everything over again.
1536 */
1537 search_start = *offset;
1538 search_bytes = *bytes;
Li Zefan34d52cb2011-03-29 13:46:06 +08001539 ret = search_bitmap(ctl, bitmap_info, &search_start,
Josef Bacik6606bb92009-07-31 11:03:58 -04001540 &search_bytes);
1541 if (ret < 0 || search_start != *offset)
1542 return -EAGAIN;
1543
Josef Bacik96303082009-07-13 21:29:25 -04001544 goto again;
Li Zefanedf6e2d2010-11-09 14:50:07 +08001545 } else if (!bitmap_info->bytes)
Li Zefan34d52cb2011-03-29 13:46:06 +08001546 free_bitmap(ctl, bitmap_info);
Josef Bacik96303082009-07-13 21:29:25 -04001547
1548 return 0;
1549}
1550
Josef Bacik2cdc3422011-05-27 14:07:49 -04001551static u64 add_bytes_to_bitmap(struct btrfs_free_space_ctl *ctl,
1552 struct btrfs_free_space *info, u64 offset,
1553 u64 bytes)
1554{
1555 u64 bytes_to_set = 0;
1556 u64 end;
1557
1558 end = info->offset + (u64)(BITS_PER_BITMAP * ctl->unit);
1559
1560 bytes_to_set = min(end - offset, bytes);
1561
1562 bitmap_set_bits(ctl, info, offset, bytes_to_set);
1563
1564 return bytes_to_set;
1565
1566}
1567
Li Zefan34d52cb2011-03-29 13:46:06 +08001568static bool use_bitmap(struct btrfs_free_space_ctl *ctl,
1569 struct btrfs_free_space *info)
Josef Bacik96303082009-07-13 21:29:25 -04001570{
Li Zefan34d52cb2011-03-29 13:46:06 +08001571 struct btrfs_block_group_cache *block_group = ctl->private;
Josef Bacik96303082009-07-13 21:29:25 -04001572
1573 /*
1574 * If we are below the extents threshold then we can add this as an
1575 * extent, and don't have to deal with the bitmap
1576 */
Li Zefan34d52cb2011-03-29 13:46:06 +08001577 if (ctl->free_extents < ctl->extents_thresh) {
Josef Bacik32cb0842011-03-18 16:16:21 -04001578 /*
1579 * If this block group has some small extents we don't want to
1580 * use up all of our free slots in the cache with them, we want
1581 * to reserve them to larger extents, however if we have plent
1582 * of cache left then go ahead an dadd them, no sense in adding
1583 * the overhead of a bitmap if we don't have to.
1584 */
1585 if (info->bytes <= block_group->sectorsize * 4) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001586 if (ctl->free_extents * 2 <= ctl->extents_thresh)
1587 return false;
Josef Bacik32cb0842011-03-18 16:16:21 -04001588 } else {
Li Zefan34d52cb2011-03-29 13:46:06 +08001589 return false;
Josef Bacik32cb0842011-03-18 16:16:21 -04001590 }
1591 }
Josef Bacik96303082009-07-13 21:29:25 -04001592
1593 /*
1594 * some block groups are so tiny they can't be enveloped by a bitmap, so
1595 * don't even bother to create a bitmap for this
1596 */
1597 if (BITS_PER_BITMAP * block_group->sectorsize >
1598 block_group->key.offset)
Li Zefan34d52cb2011-03-29 13:46:06 +08001599 return false;
1600
1601 return true;
1602}
1603
Josef Bacik2cdc3422011-05-27 14:07:49 -04001604static struct btrfs_free_space_op free_space_op = {
1605 .recalc_thresholds = recalculate_thresholds,
1606 .use_bitmap = use_bitmap,
1607};
1608
Li Zefan34d52cb2011-03-29 13:46:06 +08001609static int insert_into_bitmap(struct btrfs_free_space_ctl *ctl,
1610 struct btrfs_free_space *info)
1611{
1612 struct btrfs_free_space *bitmap_info;
Josef Bacik2cdc3422011-05-27 14:07:49 -04001613 struct btrfs_block_group_cache *block_group = NULL;
Li Zefan34d52cb2011-03-29 13:46:06 +08001614 int added = 0;
Josef Bacik2cdc3422011-05-27 14:07:49 -04001615 u64 bytes, offset, bytes_added;
Li Zefan34d52cb2011-03-29 13:46:06 +08001616 int ret;
Josef Bacik96303082009-07-13 21:29:25 -04001617
1618 bytes = info->bytes;
1619 offset = info->offset;
1620
Li Zefan34d52cb2011-03-29 13:46:06 +08001621 if (!ctl->op->use_bitmap(ctl, info))
1622 return 0;
1623
Josef Bacik2cdc3422011-05-27 14:07:49 -04001624 if (ctl->op == &free_space_op)
1625 block_group = ctl->private;
Chris Mason38e87882011-06-10 16:36:57 -04001626again:
Josef Bacik2cdc3422011-05-27 14:07:49 -04001627 /*
1628 * Since we link bitmaps right into the cluster we need to see if we
1629 * have a cluster here, and if so and it has our bitmap we need to add
1630 * the free space to that bitmap.
1631 */
1632 if (block_group && !list_empty(&block_group->cluster_list)) {
1633 struct btrfs_free_cluster *cluster;
1634 struct rb_node *node;
1635 struct btrfs_free_space *entry;
1636
1637 cluster = list_entry(block_group->cluster_list.next,
1638 struct btrfs_free_cluster,
1639 block_group_list);
1640 spin_lock(&cluster->lock);
1641 node = rb_first(&cluster->root);
1642 if (!node) {
1643 spin_unlock(&cluster->lock);
Chris Mason38e87882011-06-10 16:36:57 -04001644 goto no_cluster_bitmap;
Josef Bacik2cdc3422011-05-27 14:07:49 -04001645 }
1646
1647 entry = rb_entry(node, struct btrfs_free_space, offset_index);
1648 if (!entry->bitmap) {
1649 spin_unlock(&cluster->lock);
Chris Mason38e87882011-06-10 16:36:57 -04001650 goto no_cluster_bitmap;
Josef Bacik2cdc3422011-05-27 14:07:49 -04001651 }
1652
1653 if (entry->offset == offset_to_bitmap(ctl, offset)) {
1654 bytes_added = add_bytes_to_bitmap(ctl, entry,
1655 offset, bytes);
1656 bytes -= bytes_added;
1657 offset += bytes_added;
1658 }
1659 spin_unlock(&cluster->lock);
1660 if (!bytes) {
1661 ret = 1;
1662 goto out;
1663 }
1664 }
Chris Mason38e87882011-06-10 16:36:57 -04001665
1666no_cluster_bitmap:
Li Zefan34d52cb2011-03-29 13:46:06 +08001667 bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
Josef Bacik96303082009-07-13 21:29:25 -04001668 1, 0);
1669 if (!bitmap_info) {
1670 BUG_ON(added);
1671 goto new_bitmap;
1672 }
1673
Josef Bacik2cdc3422011-05-27 14:07:49 -04001674 bytes_added = add_bytes_to_bitmap(ctl, bitmap_info, offset, bytes);
1675 bytes -= bytes_added;
1676 offset += bytes_added;
1677 added = 0;
Josef Bacik96303082009-07-13 21:29:25 -04001678
1679 if (!bytes) {
1680 ret = 1;
1681 goto out;
1682 } else
1683 goto again;
1684
1685new_bitmap:
1686 if (info && info->bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001687 add_new_bitmap(ctl, info, offset);
Josef Bacik96303082009-07-13 21:29:25 -04001688 added = 1;
1689 info = NULL;
1690 goto again;
1691 } else {
Li Zefan34d52cb2011-03-29 13:46:06 +08001692 spin_unlock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04001693
1694 /* no pre-allocated info, allocate a new one */
1695 if (!info) {
Josef Bacikdc89e982011-01-28 17:05:48 -05001696 info = kmem_cache_zalloc(btrfs_free_space_cachep,
1697 GFP_NOFS);
Josef Bacik96303082009-07-13 21:29:25 -04001698 if (!info) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001699 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04001700 ret = -ENOMEM;
1701 goto out;
1702 }
1703 }
1704
1705 /* allocate the bitmap */
1706 info->bitmap = kzalloc(PAGE_CACHE_SIZE, GFP_NOFS);
Li Zefan34d52cb2011-03-29 13:46:06 +08001707 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04001708 if (!info->bitmap) {
1709 ret = -ENOMEM;
1710 goto out;
1711 }
1712 goto again;
1713 }
1714
1715out:
1716 if (info) {
1717 if (info->bitmap)
1718 kfree(info->bitmap);
Josef Bacikdc89e982011-01-28 17:05:48 -05001719 kmem_cache_free(btrfs_free_space_cachep, info);
Josef Bacik96303082009-07-13 21:29:25 -04001720 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04001721
1722 return ret;
1723}
1724
Chris Mason945d8962011-05-22 12:33:42 -04001725static bool try_merge_free_space(struct btrfs_free_space_ctl *ctl,
Li Zefanf333adb2010-11-09 14:57:39 +08001726 struct btrfs_free_space *info, bool update_stat)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001727{
Li Zefan120d66e2010-11-09 14:56:50 +08001728 struct btrfs_free_space *left_info;
1729 struct btrfs_free_space *right_info;
1730 bool merged = false;
1731 u64 offset = info->offset;
1732 u64 bytes = info->bytes;
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001733
Josef Bacik0f9dd462008-09-23 13:14:11 -04001734 /*
1735 * first we want to see if there is free space adjacent to the range we
1736 * are adding, if there is remove that struct and add a new one to
1737 * cover the entire range
1738 */
Li Zefan34d52cb2011-03-29 13:46:06 +08001739 right_info = tree_search_offset(ctl, offset + bytes, 0, 0);
Josef Bacik96303082009-07-13 21:29:25 -04001740 if (right_info && rb_prev(&right_info->offset_index))
1741 left_info = rb_entry(rb_prev(&right_info->offset_index),
1742 struct btrfs_free_space, offset_index);
1743 else
Li Zefan34d52cb2011-03-29 13:46:06 +08001744 left_info = tree_search_offset(ctl, offset - 1, 0, 0);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001745
Josef Bacik96303082009-07-13 21:29:25 -04001746 if (right_info && !right_info->bitmap) {
Li Zefanf333adb2010-11-09 14:57:39 +08001747 if (update_stat)
Li Zefan34d52cb2011-03-29 13:46:06 +08001748 unlink_free_space(ctl, right_info);
Li Zefanf333adb2010-11-09 14:57:39 +08001749 else
Li Zefan34d52cb2011-03-29 13:46:06 +08001750 __unlink_free_space(ctl, right_info);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001751 info->bytes += right_info->bytes;
Josef Bacikdc89e982011-01-28 17:05:48 -05001752 kmem_cache_free(btrfs_free_space_cachep, right_info);
Li Zefan120d66e2010-11-09 14:56:50 +08001753 merged = true;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001754 }
1755
Josef Bacik96303082009-07-13 21:29:25 -04001756 if (left_info && !left_info->bitmap &&
1757 left_info->offset + left_info->bytes == offset) {
Li Zefanf333adb2010-11-09 14:57:39 +08001758 if (update_stat)
Li Zefan34d52cb2011-03-29 13:46:06 +08001759 unlink_free_space(ctl, left_info);
Li Zefanf333adb2010-11-09 14:57:39 +08001760 else
Li Zefan34d52cb2011-03-29 13:46:06 +08001761 __unlink_free_space(ctl, left_info);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001762 info->offset = left_info->offset;
1763 info->bytes += left_info->bytes;
Josef Bacikdc89e982011-01-28 17:05:48 -05001764 kmem_cache_free(btrfs_free_space_cachep, left_info);
Li Zefan120d66e2010-11-09 14:56:50 +08001765 merged = true;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001766 }
1767
Li Zefan120d66e2010-11-09 14:56:50 +08001768 return merged;
1769}
1770
Li Zefan581bb052011-04-20 10:06:11 +08001771int __btrfs_add_free_space(struct btrfs_free_space_ctl *ctl,
1772 u64 offset, u64 bytes)
Li Zefan120d66e2010-11-09 14:56:50 +08001773{
1774 struct btrfs_free_space *info;
1775 int ret = 0;
1776
Josef Bacikdc89e982011-01-28 17:05:48 -05001777 info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS);
Li Zefan120d66e2010-11-09 14:56:50 +08001778 if (!info)
1779 return -ENOMEM;
1780
1781 info->offset = offset;
1782 info->bytes = bytes;
1783
Li Zefan34d52cb2011-03-29 13:46:06 +08001784 spin_lock(&ctl->tree_lock);
Li Zefan120d66e2010-11-09 14:56:50 +08001785
Li Zefan34d52cb2011-03-29 13:46:06 +08001786 if (try_merge_free_space(ctl, info, true))
Li Zefan120d66e2010-11-09 14:56:50 +08001787 goto link;
1788
1789 /*
1790 * There was no extent directly to the left or right of this new
1791 * extent then we know we're going to have to allocate a new extent, so
1792 * before we do that see if we need to drop this into a bitmap
1793 */
Li Zefan34d52cb2011-03-29 13:46:06 +08001794 ret = insert_into_bitmap(ctl, info);
Li Zefan120d66e2010-11-09 14:56:50 +08001795 if (ret < 0) {
1796 goto out;
1797 } else if (ret) {
1798 ret = 0;
1799 goto out;
1800 }
1801link:
Li Zefan34d52cb2011-03-29 13:46:06 +08001802 ret = link_free_space(ctl, info);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001803 if (ret)
Josef Bacikdc89e982011-01-28 17:05:48 -05001804 kmem_cache_free(btrfs_free_space_cachep, info);
Josef Bacik96303082009-07-13 21:29:25 -04001805out:
Li Zefan34d52cb2011-03-29 13:46:06 +08001806 spin_unlock(&ctl->tree_lock);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001807
Josef Bacik0f9dd462008-09-23 13:14:11 -04001808 if (ret) {
Josef Bacik96303082009-07-13 21:29:25 -04001809 printk(KERN_CRIT "btrfs: unable to add free space :%d\n", ret);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04001810 BUG_ON(ret == -EEXIST);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001811 }
1812
Josef Bacik0f9dd462008-09-23 13:14:11 -04001813 return ret;
1814}
1815
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001816int btrfs_remove_free_space(struct btrfs_block_group_cache *block_group,
1817 u64 offset, u64 bytes)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001818{
Li Zefan34d52cb2011-03-29 13:46:06 +08001819 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001820 struct btrfs_free_space *info;
Josef Bacik96303082009-07-13 21:29:25 -04001821 struct btrfs_free_space *next_info = NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001822 int ret = 0;
1823
Li Zefan34d52cb2011-03-29 13:46:06 +08001824 spin_lock(&ctl->tree_lock);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001825
Josef Bacik96303082009-07-13 21:29:25 -04001826again:
Li Zefan34d52cb2011-03-29 13:46:06 +08001827 info = tree_search_offset(ctl, offset, 0, 0);
Josef Bacik96303082009-07-13 21:29:25 -04001828 if (!info) {
Josef Bacik6606bb92009-07-31 11:03:58 -04001829 /*
1830 * oops didn't find an extent that matched the space we wanted
1831 * to remove, look for a bitmap instead
1832 */
Li Zefan34d52cb2011-03-29 13:46:06 +08001833 info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
Josef Bacik6606bb92009-07-31 11:03:58 -04001834 1, 0);
1835 if (!info) {
1836 WARN_ON(1);
1837 goto out_lock;
1838 }
Josef Bacik96303082009-07-13 21:29:25 -04001839 }
1840
1841 if (info->bytes < bytes && rb_next(&info->offset_index)) {
1842 u64 end;
1843 next_info = rb_entry(rb_next(&info->offset_index),
1844 struct btrfs_free_space,
1845 offset_index);
1846
1847 if (next_info->bitmap)
Li Zefan34d52cb2011-03-29 13:46:06 +08001848 end = next_info->offset +
1849 BITS_PER_BITMAP * ctl->unit - 1;
Josef Bacik96303082009-07-13 21:29:25 -04001850 else
1851 end = next_info->offset + next_info->bytes;
1852
1853 if (next_info->bytes < bytes ||
1854 next_info->offset > offset || offset > end) {
1855 printk(KERN_CRIT "Found free space at %llu, size %llu,"
1856 " trying to use %llu\n",
1857 (unsigned long long)info->offset,
1858 (unsigned long long)info->bytes,
1859 (unsigned long long)bytes);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001860 WARN_ON(1);
1861 ret = -EINVAL;
Josef Bacik96303082009-07-13 21:29:25 -04001862 goto out_lock;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001863 }
Josef Bacik96303082009-07-13 21:29:25 -04001864
1865 info = next_info;
1866 }
1867
1868 if (info->bytes == bytes) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001869 unlink_free_space(ctl, info);
Josef Bacik96303082009-07-13 21:29:25 -04001870 if (info->bitmap) {
1871 kfree(info->bitmap);
Li Zefan34d52cb2011-03-29 13:46:06 +08001872 ctl->total_bitmaps--;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001873 }
Josef Bacikdc89e982011-01-28 17:05:48 -05001874 kmem_cache_free(btrfs_free_space_cachep, info);
Josef Bacik96303082009-07-13 21:29:25 -04001875 goto out_lock;
1876 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04001877
Josef Bacik96303082009-07-13 21:29:25 -04001878 if (!info->bitmap && info->offset == offset) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001879 unlink_free_space(ctl, info);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001880 info->offset += bytes;
1881 info->bytes -= bytes;
Li Zefan34d52cb2011-03-29 13:46:06 +08001882 link_free_space(ctl, info);
Josef Bacik96303082009-07-13 21:29:25 -04001883 goto out_lock;
1884 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04001885
Josef Bacik96303082009-07-13 21:29:25 -04001886 if (!info->bitmap && info->offset <= offset &&
1887 info->offset + info->bytes >= offset + bytes) {
Chris Mason9b49c9b2008-09-24 11:23:25 -04001888 u64 old_start = info->offset;
1889 /*
1890 * we're freeing space in the middle of the info,
1891 * this can happen during tree log replay
1892 *
1893 * first unlink the old info and then
1894 * insert it again after the hole we're creating
1895 */
Li Zefan34d52cb2011-03-29 13:46:06 +08001896 unlink_free_space(ctl, info);
Chris Mason9b49c9b2008-09-24 11:23:25 -04001897 if (offset + bytes < info->offset + info->bytes) {
1898 u64 old_end = info->offset + info->bytes;
1899
1900 info->offset = offset + bytes;
1901 info->bytes = old_end - info->offset;
Li Zefan34d52cb2011-03-29 13:46:06 +08001902 ret = link_free_space(ctl, info);
Josef Bacik96303082009-07-13 21:29:25 -04001903 WARN_ON(ret);
1904 if (ret)
1905 goto out_lock;
Chris Mason9b49c9b2008-09-24 11:23:25 -04001906 } else {
1907 /* the hole we're creating ends at the end
1908 * of the info struct, just free the info
1909 */
Josef Bacikdc89e982011-01-28 17:05:48 -05001910 kmem_cache_free(btrfs_free_space_cachep, info);
Chris Mason9b49c9b2008-09-24 11:23:25 -04001911 }
Li Zefan34d52cb2011-03-29 13:46:06 +08001912 spin_unlock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04001913
1914 /* step two, insert a new info struct to cover
1915 * anything before the hole
Chris Mason9b49c9b2008-09-24 11:23:25 -04001916 */
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001917 ret = btrfs_add_free_space(block_group, old_start,
1918 offset - old_start);
Josef Bacik96303082009-07-13 21:29:25 -04001919 WARN_ON(ret);
1920 goto out;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001921 }
Josef Bacik96303082009-07-13 21:29:25 -04001922
Li Zefan34d52cb2011-03-29 13:46:06 +08001923 ret = remove_from_bitmap(ctl, info, &offset, &bytes);
Josef Bacik96303082009-07-13 21:29:25 -04001924 if (ret == -EAGAIN)
1925 goto again;
1926 BUG_ON(ret);
1927out_lock:
Li Zefan34d52cb2011-03-29 13:46:06 +08001928 spin_unlock(&ctl->tree_lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001929out:
Josef Bacik25179202008-10-29 14:49:05 -04001930 return ret;
1931}
1932
Josef Bacik0f9dd462008-09-23 13:14:11 -04001933void btrfs_dump_free_space(struct btrfs_block_group_cache *block_group,
1934 u64 bytes)
1935{
Li Zefan34d52cb2011-03-29 13:46:06 +08001936 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001937 struct btrfs_free_space *info;
1938 struct rb_node *n;
1939 int count = 0;
1940
Li Zefan34d52cb2011-03-29 13:46:06 +08001941 for (n = rb_first(&ctl->free_space_offset); n; n = rb_next(n)) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001942 info = rb_entry(n, struct btrfs_free_space, offset_index);
1943 if (info->bytes >= bytes)
1944 count++;
Josef Bacik96303082009-07-13 21:29:25 -04001945 printk(KERN_CRIT "entry offset %llu, bytes %llu, bitmap %s\n",
Joel Becker21380932009-04-21 12:38:29 -07001946 (unsigned long long)info->offset,
Josef Bacik96303082009-07-13 21:29:25 -04001947 (unsigned long long)info->bytes,
1948 (info->bitmap) ? "yes" : "no");
Josef Bacik0f9dd462008-09-23 13:14:11 -04001949 }
Josef Bacik96303082009-07-13 21:29:25 -04001950 printk(KERN_INFO "block group has cluster?: %s\n",
1951 list_empty(&block_group->cluster_list) ? "no" : "yes");
Josef Bacik0f9dd462008-09-23 13:14:11 -04001952 printk(KERN_INFO "%d blocks of free space at or bigger than bytes is"
1953 "\n", count);
1954}
1955
Li Zefan34d52cb2011-03-29 13:46:06 +08001956void btrfs_init_free_space_ctl(struct btrfs_block_group_cache *block_group)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001957{
Li Zefan34d52cb2011-03-29 13:46:06 +08001958 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001959
Li Zefan34d52cb2011-03-29 13:46:06 +08001960 spin_lock_init(&ctl->tree_lock);
1961 ctl->unit = block_group->sectorsize;
1962 ctl->start = block_group->key.objectid;
1963 ctl->private = block_group;
1964 ctl->op = &free_space_op;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001965
Li Zefan34d52cb2011-03-29 13:46:06 +08001966 /*
1967 * we only want to have 32k of ram per block group for keeping
1968 * track of free space, and if we pass 1/2 of that we want to
1969 * start converting things over to using bitmaps
1970 */
1971 ctl->extents_thresh = ((1024 * 32) / 2) /
1972 sizeof(struct btrfs_free_space);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001973}
1974
Chris Masonfa9c0d792009-04-03 09:47:43 -04001975/*
1976 * for a given cluster, put all of its extents back into the free
1977 * space cache. If the block group passed doesn't match the block group
1978 * pointed to by the cluster, someone else raced in and freed the
1979 * cluster already. In that case, we just return without changing anything
1980 */
1981static int
1982__btrfs_return_cluster_to_free_space(
1983 struct btrfs_block_group_cache *block_group,
1984 struct btrfs_free_cluster *cluster)
1985{
Li Zefan34d52cb2011-03-29 13:46:06 +08001986 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04001987 struct btrfs_free_space *entry;
1988 struct rb_node *node;
1989
1990 spin_lock(&cluster->lock);
1991 if (cluster->block_group != block_group)
1992 goto out;
1993
Josef Bacik96303082009-07-13 21:29:25 -04001994 cluster->block_group = NULL;
Chris Masonfa9c0d792009-04-03 09:47:43 -04001995 cluster->window_start = 0;
Josef Bacik96303082009-07-13 21:29:25 -04001996 list_del_init(&cluster->block_group_list);
Josef Bacik96303082009-07-13 21:29:25 -04001997
Chris Masonfa9c0d792009-04-03 09:47:43 -04001998 node = rb_first(&cluster->root);
Josef Bacik96303082009-07-13 21:29:25 -04001999 while (node) {
Josef Bacik4e69b592011-03-21 10:11:24 -04002000 bool bitmap;
2001
Chris Masonfa9c0d792009-04-03 09:47:43 -04002002 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2003 node = rb_next(&entry->offset_index);
2004 rb_erase(&entry->offset_index, &cluster->root);
Josef Bacik4e69b592011-03-21 10:11:24 -04002005
2006 bitmap = (entry->bitmap != NULL);
2007 if (!bitmap)
Li Zefan34d52cb2011-03-29 13:46:06 +08002008 try_merge_free_space(ctl, entry, false);
2009 tree_insert_offset(&ctl->free_space_offset,
Josef Bacik4e69b592011-03-21 10:11:24 -04002010 entry->offset, &entry->offset_index, bitmap);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002011 }
Eric Paris6bef4d32010-02-23 19:43:04 +00002012 cluster->root = RB_ROOT;
Josef Bacik96303082009-07-13 21:29:25 -04002013
Chris Masonfa9c0d792009-04-03 09:47:43 -04002014out:
2015 spin_unlock(&cluster->lock);
Josef Bacik96303082009-07-13 21:29:25 -04002016 btrfs_put_block_group(block_group);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002017 return 0;
2018}
2019
Chris Mason09655372011-05-21 09:27:38 -04002020void __btrfs_remove_free_space_cache_locked(struct btrfs_free_space_ctl *ctl)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002021{
2022 struct btrfs_free_space *info;
2023 struct rb_node *node;
Li Zefan581bb052011-04-20 10:06:11 +08002024
Li Zefan581bb052011-04-20 10:06:11 +08002025 while ((node = rb_last(&ctl->free_space_offset)) != NULL) {
2026 info = rb_entry(node, struct btrfs_free_space, offset_index);
Josef Bacik9b90f512011-06-24 16:02:51 +00002027 if (!info->bitmap) {
2028 unlink_free_space(ctl, info);
2029 kmem_cache_free(btrfs_free_space_cachep, info);
2030 } else {
2031 free_bitmap(ctl, info);
2032 }
Li Zefan581bb052011-04-20 10:06:11 +08002033 if (need_resched()) {
2034 spin_unlock(&ctl->tree_lock);
2035 cond_resched();
2036 spin_lock(&ctl->tree_lock);
2037 }
2038 }
Chris Mason09655372011-05-21 09:27:38 -04002039}
2040
2041void __btrfs_remove_free_space_cache(struct btrfs_free_space_ctl *ctl)
2042{
2043 spin_lock(&ctl->tree_lock);
2044 __btrfs_remove_free_space_cache_locked(ctl);
Li Zefan581bb052011-04-20 10:06:11 +08002045 spin_unlock(&ctl->tree_lock);
2046}
2047
Josef Bacik0f9dd462008-09-23 13:14:11 -04002048void btrfs_remove_free_space_cache(struct btrfs_block_group_cache *block_group)
2049{
Li Zefan34d52cb2011-03-29 13:46:06 +08002050 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002051 struct btrfs_free_cluster *cluster;
Josef Bacik96303082009-07-13 21:29:25 -04002052 struct list_head *head;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002053
Li Zefan34d52cb2011-03-29 13:46:06 +08002054 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04002055 while ((head = block_group->cluster_list.next) !=
2056 &block_group->cluster_list) {
2057 cluster = list_entry(head, struct btrfs_free_cluster,
2058 block_group_list);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002059
2060 WARN_ON(cluster->block_group != block_group);
2061 __btrfs_return_cluster_to_free_space(block_group, cluster);
Josef Bacik96303082009-07-13 21:29:25 -04002062 if (need_resched()) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002063 spin_unlock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04002064 cond_resched();
Li Zefan34d52cb2011-03-29 13:46:06 +08002065 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04002066 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04002067 }
Chris Mason09655372011-05-21 09:27:38 -04002068 __btrfs_remove_free_space_cache_locked(ctl);
Li Zefan34d52cb2011-03-29 13:46:06 +08002069 spin_unlock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002070
Josef Bacik0f9dd462008-09-23 13:14:11 -04002071}
2072
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002073u64 btrfs_find_space_for_alloc(struct btrfs_block_group_cache *block_group,
2074 u64 offset, u64 bytes, u64 empty_size)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002075{
Li Zefan34d52cb2011-03-29 13:46:06 +08002076 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002077 struct btrfs_free_space *entry = NULL;
Josef Bacik96303082009-07-13 21:29:25 -04002078 u64 bytes_search = bytes + empty_size;
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002079 u64 ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002080
Li Zefan34d52cb2011-03-29 13:46:06 +08002081 spin_lock(&ctl->tree_lock);
2082 entry = find_free_space(ctl, &offset, &bytes_search);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002083 if (!entry)
Josef Bacik96303082009-07-13 21:29:25 -04002084 goto out;
2085
2086 ret = offset;
2087 if (entry->bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002088 bitmap_clear_bits(ctl, entry, offset, bytes);
Li Zefanedf6e2d2010-11-09 14:50:07 +08002089 if (!entry->bytes)
Li Zefan34d52cb2011-03-29 13:46:06 +08002090 free_bitmap(ctl, entry);
Josef Bacik96303082009-07-13 21:29:25 -04002091 } else {
Li Zefan34d52cb2011-03-29 13:46:06 +08002092 unlink_free_space(ctl, entry);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002093 entry->offset += bytes;
2094 entry->bytes -= bytes;
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002095 if (!entry->bytes)
Josef Bacikdc89e982011-01-28 17:05:48 -05002096 kmem_cache_free(btrfs_free_space_cachep, entry);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002097 else
Li Zefan34d52cb2011-03-29 13:46:06 +08002098 link_free_space(ctl, entry);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002099 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002100
Josef Bacik96303082009-07-13 21:29:25 -04002101out:
Li Zefan34d52cb2011-03-29 13:46:06 +08002102 spin_unlock(&ctl->tree_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04002103
Josef Bacik0f9dd462008-09-23 13:14:11 -04002104 return ret;
2105}
Chris Masonfa9c0d792009-04-03 09:47:43 -04002106
2107/*
2108 * given a cluster, put all of its extents back into the free space
2109 * cache. If a block group is passed, this function will only free
2110 * a cluster that belongs to the passed block group.
2111 *
2112 * Otherwise, it'll get a reference on the block group pointed to by the
2113 * cluster and remove the cluster from it.
2114 */
2115int btrfs_return_cluster_to_free_space(
2116 struct btrfs_block_group_cache *block_group,
2117 struct btrfs_free_cluster *cluster)
2118{
Li Zefan34d52cb2011-03-29 13:46:06 +08002119 struct btrfs_free_space_ctl *ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002120 int ret;
2121
2122 /* first, get a safe pointer to the block group */
2123 spin_lock(&cluster->lock);
2124 if (!block_group) {
2125 block_group = cluster->block_group;
2126 if (!block_group) {
2127 spin_unlock(&cluster->lock);
2128 return 0;
2129 }
2130 } else if (cluster->block_group != block_group) {
2131 /* someone else has already freed it don't redo their work */
2132 spin_unlock(&cluster->lock);
2133 return 0;
2134 }
2135 atomic_inc(&block_group->count);
2136 spin_unlock(&cluster->lock);
2137
Li Zefan34d52cb2011-03-29 13:46:06 +08002138 ctl = block_group->free_space_ctl;
2139
Chris Masonfa9c0d792009-04-03 09:47:43 -04002140 /* now return any extents the cluster had on it */
Li Zefan34d52cb2011-03-29 13:46:06 +08002141 spin_lock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002142 ret = __btrfs_return_cluster_to_free_space(block_group, cluster);
Li Zefan34d52cb2011-03-29 13:46:06 +08002143 spin_unlock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002144
2145 /* finally drop our ref */
2146 btrfs_put_block_group(block_group);
2147 return ret;
2148}
2149
Josef Bacik96303082009-07-13 21:29:25 -04002150static u64 btrfs_alloc_from_bitmap(struct btrfs_block_group_cache *block_group,
2151 struct btrfs_free_cluster *cluster,
Josef Bacik4e69b592011-03-21 10:11:24 -04002152 struct btrfs_free_space *entry,
Josef Bacik96303082009-07-13 21:29:25 -04002153 u64 bytes, u64 min_start)
2154{
Li Zefan34d52cb2011-03-29 13:46:06 +08002155 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik96303082009-07-13 21:29:25 -04002156 int err;
2157 u64 search_start = cluster->window_start;
2158 u64 search_bytes = bytes;
2159 u64 ret = 0;
2160
Josef Bacik96303082009-07-13 21:29:25 -04002161 search_start = min_start;
2162 search_bytes = bytes;
2163
Li Zefan34d52cb2011-03-29 13:46:06 +08002164 err = search_bitmap(ctl, entry, &search_start, &search_bytes);
Josef Bacik96303082009-07-13 21:29:25 -04002165 if (err)
Josef Bacik4e69b592011-03-21 10:11:24 -04002166 return 0;
Josef Bacik96303082009-07-13 21:29:25 -04002167
2168 ret = search_start;
Miao Xiebb3ac5a2011-08-05 09:32:35 +00002169 __bitmap_clear_bits(ctl, entry, ret, bytes);
Josef Bacik96303082009-07-13 21:29:25 -04002170
2171 return ret;
2172}
2173
Chris Masonfa9c0d792009-04-03 09:47:43 -04002174/*
2175 * given a cluster, try to allocate 'bytes' from it, returns 0
2176 * if it couldn't find anything suitably large, or a logical disk offset
2177 * if things worked out
2178 */
2179u64 btrfs_alloc_from_cluster(struct btrfs_block_group_cache *block_group,
2180 struct btrfs_free_cluster *cluster, u64 bytes,
2181 u64 min_start)
2182{
Li Zefan34d52cb2011-03-29 13:46:06 +08002183 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002184 struct btrfs_free_space *entry = NULL;
2185 struct rb_node *node;
2186 u64 ret = 0;
2187
2188 spin_lock(&cluster->lock);
2189 if (bytes > cluster->max_size)
2190 goto out;
2191
2192 if (cluster->block_group != block_group)
2193 goto out;
2194
2195 node = rb_first(&cluster->root);
2196 if (!node)
2197 goto out;
2198
2199 entry = rb_entry(node, struct btrfs_free_space, offset_index);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002200 while(1) {
Josef Bacik4e69b592011-03-21 10:11:24 -04002201 if (entry->bytes < bytes ||
2202 (!entry->bitmap && entry->offset < min_start)) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04002203 node = rb_next(&entry->offset_index);
2204 if (!node)
2205 break;
2206 entry = rb_entry(node, struct btrfs_free_space,
2207 offset_index);
2208 continue;
2209 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04002210
Josef Bacik4e69b592011-03-21 10:11:24 -04002211 if (entry->bitmap) {
2212 ret = btrfs_alloc_from_bitmap(block_group,
2213 cluster, entry, bytes,
2214 min_start);
2215 if (ret == 0) {
Josef Bacik4e69b592011-03-21 10:11:24 -04002216 node = rb_next(&entry->offset_index);
2217 if (!node)
2218 break;
2219 entry = rb_entry(node, struct btrfs_free_space,
2220 offset_index);
2221 continue;
2222 }
2223 } else {
Josef Bacik4e69b592011-03-21 10:11:24 -04002224 ret = entry->offset;
2225
2226 entry->offset += bytes;
2227 entry->bytes -= bytes;
2228 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04002229
Li Zefan5e71b5d2010-11-09 14:55:34 +08002230 if (entry->bytes == 0)
Chris Masonfa9c0d792009-04-03 09:47:43 -04002231 rb_erase(&entry->offset_index, &cluster->root);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002232 break;
2233 }
2234out:
2235 spin_unlock(&cluster->lock);
Josef Bacik96303082009-07-13 21:29:25 -04002236
Li Zefan5e71b5d2010-11-09 14:55:34 +08002237 if (!ret)
2238 return 0;
2239
Li Zefan34d52cb2011-03-29 13:46:06 +08002240 spin_lock(&ctl->tree_lock);
Li Zefan5e71b5d2010-11-09 14:55:34 +08002241
Li Zefan34d52cb2011-03-29 13:46:06 +08002242 ctl->free_space -= bytes;
Li Zefan5e71b5d2010-11-09 14:55:34 +08002243 if (entry->bytes == 0) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002244 ctl->free_extents--;
Josef Bacik4e69b592011-03-21 10:11:24 -04002245 if (entry->bitmap) {
2246 kfree(entry->bitmap);
Li Zefan34d52cb2011-03-29 13:46:06 +08002247 ctl->total_bitmaps--;
2248 ctl->op->recalc_thresholds(ctl);
Josef Bacik4e69b592011-03-21 10:11:24 -04002249 }
Josef Bacikdc89e982011-01-28 17:05:48 -05002250 kmem_cache_free(btrfs_free_space_cachep, entry);
Li Zefan5e71b5d2010-11-09 14:55:34 +08002251 }
2252
Li Zefan34d52cb2011-03-29 13:46:06 +08002253 spin_unlock(&ctl->tree_lock);
Li Zefan5e71b5d2010-11-09 14:55:34 +08002254
Chris Masonfa9c0d792009-04-03 09:47:43 -04002255 return ret;
2256}
2257
Josef Bacik96303082009-07-13 21:29:25 -04002258static int btrfs_bitmap_cluster(struct btrfs_block_group_cache *block_group,
2259 struct btrfs_free_space *entry,
2260 struct btrfs_free_cluster *cluster,
2261 u64 offset, u64 bytes, u64 min_bytes)
2262{
Li Zefan34d52cb2011-03-29 13:46:06 +08002263 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik96303082009-07-13 21:29:25 -04002264 unsigned long next_zero;
2265 unsigned long i;
2266 unsigned long search_bits;
2267 unsigned long total_bits;
2268 unsigned long found_bits;
2269 unsigned long start = 0;
2270 unsigned long total_found = 0;
Josef Bacik4e69b592011-03-21 10:11:24 -04002271 int ret;
Josef Bacik96303082009-07-13 21:29:25 -04002272 bool found = false;
2273
2274 i = offset_to_bit(entry->offset, block_group->sectorsize,
2275 max_t(u64, offset, entry->offset));
Josef Bacikd0a365e2011-03-18 15:27:43 -04002276 search_bits = bytes_to_bits(bytes, block_group->sectorsize);
2277 total_bits = bytes_to_bits(min_bytes, block_group->sectorsize);
Josef Bacik96303082009-07-13 21:29:25 -04002278
2279again:
2280 found_bits = 0;
2281 for (i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, i);
2282 i < BITS_PER_BITMAP;
2283 i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, i + 1)) {
2284 next_zero = find_next_zero_bit(entry->bitmap,
2285 BITS_PER_BITMAP, i);
2286 if (next_zero - i >= search_bits) {
2287 found_bits = next_zero - i;
2288 break;
2289 }
2290 i = next_zero;
2291 }
2292
2293 if (!found_bits)
Josef Bacik4e69b592011-03-21 10:11:24 -04002294 return -ENOSPC;
Josef Bacik96303082009-07-13 21:29:25 -04002295
2296 if (!found) {
2297 start = i;
2298 found = true;
2299 }
2300
2301 total_found += found_bits;
2302
2303 if (cluster->max_size < found_bits * block_group->sectorsize)
2304 cluster->max_size = found_bits * block_group->sectorsize;
2305
2306 if (total_found < total_bits) {
2307 i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, next_zero);
2308 if (i - start > total_bits * 2) {
2309 total_found = 0;
2310 cluster->max_size = 0;
2311 found = false;
2312 }
2313 goto again;
2314 }
2315
2316 cluster->window_start = start * block_group->sectorsize +
2317 entry->offset;
Li Zefan34d52cb2011-03-29 13:46:06 +08002318 rb_erase(&entry->offset_index, &ctl->free_space_offset);
Josef Bacik4e69b592011-03-21 10:11:24 -04002319 ret = tree_insert_offset(&cluster->root, entry->offset,
2320 &entry->offset_index, 1);
2321 BUG_ON(ret);
Josef Bacik96303082009-07-13 21:29:25 -04002322
2323 return 0;
2324}
2325
Chris Masonfa9c0d792009-04-03 09:47:43 -04002326/*
Josef Bacik4e69b592011-03-21 10:11:24 -04002327 * This searches the block group for just extents to fill the cluster with.
2328 */
Josef Bacik3de85bb2011-05-25 13:07:37 -04002329static noinline int
2330setup_cluster_no_bitmap(struct btrfs_block_group_cache *block_group,
2331 struct btrfs_free_cluster *cluster,
2332 struct list_head *bitmaps, u64 offset, u64 bytes,
2333 u64 min_bytes)
Josef Bacik4e69b592011-03-21 10:11:24 -04002334{
Li Zefan34d52cb2011-03-29 13:46:06 +08002335 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik4e69b592011-03-21 10:11:24 -04002336 struct btrfs_free_space *first = NULL;
2337 struct btrfs_free_space *entry = NULL;
2338 struct btrfs_free_space *prev = NULL;
2339 struct btrfs_free_space *last;
2340 struct rb_node *node;
2341 u64 window_start;
2342 u64 window_free;
2343 u64 max_extent;
2344 u64 max_gap = 128 * 1024;
2345
Li Zefan34d52cb2011-03-29 13:46:06 +08002346 entry = tree_search_offset(ctl, offset, 0, 1);
Josef Bacik4e69b592011-03-21 10:11:24 -04002347 if (!entry)
2348 return -ENOSPC;
2349
2350 /*
2351 * We don't want bitmaps, so just move along until we find a normal
2352 * extent entry.
2353 */
2354 while (entry->bitmap) {
Josef Bacik86d4a772011-05-25 13:03:16 -04002355 if (list_empty(&entry->list))
2356 list_add_tail(&entry->list, bitmaps);
Josef Bacik4e69b592011-03-21 10:11:24 -04002357 node = rb_next(&entry->offset_index);
2358 if (!node)
2359 return -ENOSPC;
2360 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2361 }
2362
2363 window_start = entry->offset;
2364 window_free = entry->bytes;
2365 max_extent = entry->bytes;
2366 first = entry;
2367 last = entry;
2368 prev = entry;
2369
2370 while (window_free <= min_bytes) {
2371 node = rb_next(&entry->offset_index);
2372 if (!node)
2373 return -ENOSPC;
2374 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2375
Josef Bacik86d4a772011-05-25 13:03:16 -04002376 if (entry->bitmap) {
2377 if (list_empty(&entry->list))
2378 list_add_tail(&entry->list, bitmaps);
Josef Bacik4e69b592011-03-21 10:11:24 -04002379 continue;
Josef Bacik86d4a772011-05-25 13:03:16 -04002380 }
2381
Josef Bacik4e69b592011-03-21 10:11:24 -04002382 /*
2383 * we haven't filled the empty size and the window is
2384 * very large. reset and try again
2385 */
2386 if (entry->offset - (prev->offset + prev->bytes) > max_gap ||
2387 entry->offset - window_start > (min_bytes * 2)) {
2388 first = entry;
2389 window_start = entry->offset;
2390 window_free = entry->bytes;
2391 last = entry;
2392 max_extent = entry->bytes;
2393 } else {
2394 last = entry;
2395 window_free += entry->bytes;
2396 if (entry->bytes > max_extent)
2397 max_extent = entry->bytes;
2398 }
2399 prev = entry;
2400 }
2401
2402 cluster->window_start = first->offset;
2403
2404 node = &first->offset_index;
2405
2406 /*
2407 * now we've found our entries, pull them out of the free space
2408 * cache and put them into the cluster rbtree
2409 */
2410 do {
2411 int ret;
2412
2413 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2414 node = rb_next(&entry->offset_index);
2415 if (entry->bitmap)
2416 continue;
2417
Li Zefan34d52cb2011-03-29 13:46:06 +08002418 rb_erase(&entry->offset_index, &ctl->free_space_offset);
Josef Bacik4e69b592011-03-21 10:11:24 -04002419 ret = tree_insert_offset(&cluster->root, entry->offset,
2420 &entry->offset_index, 0);
2421 BUG_ON(ret);
2422 } while (node && entry != last);
2423
2424 cluster->max_size = max_extent;
2425
2426 return 0;
2427}
2428
2429/*
2430 * This specifically looks for bitmaps that may work in the cluster, we assume
2431 * that we have already failed to find extents that will work.
2432 */
Josef Bacik3de85bb2011-05-25 13:07:37 -04002433static noinline int
2434setup_cluster_bitmap(struct btrfs_block_group_cache *block_group,
2435 struct btrfs_free_cluster *cluster,
2436 struct list_head *bitmaps, u64 offset, u64 bytes,
2437 u64 min_bytes)
Josef Bacik4e69b592011-03-21 10:11:24 -04002438{
Li Zefan34d52cb2011-03-29 13:46:06 +08002439 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik4e69b592011-03-21 10:11:24 -04002440 struct btrfs_free_space *entry;
2441 struct rb_node *node;
2442 int ret = -ENOSPC;
2443
Li Zefan34d52cb2011-03-29 13:46:06 +08002444 if (ctl->total_bitmaps == 0)
Josef Bacik4e69b592011-03-21 10:11:24 -04002445 return -ENOSPC;
2446
Josef Bacik86d4a772011-05-25 13:03:16 -04002447 /*
2448 * First check our cached list of bitmaps and see if there is an entry
2449 * here that will work.
2450 */
2451 list_for_each_entry(entry, bitmaps, list) {
2452 if (entry->bytes < min_bytes)
2453 continue;
2454 ret = btrfs_bitmap_cluster(block_group, entry, cluster, offset,
2455 bytes, min_bytes);
2456 if (!ret)
2457 return 0;
2458 }
2459
2460 /*
2461 * If we do have entries on our list and we are here then we didn't find
2462 * anything, so go ahead and get the next entry after the last entry in
2463 * this list and start the search from there.
2464 */
2465 if (!list_empty(bitmaps)) {
2466 entry = list_entry(bitmaps->prev, struct btrfs_free_space,
2467 list);
2468 node = rb_next(&entry->offset_index);
2469 if (!node)
2470 return -ENOSPC;
2471 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2472 goto search;
2473 }
2474
Li Zefan34d52cb2011-03-29 13:46:06 +08002475 entry = tree_search_offset(ctl, offset_to_bitmap(ctl, offset), 0, 1);
Josef Bacik4e69b592011-03-21 10:11:24 -04002476 if (!entry)
2477 return -ENOSPC;
2478
Josef Bacik86d4a772011-05-25 13:03:16 -04002479search:
Josef Bacik4e69b592011-03-21 10:11:24 -04002480 node = &entry->offset_index;
2481 do {
2482 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2483 node = rb_next(&entry->offset_index);
2484 if (!entry->bitmap)
2485 continue;
2486 if (entry->bytes < min_bytes)
2487 continue;
2488 ret = btrfs_bitmap_cluster(block_group, entry, cluster, offset,
2489 bytes, min_bytes);
2490 } while (ret && node);
2491
2492 return ret;
2493}
2494
2495/*
Chris Masonfa9c0d792009-04-03 09:47:43 -04002496 * here we try to find a cluster of blocks in a block group. The goal
2497 * is to find at least bytes free and up to empty_size + bytes free.
2498 * We might not find them all in one contiguous area.
2499 *
2500 * returns zero and sets up cluster if things worked out, otherwise
2501 * it returns -enospc
2502 */
2503int btrfs_find_space_cluster(struct btrfs_trans_handle *trans,
Chris Mason451d7582009-06-09 20:28:34 -04002504 struct btrfs_root *root,
Chris Masonfa9c0d792009-04-03 09:47:43 -04002505 struct btrfs_block_group_cache *block_group,
2506 struct btrfs_free_cluster *cluster,
2507 u64 offset, u64 bytes, u64 empty_size)
2508{
Li Zefan34d52cb2011-03-29 13:46:06 +08002509 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik86d4a772011-05-25 13:03:16 -04002510 struct list_head bitmaps;
2511 struct btrfs_free_space *entry, *tmp;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002512 u64 min_bytes;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002513 int ret;
2514
2515 /* for metadata, allow allocates with more holes */
Chris Mason451d7582009-06-09 20:28:34 -04002516 if (btrfs_test_opt(root, SSD_SPREAD)) {
2517 min_bytes = bytes + empty_size;
2518 } else if (block_group->flags & BTRFS_BLOCK_GROUP_METADATA) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04002519 /*
2520 * we want to do larger allocations when we are
2521 * flushing out the delayed refs, it helps prevent
2522 * making more work as we go along.
2523 */
2524 if (trans->transaction->delayed_refs.flushing)
2525 min_bytes = max(bytes, (bytes + empty_size) >> 1);
2526 else
2527 min_bytes = max(bytes, (bytes + empty_size) >> 4);
2528 } else
2529 min_bytes = max(bytes, (bytes + empty_size) >> 2);
2530
Li Zefan34d52cb2011-03-29 13:46:06 +08002531 spin_lock(&ctl->tree_lock);
Josef Bacik7d0d2e82011-03-18 15:13:42 -04002532
2533 /*
2534 * If we know we don't have enough space to make a cluster don't even
2535 * bother doing all the work to try and find one.
2536 */
Li Zefan34d52cb2011-03-29 13:46:06 +08002537 if (ctl->free_space < min_bytes) {
2538 spin_unlock(&ctl->tree_lock);
Josef Bacik7d0d2e82011-03-18 15:13:42 -04002539 return -ENOSPC;
2540 }
2541
Chris Masonfa9c0d792009-04-03 09:47:43 -04002542 spin_lock(&cluster->lock);
2543
2544 /* someone already found a cluster, hooray */
2545 if (cluster->block_group) {
2546 ret = 0;
2547 goto out;
2548 }
Josef Bacik4e69b592011-03-21 10:11:24 -04002549
Josef Bacik86d4a772011-05-25 13:03:16 -04002550 INIT_LIST_HEAD(&bitmaps);
2551 ret = setup_cluster_no_bitmap(block_group, cluster, &bitmaps, offset,
2552 bytes, min_bytes);
Josef Bacik4e69b592011-03-21 10:11:24 -04002553 if (ret)
Josef Bacik86d4a772011-05-25 13:03:16 -04002554 ret = setup_cluster_bitmap(block_group, cluster, &bitmaps,
2555 offset, bytes, min_bytes);
2556
2557 /* Clear our temporary list */
2558 list_for_each_entry_safe(entry, tmp, &bitmaps, list)
2559 list_del_init(&entry->list);
Josef Bacik4e69b592011-03-21 10:11:24 -04002560
2561 if (!ret) {
2562 atomic_inc(&block_group->count);
2563 list_add_tail(&cluster->block_group_list,
2564 &block_group->cluster_list);
2565 cluster->block_group = block_group;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002566 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04002567out:
2568 spin_unlock(&cluster->lock);
Li Zefan34d52cb2011-03-29 13:46:06 +08002569 spin_unlock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002570
2571 return ret;
2572}
2573
2574/*
2575 * simple code to zero out a cluster
2576 */
2577void btrfs_init_free_cluster(struct btrfs_free_cluster *cluster)
2578{
2579 spin_lock_init(&cluster->lock);
2580 spin_lock_init(&cluster->refill_lock);
Eric Paris6bef4d32010-02-23 19:43:04 +00002581 cluster->root = RB_ROOT;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002582 cluster->max_size = 0;
2583 INIT_LIST_HEAD(&cluster->block_group_list);
2584 cluster->block_group = NULL;
2585}
2586
Li Dongyangf7039b12011-03-24 10:24:28 +00002587int btrfs_trim_block_group(struct btrfs_block_group_cache *block_group,
2588 u64 *trimmed, u64 start, u64 end, u64 minlen)
2589{
Li Zefan34d52cb2011-03-29 13:46:06 +08002590 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Li Dongyangf7039b12011-03-24 10:24:28 +00002591 struct btrfs_free_space *entry = NULL;
2592 struct btrfs_fs_info *fs_info = block_group->fs_info;
2593 u64 bytes = 0;
2594 u64 actually_trimmed;
2595 int ret = 0;
2596
2597 *trimmed = 0;
2598
2599 while (start < end) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002600 spin_lock(&ctl->tree_lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00002601
Li Zefan34d52cb2011-03-29 13:46:06 +08002602 if (ctl->free_space < minlen) {
2603 spin_unlock(&ctl->tree_lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00002604 break;
2605 }
2606
Li Zefan34d52cb2011-03-29 13:46:06 +08002607 entry = tree_search_offset(ctl, start, 0, 1);
Li Dongyangf7039b12011-03-24 10:24:28 +00002608 if (!entry)
Li Zefan34d52cb2011-03-29 13:46:06 +08002609 entry = tree_search_offset(ctl,
2610 offset_to_bitmap(ctl, start),
Li Dongyangf7039b12011-03-24 10:24:28 +00002611 1, 1);
2612
2613 if (!entry || entry->offset >= end) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002614 spin_unlock(&ctl->tree_lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00002615 break;
2616 }
2617
2618 if (entry->bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002619 ret = search_bitmap(ctl, entry, &start, &bytes);
Li Dongyangf7039b12011-03-24 10:24:28 +00002620 if (!ret) {
2621 if (start >= end) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002622 spin_unlock(&ctl->tree_lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00002623 break;
2624 }
2625 bytes = min(bytes, end - start);
Li Zefan34d52cb2011-03-29 13:46:06 +08002626 bitmap_clear_bits(ctl, entry, start, bytes);
Li Dongyangf7039b12011-03-24 10:24:28 +00002627 if (entry->bytes == 0)
Li Zefan34d52cb2011-03-29 13:46:06 +08002628 free_bitmap(ctl, entry);
Li Dongyangf7039b12011-03-24 10:24:28 +00002629 } else {
2630 start = entry->offset + BITS_PER_BITMAP *
2631 block_group->sectorsize;
Li Zefan34d52cb2011-03-29 13:46:06 +08002632 spin_unlock(&ctl->tree_lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00002633 ret = 0;
2634 continue;
2635 }
2636 } else {
2637 start = entry->offset;
2638 bytes = min(entry->bytes, end - start);
Li Zefan34d52cb2011-03-29 13:46:06 +08002639 unlink_free_space(ctl, entry);
Li Zefanf789b682011-04-25 19:43:52 -04002640 kmem_cache_free(btrfs_free_space_cachep, entry);
Li Dongyangf7039b12011-03-24 10:24:28 +00002641 }
2642
Li Zefan34d52cb2011-03-29 13:46:06 +08002643 spin_unlock(&ctl->tree_lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00002644
2645 if (bytes >= minlen) {
Josef Bacikfb25e912011-07-26 17:00:46 -04002646 struct btrfs_space_info *space_info;
2647 int update = 0;
2648
2649 space_info = block_group->space_info;
2650 spin_lock(&space_info->lock);
2651 spin_lock(&block_group->lock);
2652 if (!block_group->ro) {
2653 block_group->reserved += bytes;
2654 space_info->bytes_reserved += bytes;
2655 update = 1;
2656 }
2657 spin_unlock(&block_group->lock);
2658 spin_unlock(&space_info->lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00002659
2660 ret = btrfs_error_discard_extent(fs_info->extent_root,
2661 start,
2662 bytes,
2663 &actually_trimmed);
2664
Li Zefan34d52cb2011-03-29 13:46:06 +08002665 btrfs_add_free_space(block_group, start, bytes);
Josef Bacikfb25e912011-07-26 17:00:46 -04002666 if (update) {
2667 spin_lock(&space_info->lock);
2668 spin_lock(&block_group->lock);
2669 if (block_group->ro)
2670 space_info->bytes_readonly += bytes;
2671 block_group->reserved -= bytes;
2672 space_info->bytes_reserved -= bytes;
2673 spin_unlock(&space_info->lock);
2674 spin_unlock(&block_group->lock);
2675 }
Li Dongyangf7039b12011-03-24 10:24:28 +00002676
2677 if (ret)
2678 break;
2679 *trimmed += actually_trimmed;
2680 }
2681 start += bytes;
2682 bytes = 0;
2683
2684 if (fatal_signal_pending(current)) {
2685 ret = -ERESTARTSYS;
2686 break;
2687 }
2688
2689 cond_resched();
2690 }
2691
2692 return ret;
2693}
Li Zefan581bb052011-04-20 10:06:11 +08002694
2695/*
2696 * Find the left-most item in the cache tree, and then return the
2697 * smallest inode number in the item.
2698 *
2699 * Note: the returned inode number may not be the smallest one in
2700 * the tree, if the left-most item is a bitmap.
2701 */
2702u64 btrfs_find_ino_for_alloc(struct btrfs_root *fs_root)
2703{
2704 struct btrfs_free_space_ctl *ctl = fs_root->free_ino_ctl;
2705 struct btrfs_free_space *entry = NULL;
2706 u64 ino = 0;
2707
2708 spin_lock(&ctl->tree_lock);
2709
2710 if (RB_EMPTY_ROOT(&ctl->free_space_offset))
2711 goto out;
2712
2713 entry = rb_entry(rb_first(&ctl->free_space_offset),
2714 struct btrfs_free_space, offset_index);
2715
2716 if (!entry->bitmap) {
2717 ino = entry->offset;
2718
2719 unlink_free_space(ctl, entry);
2720 entry->offset++;
2721 entry->bytes--;
2722 if (!entry->bytes)
2723 kmem_cache_free(btrfs_free_space_cachep, entry);
2724 else
2725 link_free_space(ctl, entry);
2726 } else {
2727 u64 offset = 0;
2728 u64 count = 1;
2729 int ret;
2730
2731 ret = search_bitmap(ctl, entry, &offset, &count);
2732 BUG_ON(ret);
2733
2734 ino = offset;
2735 bitmap_clear_bits(ctl, entry, offset, 1);
2736 if (entry->bytes == 0)
2737 free_bitmap(ctl, entry);
2738 }
2739out:
2740 spin_unlock(&ctl->tree_lock);
2741
2742 return ino;
2743}
Li Zefan82d59022011-04-20 10:33:24 +08002744
2745struct inode *lookup_free_ino_inode(struct btrfs_root *root,
2746 struct btrfs_path *path)
2747{
2748 struct inode *inode = NULL;
2749
2750 spin_lock(&root->cache_lock);
2751 if (root->cache_inode)
2752 inode = igrab(root->cache_inode);
2753 spin_unlock(&root->cache_lock);
2754 if (inode)
2755 return inode;
2756
2757 inode = __lookup_free_space_inode(root, path, 0);
2758 if (IS_ERR(inode))
2759 return inode;
2760
2761 spin_lock(&root->cache_lock);
David Sterba7841cb22011-05-31 18:07:27 +02002762 if (!btrfs_fs_closing(root->fs_info))
Li Zefan82d59022011-04-20 10:33:24 +08002763 root->cache_inode = igrab(inode);
2764 spin_unlock(&root->cache_lock);
2765
2766 return inode;
2767}
2768
2769int create_free_ino_inode(struct btrfs_root *root,
2770 struct btrfs_trans_handle *trans,
2771 struct btrfs_path *path)
2772{
2773 return __create_free_space_inode(root, trans, path,
2774 BTRFS_FREE_INO_OBJECTID, 0);
2775}
2776
2777int load_free_ino_cache(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
2778{
2779 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
2780 struct btrfs_path *path;
2781 struct inode *inode;
2782 int ret = 0;
2783 u64 root_gen = btrfs_root_generation(&root->root_item);
2784
Chris Mason4b9465c2011-06-03 09:36:29 -04002785 if (!btrfs_test_opt(root, INODE_MAP_CACHE))
2786 return 0;
2787
Li Zefan82d59022011-04-20 10:33:24 +08002788 /*
2789 * If we're unmounting then just return, since this does a search on the
2790 * normal root and not the commit root and we could deadlock.
2791 */
David Sterba7841cb22011-05-31 18:07:27 +02002792 if (btrfs_fs_closing(fs_info))
Li Zefan82d59022011-04-20 10:33:24 +08002793 return 0;
2794
2795 path = btrfs_alloc_path();
2796 if (!path)
2797 return 0;
2798
2799 inode = lookup_free_ino_inode(root, path);
2800 if (IS_ERR(inode))
2801 goto out;
2802
2803 if (root_gen != BTRFS_I(inode)->generation)
2804 goto out_put;
2805
2806 ret = __load_free_space_cache(root, inode, ctl, path, 0);
2807
2808 if (ret < 0)
2809 printk(KERN_ERR "btrfs: failed to load free ino cache for "
2810 "root %llu\n", root->root_key.objectid);
2811out_put:
2812 iput(inode);
2813out:
2814 btrfs_free_path(path);
2815 return ret;
2816}
2817
2818int btrfs_write_out_ino_cache(struct btrfs_root *root,
2819 struct btrfs_trans_handle *trans,
2820 struct btrfs_path *path)
2821{
2822 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
2823 struct inode *inode;
2824 int ret;
2825
Chris Mason4b9465c2011-06-03 09:36:29 -04002826 if (!btrfs_test_opt(root, INODE_MAP_CACHE))
2827 return 0;
2828
Li Zefan82d59022011-04-20 10:33:24 +08002829 inode = lookup_free_ino_inode(root, path);
2830 if (IS_ERR(inode))
2831 return 0;
2832
2833 ret = __btrfs_write_out_cache(root, inode, ctl, NULL, trans, path, 0);
Josef Bacikc09544e2011-08-30 10:19:10 -04002834 if (ret) {
2835 btrfs_delalloc_release_metadata(inode, inode->i_size);
2836#ifdef DEBUG
Li Zefan82d59022011-04-20 10:33:24 +08002837 printk(KERN_ERR "btrfs: failed to write free ino cache "
2838 "for root %llu\n", root->root_key.objectid);
Josef Bacikc09544e2011-08-30 10:19:10 -04002839#endif
2840 }
Li Zefan82d59022011-04-20 10:33:24 +08002841
2842 iput(inode);
2843 return ret;
2844}