blob: 7a15fcfb3e1fb688f49d0691544a72581a328fa3 [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 Bacika67509c2011-10-05 15:18:58 -0400540
541 e = io_ctl->cur;
542 entry->offset = le64_to_cpu(e->offset);
543 entry->bytes = le64_to_cpu(e->bytes);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400544 *type = e->type;
Josef Bacika67509c2011-10-05 15:18:58 -0400545 io_ctl->cur += sizeof(struct btrfs_free_space_entry);
546 io_ctl->size -= sizeof(struct btrfs_free_space_entry);
547
548 if (io_ctl->size >= sizeof(struct btrfs_free_space_entry))
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400549 return 0;
Josef Bacika67509c2011-10-05 15:18:58 -0400550
551 io_ctl_unmap_page(io_ctl);
552
553 if (io_ctl->index >= io_ctl->num_pages)
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400554 return 0;
Josef Bacika67509c2011-10-05 15:18:58 -0400555
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400556 return io_ctl_check_crc(io_ctl, io_ctl->index);
Josef Bacika67509c2011-10-05 15:18:58 -0400557}
558
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400559static int io_ctl_read_bitmap(struct io_ctl *io_ctl,
560 struct btrfs_free_space *entry)
Josef Bacika67509c2011-10-05 15:18:58 -0400561{
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400562 int ret;
563
564 if (io_ctl->cur && io_ctl->cur != io_ctl->orig)
Josef Bacika67509c2011-10-05 15:18:58 -0400565 io_ctl_unmap_page(io_ctl);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400566
567 ret = io_ctl_check_crc(io_ctl, io_ctl->index);
568 if (ret)
569 return ret;
570
Josef Bacika67509c2011-10-05 15:18:58 -0400571 memcpy(entry->bitmap, io_ctl->cur, PAGE_CACHE_SIZE);
572 io_ctl_unmap_page(io_ctl);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400573
574 return 0;
Josef Bacika67509c2011-10-05 15:18:58 -0400575}
576
Li Zefan0414efa2011-04-20 10:20:14 +0800577int __load_free_space_cache(struct btrfs_root *root, struct inode *inode,
578 struct btrfs_free_space_ctl *ctl,
579 struct btrfs_path *path, u64 offset)
Josef Bacik9d66e232010-08-25 16:54:15 -0400580{
Josef Bacik9d66e232010-08-25 16:54:15 -0400581 struct btrfs_free_space_header *header;
582 struct extent_buffer *leaf;
Josef Bacika67509c2011-10-05 15:18:58 -0400583 struct io_ctl io_ctl;
Josef Bacik9d66e232010-08-25 16:54:15 -0400584 struct btrfs_key key;
Josef Bacika67509c2011-10-05 15:18:58 -0400585 struct btrfs_free_space *e, *n;
Josef Bacik9d66e232010-08-25 16:54:15 -0400586 struct list_head bitmaps;
587 u64 num_entries;
588 u64 num_bitmaps;
589 u64 generation;
Josef Bacika67509c2011-10-05 15:18:58 -0400590 u8 type;
Josef Bacikf6a39822011-06-06 10:50:35 -0400591 int ret = 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400592
593 INIT_LIST_HEAD(&bitmaps);
594
Josef Bacik9d66e232010-08-25 16:54:15 -0400595 /* Nothing in the space cache, goodbye */
Li Zefan0414efa2011-04-20 10:20:14 +0800596 if (!i_size_read(inode))
Josef Bacika67509c2011-10-05 15:18:58 -0400597 return 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400598
599 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
Li Zefan0414efa2011-04-20 10:20:14 +0800600 key.offset = offset;
Josef Bacik9d66e232010-08-25 16:54:15 -0400601 key.type = 0;
602
603 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
Li Zefan0414efa2011-04-20 10:20:14 +0800604 if (ret < 0)
Josef Bacika67509c2011-10-05 15:18:58 -0400605 return 0;
Li Zefan0414efa2011-04-20 10:20:14 +0800606 else if (ret > 0) {
Chris Mason945d8962011-05-22 12:33:42 -0400607 btrfs_release_path(path);
Josef Bacika67509c2011-10-05 15:18:58 -0400608 return 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400609 }
610
Li Zefan0414efa2011-04-20 10:20:14 +0800611 ret = -1;
612
Josef Bacik9d66e232010-08-25 16:54:15 -0400613 leaf = path->nodes[0];
614 header = btrfs_item_ptr(leaf, path->slots[0],
615 struct btrfs_free_space_header);
616 num_entries = btrfs_free_space_entries(leaf, header);
617 num_bitmaps = btrfs_free_space_bitmaps(leaf, header);
618 generation = btrfs_free_space_generation(leaf, header);
Chris Mason945d8962011-05-22 12:33:42 -0400619 btrfs_release_path(path);
Josef Bacik9d66e232010-08-25 16:54:15 -0400620
621 if (BTRFS_I(inode)->generation != generation) {
622 printk(KERN_ERR "btrfs: free space inode generation (%llu) did"
Li Zefan0414efa2011-04-20 10:20:14 +0800623 " not match free space cache generation (%llu)\n",
Josef Bacik9d66e232010-08-25 16:54:15 -0400624 (unsigned long long)BTRFS_I(inode)->generation,
Li Zefan0414efa2011-04-20 10:20:14 +0800625 (unsigned long long)generation);
Josef Bacika67509c2011-10-05 15:18:58 -0400626 return 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400627 }
628
629 if (!num_entries)
Josef Bacika67509c2011-10-05 15:18:58 -0400630 return 0;
Josef Bacik9d66e232010-08-25 16:54:15 -0400631
Josef Bacika67509c2011-10-05 15:18:58 -0400632 io_ctl_init(&io_ctl, inode, root);
Josef Bacik9d66e232010-08-25 16:54:15 -0400633 ret = readahead_cache(inode);
Li Zefan0414efa2011-04-20 10:20:14 +0800634 if (ret)
Josef Bacik9d66e232010-08-25 16:54:15 -0400635 goto out;
Josef Bacik9d66e232010-08-25 16:54:15 -0400636
Josef Bacika67509c2011-10-05 15:18:58 -0400637 ret = io_ctl_prepare_pages(&io_ctl, inode, 1);
638 if (ret)
639 goto out;
Josef Bacik9d66e232010-08-25 16:54:15 -0400640
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400641 ret = io_ctl_check_crc(&io_ctl, 0);
642 if (ret)
643 goto free_cache;
644
Josef Bacika67509c2011-10-05 15:18:58 -0400645 ret = io_ctl_check_generation(&io_ctl, generation);
646 if (ret)
647 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400648
Josef Bacika67509c2011-10-05 15:18:58 -0400649 while (num_entries) {
650 e = kmem_cache_zalloc(btrfs_free_space_cachep,
651 GFP_NOFS);
652 if (!e)
Josef Bacik9d66e232010-08-25 16:54:15 -0400653 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400654
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400655 ret = io_ctl_read_entry(&io_ctl, e, &type);
656 if (ret) {
657 kmem_cache_free(btrfs_free_space_cachep, e);
658 goto free_cache;
659 }
660
Josef Bacika67509c2011-10-05 15:18:58 -0400661 if (!e->bytes) {
662 kmem_cache_free(btrfs_free_space_cachep, e);
663 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400664 }
Josef Bacik9d66e232010-08-25 16:54:15 -0400665
Josef Bacika67509c2011-10-05 15:18:58 -0400666 if (type == BTRFS_FREE_SPACE_EXTENT) {
667 spin_lock(&ctl->tree_lock);
668 ret = link_free_space(ctl, e);
669 spin_unlock(&ctl->tree_lock);
670 if (ret) {
671 printk(KERN_ERR "Duplicate entries in "
672 "free space cache, dumping\n");
Josef Bacikdc89e982011-01-28 17:05:48 -0500673 kmem_cache_free(btrfs_free_space_cachep, e);
Josef Bacik9d66e232010-08-25 16:54:15 -0400674 goto free_cache;
675 }
Josef Bacika67509c2011-10-05 15:18:58 -0400676 } else {
677 BUG_ON(!num_bitmaps);
678 num_bitmaps--;
679 e->bitmap = kzalloc(PAGE_CACHE_SIZE, GFP_NOFS);
680 if (!e->bitmap) {
681 kmem_cache_free(
682 btrfs_free_space_cachep, e);
683 goto free_cache;
Josef Bacik9d66e232010-08-25 16:54:15 -0400684 }
Josef Bacika67509c2011-10-05 15:18:58 -0400685 spin_lock(&ctl->tree_lock);
686 ret = link_free_space(ctl, e);
687 ctl->total_bitmaps++;
688 ctl->op->recalc_thresholds(ctl);
689 spin_unlock(&ctl->tree_lock);
690 if (ret) {
691 printk(KERN_ERR "Duplicate entries in "
692 "free space cache, dumping\n");
693 kmem_cache_free(btrfs_free_space_cachep, e);
694 goto free_cache;
695 }
696 list_add_tail(&e->list, &bitmaps);
Josef Bacik9d66e232010-08-25 16:54:15 -0400697 }
698
Josef Bacika67509c2011-10-05 15:18:58 -0400699 num_entries--;
Josef Bacik9d66e232010-08-25 16:54:15 -0400700 }
701
Josef Bacika67509c2011-10-05 15:18:58 -0400702 /*
703 * We add the bitmaps at the end of the entries in order that
704 * the bitmap entries are added to the cache.
705 */
706 list_for_each_entry_safe(e, n, &bitmaps, list) {
707 list_del_init(&e->list);
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400708 ret = io_ctl_read_bitmap(&io_ctl, e);
709 if (ret)
710 goto free_cache;
Josef Bacika67509c2011-10-05 15:18:58 -0400711 }
712
713 io_ctl_drop_pages(&io_ctl);
Josef Bacik9d66e232010-08-25 16:54:15 -0400714 ret = 1;
715out:
Josef Bacika67509c2011-10-05 15:18:58 -0400716 io_ctl_free(&io_ctl);
Josef Bacik9d66e232010-08-25 16:54:15 -0400717 return ret;
Josef Bacik9d66e232010-08-25 16:54:15 -0400718free_cache:
Josef Bacika67509c2011-10-05 15:18:58 -0400719 io_ctl_drop_pages(&io_ctl);
Li Zefan0414efa2011-04-20 10:20:14 +0800720 __btrfs_remove_free_space_cache(ctl);
Josef Bacik9d66e232010-08-25 16:54:15 -0400721 goto out;
722}
723
Li Zefan0414efa2011-04-20 10:20:14 +0800724int load_free_space_cache(struct btrfs_fs_info *fs_info,
725 struct btrfs_block_group_cache *block_group)
Josef Bacik0cb59c92010-07-02 12:14:14 -0400726{
Li Zefan34d52cb2011-03-29 13:46:06 +0800727 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Li Zefan0414efa2011-04-20 10:20:14 +0800728 struct btrfs_root *root = fs_info->tree_root;
729 struct inode *inode;
730 struct btrfs_path *path;
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400731 int ret = 0;
Li Zefan0414efa2011-04-20 10:20:14 +0800732 bool matched;
733 u64 used = btrfs_block_group_used(&block_group->item);
734
735 /*
736 * If we're unmounting then just return, since this does a search on the
737 * normal root and not the commit root and we could deadlock.
738 */
David Sterba7841cb22011-05-31 18:07:27 +0200739 if (btrfs_fs_closing(fs_info))
Li Zefan0414efa2011-04-20 10:20:14 +0800740 return 0;
741
742 /*
743 * If this block group has been marked to be cleared for one reason or
744 * another then we can't trust the on disk cache, so just return.
745 */
746 spin_lock(&block_group->lock);
747 if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) {
748 spin_unlock(&block_group->lock);
749 return 0;
750 }
751 spin_unlock(&block_group->lock);
752
753 path = btrfs_alloc_path();
754 if (!path)
755 return 0;
756
757 inode = lookup_free_space_inode(root, block_group, path);
758 if (IS_ERR(inode)) {
759 btrfs_free_path(path);
760 return 0;
761 }
762
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400763 /* We may have converted the inode and made the cache invalid. */
764 spin_lock(&block_group->lock);
765 if (block_group->disk_cache_state != BTRFS_DC_WRITTEN) {
766 spin_unlock(&block_group->lock);
767 goto out;
768 }
769 spin_unlock(&block_group->lock);
770
Li Zefan0414efa2011-04-20 10:20:14 +0800771 ret = __load_free_space_cache(fs_info->tree_root, inode, ctl,
772 path, block_group->key.objectid);
773 btrfs_free_path(path);
774 if (ret <= 0)
775 goto out;
776
777 spin_lock(&ctl->tree_lock);
778 matched = (ctl->free_space == (block_group->key.offset - used -
779 block_group->bytes_super));
780 spin_unlock(&ctl->tree_lock);
781
782 if (!matched) {
783 __btrfs_remove_free_space_cache(ctl);
784 printk(KERN_ERR "block group %llu has an wrong amount of free "
785 "space\n", block_group->key.objectid);
786 ret = -1;
787 }
788out:
789 if (ret < 0) {
790 /* This cache is bogus, make sure it gets cleared */
791 spin_lock(&block_group->lock);
792 block_group->disk_cache_state = BTRFS_DC_CLEAR;
793 spin_unlock(&block_group->lock);
Li Zefan82d59022011-04-20 10:33:24 +0800794 ret = 0;
Li Zefan0414efa2011-04-20 10:20:14 +0800795
796 printk(KERN_ERR "btrfs: failed to load free space cache "
797 "for block group %llu\n", block_group->key.objectid);
798 }
799
800 iput(inode);
801 return ret;
802}
803
Josef Bacikc09544e2011-08-30 10:19:10 -0400804/**
805 * __btrfs_write_out_cache - write out cached info to an inode
806 * @root - the root the inode belongs to
807 * @ctl - the free space cache we are going to write out
808 * @block_group - the block_group for this cache if it belongs to a block_group
809 * @trans - the trans handle
810 * @path - the path to use
811 * @offset - the offset for the key we'll insert
812 *
813 * This function writes out a free space cache struct to disk for quick recovery
814 * on mount. This will return 0 if it was successfull in writing the cache out,
815 * and -1 if it was not.
816 */
Li Zefan0414efa2011-04-20 10:20:14 +0800817int __btrfs_write_out_cache(struct btrfs_root *root, struct inode *inode,
818 struct btrfs_free_space_ctl *ctl,
819 struct btrfs_block_group_cache *block_group,
820 struct btrfs_trans_handle *trans,
821 struct btrfs_path *path, u64 offset)
Josef Bacik0cb59c92010-07-02 12:14:14 -0400822{
823 struct btrfs_free_space_header *header;
824 struct extent_buffer *leaf;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400825 struct rb_node *node;
826 struct list_head *pos, *n;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400827 struct extent_state *cached_state = NULL;
Josef Bacik43be2142011-04-01 14:55:00 +0000828 struct btrfs_free_cluster *cluster = NULL;
829 struct extent_io_tree *unpin = NULL;
Josef Bacika67509c2011-10-05 15:18:58 -0400830 struct io_ctl io_ctl;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400831 struct list_head bitmap_list;
832 struct btrfs_key key;
Josef Bacik43be2142011-04-01 14:55:00 +0000833 u64 start, end, len;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400834 int entries = 0;
835 int bitmaps = 0;
Josef Bacikc09544e2011-08-30 10:19:10 -0400836 int ret;
837 int err = -1;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400838
Josef Bacik0cb59c92010-07-02 12:14:14 -0400839 INIT_LIST_HEAD(&bitmap_list);
840
Li Zefan0414efa2011-04-20 10:20:14 +0800841 if (!i_size_read(inode))
842 return -1;
Josef Bacik2b209822010-12-03 13:17:53 -0500843
Josef Bacika67509c2011-10-05 15:18:58 -0400844 io_ctl_init(&io_ctl, inode, root);
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400845
Josef Bacik43be2142011-04-01 14:55:00 +0000846 /* Get the cluster for this block_group if it exists */
Li Zefan0414efa2011-04-20 10:20:14 +0800847 if (block_group && !list_empty(&block_group->cluster_list))
Josef Bacik43be2142011-04-01 14:55:00 +0000848 cluster = list_entry(block_group->cluster_list.next,
849 struct btrfs_free_cluster,
850 block_group_list);
851
852 /*
853 * We shouldn't have switched the pinned extents yet so this is the
854 * right one
855 */
856 unpin = root->fs_info->pinned_extents;
857
Josef Bacika67509c2011-10-05 15:18:58 -0400858 /* Lock all pages first so we can lock the extent safely. */
859 io_ctl_prepare_pages(&io_ctl, inode, 0);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400860
Josef Bacik0cb59c92010-07-02 12:14:14 -0400861 lock_extent_bits(&BTRFS_I(inode)->io_tree, 0, i_size_read(inode) - 1,
862 0, &cached_state, GFP_NOFS);
863
Josef Bacik43be2142011-04-01 14:55:00 +0000864 /*
865 * When searching for pinned extents, we need to start at our start
866 * offset.
867 */
Li Zefan0414efa2011-04-20 10:20:14 +0800868 if (block_group)
869 start = block_group->key.objectid;
Josef Bacik43be2142011-04-01 14:55:00 +0000870
Josef Bacikf75b1302011-10-05 10:00:18 -0400871 node = rb_first(&ctl->free_space_offset);
872 if (!node && cluster) {
873 node = rb_first(&cluster->root);
874 cluster = NULL;
875 }
876
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400877 /* Make sure we can fit our crcs into the first page */
878 if (io_ctl.check_crcs &&
879 (io_ctl.num_pages * sizeof(u32)) >= PAGE_CACHE_SIZE) {
880 WARN_ON(1);
881 goto out_nospc;
882 }
883
Josef Bacika67509c2011-10-05 15:18:58 -0400884 io_ctl_set_generation(&io_ctl, trans->transid);
885
Josef Bacik0cb59c92010-07-02 12:14:14 -0400886 /* Write out the extent entries */
Josef Bacika67509c2011-10-05 15:18:58 -0400887 while (node) {
888 struct btrfs_free_space *e;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400889
Josef Bacika67509c2011-10-05 15:18:58 -0400890 e = rb_entry(node, struct btrfs_free_space, offset_index);
891 entries++;
Josef Bacik43be2142011-04-01 14:55:00 +0000892
Josef Bacika67509c2011-10-05 15:18:58 -0400893 ret = io_ctl_add_entry(&io_ctl, e->offset, e->bytes,
894 e->bitmap);
895 if (ret)
896 goto out_nospc;
897
898 if (e->bitmap) {
899 list_add_tail(&e->list, &bitmap_list);
900 bitmaps++;
901 }
902 node = rb_next(node);
903 if (!node && cluster) {
904 node = rb_first(&cluster->root);
905 cluster = NULL;
906 }
907 }
908
909 /*
910 * We want to add any pinned extents to our free space cache
911 * so we don't leak the space
912 */
913 while (block_group && (start < block_group->key.objectid +
914 block_group->key.offset)) {
915 ret = find_first_extent_bit(unpin, start, &start, &end,
916 EXTENT_DIRTY);
917 if (ret) {
918 ret = 0;
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400919 break;
920 }
921
Josef Bacika67509c2011-10-05 15:18:58 -0400922 /* This pinned extent is out of our range */
923 if (start >= block_group->key.objectid +
924 block_group->key.offset)
925 break;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400926
Josef Bacika67509c2011-10-05 15:18:58 -0400927 len = block_group->key.objectid +
928 block_group->key.offset - start;
929 len = min(len, end + 1 - start);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400930
Josef Bacika67509c2011-10-05 15:18:58 -0400931 entries++;
932 ret = io_ctl_add_entry(&io_ctl, start, len, NULL);
933 if (ret)
934 goto out_nospc;
Josef Bacik2f356122011-06-10 15:31:13 -0400935
Josef Bacika67509c2011-10-05 15:18:58 -0400936 start = end + 1;
937 }
Josef Bacik0cb59c92010-07-02 12:14:14 -0400938
939 /* Write out the bitmaps */
940 list_for_each_safe(pos, n, &bitmap_list) {
Josef Bacik0cb59c92010-07-02 12:14:14 -0400941 struct btrfs_free_space *entry =
942 list_entry(pos, struct btrfs_free_space, list);
943
Josef Bacika67509c2011-10-05 15:18:58 -0400944 ret = io_ctl_add_bitmap(&io_ctl, entry->bitmap);
945 if (ret)
946 goto out_nospc;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400947 list_del_init(&entry->list);
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400948 }
949
Josef Bacik0cb59c92010-07-02 12:14:14 -0400950 /* Zero out the rest of the pages just to make sure */
Josef Bacika67509c2011-10-05 15:18:58 -0400951 io_ctl_zero_remaining_pages(&io_ctl);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400952
Josef Bacika67509c2011-10-05 15:18:58 -0400953 ret = btrfs_dirty_pages(root, inode, io_ctl.pages, io_ctl.num_pages,
954 0, i_size_read(inode), &cached_state);
955 io_ctl_drop_pages(&io_ctl);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400956 unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
957 i_size_read(inode) - 1, &cached_state, GFP_NOFS);
958
Josef Bacikc09544e2011-08-30 10:19:10 -0400959 if (ret)
Josef Bacik2f356122011-06-10 15:31:13 -0400960 goto out;
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400961
Josef Bacikbe1a12a2011-04-06 13:05:22 -0400962
Josef Bacik549b4fd2011-10-05 16:33:53 -0400963 ret = filemap_write_and_wait(inode->i_mapping);
964 if (ret)
965 goto out;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400966
967 key.objectid = BTRFS_FREE_SPACE_OBJECTID;
Li Zefan0414efa2011-04-20 10:20:14 +0800968 key.offset = offset;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400969 key.type = 0;
970
Josef Bacika9b5fcd2011-08-19 12:06:12 -0400971 ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
Josef Bacik0cb59c92010-07-02 12:14:14 -0400972 if (ret < 0) {
Josef Bacika67509c2011-10-05 15:18:58 -0400973 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0, inode->i_size - 1,
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400974 EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0, NULL,
975 GFP_NOFS);
Josef Bacik2f356122011-06-10 15:31:13 -0400976 goto out;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400977 }
978 leaf = path->nodes[0];
979 if (ret > 0) {
980 struct btrfs_key found_key;
981 BUG_ON(!path->slots[0]);
982 path->slots[0]--;
983 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
984 if (found_key.objectid != BTRFS_FREE_SPACE_OBJECTID ||
Li Zefan0414efa2011-04-20 10:20:14 +0800985 found_key.offset != offset) {
Josef Bacika67509c2011-10-05 15:18:58 -0400986 clear_extent_bit(&BTRFS_I(inode)->io_tree, 0,
987 inode->i_size - 1,
Josef Bacik5b0e95b2011-10-06 08:58:24 -0400988 EXTENT_DIRTY | EXTENT_DELALLOC, 0, 0,
989 NULL, GFP_NOFS);
David Sterbab3b4aa72011-04-21 01:20:15 +0200990 btrfs_release_path(path);
Josef Bacik2f356122011-06-10 15:31:13 -0400991 goto out;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400992 }
993 }
Josef Bacik549b4fd2011-10-05 16:33:53 -0400994
995 BTRFS_I(inode)->generation = trans->transid;
Josef Bacik0cb59c92010-07-02 12:14:14 -0400996 header = btrfs_item_ptr(leaf, path->slots[0],
997 struct btrfs_free_space_header);
998 btrfs_set_free_space_entries(leaf, header, entries);
999 btrfs_set_free_space_bitmaps(leaf, header, bitmaps);
1000 btrfs_set_free_space_generation(leaf, header, trans->transid);
1001 btrfs_mark_buffer_dirty(leaf);
David Sterbab3b4aa72011-04-21 01:20:15 +02001002 btrfs_release_path(path);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001003
Josef Bacikc09544e2011-08-30 10:19:10 -04001004 err = 0;
Josef Bacik2f356122011-06-10 15:31:13 -04001005out:
Josef Bacika67509c2011-10-05 15:18:58 -04001006 io_ctl_free(&io_ctl);
Josef Bacikc09544e2011-08-30 10:19:10 -04001007 if (err) {
Josef Bacika67509c2011-10-05 15:18:58 -04001008 invalidate_inode_pages2(inode->i_mapping);
Josef Bacik0cb59c92010-07-02 12:14:14 -04001009 BTRFS_I(inode)->generation = 0;
1010 }
Josef Bacik0cb59c92010-07-02 12:14:14 -04001011 btrfs_update_inode(trans, root, inode);
Josef Bacikc09544e2011-08-30 10:19:10 -04001012 return err;
Josef Bacika67509c2011-10-05 15:18:58 -04001013
1014out_nospc:
1015 list_for_each_safe(pos, n, &bitmap_list) {
1016 struct btrfs_free_space *entry =
1017 list_entry(pos, struct btrfs_free_space, list);
1018 list_del_init(&entry->list);
1019 }
1020 io_ctl_drop_pages(&io_ctl);
1021 unlock_extent_cached(&BTRFS_I(inode)->io_tree, 0,
1022 i_size_read(inode) - 1, &cached_state, GFP_NOFS);
1023 goto out;
Li Zefan0414efa2011-04-20 10:20:14 +08001024}
1025
1026int btrfs_write_out_cache(struct btrfs_root *root,
1027 struct btrfs_trans_handle *trans,
1028 struct btrfs_block_group_cache *block_group,
1029 struct btrfs_path *path)
1030{
1031 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
1032 struct inode *inode;
1033 int ret = 0;
1034
1035 root = root->fs_info->tree_root;
1036
1037 spin_lock(&block_group->lock);
1038 if (block_group->disk_cache_state < BTRFS_DC_SETUP) {
1039 spin_unlock(&block_group->lock);
1040 return 0;
1041 }
1042 spin_unlock(&block_group->lock);
1043
1044 inode = lookup_free_space_inode(root, block_group, path);
1045 if (IS_ERR(inode))
1046 return 0;
1047
1048 ret = __btrfs_write_out_cache(root, inode, ctl, block_group, trans,
1049 path, block_group->key.objectid);
Josef Bacikc09544e2011-08-30 10:19:10 -04001050 if (ret) {
Li Zefan0414efa2011-04-20 10:20:14 +08001051 spin_lock(&block_group->lock);
1052 block_group->disk_cache_state = BTRFS_DC_ERROR;
1053 spin_unlock(&block_group->lock);
Li Zefan82d59022011-04-20 10:33:24 +08001054 ret = 0;
Josef Bacikc09544e2011-08-30 10:19:10 -04001055#ifdef DEBUG
Li Zefan0414efa2011-04-20 10:20:14 +08001056 printk(KERN_ERR "btrfs: failed to write free space cace "
1057 "for block group %llu\n", block_group->key.objectid);
Josef Bacikc09544e2011-08-30 10:19:10 -04001058#endif
Li Zefan0414efa2011-04-20 10:20:14 +08001059 }
1060
Josef Bacik0cb59c92010-07-02 12:14:14 -04001061 iput(inode);
1062 return ret;
1063}
1064
Li Zefan34d52cb2011-03-29 13:46:06 +08001065static inline unsigned long offset_to_bit(u64 bitmap_start, u32 unit,
Josef Bacik96303082009-07-13 21:29:25 -04001066 u64 offset)
1067{
1068 BUG_ON(offset < bitmap_start);
1069 offset -= bitmap_start;
Li Zefan34d52cb2011-03-29 13:46:06 +08001070 return (unsigned long)(div_u64(offset, unit));
Josef Bacik96303082009-07-13 21:29:25 -04001071}
1072
Li Zefan34d52cb2011-03-29 13:46:06 +08001073static inline unsigned long bytes_to_bits(u64 bytes, u32 unit)
Josef Bacik96303082009-07-13 21:29:25 -04001074{
Li Zefan34d52cb2011-03-29 13:46:06 +08001075 return (unsigned long)(div_u64(bytes, unit));
Josef Bacik96303082009-07-13 21:29:25 -04001076}
1077
Li Zefan34d52cb2011-03-29 13:46:06 +08001078static inline u64 offset_to_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001079 u64 offset)
1080{
1081 u64 bitmap_start;
1082 u64 bytes_per_bitmap;
1083
Li Zefan34d52cb2011-03-29 13:46:06 +08001084 bytes_per_bitmap = BITS_PER_BITMAP * ctl->unit;
1085 bitmap_start = offset - ctl->start;
Josef Bacik96303082009-07-13 21:29:25 -04001086 bitmap_start = div64_u64(bitmap_start, bytes_per_bitmap);
1087 bitmap_start *= bytes_per_bitmap;
Li Zefan34d52cb2011-03-29 13:46:06 +08001088 bitmap_start += ctl->start;
Josef Bacik96303082009-07-13 21:29:25 -04001089
1090 return bitmap_start;
1091}
Josef Bacik0f9dd462008-09-23 13:14:11 -04001092
1093static int tree_insert_offset(struct rb_root *root, u64 offset,
Josef Bacik96303082009-07-13 21:29:25 -04001094 struct rb_node *node, int bitmap)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001095{
1096 struct rb_node **p = &root->rb_node;
1097 struct rb_node *parent = NULL;
1098 struct btrfs_free_space *info;
1099
1100 while (*p) {
1101 parent = *p;
1102 info = rb_entry(parent, struct btrfs_free_space, offset_index);
1103
Josef Bacik96303082009-07-13 21:29:25 -04001104 if (offset < info->offset) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001105 p = &(*p)->rb_left;
Josef Bacik96303082009-07-13 21:29:25 -04001106 } else if (offset > info->offset) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001107 p = &(*p)->rb_right;
Josef Bacik96303082009-07-13 21:29:25 -04001108 } else {
1109 /*
1110 * we could have a bitmap entry and an extent entry
1111 * share the same offset. If this is the case, we want
1112 * the extent entry to always be found first if we do a
1113 * linear search through the tree, since we want to have
1114 * the quickest allocation time, and allocating from an
1115 * extent is faster than allocating from a bitmap. So
1116 * if we're inserting a bitmap and we find an entry at
1117 * this offset, we want to go right, or after this entry
1118 * logically. If we are inserting an extent and we've
1119 * found a bitmap, we want to go left, or before
1120 * logically.
1121 */
1122 if (bitmap) {
Josef Bacik207dde82011-05-13 14:49:23 -04001123 if (info->bitmap) {
1124 WARN_ON_ONCE(1);
1125 return -EEXIST;
1126 }
Josef Bacik96303082009-07-13 21:29:25 -04001127 p = &(*p)->rb_right;
1128 } else {
Josef Bacik207dde82011-05-13 14:49:23 -04001129 if (!info->bitmap) {
1130 WARN_ON_ONCE(1);
1131 return -EEXIST;
1132 }
Josef Bacik96303082009-07-13 21:29:25 -04001133 p = &(*p)->rb_left;
1134 }
1135 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04001136 }
1137
1138 rb_link_node(node, parent, p);
1139 rb_insert_color(node, root);
1140
1141 return 0;
1142}
1143
1144/*
Josef Bacik70cb0742009-04-03 10:14:19 -04001145 * searches the tree for the given offset.
1146 *
Josef Bacik96303082009-07-13 21:29:25 -04001147 * fuzzy - If this is set, then we are trying to make an allocation, and we just
1148 * want a section that has at least bytes size and comes at or after the given
1149 * offset.
Josef Bacik0f9dd462008-09-23 13:14:11 -04001150 */
Josef Bacik96303082009-07-13 21:29:25 -04001151static struct btrfs_free_space *
Li Zefan34d52cb2011-03-29 13:46:06 +08001152tree_search_offset(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001153 u64 offset, int bitmap_only, int fuzzy)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001154{
Li Zefan34d52cb2011-03-29 13:46:06 +08001155 struct rb_node *n = ctl->free_space_offset.rb_node;
Josef Bacik96303082009-07-13 21:29:25 -04001156 struct btrfs_free_space *entry, *prev = NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001157
Josef Bacik96303082009-07-13 21:29:25 -04001158 /* find entry that is closest to the 'offset' */
1159 while (1) {
1160 if (!n) {
1161 entry = NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001162 break;
1163 }
Josef Bacik96303082009-07-13 21:29:25 -04001164
1165 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1166 prev = entry;
1167
1168 if (offset < entry->offset)
1169 n = n->rb_left;
1170 else if (offset > entry->offset)
1171 n = n->rb_right;
1172 else
1173 break;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001174 }
1175
Josef Bacik96303082009-07-13 21:29:25 -04001176 if (bitmap_only) {
1177 if (!entry)
1178 return NULL;
1179 if (entry->bitmap)
1180 return entry;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001181
Josef Bacik96303082009-07-13 21:29:25 -04001182 /*
1183 * bitmap entry and extent entry may share same offset,
1184 * in that case, bitmap entry comes after extent entry.
1185 */
1186 n = rb_next(n);
1187 if (!n)
1188 return NULL;
1189 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1190 if (entry->offset != offset)
1191 return NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001192
Josef Bacik96303082009-07-13 21:29:25 -04001193 WARN_ON(!entry->bitmap);
1194 return entry;
1195 } else if (entry) {
1196 if (entry->bitmap) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001197 /*
Josef Bacik96303082009-07-13 21:29:25 -04001198 * if previous extent entry covers the offset,
1199 * we should return it instead of the bitmap entry
Josef Bacik0f9dd462008-09-23 13:14:11 -04001200 */
Josef Bacik96303082009-07-13 21:29:25 -04001201 n = &entry->offset_index;
1202 while (1) {
1203 n = rb_prev(n);
1204 if (!n)
1205 break;
1206 prev = rb_entry(n, struct btrfs_free_space,
1207 offset_index);
1208 if (!prev->bitmap) {
1209 if (prev->offset + prev->bytes > offset)
1210 entry = prev;
1211 break;
1212 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04001213 }
Josef Bacik96303082009-07-13 21:29:25 -04001214 }
1215 return entry;
1216 }
1217
1218 if (!prev)
1219 return NULL;
1220
1221 /* find last entry before the 'offset' */
1222 entry = prev;
1223 if (entry->offset > offset) {
1224 n = rb_prev(&entry->offset_index);
1225 if (n) {
1226 entry = rb_entry(n, struct btrfs_free_space,
1227 offset_index);
1228 BUG_ON(entry->offset > offset);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001229 } else {
Josef Bacik96303082009-07-13 21:29:25 -04001230 if (fuzzy)
1231 return entry;
1232 else
1233 return NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001234 }
1235 }
1236
Josef Bacik96303082009-07-13 21:29:25 -04001237 if (entry->bitmap) {
1238 n = &entry->offset_index;
1239 while (1) {
1240 n = rb_prev(n);
1241 if (!n)
1242 break;
1243 prev = rb_entry(n, struct btrfs_free_space,
1244 offset_index);
1245 if (!prev->bitmap) {
1246 if (prev->offset + prev->bytes > offset)
1247 return prev;
1248 break;
1249 }
1250 }
Li Zefan34d52cb2011-03-29 13:46:06 +08001251 if (entry->offset + BITS_PER_BITMAP * ctl->unit > offset)
Josef Bacik96303082009-07-13 21:29:25 -04001252 return entry;
1253 } else if (entry->offset + entry->bytes > offset)
1254 return entry;
1255
1256 if (!fuzzy)
1257 return NULL;
1258
1259 while (1) {
1260 if (entry->bitmap) {
1261 if (entry->offset + BITS_PER_BITMAP *
Li Zefan34d52cb2011-03-29 13:46:06 +08001262 ctl->unit > offset)
Josef Bacik96303082009-07-13 21:29:25 -04001263 break;
1264 } else {
1265 if (entry->offset + entry->bytes > offset)
1266 break;
1267 }
1268
1269 n = rb_next(&entry->offset_index);
1270 if (!n)
1271 return NULL;
1272 entry = rb_entry(n, struct btrfs_free_space, offset_index);
1273 }
1274 return entry;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001275}
1276
Li Zefanf333adb2010-11-09 14:57:39 +08001277static inline void
Li Zefan34d52cb2011-03-29 13:46:06 +08001278__unlink_free_space(struct btrfs_free_space_ctl *ctl,
Li Zefanf333adb2010-11-09 14:57:39 +08001279 struct btrfs_free_space *info)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001280{
Li Zefan34d52cb2011-03-29 13:46:06 +08001281 rb_erase(&info->offset_index, &ctl->free_space_offset);
1282 ctl->free_extents--;
Li Zefanf333adb2010-11-09 14:57:39 +08001283}
1284
Li Zefan34d52cb2011-03-29 13:46:06 +08001285static void unlink_free_space(struct btrfs_free_space_ctl *ctl,
Li Zefanf333adb2010-11-09 14:57:39 +08001286 struct btrfs_free_space *info)
1287{
Li Zefan34d52cb2011-03-29 13:46:06 +08001288 __unlink_free_space(ctl, info);
1289 ctl->free_space -= info->bytes;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001290}
1291
Li Zefan34d52cb2011-03-29 13:46:06 +08001292static int link_free_space(struct btrfs_free_space_ctl *ctl,
Josef Bacik0f9dd462008-09-23 13:14:11 -04001293 struct btrfs_free_space *info)
1294{
1295 int ret = 0;
1296
Josef Bacik96303082009-07-13 21:29:25 -04001297 BUG_ON(!info->bitmap && !info->bytes);
Li Zefan34d52cb2011-03-29 13:46:06 +08001298 ret = tree_insert_offset(&ctl->free_space_offset, info->offset,
Josef Bacik96303082009-07-13 21:29:25 -04001299 &info->offset_index, (info->bitmap != NULL));
Josef Bacik0f9dd462008-09-23 13:14:11 -04001300 if (ret)
1301 return ret;
1302
Li Zefan34d52cb2011-03-29 13:46:06 +08001303 ctl->free_space += info->bytes;
1304 ctl->free_extents++;
Josef Bacik96303082009-07-13 21:29:25 -04001305 return ret;
1306}
1307
Li Zefan34d52cb2011-03-29 13:46:06 +08001308static void recalculate_thresholds(struct btrfs_free_space_ctl *ctl)
Josef Bacik96303082009-07-13 21:29:25 -04001309{
Li Zefan34d52cb2011-03-29 13:46:06 +08001310 struct btrfs_block_group_cache *block_group = ctl->private;
Josef Bacik25891f72009-09-11 16:11:20 -04001311 u64 max_bytes;
1312 u64 bitmap_bytes;
1313 u64 extent_bytes;
Li Zefan8eb2d822010-11-09 14:48:01 +08001314 u64 size = block_group->key.offset;
Li Zefan34d52cb2011-03-29 13:46:06 +08001315 u64 bytes_per_bg = BITS_PER_BITMAP * block_group->sectorsize;
1316 int max_bitmaps = div64_u64(size + bytes_per_bg - 1, bytes_per_bg);
1317
1318 BUG_ON(ctl->total_bitmaps > max_bitmaps);
Josef Bacik96303082009-07-13 21:29:25 -04001319
1320 /*
1321 * The goal is to keep the total amount of memory used per 1gb of space
1322 * at or below 32k, so we need to adjust how much memory we allow to be
1323 * used by extent based free space tracking
1324 */
Li Zefan8eb2d822010-11-09 14:48:01 +08001325 if (size < 1024 * 1024 * 1024)
1326 max_bytes = MAX_CACHE_BYTES_PER_GIG;
1327 else
1328 max_bytes = MAX_CACHE_BYTES_PER_GIG *
1329 div64_u64(size, 1024 * 1024 * 1024);
Josef Bacik96303082009-07-13 21:29:25 -04001330
Josef Bacik25891f72009-09-11 16:11:20 -04001331 /*
1332 * we want to account for 1 more bitmap than what we have so we can make
1333 * sure we don't go over our overall goal of MAX_CACHE_BYTES_PER_GIG as
1334 * we add more bitmaps.
1335 */
Li Zefan34d52cb2011-03-29 13:46:06 +08001336 bitmap_bytes = (ctl->total_bitmaps + 1) * PAGE_CACHE_SIZE;
Josef Bacik96303082009-07-13 21:29:25 -04001337
Josef Bacik25891f72009-09-11 16:11:20 -04001338 if (bitmap_bytes >= max_bytes) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001339 ctl->extents_thresh = 0;
Josef Bacik25891f72009-09-11 16:11:20 -04001340 return;
Josef Bacik96303082009-07-13 21:29:25 -04001341 }
Josef Bacik25891f72009-09-11 16:11:20 -04001342
1343 /*
1344 * we want the extent entry threshold to always be at most 1/2 the maxw
1345 * bytes we can have, or whatever is less than that.
1346 */
1347 extent_bytes = max_bytes - bitmap_bytes;
1348 extent_bytes = min_t(u64, extent_bytes, div64_u64(max_bytes, 2));
1349
Li Zefan34d52cb2011-03-29 13:46:06 +08001350 ctl->extents_thresh =
Josef Bacik25891f72009-09-11 16:11:20 -04001351 div64_u64(extent_bytes, (sizeof(struct btrfs_free_space)));
Josef Bacik96303082009-07-13 21:29:25 -04001352}
1353
Miao Xiebb3ac5a2011-08-05 09:32:35 +00001354static inline void __bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
1355 struct btrfs_free_space *info,
1356 u64 offset, u64 bytes)
Josef Bacik96303082009-07-13 21:29:25 -04001357{
Li Zefanf38b6e72011-03-14 13:40:51 +08001358 unsigned long start, count;
Josef Bacik96303082009-07-13 21:29:25 -04001359
Li Zefan34d52cb2011-03-29 13:46:06 +08001360 start = offset_to_bit(info->offset, ctl->unit, offset);
1361 count = bytes_to_bits(bytes, ctl->unit);
Li Zefanf38b6e72011-03-14 13:40:51 +08001362 BUG_ON(start + count > BITS_PER_BITMAP);
Josef Bacik96303082009-07-13 21:29:25 -04001363
Li Zefanf38b6e72011-03-14 13:40:51 +08001364 bitmap_clear(info->bitmap, start, count);
Josef Bacik96303082009-07-13 21:29:25 -04001365
1366 info->bytes -= bytes;
Miao Xiebb3ac5a2011-08-05 09:32:35 +00001367}
1368
1369static void bitmap_clear_bits(struct btrfs_free_space_ctl *ctl,
1370 struct btrfs_free_space *info, u64 offset,
1371 u64 bytes)
1372{
1373 __bitmap_clear_bits(ctl, info, offset, bytes);
Li Zefan34d52cb2011-03-29 13:46:06 +08001374 ctl->free_space -= bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001375}
1376
Li Zefan34d52cb2011-03-29 13:46:06 +08001377static void bitmap_set_bits(struct btrfs_free_space_ctl *ctl,
Josef Bacik817d52f2009-07-13 21:29:25 -04001378 struct btrfs_free_space *info, u64 offset,
1379 u64 bytes)
Josef Bacik96303082009-07-13 21:29:25 -04001380{
Li Zefanf38b6e72011-03-14 13:40:51 +08001381 unsigned long start, count;
Josef Bacik96303082009-07-13 21:29:25 -04001382
Li Zefan34d52cb2011-03-29 13:46:06 +08001383 start = offset_to_bit(info->offset, ctl->unit, offset);
1384 count = bytes_to_bits(bytes, ctl->unit);
Li Zefanf38b6e72011-03-14 13:40:51 +08001385 BUG_ON(start + count > BITS_PER_BITMAP);
Josef Bacik96303082009-07-13 21:29:25 -04001386
Li Zefanf38b6e72011-03-14 13:40:51 +08001387 bitmap_set(info->bitmap, start, count);
Josef Bacik96303082009-07-13 21:29:25 -04001388
1389 info->bytes += bytes;
Li Zefan34d52cb2011-03-29 13:46:06 +08001390 ctl->free_space += bytes;
Josef Bacik96303082009-07-13 21:29:25 -04001391}
1392
Li Zefan34d52cb2011-03-29 13:46:06 +08001393static int search_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001394 struct btrfs_free_space *bitmap_info, u64 *offset,
1395 u64 *bytes)
1396{
1397 unsigned long found_bits = 0;
1398 unsigned long bits, i;
1399 unsigned long next_zero;
1400
Li Zefan34d52cb2011-03-29 13:46:06 +08001401 i = offset_to_bit(bitmap_info->offset, ctl->unit,
Josef Bacik96303082009-07-13 21:29:25 -04001402 max_t(u64, *offset, bitmap_info->offset));
Li Zefan34d52cb2011-03-29 13:46:06 +08001403 bits = bytes_to_bits(*bytes, ctl->unit);
Josef Bacik96303082009-07-13 21:29:25 -04001404
1405 for (i = find_next_bit(bitmap_info->bitmap, BITS_PER_BITMAP, i);
1406 i < BITS_PER_BITMAP;
1407 i = find_next_bit(bitmap_info->bitmap, BITS_PER_BITMAP, i + 1)) {
1408 next_zero = find_next_zero_bit(bitmap_info->bitmap,
1409 BITS_PER_BITMAP, i);
1410 if ((next_zero - i) >= bits) {
1411 found_bits = next_zero - i;
1412 break;
1413 }
1414 i = next_zero;
1415 }
1416
1417 if (found_bits) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001418 *offset = (u64)(i * ctl->unit) + bitmap_info->offset;
1419 *bytes = (u64)(found_bits) * ctl->unit;
Josef Bacik96303082009-07-13 21:29:25 -04001420 return 0;
1421 }
1422
1423 return -1;
1424}
1425
Li Zefan34d52cb2011-03-29 13:46:06 +08001426static struct btrfs_free_space *
1427find_free_space(struct btrfs_free_space_ctl *ctl, u64 *offset, u64 *bytes)
Josef Bacik96303082009-07-13 21:29:25 -04001428{
1429 struct btrfs_free_space *entry;
1430 struct rb_node *node;
1431 int ret;
1432
Li Zefan34d52cb2011-03-29 13:46:06 +08001433 if (!ctl->free_space_offset.rb_node)
Josef Bacik96303082009-07-13 21:29:25 -04001434 return NULL;
1435
Li Zefan34d52cb2011-03-29 13:46:06 +08001436 entry = tree_search_offset(ctl, offset_to_bitmap(ctl, *offset), 0, 1);
Josef Bacik96303082009-07-13 21:29:25 -04001437 if (!entry)
1438 return NULL;
1439
1440 for (node = &entry->offset_index; node; node = rb_next(node)) {
1441 entry = rb_entry(node, struct btrfs_free_space, offset_index);
1442 if (entry->bytes < *bytes)
1443 continue;
1444
1445 if (entry->bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001446 ret = search_bitmap(ctl, entry, offset, bytes);
Josef Bacik96303082009-07-13 21:29:25 -04001447 if (!ret)
1448 return entry;
1449 continue;
1450 }
1451
1452 *offset = entry->offset;
1453 *bytes = entry->bytes;
1454 return entry;
1455 }
1456
1457 return NULL;
1458}
1459
Li Zefan34d52cb2011-03-29 13:46:06 +08001460static void add_new_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001461 struct btrfs_free_space *info, u64 offset)
1462{
Li Zefan34d52cb2011-03-29 13:46:06 +08001463 info->offset = offset_to_bitmap(ctl, offset);
Josef Bacikf019f422009-09-11 16:11:20 -04001464 info->bytes = 0;
Li Zefan34d52cb2011-03-29 13:46:06 +08001465 link_free_space(ctl, info);
1466 ctl->total_bitmaps++;
Josef Bacik96303082009-07-13 21:29:25 -04001467
Li Zefan34d52cb2011-03-29 13:46:06 +08001468 ctl->op->recalc_thresholds(ctl);
Josef Bacik96303082009-07-13 21:29:25 -04001469}
1470
Li Zefan34d52cb2011-03-29 13:46:06 +08001471static void free_bitmap(struct btrfs_free_space_ctl *ctl,
Li Zefanedf6e2d2010-11-09 14:50:07 +08001472 struct btrfs_free_space *bitmap_info)
1473{
Li Zefan34d52cb2011-03-29 13:46:06 +08001474 unlink_free_space(ctl, bitmap_info);
Li Zefanedf6e2d2010-11-09 14:50:07 +08001475 kfree(bitmap_info->bitmap);
Josef Bacikdc89e982011-01-28 17:05:48 -05001476 kmem_cache_free(btrfs_free_space_cachep, bitmap_info);
Li Zefan34d52cb2011-03-29 13:46:06 +08001477 ctl->total_bitmaps--;
1478 ctl->op->recalc_thresholds(ctl);
Li Zefanedf6e2d2010-11-09 14:50:07 +08001479}
1480
Li Zefan34d52cb2011-03-29 13:46:06 +08001481static noinline int remove_from_bitmap(struct btrfs_free_space_ctl *ctl,
Josef Bacik96303082009-07-13 21:29:25 -04001482 struct btrfs_free_space *bitmap_info,
1483 u64 *offset, u64 *bytes)
1484{
1485 u64 end;
Josef Bacik6606bb92009-07-31 11:03:58 -04001486 u64 search_start, search_bytes;
1487 int ret;
Josef Bacik96303082009-07-13 21:29:25 -04001488
1489again:
Li Zefan34d52cb2011-03-29 13:46:06 +08001490 end = bitmap_info->offset + (u64)(BITS_PER_BITMAP * ctl->unit) - 1;
Josef Bacik96303082009-07-13 21:29:25 -04001491
Josef Bacik6606bb92009-07-31 11:03:58 -04001492 /*
1493 * XXX - this can go away after a few releases.
1494 *
1495 * since the only user of btrfs_remove_free_space is the tree logging
1496 * stuff, and the only way to test that is under crash conditions, we
1497 * want to have this debug stuff here just in case somethings not
1498 * working. Search the bitmap for the space we are trying to use to
1499 * make sure its actually there. If its not there then we need to stop
1500 * because something has gone wrong.
1501 */
1502 search_start = *offset;
1503 search_bytes = *bytes;
Josef Bacik13dbc082011-02-03 02:39:52 +00001504 search_bytes = min(search_bytes, end - search_start + 1);
Li Zefan34d52cb2011-03-29 13:46:06 +08001505 ret = search_bitmap(ctl, bitmap_info, &search_start, &search_bytes);
Josef Bacik6606bb92009-07-31 11:03:58 -04001506 BUG_ON(ret < 0 || search_start != *offset);
1507
Josef Bacik96303082009-07-13 21:29:25 -04001508 if (*offset > bitmap_info->offset && *offset + *bytes > end) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001509 bitmap_clear_bits(ctl, bitmap_info, *offset, end - *offset + 1);
Josef Bacik96303082009-07-13 21:29:25 -04001510 *bytes -= end - *offset + 1;
1511 *offset = end + 1;
1512 } else if (*offset >= bitmap_info->offset && *offset + *bytes <= end) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001513 bitmap_clear_bits(ctl, bitmap_info, *offset, *bytes);
Josef Bacik96303082009-07-13 21:29:25 -04001514 *bytes = 0;
1515 }
1516
1517 if (*bytes) {
Josef Bacik6606bb92009-07-31 11:03:58 -04001518 struct rb_node *next = rb_next(&bitmap_info->offset_index);
Li Zefanedf6e2d2010-11-09 14:50:07 +08001519 if (!bitmap_info->bytes)
Li Zefan34d52cb2011-03-29 13:46:06 +08001520 free_bitmap(ctl, bitmap_info);
Josef Bacik96303082009-07-13 21:29:25 -04001521
Josef Bacik6606bb92009-07-31 11:03:58 -04001522 /*
1523 * no entry after this bitmap, but we still have bytes to
1524 * remove, so something has gone wrong.
1525 */
1526 if (!next)
Josef Bacik96303082009-07-13 21:29:25 -04001527 return -EINVAL;
1528
Josef Bacik6606bb92009-07-31 11:03:58 -04001529 bitmap_info = rb_entry(next, struct btrfs_free_space,
1530 offset_index);
1531
1532 /*
1533 * if the next entry isn't a bitmap we need to return to let the
1534 * extent stuff do its work.
1535 */
Josef Bacik96303082009-07-13 21:29:25 -04001536 if (!bitmap_info->bitmap)
1537 return -EAGAIN;
1538
Josef Bacik6606bb92009-07-31 11:03:58 -04001539 /*
1540 * Ok the next item is a bitmap, but it may not actually hold
1541 * the information for the rest of this free space stuff, so
1542 * look for it, and if we don't find it return so we can try
1543 * everything over again.
1544 */
1545 search_start = *offset;
1546 search_bytes = *bytes;
Li Zefan34d52cb2011-03-29 13:46:06 +08001547 ret = search_bitmap(ctl, bitmap_info, &search_start,
Josef Bacik6606bb92009-07-31 11:03:58 -04001548 &search_bytes);
1549 if (ret < 0 || search_start != *offset)
1550 return -EAGAIN;
1551
Josef Bacik96303082009-07-13 21:29:25 -04001552 goto again;
Li Zefanedf6e2d2010-11-09 14:50:07 +08001553 } else if (!bitmap_info->bytes)
Li Zefan34d52cb2011-03-29 13:46:06 +08001554 free_bitmap(ctl, bitmap_info);
Josef Bacik96303082009-07-13 21:29:25 -04001555
1556 return 0;
1557}
1558
Josef Bacik2cdc3422011-05-27 14:07:49 -04001559static u64 add_bytes_to_bitmap(struct btrfs_free_space_ctl *ctl,
1560 struct btrfs_free_space *info, u64 offset,
1561 u64 bytes)
1562{
1563 u64 bytes_to_set = 0;
1564 u64 end;
1565
1566 end = info->offset + (u64)(BITS_PER_BITMAP * ctl->unit);
1567
1568 bytes_to_set = min(end - offset, bytes);
1569
1570 bitmap_set_bits(ctl, info, offset, bytes_to_set);
1571
1572 return bytes_to_set;
1573
1574}
1575
Li Zefan34d52cb2011-03-29 13:46:06 +08001576static bool use_bitmap(struct btrfs_free_space_ctl *ctl,
1577 struct btrfs_free_space *info)
Josef Bacik96303082009-07-13 21:29:25 -04001578{
Li Zefan34d52cb2011-03-29 13:46:06 +08001579 struct btrfs_block_group_cache *block_group = ctl->private;
Josef Bacik96303082009-07-13 21:29:25 -04001580
1581 /*
1582 * If we are below the extents threshold then we can add this as an
1583 * extent, and don't have to deal with the bitmap
1584 */
Li Zefan34d52cb2011-03-29 13:46:06 +08001585 if (ctl->free_extents < ctl->extents_thresh) {
Josef Bacik32cb0842011-03-18 16:16:21 -04001586 /*
1587 * If this block group has some small extents we don't want to
1588 * use up all of our free slots in the cache with them, we want
1589 * to reserve them to larger extents, however if we have plent
1590 * of cache left then go ahead an dadd them, no sense in adding
1591 * the overhead of a bitmap if we don't have to.
1592 */
1593 if (info->bytes <= block_group->sectorsize * 4) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001594 if (ctl->free_extents * 2 <= ctl->extents_thresh)
1595 return false;
Josef Bacik32cb0842011-03-18 16:16:21 -04001596 } else {
Li Zefan34d52cb2011-03-29 13:46:06 +08001597 return false;
Josef Bacik32cb0842011-03-18 16:16:21 -04001598 }
1599 }
Josef Bacik96303082009-07-13 21:29:25 -04001600
1601 /*
1602 * some block groups are so tiny they can't be enveloped by a bitmap, so
1603 * don't even bother to create a bitmap for this
1604 */
1605 if (BITS_PER_BITMAP * block_group->sectorsize >
1606 block_group->key.offset)
Li Zefan34d52cb2011-03-29 13:46:06 +08001607 return false;
1608
1609 return true;
1610}
1611
Josef Bacik2cdc3422011-05-27 14:07:49 -04001612static struct btrfs_free_space_op free_space_op = {
1613 .recalc_thresholds = recalculate_thresholds,
1614 .use_bitmap = use_bitmap,
1615};
1616
Li Zefan34d52cb2011-03-29 13:46:06 +08001617static int insert_into_bitmap(struct btrfs_free_space_ctl *ctl,
1618 struct btrfs_free_space *info)
1619{
1620 struct btrfs_free_space *bitmap_info;
Josef Bacik2cdc3422011-05-27 14:07:49 -04001621 struct btrfs_block_group_cache *block_group = NULL;
Li Zefan34d52cb2011-03-29 13:46:06 +08001622 int added = 0;
Josef Bacik2cdc3422011-05-27 14:07:49 -04001623 u64 bytes, offset, bytes_added;
Li Zefan34d52cb2011-03-29 13:46:06 +08001624 int ret;
Josef Bacik96303082009-07-13 21:29:25 -04001625
1626 bytes = info->bytes;
1627 offset = info->offset;
1628
Li Zefan34d52cb2011-03-29 13:46:06 +08001629 if (!ctl->op->use_bitmap(ctl, info))
1630 return 0;
1631
Josef Bacik2cdc3422011-05-27 14:07:49 -04001632 if (ctl->op == &free_space_op)
1633 block_group = ctl->private;
Chris Mason38e87882011-06-10 16:36:57 -04001634again:
Josef Bacik2cdc3422011-05-27 14:07:49 -04001635 /*
1636 * Since we link bitmaps right into the cluster we need to see if we
1637 * have a cluster here, and if so and it has our bitmap we need to add
1638 * the free space to that bitmap.
1639 */
1640 if (block_group && !list_empty(&block_group->cluster_list)) {
1641 struct btrfs_free_cluster *cluster;
1642 struct rb_node *node;
1643 struct btrfs_free_space *entry;
1644
1645 cluster = list_entry(block_group->cluster_list.next,
1646 struct btrfs_free_cluster,
1647 block_group_list);
1648 spin_lock(&cluster->lock);
1649 node = rb_first(&cluster->root);
1650 if (!node) {
1651 spin_unlock(&cluster->lock);
Chris Mason38e87882011-06-10 16:36:57 -04001652 goto no_cluster_bitmap;
Josef Bacik2cdc3422011-05-27 14:07:49 -04001653 }
1654
1655 entry = rb_entry(node, struct btrfs_free_space, offset_index);
1656 if (!entry->bitmap) {
1657 spin_unlock(&cluster->lock);
Chris Mason38e87882011-06-10 16:36:57 -04001658 goto no_cluster_bitmap;
Josef Bacik2cdc3422011-05-27 14:07:49 -04001659 }
1660
1661 if (entry->offset == offset_to_bitmap(ctl, offset)) {
1662 bytes_added = add_bytes_to_bitmap(ctl, entry,
1663 offset, bytes);
1664 bytes -= bytes_added;
1665 offset += bytes_added;
1666 }
1667 spin_unlock(&cluster->lock);
1668 if (!bytes) {
1669 ret = 1;
1670 goto out;
1671 }
1672 }
Chris Mason38e87882011-06-10 16:36:57 -04001673
1674no_cluster_bitmap:
Li Zefan34d52cb2011-03-29 13:46:06 +08001675 bitmap_info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
Josef Bacik96303082009-07-13 21:29:25 -04001676 1, 0);
1677 if (!bitmap_info) {
1678 BUG_ON(added);
1679 goto new_bitmap;
1680 }
1681
Josef Bacik2cdc3422011-05-27 14:07:49 -04001682 bytes_added = add_bytes_to_bitmap(ctl, bitmap_info, offset, bytes);
1683 bytes -= bytes_added;
1684 offset += bytes_added;
1685 added = 0;
Josef Bacik96303082009-07-13 21:29:25 -04001686
1687 if (!bytes) {
1688 ret = 1;
1689 goto out;
1690 } else
1691 goto again;
1692
1693new_bitmap:
1694 if (info && info->bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001695 add_new_bitmap(ctl, info, offset);
Josef Bacik96303082009-07-13 21:29:25 -04001696 added = 1;
1697 info = NULL;
1698 goto again;
1699 } else {
Li Zefan34d52cb2011-03-29 13:46:06 +08001700 spin_unlock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04001701
1702 /* no pre-allocated info, allocate a new one */
1703 if (!info) {
Josef Bacikdc89e982011-01-28 17:05:48 -05001704 info = kmem_cache_zalloc(btrfs_free_space_cachep,
1705 GFP_NOFS);
Josef Bacik96303082009-07-13 21:29:25 -04001706 if (!info) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001707 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04001708 ret = -ENOMEM;
1709 goto out;
1710 }
1711 }
1712
1713 /* allocate the bitmap */
1714 info->bitmap = kzalloc(PAGE_CACHE_SIZE, GFP_NOFS);
Li Zefan34d52cb2011-03-29 13:46:06 +08001715 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04001716 if (!info->bitmap) {
1717 ret = -ENOMEM;
1718 goto out;
1719 }
1720 goto again;
1721 }
1722
1723out:
1724 if (info) {
1725 if (info->bitmap)
1726 kfree(info->bitmap);
Josef Bacikdc89e982011-01-28 17:05:48 -05001727 kmem_cache_free(btrfs_free_space_cachep, info);
Josef Bacik96303082009-07-13 21:29:25 -04001728 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04001729
1730 return ret;
1731}
1732
Chris Mason945d8962011-05-22 12:33:42 -04001733static bool try_merge_free_space(struct btrfs_free_space_ctl *ctl,
Li Zefanf333adb2010-11-09 14:57:39 +08001734 struct btrfs_free_space *info, bool update_stat)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001735{
Li Zefan120d66e2010-11-09 14:56:50 +08001736 struct btrfs_free_space *left_info;
1737 struct btrfs_free_space *right_info;
1738 bool merged = false;
1739 u64 offset = info->offset;
1740 u64 bytes = info->bytes;
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001741
Josef Bacik0f9dd462008-09-23 13:14:11 -04001742 /*
1743 * first we want to see if there is free space adjacent to the range we
1744 * are adding, if there is remove that struct and add a new one to
1745 * cover the entire range
1746 */
Li Zefan34d52cb2011-03-29 13:46:06 +08001747 right_info = tree_search_offset(ctl, offset + bytes, 0, 0);
Josef Bacik96303082009-07-13 21:29:25 -04001748 if (right_info && rb_prev(&right_info->offset_index))
1749 left_info = rb_entry(rb_prev(&right_info->offset_index),
1750 struct btrfs_free_space, offset_index);
1751 else
Li Zefan34d52cb2011-03-29 13:46:06 +08001752 left_info = tree_search_offset(ctl, offset - 1, 0, 0);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001753
Josef Bacik96303082009-07-13 21:29:25 -04001754 if (right_info && !right_info->bitmap) {
Li Zefanf333adb2010-11-09 14:57:39 +08001755 if (update_stat)
Li Zefan34d52cb2011-03-29 13:46:06 +08001756 unlink_free_space(ctl, right_info);
Li Zefanf333adb2010-11-09 14:57:39 +08001757 else
Li Zefan34d52cb2011-03-29 13:46:06 +08001758 __unlink_free_space(ctl, right_info);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001759 info->bytes += right_info->bytes;
Josef Bacikdc89e982011-01-28 17:05:48 -05001760 kmem_cache_free(btrfs_free_space_cachep, right_info);
Li Zefan120d66e2010-11-09 14:56:50 +08001761 merged = true;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001762 }
1763
Josef Bacik96303082009-07-13 21:29:25 -04001764 if (left_info && !left_info->bitmap &&
1765 left_info->offset + left_info->bytes == offset) {
Li Zefanf333adb2010-11-09 14:57:39 +08001766 if (update_stat)
Li Zefan34d52cb2011-03-29 13:46:06 +08001767 unlink_free_space(ctl, left_info);
Li Zefanf333adb2010-11-09 14:57:39 +08001768 else
Li Zefan34d52cb2011-03-29 13:46:06 +08001769 __unlink_free_space(ctl, left_info);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001770 info->offset = left_info->offset;
1771 info->bytes += left_info->bytes;
Josef Bacikdc89e982011-01-28 17:05:48 -05001772 kmem_cache_free(btrfs_free_space_cachep, left_info);
Li Zefan120d66e2010-11-09 14:56:50 +08001773 merged = true;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001774 }
1775
Li Zefan120d66e2010-11-09 14:56:50 +08001776 return merged;
1777}
1778
Li Zefan581bb052011-04-20 10:06:11 +08001779int __btrfs_add_free_space(struct btrfs_free_space_ctl *ctl,
1780 u64 offset, u64 bytes)
Li Zefan120d66e2010-11-09 14:56:50 +08001781{
1782 struct btrfs_free_space *info;
1783 int ret = 0;
1784
Josef Bacikdc89e982011-01-28 17:05:48 -05001785 info = kmem_cache_zalloc(btrfs_free_space_cachep, GFP_NOFS);
Li Zefan120d66e2010-11-09 14:56:50 +08001786 if (!info)
1787 return -ENOMEM;
1788
1789 info->offset = offset;
1790 info->bytes = bytes;
1791
Li Zefan34d52cb2011-03-29 13:46:06 +08001792 spin_lock(&ctl->tree_lock);
Li Zefan120d66e2010-11-09 14:56:50 +08001793
Li Zefan34d52cb2011-03-29 13:46:06 +08001794 if (try_merge_free_space(ctl, info, true))
Li Zefan120d66e2010-11-09 14:56:50 +08001795 goto link;
1796
1797 /*
1798 * There was no extent directly to the left or right of this new
1799 * extent then we know we're going to have to allocate a new extent, so
1800 * before we do that see if we need to drop this into a bitmap
1801 */
Li Zefan34d52cb2011-03-29 13:46:06 +08001802 ret = insert_into_bitmap(ctl, info);
Li Zefan120d66e2010-11-09 14:56:50 +08001803 if (ret < 0) {
1804 goto out;
1805 } else if (ret) {
1806 ret = 0;
1807 goto out;
1808 }
1809link:
Li Zefan34d52cb2011-03-29 13:46:06 +08001810 ret = link_free_space(ctl, info);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001811 if (ret)
Josef Bacikdc89e982011-01-28 17:05:48 -05001812 kmem_cache_free(btrfs_free_space_cachep, info);
Josef Bacik96303082009-07-13 21:29:25 -04001813out:
Li Zefan34d52cb2011-03-29 13:46:06 +08001814 spin_unlock(&ctl->tree_lock);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001815
Josef Bacik0f9dd462008-09-23 13:14:11 -04001816 if (ret) {
Josef Bacik96303082009-07-13 21:29:25 -04001817 printk(KERN_CRIT "btrfs: unable to add free space :%d\n", ret);
Stoyan Gaydarovc2934982009-04-02 17:05:11 -04001818 BUG_ON(ret == -EEXIST);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001819 }
1820
Josef Bacik0f9dd462008-09-23 13:14:11 -04001821 return ret;
1822}
1823
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001824int btrfs_remove_free_space(struct btrfs_block_group_cache *block_group,
1825 u64 offset, u64 bytes)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001826{
Li Zefan34d52cb2011-03-29 13:46:06 +08001827 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001828 struct btrfs_free_space *info;
Josef Bacik96303082009-07-13 21:29:25 -04001829 struct btrfs_free_space *next_info = NULL;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001830 int ret = 0;
1831
Li Zefan34d52cb2011-03-29 13:46:06 +08001832 spin_lock(&ctl->tree_lock);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001833
Josef Bacik96303082009-07-13 21:29:25 -04001834again:
Li Zefan34d52cb2011-03-29 13:46:06 +08001835 info = tree_search_offset(ctl, offset, 0, 0);
Josef Bacik96303082009-07-13 21:29:25 -04001836 if (!info) {
Josef Bacik6606bb92009-07-31 11:03:58 -04001837 /*
1838 * oops didn't find an extent that matched the space we wanted
1839 * to remove, look for a bitmap instead
1840 */
Li Zefan34d52cb2011-03-29 13:46:06 +08001841 info = tree_search_offset(ctl, offset_to_bitmap(ctl, offset),
Josef Bacik6606bb92009-07-31 11:03:58 -04001842 1, 0);
1843 if (!info) {
1844 WARN_ON(1);
1845 goto out_lock;
1846 }
Josef Bacik96303082009-07-13 21:29:25 -04001847 }
1848
1849 if (info->bytes < bytes && rb_next(&info->offset_index)) {
1850 u64 end;
1851 next_info = rb_entry(rb_next(&info->offset_index),
1852 struct btrfs_free_space,
1853 offset_index);
1854
1855 if (next_info->bitmap)
Li Zefan34d52cb2011-03-29 13:46:06 +08001856 end = next_info->offset +
1857 BITS_PER_BITMAP * ctl->unit - 1;
Josef Bacik96303082009-07-13 21:29:25 -04001858 else
1859 end = next_info->offset + next_info->bytes;
1860
1861 if (next_info->bytes < bytes ||
1862 next_info->offset > offset || offset > end) {
1863 printk(KERN_CRIT "Found free space at %llu, size %llu,"
1864 " trying to use %llu\n",
1865 (unsigned long long)info->offset,
1866 (unsigned long long)info->bytes,
1867 (unsigned long long)bytes);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001868 WARN_ON(1);
1869 ret = -EINVAL;
Josef Bacik96303082009-07-13 21:29:25 -04001870 goto out_lock;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001871 }
Josef Bacik96303082009-07-13 21:29:25 -04001872
1873 info = next_info;
1874 }
1875
1876 if (info->bytes == bytes) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001877 unlink_free_space(ctl, info);
Josef Bacik96303082009-07-13 21:29:25 -04001878 if (info->bitmap) {
1879 kfree(info->bitmap);
Li Zefan34d52cb2011-03-29 13:46:06 +08001880 ctl->total_bitmaps--;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001881 }
Josef Bacikdc89e982011-01-28 17:05:48 -05001882 kmem_cache_free(btrfs_free_space_cachep, info);
Chris Mason1eae31e2011-10-14 06:31:20 -04001883 ret = 0;
Josef Bacik96303082009-07-13 21:29:25 -04001884 goto out_lock;
1885 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04001886
Josef Bacik96303082009-07-13 21:29:25 -04001887 if (!info->bitmap && info->offset == offset) {
Li Zefan34d52cb2011-03-29 13:46:06 +08001888 unlink_free_space(ctl, info);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001889 info->offset += bytes;
1890 info->bytes -= bytes;
Chris Mason1eae31e2011-10-14 06:31:20 -04001891 ret = link_free_space(ctl, info);
1892 WARN_ON(ret);
Josef Bacik96303082009-07-13 21:29:25 -04001893 goto out_lock;
1894 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04001895
Josef Bacik96303082009-07-13 21:29:25 -04001896 if (!info->bitmap && info->offset <= offset &&
1897 info->offset + info->bytes >= offset + bytes) {
Chris Mason9b49c9b2008-09-24 11:23:25 -04001898 u64 old_start = info->offset;
1899 /*
1900 * we're freeing space in the middle of the info,
1901 * this can happen during tree log replay
1902 *
1903 * first unlink the old info and then
1904 * insert it again after the hole we're creating
1905 */
Li Zefan34d52cb2011-03-29 13:46:06 +08001906 unlink_free_space(ctl, info);
Chris Mason9b49c9b2008-09-24 11:23:25 -04001907 if (offset + bytes < info->offset + info->bytes) {
1908 u64 old_end = info->offset + info->bytes;
1909
1910 info->offset = offset + bytes;
1911 info->bytes = old_end - info->offset;
Li Zefan34d52cb2011-03-29 13:46:06 +08001912 ret = link_free_space(ctl, info);
Josef Bacik96303082009-07-13 21:29:25 -04001913 WARN_ON(ret);
1914 if (ret)
1915 goto out_lock;
Chris Mason9b49c9b2008-09-24 11:23:25 -04001916 } else {
1917 /* the hole we're creating ends at the end
1918 * of the info struct, just free the info
1919 */
Josef Bacikdc89e982011-01-28 17:05:48 -05001920 kmem_cache_free(btrfs_free_space_cachep, info);
Chris Mason9b49c9b2008-09-24 11:23:25 -04001921 }
Li Zefan34d52cb2011-03-29 13:46:06 +08001922 spin_unlock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04001923
1924 /* step two, insert a new info struct to cover
1925 * anything before the hole
Chris Mason9b49c9b2008-09-24 11:23:25 -04001926 */
Josef Bacik6226cb0a2009-04-03 10:14:18 -04001927 ret = btrfs_add_free_space(block_group, old_start,
1928 offset - old_start);
Josef Bacik96303082009-07-13 21:29:25 -04001929 WARN_ON(ret);
1930 goto out;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001931 }
Josef Bacik96303082009-07-13 21:29:25 -04001932
Li Zefan34d52cb2011-03-29 13:46:06 +08001933 ret = remove_from_bitmap(ctl, info, &offset, &bytes);
Josef Bacik96303082009-07-13 21:29:25 -04001934 if (ret == -EAGAIN)
1935 goto again;
1936 BUG_ON(ret);
1937out_lock:
Li Zefan34d52cb2011-03-29 13:46:06 +08001938 spin_unlock(&ctl->tree_lock);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001939out:
Josef Bacik25179202008-10-29 14:49:05 -04001940 return ret;
1941}
1942
Josef Bacik0f9dd462008-09-23 13:14:11 -04001943void btrfs_dump_free_space(struct btrfs_block_group_cache *block_group,
1944 u64 bytes)
1945{
Li Zefan34d52cb2011-03-29 13:46:06 +08001946 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001947 struct btrfs_free_space *info;
1948 struct rb_node *n;
1949 int count = 0;
1950
Li Zefan34d52cb2011-03-29 13:46:06 +08001951 for (n = rb_first(&ctl->free_space_offset); n; n = rb_next(n)) {
Josef Bacik0f9dd462008-09-23 13:14:11 -04001952 info = rb_entry(n, struct btrfs_free_space, offset_index);
1953 if (info->bytes >= bytes)
1954 count++;
Josef Bacik96303082009-07-13 21:29:25 -04001955 printk(KERN_CRIT "entry offset %llu, bytes %llu, bitmap %s\n",
Joel Becker21380932009-04-21 12:38:29 -07001956 (unsigned long long)info->offset,
Josef Bacik96303082009-07-13 21:29:25 -04001957 (unsigned long long)info->bytes,
1958 (info->bitmap) ? "yes" : "no");
Josef Bacik0f9dd462008-09-23 13:14:11 -04001959 }
Josef Bacik96303082009-07-13 21:29:25 -04001960 printk(KERN_INFO "block group has cluster?: %s\n",
1961 list_empty(&block_group->cluster_list) ? "no" : "yes");
Josef Bacik0f9dd462008-09-23 13:14:11 -04001962 printk(KERN_INFO "%d blocks of free space at or bigger than bytes is"
1963 "\n", count);
1964}
1965
Li Zefan34d52cb2011-03-29 13:46:06 +08001966void btrfs_init_free_space_ctl(struct btrfs_block_group_cache *block_group)
Josef Bacik0f9dd462008-09-23 13:14:11 -04001967{
Li Zefan34d52cb2011-03-29 13:46:06 +08001968 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001969
Li Zefan34d52cb2011-03-29 13:46:06 +08001970 spin_lock_init(&ctl->tree_lock);
1971 ctl->unit = block_group->sectorsize;
1972 ctl->start = block_group->key.objectid;
1973 ctl->private = block_group;
1974 ctl->op = &free_space_op;
Josef Bacik0f9dd462008-09-23 13:14:11 -04001975
Li Zefan34d52cb2011-03-29 13:46:06 +08001976 /*
1977 * we only want to have 32k of ram per block group for keeping
1978 * track of free space, and if we pass 1/2 of that we want to
1979 * start converting things over to using bitmaps
1980 */
1981 ctl->extents_thresh = ((1024 * 32) / 2) /
1982 sizeof(struct btrfs_free_space);
Josef Bacik0f9dd462008-09-23 13:14:11 -04001983}
1984
Chris Masonfa9c0d792009-04-03 09:47:43 -04001985/*
1986 * for a given cluster, put all of its extents back into the free
1987 * space cache. If the block group passed doesn't match the block group
1988 * pointed to by the cluster, someone else raced in and freed the
1989 * cluster already. In that case, we just return without changing anything
1990 */
1991static int
1992__btrfs_return_cluster_to_free_space(
1993 struct btrfs_block_group_cache *block_group,
1994 struct btrfs_free_cluster *cluster)
1995{
Li Zefan34d52cb2011-03-29 13:46:06 +08001996 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04001997 struct btrfs_free_space *entry;
1998 struct rb_node *node;
1999
2000 spin_lock(&cluster->lock);
2001 if (cluster->block_group != block_group)
2002 goto out;
2003
Josef Bacik96303082009-07-13 21:29:25 -04002004 cluster->block_group = NULL;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002005 cluster->window_start = 0;
Josef Bacik96303082009-07-13 21:29:25 -04002006 list_del_init(&cluster->block_group_list);
Josef Bacik96303082009-07-13 21:29:25 -04002007
Chris Masonfa9c0d792009-04-03 09:47:43 -04002008 node = rb_first(&cluster->root);
Josef Bacik96303082009-07-13 21:29:25 -04002009 while (node) {
Josef Bacik4e69b592011-03-21 10:11:24 -04002010 bool bitmap;
2011
Chris Masonfa9c0d792009-04-03 09:47:43 -04002012 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2013 node = rb_next(&entry->offset_index);
2014 rb_erase(&entry->offset_index, &cluster->root);
Josef Bacik4e69b592011-03-21 10:11:24 -04002015
2016 bitmap = (entry->bitmap != NULL);
2017 if (!bitmap)
Li Zefan34d52cb2011-03-29 13:46:06 +08002018 try_merge_free_space(ctl, entry, false);
2019 tree_insert_offset(&ctl->free_space_offset,
Josef Bacik4e69b592011-03-21 10:11:24 -04002020 entry->offset, &entry->offset_index, bitmap);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002021 }
Eric Paris6bef4d32010-02-23 19:43:04 +00002022 cluster->root = RB_ROOT;
Josef Bacik96303082009-07-13 21:29:25 -04002023
Chris Masonfa9c0d792009-04-03 09:47:43 -04002024out:
2025 spin_unlock(&cluster->lock);
Josef Bacik96303082009-07-13 21:29:25 -04002026 btrfs_put_block_group(block_group);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002027 return 0;
2028}
2029
Chris Mason09655372011-05-21 09:27:38 -04002030void __btrfs_remove_free_space_cache_locked(struct btrfs_free_space_ctl *ctl)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002031{
2032 struct btrfs_free_space *info;
2033 struct rb_node *node;
Li Zefan581bb052011-04-20 10:06:11 +08002034
Li Zefan581bb052011-04-20 10:06:11 +08002035 while ((node = rb_last(&ctl->free_space_offset)) != NULL) {
2036 info = rb_entry(node, struct btrfs_free_space, offset_index);
Josef Bacik9b90f512011-06-24 16:02:51 +00002037 if (!info->bitmap) {
2038 unlink_free_space(ctl, info);
2039 kmem_cache_free(btrfs_free_space_cachep, info);
2040 } else {
2041 free_bitmap(ctl, info);
2042 }
Li Zefan581bb052011-04-20 10:06:11 +08002043 if (need_resched()) {
2044 spin_unlock(&ctl->tree_lock);
2045 cond_resched();
2046 spin_lock(&ctl->tree_lock);
2047 }
2048 }
Chris Mason09655372011-05-21 09:27:38 -04002049}
2050
2051void __btrfs_remove_free_space_cache(struct btrfs_free_space_ctl *ctl)
2052{
2053 spin_lock(&ctl->tree_lock);
2054 __btrfs_remove_free_space_cache_locked(ctl);
Li Zefan581bb052011-04-20 10:06:11 +08002055 spin_unlock(&ctl->tree_lock);
2056}
2057
Josef Bacik0f9dd462008-09-23 13:14:11 -04002058void btrfs_remove_free_space_cache(struct btrfs_block_group_cache *block_group)
2059{
Li Zefan34d52cb2011-03-29 13:46:06 +08002060 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002061 struct btrfs_free_cluster *cluster;
Josef Bacik96303082009-07-13 21:29:25 -04002062 struct list_head *head;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002063
Li Zefan34d52cb2011-03-29 13:46:06 +08002064 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04002065 while ((head = block_group->cluster_list.next) !=
2066 &block_group->cluster_list) {
2067 cluster = list_entry(head, struct btrfs_free_cluster,
2068 block_group_list);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002069
2070 WARN_ON(cluster->block_group != block_group);
2071 __btrfs_return_cluster_to_free_space(block_group, cluster);
Josef Bacik96303082009-07-13 21:29:25 -04002072 if (need_resched()) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002073 spin_unlock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04002074 cond_resched();
Li Zefan34d52cb2011-03-29 13:46:06 +08002075 spin_lock(&ctl->tree_lock);
Josef Bacik96303082009-07-13 21:29:25 -04002076 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04002077 }
Chris Mason09655372011-05-21 09:27:38 -04002078 __btrfs_remove_free_space_cache_locked(ctl);
Li Zefan34d52cb2011-03-29 13:46:06 +08002079 spin_unlock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002080
Josef Bacik0f9dd462008-09-23 13:14:11 -04002081}
2082
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002083u64 btrfs_find_space_for_alloc(struct btrfs_block_group_cache *block_group,
2084 u64 offset, u64 bytes, u64 empty_size)
Josef Bacik0f9dd462008-09-23 13:14:11 -04002085{
Li Zefan34d52cb2011-03-29 13:46:06 +08002086 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002087 struct btrfs_free_space *entry = NULL;
Josef Bacik96303082009-07-13 21:29:25 -04002088 u64 bytes_search = bytes + empty_size;
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002089 u64 ret = 0;
Josef Bacik0f9dd462008-09-23 13:14:11 -04002090
Li Zefan34d52cb2011-03-29 13:46:06 +08002091 spin_lock(&ctl->tree_lock);
2092 entry = find_free_space(ctl, &offset, &bytes_search);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002093 if (!entry)
Josef Bacik96303082009-07-13 21:29:25 -04002094 goto out;
2095
2096 ret = offset;
2097 if (entry->bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002098 bitmap_clear_bits(ctl, entry, offset, bytes);
Li Zefanedf6e2d2010-11-09 14:50:07 +08002099 if (!entry->bytes)
Li Zefan34d52cb2011-03-29 13:46:06 +08002100 free_bitmap(ctl, entry);
Josef Bacik96303082009-07-13 21:29:25 -04002101 } else {
Li Zefan34d52cb2011-03-29 13:46:06 +08002102 unlink_free_space(ctl, entry);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002103 entry->offset += bytes;
2104 entry->bytes -= bytes;
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002105 if (!entry->bytes)
Josef Bacikdc89e982011-01-28 17:05:48 -05002106 kmem_cache_free(btrfs_free_space_cachep, entry);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002107 else
Li Zefan34d52cb2011-03-29 13:46:06 +08002108 link_free_space(ctl, entry);
Josef Bacik6226cb0a2009-04-03 10:14:18 -04002109 }
Josef Bacik0f9dd462008-09-23 13:14:11 -04002110
Josef Bacik96303082009-07-13 21:29:25 -04002111out:
Li Zefan34d52cb2011-03-29 13:46:06 +08002112 spin_unlock(&ctl->tree_lock);
Josef Bacik817d52f2009-07-13 21:29:25 -04002113
Josef Bacik0f9dd462008-09-23 13:14:11 -04002114 return ret;
2115}
Chris Masonfa9c0d792009-04-03 09:47:43 -04002116
2117/*
2118 * given a cluster, put all of its extents back into the free space
2119 * cache. If a block group is passed, this function will only free
2120 * a cluster that belongs to the passed block group.
2121 *
2122 * Otherwise, it'll get a reference on the block group pointed to by the
2123 * cluster and remove the cluster from it.
2124 */
2125int btrfs_return_cluster_to_free_space(
2126 struct btrfs_block_group_cache *block_group,
2127 struct btrfs_free_cluster *cluster)
2128{
Li Zefan34d52cb2011-03-29 13:46:06 +08002129 struct btrfs_free_space_ctl *ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002130 int ret;
2131
2132 /* first, get a safe pointer to the block group */
2133 spin_lock(&cluster->lock);
2134 if (!block_group) {
2135 block_group = cluster->block_group;
2136 if (!block_group) {
2137 spin_unlock(&cluster->lock);
2138 return 0;
2139 }
2140 } else if (cluster->block_group != block_group) {
2141 /* someone else has already freed it don't redo their work */
2142 spin_unlock(&cluster->lock);
2143 return 0;
2144 }
2145 atomic_inc(&block_group->count);
2146 spin_unlock(&cluster->lock);
2147
Li Zefan34d52cb2011-03-29 13:46:06 +08002148 ctl = block_group->free_space_ctl;
2149
Chris Masonfa9c0d792009-04-03 09:47:43 -04002150 /* now return any extents the cluster had on it */
Li Zefan34d52cb2011-03-29 13:46:06 +08002151 spin_lock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002152 ret = __btrfs_return_cluster_to_free_space(block_group, cluster);
Li Zefan34d52cb2011-03-29 13:46:06 +08002153 spin_unlock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002154
2155 /* finally drop our ref */
2156 btrfs_put_block_group(block_group);
2157 return ret;
2158}
2159
Josef Bacik96303082009-07-13 21:29:25 -04002160static u64 btrfs_alloc_from_bitmap(struct btrfs_block_group_cache *block_group,
2161 struct btrfs_free_cluster *cluster,
Josef Bacik4e69b592011-03-21 10:11:24 -04002162 struct btrfs_free_space *entry,
Josef Bacik96303082009-07-13 21:29:25 -04002163 u64 bytes, u64 min_start)
2164{
Li Zefan34d52cb2011-03-29 13:46:06 +08002165 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik96303082009-07-13 21:29:25 -04002166 int err;
2167 u64 search_start = cluster->window_start;
2168 u64 search_bytes = bytes;
2169 u64 ret = 0;
2170
Josef Bacik96303082009-07-13 21:29:25 -04002171 search_start = min_start;
2172 search_bytes = bytes;
2173
Li Zefan34d52cb2011-03-29 13:46:06 +08002174 err = search_bitmap(ctl, entry, &search_start, &search_bytes);
Josef Bacik96303082009-07-13 21:29:25 -04002175 if (err)
Josef Bacik4e69b592011-03-21 10:11:24 -04002176 return 0;
Josef Bacik96303082009-07-13 21:29:25 -04002177
2178 ret = search_start;
Miao Xiebb3ac5a2011-08-05 09:32:35 +00002179 __bitmap_clear_bits(ctl, entry, ret, bytes);
Josef Bacik96303082009-07-13 21:29:25 -04002180
2181 return ret;
2182}
2183
Chris Masonfa9c0d792009-04-03 09:47:43 -04002184/*
2185 * given a cluster, try to allocate 'bytes' from it, returns 0
2186 * if it couldn't find anything suitably large, or a logical disk offset
2187 * if things worked out
2188 */
2189u64 btrfs_alloc_from_cluster(struct btrfs_block_group_cache *block_group,
2190 struct btrfs_free_cluster *cluster, u64 bytes,
2191 u64 min_start)
2192{
Li Zefan34d52cb2011-03-29 13:46:06 +08002193 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002194 struct btrfs_free_space *entry = NULL;
2195 struct rb_node *node;
2196 u64 ret = 0;
2197
2198 spin_lock(&cluster->lock);
2199 if (bytes > cluster->max_size)
2200 goto out;
2201
2202 if (cluster->block_group != block_group)
2203 goto out;
2204
2205 node = rb_first(&cluster->root);
2206 if (!node)
2207 goto out;
2208
2209 entry = rb_entry(node, struct btrfs_free_space, offset_index);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002210 while(1) {
Josef Bacik4e69b592011-03-21 10:11:24 -04002211 if (entry->bytes < bytes ||
2212 (!entry->bitmap && entry->offset < min_start)) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04002213 node = rb_next(&entry->offset_index);
2214 if (!node)
2215 break;
2216 entry = rb_entry(node, struct btrfs_free_space,
2217 offset_index);
2218 continue;
2219 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04002220
Josef Bacik4e69b592011-03-21 10:11:24 -04002221 if (entry->bitmap) {
2222 ret = btrfs_alloc_from_bitmap(block_group,
2223 cluster, entry, bytes,
2224 min_start);
2225 if (ret == 0) {
Josef Bacik4e69b592011-03-21 10:11:24 -04002226 node = rb_next(&entry->offset_index);
2227 if (!node)
2228 break;
2229 entry = rb_entry(node, struct btrfs_free_space,
2230 offset_index);
2231 continue;
2232 }
2233 } else {
Josef Bacik4e69b592011-03-21 10:11:24 -04002234 ret = entry->offset;
2235
2236 entry->offset += bytes;
2237 entry->bytes -= bytes;
2238 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04002239
Li Zefan5e71b5d2010-11-09 14:55:34 +08002240 if (entry->bytes == 0)
Chris Masonfa9c0d792009-04-03 09:47:43 -04002241 rb_erase(&entry->offset_index, &cluster->root);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002242 break;
2243 }
2244out:
2245 spin_unlock(&cluster->lock);
Josef Bacik96303082009-07-13 21:29:25 -04002246
Li Zefan5e71b5d2010-11-09 14:55:34 +08002247 if (!ret)
2248 return 0;
2249
Li Zefan34d52cb2011-03-29 13:46:06 +08002250 spin_lock(&ctl->tree_lock);
Li Zefan5e71b5d2010-11-09 14:55:34 +08002251
Li Zefan34d52cb2011-03-29 13:46:06 +08002252 ctl->free_space -= bytes;
Li Zefan5e71b5d2010-11-09 14:55:34 +08002253 if (entry->bytes == 0) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002254 ctl->free_extents--;
Josef Bacik4e69b592011-03-21 10:11:24 -04002255 if (entry->bitmap) {
2256 kfree(entry->bitmap);
Li Zefan34d52cb2011-03-29 13:46:06 +08002257 ctl->total_bitmaps--;
2258 ctl->op->recalc_thresholds(ctl);
Josef Bacik4e69b592011-03-21 10:11:24 -04002259 }
Josef Bacikdc89e982011-01-28 17:05:48 -05002260 kmem_cache_free(btrfs_free_space_cachep, entry);
Li Zefan5e71b5d2010-11-09 14:55:34 +08002261 }
2262
Li Zefan34d52cb2011-03-29 13:46:06 +08002263 spin_unlock(&ctl->tree_lock);
Li Zefan5e71b5d2010-11-09 14:55:34 +08002264
Chris Masonfa9c0d792009-04-03 09:47:43 -04002265 return ret;
2266}
2267
Josef Bacik96303082009-07-13 21:29:25 -04002268static int btrfs_bitmap_cluster(struct btrfs_block_group_cache *block_group,
2269 struct btrfs_free_space *entry,
2270 struct btrfs_free_cluster *cluster,
2271 u64 offset, u64 bytes, u64 min_bytes)
2272{
Li Zefan34d52cb2011-03-29 13:46:06 +08002273 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik96303082009-07-13 21:29:25 -04002274 unsigned long next_zero;
2275 unsigned long i;
2276 unsigned long search_bits;
2277 unsigned long total_bits;
2278 unsigned long found_bits;
2279 unsigned long start = 0;
2280 unsigned long total_found = 0;
Josef Bacik4e69b592011-03-21 10:11:24 -04002281 int ret;
Josef Bacik96303082009-07-13 21:29:25 -04002282 bool found = false;
2283
2284 i = offset_to_bit(entry->offset, block_group->sectorsize,
2285 max_t(u64, offset, entry->offset));
Josef Bacikd0a365e2011-03-18 15:27:43 -04002286 search_bits = bytes_to_bits(bytes, block_group->sectorsize);
2287 total_bits = bytes_to_bits(min_bytes, block_group->sectorsize);
Josef Bacik96303082009-07-13 21:29:25 -04002288
2289again:
2290 found_bits = 0;
2291 for (i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, i);
2292 i < BITS_PER_BITMAP;
2293 i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, i + 1)) {
2294 next_zero = find_next_zero_bit(entry->bitmap,
2295 BITS_PER_BITMAP, i);
2296 if (next_zero - i >= search_bits) {
2297 found_bits = next_zero - i;
2298 break;
2299 }
2300 i = next_zero;
2301 }
2302
2303 if (!found_bits)
Josef Bacik4e69b592011-03-21 10:11:24 -04002304 return -ENOSPC;
Josef Bacik96303082009-07-13 21:29:25 -04002305
2306 if (!found) {
2307 start = i;
2308 found = true;
2309 }
2310
2311 total_found += found_bits;
2312
2313 if (cluster->max_size < found_bits * block_group->sectorsize)
2314 cluster->max_size = found_bits * block_group->sectorsize;
2315
2316 if (total_found < total_bits) {
2317 i = find_next_bit(entry->bitmap, BITS_PER_BITMAP, next_zero);
2318 if (i - start > total_bits * 2) {
2319 total_found = 0;
2320 cluster->max_size = 0;
2321 found = false;
2322 }
2323 goto again;
2324 }
2325
2326 cluster->window_start = start * block_group->sectorsize +
2327 entry->offset;
Li Zefan34d52cb2011-03-29 13:46:06 +08002328 rb_erase(&entry->offset_index, &ctl->free_space_offset);
Josef Bacik4e69b592011-03-21 10:11:24 -04002329 ret = tree_insert_offset(&cluster->root, entry->offset,
2330 &entry->offset_index, 1);
2331 BUG_ON(ret);
Josef Bacik96303082009-07-13 21:29:25 -04002332
2333 return 0;
2334}
2335
Chris Masonfa9c0d792009-04-03 09:47:43 -04002336/*
Josef Bacik4e69b592011-03-21 10:11:24 -04002337 * This searches the block group for just extents to fill the cluster with.
2338 */
Josef Bacik3de85bb2011-05-25 13:07:37 -04002339static noinline int
2340setup_cluster_no_bitmap(struct btrfs_block_group_cache *block_group,
2341 struct btrfs_free_cluster *cluster,
2342 struct list_head *bitmaps, u64 offset, u64 bytes,
2343 u64 min_bytes)
Josef Bacik4e69b592011-03-21 10:11:24 -04002344{
Li Zefan34d52cb2011-03-29 13:46:06 +08002345 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik4e69b592011-03-21 10:11:24 -04002346 struct btrfs_free_space *first = NULL;
2347 struct btrfs_free_space *entry = NULL;
2348 struct btrfs_free_space *prev = NULL;
2349 struct btrfs_free_space *last;
2350 struct rb_node *node;
2351 u64 window_start;
2352 u64 window_free;
2353 u64 max_extent;
2354 u64 max_gap = 128 * 1024;
2355
Li Zefan34d52cb2011-03-29 13:46:06 +08002356 entry = tree_search_offset(ctl, offset, 0, 1);
Josef Bacik4e69b592011-03-21 10:11:24 -04002357 if (!entry)
2358 return -ENOSPC;
2359
2360 /*
2361 * We don't want bitmaps, so just move along until we find a normal
2362 * extent entry.
2363 */
2364 while (entry->bitmap) {
Josef Bacik86d4a772011-05-25 13:03:16 -04002365 if (list_empty(&entry->list))
2366 list_add_tail(&entry->list, bitmaps);
Josef Bacik4e69b592011-03-21 10:11:24 -04002367 node = rb_next(&entry->offset_index);
2368 if (!node)
2369 return -ENOSPC;
2370 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2371 }
2372
2373 window_start = entry->offset;
2374 window_free = entry->bytes;
2375 max_extent = entry->bytes;
2376 first = entry;
2377 last = entry;
2378 prev = entry;
2379
2380 while (window_free <= min_bytes) {
2381 node = rb_next(&entry->offset_index);
2382 if (!node)
2383 return -ENOSPC;
2384 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2385
Josef Bacik86d4a772011-05-25 13:03:16 -04002386 if (entry->bitmap) {
2387 if (list_empty(&entry->list))
2388 list_add_tail(&entry->list, bitmaps);
Josef Bacik4e69b592011-03-21 10:11:24 -04002389 continue;
Josef Bacik86d4a772011-05-25 13:03:16 -04002390 }
2391
Josef Bacik4e69b592011-03-21 10:11:24 -04002392 /*
2393 * we haven't filled the empty size and the window is
2394 * very large. reset and try again
2395 */
2396 if (entry->offset - (prev->offset + prev->bytes) > max_gap ||
2397 entry->offset - window_start > (min_bytes * 2)) {
2398 first = entry;
2399 window_start = entry->offset;
2400 window_free = entry->bytes;
2401 last = entry;
2402 max_extent = entry->bytes;
2403 } else {
2404 last = entry;
2405 window_free += entry->bytes;
2406 if (entry->bytes > max_extent)
2407 max_extent = entry->bytes;
2408 }
2409 prev = entry;
2410 }
2411
2412 cluster->window_start = first->offset;
2413
2414 node = &first->offset_index;
2415
2416 /*
2417 * now we've found our entries, pull them out of the free space
2418 * cache and put them into the cluster rbtree
2419 */
2420 do {
2421 int ret;
2422
2423 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2424 node = rb_next(&entry->offset_index);
2425 if (entry->bitmap)
2426 continue;
2427
Li Zefan34d52cb2011-03-29 13:46:06 +08002428 rb_erase(&entry->offset_index, &ctl->free_space_offset);
Josef Bacik4e69b592011-03-21 10:11:24 -04002429 ret = tree_insert_offset(&cluster->root, entry->offset,
2430 &entry->offset_index, 0);
2431 BUG_ON(ret);
2432 } while (node && entry != last);
2433
2434 cluster->max_size = max_extent;
2435
2436 return 0;
2437}
2438
2439/*
2440 * This specifically looks for bitmaps that may work in the cluster, we assume
2441 * that we have already failed to find extents that will work.
2442 */
Josef Bacik3de85bb2011-05-25 13:07:37 -04002443static noinline int
2444setup_cluster_bitmap(struct btrfs_block_group_cache *block_group,
2445 struct btrfs_free_cluster *cluster,
2446 struct list_head *bitmaps, u64 offset, u64 bytes,
2447 u64 min_bytes)
Josef Bacik4e69b592011-03-21 10:11:24 -04002448{
Li Zefan34d52cb2011-03-29 13:46:06 +08002449 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik4e69b592011-03-21 10:11:24 -04002450 struct btrfs_free_space *entry;
2451 struct rb_node *node;
2452 int ret = -ENOSPC;
2453
Li Zefan34d52cb2011-03-29 13:46:06 +08002454 if (ctl->total_bitmaps == 0)
Josef Bacik4e69b592011-03-21 10:11:24 -04002455 return -ENOSPC;
2456
Josef Bacik86d4a772011-05-25 13:03:16 -04002457 /*
2458 * First check our cached list of bitmaps and see if there is an entry
2459 * here that will work.
2460 */
2461 list_for_each_entry(entry, bitmaps, list) {
2462 if (entry->bytes < min_bytes)
2463 continue;
2464 ret = btrfs_bitmap_cluster(block_group, entry, cluster, offset,
2465 bytes, min_bytes);
2466 if (!ret)
2467 return 0;
2468 }
2469
2470 /*
2471 * If we do have entries on our list and we are here then we didn't find
2472 * anything, so go ahead and get the next entry after the last entry in
2473 * this list and start the search from there.
2474 */
2475 if (!list_empty(bitmaps)) {
2476 entry = list_entry(bitmaps->prev, struct btrfs_free_space,
2477 list);
2478 node = rb_next(&entry->offset_index);
2479 if (!node)
2480 return -ENOSPC;
2481 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2482 goto search;
2483 }
2484
Li Zefan34d52cb2011-03-29 13:46:06 +08002485 entry = tree_search_offset(ctl, offset_to_bitmap(ctl, offset), 0, 1);
Josef Bacik4e69b592011-03-21 10:11:24 -04002486 if (!entry)
2487 return -ENOSPC;
2488
Josef Bacik86d4a772011-05-25 13:03:16 -04002489search:
Josef Bacik4e69b592011-03-21 10:11:24 -04002490 node = &entry->offset_index;
2491 do {
2492 entry = rb_entry(node, struct btrfs_free_space, offset_index);
2493 node = rb_next(&entry->offset_index);
2494 if (!entry->bitmap)
2495 continue;
2496 if (entry->bytes < min_bytes)
2497 continue;
2498 ret = btrfs_bitmap_cluster(block_group, entry, cluster, offset,
2499 bytes, min_bytes);
2500 } while (ret && node);
2501
2502 return ret;
2503}
2504
2505/*
Chris Masonfa9c0d792009-04-03 09:47:43 -04002506 * here we try to find a cluster of blocks in a block group. The goal
2507 * is to find at least bytes free and up to empty_size + bytes free.
2508 * We might not find them all in one contiguous area.
2509 *
2510 * returns zero and sets up cluster if things worked out, otherwise
2511 * it returns -enospc
2512 */
2513int btrfs_find_space_cluster(struct btrfs_trans_handle *trans,
Chris Mason451d7582009-06-09 20:28:34 -04002514 struct btrfs_root *root,
Chris Masonfa9c0d792009-04-03 09:47:43 -04002515 struct btrfs_block_group_cache *block_group,
2516 struct btrfs_free_cluster *cluster,
2517 u64 offset, u64 bytes, u64 empty_size)
2518{
Li Zefan34d52cb2011-03-29 13:46:06 +08002519 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Josef Bacik86d4a772011-05-25 13:03:16 -04002520 struct list_head bitmaps;
2521 struct btrfs_free_space *entry, *tmp;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002522 u64 min_bytes;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002523 int ret;
2524
2525 /* for metadata, allow allocates with more holes */
Chris Mason451d7582009-06-09 20:28:34 -04002526 if (btrfs_test_opt(root, SSD_SPREAD)) {
2527 min_bytes = bytes + empty_size;
2528 } else if (block_group->flags & BTRFS_BLOCK_GROUP_METADATA) {
Chris Masonfa9c0d792009-04-03 09:47:43 -04002529 /*
2530 * we want to do larger allocations when we are
2531 * flushing out the delayed refs, it helps prevent
2532 * making more work as we go along.
2533 */
2534 if (trans->transaction->delayed_refs.flushing)
2535 min_bytes = max(bytes, (bytes + empty_size) >> 1);
2536 else
2537 min_bytes = max(bytes, (bytes + empty_size) >> 4);
2538 } else
2539 min_bytes = max(bytes, (bytes + empty_size) >> 2);
2540
Li Zefan34d52cb2011-03-29 13:46:06 +08002541 spin_lock(&ctl->tree_lock);
Josef Bacik7d0d2e82011-03-18 15:13:42 -04002542
2543 /*
2544 * If we know we don't have enough space to make a cluster don't even
2545 * bother doing all the work to try and find one.
2546 */
Li Zefan34d52cb2011-03-29 13:46:06 +08002547 if (ctl->free_space < min_bytes) {
2548 spin_unlock(&ctl->tree_lock);
Josef Bacik7d0d2e82011-03-18 15:13:42 -04002549 return -ENOSPC;
2550 }
2551
Chris Masonfa9c0d792009-04-03 09:47:43 -04002552 spin_lock(&cluster->lock);
2553
2554 /* someone already found a cluster, hooray */
2555 if (cluster->block_group) {
2556 ret = 0;
2557 goto out;
2558 }
Josef Bacik4e69b592011-03-21 10:11:24 -04002559
Josef Bacik86d4a772011-05-25 13:03:16 -04002560 INIT_LIST_HEAD(&bitmaps);
2561 ret = setup_cluster_no_bitmap(block_group, cluster, &bitmaps, offset,
2562 bytes, min_bytes);
Josef Bacik4e69b592011-03-21 10:11:24 -04002563 if (ret)
Josef Bacik86d4a772011-05-25 13:03:16 -04002564 ret = setup_cluster_bitmap(block_group, cluster, &bitmaps,
2565 offset, bytes, min_bytes);
2566
2567 /* Clear our temporary list */
2568 list_for_each_entry_safe(entry, tmp, &bitmaps, list)
2569 list_del_init(&entry->list);
Josef Bacik4e69b592011-03-21 10:11:24 -04002570
2571 if (!ret) {
2572 atomic_inc(&block_group->count);
2573 list_add_tail(&cluster->block_group_list,
2574 &block_group->cluster_list);
2575 cluster->block_group = block_group;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002576 }
Chris Masonfa9c0d792009-04-03 09:47:43 -04002577out:
2578 spin_unlock(&cluster->lock);
Li Zefan34d52cb2011-03-29 13:46:06 +08002579 spin_unlock(&ctl->tree_lock);
Chris Masonfa9c0d792009-04-03 09:47:43 -04002580
2581 return ret;
2582}
2583
2584/*
2585 * simple code to zero out a cluster
2586 */
2587void btrfs_init_free_cluster(struct btrfs_free_cluster *cluster)
2588{
2589 spin_lock_init(&cluster->lock);
2590 spin_lock_init(&cluster->refill_lock);
Eric Paris6bef4d32010-02-23 19:43:04 +00002591 cluster->root = RB_ROOT;
Chris Masonfa9c0d792009-04-03 09:47:43 -04002592 cluster->max_size = 0;
2593 INIT_LIST_HEAD(&cluster->block_group_list);
2594 cluster->block_group = NULL;
2595}
2596
Li Dongyangf7039b12011-03-24 10:24:28 +00002597int btrfs_trim_block_group(struct btrfs_block_group_cache *block_group,
2598 u64 *trimmed, u64 start, u64 end, u64 minlen)
2599{
Li Zefan34d52cb2011-03-29 13:46:06 +08002600 struct btrfs_free_space_ctl *ctl = block_group->free_space_ctl;
Li Dongyangf7039b12011-03-24 10:24:28 +00002601 struct btrfs_free_space *entry = NULL;
2602 struct btrfs_fs_info *fs_info = block_group->fs_info;
2603 u64 bytes = 0;
2604 u64 actually_trimmed;
2605 int ret = 0;
2606
2607 *trimmed = 0;
2608
2609 while (start < end) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002610 spin_lock(&ctl->tree_lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00002611
Li Zefan34d52cb2011-03-29 13:46:06 +08002612 if (ctl->free_space < minlen) {
2613 spin_unlock(&ctl->tree_lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00002614 break;
2615 }
2616
Li Zefan34d52cb2011-03-29 13:46:06 +08002617 entry = tree_search_offset(ctl, start, 0, 1);
Li Dongyangf7039b12011-03-24 10:24:28 +00002618 if (!entry)
Li Zefan34d52cb2011-03-29 13:46:06 +08002619 entry = tree_search_offset(ctl,
2620 offset_to_bitmap(ctl, start),
Li Dongyangf7039b12011-03-24 10:24:28 +00002621 1, 1);
2622
2623 if (!entry || entry->offset >= end) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002624 spin_unlock(&ctl->tree_lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00002625 break;
2626 }
2627
2628 if (entry->bitmap) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002629 ret = search_bitmap(ctl, entry, &start, &bytes);
Li Dongyangf7039b12011-03-24 10:24:28 +00002630 if (!ret) {
2631 if (start >= end) {
Li Zefan34d52cb2011-03-29 13:46:06 +08002632 spin_unlock(&ctl->tree_lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00002633 break;
2634 }
2635 bytes = min(bytes, end - start);
Li Zefan34d52cb2011-03-29 13:46:06 +08002636 bitmap_clear_bits(ctl, entry, start, bytes);
Li Dongyangf7039b12011-03-24 10:24:28 +00002637 if (entry->bytes == 0)
Li Zefan34d52cb2011-03-29 13:46:06 +08002638 free_bitmap(ctl, entry);
Li Dongyangf7039b12011-03-24 10:24:28 +00002639 } else {
2640 start = entry->offset + BITS_PER_BITMAP *
2641 block_group->sectorsize;
Li Zefan34d52cb2011-03-29 13:46:06 +08002642 spin_unlock(&ctl->tree_lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00002643 ret = 0;
2644 continue;
2645 }
2646 } else {
2647 start = entry->offset;
2648 bytes = min(entry->bytes, end - start);
Li Zefan34d52cb2011-03-29 13:46:06 +08002649 unlink_free_space(ctl, entry);
Li Zefanf789b682011-04-25 19:43:52 -04002650 kmem_cache_free(btrfs_free_space_cachep, entry);
Li Dongyangf7039b12011-03-24 10:24:28 +00002651 }
2652
Li Zefan34d52cb2011-03-29 13:46:06 +08002653 spin_unlock(&ctl->tree_lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00002654
2655 if (bytes >= minlen) {
Josef Bacikfb25e912011-07-26 17:00:46 -04002656 struct btrfs_space_info *space_info;
2657 int update = 0;
2658
2659 space_info = block_group->space_info;
2660 spin_lock(&space_info->lock);
2661 spin_lock(&block_group->lock);
2662 if (!block_group->ro) {
2663 block_group->reserved += bytes;
2664 space_info->bytes_reserved += bytes;
2665 update = 1;
2666 }
2667 spin_unlock(&block_group->lock);
2668 spin_unlock(&space_info->lock);
Li Dongyangf7039b12011-03-24 10:24:28 +00002669
2670 ret = btrfs_error_discard_extent(fs_info->extent_root,
2671 start,
2672 bytes,
2673 &actually_trimmed);
2674
Li Zefan34d52cb2011-03-29 13:46:06 +08002675 btrfs_add_free_space(block_group, start, bytes);
Josef Bacikfb25e912011-07-26 17:00:46 -04002676 if (update) {
2677 spin_lock(&space_info->lock);
2678 spin_lock(&block_group->lock);
2679 if (block_group->ro)
2680 space_info->bytes_readonly += bytes;
2681 block_group->reserved -= bytes;
2682 space_info->bytes_reserved -= bytes;
2683 spin_unlock(&space_info->lock);
2684 spin_unlock(&block_group->lock);
2685 }
Li Dongyangf7039b12011-03-24 10:24:28 +00002686
2687 if (ret)
2688 break;
2689 *trimmed += actually_trimmed;
2690 }
2691 start += bytes;
2692 bytes = 0;
2693
2694 if (fatal_signal_pending(current)) {
2695 ret = -ERESTARTSYS;
2696 break;
2697 }
2698
2699 cond_resched();
2700 }
2701
2702 return ret;
2703}
Li Zefan581bb052011-04-20 10:06:11 +08002704
2705/*
2706 * Find the left-most item in the cache tree, and then return the
2707 * smallest inode number in the item.
2708 *
2709 * Note: the returned inode number may not be the smallest one in
2710 * the tree, if the left-most item is a bitmap.
2711 */
2712u64 btrfs_find_ino_for_alloc(struct btrfs_root *fs_root)
2713{
2714 struct btrfs_free_space_ctl *ctl = fs_root->free_ino_ctl;
2715 struct btrfs_free_space *entry = NULL;
2716 u64 ino = 0;
2717
2718 spin_lock(&ctl->tree_lock);
2719
2720 if (RB_EMPTY_ROOT(&ctl->free_space_offset))
2721 goto out;
2722
2723 entry = rb_entry(rb_first(&ctl->free_space_offset),
2724 struct btrfs_free_space, offset_index);
2725
2726 if (!entry->bitmap) {
2727 ino = entry->offset;
2728
2729 unlink_free_space(ctl, entry);
2730 entry->offset++;
2731 entry->bytes--;
2732 if (!entry->bytes)
2733 kmem_cache_free(btrfs_free_space_cachep, entry);
2734 else
2735 link_free_space(ctl, entry);
2736 } else {
2737 u64 offset = 0;
2738 u64 count = 1;
2739 int ret;
2740
2741 ret = search_bitmap(ctl, entry, &offset, &count);
2742 BUG_ON(ret);
2743
2744 ino = offset;
2745 bitmap_clear_bits(ctl, entry, offset, 1);
2746 if (entry->bytes == 0)
2747 free_bitmap(ctl, entry);
2748 }
2749out:
2750 spin_unlock(&ctl->tree_lock);
2751
2752 return ino;
2753}
Li Zefan82d59022011-04-20 10:33:24 +08002754
2755struct inode *lookup_free_ino_inode(struct btrfs_root *root,
2756 struct btrfs_path *path)
2757{
2758 struct inode *inode = NULL;
2759
2760 spin_lock(&root->cache_lock);
2761 if (root->cache_inode)
2762 inode = igrab(root->cache_inode);
2763 spin_unlock(&root->cache_lock);
2764 if (inode)
2765 return inode;
2766
2767 inode = __lookup_free_space_inode(root, path, 0);
2768 if (IS_ERR(inode))
2769 return inode;
2770
2771 spin_lock(&root->cache_lock);
David Sterba7841cb22011-05-31 18:07:27 +02002772 if (!btrfs_fs_closing(root->fs_info))
Li Zefan82d59022011-04-20 10:33:24 +08002773 root->cache_inode = igrab(inode);
2774 spin_unlock(&root->cache_lock);
2775
2776 return inode;
2777}
2778
2779int create_free_ino_inode(struct btrfs_root *root,
2780 struct btrfs_trans_handle *trans,
2781 struct btrfs_path *path)
2782{
2783 return __create_free_space_inode(root, trans, path,
2784 BTRFS_FREE_INO_OBJECTID, 0);
2785}
2786
2787int load_free_ino_cache(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
2788{
2789 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
2790 struct btrfs_path *path;
2791 struct inode *inode;
2792 int ret = 0;
2793 u64 root_gen = btrfs_root_generation(&root->root_item);
2794
Chris Mason4b9465c2011-06-03 09:36:29 -04002795 if (!btrfs_test_opt(root, INODE_MAP_CACHE))
2796 return 0;
2797
Li Zefan82d59022011-04-20 10:33:24 +08002798 /*
2799 * If we're unmounting then just return, since this does a search on the
2800 * normal root and not the commit root and we could deadlock.
2801 */
David Sterba7841cb22011-05-31 18:07:27 +02002802 if (btrfs_fs_closing(fs_info))
Li Zefan82d59022011-04-20 10:33:24 +08002803 return 0;
2804
2805 path = btrfs_alloc_path();
2806 if (!path)
2807 return 0;
2808
2809 inode = lookup_free_ino_inode(root, path);
2810 if (IS_ERR(inode))
2811 goto out;
2812
2813 if (root_gen != BTRFS_I(inode)->generation)
2814 goto out_put;
2815
2816 ret = __load_free_space_cache(root, inode, ctl, path, 0);
2817
2818 if (ret < 0)
2819 printk(KERN_ERR "btrfs: failed to load free ino cache for "
2820 "root %llu\n", root->root_key.objectid);
2821out_put:
2822 iput(inode);
2823out:
2824 btrfs_free_path(path);
2825 return ret;
2826}
2827
2828int btrfs_write_out_ino_cache(struct btrfs_root *root,
2829 struct btrfs_trans_handle *trans,
2830 struct btrfs_path *path)
2831{
2832 struct btrfs_free_space_ctl *ctl = root->free_ino_ctl;
2833 struct inode *inode;
2834 int ret;
2835
Chris Mason4b9465c2011-06-03 09:36:29 -04002836 if (!btrfs_test_opt(root, INODE_MAP_CACHE))
2837 return 0;
2838
Li Zefan82d59022011-04-20 10:33:24 +08002839 inode = lookup_free_ino_inode(root, path);
2840 if (IS_ERR(inode))
2841 return 0;
2842
2843 ret = __btrfs_write_out_cache(root, inode, ctl, NULL, trans, path, 0);
Josef Bacikc09544e2011-08-30 10:19:10 -04002844 if (ret) {
2845 btrfs_delalloc_release_metadata(inode, inode->i_size);
2846#ifdef DEBUG
Li Zefan82d59022011-04-20 10:33:24 +08002847 printk(KERN_ERR "btrfs: failed to write free ino cache "
2848 "for root %llu\n", root->root_key.objectid);
Josef Bacikc09544e2011-08-30 10:19:10 -04002849#endif
2850 }
Li Zefan82d59022011-04-20 10:33:24 +08002851
2852 iput(inode);
2853 return ret;
2854}