blob: 8c32434da2cbbba1c07087092aeac50f9d427f2b [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 Bacikc8174312011-11-02 09:29:35 -0400201 u64 needed_bytes;
Josef Bacik0af3d002010-06-21 14:48:16 -0400202 loff_t oldsize;
203 int ret = 0;
204
Liu Bo65450aa2011-09-11 10:52:24 -0400205 rsv = trans->block_rsv;
Josef Bacikc8174312011-11-02 09:29:35 -0400206 trans->block_rsv = &root->fs_info->global_block_rsv;
207
208 /* 1 for slack space, 1 for updating the inode */
209 needed_bytes = btrfs_calc_trunc_metadata_size(root, 1) +
210 btrfs_calc_trans_metadata_size(root, 1);
211
212 spin_lock(&trans->block_rsv->lock);
213 if (trans->block_rsv->reserved < needed_bytes) {
214 spin_unlock(&trans->block_rsv->lock);
215 trans->block_rsv = rsv;
216 return -ENOSPC;
217 }
218 spin_unlock(&trans->block_rsv->lock);
Josef Bacik0af3d002010-06-21 14:48:16 -0400219
220 oldsize = i_size_read(inode);
221 btrfs_i_size_write(inode, 0);
222 truncate_pagecache(inode, oldsize, 0);
223
224 /*
225 * We don't need an orphan item because truncating the free space cache
226 * will never be split across transactions.
227 */
228 ret = btrfs_truncate_inode_items(trans, root, inode,
229 0, BTRFS_EXTENT_DATA_KEY);
Liu Bo65450aa2011-09-11 10:52:24 -0400230
Josef Bacik0af3d002010-06-21 14:48:16 -0400231 if (ret) {
Josef Bacikc8174312011-11-02 09:29:35 -0400232 trans->block_rsv = rsv;
Josef Bacik0af3d002010-06-21 14:48:16 -0400233 WARN_ON(1);
234 return ret;
235 }
236
Li Zefan82d59022011-04-20 10:33:24 +0800237 ret = btrfs_update_inode(trans, root, inode);
Josef Bacikc8174312011-11-02 09:29:35 -0400238 trans->block_rsv = rsv;
239
Li Zefan82d59022011-04-20 10:33:24 +0800240 return ret;
Josef Bacik0af3d002010-06-21 14:48:16 -0400241}
242
Josef Bacik9d66e232010-08-25 16:54:15 -0400243static int readahead_cache(struct inode *inode)
244{
245 struct file_ra_state *ra;
246 unsigned long last_index;
247
248 ra = kzalloc(sizeof(*ra), GFP_NOFS);
249 if (!ra)
250 return -ENOMEM;
251
252 file_ra_state_init(ra, inode->i_mapping);
253 last_index = (i_size_read(inode) - 1) >> PAGE_CACHE_SHIFT;
254
255 page_cache_sync_readahead(inode->i_mapping, ra, NULL, 0, last_index);
256
257 kfree(ra);
258
259 return 0;
260}
261
Josef Bacika67509c2011-10-05 15:18:58 -0400262struct io_ctl {
263 void *cur, *orig;
264 struct page *page;
265 struct page **pages;
266 struct btrfs_root *root;
267 unsigned long size;
268 int index;
269 int num_pages;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400270 unsigned check_crcs:1;
Josef Bacika67509c2011-10-05 15:18:58 -0400271};
272
273static int io_ctl_init(struct io_ctl *io_ctl, struct inode *inode,
274 struct btrfs_root *root)
275{
276 memset(io_ctl, 0, sizeof(struct io_ctl));
277 io_ctl->num_pages = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
278 PAGE_CACHE_SHIFT;
279 io_ctl->pages = kzalloc(sizeof(struct page *) * io_ctl->num_pages,
280 GFP_NOFS);
281 if (!io_ctl->pages)
282 return -ENOMEM;
283 io_ctl->root = root;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400284 if (btrfs_ino(inode) != BTRFS_FREE_INO_OBJECTID)
285 io_ctl->check_crcs = 1;
Josef Bacika67509c2011-10-05 15:18:58 -0400286 return 0;
287}
288
289static void io_ctl_free(struct io_ctl *io_ctl)
290{
291 kfree(io_ctl->pages);
292}
293
294static void io_ctl_unmap_page(struct io_ctl *io_ctl)
295{
296 if (io_ctl->cur) {
297 kunmap(io_ctl->page);
298 io_ctl->cur = NULL;
299 io_ctl->orig = NULL;
300 }
301}
302
303static void io_ctl_map_page(struct io_ctl *io_ctl, int clear)
304{
305 WARN_ON(io_ctl->cur);
306 BUG_ON(io_ctl->index >= io_ctl->num_pages);
307 io_ctl->page = io_ctl->pages[io_ctl->index++];
308 io_ctl->cur = kmap(io_ctl->page);
309 io_ctl->orig = io_ctl->cur;
310 io_ctl->size = PAGE_CACHE_SIZE;
311 if (clear)
312 memset(io_ctl->cur, 0, PAGE_CACHE_SIZE);
313}
314
315static void io_ctl_drop_pages(struct io_ctl *io_ctl)
316{
317 int i;
318
319 io_ctl_unmap_page(io_ctl);
320
321 for (i = 0; i < io_ctl->num_pages; i++) {
322 ClearPageChecked(io_ctl->pages[i]);
323 unlock_page(io_ctl->pages[i]);
324 page_cache_release(io_ctl->pages[i]);
325 }
326}
327
328static int io_ctl_prepare_pages(struct io_ctl *io_ctl, struct inode *inode,
329 int uptodate)
330{
331 struct page *page;
332 gfp_t mask = btrfs_alloc_write_mask(inode->i_mapping);
333 int i;
334
335 for (i = 0; i < io_ctl->num_pages; i++) {
336 page = find_or_create_page(inode->i_mapping, i, mask);
337 if (!page) {
338 io_ctl_drop_pages(io_ctl);
339 return -ENOMEM;
340 }
341 io_ctl->pages[i] = page;
342 if (uptodate && !PageUptodate(page)) {
343 btrfs_readpage(NULL, page);
344 lock_page(page);
345 if (!PageUptodate(page)) {
346 printk(KERN_ERR "btrfs: error reading free "
347 "space cache\n");
348 io_ctl_drop_pages(io_ctl);
349 return -EIO;
350 }
351 }
352 }
353
354 return 0;
355}
356
357static void io_ctl_set_generation(struct io_ctl *io_ctl, u64 generation)
358{
359 u64 *val;
360
361 io_ctl_map_page(io_ctl, 1);
362
363 /*
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400364 * Skip the csum areas. If we don't check crcs then we just have a
365 * 64bit chunk at the front of the first page.
Josef Bacika67509c2011-10-05 15:18:58 -0400366 */
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400367 if (io_ctl->check_crcs) {
368 io_ctl->cur += (sizeof(u32) * io_ctl->num_pages);
369 io_ctl->size -= sizeof(u64) + (sizeof(u32) * io_ctl->num_pages);
370 } else {
371 io_ctl->cur += sizeof(u64);
372 io_ctl->size -= sizeof(u64) * 2;
373 }
Josef Bacika67509c2011-10-05 15:18:58 -0400374
375 val = io_ctl->cur;
376 *val = cpu_to_le64(generation);
377 io_ctl->cur += sizeof(u64);
Josef Bacika67509c2011-10-05 15:18:58 -0400378}
379
380static int io_ctl_check_generation(struct io_ctl *io_ctl, u64 generation)
381{
382 u64 *gen;
383
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400384 /*
385 * Skip the crc area. If we don't check crcs then we just have a 64bit
386 * chunk at the front of the first page.
387 */
388 if (io_ctl->check_crcs) {
389 io_ctl->cur += sizeof(u32) * io_ctl->num_pages;
390 io_ctl->size -= sizeof(u64) +
391 (sizeof(u32) * io_ctl->num_pages);
392 } else {
393 io_ctl->cur += sizeof(u64);
394 io_ctl->size -= sizeof(u64) * 2;
395 }
Josef Bacika67509c2011-10-05 15:18:58 -0400396
Josef Bacika67509c2011-10-05 15:18:58 -0400397 gen = io_ctl->cur;
398 if (le64_to_cpu(*gen) != generation) {
399 printk_ratelimited(KERN_ERR "btrfs: space cache generation "
400 "(%Lu) does not match inode (%Lu)\n", *gen,
401 generation);
402 io_ctl_unmap_page(io_ctl);
403 return -EIO;
404 }
405 io_ctl->cur += sizeof(u64);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400406 return 0;
407}
408
409static void io_ctl_set_crc(struct io_ctl *io_ctl, int index)
410{
411 u32 *tmp;
412 u32 crc = ~(u32)0;
413 unsigned offset = 0;
414
415 if (!io_ctl->check_crcs) {
416 io_ctl_unmap_page(io_ctl);
417 return;
418 }
419
420 if (index == 0)
421 offset = sizeof(u32) * io_ctl->num_pages;;
422
423 crc = btrfs_csum_data(io_ctl->root, io_ctl->orig + offset, crc,
424 PAGE_CACHE_SIZE - offset);
425 btrfs_csum_final(crc, (char *)&crc);
426 io_ctl_unmap_page(io_ctl);
427 tmp = kmap(io_ctl->pages[0]);
428 tmp += index;
429 *tmp = crc;
430 kunmap(io_ctl->pages[0]);
431}
432
433static int io_ctl_check_crc(struct io_ctl *io_ctl, int index)
434{
435 u32 *tmp, val;
436 u32 crc = ~(u32)0;
437 unsigned offset = 0;
438
439 if (!io_ctl->check_crcs) {
440 io_ctl_map_page(io_ctl, 0);
441 return 0;
442 }
443
444 if (index == 0)
445 offset = sizeof(u32) * io_ctl->num_pages;
446
447 tmp = kmap(io_ctl->pages[0]);
448 tmp += index;
449 val = *tmp;
450 kunmap(io_ctl->pages[0]);
451
452 io_ctl_map_page(io_ctl, 0);
453 crc = btrfs_csum_data(io_ctl->root, io_ctl->orig + offset, crc,
454 PAGE_CACHE_SIZE - offset);
455 btrfs_csum_final(crc, (char *)&crc);
456 if (val != crc) {
457 printk_ratelimited(KERN_ERR "btrfs: csum mismatch on free "
458 "space cache\n");
459 io_ctl_unmap_page(io_ctl);
460 return -EIO;
461 }
462
Josef Bacika67509c2011-10-05 15:18:58 -0400463 return 0;
464}
465
466static int io_ctl_add_entry(struct io_ctl *io_ctl, u64 offset, u64 bytes,
467 void *bitmap)
468{
469 struct btrfs_free_space_entry *entry;
470
471 if (!io_ctl->cur)
472 return -ENOSPC;
473
474 entry = io_ctl->cur;
475 entry->offset = cpu_to_le64(offset);
476 entry->bytes = cpu_to_le64(bytes);
477 entry->type = (bitmap) ? BTRFS_FREE_SPACE_BITMAP :
478 BTRFS_FREE_SPACE_EXTENT;
479 io_ctl->cur += sizeof(struct btrfs_free_space_entry);
480 io_ctl->size -= sizeof(struct btrfs_free_space_entry);
481
482 if (io_ctl->size >= sizeof(struct btrfs_free_space_entry))
483 return 0;
484
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400485 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
Josef Bacika67509c2011-10-05 15:18:58 -0400486
487 /* No more pages to map */
488 if (io_ctl->index >= io_ctl->num_pages)
489 return 0;
490
491 /* map the next page */
492 io_ctl_map_page(io_ctl, 1);
493 return 0;
494}
495
496static int io_ctl_add_bitmap(struct io_ctl *io_ctl, void *bitmap)
497{
498 if (!io_ctl->cur)
499 return -ENOSPC;
500
501 /*
502 * If we aren't at the start of the current page, unmap this one and
503 * map the next one if there is any left.
504 */
505 if (io_ctl->cur != io_ctl->orig) {
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400506 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
Josef Bacika67509c2011-10-05 15:18:58 -0400507 if (io_ctl->index >= io_ctl->num_pages)
508 return -ENOSPC;
509 io_ctl_map_page(io_ctl, 0);
510 }
511
512 memcpy(io_ctl->cur, bitmap, PAGE_CACHE_SIZE);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400513 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
Josef Bacika67509c2011-10-05 15:18:58 -0400514 if (io_ctl->index < io_ctl->num_pages)
515 io_ctl_map_page(io_ctl, 0);
516 return 0;
517}
518
519static void io_ctl_zero_remaining_pages(struct io_ctl *io_ctl)
520{
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400521 /*
522 * If we're not on the boundary we know we've modified the page and we
523 * need to crc the page.
524 */
525 if (io_ctl->cur != io_ctl->orig)
526 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
527 else
528 io_ctl_unmap_page(io_ctl);
Josef Bacika67509c2011-10-05 15:18:58 -0400529
530 while (io_ctl->index < io_ctl->num_pages) {
531 io_ctl_map_page(io_ctl, 1);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400532 io_ctl_set_crc(io_ctl, io_ctl->index - 1);
Josef Bacika67509c2011-10-05 15:18:58 -0400533 }
534}
535
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400536static int io_ctl_read_entry(struct io_ctl *io_ctl,
537 struct btrfs_free_space *entry, u8 *type)
Josef Bacika67509c2011-10-05 15:18:58 -0400538{
539 struct btrfs_free_space_entry *e;
Josef Bacik2f120c02011-11-10 20:45:05 -0500540 int ret;
541
542 if (!io_ctl->cur) {
543 ret = io_ctl_check_crc(io_ctl, io_ctl->index);
544 if (ret)
545 return ret;
546 }
Josef Bacika67509c2011-10-05 15:18:58 -0400547
548 e = io_ctl->cur;
549 entry->offset = le64_to_cpu(e->offset);
550 entry->bytes = le64_to_cpu(e->bytes);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400551 *type = e->type;
Josef Bacika67509c2011-10-05 15:18:58 -0400552 io_ctl->cur += sizeof(struct btrfs_free_space_entry);
553 io_ctl->size -= sizeof(struct btrfs_free_space_entry);
554
555 if (io_ctl->size >= sizeof(struct btrfs_free_space_entry))
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400556 return 0;
Josef Bacika67509c2011-10-05 15:18:58 -0400557
558 io_ctl_unmap_page(io_ctl);
559
Josef Bacik2f120c02011-11-10 20:45:05 -0500560 return 0;
Josef Bacika67509c2011-10-05 15:18:58 -0400561}
562
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400563static int io_ctl_read_bitmap(struct io_ctl *io_ctl,
564 struct btrfs_free_space *entry)
Josef Bacika67509c2011-10-05 15:18:58 -0400565{
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400566 int ret;
567
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400568 ret = io_ctl_check_crc(io_ctl, io_ctl->index);
569 if (ret)
570 return ret;
571
Josef Bacika67509c2011-10-05 15:18:58 -0400572 memcpy(entry->bitmap, io_ctl->cur, PAGE_CACHE_SIZE);
573 io_ctl_unmap_page(io_ctl);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400574
575 return 0;
Josef Bacika67509c2011-10-05 15:18:58 -0400576}
577
Li Zefan0414efa2011-04-20 10:20:14 +0800578int __load_free_space_cache(struct btrfs_root *root, struct inode *inode,
579 struct btrfs_free_space_ctl *ctl,
580 struct btrfs_path *path, u64 offset)
Josef Bacik9d66e232010-08-25 16:54:15 -0400581{
Josef Bacik9d66e232010-08-25 16:54:15 -0400582 struct btrfs_free_space_header *header;
583 struct extent_buffer *leaf;
Josef Bacika67509c2011-10-05 15:18:58 -0400584 struct io_ctl io_ctl;
Josef Bacik9d66e232010-08-25 16:54:15 -0400585 struct btrfs_key key;
Josef Bacika67509c2011-10-05 15:18:58 -0400586 struct btrfs_free_space *e, *n;
Josef Bacik9d66e232010-08-25 16:54:15 -0400587 struct list_head bitmaps;
588 u64 num_entries;
589 u64 num_bitmaps;
590 u64 generation;
Josef Bacika67509c2011-10-05 15:18:58 -0400591 u8 type;
Josef Bacikf6a39822011-06-06 10:50:35 -0400592 int ret = 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400593
594 INIT_LIST_HEAD(&bitmaps);
595
Josef Bacik9d66e232010-08-25 16:54:15 -0400596 /* Nothing in the space cache, goodbye */
Li Zefan0414efa2011-04-20 10:20:14 +0800597 if (!i_size_read(inode))
Josef Bacika67509c2011-10-05 15:18:58 -0400598 return 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400599
600 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
Li Zefan0414efa2011-04-20 10:20:14 +0800601 key.offset = offset;
Josef Bacik9d66e232010-08-25 16:54:15 -0400602 key.type = 0;
603
604 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Li Zefan0414efa2011-04-20 10:20:14 +0800605 if (ret < 0)
Josef Bacika67509c2011-10-05 15:18:58 -0400606 return 0;
Li Zefan0414efa2011-04-20 10:20:14 +0800607 else if (ret > 0) {
Chris Mason945d8962011-05-22 12:33:42 -0400608 btrfs_release_path(path);
Josef Bacika67509c2011-10-05 15:18:58 -0400609 return 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400610 }
611
Li Zefan0414efa2011-04-20 10:20:14 +0800612 ret = -1;
613
Josef Bacik9d66e232010-08-25 16:54:15 -0400614 leaf = path->nodes[0];
615 header = btrfs_item_ptr(leaf, path->slots[0],
616 struct btrfs_free_space_header);
617 num_entries = btrfs_free_space_entries(leaf, header);
618 num_bitmaps = btrfs_free_space_bitmaps(leaf, header);
619 generation = btrfs_free_space_generation(leaf, header);
Chris Mason945d8962011-05-22 12:33:42 -0400620 btrfs_release_path(path);
Josef Bacik9d66e232010-08-25 16:54:15 -0400621
622 if (BTRFS_I(inode)->generation != generation) {
623 printk(KERN_ERR "btrfs: free space inode generation (%llu) did"
Li Zefan0414efa2011-04-20 10:20:14 +0800624 " not match free space cache generation (%llu)\n",
Josef Bacik9d66e232010-08-25 16:54:15 -0400625 (unsigned long long)BTRFS_I(inode)->generation,
Li Zefan0414efa2011-04-20 10:20:14 +0800626 (unsigned long long)generation);
Josef Bacika67509c2011-10-05 15:18:58 -0400627 return 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400628 }
629
630 if (!num_entries)
Josef Bacika67509c2011-10-05 15:18:58 -0400631 return 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400632
Josef Bacika67509c2011-10-05 15:18:58 -0400633 io_ctl_init(&io_ctl, inode, root);
Josef Bacik9d66e232010-08-25 16:54:15 -0400634 ret = readahead_cache(inode);
Li Zefan0414efa2011-04-20 10:20:14 +0800635 if (ret)
Josef Bacik9d66e232010-08-25 16:54:15 -0400636 goto out;
Josef Bacik9d66e232010-08-25 16:54:15 -0400637
Josef Bacika67509c2011-10-05 15:18:58 -0400638 ret = io_ctl_prepare_pages(&io_ctl, inode, 1);
639 if (ret)
640 goto out;
Josef Bacik9d66e232010-08-25 16:54:15 -0400641
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400642 ret = io_ctl_check_crc(&io_ctl, 0);
643 if (ret)
644 goto free_cache;
645
Josef Bacika67509c2011-10-05 15:18:58 -0400646 ret = io_ctl_check_generation(&io_ctl, generation);
647 if (ret)
648 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400649
Josef Bacika67509c2011-10-05 15:18:58 -0400650 while (num_entries) {
651 e = kmem_cache_zalloc(btrfs_free_space_cachep,
652 GFP_NOFS);
653 if (!e)
Josef Bacik9d66e232010-08-25 16:54:15 -0400654 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400655
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400656 ret = io_ctl_read_entry(&io_ctl, e, &type);
657 if (ret) {
658 kmem_cache_free(btrfs_free_space_cachep, e);
659 goto free_cache;
660 }
661
Josef Bacika67509c2011-10-05 15:18:58 -0400662 if (!e->bytes) {
663 kmem_cache_free(btrfs_free_space_cachep, e);
664 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400665 }
Josef Bacik9d66e232010-08-25 16:54:15 -0400666
Josef Bacika67509c2011-10-05 15:18:58 -0400667 if (type == BTRFS_FREE_SPACE_EXTENT) {
668 spin_lock(&ctl->tree_lock);
669 ret = link_free_space(ctl, e);
670 spin_unlock(&ctl->tree_lock);
671 if (ret) {
672 printk(KERN_ERR "Duplicate entries in "
673 "free space cache, dumping\n");
Josef Bacikdc89e982011-01-28 17:05:48 -0500674 kmem_cache_free(btrfs_free_space_cachep, e);
Josef Bacik9d66e232010-08-25 16:54:15 -0400675 goto free_cache;
676 }
Josef Bacika67509c2011-10-05 15:18:58 -0400677 } else {
678 BUG_ON(!num_bitmaps);
679 num_bitmaps--;
680 e->bitmap = kzalloc(PAGE_CACHE_SIZE, GFP_NOFS);
681 if (!e->bitmap) {
682 kmem_cache_free(
683 btrfs_free_space_cachep, e);
684 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400685 }
Josef Bacika67509c2011-10-05 15:18:58 -0400686 spin_lock(&ctl->tree_lock);
687 ret = link_free_space(ctl, e);
688 ctl->total_bitmaps++;
689 ctl->op->recalc_thresholds(ctl);
690 spin_unlock(&ctl->tree_lock);
691 if (ret) {
692 printk(KERN_ERR "Duplicate entries in "
693 "free space cache, dumping\n");
694 kmem_cache_free(btrfs_free_space_cachep, e);
695 goto free_cache;
696 }
697 list_add_tail(&e->list, &bitmaps);
Josef Bacik9d66e232010-08-25 16:54:15 -0400698 }
699
Josef Bacika67509c2011-10-05 15:18:58 -0400700 num_entries--;
Josef Bacik9d66e232010-08-25 16:54:15 -0400701 }
702
Josef Bacik2f120c02011-11-10 20:45:05 -0500703 io_ctl_unmap_page(&io_ctl);
704
Josef Bacika67509c2011-10-05 15:18:58 -0400705 /*
706 * We add the bitmaps at the end of the entries in order that
707 * the bitmap entries are added to the cache.
708 */
709 list_for_each_entry_safe(e, n, &bitmaps, list) {
710 list_del_init(&e->list);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400711 ret = io_ctl_read_bitmap(&io_ctl, e);
712 if (ret)
713 goto free_cache;
Josef Bacika67509c2011-10-05 15:18:58 -0400714 }
715
716 io_ctl_drop_pages(&io_ctl);
Josef Bacik9d66e232010-08-25 16:54:15 -0400717 ret = 1;
718out:
Josef Bacika67509c2011-10-05 15:18:58 -0400719 io_ctl_free(&io_ctl);
Josef Bacik9d66e232010-08-25 16:54:15 -0400720 return ret;
Josef Bacik9d66e232010-08-25 16:54:15 -0400721free_cache:
Josef Bacika67509c2011-10-05 15:18:58 -0400722 io_ctl_drop_pages(&io_ctl);
Li Zefan0414efa2011-04-20 10:20:14 +0800723 __btrfs_remove_free_space_cache(ctl);
Josef Bacik9d66e232010-08-25 16:54:15 -0400724 goto out;
725}
726
Li Zefan0414efa2011-04-20 10:20:14 +0800727int load_free_space_cache(struct btrfs_fs_info *fs_info,
728 struct btrfs_block_group_cache *block_group)
Josef Bacik0cb59c92010-07-02 12:14:14 -0400729{
Li Zefan34d52cb2011-03-29 13:46:06 +0800730 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Li Zefan0414efa2011-04-20 10:20:14 +0800731 struct btrfs_root *root = fs_info->tree_root;
732 struct inode *inode;
733 struct btrfs_path *path;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400734 int ret = 0;
Li Zefan0414efa2011-04-20 10:20:14 +0800735 bool matched;
736 u64 used = btrfs_block_group_used(&block_group->item);
737
738 /*
739 * If we're unmounting then just return, since this does a search on the
740 * normal root and not the commit root and we could deadlock.
741 */
David Sterba7841cb22011-05-31 18:07:27 +0200742 if (btrfs_fs_closing(fs_info))
Li Zefan0414efa2011-04-20 10:20:14 +0800743 return 0;
744
745 /*
746 * If this block group has been marked to be cleared for one reason or
747 * another then we can't trust the on disk cache, so just return.
748 */
749 spin_lock(&block_group->lock);
750 if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) {
751 spin_unlock(&block_group->lock);
752 return 0;
753 }
754 spin_unlock(&block_group->lock);
755
756 path = btrfs_alloc_path();
757 if (!path)
758 return 0;
759
760 inode = lookup_free_space_inode(root, block_group, path);
761 if (IS_ERR(inode)) {
762 btrfs_free_path(path);
763 return 0;
764 }
765
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400766 /* We may have converted the inode and made the cache invalid. */
767 spin_lock(&block_group->lock);
768 if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) {
769 spin_unlock(&block_group->lock);
770 goto out;
771 }
772 spin_unlock(&block_group->lock);
773
Li Zefan0414efa2011-04-20 10:20:14 +0800774 ret = __load_free_space_cache(fs_info->tree_root, inode, ctl,
775 path, block_group->key.objectid);
776 btrfs_free_path(path);
777 if (ret <= 0)
778 goto out;
779
780 spin_lock(&ctl->tree_lock);
781 matched = (ctl->free_space == (block_group->key.offset - used -
782 block_group->bytes_super));
783 spin_unlock(&ctl->tree_lock);
784
785 if (!matched) {
786 __btrfs_remove_free_space_cache(ctl);
787 printk(KERN_ERR "block group %llu has an wrong amount of free "
788 "space\n", block_group->key.objectid);
789 ret = -1;
790 }
791out:
792 if (ret < 0) {
793 /* This cache is bogus, make sure it gets cleared */
794 spin_lock(&block_group->lock);
795 block_group->disk_cache_state = BTRFS_DC_CLEAR;
796 spin_unlock(&block_group->lock);
Li Zefan82d59022011-04-20 10:33:24 +0800797 ret = 0;
Li Zefan0414efa2011-04-20 10:20:14 +0800798
799 printk(KERN_ERR "btrfs: failed to load free space cache "
800 "for block group %llu\n", block_group->key.objectid);
801 }
802
803 iput(inode);
804 return ret;
805}
806
Josef Bacikc09544e2011-08-30 10:19:10 -0400807/**
808 * __btrfs_write_out_cache - write out cached info to an inode
809 * @root - the root the inode belongs to
810 * @ctl - the free space cache we are going to write out
811 * @block_group - the block_group for this cache if it belongs to a block_group
812 * @trans - the trans handle
813 * @path - the path to use
814 * @offset - the offset for the key we'll insert
815 *
816 * This function writes out a free space cache struct to disk for quick recovery
817 * on mount. This will return 0 if it was successfull in writing the cache out,
818 * and -1 if it was not.
819 */
Li Zefan0414efa2011-04-20 10:20:14 +0800820int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode,
821 struct btrfs_free_space_ctl *ctl,
822 struct btrfs_block_group_cache *block_group,
823 struct btrfs_trans_handle *trans,
824 struct btrfs_path *path, u64 offset)
Josef Bacik0cb59c92010-07-02 12:14:14 -0400825{
826 struct btrfs_free_space_header *header;
827 struct extent_buffer *leaf;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400828 struct rb_node *node;
829 struct list_head *pos, *n;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400830 struct extent_state *cached_state = NULL;
Josef Bacik43be2142011-04-01 14:55:00 +0000831 struct btrfs_free_cluster *cluster = NULL;
832 struct extent_io_tree *unpin = NULL;
Josef Bacika67509c2011-10-05 15:18:58 -0400833 struct io_ctl io_ctl;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400834 struct list_head bitmap_list;
835 struct btrfs_key key;
Josef Bacik43be2142011-04-01 14:55:00 +0000836 u64 start, end, len;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400837 int entries = 0;
838 int bitmaps = 0;
Josef Bacikc09544e2011-08-30 10:19:10 -0400839 int ret;
840 int err = -1;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400841
Josef Bacik0cb59c92010-07-02 12:14:14 -0400842 INIT_LIST_HEAD(&bitmap_list);
843
Li Zefan0414efa2011-04-20 10:20:14 +0800844 if (!i_size_read(inode))
845 return -1;
Josef Bacik2b209822010-12-03 13:17:53 -0500846
Josef Bacika67509c2011-10-05 15:18:58 -0400847 io_ctl_init(&io_ctl, inode, root);
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400848
Josef Bacik43be2142011-04-01 14:55:00 +0000849 /* Get the cluster for this block_group if it exists */
Li Zefan0414efa2011-04-20 10:20:14 +0800850 if (block_group && !list_empty(&block_group->cluster_list))
Josef Bacik43be2142011-04-01 14:55:00 +0000851 cluster = list_entry(block_group->cluster_list.next,
852 struct btrfs_free_cluster,
853 block_group_list);
854
855 /*
856 * We shouldn't have switched the pinned extents yet so this is the
857 * right one
858 */
859 unpin = root->fs_info->pinned_extents;
860
Josef Bacika67509c2011-10-05 15:18:58 -0400861 /* Lock all pages first so we can lock the extent safely. */
862 io_ctl_prepare_pages(&io_ctl, inode, 0);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400863
Josef Bacik0cb59c92010-07-02 12:14:14 -0400864 lock_extent_bits(&BTRFS_I(inode)->io_tree, 0, i_size_read(inode) - 1,
865 0, &cached_state, GFP_NOFS);
866
Josef Bacik43be2142011-04-01 14:55:00 +0000867 /*
868 * When searching for pinned extents, we need to start at our start
869 * offset.
870 */
Li Zefan0414efa2011-04-20 10:20:14 +0800871 if (block_group)
872 start = block_group->key.objectid;
Josef Bacik43be2142011-04-01 14:55:00 +0000873
Josef Bacikf75b1302011-10-05 10:00:18 -0400874 node = rb_first(&ctl->free_space_offset);
875 if (!node && cluster) {
876 node = rb_first(&cluster->root);
877 cluster = NULL;
878 }
879
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400880 /* Make sure we can fit our crcs into the first page */
881 if (io_ctl.check_crcs &&
882 (io_ctl.num_pages * sizeof(u32)) >= PAGE_CACHE_SIZE) {
883 WARN_ON(1);
884 goto out_nospc;
885 }
886
Josef Bacika67509c2011-10-05 15:18:58 -0400887 io_ctl_set_generation(&io_ctl, trans->transid);
888
Josef Bacik0cb59c92010-07-02 12:14:14 -0400889 /* Write out the extent entries */
Josef Bacika67509c2011-10-05 15:18:58 -0400890 while (node) {
891 struct btrfs_free_space *e;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400892
Josef Bacika67509c2011-10-05 15:18:58 -0400893 e = rb_entry(node, struct btrfs_free_space, offset_index);
894 entries++;
Josef Bacik43be2142011-04-01 14:55:00 +0000895
Josef Bacika67509c2011-10-05 15:18:58 -0400896 ret = io_ctl_add_entry(&io_ctl, e->offset, e->bytes,
897 e->bitmap);
898 if (ret)
899 goto out_nospc;
900
901 if (e->bitmap) {
902 list_add_tail(&e->list, &bitmap_list);
903 bitmaps++;
904 }
905 node = rb_next(node);
906 if (!node && cluster) {
907 node = rb_first(&cluster->root);
908 cluster = NULL;
909 }
910 }
911
912 /*
913 * We want to add any pinned extents to our free space cache
914 * so we don't leak the space
915 */
916 while (block_group && (start < block_group->key.objectid +
917 block_group->key.offset)) {
918 ret = find_first_extent_bit(unpin, start, &start, &end,
919 EXTENT_DIRTY);
920 if (ret) {
921 ret = 0;
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400922 break;
923 }
924
Josef Bacika67509c2011-10-05 15:18:58 -0400925 /* This pinned extent is out of our range */
926 if (start >= block_group->key.objectid +
927 block_group->key.offset)
928 break;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400929
Josef Bacika67509c2011-10-05 15:18:58 -0400930 len = block_group->key.objectid +
931 block_group->key.offset - start;
932 len = min(len, end + 1 - start);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400933
Josef Bacika67509c2011-10-05 15:18:58 -0400934 entries++;
935 ret = io_ctl_add_entry(&io_ctl, start, len, NULL);
936 if (ret)
937 goto out_nospc;
Josef Bacik2f356122011-06-10 15:31:13 -0400938
Josef Bacika67509c2011-10-05 15:18:58 -0400939 start = end + 1;
940 }
Josef Bacik0cb59c92010-07-02 12:14:14 -0400941
942 /* Write out the bitmaps */
943 list_for_each_safe(pos, n, &bitmap_list) {
Josef Bacik0cb59c92010-07-02 12:14:14 -0400944 struct btrfs_free_space *entry =
945 list_entry(pos, struct btrfs_free_space, list);
946
Josef Bacika67509c2011-10-05 15:18:58 -0400947 ret = io_ctl_add_bitmap(&io_ctl, entry->bitmap);
948 if (ret)
949 goto out_nospc;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400950 list_del_init(&entry->list);
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400951 }
952
Josef Bacik0cb59c92010-07-02 12:14:14 -0400953 /* Zero out the rest of the pages just to make sure */
Josef Bacika67509c2011-10-05 15:18:58 -0400954 io_ctl_zero_remaining_pages(&io_ctl);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400955
Josef Bacika67509c2011-10-05 15:18:58 -0400956 ret = btrfs_dirty_pages(root, inode, io_ctl.pages, io_ctl.num_pages,
957 0, i_size_read(inode), &cached_state);
958 io_ctl_drop_pages(&io_ctl);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400959 unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
960 i_size_read(inode) - 1, &cached_state, GFP_NOFS);
961
Josef Bacikc09544e2011-08-30 10:19:10 -0400962 if (ret)
Josef Bacik2f356122011-06-10 15:31:13 -0400963 goto out;
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400964
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400965
Josef Bacik549b4fd2011-10-05 16:33:53 -0400966 ret = filemap_write_and_wait(inode->i_mapping);
967 if (ret)
968 goto out;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400969
970 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
Li Zefan0414efa2011-04-20 10:20:14 +0800971 key.offset = offset;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400972 key.type = 0;
973
Josef Bacika9b5fcd2011-08-19 12:06:12 -0400974 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400975 if (ret < 0) {
Josef Bacika67509c2011-10-05 15:18:58 -0400976 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, inode->i_size - 1,
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400977 EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0, NULL,
978 GFP_NOFS);
Josef Bacik2f356122011-06-10 15:31:13 -0400979 goto out;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400980 }
981 leaf = path->nodes[0];
982 if (ret > 0) {
983 struct btrfs_key found_key;
984 BUG_ON(!path->slots[0]);
985 path->slots[0]--;
986 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
987 if (found_key.objectid != BTRFS_FREE_SPACE_OBJECTID ||
Li Zefan0414efa2011-04-20 10:20:14 +0800988 found_key.offset != offset) {
Josef Bacika67509c2011-10-05 15:18:58 -0400989 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0,
990 inode->i_size - 1,
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400991 EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0,
992 NULL, GFP_NOFS);
David Sterbab3b4aa72011-04-21 01:20:15 +0200993 btrfs_release_path(path);
Josef Bacik2f356122011-06-10 15:31:13 -0400994 goto out;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400995 }
996 }
Josef Bacik549b4fd2011-10-05 16:33:53 -0400997
998 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400999 header = btrfs_item_ptr(leaf, path->slots[0],
1000 struct btrfs_free_space_header);
1001 btrfs_set_free_space_entries(leaf, header, entries);
1002 btrfs_set_free_space_bitmaps(leaf, header, bitmaps);
1003 btrfs_set_free_space_generation(leaf, header, trans->transid);
1004 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02001005 btrfs_release_path(path);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001006
Josef Bacikc09544e2011-08-30 10:19:10 -04001007 err = 0;
Josef Bacik2f356122011-06-10 15:31:13 -04001008out:
Josef Bacika67509c2011-10-05 15:18:58 -04001009 io_ctl_free(&io_ctl);
Josef Bacikc09544e2011-08-30 10:19:10 -04001010 if (err) {
Josef Bacika67509c2011-10-05 15:18:58 -04001011 invalidate_inode_pages2(inode->i_mapping);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001012 BTRFS_I(inode)->generation = 0;
1013 }
Josef Bacik0cb59c92010-07-02 12:14:14 -04001014 btrfs_update_inode(trans, root, inode);
Josef Bacikc09544e2011-08-30 10:19:10 -04001015 return err;
Josef Bacika67509c2011-10-05 15:18:58 -04001016
1017out_nospc:
1018 list_for_each_safe(pos, n, &bitmap_list) {
1019 struct btrfs_free_space *entry =
1020 list_entry(pos, struct btrfs_free_space, list);
1021 list_del_init(&entry->list);
1022 }
1023 io_ctl_drop_pages(&io_ctl);
1024 unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
1025 i_size_read(inode) - 1, &cached_state, GFP_NOFS);
1026 goto out;
Li Zefan0414efa2011-04-20 10:20:14 +08001027}
1028
1029int btrfs_write_out_cache(struct btrfs_root *root,
1030 struct btrfs_trans_handle *trans,
1031 struct btrfs_block_group_cache *block_group,
1032 struct btrfs_path *path)
1033{
1034 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
1035 struct inode *inode;
1036 int ret = 0;
1037
1038 root = root->fs_info->tree_root;
1039
1040 spin_lock(&block_group->lock);
1041 if (block_group->disk_cache_state < BTRFS_DC_SETUP) {
1042 spin_unlock(&block_group->lock);
1043 return 0;
1044 }
1045 spin_unlock(&block_group->lock);
1046
1047 inode = lookup_free_space_inode(root, block_group, path);
1048 if (IS_ERR(inode))
1049 return 0;
1050
1051 ret = __btrfs_write_out_cache(root, inode, ctl, block_group, trans,
1052 path, block_group->key.objectid);
Josef Bacikc09544e2011-08-30 10:19:10 -04001053 if (ret) {
Li Zefan0414efa2011-04-20 10:20:14 +08001054 spin_lock(&block_group->lock);
1055 block_group->disk_cache_state = BTRFS_DC_ERROR;
1056 spin_unlock(&block_group->lock);
Li Zefan82d59022011-04-20 10:33:24 +08001057 ret = 0;
Josef Bacikc09544e2011-08-30 10:19:10 -04001058#ifdef DEBUG
Li Zefan0414efa2011-04-20 10:20:14 +08001059 printk(KERN_ERR "btrfs: failed to write free space cace "
1060 "for block group %llu\n", block_group->key.objectid);
Josef Bacikc09544e2011-08-30 10:19:10 -04001061#endif
Li Zefan0414efa2011-04-20 10:20:14 +08001062 }
1063
Josef Bacik0cb59c92010-07-02 12:14:14 -04001064 iput(inode);
1065 return ret;
1066}
1067
Li Zefan34d52cb2011-03-29 13:46:06 +08001068static inline unsigned long offset_to_bit(u64 bitmap_start, u32 unit,
Josef Bacik96303082009-07-13 21:29:25 -04001069 u64 offset)
1070{
1071 BUG_ON(offset < bitmap_start);
1072 offset -= bitmap_start;
Li Zefan34d52cb2011-03-29 13:46:06 +08001073 return (unsigned long)(div_u64(offset, unit));
Josef Bacik96303082009-07-13 21:29:25 -04001074}
1075
Li Zefan34d52cb2011-03-29 13:46:06 +08001076static inline unsigned long bytes_to_bits(u64 bytes, u32 unit)
Josef Bacik96303082009-07-13 21:29:25 -04001077{
Li Zefan34d52cb2011-03-29 13:46:06 +08001078 return (unsigned long)(div_u64(bytes, unit));
Josef Bacik96303082009-07-13 21:29:25 -04001079}
1080
Li Zefan34d52cb2011-03-29 13:46:06 +08001081static inline u64 offset_to_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001082 u64 offset)
1083{
1084 u64 bitmap_start;
1085 u64 bytes_per_bitmap;
1086
Li Zefan34d52cb2011-03-29 13:46:06 +08001087 bytes_per_bitmap = BITS_PER_BITMAP * ctl->unit;
1088 bitmap_start = offset - ctl->start;
Josef Bacik96303082009-07-13 21:29:25 -04001089 bitmap_start = div64_u64(bitmap_start, bytes_per_bitmap);
1090 bitmap_start *= bytes_per_bitmap;
Li Zefan34d52cb2011-03-29 13:46:06 +08001091 bitmap_start += ctl->start;
Josef Bacik96303082009-07-13 21:29:25 -04001092
1093 return bitmap_start;
1094}
Josef Bacik0f9dd462008-09-23 13:14:11 -04001095
1096static int tree_insert_offset(struct rb_root *root, u64 offset,
Josef Bacik96303082009-07-13 21:29:25 -04001097 struct rb_node *node, int bitmap)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001098{
1099 struct rb_node **p = &root->rb_node;
1100 struct rb_node *parent = NULL;
1101 struct btrfs_free_space *info;
1102
1103 while (*p) {
1104 parent = *p;
1105 info = rb_entry(parent, struct btrfs_free_space, offset_index);
1106
Josef Bacik96303082009-07-13 21:29:25 -04001107 if (offset < info->offset) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001108 p = &(*p)->rb_left;
Josef Bacik96303082009-07-13 21:29:25 -04001109 } else if (offset > info->offset) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001110 p = &(*p)->rb_right;
Josef Bacik96303082009-07-13 21:29:25 -04001111 } else {
1112 /*
1113 * we could have a bitmap entry and an extent entry
1114 * share the same offset. If this is the case, we want
1115 * the extent entry to always be found first if we do a
1116 * linear search through the tree, since we want to have
1117 * the quickest allocation time, and allocating from an
1118 * extent is faster than allocating from a bitmap. So
1119 * if we're inserting a bitmap and we find an entry at
1120 * this offset, we want to go right, or after this entry
1121 * logically. If we are inserting an extent and we've
1122 * found a bitmap, we want to go left, or before
1123 * logically.
1124 */
1125 if (bitmap) {
Josef Bacik207dde82011-05-13 14:49:23 -04001126 if (info->bitmap) {
1127 WARN_ON_ONCE(1);
1128 return -EEXIST;
1129 }
Josef Bacik96303082009-07-13 21:29:25 -04001130 p = &(*p)->rb_right;
1131 } else {
Josef Bacik207dde82011-05-13 14:49:23 -04001132 if (!info->bitmap) {
1133 WARN_ON_ONCE(1);
1134 return -EEXIST;
1135 }
Josef Bacik96303082009-07-13 21:29:25 -04001136 p = &(*p)->rb_left;
1137 }
1138 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04001139 }
1140
1141 rb_link_node(node, parent, p);
1142 rb_insert_color(node, root);
1143
1144 return 0;
1145}
1146
1147/*
Josef Bacik70cb0742009-04-03 10:14:19 -04001148 * searches the tree for the given offset.
1149 *
Josef Bacik96303082009-07-13 21:29:25 -04001150 * fuzzy - If this is set, then we are trying to make an allocation, and we just
1151 * want a section that has at least bytes size and comes at or after the given
1152 * offset.
Josef Bacik0f9dd462008-09-23 13:14:11 -04001153 */
Josef Bacik96303082009-07-13 21:29:25 -04001154static struct btrfs_free_space *
Li Zefan34d52cb2011-03-29 13:46:06 +08001155tree_search_offset(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001156 u64 offset, int bitmap_only, int fuzzy)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001157{
Li Zefan34d52cb2011-03-29 13:46:06 +08001158 struct rb_node *n = ctl->free_space_offset.rb_node;
Josef Bacik96303082009-07-13 21:29:25 -04001159 struct btrfs_free_space *entry, *prev = NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001160
Josef Bacik96303082009-07-13 21:29:25 -04001161 /* find entry that is closest to the 'offset' */
1162 while (1) {
1163 if (!n) {
1164 entry = NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001165 break;
1166 }
Josef Bacik96303082009-07-13 21:29:25 -04001167
1168 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1169 prev = entry;
1170
1171 if (offset < entry->offset)
1172 n = n->rb_left;
1173 else if (offset > entry->offset)
1174 n = n->rb_right;
1175 else
1176 break;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001177 }
1178
Josef Bacik96303082009-07-13 21:29:25 -04001179 if (bitmap_only) {
1180 if (!entry)
1181 return NULL;
1182 if (entry->bitmap)
1183 return entry;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001184
Josef Bacik96303082009-07-13 21:29:25 -04001185 /*
1186 * bitmap entry and extent entry may share same offset,
1187 * in that case, bitmap entry comes after extent entry.
1188 */
1189 n = rb_next(n);
1190 if (!n)
1191 return NULL;
1192 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1193 if (entry->offset != offset)
1194 return NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001195
Josef Bacik96303082009-07-13 21:29:25 -04001196 WARN_ON(!entry->bitmap);
1197 return entry;
1198 } else if (entry) {
1199 if (entry->bitmap) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001200 /*
Josef Bacik96303082009-07-13 21:29:25 -04001201 * if previous extent entry covers the offset,
1202 * we should return it instead of the bitmap entry
Josef Bacik0f9dd462008-09-23 13:14:11 -04001203 */
Josef Bacik96303082009-07-13 21:29:25 -04001204 n = &entry->offset_index;
1205 while (1) {
1206 n = rb_prev(n);
1207 if (!n)
1208 break;
1209 prev = rb_entry(n, struct btrfs_free_space,
1210 offset_index);
1211 if (!prev->bitmap) {
1212 if (prev->offset + prev->bytes > offset)
1213 entry = prev;
1214 break;
1215 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04001216 }
Josef Bacik96303082009-07-13 21:29:25 -04001217 }
1218 return entry;
1219 }
1220
1221 if (!prev)
1222 return NULL;
1223
1224 /* find last entry before the 'offset' */
1225 entry = prev;
1226 if (entry->offset > offset) {
1227 n = rb_prev(&entry->offset_index);
1228 if (n) {
1229 entry = rb_entry(n, struct btrfs_free_space,
1230 offset_index);
1231 BUG_ON(entry->offset > offset);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001232 } else {
Josef Bacik96303082009-07-13 21:29:25 -04001233 if (fuzzy)
1234 return entry;
1235 else
1236 return NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001237 }
1238 }
1239
Josef Bacik96303082009-07-13 21:29:25 -04001240 if (entry->bitmap) {
1241 n = &entry->offset_index;
1242 while (1) {
1243 n = rb_prev(n);
1244 if (!n)
1245 break;
1246 prev = rb_entry(n, struct btrfs_free_space,
1247 offset_index);
1248 if (!prev->bitmap) {
1249 if (prev->offset + prev->bytes > offset)
1250 return prev;
1251 break;
1252 }
1253 }
Li Zefan34d52cb2011-03-29 13:46:06 +08001254 if (entry->offset + BITS_PER_BITMAP * ctl->unit > offset)
Josef Bacik96303082009-07-13 21:29:25 -04001255 return entry;
1256 } else if (entry->offset + entry->bytes > offset)
1257 return entry;
1258
1259 if (!fuzzy)
1260 return NULL;
1261
1262 while (1) {
1263 if (entry->bitmap) {
1264 if (entry->offset + BITS_PER_BITMAP *
Li Zefan34d52cb2011-03-29 13:46:06 +08001265 ctl->unit > offset)
Josef Bacik96303082009-07-13 21:29:25 -04001266 break;
1267 } else {
1268 if (entry->offset + entry->bytes > offset)
1269 break;
1270 }
1271
1272 n = rb_next(&entry->offset_index);
1273 if (!n)
1274 return NULL;
1275 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1276 }
1277 return entry;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001278}
1279
Li Zefanf333adb2010-11-09 14:57:39 +08001280static inline void
Li Zefan34d52cb2011-03-29 13:46:06 +08001281__unlink_free_space(struct btrfs_free_space_ctl *ctl,
Li Zefanf333adb2010-11-09 14:57:39 +08001282 struct btrfs_free_space *info)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001283{
Li Zefan34d52cb2011-03-29 13:46:06 +08001284 rb_erase(&info->offset_index, &ctl->free_space_offset);
1285 ctl->free_extents--;
Li Zefanf333adb2010-11-09 14:57:39 +08001286}
1287
Li Zefan34d52cb2011-03-29 13:46:06 +08001288static void unlink_free_space(struct btrfs_free_space_ctl *ctl,
Li Zefanf333adb2010-11-09 14:57:39 +08001289 struct btrfs_free_space *info)
1290{
Li Zefan34d52cb2011-03-29 13:46:06 +08001291 __unlink_free_space(ctl, info);
1292 ctl->free_space -= info->bytes;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001293}
1294
Li Zefan34d52cb2011-03-29 13:46:06 +08001295static int link_free_space(struct btrfs_free_space_ctl *ctl,
Josef Bacik0f9dd462008-09-23 13:14:11 -04001296 struct btrfs_free_space *info)
1297{
1298 int ret = 0;
1299
Josef Bacik96303082009-07-13 21:29:25 -04001300 BUG_ON(!info->bitmap && !info->bytes);
Li Zefan34d52cb2011-03-29 13:46:06 +08001301 ret = tree_insert_offset(&ctl->free_space_offset, info->offset,
Josef Bacik96303082009-07-13 21:29:25 -04001302 &info->offset_index, (info->bitmap != NULL));
Josef Bacik0f9dd462008-09-23 13:14:11 -04001303 if (ret)
1304 return ret;
1305
Li Zefan34d52cb2011-03-29 13:46:06 +08001306 ctl->free_space += info->bytes;
1307 ctl->free_extents++;
Josef Bacik96303082009-07-13 21:29:25 -04001308 return ret;
1309}
1310
Li Zefan34d52cb2011-03-29 13:46:06 +08001311static void recalculate_thresholds(struct btrfs_free_space_ctl *ctl)
Josef Bacik96303082009-07-13 21:29:25 -04001312{
Li Zefan34d52cb2011-03-29 13:46:06 +08001313 struct btrfs_block_group_cache *block_group = ctl->private;
Josef Bacik25891f72009-09-11 16:11:20 -04001314 u64 max_bytes;
1315 u64 bitmap_bytes;
1316 u64 extent_bytes;
Li Zefan8eb2d822010-11-09 14:48:01 +08001317 u64 size = block_group->key.offset;
Li Zefan34d52cb2011-03-29 13:46:06 +08001318 u64 bytes_per_bg = BITS_PER_BITMAP * block_group->sectorsize;
1319 int max_bitmaps = div64_u64(size + bytes_per_bg - 1, bytes_per_bg);
1320
1321 BUG_ON(ctl->total_bitmaps > max_bitmaps);
Josef Bacik96303082009-07-13 21:29:25 -04001322
1323 /*
1324 * The goal is to keep the total amount of memory used per 1gb of space
1325 * at or below 32k, so we need to adjust how much memory we allow to be
1326 * used by extent based free space tracking
1327 */
Li Zefan8eb2d822010-11-09 14:48:01 +08001328 if (size < 1024 * 1024 * 1024)
1329 max_bytes = MAX_CACHE_BYTES_PER_GIG;
1330 else
1331 max_bytes = MAX_CACHE_BYTES_PER_GIG *
1332 div64_u64(size, 1024 * 1024 * 1024);
Josef Bacik96303082009-07-13 21:29:25 -04001333
Josef Bacik25891f72009-09-11 16:11:20 -04001334 /*
1335 * we want to account for 1 more bitmap than what we have so we can make
1336 * sure we don't go over our overall goal of MAX_CACHE_BYTES_PER_GIG as
1337 * we add more bitmaps.
1338 */
Li Zefan34d52cb2011-03-29 13:46:06 +08001339 bitmap_bytes = (ctl->total_bitmaps + 1) * PAGE_CACHE_SIZE;
Josef Bacik96303082009-07-13 21:29:25 -04001340
Josef Bacik25891f72009-09-11 16:11:20 -04001341 if (bitmap_bytes >= max_bytes) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001342 ctl->extents_thresh = 0;
Josef Bacik25891f72009-09-11 16:11:20 -04001343 return;
Josef Bacik96303082009-07-13 21:29:25 -04001344 }
Josef Bacik25891f72009-09-11 16:11:20 -04001345
1346 /*
1347 * we want the extent entry threshold to always be at most 1/2 the maxw
1348 * bytes we can have, or whatever is less than that.
1349 */
1350 extent_bytes = max_bytes - bitmap_bytes;
1351 extent_bytes = min_t(u64, extent_bytes, div64_u64(max_bytes, 2));
1352
Li Zefan34d52cb2011-03-29 13:46:06 +08001353 ctl->extents_thresh =
Josef Bacik25891f72009-09-11 16:11:20 -04001354 div64_u64(extent_bytes, (sizeof(struct btrfs_free_space)));
Josef Bacik96303082009-07-13 21:29:25 -04001355}
1356
Miao Xiebb3ac5a2011-08-05 09:32:35 +00001357static inline void __bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
1358 struct btrfs_free_space *info,
1359 u64 offset, u64 bytes)
Josef Bacik96303082009-07-13 21:29:25 -04001360{
Li Zefanf38b6e72011-03-14 13:40:51 +08001361 unsigned long start, count;
Josef Bacik96303082009-07-13 21:29:25 -04001362
Li Zefan34d52cb2011-03-29 13:46:06 +08001363 start = offset_to_bit(info->offset, ctl->unit, offset);
1364 count = bytes_to_bits(bytes, ctl->unit);
Li Zefanf38b6e72011-03-14 13:40:51 +08001365 BUG_ON(start + count > BITS_PER_BITMAP);
Josef Bacik96303082009-07-13 21:29:25 -04001366
Li Zefanf38b6e72011-03-14 13:40:51 +08001367 bitmap_clear(info->bitmap, start, count);
Josef Bacik96303082009-07-13 21:29:25 -04001368
1369 info->bytes -= bytes;
Miao Xiebb3ac5a2011-08-05 09:32:35 +00001370}
1371
1372static void bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
1373 struct btrfs_free_space *info, u64 offset,
1374 u64 bytes)
1375{
1376 __bitmap_clear_bits(ctl, info, offset, bytes);
Li Zefan34d52cb2011-03-29 13:46:06 +08001377 ctl->free_space -= bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001378}
1379
Li Zefan34d52cb2011-03-29 13:46:06 +08001380static void bitmap_set_bits(struct btrfs_free_space_ctl *ctl,
Josef Bacik817d52f2009-07-13 21:29:25 -04001381 struct btrfs_free_space *info, u64 offset,
1382 u64 bytes)
Josef Bacik96303082009-07-13 21:29:25 -04001383{
Li Zefanf38b6e72011-03-14 13:40:51 +08001384 unsigned long start, count;
Josef Bacik96303082009-07-13 21:29:25 -04001385
Li Zefan34d52cb2011-03-29 13:46:06 +08001386 start = offset_to_bit(info->offset, ctl->unit, offset);
1387 count = bytes_to_bits(bytes, ctl->unit);
Li Zefanf38b6e72011-03-14 13:40:51 +08001388 BUG_ON(start + count > BITS_PER_BITMAP);
Josef Bacik96303082009-07-13 21:29:25 -04001389
Li Zefanf38b6e72011-03-14 13:40:51 +08001390 bitmap_set(info->bitmap, start, count);
Josef Bacik96303082009-07-13 21:29:25 -04001391
1392 info->bytes += bytes;
Li Zefan34d52cb2011-03-29 13:46:06 +08001393 ctl->free_space += bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001394}
1395
Li Zefan34d52cb2011-03-29 13:46:06 +08001396static int search_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001397 struct btrfs_free_space *bitmap_info, u64 *offset,
1398 u64 *bytes)
1399{
1400 unsigned long found_bits = 0;
1401 unsigned long bits, i;
1402 unsigned long next_zero;
1403
Li Zefan34d52cb2011-03-29 13:46:06 +08001404 i = offset_to_bit(bitmap_info->offset, ctl->unit,
Josef Bacik96303082009-07-13 21:29:25 -04001405 max_t(u64, *offset, bitmap_info->offset));
Li Zefan34d52cb2011-03-29 13:46:06 +08001406 bits = bytes_to_bits(*bytes, ctl->unit);
Josef Bacik96303082009-07-13 21:29:25 -04001407
1408 for (i = find_next_bit(bitmap_info->bitmap, BITS_PER_BITMAP, i);
1409 i < BITS_PER_BITMAP;
1410 i = find_next_bit(bitmap_info->bitmap, BITS_PER_BITMAP, i + 1)) {
1411 next_zero = find_next_zero_bit(bitmap_info->bitmap,
1412 BITS_PER_BITMAP, i);
1413 if ((next_zero - i) >= bits) {
1414 found_bits = next_zero - i;
1415 break;
1416 }
1417 i = next_zero;
1418 }
1419
1420 if (found_bits) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001421 *offset = (u64)(i * ctl->unit) + bitmap_info->offset;
1422 *bytes = (u64)(found_bits) * ctl->unit;
Josef Bacik96303082009-07-13 21:29:25 -04001423 return 0;
1424 }
1425
1426 return -1;
1427}
1428
Li Zefan34d52cb2011-03-29 13:46:06 +08001429static struct btrfs_free_space *
1430find_free_space(struct btrfs_free_space_ctl *ctl, u64 *offset, u64 *bytes)
Josef Bacik96303082009-07-13 21:29:25 -04001431{
1432 struct btrfs_free_space *entry;
1433 struct rb_node *node;
1434 int ret;
1435
Li Zefan34d52cb2011-03-29 13:46:06 +08001436 if (!ctl->free_space_offset.rb_node)
Josef Bacik96303082009-07-13 21:29:25 -04001437 return NULL;
1438
Li Zefan34d52cb2011-03-29 13:46:06 +08001439 entry = tree_search_offset(ctl, offset_to_bitmap(ctl, *offset), 0, 1);
Josef Bacik96303082009-07-13 21:29:25 -04001440 if (!entry)
1441 return NULL;
1442
1443 for (node = &entry->offset_index; node; node = rb_next(node)) {
1444 entry = rb_entry(node, struct btrfs_free_space, offset_index);
1445 if (entry->bytes < *bytes)
1446 continue;
1447
1448 if (entry->bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001449 ret = search_bitmap(ctl, entry, offset, bytes);
Josef Bacik96303082009-07-13 21:29:25 -04001450 if (!ret)
1451 return entry;
1452 continue;
1453 }
1454
1455 *offset = entry->offset;
1456 *bytes = entry->bytes;
1457 return entry;
1458 }
1459
1460 return NULL;
1461}
1462
Li Zefan34d52cb2011-03-29 13:46:06 +08001463static void add_new_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001464 struct btrfs_free_space *info, u64 offset)
1465{
Li Zefan34d52cb2011-03-29 13:46:06 +08001466 info->offset = offset_to_bitmap(ctl, offset);
Josef Bacikf019f422009-09-11 16:11:20 -04001467 info->bytes = 0;
Li Zefan34d52cb2011-03-29 13:46:06 +08001468 link_free_space(ctl, info);
1469 ctl->total_bitmaps++;
Josef Bacik96303082009-07-13 21:29:25 -04001470
Li Zefan34d52cb2011-03-29 13:46:06 +08001471 ctl->op->recalc_thresholds(ctl);
Josef Bacik96303082009-07-13 21:29:25 -04001472}
1473
Li Zefan34d52cb2011-03-29 13:46:06 +08001474static void free_bitmap(struct btrfs_free_space_ctl *ctl,
Li Zefanedf6e2d2010-11-09 14:50:07 +08001475 struct btrfs_free_space *bitmap_info)
1476{
Li Zefan34d52cb2011-03-29 13:46:06 +08001477 unlink_free_space(ctl, bitmap_info);
Li Zefanedf6e2d2010-11-09 14:50:07 +08001478 kfree(bitmap_info->bitmap);
Josef Bacikdc89e982011-01-28 17:05:48 -05001479 kmem_cache_free(btrfs_free_space_cachep, bitmap_info);
Li Zefan34d52cb2011-03-29 13:46:06 +08001480 ctl->total_bitmaps--;
1481 ctl->op->recalc_thresholds(ctl);
Li Zefanedf6e2d2010-11-09 14:50:07 +08001482}
1483
Li Zefan34d52cb2011-03-29 13:46:06 +08001484static noinline int remove_from_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001485 struct btrfs_free_space *bitmap_info,
1486 u64 *offset, u64 *bytes)
1487{
1488 u64 end;
Josef Bacik6606bb92009-07-31 11:03:58 -04001489 u64 search_start, search_bytes;
1490 int ret;
Josef Bacik96303082009-07-13 21:29:25 -04001491
1492again:
Li Zefan34d52cb2011-03-29 13:46:06 +08001493 end = bitmap_info->offset + (u64)(BITS_PER_BITMAP * ctl->unit) - 1;
Josef Bacik96303082009-07-13 21:29:25 -04001494
Josef Bacik6606bb92009-07-31 11:03:58 -04001495 /*
1496 * XXX - this can go away after a few releases.
1497 *
1498 * since the only user of btrfs_remove_free_space is the tree logging
1499 * stuff, and the only way to test that is under crash conditions, we
1500 * want to have this debug stuff here just in case somethings not
1501 * working. Search the bitmap for the space we are trying to use to
1502 * make sure its actually there. If its not there then we need to stop
1503 * because something has gone wrong.
1504 */
1505 search_start = *offset;
1506 search_bytes = *bytes;
Josef Bacik13dbc082011-02-03 02:39:52 +00001507 search_bytes = min(search_bytes, end - search_start + 1);
Li Zefan34d52cb2011-03-29 13:46:06 +08001508 ret = search_bitmap(ctl, bitmap_info, &search_start, &search_bytes);
Josef Bacik6606bb92009-07-31 11:03:58 -04001509 BUG_ON(ret < 0 || search_start != *offset);
1510
Josef Bacik96303082009-07-13 21:29:25 -04001511 if (*offset > bitmap_info->offset && *offset + *bytes > end) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001512 bitmap_clear_bits(ctl, bitmap_info, *offset, end - *offset + 1);
Josef Bacik96303082009-07-13 21:29:25 -04001513 *bytes -= end - *offset + 1;
1514 *offset = end + 1;
1515 } else if (*offset >= bitmap_info->offset && *offset + *bytes <= end) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001516 bitmap_clear_bits(ctl, bitmap_info, *offset, *bytes);
Josef Bacik96303082009-07-13 21:29:25 -04001517 *bytes = 0;
1518 }
1519
1520 if (*bytes) {
Josef Bacik6606bb92009-07-31 11:03:58 -04001521 struct rb_node *next = rb_next(&bitmap_info->offset_index);
Li Zefanedf6e2d2010-11-09 14:50:07 +08001522 if (!bitmap_info->bytes)
Li Zefan34d52cb2011-03-29 13:46:06 +08001523 free_bitmap(ctl, bitmap_info);
Josef Bacik96303082009-07-13 21:29:25 -04001524
Josef Bacik6606bb92009-07-31 11:03:58 -04001525 /*
1526 * no entry after this bitmap, but we still have bytes to
1527 * remove, so something has gone wrong.
1528 */
1529 if (!next)
Josef Bacik96303082009-07-13 21:29:25 -04001530 return -EINVAL;
1531
Josef Bacik6606bb92009-07-31 11:03:58 -04001532 bitmap_info = rb_entry(next, struct btrfs_free_space,
1533 offset_index);
1534
1535 /*
1536 * if the next entry isn't a bitmap we need to return to let the
1537 * extent stuff do its work.
1538 */
Josef Bacik96303082009-07-13 21:29:25 -04001539 if (!bitmap_info->bitmap)
1540 return -EAGAIN;
1541
Josef Bacik6606bb92009-07-31 11:03:58 -04001542 /*
1543 * Ok the next item is a bitmap, but it may not actually hold
1544 * the information for the rest of this free space stuff, so
1545 * look for it, and if we don't find it return so we can try
1546 * everything over again.
1547 */
1548 search_start = *offset;
1549 search_bytes = *bytes;
Li Zefan34d52cb2011-03-29 13:46:06 +08001550 ret = search_bitmap(ctl, bitmap_info, &search_start,
Josef Bacik6606bb92009-07-31 11:03:58 -04001551 &search_bytes);
1552 if (ret < 0 || search_start != *offset)
1553 return -EAGAIN;
1554
Josef Bacik96303082009-07-13 21:29:25 -04001555 goto again;
Li Zefanedf6e2d2010-11-09 14:50:07 +08001556 } else if (!bitmap_info->bytes)
Li Zefan34d52cb2011-03-29 13:46:06 +08001557 free_bitmap(ctl, bitmap_info);
Josef Bacik96303082009-07-13 21:29:25 -04001558
1559 return 0;
1560}
1561
Josef Bacik2cdc3422011-05-27 14:07:49 -04001562static u64 add_bytes_to_bitmap(struct btrfs_free_space_ctl *ctl,
1563 struct btrfs_free_space *info, u64 offset,
1564 u64 bytes)
1565{
1566 u64 bytes_to_set = 0;
1567 u64 end;
1568
1569 end = info->offset + (u64)(BITS_PER_BITMAP * ctl->unit);
1570
1571 bytes_to_set = min(end - offset, bytes);
1572
1573 bitmap_set_bits(ctl, info, offset, bytes_to_set);
1574
1575 return bytes_to_set;
1576
1577}
1578
Li Zefan34d52cb2011-03-29 13:46:06 +08001579static bool use_bitmap(struct btrfs_free_space_ctl *ctl,
1580 struct btrfs_free_space *info)
Josef Bacik96303082009-07-13 21:29:25 -04001581{
Li Zefan34d52cb2011-03-29 13:46:06 +08001582 struct btrfs_block_group_cache *block_group = ctl->private;
Josef Bacik96303082009-07-13 21:29:25 -04001583
1584 /*
1585 * If we are below the extents threshold then we can add this as an
1586 * extent, and don't have to deal with the bitmap
1587 */
Li Zefan34d52cb2011-03-29 13:46:06 +08001588 if (ctl->free_extents < ctl->extents_thresh) {
Josef Bacik32cb0842011-03-18 16:16:21 -04001589 /*
1590 * If this block group has some small extents we don't want to
1591 * use up all of our free slots in the cache with them, we want
1592 * to reserve them to larger extents, however if we have plent
1593 * of cache left then go ahead an dadd them, no sense in adding
1594 * the overhead of a bitmap if we don't have to.
1595 */
1596 if (info->bytes <= block_group->sectorsize * 4) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001597 if (ctl->free_extents * 2 <= ctl->extents_thresh)
1598 return false;
Josef Bacik32cb0842011-03-18 16:16:21 -04001599 } else {
Li Zefan34d52cb2011-03-29 13:46:06 +08001600 return false;
Josef Bacik32cb0842011-03-18 16:16:21 -04001601 }
1602 }
Josef Bacik96303082009-07-13 21:29:25 -04001603
1604 /*
1605 * some block groups are so tiny they can't be enveloped by a bitmap, so
1606 * don't even bother to create a bitmap for this
1607 */
1608 if (BITS_PER_BITMAP * block_group->sectorsize >
1609 block_group->key.offset)
Li Zefan34d52cb2011-03-29 13:46:06 +08001610 return false;
1611
1612 return true;
1613}
1614
Josef Bacik2cdc3422011-05-27 14:07:49 -04001615static struct btrfs_free_space_op free_space_op = {
1616 .recalc_thresholds = recalculate_thresholds,
1617 .use_bitmap = use_bitmap,
1618};
1619
Li Zefan34d52cb2011-03-29 13:46:06 +08001620static int insert_into_bitmap(struct btrfs_free_space_ctl *ctl,
1621 struct btrfs_free_space *info)
1622{
1623 struct btrfs_free_space *bitmap_info;
Josef Bacik2cdc3422011-05-27 14:07:49 -04001624 struct btrfs_block_group_cache *block_group = NULL;
Li Zefan34d52cb2011-03-29 13:46:06 +08001625 int added = 0;
Josef Bacik2cdc3422011-05-27 14:07:49 -04001626 u64 bytes, offset, bytes_added;
Li Zefan34d52cb2011-03-29 13:46:06 +08001627 int ret;
Josef Bacik96303082009-07-13 21:29:25 -04001628
1629 bytes = info->bytes;
1630 offset = info->offset;
1631
Li Zefan34d52cb2011-03-29 13:46:06 +08001632 if (!ctl->op->use_bitmap(ctl, info))
1633 return 0;
1634
Josef Bacik2cdc3422011-05-27 14:07:49 -04001635 if (ctl->op == &free_space_op)
1636 block_group = ctl->private;
Chris Mason38e87882011-06-10 16:36:57 -04001637again:
Josef Bacik2cdc3422011-05-27 14:07:49 -04001638 /*
1639 * Since we link bitmaps right into the cluster we need to see if we
1640 * have a cluster here, and if so and it has our bitmap we need to add
1641 * the free space to that bitmap.
1642 */
1643 if (block_group && !list_empty(&block_group->cluster_list)) {
1644 struct btrfs_free_cluster *cluster;
1645 struct rb_node *node;
1646 struct btrfs_free_space *entry;
1647
1648 cluster = list_entry(block_group->cluster_list.next,
1649 struct btrfs_free_cluster,
1650 block_group_list);
1651 spin_lock(&cluster->lock);
1652 node = rb_first(&cluster->root);
1653 if (!node) {
1654 spin_unlock(&cluster->lock);
Chris Mason38e87882011-06-10 16:36:57 -04001655 goto no_cluster_bitmap;
Josef Bacik2cdc3422011-05-27 14:07:49 -04001656 }
1657
1658 entry = rb_entry(node, struct btrfs_free_space, offset_index);
1659 if (!entry->bitmap) {
1660 spin_unlock(&cluster->lock);
Chris Mason38e87882011-06-10 16:36:57 -04001661 goto no_cluster_bitmap;
Josef Bacik2cdc3422011-05-27 14:07:49 -04001662 }
1663
1664 if (entry->offset == offset_to_bitmap(ctl, offset)) {
1665 bytes_added = add_bytes_to_bitmap(ctl, entry,
1666 offset, bytes);
1667 bytes -= bytes_added;
1668 offset += bytes_added;
1669 }
1670 spin_unlock(&cluster->lock);
1671 if (!bytes) {
1672 ret = 1;
1673 goto out;
1674 }
1675 }
Chris Mason38e87882011-06-10 16:36:57 -04001676
1677no_cluster_bitmap:
Li Zefan34d52cb2011-03-29 13:46:06 +08001678 bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
Josef Bacik96303082009-07-13 21:29:25 -04001679 1, 0);
1680 if (!bitmap_info) {
1681 BUG_ON(added);
1682 goto new_bitmap;
1683 }
1684
Josef Bacik2cdc3422011-05-27 14:07:49 -04001685 bytes_added = add_bytes_to_bitmap(ctl, bitmap_info, offset, bytes);
1686 bytes -= bytes_added;
1687 offset += bytes_added;
1688 added = 0;
Josef Bacik96303082009-07-13 21:29:25 -04001689
1690 if (!bytes) {
1691 ret = 1;
1692 goto out;
1693 } else
1694 goto again;
1695
1696new_bitmap:
1697 if (info && info->bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001698 add_new_bitmap(ctl, info, offset);
Josef Bacik96303082009-07-13 21:29:25 -04001699 added = 1;
1700 info = NULL;
1701 goto again;
1702 } else {
Li Zefan34d52cb2011-03-29 13:46:06 +08001703 spin_unlock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04001704
1705 /* no pre-allocated info, allocate a new one */
1706 if (!info) {
Josef Bacikdc89e982011-01-28 17:05:48 -05001707 info = kmem_cache_zalloc(btrfs_free_space_cachep,
1708 GFP_NOFS);
Josef Bacik96303082009-07-13 21:29:25 -04001709 if (!info) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001710 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04001711 ret = -ENOMEM;
1712 goto out;
1713 }
1714 }
1715
1716 /* allocate the bitmap */
1717 info->bitmap = kzalloc(PAGE_CACHE_SIZE, GFP_NOFS);
Li Zefan34d52cb2011-03-29 13:46:06 +08001718 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04001719 if (!info->bitmap) {
1720 ret = -ENOMEM;
1721 goto out;
1722 }
1723 goto again;
1724 }
1725
1726out:
1727 if (info) {
1728 if (info->bitmap)
1729 kfree(info->bitmap);
Josef Bacikdc89e982011-01-28 17:05:48 -05001730 kmem_cache_free(btrfs_free_space_cachep, info);
Josef Bacik96303082009-07-13 21:29:25 -04001731 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04001732
1733 return ret;
1734}
1735
Chris Mason945d8962011-05-22 12:33:42 -04001736static bool try_merge_free_space(struct btrfs_free_space_ctl *ctl,
Li Zefanf333adb2010-11-09 14:57:39 +08001737 struct btrfs_free_space *info, bool update_stat)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001738{
Li Zefan120d66e2010-11-09 14:56:50 +08001739 struct btrfs_free_space *left_info;
1740 struct btrfs_free_space *right_info;
1741 bool merged = false;
1742 u64 offset = info->offset;
1743 u64 bytes = info->bytes;
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001744
Josef Bacik0f9dd462008-09-23 13:14:11 -04001745 /*
1746 * first we want to see if there is free space adjacent to the range we
1747 * are adding, if there is remove that struct and add a new one to
1748 * cover the entire range
1749 */
Li Zefan34d52cb2011-03-29 13:46:06 +08001750 right_info = tree_search_offset(ctl, offset + bytes, 0, 0);
Josef Bacik96303082009-07-13 21:29:25 -04001751 if (right_info && rb_prev(&right_info->offset_index))
1752 left_info = rb_entry(rb_prev(&right_info->offset_index),
1753 struct btrfs_free_space, offset_index);
1754 else
Li Zefan34d52cb2011-03-29 13:46:06 +08001755 left_info = tree_search_offset(ctl, offset - 1, 0, 0);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001756
Josef Bacik96303082009-07-13 21:29:25 -04001757 if (right_info && !right_info->bitmap) {
Li Zefanf333adb2010-11-09 14:57:39 +08001758 if (update_stat)
Li Zefan34d52cb2011-03-29 13:46:06 +08001759 unlink_free_space(ctl, right_info);
Li Zefanf333adb2010-11-09 14:57:39 +08001760 else
Li Zefan34d52cb2011-03-29 13:46:06 +08001761 __unlink_free_space(ctl, right_info);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001762 info->bytes += right_info->bytes;
Josef Bacikdc89e982011-01-28 17:05:48 -05001763 kmem_cache_free(btrfs_free_space_cachep, right_info);
Li Zefan120d66e2010-11-09 14:56:50 +08001764 merged = true;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001765 }
1766
Josef Bacik96303082009-07-13 21:29:25 -04001767 if (left_info && !left_info->bitmap &&
1768 left_info->offset + left_info->bytes == offset) {
Li Zefanf333adb2010-11-09 14:57:39 +08001769 if (update_stat)
Li Zefan34d52cb2011-03-29 13:46:06 +08001770 unlink_free_space(ctl, left_info);
Li Zefanf333adb2010-11-09 14:57:39 +08001771 else
Li Zefan34d52cb2011-03-29 13:46:06 +08001772 __unlink_free_space(ctl, left_info);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001773 info->offset = left_info->offset;
1774 info->bytes += left_info->bytes;
Josef Bacikdc89e982011-01-28 17:05:48 -05001775 kmem_cache_free(btrfs_free_space_cachep, left_info);
Li Zefan120d66e2010-11-09 14:56:50 +08001776 merged = true;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001777 }
1778
Li Zefan120d66e2010-11-09 14:56:50 +08001779 return merged;
1780}
1781
Li Zefan581bb052011-04-20 10:06:11 +08001782int __btrfs_add_free_space(struct btrfs_free_space_ctl *ctl,
1783 u64 offset, u64 bytes)
Li Zefan120d66e2010-11-09 14:56:50 +08001784{
1785 struct btrfs_free_space *info;
1786 int ret = 0;
1787
Josef Bacikdc89e982011-01-28 17:05:48 -05001788 info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS);
Li Zefan120d66e2010-11-09 14:56:50 +08001789 if (!info)
1790 return -ENOMEM;
1791
1792 info->offset = offset;
1793 info->bytes = bytes;
1794
Li Zefan34d52cb2011-03-29 13:46:06 +08001795 spin_lock(&ctl->tree_lock);
Li Zefan120d66e2010-11-09 14:56:50 +08001796
Li Zefan34d52cb2011-03-29 13:46:06 +08001797 if (try_merge_free_space(ctl, info, true))
Li Zefan120d66e2010-11-09 14:56:50 +08001798 goto link;
1799
1800 /*
1801 * There was no extent directly to the left or right of this new
1802 * extent then we know we're going to have to allocate a new extent, so
1803 * before we do that see if we need to drop this into a bitmap
1804 */
Li Zefan34d52cb2011-03-29 13:46:06 +08001805 ret = insert_into_bitmap(ctl, info);
Li Zefan120d66e2010-11-09 14:56:50 +08001806 if (ret < 0) {
1807 goto out;
1808 } else if (ret) {
1809 ret = 0;
1810 goto out;
1811 }
1812link:
Li Zefan34d52cb2011-03-29 13:46:06 +08001813 ret = link_free_space(ctl, info);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001814 if (ret)
Josef Bacikdc89e982011-01-28 17:05:48 -05001815 kmem_cache_free(btrfs_free_space_cachep, info);
Josef Bacik96303082009-07-13 21:29:25 -04001816out:
Li Zefan34d52cb2011-03-29 13:46:06 +08001817 spin_unlock(&ctl->tree_lock);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001818
Josef Bacik0f9dd462008-09-23 13:14:11 -04001819 if (ret) {
Josef Bacik96303082009-07-13 21:29:25 -04001820 printk(KERN_CRIT "btrfs: unable to add free space :%d\n", ret);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04001821 BUG_ON(ret == -EEXIST);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001822 }
1823
Josef Bacik0f9dd462008-09-23 13:14:11 -04001824 return ret;
1825}
1826
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001827int btrfs_remove_free_space(struct btrfs_block_group_cache *block_group,
1828 u64 offset, u64 bytes)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001829{
Li Zefan34d52cb2011-03-29 13:46:06 +08001830 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001831 struct btrfs_free_space *info;
Josef Bacik96303082009-07-13 21:29:25 -04001832 struct btrfs_free_space *next_info = NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001833 int ret = 0;
1834
Li Zefan34d52cb2011-03-29 13:46:06 +08001835 spin_lock(&ctl->tree_lock);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001836
Josef Bacik96303082009-07-13 21:29:25 -04001837again:
Li Zefan34d52cb2011-03-29 13:46:06 +08001838 info = tree_search_offset(ctl, offset, 0, 0);
Josef Bacik96303082009-07-13 21:29:25 -04001839 if (!info) {
Josef Bacik6606bb92009-07-31 11:03:58 -04001840 /*
1841 * oops didn't find an extent that matched the space we wanted
1842 * to remove, look for a bitmap instead
1843 */
Li Zefan34d52cb2011-03-29 13:46:06 +08001844 info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
Josef Bacik6606bb92009-07-31 11:03:58 -04001845 1, 0);
1846 if (!info) {
1847 WARN_ON(1);
1848 goto out_lock;
1849 }
Josef Bacik96303082009-07-13 21:29:25 -04001850 }
1851
1852 if (info->bytes < bytes && rb_next(&info->offset_index)) {
1853 u64 end;
1854 next_info = rb_entry(rb_next(&info->offset_index),
1855 struct btrfs_free_space,
1856 offset_index);
1857
1858 if (next_info->bitmap)
Li Zefan34d52cb2011-03-29 13:46:06 +08001859 end = next_info->offset +
1860 BITS_PER_BITMAP * ctl->unit - 1;
Josef Bacik96303082009-07-13 21:29:25 -04001861 else
1862 end = next_info->offset + next_info->bytes;
1863
1864 if (next_info->bytes < bytes ||
1865 next_info->offset > offset || offset > end) {
1866 printk(KERN_CRIT "Found free space at %llu, size %llu,"
1867 " trying to use %llu\n",
1868 (unsigned long long)info->offset,
1869 (unsigned long long)info->bytes,
1870 (unsigned long long)bytes);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001871 WARN_ON(1);
1872 ret = -EINVAL;
Josef Bacik96303082009-07-13 21:29:25 -04001873 goto out_lock;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001874 }
Josef Bacik96303082009-07-13 21:29:25 -04001875
1876 info = next_info;
1877 }
1878
1879 if (info->bytes == bytes) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001880 unlink_free_space(ctl, info);
Josef Bacik96303082009-07-13 21:29:25 -04001881 if (info->bitmap) {
1882 kfree(info->bitmap);
Li Zefan34d52cb2011-03-29 13:46:06 +08001883 ctl->total_bitmaps--;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001884 }
Josef Bacikdc89e982011-01-28 17:05:48 -05001885 kmem_cache_free(btrfs_free_space_cachep, info);
Chris Mason1eae31e2011-10-14 06:31:20 -04001886 ret = 0;
Josef Bacik96303082009-07-13 21:29:25 -04001887 goto out_lock;
1888 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04001889
Josef Bacik96303082009-07-13 21:29:25 -04001890 if (!info->bitmap && info->offset == offset) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001891 unlink_free_space(ctl, info);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001892 info->offset += bytes;
1893 info->bytes -= bytes;
Chris Mason1eae31e2011-10-14 06:31:20 -04001894 ret = link_free_space(ctl, info);
1895 WARN_ON(ret);
Josef Bacik96303082009-07-13 21:29:25 -04001896 goto out_lock;
1897 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04001898
Josef Bacik96303082009-07-13 21:29:25 -04001899 if (!info->bitmap && info->offset <= offset &&
1900 info->offset + info->bytes >= offset + bytes) {
Chris Mason9b49c9b2008-09-24 11:23:25 -04001901 u64 old_start = info->offset;
1902 /*
1903 * we're freeing space in the middle of the info,
1904 * this can happen during tree log replay
1905 *
1906 * first unlink the old info and then
1907 * insert it again after the hole we're creating
1908 */
Li Zefan34d52cb2011-03-29 13:46:06 +08001909 unlink_free_space(ctl, info);
Chris Mason9b49c9b2008-09-24 11:23:25 -04001910 if (offset + bytes < info->offset + info->bytes) {
1911 u64 old_end = info->offset + info->bytes;
1912
1913 info->offset = offset + bytes;
1914 info->bytes = old_end - info->offset;
Li Zefan34d52cb2011-03-29 13:46:06 +08001915 ret = link_free_space(ctl, info);
Josef Bacik96303082009-07-13 21:29:25 -04001916 WARN_ON(ret);
1917 if (ret)
1918 goto out_lock;
Chris Mason9b49c9b2008-09-24 11:23:25 -04001919 } else {
1920 /* the hole we're creating ends at the end
1921 * of the info struct, just free the info
1922 */
Josef Bacikdc89e982011-01-28 17:05:48 -05001923 kmem_cache_free(btrfs_free_space_cachep, info);
Chris Mason9b49c9b2008-09-24 11:23:25 -04001924 }
Li Zefan34d52cb2011-03-29 13:46:06 +08001925 spin_unlock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04001926
1927 /* step two, insert a new info struct to cover
1928 * anything before the hole
Chris Mason9b49c9b2008-09-24 11:23:25 -04001929 */
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001930 ret = btrfs_add_free_space(block_group, old_start,
1931 offset - old_start);
Josef Bacik96303082009-07-13 21:29:25 -04001932 WARN_ON(ret);
1933 goto out;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001934 }
Josef Bacik96303082009-07-13 21:29:25 -04001935
Li Zefan34d52cb2011-03-29 13:46:06 +08001936 ret = remove_from_bitmap(ctl, info, &offset, &bytes);
Josef Bacik96303082009-07-13 21:29:25 -04001937 if (ret == -EAGAIN)
1938 goto again;
1939 BUG_ON(ret);
1940out_lock:
Li Zefan34d52cb2011-03-29 13:46:06 +08001941 spin_unlock(&ctl->tree_lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001942out:
Josef Bacik25179202008-10-29 14:49:05 -04001943 return ret;
1944}
1945
Josef Bacik0f9dd462008-09-23 13:14:11 -04001946void btrfs_dump_free_space(struct btrfs_block_group_cache *block_group,
1947 u64 bytes)
1948{
Li Zefan34d52cb2011-03-29 13:46:06 +08001949 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001950 struct btrfs_free_space *info;
1951 struct rb_node *n;
1952 int count = 0;
1953
Li Zefan34d52cb2011-03-29 13:46:06 +08001954 for (n = rb_first(&ctl->free_space_offset); n; n = rb_next(n)) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001955 info = rb_entry(n, struct btrfs_free_space, offset_index);
1956 if (info->bytes >= bytes)
1957 count++;
Josef Bacik96303082009-07-13 21:29:25 -04001958 printk(KERN_CRIT "entry offset %llu, bytes %llu, bitmap %s\n",
Joel Becker21380932009-04-21 12:38:29 -07001959 (unsigned long long)info->offset,
Josef Bacik96303082009-07-13 21:29:25 -04001960 (unsigned long long)info->bytes,
1961 (info->bitmap) ? "yes" : "no");
Josef Bacik0f9dd462008-09-23 13:14:11 -04001962 }
Josef Bacik96303082009-07-13 21:29:25 -04001963 printk(KERN_INFO "block group has cluster?: %s\n",
1964 list_empty(&block_group->cluster_list) ? "no" : "yes");
Josef Bacik0f9dd462008-09-23 13:14:11 -04001965 printk(KERN_INFO "%d blocks of free space at or bigger than bytes is"
1966 "\n", count);
1967}
1968
Li Zefan34d52cb2011-03-29 13:46:06 +08001969void btrfs_init_free_space_ctl(struct btrfs_block_group_cache *block_group)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001970{
Li Zefan34d52cb2011-03-29 13:46:06 +08001971 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001972
Li Zefan34d52cb2011-03-29 13:46:06 +08001973 spin_lock_init(&ctl->tree_lock);
1974 ctl->unit = block_group->sectorsize;
1975 ctl->start = block_group->key.objectid;
1976 ctl->private = block_group;
1977 ctl->op = &free_space_op;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001978
Li Zefan34d52cb2011-03-29 13:46:06 +08001979 /*
1980 * we only want to have 32k of ram per block group for keeping
1981 * track of free space, and if we pass 1/2 of that we want to
1982 * start converting things over to using bitmaps
1983 */
1984 ctl->extents_thresh = ((1024 * 32) / 2) /
1985 sizeof(struct btrfs_free_space);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001986}
1987
Chris Masonfa9c0d792009-04-03 09:47:43 -04001988/*
1989 * for a given cluster, put all of its extents back into the free
1990 * space cache. If the block group passed doesn't match the block group
1991 * pointed to by the cluster, someone else raced in and freed the
1992 * cluster already. In that case, we just return without changing anything
1993 */
1994static int
1995__btrfs_return_cluster_to_free_space(
1996 struct btrfs_block_group_cache *block_group,
1997 struct btrfs_free_cluster *cluster)
1998{
Li Zefan34d52cb2011-03-29 13:46:06 +08001999 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002000 struct btrfs_free_space *entry;
2001 struct rb_node *node;
2002
2003 spin_lock(&cluster->lock);
2004 if (cluster->block_group != block_group)
2005 goto out;
2006
Josef Bacik96303082009-07-13 21:29:25 -04002007 cluster->block_group = NULL;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002008 cluster->window_start = 0;
Josef Bacik96303082009-07-13 21:29:25 -04002009 list_del_init(&cluster->block_group_list);
Josef Bacik96303082009-07-13 21:29:25 -04002010
Chris Masonfa9c0d792009-04-03 09:47:43 -04002011 node = rb_first(&cluster->root);
Josef Bacik96303082009-07-13 21:29:25 -04002012 while (node) {
Josef Bacik4e69b592011-03-21 10:11:24 -04002013 bool bitmap;
2014
Chris Masonfa9c0d792009-04-03 09:47:43 -04002015 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2016 node = rb_next(&entry->offset_index);
2017 rb_erase(&entry->offset_index, &cluster->root);
Josef Bacik4e69b592011-03-21 10:11:24 -04002018
2019 bitmap = (entry->bitmap != NULL);
2020 if (!bitmap)
Li Zefan34d52cb2011-03-29 13:46:06 +08002021 try_merge_free_space(ctl, entry, false);
2022 tree_insert_offset(&ctl->free_space_offset,
Josef Bacik4e69b592011-03-21 10:11:24 -04002023 entry->offset, &entry->offset_index, bitmap);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002024 }
Eric Paris6bef4d32010-02-23 19:43:04 +00002025 cluster->root = RB_ROOT;
Josef Bacik96303082009-07-13 21:29:25 -04002026
Chris Masonfa9c0d792009-04-03 09:47:43 -04002027out:
2028 spin_unlock(&cluster->lock);
Josef Bacik96303082009-07-13 21:29:25 -04002029 btrfs_put_block_group(block_group);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002030 return 0;
2031}
2032
Chris Mason09655372011-05-21 09:27:38 -04002033void __btrfs_remove_free_space_cache_locked(struct btrfs_free_space_ctl *ctl)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002034{
2035 struct btrfs_free_space *info;
2036 struct rb_node *node;
Li Zefan581bb052011-04-20 10:06:11 +08002037
Li Zefan581bb052011-04-20 10:06:11 +08002038 while ((node = rb_last(&ctl->free_space_offset)) != NULL) {
2039 info = rb_entry(node, struct btrfs_free_space, offset_index);
Josef Bacik9b90f512011-06-24 16:02:51 +00002040 if (!info->bitmap) {
2041 unlink_free_space(ctl, info);
2042 kmem_cache_free(btrfs_free_space_cachep, info);
2043 } else {
2044 free_bitmap(ctl, info);
2045 }
Li Zefan581bb052011-04-20 10:06:11 +08002046 if (need_resched()) {
2047 spin_unlock(&ctl->tree_lock);
2048 cond_resched();
2049 spin_lock(&ctl->tree_lock);
2050 }
2051 }
Chris Mason09655372011-05-21 09:27:38 -04002052}
2053
2054void __btrfs_remove_free_space_cache(struct btrfs_free_space_ctl *ctl)
2055{
2056 spin_lock(&ctl->tree_lock);
2057 __btrfs_remove_free_space_cache_locked(ctl);
Li Zefan581bb052011-04-20 10:06:11 +08002058 spin_unlock(&ctl->tree_lock);
2059}
2060
Josef Bacik0f9dd462008-09-23 13:14:11 -04002061void btrfs_remove_free_space_cache(struct btrfs_block_group_cache *block_group)
2062{
Li Zefan34d52cb2011-03-29 13:46:06 +08002063 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002064 struct btrfs_free_cluster *cluster;
Josef Bacik96303082009-07-13 21:29:25 -04002065 struct list_head *head;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002066
Li Zefan34d52cb2011-03-29 13:46:06 +08002067 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04002068 while ((head = block_group->cluster_list.next) !=
2069 &block_group->cluster_list) {
2070 cluster = list_entry(head, struct btrfs_free_cluster,
2071 block_group_list);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002072
2073 WARN_ON(cluster->block_group != block_group);
2074 __btrfs_return_cluster_to_free_space(block_group, cluster);
Josef Bacik96303082009-07-13 21:29:25 -04002075 if (need_resched()) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002076 spin_unlock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04002077 cond_resched();
Li Zefan34d52cb2011-03-29 13:46:06 +08002078 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04002079 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04002080 }
Chris Mason09655372011-05-21 09:27:38 -04002081 __btrfs_remove_free_space_cache_locked(ctl);
Li Zefan34d52cb2011-03-29 13:46:06 +08002082 spin_unlock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002083
Josef Bacik0f9dd462008-09-23 13:14:11 -04002084}
2085
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002086u64 btrfs_find_space_for_alloc(struct btrfs_block_group_cache *block_group,
2087 u64 offset, u64 bytes, u64 empty_size)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002088{
Li Zefan34d52cb2011-03-29 13:46:06 +08002089 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002090 struct btrfs_free_space *entry = NULL;
Josef Bacik96303082009-07-13 21:29:25 -04002091 u64 bytes_search = bytes + empty_size;
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002092 u64 ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002093
Li Zefan34d52cb2011-03-29 13:46:06 +08002094 spin_lock(&ctl->tree_lock);
2095 entry = find_free_space(ctl, &offset, &bytes_search);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002096 if (!entry)
Josef Bacik96303082009-07-13 21:29:25 -04002097 goto out;
2098
2099 ret = offset;
2100 if (entry->bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002101 bitmap_clear_bits(ctl, entry, offset, bytes);
Li Zefanedf6e2d2010-11-09 14:50:07 +08002102 if (!entry->bytes)
Li Zefan34d52cb2011-03-29 13:46:06 +08002103 free_bitmap(ctl, entry);
Josef Bacik96303082009-07-13 21:29:25 -04002104 } else {
Li Zefan34d52cb2011-03-29 13:46:06 +08002105 unlink_free_space(ctl, entry);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002106 entry->offset += bytes;
2107 entry->bytes -= bytes;
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002108 if (!entry->bytes)
Josef Bacikdc89e982011-01-28 17:05:48 -05002109 kmem_cache_free(btrfs_free_space_cachep, entry);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002110 else
Li Zefan34d52cb2011-03-29 13:46:06 +08002111 link_free_space(ctl, entry);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002112 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002113
Josef Bacik96303082009-07-13 21:29:25 -04002114out:
Li Zefan34d52cb2011-03-29 13:46:06 +08002115 spin_unlock(&ctl->tree_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04002116
Josef Bacik0f9dd462008-09-23 13:14:11 -04002117 return ret;
2118}
Chris Masonfa9c0d792009-04-03 09:47:43 -04002119
2120/*
2121 * given a cluster, put all of its extents back into the free space
2122 * cache. If a block group is passed, this function will only free
2123 * a cluster that belongs to the passed block group.
2124 *
2125 * Otherwise, it'll get a reference on the block group pointed to by the
2126 * cluster and remove the cluster from it.
2127 */
2128int btrfs_return_cluster_to_free_space(
2129 struct btrfs_block_group_cache *block_group,
2130 struct btrfs_free_cluster *cluster)
2131{
Li Zefan34d52cb2011-03-29 13:46:06 +08002132 struct btrfs_free_space_ctl *ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002133 int ret;
2134
2135 /* first, get a safe pointer to the block group */
2136 spin_lock(&cluster->lock);
2137 if (!block_group) {
2138 block_group = cluster->block_group;
2139 if (!block_group) {
2140 spin_unlock(&cluster->lock);
2141 return 0;
2142 }
2143 } else if (cluster->block_group != block_group) {
2144 /* someone else has already freed it don't redo their work */
2145 spin_unlock(&cluster->lock);
2146 return 0;
2147 }
2148 atomic_inc(&block_group->count);
2149 spin_unlock(&cluster->lock);
2150
Li Zefan34d52cb2011-03-29 13:46:06 +08002151 ctl = block_group->free_space_ctl;
2152
Chris Masonfa9c0d792009-04-03 09:47:43 -04002153 /* now return any extents the cluster had on it */
Li Zefan34d52cb2011-03-29 13:46:06 +08002154 spin_lock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002155 ret = __btrfs_return_cluster_to_free_space(block_group, cluster);
Li Zefan34d52cb2011-03-29 13:46:06 +08002156 spin_unlock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002157
2158 /* finally drop our ref */
2159 btrfs_put_block_group(block_group);
2160 return ret;
2161}
2162
Josef Bacik96303082009-07-13 21:29:25 -04002163static u64 btrfs_alloc_from_bitmap(struct btrfs_block_group_cache *block_group,
2164 struct btrfs_free_cluster *cluster,
Josef Bacik4e69b592011-03-21 10:11:24 -04002165 struct btrfs_free_space *entry,
Josef Bacik96303082009-07-13 21:29:25 -04002166 u64 bytes, u64 min_start)
2167{
Li Zefan34d52cb2011-03-29 13:46:06 +08002168 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik96303082009-07-13 21:29:25 -04002169 int err;
2170 u64 search_start = cluster->window_start;
2171 u64 search_bytes = bytes;
2172 u64 ret = 0;
2173
Josef Bacik96303082009-07-13 21:29:25 -04002174 search_start = min_start;
2175 search_bytes = bytes;
2176
Li Zefan34d52cb2011-03-29 13:46:06 +08002177 err = search_bitmap(ctl, entry, &search_start, &search_bytes);
Josef Bacik96303082009-07-13 21:29:25 -04002178 if (err)
Josef Bacik4e69b592011-03-21 10:11:24 -04002179 return 0;
Josef Bacik96303082009-07-13 21:29:25 -04002180
2181 ret = search_start;
Miao Xiebb3ac5a2011-08-05 09:32:35 +00002182 __bitmap_clear_bits(ctl, entry, ret, bytes);
Josef Bacik96303082009-07-13 21:29:25 -04002183
2184 return ret;
2185}
2186
Chris Masonfa9c0d792009-04-03 09:47:43 -04002187/*
2188 * given a cluster, try to allocate 'bytes' from it, returns 0
2189 * if it couldn't find anything suitably large, or a logical disk offset
2190 * if things worked out
2191 */
2192u64 btrfs_alloc_from_cluster(struct btrfs_block_group_cache *block_group,
2193 struct btrfs_free_cluster *cluster, u64 bytes,
2194 u64 min_start)
2195{
Li Zefan34d52cb2011-03-29 13:46:06 +08002196 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002197 struct btrfs_free_space *entry = NULL;
2198 struct rb_node *node;
2199 u64 ret = 0;
2200
2201 spin_lock(&cluster->lock);
2202 if (bytes > cluster->max_size)
2203 goto out;
2204
2205 if (cluster->block_group != block_group)
2206 goto out;
2207
2208 node = rb_first(&cluster->root);
2209 if (!node)
2210 goto out;
2211
2212 entry = rb_entry(node, struct btrfs_free_space, offset_index);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002213 while(1) {
Josef Bacik4e69b592011-03-21 10:11:24 -04002214 if (entry->bytes < bytes ||
2215 (!entry->bitmap && entry->offset < min_start)) {
Chris Masonfa9c0d792009-04-03 09:47:43 -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 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04002223
Josef Bacik4e69b592011-03-21 10:11:24 -04002224 if (entry->bitmap) {
2225 ret = btrfs_alloc_from_bitmap(block_group,
2226 cluster, entry, bytes,
2227 min_start);
2228 if (ret == 0) {
Josef Bacik4e69b592011-03-21 10:11:24 -04002229 node = rb_next(&entry->offset_index);
2230 if (!node)
2231 break;
2232 entry = rb_entry(node, struct btrfs_free_space,
2233 offset_index);
2234 continue;
2235 }
2236 } else {
Josef Bacik4e69b592011-03-21 10:11:24 -04002237 ret = entry->offset;
2238
2239 entry->offset += bytes;
2240 entry->bytes -= bytes;
2241 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04002242
Li Zefan5e71b5d2010-11-09 14:55:34 +08002243 if (entry->bytes == 0)
Chris Masonfa9c0d792009-04-03 09:47:43 -04002244 rb_erase(&entry->offset_index, &cluster->root);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002245 break;
2246 }
2247out:
2248 spin_unlock(&cluster->lock);
Josef Bacik96303082009-07-13 21:29:25 -04002249
Li Zefan5e71b5d2010-11-09 14:55:34 +08002250 if (!ret)
2251 return 0;
2252
Li Zefan34d52cb2011-03-29 13:46:06 +08002253 spin_lock(&ctl->tree_lock);
Li Zefan5e71b5d2010-11-09 14:55:34 +08002254
Li Zefan34d52cb2011-03-29 13:46:06 +08002255 ctl->free_space -= bytes;
Li Zefan5e71b5d2010-11-09 14:55:34 +08002256 if (entry->bytes == 0) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002257 ctl->free_extents--;
Josef Bacik4e69b592011-03-21 10:11:24 -04002258 if (entry->bitmap) {
2259 kfree(entry->bitmap);
Li Zefan34d52cb2011-03-29 13:46:06 +08002260 ctl->total_bitmaps--;
2261 ctl->op->recalc_thresholds(ctl);
Josef Bacik4e69b592011-03-21 10:11:24 -04002262 }
Josef Bacikdc89e982011-01-28 17:05:48 -05002263 kmem_cache_free(btrfs_free_space_cachep, entry);
Li Zefan5e71b5d2010-11-09 14:55:34 +08002264 }
2265
Li Zefan34d52cb2011-03-29 13:46:06 +08002266 spin_unlock(&ctl->tree_lock);
Li Zefan5e71b5d2010-11-09 14:55:34 +08002267
Chris Masonfa9c0d792009-04-03 09:47:43 -04002268 return ret;
2269}
2270
Josef Bacik96303082009-07-13 21:29:25 -04002271static int btrfs_bitmap_cluster(struct btrfs_block_group_cache *block_group,
2272 struct btrfs_free_space *entry,
2273 struct btrfs_free_cluster *cluster,
2274 u64 offset, u64 bytes, u64 min_bytes)
2275{
Li Zefan34d52cb2011-03-29 13:46:06 +08002276 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik96303082009-07-13 21:29:25 -04002277 unsigned long next_zero;
2278 unsigned long i;
2279 unsigned long search_bits;
2280 unsigned long total_bits;
2281 unsigned long found_bits;
2282 unsigned long start = 0;
2283 unsigned long total_found = 0;
Josef Bacik4e69b592011-03-21 10:11:24 -04002284 int ret;
Josef Bacik96303082009-07-13 21:29:25 -04002285 bool found = false;
2286
2287 i = offset_to_bit(entry->offset, block_group->sectorsize,
2288 max_t(u64, offset, entry->offset));
Josef Bacikd0a365e2011-03-18 15:27:43 -04002289 search_bits = bytes_to_bits(bytes, block_group->sectorsize);
2290 total_bits = bytes_to_bits(min_bytes, block_group->sectorsize);
Josef Bacik96303082009-07-13 21:29:25 -04002291
2292again:
2293 found_bits = 0;
2294 for (i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, i);
2295 i < BITS_PER_BITMAP;
2296 i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, i + 1)) {
2297 next_zero = find_next_zero_bit(entry->bitmap,
2298 BITS_PER_BITMAP, i);
2299 if (next_zero - i >= search_bits) {
2300 found_bits = next_zero - i;
2301 break;
2302 }
2303 i = next_zero;
2304 }
2305
2306 if (!found_bits)
Josef Bacik4e69b592011-03-21 10:11:24 -04002307 return -ENOSPC;
Josef Bacik96303082009-07-13 21:29:25 -04002308
2309 if (!found) {
2310 start = i;
2311 found = true;
2312 }
2313
2314 total_found += found_bits;
2315
2316 if (cluster->max_size < found_bits * block_group->sectorsize)
2317 cluster->max_size = found_bits * block_group->sectorsize;
2318
2319 if (total_found < total_bits) {
2320 i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, next_zero);
2321 if (i - start > total_bits * 2) {
2322 total_found = 0;
2323 cluster->max_size = 0;
2324 found = false;
2325 }
2326 goto again;
2327 }
2328
2329 cluster->window_start = start * block_group->sectorsize +
2330 entry->offset;
Li Zefan34d52cb2011-03-29 13:46:06 +08002331 rb_erase(&entry->offset_index, &ctl->free_space_offset);
Josef Bacik4e69b592011-03-21 10:11:24 -04002332 ret = tree_insert_offset(&cluster->root, entry->offset,
2333 &entry->offset_index, 1);
2334 BUG_ON(ret);
Josef Bacik96303082009-07-13 21:29:25 -04002335
2336 return 0;
2337}
2338
Chris Masonfa9c0d792009-04-03 09:47:43 -04002339/*
Josef Bacik4e69b592011-03-21 10:11:24 -04002340 * This searches the block group for just extents to fill the cluster with.
2341 */
Josef Bacik3de85bb2011-05-25 13:07:37 -04002342static noinline int
2343setup_cluster_no_bitmap(struct btrfs_block_group_cache *block_group,
2344 struct btrfs_free_cluster *cluster,
2345 struct list_head *bitmaps, u64 offset, u64 bytes,
2346 u64 min_bytes)
Josef Bacik4e69b592011-03-21 10:11:24 -04002347{
Li Zefan34d52cb2011-03-29 13:46:06 +08002348 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik4e69b592011-03-21 10:11:24 -04002349 struct btrfs_free_space *first = NULL;
2350 struct btrfs_free_space *entry = NULL;
2351 struct btrfs_free_space *prev = NULL;
2352 struct btrfs_free_space *last;
2353 struct rb_node *node;
2354 u64 window_start;
2355 u64 window_free;
2356 u64 max_extent;
2357 u64 max_gap = 128 * 1024;
2358
Li Zefan34d52cb2011-03-29 13:46:06 +08002359 entry = tree_search_offset(ctl, offset, 0, 1);
Josef Bacik4e69b592011-03-21 10:11:24 -04002360 if (!entry)
2361 return -ENOSPC;
2362
2363 /*
2364 * We don't want bitmaps, so just move along until we find a normal
2365 * extent entry.
2366 */
2367 while (entry->bitmap) {
Josef Bacik86d4a772011-05-25 13:03:16 -04002368 if (list_empty(&entry->list))
2369 list_add_tail(&entry->list, bitmaps);
Josef Bacik4e69b592011-03-21 10:11:24 -04002370 node = rb_next(&entry->offset_index);
2371 if (!node)
2372 return -ENOSPC;
2373 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2374 }
2375
2376 window_start = entry->offset;
2377 window_free = entry->bytes;
2378 max_extent = entry->bytes;
2379 first = entry;
2380 last = entry;
2381 prev = entry;
2382
2383 while (window_free <= min_bytes) {
2384 node = rb_next(&entry->offset_index);
2385 if (!node)
2386 return -ENOSPC;
2387 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2388
Josef Bacik86d4a772011-05-25 13:03:16 -04002389 if (entry->bitmap) {
2390 if (list_empty(&entry->list))
2391 list_add_tail(&entry->list, bitmaps);
Josef Bacik4e69b592011-03-21 10:11:24 -04002392 continue;
Josef Bacik86d4a772011-05-25 13:03:16 -04002393 }
2394
Josef Bacik4e69b592011-03-21 10:11:24 -04002395 /*
2396 * we haven't filled the empty size and the window is
2397 * very large. reset and try again
2398 */
2399 if (entry->offset - (prev->offset + prev->bytes) > max_gap ||
2400 entry->offset - window_start > (min_bytes * 2)) {
2401 first = entry;
2402 window_start = entry->offset;
2403 window_free = entry->bytes;
2404 last = entry;
2405 max_extent = entry->bytes;
2406 } else {
2407 last = entry;
2408 window_free += entry->bytes;
2409 if (entry->bytes > max_extent)
2410 max_extent = entry->bytes;
2411 }
2412 prev = entry;
2413 }
2414
2415 cluster->window_start = first->offset;
2416
2417 node = &first->offset_index;
2418
2419 /*
2420 * now we've found our entries, pull them out of the free space
2421 * cache and put them into the cluster rbtree
2422 */
2423 do {
2424 int ret;
2425
2426 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2427 node = rb_next(&entry->offset_index);
2428 if (entry->bitmap)
2429 continue;
2430
Li Zefan34d52cb2011-03-29 13:46:06 +08002431 rb_erase(&entry->offset_index, &ctl->free_space_offset);
Josef Bacik4e69b592011-03-21 10:11:24 -04002432 ret = tree_insert_offset(&cluster->root, entry->offset,
2433 &entry->offset_index, 0);
2434 BUG_ON(ret);
2435 } while (node && entry != last);
2436
2437 cluster->max_size = max_extent;
2438
2439 return 0;
2440}
2441
2442/*
2443 * This specifically looks for bitmaps that may work in the cluster, we assume
2444 * that we have already failed to find extents that will work.
2445 */
Josef Bacik3de85bb2011-05-25 13:07:37 -04002446static noinline int
2447setup_cluster_bitmap(struct btrfs_block_group_cache *block_group,
2448 struct btrfs_free_cluster *cluster,
2449 struct list_head *bitmaps, u64 offset, u64 bytes,
2450 u64 min_bytes)
Josef Bacik4e69b592011-03-21 10:11:24 -04002451{
Li Zefan34d52cb2011-03-29 13:46:06 +08002452 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik4e69b592011-03-21 10:11:24 -04002453 struct btrfs_free_space *entry;
Josef Bacik4e69b592011-03-21 10:11:24 -04002454 int ret = -ENOSPC;
Li Zefan0f0fbf12011-11-20 07:33:38 -05002455 u64 bitmap_offset = offset_to_bitmap(ctl, offset);
Josef Bacik4e69b592011-03-21 10:11:24 -04002456
Li Zefan34d52cb2011-03-29 13:46:06 +08002457 if (ctl->total_bitmaps == 0)
Josef Bacik4e69b592011-03-21 10:11:24 -04002458 return -ENOSPC;
2459
Josef Bacik86d4a772011-05-25 13:03:16 -04002460 /*
Li Zefan0f0fbf12011-11-20 07:33:38 -05002461 * The bitmap that covers offset won't be in the list unless offset
2462 * is just its start offset.
2463 */
2464 entry = list_first_entry(bitmaps, struct btrfs_free_space, list);
2465 if (entry->offset != bitmap_offset) {
2466 entry = tree_search_offset(ctl, bitmap_offset, 1, 0);
2467 if (entry && list_empty(&entry->list))
2468 list_add(&entry->list, bitmaps);
2469 }
2470
Josef Bacik86d4a772011-05-25 13:03:16 -04002471 list_for_each_entry(entry, bitmaps, list) {
2472 if (entry->bytes < min_bytes)
2473 continue;
2474 ret = btrfs_bitmap_cluster(block_group, entry, cluster, offset,
2475 bytes, min_bytes);
2476 if (!ret)
2477 return 0;
2478 }
2479
2480 /*
Li Zefan52621cb2011-11-20 07:33:38 -05002481 * The bitmaps list has all the bitmaps that record free space
2482 * starting after offset, so no more search is required.
Josef Bacik86d4a772011-05-25 13:03:16 -04002483 */
Li Zefan52621cb2011-11-20 07:33:38 -05002484 return -ENOSPC;
Josef Bacik4e69b592011-03-21 10:11:24 -04002485}
2486
2487/*
Chris Masonfa9c0d792009-04-03 09:47:43 -04002488 * here we try to find a cluster of blocks in a block group. The goal
2489 * is to find at least bytes free and up to empty_size + bytes free.
2490 * We might not find them all in one contiguous area.
2491 *
2492 * returns zero and sets up cluster if things worked out, otherwise
2493 * it returns -enospc
2494 */
2495int btrfs_find_space_cluster(struct btrfs_trans_handle *trans,
Chris Mason451d7582009-06-09 20:28:34 -04002496 struct btrfs_root *root,
Chris Masonfa9c0d792009-04-03 09:47:43 -04002497 struct btrfs_block_group_cache *block_group,
2498 struct btrfs_free_cluster *cluster,
2499 u64 offset, u64 bytes, u64 empty_size)
2500{
Li Zefan34d52cb2011-03-29 13:46:06 +08002501 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik86d4a772011-05-25 13:03:16 -04002502 struct btrfs_free_space *entry, *tmp;
Li Zefan52621cb2011-11-20 07:33:38 -05002503 LIST_HEAD(bitmaps);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002504 u64 min_bytes;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002505 int ret;
2506
2507 /* for metadata, allow allocates with more holes */
Chris Mason451d7582009-06-09 20:28:34 -04002508 if (btrfs_test_opt(root, SSD_SPREAD)) {
2509 min_bytes = bytes + empty_size;
2510 } else if (block_group->flags & BTRFS_BLOCK_GROUP_METADATA) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04002511 /*
2512 * we want to do larger allocations when we are
2513 * flushing out the delayed refs, it helps prevent
2514 * making more work as we go along.
2515 */
2516 if (trans->transaction->delayed_refs.flushing)
2517 min_bytes = max(bytes, (bytes + empty_size) >> 1);
2518 else
2519 min_bytes = max(bytes, (bytes + empty_size) >> 4);
2520 } else
2521 min_bytes = max(bytes, (bytes + empty_size) >> 2);
2522
Li Zefan34d52cb2011-03-29 13:46:06 +08002523 spin_lock(&ctl->tree_lock);
Josef Bacik7d0d2e82011-03-18 15:13:42 -04002524
2525 /*
2526 * If we know we don't have enough space to make a cluster don't even
2527 * bother doing all the work to try and find one.
2528 */
Li Zefan34d52cb2011-03-29 13:46:06 +08002529 if (ctl->free_space < min_bytes) {
2530 spin_unlock(&ctl->tree_lock);
Josef Bacik7d0d2e82011-03-18 15:13:42 -04002531 return -ENOSPC;
2532 }
2533
Chris Masonfa9c0d792009-04-03 09:47:43 -04002534 spin_lock(&cluster->lock);
2535
2536 /* someone already found a cluster, hooray */
2537 if (cluster->block_group) {
2538 ret = 0;
2539 goto out;
2540 }
Josef Bacik4e69b592011-03-21 10:11:24 -04002541
Josef Bacik86d4a772011-05-25 13:03:16 -04002542 ret = setup_cluster_no_bitmap(block_group, cluster, &bitmaps, offset,
2543 bytes, min_bytes);
Josef Bacik4e69b592011-03-21 10:11:24 -04002544 if (ret)
Josef Bacik86d4a772011-05-25 13:03:16 -04002545 ret = setup_cluster_bitmap(block_group, cluster, &bitmaps,
2546 offset, bytes, min_bytes);
2547
2548 /* Clear our temporary list */
2549 list_for_each_entry_safe(entry, tmp, &bitmaps, list)
2550 list_del_init(&entry->list);
Josef Bacik4e69b592011-03-21 10:11:24 -04002551
2552 if (!ret) {
2553 atomic_inc(&block_group->count);
2554 list_add_tail(&cluster->block_group_list,
2555 &block_group->cluster_list);
2556 cluster->block_group = block_group;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002557 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04002558out:
2559 spin_unlock(&cluster->lock);
Li Zefan34d52cb2011-03-29 13:46:06 +08002560 spin_unlock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002561
2562 return ret;
2563}
2564
2565/*
2566 * simple code to zero out a cluster
2567 */
2568void btrfs_init_free_cluster(struct btrfs_free_cluster *cluster)
2569{
2570 spin_lock_init(&cluster->lock);
2571 spin_lock_init(&cluster->refill_lock);
Eric Paris6bef4d32010-02-23 19:43:04 +00002572 cluster->root = RB_ROOT;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002573 cluster->max_size = 0;
2574 INIT_LIST_HEAD(&cluster->block_group_list);
2575 cluster->block_group = NULL;
2576}
2577
Li Dongyangf7039b12011-03-24 10:24:28 +00002578int btrfs_trim_block_group(struct btrfs_block_group_cache *block_group,
2579 u64 *trimmed, u64 start, u64 end, u64 minlen)
2580{
Li Zefan34d52cb2011-03-29 13:46:06 +08002581 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Li Dongyangf7039b12011-03-24 10:24:28 +00002582 struct btrfs_free_space *entry = NULL;
2583 struct btrfs_fs_info *fs_info = block_group->fs_info;
2584 u64 bytes = 0;
2585 u64 actually_trimmed;
2586 int ret = 0;
2587
2588 *trimmed = 0;
2589
2590 while (start < end) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002591 spin_lock(&ctl->tree_lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00002592
Li Zefan34d52cb2011-03-29 13:46:06 +08002593 if (ctl->free_space < minlen) {
2594 spin_unlock(&ctl->tree_lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00002595 break;
2596 }
2597
Li Zefan34d52cb2011-03-29 13:46:06 +08002598 entry = tree_search_offset(ctl, start, 0, 1);
Li Dongyangf7039b12011-03-24 10:24:28 +00002599 if (!entry)
Li Zefan34d52cb2011-03-29 13:46:06 +08002600 entry = tree_search_offset(ctl,
2601 offset_to_bitmap(ctl, start),
Li Dongyangf7039b12011-03-24 10:24:28 +00002602 1, 1);
2603
2604 if (!entry || entry->offset >= end) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002605 spin_unlock(&ctl->tree_lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00002606 break;
2607 }
2608
2609 if (entry->bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002610 ret = search_bitmap(ctl, entry, &start, &bytes);
Li Dongyangf7039b12011-03-24 10:24:28 +00002611 if (!ret) {
2612 if (start >= end) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002613 spin_unlock(&ctl->tree_lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00002614 break;
2615 }
2616 bytes = min(bytes, end - start);
Li Zefan34d52cb2011-03-29 13:46:06 +08002617 bitmap_clear_bits(ctl, entry, start, bytes);
Li Dongyangf7039b12011-03-24 10:24:28 +00002618 if (entry->bytes == 0)
Li Zefan34d52cb2011-03-29 13:46:06 +08002619 free_bitmap(ctl, entry);
Li Dongyangf7039b12011-03-24 10:24:28 +00002620 } else {
2621 start = entry->offset + BITS_PER_BITMAP *
2622 block_group->sectorsize;
Li Zefan34d52cb2011-03-29 13:46:06 +08002623 spin_unlock(&ctl->tree_lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00002624 ret = 0;
2625 continue;
2626 }
2627 } else {
2628 start = entry->offset;
2629 bytes = min(entry->bytes, end - start);
Li Zefan34d52cb2011-03-29 13:46:06 +08002630 unlink_free_space(ctl, entry);
Li Zefanf789b682011-04-25 19:43:52 -04002631 kmem_cache_free(btrfs_free_space_cachep, entry);
Li Dongyangf7039b12011-03-24 10:24:28 +00002632 }
2633
Li Zefan34d52cb2011-03-29 13:46:06 +08002634 spin_unlock(&ctl->tree_lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00002635
2636 if (bytes >= minlen) {
Josef Bacikfb25e912011-07-26 17:00:46 -04002637 struct btrfs_space_info *space_info;
2638 int update = 0;
2639
2640 space_info = block_group->space_info;
2641 spin_lock(&space_info->lock);
2642 spin_lock(&block_group->lock);
2643 if (!block_group->ro) {
2644 block_group->reserved += bytes;
2645 space_info->bytes_reserved += bytes;
2646 update = 1;
2647 }
2648 spin_unlock(&block_group->lock);
2649 spin_unlock(&space_info->lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00002650
2651 ret = btrfs_error_discard_extent(fs_info->extent_root,
2652 start,
2653 bytes,
2654 &actually_trimmed);
2655
Li Zefan34d52cb2011-03-29 13:46:06 +08002656 btrfs_add_free_space(block_group, start, bytes);
Josef Bacikfb25e912011-07-26 17:00:46 -04002657 if (update) {
2658 spin_lock(&space_info->lock);
2659 spin_lock(&block_group->lock);
2660 if (block_group->ro)
2661 space_info->bytes_readonly += bytes;
2662 block_group->reserved -= bytes;
2663 space_info->bytes_reserved -= bytes;
2664 spin_unlock(&space_info->lock);
2665 spin_unlock(&block_group->lock);
2666 }
Li Dongyangf7039b12011-03-24 10:24:28 +00002667
2668 if (ret)
2669 break;
2670 *trimmed += actually_trimmed;
2671 }
2672 start += bytes;
2673 bytes = 0;
2674
2675 if (fatal_signal_pending(current)) {
2676 ret = -ERESTARTSYS;
2677 break;
2678 }
2679
2680 cond_resched();
2681 }
2682
2683 return ret;
2684}
Li Zefan581bb052011-04-20 10:06:11 +08002685
2686/*
2687 * Find the left-most item in the cache tree, and then return the
2688 * smallest inode number in the item.
2689 *
2690 * Note: the returned inode number may not be the smallest one in
2691 * the tree, if the left-most item is a bitmap.
2692 */
2693u64 btrfs_find_ino_for_alloc(struct btrfs_root *fs_root)
2694{
2695 struct btrfs_free_space_ctl *ctl = fs_root->free_ino_ctl;
2696 struct btrfs_free_space *entry = NULL;
2697 u64 ino = 0;
2698
2699 spin_lock(&ctl->tree_lock);
2700
2701 if (RB_EMPTY_ROOT(&ctl->free_space_offset))
2702 goto out;
2703
2704 entry = rb_entry(rb_first(&ctl->free_space_offset),
2705 struct btrfs_free_space, offset_index);
2706
2707 if (!entry->bitmap) {
2708 ino = entry->offset;
2709
2710 unlink_free_space(ctl, entry);
2711 entry->offset++;
2712 entry->bytes--;
2713 if (!entry->bytes)
2714 kmem_cache_free(btrfs_free_space_cachep, entry);
2715 else
2716 link_free_space(ctl, entry);
2717 } else {
2718 u64 offset = 0;
2719 u64 count = 1;
2720 int ret;
2721
2722 ret = search_bitmap(ctl, entry, &offset, &count);
2723 BUG_ON(ret);
2724
2725 ino = offset;
2726 bitmap_clear_bits(ctl, entry, offset, 1);
2727 if (entry->bytes == 0)
2728 free_bitmap(ctl, entry);
2729 }
2730out:
2731 spin_unlock(&ctl->tree_lock);
2732
2733 return ino;
2734}
Li Zefan82d59022011-04-20 10:33:24 +08002735
2736struct inode *lookup_free_ino_inode(struct btrfs_root *root,
2737 struct btrfs_path *path)
2738{
2739 struct inode *inode = NULL;
2740
2741 spin_lock(&root->cache_lock);
2742 if (root->cache_inode)
2743 inode = igrab(root->cache_inode);
2744 spin_unlock(&root->cache_lock);
2745 if (inode)
2746 return inode;
2747
2748 inode = __lookup_free_space_inode(root, path, 0);
2749 if (IS_ERR(inode))
2750 return inode;
2751
2752 spin_lock(&root->cache_lock);
David Sterba7841cb22011-05-31 18:07:27 +02002753 if (!btrfs_fs_closing(root->fs_info))
Li Zefan82d59022011-04-20 10:33:24 +08002754 root->cache_inode = igrab(inode);
2755 spin_unlock(&root->cache_lock);
2756
2757 return inode;
2758}
2759
2760int create_free_ino_inode(struct btrfs_root *root,
2761 struct btrfs_trans_handle *trans,
2762 struct btrfs_path *path)
2763{
2764 return __create_free_space_inode(root, trans, path,
2765 BTRFS_FREE_INO_OBJECTID, 0);
2766}
2767
2768int load_free_ino_cache(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
2769{
2770 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
2771 struct btrfs_path *path;
2772 struct inode *inode;
2773 int ret = 0;
2774 u64 root_gen = btrfs_root_generation(&root->root_item);
2775
Chris Mason4b9465c2011-06-03 09:36:29 -04002776 if (!btrfs_test_opt(root, INODE_MAP_CACHE))
2777 return 0;
2778
Li Zefan82d59022011-04-20 10:33:24 +08002779 /*
2780 * If we're unmounting then just return, since this does a search on the
2781 * normal root and not the commit root and we could deadlock.
2782 */
David Sterba7841cb22011-05-31 18:07:27 +02002783 if (btrfs_fs_closing(fs_info))
Li Zefan82d59022011-04-20 10:33:24 +08002784 return 0;
2785
2786 path = btrfs_alloc_path();
2787 if (!path)
2788 return 0;
2789
2790 inode = lookup_free_ino_inode(root, path);
2791 if (IS_ERR(inode))
2792 goto out;
2793
2794 if (root_gen != BTRFS_I(inode)->generation)
2795 goto out_put;
2796
2797 ret = __load_free_space_cache(root, inode, ctl, path, 0);
2798
2799 if (ret < 0)
2800 printk(KERN_ERR "btrfs: failed to load free ino cache for "
2801 "root %llu\n", root->root_key.objectid);
2802out_put:
2803 iput(inode);
2804out:
2805 btrfs_free_path(path);
2806 return ret;
2807}
2808
2809int btrfs_write_out_ino_cache(struct btrfs_root *root,
2810 struct btrfs_trans_handle *trans,
2811 struct btrfs_path *path)
2812{
2813 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
2814 struct inode *inode;
2815 int ret;
2816
Chris Mason4b9465c2011-06-03 09:36:29 -04002817 if (!btrfs_test_opt(root, INODE_MAP_CACHE))
2818 return 0;
2819
Li Zefan82d59022011-04-20 10:33:24 +08002820 inode = lookup_free_ino_inode(root, path);
2821 if (IS_ERR(inode))
2822 return 0;
2823
2824 ret = __btrfs_write_out_cache(root, inode, ctl, NULL, trans, path, 0);
Josef Bacikc09544e2011-08-30 10:19:10 -04002825 if (ret) {
2826 btrfs_delalloc_release_metadata(inode, inode->i_size);
2827#ifdef DEBUG
Li Zefan82d59022011-04-20 10:33:24 +08002828 printk(KERN_ERR "btrfs: failed to write free ino cache "
2829 "for root %llu\n", root->root_key.objectid);
Josef Bacikc09544e2011-08-30 10:19:10 -04002830#endif
2831 }
Li Zefan82d59022011-04-20 10:33:24 +08002832
2833 iput(inode);
2834 return ret;
2835}